| ||||||||||
| 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 | |||||||||
贪心贪心
先将他们移动到同一横排,这横排应该是他们纵坐标的中位数,才能使得此过程总步数最少。然后要把他们紧凑起来。我们先把横坐标排序,并假设起点是a,那么我们就是要求abs(a+i - xi),i=1~n的加和。即abs(a-(xi-i)),i=1~n的加和。我们构建一个新数列zi = xi - i,当a等于z的中位数时原式的值最小
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX = 10010;
int x[MAX];
int y[MAX];
int z[MAX];
int main()
{
int N;
while (~scanf("%d", &N))
{
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(x));
memset(z, 0, sizeof(x));
for (int i = 0; i < N; i++)
{
scanf("%d%d", &x[i], &y[i]);
}
sort(x, x + N);
sort(y, y + N);
for (int i = 0; i < N; i++)
{
z[i] = x[i] - i;
}
sort(z, z + N);
int ans_x = z[N / 2], ans_y = y[N / 2], res = 0;
for (int i = 0; i < N; i++)
{
res += abs(z[i] - ans_x);
res += abs(y[i] - ans_y);
}
printf("%d", res);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator