poj 3544 Journey with Pigs---贪心

来源:互联网 发布:网络诈骗1000元立案吗 编辑:程序博客网 时间:2024/05/29 16:29

每头猪在某个村庄的单价都是固定的。

真是无法理解贪心中的条件按什么方法去处理。

这个处理好重要啊 好难感受啊。。


第i 个物品在第 j 个村庄卖出的收益是:

        cost[j]*w[i]-t*w[i]*d[j]

即 w[i]*(cost[j]-t*d[j])

总收益相当于 ∑ a[i]*b[j] ,问题等价于不等式排序问题。


如何取得最大的收益呢

根据。。

“不等式排序是这样的:
假设有两个数列,a1 <= a2 <= a3 <= ... <= an, b1 <= b2 <= b3 <= ... <= bn,设c1, c2 ,c3 ... cn是b1,b2,b3,,,bn的任意一个排列,则a1 * bn + a2 * b(n-1) +... + an * b1 <= a1 *c1 + a2 * c2 + ... + an * cn <= a1 * b1 + a2 * b2 + ... + an * bn,概括起来就是反序和 <= 乱序和 <= 顺序和。”

可得,此题中要求的显然是顺序和。分别排序后对号入座就是了。



#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3fusing namespace std;struct node1{    __int64 w;    __int64 id;}pig[1010];struct node2{    __int64 c;    __int64 id;}cost[1010];__int64 dis[1010],n,t,i,cc,ans[1010];bool cmp1(node1 a,node1 b){    return a.w>b.w;}bool cmp2(node2 a,node2 b){    return a.c>b.c;}int main(){    while(~scanf("%I64d%I64d",&n,&t))    {        for(i=0;i<n;i++)        {            scanf("%I64d",&pig[i].w);            pig[i].id=i+1;        }        for(i=0;i<n;i++)            scanf("%I64d",&dis[i]);        for(i=0;i<n;i++)        {            scanf("%I64d",&cc);            cost[i].c=cc-t*dis[i];            cost[i].id=i+1;        }        sort(pig,pig+n,cmp1);        sort(cost,cost+n,cmp2);        for(i=0;i<n;i++)            ans[cost[i].id]=pig[i].id;        for(i=1;i<n;i++)            printf("%d ",ans[i]);        printf("%d\n",ans[i]);    }    return 0;}


0 0
原创粉丝点击