zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 6 C

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

    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 处理 注意最后一段的右区间一定为n

    2000ms 超时代码
    #include<bits/stdc++.h>
    using namespace std;
    int n;
    struct node
    {
        int l;
        int r;
    }m[300005];
    int a[300005];
    int main()
    {
        scanf("%d",&n);
        int st=0;
        int jishu=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=st+1;j<i;j++)
            {
                if(a[j]==a[i])
                {
                    m[jishu].l=st+1;
                    m[jishu].r=i;
                    jishu++;
                    st=i;
                }
            }
        }
        if(jishu==0)
            printf("-1
    ");
        else
        {
            printf("%d
    ",jishu);
            for(int i=0;i<jishu-1;i++)
            {
                printf("%d %d
    ",m[i].l,m[i].r);
            }
            printf("%d %d
    ",m[jishu-1].l,n);
        }
      return 0;
    }
    

      

    set 处理  218ms

    #include<bits/stdc++.h>
    using namespace std;
    struct node
    {
        int l;
        int r;
    }m[300005];
    int exm;
    set<int>s;
    set<int>::iterator it;
    int n;
    int main()
    {
        scanf("%d",&n);
        int jishu=0;
        for(int i=1;i<=n;)
        {
            m[jishu].l=i;
            while(i<=n)
            {
                scanf("%d",&exm);
                i++;
                it=s.find(exm);
                if(it!=s.end())
                {
                    s.clear();
                    m[jishu].r=i-1;
                    jishu++;
                    break;
                }
                else
                    s.insert(exm);
            }
        }
        if(jishu==0)
            printf("-1
    ");
        else
        {
            printf("%d
    ",jishu);
            for(int i=0;i<jishu-1;i++)
            {
                printf("%d %d
    ",m[i].l,m[i].r);
            }
            printf("%d %d
    ",m[jishu-1].l,n);
        }
        return 0;
    }
    

      

  • 相关阅读:
    .NET Core技术研究-HttpContext访问的正确姿势
    .NET 5 Preview 1的深度解读和跟进
    玩转VSCode-完整构建VSCode开发调试环境
    China .NET Conf 2019-.NET技术架构下的混沌工程实践
    如何做好开发自测
    .NetCore技术研究-.NET Core迁移前的准备工作
    .NetCore技术研究-一套代码同时支持.NET Framework和.NET Core
    .NetCore技术研究-ConfigurationManager在单元测试下的坑
    统一流控服务开源:基于.Net Core的流控服务
    计组:计算机为什么有反码补码?不列公式!
  • 原文地址:https://www.cnblogs.com/hsd-/p/5164595.html
Copyright © 2011-2022 走看看