2026.3.28课堂总结(贪心+优先队列)

· · 个人记录

P15413 [CCC 2019 J4/S1] Flipper 翻转

思路
一道简单到爆的纯模拟,按题意交换即可。
代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int mp[3][3];
    //初始化
    mp[1][1]=1;
    mp[1][2]=2;
    mp[2][1]=3;
    mp[2][2]=4;
    string s;
    cin >> s;
    for(int i=0;i<s.size();i++)
    {
        //水平翻转
        if(s[i]=='H')
        {
            swap(mp[1][1],mp[2][1]);
            swap(mp[1][2],mp[2][2]);
        }
        //垂直翻转
        else if(s[i]=='V')
        {
            swap(mp[1][1],mp[1][2]);
            swap(mp[2][1],mp[2][2]);
        }
    }
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            cout<<mp[i][j]<<' ';
        }
        cout << endl;
    }
    return 0;
}

P13646 [NOISG 2016] LunchBox

思路
一道反悔贪心模板题,只需要用大顶堆存储每个学校需付出餐盒数,用n-a_i,如果减到n<0了,就反悔,把之前给出的最多餐盘的学校踢出分配队列,ans-1,n+其餐盘数量。最后输出即可。
代码

#include <bits/stdc++.h>
using namespace std;
priority_queue<int> q;
int a[1000005];
int main() 
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) 
    {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) 
    {
        q.push(a[i]);// 把它的需求量加入大顶堆
        n -= a[i];// 消耗对应的饭盒
        ans++;// 学校数量 +1
        // 如果饭盒不够用了
        if (n < 0) 
        {
            // 因为它占用资源最多,去掉它能让总消耗减少最多,从而保留更多学校
            n += q.top();// 把饭盒还回去
            q.pop();// 从分配名单中移除这所学校
            ans--;// 学校数量 -1
        }
    }
    cout << ans;
    return 0;
}

P15024 [UOI 2020 II Stage] 修理帕萨特

思路
这道题是贪心+二分,做法如下:

check函数:

  1. 如果安排完后超过了该广告的截止日则返回false
  2. 如果当前 num < x+1,那么我们必须推迟到第 x+1 天才开始做这个广告 ,所以把 num 设为 x+1
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long 
    vector<pair<int,int>> a;
    int n, maxn = -1, ans;
    bool check(int x) 
    {
     int num = x;  // num 表示当前已经用到第 num 天
     for (auto &i : a) 
     {
         // 尝试安排这个广告
         num += i.first;
         // 如果安排完后超过了该广告的截止日
         if (num > i.second) 
         {
             return false;
         }
         // 不能在第 x 天之前工作
         if (num < x + 1) 
         {
             num = x + 1;
         }
     }
     return true;
    }
    bool cmp(pair<int,int> a, pair<int,int> b) 
    {
     return a.second < b.second;
    }
    signed main() 
    {
     ios::sync_with_stdio(false);
     cin.tie(0);
     cout.tie(0);
     cin >> n;
     for (int i = 0; i < n; i++) 
     {
         int x, y;
         cin >> x >> y;
         a.push_back({x, y});
         maxn = max(maxn, y);
     }
     // 按截止时间排序
     sort(a.begin(), a.end(), cmp);
     // 二分答案
     int l = 0, r = maxn;
     while (l <= r) 
     {
         int mid = (l + r) / 2;
         // 检查是否可以空闲 x 天后再开始工作
         if (check(mid)) 
         {
             ans = mid;   // 尝试更大的空闲天数
             l = mid + 1;
         }
         else 
         {
             r = mid - 1; // 减少空闲天数
         }
     }
     cout << ans;
     return 0;
    }

    P6473 [NOI Online #2 入门组] 未了

    思路
    为最少魔法使总时间>t,贪心选最大的a_i ,排序后前缀和二分即可。在二分中可以用upper_bound 找第一个 > (t - y) 的前缀和,输出即可。记得特判即使使用所有魔法,总时间 y + s[n] <= t , 无法超过 t,就输出 -1。
    代码

    #include<bits/stdc++.h>
    using namespace std;
    double l, v, a[200005], s[200005], t, y, q;
    int n, i, ans;
    bool cmp(double r, double s)
    {
     return r > s;
    }
    int main() 
    {
     cin >> n >> l >> v;
     y = l / v;
     for (i = 1; i <= n; i++) 
     {
         cin >> a[i];
         a[i] /= v;  // 魔法触发后,会额外增加 a[i]/v 的时间
     }
     sort(a + 1, a + n + 1, cmp);
     for (i = 1; i <= n; i++) 
     {
         s[i] = s[i - 1] + a[i];
     }
     cin >> q;
     while (q--) 
     {
         cin >> t;
         // 基础时间 >= t , 不用魔法就满足,输出 0
         if (y >= t)
         {
             cout << 0 << endl;
         }
         //即使使用所有魔法,总时间 y + s[n] <= t , 无法超过 t,就输出 -1
         else if (s[n] + y <= t) 
         {
             cout << -1 << endl;
         }
         // 找最小的 k
         else 
         {
             ans = upper_bound(s + 1, s + n + 1, t - y) - s;// upper_bound 找第一个 > (t - y) 的前缀和
             cout << ans << endl;
         }
     }
     return 0;
    }

    P3611 [USACO17JAN] Cow Dance Show S

    思路
    这道题二分答案 K(舞台大小),用 check(k) 模拟: