A - Freight Train
思路:
阅读理解题,题意很难读,读懂后二分贪心模拟就行
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int INF = 0x3f3f3f3f, mod = 998244353 ;
const int N = 1e4 + 10;
int n, m, k;
int id[N];
bool check(int x){
int cnt = 0, now = 0;
for (int i = 0; i < m; i ++){
if (id[i] <= now) continue;
if (id[i] <= now + x){
now += x;
cnt ++;
}
else {
now = id[i] + x - 1;
cnt += 2;
}
}
if (n > now) cnt ++;
return cnt <= k;
}
void solve(){
cin >> n >> m >> k;
for (int i = 0; i < m; i ++) cin >> id[i];
int l = 1, r = n;
while (l < r){
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T --){
solve();
}
return 0;
}
D - Map Colouring
题意:
图上染色问题,相邻点不能有相同颜色,问染完所有点所需要的颜色种类,大于4输出many
思路:
数据范围很小,直接暴力4遍dfs即可
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int INF = 0x3f3f3f3f, mod = 998244353 ;
const int N = 500;
int n, m;
int col[N];
int g[N][N];
bool flag;
bool dfs(int u, int c){
for (int i = 0; i < u; i ++){
if (g[u][i] && col[u] == col[i]){
return false;
}
}
if (u == n - 1) return true;
for (int i = 0; i < c; i ++){
col[u + 1] = i;
if (dfs(u + 1, c)) return true;
}
return false;
}
void solve(){
memset(g, 0, sizeof(g));
cin >> n >> m;
for (int i = 0; i < m; i ++){
int u, v;
cin >> u >> v;
g[u][v] = g[v][u] = 1;
}
for (int i = 1; i <= 4; i ++){
memset(col, 0, sizeof(col));
if (dfs(0, i)){
cout << i << endl;
return;
}
}
cout << "many" << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T --){
solve();
}
return 0;
}
J - The King’s Walk
思路:
n * n 的方格中, 求从起点到终点不同最短路个数
思路:
切题点在于最短路,最短为横纵坐标差的较小值,我们可以将原网格转化为 max(差)* n 的网格,
并保证从左下走到右上,此时方向就仅有斜上,右,斜下三个方向,因为其他方向会导致最短路增大,然后dp即可
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int INF = 0x3f3f3f3f, mod = 5318008;
const int N = 5000;
int n;
int sx, sy, ex, ey;
int dp[N][N];
int f(int sy, int ey, int n, int m){
memset(dp, 0, sizeof(dp));
dp[0][sy] = 1;
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= m; j ++){
dp[i][j] = dp[i - 1][j] % mod;
if (j < m) dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % mod;
if (j > 1) dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
}
}
return dp[n][ey];
}
void solve(){
cin >> n;
cin >> sx >> sy >> ex >> ey;
int a = abs(sx - ex), b = abs(sy - ey);
if (a > b) cout << f(sy, ey, a, n) << endl;
else cout << f(sx, ex, b, n) << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T --){
solve();
}
return 0;
}
L - Wipe Your Whiteboards
扩欧,属于是学了就忘了
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int INF = 0x3f3f3f3f, mod = 5318008;
const int N = 5000;
int r, s, q;
int x, y;
int exgcd(int a, int b, int &x, int &y){
if (!b){
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
void solve(){
cin >> r >> s >> q;
int d = exgcd(r, -s, x, y);
y = -y;
int A = (q / d) * x, B = (q / d) * y;
x = (-s) / d, y = r / d;
if (A - x >= 1 && B - y >= 1){
while (A - x >= 1 && B - y >= 1){
A -= x;
B -= y;
}
cout << A << ' ' << B << endl;
return;
}
while (A < 1){
A += x, B += y;
}
while (B < 1){
A += x, B += y;
}
cout << A << ' ' << B << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T --){
solve();
}
return 0;
}