CF 620 C. Pearls in a Row 贪心

来源:互联网 发布:qt ros 显示界面编程 编辑:程序博客网 时间:2024/05/22 22:02

题目大意:给出一个数组,要求将数组分成若干段,每个数都必须属于一段,使得每一段都至少有两个数相同。

为何不贪心呢?从左往右扫描,只要满足形成一段的条件,立马形成一段,注意最后一段结尾必须是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
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<cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<iterator>#include<stack>using namespace std;typedef __int64 LL;const int INF=1e9+7;const double eps=1e-7;const int maxn=3*100000;int n,a[maxn+10];map<int,int >mp;vector<int >ve;void pre()//离散化,其实这题不用map离散化,可用set代替。{    mp.clear();    for(int i=1;i<=n;i++)    {        if(!mp.count(a[i]))        {            int k=mp.size();            mp[a[i]]=k;            a[i]=k;        }        else        {            int num=mp[a[i] ];            a[i]=num;        }    }}int vis[maxn+10];//可用set代替标记vis+离散化+mapvoid work(){    memset(vis,-1,(n+1)*sizeof vis[0] );    ve.clear();    int ans=0;    for(int i=1;i<=n;i++)    {        int x=a[i];        if(vis[x]==ans)        {            ans++;            ve.push_back(i);            vis[x]=-1;        }        else        {            vis[x]=ans;        }    }    if(ve.size()==0)  {puts("-1");return;}    printf("%d\n",ve.size());    int last=1;    for(int i=0;i<ve.size();i++)    {        int x=ve[i];        if(i!=ve.size()-1)  printf("%d %d\n",last,x);        else   printf("%d %d\n",last,n);        last=x+1;    }}int main(){    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        pre();        work();    }    return 0;}


0 0
原创粉丝点击