いいアレンジ。
http://yukicoder.me/problems/103
http://yukicoder.me/problems/115
問題
2人が1km離れた距離で通信できる無線機を持っている。
また、幾つかの中継機を中継することでさらに長い距離で通信できる。
無線機同士や、無線機と中継機は1kmまで通信でき、中継機同士は10kmまで通信できる。
N個の中継機の座標(X[i],Y[i])が与えられる。
2人が任意の位置に移動できるとき、通信できる最大距離を求めよ。
解法(easy)
N≦1000なので、2中継機の距離が10km以内か、総当たりでチェックできる。
そこで、まず互いに通信可能な中継機をUnion-Findで連結していく。
連結成分中で、最大距離となる中継機の対を求められれば、後はその距離に2を足したものが解である。
N==0の時は解が1となる点に注意。
class UF { public: static const int ufmax=1052; int ufpar[ufmax],ufrank[ufmax],ufcnt[ufmax]; UF() { init();} void init(){int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; ufcnt[i]=1; } } int find(int x) { return (ufpar[x]==x)?(x):(ufpar[x] = find(ufpar[x]));} int operator[](int x) {return find(x);} int count(int x) {return ufcnt[find(x)];} void unite(int x,int y) { x = find(x); y = find(y); if(x==y) return; if(ufrank[x]<ufrank[y]) ufpar[x]=y, ufcnt[y]+=ufcnt[x]; else {ufpar[y]=x; ufcnt[x]+=ufcnt[y]; if(ufrank[x]==ufrank[y]) ufrank[x]++;} } }; int N; int X[1001],Y[1001]; UF uf; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) cin>>X[i]>>Y[i]; FOR(x,N) FOR(y,N) if((X[x]-X[y])*(X[x]-X[y])+(Y[x]-Y[y])*(Y[x]-Y[y])<=100) uf.unite(x,y); double ma=1; if(N>0) ma=2; FOR(x,N) FOR(y,N) if(uf[x]==uf[y]) ma=max(ma,2+sqrt((X[x]-X[y])*(X[x]-X[y])+(Y[x]-Y[y])*(Y[x]-Y[y]))); _P("%.12lf\n",ma); }
解法(hard)
こちらはNが120,000まで増えたため、2中継機の距離の総当たりはできない。
ここで、中継機が直接通信する距離が10kmと小さいことを用いて、直接通信する中継機の範囲を絞りこむ。
まず中継機をX座標ごとに分類する。
そして(X[i],Y[i])の中継機に対しては、(X[i],Y[i]-10)~(X[i]+10,Y[i]+10)の範囲にある中継機だけ判定すればよい。
上記座標の範囲に含まれる中継機は200個程度なので、120,000*200の総当たりをしても間に合う。
上記処理でUnion-Findにより中継機の連結成分を求める。
次に連結成分内の最遠点の対を求めるが、これも総当たりするとO(N^2)かかりTLEする。
そこで(X[i],Y[i])の範囲が狭いことを利用し、これらの点の凸包を求め、凸包の頂点間のみ最遠点判定を行えばよい。
class UF { public: static const int ufmax=120200; int ufpar[ufmax],ufrank[ufmax],ufcnt[ufmax]; UF() { init();} void init(){int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; ufcnt[i]=1; } } int find(int x) { return (ufpar[x]==x)?(x):(ufpar[x] = find(ufpar[x]));} int operator[](int x) {return find(x);} int count(int x) {return ufcnt[find(x)];} void unite(int x,int y) { x = find(x); y = find(y); if(x==y) return; if(ufrank[x]<ufrank[y]) ufpar[x]=y, ufcnt[y]+=ufcnt[x]; else {ufpar[y]=x; ufcnt[x]+=ufcnt[y]; if(ufrank[x]==ufrank[y]) ufrank[x]++;} } }; ll veccross(pair<int,int> p1,pair<int,int> p2,pair<int,int> p3) { p3.first-=p1.first;p2.first-=p1.first; p3.second-=p1.second;p2.second-=p1.second; return p3.first*(ll)p2.second-p2.first*(ll)p3.second; } vector<int> convex_hull(vector< pair<int, int> >& vp) { vector<pair<pair<int, int>, int> > sorted; vector<int> res; int i,k=0,rb; if(vp.size()<=2) { if(vp.size()>=1) res.push_back(0); if(vp.size()>=2) res.push_back(1); return res; } FOR(i,vp.size()) sorted.push_back(make_pair(vp[i],i)); sort(sorted.begin(),sorted.end()); res.resize(vp.size()*2); /* bottom */ FOR(i,vp.size()) { while(k>1 && veccross(vp[res[k-2]],vp[res[k-1]],sorted[i].first)<=0) k--; res[k++]=sorted[i].second; } /* top */ for(rb=k, i=vp.size()-2;i>=0;i--) { while(k>rb && veccross(vp[res[k-2]],vp[res[k-1]],sorted[i].first)<=0) k--; res[k++]=sorted[i].second; } res.resize(k-1); return res; } int N; int X[120001],Y[120001]; UF uf; map<int,int> YS[20015]; vector<pair<int,int> > V[120001]; void solve() { int i,j,k,l,r,x,y,y2,x2; cin>>N; FOR(i,N) { cin>>X[i]>>Y[i], X[i]+=10001,Y[i]+=10001; YS[X[i]][Y[i]]=i; } FOR(x,20002) { ITR(it,YS[x]) { int cur=it->first; map<int,int>::iterator it2=it; for(it2++;it2!=YS[x].end();it2++) { if(it2->first-it->first>10) break; uf.unite(it->second,it2->second); } for(x2=x+1;x2<=x+10;x2++) { for(map<int,int>::iterator it2=YS[x2].lower_bound(it->first-10); it2!=YS[x2].end();it2++) { if(it2->first<it->first && (x2-x)*(x2-x)+(it->first-it2->first)*(it->first-it2->first)>100) continue; if((x2-x)*(x2-x)+(it->first-it2->first)*(it->first-it2->first)>100) break; uf.unite(it->second,it2->second); } } } } FOR(i,N) V[uf[i]].push_back(make_pair(X[i],Y[i])); double ma=1; FOR(i,N) if(V[i].size()) { if(V[i].size()==1) ma=max(ma,2.0); else if(V[i].size()==2) ma=max(ma,2+sqrt((V[i][0].first-V[i][1].first)*(V[i][0].first-V[i][1].first)+(V[i][0].second-V[i][1].second)*(V[i][0].second-V[i][1].second))); else { vector<int> ch=convex_hull(V[i]); FOR(x,ch.size()) for(y=x+1;y<ch.size();y++) ma=max(ma,2+sqrt((V[i][ch[x]].first-V[i][ch[y]].first)*(V[i][ch[x]].first-V[i][ch[y]].first)+(V[i][ch[x]].second-V[i][ch[y]].second)*(V[i][ch[x]].second-V[i][ch[y]].second))); } } _P("%.12lf\n",ma); }
まとめ
Hardは若干難しいけど、適度にテクニックが必要な一方、トリッキーな解法は不要で着実に実装力が試される良い練習問題。