Educational Codeforces Round 6-C. Pearls in a Row(贪心)
2016-01-22 01:16
633 查看
C. Pearls in a Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
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
output
input
output
input
output
思路:
这题就是一道简单的贪心题目,只要一找到是有两个相同的就立即变为一段,再特判一下最后的区间是否n值就行了。
AC代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
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
思路:
这题就是一道简单的贪心题目,只要一找到是有两个相同的就立即变为一段,再特判一下最后的区间是否n值就行了。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<cstdio> #include<cmath> #include<set> using namespace std; #define CRL(a) memset(a,0,sizeof(a)) typedef unsigned __int64 LL; typedef __int64 ll; const int T = 3000100; const int mod = 1000000007; set<int> se; int u[T],v[T]; int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif int n,m,i,j,k; int cnt; while(~scanf("%d",&n)) { cnt = 0;se.clear(); scanf("%d",&k); u[cnt]=1; se.insert(k); for(i=1;i<n;++i){ scanf("%d",&k); if(se.find(k)!=se.end()){ se.clear(); v[cnt++] = i+1; u[cnt] = i+2; } else se.insert(k); } if(cnt==0){ printf("-1\n"); continue; } printf("%d\n",cnt); if(v[cnt-1]!=n){ v[cnt-1]=n; } for(i=0;i<cnt;++i){ printf("%d %d\n",u[i],v[i]); } } return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima