Supermarket

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34236#problem/F

题意:有n种商品,每个商品有一个价格,和最后售出时间,问价格最大是什么;

解析:运用贪心的思想,使用并查集来优化;

其余没什么说的,主要就是并查集将时间划分到同一区域中

注意:n=0不要跳出

// f.cpp :  定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn = 10005;

int fa[ maxn ], n, m, ans;

struct node{
	int pro, da;
	bool operator<(const node &x )const{
		return pro > x.pro;
	}
}edge[ maxn ];

void Start(){
	for( int i = 0; i < maxn; ++ i ){
		fa[ i ] = i;
	}
}

int find( int x ){
	if( x == fa[ x ] )
		return x;
	int temp = find( fa[ x ] );
	return fa[ x ] = temp;
}

void Union( int a, int b ){
	int x = find( a );
	int y = find( b );
	fa[ y ] = x;
}

//int _tmain(int argc, _TCHAR* argv[])
int main()
{
	while( cin >> n ){
		//if( !n )
		//	break;
		Start();
		for( int i = 0; i < n; ++i ){
			cin >> edge[ i ].pro >> edge[ i ].da;
		}
		sort( edge, edge + n );
		ans = 0;
		for( int i = 0; i < n; ++i ){
			int temp = find( edge[ i ].da );
			if( temp ){
				Union( temp - 1, temp );
				ans += edge[ i ].pro;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值