跳到正文

个人记录

2026.8.17比赛总结&反悔贪心(来追梦)

zdxfh约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年8月17日
收录
2026年8月17日
更新
2026年11月17日
篇幅
约 9 分钟
获赞
1

版本

  1. v2当前
  2. v1

考试情况

模拟赛分数改题用时
T11001000 min0\ min
T220201 min1\ min
T3333313 min13\ min
T4303012 min12\ min
T50016 min16\ min

考题分析

P1090 [NOIP 2004 提高组] 合并果子

思路
模板题,每次取最小合并即可
代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int a[10005];
struct node
{
	int val;
	bool operator <(node y) const
	{
		return val>y.val;
	}
};
priority_queue<node> q;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
		q.push({a[i]});
	}
	int ans=0;
	while(q.size()>1)
	{
		int x=q.top().val;
		q.pop();
		int y=q.top().val;
		q.pop();
		ans+=x+y;
		q.push({x+y});
	}
	cout <<ans;
	return 0;
}

P11886 「Stoi2025」爱你没差

思路
跟第一题差不多,但是10*2的64次方会爆掉,所以在比较时需要两边同时除以m,还有一点就是一定要开unsigned long long,痛失80分。
代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define endl "\n"
int n,m;
int a[100000005];
priority_queue<int,vector<int>,greater<int> > q;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
		q.push(a[i]);
	}
	int ans=0;
	while(q.size()>1)
	{
		int x=q.top();
		q.pop();
		int y=q.top();
		q.pop();
		if(x*1.0>=y*1.0/m&&y*1.0>=x*1.0/m)
		{
			ans++;
		}
		q.push(x+y);
	}
	cout << ans;
	return 0;
}

P2949 [USACO09OPEN] Work Scheduling G

思路
先按截止时间排序,保证处理任务时,已选任务都能安排。

  • 有空位(size < d)→ 直接加入。
  • 没有空位的话如果当前利润 > 堆顶,就替换并更新答案;否则直接跳过。

代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int n;
struct node
{
	int d,val;
}a[1000005];
bool cmp(node x,node y)
{
	if(x.d==y.d)
	{
		return x.val>y.val;
	}
	return x.d<y.d;
}
priority_queue<int,vector<int>,greater<int> > q;
int d[100005],p[100005];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i].d>>a[i].val;
	} 
	sort(a+1,a+n+1,cmp);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i].d<=q.size())
		{
			if(q.top()<a[i].val)
			{
				ans+=a[i].val-q.top();
				q.pop();
				q.push(a[i].val);
			}
		}
		else
		{
			q.push(a[i].val);
			ans+=a[i].val;
		}
	}
	cout << ans;
	return 0;
}

P2107 小 Z 的 AK 计划

思路
反悔贪心,只要时间用完了就把用时最大的去掉,板子题。
代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
int temp;
int ans=-1;
struct node
{
    int x,t;
}a[100005];
bool cmp(node a,node b)
{
    return a.x<b.x;
}
priority_queue<int> q;
signed main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);  
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n,m;
    cin >>n >>m;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i].x>>a[i].t;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++) 
    {
        q.push(a[i].t);
        temp+=a[i].t;
        while(!q.empty()&&a[i].x+temp>m) 
        {
            temp-=q.top();
            q.pop();
        }
        ans=max(ans,(int)(q.size()));
    }
    cout << ans;
    return 0;
}

P9833 [USACO05OPEN] Expedition G

思路
这道题也是可以用反悔贪心,先算出上次位置到这个这个加油站的距离dist,如果当前剩余的油<dist,就去上限最大且没有加过油的加油站加油,一直加到可以到达这个加油站,然后打擂台比较q.size(),并且更新剩余的油,然后把这个加油站的上限加入队列中。
代码

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
pair<int,int> temp[100005];
pair<int,int> a[100005];
priority_queue<int> pq;
signed main() 
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) 
    {
        int d,f;
        cin >> d >> f;
        temp[i]={d,f};
    }
    int l,p;
    cin >> l >> p;
    for(int i=1;i<=n;i++)
    {
        int d=temp[i].first;
        int f=temp[i].second;
        a[i]={l-d,f};
    }
    a[n+1]={l,0};
    sort(a+1,a+n+2);
    int pos=0,you=p,ans=0;
    for(int i=1;i<=n+1;i++) 
    {
        pair<int,int> s=a[i];
        int dist=s.first-pos;
        while(you<dist) 
        {
            if(pq.empty())
            {
                cout << -1;
                return 0;
            }
            you+=pq.top();
            pq.pop();
            ans++; 
        }
        you-=dist; 
        pos=s.first;
        pq.push(s.second);
    }
    cout << ans;
    return 0;
}
原文