Codeforces Beta Round #94 div 1 D Numbers map+思路
2 seconds
256 megabytes
standard input
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?
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.
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).
4
1 2 3 2
YES
6
1 1 2 2 2 3
YES
6
2 4 1 1 2 2
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 = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
map<int,int>m;
int a[];
int b[];
int main()
{
int x,y,z,i,t=;
scanf("%d",&x);
for(i=;i<x;i++)
{
scanf("%d",&a[i]);
m[a[i]]++;
t=max(t,a[i]);
}
b[]=t;
m[t]--;
int ans=;
for(i=;i<x;i++)
{
if(m[b[i-]-])
{
b[i]=b[i-]-;
m[b[i-]-]--;
}
else if(m[b[i-]+])
{
b[i]=b[i-]+;
m[b[i-]+]--;
}
else
{
ans=;
break;
}
}
if(b[]-b[x-]!=)
ans=;
if(ans)
printf("NO\n");
else
printf("YES\n");
return ;
}
Codeforces Beta Round #94 div 1 D Numbers map+思路的更多相关文章
- 图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces
		
题目传送门 /* 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) */ #include <cstdio> #include <cs ...
 - BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues
		
题目传送门 /* BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 只要撑过这个时间就能win,否则lose */ #include <c ...
 - Codeforces Beta Round #94 div 2 B
		
B. Students and Shoelaces time limit per test 2 seconds memory limit per test 256 megabytes input st ...
 - Codeforces Beta Round #94 (Div. 1 Only)B. String sam
		
题意:给你一个字符串,找第k大的子字符串.(考虑相同的字符串) 题解:建sam,先预处理出每个节点的出现次数,然后处理出每个节点下面的出现次数,然后在dfs时判断一下往哪边走即可,注意一下num会爆i ...
 - Codeforces Beta Round #94 div 2 C Statues dfs或者bfs
		
C. Statues time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
 - Codeforces Beta Round #76 (Div. 2 Only)
		
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
 - Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
		
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
 - Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
		
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
 - Codeforces Beta Round #79 (Div. 2 Only)
		
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
 
随机推荐
- 从浏览器输入参数,到后台处理的vertx程序
			
vertx由于性能较高,逐渐变得流行.下面将一个vertx的入门案例. 添加依赖 <!-- vertx --> <dependency> <groupId>io.v ...
 - Python 列表 extend() 方法
			
描述 Python 列表 extend() 方法通过在列表末尾追加可迭代对象中的元素来扩展列表. 语法 extend() 方法语法: L.extend(iterable) 参数 iterable -- ...
 - this与$scope
			
最近在Angular项目中遇到关于controller内使用$scope&this 暴露数据的问题,下面来分析一下: "controller as" 是Angular在1. ...
 - Ajax棵
			
ajax 1.什么是ajax?(异步请求,局部刷新) ajax是一个改善用户体验的技术,实质上是利用浏览器端ajax对象()向服务器发送异步(ajax对象在向服务器发送请求的时候,用户可以继续其他操作 ...
 - Lintcode: Kth Prime Number (Original Name: Ugly Number)
			
Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...
 - img height test
 - VueJS 数据驱动和依赖追踪分析
			
之前关于 Vue 数据绑定原理的一点分析,最近需要回顾,就顺便发到随笔上了 在之前实现一个自己的Mvvm中,用 setter 来观测model,将界面上所有的 viewModel 绑定到 model ...
 - linux常用命令:telnet 命令
			
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...
 - linux服务器---安装samba
			
安装samba 1.检测samba是否安装,如果没有,那么可以使用yum来安装.至少需要安装3个软件:samba,samba-client.samba-common [root@localhost p ...
 - HexDump.java解析,android 16进制转换
			
HexDump.java解析android 16进制转换 package com.android.internal.util; public class HexDump { private final ...