Cod sursa(job #458973)

Utilizator hendrikHendrik Lai hendrik Data 27 mai 2010 14:02:55
Problema Inundatii Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;
bool debug;
#define MAXN 50010
int n,x[MAXN][3],lo,hi,mid1,mid2;
ll ans;

void open(){
	freopen("inundatii.in","r",stdin);
	freopen("inundatii.out","w",stdout);
}

ll f(int a,int b){
	ll res=0;
	for (int i=0;i<n;i++){
		res+=(ll)abs(x[i][b]-(a+i));
	}
	return res;
}

int main(){
	debug=0;
	if (!debug) open();
	scanf("%d",&n);
	for (int i=0;i<n;i++){
		for (int j=0;j<3;j++){
			scanf("%d",&x[i][j]);
		}
	}
	ans=0;
	for (int i=0;i<3;i++){
		lo=0;hi=100000000;
		while (lo<hi){
			mid1=(2*lo+hi)/3;
			mid2=(lo+hi*2)/3;
			if (f(mid1,i)<=f(mid2,i)){
				hi=mid2-1;
			}
			else lo=mid1+1;
		}
		ans+=f(lo,i);
	}
	printf("%lld\n",ans);
	if (debug) system("pause");
	return 0;
}