首页 > 其他 > 详细

POJ1456 Supermarket 贪心

时间:2016-05-05 12:41:33      阅读:178      评论:0      收藏:0      [点我收藏+]

贪心策略:一定先卖价值最大的,然后考虑卖当前的物品,卖的日期越靠后,越优,可以为以后的物品提供机会

技术分享
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=1e4+5;
struct Node{
   int v,d;
   bool operator<(const Node &rhs)const{
      return v>rhs.v;
   }
}p[N];
bool vis[N];
int main()
{
    int n;
    while(~scanf("%d",&n)){
      memset(vis,0,sizeof(vis));
      for(int i=1;i<=n;++i){
        scanf("%d%d",&p[i].v,&p[i].d);
      }
      sort(p+1,p+1+n);
      int ans=0;
      for(int i=1;i<=n;++i){
         for(int j=p[i].d;j>=1;--j)
          if(!vis[j]){ans+=p[i].v,vis[j]=1;break;}
      }
      printf("%d\n",ans);
    }
    return 0;
}
View Code

 

POJ1456 Supermarket 贪心

原文:http://www.cnblogs.com/shuguangzw/p/5461121.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!