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

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

#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|1
typedef 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|1
typedef 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]

或者直接贪心——
第一次重现重复的数就截断即可。

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值