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$ 次 ...
随机推荐
- javascript 判断数据类型
Object.prototype.toString.call(asddfff) //报错asddfff没被定义Object.prototype.toString.call(undefined) //& ...
- EOJ Monthly 2018.11 猜价格 (模拟)
分三种情况: 1.k=1.此时每次都说反话,反着二分即可. 2.1<k <= n.那么在前n次问答中一定会出现一次错误,通过不断输出1找出那个错误发生的位置(若回答是>那这就是错误) ...
- 20154312 曾林 Exp4恶意软件分析
写在前面 如果把恶意软件比作罪犯的话,怎么看这次实验? 实验目的:以后能够在茫茫人海中找到罪犯. 实验过程:现在以及抓到了一个罪犯,把他放到茫茫人海里去,看看他和普通人有啥区别.这些区别就是罪犯的特征 ...
- za
http://www.szjs.gov.cn/bsfw/zdyw_1/zfbz/jgcx/
- linux make命令安装详解
对于GNU Make或许很多Windows开发的程序员并不是很了解,因为Windows中的很多集成开发环境(IDE)都帮我们做了这件事.但是作为一个专业从事 Linux嵌入式开发的程序员就必须要了解G ...
- 用户可在Messenger内读书并与角色互动
畅销小说作家 James Patterson 与 Facebook 合作,为新书<The Chef>在 Messenger 推出互动体验版. 10 月 30 日下午消息,据<香港经济 ...
- shell字符串操作技巧
操作字符串 -------------- Bash支持超多的字符串操作,操作的种类和数量令人惊异.但不幸的是,这些工具缺乏集中性. 一些是参数替换的子集,但是另一些则属于UNIX的expr命令.这就导 ...
- 负载均衡之-haproxy
老规矩,先介绍?复制一段? 1)HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. 2)HAProxy特别适用于那些负载特大的 ...
- 20145336《网络对抗技术》Exp6 信息搜集技术
20145336张子扬 <网络对抗技术> 信息搜集与漏洞扫描 实验内容 使用whois进行域名注册信息查询,使用nslookup进行域名查询 实现对IP地理位置的查询 使用PING.nam ...
- Python3基础 list remove 删除元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...