2021CCPC桂林


题目来自: The 2021 CCPC Guilin Onsite (XXII Open Cup, Grand Prix of EDG)

过了一车的贪心不会…

D - Assumption is All You Need

SOLUTION

贪心,尽量让大的换到前面来让逆序对尽可能多。

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
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10, Log = 20, inf = 0x3f3f3f3f;

void solve() {
int n;
cin >> n;
vector<int> a(n + 1), b(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) cin >> b[i];
vector<pii> ans;
for(int i = 1; i <= n; i++){
if(a[i] == b[i]) continue;
if(a[i] < b[i]){
cout << "-1\n";
return;
}
vector<int> p;
p.push_back(i);
for(int j = i + 1; j <= n; j++){
if(a[j] < a[p.back()] && a[j] >= b[i]){
p.push_back(j);
}
if(a[j] == b[i]) break;
}
reverse(p.begin(), p.end());
for(int j = 0; j + 1 < p.size(); j++){
ans.emplace_back(p[j], p[j + 1]);
swap(a[p[j]], a[p[j + 1]]);
}
// for(int j = 1; j <= n; j++) cout << a[j] << ' '; cout << '\n';
}
cout << ans.size() << '\n';
for(pii i : ans){
if(i.first > i.second) swap(i.second, i.first);
cout << i.first << ' ' << i.second << '\n';
}
}

int main() {
int T = 1;
ios::sync_with_stdio(false);
cin >> T;
while (T--) solve();
return 0;
}

B - A Plus B Problem

SOLUTION

经典线段树区间找最右边的0和9。

虽然经典,但是我vp写的时候bug还是一堆。

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N=1e6+10;
const int INF=2e9;

int mx[N << 2], mn[N << 2], lz[N << 2];
int ai[N], bi[N], c[N], n;

void apply(int k, int x){
mx[k] = mn[k] = x;
lz[k] = x;
}

void pd(int k){
if(lz[k] == -1) return;
apply(k << 1, lz[k]);
apply(k << 1 | 1, lz[k]);
lz[k] = -1;
}

void pu(int k){
mx[k] = max(mx[k << 1], mx[k << 1 | 1]);
mn[k] = min(mn[k << 1], mn[k << 1 | 1]);
}

void build(int k, int l, int r){
lz[k] = -1;
if(l == r){
mx[k] = mn[k] = c[l];
return;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
pu(k);
}

void upd(int k, int l, int r, int ql, int qr, int x){
if(l > qr || r < ql) return;
if(l >= ql && r <= qr){
mx[k] = mn[k] = x;
lz[k] = x;
return;
}
pd(k);
int mid = (l + r) >> 1;
upd(k << 1, l, mid, ql, qr, x);
upd(k << 1 | 1, mid + 1, r, ql, qr, x);
pu(k);
}

void upd(int k, int l, int r, int p, int x){
if(l > p || r < p) return;
if(l == r){
mx[k] += x;
mn[k] += x;
return;
}
pd(k);
int mid = (l + r) >> 1;
upd(k << 1, l, mid, p, x);
upd(k << 1 | 1, mid + 1, r, p, x);
pu(k);
}

int qry(int k, int l, int r, int p){
if(l > p || r < p) return -1;
if(l == r){
return mx[k];
}
pd(k);
int mid = (l + r) >> 1;
return max(qry(k << 1, l, mid, p), qry(k << 1 | 1, mid + 1, r, p));
}

int qry0(int k, int l, int r, int ql, int qr){
if(l > qr || r < ql) return 0;
if(l == r) return l;
int mid = (l + r) >> 1;
pd(k);
if(l >= ql && r <= qr){
int res = 0;
if(mx[k << 1 | 1]) res = max(res, qry0(k << 1 | 1, mid + 1, r, ql, qr));
else if(mx[k << 1]) res = max(res, qry0(k << 1, l, mid, ql, qr));
return res;
}
int res = 0;
if(mid < qr && mx[k << 1 | 1]) res = max(qry0(k << 1 | 1, mid + 1, r, ql, qr), res);
if(mid >= ql && res == 0 && mx[k << 1]) res = max(res, qry0(k << 1, l, mid, ql, qr));
return res;
}

int qry9(int k, int l, int r, int ql, int qr){
if(l > qr || r < ql) return 0;
if(l == r) return l;
int mid = (l + r) >> 1;
pd(k);
if(l >= ql && r <= qr){
int res = 0;
if(mn[k << 1 | 1] < 9) res = max(res, qry9(k << 1 | 1, mid + 1, r, ql, qr));
else if(mn[k << 1] < 9) res = max(res, qry9(k << 1, l, mid, ql, qr));
return res;
}
int res = 0;
if(mid < qr && mn[k << 1 | 1] < 9) res = max(qry9(k << 1 | 1, mid + 1, r, ql, qr), res);
if(mid >= ql && res == 0 && mn[k << 1] < 9) res = max(res, qry9(k << 1, l, mid, ql, qr));
return res;
}

void handle(int ci, int di, int a[]){
int p = qry(1, 1, n, ci);
if(a[ci] == di){
printf("%d 0\n", p);
}else if(a[ci] > di){ // -
int x = a[ci] - di;
if(p >= x){
upd(1, 1, n, ci, -x);
printf("%d 2\n", p - x);
}else{
// printf("qry0::%d %d\n", 1, ci - 1);
int p0 = qry0(1, 1, n, 1, ci - 1), cnt = ci - 1 - p0 + 1;
// printf("!0:: %d\n", p0);
upd(1, 1, n, p0 + 1, ci - 1, 9);
p = p + 10 - x;
upd(1, 1, n, ci, ci, p);
if(p0){
upd(1, 1, n, p0, -1);
cnt++;
}
printf("%d %d\n", p, cnt + 1);
}
}else{// +
int x = di - a[ci];
if(p + x < 10){
upd(1, 1, n, ci, x);
printf("%d 2\n", p + x);
}else{
int p0 = qry9(1, 1, n, 1, ci - 1), cnt = ci - 1 - p0 + 1;
// printf("!9:: %d\n", p0);
upd(1, 1, n, p0 + 1, ci - 1, 0);
p = p + x - 10;
upd(1, 1, n, ci, ci, p);
if(p0){
upd(1, 1, n, p0, 1);
cnt++;
}
printf("%d %d\n", p, cnt + 1);
}
}
a[ci] = di;
}

void solve(){
int q;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++){
scanf("%1d", &ai[i]);
}
for(int i = 1; i <= n; i++){
scanf("%1d", &bi[i]);
}
for(int i = n; i >= 1; i--){
c[i] += ai[i] + bi[i];
c[i - 1] += c[i] / 10;
c[i] %= 10;
}
build(1, 1, n);
// for(int i = 1; i <= n; i++) cout << qry(1, 1, n, i);cout << '\n';
while(q--){
int ri, ci, di;
scanf("%d%d%d", &ri, &ci, &di);
if(ri == 1){
handle(ci, di, ai);
}else{
handle(ci, di, bi);
}
// for(int i = 1; i <= n; i++) cout << qry(1, 1, n, i);cout << '\n';
}
}

int main() {
int T=1;
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
// cin>>T;
//init();
for(int i=1;i<=T;i++) {
solve();
}
}

K - Tax

SOLUTION

看了题解瞬间恍然大悟,顿时感觉自己不太聪明。

在最短路的情况下最多只有$3^{\frac{n}{3}}$种情况,于是爆搜即可。

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
58
59
60
61
62
63
64
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e4 + 10, Log = 20, inf = 0x3f3f3f3f;

int d[N], dis[N], v[N], res[N], cnt[N];

struct Edge{
int v, p;
};

vector<Edge> e[N];

void dfs(int u, int dist){
res[u] = min(res[u], dist);
for(Edge i : e[u]){
int v = i.v, p = i.p;
if(dis[v] == dis[u] + 1){
cnt[p]++;
dfs(v, dist + d[p] * cnt[p]);
cnt[p]--;
}
}
}

void solve() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> d[i];
}
for(int i = 0, u, v, p; i < m; i++){
cin >> u >> v >> p;
e[u].push_back({v, p});
e[v].push_back({u, p});
}
queue<int> q;
q.push(1);
v[1] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(Edge i : e[x]){
if(v[i.v]) continue;
dis[i.v] = dis[x] + 1;
v[i.v] = 1;
q.push(i.v);
}
}
for(int i = 1; i <= n; i++) res[i] = inf;
dfs(1, 0);
for(int i = 2; i <= n; i++) cout << res[i] << '\n';
}

int main() {
int T = 1;
ios::sync_with_stdio(false);
// cin >> T;
while (T--) solve();
return 0;
}