zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #94 div 1 D Numbers map+思路

    D. Numbers
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?

    Input

    The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.

    Output

    Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes).

    Examples
    input
    4
    1 2 3 2
    output
    YES
    input
    6
    1 1 2 2 2 3
    output
    YES
    input
    6
    2 4 1 1 2 2
    output
    NO
    题意:给你n个数,围成环,相邻的两个数相差1;成立yes,否则no;
    思路:设得到得环为b数组,b[0]=最大值;如果map中有b[i-1]-1,取b[i-1]-1;否则取b[i-1]+1,没有就输出no;
       最后特判b数组的最前面的数和最后面的数;(不要问我怎么证明,我不会,想了下可以就拍了一发,居然a了);
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    #define true ture
    #define false flase
    using namespace std;
    #define ll __int64
    #define inf 0xfffffff
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF )  return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    map<int,int>m;
    int a[100010];
    int b[100010];
    int main()
    {
        int x,y,z,i,t=0;
        scanf("%d",&x);
        for(i=0;i<x;i++)
        {
            scanf("%d",&a[i]);
            m[a[i]]++;
            t=max(t,a[i]);
        }
        b[0]=t;
        m[t]--;
        int ans=0;
        for(i=1;i<x;i++)
        {
            if(m[b[i-1]-1])
            {
                b[i]=b[i-1]-1;
                m[b[i-1]-1]--;
            }
            else if(m[b[i-1]+1])
            {
                b[i]=b[i-1]+1;
                m[b[i-1]+1]--;
            }
            else
            {
                ans=1;
                break;
            }
        }
        if(b[0]-b[x-1]!=1)
        ans=1;
        if(ans)
        printf("NO
    ");
        else
        printf("YES
    ");
        return 0;
    }
  • 相关阅读:
    微软系统及开发工具官方下载地址
    Office2010文档的MIME类型
    Android开发中Activity的生命周期
    DWZ框架POST方式提交Form提示JSON文件下载
    JAVA开发环境搭建
    FastJSON解析.Net生成的JSON字符串
    SQL Server 2008 如何更改服务器身份验证模式
    如何快速解决IIS中发布的网站不能访问。
    QlickView
    Lamabda表达式
  • 原文地址:https://www.cnblogs.com/jhz033/p/5456140.html
Copyright © 2011-2022 走看看