C. Pearls in a Row
C. Pearls in a Rowtime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.
Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.
Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
InputThe first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.
The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.
OutputOn the first line print integer k — the maximal number of segments in a partition of the row.
Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment.
Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.
If there are several optimal solutions print any of them. You can print the segments in any order.
If there are no correct partitions of the row print the number "-1".
Sample test(s)input5
1 2 3 4 1output1
1 5input5
1 2 3 4 5output-1input7
1 2 1 3 1 2 1output2
1 3
4 7
题意:
一串珍珠,分为若干小段,每段至少存在两个相同种类的珍珠,求最多可以分多少段。
要求输入珍珠总数目n和n个珍珠的种类,输出最多可分的段数,和每段珍珠的起点和终点。
本题我们可用set容器储存珍珠串,每当输入珍珠与前串含有种类相同时,则记录首尾两点,总段数+1。
附AC代码:
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std; const int MAX=; struct interval{//定义good区间
int l,r;
}a[MAX]; int n;
set<long long> q;//定义set容器 int main(){
while(~scanf("%d",&n)){
q.clear();//每次清空
long long t;
int sum=;
int left=;
for(int i=;i<=n;i++){
scanf("%d",&t);
if(q.count(t)){//count返回set内该元素个数,当含有时则标记区间,清空容器;没有则插入元素
a[sum].l=left;
a[sum].r=i;
sum++;
left=i+;
q.clear();
}
else
q.insert(t);
}
if(a[sum-].r<n)//注意最后一个区间的处理
a[sum-].r=n;
if(sum>){
printf("%d\n",sum);
for(int i=;i<sum;i++){
printf("%d %d\n",a[i].l,a[i].r);
}
}
else
printf("-1\n");
}
return ;
}
C. Pearls in a Row的更多相关文章
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- CodeForces - 620C Pearls in a Row 贪心 STL
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 620C EDU C.Pearls in a Row ( set + greed )
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- codeforces C. Pearls in a Row map的应用
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 6 C. Pearls in a Row set
C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- Codeforce C. Pearls in a Row
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- Qt5的插件机制(6)--开发Qt插件时几个重要的宏
怎样开发Qt插件,能够在Qt Assistant 中搜索"Qt Plugins"或"How to Create Qt Plugins",看看那篇manual中的 ...
- Django中的模板和分页
模板 在Templates中添加母版: - 母版...html 母版(master.html)中可变化的地方加入: {%block content%}{%endblock%} 在子版 (usermg. ...
- 代写GIS系统
代写GIS系统,熟悉arcgis,leaflet ,百度地图等api.可以提供系统代写,技术咨询等
- Chisel辅助iOS 应用程序调试,MusicApp模仿酷狗4.0 UI框架
本文转载至 http://www.cocoachina.com/ios/20140825/9446.html Chisel Chisel集合了大量的LLDB 命令来辅助iOS 应用程序调试,并支持添 ...
- WWDC2014 IOS8 APP Extensions
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35558623 感谢撰文作者的分享 WWDC14 最令人兴奋的除了新语言sw ...
- Avro schemas are defined with JSON . This facilitates implementation in languages that already have JSON libraries.
https://avro.apache.org/docs/current/ Introduction Apache Avro™ is a data serialization system. Avro ...
- Building REST services with Spring
https://spring.io/guides/tutorials/bookmarks/
- ssh key 生成
1.设置好git的name和email $ git config --global user.name "姓名" $ git config --global user.email ...
- tomcat安装后问题解决
tomcat安装后问题解决 (1)tomcat无法正常启动的原因分析 JAVA_HOME 配置错误,或者没有配置 如果你的机器已经占有了8080 端口,则无法启动, 解决方法 (1) 你可以808 ...
- keybd_event、SendInput笔记
void keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, ULONG_PTR dwExtraInfo); bVk:虚拟键码 bScan:键的硬件扫描码 ...