Codeforces Beta Round #94 div 1 D Numbers map+思路

本文介绍了一个关于环形数列的任务,即如何通过给定的一组整数构造一个环形数列,使得任意相邻的两个数之差恰好为1,并提供了一种实现此任务的算法思路及代码实现。
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\n");
    else
    printf("YES\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/5456140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值