Team Queue POJ - 2259
题意:有n个小组要进行排队,每个小组中有若干个人。当一个人来到队伍时,如果队伍中已经有自己小组的成员,他就直接插队排在自己小组成员的后面,否则就站在队伍的最后面。给定不超过2∗10^5个入队指令(编号为X的人来到队伍)和出队指令(队头的人出队),输出出队的顺序。
输入:
多组输入。第一行是小组个数n(1<=n<=1000),接下来n行每行包括每组人数k(k<=1000)和成员编号x,范围0~999999,n=0时结束。
后面是指令行,有三种不同的指令:
ENQUEUE x 将元素x输入到团队队列中
DEQUEUE 处理第一个元素并将其从队列中移除
STOP 该组样例结束
输出:
第一行打印Scenario #cnt,表示第cnt组样例,然后,打印每个出列的元素,最后打印一个空行。
思路:使用n个队列来记录每个小组进入队伍中的成员,再额外使用一个队列记录队伍中存在的小组编号,同时用vis数组记录每个小组是否在队伍中,map用来记录成员编号x对应的小组编号。不要忘了上面三个的初始化问题。入队时特判加入到队伍最后的情况,出队时记得输出并特判队伍第一个小组当前为空的情况。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//#define double long double
#define sys system("pause");
const double inf = 0x3f3f3f3f;
const int maxn = 2010;
const int mod = 998244353;
const double PI = acos(-1.0);
const double eps = 1e-9;
int n, k, x, cnt;
queue<int>qu[1010]; //qu[0]存小组编号
bool vis[1010];
map<int, int>mp;
char s[10];
int main() {
cnt = 0;
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
for (int i = 0; i <= n; i++) {
while (!qu[i].empty()) qu[i].pop();
}
memset(vis, 0, sizeof(0));
mp.clear();
for (int i = 1; i <= n; i++) {
scanf("%d", &k);
while (k--) {
scanf("%d", &x);
mp[x] = i;
}
}
printf("Scenario #%d\n", ++cnt);
while (scanf("%s", s) != EOF) {
if (s[0] == 'E') {
scanf("%d", &x);
int p = mp[x];
if (!vis[p]) {
qu[0].push(p);
vis[p] = 1;
}
qu[p].push(x);
}
else if (s[0] == 'D') {
int p = qu[0].front();
printf("%d\n", qu[p].front());
qu[p].pop();
if (qu[p].empty()) {
vis[p] = 0;
qu[0].pop();
}
}
else break;
}
printf("\n");
}
}
Game with string CodeForces - 1104B
题意:两个人在玩字符串消除游戏,轮到某个人时需要从字符串中选择两个连续的相同字母并消除,两个人都发挥最佳,先不能消除的人输。
输入:一行字符串,长度不超过100000。
输出:如果第一个玩家赢了,输出Yes,否则输出No。
思路:遍历每个字符时,如果栈非空且栈顶元素和当前遍历到的元素相同,则对栈进行pop()操作,并记录操作次数,否则当前元素入栈。最后判断操作次数是否为奇数即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//#define double long double
#define sys system("pause");
const double inf = 0x3f3f3f3f;
const int maxn = 2010;
const int mod = 998244353;
const double PI = acos(-1.0);
const double eps = 1e-9;
char s[100010];
stack<char>st;
int cnt;
int main() {
while (scanf("%s", s) != EOF) {
int len = strlen(s);
cnt = 0;
while (!st.empty()) st.pop();
for (int i = 0; i < len; i++) {
if (!st.empty() && s[i] == st.top()) {
cnt++;
st.pop();
}
else {
st.push(s[i]);
}
}
if (cnt % 2) {
printf("Yes\n");
}
else printf("No\n");
}
}
Unusual Competitions CodeForces - 1323C
题意:输入字符串长度及字符串,该字符串都由左右括号’(’,’)‘组成。对于某个区间可进行“重新排序”操作使得其中字符为任意顺序,每次操作消耗时间为区间长度,求使得整个字符串变为正则括号需要的最短时间。
思路:依次遍历每个字符,若当前字符为左括号’(’,直接入栈并记录前左括号个数;若当前字符为右括号’)’,判断栈是否非空同时栈顶是否为左括号,满足则弹出栈顶,意味着包含这两个左右括号在内的闭区间不需要“重新排序”操作,否则说明该右括号所在位置即为下次操作的起点位置,直到遍历到左右括号数量相同时即为操作区间终点,记录区间长度。最后记得特判左右括号的数量是否相同。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
//#define double long double
const int inf = 0x3f3f3f3f;
const int maxn = 100010;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-6;
stack<char>st;
int n, ans = 0, cnt1 = 0, cnt2 = 0, p;
char s[1000010];
int main() {
scanf("%d", &n);
scanf("%s", s);
bool flag = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
cnt1++;
st.push('(');
}
else {
cnt2++;
if (!st.empty() && st.top() == '(') st.pop();
else {
st.push(')');
if (!flag) p = i, flag = 1;
}
}
if (cnt1 == cnt2 && !st.empty()) {
ans += i - p + 1; flag = 0;
while (!st.empty()) st.pop();
}
}
if (cnt1 != cnt2) {
printf("-1");
return 0;
}
else printf("%d", ans);
}