Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)
来源:互联网 发布:aes解密 java 编辑:程序博客网 时间:2024/05/16 23:39
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.
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".
51 2 3 4 1
11 5
51 2 3 4 5
-1
71 2 1 3 1 2 1
21 34 7
题意:good的含义是某一段里有两个数相同,给你一串数组,尽可能地多分成good段,问最多能分成多少,并输出每一段的开头结尾,若没有则输出-1
这题没什么难度,用map存扫一遍直接就过了,写出来主要是想记录一下自己纱布的RE。。。。。。
看对了范围,是的,没看错,范围没看错!手抖少打了一个0。。。也是醉醉的,写出来长长记性
#include <iostream>#include <algorithm>#include <cstdio>#include <stdlib.h>#include <cmath>#include <cstring>#include <string.h>#include <cstdlib>#include <iostream>#include <set>#include <map>#include <vector>using namespace std;int a[300005]; // 范围!!!!!没看错但是敲错!!!!!!int b[300005];int main(){ int n; while(scanf("%d", &n) != EOF){ for(int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(b, 0, sizeof(b)); int k = 0, cnt = 1; map<int, int> sa; sa.clear(); for(int i = 1; i <= n; i++){ if(sa[a[i]] > 0){ b[k++] = cnt; b[k++] = i; cnt = i + 1; sa.clear(); } else sa[a[i]]++; } if(!k) printf("-1\n"); else{ if(cnt <= n){ b[k - 1] = n; } printf("%d\n", k / 2); for(int i = 0; i < k; i++){ if(i % 2 == 0) printf("%d", b[i]); else printf(" %d\n", b[i]); } } } return 0;}
- Codeforces - Educational Codeforces Round 6C - 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
- 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 (分离序列+set)
- Educational Codeforces Round 6 620C Pearls in a Row(stl)
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Codeforces-Edu6-C.Pearls in a Row
- codeforces 620C Pearls in a Row
- CodeForces 620 C. Pearls in a Row(贪心)
- Pearls in a Row CodeForces
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果
- APK瘦身实践
- POJ 2586 Y2K Accounting Bug
- 将Xcode的本地代码push到github仓库上
- Http 400 --- The request sent by the client was syntactically incorrect
- Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)
- Android中实现可滑动的Tab的3种方式
- activity横竖屏幕切换(解决方法)
- UITabbarViewController 动态插入选项可行
- ORA-03113:end-of-file on communication channel解决方法
- 强大的vim配置文件,让编程更随意
- linux下zookeeper安装与测试
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- labview学习心得