LibreOJ #10008. 「一本通 1.1 练习 4」家庭作业 题解

2 篇文章 0 订阅
2 篇文章 0 订阅

原题地址

定睛一看,!这和前面例题一样罢,然后反手敲了个 O ( n t ) O(nt) O(nt)的贪心,可惜没看数据范围… 80 p t s 80pts 80pts,还以为是要卡常…
然后才发现需要用并查集优化。
首先是先把价值从大到小排序,然后瞎搞。
C o d e \color{blue}Code Code

# include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n;
struct node
{
	int t,fen;
}a[N];
int f[N];
int maxl=-1;
bool cmp(struct node x,struct node y) 
{
	return x.fen>y.fen;
}
int find(int x) 
{
	if(f[x]!=x) return f[x]=find(f[x]);
	return f[x];
}
inline int read(void) 
{
	int s=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)) 
	{
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(isdigit(ch)) 
	{
		s=(s<<1)+(s<<3)+ch-'0';
		ch=getchar();
	}
	return s*w;
}
int main(void) 
{
	n=read();
	for(int i=1;i<=n;i++) 
	{
		a[i].t=read(),a[i].fen=read();
		maxl=max(maxl,a[i].t);
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=maxl;i++) f[i]=i;
	int tot=0;
	for(int i=1;i<=n;i++)
	{
		int _find=find(a[i].t);
		if(_find) 
		{
			tot+=a[i].fen;
			f[_find]=_find-1;
		}
	}
	printf("%d\n",tot);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值