Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
- Take the first element of a, push it into s and remove it from a (if a is not empty);
- Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
- Remove 3 from a and push it into s;
- Remove 1 from a and push it into s;
- Remove 1 from s and append it to the end of b;
- Remove 2 from a and push it into s;
- Remove 2 from s and append it to the end of b;
- Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k qi = pi, and qk > pk). You may not swap or change any of first k elements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
Input
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the first kelements of p. These integers are pairwise distinct.
Output
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
Examples
5 3
3 2 1
3 2 1 5 4
5 3
2 3 1
-1
5 1
3
3 2 1 5 4
5 2
3 4
-1 题意:给你一个数N和一个数 k , 然后是长度为K的数组,
让你构造出一个N的全排列,使之前K项是给定的数组,并且满足这个全排列是stack-sortable
题目给了stack-sortable的定义。 思路:
可以通过折耳根stack-sortab的性质和stack的性质来完成本题。
首先我们要知道这题的一个关键点,当一个数插入到栈的条件是这个数x小于栈中所以的数。
那么我们首先对这K个数进行操作,对于每一个数p[i],先判断能不能加到栈中(判断条件是栈为空或者比栈顶小),
不能加入到栈中的就一定是符合条件的,那么是直接输出-1.
加入到栈中之后,进行弹出操作,从1开始用一个变量来维护弹出到的最大数,对于栈顶就是能弹出的就先从栈中弹出。
扫完后对剩余的栈中元素进行操作,剩余的栈中元素只所以没有被弹出是因为肯定有some比它小的数在这K个中没出现。
那么我们就把(栈中元素之间)的数倒序分别输出
然后再把前K个没有的数进行倒序输出即可。
因为要求字典序最大,所以是倒序输出这些。 具体细节见accode
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,k;
int p[maxn];
int vis[maxn]; int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>k;
repd(i,,k)
{
cin>>p[i];
vis[p[i]]++;
}
stack<int> st;
int m=inf;
int isok=;
int now=;
repd(i,,k)
{
if(p[i]+==now)
{
now++;
}else
{ if(st.empty())
{
st.push(p[i]);
}else if(st.top()>p[i])
{
st.push(p[i]);
}else
{
isok=;
break;
}
while(st.size()&&st.top()==now+)
{
st.pop();
now++;
} }
}
if(!isok)
{
cout<<-<<endl;
return ;
}
// while(!st.empty()&&st.size()!=1)
// {
// st.pop();
// }
repd(i,,k)
{
cout<<p[i]<<" ";
} while(!st.empty())
{
for (int i = st.top()-;i>=; --i)
{
if(!vis[i])
{
cout<<i<<" ";
vis[i]=;
}else
{
break;
}
/* code */
}
now=max(now,st.top());
st.pop();
} for(int i=n;i>now;i--)
{
cout<<i<<" ";
}
cout<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Stack Sorting CodeForces - 911E (思维+单调栈思想)的更多相关文章
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...
- Largest Submatrix of All 1’s(思维+单调栈)
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ...
- Mike and Feet CodeForces - 548D (单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- codeforces 547B【单调栈】
题意: 有一个长度为n的序列,序列有长度为1...n的连续子序列, 一个连续子序列里面最小的值称作这个子序列的子序列的strength, 要求出每种长度的连续子序列的最大的strength. 思路: ...
- CF911E Stack Sorting
洛谷题目链接:CF911E Stack Sorting Codeforces题目链接:Stack Sorting 题意翻译 给你一排列的一部分,让你补全整个排列使其字典序最大并且经过一个栈调整顺序之后 ...
- hdu 1506 单调栈问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目的意思其实就是要找到一个尽可能大的矩形来完全覆盖这个矩形下的所有柱子,只能覆盖柱子,不能留空 ...
随机推荐
- SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
2017-11-01 09:49:44.35 spid166 SQL Server has encountered 1 occurrence(s) of cachestore flush for th ...
- 2. svg学习笔记-svg中的坐标系统和viewbox
我是通过<SVG精髓>这本书学习的svg,说实话,这本书写的不好,或者说翻译的不好,我没有看过这本书的原版,不知道原文写的怎么样,但是翻译出来的有些句子真的很拗口.以前老师给我们API文档 ...
- 记录参加QCon2017北京站的心得
如有侵权,请告知作者删除.scottzg@126.com 很荣幸参加QCon全球软件开发大会,这里特别感谢我们部门的总经理,也是<互联网广告算法和系统实践>此书的作者王勇睿.因为他我才有这 ...
- 【Linux基础】VM使用
VM三种联网方法和原理 (1)Bridged桥接 使用VMnet0虚拟交换机,此时虚拟机相当与网络上的一台独立计算机与主机一样,拥有一个独立的IP地址,所有机器均可互访,可以联网.使用桥接方式,A,A ...
- Zookeeper运维小结--CancelledKeyException
https://www.jianshu.com/p/73eec030db86 项目中用到storm+kafka+zookeeper,在实际应用中zk和kafka常出问题,这里记录下在使用zk过程中的问 ...
- 为什么黑客都不用鼠标?你听说过Linux吗?
为什么黑客都不用鼠标?你听说过Linux吗? 微软和它的朋友们的如意算盘. 下面来看看微软的收入是怎么来的.首先,Windows系列操作系统,一个就是 800+RMB,每次升级又是几乎同样的价钱.Wi ...
- c# 链接mongDB集群实战开发3
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zuoming120/article/details/25702295 c# 链接mongDB集群 一 ...
- Spring国际化模块
1.Spring3.1.0实现原理分析(二).国际化(i18n) https://blog.csdn.net/roberts939299/article/details/69666291
- UVA10603-Fill(BFS)
Problem UVA10603-Fill Accept:1162 Submit:10693 Time Limit: 3000 mSec Problem Description There are ...
- 【转】Android动态破解微信本地数据库(EnMicroMsg.db)
最近在公司接了一个任务,需要在几百台手机上安装一个app,目的是获取微信里面的通讯录,并且定时的把他发送到我们的服务器上.当时依次尝试的如下几个方案: 1.通过群控,将好友截图发送到服务端(pytho ...