# AtCoder Grand Contest 006

## A - Prefix and Suffix

#include <iostream>
#include <string>

int N;
std::string s, t;

int main() {
std::cin >> N >> s >> t;
for (int i = N; i >= 0; --i)
if (s.substr(N - i) == t.substr(0, i))
return std::cout << N + N - i, 0;
}


## B - Median Pyramid Easy

#include <cstdio>

int N, x;

int main() {
scanf("%d%d", &N, &x);
if (x == 1 || x == 2 * N - 1) return puts("No"), 0;
puts("Yes");
if (N == 2) return puts("1\n2\n3"), 0;
if (x == 2) {
for (int i = 1; i <= 2 * N - 1; ++i)
printf("%d\n", (N + i) % (2 * N - 1) + 1);
return 0;
}
int c = 0;
for (int i = 1; i <= N - 3; ++i) {
for (++c; x - 2 <= c && c <= x + 1; ++c) ;
printf("%d\n", c);
}
printf("%d\n%d\n%d\n%d\n", x - 2, x + 1, x, x - 1);
for (int i = 1; i <= N - 2; ++i) {
for (++c; x - 2 <= c && c <= x + 1; ++c) ;
printf("%d\n", c);
}
return 0;
}


## C - Rabbit Exercise

#include <cstdio>
#include <algorithm>

typedef long long LL;
const int MN = 100005;

int N, M; LL K;
LL x[MN];
int per[MN], vis[MN], stk[MN], tp;

int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%lld", &x[i]);
for (int i = N; i >= 2; --i) x[i] -= x[i - 1];
for (int i = 1; i <= N; ++i) per[i] = i;
scanf("%d%lld", &M, &K);
while (M--) {
int p;
scanf("%d", &p);
std::swap(per[p], per[p + 1]);
}
for (int i = 1; i <= N; ++i) if (!vis[i]) {
tp = 0;
int u = i;
do vis[u] = 1, stk[tp++] = u, u = per[u]; while (u != i);
for (int j = 0; j < tp; ++j) per[stk[j]] = stk[(j + K) % tp];
}
LL s = 0;
for (int i = 1; i <= N; ++i) s += x[per[i]], printf("%lld\n", s);
return 0;
}


## D - Median Pyramid Hard

#include <cstdio>

const int MN = 200005;

int N, M, A[MN];

inline bool check(int x) {
int l = 1, r = M;
for (int i = N; i > 1; --i)
if ((A[i] >= x) == (A[i - 1] >= x)) { l = i; break; }
for (int i = N; i < M; ++i)
if ((A[i] >= x) == (A[i + 1] >= x)) { r = i; break; }
return N - l <= r - N ? A[l] >= x : A[r] >= x;
}

int main() {
scanf("%d", &N), M = 2 * N - 1;
for (int i = 1; i <= M; ++i) scanf("%d", &A[i]);
int l = 3, r = M - 1, mid, ans = 2;
while (l <= r) {
mid = (l + r) / 2;
if (check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}


## E - Rotate 3x3

#include <cstdio>

const int MN = 100005;

int N, A[3][MN], B[MN];
int bit[MN];
inline void Add(int i) { for (; i; i -= i & -i) bit[i] ^= 1; }
inline int Qur(int i) { int s = 0; for (; i <= N; i += i & -i) s ^= bit[i]; return s; }

int main() {
scanf("%d", &N);
for (int j = 0; j < 3; ++j)
for (int i = 1; i <= N; ++i)
scanf("%d", &A[j][i]);
int p1 = 0, p2 = 0;
for (int i = 1; i <= N; ++i) {
if (A[1][i] % 3 != 2 || A[0][i] + A[2][i] != 2 * A[1][i]) return puts("No"), 0;
int x = A[1][i] / 3 + 1;
if ((A[0][i] + 2) / 3 != x || ((B[i] = x) ^ i) & 1) return puts("No"), 0;
if (A[0][i] == x * 3) (i & 1 ? p2 : p1) ^= 1;
}
for (int i = 1; i <= N; i += 2)
for (int i = 1; i <= N; ++i) bit[i] = 0;
for (int i = 2; i <= N; i += 2)
puts(p1 || p2 ? "No" : "Yes");
return 0;
}


## F - Blackout

#include <cstdio>
#include <vector>

typedef long long LL;
const int MN = 100005;

int N, M; LL Ans;
std::vector<int> G[MN], iG[MN];

int ok, col[MN], a[3], cv, ce;
void DFS(int u, int c) {
++a[col[u] = c], ++cv;
ce += G[u].size();
for (int v : G[u])
if (!~col[v]) DFS(v, (c + 1) % 3);
else if (col[v] != (c + 1) % 3) ok = 0;
for (int v : iG[u])
if (!~col[v]) DFS(v, (c + 2) % 3);
else if (col[v] != (c + 2) % 3) ok = 0;
}

int main() {
scanf("%d%d", &N, &M);
while (M--) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
iG[y].push_back(x);
}
for (int i = 1; i <= N; ++i) col[i] = -1;
for (int i = 1; i <= N; ++i) if (!~col[i]) {
ok = 1, a[0] = a[1] = a[2] = cv = ce = 0;
DFS(i, 0);
if (!ok) Ans += (LL)cv * cv;
else if (a[0] && a[1] && a[2]) Ans += (LL)a[0] * a[1] + (LL)a[1] * a[2] + (LL)a[2] * a[0];
else Ans += ce;
}
printf("%lld\n", Ans);
return 0;
}

posted @ 2020-06-23 18:16  粉兔  阅读(78)  评论(0编辑  收藏