1、Codeforces 570C Replacement
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 3e5 + 10;
int n, m, sum = 0, x;
char c;
char s[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H_
scanf("%d %d %s", &n, &m, s);
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '.') {
++cnt;
if (cnt >= 2) {
++sum;
}
} else {
cnt = 0;
}
}
while (m--) {
scanf("%d %c", &x, &c);
if (c != '.' && s[x - 1] == '.') {
if (x > 1 && s[x - 2] == '.') {
--sum;
}
if (x < n + 1 && s[x] == '.') {
--sum;
}
} else if (c == '.' && s[x - 1] != '.') {
if (x > 1 && s[x - 2] == '.') {
++sum;
}
if (x < n + 1 && s[x] == '.') {
++sum;
}
}
s[x - 1] = c;
printf("%d\n", sum);
}
return 0;
}
2、Codeforces 713A Sonya and Queries
参考:http://codeforces.com/contest/713/standings
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 5e5 + 10;
int t;
int cnt[maxn];
char cmd[10], s[30];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H_
scanf("%d", &t);
while (t--) {
scanf("%s %s", cmd, s);
int len = strlen(s);
int key = 0;
for (int i = 0; i < len; ++i) {
key = key << 1 | ((s[i] - '0') & 1);
}
if (cmd[0] == '+') {
++cnt[key];
} else if (cmd[0] == '-') {
--cnt[key];
} else {
printf("%d\n", cnt[key]);
}
}
return 0;
}
3、Codeforces 704A Thor
参考:http://codeforces.com/contest/704/standings
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 3e5 + 10;
int n, q, type, x, cur = 1;
vector<int> V[maxn];
bool vis[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H_
scanf("%d %d", &n, &q);
int ans = 0, cnt = 0;
for (int i = 1; i <= q; ++i) {
scanf("%d %d", &type, &x);
if (type == 1) {
++ans;
V[x].push_back(++cnt);
} else if (type == 2) {
int Size = V[x].size();
for (int i = 0; i < Size; ++i) {
if (!vis[V[x][i]]) {
vis[V[x][i]] = true, --ans;
}
}
V[x].clear();
} else {
for (int i = cur; i <= x; ++i) {
if (!vis[i]) {
vis[i] = true, --ans;
}
}
cur = max(cur, x + 1);
}
printf("%d\n", ans);
}
return 0;
}
4、Codeforces 518C Anya and Smartphone
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int n, m, k;
int a[maxn], b[maxn], belong[maxn], pos[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H_
ll ans = 0;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
pos[a[i]] = i + 1;
belong[a[i]] = i / k + 1;
}
for (int i = 0; i < m; ++i) {
scanf("%d", &b[i]);
ans += (ll)belong[b[i]];
if (pos[b[i]] != 1) {
int l = a[pos[b[i]] - 2], r = b[i];
int pos_r = pos[r], pos_l = pos[l];
swap(a[pos_l - 1], a[pos_r - 1]);
++pos[l], --pos[r];
belong[l] = (pos[l] - 1) / k + 1;
belong[r] = (pos[r] - 1) / k + 1;
}
}
printf("%lld\n", ans);
return 0;
}
5、POJ 3276 Face The Right Way
参考:《挑战程序设计竞赛》P152
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x7fffffff;
const int maxn = 5e3 + 10;
int N;
int dp[maxn], key[maxn];
char face[2];
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // __AiR_H
scanf("%d", &N);
REP(i, N) {
scanf("%s", face);
if (face[0] == 'F') { key[i] = 1; }
else { key[i] = 0; }
}
int sum = 0, cur = 0, pos = 0, ans_t = 0, ans_M = INF, ans_K = 0;
bool flag = true;
for (int i = 1; i <= N; ++i) {
ans_t = 0; cur = 0; pos = 0; flag = true;
memset(dp, 0, sizeof(dp));
REP(j, N) {
if (j >= i && j - i >= pos) { sum -= dp[j - i]; }
if (cur <= j) {
sum = 0; pos = j;
if (key[j] == 0) { dp[j] = 1; ++sum; ++ans_t; cur = j + i; }
else { cur = max(cur, j + 1); }
} else if (((sum & 1) && key[j] == 1) || (!(sum & 1) && key[j] == 0)) {
dp[j] = 1; ++sum; ++ans_t, cur = j + i;
}
if (cur > N) { flag = false; break; }
if (ans_t > ans_M) { break; }
}
if (flag && ans_t < ans_M) { ans_M = ans_t; ans_K = i; }
}
printf("%d %d\n", ans_K, ans_M);
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}