G - Supermarket POJ - 1456

题目大意:

      超市里有很多商品,商品有价格pi和保质期di,卖一件商品需要一天,问最多能获利多少钱?

      多组测试。每组的第一个数字代表n种商品,接下来每一对数字代表一种商品的价格和保质期。0<=n<=10000,1<=pi<=10000,1<=di<=10000。

解题思路:

      偷偷用了贪心过了。并查集优化暂时不写(有空补上)。

      不可以简单的想把商品按照时间排序然后按照价格排序。(开头是这么想的 ,哈哈头脑简单)

      先把商品按照价格排序,然后开一个数组代表时间,如果时间被占了,就往前面的时间去找。

参考代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <map>
 4 #include <string>
 5 #include <queue>
 6 #include <stack>
 7 #include <set>
 8 #include <algorithm>
 9 
10 #include <cstdio>
11 #include <cstring>
12 #include <cmath>
13 #include <cstdlib>
14 using namespace std;
15 
16 const int INF=0x3f3f3f3f;
17 const int SIZE=1e4+10;
18 
19 int vis[SIZE];
20 int n;
21 
22 struct node{
23     int pri;
24     int dl;
25 }goods[SIZE];
26 
27 bool cmp(node a,node b)
28 {
29     if(a.pri==b.pri)  return a.dl<b.dl;
30     return a.pri>b.pri;
31 }
32 
33 int main()
34 {
35     int a,b;
36 
37     while(cin>>n)
38     {
39         memset(vis,0,sizeof(vis));
40         for(int i=1;i<=n;i++)
41             cin>>goods[i].pri>>goods[i].dl;
42         sort(goods+1,goods+n+1,cmp);
43         long long sum=0;
44         for(int i=1;i<=n;i++)
45         {
46             if(vis[goods[i].dl]==0)
47             {
48                 vis[goods[i].dl]=1;
49                 sum+=goods[i].pri;
50             }
51             else
52             {
53                 for(int j=goods[i].dl-1;j>=1;j--)
54                 if(vis[j]==0){vis[j]=1;sum+=goods[i].pri;break;}
55             }
56         }
57         cout<<sum<<endl;
58     }
59     return 0;
60 
61 }
62  

 

posted @ 2017-09-01 20:42  小可爱的小可爱  阅读(162)  评论(0编辑  收藏  举报