zoj3741

简单dp。wa了两个小时,中间改了好多细节。后来还是不对,参考了别人的代码,发现一个致命问题,初始化的时候,不是每种状态都能直接达到的。初始化成-1。

(题目有个小坑,0<=L<=5, 即使吃药了,也不能到6 )

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define pk printf("lalala");
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1.0)    // 自然对数
#define ESP 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;
const int N = 105;
int dp[N][2*N];
int a[N];void print()
{for (int k = 1; k <= 6; ++k) {for (int i = 98; i <= 101; ++i)printf("%d ", dp[k][i]);printf("\n");}}int main()
{int l, n, x, y;while (~scanf("%d%d%d%d", &l, &n, &x, &y)) {clr(dp, -1);for (int i = 1; i <= n; ++i) {scanf("%d", &a[i]);}dp[0][100] = 0;for (int i = 1; i <= n; ++i) {for (int j = 100 - y; j <= 100 + x; ++j) {if (j == 100 - y) {if (dp[i - 1][100 + x] == -1) continue;if (0 >= a[i]) dp[i][j] = dp[i - 1][100 + x] + 1;else dp[i][j] = dp[i - 1][100 + x];} else if (j < 100) {if (dp[i - 1][j - 1] == -1) continue;if (0 >= a[i]) dp[i][j] = dp[i - 1][j - 1] + 1;else dp[i][j] = dp[i - 1][j - 1];} else if (j == 100 + 1) {if (dp[i - 1][j - 1] == -1 && dp[i - 1][100 - 1] == -1) continue;if ((l + 1 <= 5 && l + 1 >= a[i]) || l >= a[i]) {dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][100 - 1]) + 1;} else {dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][100 - 1]);}} else if (j > 100) {if (dp[i - 1][j - 1] == -1) continue;if ((l + 1 <= 5 && l + 1 >= a[i]) || l >= a[i]) dp[i][j] = dp[i - 1][j - 1] + 1;else dp[i][j] = dp[i - 1][j - 1];} else {if (dp[i - 1][j] == -1 && dp[i - 1][100 - 1] == -1) continue;if (l >= a[i]) {dp[i][j] = max(dp[i - 1][j], dp[i - 1][100 - 1]) + 1;} else {dp[i][j] = max(dp[i - 1][j], dp[i - 1][100 - 1]);}}}}int ans = 0;for (int i = 100 - y; i <= 100 + x; ++i) {ans = max(ans, dp[n][i]);}//print();printf("%d\n", ans);}return 0;
}/*
3 6 1 2
1 3 4 5 6 40 6 3 1
1 0 1 0 1 03 6 1 3
3 4 3 3 4 43 6 1 2
3 4 3 4 3 43 6 1 2
4 5 4 5 4 55 6 1 2
6 5 6 4 5 65 6 2 3
6 5 6 5 6 54 6 1 6
5 5 5 5 5 54 6 5 2
5 5 0 0 5 54
6
4
4
2
3
3
1
5
*/

  

zoj 3911

线段树区间更新,点更新,区间查询。好久不写,不是很会写了 (尴尬 - -#)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>        // pair
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define pk printf("lalala");
using namespace std;
#define PI acos(-1.0)
#define EXP exp(1.0)    // 自然对数
#define ESP 1E-6
#define clr(x,c) memset(x,c,sizeof(x))
typedef long long ll;const int MAX_N = 10000005;
int prime[MAX_N];
bool is_prime[MAX_N];int sieve(int n)
{int p = 0;for (int i = 0; i <= n; ++i) is_prime[i] = true;is_prime[0] = is_prime[1] = false;for (int i = 2; i <= n; ++i) {if (is_prime[i]) {prime[p++] = i;for (int j = 2 * i; j <= n; j += i)is_prime[j] = false;}}return p;
}#define lson (o<<1)
#define rson (o<<1|1)
#define mid ((l+r)>>1)const int N = 100005;
int tr[N * 3];
int ans[N * 3];
int v, yl, yr;void pushup(int o)
{ans[o] = ans[lson] + ans[rson];
}void build(int o, int l, int r)
{if (l == r) {scanf("%d", &tr[o]);if (is_prime[ tr[o] ]) ans[o] = 1;else ans[o] = 0;return ;}build(lson, l, mid);build(rson, mid + 1, r);pushup(o);
}void pushdown(int o, int l, int r)
{if (tr[o] == 0) return ;tr[lson] = tr[rson] = tr[o];if (is_prime[ tr[o] ]) {ans[lson] = mid - l + 1;ans[rson] = r - mid;} else {ans[lson] = ans[rson] = 0;}tr[o] = 0;
}void add(int o, int l, int r)
{if (l == r) {tr[o] += v;if (is_prime[ tr[o] ]) ans[o] = 1;else ans[o] = 0;return ;}pushdown(o, l, r);if (mid >= yl) add(lson, l, mid);else add(rson, mid + 1, r);pushup(o);
}void update(int o, int l, int r)
{if (yl <= l && yr >= r) {tr[o] = v;if (is_prime[v]) {ans[o] = r - l + 1;} else {ans[o] = 0;}return ;}pushdown(o, l, r);if (yl <= mid) update(lson, l, mid);if (yr > mid) update(rson, mid + 1, r);pushup(o);
}int query(int o, int l, int r)
{if (yl <= l && yr >= r) return ans[o];pushdown(o, l, r);int res = 0;if (yl <= mid) res += query(lson, l, mid);if (yr > mid) res += query(rson, mid + 1, r);return res;
}int main()
{sieve(10000000);int t;scanf("%d", &t);while (t--) {int n, q;scanf("%d%d", &n, &q);clr(tr, 0);build(1, 1, n);while (q--) {char op[4];scanf("%s", op);if (*op == 'A') {scanf("%d%d", &v, &yl);add(1, 1, n);} else if (*op == 'R') {scanf("%d%d%d", &v, &yl, &yr);update(1, 1, n);} else {scanf("%d%d", &yl, &yr);printf("%d\n", query(1, 1, n));}}}return 0;
}

  

转载于:https://www.cnblogs.com/wenruo/p/5161909.html

群赛 ZOJ3741(dp) ZOJ3911(线段树)相关推荐

  1. CCSU团队训练赛 ( A 数学 B tarjan F dij G dp H 线段树 )

    题目链接 设的私密,可能进不去,每个题有给原题链接.算作是私人题解吧. A - Play the Dice HDU - 4586  There is a dice with n sides, whic ...

  2. 【DP、线段树优化】琪露诺

    跟去年(2017)PJ第四题几乎是一样的?/吐血 DP方程可以很简单的推出来,f[i]=max{f[k]}+a[i] 然而这样做是O(n^2)的 看一下数据,200000的话要不nlogn 要不n 由 ...

  3. AtCoder abc256全题解(区间合并模板、矩阵快速幂优化dp、线段树……)

    文章目录 A B C-枚举 D-区间合并模板 E-图论建模,函数图的性质 题意 思路 代码 F-树状数组 题意 思路 代码 G-矩阵快速幂优化dp H-线段树 思路 实现 传送门 本文CSDN 本文j ...

  4. 中石油训练赛 - 小A盗墓(线段树+异或结论)

    题目链接:点击查看 题目大意:给出n个数,以及m个操作,每个操作分为两种: 1 x y:将第x个数变为y 2 x y:判断闭区间[x,y]中的数能否在进行升序排序后构成一段连续且上升序列 题目分析:因 ...

  5. 2017 ICPC西安区域赛 A - XOR ,线段树合并线性基

    题目链接:A - XOR 题意;给个数组,每次询问一个区间你可以挑任意个数的数字异或和 然后在或上k的最大值 题解:线性基不知道的先看这个,一个线性基可以log的求最大值把对应去区间的线性基求出来然后 ...

  6. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面: Consider an array AA with n elements . Each of its element ...

  7. 【Codeforces】WHU校赛2019 Store(线段树+二分)

    题目链接 C. Store time limit per test 1.0 s memory limit per test 256 MB input standard input output sta ...

  8. 瓜瓜的时空旅行,第三次模拟赛,dfs序+线段树维护最小值

    题目描述 西瓜们生活在编号 1⋯n 的 n个平行时空中,2n−2 台时光机将这些平行时空联系在一起.一台时光机有 3个整数参数 u,v,t 表示从时空 u 可以花费 t 的时间穿梭到时空 v.为了确保 ...

  9. 2018 ACM/ICPC 北京赛区网络赛 D 80 Days 线段树

    http://hihocoder.com/problemset/problem/1831?sid=1390457 描述 80 Days is an interesting game based on ...

最新文章

  1. MATLAB_图形学_形态学课程_找出薛之谦的歌词所有字数
  2. 4.Azure创建点到站点的***隧道(下)
  3. HDU5196--DZY Loves Inversions 树状数组 逆序数
  4. java版本号分段比较_Java实现比较版本号
  5. idea中maven执行install报错_IntelliJ IDEA Maven编译install时报错,无效的发行版:1.8
  6. linux servlet 乱码问题,Servlet一次乱码排查后的总结
  7. 64位系统目录在那里_教你玩转Linux系统目录结构
  8. 运算符重载 - C++快速入门25
  9. 【数据结构笔记04】线性结构:线性表及其实现
  10. Qt 中实现在控件中点击鼠标,就在鼠标点击处加载图片的方法
  11. 通过stream去重_分享几种 Java8 中通过 Stream 对列表进行去重的方法
  12. cloud-api-commons抽取公共类
  13. 读嵌入式linux驱动程序设计从入门到精通1
  14. python网页制作web_python web麻瓜编程
  15. 我的世界服务器租服_我的世界中国版开服教程 网易国服怎么租赁服务器
  16. ArcMap(ArcGIS)批量裁剪图片【超详细】
  17. Xcode Undefined symbols 错误
  18. Python之遍历文件夹图片并重命名
  19. 2021年全球网络保险收入大约9593.9百万美元,预计2028年达到68230百万美元,2022至2028期间,年复合增长率CAGR为35.1%
  20. 写一个简单的校园网多拨思路

热门文章

  1. arcgis flexviewer中由Application向widget传值
  2. [Windows Server 2012] 安装IIS8.5及FTP
  3. ZOJ 1610 Count the Colors
  4. 使用Java处理大文件
  5. ASP.NET2.0快速入门--高级数据方案(3)
  6. JDK1.8 HashSet
  7. (5)FPGA面试题同步电路和异步电路
  8. (2)verilog语言编写打两拍
  9. python dictionary_Python 字典(Dictionary)
  10. 图片不能及时显示_电脑主机正常运行,显示器黑屏,有六种原因,前三种方法要掌握!...