[Codeforces] 477 C. Stairs and Elevators
題目連結:http://codeforces.com/contest/967/problem/C
指有可能是走最近的樓梯或電梯其中一種,但要小心如果在同一層的話要特判。
指有可能是走最近的樓梯或電梯其中一種,但要小心如果在同一層的話要特判。
#include<bits/stdc++.h> #define debug(a) cout << #a << " = " << a << endl; #define pb push_back #define int long long #define all(v) (v).begin(),(v).end() using namespace std; typedef pair<int,int> pi; int n,m,cl,ce,v; vector<int> cll,cee; signed main() { cin >> n >> m >> cl >> ce >> v; for(int i = 0; i < cl; i++) { int a; cin >> a; cll.pb(a); } for(int i = 0; i < ce; i++) { int a; cin >> a; cee.pb(a); } sort(all(cll)); sort(all(cee)); int q; cin >> q; while(q--) { int x11,y11,x22,y22; cin >> x11 >> y11 >> x22 >> y22; swap(x11,y11); swap(x22,y22); if(y11 == y22) { cout << abs(x11-x22) << endl; continue; } int d1 = lower_bound(all(cll),x11)-cll.begin(); int d2 = upper_bound(all(cll),x11)-cll.begin()-1; int ans = 1e9; if(d1 != cll.size() && d1 != -1 && cll.size() > 0) ans = min(ans,abs(x11-cll[d1])+abs(cll[d1]-x22)+abs(y22-y11)); if(d2 != cll.size() && d2 != -1 && cll.size() > 0) ans = min(ans,abs(x11-cll[d2])+abs(cll[d2]-x22)+abs(y22-y11)); d1 = lower_bound(all(cee),x11)-cee.begin(); d2 = upper_bound(all(cee),x11)-cee.begin()-1; if(d1 != cee.size() && d1 != -1 && cee.size() > 0) { int kk = abs(y22-y11); if(kk%v == 0) kk /= v; else kk = kk/v+1; ans = min(ans,abs(x11-cee[d1])+abs(cee[d1]-x22)+kk); } if(d2 != cee.size() && d2 != -1 && cee.size() > 0) { int kk = abs(y22-y11); if(kk%v == 0) kk /= v; else kk = kk/v+1; ans = min(ans,abs(x11-cee[d2])+abs(cee[d2]-x22)+kk); } cout << ans << endl; } }
留言
張貼留言