POJ1456 Supermarket 贪心

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

#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

 

posted @ 2016-05-05 11:10  shuguangzw  阅读(107)  评论(0编辑  收藏  举报