Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法
D. Longest k-Good Segment
题目连接:
http://www.codeforces.com/contest/616/problem/D
Description
The array a with n integers is given. Let's call the sequence of one or more consecutive elements in a segment. Also let's call the segment k-good if it contains no more than k different values.
Find any longest k-good segment.
As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
input
The first line contains two integers n, k (1 ≤ k ≤ n ≤ 5·105) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 106) — the elements of the array a.
Output
Print two integers l, r (1 ≤ l ≤ r ≤ n) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in a are numbered from 1 to n from left to right.
Sample Input
5 5
1 2 3 4 5
Sample Output
1 5
Hint
题意
给你n个数,你需要找到一个最长的区间,使得这个区间里面不同的数小于等于k个
题解:
尺取法扫一遍就好了
代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
int a[maxn];
int vis[maxn];
int main()
{
int n,k;scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int al=0,ar=0,ans=0,now=0,l=1;
for(int i=1;i<=n;i++)
{
vis[a[i]]++;
if(vis[a[i]]==1)now++;
while(now>k)
{
vis[a[l]]--;
if(vis[a[l]]==0)
now--;
l++;
}
if(i-l+1>=ar-al+1)
ar=i,al=l;
}
cout<<al<<" "<<ar<<endl;
}
Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- A - Longest k-Good Segment (尺取法)
题目链接: https://cn.vjudge.net/contest/249801#problem/A 解题思路:尺取法,每次让尺子中包含k种不同的数,然后求最大. 代码: #include< ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...
随机推荐
- DDoS攻防战(二):CC攻击工具实现与防御理论
我们将要实现一个进行应用层DDoS攻击的工具,综合考虑,CC攻击方式是最佳选择,并用bash shell脚本来快速实现并验证这一工具,并在最后,讨论如何防御来自应用层的DDoS攻击. 第一步:获取大量 ...
- Best Practices for Speeding Up Your Web Site
The Exceptional Performance team has identified a number of best practices for making web pages fast ...
- 基于MapReduce的矩阵乘法运算
1.采用两个MapReduce运算串联来实现 Pik= Mij*Njk 第一步: Map函数:将每个矩阵运算mij传给键值对(j,(M,i,mij)),将每个矩阵元素njk传给键值对(j,(N,k,n ...
- Arduino1.7.10在Ubuntu下创建快捷方式
从官网下载的arduino1.7.10版本没有快捷方式只有可执行文件arduino,通过下面的方法可以创建快捷方式 打开链接:http://www.easyicon.net/1171938-ardui ...
- phonegap 新窗口 WebView
自定义WebView窗口打开 import com.ap.work.QuickWeb public class QuickPlugin extends CordovaPlugin { /** * 新开 ...
- delphi请求idhttp数据
idhttp ss : TStringStream; begin ss := TStringStream.)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对 ...
- 使用cocos2d-x 3.2下载图片资源小例子
cocos2d-x(ios)下载资源可以使用以下两种方式: 第一种使用libcurl下载图片 使用这种方法需要注意的是,我们需要引入libcurl.a这个库,同时配置对应的库目录和头文件目录具体方法是 ...
- js上三行下三行和添加多个附件
function addTr(num) { no ++; var obj = document.getElementById(tableID); var oneRow = obj.insertRow( ...
- C# Control 控件DrapDrop不触发的问题
今天在做一个鼠标拖拽功能时,需要用到PictureBox的拖拽,即拖拽一个图标到PictureBox上实现加载绘制,可是怎么整也触发不了DrapDrop事件,最后终于找到了解决方法:原来需要在Drog ...
- A - Oulipo
A - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...