题解:P8021 [ONTAK2015] Bajtman i Okrągły Robin
zhangzirui66 · · 题解
把它看成二分图,每一个区间都连上右边对应的。
问题在于带权,但是显然可以贪,先以抢劫价格单调不增排序,后面的东西去挤掉前面的不优。
然后就是类似匈牙利的东西,每次都让别人让一让,虽然是
#include<bits/stdc++.h>
#define int long long
using namespace std;
bool Begin;
const int inf = 0x3f3f3f3f3f3f3f3f, MAX_Memory = 2048;
struct node{
int l, r, k;
}a[1000005];
int n, id[1000005], pos[1000005], use[1000005], vis[1000005], now;
bool cmp(node x, node y){
return x.k > y.k;
}
bool dfs(int u){
vis[u] = now;
for(int i = a[u].l; i <= a[u].r; i ++){
if(!use[i]){
use[i] = 1; pos[u] = i; id[i] = u;
return 1;
}
}
for(int i = a[u].l; i <= a[u].r; i ++){
if(vis[id[i]] != now){
if(dfs(id[i])){
use[i] = 1; pos[u] = i; id[i] = u;
return 1;
}
}
}
return 0;
}
void Solve(int Case){
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i].l >> a[i].r >> a[i].k;
a[i].r --;
}
sort(a + 1, a + n + 1, cmp);
int ans = 0;
for(int i = 1; i <= n; i ++){
int flag = 0;
for(int j = a[i].l; j <= a[i].r; j ++){
if(!use[j]){
use[j] = 1; pos[i] = j; id[j] = i;
ans += a[i].k;
flag = 1; break;
}
}
if(!flag){
now = i;
if(dfs(i)){
ans += a[i].k;
}
}
}
cout << ans;
}
bool End;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i ++){
Solve(i);
}
cerr << "\nMemory Limit: " << abs(&End - &Begin) / 1048576 << "MB / " << MAX_Memory << "MB";
if(abs(&End - &Begin) > MAX_Memory * 1048576) cerr << "\nMemory is Not Enough!"; return 0;
}
update:这个东西也许可以接着优化,但笔者在加强版错了,有能指正的可以在评论区指导一下。
注意到一个区间如果挪不动后面加了更多区间后就更挪不动了,因此可以打上一个标记,复杂度来到