1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
const int maxn = 100000 + 5, inf = 0x3f3f3f3f; const int maxlog = 20; int N, lgN, m; int h[maxn], x[maxn], s[maxn]; int ga[maxn], gb[maxn]; int f[maxlog][maxn][2], da[maxlog][maxn][2], db[maxlog][maxn][2]; int la = 0, lb = 0;
multiset<pair<int, int> > st; typedef multiset<pair<int, int> >::iterator SII; SII it1, it2, it3, it4, it;
void init() { st.clear(); st.insert(make_pair(inf, 0)); st.insert(make_pair(-inf, 0)); st.insert(make_pair(inf, 0)); st.insert(make_pair(-inf, 0)); h[0] = 0; Set(ga, 0); Set(gb, 0); la = lb = 0; }
// pair(value, id) inline void calg() { for(int i = N; i; i--) { st.insert(MPR(h[i], i)); it = st.find(MPR(h[i], i));
it1 = (++it); it2 = (++it); it3 = (--(--(--it))); it4 = (--it);
int a = (*it3).first != -inf ? h[i] - (*it3).first : inf; int b = (*it1).first != inf ? (*it1).first - h[i] : inf; if(a <= b) { // choose it3 as gb[] gb[i] = (*it3).second; // (it1, it4) cmp a = (*it4).first != -inf ? h[i] - (*it4).first : inf; ga[i] = (a <= b ? (*it4).second : (*it1).second); } else { // choose it1 as gb[] gb[i] = (*it1).second; b = (*it2).first != inf ? (*it2).first - h[i] : inf; ga[i] = (a <= b ? (*it3).second : (*it2).second); } } }
void calf() { _rep(i, 1, N) { f[0][i][0] = ga[i]; f[0][i][1] = gb[i]; } _rep(i, 1, N) { f[1][i][0] = f[0][ f[0][i][0] ][1]; f[1][i][1] = f[0][ f[0][i][1] ][0]; } _for(i, 2, lgN) { _rep(j, 1, N) { f[i][j][0] = f[i-1][ f[i-1][j][0] ][0]; f[i][j][1] = f[i-1][ f[i-1][j][1] ][1]; } } }
void cald() { _rep(i, 1, N) { da[0][i][0] = abs(h[i] - h[ga[i]]); db[0][i][1] = abs(h[i] - h[gb[i]]); da[0][i][1] = db[0][i][0] = 0; } _rep(i, 1, N) { da[1][i][0] = da[0][i][0]; db[1][i][1] = db[0][i][1];
da[1][i][1] = da[0][ f[0][i][1] ][0]; db[1][i][0] = db[0][ f[0][i][0] ][1]; }
_for(i, 2, lgN) _rep(j, 1, N) _for(k, 0, 2) { da[i][j][k] = da[i-1][j][k] + da[i-1][ f[i-1][j][k] ][k]; db[i][j][k] = db[i-1][j][k] + db[i-1][ f[i-1][j][k] ][k]; } }
void calS(int S, int X) { // always start from A, A first la = lb = 0; int p = S; _forDown(i, lgN, 0) { if(f[i][p][0] && la + lb + da[i][p][0] + db[i][p][0] <= X) { la += da[i][p][0]; lb += db[i][p][0]; p = f[i][p][0]; } } }
void solve1() { calS(1, x[0]); // ans1 (pID, heightValue) //double ans1[2] = {1, (lb ? (double)la / lb : inf)}; pair<int, double> ans1 = MPR(1, (double)(lb ? (double)la / lb : inf)); _rep(i, 2, N) { calS(i, x[0]); if((double)la / lb < ans1.second || ((double)la / lb == ans1.second && h[i] > h[ans1.first])) { ans1 = MPR(i, (double)la / lb); } } cout << ans1.first << endl; }
void solve2() { _rep(i, 1, m) { calS(s[i], x[i]); //cout << s[i] << " " << x[i] << endl; printf("%d %d\n", la, lb); } }
int main() { freopen("input.txt", "r", stdin); cin >> N; lgN = log(N) / log(2.0); // debug(lgN); _rep(i, 1, N) scanf("%d", &h[i]); cin >> x[0] >> m; _rep(i, 1, m) scanf("%d%d", &s[i], &x[i]);
// input finished, then solve init(); calg(); calf(); cald(); solve1(); solve2(); }
|