【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数

来源:互联网 发布:土豆网络蓝色蜘蛛网 编辑:程序博客网 时间:2024/05/19 04:03

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".

Examples
input
51 2 3 4 1
output
11 5
input
51 2 3 4 5
output
-1
input
71 2 1 3 1 2 1
output
21 34 7

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=3e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;int n,x;map<int,int>pre;int f[N];int lft[N];void print(int p){if(f[p]==1)printf("%d %d\n",1,p);else{print(lft[p]-1);printf("%d %d\n",lft[p],p);}}int main(){while(~scanf("%d",&n)){pre.clear();for(int i=1;i<=n;++i){scanf("%d",&x);f[i]=f[i-1];lft[i]=lft[i-1];int p=pre[x];if(p){int tmp=f[p-1]+1;if(tmp>f[i]){f[i]=tmp;lft[i]=p;}}pre[x]=i;}if(f[n]==0)puts("-1");else printf("%d\n",f[n]),print(n);}return 0;}/*【题意】有n(3e5)个数每个数的权值在[1,1e9]之间我们想要把这1~n的n个数,划分成尽可能多的区间,使得每个区间都包含重复的数。【类型】DP or 贪心【分析】DP做法是——记录上一个,f[i]=max(f[i-1],f[lastx-1]+1);ans=f[n]或者直接贪心——第一次重现重复的数就截断即可。【时间复杂度&&优化】O(nlogn)*/

【Educational Codeforces Round 6C】【贪心做法】Pearls in a Row n个数分最多区间使得每个区间都有重复数

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 3e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n, x;map<int, int>pre;int f[N];int lft[N];void print(int p){if (f[p] == 1)printf("%d %d\n", 1, p);else{print(lft[p] - 1);printf("%d %d\n", lft[p], p);}}pair<int, int>a[N];int main(){while (~scanf("%d", &n)){int lft = 1;int ans = 0;pre.clear();for (int i = 1; i <= n; ++i){scanf("%d", &x);if (pre[x] >= lft){a[++ans] = MP(lft, i);lft = i + 1;}pre[x] = i;}if (ans == 0)puts("-1");else{printf("%d\n", ans);a[ans].second = n;for (int i = 1; i <= ans; ++i)printf("%d %d\n", a[i].first, a[i].second);}}return 0;}/*【题意】有n(3e5)个数每个数的权值在[1,1e9]之间我们想要把这1~n的n个数,划分成尽可能多的区间,使得每个区间都包含重复的数。【类型】DP or 贪心【分析】DP做法是——记录上一个,f[i]=max(f[i-1],f[lastx-1]+1);ans=f[n]或者直接贪心——第一次重现重复的数就截断即可。*/


0 0
原创粉丝点击