A. Pens and Pencils
题意
组数据,每组
一只钢笔可以写次讲座,一只铅笔可以写
次绘画课,现在有
次讲座,
次绘画课,文具盒里面可以存
支笔,如果有解满足上完所有课程,则输出解,否则输出
-1
题解
判断
B. Rooms and Staircases
题意
有两层房间,每层 个,我们用数对
来表示每个房子,其中
表示第几层,
表示从左向右数第几个
对于房子 或
,都与
或
相连
而在若干个或个位置中,又有一个双向的梯子,具体来说,若在 的位置有一个梯子,则
是相连的
求不重复经过同一个房间的情况下,最多能走过多少个房间?
题解
枚举经过第个格子到达另外一层,那么答案就是
C. The Football Season
题意
Berland capital team比了场比赛,总得分为
。已知胜一场得
分,平局
分,败了
分。
答案表示为:表示胜了
场,平局
场,败了
场。使得:
若无方案则输出-1
;若有多重方案,输出任意一个即可
The first line contains four integers ,
,
and
— 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
, so the number of points awarded for winning is strictly greater than the number of points awarded for draw.
题解
,显然
越小越好。根据
,那么
越小,
越小。
较小,而满足条件的
,所以枚举
即可。也可以在
下用同余方程的方法做。
S1
#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
#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种颜色,第个节点染成第
种颜色的代价是
,现在要你求出一种染色方案,使得总代价最小,且对于任意三个相邻的节点,颜色不能相同。输出最小代价与其中一种方案。无解输出
。
题解
这个树不是链输出。
然后枚举第一个点和第二个点的颜色,那么所有的颜色都能确定。
F. Chips
题意
有个棋子排成环状,标号为
一开始每个棋子都是黑色或白色的。随后有次操作。操作时,棋子变换的规则如下:我们考虑一个棋子本身以及与其相邻的两个棋子(共3个),如果其中白子占多数,那么这个棋子就变成白子,否则这个棋子就变成黑子。注意,对于每个棋子,在确定要变成什么颜色之后,并不会立即改变颜色,而是等到所有棋子确定变成什么颜色后,所有棋子才同时变换颜色。
对于一个棋子,与其相邻的棋子是
和
。特别地,对于棋子
,与其相邻的棋子是
和
;对于棋子
,与其相邻的棋子是
和
。
如图是在时进行的一次操作。
给你和初始时每个棋子的颜色,你需要求出在
次操作后每个棋子的颜色。
和
题解
把B
W
相间的区间提出来,每执行一次操作,它的两个端点就会变成。
对于连续个及以上连续的块,他们是不会变的。
#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,使得 尽量大且不超过给定的
。
n和k
题解
固定数字是
,然后考虑改变
(这等价
的其他形态)
初始也是
,若把
和
位置调换答案还是小于等于
,那么久调换。
或者调换和
#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; }