反悔自动机

· · 个人记录

例题

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 股票交易

这道题我们可以这样考虑:

一开始我们得到了这个数 y,前面有一个数 x 比他小,就决定卖出。但是后面有一个数也可以买 z,因为 z>y,所以 y 在后面可能会被买入,因此考虑在 z 的时候卖出 x,y 在后面继续考虑。

但是怎么实现呢?我们可以对于每个数都先入队,如果卖掉之后,把 x 删掉,y 不仅不删还要增加一个。

我们分析一下,如果后面还有一个 z,就可以看成在 y 的时候买入,z 的时候卖出,及 y 的时候什么都没干,但是队里还有一个 y,所以后面还是可以进行一些操作。但如果后面没有,也没有什么影响。

这大概就是自动机吧。

//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

暴力做法

我们设 dp_{i,j} 表示为现在是第 i 个格子,挖的深度是 j,最大的贡献值。在转移时,分三种情况转移即可。初始化和输出时要初始化 0 和 1,统计答案时也要统计 0 和 1。

注意用滚动数组,且记得清空(别问我怎么知道的)。

//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。我们还知道,设最终答案是 ans:

设 suf_i=\sum_{j=i}^n p_j\\ \begin{aligned} ans&=\sum_{i=1}^n(\sum_{j=1}^i d_j)\times p_i\\ &=\sum_{i=1}^n(\sum_{j=i}^np_j)\times d_i\\ &=\sum_{i=1}^n d_i\times suf_i\\ \end{aligned}

如果我们再加上一个 d_{n+1}\times suf_{n+1},虽然结果不变但是有大用处。发现他最终都会变成 0,及 \sum_{i=1}^{n+1}d_i=0。那么 -1 和 1 的出现都是成对出现的。

我们可以发现 d_i 的 -1 就可以看成是买入,-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 ;
}