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的更多相关文章

  1. CF911E Stack Sorting

    洛谷题目链接:CF911E Stack Sorting Codeforces题目链接:Stack Sorting 题意翻译 给你一排列的一部分,让你补全整个排列使其字典序最大并且经过一个栈调整顺序之后 ...

  2. Stack Sorting CodeForces - 911E (思维+单调栈思想)

    Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...

  3. 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 ...

  4. codeforces 876 D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...

  5. Codeforces Round #335 Sorting Railway Cars 动态规划

    题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...

  6. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))

    题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...

  8. codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C

    思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...

  9. Codeforces 1240B. Sequence Sorting

    传送门 分析题目发现如果把某个数 $x$ 往左移,那么之后所有小于 $x$ 的数也都要往左移 如果把 $x$ 往右移,那么之后所有大于 $x$ 的数也都要往右移 考虑我们首先一定有一个操作 $n$ 次 ...

随机推荐

  1. Linux系统——DHCP

    DHCP定义DHCP服务是负责IP.掩码.网关地址.DNS地址等自动分发的软件服务DHCP的分配方式(1)自动分配:分配到一个IP地址后永久使用(2)手动分配:由DHCP服务器管理员专门指定IP地址( ...

  2. HDU 1392 Surround the Trees(几何 凸包模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模 ...

  3. 008-centos服务管理

  4. marquee实现跑马灯

    <!DOCTYPE html><html> <head><title>跑马灯大全</title> </head> <bod ...

  5. ac1067

    这题说的是 有n个点在 圆上等分这个圆,然后 然后计算其中任意三个点能组成的锐角三角形的个数 首先这些点能组成的三角形的个数为 n*(n-1)*(n-2)/6  接下来计算不是锐角三角形的个数 固定任 ...

  6. linux基础命令---chattr

    chattr 改变文件的属性,这个命令只有超级用户才能使用.这个指令适用于ext2.ext3.ext4.xfs.ubifs.reiserfs.jfs系统. 此命令的适用范围:RedHat.RHEL.U ...

  7. python之路----面向对象的封装特性

    封装 [封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. 广义上面向对象的封装 :代码的保护,面向对象的思想本身就是一种只让自己的对象能调用自己类中的方法 狭义上的封装 —— 面向对象的三大 ...

  8. 第一个c++泛型函数(即模板)

    先定义如下: ns.h template <typename T> // 这个关键字typename, 明显多此一举 inline void PRINT_ELEMENTS(const T& ...

  9. elasticsearch分词器ik

    1. 下载和es配套的版本 git clone https://github.com/medcl/elasticsearch-analysis-ik 2. 编译 cd elasticsearch-an ...

  10. bzoj 3223 文艺平衡树 - Splay

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3884  Solved: 2235[Submit][Sta ...