ZOJ 3790 Consecutive Blocks 贪心
来源:互联网 发布:因笑谓迈曰 汝识之乎 编辑:程序博客网 时间:2024/05/30 05:41
There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 toN from left to right) which are lined up in a row. And the i-th block's color isCi (1 ≤ Ci ≤ 109). Now you can remove at mostK (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. Thei-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
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 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 模拟题
- 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 Consecutive Blocks(左右指针移动)
- ZOJ Monthly, June 2014 C - Consecutive Blocks
- hdoj 1177 "Accepted today?"
- hdoj 1872 稳定排序 【冒泡】
- 感悟
- chansons jamais aimé
- c编程基础
- ZOJ 3790 Consecutive Blocks 贪心
- ZOJ 3792 Romantic Value 最小割
- ZOJ 3795 Grouping 强连通缩点 + DAG最长路
- 序卦와雜卦
- 设计模式(五)——工厂模式(Factory Method)
- 乾爲天章
- Hadoop Hive与Hbase关系 整合
- 今日作息及食谱(6.2)
- 在myeclipse中加入jquery代码提示功能