日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nyoj 208 Supermarket(贪心)

發(fā)布時間:2025/3/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nyoj 208 Supermarket(贪心) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Supermarket

時間限制:1000?ms ?|? 內(nèi)存限制:65535?KB 難度:4 描述
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.?

輸入
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.
輸出
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.
樣例輸入
4 50 2 10 1 20 2 30 17 20 1 2 1 10 3 100 2 8 25 20 50 10
樣例輸出
80

185


解題思路:這里用貪心的思想,對物品按照價值從大到小排序,然后每次選擇比當(dāng)前時間小的時間點,若不存在可用時間則不選。

最開始的想法是用dp,結(jié)果看錯題意了,目前還沒有想到dp的解法。。

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std;const int maxn=1e4+100; struct node {int pri;int time;bool operator <(const node& a)const{if(pri==a.pri)return time<a.time;return pri>a.pri;} }a[maxn]; int n; bool vis[maxn];int main() {while(scanf("%d",&n)!=EOF){memset(vis,0,sizeof(vis));for(int i = 0; i < n; i++)scanf("%d %d",&a[i].pri,&a[i].time);sort(a,a + n);int ans = 0;for(int i = 0; i < n; i++)for(int j = a[i].time; j >= 1; j--)if(!vis[j]){vis[j] = 1;ans += a[i].pri;break;}printf("%d\n",ans);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的nyoj 208 Supermarket(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。