ZOJ Consecutive Blocks(左右指针移动)
来源:互联网 发布:visio 软件开发流程图 编辑:程序博客网 时间:2024/05/29 18:03
题意很简单就是让你看看隔着k个不同的,最多还能构成多少个连续的啊。
10^5托托的不能o(n^2),显然是左右指针瞎搞啊,但是这道题可以排序使得更简单。
按照数字的大小,和地址的先后二级排序,然后分相同数字和不相同数字进行讨论,找到满足条件的最优的解。
There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block's color is Ci (1 ≤ Ci ≤ 109). Now you can remove at most K (0 ≤ K ≤N) blocks, then rearrange the blocks by their index from left to right. Please figure out the length of the largest consecutive blocks with the same color in the new blocks created by doing this.
For example, one sequence is {1 1 1 2 2 3 2 2} and K=1. We can remove the 6-th block, then we will get sequence {1 1 1 2 2 2 2}. The length of the largest consecutive blocks with the same color is 4.
Input
Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integers N and K, separated by a blank, from the first line. The color of the blocks will be given in the second line of the test case, separated by a blank. The i-th integer means Ci.
Output
Please output the corresponding length of the largest consecutive blocks, one line for one case.
Sample Input
8 11 1 1 2 2 3 2 2
Sample Output
4
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-8#define M 1000100//#define LL __int64#define LL long long#define INF 0x7ffffff#define PI 3.1415926535898const int maxn = 101000;using namespace std;struct node{ int x; int point;}f[maxn];bool cmp(node a, node b){ if(a.x == b.x) return a.point < b.point; return a.x < b.x;}int main(){ int n, k; while(cin >>n>>k) { int x; for(int i = 0; i < n; i++) { cin >>x; f[i].x = x; f[i].point = i; } sort(f, f+n, cmp); int Max = -1; int i, j; int r = 1; int dis = 1; for(i = 0; i < n;) { dis = r-i; for(j = r; j < n; j++) { if(f[i].x != f[j].x) { Max = max(Max, dis); i = j; r = j+1; break; } if(f[j].point-f[i].point > j-i+k) { Max = max(Max, dis); i++; dis--; r = j; break; } dis++; } if(j == n) { Max = max(dis, Max); break; } } cout<<Max<<endl; } return 0;}
- ZOJ Consecutive Blocks(左右指针移动)
- ZOJ 3790 Consecutive Blocks(迟取法)
- ZOJ 3790 Consecutive Blocks(尺取法)
- ZOJ 3790 Consecutive Blocks
- zoj-3790-Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks
- ZOJ 3790 Consecutive Blocks 贪心
- ZOJ月赛3790Consecutive Blocks(贪心)
- ZOJ 3790 Consecutive Blocks(枚举+二分查找)
- Consecutive Blocks (ZOJ3970)
- ZOJ 3790 Consecutive Blocks 模拟题
- ZOJ Monthly, June 2014 C - Consecutive Blocks
- ZOJ 3790 Consecutive Blocks 排序+扫描
- ZOJ 3790 Consecutive Blocks [排序+扫描]
- ZOJ 3790 Consecutive Blocks【离散化】【贪心】
- nginx源码分析--从源码看nginx框架总结
- java web学习基础(三)4中属性范围 request对象 response对象
- 乘蒙又百彻磐谓曰弥炭松比
- 编译64位 hadoop 2.4 源码
- Scala 的Trait和类型转换
- ZOJ Consecutive Blocks(左右指针移动)
- Convert Array to Hash while preserving Array index values in Ruby
- Web.config
- system.out.pintln() 和 system.err.println() 的使用注意
- duplicate symbol _OBJC_METACLASS_$_ClassName
- uboot第二阶段代码详细分析
- Matlab 默认路径修改
- Android的硬件加速
- 树状数组模板题—hdu1166+poj2352