A - Buying Sweets
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a, b, x;
int main() {
cin >> x >> a >> b;
x-=a;
cout << x % b << endl;
return 0;
}
B - Coins
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a, b, c, x;
int main() {
cin >> a >> b >> c >> x;
int res = 0;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
for (int k = 0; k <= c; k++) {
if (i * 500 + j * 100 + k * 50 == x) res++;
}
}
}
cout << res << endl;
return 0;
}
C - Candies
给出一个2xN的矩阵,问从左上角走到右下角能获得的权值最大是多少
类似数塔模型的做法,推一下即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n, a[2][N], dp[2][N];
int main() {
cin >> n;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
for (int i = n - 1; i >= 0; i--) {
dp[1][i] = dp[1][i + 1] + a[1][i];
dp[0][i] = max(dp[1][i], dp[0][i + 1]) + a[0][i];
}
cout << dp[0][0] << endl;
return 0;
}
D - People on a Line
给出n个人,他们都有一个权值,现在给出m个说法,每个说法代表第y个人比第x个人大d,问这m个说法是否能够自洽
带权并查集裸题
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int n, m;
int f[N], d[N];
int findf(int x) {
if (f[x] == x) return x;
int tmp = findf(f[x]);
d[x] += d[f[x]];
return f[x] = tmp;
}
void Union(int x, int y, int w) {
int fx = findf(x), fy = findf(y);
if (fx != fy) {
f[fy] = fx;
d[fy] = w - d[y] + d[x];
}
}
int main() {
cin >> n>>m;
for (int i = 0; i <= n; i++) f[i] = i;
int flag = 0;
for (int i = 0; i < m; i++) {
int x, y, w;
cin >> x >> y >> w;
int fx = findf(x), fy = findf(y);
if (fx == fy) {
if (d[y] - d[x] != w) flag = 1;
} else
Union(x, y, w);
}
if (flag) cout << "No" << endl;
else
cout << "Yes" << endl;
return 0;
}