POJ-3244 Difference between Triplets

Difference between Triplets POJ - 3244

分析
對於三元組{x1,y1,z1},{x2,y2,z2} ,我們設

a1=x1x2b1=y1y2c1=z1z2

然後,在數軸上畫出a1,b1,c1 ,可以發現maxmin=|a1b1|+|b1c1|+|c1a1|2
那麼這裏就可以先預處理出所有的ai,bi,ci ,然後升序排序,可以發現對於ai,aj 如果ai>aj 那麼在計算時,ai 應該是正的,而aj 應爲負的,那麼我們分開枚舉,對於ai 它貢獻了(i1) 次正數,貢獻了(ni) 次負數,那麼總貢獻就爲2in1
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int MAXN=2000000+10;

LL a[MAXN],b[MAXN],c[MAXN];
LL n,ans,a1,b1,c1;

bool cmp(const int A,const int B){return A<B;}

int main()
{
    while(scanf("%lld",&n)!=EOF)
    {
        if(n==0)    return 0;
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        memset(c,0,sizeof c);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld%lld",&a1,&b1,&c1);
            a[i]=a1-b1,b[i]=b1-c1,c[i]=c1-a1;
        }
        sort(a+1,a+n+1,cmp);
        sort(b+1,b+n+1,cmp);
        sort(c+1,c+n+1,cmp);
        ans=0;
        for(int i=1;i<=n;i++)
            ans=ans+(2*i-n-1)*(a[i]+b[i]+c[i]);
        printf("%lld\n",ans/2);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章