CF821C Okabe and Boxes

思路:

模拟。不一致的时候直接清空栈即可。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 int main()
 6 {
 7     vector<int> v;
 8     int n, cnt = 1, ans = 0;
 9     string s;
10     cin >> n;
11     for (int i = 0; i < 2 * n; i++)
12     {
13         cin >> s;
14         if (s[0] == ‘a‘)
15         {
16             int tmp;
17             scanf("%d", &tmp);
18             v.push_back(tmp);
19         }
20         else
21         {
22             if (!v.empty())
23             {
24                 if (v[v.size() - 1] == cnt) v.pop_back();
25                 else
26                 {
27                     v.clear();
28                     ans++;
29                 }
30             }
31             cnt++;
32         }
33     }
34     printf("%d\n", ans);
35     return 0;
36 }
时间: 2024-08-26 08:13:16

CF821C Okabe and Boxes的相关文章

Codeforces 821C Okabe and Boxes(模拟)

题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表示已经排过序了,如果下一次remove时栈为空,说明已经排序过且没有新的箱子放入,因为题目确保在需要删除之前添加每个箱子,所以肯定栈顶的箱子是我们所需要的.如果栈不为空则判断栈顶箱子序号是否是我们需要的,不是则重新排序,用清空栈来表示. 这里用数字模拟了栈. 1 #include<iostream>

CF821 C. Okabe and Boxes 栈模拟

Link 题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次. 思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操作序列根本无法做到有序出栈.所以碰到不合顺序的数,将栈中所有元素出栈一遍即可. /** @Date : 2017-07-04 15:21:52 * @FileName: C.cpp * @Platform: Windows * @Author : Lweleth ([email protected]

Codeforces 821C Okabe and Boxes

题意: 给定一个n,然后有2n个指令,分别是add x, remove, add x 就是将x加入到栈中, remove 就是从栈顶移除, 然后移除的元素一定要有序, 不然就需要resort(重排)一次, 问最少需要重排多少次. 分析: 可以看出,每次只有栈顶元素和应该移除的元素不符合时候才需要重排, 所以重排后可以想象成栈中所有的元素都在他最佳的位置,那么我们就可以忽视掉这些重排后的元素, 把栈清空然后继续执行操作, 这样可以做到O(n)的复杂度. 另外如果栈顶元素正是需要移除的元素, 那么我

Codeforces Round #420 (Div. 2) A-E

本来打算划划水洗洗睡了,突然听到这次的主人公是冈部伦太郎 石头门(<steins;gate>)主题的比赛,岂有不打之理! 石头门真的很棒啊!人设也好剧情也赞曲子也特别好听. 推荐http://music.163.com/#/m/song?id=26259014&userid=115264555 (强行跑题) Okabe and Future Gadget Laboratory O(n^4)暴力妥妥的 1 #include<iostream> 2 #include<al

Codeforces Round #420 (Div. 2)

/****************************************************************************************************************** 因为发现不敲题会变蠢...所以没事还是做两道题吧.... 很久没做过题然后发现C和E全都是因为没用LL给卡住了......  我真的是太蠢了 ***************************************************************

Codeforces Round #420 C

Okabe and Boxes 题意:有2个操作,add x表示往栈里加入一个数x,remove表示从栈里拿出一个数,若要使得出栈的顺序为递增的,那么至少要对栈里面的元素进行多少次重新排序 思路:stack模拟栈,优先队列模拟出栈,每次记录当前最顶上的元素top,用来和当前出栈的数比较,如果不相等,则进行排序,并标记f表示已经是排好序的位置,若相等,则出栈,然后更新top,注意更新top的时候是取优先队列里的还是stack里的数,若当前的位置大于f,则取栈里的元素更新top,否则取优先队列里的数

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

[LeetCode] Remove Boxes 移除盒子

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

UVa 12657 Boxes in a Line(应用双链表)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th