zoukankan      html  css  js  c++  java
  • C. Pearls in a Row

    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

    题意:

    一串珍珠,分为若干小段,每段至少存在两个相同种类的珍珠,求最多可以分多少段。

    要求输入珍珠总数目n和n个珍珠的种类,输出最多可分的段数,和每段珍珠的起点和终点。

    本题我们可用set容器储存珍珠串,每当输入珍珠与前串含有种类相同时,则记录首尾两点,总段数+1。

    附AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<set>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int MAX=1000000;
     8 
     9 struct interval{//定义good区间 
    10     int l,r;
    11 }a[MAX];
    12 
    13 int n;
    14 set<long long> q;//定义set容器 
    15 
    16 int main(){
    17     while(~scanf("%d",&n)){
    18         q.clear();//每次清空 
    19         long long t;
    20         int sum=0;
    21         int left=1;
    22         for(int i=1;i<=n;i++){
    23             scanf("%d",&t);
    24             if(q.count(t)){//count返回set内该元素个数,当含有时则标记区间,清空容器;没有则插入元素 
    25                 a[sum].l=left;
    26                 a[sum].r=i;
    27                 sum++;
    28                 left=i+1;
    29                 q.clear();
    30             }
    31             else
    32             q.insert(t);
    33         }
    34         if(a[sum-1].r<n)//注意最后一个区间的处理 
    35         a[sum-1].r=n;
    36         if(sum>0){
    37             printf("%d
    ",sum);
    38             for(int i=0;i<sum;i++){
    39                 printf("%d %d
    ",a[i].l,a[i].r);
    40             }
    41         }
    42         else
    43         printf("-1
    ");
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    CSRF的防御解决过程
    Spring生态研习【三】:Spring-kafka
    Spring生态研习【二】:SpEL(Spring Expression Language)
    Spring生态研习【一】:定时任务Spring-task
    给定一个大的任务,需要在考虑性能的情况下,快速处理完,并报告结果
    给定一个大于2的偶数,将其分解为两个质数的和
    一个求解平方根的算法题
    Kafka研究【一】:bring up环境
    LB+nginx+tomcat7集群模式下的https请求重定向(redirect)后变成http的解决方案
    IDEA使用笔记(八)——自动生成 serialVersionUID 的设置
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/5532339.html
Copyright © 2011-2022 走看看