zoukankan      html  css  js  c++  java
  • CodeForces

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

    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

    这题找规律,从第一个数开始向后搜索,当有一个数字重复时则记录该位置,再从下一个数字开始向后重新搜索。可使用STL中的set函数

    AC代码

    #include<stdio.h>
    #include<set>
    using namespace std;
    int pos[300005];
    set<int>a;
    int main()
    {
    	int n,m=0,b;
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&b);
    		if(a.count(b)==0)//检查b是否出现过
    		{
    			a.insert(b);//在末尾插入b
    		}
    		else
    		{
    			pos[m]=i;
    			m++;
    			a.clear();//清除a中数据
    		}
    		
    	}
    	if(m==0)
    	{
    		printf("-1
    ");
    	}
    	else
    	{
    		pos[m-1]=n;
    		printf("%d
    ",m);
    		printf("1 %d
    ",pos[0]);
    		for(int i=0;i<m-1;i++)
    		{
    			printf("%d %d
    ",pos[i]+1,pos[i+1]);
    		}
    	}
    	return 0;
    } 

    最近刚学的STL,本来想用循环来搜索重复数字的,结果超时,不过可作为思路

    #include<stdio.h>
    int a[300005],pos[300005];
    int main()
    {
    	int n,m=0;
    	pos[m]=0;
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%d",&a[i]);
    		for(int j=pos[m]+1;j<i;j++)
    		{
    			if(a[j]==a[i])
    			{
    				m++;
    				pos[m]=i;
    			}
    		}
    	}
    	if(m==0)
    	{
    		printf("-1
    ");
    	}
    	else
    	{
    		pos[m]=n;
    		printf("%d
    ",m);
    		printf("1 %d
    ",pos[1]);
    		for(int i=1;i<m;i++)
    		{
    			printf("%d %d
    ",pos[i]+1,pos[i+1]);
    		}
    	}
    	return 0;
    } 





  • 相关阅读:
    Linux kernel 拒绝服务漏洞
    WordPress Checkout插件跨站脚本漏洞和任意文件上传漏洞
    SpringMVC指定webapp的首页
    maven-jetty插件配置时,webdefault.xml的取得和修改
    pom.xml报错 : Missing artifact org.apache.shiro:shiro-spring:bundle:1.2.5
    maven+SSM+junit+jetty+log4j2环境配置的最佳实践
    “不让工具类能够实例化”的做法是适合的
    静态域/域的初始化、静态代码块/构造代码块的实行、构造方法的调用 顺序
    Java Collection Framework 备忘点
    JVM备忘点(1.8以前)
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124484.html
Copyright © 2011-2022 走看看