第六届福建省大学生程序设计竞赛——E The Longest Straight(尺取法)
2015-12-28 21:02
579 查看
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2216
题目大意:给出一个N和M,再给出N个数,这N个数都是在0~M这个范围内的。其中的0当做1~M的任意数,不同的0,可以当做不同的数。问最多的连续的数有多少个(重复算一个)?
解题思路:先把N个数映射到1~M的数组上,并同时记录0的个数。然后直接尺取法,当[l, r]区间内0的个数小于等于原有的0的个数时,r就往前挪一个位置,否则l往前挪一个。每次挪动r时判断是否需要更新答案就行了。
Show me the code!
题目大意:给出一个N和M,再给出N个数,这N个数都是在0~M这个范围内的。其中的0当做1~M的任意数,不同的0,可以当做不同的数。问最多的连续的数有多少个(重复算一个)?
解题思路:先把N个数映射到1~M的数组上,并同时记录0的个数。然后直接尺取法,当[l, r]区间内0的个数小于等于原有的0的个数时,r就往前挪一个位置,否则l往前挪一个。每次挪动r时判断是否需要更新答案就行了。
Show me the code!
#include <cstdio> #include <cstring> using namespace std; const int maxn = 1e5 + 1; int arr[maxn]; int max(int a, int b) { return a >= b ? a : b; } int main() { int T; scanf("%d", &T); while (T--) { memset(arr, 0, sizeof arr); int N, M, joker = 0, num; scanf("%d%d", &N, &M); for (int a = 0; a < N; ++a) { scanf("%d", &num); if (!num) joker += 1; else arr[num] = 1; } int l = 1, r = 1, count = arr[l] == 0, res = 1; while (r <= M) { if (l < r && count > joker) { if (arr[l] == 0) count -= 1; l += 1; } else { r += 1; if (r <= M && arr[r] == 0) count += 1; if (r <= M && count <= joker) res = max(res, r - l + 1); } } printf("%d\n", res); } return 0; }
相关文章推荐
- Polymorphic Inline Caches explained
- 关于Activity间Intent传值“FAILED BINDER TRANSACTION”问题
- AIDL
- FZU 2216 The Longest Straight 二分
- offsetof与container_of宏[总结]
- jms的failover方式
- 服务器主机上RAID Controller的Read Ahead Policy
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)
- 自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)
- RAID6磁盘阵列数据如何恢复
- MiniSnail子模块的规划
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
- Training set、 gallery set、Probe set
- WSAWaitForMultipleEvents
- stl map用法和make_pair函数和pair结构体
- Clairewd’s message
- Failed to restart network.service: Unit network.service failed to load: No such file or directory.
- v8 bailout
- UVA-Matrix Chain Multiplication(栈)
- Containing ViewControllers