(贪心+并查集综合)[POJ1456] Supermarket
Time Limit: 2000MS | Memory Limit: 65536K | |
---|---|---|
Total Submissions: 20725 | Accepted: 9192 |
Description
A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit.
For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80.
Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products.
Input
A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.
Output
For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.
Sample Input
4 50 2 10 1 20 2 30 1
7 20 1 2 1 10 3 100 2 8 2
5 20 50 10
Sample Output
80
185
Hint
The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.
Source
解题思路
代码样例
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 10000 + 5;
pair<ll, ll> list[maxn];
ll pa[maxn];
bool cmp(pair<ll, ll> a, pair<ll, ll> b)
{
return a.first > b.first;
}
ll find_root(ll x)
{
int x_root = x;
while (pa[x_root] != x_root)
x_root = pa[x_root];
int i = x, j;
while (pa[i] != x_root)
{
j = pa[i];
pa[i] = x_root;
i = j;
}
return x_root;
}
int main()
{
int t;
while (cin >> t)
{
for (int i = 1; i <= t; i++)
cin >> list[i].first >> list[i].second;
for (int i = 1; i < maxn; i++)
pa[i] = i;
sort(list + 1, list + 1 + t, cmp);
int ans = 0;
for (int i = 1; i <= t; i++)
{
int root_ = find_root(list[i].second);
if (root_ == 0)
continue;
else
{
pa[root_] = find_root(root_ - 1);
ans += list[i].first;
}
}
cout << ans << endl;
}
//system("pause");
return 0;
}
原文地址:https://www.cnblogs.com/cafu-chino/p/11759434.html
- Golang语言调用百度帐号
- Golang语言 加密系列之AES
- 转--Golang语言 rpc 简单范例
- Golang语言 之网络
- Golang语言作为服务器,H5作为前端的视频传输
- Pandas——高效的数据处理Python库
- Oracle中的段(r10笔记第81天)
- 转-- Golang中timer定时器实现原理
- Golang语言 -并行程序
- 深度学习中的优化问题以及常用优化算法
- GoldenGate简单复制环境的搭建(r10笔记第79天)
- 在Golang语言中统计程序执行时间
- 经典面试问题: Top K 之 ---- 海量数据找出现次数最多或,不重复的。
- 每天一个Linux命令(2):cd命令
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 详解Linux下crontab的使用与注意事项
- Linux内核设备驱动之Linux内核基础笔记整理
- Ubuntu18.04 Server版安装及使用(图文)
- Ubuntu18.04安装vsftpd的实现代码
- ubuntu系统theano和keras的安装方法
- Linux安装Jenkins步骤及各种问题解决(页面访问初始化密码)
- 解决Ubuntu19 安装Theano问题
- centos7 esxi6.7模板实际应用详解
- Centos8搭建本地Web服务器的实现步骤
- 总结Linux 6种日志查看方法
- Ubuntu18.04一次性升级Python所有库的方法步骤
- linux下php安装xml扩展的详细步骤
- 查看linux文件的命令详解
- 解决Linux+Apache服务器URL区分大小写问题
- Centos8(最小化安装)全新安装Python3.8+pip的方法教程