nyoj 208 Supermarket(贪心)
Supermarket
時間限制:1000?ms ?|? 內(nèi)存限制:65535?KB 難度:4 描述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.?
輸入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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nyoj 1216 整理图书(dp)
- 下一篇: nyoj 174 Max Sequenc