【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
来源:互联网 发布:土豆网络蓝色蜘蛛网 编辑:程序博客网 时间:2024/05/19 04:03
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
#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]或者直接贪心——第一次重现重复的数就截断即可。*/
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Educational Codeforces Round 6 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 620C Pearls in a Row
- Educational Codeforces Round 6 C. 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 620C Pearls in a Row(stl)
- Educational Codeforces Round 6 C. Pearls in a Row (分离序列+set)
- 【Codeforces Round 332 (Div 2)C】【贪心】Day at the Beach 最多区间数划分使得区间排序构成全局排序
- CodeForces 620 C. Pearls in a Row(贪心)
- Codeforces 620C Pearls in a Row(贪心)
- Codeforces-Edu6-C.Pearls in a Row
- codeforces 620C Pearls in a Row
- Educational Codeforces Round 11 C. Hard Process(尺取 二分 区间天数最多
- Pearls in a Row CodeForces
- [Andriod Monkey测试] Monkey黑/白名单
- IntentService的原理及使用
- 经典算法之——快速排序
- java类的加载顺序
- 运维日志排查常用shell
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)(转)
- 【POJ 3691】【hdu 2457】DNA repair 中文题意&题解&代码(C++)
- 基于用户协同过滤的推荐系统算法,python 实现
- gitosis 手记
- spring mvc 框架搭建及详解
- 【Educational Codeforces Round 6D】【暴力 SET二分】Professor GukiZ and Two Arrays
- JVM > OutOfMemoryError异常
- OSG 天空盒子