Codeforces 821C Okabe and Boxes 思维模拟
2017-07-07 13:23
585 查看
传送门:codeforces 821C
题意:给定两种操作,一种是‘add x’,将x放到栈顶,一种是remove,将栈顶移除,但是移除的顺序必须是从1到n,操作过程中可以将栈内元素顺序重排,问至少重排几次才能满足移除的顺序。
思路:只想到每次不满足就暴力排序。。虽然明知道会超时还是勇敢的交了一发。
正解:我们完全可以将排序的过程省去,每次要是需要排序了,我们就可以将栈清空,以后再remove的时候如果栈是空的就说明可以直接remove掉,因为题目保证了要remove的元素一定已经add进栈了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int q[300005];
int main()
{
string s;
int n,id,t=1,ans=0,top=0;
cin>>n;
n<<=1;
while(n--)
{
cin>>s;
if(s=="add")
{
cin>>id;
q[top++]=id;
}
else
{
if(!top){t++;continue;};
if(q[top-1]==t)
{
t++;
top--;
}
else
{
ans++;//
top=0;
t++;
}
}
}
cout<<ans;
return 0;
}
题意:给定两种操作,一种是‘add x’,将x放到栈顶,一种是remove,将栈顶移除,但是移除的顺序必须是从1到n,操作过程中可以将栈内元素顺序重排,问至少重排几次才能满足移除的顺序。
思路:只想到每次不满足就暴力排序。。虽然明知道会超时还是勇敢的交了一发。
正解:我们完全可以将排序的过程省去,每次要是需要排序了,我们就可以将栈清空,以后再remove的时候如果栈是空的就说明可以直接remove掉,因为题目保证了要remove的元素一定已经add进栈了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int q[300005];
int main()
{
string s;
int n,id,t=1,ans=0,top=0;
cin>>n;
n<<=1;
while(n--)
{
cin>>s;
if(s=="add")
{
cin>>id;
q[top++]=id;
}
else
{
if(!top){t++;continue;};
if(q[top-1]==t)
{
t++;
top--;
}
else
{
ans++;//
top=0;
t++;
}
}
}
cout<<ans;
return 0;
}
相关文章推荐
- CodeForces - 758C Unfair Poll (模拟+暴力+思维)
- Codeforces 272D Dima and Two Sequences【思维+模拟】
- CodeForces - 767A Snacktower(模拟+思维)
- Codeforces 954C - Matrix Walk(思维 + 模拟)
- Codeforces-868B ,C 模拟,思维。。
- codeforces 691C Exponential notation(思维 + 比较精妙的模拟)
- codeforces 496C Removing Columns (模拟,思维)
- CodeForces 811B——Vladik and Complicated Book ——思维,模拟
- codeforces 845D Driving Test (模拟+思维)
- codeforces-884D Boxes And Balls(思维、三叉哈夫曼树)
- CodeForces - 821C Okabe and Boxes(思维)
- Codeforces 155D Colliders【思维+模拟】
- Codeforces 821C Okabe and Boxes 题解
- CodeForces - 876D Sorting the Coins 模拟+思维
- Codeforces 469C 24 Game【思维+模拟】
- Codeforces 770D Draw Brackets!【模拟+思维】
- Codeforces 586B Laurenty and Shop【思维+模拟】
- Codeforces 746C Tram 模拟+思维
- Codeforces 492D Vanya and Computer Game【模拟+循环节+思维】好题~
- codeforces-305A Strange Addition(思维+模拟)