题目链接: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】的更多相关文章

  1. Magic Numbers CodeForces - 628D

    Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...

  2. CodeForces 128D Numbers 构造

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

  3. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  4. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  5. Really Big Numbers CodeForces - 817C (数学规律+二分)

    C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. The Contest CodeForces - 813A (思维)

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...

  7. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  8. Queue CodeForces - 353D (思维dp)

    https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...

  9. 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> # ...

  10. AC日记——Little Elephant and Numbers codeforces 221b

    221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...

随机推荐

  1. 使用harborv1.8.0-rc1 搭建docker私有镜像仓库

    概述 搭建一个私有仓库 harbor介绍 harbor是一个开源的docker容器仓库,由下面几个组件组成 + proxy:用来接收docker客户端和浏览器端的请求,并且把请求转发给后端的服务 + ...

  2. python 获取主机名称和ip地址

    python2.7 #!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - # This program is ...

  3. Android studio 导入项目报 Error:Cause: peer not authenticated 异常

    修改build.gradle文件(project级的) 一.dependencies { classpath 'com.android.tools.build:gradle:1.0.1'}将class ...

  4. Jmeter Web 性能测试入门 (七):Performance 测试中踩过 Jmeter 的坑

    脚本运行的过程中,大量request抛error,但没有地方能够查看request是因为什么error的. 原因:Jmeter默认禁掉了运行过程中每个request的具体response信息收集,只保 ...

  5. 感知机算法及BP神经网络

    简介:感知机在1957年就已经提出,可以说是最为古老的分类方法之一了.是很多算法的鼻祖,比如说BP神经网络.虽然在今天看来它的分类模型在很多数时候泛化能力不强,但是它的原理却值得好好研究.先学好感知机 ...

  6. mysql—并发控制及事务

    并发控制 实现的并发访问的控制技术是基于锁: 锁分为表级锁和行级锁,MyISAM存储引擎不支持行级锁:InnoDB支持表级锁和行级锁: 锁的分类有读锁和写锁,读锁也被称为共享锁,加读锁的时候其他的人可 ...

  7. 第11组 Beta冲刺(5/5)

    第11组 Beta冲刺(5/5)   队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/12031050.html 作业博客 https://edu. ...

  8. Mac下持续集成-jenkins设置密码及启动

    什么情况呢,现在想起来重新启动jenkins时发现,一切都要从头开始... 输入原始密码: 提示密码在:/var/root/.jenkins/secrets/initialAdminPassword ...

  9. Mapper抽象类参数

    Mapper< Object, Text, Text, IntWritable> Mapper< Text, Text, Text, Text> Mapper< Text ...

  10. C#与C++数据类型对应表(搜集整理一)

    C#与C++数据类型对应表(搜集整理一) C#与C++数据类型对应表   C#调用DLL文件时参数对应表 Wtypes.h 中的非托管类型 非托管 C 语言类型 托管类名 说明 HANDLE void ...