CodeForces
来源:互联网 发布:优盘数据恢复多少钱 编辑:程序博客网 时间:2024/06/03 22:50
C. Pearls in a Row
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.
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.
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“.
5
1 2 3 4 1
1
1 5
5
1 2 3 4 5
-1
7
1 2 1 3 1 2 1
2
1 3
4 7
数据量10^9 数组模拟行不通 可以用set标记
最后一个位置记录错误 一直搞错2333 找半天
#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <map>#include <vector>#include <algorithm>using namespace std;#define LL long long#define INF 0x3f3f3f3f#define N 1000000const int mod = 1e9+7;int pos[N];int main(){ int n; while(~scanf("%d",&n)) { set<int> st; int x, k = 0; for(int i = 1;i <= n; i++) { scanf("%d",&x); if(st.count(x)) { pos[k++] = i; st.clear(); } else st.insert(x); } if(k == 0) puts("-1"); else { pos[k-1] = n; // 没有考虑到这个点,一直wa在第8组数据 printf("%d\n",k); k--; printf("1 %d\n",pos[0]); for(int i = 0;i < k; i++) { printf("%d %d\n",pos[i]+1,pos[i+1]); } } }return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- MySql四种存储引擎
- java web项目启动加载顺序
- “让云计算落地”系列之三:OpenStack的身世之谜
- 提交中文数据乱码问题总结
- java反射详解
- CodeForces
- java类的生命周期
- IE8中文件下载不兼容问题
- Java反射 getFields()和getDeclaredFields()的区别
- JSTL字符串处理函数
- java的IO性能调整
- idea问题(maven忽略版本)
- idea提示忽略大小写
- Docker学习文档之三 其他相关-Dockerfile指令