首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

2012多校联结2&&1001 hero 贪心

2012-08-29 
2012多校联合2&&1001 hero 贪心刚开始排序时有问题;后来看到楼长的解题告,感觉和我的思想一样;我的还要简

2012多校联合2&&1001 hero 贪心

刚开始排序时有问题;后来看到楼长的解题告,感觉和我的思想一样;我的还要简单一些:结果由于排序的问题wa了好多次;

#include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>#include <math.h>using namespace std;const int N=21;struct node{ long long x; long long y;  }a[N];bool cmp(node a,node b){ return (a.y+0.0)/a.x>(b.y+0.0)/b.x;}/* 刚开始时我是用的这样的排序方法结果wa的心碎了 下面是错误的排序 bool cmp(node a,node b) {   return a.y/a.x>b.y/b.x; } 这个改成上面的排序函数就行了 */int main(){/*freopen("1.txt","r",stdin); freopen("2.txt","w",stdout);*/    int n;while(~scanf("%d",&n)){long val=0;  for(int i=0;i<n;++i)  {   scanf("%d%d",&a[i].y,&a[i].x);      val+=a[i].y;  }  sort(a,a+n,cmp);       long long sum=0;      for(int i=0;i<n;i++)   {       sum += ( val * a[i].x );  val-=a[i].y;   }   printf("%lld\n",sum);}return 0;} 


热点排行