反悔自动机
例题
P14361 [CSP-S 2025] 社团招新
先把每个人放入其最大的满意度的项目。如果有一组超过了,就按照每个人到他第二大的差值最小到大求出来,插到对应的里面去即可。
//Code by hhy
#include<bits/stdc++.h>
#define int long long
using namespace std ;
int n ;
struct node
{
int a1 , a2 , a3 ;
}a[100005] ;
struct Node
{
int x , id ;
bool operator < (const Node &a) const
{
return x > a.x ;
}
};
priority_queue<Node> pq1 , pq2 , pq3 ;
void solve()
{
cin >> n ;
for( int i = 1 ; i <= n ; i++ ) cin >> a[i].a1 >> a[i].a2 >> a[i].a3 ;
int as = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
if(a[i].a1 == max({a[i].a1 , a[i].a2 , a[i].a3}))
{
as += a[i].a1 ;
pq1.push({a[i].a1 - max(a[i].a2 , a[i].a3) , i}) ;
}
else if(a[i].a2 == max({a[i].a1 , a[i].a2 , a[i].a3}))
{
as += a[i].a2 ;
pq2.push({a[i].a2 - max(a[i].a1 , a[i].a3) , i}) ;
}
else
{
as += a[i].a3 ;
pq3.push({a[i].a3 - max(a[i].a2 , a[i].a1) , i}) ;
}
}
while(pq1.size() > n / 2)
{
int id = pq1.top().id , x = pq1.top().x ;
pq1.pop() ;
as -= x ;
}
while(pq2.size() > n / 2)
{
int id = pq2.top().id , x = pq2.top().x ;
pq2.pop() ;
as -= x ;
}
while(pq3.size() > n / 2)
{
int id = pq3.top().id , x = pq3.top().x ;
pq3.pop() ;
as -= x ;
}
while(!pq1.empty())
{
pq1.pop() ;
}
while(!pq2.empty())
{
pq2.pop() ;
}
while(!pq3.empty())
{
pq3.pop() ;
}
cout << as << "\n" ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}
CF730I Olympiad in Programming and Sports
我们先考虑把体育值最大的一些体育生先选上,在考虑信息生。
信息生选有两种办法:
- 直接从剩下的里面选出信息值最大的
- 从体育生里面求出增加的最大的,再把剩下的里面选出值最大的体育生加进去
答案就是每次取最大值即可。
//Code by hhy
#include<bits/stdc++.h>
#define int long long
using namespace std ;
int n , p , s ;
struct node
{
int a1 , a2 ;
}a[3005] ;
struct Node
{
int x , id ;
bool operator < (const Node &a) const
{
return x < a.x ;
}
};
priority_queue<Node> pq1 , pq2 , hn ;
int vis[3005] ;
void solve()
{
cin >> n >> p >> s ;
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i].a1 ;
pq1.push({a[i].a1 , i}) ;
}
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i].a2 ;
pq2.push({a[i].a2 , i}) ;
}
int as = 0 ;
for( int i = 1 ; i <= s ; i++ )
{
Node u = pq2.top() ;
pq2.pop() ;
vis[u.id] = 2 ;
hn.push({a[u.id].a1 - a[u.id].a2 , u.id}) ;
as += a[u.id].a2 ;
}
while(p--)
{
while(vis[pq1.top().id]) pq1.pop() ;
while(vis[pq2.top().id]) pq2.pop() ;
int tmp1 = pq1.top().x ;
int tmp2 = pq2.top().x + hn.top().x ;
if(tmp1 > tmp2)
{
as += tmp1 ;
vis[pq1.top().id] = 1 ;
pq1.pop() ;
}
else
{
as += tmp2 ;
vis[hn.top().id] = 1 ;
vis[pq2.top().id] = 2 ;
Node _1 = {a[pq2.top().id].a1 - a[pq2.top().id].a2 , pq2.top().id} ;
hn.pop() ;
pq2.pop() ;
hn.push(_1) ;
}
}
cout << as << "\n" ;
for( int i = 1 ; i <= n ; i++ )
{
if(vis[i] == 1) cout << i << " " ;
}
cout << "\n" ;
for( int i = 1 ; i <= n ; i++ )
{
if(vis[i] == 2) cout << i << " " ;
}
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}
U546879 股票交易
这道题我们可以这样考虑:
一开始我们得到了这个数
但是怎么实现呢?我们可以对于每个数都先入队,如果卖掉之后,把
我们分析一下,如果后面还有一个
这大概就是自动机吧。
//Code by hhy
#include<bits/stdc++.h>
#define int long long
using namespace std ;
int n ;
priority_queue<int , vector<int> , greater<int> > Q ;
void solve()
{
cin >> n ;
int as = 0 ;
for( int i = 1 ; i <= n ; i++ )
{
int x ;
cin >> x ;
Q.push(x) ;
if(!Q.empty() && x > Q.top())
{
as += x - Q.top() ;
Q.pop() ;
Q.push(x) ;
}
}
cout << as ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}
P10736 [SEERC 2020] Archeologists
暴力做法
我们设
注意用滚动数组,且记得清空(别问我怎么知道的)。
//Code by hhy
#include<bits/stdc++.h>
#define int long long
using namespace std ;
int n ;
int x ;
int dp[2][250005] ;
void solve()
{
cin >> n >> x ;
memset(dp , -0x3f , sizeof dp) ;
dp[1][1] = x ;
dp[1][0] = 0 ;
for( int i = 2 ; i <= n ; i++ )
{
cin >> x ;
for( int j = 0 ; j <= i ; j++ ) dp[i % 2][j] = -0x3f3f3f3f3f3f3f3f ;
for( int j = 0 ; j < i ; j++ )
{
if(dp[(i - 1) % 2][j] == -0x3f3f3f3f3f3f3f3f) continue ;
if(j > 0)
{
dp[i % 2][j - 1] = max(dp[i % 2][j - 1] , dp[(i - 1) % 2][j] + x * (j - 1)) ;
}
dp[i % 2][j] = max(dp[i % 2][j] , dp[(i - 1) % 2][j] + x * j) ;
dp[i % 2][j + 1] = max(dp[i % 2][j + 1] , dp[(i - 1) % 2][j] + x * (j + 1)) ;
}
}
cout << max(dp[n % 2][1] , dp[n % 2][0]) ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}
正解做法
这道题我们可以从差值不超过 1 开始入手。因为差值不超过 1,所以深度的差分数组的值只可能是 -1,0,1。我们还知道,设最终答案是
如果我们再加上一个
我们可以发现
但是想到深度不能是负数,所以一定是先出现一个 1,再出现一个 -1,正好相反(但老师说我只说对了一半),改一下即可。
//Code by hhy
#include<bits/stdc++.h>
#define int long long
using namespace std ;
int n ;
priority_queue<int> Q ;
int p[250005] ;
void solve()
{
cin >> n ;
for( int i = 1 ; i <= n ; i++ ) cin >> p[i] ;
for( int i = n ; i >= 1 ; i-- ) p[i] += p[i + 1] ;
int as = 0 ;
for( int i = 1 ; i <= n + 1 ; i++ )
{
Q.push(p[i]) ;
if(!Q.empty() && p[i] < Q.top())
{
as += Q.top() - p[i] ;
Q.pop() ;
Q.push(p[i]) ;
}
}
cout << as ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int oT_To = 1 ;
// cin >> oT_To ;
while(oT_To--) solve() ;
return 0 ;
}