zoukankan      html  css  js  c++  java
  • codeforces C. Pearls in a Row map的应用

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

    Sample test(s)
    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

     题意:将这些算分成一段一段的,每段最多包含两个相同的数字,最多有多少段,和怎么分的段;

     思路:用map记录这个数字在当前这段出现的次数,用队列存当前这段的数字,当有一个数字出现第二次时,清空队列而且保存起点和终点位置;

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+5;
    int a[5*N];
    struct node
    {
        int a,pos;
    };
    struct po
    {
        int le,ri;
    };
    po ans[5*N];
    int main()
    {
        map<int,int>mp;
        queue<node>qu;
        node x;
       int n;
       int cnt=0;
       scanf("%d",&n);
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&a[i]);
       }
       for(int i=1;i<=n;i++)
       {
           if(mp[a[i]]==0)
           {
               mp[a[i]]++;
               x.a=a[i];
               x.pos=i;
               qu.push(x);
           }
           else
           {
               mp[a[i]]++;
               x.a=a[i];
               x.pos=i;
               qu.push(x);
               int l,r;
               l=qu.front().pos;
               while(!qu.empty())
               {
                   x=qu.front();
                   mp[x.a]--;
                   qu.pop();
               }
               r=x.pos;
               cnt++;
               ans[cnt].le=l;
               ans[cnt].ri=r;
           }
       }
       if(!cnt)cout<<"-1"<<endl;
       else
       {
           printf("%d
    ",cnt);
           for(int i=1;i<cnt;i++)
           {
               printf("%d %d
    ",ans[i].le,ans[i].ri);
           }
           printf("%d %d
    ",ans[cnt].le,n);
       }
        return 0;
    }
  • 相关阅读:
    No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=arm64, VALID_ARCHS=armv7 armv7s).
    播放器 倒计时 闹钟 日期 分秒 时间算法
    iOS 8 以后获取地图坐标:
    数据存储(直接写入、NSUserDefaults、NSkeyedArchiver)
    图片处理 模糊效果
    手把手教你Windows下Go语言的环境搭建
    github 上传或删除 文件 命令
    域名解析-delphi 源码
    指针与引用
    指针
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5158545.html
Copyright © 2011-2022 走看看