题目
T2:礼物
我连边的对象错了,计算答案太猎奇了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int n, a[N], b[N], c[N], d[N], vis[N], vi[N], px, py, ans, sum;
vector<int> v[N], p, op, vv[N];
map<pair<int, int>, int> mp;
int d1(int x, int y){
if(a[x] != y) return max(c[x], d[x]) * b[y];
return max(c[y], d[y]) * b[x];
}
int d2(int x, int y){
if(a[x] != y) return min(c[x], d[x]) * b[y];
return min(c[y], d[y]) * b[x];
}
void dfs(int x, int fa){
if(vis[x]){
if(vis[x] == -1) return ;
for(int i = p.size() - 1; i >= 0; i--){ op.push_back(p[i]); if(p[i] == x) break; } px = x, py = op[0];
return;
}
vis[x] = 1, p.push_back(x);
for(int i : vv[x]) dfs(i, x);
vis[x] = -1, p.pop_back();
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
v[a[i]].push_back(i), v[i].push_back(a[i]), vv[i].push_back(a[i]), mp[{i, a[i]}]++, mp[{a[i], i}]++;
}
for(int i = 1; i <= n; i++) {
ans += max(c[a[i]], d[a[i]]) * b[i];
}
for(int i = 1; i <= n; i++){
if(!vis[i] && !vi[i]){
op.clear(), p.clear(), dfs(i, 0), sum /= 2;
int l = op.size();
int zrr = 0, fl = 0, s = 0, mn = 1e18; px = op[0], py = op.back();
for(int i = 1; i < l; i++){
int id = op[i]; zrr ^= (d[id] > c[id]), fl |= d[id] == c[id];
mn = min(mn, d1(op[i - 1], op[i]) - d2(op[i - 1], op[i]));
}
mn = min(mn, d1(px, py) - d2(px, py)); int idd = py;
if(!(fl || zrr == (d[idd] > c[idd]) || d[idd] == c[idd])) ans -= mn;
}
}
cout << ans;
return 0;
}T3:硬币难题
首先观察题面,若 连边最终的图就只有简单环与链,把环判掉后,先减掉必须选的然后对于链的后缀来做背包即可,这波被题面阴了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 310, M = 1e5 + 10, MOD = 1e9 + 7;
int n, q, m, a[N], in[N], dp[M], f[N], p[N];
int find(int x){
return x == f[x] ? x : f[x] = find(f[x]);
}
signed main(){
// freopen("coin.in", "r", stdin);
// freopen("coin.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q >> m;
for(int i = 1; i <= n; i++) cin >> a[i], f[i] = i;
for(int i = 1, x, y; i <= q; i++){
cin >> x >> y; int fx = find(x), fy = find(y);
if(fx == fy){ cout << "0"; return 0; }
p[y] = x, in[x]++, f[fy] = fx;
}
vector<int> op;
for(int i = 1; i <= n; i++){
if(in[i]) continue; int s = 0; vector<int> l;
for(int j = i; j; j = p[j]) l.push_back(a[j]);
for(int j = l.size() - 1; j >= 0; j--) m -= j * l[j], s += l[j], op.push_back(s);
} dp[0] = 1;
if(m < 0){
cout << "0";
return 0;
}
for(int i : op) for(int j = 0; j + i <= m; j++) (dp[j + i] += dp[j]) %= MOD;
cout << dp[m];
return 0;
}
T4:Event Hopping 2-[JOISC 2021] (Day4)
很神秘的一道题,虽然考场上没看。
首先,对于找 之中被完全包含且不重合的区间,我们暴力枚举右端点可以 ,然后用倍增优化一下就变成了 。
然后我们按下标从小到大枚举插入,每次就看插入后剩下的空间够不够塞下 个区间,够就可以选。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, k, l[N], r[N], h[N * 2], x, top, f[25][N * 2];
vector<int> ans;
int jump(int l, int r){ if(l >= r) return 0; int x = l, ans = 0;
for(int i = 20; i >= 0; i--) if(f[i][x] <= r) ans += (1 << i), x = f[i][x];
return ans;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> l[i] >> r[i], h[++top] = l[i], h[++top] = r[i];
sort(h + 1, h + 1 + top), top = unique(h + 1, h + 1 + top) - h - 1;
for(int i = 1; i <= n; i++) l[i] = lower_bound(h + 1, h + 1 + top, l[i]) - h, r[i] = lower_bound(h + 1, h + 1 + top, r[i]) - h;
fill(&f[0][0], &f[20][top] + 1, top + 1);
for(int i = 1; i <= n; i++) f[0][l[i]] = min(f[0][l[i]], r[i]);
for(int i = top - 1; i >= 1; i--) f[0][i] = min(f[0][i], f[0][i + 1]);
for(int i = 1; i <= 20; i++) for(int j = 1; j <= top + 1; j++) f[i][j] = f[i - 1][f[i - 1][j]];
if((x = jump(1, top)) < k){ cout << -1; return 0; }
set<pair<int, int>> s; s.insert({1, top});
for(int i = 1; i <= n && k; i++){
auto t = s.upper_bound({l[i], top + 1});
if(t == s.begin()) continue; t--;
int ll = (*t).first, rr = (*t).second;
if(ll <= l[i] && r[i] <= rr){
int zrr = x - jump(ll, rr) + jump(ll, l[i]) + jump(r[i], rr);
if(zrr >= k - 1) { k--, x = zrr, ans.push_back(i), s.erase(t); if(ll < l[i]) s.insert({ll, l[i]}); if(r[i] < rr) s.insert({r[i], rr}); }
}
}
for(int i : ans){
cout << i << '\n';
}
return 0;
}