# AtCoder Grand Contest 001

## A - BBQ Easy

#include <cstdio>
#include <algorithm>

const int MN = 205;

int N, A[MN];

int main() {
scanf("%d", &N), N *= 2;
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
std::sort(A + 1, A + N + 1);
int Sum = 0;
for (int i = 1; i <= N; i += 2) Sum += A[i];
printf("%d\n", Sum);
return 0;
}


## B - Mysterious Light

#include <cstdio>

typedef long long LL;

LL N, X;

LL f(LL a, LL b) {
return b ? a / b * b * 2 + f(b, a % b) : -a;
}

int main() {
scanf("%lld%lld", &N, &X);
printf("%lld\n", N + f(X, N - X));
return 0;
}


## C - Shorten Diameter

#include <cstdio>
#include <vector>

const int MN = 2005;

int N, K, eu[MN], ev[MN];
std::vector<int> G[MN];

int dep[MN];
void DFS(int u, int p) {
for (int v : G[u]) if (v != p) dep[v] = dep[u] + 1, DFS(v, u);
}

int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i < N; ++i) {
scanf("%d%d", &eu[i], &ev[i]);
G[eu[i]].push_back(ev[i]);
G[ev[i]].push_back(eu[i]);
}
int Ans = N;
if (K & 1) {
for (int i = 1; i < N; ++i) {
int u = eu[i], v = ev[i];
dep[u] = dep[v] = 0;
DFS(u, v), DFS(v, u);
int cnt = 0;
for (int j = 1; j <= N; ++j) if (dep[j] > K / 2) ++cnt;
Ans = Ans > cnt ? cnt : Ans;
}
} else {
for (int i = 1; i <= N; ++i) {
dep[i] = 0, DFS(i, 0);
int cnt = 0;
for (int j = 1; j <= N; ++j) if (dep[j] > K / 2) ++cnt;
Ans = Ans > cnt ? cnt : Ans;
}
}
printf("%d\n", Ans);
return 0;
}


## D - Arrays and Palindrome

#include <cstdio>
#include <algorithm>

const int MN = 100005;

int N, M, A[MN];

int main() {
scanf("%d%d", &N, &M);
int C = 0;
for (int i = 1; i <= M; ++i) scanf("%d", &A[i]), C += A[i] & 1;
if (C > 2) return puts("Impossible"), 0;
int p1 = 0, p2 = 0;
for (int i = 1; i <= M; ++i)
if (A[i] & 1) (p1 ? p2 : p1) = i;
if (p1 && !p2) std::swap(A[1], A[p1]);
if (p2) std::swap(A[1], A[p1]), std::swap(A[p2], A[M]);
for (int i = 1; i <= M; ++i) printf("%d%c", A[i], " \n"[i == M]);
if (N == 1) return puts("1\n1"), 0;
if (M == 1) return printf("2\n1 %d\n", N - 1), 0;
--A[1], ++A[M];
printf("%d\n", M - !A[1]);
for (int i = A[1] ? 1 : 2; i <= M; ++i) printf("%d%c", A[i], " \n"[i == M]);
return 0;
}


## E - BBQ Hard

$i = j$ 的扣掉，然后除以 $2$ 就是真实的答案了。

#include <cstdio>

typedef long long LL;
const int Mod = 1000000007, Inv2 = (Mod + 1) / 2;
const int MN = 200005, C = 2000, O = C + 1, MS = O + C + 1;

inline int qPow(int b, int e) {
int a = 1;
for (; e; e >>= 1, b = (LL)b * b % Mod)
if (e & 1) a = (LL)a * b % Mod;
return a;
}

int N, A[MN], B[MN];
int Fac[MS * 2], iFac[MS * 2];
int S[MS][MS];
int Ans;

int main() {
scanf("%d", &N);
Fac[0] = 1;
for (int i = 1; i <= 4 * C; ++i) Fac[i] = (LL)Fac[i - 1] * i % Mod;
iFac[4 * C] = qPow(Fac[4 * C], Mod - 2);
for (int i = 4 * C; i >= 1; --i) iFac[i - 1] = (LL)iFac[i] * i % Mod;
for (int i = 1; i <= N; ++i) {
scanf("%d%d", &A[i], &B[i]);
++S[O - A[i]][O - B[i]];
Ans = (Ans - (LL)Fac[2 * (A[i] + B[i])] * iFac[2 * A[i]] % Mod * iFac[2 * B[i]]) % Mod;
}
for (int i = O - C; i <= O + C; ++i)
for (int j = O - C; j <= O + C; ++j)
S[i][j] = (S[i][j] + S[i - 1][j] + S[i][j - 1]) % Mod;
for (int i = 1; i <= N; ++i)
Ans = (Ans + S[O + A[i]][O + B[i]]) % Mod;
Ans = (LL)Ans * Inv2 % Mod;
printf("%d\n", (Ans + Mod) % Mod);
return 0;
}


## F - Wide Swap

#include <cstdio>
#include <algorithm>
#include <queue>

const int Inf = 0x3f3f3f3f;
const int MN = 500005, MS = 1 << 20 | 7;

int N, K, P[MN], Ans[MN];

#define li (i << 1)
#define ri (li | 1)
#define mid ((l + r) >> 1)
#define ls li, l, mid
#define rs ri, mid + 1, r
int mxp[MS];
void Build(int i, int l, int r) {
if (l == r) return mxp[i] = l, void();
Build(ls), Build(rs);
mxp[i] = P[mxp[li]] > P[mxp[ri]] ? mxp[li] : mxp[ri];
}
void Del(int i, int l, int r, int p) {
if (l == r) return mxp[i] = 0, void();
p <= mid ? Del(ls, p) : Del(rs, p);
mxp[i] = P[mxp[li]] > P[mxp[ri]] ? mxp[li] : mxp[ri];
}
int Qur(int i, int l, int r, int a, int b) {
if (r < a || b < l) return 0;
if (a <= l && r <= b) return mxp[i];
int v1 = Qur(ls, a, b), v2 = Qur(rs, a, b);
return P[v1] > P[v2] ? v1 : v2;
}

int inq[MN];
std::priority_queue<int> pq;
inline void check(int id) {
if (inq[id]) return ;
if (Qur(1, 1, N, id - K + 1, id + K - 1) == id)
pq.push(id), inq[id] = 1;
}

int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; ++i) scanf("%d", &P[i]);
P[0] = -Inf;
Build(1, 1, N);
for (int i = 1; i <= N; ++i) check(i);
for (int i = N; i >= 1; --i) {
int u = pq.top(); pq.pop();
Ans[u] = i;
Del(1, 1, N, u);
int pos;
if ((pos = Qur(1, 1, N, u - K + 1, u - 1))) check(pos);
if ((pos = Qur(1, 1, N, u + 1, u + K - 1))) check(pos);
}
for (int i = 1; i <= N; ++i) printf("%d\n", Ans[i]);
return 0;
}

posted @ 2020-06-07 04:59  粉兔  阅读(60)  评论(0编辑  收藏