Codeforces 911E - Stack Sorting
思路:
用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则加入栈 n 到 temp 的值,如果需要加入的数之前已经出现过,答案则不存在。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define meme(a,b) memset(a,b,sizeof(a)) const int N=2e5+;
bool vis[N];
int a[N];
stack<int>s;
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n,k;
cin>>n>>k;
int temp=;
for(int i=;i<=k;i++){
cin>>a[i];
vis[a[i]]=true;
s.push(a[i]);
while(s.size()&&s.top()==temp){
s.pop();
temp++;
}
}
while(s.size()&&s.top()>temp||n+>temp){
int t;
if(s.size())t=s.top();
else t=n+;
for(int i=t-;i>=temp;i--){
if(vis[i]){
cout<<-<<endl;
return ;
}
else s.push(i),a[++k]=i,vis[i]=true;
}
while(s.size()&&s.top()==temp){
s.pop();
temp++;
}
}
if(s.size())cout<<-<<endl;
else for(int i=;i<=n;i++)cout<<a[i]<<' ';
cout<<endl;
return ;
}
Codeforces 911E - Stack Sorting的更多相关文章
- CF911E Stack Sorting
洛谷题目链接:CF911E Stack Sorting Codeforces题目链接:Stack Sorting 题意翻译 给你一排列的一部分,让你补全整个排列使其字典序最大并且经过一个栈调整顺序之后 ...
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...
- Codeforces 606-C:Sorting Railway Cars(LIS)
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 876 D. Sorting the Coins
http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
- codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C
思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...
- Codeforces 1240B. Sequence Sorting
传送门 分析题目发现如果把某个数 $x$ 往左移,那么之后所有小于 $x$ 的数也都要往左移 如果把 $x$ 往右移,那么之后所有大于 $x$ 的数也都要往右移 考虑我们首先一定有一个操作 $n$ 次 ...
随机推荐
- chrome浏览器使用
1.如何打开多个历史网页.这个需求是这样的,有时候开了多个网页查找资料,但是又还没有做完,然后又需要重启电脑.显然重启电脑后再开启浏览器,一般都是显示浏览器的主页了,上次开的那些网页全部在历史记录里面 ...
- MySQL从删库到跑路_高级(五)——触发器
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.触发器简介 1.触发器简介 触发器是和表关联的特殊的存储过程,可以再插入,删除或修改表中的数据时触发执行,比数据 ...
- Linux服务器---本地yum
本地yum 本地yum可以实现各种包的快速安装,避免漫长的下载过程 1.找一个centos的安装包,将其挂载的系统中 [root@localhost ~]# mount –t iso9660 –loo ...
- Linux基础命令---ln
ln 为指定的目录或者文件创建链接,如果没有指定链接名,那么会创建一个和源文件名字一样的链接. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fed ...
- Redis 如何保持和MySQL数据一致【二】
需求起因 在高并发的业务场景下,数据库大多数情况都是用户并发访问最薄弱的环节.所以,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问MySQL等数据库. 这个业务场景,主要 ...
- 01: awk常用
1.1 awk基本使用 1.找出当前登录用户数量 [root@localhost ~]# w 14:09:07 up 48 min, 2 users, load average: 0.00, 0.01 ...
- 安装OpenOffice
附件: OpenOffice 1.下载解压 tar -zxvf Apache_OpenOffice_4.1.3_Linux_x86-64_install-rpm_zh-CN.tar.gz 2.安装 / ...
- bzoj 2654 tree - 二分法 - 最小生成树
给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行,每行 ...
- C++ shared_ptr的用法
一. http://www.cnblogs.com/welkinwalker/archive/2011/10/20/2218804.html 二.http://www.cnblogs.com/Tian ...
- 通过例子来理解python闭包。
闭包:就是内部函数对enclosing作用域的变量进行引用.(可先参考python函数作用域LEGB) 通过一个例子体会 def func_150(val): passline = 90 if val ...