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 ...
随机推荐
- React组件间通信
众所周知,ReactJS组件与组件之间的通信是一个难点.在React实际开发中,父子组件之间的传值是比较常见的,刚入门的小伙伴很容易被组件之间的通信绕懵. 今天花了点时间总结了一下React父子组件之 ...
- iOS 骰子战争 Dice Wars
占坑 这个游戏之前在网页端玩过,App Store 上没有搜到特别好的,想自己做个类似的iOS APP 游戏 目测实现难度适中,可玩性较高
- iOS8之后,UITableViewRowAction实现滑动多个按钮
#pragma mark - View lifeCycle - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = ...
- C++函数的默认参数补充
1.函数定义时指定默认参数 在C++中,定义函数时可以给形参指定一个默认的值,这样调用函数时如果没有给这个形参赋值(没有对应的实参),那么就使用这个默认的值.也就是说,调用函数时可以省略有默认值的参数 ...
- Java 的Throwable、error、exception的区别
1. 什么是异常? 异常本质上是程序上的错误,包括程序逻辑错误和系统错误.比如使用空的引用(NullPointerException).数组下标越界(IndexOutOfBoundsException ...
- 笔记--Day1--python基础1
一.目录 1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum),目前已经是使用频度特别高的开发语言. 主要应用领域: 云计算:云计算最火的语言,典型应用有Op ...
- python入门:while 循环的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #while 循环的作用 import time while True: ") time.sleep ...
- Hard problem CodeForces - 706C
Time limit1000 ms Memory limit262144 kB 题目: Vasiliy is fond of solving different tasks. Today he fou ...
- spring+struts2+hibernate框架依赖pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 给vagrant中的precise64升级VBoxGuestAdditions
位置:/usr/share/virtualbox/VBoxGuestAdditions.iso 在host(ubuntu 12.04 64)中: 查看虚拟机的名字:jb@H38:~/vm/vagran ...