text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; vector<long long> turns_positive_on(n); for (long long i = 0; i < n; i++) { turns_positive_on[a[i] - 1] = (a[i] - i - 1 + n) % n; } vector<vector<long long>> transition_map(n, vector<long long>({})); for (long long i = 0; i < n; i++) { transition_map[turns_positive_on[i]].push_back((i + 1)); } long long curr = 0; for (long long i = 0; i < n; i++) curr += abs(a[i] - (i + 1)); long long ans = curr; long long shiftID = 0; long long incr = 0; long long decr = 0; for (long long i = 0; i < n; i++) { if (a[i] <= (i + 1)) incr++; else decr++; } long long lastPtr = (n - 1); for (long long shift = 1; shift < n; shift++) { long long last = a[lastPtr]; curr += incr; curr -= decr; assert(incr); curr--; curr -= (abs(n - last)); curr += (abs(1 - last)); lastPtr--; incr--; decr++; decr -= (long long)(transition_map[shift].size()); incr += (long long)(transition_map[shift].size()); if (curr < ans) { ans = curr; shiftID = shift; } } cout << (ans) << ' ' << (shiftID) << ' ' << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[2000004], a[1000004]; int main() { ios_base::sync_with_stdio; long long n, m, i, j, k = 0, l = 0; cin >> n; long long sum = 0; for (i = 1; i <= n; i++) { cin >> j; a[i] = j; if (j > i) { dp[j - i]++; l++; } else k++; sum += abs(i - j); } int ans = 0; long long anssum = sum; for (i = 1; i <= n - 1; i++) { sum = sum - abs(a[n - i + 1] - n) + abs(a[n - i + 1] - 1) - l + k - 1; if (a[n - i + 1] > 1) { dp[i + a[n - i + 1] - 1]++; l++; k--; } if (sum < anssum) { ans = i; anssum = sum; } k += dp[i]; l -= dp[i]; dp[i] = 0; } cout << anssum << " " << ans; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, p[2000200]; long long cnt[2], kind[2000200]; vector<long long> G[2000200]; long long ans = 1ll << 60, ret = -1, now; void work(long long u) { now += abs(p[u] - u); if (p[u] <= u) cnt[0]++, kind[u] = 0; else cnt[1]++, kind[u] = 1; if (p[u] <= u) { G[n - u].push_back(u); if (p[u]) G[n - u + p[u]].push_back(u); } else { G[p[u] - u].push_back(u); G[p[u] - u + n - p[u]].push_back(u); } } int main() { n = read(); for (long long i = 0; i < n; i++) p[i] = read() - 1; for (long long i = 0; i < n; i++) work(i); if (now < ans) ans = now, ret = 0; for (long long k = 1; k < n; k++) { long long sz = G[k].size(); for (long long j = 0; j < sz; j++) { long long x = G[k][j]; cnt[kind[x]]--; kind[x] ^= 1; if (kind[x] == 1) { now -= abs(n - 1 - p[x]), now += p[x]; if (p[x] == 0) kind[x] ^= 1; } else now--; } now += cnt[0], now -= cnt[1]; if (ans > now) ans = now, ret = k; for (long long j = 0; j < sz; j++) { long long x = G[k][j]; cnt[kind[x]]++; } } printf("%lld %lld\n", ans, ret); }
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n + 1, 0), kul(n + 1, 0), mime(n + 1, 0); long long neg = 0, poz = 0; long long ossz = 0; for (int i = 1; i < n + 1; i++) { cin >> v[i]; kul[i] = v[i] - i; if (kul[i] <= 0) neg++; else poz++; ossz += (long long)abs(kul[i]); if (kul[i] > 0) mime[kul[i]]++; } long long ki = ossz, hely = 0; for (int i = 1; i < n; i++) { neg--; ossz += v[n - i + 1] - 1 - abs(kul[n - i + 1] - i + 1); if (v[n - i + 1] + i - 1 <= n) mime[v[n - i + 1] + i - 1]++; ossz += neg - poz; poz++; poz -= mime[i]; neg += mime[i]; if (ossz < ki) { ki = ossz; hely = i; } } cout << ki << " " << hely << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, p[1000010], cur[1000010 * 2]; int main() { long long ans = 0, sum = 0; int l = 0, r = 0, temp = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) { sum += abs(p[i] - i); if (p[i] >= i) { l++; cur[p[i] - i]++; } else r++; } ans = sum; for (int i = 0; i < n - 1; i++) { l -= cur[i]; r += cur[i]; sum = sum - l + r - abs(p[n - i] - n - 1) + p[n - i] - 1; cur[p[n - i] + i]++; l++, r--; if (sum < ans) { ans = sum; temp = i + 1; } } printf("%lld %d\n", ans, temp); return 0; }
#include <bits/stdc++.h> using namespace std; long long pos = 0; long long neg = 0; long long ans1 = 1e18; long long ans2 = 0; int a[1000005]; int cnt[2000005]; int tot1 = 0; int tot2 = 0; int n; void update(int i, int p) { if (a[i] - i >= 0) { if (a[i] - n >= 0) { cnt[a[i] - i]--; pos -= a[i] - n; tot1--; } else { neg -= n - a[i]; tot2--; } } else { neg -= n - a[i]; tot2--; } tot1 -= cnt[p]; pos -= tot1; neg += cnt[p]; neg += tot2; tot2 += cnt[p]; cnt[a[i] - 1 + p + 1]++; pos += a[i] - 1; tot1++; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] - i >= 0) { cnt[a[i] - i]++; pos += a[i] - i; tot1++; } else { neg += i - a[i]; tot2++; } } int p = 0; if (ans1 > neg + pos) { ans1 = neg + pos; ans2 = p; } update(n, p); p++; for (int i = n - 1; i >= 1; i--, p++) { if (ans1 > neg + pos) { ans1 = neg + pos; ans2 = p; } update(i, p); } printf("%lld %lld\n", ans1, ans2); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; long long p[maxn]; long long has[maxn << 1]; int main() { long long n; long long bigger = 0, smaller = 0, equ = 0, ans1 = 0, ans2 = 0; scanf("%I64d", &n); for (long long i = 1LL; i <= n; i++) { scanf("%I64d", &p[i]); if (p[i] > i) { bigger++; has[p[i] - i]++; } else if (p[i] == i) { equ++; has[0]++; } else smaller++; ans1 += abs(p[i] - i); } long long temp = ans1; for (long long last = n - 1LL, now = 1LL; last >= 1LL; last--, now++) { temp += (equ + smaller); temp -= bigger; smaller += equ; bigger -= has[now]; if (p[last + 1] >= last + 1LL) has[p[last + 1] - last - 1]--; has[p[last + 1] - 1 + now]++; equ = has[now]; if (p[last + 1] > 1LL) bigger++; smaller = n - equ - bigger; temp -= abs(p[last + 1] - n - 1LL); temp += abs(p[last + 1] - 1LL); if (temp < ans1) { ans1 = temp; ans2 = now; } } printf("%I64d %I64d\n", ans1, ans2); return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000005]; int b[1000005]; int main() { int n; scanf("%d", &n); long long ans = 0; int cnt1 = 0, cnt2; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] > i) { b[a[i] - i]++; cnt1++; } else { b[a[i] - i + n]++; } ans += abs(a[i] - i); } long long tmp = ans; int id = 0; for (int i = 1; i < n; i++) { int cnt2 = n - cnt1; tmp += (a[n - i + 1] - 1) + (cnt2 - cnt1 + (-(n - a[n - i + 1]))) - 1; cnt1 -= b[i]; cnt1++; if (tmp < ans) { ans = tmp; id = i; } } printf("%I64d %d\n", ans, id); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 10; int p[maxn]; int cnt[maxn]; int w[maxn]; void nex(int *a, int n) { int t = a[n]; for (int i = n; i >= 2; i--) a[i] = a[i - 1]; a[1] = t; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i]), w[i] = p[i]; int a = 0, b = 0; long long ans = 0; for (int i = 1; i <= n; i++) { if (p[i] > i) a++; else if (p[i] < i) b++; ans += abs(p[i] - i); cnt[(p[i] - i + n) % n]++; } long long ANS = ans, x = 0; for (int i = 1; i <= n; i++) { int j = n - i + 1; ans -= abs(p[j] - (j + i - 1)); ans += abs(p[j] - 1); b--; b += cnt[i - 1]; ans += b - a; if (ANS > ans) ANS = ans, x = i; a++; a -= cnt[i]; } cout << ANS << ' ' << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000005]; long long ans[1000005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) { int time1, time2; time1 = n - i + 1; time2 = a[i] - i; if (time2 < 0) time2 += n; int ch = abs(n - a[i]); if (time1 <= time2) { int bg = abs(i - a[i]); int se = abs(1 - a[i]); int ed = abs(n - a[i]); ans[0] += bg; ans[1] += -bg + 1; ans[time1] += -1 - ed + se; ans[time1 + 1] += -1 + ed - se; ans[time2 + 1] += 2; } else { int bg = abs(i - a[i]); int se = abs(1 - a[i]); int ed = abs(n - a[i]); ans[0] += bg; ans[1] += -bg - 1; ans[time2 + 1] += 2; ans[time1] += -1 - ed + se; ans[time1 + 1] += -1 + ed - se; } } for (int i = 1; i < n; i++) ans[i] += ans[i - 1]; for (int i = 1; i < n; i++) ans[i] += ans[i - 1]; long long best = 1e18, besti = -1; for (int i = 0; i < n; i++) { if (ans[i] < best) { best = ans[i]; besti = i; } } cout << best << " " << besti << endl; return 0; }
#include <bits/stdc++.h> using namespace std; multiset<int> b; multiset<int>::iterator it; int sia = 0; long long ansa = 0, ansb = 0; stack<int> sta; int k[1000005]; int main() { int n, ansk = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", k + i); if (k[i] > i) { b.insert(k[i] - i); ansb += k[i] - i; } else { ++sia; ansa += i - k[i]; } } long long mi = ansa + ansb; int co = 1; for (int i = n; i > 1; --i) { ansb -= b.size(); ansa += sia; int t = b.size(); it = b.upper_bound(co); b.erase(b.begin(), it); t -= b.size(); sia += t; if (k[i] > n + 1) { b.erase(b.find(k[i] - n - 1 + co)); ansb -= k[i] - n - 1; } else { --sia; ansa -= n + 1 - k[i]; } if (k[i] > 1) { b.insert(k[i] - 1 + co); ansb += k[i] - 1; } else { ++sia; ansa += 1 - k[i]; } ++co; if (mi > ansa + ansb) { mi = ansa + ansb; ansk = co - 1; } } cout << mi << ' ' << ansk << endl; return 0; }
#include <bits/stdc++.h> const int maxn = 1e6 + 1e2; int val[maxn]; int derivative1[maxn]; int derivative2[maxn]; long long curr = 0; long long curr_change = 0; long long res = 1e18; long long res_pos = 0; int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> val[i]; val[i]--; curr += std::abs(val[i] - i); if (i <= val[i]) { derivative2[1]--; derivative2[val[i] - i + 1] += 2; derivative2[n - i]--; derivative1[n - i] += std::abs(val[i] - 0) - std::abs(val[i] - (n - 1)); derivative2[n - i + 1]--; } else { derivative2[1]++; derivative2[n - i]--; derivative1[n - i] += std::abs(val[i] - 0) - std::abs(val[i] - (n - 1)); derivative2[n - i + 1]--; derivative2[n - i + val[i] + 1] += 2; } } for (int i = 0; i < n; i++) { curr_change += derivative2[i]; curr += curr_change + derivative1[i]; if (res > curr) { res = curr; res_pos = i; } } std::cout << res << ' ' << res_pos << std::endl; }
#include <bits/stdc++.h> using namespace std; int rd() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') f = c == '-' ? -1 : 1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x * f; } void wt(long long x) { if (x >= 10) wt(x / 10); putchar(x % 10 + 48); } const int N = 1e6 + 10; priority_queue<int, vector<int>, greater<int>> q; int a[N], n, tot, Ans; long long ans, u; int main() { n = rd(); for (int i = 1; i <= n; i++) a[i] = rd(); for (int i = 1; i <= n; i++) { if (a[i] >= i) tot++; else q.push(i - a[i]); ans += abs(a[i] - i); } u = ans; for (int k = 1; k <= n; k++) { tot--; u += n - 2 * a[k] + 1; u += tot, u -= q.size(); q.push(n - a[k] + k); while (!q.empty()) { if (q.top() - k == 0) q.pop(), tot++; else break; } if (u < ans) ans = u, Ans = n - k; } wt(ans), putchar(32), wt(Ans); return 0; }
#include <bits/stdc++.h> const int offset = 11E5; int N; int leftcnt[2400000], rightcnt[2400000]; int A[1100000]; int best = 0; long long bans = 0; long long ans = 0; int main() { scanf("%d", &N); long long cur = 0; for (int i = 1; i <= N; ++i) { scanf("%d", &A[i]); ans += abs(i - A[i]); cur += (A[i] <= i) ? 1 : -1; ++leftcnt[A[i] - i + offset]; } best = 0; bans = ans; for (int i = 1; i <= N - 1; ++i) { ans += cur; ans = ans - (N + 1 - A[N + 1 - i]) + A[N + 1 - i] - 1; --leftcnt[A[N + 1 - i] - (N + 1 - i) + offset]; ++rightcnt[A[N + 1 - i] - (N + 1 - i) + offset]; cur -= 2; cur += leftcnt[i + offset] * 2; cur += rightcnt[i - N + offset] * 2; if (bans > ans) { best = i; bans = ans; } } printf("%lld %d\n", bans, best); }
#include <bits/stdc++.h> using namespace std; const int maxN = 1e6 + 10; int n; int p[maxN], value[3 * maxN]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", p + i); long long sum = 0; int positive = 0, negative = 0; for (int i = 1; i <= n; ++i) { sum += abs(p[i] - i); value[p[i] - i + maxN]++; if (p[i] <= i) negative++; else positive++; } long long maxValue = sum, pos = 0; for (int i = 1; i < n; ++i) { sum += negative; sum -= positive; sum -= (n + 1 - p[n - i + 1]); sum += (p[n - i + 1] - 1); if (sum < maxValue) { maxValue = sum; pos = i; } negative += value[maxN + i]; positive -= value[maxN + i]; value[maxN + p[n - i + 1] - (n - i + 1)]--; value[maxN + p[n - i + 1] + i - 1]++; if (p[n - i + 1] != 1) { positive++; negative--; } } printf("%I64d %d\n", maxValue, pos); return 0; }
#include <bits/stdc++.h> using namespace std; int n, p[1000010], a[1000010], addall, cnt0, cnt1, cnt2; long long tmp; vector<int> v[1000010]; bool flag[1000010]; int ask(int x) { return a[x] + addall; } long long Abs(long long x) { if (x < 0) return -x; return x; } void dec(int n) { if (p[n] - ask(n) > 0) cnt1--; else if (p[n] - ask(n) == 0) cnt0--; else cnt2--; tmp -= cnt1; tmp += cnt2 + cnt0; tmp -= Abs(p[n] - ask(n)); addall++; a[n] = 1 - addall; tmp += Abs(p[n] - ask(n)); if (p[n] - ask(n) == 0) cnt0++; else cnt1++; } void add(int j, int v) { if (p[j] - ask(j) > 0) cnt1 += v; else if (p[j] - ask(j) == 0) cnt0 += v; else cnt2 += v; } int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); a[i] = i; if (p[i] > i) { cnt1++; tmp += p[i] - (long long)i; v[p[i] - i].push_back(i); } else if (p[i] == i) { cnt0++; v[0].push_back(i); } else if (p[i] < i) { cnt2++; tmp += i - (long long)p[i]; } v[n - i].push_back(i); if (n - i + p[i] - 1 < n) v[n - i + p[i] - 1].push_back(i); } long long ans = tmp; int tk = 0; dec(n); for (int i = 0; i < v[0].size(); i++) { int j = v[0][i]; if (j == n || flag[j]) continue; flag[j] = 1; addall--, add(j, -1); addall++, add(j, 1); } for (int i = 0; i < v[1].size(); i++) { int j = v[1][i]; if (j == n || flag[j]) continue; flag[j] = 1; addall--, add(j, -1); addall++, add(j, 1); } for (int i = 0; i < v[0].size(); i++) flag[v[0][i]] = 0; for (int i = 0; i < v[1].size(); i++) flag[v[1][i]] = 0; for (int k = 1; k < n; k++) { if (tmp < ans) ans = tmp, tk = k; dec(n - k); for (int i = 0; i < v[k].size(); i++) { int j = v[k][i]; if (j == n - k || flag[j]) continue; flag[j] = 1; addall--, add(j, -1); addall++, add(j, 1); } for (int i = 0; i < v[k + 1].size(); i++) { int j = v[k + 1][i]; if (j == n - k || flag[j]) continue; flag[j] = 1; addall--, add(j, -1); addall++, add(j, 1); } for (int i = 0; i < v[k].size(); i++) flag[v[k][i]] = 0; for (int i = 0; i < v[k + 1].size(); i++) flag[v[k + 1][i]] = 0; } cout << ans << ' ' << tk << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; long long a[N], n, l[N]; int main() { cin >> n; long long ans = 0; long long id = 0, L = 0, R; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > i) { l[a[i] - i]++; L++; } else { l[n - i + a[i]]++; } ans += (a[i] > i) ? a[i] - i : i - a[i]; } long long tmp = ans; for (int i = 1; i < n; i++) { R = n - L; tmp += (-L + R + (-(n - a[n - i + 1])) + (a[n - i + 1] - 1) - 1); L -= l[i]; L++; if (tmp < ans) { ans = tmp; id = i; } } cout << ans << ' ' << id << endl; return 0; }
#include <bits/stdc++.h> inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } long long int gcd(long long int a, long long int b) { return (a ? gcd(b % a, a) : b); } long long int modPow(long long int a, long long int b, long long int MOD) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % MOD; } b /= 2; y = (y * y) % MOD; } return x; } long long int modInverse(long long int a, long long int p) { return modPow(a, p - 2, p); } using namespace std; const int N = 1e6 + 6; long long int arr[3 * N]; long long int rot_b4_zero[3 * N]; pair<long long int, long long int> ans; long long int poscnt; long long int curr; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int n, i, j, k, dev, idx; cin >> n; for (int i = (1); i <= (n); i++) { cin >> arr[i]; ans.first += abs(arr[i] - i); if (arr[i] > i) { rot_b4_zero[arr[i] - i]++; poscnt++; } } curr = ans.first; for (int k = (1); k <= (n - 1); k++) { curr -= abs(arr[n + 1 - k] - n); curr += abs(arr[n + 1 - k] - 1); curr -= poscnt; curr += n - 1 - poscnt; poscnt -= rot_b4_zero[k]; if (curr < ans.first) ans = {curr, k}; if (arr[n + 1 - k] > 1) { poscnt++; rot_b4_zero[k + arr[n + 1 - k] - 1]++; } } cout << ans.first << " " << ans.second << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int a[N], n, l[N]; int main() { cin >> n; long long ans = 0; int id = 0, L = 0, R; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] > i) { l[a[i] - i]++; L++; } else { l[n - i + a[i]]++; } ans += (a[i] > i) ? a[i] - i : i - a[i]; } long long tmp = ans; for (int i = 1; i < n; i++) { R = n - L; tmp += (-L + R + (-(n - a[n - i + 1])) + (a[n - i + 1] - 1) - 1); L -= l[i]; L++; if (tmp < ans) { ans = tmp; id = i; } } cout << ans << ' ' << id << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int ara[1000004]; long long cnt[1000004]; int main() { int n; scanf("%d", &n); long long positive = 0, negative = 0; long long ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &ara[i]); if (ara[i] <= i + 1) { positive++; cnt[ara[i] - i - 1 + n]++; } else { negative++; cnt[ara[i] - i - 1]++; } ans += abs(ara[i] - i - 1); } long long mini = ans, to = 0; for (int i = 1; i < n; i++) { ans += positive; ans -= negative; ans--; ans -= abs(ara[n - i] - n) - abs(ara[n - i] - 1); negative++; positive--; positive += cnt[i]; negative -= cnt[i]; if (mini > ans) { mini = ans; to = i; } } cout << mini << " " << to << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, p[N], cnt[N]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> p[i]; int L = 0, R = 0; long long res = 0, ans; for (int i = 1; i <= n; i++) { res += abs(p[i] - i); cnt[(p[i] - i + n) % n]++; if (p[i] <= i) R++; else L++; } ans = res; int idx = 0; for (int k = 1; k < n; k++) { res -= L; res += R - 1; res += p[(n - k) % n + 1] - n + p[(n - k) % n + 1] - 1; L -= cnt[k] - 1; R += cnt[k] - 1; if (res < ans) { idx = k, ans = res; } } cout << ans << " " << idx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(rand()); long long inf = 1e9; long long mod = 1e9 + 7; long long big_inf = 1e18; signed main() { ios_base::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n); set<pair<long long, long long> > s1; long long s2_size = 0; vector<long long> set_val(n); long long my_sum = 0; for (long long i = 0; i < n; ++i) { cin >> a[i]; long long r = i + 1 - a[i]; set_val[i] = r; if (r < 0) s1.insert({r, i}); else s2_size++; my_sum += abs(i + 1 - a[i]); } long long j = n - 1; long long ans = my_sum; long long ans_num = 0; for (long long i = 0; i < n - 1; ++i) { my_sum -= abs(n - a[j]); my_sum += abs(1 - a[j]); my_sum -= s1.size(); my_sum += s2_size - 1; if (my_sum < ans) { ans = my_sum; ans_num = i + 1; } while (s1.size() && s1.rbegin()->first + i + 1 == 0) { s2_size++; s1.erase(*s1.rbegin()); } if (a[j] > 1) { s2_size--; s1.insert({1 - a[j] - i - 1, j}); } --j; } cout << ans << " " << ans_num; return 0; }
#include <bits/stdc++.h> int main() { long long n; std::cin >> n; long long inversion = 0; std::vector<long long> a; a.reserve(n); std::vector<long long> chan(n, 0); long long total = 0; for (long long i = 0; i < n; ++i) { long long b; std::cin >> b; a.push_back(b); inversion += abs(b - i - 1); if (b > i + 1) { total++; } ++chan[(b - i - 1 + n) % n]; } long long min = inversion; long long id = 0; for (long long i = n - 1; i > 0; --i) { inversion -= total; inversion += n - total; inversion -= n + 1 - a[i]; inversion += a[i] - 1; total -= chan[n - i] - 1; if (min > inversion) { min = inversion; id = n - i; } } std::cout << min << " " << id << std::endl; }
#include <bits/stdc++.h> using namespace std; int a[1000005], p[1000005]; int main() { int n; cin >> n; long long l = 0, tmp = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; tmp += abs(a[i] - i); a[i] - i > 0 ? p[a[i] - i]++ : l++; } long long ans = tmp, id = 0; for (int i = 1; i < n; i++) { l--; if ((a[n - i + 1] - n - i + 1) > 0) p[a[n - i + 1] - n - i + 1]--; p[min(a[n - i + 1] + i - 1, 1000000)]++; tmp -= (n - l - 1); tmp += l; tmp -= abs(a[n - i + 1] - n); tmp += abs(a[n - i + 1] - 1); l += p[i]; if (tmp < ans) ans = tmp, id = i; } cout << ans << ' ' << id << endl; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 239; long long my_get(long long n, vector<long long> p, long long x) { long long cnt_minus = 0; long long cnt_plus = 0; vector<long long> by_step(n); for (long long i = 0; i < n; i++) { if (p[i] - i <= 0) { cnt_plus++; } else { cnt_minus++; } if (p[i] - i > 0) { by_step[p[i] - i]++; } if (p[i] - i <= 0) { } } long long cur = 0; for (long long i = 0; i < n; i++) { cur += abs(p[i] - i); } long long best = INF; long long ans = -1; for (long long i = 0; i < n; i++) { if (i > 0) { long long ind = (n - i); cur -= abs(n - p[ind]); cur += abs(0 - p[ind]); } if (i == x) { return cur; } if (cur < best) { best = cur; ans = i; } cur = cur + cnt_plus - cnt_minus; if (i + 1 <= n) { long long pos = (n - (i + 1)); if (p[pos] != 0) { cnt_plus--; cnt_minus++; if (i + p[pos] + 1 < n) { by_step[i + p[pos] + 1]++; } } } if (i + 1 < n) { cnt_plus += by_step[i + 1]; cnt_minus -= by_step[i + 1]; } } cout << best << '\n'; cout << ans << '\n'; return -1; } long long stupid_get(long long n, vector<long long> p, long long x) { while (x > 0) { auto f = p.back(); p.pop_back(); p.insert(p.begin(), f); x--; } long long ans = 0; for (long long i = 0; i < n; i++) { ans += abs(p[i] - i); } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> p(n); for (auto &t : p) { cin >> t; t--; } my_get(n, p, -1); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int n; int p[MAXN], ones[MAXN << 1]; int main() { while (~scanf("%d", &n)) { long long sum = 0, mn; int mn_idx = 0, pos = 0, neg = 0; memset(ones, 0, sizeof(ones)); for (int i = 1; i <= n; ++i) { scanf("%d", p + i); sum += ((p[i] - i) < 0 ? -(p[i] - i) : (p[i] - i)); if (p[i] > i) ++pos; else ++neg; if (p[i] >= i) ++ones[p[i] - i]; } mn = sum; for (int i = 1; i <= n; ++i) { sum = sum - pos + neg - 1; sum = sum - ((p[n - i + 1] - n) < 0 ? -(p[n - i + 1] - n) : (p[n - i + 1] - n)) + p[n - i + 1] - 1; if (p[n - i + 1] != 1) { ++pos; --neg; } pos -= ones[i]; neg += ones[i]; ++ones[p[n - i + 1] - 1 + i]; if (sum < mn) { mn = sum; mn_idx = i; } } printf("%I64d %d\n", mn, mn_idx); } }
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(false); long long n; cin >> n; vector<long long> p(n); vector<long long> passing(n); long long before = 0; long long after = 0; long long sum = 0; for (int i = 0; i < int(n); ++i) { cin >> p[i]; --p[i]; ++passing[(p[i] - i + n) % n]; if (p[i] < i) ++after; else ++before; sum += abs(p[i] - i); } long long minSum = sum; long long best = 0; for (int i = 0; i < int(n); ++i) { if (sum < minSum) { minSum = sum; best = i; } before -= passing[i]; after += passing[i]; sum -= before; sum += after; long long last = n - 1 - i; ++before; --after; sum += p[last] - ((n)-p[last]); } cout << minSum << " " << best << endl; }
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int b[2000010], a[2000010]; int main() { int n = read(); long long res = 0, ans = 0, ans1 = 0; int change = 0; for (int i = (1); i <= (n); i++) a[i] = read(); for (int i = (1); i <= (n); i++) { if (a[i] >= i) { res += a[i] - i; change--; b[a[i] - i]++; } else { res += i - a[i]; change++; b[a[i] - i + n]++; } } ans = res; for (int i = (1); i <= (n - 1); i++) { change += b[i - 1]; change += b[i - 1]; res += abs(a[n - i + 1] - 1) - abs(a[n - i + 1] - n) + 1; change -= 2; res += change; if (res < ans) ans = res, ans1 = i; } cout << ans << " " << ans1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2000000; const long long INF = 1000000000000000000; long long n; long long p[MAXN + 10]; long long f[MAXN + 10]; int main() { scanf("%I64d", &n); long long sum = 0, py = 0; for (int i = 1; i <= n; ++i) { scanf("%I64d", p + i); if (i > p[i]) { sum += i - p[i]; ++py; f[p[i] + n - i] += 2; } else { sum += p[i] - i; --py; f[p[i] - i] += 2; } } long long mmin = sum, r = 0; for (int i = 1; i < n; ++i) { py += f[i - 1] - 2; sum += py + 1; sum += abs(p[n - i + 1] - 1) - abs(p[n - i + 1] - n); if (sum < mmin) { mmin = sum; r = i; } } printf("%I64d %I64d", mmin, r); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long longinf = 1LL << 60; const long long mod = 1e9 + 7; const long long mod2 = 998244353; const long double eps = 1e-10; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> P(n); for (int i = (int)(0); i < (int)(n); i++) cin >> P[i]; vector<int> cnt(n); for (int i = (int)(0); i < (int)(n); i++) cnt[(P[i] - i - 1 + n) % n]++; long long tmp = 0; long long mn = longinf; int mni = 0; int h = 0, l = 0; for (int i = (int)(0); i < (int)(n); i++) { tmp += abs(P[i] - i - 1); (P[i] - i - 1 > 0 ? h : l)++; } for (int i = (int)(0); i < (int)(n); i++) { if (mn > tmp) { mn = tmp; mni = i; } tmp -= abs(P[n - 1 - i] - n); tmp += abs(P[n - 1 - i] - 1); tmp += (l - 1) - h; l += cnt[(i + 1) % n]; h -= cnt[(i + 1) % n]; l--; h++; } cout << mn << ' ' << mni << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, ans, len, sumi, kl, k[2000100], p[2000100], sum, wapi[2000100], wai[2000100]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { scanf("%d", &p[i]); wapi[max(0ll, i - p[i])] += p[i]; wai[max(0ll, i - p[i])] += i; ++k[max(0ll, i - p[i])]; sum += p[i]; } for (int i = 1; i <= n; ++i) { p[i + n] = p[i]; wapi[max(0ll, i + n - p[i])] += p[i]; wai[max(0ll, i + n - p[i])] += i + n; ++k[max(0ll, i + n - p[i])]; } len = 1e18; long long sumwapi = 0, sumwai = 0; for (int i = n * 2; i > n + 1; --i) { sumwapi += wapi[i]; sumwai += wai[i]; kl += k[i]; } for (int i = n + 1; i; --i) { sumwapi += wapi[i] - wapi[i + n]; sumwai += wai[i] - wai[i + n]; kl += k[i] + k[i + n]; long long s = 2 * (sumwai - i * kl + kl) - 2 * sumwapi; wapi[max(0ll, i + n - 1 - p[i + n - 1])] -= p[i + n - 1]; wai[max(0ll, i + n - 1 - p[i + n - 1])] -= i + n - 1; --k[max(0ll, i + n - 1 - p[i + n - 1])]; if (max(0ll, i + n - 1 - p[i + n - 1]) >= i) { sumwapi -= p[i + n - 1]; sumwai -= i + n - 1; --kl; } if (s < len) { len = s; ans = n + 1 - i; } } cout << len << ' ' << ans; }
#include <bits/stdc++.h> using namespace std; const int N = 3000010; int n, a[N]; int maxr[N]; long long ans, cur; int id, ansid; int tmax, tmin, zero; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); ansid = 0; for (int i = 1; i <= n; i++) { if (a[i] > i) maxr[a[i] - i]++, tmax++; else if (a[i] < i) tmin++; else zero++; ans += abs(i - a[i]); } cur = ans; for (int i = 1; i < n; i++) { if (a[n - i + 1] == n) { cur -= tmax; tmax++; tmin += zero - 1; cur += tmin; zero = maxr[i]; tmax -= maxr[i]; maxr[i] = 0; cur += n - 1; } else if (a[n - i + 1] == 1) { cur -= tmax; tmin--; tmin += zero; cur += tmin; zero = maxr[i] + 1; tmax -= maxr[i]; maxr[i] = 0; cur -= n - 1; } else { cur -= tmax; tmax++; maxr[a[n - i + 1] - 1 + i]++; tmin--; tmin += zero; cur += tmin; zero = maxr[i]; tmax -= maxr[i]; maxr[i] = 0; cur -= n - a[n - i + 1]; cur += a[n - i + 1] - 1; } if (cur < ans) { ans = cur; ansid = i; } } cout << ans << " " << ansid << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 10; int p[maxn]; int cnt[maxn]; int main() { int n; scanf("%d", &n); int a = 0, b = 0; long long ans = 0; for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); if (p[i] > i) a++; else b++; ans += abs(p[i] - i); cnt[(p[i] - i + n) % n]++; } long long ANS = ans, x = 0; for (int i = 1; i <= n; i++) { int j = n - i + 1; ans -= abs(p[j] - (j + i - 1)); ans += abs(p[j] - 1); b--; ans += b - a; if (ANS > ans) ANS = ans, x = i; ++a; a -= cnt[i % n]; b += cnt[i % n]; } cout << ANS << ' ' << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long base = 37; long long large = 1000000000000000000LL; long long labs(long long x) { if (x < 0) return -x; return x; } int main() { int n; cin >> n; vector<long long> p(n, 0); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } long long s = 0; for (int i = 0; i < n; i++) s += labs(p[i] - i); long long ans = s; vector<long long> adj; long long cnt = 0; vector<long long> eve(n + 1, 0); for (int i = 0; i < n; i++) { if (p[i] <= i) { cnt++; eve[n - i + p[i]]++; } else { eve[p[i] - i]++; } } int k = 0; for (int i = 1; i < n; i++) { s -= labs(p[n - i] - (n - 1)); s += p[n - i]; cnt--; s += cnt; s -= (1LL * (n - 1) - cnt); cnt += eve[i]; if (s < ans) { ans = s; k = i; } } cout << ans << " " << k << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool Max(T1& a, T2 b) { return a < b ? a = b, 1 : 0; } template <typename T1, typename T2> bool Min(T1& a, T2 b) { return a > b ? a = b, 1 : 0; } const int N = 1000001; long long _a[3 * N], *a = _a + 2 * N; long long pos[N]; void solve() { long long n, v; cin >> n; long long less = 0; long long dif = 0; for (int i = 1; i <= (n); ++i) { cin >> v; a[i - v]++; pos[i] = i - v; less += i < v; dif += abs(i - v); } long long mini = 0; long long ans = dif; long long cur = 0; for (int i = 1; i <= (n - 1); ++i) { dif += 2 * (cur - pos[n + 1 - i]) + n; a[pos[n + 1 - i] - n]++; less++; dif += n - 2 * less; if (Min(ans, dif)) mini = i; less -= a[--cur]; } cout << ans << ' ' << mini << '\n'; } void init() {} int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n; int b[2000010], a[2000010]; int main() { cin >> n; long long res = 0, ans = 0, ans1 = 0; int change = 0; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { if (a[i] >= i) { res += a[i] - i; change--; b[a[i] - i]++; } else { res += i - a[i]; change++; b[a[i] - i + n]++; } } ans = res; for (int i = 1; i <= n - 1; ++i) { change += b[i - 1]; change += b[i - 1]; res += abs(a[n - i + 1] - 1) - abs(a[n - i + 1] - n) + 1; change -= 2; res += change; if (res < ans) ans = res, ans1 = i; } cout << ans << " " << ans1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int p[N], add[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, best = 0; long long sum = 0, Min; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i]; int nrdecr = 0, nrincr = 0; for (int i = 1; i <= n; i++) { sum += abs(p[i] - i); if (i >= p[i]) add[n - (i - p[i])]++; else add[p[i] - i]++; if (i < n) { if (i < p[i]) nrdecr++; else nrincr++; } } Min = sum; for (int i = 1; i < n; i++) { sum += nrincr - nrdecr; sum -= abs(p[n + 1 - i] - n); sum += abs(p[n + 1 - i] - 1); nrincr += add[i] - 1; nrdecr -= add[i] - 1; if (sum < Min) { Min = sum; best = i; } } cout << Min << " " << best; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int n; int p[MAXN], ones[MAXN << 1]; int main() { while (~scanf("%d", &n)) { long long sum = 0, mn; int mn_idx = 0, pos = 0, neg = 0; memset(ones, 0, sizeof(ones)); for (int i = 1; i <= n; ++i) { scanf("%d", p + i); sum += abs(p[i] - i); if (p[i] > i) ++pos; else ++neg; if (p[i] >= i) ++ones[p[i] - i]; } mn = sum; for (int i = 1; i <= n; ++i) { sum = sum - pos + neg - 1; sum = sum - abs(p[n - i + 1] - n) + p[n - i + 1] - 1; if (p[n - i + 1] != 1) { ++pos; --neg; } pos -= ones[i]; neg += ones[i]; ++ones[p[n - i + 1] - 1 + i]; if (sum < mn) { mn = sum; mn_idx = i; } } printf("%I64d %d\n", mn, mn_idx); } }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") long long mul(long long a, long long b) { return (a * b) % (1000000007); } long long add(long long a, long long b) { return (a + b) % (1000000007); } long long sub(long long a, long long b) { return ((a - b) % (1000000007) + (1000000007)) % (1000000007); } void upd(long long &a, long long b) { a = (a % (1000000007) + b % (1000000007)) % (1000000007); } inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long a2[1234567], a[1234567]; long long c[2500000] = {}; long long abs2(long long a) { return max(a, -a); } int main() { int n = read(); for (int i = 1; i <= n; i++) a[i] = read() - i, a2[i] = a[i] + i; long long ans = 0; for (int i = 1; i <= n; i++) ans += abs2(a[i]); long long p = 0, lcnt = 0; for (int i = 1; i <= n; i++) if (a[i] < 0) lcnt++; else c[a[i]]++; long long m1 = 0, m2 = ans; for (int i = 1; i <= n - 1; i++) { int v = a[n - i + 1]; ans -= abs2(v + n - i + 1 - n); if (v < i - 1) lcnt--; else c[v]--; v += n; lcnt += c[i - 1]; c[i - 1] = 0; ans += lcnt - (n - lcnt - 1); if (v <= i - 1) lcnt++; else c[v]++; ans += abs2(v - i); if (ans < m2) { m2 = ans; m1 = i; } } cout << m2 << ' ' << m1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int countDiff[MAXN * 3]; int nums[MAXN]; int nNums; int shifts; long long minDiff; long long diffs[MAXN]; int countNegative; void handleShift(int n) { int ind = nNums - n; int former = nums[ind] - nNums; int latter = nums[ind]; countDiff[nums[ind] + nNums - ind - 2]--; countDiff[nums[ind] + nNums - ind - 2 + nNums]++; long long currResult = diffs[n - 1]; currResult += abs(latter) - abs(former); if (former < 0) { countNegative--; } countNegative += countDiff[nNums - 2 + n]; currResult += countNegative; currResult -= (nNums - countNegative); diffs[n] = currResult; if (diffs[n] < minDiff) { minDiff = diffs[n]; shifts = n; } } int main() { scanf("%d", &nNums); for (int i = 0; i < nNums; i++) { scanf("%d", nums + i); int diff = nums[i] - (i + 1); minDiff += abs(diff); countDiff[diff + (nNums - 1)]++; if (diff < 0) { countNegative++; } } diffs[0] = minDiff; for (int i = 1; i < nNums; i++) { handleShift(i); } cout << minDiff << ' ' << shifts << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 100; int a[maxn]; int cnt[2 * maxn]; int main() { long long sum = 0, ans = 0; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } long long l = 0, r = 0; for (int i = 1; i <= n; i++) { sum += abs(a[i] - i); if (a[i] >= i) { cnt[a[i] - i]++; l++; } else { r++; } } int id = 0; long long Min = sum; for (int i = 1; i <= n; i++) { r += cnt[i - 1]; l -= cnt[i - 1]; sum += (r - l) - 1; sum = sum - (n - a[n - i + 1]) + (a[n - i + 1] - 1); r--; l++; cnt[i + (a[n - i + 1] - 1)]++; if (sum < Min) { Min = sum; id = i; } } printf("%lld %d\n", Min, id); return 0; }
#include <bits/stdc++.h> using namespace std; int nums[1000005], pos[1000005], idx; long long int diff[1000005], deviation[1000005], initialDeviation, result; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &nums[i]); initialDeviation += abs(nums[i] - i); pos[nums[i]] = i; } result = initialDeviation; idx = 0; for (int i = 1; i <= n; i++) { if (pos[i] <= i) { int shift = i - pos[i] + 1; diff[1]--; diff[shift] += 2; shift = n - pos[i] + 1; diff[shift]--; int change = 2 * i - n - 1; diff[shift] += change; diff[shift + 1] -= change; diff[shift + 1]--; diff[n]++; } else { int shift = n - pos[i] + 1; diff[1]++; diff[shift]--; int change = 2 * i - n - 1; diff[shift] += change; diff[shift + 1] -= change; diff[shift + 1]--; diff[shift + 1 + i - 1] += 2; diff[n]--; } } for (int i = 1; i <= n; i++) diff[i] = diff[i - 1] + diff[i]; deviation[0] = initialDeviation; for (int i = 1; i < n; i++) deviation[i] = deviation[i - 1] + diff[i]; for (int i = 1; i < n; i++) { if (deviation[i] < result) { result = deviation[i]; idx = i; } } printf("%lld %d\n", result, idx); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0, t = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') t = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { sum = sum * 10 + ch - '0'; ch = getchar(); } return sum * t; } int n, p[1000005], pos; long long step[1000005], now, S, ans; int main() { n = read(); for (int i = 1; i <= n; i++) p[i] = read(); for (int i = 1; i <= n; i++) { S += abs(p[i] - i); if (p[i] >= i) { step[0] += -1; step[p[i] - i] += 1 + 1; step[n - i] += abs(p[i] - 1) - abs(p[i] - n) - 1; step[n - i + 1] += -1 - abs(p[i] - 1) + abs(p[i] - n); } else if (p[i] < i) { step[0] += 1; step[n - i] += abs(p[i] - 1) - abs(p[i] - n) - 1; step[n - i + 1] += -1 - abs(p[i] - 1) + abs(p[i] - n); step[p[i] + n - i] += 1 + 1; } } ans = S; pos = 0; for (int k = 0; k < n; k++) { S += now; if (S < ans) ans = S, pos = k; now += step[k]; } printf("%lld %d\n", ans, pos); }
#include <bits/stdc++.h> using namespace std; map<int, int> b; int sib = 0; long long ansb = 0; int k[1000005]; int main() { int n, ansk = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", k + i); if (k[i] > i) { ++b[k[i] - i]; ansb += k[i] - i; ++sib; } } long long mi = ansb; int co = 0; for (int i = n; i > 1; --i) { ++co; ansb -= sib; sib -= b[co]; if (k[i] > n + 1) { --b[k[i] - n - 1 + co]; ansb -= k[i] - n - 1; --sib; } if (k[i] > 1) { ++b[k[i] - 1 + co]; ansb += k[i] - 1; ++sib; } if (mi > ansb) { mi = ansb; ansk = co; } } cout << mi * 2 << ' ' << ansk << endl; return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int N = 2 * ((int)3e6 + 10); long long p[N], n; int d[N + N]; long long cnt_left, cnt_right; long long add_left, add_right; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i]; int mid = N / 2; long long cur_ans = 0; int step = 0; for (int i = 1; i <= n; ++i) { d[mid + p[i] - i]++; if (p[i] < i) cnt_left++, add_left += i - p[i]; else if (p[i] > i) cnt_right++, add_right += p[i] - i; cur_ans += abs(p[i] - i); } for (int i = 1; i <= n; ++i) { d[mid + p[i] - 1]--; if (p[i] > 1) { cnt_right--; add_right -= p[i] - 1; } else if (p[i] < 1) { cnt_left--; add_left -= 1 - p[i]; } cnt_right += d[mid]; add_right += cnt_right; add_left -= cnt_left; cnt_left -= d[mid - 1]; mid--; d[mid + p[i] - n]++; if (p[i] < n) cnt_left++, add_left += n - p[i]; else if (p[i] > n) cnt_right++, add_right += p[i] - n; if (cur_ans > add_left + add_right) { cur_ans = add_left + add_right; step = n - i; } } cout << cur_ans << " " << step << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int p[maxn], n; struct Node { int i, v; Node(int pi = 0, int pv = 0) { i = pi; v = pv; } bool operator<(Node a) const { return v < a.v || (v == a.v && i < a.i); } }; priority_queue<int, vector<int>, greater<int> > q; int pointer; int s1size; long long cur, bestv, bestk; int main() { scanf("%d", &n); bestk = cur = 0; s1size = 0; for (int i = 1; i <= n; i++) { scanf("%d", p + i); cur += abs(p[i] - i); if (p[i] <= i) s1size++; else q.push(p[i] - i); } bestv = cur; pointer = n; for (int k = 1; k < n; k++) { s1size--; cur += p[pointer] - 1LL - (n - p[pointer]); cur += s1size - 0LL - q.size(); q.push(k + p[pointer] - 1); if (!--pointer) pointer = n; for (int d = q.top(); q.size() && d == k; d = q.top()) { q.pop(); s1size++; } if (cur < bestv) { bestk = k; bestv = cur; } } printf("%I64d %d\n", bestv, bestk); }
#include <bits/stdc++.h> using namespace std; const long long oo = 0x3f3f3f3f3f3f3f3fll; const int LGN = 25; const int MOD = 1e9 + 7; const int N = 1e6 + 5; int n; long long delta[N]; void add(int l, int r, int v) { if (r < l || n <= l) return; delta[l] += v; delta[r + 1] -= v; } int main() { std::ios::sync_with_stdio(false); scanf("%d", &(n)); static int p[N]; long long d = 0; for (long long i = 1, __R = n; i <= __R; i++) { scanf("%d", &(p[i])); d += abs(i - p[i]); if (p[i] > i) { add(1, p[i] - i, -1); add(p[i] - i + 1, n - i, +1); add(n - i + 1, n - i + 1, -(n - p[i]) + (p[i] - 1)); add(n - i + 2, n - 1, -1); } else if (p[i] == i) { add(1, n - i, +1); add(n - i + 1, n - i + 1, -(n - p[i]) + (p[i] - 1)); add(n - i + 2, n - 1, -1); } else { add(1, n - i, +1); add(n - i + 1, n - i + 1, -(n - p[i]) + (p[i] - 1)); add(n - i + 2, n - i + p[i], -1); add(n - i + p[i] + 1, n - 1, +1); } } long long mind = d; int mink = 0; for (long long k = 1, __R = n - 1; k <= __R; k++) { delta[k] += delta[k - 1]; d += delta[k]; if (d < mind) { mind = d; mink = k; } } printf("%lld %d\n", mind, mink); return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.141592653589793; int n; long long P[1 << 20]; long long H[1 << 20]; void update(int i, int j, int inr) { H[i] += inr; H[j + 1] -= inr; } int main() { ios::sync_with_stdio(false); cin >> n; long long val = 0, which = 0; for (int i = 0; i < n; i++) cin >> P[i + 1]; for (int k = 1; k <= n; k++) { val += abs(P[k] - k); if (P[k] >= k) { int s = 1, diff; diff = P[k] - k; if (diff > 0) { update(s, s + diff - 1, -1); s = s + diff; } if (s == n) continue; diff = (n - P[k]); if (diff > 0) { update(s, s + diff - 1, 1); s = s + diff; } if (s == n) continue; diff = 1; if (n - 2 * P[k] + 1 > 0) update(s, s + diff - 1, -(n - 2 * P[k] + 1)); else update(s, s + diff - 1, -(n - 2 * P[k] + 1)); s = s + diff; if (s == n) continue; if (k - 2 > 0) update(s, n - 1, -1); } else { int s = 1, diff; diff = n - k; if (diff > 0) { update(s, s + diff - 1, 1); s = s + diff; } if (s == n) continue; diff = 1; if (n - 2 * P[k] + 1 > 0) update(s, s + diff - 1, -(n - 2 * P[k] + 1)); else update(s, s + diff - 1, -(n - 2 * P[k] + 1)); s = s + diff; if (s == n) continue; diff = (P[k] - 1); if (diff > 0) { update(s, s + diff - 1, -1); s = s + diff; } if (s == n) continue; if (k - 1 - P[k] > 0) update(s, n - 1, 1); } } for (int i = 1; i < n; i++) H[i] += H[i - 1]; long long ans = val; for (int i = 1; i < n; i++) { val = val + H[i]; if (val < ans) { ans = val; which = i; } } cout << ans << " " << which << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000009; int cnt[maxn << 2], p[maxn]; int n, L, R, lim, rec; long long now, ans; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void update(int x, int opt) { if (x <= lim) L += opt; else R += opt; cnt[x]++; } int main() { n = read(); lim = n; for (int i = 1; i <= n; i++) { p[i] = read(); now += abs(p[i] - i); update(p[i] - i + lim, 1); } ans = now; rec = 0; for (int i = n; i >= 2; i--) { update(p[i] - n + lim, -1); now -= abs(p[i] - n); now -= R; now += L; now += abs(p[i] - 1); lim++; R -= cnt[lim]; L += cnt[lim]; update(p[i] - 1 + lim, 1); if (now < ans) rec = n - i + 1, ans = now; } printf("%I64d %d\n", ans, rec); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2000005; int per[maxn], c[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> per[i]; long long turn = 0, dis = 0; for (int i = 1; i <= n; i++) { if (per[i] >= i) { c[per[i] - i]++; turn += per[i] - i; dis--; } else { c[per[i] + n - i]++; turn += i - per[i]; dis++; } } long long ret = turn; int ps = 0; for (int i = 1; i <= n; i++) { dis += c[i - 1] * 2 - 2; turn += abs(per[n - i + 1] - 1) - abs(per[n - i + 1] - n) + 1 + dis; if (turn < ret) { ret = turn; ps = i; } } cout << ret << " " << ps; }
#include <bits/stdc++.h> using namespace std; long long n, no, add, a[1000100], cf[1000100]; int main() { scanf("%lld", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), ++cf[(a[i] + n - i) % n], no += abs(a[i] - i), a[i] >= i ? --add : ++add; long long ans = no, id = 0; for (int i = 1; i < n; ++i) add += cf[i - 1] * 2 - 2, no += abs(a[n - i + 1] - 1) - abs(a[n - i + 1] - n) + 1 + add, no < ans ? ans = no, id = i : 0; printf("%lld %lld", ans, id); }
#include <bits/stdc++.h> using namespace std; const int M = 1e6 + 10; int a[M], pre[M]; int abs(int a, int b) { if (a >= b) return a - b; return b - a; } int main() { int n; scanf("%d", &n); long long sum = 0, minn; memset(pre, 0, sizeof(pre)); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += abs(a[i], i); if (a[i] - i <= 0) pre[0]++; else pre[a[i] - i]++; } minn = sum; int cnt = pre[0], ans = 0; for (int i = 1; i < n; i++) { int pos = n - i + 1; if (a[pos] - n <= 0) cnt--; if (a[pos] - pos <= 0) pre[0]--; else pre[a[pos] - pos]--; pre[min(a[pos] - 1 + i, M - 10)]++; sum += cnt; sum -= (n - cnt - 1); sum -= abs(a[pos] - n); sum += abs(a[pos] - 1); cnt += pre[i]; if (sum < minn) { minn = sum; ans = i; } } printf("%lld %d\n", minn, ans); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long linf = 1e18 + 7; const int inf = 1e9 + 7; const int N = 1e6 + 5; int n, a[N], cnt, pc, mc; long long ans = linf, ans_p, cur; deque<int> dq; set<pair<int, int> > st; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); int x = i - a[i]; if (x >= 0) { cur += x; pc++; } else { cur -= x; mc++; st.insert(make_pair(-x, i)); } dq.push_back(x); } for (int i = n; i >= 1; i--) { if (cur < ans) { ans = cur; ans_p = n - i; } int x = dq.back(); if (x + cnt >= 0) { cur -= x + cnt; pc--; } else { cur += x + cnt; mc--; } dq.pop_back(); cur += pc; cur -= mc; cnt++; x = 1 - a[i] - cnt; if (x + cnt >= 0) { cur += x + cnt; pc++; } else { cur -= x + cnt; mc++; st.insert(make_pair(-x, i + n)); } dq.push_front(x); while (!st.empty()) { pair<int, int> pr = *st.begin(); pr.first *= -1; if (pr.first + cnt >= 0) { cur += pr.first + cnt; pc++; mc--; st.erase(st.begin()); } else break; } } cout << ans << " " << ans_p << endl; }
#include <bits/stdc++.h> using namespace std; int i, j, k, m, n, s, t; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long mn, c[1001000], a[1001000]; int main() { n = read(); for (i = 1; i <= n; i++) a[i] = read(); for (i = 1; i <= n; i++) { if (i == a[i]) { c[1]++; c[n - i + 1] += i + i - n - 2; c[n - i + 2] += -1 - 2 * i + 1 + n; } if (i > a[i]) { c[0] += i - a[i]; c[1] += 1 - i + a[i]; c[n - i + 1] += a[i] + a[i] - 2 - n; c[n - i + 2] += -1 - a[i] * 2 + 1 + n; c[n - i + a[i] + 1] += 2; } if (i < a[i]) { c[0] += a[i] - i; c[1] += -1 - a[i] + i; c[a[i] - i + 1] += 2; c[n - i + 1] += a[i] + a[i] - 2 - n; c[n - i + 2] += -1 - a[i] * 2 + 1 + n; } } mn = (long long)n * n; for (i = 1; i < n; i++) c[i] += c[i - 1]; for (i = 1; i < n; i++) c[i] += c[i - 1]; for (i = 0; i < n; i++) { if (c[i] < mn) { mn = c[i]; j = i; } } cout << mn << " " << j << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; int n; int ar[maxn]; int cnt[maxn << 2]; int main() { scanf("%d", &n); long long d = 0; int s = 0; int f = 0, z = 0; for (int i = 1; i <= n; i++) { scanf("%d", ar + i); int tmp = ar[i] - i; if (tmp > 0) { z++; cnt[tmp]++; } else f++; d += abs(tmp); } int o = 1; long long td = d; for (int i = n, j = 1; i > 1; i--, j++, o++) { if (ar[i] > i) cnt[ar[i] - i]--; if (ar[i] > n) z--; else f--; td += f - z; td += abs(ar[i] - 1) - abs(ar[i] - n); f += cnt[o]; z -= cnt[o]; if (ar[i] != 1) { cnt[o + ar[i] - 1]++; z++; } else f++; if (td < d) { d = td; s = j; } } printf("%I64d %d\n", d, s); return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000000 + 9]; long long d[1000000 + 9]; long long inst[1000000 + 9]; long long perm[1000000 + 9]; int n; int main() { cin.tie(0); cin.sync_with_stdio(0); cin >> n; for (int i = (1); i <= (n); i++) cin >> a[i]; for (int i = 1; i <= n; i++) { if (a[i] >= i) { int x = a[i] - i; int l = 0, r = x; inst[l] += x + 1; perm[l] += -1; perm[r + 1] += 1; l = x + 1, r = n - i; perm[l] += 1; perm[r + 1] -= 1; inst[r + 1] -= (r - l + 1); x = a[i] - 1; l = n - i + 1, r = n - 1; inst[l] += x + 1; perm[l] += -1; } else { int x = i - a[i]; int l = 0, r = n - i; inst[l] += x; perm[l + 1] += 1; perm[r + 1] -= 1; inst[r + 1] -= x + (r - l); x = a[i] - 1; l = n - i + 1, r = l + x; inst[l] += x + 1; perm[l] -= 1; perm[r + 1] += 1; l = r + 1, r = n - 1; perm[l] += 1; } } long long val = 0, inc = 0; for (int i = 0; i < n; i++) { val += inst[i]; inc += perm[i]; val += inc; d[i] = val; } long long minDev = d[0]; int bestShift = 0; for (int i = 0; i < n; i++) { if (d[i] < minDev) { minDev = d[i]; bestShift = i; } } cout << minDev << " " << bestShift << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; multiset<int> b; multiset<int>::iterator it; int sia = 0; long long ansa = 0, ansb = 0; stack<int> sta; int k[1000005]; int main() { int n, ansk = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", k + i); if (k[i] > i) { b.insert(k[i] - i); ansb += k[i] - i; } else { ++sia; ansa += i - k[i]; } } long long mi = ansa + ansb; int co = 1; for (int i = n; i > 1; --i) { ansb -= b.size(); ansa += sia; int t = b.size(); it = b.upper_bound(co); b.erase(b.begin(), it); t -= b.size(); sia += t; if (k[i] > n + 1) { b.erase(b.find(k[i] - n - 1 + co)); ansb -= k[i] - n + 1; } else { --sia; ansa -= n + 1 - k[i]; } if (k[i] > 1) { b.insert(k[i] - 1 + co); ansb += k[i] - 1; } else { ++sia; ansa += 1 - k[i]; } ++co; if (mi > ansa + ansb) { mi = ansa + ansb; ansk = co - 1; } } cout << mi << ' ' << ansk << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int a[maxn], b[maxn]; int main() { int n, id = 0, cnt = 0; scanf("%d", &n); long long diff = 0; for (int i = (1); i < (n + 1); ++i) { scanf("%d", &a[i]); if (a[i] > i) b[a[i] - i]++, cnt++; diff += abs(a[i] - i); } long long ans = diff; for (int i = (1); i < (n); ++i) { int last = a[n - i + 1]; diff += last - 1 - (n - last); diff += n - 2 * cnt - 1; if (diff < ans) ans = diff, id = i; cnt -= b[i]; if (last > 1) { cnt++; if (last - 1 + i <= n) b[last - 1 + i]++; } } cout << ans << " " << id << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; int A[1000005]; int big[1000005]; int main() { int a = 0, b = 0, c = 0; long long ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); ans += abs(A[i] - i); if (A[i] > i) { c++; big[A[i] - i]++; } else if (A[i] == i) { a++; big[0]++; } else { b++; } } int pos = n, id = 0; int ans_pos = 0; long long tmp = ans; while (--pos) { id++; tmp += b + a; tmp -= c; b = b + a; c = c - big[id]; a = big[id]; if (A[pos + 1] == n) { tmp += n - 2; int t = id + n - 1; if (t <= n) big[t]++; b--; c++; } else if (A[pos + 1] == 1) { b--; a++; tmp -= n; } else { b--; c++; int t = id + A[pos + 1] - 1; if (t <= n) big[t]++; tmp += -abs(A[pos + 1] - n - 1) + (A[pos + 1] - 1); } if (tmp <= ans) { ans = tmp; ans_pos = id; } } printf("%I64d %d\n", ans, ans_pos); return 0; }
#include <bits/stdc++.h> using namespace std; int p[1000010], n; long long hold[1000010], sum, res, ind; int main() { sum = ind = res = 0; memset(hold, 0, sizeof(hold)); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", p + i); if (p[i] > i) { hold[1]--; hold[p[i] - i + 1] += 2; hold[p[i] - i + 1 + n - p[i]]--; hold[p[i] - i + 1 + n - p[i] + 1]--; sum += ((long long)p[i] - (long long)i); } else { hold[1]++; hold[n - i + 1]--; hold[n - i + 2]--; hold[n - i + 2 + p[i] - 1] += 2; sum += ((long long)i - (long long)p[i]); } } res = sum; ind = 0; for (int i = 1; i <= n; i++) { hold[i] += hold[i - 1]; } hold[n] = 0; for (int i = 1; i < n; i++) { sum += hold[i]; sum += (p[n - i + 1] - 1 - abs(p[n - i + 1] - n)); if (sum < res) { res = sum; ind = i; } } printf("%lld %lld", res, ind); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long input() { int32_t x; scanf("%d", &x); return x; } const long long maxn = 2e6 + 1000; const long long mod = 1e9 + 7; struct old { long long sum_[maxn], a[maxn]; long long cnt[maxn], add[maxn]; long long Q = 0; long long L[maxn], R[maxn]; inline long long Sum(long long n) { return (n * (n + 1) / 2); } inline long long ok(long long x) { return x; } inline long long answer(long long l, long long r) { long long res = ok(sum_[r] - (l ? sum_[l - 1] : 0)); for (long long pt = 0; pt < Q; pt++) { pair<long long, long long> eshterak = {max(l, L[pt]), min(r, R[pt])}; if (eshterak.first > eshterak.second) continue; long long sz = eshterak.second - eshterak.first + 1; long long start = eshterak.first - L[pt] + 1; res = ok(ok(Sum(start + sz - 1) - Sum(start - 1)) + res); } return res; } inline void clear() { for (long long i = 1; i < maxn; i++) { cnt[i] = ok(cnt[i] + cnt[i - 1]); add[i] = ok(add[i] + add[i - 1] + cnt[i - 1]); } memset(cnt, 0, sizeof cnt); for (long long i = 0; i < maxn; i++) { add[i] = ok(add[i] + (i ? add[i - 1] : 0)); sum_[i] = ok(sum_[i] + add[i]); } memset(add, 0, sizeof add); Q = 0; } long long shit(char tp, long long l = -1, long long r = -1) { if (tp == 'A') return answer(l, r); else { cnt[l] = ok(cnt[l] + 1); add[l] = ok(1 + add[l]); cnt[r + 1] = ok(cnt[r + 1] - 1); add[r + 1] = ok(add[r + 1] - (r - l + 2)); L[Q] = l, R[Q] = r; Q++; return 0; } } old() { for (long long i = 0; i < maxn; i++) { sum_[i] = a[i] = cnt[i] = add[i] = L[i] = R[i] = 0; } } } pos, neg; long long a[maxn], n, dp[maxn]; inline long long reverse(long long x) { return n - x - 1; } inline void dj() { for (long long i = 1; i < n; i++) dp[i] += dp[i - 1]; } inline void add_seg(long long from, long long to, long long val) { dp[from] += val; dp[to + 1] -= val; } int32_t main() { n = input(); for (long long i = 0; i < n; i++) a[i] = input(); for (long long i = 0; i < n; i++) { long long L = i - a[i] + 1; if (L >= 0) { long long from = reverse(i), to = reverse(L); add_seg(from, to, a[i]); neg.shit('Q', from, to); if (L > 0) { from = reverse(L - 1), to = reverse(0); pos.shit('Q', from, to); } if (i == n - 1) continue; from = reverse(n - 1), to = reverse(i + 1); pos.shit('Q', from, to); add_seg(from, to, L); } else { long long from = reverse(n - 1), to = reverse(n + L); add_seg(from, to, a[i]); neg.shit('Q', from, to); add_seg(from, to, -(i + 1)); from = reverse(i), to = reverse(0); add_seg(from, to, a[i]); neg.shit('Q', from, to); from = reverse(n + L - 1), to = reverse(i + 1); if (from > to) continue; pos.shit('Q', from, to); } } dj(); pos.clear(); neg.clear(); long long id, res = 1e18; for (long long i = 0; i < n; i++) { long long value = pos.shit('A', n - i - 1, n - i - 1) - neg.shit('A', n - i - 1, n - i - 1) + dp[n - i - 1]; if (value < res) res = value, id = (n - i) % n; } cout << res << " " << id << endl; }
#include <bits/stdc++.h> using namespace std; int n, p[1000010], cnt[1000010]; int neg; long long res, ans, pos; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i]; for (int i = 1; i <= n; ++i) { res += abs(p[i] - i); cnt[(p[i] - i + n) % n]++; if (p[i] <= i) neg++; else pos++; } ans = res; int idx = 0; for (int k = 1; k < n; ++k) { res -= pos; res += neg - 1; res += p[(n - k) % n + 1] - n + p[(n - k) % n + 1] - 1; pos -= cnt[k] - 1; neg += cnt[k] - 1; if (res < ans) { idx = k; ans = res; } } cout << ans << " " << idx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long n; cin >> n; long long p[n], to_zero[n], numl = 0, numg = 0; fill(to_zero, to_zero + n, 0); for (int i = 0; i < n; i++) cin >> p[i], p[i]--; for (int i = 0; i < n; i++) { if (i <= p[i]) to_zero[p[i] - i]++; else if (i > p[i]) to_zero[n - i + p[i]]++; } int shift = 0; long long curr = 0, min_ans; for (long long i = 0; i < n; i++) { curr += labs(p[i] - i); if (i < p[i]) numl++; else numg++; } min_ans = curr; for (long long k = 1; k < n; k++) { curr += numg - numl + 2 * p[n - k] - n; numl -= to_zero[k] - 1; numg = n - numl; if (curr < min_ans) min_ans = curr, shift = k; } cout << min_ans << " " << shift << endl; ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000000; int n, p[N + 1], cnt[N + N + N]; int main() { scanf("%d", &n); int shift = 0, gt = 0, lte = 0; long long dev = 0; for (int i = 1; i <= n; ++i) { scanf("%d", p + i); ++cnt[p[i] - i + n]; dev += abs(p[i] - i); if (p[i] <= i) ++lte; else ++gt; } long long ans = dev; for (int k = 1; k < n; ++k) { dev += lte - gt - 1 + 2 * p[n - k + 1] - (n + 1); --cnt[p[n - k + 1] - (n - k + 1) + n]; ++cnt[p[n - k + 1] - (n - k + 1) + n + n]; lte += cnt[k + n] - 1; gt -= cnt[k + n] - 1; if (dev < ans) { shift = k; ans = dev; } } printf("%lld %d\n", ans, shift); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long int LINF = 0x3f3f3f3f3f3f3f3fll; const long double pi = acos(-1); const int MOD = 1e9 + 7; int v[1000100]; int v2[1000100]; int n; long long int S = 0; int antes = 0, depois = 0; int mapa[1000100]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &v[i]); v[i]--; S += abs(v[i] - i); if (i < n - 1) { antes += ((v[i] - i) > 0); depois += ((v[i] - i) <= 0); } } for (int i = 0; i < n; i++) { int s = v[i] - i; if (s < 0) s += n; v2[i] = s; mapa[v2[i]]++; } int j = n - 1; long long int r = S; int idx = 0; for (int i = 1; i < n; i++) { S = S - abs(v[j] - n + 1); S = S + v[j] - antes + depois; antes = antes - mapa[i] + 1 - (v[j] == 0) - (v[j - 1] - n + 1 > 0); depois = depois + mapa[i] - 1 + (v[j] != 0) - (v[j - 1] - n + 1 <= 0); j--; if (S < r) { r = S; idx = i; } } cout << r << " " << idx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e6 + 100; long long bestDev, cur; int cntPos = 0, cntNeg; int N, A[MAXN], cnt[MAXN], bestIdx; int main() { scanf("%d", &N); for (int i = 0; i < (int)N; i++) scanf("%d", &A[i]), A[i]--; for (int i = 0; i < (int)N; i++) bestDev += abs(A[i] - i); for (int i = 0; i < (int)N; i++) if (A[i] - i >= 0) cnt[A[i] - i]++, cntPos++; cur = bestDev; cntNeg = N - cntPos; for (int i = 0; i < (int)N; i++) { if (cur < bestDev) { bestDev = cur; bestIdx = i; } cnt[A[N - i - 1] + i + 1]++; if (A[N - i - 1] - (N - i - 1) >= 0) cnt[A[N - i - 1] - (N - i - 1)]--; if (A[N - i - 1] - (N - i - 1) < i) cntPos++, cntNeg--; cntNeg += cnt[i]; cntPos -= cnt[i]; cur += cntNeg - cntPos + A[N - i - 1] - abs(A[N - i - 1] - (N - i - 1)); if (A[N - i - 1] > N - i - 1) cur += (A[N - i - 1] - (N - i - 1) + 1) - (N - A[N - i - 1] - 1); else cur -= i - 1; } printf("%lld %d\n", bestDev, bestIdx); }
#include <bits/stdc++.h> using namespace std; void ga(int N, int *A) { for (int i(0); i < N; i++) scanf("%d", A + i); } int N, A[(1000006)], g, l, G[(1000006)], L[(1000006)], P[(1000006)], a, W; long long S, X; int main(void) { scanf("%d", &N), ga(N, A + 1); for (int k(1); k < N + 1; k++) if (A[k] > k) ++g, S += a = A[k] - k, --G[a], ++L[a], P[N - k] += 2 * A[k] - 1 - N, ++G[N - k], --L[N - k]; else ++l, S += k - A[k], a = N - k + A[k], --G[a], ++L[a], P[N - k] += 2 * A[k] - 1 - N, ++G[N - k], --L[N - k]; X = S; for (int k(0); k < N; k++) { l += L[k], g += G[k], S += P[k] + l - g + 1; if (S < X) X = S, W = k + 1; } printf("%lld %d\n", X, W); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename G> inline void read(G &x) { x = 0; char c; while (!isdigit(c = getchar())) ; do { x = x * 10 + c - '0'; } while (isdigit(c = getchar())); } template <typename G> inline void write(G x) { if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <class T> inline T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> inline T min(T a, T b, T c, T d) { return min(a, min(b, c, d)); } template <class T> inline T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> inline T max(T a, T b, T c, T d) { return max(a, max(b, c, d)); } const int MAXN = 1e6 + 7; const int base = 1e9 + 7; const int N = 2e6 + 7; int n, m; int p[MAXN], bit[2 * MAXN]; long long res; void upd(int x, int value) { while (x <= 2 * m) { bit[x] += value; x += x & (-x); } } int get(int x) { int res = 0; while (x) { res += bit[x]; x -= x & (-x); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); read(n); for (int i = (1); i <= (n); ++i) read(p[i]); m = n; for (int i = (1); i <= (n); ++i) { upd(p[i] - i + m, 1); res += abs(p[i] - i); } long long mii = res; int ans = 0; for (int k = (1); k <= (n); ++k) { int i = n - k + 1; res -= abs(p[i] - n); res += abs(p[i] - 1); upd(p[i] - n + m, -1); int x = get(m + k - 1); res += x; res -= (n - 1 - x); upd(p[i] - 1 + m + k, 1); if (res < mii) { ans = k; mii = res; } } cout << mii << ' ' << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 100; int a[maxn]; int change[maxn]; int main() { int n; int down, up; while (scanf("%d", &n) != EOF) { memset(change, 0, sizeof(change)); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); long long sum = 0; long long down = up = 0; for (int i = 1; i <= n; i++) { change[(a[i] - i + n) % n]++; if (a[i] > i) down++; else if (a[i] <= i) up++; sum += abs(a[i] - i); } long long ans = sum; long long pos = 0; for (int i = 1; i < n; i++) { sum = sum + up - down - 1; sum = sum + 2 * a[(n - i) % n + 1] - n - 1; up--; down++; up += change[i]; down -= change[i]; if (sum < ans) { ans = sum; pos = i; } } printf("%I64d %I64d\n", ans, pos); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using db = double; ll a[1000006]; ll p[1000006]; ll s[1000006]; void add(ll l, ll r, ll L, ll R) { if (L <= R) { a[l] += 1; a[r] -= 1; p[r] -= r - l; p[l] += L - 1; p[r] -= L - 1; } else { a[l] -= 1; a[r] += 1; p[r] += r - l; p[l] += L + 1; p[r] -= L + 1; } } int n; int main() { cin >> n; for (int i = 0; i < n; ++i) { ll a; scanf("%lld", &a); --a; if (i >= a) { add(0, n - i, i - a, n - a); add(n - i, n - i + a, a - 0, 0); add(n - i + a, n, 0, i - a); } else { add(0, a - i, a - i, 0); add(a - i, n - i, 0, n - a); add(n - i, n, a, a - i); } } partial_sum(a, a + n, a); partial_sum(a, a + n, a); partial_sum(p, p + n, p); transform(a, a + n, p, s, plus<ll>()); auto t = min_element(s, s + n); cout << *t << ' ' << t - s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int p[1000010], n; long long hold[1000010], sum, res, ind; int main() { sum = ind = res = 0; memset(hold, 0, sizeof(hold)); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", p + i); if (p[i] > i) { hold[1]--; hold[p[i] - i + 1] += 2; hold[n - i + 1]--; hold[n - i + 2]--; sum += ((long long)p[i] - (long long)i); } else { hold[1]++; hold[n - i + 1]--; hold[n - i + 2]--; hold[n - i + 1 + p[i]] += 2; sum += ((long long)i - (long long)p[i]); } } res = sum; for (int i = 1; i <= n; i++) { hold[i] += hold[i - 1]; } hold[n] = 0; for (int i = 1; i < n; i++) { sum += hold[i]; sum += (p[n - i + 1] - 1 - abs(p[n - i + 1] - n)); if (sum < res) { res = sum; ind = i; } } printf("%lld %lld", res, ind); return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000000 + 9]; long long inst[1000000 + 9], perm[1000000 + 9]; long long d[1000000 + 9]; void updateRange(int x, int k, int l, int r) { inst[l] += x; perm[l + 1] += k; perm[r + 1] -= k; inst[r + 1] -= x + (long long)(r - l) * k; } void acc(long long* a, int l, int r) { long long val = 0, inc = 0; for (int i = l; i <= r; i++) { val += inst[i]; inc += perm[i]; val += inc; a[i] = val; } } int main() { cin.tie(0); cin.sync_with_stdio(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { if (a[i] >= i) { int x = a[i] - i; updateRange(x, -1, 0, x); updateRange(1, 1, x + 1, n - i); updateRange(a[i] - 1, -1, n - i + 1, n - 1); } else { int x = i - a[i]; updateRange(x, 1, 0, n - i); updateRange(a[i] - 1, -1, n - i + 1, n - i + a[i]); updateRange(1, 1, n - i + a[i] + 1, n - 1); } } acc(d, 0, n - 1); long long bestDev = d[0], besti = 0; for (int i = 0; i <= n - 1; i++) { if (d[i] < bestDev) { bestDev = d[i]; besti = i; } } cout << bestDev << " " << besti << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long ans[1000005], num[1000005]; int main() { int n; long long minn, x; int temp; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &x); num[i] = x; if (i < x) { ans[1]--; ans[x - i + 1]++; ans[x - i + 1]++; ans[n - i + 1]--; ans[n - i + 1] += 2 * x - 1 - n; ans[n - i + 2] -= 2 * x - 1 - n; ans[n - i + 2]--; } else { ans[1]++; ans[n - i + 1]--; ans[n - i + 1] += 2 * x - 1 - n; ans[n - i + 2] -= 2 * x - 1 - n; ans[n - i + 2]--; ans[n - i + x + 1]++; ans[n - i + x + 1]++; } } for (int i = 1; i < n; i++) ans[i] += ans[i - 1]; for (int i = 1; i <= n; i++) ans[0] += abs(i - num[i]); temp = 0; minn = ans[0]; for (int i = 1; i < n; i++) { ans[i] += ans[i - 1]; if (ans[i] < minn) { minn = ans[i]; temp = i; } } printf("%lld %d\n", minn, temp); return 0; }
#include <bits/stdc++.h> const double Pi = acos(-1.0); using namespace std; const int maxN = 100005; const long long inf = (long long)1e15; int n; long long b[maxN]; long long k[maxN]; long long req[maxN]; vector<int> G[maxN]; void dfs(int cur) { for (int i = 0; i < (int)G[cur].size(); i++) { int nxt = G[cur][i]; dfs(nxt); if (req[nxt] < 0) { if (inf / k[nxt] < (-req[nxt])) { puts("NO"); exit(0); } req[cur] += k[nxt] * req[nxt]; } else req[cur] += req[nxt]; } } int main(int argc, char** argv) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &b[i]); long long a; for (int i = 1; i <= n; i++) { scanf("%lld", &a); req[i] = b[i] - a; } int x; for (int i = 2; i <= n; i++) { scanf("%d %lld", &x, &k[i]); G[x].push_back(i); } dfs(1); puts((req[1] < 0) ? "NO" : "YES"); return 0; }
#include <bits/stdc++.h> using namespace std; auto scan = [] { static long long a; scanf("%lld", &a); return a; }; vector<long long> g[100000]; long long A[100000], cost[100000], ok = 1; double R[100000]; void dfs(long long v, long long p = -1) { for (long long u : g[v]) dfs(u, v); if (~p) R[p] += R[v] * (R[v] < 0 ? cost[v] : 1), A[p] += A[v] * (A[v] < 0 ? cost[v] : 1); } int main() { long long n = scan(), x; for (long long i = 0; i < n; ++i) A[i] = scan(); for (long long i = 0; i < n; ++i) A[i] -= scan(), R[i] = A[i]; for (long long i = 1; i < n; ++i) { x = scan() - 1, cost[i] = scan(); g[x].push_back(i); } dfs(0); puts(A[0] >= 0 && R[0] >= -100 ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const long long INF = (1LL << 60) - 1; vector<pair<int, int> > e[MAXN]; long long a[MAXN], b[MAXN], dp[MAXN]; void dfs(int u) { dp[u] = b[u] - a[u]; for (int i = 0; i < (int)e[u].size(); i++) { int v = e[u][i].first, r = e[u][i].second; dfs(v); if (dp[v] < 0) { if (-dp[v] < INF / r) dp[u] += dp[v] * r; else dp[u] = -INF; if (dp[u] < -INF) dp[u] = -INF; } else dp[u] += dp[v]; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &b[i]); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 2; i <= n; i++) { int x, k; scanf("%d%d", &x, &k); e[x].push_back(make_pair(i, k)); } dfs(1); return 0 * printf("%s\n", (dp[1] >= 0 ? "YES" : "NO")); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const long long INF = 5e18; const long long LOG_INF = 60; int p[MAXN]; long long a[MAXN], k[MAXN]; int n, qu[MAXN], deg[MAXN]; int my_log(long long x) { int ans = 0; while (x > 0) { x /= 2; ++ans; } return ans; } void solve() { cin >> n; for (int i = 0; i < n; i++) { long long x; cin >> x; a[i] = x; } for (int i = 0; i < n; i++) { long long x; cin >> x; a[i] = x - a[i]; } for (int v = 1; v < n; v++) { long long kk; cin >> p[v] >> kk; --p[v]; ++deg[p[v]]; k[v] = kk; } int h = 0, t = 0; for (int v = 0; v < n; v++) { if (deg[v] == 0) { qu[t++] = v; } } while (t - h) { int v = qu[h++]; if (v == 0) { if (a[v] > 0) { cout << "NO\n"; return; } break; } if (a[v] > 0) { if (my_log(a[v]) + my_log(k[v]) > LOG_INF) { cout << "NO\n"; return; } if (my_log(a[p[v]]) > LOG_INF) { cout << "NO\n"; return; } a[p[v]] += a[v] * k[v]; } else { a[p[v]] += a[v]; } if (--deg[p[v]] == 0) qu[t++] = p[v]; } cout << "YES\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int te = 1; for (int w = 1; w <= te; w++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void cmax(T& a, T b) { a = max(a, b); } template <class T> void cmin(T& a, T b) { a = min(a, b); } void _BG(const char* s) {} template <class T, class... TT> void _BG(const char* s, T a, TT... b) { for (int c = 0; *s && (c || *s != ','); ++s) { cerr << *s; for (char x : "([{") c += *s == x; for (char x : ")]}") c -= *s == x; } cerr << " = " << a; if (*s) { cerr << ", "; _BG(++s, b...); } else cerr << endl; } bool RD() { return 1; } bool RD(char& a) { return scanf(" %c", &a) == 1; } bool RD(char* a) { return scanf("%s", a) == 1; } bool RD(double& a) { return scanf("%lf", &a) == 1; } bool RD(int& a) { return scanf("%d", &a) == 1; } bool RD(long long& a) { return scanf("%lld", &a) == 1; } template <class T, class... TT> bool RD(T& a, TT&... b) { return RD(a) && RD(b...); } void PT(const char& a) { putchar(a); } void PT(char const* const& a) { fputs(a, stdout); } void PT(const double& a) { printf("%.16f", a); } void PT(const int& a) { printf("%d", a); } void PT(const long long& a) { printf("%lld", a); } template <char s = ' ', char e = '\n'> void PL() { if (e) PT(e); } template <char s = ' ', char e = '\n', class T, class... TT> void PL(const T& a, const TT&... b) { PT(a); if (sizeof...(b) && s) PT(s); PL<s, e>(b...); } const int N = 1e5 + 87; const long long inf = 1e18; long long a[N]; vector<pair<int, int> > g[N]; long long dfs(int u) { for (auto v : g[u]) { long long k = dfs(v.first); if (k >= 0) a[u] += k; else if (-k <= (inf + a[u]) / v.second) a[u] += k * v.second; else return a[u] = -inf; } return a[u]; } int main() { int n; RD(n); for (int i(1); i < ((n) + 1); ++i) RD(a[i]); for (int i(1); i < ((n) + 1); ++i) { long long b; RD(b); a[i] -= b; } for (int i(2); i < ((n) + 1); ++i) { int p, k; RD(p, k); g[p].push_back({i, k}); } PL(dfs(1) >= 0 ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void read(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void readc(char &x) { char c; while ((c = getchar()) == ' ') ; x = c; } inline void writes(string s) { puts(s.c_str()); } inline void writeln() { writes(""); } inline void writei(int x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } inline void writell(long long x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } long long n, m, i, j, b[100005], a[100005], f[100005], x, y; vector<pair<long long, long long> > e[100005]; long long mul(long long x, long long y) { x = abs(x); long long t = 0x1bbbbbbbbbbbbbbbll / y; if (t < x) return -0x1bbbbbbbbbbbbbbbll; return -min(0x1bbbbbbbbbbbbbbbll, x * y); } long long add(long long x, long long y) { return max(-0x1bbbbbbbbbbbbbbbll, x + y); } void dfs(long long x) { f[x] = b[x] - a[x]; for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) { dfs(it->first); if (f[it->first] < 0) { f[it->first] = mul(f[it->first], it->second); } f[x] = add(f[x], f[it->first]); } } int main() { read(n); for (((i)) = (1); ((i)) <= ((n)); ((i))++) { read(b[i]); } for (((i)) = (1); ((i)) <= ((n)); ((i))++) { read(a[i]); } for ((i) = (2); (i) <= (n); (i)++) { read(x); read(y); e[x].push_back(make_pair(i, y)); } dfs(1); if (f[1] >= 0) { cout << "YES"; return 0; } { cout << "NO"; return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long oo = 1e17 + 10; long long mul(long long a, long long b) { if ((double)a * (double)b > oo) return oo; return a * b; } long long sum(long long a, long long b) { if ((double)a + (double)b > oo) return oo; return a + b; } const int N = 100010; int deg[N]; long long extra[N], need[N], b[N], a[N]; int x[N], k[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", b + i); for (int i = 1; i <= n; i++) scanf("%lld", a + i); for (int i = 2; i <= n; i++) scanf("%d %d", x + i, k + i); for (int i = n; i >= 1; i--) { if (b[i] < a[i]) need[i] += a[i] - b[i]; else extra[i] += b[i] - a[i]; if (need[i] <= extra[i]) { extra[i] -= need[i]; need[i] = 0; } else { need[i] -= extra[i]; extra[i] = 0; } if (i > 1) { if (extra[i]) { extra[x[i]] += extra[i]; extra[i] = 0; } else { need[x[i]] = sum(need[x[i]], mul(need[i], k[i])); need[i] = 0; } } } printf("%s\n", need[1] ? "NO" : "YES"); return 0; }
#include <bits/stdc++.h> using namespace std; const int Z = (int)1e5 + 111; const int inf = (int)1e9 + 111; const long long llinf = (long long)1e18 + 5; const int MOD = (int)1e9 + 7; long double need[Z], a[Z], b[Z], c[Z]; vector<int> g[Z]; void dfs(int v) { need[v] = a[v] - b[v]; for (int to : g[v]) { dfs(to); } for (int to : g[v]) { if (need[to] < 0) { need[v] += need[to]; } else { need[v] += need[to] * c[to]; } } } int main() { srand(time(0)); ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> b[i]; } for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 2; i <= n; ++i) { int x, k; cin >> x >> k; g[x].push_back(i); c[i] = k; } dfs(1); cout << (need[1] <= 0 ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; long long a[MAX], b[MAX], k[MAX]; vector<int> adj[MAX]; int parent[MAX]; int n; void die() { puts("NO"); exit(0); } inline bool overflow(long long a, long long b) { return a >= (long long)1e18 / b; } void dfs(int source) { for (auto &each : adj[source]) { dfs(each); } if (b[source] > a[source]) { b[parent[source]] += b[source] - a[source]; b[source] = a[source]; } else if (source > 1) { long long need = a[source] - b[source]; b[source] = a[source]; if (overflow(need, k[source])) { die(); } b[parent[source]] -= need * k[source]; if (b[parent[source]] <= (long long)-1e18) { die(); } } } int main() { scanf("%d", &n); long long sum_b = 0, sum_a = 0; for (int i = int(1); i < int(n + 1); i++) { scanf("%lld", b + i); sum_b += b[i]; } for (int i = int(1); i < int(n + 1); i++) { scanf("%lld", a + i); sum_a += a[i]; } if (sum_a > sum_b) { puts("NO"); return 0; } for (int i = int(2); i < int(n + 1); i++) { scanf("%d %lld", parent + i, k + i); adj[parent[i]].push_back(i); } dfs(1); bool ok = true; for (int i = int(1); i < int(n + 1); i++) { if (a[i] > b[i]) { ok = false; } } puts(ok ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, i, j, k, x, y, z, m, q, h, l, timer; long long a[100500], b[100500], w[100500]; vector<pair<long long, long long> > g[100500]; void dfs(long long v, long long p = -1) { w[v] = a[v] - b[v]; for (int i = 0; i < g[v].size(); i++) { long long to = g[v][i].first; if (to != p) { dfs(to, v); long long val = g[v][i].second; if (w[to] > 0) w[v] += w[to]; else if (w[to] < 0) { long long cur = -w[to]; if (cur > ((long long)1e+18) / val + 1 || w[v] < -(long long)1e+18) { cout << "NO" << endl; exit(0); } w[v] -= cur * val; } } } } int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { cin >> b[i]; } for (i = 2; i <= n; i++) { cin >> x >> y; g[x].push_back(make_pair(i, y)); g[i].push_back(make_pair(x, y)); } dfs(1); if (w[1] < 0) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long double a[100010], b[100010], w[200010], f[100010]; int cnt, head[100010], to[200010], nxt[200010]; void add(int x, int y, int z) { cnt++; to[cnt] = y; nxt[cnt] = head[x]; head[x] = cnt; w[cnt] = z; } void dfs(int x) { for (int i = head[x]; i; i = nxt[i]) { int y = to[i]; dfs(y); if (f[y] > 0) { f[x] += f[y]; } else { f[x] += f[y] * w[i]; f[y] = 0; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { cin >> a[i]; f[i] = b[i] - a[i]; } for (int i = 2; i <= n; i++) { int x, k; cin >> x >> k; add(x, i, k); } dfs(1); if (f[1] < 0) { puts("NO"); } else { puts("YES"); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long a[100000], b[100000]; vector<pair<int, long long>> connections[100000]; pair<long long, long long> needed(int node) { long long temp = a[node] - b[node]; long long temp2 = temp / 1000000007; temp %= 1000000007; for (const pair<int, int> edge : connections[node]) { pair<long long, long long> curr = needed(edge.first); if (curr.first > 0 || curr.first == 0 && curr.second >= 0) { curr.first *= edge.second; curr.second *= edge.second; } temp += curr.second; temp2 += temp / 1000000007; temp %= 1000000007; temp2 += curr.first; } return make_pair(temp2, temp); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < (n); i++) cin >> b[i]; for (int i = 0; i < (n); i++) cin >> a[i]; for (int i = 0; i < (n - 1); i++) { int x; long long k; cin >> x >> k; connections[--x].push_back(make_pair(i + 1, k)); } pair<long long, long long> res = needed(0); if (res.first <= 0 && res.second <= 0) cout << "YES\n"; else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <typename T, typename U> pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } const long long int md = 998244353; const long long int maxn = 1e5 + 5; long long int a[maxn], b[maxn]; vector<pair<long long int, long long int>> g[maxn]; long long int dfs(long long int u, long long int p) { for (auto v : g[u]) { long long int x = dfs(v.first, u); if (x < 0) { b[u] += -x; } else { if ((long double)(v.second) * x > 1e16) { cout << "NO" << '\n'; exit(0); } b[u] -= v.second * x; if (b[u] < -1e16) { cout << "NO" << '\n'; exit(0); } } } return a[u] - b[u]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; vector<long long int> p(n + 1), k(n + 1); for (long long int i = 1; i <= n; i++) { cin >> b[i]; } for (long long int i = 1; i <= n; i++) { cin >> a[i]; } for (long long int i = 2; i <= n; i++) { cin >> p[i] >> k[i]; g[p[i]].push_back({i, k[i]}); } long long int res = dfs(1, 0); if (res > 0) cout << "NO" << '\n'; else cout << "YES" << '\n'; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MOD2 = 1007681537; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } const int maxn = 1e5 + 5; int n; long long a[maxn]; long long b[maxn]; int x[maxn]; int k[maxn]; vector<pair<int, int> > adj[maxn]; long long dfs(int u) { long long res = 0; for (int i = (0); i < (int((adj[u]).size())); i++) { int v = adj[u][i].first; int w = adj[u][i].second; long long t = dfs(v); if (t < 0) { res += t; } else { res += min(t, (LINF + LINF) / w) * w; } chkmin(res, LINF + LINF); } res += a[u] - b[u]; return res; } void solve() { cin >> n; for (int i = (0); i < (n); i++) cin >> b[i]; for (int i = (0); i < (n); i++) cin >> a[i]; for (int i = (1); i < (n); i++) { cin >> x[i] >> k[i], x[i]--; adj[x[i]].push_back(make_pair(i, k[i])); } if (dfs(0) <= 0) { cout << "YES\n"; } else { cout << "NO\n"; } } int main() { int JUDGE_ONLINE = 1; if (fopen("in.txt", "r")) { JUDGE_ONLINE = 0; assert(freopen("in.txt", "r", stdin)); } else { ios_base::sync_with_stdio(0), cin.tie(0); } solve(); if (!JUDGE_ONLINE) { } return 0; }
#include <bits/stdc++.h> using namespace std; inline int iin() { int x; scanf("%d", &x); return x; } inline long long int lin() { long long int x; scanf("%lld", &x); return x; } const long long int N = 1e6 + 85; const long long int M = 1e3 + 85; const long long int mod = 1e9 + 7; const long long int pinf = LLONG_MAX; const long long int minf = LLONG_MIN; long long int arr[1000 * 100 + 10]; vector<pair<int, double> > edge[1000 * 100 + 10]; bool mark[1000 * 100 + 10]; void DFS(int v) { mark[v] = true; for (auto e : edge[v]) if (!mark[e.first]) { DFS(e.first); if (arr[e.first] >= 0) arr[v] += arr[e.first]; else { if (arr[e.first] * 0.1 * e.second + arr[v] < -1e18) { cout << "NO\n"; exit(0); } arr[v] += arr[e.first] * e.second; } } } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) { long long int x; cin >> x; arr[i] -= x; } for (int i = 2; i <= n; i++) { long long int x, k; cin >> x >> k; edge[x].push_back({i, k}); } DFS(1); return cout << (arr[1] >= 0 ? "YES\n" : "NO\n") << endl, 0; }
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> arr, brr, need, excess; vector<pair<long long, long long> > adj; bool ovr = false; void compute(long long s) { if (excess[s] >= need[s]) { excess[s] = excess[s] - need[s]; need[s] = 0; } else { need[s] = need[s] - excess[s]; excess[s] = 0; } if (s != 0) { long long next = adj[s].first; long long mul = adj[s].second; long long temp = need[s] * mul; if (temp < 0) { ovr = true; } need[next] += temp; if (need[next] < 0) ovr = true; excess[next] += excess[s]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n; arr.resize(n); brr.resize(n); need.resize(n); excess.resize(n); adj.resize(n); for (int i = 0; i < n; i++) cin >> brr[i]; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 1; i < n; i++) { long long x, k; cin >> x >> k; adj[i] = {x - 1, k}; } for (long long i = 0; i < n; i++) { need[i] = max(arr[i] - brr[i], 0LL); excess[i] = max(brr[i] - arr[i], 0LL); } for (long long i = n - 1; i >= 0; i--) { compute(i); } if (need[0] == 0 && ovr == false) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5, inf = 2e9; int n; long long a[N], b[N], val[N]; vector<pair<int, int>> g[N]; void dfs(int x, int k) { for (auto u : g[x]) { int to = u.first, nk = u.second; dfs(to, nk); if ((__int128)a[x] + val[to] <= (__int128)-1e18) { puts("NO"); exit(0); } a[x] += val[to]; } if (!x) return; if (b[x] > a[x]) { __int128 want = (__int128)(a[x] - b[x]) * k; if (want <= (__int128)-1e18) { puts("NO"); exit(0); } val[x] = want; } else { val[x] = a[x] - b[x]; } } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld", a + i); } for (int i = 0; i < n; ++i) { scanf("%lld", b + i); } for (int i = 0; i < n - 1; ++i) { int x, k; scanf("%d%d", &x, &k); x--; g[x].push_back({i + 1, k}); } dfs(0, 0); if (a[0] >= b[0]) puts("YES"); else puts("NO"); }
#include <bits/stdc++.h> using namespace std; const long long mx = 1e18; long long n, tot, e[500010], hd[500010], nt[500010], f[500010], val[500010], a[500010], b[500010]; void build(long long x, long long y, long long w) { tot++; e[tot] = y; val[tot] = w; nt[tot] = hd[x]; hd[x] = tot; } void dfs(long long x, long long fa) { long long i, now; f[x] = a[x]; for (i = hd[x]; i; i = nt[i]) { if (e[i] == fa) continue; dfs(e[i], x); if (f[e[i]] < 0) { f[e[i]] = -f[e[i]]; if (mx / val[i] < f[e[i]]) now = mx; else now = f[e[i]] * val[i]; f[x] -= now; f[x] = max(f[x], -mx); } else f[x] += f[e[i]]; } } int main() { long long i, x, y; scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld", &b[i]); } for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); a[i] = b[i] - a[i]; } for (i = 2; i <= n; i++) { scanf("%lld%lld", &x, &y); build(x, i, y); } dfs(1, 0); if (f[1] >= 0) puts("YES"); else puts("NO"); }
#include <bits/stdc++.h> using namespace std; class Scanner { private: static const long long BUFFER_SIZE = 10000; char buff[BUFFER_SIZE]; long long buffPos, buffLim; public: Scanner() { buffLim = fread(buff, 1, BUFFER_SIZE, stdin); buffPos = 0; } private: inline void flushBuff() { buffLim = fread(buff, 1, BUFFER_SIZE, stdin); if (buffLim == 0) { buff[buffLim++] = '\n'; } buffPos = 0; } inline bool isWS(char t) { return t == ' ' || t == '\n'; } inline bool isDig(char t) { return t >= '0' && t <= '9'; } void nextPos() { buffPos++; if (buffPos == buffLim) { flushBuff(); } } public: inline char getchar() { char ch = buff[buffPos]; nextPos(); return ch; } inline void next(char* s) { while (isWS(buff[buffPos])) { nextPos(); } while (!isWS(buff[buffPos])) { *s = buff[buffPos]; s++; nextPos(); } *s = '\0'; } inline void nextLine(char* s) { while (buff[buffPos] != '\n') { nextPos(); } if (buff[buffPos] == '\n') { nextPos(); } while (buff[buffPos] != '\n') { *s = buff[buffPos]; s++; buffPos++; } *s = '\0'; } inline long long nextInt() { while (!isDig(buff[buffPos])) { nextPos(); } long long sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1; long long res = 0; while (isDig(buff[buffPos])) { res = res * 10 + buff[buffPos] - '0'; nextPos(); } return res * sign; } inline double nextDouble() { while (isWS(buff[buffPos])) { nextPos(); } long long sign = (buff[buffPos] == '-') ? nextPos(), -1 : 1; double res = 0; while (isDig(buff[buffPos])) { res = res * 10 + buff[buffPos] - '0'; nextPos(); } if (buff[buffPos] == '.') { nextPos(); double ep = 1; while (isDig(buff[buffPos])) { ep *= 0.1; res += ep * (buff[buffPos] - '0'); nextPos(); } } return sign * res; } inline char nextChar() { while (isWS(buff[buffPos])) nextPos(); char res = buff[buffPos]; nextPos(); return res; } }; Scanner sc; template <class T> inline bool checkMin(T& a, T b) { return (a > b ? a = b, 1 : 0); } template <class T> inline bool checkMax(T& a, T b) { return (a < b ? a = b, 1 : 0); } void ALERT(bool judgememt, const char* phrase) { if (judgememt) { puts(phrase); throw "ALERT"; } } bool alert(bool judgememt, const char* phrase) { if (judgememt) puts(phrase); return judgememt; } void preInit(); void init(); void solve(); int32_t main() { preInit(); init(); solve(); return 0; } const long long N = 100005; const long long INF = 0x3f3f3f3f3f3f3f3f; long long b[N], t[N]; vector<long long> e[N]; long long n; void preInit() { n = sc.nextInt(); for (long long i = 1; i <= n; i++) { b[i] = sc.nextInt(); } for (long long i = 1; i <= n; i++) { b[i] -= sc.nextInt(); } for (long long i = 2; i <= n; i++) { e[sc.nextInt()].push_back(i); t[i] = sc.nextInt(); } } void init() {} void dfs(long long u, long long f) { for (long long v : e[u]) if (v != f) { dfs(v, u); if (b[v] > 0) { b[u] += b[v]; } else { if (b[v] < (-INF) / t[v]) { b[u] = -INF; } else { b[u] += b[v] * t[v]; } checkMax(b[u], -INF); } }; ; } void solve() { dfs(1, 0); puts(b[1] >= 0 ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5; const long long inf = 1e18; long long a[N]; long long bal[N]; long long zn[N]; int n; long long b[N]; long long limit; long long mul(long long &x, long long y) { if (-x >= inf / y + ((inf % y) != 0)) return -inf; else return x * y; } void add(long long &x, long long y) { x += y; if (x > inf) x = inf; if (x < -inf) x = -inf; } vector<int> ed[N]; void dfs(int x) { long long sum = a[x] - b[x]; for (int j = 0; j < ed[x].size(); j++) { int to = ed[x][j]; dfs(to); if (bal[to] < 0) add(sum, mul(bal[to], zn[to])); } for (int j = 0; j < ed[x].size(); j++) { int to = ed[x][j]; if (bal[to] > 0) sum += bal[to]; } bal[x] = sum; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 2; i <= n; i++) { int par, k; cin >> par >> k; ed[par].push_back(i); zn[i] = k; } dfs(1); if (bal[1] < 0) cout << "NO\n"; else cout << "YES\n"; return 0; }
#include <bits/stdc++.h> using namespace std; double req[100005], now[100005]; int x[100005], k[100005], n; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } inline long long readll() { long long x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10ll + c - '0', c = getchar(); return x; } int main() { int i; n = read(); for (i = 1; i <= n; i++) now[i] = readll(); for (i = 1; i <= n; i++) req[i] = readll(); for (i = 2; i <= n; i++) x[i] = read(), k[i] = read(); for (i = n; i >= 2; i--) { if (now[i] > req[i]) { now[x[i]] += (now[i] - req[i]); } else { now[x[i]] -= (req[i] - now[i]) * k[i]; } } if (now[1] + 0.000001 > req[1]) { puts("YES"); } else { puts("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const double INF = 1e18; struct Tree { int n; vector<vector<int>> g; vector<int> parent; vector<long long> target; vector<long long> current; vector<long long> rate; Tree(int n) : n(n) { g.assign(n, vector<int>()); parent.assign(n, -1); current.assign(n, 0); target.assign(n, 0); rate.assign(n, 0); } void addNodeCurrent(int i, long long v) { current[i] = v; } void addNodeTarget(int i, long long v) { target[i] = v; } void addEdge(int u, int v, int k) { g[u].push_back(v); g[v].push_back(u); rate[u] = k; } bool solve(int root) { vector<int> order; deque<int> queue; queue.push_back(root); parent[root] = -2; while (!queue.empty()) { int u = queue.front(); queue.pop_front(); order.push_back(u); for (int v : g[u]) { if (parent[v] == -1) { parent[v] = u; queue.push_back(v); } } } bool success = true; for (int i = order.size() - 1; i >= 1; i--) { int u = order[i]; int p = parent[u]; if (current[u] >= target[u]) { current[p] += (current[u] - target[u]); current[u] = target[u]; } else if (current[u] < target[u]) { if (INF / rate[u] > (target[u] - current[u])) { current[p] -= (target[u] - current[u]) * rate[u]; current[u] = target[u]; if (current[p] < -INF) { success = false; break; } } else { success = false; break; } } } return success && (current[0] >= target[0]); } }; int main() { int n; cin >> n; Tree tree(n); for (int i = 0; i < n; i++) { long long w; cin >> w; tree.addNodeCurrent(i, w); } for (int i = 0; i < n; i++) { long long w; cin >> w; tree.addNodeTarget(i, w); } for (int i = 1; i < n; i++) { int v, k; cin >> v >> k; v--; tree.addEdge(i, v, k); } bool ans = tree.solve(0); cout << (ans ? "YES" : "NO") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 15; const long long inf = -4e18; long long value[N]; vector<pair<int, long long> > adj[N]; long long convert(long long x, long long k) { if (x >= 0) return x; if (abs(x) <= abs(inf) / k) { return x * k; } else { return inf; } } long long add(long long x, long long y) { return max(x + y, inf); } long long dfs(int node) { long long &ret = value[node]; for (pair<int, long long> edge : adj[node]) { ret = add(ret, convert(dfs(edge.first), edge.second)); } return ret; } void solve() { int n; scanf("%d ", &n); for (int i = 1; i <= n; ++i) { scanf("%lld ", &value[i]); } for (int i = 1; i <= n; ++i) { long long a; scanf("%lld ", &a); value[i] -= a; } for (int i = 2; i <= n; ++i) { int p; long long w; scanf("%d %lld ", &p, &w); adj[p].push_back({i, w}); } if (dfs(1) >= 0) { printf("YES\n"); } else { printf("NO\n"); } } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 111111; const long long MAX = 10ll * 10000 * 10000 * 10000 * 10000; long long A[maxn]; int fir[maxn], nxt[maxn], to[maxn], D[maxn], tot; void add_edge(int x, int y, int z) { nxt[++tot] = fir[x], to[fir[x] = tot] = y, D[tot] = z; } int weishu(long long x) { int op = 0; long long uuu = 1; while (x >= uuu) ++op, uuu *= 10; return op; } long long dfs(int x) { long long uuu = A[x]; for (int i = fir[x], v = to[i]; i; i = nxt[i], v = to[i]) { long long op = dfs(v); if (op == -MAX) return -MAX; if (op >= 0) { uuu += op; if (uuu >= MAX) return -MAX; } else { if (weishu(-op) + weishu(D[i]) > 17) return -MAX; uuu += op * D[i]; if (uuu <= -MAX) return -MAX; } } return uuu; } int main() { int N; scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%I64d", A + i); for (int i = 1; i <= N; ++i) scanf("%I64d", A), A[i] -= A[0]; for (int i = 2; i <= N; ++i) { int a, b; scanf("%d%d", &a, &b); add_edge(a, i, b); } puts(dfs(1) < 0 ? "NO" : "YES"); return 0; }
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f; using namespace std; template <typename IO> inline void write(IO x) { if (x == 0) return (void)putchar('0'); if (x < 0) putchar('-'), x = -x; static char buf[30]; char *p = buf; while (x) { *(p++) = x % 10 + '0'; x /= 10; } while (p > buf) putchar(*(--p)); } inline void writestr(const char *s) { while (*s != 0) putchar(*(s++)); } template <typename IO> inline void writeln(IO x) { write(x), putchar('\n'); } template <typename IO> inline void writesp(IO x) { write(x), putchar(' '); } inline int readstr(char *s) { char *begin = s, c = getchar(); while (c < 33 || c > 127) { c = getchar(); } while (c >= 33 && c <= 127) { *(s++) = c; c = getchar(); } *s = 0; return s - begin; } template <typename IO> inline IO read() { IO x = 0; register bool w = 0; register char c = getchar(); while (c > '9' || c < '0') { if (c == '-') w = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return w ? -x : x; } template <> inline double read<double>() { double x = 0; int w = 0, y = 0; long long z = 1; register char c = getchar(); while (c > '9' || c < '0') { if (c == '-') w = 1; c = getchar(); } while (c >= '0' && c <= '9' || c == '.') { if (c == '.') { y = 1, c = getchar(); continue; } x = x * 10 + (c ^ 48); if (y) z *= 10; c = getchar(); } return (w ? -x : x) / z; } vector<long long> ve[100001], rat[100001]; long long a[100001]; inline void dfs(long long x) { for (register long long i = 0, siz = ve[x].size(), y; i < siz; i++) { dfs(y = ve[x][i]); if (a[y] >= 0) { a[x] += a[y]; } else { if (log10(-a[y] + 1) + log10(rat[x][i] + 1) > 18) writestr("NO\n"), exit(0); a[x] += a[y] * rat[x][i]; } if (a[x] < -1e18) writestr("NO\n"), exit(0); } } signed main() { register long long n = read<long long>(), x; for (register long long i = 1; i <= n; i++) a[i] = read<long long>(); for (register long long i = 1; i <= n; i++) a[i] = a[i] - read<long long>(); for (register long long i = 2; i <= n; i++) x = read<long long>(), ve[x].push_back(i), rat[x].push_back(read<long long>()); dfs(1), writestr(a[1] < 0 ? "NO\n" : "YES\n"); }
#include <bits/stdc++.h> using namespace std; const int NIL = -1; template <class T> inline T mx(T a, T b) { return a > b ? a : b; } template <class T> inline T mi(T a, T b) { return a < b ? a : b; } template <class T> inline void sw(T &a, T &b) { T t = a; a = b; b = t; } template <class T> inline T mabs(T x) { return x < 0 ? -x : x; } inline char gc() { char ret; while ((ret = getchar()) == ' ' || ret == '\n' || ret == '\t') ; return ret; } const int LIM = 1e5 + 10; int n; long long a[LIM], b[LIM]; vector<pair<int, long long> > adj[LIM]; const long long MX = 1e18; bool dfs(int crt, long long pre, int fa) { vector<pair<int, long long> > &t = adj[crt]; for (int i = 0; i < t.size(); i++) if (!dfs(t[i].first, t[i].second, crt)) return false; if (crt == 1) return a[crt] >= b[crt]; if (b[crt] > a[crt]) { long long dif = b[crt] - a[crt]; if (dif * pre < 0) return false; long long need = dif * pre; long long tmp = a[fa]; a[crt] += dif, a[fa] -= need; if (a[fa] > tmp) return false; return true; } else if (b[crt] < a[crt]) { long long dif = a[crt] - b[crt]; long long tmp = a[fa]; a[crt] -= dif, a[fa] += dif; if (a[fa] < tmp) return false; return true; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; int v; long long w; for (int i = 2; i <= n; i++) { cin >> v >> w; adj[v].push_back(make_pair(i, w)); } cout << (dfs(1, 0, -1) ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100010], b[100010], k[100010], c[100010]; int n, x[100010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]), c[i] = b[i] - a[i]; for (int i = 2; i <= n; i++) scanf("%d%I64d", &x[i], &k[i]); for (int i = n; i; i--) { if (c[i] < 0) { if (i == 1 || 100000000000000000 / k[i] <= -c[i] || c[x[i]] + k[i] * c[i] <= -100000000000000000) { printf("NO\n"); return 0; } c[x[i]] += k[i] * c[i]; c[i] = 0; } else if (c[i] > 0) { c[x[i]] += c[i]; c[i] = 0; } } printf("YES\n"); return 0; }