# Codeforces Round #551 (Div.2) 题解 (翻车记)

### Codeforces Round #551 (Div.2) 题解

$$Out \ \ of \ \ Competition$$ 选手翻车记……

$$C$$题之后的题写写题解吧：

C. Serval and Parenthesis Sequence

#### Code：

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;

int n, c1, c2;
char s[N];
int sum = 0;

int main() {
scanf("%d", &n);
if(n & 1) return puts(":("), 0;
scanf("%s", s + 1);
for(int i = 1; i <= n; i++) {
if(s[i] == '(') c1++;
else if(s[i] == ')' ) c2++;
}
for(int i = 1; i <= n; i++) {
if(s[i] == '(') {
sum ++;
}
else if(s[i] == ')') {
sum --;
}
else {
if(c1 == n / 2) sum--, c2++, s[i] = ')';
else sum++, c1++, s[i] = '(';
}
if(i != n && sum <= 0) return puts(":("), 0;
if(i == n && sum) return puts(":("), 0;
}
for(int i = 1; i <= n; i++) printf("%c", s[i]);
puts("");
return 0;
}


D. Serval and Rooted Tree

#### Code：

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;
const int INF = 1e7;
typedef vector<int> Vec;

int n, c;
int f[N], dp[N];
Vec G[N];

void Dfs(int o) {
if(!G[o].size()) return (void) ( dp[o] = 1, c++);
for(int i = 0; i < (int) G[o].size(); i++) {
int to = G[o][i];
Dfs(to);
}
if(f[o] == 1) {
dp[o] = INF;
for(int i = 0; i < (int) G[o].size(); i++) {
int to = G[o][i];
dp[o] = min(dp[o], dp[to]);
}
}
else {
dp[o] = 0;
for(int i = 0; i < (int) G[o].size(); i++) {
int to = G[o][i];
dp[o] += dp[to];
}
}
return ;
}

int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &f[i]);
for(int i = 2, x; i <= n; i++) scanf("%d", &x), G[x].push_back(i);
Dfs(1);
int ans = c - dp[1] + 1;
printf("%d\n", ans);
return 0;
}


E. Serval and Snake

#### Code：

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;

struct Point {
int x, y;
scanf("%d%d", &x, &y);
}
};

typedef pair<Point, Point> P;
#define fi first
#define se second
#define mk make_pair

int n;

int Ask(Point a, Point b) {
printf("? %d %d %d %d\n", a.x, a.y, b.x, b.y);
fflush(stdout);
int x;
scanf("%d", &x);
if(x == -1) assert(0);
return x;
}

void Answer(Point a, Point b) {
printf("! %d %d %d %d\n", a.x, a.y, b.x, b.y);
exit(0);
}

int main() {
scanf("%d", &n);
Point Ansx, Ansy;
int l = 1, r = n;
int f = 0;
for(int i = 1; i <= n; i++) {
Point A, B;
A.x = A.y = 1; B.x = n, B.y = i;
if(c & 1) {
f = 1;
Ansx.y = i;
break;
}
}
if(!f) {
for(int i = 1; i <= n; i++) {
Point A, B;
A.x = A.y = 1; B.x = i, B.y = n;
if(c & 1) {
Ansx.x = i;
break;
}
}
int l = 1, r = n;
while(l < r) {
if(l + 1 == r) {
Point A, B;
A.x = A.y = 1; B.x = Ansx.x, B.y = l;
if(c & 1) r--;
else l++;
break;
}
int mid = (l + r) >> 1;
Point A, B;
A.x = A.y = 1; B.x = Ansx.x, B.y = mid;
if(!(c & 1)) l = mid + 1;
else r = mid;
}
Ansx.y = Ansy.y = l;
l = Ansx.x + 1, r = n;
while(l < r) {
if(l + 1 == r) {
Point A, B;
A.x = r, A.y = 1; B.x = B.y = n;
if(c & 1) l++;
else r--;
break;
}
int mid = (l + r) >> 1;
Point A, B;
A.x = mid, A.y = 1; B.x = B.y = n;
if(!(c & 1)) r = mid - 1;
else l = mid;
}
Ansy.x = l;
}
else {
for(int i = n; i >= 1; i--) {
Point A, B;
A.x = 1, A.y = i; B.x = B.y = n;
if(c & 1) {
Ansy.y = i;
break;
}
}
int l = 1, r = n;
while(l < r) {
if(l + 1 == r) {
Point A, B;
A.x = 1; A.y = Ansx.y; B.x = l, B.y = Ansx.y;
if(c & 1) r--;
else l++;
break;
}
int mid = (l + r) >> 1;
Point A, B;
A.x = 1, A.y = Ansx.y; B.x = mid, B.y = Ansx.y;
if(c & 1) r = mid;
else l = mid + 1;
}
Ansx.x = l;
l = 1, r = n;
while(l < r) {
if(l == r) {
Point A, B;
A.x = 1, A.y = Ansy.y; B.x = l; B.y = Ansy.y;
if(c & 1) r--;
else l++;
break;
}
int mid = (l + r) >> 1;
Point A, B;
A.x = 1, A.y = Ansy.y; B.x = mid, B.y = Ansy.y;
if(c & 1) r = mid;
else l = mid + 1;
}
Ansy.x = l;
}
return 0;
}


F. Serval and Bonus Problem

#### Code：

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5005;
const int Md = 998244353;

inline int Add(const int &x, const int &y) { return (x + y >= Md) ? (x + y - Md) : (x + y); }
inline int Sub(const int &x, const int &y) { return (x - y < 0) ? (x - y + Md) : (x - y); }
inline int Mul(const int &x, const int &y) { return (ll)x * y % Md; }
int Powe(int x, int y = Md - 2) {
int ans = 1;
for(; y; y >>= 1, x = (ll)x * x % Md) if(y & 1) ans = 1ll * ans * x % Md;
return ans;
}

int n, k, l;
int fac[N], Inv[N], f[N][N];

void Init() {
fac[0] = Inv[0] = 1;
for(int i = 1; i < N; i++) fac[i] = Mul(fac[i - 1], i);
Inv[N - 1] = Powe(fac[N - 1]);
for(int i = N - 2; i; i--) Inv[i] = Mul(Inv[i + 1], i + 1);
}

int main() {
scanf("%d%d%d", &n, &k, &l);
Init();
f[0][0] = 1;
for(int i = 0; i <= (n << 1); i++) {
for(int j = 0; j <= min(n, i); j++) {
f[i + 1][j + 1] = Add(f[i + 1][j + 1], f[i][j]);
if(j) f[i + 1][j - 1] = Add(f[i + 1][j - 1], Mul(f[i][j], j));
}
}
int ans = 0;
int len = Mul(l, Powe((n << 1) + 1));
for(int i = 1; i <= (n << 1) - 1; i++) {
for(int j = k; j <= min(n, i); j++) ans = Add(ans, Mul(f[i][j], Mul(f[(n << 1) - i][j], fac[j])));
}
ans = Mul(ans, Powe(f[n << 1][0]));
ans = Mul(ans, len);
printf("%d\n", ans);
return 0;
}

posted @ 2019-04-14 21:58  Apocrypha  阅读(380)  评论(0编辑  收藏  举报