ZOJ 3790 Consecutive Blocks 贪心
2014-06-02 05:04
351 查看
Consecutive Blocks
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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.
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.
Author: LIN, Xi
Source: ZOJ Monthly, June 2014
题目链接:ZOJ 3790 Consecutive Blocks
题目大意:给你n个点,每个点有一种颜色ci,给你至多k次删除操作,每次删除一个点,问最多k次操作后连在一起的点颜色相同的最大长度。
题目分析:
由题意可知,最长的必定是同一种颜色的。
每个点记录两种状态,color记录颜色,pos记录下标。
以color为第一关键字,pos为第二关键字排序,得到颜色不同则按颜色递增,颜色相同则下标递增的队列。
我们可以贪心的为每种颜色在至多k次操作的条件下选出最长的颜色。
对于两个排序后相邻的i - 1, i。pos不相邻,说明原序中间还夹着一些其他颜色的点,需要pos[i] - pos[i - 1] - 1的操作删除,如果现在已经删除的点的个数tmpk + 这次的操作小于等于k,则tmpk +=
pos[i] - pos[i - 1] - 1,连接这两个点,当前长度 + 1。对于相邻的点,直接长度 + 1。如果如果现在已经删除的点的个数tmpk + 这次的操作大于k,则在左端删除已连接的点,回收部分操作数,直到大于等于pos[i] - pos[i - 1] - 1时停止并连接这两个点。易知这样是不会破坏最优解的,因为包括左端的最长链已经计算过。
那么我们可以得到一种方法,对于相同颜色的点,如果连接第i - 1 和第 i 个点的操作数 + 之前的操作数小于等于k,直接连接,否则,从左边删除已连接的点,回收最少的点使得可以进行这次操作为止,再连接。对于不同颜色的点,所有的变量全部归为初始值,重新再来。
结果即为所有颜色能达到的最大长度的最大值。
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 integersN 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 1 1 1 1 2 2 3 2 2
Sample Output
4
Author: LIN, Xi
Source: ZOJ Monthly, June 2014
题目链接:ZOJ 3790 Consecutive Blocks
题目大意:给你n个点,每个点有一种颜色ci,给你至多k次删除操作,每次删除一个点,问最多k次操作后连在一起的点颜色相同的最大长度。
题目分析:
由题意可知,最长的必定是同一种颜色的。
每个点记录两种状态,color记录颜色,pos记录下标。
以color为第一关键字,pos为第二关键字排序,得到颜色不同则按颜色递增,颜色相同则下标递增的队列。
我们可以贪心的为每种颜色在至多k次操作的条件下选出最长的颜色。
对于两个排序后相邻的i - 1, i。pos不相邻,说明原序中间还夹着一些其他颜色的点,需要pos[i] - pos[i - 1] - 1的操作删除,如果现在已经删除的点的个数tmpk + 这次的操作小于等于k,则tmpk +=
pos[i] - pos[i - 1] - 1,连接这两个点,当前长度 + 1。对于相邻的点,直接长度 + 1。如果如果现在已经删除的点的个数tmpk + 这次的操作大于k,则在左端删除已连接的点,回收部分操作数,直到大于等于pos[i] - pos[i - 1] - 1时停止并连接这两个点。易知这样是不会破坏最优解的,因为包括左端的最长链已经计算过。
那么我们可以得到一种方法,对于相同颜色的点,如果连接第i - 1 和第 i 个点的操作数 + 之前的操作数小于等于k,直接连接,否则,从左边删除已连接的点,回收最少的点使得可以进行这次操作为止,再连接。对于不同颜色的点,所有的变量全部归为初始值,重新再来。
结果即为所有颜色能达到的最大长度的最大值。
#include <stdio.h> #include <string.h> #include <algorithm> #define min(A, B) ((A) < (B) ? (A) : (B)) #define max(A, B) ((A) > (B) ? (A) : (B)) #define clear(A, X) memset(A, X, sizeof A) using namespace std; const int maxN = 100005; const int maxE = 1000000; int n, m; struct Node{ int color; int pos; bool operator < (const Node &t) const{ return color != t.color ? color < t.color : pos < t.pos; } }; Node a[maxN]; void work(){ for(int i = 1; i <= n; ++i){ scanf("%d", &a[i].color); a[i].pos = i; } sort(a + 1, a + n + 1); int ans = 1, tmpans = 1, count = 1; int l = 1, tmpk = m; for(int i = 2; i <= n; ++i){ if(a[i].color == a[i - 1].color){ tmpk -= a[i].pos - a[i - 1].pos - 1; count++; while(tmpk < 0){ tmpk += a[l + 1].pos - a[l].pos - 1; ++l; count--; } tmpans = max(tmpans, count); } else{ ans = max(tmpans, ans); tmpans = 1; l = i; count = 1; tmpk = m; } } ans = max(tmpans, ans); printf("%d\n", ans); } int main(){ while(~scanf("%d%d", &n, &m)) work(); return 0; }
相关文章推荐
- ZOJ 3790 Consecutive Blocks【离散化】【贪心】
- ZOJ 2422——贪心 田忌赛马
- zoj 2921 Stock(贪心)
- ZOJ 2315 New Year Bonus Grant(贪心)
- ZOJ1360 POJ1328 Radar Installation, 贪心
- ZOJ 3905 Cake(贪心+dp)
- ZOJ 3607 Lazier Salesgirl(贪心啊 )
- ZOJ 3607 Lazier Salesgirl (贪心)
- ZOJ 3829 Known Notation【贪心】【好题】
- zoj 3627 贪心模拟
- ZOJ-#3464 Rugby Football(贪心)
- ZOJ 3699 Dakar Rally(贪心)
- ZOJ 3820 Building Fire Stations 贪心+树的直径
- ZOJ 3790 Consecutive Blocks(枚举+二分查找)
- 【贪心+一点小思路】Zoj - 3829 Known Notation
- [贪心](JZOJ)【USACO Open 2012铜】岛屿Islands
- ZOJ 3689 Digging(贪心+dp)
- ZOJ 3790 Consecutive Blocks(尺取法)
- ZOJ-3715-Kindergarten Election(贪心 枚举 模拟)
- ZOJ 3715 Kindergarten Election(枚举+贪心)