1 条题解

  • 0
    @ 2025-10-8 17:12:41
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 2e5 + 5;
    struct node
    {
    	LL t, d;
    } a[N];
    bool cmp(node n1, node n2)
    {
    	if (n1.d != n2.d)
    		return n1.d < n2.d;
    	return n1.t < n2.t;
    }
    // 确保按结束时间排序,没有=
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while (T--)
    	{
    		int n;
    		scanf("%d", &n);
    		for (int i = 1; i <= n; i++)
    			scanf("%lld%lld", &a[i].d, &a[i].t), a[i].d += a[i].t;
    		sort(a + 1, a + n + 1, cmp); // 排序
    		LL t = 0, ans = 0;			 // t=时间 ans=计算能够做完作业的个数
    		priority_queue<LL> Q;
    		for (int i = 1; i <= n; i++)
    		{
    			Q.push(a[i].t); // 放入堆中
    			t = t + a[i].t; // 累加时间
    			ans++;			// 总作业数+1
    
    			if (t > a[i].d) // 若当前总时间超过截止时间
    			{
    				t = t - Q.top(); // 减去用时最长的作业时间
    				Q.pop();		 // 弹出该作业
    				ans--;			 // 总作业数-1
    			}
    		}
    		printf("%lld\n", ans);
    	}
    
    	return 0;
    }
    
    
    • 1

    *【反悔贪心】[USACO24DEC] Job Completion G

    信息

    ID
    6911
    时间
    2000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    380
    已通过
    52
    上传者