Yandex.Algorithm 2015
Yandex.Algorithm 2015 Round 1
問題文
$2$ 次元平面上に $N$ 個の村がある.
村 $i$ は座標 $(X_i, Y_i)$ にあって,人口は $A_i$ 人である.
どの $3$ つの村も同一直線上にはない.
直線で村を $2$ 分割した時の,人口の差の最小値を求める問題.
任意の $2$ つの村を選び,その近くを通る直線を考えて,$2$ 分割する方法を全て列挙しようとすると $O(N^3)$ 時間かかりTLEとなる.
そこで中心を $1$ つ固定する.
ある村 $i$ を中心として,その他の村はどの方角にあるか,$x$ 軸方向からの角度でソートする.
村 $i$ のすぐ近くを通る直線を考えると,偏角の差が $\pi$ 以下の部分を同じブロックにできるので,尺取法にて,全ての同じブロックにできる村の集合を列挙できる.
中心の村 $i$ はどちらのブロックに属しても良い.
中心となる村を全部試しても時間計算量 $O(N^2 \log N)$ でできる.
#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)
void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T1, class T2> void sort(int N, T1 a[], T2 b[], void *mem){int i;pair<T1,T2> *r=(pair<T1, T2>*)mem;rep(i,N)r[i].first=a[i],r[i].second=b[i];sort(r,r+N);rep(i,N)a[i]=r[i].first,b[i]=r[i].second;}
char memarr[27000000]; void *mem = memarr;
int N;
int X[1111], Y[1111];
int A[1111];
int dx[1111], dy[1111];
double th[2111]; int pop[2111];
int main(){
int i, j, k, m;
int res;
int sum, tmp, dif;
int st, ed;
double pi = acos(0) * 2;
reader(&N);
rep(i,N) reader(X+i,Y+i,A+i);
sum = 0;
rep(i,N) sum += A[i];
res = sum;
rep(i,N){
k = 0;
rep(j,N) if(j!=i){
th[k] = atan2(Y[j] - Y[i], X[j] - X[i]);
pop[k++] = A[j];
}
m = k;
rep(j,m){
th[j+m] = th[j] + 2*pi;
pop[j+m] = pop[j];
k++;
}
sort(k, th, pop, mem);
tmp = 0;
ed = 0;
rep(st,k){
while(ed < k && th[ed]-th[st] < pi) tmp+=pop[ed++];
if(ed == k) break;
dif = abs((sum - tmp) - tmp);
res = min(res, dif);
dif = abs((sum - (tmp+A[i])) - (tmp + A[i]));
res = min(res, dif);
tmp -= pop[st];
}
}
writerLn(res);
return 0;
}
Current time: 2024年04月27日04時22分01秒
Last modified: 2015年05月26日23時31分18秒 (by laycrs)
Tags: Competitive_Programming Yandex_Algorithm Yandex_Algorithm_2015 Yandex_Algorithm_2015_Round1
トップページに戻る
Logged in as: unknown user (not login)