| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:同问求x的算法~In Reply To:Re:同问求x的算法~ Posted by:wpc0000 at 2006-12-30 11:03:55 > x: 要将所有的x1集中到某个x0。其他xi依次相间1排下去。
> 先将x排序,可以证明x的顺序一定就是最终的序列的顺序(因为交叉位置的话解更差)。
> 由于定了序,所以有xi = x0 + i - 1,则可以将问题转化为x'i = xi - (i - 1) = x0。
> x'就是与y同样的问题了,求xi - (i - 1)的中位数x0就可以了。
thx...一开始以为自己想的是对的..你这思路很好..
帖个刚ac代码:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int absd(int n)
{
if(n>=0)
return n;
return -n;
}
int main()
{
// ifstream cin("0.txt");
int n,i,aa[10000],bb[10000];
cin>>n;
for(i=0;i<n;i++)
cin>>aa[i]>>bb[i];
sort(aa,aa+n);
sort(bb,bb+n);
for(i=0;i<n;i++)
aa[i]-=i;
sort(aa,aa+n);
unsigned long long sum=0;
for(i=0;i<n/2;i++)
sum+=bb[n-1-i]-bb[i]+aa[n-1-i]-aa[i];
cout<<sum<<endl;
// system("pause");
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator