http://acm.hdu.edu.cn/showproblem.php?pid=4311

思维呀 亲   你想到就可以做出来  想不到就做不出了 什么都不说了  上代码

不知道为什么 在hdu 上 long long 和 int 相乘就让我错

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<cmath>
#define LL long long

using namespace std;
const int N=100005;

struct node
{
    int I;
    LL x,y;
}mem[N];
LL l[N];
LL sumx[N];
LL sumy[N];
bool cmp1(node a,node b)
{
    return a.x<b.x;
}
bool cmp2(node a,node b)
{
    return a.y<b.y;
}
int main()
{
   int T;
   scanf("%d",&T);
   while(T--)
   {
       int n;
       scanf("%d",&n);
       for(int i=1;i<=n;++i)
       {
           cin>>mem[i].x>>mem[i].y;
           mem[i].I=i;
       }
       sort(mem+1,mem+n+1,cmp1);
       l[0]=0;
       for(int i=1;i<=n;++i)
       {
           l[i]=l[i-1]+mem[i].x;
       }
       for(int i=1;i<=n;++i)
       {
           sumx[mem[i].I]=((i-1)*mem[i].x-l[i-1])+((l[n]-l[i])-(n-i)*mem[i].x);
       }
       sort(mem+1,mem+n+1,cmp2);
       l[0]=0;
       for(int i=1;i<=n;++i)
       {
           l[i]=l[i-1]+mem[i].y;
       }
       for(int i=1;i<=n;++i)
       {
           sumy[mem[i].I]=((i-1)*mem[i].y-l[i-1])+((l[n]-l[i])-(n-i)*mem[i].y);
       }
       LL ans=sumx[1]+sumy[1];
       for(int i=2;i<=n;++i)
       {
           if(sumx[i]+sumy[i]<ans)
           ans=sumx[i]+sumy[i];
       }
       cout<<ans<<endl;
   }
   return 0;
}

  

posted on 2012-07-26 18:46  夜->  阅读(248)  评论(0编辑  收藏  举报