srm 553

250


Solution

把$-1$分别替换为$0,1,2$来判断答案是否变化即可。简单粗暴

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
LL gao(vector<int> a) {
stack<LL> S;
int n = a.size();
for (int i = 0; i < 100; ++i) S.push(0);
for (int i = 0; i < n; ++i) {
if (!a[i]) {
LL x = S.top();
S.pop();
LL y = S.top();
S.pop();
S.push(x + y);
}
else S.push(a[i]);
}
return S.top();
}
struct Suminator {
int findMissing(vector <int> a, int S) {
int n = a.size();
int p;
for (int i = 0; i < n; ++i)
if (a[i] == -1) p = i;
a[p] = 0;
LL t = gao(a);
if (t == S) return 0;
a[p] = 1;
LL u = gao(a);
a[p] = 2;
LL v = gao(a);
if (u == v) return -1;
if (u <= S) return S - u + 1;
return -1;
}
};

500


Description

求将地图染成$2$个凸连通块的方案数

Solution

这是道比较有趣的题,首先发现,如果有一段连续的格子被染成同一颜色,在以后的行上,被同色染的格子数一定非递增或非递减,且会确定以后行的状态。于是可以$dp$,大概就是$dp$到第$i$行连续$j$个格子被染成同一颜色,当前状态是非递增还是非递减还是尚未确定。$记忆化搜索可能好写一点$。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>//dp
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const int N = 55, M = 1e9 + 7;
int n, m;
int dp[N][N][3][2][2];
bool bw[N][N], wb[N][N];
int f(int x, int y, int dir, int ok1, int ok2) {
if (x == n) {
if (y == m && dir == 2) return 0;
return ok1 + ok2;
}
int &t = dp[x][y][dir][ok1][ok2];
if (~t) return t;
t = 0;
for (int num = 0; num <= m; ++num) {
if (dir == 0 && num < y || dir == 1 && num > y) continue;
int ndir = dir;
if (dir == 2 && num < y) ndir = 1;
if (dir == 2 && num > y) ndir = 0;
if (x == 0) ndir = 2;
if (y == m && num == 0) continue;
int t1 = ok1, t2 = ok2;
if (!bw[x][num]) t1 = 0;
if (!wb[x][num]) t2 = 0;
(t += f(x + 1, num, ndir, t1, t2)) %= M;
}
return t;
}
struct TwoConvexShapes {
int countWays(vector <string> grid) {
n = grid.size(), m = grid[0].size();
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i)
for (int j = 0; j <= m; ++j) {
bw[i][j] = 1;
for (int k = 0; k < j; ++k)
if (grid[i][k] == 'W') bw[i][j] = 0;
for (int k = j; k < m; ++k)
if (grid[i][k] == 'B') bw[i][j] = 0;
wb[i][j] = 1;
for (int k = 0; k < j; ++k)
if (grid[i][k] == 'B') wb[i][j] = 0;
for (int k = j; k < m; ++k)
if (grid[i][k] == 'W') wb[i][j] = 0;
}
int ans = 0;
(ans += f(0, 0, 2, 1, 1)) %= M;
return ans;

}
};