Codeforces Round 592 (Div. 2)

A. Pens and Pencils

题意

tt组数据,每组a,b,c,d,ka,b,c,d,k
一只钢笔可以写cc次讲座,一只铅笔可以写dd次绘画课,现在有aa次讲座,bb次绘画课,文具盒里面可以存kk支笔,如果有解满足上完所有课程,则输出解,否则输出-1

题解

判断ac+bdk\lceil\frac{a}{c}\rceil+\lceil\frac{b}{d}\rceil\leq k

B. Rooms and Staircases

题意

有两层房间,每层 nn 个,我们用数对 (a,b)(a, b) 来表示每个房子,其中 aa 表示第几层,bb 表示从左向右数第几个

对于房子 (1,i)(1, i)(2,i)(2, i),都与 (1,i1),(1,i+1)(1, i - 1), (1, i + 1)(2,i1),(2,i+1)(2, i - 1), (2, i + 1) 相连

而在若干个或个位置中,又有一个双向的梯子,具体来说,若在 ii 的位置有一个梯子,则 (1,i),(2,i)(1, i), (2, i) 是相连的

求不重复经过同一个房间的情况下,最多能走过多少个房间?

题解

ansnans\ge n
枚举经过第ii个格子到达另外一层,那么答案就是2max{i,ni+1}2\max\{i,n-i+1\}

C. The Football Season

题意

Berland capital team比了nn场比赛,总得分为pp。已知胜一场得ww分,平局dd分,败了00分。

答案表示为(x,y,z)(x,y,z):表示胜了xx场,平局yy场,败了zz场。使得:

  • x×w+y×d=px \times w + y \times d=p
  • x+y+z=nx+y+z=n

若无方案则输出-1;若有多重方案,输出任意一个即可
The first line contains four integers $ n $ , $ p $ , $ w $ and $ d $ $ (1 \le n \le 10^{12}, 0 \le p \le 10^{17}, 1 \le d < w \le 10^{5}) $ — the number of games, the number of points the team got, the number of points awarded for winning a match, and the number of points awarded for a draw, respectively. Note that $ w > d $ , so the number of points awarded for winning is strictly greater than the number of points awarded for draw.

题解

z=n(x+y)z=n-(x+y),显然x+yx+y越小越好。根据w>dw>d,那么yy越小,x+yx+y越小。dd较小,而满足条件的y[0,w)y\in[0,w),所以枚举yy即可。也可以在(modw)\pmod w下用同余方程的方法做。

S1

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
#include<bits/stdc++.h>
typedef long long ll;
inline ll read() {ll x = 0;char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {
if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();
}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
using namespace std;



ll n, p, w, d;
//
int main() {
n = read(), p = read(), w = read(), d = read();
ll y = 0;
while(y < w && (p - y * d) % w) ++y;
ll x = (p - y * d) / w;
if(y == w || x + y > n ||x<0) {
puts("-1");
return 0;
}
printf("%lld %lld %lld\n", x, y, n - x - y);
return 0;
}

S2

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
#include<bits/stdc++.h>
typedef long long ll;
inline ll read() {ll x = 0;char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {
if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();
}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
using namespace std;



ll n, p, w, d;
//
ll exgcd(ll a, ll &x, ll b, ll &y) {
if(b) {
ll d = exgcd(b, y, a % b, x);
y -= a / b * x;
return d;
} else {
x = 1;
y = 0;
return a;
}
}
int main() {
n = read(), p = read(), w = read(), d = read();
ll x, y;

ll gd = exgcd(w, x, d, y);
y = y %w* ((p / gd) % w) %w;
/*
y / (w / gd)
*/
y = (y % (w / gd) + (w / gd)) % (w / gd);
/*
wx + dy = p

*/
// y = (y + w) %w;
// printf("x=%lld,y=%lld,v=%lld,p=%lld\n",x,y,(d%w*y%w+w)%w, p%w);
x = (p - y * d) / w;
// printf("%lld %lld v=%lld\n", x, y, (p-y*d)%w);
if((p-y*d)%w||x < 0 ||y <0||x+y>n) {
puts("-1");
return 0;
}
printf("%lld %lld %lld\n", x, y, n - x - y);
return 0;
}

D. Paint the Tree

题意

有一棵树,有3种颜色,第ii个节点染成第jj种颜色的代价是cj,ic_{j,i},现在要你求出一种染色方案,使得总代价最小,且对于任意三个相邻的节点,颜色不能相同。输出最小代价与其中一种方案。无解输出1-1

3n1053\le n\le 10^5

题解

这个树不是链输出1-1
然后枚举第一个点和第二个点的颜色,那么所有的颜色都能确定。

F. Chips

题意

nn个棋子排成环状,标号为1..n1..n

一开始每个棋子都是黑色或白色的。随后有kk次操作。操作时,棋子变换的规则如下:我们考虑一个棋子本身以及与其相邻的两个棋子(共3个),如果其中白子占多数,那么这个棋子就变成白子,否则这个棋子就变成黑子。注意,对于每个棋子,在确定要变成什么颜色之后,并不会立即改变颜色,而是等到所有棋子确定变成什么颜色后,所有棋子才同时变换颜色。

对于一个棋子ii,与其相邻的棋子是i1i-1i+1i+1。特别地,对于棋子11,与其相邻的棋子是22nn;对于棋子nn,与其相邻的棋子是11n1n-1

如图是在n=6n=6时进行的一次操作。

图像
给你nn和初始时每个棋子的颜色,你需要求出在kk次操作后每个棋子的颜色。
n (3n2105)n\ (3 \leq n \leq 2\cdot 10^5)k (1k109)k\ (1 \leq k \leq 10^9)

题解

B W相间的区间提出来,每执行一次操作,它的两个端点就会变成a[L1],a[R+1]a[L-1],a[R+1]
对于连续22个及以上连续的块,他们是不会变的。

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
#include<bits/stdc++.h>
typedef long long ll;
inline ll read() {ll x = 0;char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {
if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();
}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
using namespace std;


const int N = 2e5 + 666;
int n;
ll k;
char s[N];
int b[N];
int L(int x) {
return (x - 1 + n) %n;
}
int R(int x) {
return (x + 1) %n;
}
int cal(int l, int r) {
if(l <= r) return r - l + 1;
else return n - r + l - 1;
}
char st[] = "WB";
#define pa pair<int,int>
#define fi first
#define se second
#define mk make_pair
queue<pa> S;
int main() {
n = read(), k = read();
scanf("%s", s);
for(int i = 0; i < n; ++i) {
if(s[i] == 'W') b[i] = 0;
else b[i] = 1;
}
int p = 0;
while(p < n - 1 && b[p] != b[p + 1]) ++p;
if(b[n - 1] == b[0]) p = n - 1;
if(b[p] != b[R(p)]) {
if(k & 1) for(int i = 0; i < n; ++i) putchar(st[b[i] ^ 1]);
else for(int i = 0; i < n; ++i) putchar(st[b[i]]);
puts("");
return 0;
}
int q = R(p);
while(b[q] == b[p] && p != q) q = R(q);

if(p == q) {
puts(s);
return 0;
}
q = L(q);
for(int i = L(p), j; i != q; i = j) {
if(b[i] != b[R(i)]){

j = L(i);
while(b[j] == (b[R(j)] ^ 1)) j = L(j);
if(R(j) == i) continue;
S.push(mk(R(R(j)), i));
if(R(j) == q) j = R(j);
} else j = L(i);
}
while(!S.empty()) {
pa now = S.front(); S.pop();
int l = now.fi;
int r = now.se;
if((cal(l, r)+1)/ 2 <= k) {

int cnt = cal(l, r);
for(int i = l, j = 0; j < (cnt+1)/2; i = R(i), ++j) b[i] = b[L(l)];
for(int i = r, j = 0; j < cnt/2; i = L(i), ++j) b[i] = b[R(r)];
} else {
int x = l;
int y = r;
int cnt = cal(l, r);
for(int i = 1; i <= (cnt + 1) / 2; ++i) {
if(i <= k) {
b[x] = b[L(l)];
b[y] = b[R(r)];

} else {
b[x] ^= (k & 1);
if(x != y) b[y] ^= (k & 1);
}
x = R(x);
y = L(y);
}
}
}
for(int i = 0; i < n; ++i)putchar(st[b[i]]);
puts("");
return 0;
}

G. Running in Pairs

题意

找出两个1到n的全排列p和q,使得
i=1nmax(pi,qi)\sum^n_{i=1} \max(p_i,q_i)尽量大且不超过给定的kk
n和k (1n106,1kn2)(1\le n\le 10^6,1\le k\le n^2)

题解

固定pp数字是1,2,,n1,2,\cdots,n,然后考虑改变qq(这等价pp的其他形态)
初始qq也是1,2,,n1,2,\cdots,n,若把ni+1n-i+1ii位置调换答案还是小于等于kk,那么久调换。
或者调换iii+ki+k

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
#include<bits/stdc++.h>
typedef long long ll;
inline ll read() {ll x = 0;char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {
if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();
}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
using namespace std;

const int N = 1e6 + 666;


int n;
ll k, m;
int a[N];
int main() {
n = read(), k = read();
m = k;
k -= (ll)n * (n+1)/2;
if(k < 0) {
puts("-1");
return 0;
}
for(int i = 1; i <= n; ++i) a[i] = i;
for(int i = 1; i <= n / 2; ++i) {
if(k >= (n - i + 1 - i)) {
swap(a[n - i + 1], a[i]);
k -= (n - 2 * i + 1);
} else {
int tmp = i + k;
swap(a[i], a[tmp]);
k = 0;
break;
}
}
writeln(m - k);
for(int i = 1; i <= n; ++i) printf("%d ", i);
puts("");
for(int i = 1; i <= n; ++i) printf("%d ", a[i]);
return 0;
}