FZU2216 最长连续子序列
2015-12-31 13:12
225 查看
题目大意:给定一个序列,长度为n,所有数据大小不超过m,若为0则代表可以是任意数,问最长的连续子序列是多长,(2,3,4,5,)为连续
思路:设数组g[i]表示前i个自然数中0的个数,初始化g时若i被访问则g[i]=g[i-1],否则g[i]=g[i-1]+1.那么结果就是从0到m遍历找到g[i]+num(0的个数)的最小上界即可。
思路:设数组g[i]表示前i个自然数中0的个数,初始化g时若i被访问则g[i]=g[i-1],否则g[i]=g[i-1]+1.那么结果就是从0到m遍历找到g[i]+num(0的个数)的最小上界即可。
#include <cstdio> #include <string> #include <cstring> #include <fstream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <iostream> #include <iomanip> using namespace std; #define maxn 100005 #define MOD 1000000007 #define mem(a) memset(a , 0 , sizeof(a)) #define LL __int64 #define INF 999999999 int n ; int g[maxn]; int arr[maxn]; int main() { int n , m; int t = 0; scanf("%d" , &t); while(t--) { scanf("%d %d" , &n , &m) ; int x , num = 0; mem(arr); mem(g); for(int i = 0 ; i < n ; i ++) { scanf("%d" , &x); if(x) arr[x] = 1; else num++; } for(int i = 1 ; i <= m ; i ++) { if(arr[i]) g[i] = g[i-1]; else g[i] = g[i-1] + 1; } int ans = 0; for(int i = 0 ; i <= m ; i ++) { int l = i , r = m; int mid; while(l <= r) { mid = (r + l ) / 2; if(g[mid] - g[i] > num) r = mid - 1; else l = mid + 1; } ans = max(ans , r - i); } printf("%d\n" , ans); } return 0; }
相关文章推荐
- UIView
- 一篇关于用户需求,己方产品(服务)与竞争对手的小清单
- 数据结构--Chapter9(查找)
- 去除手机页面点击元素的阴影
- C++ function overriding
- QT收费吗?
- windows无硬件连接时如何与虚拟机中linux通信
- Bag of Words cpp实现(stable version 0.01)
- Dubbo采过的坑
- 公司人员管理程序(一)
- 采用开源的架构提高业务敏捷性
- C++面试一
- android studio 工程导入部分注意事项
- Ring3下的DLL注入工具 x86&x64(NtCreateThreadEx + LdrLoadDll方式实现,可以注入系统进程)
- 在Matlab中显示Maple公式
- POJ1580 水题,积累!
- SIFT(ASIFT) Matching with RANSAC
- POJ 1469 COURSES(二分匹配-hungary)
- 读取xml文件"分析 EntityName 时出错"的解决方案
- [转载] Memory Layout of C programs