NOJ 1072 The longest same color grid(尺取)

时间:2022-12-24 18:43:29

Problem 1072: The longest same color grid

Time Limits:  1000 MS   Memory Limits:  65536 KB

64-bit interger IO format:  %lld   Java class name:  Main

Description

There are n grid, m kind of color. Grid number 1 to N, color number 1 to M. 
The color of each grid is painted in one of the m colors. Now let you remove the Grid does not exceed K, get a new sequence.
Ask you the same color and continuous length of the grid sequence is the length of the number?

Input

The first line of input T, T group data. (T <= 20)
The next line of input three numbers n, m and k. (1 < n, m,k <= 10^5)
The next line of input n number, indicating that the color of each grid. (1 <= a[i] <= m)

Output

For each group of data output the same color and continuous grid sequence length.

Sample Input

1
10 2 2
1 2 1 2 1 1 2 1 1 2

Output for Sample Input

5

Hint

For example we can delete the fourth position and the seventh position

题意:给你一个连续的数字染色序列,你最多可以去掉k个格子使得某些格子变成连续的,比如样例的去掉第二和第四个格子,中间的便有5个1是连续的……

学长出的题,刚看到真的是跪了,1e5的范围知道肯定不能两个for去暴力,想过尺取法,然而只是在原序列上进行尺取,这样并不知道到底删除[L,R]中哪几个点才能得到最大的连续长度。

后来学长说是用vector数组记录每一种颜色所出现的下标,对每一种颜色的vector进行尺取,若设左右游标为L、R,则区间内的元素下标就是 $vector[color][Li……Ri]$,区间总长度(闭区间)为 $vector[color][R]-vector[color][L]+1$,所删除的块为记为 $cnt$个,则可获得 $vector[color][R]-vector[color][L]+1-cnt$,依次对每一个颜色的vector进行尺取更新答案即可

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e5+7;
int arr[N];
vector<int>vec[N]; void init(int m)
{
for (int i=0; i<=m; ++i)//这里写成 <m 又让我WA了一次,苦逼
vec[i].clear();
}
int main(void)
{
int tcase,n,m,k,i,color;
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d%d%d",&n,&m,&k);
init(m);
for (i=1; i<=n; ++i)
{
scanf("%d",&color);
vec[color].push_back(i-1);
//printf("%d<-%d\n",color,i-1);
}
int ans=1;
for (i=1; i<=m; ++i)
{
if(vec[i].empty())
continue;
int L=0,R=0;
int cnt=0;
int SZ=vec[i].size();
while (L<SZ)
{
while (R<SZ)
{
++R;
if(R>=SZ)
{
--R;
break;
}
//printf("[%d %d]\n",vec[i][R-1],vec[i][R]);
cnt=cnt+vec[i][R]-vec[i][R-1]-1;
if(cnt>k)
{
cnt=cnt-(vec[i][R]-vec[i][R-1]-1);
--R;
break;
}
}
int now=vec[i][R]-vec[i][L]-cnt+1;
if(now>ans)
ans=now;
++L;
if(L>=SZ)
break;
else
cnt=cnt-(vec[i][L]-vec[i][L-1]-1);
}
}
printf("%d\n",ans);
}
return 0;
}

NOJ 1072 The longest same color grid(尺取)的更多相关文章

  1. hdu 4123 Bob’s Race 树的直径&plus;rmq&plus;尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  2. HDU-4123-树形dp&plus;rmq&plus;尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. Gym 100703I---Endeavor for perfection(尺取)

    题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...

  4. Codeforces Round &num;116 &lpar;Div&period; 2&comma; ACM-ICPC Rules&rpar; E&period; Cubes &lpar;尺取&rpar;

    题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...

  5. poj2566尺取变形

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...

  6. poj2100还是尺取

    King George has recently decided that he would like to have a new design for the royal graveyard. Th ...

  7. hdu 6231 -- K-th Number&lpar;二分&plus;尺取&rpar;

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  8. Codeforces 939E Maximize&excl; &lpar;三分 &vert;&vert; 尺取&rpar;

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  9. cf1121d 尺取

    尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...

随机推荐

  1. 【译】css动画里的steps&lpar;&rpar;用法详解

    原文地址:http://designmodo.com/steps-c... 原文作者:Joni Trythall 我想你在css 动画里使用steps()会和我一样有很多困惑.一开始我不清楚怎样使用它 ...

  2. Linux下安装国际版QQ (转)

    原文链接:http://www.linuxidc.com/Linux/2016-09/134923.htm 说明:一开始,我在Ubuntu 16.04下安装的QQ版本是Wineqq2013SP6-20 ...

  3. 转 : c&plus;&plus; 结构体 前向声明

    typedef struct tag_guid { ULONGLONG utime; ULONGLONG umac; }tpguid; class A { private: int m_teset1; ...

  4. WF4&period;0 自定义CodeActivity与Bookmark&lt&semi;第三篇&gt&semi;

    一.自定义CodeActivity CodeActivity用于自定义一段代码,可实现你自己写的任意功能. 要注意的有两点: 1.自定义CodeActivity必须继承自CodeActivity; 2 ...

  5. poj 2251 Dungeon Master(bfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  6. VS2013中实现angular代码智能提示

    第一步:在项目同添加angular js文件的引用: 这里使用NuGet包管理器来给项目添加angular js install-package angularjs 第二步:添加智能提示js文件 我们 ...

  7. 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle

    #include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...

  8. C&sol;C&plus;&plus;静态代码安全检查工具

    静态代码安全检查工具是一种能够帮助程序员自动检测出源程序中是否存在安全缺陷的软件.它通过逐行分析程序的源代码,发现软件中潜在的安全漏洞.本文针对 C/C++语言程序设计中容易存在的多种安全问题,分别分 ...

  9. linux上的文件服务

    主要的文件服务vsftp.Samba.NFS对比 服务器名称 用户客户端平台 使用范围 服务端口 VSFTP Windows/linux/unix/macOS等 发布网站,文件共享 Tcp/21 Sa ...

  10. js 特效

    栏目1 栏目1->菜单1 栏目1->菜单2 栏目1->菜单3 栏目1->菜单4 栏目2 栏目2->菜单1 栏目2->菜单2 栏目2->菜单3 栏目2-> ...