跳到正文

个人记录

题目太冷门了于是到广场上求助

Eternity_x_z约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月20日
收录
2026年7月20日
更新
2026年7月20日
篇幅
约 2 分钟
获赞
1

版本

  1. v1当前

P3045

C++
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,k,m,ans;
priority_queue<ll,vector<ll>,greater<ll>> q;
struct Node
{
    ll p,c;
}a[50005];
bool cmp1(Node a,Node b)
{
    return a.c<b.c;
}
bool cmp2(Node a,Node b)
{
    return a.p<b.p;
}
int main()
{
    cin>>n>>k>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].p>>a[i].c;
    }
    sort(a+1,a+n+1,cmp1);
    for(int i=1;i<=k&&m>=a[i].c;i++)
    {
        m-=a[i].c;
        q.push(a[i].p-a[i].c);
        ans++;
    }
    sort(a+k+1,a+n+1,cmp2);
    for(int i=k+1;i<=n;i++)
    {
        if(q.empty())
        {
            if(m>=a[i].p)
            {
                m-=a[i].p;
                ans++;
            }
            continue;
        }
        ll t=q.top();
        if(a[i].p-a[i].c>t)
        {
            if(m<t+a[i].c) continue;
            m-=t+a[i].c;
            ans++;
            q.pop();
            q.push(a[i].p-a[i].c);
        }
        else
        {
            if(m<a[i].p) continue;
            m-=a[i].p;
            ans++;
        }
    }cout<<ans<<endl;
    return 0;
}

WA19 20 21点,反悔贪心的思路 P3045 P3045 P3045 重要的事情说三遍

原文