COCI 2024/2025 #3
T1 P11474 [COCI 2024/2025 #3] 公交车 / Autobus
愤怒,从红升橙足以说明其恶心,考场上调了半小时才过。
这道题的车能够开 \(24\) 小时,并且他能从前一天开到第二天,由于它只能开 \(24\) 小时,所以说发车时间的时刻晚于或等于到达时间,说明他开了一天,由于这个,所以我们要处理 \(3\) 天的情况。
先把所有的时间段存下来,然后分别加 \(1440\) 和 \(2880\) 分钟,暴力匹配,时间复杂度 \(O(600^2) = O(1)\) (不是)。
//# pragma GCC optimize("Ofast")
# include <bits/stdc++.h>
# define fr front
# define il inline
# define fir first
# define sec second
# define vec vector
# define it iterator
# define pb push_back
# define lb lower_bound
# define ub upper_bound
# define all(x) x.begin(), x.end()
# define mem(a, b) memset(a, b, sizeof(a))
# define lc (t[p].l)
# define rc (t[p].r)
# define ls(x) (x << 1)
# define rs(x) (x << 1 | 1)
# define lson ls(p), l, mid
# define rson rs(p), mid + 1, r
# define sqr(x) ((x) * (x))
# define bpc __builtin_popcount
# define lowbit(x) ((x) & (-(x)))
# define geti(x, i) (((x) >> (i)) & 1)
# define set1(x, i) ((x) | (1 << (i)))
# define set0(x, i) ((x) & (~(1 << (i))))
# define debug1(x) cerr << #x << " = " << x << " "
# define debug2(x) cerr << #x << " = " << x << "\n"
# define bug cerr << "--------------------------\n"
# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
# define G(i, h, u, ne) for(int i = h[(u)]; i; i = ne[i])
# define reps(i, a, b, c) for(int i = (a); i <= (b); i += (c))
# define pres(i, a, b, c) for(int i = (a); i >= (b); i -= (c))
using namespace std;
using DB = double;
using LL = long long;
using LDB = long double;
using PII = pair<int, int>;
using ULL = unsigned long long;
const int INF1 = 0x3f3f3f3f, INF2 = INT_MAX;
const LL INF3 = (LL)1e18, INF4 = 0x3f3f3f3f3f3f3f3f, INF5 = LLONG_MAX;
int n, ans;
vec<PII> tmp1, tmp2, v1, v2;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
ans = INF1;
char ch, s[114];
int h1, h2, m1, m2;
cin >> n;
rep(i, 1, n){
cin >> s + 1;
if(s[1] == 'Z'){
cin >> h1 >> ch >> ch;
m1 = (ch - '0') * 10;
cin >> ch;
m1 += ch - '0';
cin >> ch >> ch;
cin >> h2 >> ch >> ch;
m2 = (ch - '0') * 10;
cin >> ch;
m2 += ch - '0';
int t1 = h1 * 60 + m1;
int t2 = h2 * 60 + m2;
if(t1 > t2) tmp1.pb({t1, t2 + 1440});
else tmp1.pb({t1, t2});
}
else{
cin >> h1 >> ch >> ch;
m1 = (ch - '0') * 10;
cin >> ch;
m1 += ch - '0';
cin >> ch >> ch;
cin >> h2 >> ch >> ch;
m2 = (ch - '0') * 10;
cin >> ch;
m2 += ch - '0';
int t1 = h1 * 60 + m1;
int t2 = h2 * 60 + m2;
if(t1 > t2) tmp2.pb({t1, t2 + 1440});
else tmp2.pb({t1, t2});
}
}
each2(x, tmp1){
v1.pb(x);
v1.pb({x.fir + 1440, x.sec + 1440});
v1.pb({x.fir + 2880, x.sec + 2880});
}
each2(x, tmp2){
v2.pb(x);
v2.pb({x.fir + 1440, x.sec + 1440});
v2.pb({x.fir + 2880, x.sec + 2880});
}
each2(a, v1){
each2(b, v2){
if(a.sec >= b.fir) continue;
ans = min(ans, b.sec - a.fir + 1);
}
}
if(ans == INF1) cout << "NEMOGUCE";
else{
cout << ans / 60 << ":";
ans %= 60;
if(ans < 10) cout << "0";
cout << ans;
}
return 0;
}
T2 P11475 [COCI 2024/2025 #3] 红蓝牌 / Karte
考场上轻信了 @ma_niu_bi 的话,用 DP 做,实际上我没有按照 DP 想的时候,直接 \(5\) 分钟就秒了。
看到 \(1 \le N,M \le 21\),\(0 \le X,Y \le 30\),其实我就有想法了,\(\rm strength = w − X \times r − Y \times b\),如果说我们能够枚举哪些红牌需要选,对于每张蓝牌,选与不选就取决于他能与红牌组成好对的数量大不大于 \(Y\),因为 \(X \times r\) 已经确定了(枚举的)。时间复杂度 \(O(2^n \times m \times n)\) 改好能卡过。
//# pragma GCC optimize("Ofast")
# include <bits/stdc++.h>
# define int LL
# define fr front
# define il inline
# define fir first
# define sec second
# define vec vector
# define it iterator
# define pb push_back
# define lb lower_bound
# define ub upper_bound
# define all(x) x.begin(), x.end()
# define mem(a, b) memset(a, b, sizeof(a))
# define lc (t[p].l)
# define rc (t[p].r)
# define ls(x) (x << 1)
# define rs(x) (x << 1 | 1)
# define lson ls(p), l, mid
# define rson rs(p), mid + 1, r
# define sqr(x) ((x) * (x))
# define bpc __builtin_popcount
# define lowbit(x) ((x) & (-(x)))
# define geti(x, i) (((x) >> (i)) & 1)
# define set1(x, i) ((x) | (1 << (i)))
# define set0(x, i) ((x) & (~(1 << (i))))
# define debug1(x) cerr << #x << " = " << x << " "
# define debug2(x) cerr << #x << " = " << x << "\n"
# define bug cerr << "--------------------------\n"
# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
# define G(i, h, u, ne) for(int i = h[(u)]; i; i = ne[i])
# define reps(i, a, b, c) for(int i = (a); i <= (b); i += (c))
# define pres(i, a, b, c) for(int i = (a); i >= (b); i -= (c))
using namespace std;
using DB = double;
using LL = long long;
using LDB = long double;
using PII = pair<int, int>;
using ULL = unsigned long long;
const int N = 25, X = 35;
const int INF1 = 0x3f3f3f3f, INF2 = INT_MAX;
const LL INF3 = (LL)1e18, INF4 = 0x3f3f3f3f3f3f3f3f, INF5 = LLONG_MAX;
int n, m, x, y, ans, val, cnt;
char ok[N][N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> x >> y;
rep(i, 1, n){
cin >> ok[i] + 1;
}
rep(s, 0, (1 << n) - 1){
val = bpc(s) * (-x);
rep(i, 1, m){
cnt = 0;
rep(j, 0, n - 1){
if(geti(s, j)) cnt += (ok[j + 1][i] - '0');
}
if(cnt - y > 0) val += cnt - y;
}
ans = max(ans, val);
}
cout << ans;
return 0;
}
T3 P11476 [COCI 2024/2025 #3] 涂矩阵 / Bojanje
手模样例可以隐隐约约的感觉到,如果一行/列的颜色全部为 \(1/2\),那么这一行/列就可以在最后填。但是我们发现全部为 \(1/2\) 的就那么多,剩下的咋办?我们可以把最后填的一行/列的颜色在脑海中置为 \(-1\),也就是两种颜色都可以。然后就可以一直推下去。我们维护每一行/列的 \(1/2\) 的个数,每次从新 \(O(n)\) 遍历,把个数为 \(n\) 在原数组 \(a\) 中置为 \(-1\)。如果没有找到那我们直接判断有无解,那就是原数组 \(a\) 是否只由 \(-1/0\) 构成,如果不是,无解(此时已经找不到填的了,当然无解),有解就倒着输出答案。时间复杂度应该是 \(O(n^2)\),反正是能过。
# include <bits/stdc++.h>
# define vec vector
# define pb push_back
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
const int N = 2e3 + 10;
int n;
int a[N][N], row[3][N], col[3][N];
bitset<N> visrow, viscol;
struct node{
int type, rowcol, c;
};
vec<node> ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n){
rep(j, 1, n){
cin >> a[i][j];
}
}
rep(i, 1, n){
rep(j, 1, n){
row[1][i] += (a[i][j] == 1);
row[2][i] += (a[i][j] == 2);
}
}
rep(i, 1, n){
rep(j, 1, n){
col[1][i] += (a[j][i] == 1);
col[2][i] += (a[j][i] == 2);//初始化数组
}
}
while(1){
bool sol = false;
rep(c, 1, 2){
rep(i, 1, n){
if(row[c][i] == n && !visrow[i]){//前提是没填过
sol = true;
visrow[i] = true;
ans.pb({1, i, c});
rep(j, 1, n){
col[3 - c][j] ++;//相同颜色已经计算过,不能再次计算,所以3-c
a[i][j] = -1;//记得置为-1
}
}
}
}
rep(c, 1, 2){
rep(i, 1, n){
if(col[c][i] == n && !viscol[i]){
sol = true;
viscol[i] = true;
ans.pb({2, i, c});
rep(j, 1, n){
a[j][i] = -1;
row[3 - c][j] ++;
}
}
}
}
if(!sol){//没法填了
rep(i, 1, n){
rep(j, 1, n){
if(a[i][j] != 0 && a[i][j] != -1){//无解,你已经没法填了,当然是无解
cout << "-1";
return 0;
}
}
}
break;
}
}
cout << ans.size() << "\n";
while(!ans.empty()) cout << ans.back().type << " " << ans.back().rowcol << " " << ans.back().c << "\n", ans.pop_back();//倒着输出
return 0;
}
T4 P11477 [COCI 2024/2025 #3] 林卡树 / Stablo
这道题很唐,考场上我们机房都没有人开,以为是紫色的,后来发现我 \(10\) 分钟就能秒。
如果一个玩意带修,你可以先看它的不带修版本咋做,直接树形 DP:
void dfs(int u, int fa){
sum[u] = val[u];
each2(v, e[u]){
if(fa == v) continue;
dfs(v, u);
dp[u] += dp[v] + sum[v];
sum[u] += sum[v];
}
}
因为每次距离加 \(1\),所以说 DP 值就应该是子树 DP 和再加上子树的值和。
但是这玩意也不能说他是完全带修,因为每次操作都是在初始形态的树上操作。既然已经想到这一步,我们直接看操作。首先如果 \(x\) 是 \(y\) 的儿子,那直接输出 \(dp_y\)。否则我们看操作的影响,对于 \(dp_y\) 来说,由于 \(x\) 接在 \(z\) 上,那么需要先预处理跳 \(z\) 的倍增(不会可以学倍增 LCA),跳了以后,\(z\) 子树除 \(x\) 的子树全部深度加一,所以加 \(sum_z\) 再减去 \(sum_x\),而 \(x\) 多算了 \(deep_x - deep_y\) 次,减去 \((deep_x - deep_y) \times val_x\),最后再加上 \(val_x\),因为这时候 \(x\) 是 \(y\) 的儿子。预处理时间复杂度 \(O(n)\),单次询问跳倍增 \(O(\log n)\),总时间复杂 \(O(n + q \times \log n)\),可以通过此题。
# include <bits/stdc++.h>
# define int LL
# define vec vector
# define pb push_back
# define each1(i, x) for(auto (i) : (x))
# define each2(i, x) for(auto (&i) : (x))
# define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
# define pre(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
using LL = long long;
const int N = 5e5 + 10;
int n, q;
int ln[N], sum[N], val[N], dp[N], deep[N], f[N][20];
vec<int> e[N];
void init(){
ln[1] = 0;
rep(i, 2, N - 10){
ln[i] = ln[i >> 1] + 1;
}
}
void dfs(int u, int fa){
sum[u] = val[u];
deep[u] = deep[fa] + 1;
f[u][0] = fa;
rep(i, 1, ln[deep[u]]) f[u][i] = f[f[u][i - 1]][i - 1];//预处理
each2(v, e[u]){
if(fa == v) continue;
dfs(v, u);
dp[u] += dp[v] + sum[v];
sum[u] += sum[v];
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
init();
cin >> n >> q;
rep(i, 1, n) cin >> val[i];
rep(i, 2, n){
int f;
cin >> f;
e[i].pb(f);
e[f].pb(i);
}
dfs(1, 0);
while(q --){
int x, y;
cin >> x >> y;
if(deep[y] == deep[x] - 1){
cout << dp[y] << "\n";
continue;
}
int z = x;
int s = ln[deep[z]];//倍增
while(s >= 0){
if(deep[f[z][s]] >= deep[y] + 1) z = f[z][s];
s --;
}
cout << dp[y] + sum[z] - sum[x] - val[x] * (deep[x] - deep[y]) + val[x] << "\n";
}
return 0;
}
T5 P11478 [COCI 2024/2025 #3] 处理器 / Procesor
没补,不会。