CF 620 C. 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<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+离散化+map
void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《编程珠玑》是一本经典的计算机编程书籍,由Jon Bentley所著。书中作者以宝石的形式,以自己在编程领域的经验和思考作为原料,展现了许多精彩的编程技巧和解决问题的思路。 这本书被誉为计算机界的经典之作,其价值不仅在于作者分享的个人经验,更在于其中蕴含的许多智慧和洞见。书中的每个章节都涵盖了一个独立的编程话题,包括算法优化、代码调试、程序设计等,并通过大量的实例和案例讲解了相应的技巧和方法。这些实例涵盖了几乎所有计算机领域,包括排序、查找、字符串处理等,从而帮助读者理解和掌握这些技巧。 与其他编程书籍不同,《编程珠玑》强调的是实用性和深度思考。书中的每个问题都是作者亲自经历过的,每个解决方案都是经过深思熟虑的。通过阅读这本书,读者可以学习到如何通过合理的数据结构和算法设计,将问题简化并高效解决。同时,作者还教会了读者如何思考和分析问题,如何避免一些常见的陷阱和错误。 《编程珠玑》不仅适合初级的程序员,也适合有一定编程经验的开发者阅读。无论是学习编程的初学者,还是已经有多年开发经验的工程师,都可以通过阅读这本书,提升自己的编程能力和思维方式。读者可以在书中找到灵感和启示,解决实际的编程难题,并学到一些通用的编程技巧和原则。 总之,《编程珠玑》是一本极具价值的编程书籍,其内容丰富实用,思想深入,对于提升编程能力和思维方式都具有重要意义。无论是想成为一名优秀的程序员,还是找到解决实际问题的思路,这本书都值得一读。 ### 回答2: 《编程珠玑》是由Jon Bentley于1986年出版的一本经典计算机科学书籍。书中主要讨论了各种编程问题和解决方案,旨在帮助读者提高编程技巧和思维能力。 这本书是由编程问题组成的,每个问题都来自实际编程中的实际情况。每个问题都是用C语言描述并解决的。在解决问题的过程中,书中提出了一些有趣的技巧和方法,例如位操作、动态规划、递归和分治等。这些技巧和方法不仅可以帮助我们解决具体的问题,还可以帮助我们提高编程效率和代码质量。 《编程珠玑》的作者Jon Bentley是一位计算机科学家,他在书中分享了他多年来在编程领域的经验和见解。通过这本书,读者可以学到很多关于编程的技巧和思维方式,有助于提高解决实际编程问题的能力。 总的来说,《编程珠玑》是一本非常有价值的编程书籍,无论是初学者还是有经验的程序员都可以从中受益。它可以帮助我们了解并掌握解决实际编程问题的方法和技巧,提高我们的编程能力和思维能力。如果你对编程有兴趣,并且想提高自己的编程技术,我强烈推荐你阅读这本书。 ### 回答3: 《编程珠玑》是一本由Jon Bentley编写的计算机科学经典著作,被誉为程序员必读之书。该书探讨了一系列关于编程和算法的知识和技巧,以及一些经典的编程问题和解决方法。 《编程珠玑》的主要目的是通过具体的编程案例和讨论,帮助读者提高编程技巧和解决问题的能力。书中提供了众多的编程小技巧和优化思路,让程序员能够对复杂问题进行有针对性的分析和解决。这些技巧涵盖了各个方面,包括数据结构、算法设计、性能优化等。 该书中的编程珠玑主要包括程序设计的三个重要原则:正确性、性能和可读性。作者通过讲述自己的实践经验,以及一些经典的编程问题和解决方法,来阐述这些原则。读者可以通过学习和理解这些原则,提高自己的程序设计能力,并编写出更加优秀的代码。 除了传授编程技巧和知识外,该书还强调了问题解决的思维方式。作者通过讲述一些实际问题的解决过程,鼓励读者灵活运用各种方法和工具,来解决复杂的编程难题。这种思维方式对于程序员来说非常重要,可以培养解决问题的能力,提高工作效率。 总的来说,《编程珠玑》是一本经典的计算机科学读物,通过具体的案例和讨论,帮助读者提高编程技巧和解决问题的能力。无论是想提升自己的程序设计能力,还是希望更好地解决编程难题,这本书都是值得一读的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值