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 ...
随机推荐
- jquer属性 offset、position、scrollTop
尺寸操作 1.获取宽高 a) jq对象.height/width () :只有获取高度/宽度 尺寸,不包括padding和margin 和 border 2.设置宽度 ...
- 从Maven中央仓库下载jar包
1. Maven中央仓库 Maven中央仓库存放着比较新版本比较全的 jar 包 仓库网址:https://mvnrepository.com/ 2. 使用Maven中央仓库的 jar 包 (1) 在 ...
- 文本处理三剑客之awk
简介 awk是一种处理文本文件的语言,是一个强大的文本编辑工具.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分在进行各种分析处理. gawk 用法:gawk [optio ...
- Spring事务知识点
事务的传播属性 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中.// 最常用,@Transactional注解默认 PROPAGA ...
- RGB-D(深度图像) & 图像深度
RGB-D(深度图像) 深度图像 = 普通的RGB三通道彩色图像 + Depth Map 在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...
- 认识理解Java中native方法(本地方法)
Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能. 可 ...
- maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
对Pom文件进行配置(亲自尝试,已成功解决) <build> <plugins> <plugin> <groupId>org.apache.maven. ...
- linux中如何配置vim的别名为vi?
答: 向~/.bashrc中添加如下内容: alias vi=vim
- android指纹识别认证实现
Android从6.0系统支持指纹认证功能 启动页面简单实现 package com.loaderman.samplecollect.zhiwen; import android.annotation ...
- MySQLUNION_连接两个以上的 SELECT 语句的结果组合到一个结果集合
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 描述 MySQL UNION 操作符用于连接两个以上的 SE ...