poj 1456 Supermarket(貪心+並查集,貪心+優先佇列)
Time Limit: 2000MS | Memory Limit: 65536K |
Total Submissions: 7677 | Accepted: 3252 |
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∈SellFor 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
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. 題意:有n個商品,每個商品都有相應的價值pi和賣出去的最後期限di,每天只能賣一件商品。求最多能獲得的價值。 思路:貪心法。 法一:並查集優化:列舉每個商品,找出最接近該商品最後期限的未被佔用的一天來賣該商品。而要找出這一天可以用到並查集來快速查詢。 AC程式碼:#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
using namespace std;
struct node
{
int pi,di;
}pro[10005];
int fa[10005];
bool cmp(node a,node b)
{
return a.pi>b.pi;
}
void init()
{
for(int i=1;i<=10005;i++)
fa[i]=i;
}
int Find_set(int x)
{
if(x==fa[x]) return x;
fa[x]=Find_set(fa[x]);
return fa[x];
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d",&pro[i].pi,&pro[i].di);
sort(pro,pro+n,cmp);
init();
int sum=0;
for(int i=0;i<n;i++)
{
int rt=Find_set(pro[i].di);
if(rt>0)
{
sum+=pro[i].pi;
fa[rt]=rt-1;
}
}
printf("%d\n",sum);
}
return 0;
}
法二:優先佇列優化 AC程式碼:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
using namespace std;
struct node
{
int pi,di;
}pro[10005];
int fa[10005];
bool cmp(const node& p1,const node& p2){
return p1.di<p2.di;
}
bool operator < (const node& p1,const node& p2){
return p2.pi<p1.pi;
}
int main()
{
int n;
priority_queue<node>q;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d",&pro[i].pi,&pro[i].di);
sort(pro,pro+n,cmp);
int sum=0;
for(int i=0;i<n;i++)
{
if(pro[i].di>q.size())
{
q.push(pro[i]);
}
else if(pro[i].pi>q.top().pi)
{
q.pop();
q.push(pro[i]);
}
}
while(!q.empty())
{
sum+=q.top().pi;
q.pop();
}
printf("%d\n",sum);
}
return 0;
}
相關推薦
poj 1456 Supermarket(貪心+並查集,貪心+優先佇列)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7677 Accepted: 3252 Description A supermarket has a set Prod o
.Kruskal演算法 優先佇列+並查集,用優先佇列代替排序。
程式碼 /* 思路,將邊集加入到最小優先佇列,每次取出一個最小邊,如果邊的兩個端點有一個沒有訪問過,說明加入這條邊,就沒有構成環。可以加入。 突然發現這個思路是錯的。判斷是不是環,我的說法是錯誤的。還沒有想到解決辦法。 如果解決了:加入一條邊
POJ 1182 食物鏈(種類並查集)
Description 動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃A。 現有N個動物,以1-N編號。每個動物都是A,B,C中的一種,但是我們並不知道它到底是哪一種。 有人用兩種說法對這N個動物所構成的食物鏈關係進行描述:
HDU 3938 Portal (離線並查集,此題思路很強!!!,得到所謂的距離很巧妙)
rain multipl rtu int total als arc sep 順序 Portal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota
HDU5575 Discover Water Tank 2015上海現場賽D題 (樹形dp,並查集,左偏樹)
題目大意: 有一個1維的長度為N,高度無限的水櫃,現在要用N-1個擋板將其分為N個長度為1的小格,然後向水櫃中注水,水可以低於擋板也可以以溢位去(這樣就要與旁邊格子的水位相同),現在有M次探測,探測i,y,k為查詢第i個格子高度y+0.5處是否有水,k=1表示
使用並查集UnionFind和優先佇列PriorityQueue實現Kruskal演算法
拿到題目,先看看UnionFind 和 PriorityQueue 怎麼實現吧,誰讓上課沒好好聽呢。 Kruskal演算法是通過按照權值遞增的順序依次選擇圖中的邊,當邊不處於同一連通分量時加入生成樹,
Supermarket POJ - 1456 (貪心+並查集)
先將n個物品按價值降序排個序,從頭掃到尾,對於每一個物品i,判斷能不能在<=di的最大時間點賣掉。 #include<stdio.h> #include<algorithm> #include<iostream> #include<string
POJ 1456 (貪心+並查集)
題意:買賣N件東西,每件東西都有個截止時間,在截止時間之前買都可以,而每個單位時間只能買一件。問最大獲利。 思路:一開始我想錯了,以為每個東西只能在特定的時間買,結果是截止時間前買都可以,所以先對所有資料按利潤由大到小排序,然後用並查集來找截止時間的更新,也就是如果某個點的截止時間大於0,那麼截
poj 1456 Supermarket 單純貪心||貪心+優先佇列||貪心+並查集
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
POJ 1456 Supermarket [貪心+並查集]
題目地址 貪心:優先選利益最大的商品,從deadline向前推直到都放不了再選下一個商品利益最大的 關鍵是判斷哪一些日子不能再放了,最簡單的方法就是一個數組+一個for迴圈判斷 但更快的是用並查集,p=find(deadline) 當p>0,p就是可以放的日子 否則
POJ 1456 Supermarket【貪心+並查集】
Supermarket Time Limit:2000MS Memory Limit:65536K Total Submissions:11450 Accepted:5053 Description A supermarket has a set Prod of p
1456 Supermarket 貪心+並查集 /小根堆
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
poj 1703 Find them, Catch them(種類並查集和一種巧妙的方法)
ogr not 帶權並查集 drag single sca course first req Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions
poj 1733 Parity game(種類並查集)
scanf split class ber ont dsm 種類 uil this 題意: 有0或1構成的一段區間總長度為n。m個詢問,每次詢問一段區間1的個數為奇數還是偶數,問從第一個詢問開始,前幾個詢問正確的個數有幾個; 思路:
GYM 101173 F.Free Figurines(貪心||並查集)
efi can 初始 typedef 多余 一個 class type pri 原題鏈接 題意:俄羅斯套娃,給出一個初始狀態和終止狀態,問至少需要多少步操作才能實現狀態轉化 貪心做法如果完全拆掉再重裝,答案是p[i]和q[i]中不為0的值的個數。現在要求尋找最小步數,顯
POJ 2524 獨一無二的宗教(裸並查集)
路徑壓縮 tro not lines () using number rest targe 題目鏈接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory L
POJ 1733 Parity game(加權並查集)
題意:這是一個01的串,然後有m個類似於詢問的東西,每次詢問都告訴你這個區間的和為奇數還是偶數,讓你判斷正確的有幾句,如果不正確,直接跳出 思路:和華中科技大學的決賽差不多,我們將奇數設為1,偶數為0,那我們可以發現他們的奇偶性可以用異或代替,然後就穿一樣了,加上判斷條件就OK了,記得離散化 程式碼:
POJ 2492 - A Bug's Life (種類並查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that
【CCF 201703-4】地鐵修建(Kruskal 貪心 並查集)
題目抽象 修建一條結點1到結點n的一條路,使得這條路上最大的邊權最小 思路 從Kruskal演算法得到啟示,將邊按權重排序,不斷地加入最短的邊,直到結點1到結點n連通即可 判斷是否連通的方
zcmu——4941: 繼續暢通工程(貪心+並查集)
題目連結: #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> using names