Numbers(CodeForces-128D)【思维/list】
题目链接:https://vjudge.net/problem/CodeForces-128D
题意:给出一组数,要求将这些数排列成一个环,满足每相邻两个数的差值为1,问能否完成。
思路:先取出最小的数min,作为环中一点,则如果能够完成排列,该数两侧的数字即为min+1,如果min有多个,则放在min+1旁边,重复这样的过程,使用list可以很容易地完成。
代码如下:
#include<cstdio>
#include<list>
#include<iostream>
#include<cmath>
#include<unordered_map>
using namespace std;
int n,arr[],book[];
unordered_map<int,int> mp;
int main(){
scanf("%d",&n);
int ha=,minVal=;
for(int i=;i<=n;i++){
scanf("%d",&arr[i]);
minVal=min(minVal,arr[i]);
if(mp[arr[i]]==)
mp[arr[i]]=++ha;
book[mp[arr[i]]]++;
}
int ans=;
list<int> lt;
lt.push_front(minVal);
book[mp[minVal]]--;
int tmpFro=minVal,tmpBac=minVal,nn=n-;
while(){
if(book[mp[tmpFro+]]){
book[mp[tmpFro+]]--;
lt.push_front(tmpFro+);
nn--;
if(book[mp[tmpFro]]){
book[mp[tmpFro]]--;
lt.push_front(tmpFro);
nn--;
}
else tmpFro++;
}
if(book[mp[tmpBac+]]){
book[mp[tmpBac+]]--;
lt.push_back(tmpBac+);
nn--;
if(book[mp[tmpBac]]){
book[mp[tmpBac]]--;
lt.push_back(tmpBac);
nn--;
}
else tmpBac++;
}
else break;
}
if(nn==&&abs(*lt.begin()-*lt.rbegin())==){
printf("YES");
}
else printf("NO");
return ;
}
By xxmlala
Numbers(CodeForces-128D)【思维/list】的更多相关文章
- Magic Numbers CodeForces - 628D
Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...
- CodeForces 128D Numbers 构造
D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- Really Big Numbers CodeForces - 817C (数学规律+二分)
C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
- The Contest CodeForces - 813A (思维)
Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
- D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)
http://codeforces.com/contest/851/problem/D 分区间操作 #include <cstdio> #include <cstdlib> # ...
- AC日记——Little Elephant and Numbers codeforces 221b
221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...
随机推荐
- RISC-V riscv64-unknown-elf
riscv64-unknown-elf 为 RISC-V指令集的交叉编译工具 以下环境在Liunx ubuntu x86_64 环境下进行,下面示例以生成32位文件为目标来操作使用. screen / ...
- legend3---14、所有增删改的前置操作可以是什么
legend3---14.所有增删改的前置操作可以是什么 一.总结 一句话总结: 先查询数据有没有,这样既起到了服务端验证的功能,也避免了很多增删改可能的错误 1.用户收藏视频,收藏课程有必要建立用户 ...
- the requested PHP extension dom is missing from your system
composer 出错 the requested PHP extension dom is missing from your system 解决办法 yum install php70w ...
- Flume-自定义 Sink
Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统.或者被发送到另一个 Flume Agent. Sink 是完全事务性的. 在从 Channel 批 ...
- linux高可用集群(HA)原理详解
高可用集群 一.什么是高可用集群 高可用集群就是当某一个节点或服务器发生故障时,另一个节点能够自动且立即向外提供服务,即将有故障节点上的资源转移到另一个节点上去,这样另一个节点有了资源既可以向外提供服 ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_13-删除页面-前端-Api调用
增加删除链接 <el-button size="small" type="text" @click="del(page.row.pageId)& ...
- selenium历史版本下载
python历史版本下载 https://pypi.org/project/selenium/#history java历史版本下载 http://selenium-release.storage. ...
- JavaScript new对象的四个过程
new 一个对象 function Person(name, age) { this.name = name; this.age = age; } var person = new Person(&q ...
- Masonry问题
1. Masonry的布局问题: 参考博客: https://github.com/huang303513/UILayoutOfiOS 2. Masonry的自适应行高问题: 参考博客:https:/ ...
- iOS-SVPullToRefresh下拉刷新,上拉加载(转)
https://github.com/Sephiroth87/ODRefreshControl 类似刷新控件,类似qq动画的那种刷新. 一.下载第三方库 https://github.com/samv ...