zoj 3790 Consecutive Blocks 离散化+二分
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 1
1 1 1 2 2 3 2 2
Sample Output
4
题目大意:给一串连续的数字,最多可以删除k个,求相同数字连续个数最大的值。
可以离散化一下把相同数字的下标Push到同一个容器中,再去每个容器中暴力求最大值。
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; const int maxn=;
vector<int> v[maxn];
int f[maxn],X;
int test[maxn]; int Max(int a,int b){ return a>b?a:b;} int fun(int a,int b)
{
int i,j,len=v[a].size();
for(i=len;i>=;i--)
{
for(j=;j<=len-i;j++)
{
if(v[a][i+j-]-v[a][j]+-i<=b)
return i;
}
}
return ;
} int main()
{
int n,k,i,max;
while(~scanf("%d %d",&n,&k))
{
for(i=;i<n;i++) v[i].clear();
for(i=;i<n;i++)
{
scanf("%d",f+i);
test[i]=f[i];
}
sort(test,test+n);
X=unique(test,test+n)-test;
for(i=;i<n;i++)
{
int t=lower_bound(test,test+X,f[i])-test;
v[t].push_back(i);
}
max=;
for(i=;i<X;i++)
{
max=Max(max,fun(i,k));
}
printf("%d\n",max);
}
return ;
}
zoj 3790 Consecutive Blocks 离散化+二分的更多相关文章
- ZOJ 3790 Consecutive Blocks (离散化 + 暴力)
题目链接 虽然是一道暴力的题目,但是思路不好想.刚开始还超时,剪枝了以后1200ms,不知道为什么还是这么慢. 题意:给你n个点,每个点有一种颜色ci,给你至多k次删除操作,每次删除一个点,问最多k次 ...
- ZOJ 3790 Consecutive Blocks 模拟题
problemCode=3790">Consecutive Blocks 先离散一下,然后模拟,把一种颜色i所在的位置都放入G[i]中.然后枚举一下终点位置,滑动窗体使得起点和终点间花 ...
- ZOJ 3790 Consecutive Blocks
大致题意就是给你一个数列,让你最多移除K个数字,使得连续的相同数字的长度最大,并求出最大长度. 我们将此序列按颜色排序,颜色相同的话按位置排序,那么排完序之后颜色相同的blocks就在一起,只是他们的 ...
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- POJ2391 Floyd+离散化+二分+DINIC
题意: 有n个猪圈,每个猪圈里面都有一定数量的猪(可能大于当前猪圈的数量),每个猪圈都有自己的容量,猪圈与猪圈之间给出了距离,然后突然下雨了,问多久之后所有的猪都能进圈. 思路: ...
- zoj 3299(区间改动+离散化)
题意:有n个由小木块组成的长条木块要掉下来.给出木块的左右区间,然后有给了m个木板的左右区间和高度用来接住木块,由于木块是由小木块接触组成的,也就是木板能够接住一部分的木块.剩下的会继续掉落,问最后每 ...
- zoj 2362 Beloved Sons【二分匹配】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2361 来源:http://acm.hust.edu.cn/vjudg ...
- hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)
Mines Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- ZOJ 1654 Place the Robots (二分匹配 )
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...
随机推荐
- Hibernate异常:identifier of an instance of 错误
今天写项目时,在使用hibernate封装的插入方法时,由于需要同时保存多个数据,导致出现identifier of an instance of 如下代码 :(由于最大最小分数不同所以需要插入两条数 ...
- Java Object类 instanceof关键字 练习:判断是否为同一人 集合按照人的年龄排序,如果年龄相同按名字的字母顺序升序 Comparator比较器
package com.swift; public class Same_Person_Test { public static void main(String[] args) { /* * Obj ...
- cocos2dx 通过jni调用安卓底层方法
cocos2dx通过封装JniHelper类来调用安卓api底层函数,该文件在cocos/platform/android/jni/JniHelper.h,使用方法如下: 打开eclipse,导入co ...
- IOS中将颜色转换为image
- (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f ...
- python面向对象之抽象工厂设计模式
class truck: def use(self): return "拉货" def __str__(self): return "大卡车" class mi ...
- 笔记--Day2--python基础2
一.鸡汤 1.提高自我修养 2.人丑就要多读书 3.多走走,开拓眼界 二.目录: 1.列表.元组操作 2.字符串操作 3.字典操作 dict是无序的 key必须是唯一的 4.集合操作 集合是一个无序的 ...
- 共享服务-FTP基础(二)
续接上一篇 使用pam(Pluggable Authentication Modules)完成用户认证 pam_service_name=vsftpd pam配置文件:/etc/pam.d/vsftp ...
- 序列内置方法详解(string/list/tuple)
一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...
- 常用排序算法的总结以及编码(Java实现)
常用排序算法的总结以及编码(Java实现) 本篇主要是总结了常用算法的思路以及相应的编码实现,供复习的时候使用.如果需要深入进行学习,可以使用以下两个网站: GeeksForGeeks网站用于学习相应 ...
- python并发编程之进程2(管道,事件,信号量,进程池)
管道 Conn1,conn2 = Pipe() Conn1.recv() Conn1.send() 数据接收一次就没有了 from multiprocessing import Process,Pip ...