ZOJ 3790 Consecutive Blocks 贪心

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 ≤ KN) 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 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值