题目链接
C. Nauuo and Cards
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Nauuo is a girl who loves playing cards.
One day she was playing cards but found that the cards were mixed with some empty ones.
There are nn cards numbered from 11 to nn, and they were mixed with another nn empty cards. She piled up the 2n2n cards and drew nn of them. The nn cards in Nauuo's hands are given. The remaining nn cards in the pile are also given in the order from top to bottom.
In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.
Nauuo wants to make the nn numbered cards piled up in increasing order (the ii-th card in the pile from top to bottom is the card ii) as quickly as possible. Can you tell her the minimum number of operations?
Input
The first line contains a single integer nn (1≤n≤2⋅105) — the number of numbered cards.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤n0≤ai≤n) — the initial cards in Nauuo's hands. 00 represents an empty card.
The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (0≤bi≤n0≤bi≤n) — the initial cards in the pile, given in order from top to bottom. 00represents an empty card.
It is guaranteed that each number from 11 to nn appears exactly once, either in a1..na1..n or b1..nb1..n.
Output
The output contains a single integer — the minimum number of operations to make the nn numbered cards piled up in increasing order.
Examples
input
Copy
3 0 2 0 3 0 1
output
Copy
2
input
Copy
3 0 2 0 1 0 3
output
Copy
4
input
Copy
11 0 0 0 5 0 0 0 4 0 0 11 9 2 6 0 8 1 7 0 3 0 10
output
Copy
18
Note
Example 1
We can play the card 22 and draw the card 33 in the first operation. After that, we have [0,3,0][0,3,0] in hands and the cards in the pile are [0,1,2][0,1,2] from top to bottom.
Then, we play the card 33 in the second operation. The cards in the pile are [1,2,3][1,2,3], in which the cards are piled up in increasing order.
Example 2
Play an empty card and draw the card 11, then play 11, 22, 33 in order.
题意:有n个空白牌,n个有数字牌,手上拿n张,桌子上放n张,操作:每次可将手中的任意一张放在桌子那一摞牌的最小边,然后将桌子上那一摞牌的最上边一张拿在手中,问:需要操作最少多少次可将桌子上的牌从顶部到底部依次递增排列。
题解:我是用二分位置来做的。
#include
#include
#include
#include
#include
using namespace std;
const int N=2e5+5;
int a[N],b[N];
int n,leap=0,h=0;
sets1,s2;
struct fun{
int x,id;
};
struct cmp{
bool operator()(fun &x,fun &y)const{
return x.id>y.id;
}
};
priority_queue,cmp>qq;
int ans=9999999,cut;
bool C(int r,int mid,int pos){
int h=0,p=0;
s2.clear(); while(!qq.empty()) qq.pop(); // 记得清空队列
for(int i=mid+1;i<=r;i++) { if(b[i]) s2.insert(b[i]); qq.push({b[i],p++});}
for(int i=1;i<=mid;i++) { cut++; qq.push({0,p++});}
while(true){
if(s2.count(pos)) return false;
else{
cut++;
fun t=qq.top();
qq.pop();
if(t.x) s2.erase(t.x);
}
pos++;
if(pos>n) break;
}
return true;
}
void work(int l,int r,int pos,int m){
while(r>=l){
int mid=l+r>>1;
// printf("mid == %d\n",mid);
cut=0;
if((C(m,mid,pos))){
r=mid-1;
ans=min(ans,cut);
}
else l=mid+1;
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==1) leap=1;
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
if(b[i]==1) h=i;
}
if(!leap){
int flag=0;
for(int i=h+1;i<=n;i++){
if((b[i]-1)==b[i-1]) continue;
flag=1;
break;
}
if(flag){
work(0,n,1,n);
printf("%d\n",ans);
}
else{
int d=n-h+1;
if(d==n)
printf("0\n");
else{
if(C(h-1,0,d+1)){ // 出现 0 0 1 2 3 和 3 0 1 要分类讨论
printf("%d\n",cut); // 3 0 1 是可以直接从上边数列中取出放到下边
} // 0 0 1 2 3 是不可以
else{
work(0,n,1,n);
printf("%d\n",ans);
}
}
}
}
else{
work(0,n,1,n);
printf("%d\n",ans);
}
return 0;
}