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
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.
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 10Sample Output
80 185Hint
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.
这道题我没有想到是那么多办法去做的,我只能想到单纯贪心,而优先队列和并查集优化我并不会,下面会呈现代码,这里重点讲一下并查集优化,利用并查集优化其实是在单纯贪心的基础上实现的,单纯贪心是暴力,而并查集通过压缩路径可以使往前遍历次数减少,例如说3个物品价值时间分别为 v=50 t=5 ,v=40 t=4,v=30 t=3,我们根据贪心我们是先选v=50的,那么father[5]=4,也就是说将这个时间点的父节点定义为t-1,依次类推father[4]=3,father[3]=2,那么我们再遇到一个v=20,t=5的时候,我们会找到t=5的根节点,回溯到2,因此father[5]=2,father[4]=2,father[3]=2,那么以后找再t=5的时候我们会直接回溯到2,而不是到4,那么优化的效果就出来了。
单纯贪心||贪心+并查集优化写在同一代码中
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
struct node
{
int t;
int x;
} a[10009];
int vis[10009];
int father[10009];
int cmp(node a,node b)
{
if(a.x==b.x) return a.t<b.t;
else return a.x>b.x;
}
int Find(int x)
{
if(x!=father[x]) father[x]=Find(father[x]);
return father[x];
}
/*int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
int i,sum=0,j;
for(i=0; i<=n-1; i++) scanf("%d%d",&a[i].x,&a[i].t);
sort(a,a+n,cmp);
for(i=0; i<=n-1; i++)
{
for(j=a[i].t; j>=1; j--)
{
if(!vis[j])
{
vis[j]=1;
sum+=a[i].x;
break;
}
}
}
printf("%d\n",sum);
}
}*/
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sum=0,i,j,x;
for(i=0;i<=10000;i++)
{
father[i]=i;
}
for(i=0;i<=n-1;i++)
{
scanf("%d%d",&a[i].x,&a[i].t);
}
sort(a,a+n,cmp);
for(i=0;i<=n-1;i++)
{
x=Find(a[i].t);
if(x>0)
{
sum+=a[i].x;
father[x]--;
}
}
printf("%d\n",sum);
}
}
优先队列优化,这是我看别人博客的,也自己去模拟了一下,也是比较巧妙
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 10005;
struct Node{
int px, dx;
friend bool operator<(const Node&a,const Node&b){
return a.dx>b.dx;
}
}arr[N];
priority_queue<int,vector<int>,less<int> >q;
int n;
int main(){
while(~scanf("%d",&n)){
int maxTime=0;
for(int i=0; i<n; ++i){
scanf("%d%d",&arr[i].px, &arr[i].dx);
if(arr[i].dx>maxTime) maxTime = arr[i].dx;
}
sort(arr,arr+n);
// printf("%d\n",maxTime);
//for(int i=0;i<=n-1;i++) printf("%d %d\n",arr[i].px,arr[i].dx);
int ans = 0, pos=0;;
while(!q.empty()) q.pop();
for(int t=maxTime; t>=1; --t){
while(pos<n&&arr[pos].dx>=t){
q.push(arr[pos++].px);
//printf("push==%d\n",arr[pos-1].px);
}
if(!q.empty()){
ans += q.top();
//printf("top==%d\n",q.top());
q.pop();
}
}
printf("%d\n", ans);
}
return 0;
}