Codeforces-Edu6-C.Pearls in a Row
来源:互联网 发布:icmp协议的ip端口号 编辑:程序博客网 时间:2024/04/29 21:14
C. Pearls in a Row
time 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/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The 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.
Output
On 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)
input
5
1 2 3 4 1
output
1
1 5
input
5
1 2 3 4 5
output
-1
input
7
1 2 1 3 1 2 1
output
2
1 3
4 7
这个题是最简单的贪心题,唯一注意最后一个segment的结尾是n;
代码如下:
#include <bits/stdc++.h>using namespace std;typedef struct data { int l; int r;}data;int main(){ data b[109000]; int a[300015]; int i,n,j,st=0,m=0; cin>>n; for(i=1;i<=n;i++) { scanf("%d",&a[i]); for(j=st+1;j<i;j++) { if(a[i]==a[j]) { m++; b[m].l=st+1; b[m].r=i; st=i; } } } if(m==0) { cout<<"-1"; } else { cout<<m<<endl; for(i=1;i<m;i++) { cout<<b[i].l<<" "<<b[i].r<<endl; } cout<<b[m].l<<" "<<n; } return 0;}
仅代表个人观点,不喜勿喷!
- Codeforces-Edu6-C.Pearls in a Row
- codeforces 620C Pearls in a Row
- Pearls in a Row CodeForces
- C. Pearls in a Row
- cf Educational Codeforces Round 6 C Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- Educational Codeforces Round 6 620C Pearls in a Row
- CodeForces 620 C. Pearls in a Row(贪心)
- Codeforces 620C Pearls in a Row(贪心)
- 【32.26%】【codeforces 620C】Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- Educational Codeforces Round 6 620C Pearls in a Row(stl)
- Educational Codeforces Round 6 C. Pearls in a Row (分离序列+set)
- CF620C - Pearls in a Row
- 5-11 UVA 12504 Updating a dictionary更新字典
- 深入理解JVM--JVM垃圾回收机制
- Codeforces-Edu6-B. Grandfather Dovlet’s calculator
- Make VLC player support polarized 3D.让VLC播放器支持偏振3D效果。
- 16初出茅庐B题
- Codeforces-Edu6-C.Pearls in a Row
- 数据结构总结,指针
- 16初出茅庐C题
- **LeetCode 54. Spiral Matrix
- 字符集的初步认识
- 16初出茅庐D题
- 16初出茅庐E题
- cookieless domain
- 16初出茅庐F题