反悔贪心1

· · 个人记录

基础

反悔贪心的本质:

  1. 能买就买
  2. 买不了的话,就找一个等价的东西(已经买的),如果更优,那么就替换。

因为个数等价,但是价格更优,所以很显然是对的。

例题

P3545 [POI 2012] HUR-Warehouse Store

这道题能满足就先满足,如果不够了,就把之前要的最多的人不满足,满足这个人。但是需要那个人要的比现在要的多。

#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
int a[250005] , b[250005] ;
struct node
{
    int val , id ;
    bool operator < (const node &a) const
    {
        return val < a.val ;
    }
};
priority_queue<node> pq ;
void solve()
{
    cin >> n ;
    for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
    for( int i = 1 ; i <= n ; i++ ) cin >> b[i] ;
    int sum = 0 ;
    set<int> st ;
    for( int i = 1 ; i <= n ; i++ )
    {
        sum += a[i] ;
        while(sum < b[i] && !pq.empty() && pq.top().val > b[i])
        {
            sum += pq.top().val ;
            st.erase(pq.top().id) ;
            pq.pop() ;
        }
        if(sum >= b[i])
        {
            sum -= b[i] ;
            st.insert(i) ;
            pq.push({b[i] , i}) ;
        }
    }
    cout << st.size() << "\n" ;
    for( auto it : st )
    {
        cout << it << " " ;
    }
}
signed main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
    int oT_To = 1 ;
//  cin >> oT_To ;
    while(oT_To--)  solve() ;
    return 0 ;
} 

P2949 [USACO09OPEN] Work Scheduling G

考虑一个一个的加到每一天里来,如果这天已经超过了,那么就看看可不可以更优。

#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
struct node
{
    int d , p ;
    bool operator < (const node &a) const
    {
        return d < a.d ;
    }
}a[100005] ;
int nw[100005] ;
priority_queue<int , vector<int> , greater<int> > pq ;
void solve()
{
    cin >> n ;
    for( int i = 1 ; i <= n ; i++ )
    {
        cin >> a[i].d >> a[i].p ;
    }
    sort(a + 1 , a + n + 1) ;
    int as = 0 ;
    for( int i = 1 ; i <= n ; i++ )
    {
        if(pq.size() < a[i].d)
        {
            pq.push(a[i].p) ;
//          cout << pq.top() ;
        }
        else
        {
            if(pq.top() < a[i].p)
            {
                pq.pop() ;
                pq.push(a[i].p) ;
            }
        }
    }
    while(!pq.empty())
    {
        as += pq.top() ;
        pq.pop() ;
    }
    cout << as ;    
}
signed main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
    int oT_To = 1 ;
//  cin >> oT_To ;
    while(oT_To--)  solve() ;
    return 0 ;
} 

P4053 [JSOI2007] 建筑抢修

和第一题一样,但是不知道问什么挂了,换了一个写法。

#include<bits/stdc++.h>
using namespace std ;
#define int long long
int n ;
struct node
{
    int t1 , t2 ;
    bool operator < (const node &a) const
    {
        return t2 < a.t2 ;
    }
}a[150005] ;
priority_queue<int> pq ;
void solve()
{
    cin >> n ;
    for( int i = 1 ; i <= n ; i++ )
    {
        cin >> a[i].t1 >> a[i].t2 ;
    }
    sort(a + 1 , a + n + 1) ;
    int nw = 0 , as = 0 ;
    for( int i = 1 ; i <= n ; i++ )
    {
        nw += a[i].t1 ;
        as++ ;
        pq.push(a[i].t1) ;
        if(nw > a[i].t2)
        {
            nw -= pq.top() ;
            as-- ;
            pq.pop() ;
        }
    }
    cout << as ;
}
signed main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
    int oT_To = 1 ;
//  cin >> oT_To ;
    while(oT_To--)  solve() ;
    return 0 ;
}