zoukankan      html  css  js  c++  java
  • G

    题目大意:

          超市里有很多商品,商品有价格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  
    まだまだだね
  • 相关阅读:
    转载、收藏
    AndroidStudio 问题收集
    android4.4 添加快捷开关(以截屏为例)
    打开USB调试功能
    关于Http通信
    【原创】JMS生产者和消费者【PTP同步接收消息】
    【学习】JMS通信模式
    【原创】Unable to read TLD "META-INF/c.tld" from JAR file 解决方法
    【原创】websphere部署war包报错
    【转载】tom的RUNSTATS测试工具
  • 原文地址:https://www.cnblogs.com/xxQ-1999/p/7469766.html
Copyright © 2011-2022 走看看