跳到正文

个人记录

总结-7月11日-专项练习-贪心1

wangsy2014约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年7月11日
收录
2026年7月12日
更新
2026年11月11日
篇幅
约 7 分钟

版本

  1. v1当前

T1:

本题我们可以直接结构体排序,得到一个序列,然后选满mm种宝石,然后再尽量选大的。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mx=2e5+5;
int n,m,k,ans;
struct node{
	int c,v;
}a[mx];
bool cmp(node x,node y)
{
	return x.v>y.v;
}
bool mp[mx],mp2[mx];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n>>k>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].c>>a[i].v;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		if(m==0 || k==0)
		{
			break;
		}
		if(mp[a[i].c]==false)
		{
			mp[a[i].c]=true;
			mp2[i]=true;
			ans+=a[i].v;
			m--;
			k--;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(k==0)
		{
			break;
		}
		if(mp2[i]==false)
		{
			ans+=a[i].v;
			k--;
		}
	}
	cout<<ans<<'\n';
	return 0;
}

T2:

本题我们可以将那个数不断除以99及以下的数,如果剩下的大于等于1010,那么我们再将这些数字排序得到答案。

注意,如果这里只有一位数就需要在前面加一个1。

C++
#include<bits/stdc++.h>
using namespace std;
//const int mx=;
long long n,t;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>t;
	while(t--)
	{
		cin>>n;
		string ans="";
		for(int i=9;i>=2;i--)
		{
			if(n%i==0)
			{
				n/=i;
				ans=ans+char(i+'0');
				i++;
			}
		}
		sort(ans.begin(),ans.end());
		if(ans.size()<=1)
		{
			ans='1'+ans;
		}
		if(n!=1)
		{
			cout<<-1<<'\n';
		}
		else
		{
			cout<<ans<<'\n';
		}
	}
	return 0;
}

T3:

本体我们不妨倒着想:我们从后向前枚举天数(我是枚举剩余天数)
然后我们就可以直接将对应剩余天数的时间的进入队列,再弹出最值。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mx=1e5+5;
int n,m;
struct node{
	int a,b;
}a[mx];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
priority_queue<int>pq;
signed main()//signed
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].a>>a[i].b;
	}
	sort(a+1,a+n+1,cmp);
	int mid=1,ans=0;
	for(int i=1;i<=m;i++)
	{
		while(a[mid].a<=i && mid<=n)
		{
			pq.push(a[mid].b);
			mid++;
		}
		if(pq.size()==0)
		{
			continue;
		}
		ans+=pq.top();
		pq.pop();
	}
	cout<<ans;
	return 0;
}
/*



*/

T4:

本题我们可以发现:
一个海狸的能力值是两项及以上最高,那么就可以直接不选了,因为这样子,就一定会有一个海狸不满足条件。
如果三个队首元素(堆顶元素)都不一样,输出答案。

C++
#include<bits/stdc++.h>
using namespace std;
const int mx=2e5+5;
int n,a[mx],b[mx],c[mx];
struct node{
	int v,id;
	bool operator <(const node &k)const
	{
		return v<k.v;
	}
};
bool fl[mx];
priority_queue<node>qa,qb,qc;
int main()//signed
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i]>>c[i];
		qa.push({a[i],i});
		qb.push({b[i],i});
		qc.push({c[i],i});
	}
	while(qa.size() && qb.size() && qc.size())
	{
		node na=qa.top(),nb=qb.top(),nc=qc.top();
		if(fl[na.id])
		{
			qa.pop();
			continue;
		}
		if(fl[nb.id])
		{
			qb.pop();
			continue;
		}
		if(fl[nc.id])
		{
			qc.pop();
			continue;
		}
		if(a[nb.id]==na.v)
		{
			fl[nb.id]=true;
			qb.pop();
			continue;
		}
		if(a[nc.id]==na.v)
		{
			fl[nc.id]=true;
			qc.pop();
			continue;
		}
		if(b[na.id]==nb.v)
		{
			fl[na.id]=true;
			qa.pop();
			continue;
		}
		if(b[nc.id]==nb.v)
		{
			fl[nc.id]=true;
			qc.pop();
			continue;
		}
		if(c[na.id]==nc.v)
		{
			fl[na.id]=true;
			qa.pop();
			continue;
		}
		if(c[nb.id]==nc.v)
		{
			fl[nb.id]=true;
			qb.pop();
			continue;
		}
		cout<<na.v+nb.v+nc.v;
		return 0;
	}
	cout<<-1;
	return 0;
}
/*



*/
原文