zoukankan      html  css  js  c++  java
  • CodeForces 128D Numbers 构造

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

    Sample test(s)
    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张牌,这N张牌需要围成一个圈,要求牌和他相邻的牌最多相差1
    问可不可行

    思路:大概就是缩吧,比如 121可以看成1,12121也可以看成1。
    在草稿本上画画就可以知道,最后状态是类似于12这种环
    当然,出现断链的时候,当然是不可行的啦
    于是就乱搞吧= =
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100001
    const int inf=0x7fffffff;   //无限大
    int a[maxn];
    int t[maxn];
    int main()
    {
        int n;
        while(cin>>n){
        cin>>a[0];
        int mi=a[0];
        for(int i=1;i<n;i++)
        {
            cin>>a[i];
            mi=min(a[i],mi);
        }
        sort(a,a+n);
        int flag=1;
    
        for(int i=0;i<n;i++)
        {
            if(a[i]-mi>=maxn)
            {
                flag=0;
                break;
            }
            t[a[i]-mi]++;
        }
    
        if(flag==1)
        for(int i=1;i<a[n-1]-mi+1;i++)
        {
            //cout<<t[i]<<endl;
            if(t[i]==0)
            {
                flag=0;
                break;
            }
            if(i!=a[n-1]-mi&&t[i]<=t[i-1])
            {
                flag=0;
                break;
            }
            if(i==a[n-1]-mi&&t[i]!=t[i-1])
            {
                flag=0;
                break;
            }
            t[i]-=t[i-1];
        }
        if(flag==0)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
        }
        return 0;
    }
     


  • 相关阅读:
    ReactiveCocoa 谈谈RACMulticastConnection
    ReactiveCocoa 谈谈concat
    Swift 一些环境配置
    hpple 简单使用
    Swift 学习手记1,pod 的 类库使用
    [转]SQL语句:Group By总结
    Jquery VailDate初探
    C#RSA加密解密详解
    电子印章制作管理系统 -升级版本
    tensorflow 实现的第一个目标检测模型
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4230857.html
Copyright © 2011-2022 走看看