ABC371C-D
C - Make Isomorphic
此题思路为,利用全排列枚举所有点的关系,如果需要调整则调整,不需要就继续,代码如下:
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
58
59
60
61
62
using ll = long long;
using namespace std;
const ll N = 1e6 + 5;
ll n, mg, mh;
bool g[10][10], h[10][10];
int res[10];
int a[10][10];
ll ans = 1e18;
int used[10];
void dfs(int step) {
if (step > n) {
ll cost = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (g[res[i]][res[j]] != h[i][j]) {
cost += a[i][j];
}
}
}
ans = min(ans, cost);
return;
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
used[i] = 1;
res[step] = i;
dfs(step + 1);
used[i] = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
cin >> mg;
for (int i = 1; i <= mg; i++) {
int u, v;
cin >> u >> v;
g[u][v] = 1;
g[v][u] = 1;
}
cin >> mh;
for (int i = 1; i <= mh; i++) {
int u, v;
cin >> u >> v;
h[u][v] = 1;
h[v][u] = 1;
}
for (int i = 1; i <= n - 1; i++) {
for (int j = i + 1; j <= n; j++) {
cin >> a[i][j];
}
}
dfs(1);
cout << ans << endl;
return 0;
}
D - 1D Country
此题使用二分查找或lower_bound和upper_bound找到区间内的村庄个数,然受前缀和即可找出答案,代码如下:
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
using namespace std;
using ll = long long;
const ll N = 2e5 + 5;
ll sum[N];
int main() {
int n;
cin >> n;
int x[N], p[N];
for (int i = 1; i <= n; i++) {
cin >> x[i];
}
for (int i = 1; i <= n; i++) {
cin >> p[i];
sum[i] = sum[i - 1] + p[i];
}
int t;
cin >> t;
while(t--) {
int pos1, pos2;
int l, r;
cin >> l >> r;
pos1 = lower_bound(x + 1, x + n + 1, l) - x - 1;
pos2 = upper_bound(x + 1, x + n + 1, r) - x - 1;
ll ans = sum[pos2] - sum[pos1];
cout << ans << endl;
}
return 0;
}
后面会补补后面的题,等更新吧