zoukankan      html  css  js  c++  java
  • CoderFocers-620C

    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/printf instead 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".

    Example
    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

    题解:简单的贪心题目,看代码就懂了,很简单。

    AC代码为:

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<algorithm>
    using namespace std;


    const int maxn=3e5+10;
    int a[maxn];
    map<int ,int> mp;
    struct node{
    int l;
    int r;
    }s[maxn];


    int main()
    {
    int k,li,ri,f=0,cnt=0;
    li=ri=1;
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
    scanf("%d",&a[i]);

    if(!mp.count(a[i]))
    {
    mp[a[i]]=i;
    }
    else
    {
    ri=i;
    s[f].l =li;
    s[f].r =ri;
    f++;
    li=i+1;
    cnt++;

    mp.clear();
    }

    }

    s[f-1].r = k;

    if(!cnt)
    printf("-1");
    else
    {
    printf("%d ",cnt);

    for(int i=0;i<f;i++)
    {
    printf("%d %d ",s[i].l,s[i].r );
    }




    return 0;
    }




  • 相关阅读:
    iOS- Swift:使用FMDB进行数据库操作(线程安全:增删改查)
    iOS- Swift:如何使用iOS8中的UIAlertController
    iOS- Swift和Object-C的混合编程
    iOS- 再谈ARC里内存问题,ARC里数组、对象内存得不到释放?
    iOS- 如何建立索引实现本地文本搜索引擎,允许容错搜索?
    iOS- 全方位解析.crash文件崩溃报告
    iOS- 关于AVAudioSession的使用——后台播放音乐
    iOS- Autolayout自动布局
    iOS- 详解如何使用ZBarSDK集成扫描二维码/条形码,点我!
    419. Roman to Integer【medium】
  • 原文地址:https://www.cnblogs.com/csushl/p/9386601.html
Copyright © 2011-2022 走看看