problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03158
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ll long long int #define vl vector<ll> #define vi vector<int> const int INF = 2147483647; const ll MOD = 1000000007; using namespace std; int main() { int n, q; cin >> n >> q; vl a(n), x(n); REP(i, n) { cin >> a[i]; } REP(i, q) { cin >> x[i]; } vl cum(n + 1), cumtobi(n + 1); cum[0] = 0; cumtobi[0] = 0; cum[1] = a[0]; cumtobi[1] = a[0]; FOR(i, 2, n + 1) { cum[i] = cum[i - 1] + a[i - 1]; cumtobi[i] = cumtobi[i - 2] + a[i - 1]; } vector<pair<ll, ll>> map; for (int i = 1; i <= (n + 1) / 2; i++) { if (n - 2 * i - 1 < 0) { ll tx = 0; ll v = cum[n] - cum[n / 2]; map.push_back(make_pair(tx, v)); } else { ll tx = (a[n - i - 1] + a[n - 2 * i - 1]) / 2; ll v = cum[n] - cum[n - i] + cumtobi[n - 2 * i]; map.push_back(make_pair(tx, v)); } } sort(map.begin(), map.end()); REP(i, q) { int idx = (upper_bound(map.begin(), map.end(), make_pair(x[i], 0LL)) - map.begin()) - 1; cout << map[idx].second << endl; } }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ll long long int #define vl vector<ll> #define vi vector<int> const int INF = 2147483647; const ll MOD = 1000000007; using namespace std; int main() { int n, q; cin >> n >> q; vl a(n), x(q); REP(i, n) { cin >> a[i]; } REP(i, q) { cin >> x[i]; } vl cum(n + 1), cumtobi(n + 1); cum[0] = 0; cumtobi[0] = 0; cum[1] = a[0]; cumtobi[1] = a[0]; FOR(i, 2, n + 1) { cum[i] = cum[i - 1] + a[i - 1]; cumtobi[i] = cumtobi[i - 2] + a[i - 1]; } vector<pair<ll, ll>> map; for (int i = 1; i <= (n + 1) / 2; i++) { if (n - 2 * i - 1 < 0) { ll tx = 0; ll v = cum[n] - cum[n / 2]; map.push_back(make_pair(tx, v)); } else { ll tx = (a[n - i - 1] + a[n - 2 * i - 1]) / 2; ll v = cum[n] - cum[n - i] + cumtobi[n - 2 * i]; map.push_back(make_pair(tx, v)); } } sort(map.begin(), map.end()); REP(i, q) { int idx = (upper_bound(map.begin(), map.end(), make_pair(x[i], 0LL)) - map.begin()) - 1; cout << map[idx].second << endl; } }
replace
14
15
14
15
0
p03158
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> #define int long long int #define rep(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using namespace std; typedef pair<int, int> P; const int INF = 1e15; const int MOD = 1e9 + 7; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> s(n + 1); rep(i, n) s[i + 1] = s[i] + a[i]; vector<int> ss((n + 1) / 2 + 1); int x = (n + 1) % 2; rep(i, (n + 1) / 2) { ss[i + 1] = ss[i] + a[x + 2 * i]; } vector<P> query(q); rep(i, q) { int x; cin >> x; query[i] = {x, i}; } sort(ALL(query)); vector<int> ans(n); int y = 1; rep(i, q) { int x = query[i].first; while (y < n && a[y - 1] < x) { y++; } while (y < n) { int limit = max(1LL, 2 * x - a[y - 1]); int lo = -1; int hi = n - 1; while (hi - lo > 1) { int mid = (lo + hi) / 2; if (a[mid] >= limit) { hi = mid; } else { lo = mid; } } int upper = n - y + 1; int lower = y - hi; if (upper > lower) { y++; } else { break; } } int result = s[n] - s[y - 1]; int remain = n - (n - y + 1) * 2; if (remain > 0) { result += ss[(remain + 1) / 2]; } ans[query[i].second] = result; } rep(i, q) { cout << ans[i] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <utility> #include <vector> #define int long long int #define rep(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using namespace std; typedef pair<int, int> P; const int INF = 1e15; const int MOD = 1e9 + 7; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> a(n); rep(i, n) cin >> a[i]; vector<int> s(n + 1); rep(i, n) s[i + 1] = s[i] + a[i]; vector<int> ss((n + 1) / 2 + 1); int x = (n + 1) % 2; rep(i, (n + 1) / 2) { ss[i + 1] = ss[i] + a[x + 2 * i]; } vector<P> query(q); rep(i, q) { int x; cin >> x; query[i] = {x, i}; } sort(ALL(query)); vector<int> ans(q); int y = 1; rep(i, q) { int x = query[i].first; while (y < n && a[y - 1] < x) { y++; } while (y < n) { int limit = max(1LL, 2 * x - a[y - 1]); int lo = -1; int hi = n - 1; while (hi - lo > 1) { int mid = (lo + hi) / 2; if (a[mid] >= limit) { hi = mid; } else { lo = mid; } } int upper = n - y + 1; int lower = y - hi; if (upper > lower) { y++; } else { break; } } int result = s[n] - s[y - 1]; int remain = n - (n - y + 1) * 2; if (remain > 0) { result += ss[(remain + 1) / 2]; } ans[query[i].second] = result; } rep(i, q) { cout << ans[i] << endl; } return 0; }
replace
51
52
51
52
0
p03158
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= (int)(m); --(i)) #define ALL(x) begin(x), end(x) #define dump(x) cerr << #x " = " << x << endl #define unittest_name_helper(counter) unittest_##counter #define unittest_name(counter) unittest_name_helper(counter) #define unittest __attribute__((constructor)) void unittest_name(__COUNTER__)() using ll = long long; using namespace std; template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; template <class T, class U> inline void chmax(T &a, U const &b) { a = max<T>(a, b); } template <class T, class U> inline void chmin(T &a, U const &b) { a = min<T>(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) { REP(i, int(xs.size()) - 1) out << xs[i] << ' '; if (not xs.empty()) out << xs.back(); return out; } template <typename UnaryPredicate> int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) { assert(l <= r); --l; while (r - l > 1) { int64_t m = l + (r - l) / 2; (p(m) ? r : l) = m; } return r; } vector<ll> solve(int n, int q, vector<ll> const &a, vector<ll> const &x) { vector<ll> acc(n + 1); partial_sum(ALL(a), acc.begin() + 1); vector<ll> acc_even(n + 1); REP(i, n) { acc_even[i + 1] = acc_even[i] + (i % 2 == 0 ? a[i] : 0); } vector<int> order(q); iota(ALL(order), 0); sort(ALL(order), [&](int i, int j) { return a[i] > a[j]; }); auto get_l_r = [&](int k) { int l = max(0, n - 2 * k); int r = n - k; return make_pair(l, r); }; auto pred = [&](ll x, int k) { int l, r; tie(l, r) = get_l_r(k); ll d = max(abs(a[l] - x), abs(a[r - 1] - x)); if (l - 1 >= 0 and abs(a[l - 1] - x) <= d) return false; // if (r < n and abs(a[r] - x) < d) return false; return true; }; vector<ll> answer(q); int k = 1; // dump(n); // dump(a); for (int i : order) { while (k < (n + 1) / 2 and not pred(x[i], k)) ++k; int l, r; tie(l, r) = get_l_r(k); answer[i] = (acc[n] - acc[r]) + (n % 2 == 0 ? acc[l] - acc_even[l] : acc_even[l]); // cerr << endl; // dump(i); // dump(x[i]); // dump(k); // dump(l); // dump(r); } return answer; } int main() { int n, q; cin >> n >> q; vector<ll> a(n); REP(i, n) cin >> a[i]; vector<ll> x(q); REP(i, q) cin >> x[i]; for (ll y_i : solve(n, q, a, x)) { cout << y_i << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i)) #define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; --(i)) #define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= (int)(m); --(i)) #define ALL(x) begin(x), end(x) #define dump(x) cerr << #x " = " << x << endl #define unittest_name_helper(counter) unittest_##counter #define unittest_name(counter) unittest_name_helper(counter) #define unittest __attribute__((constructor)) void unittest_name(__COUNTER__)() using ll = long long; using namespace std; template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>; template <class T, class U> inline void chmax(T &a, U const &b) { a = max<T>(a, b); } template <class T, class U> inline void chmin(T &a, U const &b) { a = min<T>(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) { REP(i, int(xs.size()) - 1) out << xs[i] << ' '; if (not xs.empty()) out << xs.back(); return out; } template <typename UnaryPredicate> int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) { assert(l <= r); --l; while (r - l > 1) { int64_t m = l + (r - l) / 2; (p(m) ? r : l) = m; } return r; } vector<ll> solve(int n, int q, vector<ll> const &a, vector<ll> const &x) { vector<ll> acc(n + 1); partial_sum(ALL(a), acc.begin() + 1); vector<ll> acc_even(n + 1); REP(i, n) { acc_even[i + 1] = acc_even[i] + (i % 2 == 0 ? a[i] : 0); } vector<int> order(q); iota(ALL(order), 0); sort(ALL(order), [&](int i, int j) { return x[i] > x[j]; }); auto get_l_r = [&](int k) { int l = max(0, n - 2 * k); int r = n - k; return make_pair(l, r); }; auto pred = [&](ll x, int k) { int l, r; tie(l, r) = get_l_r(k); ll d = max(abs(a[l] - x), abs(a[r - 1] - x)); if (l - 1 >= 0 and abs(a[l - 1] - x) <= d) return false; // if (r < n and abs(a[r] - x) < d) return false; return true; }; vector<ll> answer(q); int k = 1; // dump(n); // dump(a); for (int i : order) { while (k < (n + 1) / 2 and not pred(x[i], k)) ++k; int l, r; tie(l, r) = get_l_r(k); answer[i] = (acc[n] - acc[r]) + (n % 2 == 0 ? acc[l] - acc_even[l] : acc_even[l]); // cerr << endl; // dump(i); // dump(x[i]); // dump(k); // dump(l); // dump(r); } return answer; } int main() { int n, q; cin >> n >> q; vector<ll> a(n); REP(i, n) cin >> a[i]; vector<ll> x(q); REP(i, q) cin >> x[i]; for (ll y_i : solve(n, q, a, x)) { cout << y_i << endl; } return 0; }
replace
54
55
54
55
0
p03158
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 998244353 template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x += y; return x < MOD ? x : x - MOD; } template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x += y; if (x >= MOD) x -= MOD; } template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x -= y; return x < 0 ? x + MOD : x; } template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x -= y; if (x < 0) x += MOD; } template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; return 1ll * x * y % MOD; } template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x = 1ll * x * y % MOD; } long long int gcd(long long int a, long long int b) { if (a > b) { swap(a, b); } while (a) { swap(a, b); a %= b; } return b; } long long int lcm(long long int a, long long int b) { return a / gcd(a, b) * b; } long long int ppow(long long int i, long long int j) { long long int res = 1LL; while (j) { if ((j & 1LL)) { res *= i; if (res >= MOD) { res %= MOD; } } j >>= 1; i *= i; if (i >= MOD) { i %= MOD; } } return res; } class Combination { public: vector<long long int> k; vector<long long int> r; void resize(int N) { k.resize(N + 2); r.resize(N + 2); k[0] = 1; for (int i = 1; i < N + 2; i++) { k[i] = k[i - 1]; k[i] *= i; if (k[i] >= MOD) k[i] %= MOD; } long long int al = k[k.size() - 1]; long long int iv = ppow(k[k.size() - 1], MOD - 2); r[k.size() - 1] = iv; for (int i = (int)(r.size()) - 2; i >= 0; i--) { r[i] = r[i + 1] * (i + 1); if (r[i] >= MOD) { r[i] %= MOD; } } } long long int C(int a, int b) { if (a < b) return 0; long long int up = k[a]; long long int dw = r[b] * r[a - b]; dw %= MOD; up *= dw; up %= MOD; return up; } long long int H(int a, int b) { return C(a + b - 1, b); } long long int catalan_number(int n) { return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD; } }; Combination C; #define MAX 100002 int n; int q; vector<int> v; long long int im[MAX][2]; inline int rng(int x, int mint) { int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) - v.begin(); int rig = mint; return rig - lef + 1; } long long int sum(int lef, int rig) { if (lef > rig) return 0; long long int ret = im[rig][0] + im[rig][1]; if (lef) { ret -= im[lef - 1][0] + im[lef - 1][1]; } return ret; } long long int skip(int lef) { if (lef < 0) return 0; return im[lef][lef & 1]; } int main() { cin >> n >> q; long long int overall = 0; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); v.push_back(a); overall += a; im[i][i & 1] += a; if (i) { im[i][0] += im[i - 1][0]; im[i][1] += im[i - 1][1]; } } for (int i = 0; i < q; i++) { int x; scanf("%d", &x); if (v.back() <= x) { printf("%lld\n", skip(n - 1)); } else { int id = lower_bound(v.begin(), v.end(), x) - v.begin(); int mint = id; int maxt = v.size() - 1; while (mint + 1 < maxt) { int mid = (mint + maxt) / 2; if (rng(x, mid) <= v.size() - mid - 1) { mint = mid; } else { maxt = mid; } } int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) - v.begin(); int rig = mint; if (rng(x, mint) <= v.size() - mint - 1) { } else { rig = id - 1; mint = id - 1; lef = id - (v.size() - id); lef = max(0, lef); } long long int ao = sum(lef, rig); // cerr<<"sum "<<sum(lef,rig)<<endl; int ao_turn = rig - lef + 1; int tk_turn = v.size() - mint - 1; // cerr<<lef<<" "<<rig<<" "<<ao_turn<<" "<<tk_turn<<endl; if (ao_turn == tk_turn) { ao += skip(lef - 2); } else { assert(ao_turn + 1 == tk_turn); ao += skip(lef - 1); // cerr<<"skip"<<" "<<skip(lef-1)<<endl; } printf("%lld\n", overall - ao); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 998244353 template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x += y; return x < MOD ? x : x - MOD; } template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x += y; if (x >= MOD) x -= MOD; } template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x -= y; return x < 0 ? x + MOD : x; } template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x -= y; if (x < 0) x += MOD; } template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; return 1ll * x * y % MOD; } template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) { if (y >= MOD) y %= MOD; if (x >= MOD) x %= MOD; x = 1ll * x * y % MOD; } long long int gcd(long long int a, long long int b) { if (a > b) { swap(a, b); } while (a) { swap(a, b); a %= b; } return b; } long long int lcm(long long int a, long long int b) { return a / gcd(a, b) * b; } long long int ppow(long long int i, long long int j) { long long int res = 1LL; while (j) { if ((j & 1LL)) { res *= i; if (res >= MOD) { res %= MOD; } } j >>= 1; i *= i; if (i >= MOD) { i %= MOD; } } return res; } class Combination { public: vector<long long int> k; vector<long long int> r; void resize(int N) { k.resize(N + 2); r.resize(N + 2); k[0] = 1; for (int i = 1; i < N + 2; i++) { k[i] = k[i - 1]; k[i] *= i; if (k[i] >= MOD) k[i] %= MOD; } long long int al = k[k.size() - 1]; long long int iv = ppow(k[k.size() - 1], MOD - 2); r[k.size() - 1] = iv; for (int i = (int)(r.size()) - 2; i >= 0; i--) { r[i] = r[i + 1] * (i + 1); if (r[i] >= MOD) { r[i] %= MOD; } } } long long int C(int a, int b) { if (a < b) return 0; long long int up = k[a]; long long int dw = r[b] * r[a - b]; dw %= MOD; up *= dw; up %= MOD; return up; } long long int H(int a, int b) { return C(a + b - 1, b); } long long int catalan_number(int n) { return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD; } }; Combination C; #define MAX 100002 int n; int q; vector<int> v; long long int im[MAX][2]; inline int rng(int x, int mint) { int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) - v.begin(); int rig = mint; return rig - lef + 1; } long long int sum(int lef, int rig) { if (lef > rig) return 0; long long int ret = im[rig][0] + im[rig][1]; if (lef) { ret -= im[lef - 1][0] + im[lef - 1][1]; } return ret; } long long int skip(int lef) { if (lef < 0) return 0; return im[lef][lef & 1]; } int main() { cin >> n >> q; long long int overall = 0; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); v.push_back(a); overall += a; im[i][i & 1] += a; if (i) { im[i][0] += im[i - 1][0]; im[i][1] += im[i - 1][1]; } } for (int i = 0; i < q; i++) { int x; scanf("%d", &x); if (v.back() <= x) { printf("%lld\n", skip(n - 1)); } else { int id = lower_bound(v.begin(), v.end(), x) - v.begin(); int mint = id; int maxt = v.size() - 1; while (mint + 1 < maxt) { int mid = (mint + maxt) / 2; if (rng(x, mid) <= v.size() - mid - 1) { mint = mid; } else { maxt = mid; } } int lef = lower_bound(v.begin(), v.end(), (long long int)x - (v[mint] - x)) - v.begin(); int rig = mint; if (rng(x, mint) <= v.size() - mint - 1) { } else { rig = id - 1; mint = id - 1; lef = id - (v.size() - id); lef = max(0, lef); } long long int ao = sum(lef, rig); // cerr<<"sum "<<sum(lef,rig)<<endl; int ao_turn = rig - lef + 1; int tk_turn = v.size() - mint - 1; if (ao_turn + 1 < tk_turn) { lef -= (tk_turn - ao_turn - 1); ao = sum(lef, rig); ao_turn = rig - lef + 1; } cerr << lef << " " << rig << " " << ao_turn << " " << tk_turn << endl; if (ao_turn == tk_turn) { ao += skip(lef - 2); } else { assert(ao_turn + 1 == tk_turn); ao += skip(lef - 1); // cerr<<"skip"<<" "<<skip(lef-1)<<endl; } printf("%lld\n", overall - ao); } } return 0; }
replace
198
199
198
204
0
p03158
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define DEBUGP(val) cerr << #val << "=" << val << "\n" using namespace std; typedef long long int ll; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PI; const ll mod = 1e9 + 7; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; VL a(n); REP(i, 0, n) cin >> a[i]; REP(i, 0, n) a[i] = 10 * a[i] + 1; VL x(q); REP(i, 0, q) cin >> x[i]; REP(i, 0, q) x[i] *= 10; VL acc(n + 1); VL acc2(n + 1); REP(i, 0, n) acc[i + 1] = acc[i] + a[i] / 10; acc2[1] = a[0] / 10; REP(i, 1, n) acc2[i + 1] = acc2[i - 1] + a[i] / 10; REP(i, 0, q) { ll fail = 1e9; ll pass = -1; while (fail - pass > 1) { ll mid = (pass + fail) / 2; int idx0 = lower_bound(a.begin(), a.end(), x[i] - mid) - a.begin(); int idx1 = upper_bound(a.begin(), a.end(), x[i] + mid) - a.begin(); if (n - idx1 >= idx1 - idx0) pass = mid; else fail = mid; } int idx0 = lower_bound(a.begin(), a.end(), x[i] - pass) - a.begin(); int idx1 = upper_bound(a.begin(), a.end(), x[i] + pass) - a.begin(); ll tot = 0; assert((n - idx1) - (idx1 - idx0) >= 0); assert((n - idx1) - (idx1 - idx0) <= 1); if (n - idx1 == idx1 - idx0) { tot += acc[n] - acc[idx1] + acc2[idx0]; } else { tot += acc[n] - acc[idx1] + (idx0 >= 1 ? acc2[idx0 - 1] : 0); } cout << tot << "\n"; } }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define DEBUGP(val) cerr << #val << "=" << val << "\n" using namespace std; typedef long long int ll; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PI; const ll mod = 1e9 + 7; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; VL a(n); REP(i, 0, n) cin >> a[i]; REP(i, 0, n) a[i] = 10 * a[i] + 1; VL x(q); REP(i, 0, q) cin >> x[i]; REP(i, 0, q) x[i] *= 10; VL acc(n + 1); VL acc2(n + 1); REP(i, 0, n) acc[i + 1] = acc[i] + a[i] / 10; acc2[1] = a[0] / 10; REP(i, 1, n) acc2[i + 1] = acc2[i - 1] + a[i] / 10; REP(i, 0, q) { ll fail = 1e11; ll pass = -1; while (fail - pass > 1) { ll mid = (pass + fail) / 2; int idx0 = lower_bound(a.begin(), a.end(), x[i] - mid) - a.begin(); int idx1 = upper_bound(a.begin(), a.end(), x[i] + mid) - a.begin(); if (n - idx1 >= idx1 - idx0) pass = mid; else fail = mid; } int idx0 = lower_bound(a.begin(), a.end(), x[i] - pass) - a.begin(); int idx1 = upper_bound(a.begin(), a.end(), x[i] + pass) - a.begin(); ll tot = 0; assert((n - idx1) - (idx1 - idx0) >= 0); assert((n - idx1) - (idx1 - idx0) <= 1); if (n - idx1 == idx1 - idx0) { tot += acc[n] - acc[idx1] + acc2[idx0]; } else { tot += acc[n] - acc[idx1] + (idx0 >= 1 ? acc2[idx0 - 1] : 0); } cout << tot << "\n"; } }
replace
48
49
48
49
0
p03158
C++
Runtime Error
// #define DEBUGGING // Enables DEBUG macro. #include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; #define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i) #ifndef DEBUGGING #define DEBUG(...) #else template <typename T> void _debug(T value) { std::cerr << value; } template <typename T, typename... Ts> void _debug(T value, Ts... args) { std::cerr << value << ", "; _debug(args...); } #define DEBUG(...) \ do { \ cerr << " (L" << __LINE__ << ") "; \ cerr << #__VA_ARGS__ << ": "; \ _debug(__VA_ARGS__); \ cerr << endl; \ } while (0) #endif int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<i64> A(N); // sorted for (auto &x : A) cin >> x; // rcum[i] = sum of A[i] and its right, inclusive. vector<i64> rcum(N + 1); REP(i, N) { rcum[N - 1 - i] = rcum[N - i] + A[N - 1 - i]; } // lhcum[i] = sum of A[i-2] and its left. vector<i64> lhcum(N + 2); for (int i = 2; i < N + 2; ++i) { lhcum[i] = lhcum[i - 2] + A[i - 2]; } auto solve = [&](const i64 X) -> i64 { auto closest_it = lower_bound(A.begin(), A.end(), X); if (closest_it == A.end()) { return lhcum[N + 1]; } int closest_i = closest_it - A.begin(); if (closest_i > 0 && A[closest_i] - X >= X - A[closest_i - 1]) { --closest_i; --closest_it; } if (closest_i == N - 1) { return lhcum[N + 1]; } DEBUG(closest_i, A[closest_i]); int ok = closest_i, ng = N - 1; while (ng - ok > 1) { int m = (ng + ok) / 2; i64 delta = abs(A[m] - X); int lb = lower_bound(A.begin(), A.end(), X - delta) - A.begin(); bool b = m - lb + 1 > N - 1 - m; DEBUG(m, A[m], lb, A[lb], b); if (m - lb + 1 > N - 1 - m) { // m is too big ng = m; } else { ok = m; } } int olb = lower_bound(A.begin(), A.end(), X - abs(A[ok] - X)) - A.begin(); DEBUG(ok, ng, olb, rcum[ng], lhcum[olb + 1]); if (ng - olb == N - ng) { return rcum[ng] + lhcum[olb + 1]; } else { assert(ng - olb == N - ng - 1); return rcum[ng] + lhcum[olb]; } }; REP(i, Q) { i64 X; DEBUG(i); cin >> X; cout << solve(X) << '\n'; } }
// #define DEBUGGING // Enables DEBUG macro. #include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; #define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i) #ifndef DEBUGGING #define DEBUG(...) #else template <typename T> void _debug(T value) { std::cerr << value; } template <typename T, typename... Ts> void _debug(T value, Ts... args) { std::cerr << value << ", "; _debug(args...); } #define DEBUG(...) \ do { \ cerr << " (L" << __LINE__ << ") "; \ cerr << #__VA_ARGS__ << ": "; \ _debug(__VA_ARGS__); \ cerr << endl; \ } while (0) #endif int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<i64> A(N); // sorted for (auto &x : A) cin >> x; // rcum[i] = sum of A[i] and its right, inclusive. vector<i64> rcum(N + 1); REP(i, N) { rcum[N - 1 - i] = rcum[N - i] + A[N - 1 - i]; } // lhcum[i] = sum of A[i-2] and its left. vector<i64> lhcum(N + 2); for (int i = 2; i < N + 2; ++i) { lhcum[i] = lhcum[i - 2] + A[i - 2]; } auto solve = [&](const i64 X) -> i64 { auto closest_it = lower_bound(A.begin(), A.end(), X); if (closest_it == A.end()) { return lhcum[N + 1]; } int closest_i = closest_it - A.begin(); if (closest_i > 0 && A[closest_i] - X >= X - A[closest_i - 1]) { --closest_i; --closest_it; } if (closest_i == N - 1) { return lhcum[N + 1]; } DEBUG(closest_i, A[closest_i]); int ok = closest_i, ng = N - 1; while (ng - ok > 1) { int m = (ng + ok) / 2; i64 delta = abs(A[m] - X); int lb = lower_bound(A.begin(), A.end(), X - delta) - A.begin(); bool b = m - lb + 1 > N - 1 - m; DEBUG(m, A[m], lb, A[lb], b); if (m - lb + 1 > N - 1 - m) { // m is too big ng = m; } else { ok = m; } } return rcum[ng] + lhcum[ok - (N - ng) + 2]; // int olb = lower_bound(A.begin(), A.end(), X - abs(A[ok] - X)) - // A.begin(); DEBUG(ok, ng, olb, rcum[ng], lhcum[olb + 1]); if (ng - olb == // N - ng) { // return rcum[ng] + lhcum[olb + 1]; // } else { // N-ng - // (N-ng) - (ng-olb) // assert(ng - olb == N - ng - 1); // return rcum[ng] + lhcum[olb]; // } }; REP(i, Q) { i64 X; DEBUG(i); cin >> X; cout << solve(X) << '\n'; } }
replace
72
80
72
84
0
p03158
C++
Runtime Error
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < ll(n); ++i) #define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < ll(n); ++i) #define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vl v(n); REP(i, n) cin >> v[i]; vl e(n + 1, 0), o(n + 1, 0); REP(i, n) { e[i + 1] += e[i]; o[i + 1] += o[i]; if (i & 1) o[i + 1] += v[i]; else e[i + 1] += v[i]; } vl p(n + 1, 0); RREP(i, n) p[i] = p[i + 1] + v[i]; REP(aa, q) { int x; cin >> x; if (x >= v.back()) { if (n & 1) cout << e[n] << endl; else cout << o[n] << endl; continue; } if (x <= v.front()) { cout << p[n / 2] << endl; continue; } int l = 0, r = INF; while (r - l > 1) { int mid = (r + l) / 2; int a = lower_bound(ALL(v), x - mid) - v.begin(); int b1 = lower_bound(ALL(v), x + mid) - v.begin(); int b2 = upper_bound(ALL(v), x + mid) - v.begin(); if (n - b1 - (b1 - a) > 1) { if (n - b2 - (b2 - a) > 1) { l = mid; } else r = mid; } else { r = mid; } } int a = lower_bound(ALL(v), x - r) - v.begin(); int b1 = lower_bound(ALL(v), x + r) - v.begin(); int b2 = upper_bound(ALL(v), x + r) - v.begin(); if (n - b1 - (b1 - a) > 1) { if (n - b2 - (b2 - a) == 1) { ll ans = p[b2]; if (a == 0) { cout << ans << endl; } else { a--; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { // assert(n - b2 - (b2 - a) == 0); ll ans = p[b2]; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { if (n - b1 - (b1 - a) == 1) { ll ans = p[b1]; if (a == 0) { cout << ans << endl; } else { a--; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { assert(n - b1 - (b1 - a) == 0); ll ans = p[b1]; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } } }
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < ll(n); ++i) #define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < ll(n); ++i) #define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; vl v(n); REP(i, n) cin >> v[i]; vl e(n + 1, 0), o(n + 1, 0); REP(i, n) { e[i + 1] += e[i]; o[i + 1] += o[i]; if (i & 1) o[i + 1] += v[i]; else e[i + 1] += v[i]; } vl p(n + 1, 0); RREP(i, n) p[i] = p[i + 1] + v[i]; REP(aa, q) { int x; cin >> x; if (x >= v.back()) { if (n & 1) cout << e[n] << endl; else cout << o[n] << endl; continue; } if (x <= v.front()) { cout << p[n / 2] << endl; continue; } int l = -1, r = INF; while (r - l > 1) { int mid = (r + l) / 2; int a = lower_bound(ALL(v), x - mid) - v.begin(); int b1 = lower_bound(ALL(v), x + mid) - v.begin(); int b2 = upper_bound(ALL(v), x + mid) - v.begin(); if (n - b1 - (b1 - a) > 1) { if (n - b2 - (b2 - a) > 1) { l = mid; } else r = mid; } else { r = mid; } } int a = lower_bound(ALL(v), x - r) - v.begin(); int b1 = lower_bound(ALL(v), x + r) - v.begin(); int b2 = upper_bound(ALL(v), x + r) - v.begin(); if (n - b1 - (b1 - a) > 1) { if (n - b2 - (b2 - a) == 1) { ll ans = p[b2]; if (a == 0) { cout << ans << endl; } else { a--; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { // assert(n - b2 - (b2 - a) == 0); ll ans = p[b2]; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { if (n - b1 - (b1 - a) == 1) { ll ans = p[b1]; if (a == 0) { cout << ans << endl; } else { a--; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } else { assert(n - b1 - (b1 - a) == 0); ll ans = p[b1]; if (a & 1) ans += e[a]; else ans += o[a]; cout << ans << endl; } } } }
replace
72
73
72
73
0
p03158
C++
Runtime Error
#include <bits/stdc++.h> #define fo(i, a, b) for (int i = (a); i <= (b); ++i) #define fd(i, a, b) for (int i = (a); i >= (b); --i) #define edge(i, u) \ for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v) #define mod 1000000007 #define ll long long #define N 405 #define pb push_back ll n, q, x, a[N], s[N], sum[N], l, r, ss; int main() { scanf("%lld %lld", &n, &q); fo(i, 1, n) scanf("%lld", &a[i]); s[1] = a[1]; s[2] = a[2]; sum[1] = a[1]; sum[2] = a[1] + a[2]; fo(i, 3, n) { s[i] = s[i - 2] + a[i]; sum[i] = sum[i - 1] + a[i]; } ss = sum[n]; fo(i, 1, q) { scanf("%lld\n", &x); if (x <= a[1]) { printf("%lld\n", ss - sum[n >> 1]); continue; } l = x; r = 1e9; ll rp, rpos, ans, lp, lpos; while (l < r) { rp = l + r >> 1; rpos = std::upper_bound(a + 1, a + n, rp) - a - 1; lp = x * 2 - rp; lpos = std::lower_bound(a + 1, a + n, lp) - a; if (n - rpos > rpos - lpos + 1) l = rp + 1; else r = rp; } rp = l; rpos = std::upper_bound(a + 1, a + n, rp) - a - 1; lp = x * 2 - rp; lpos = std::lower_bound(a + 1, a + n, lp) - a; if (n - rpos < rpos - lpos + 1) --rpos; ans = sum[rpos] - sum[lpos - 1]; if (n - rpos == rpos - lpos + 1) { if (lpos > 2) ans += s[lpos - 2]; } else { ans += s[lpos - 1]; } printf("%lld\n", ss - ans); } return 0; }
#include <bits/stdc++.h> #define fo(i, a, b) for (int i = (a); i <= (b); ++i) #define fd(i, a, b) for (int i = (a); i >= (b); --i) #define edge(i, u) \ for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v) #define mod 1000000007 #define ll long long #define N 100005 #define pb push_back ll n, q, x, a[N], s[N], sum[N], l, r, ss; int main() { scanf("%lld %lld", &n, &q); fo(i, 1, n) scanf("%lld", &a[i]); s[1] = a[1]; s[2] = a[2]; sum[1] = a[1]; sum[2] = a[1] + a[2]; fo(i, 3, n) { s[i] = s[i - 2] + a[i]; sum[i] = sum[i - 1] + a[i]; } ss = sum[n]; fo(i, 1, q) { scanf("%lld\n", &x); if (x <= a[1]) { printf("%lld\n", ss - sum[n >> 1]); continue; } l = x; r = 1e9; ll rp, rpos, ans, lp, lpos; while (l < r) { rp = l + r >> 1; rpos = std::upper_bound(a + 1, a + n, rp) - a - 1; lp = x * 2 - rp; lpos = std::lower_bound(a + 1, a + n, lp) - a; if (n - rpos > rpos - lpos + 1) l = rp + 1; else r = rp; } rp = l; rpos = std::upper_bound(a + 1, a + n, rp) - a - 1; lp = x * 2 - rp; lpos = std::lower_bound(a + 1, a + n, lp) - a; if (n - rpos < rpos - lpos + 1) --rpos; ans = sum[rpos] - sum[lpos - 1]; if (n - rpos == rpos - lpos + 1) { if (lpos > 2) ans += s[lpos - 2]; } else { ans += s[lpos - 1]; } printf("%lld\n", ss - ans); } return 0; }
replace
7
8
7
8
0
p03159
C++
Runtime Error
// https://atcoder.jp/contests/aising2019/tasks/aising2019_e #include <bits/stdc++.h> using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if (1) #else #define CIN_ONLY if (1) struct cww { cww() { CIN_ONLY { ios::sync_with_stdio(false); cin.tie(0); } } } star; #define DEBUG if (0) #endif #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &it : v) is >> it; return is; } class range { private: struct I { int x; int operator*() { return x; } bool operator!=(I &lhs) { return x < lhs.x; } void operator++() { ++x; } }; I i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} I &begin() { return i; } I &end() { return n; } }; int N; LL A[5123]; vector<int> g[5123]; vector<int> t[5123]; LL allB[5123][5123]; LL dp[5123][5123]; LL tmp[5123]; int sub[5123]; void dfs(int v, int p) { sub[v] = 1; for (int u : g[v]) { if (u == p) continue; t[v].push_back(u); dfs(u, v); sub[v] += sub[u]; } } void solve(int v) { for (int u : t[v]) { solve(u); } int x = 1; if (A[v] > 0) { allB[v][0] = 1; } else { allB[v][0] = 0; } dp[v][0] = A[v]; for (int u : t[v]) { const int nx = x + sub[u]; for (int i : range(nx + 1)) { tmp[i] = 0; } for (int i : range(x)) for (int j : range(sub[u] + 1)) { tmp[i + j] |= (allB[v][i] & allB[u][j]); } for (int i : range(nx + 1)) { allB[v][i] = tmp[i]; tmp[i] = 1e18; } for (int i : range(x)) for (int j : range(sub[u] + 1)) { chmin(tmp[i + j], dp[v][i] + dp[u][j]); } for (int i : range(nx + 1)) { dp[v][i] = tmp[i]; } x += nx; } for (int i : range(x)) { if (allB[v][i] || dp[v][i] < 0) { chmin(dp[v][i + 1], 0ll); allB[v][i + 1] = 1; } } } int main() { cin >> N; for (int i : range(N)) { cin >> A[i + 1]; } for (int i : range(N - 1)) { int u, v; cin >> u >> v; g[v].push_back(u); g[u].push_back(v); } dfs(1, 1); solve(1); int ret = N - 1; for (int i : range(N + 1)) { if (allB[1][i]) chmin(ret, i); if (dp[1][i] < 0) chmin(ret, i); } cout << ret << endl; return 0; }
// https://atcoder.jp/contests/aising2019/tasks/aising2019_e #include <bits/stdc++.h> using namespace std; typedef long long LL; #ifdef BTK #define DEBUG if (1) #else #define CIN_ONLY if (1) struct cww { cww() { CIN_ONLY { ios::sync_with_stdio(false); cin.tie(0); } } } star; #define DEBUG if (0) #endif #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &it : v) is >> it; return is; } class range { private: struct I { int x; int operator*() { return x; } bool operator!=(I &lhs) { return x < lhs.x; } void operator++() { ++x; } }; I i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} I &begin() { return i; } I &end() { return n; } }; int N; LL A[5123]; vector<int> g[5123]; vector<int> t[5123]; LL allB[5123][5123]; LL dp[5123][5123]; LL tmp[5123]; int sub[5123]; void dfs(int v, int p) { sub[v] = 1; for (int u : g[v]) { if (u == p) continue; t[v].push_back(u); dfs(u, v); sub[v] += sub[u]; } } void solve(int v) { for (int u : t[v]) { solve(u); } int x = 1; if (A[v] > 0) { allB[v][0] = 1; } else { allB[v][0] = 0; } dp[v][0] = A[v]; for (int u : t[v]) { const int nx = x + sub[u]; for (int i : range(nx + 1)) { tmp[i] = 0; } for (int i : range(x)) for (int j : range(sub[u] + 1)) { tmp[i + j] |= (allB[v][i] & allB[u][j]); } for (int i : range(nx + 1)) { allB[v][i] = tmp[i]; tmp[i] = 1e18; } for (int i : range(x)) for (int j : range(sub[u] + 1)) { chmin(tmp[i + j], dp[v][i] + dp[u][j]); } for (int i : range(nx + 1)) { dp[v][i] = tmp[i]; } x = nx; } for (int i : range(x)) { if (allB[v][i] || dp[v][i] < 0) { chmin(dp[v][i + 1], 0ll); allB[v][i + 1] = 1; } } } int main() { cin >> N; for (int i : range(N)) { cin >> A[i + 1]; } for (int i : range(N - 1)) { int u, v; cin >> u >> v; g[v].push_back(u); g[u].push_back(v); } dfs(1, 1); solve(1); int ret = N - 1; for (int i : range(N + 1)) { if (allB[1][i]) chmin(ret, i); if (dp[1][i] < 0) chmin(ret, i); } cout << ret << endl; return 0; }
replace
103
104
103
104
-11
p03159
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define _USE_MATH_DEFINES #include <complex> #include <functional> #include <math.h> using namespace std; #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 1000000007; const ll MAX = 4000001; const double eps = 1E-18; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint &a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b >> 1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// ll N; vll A; vvll g; vvll mn; vll dp1; vll dp2; vll merge(vll a, vll b) { // rep(i, a.size())cout << a[i] << " "; // cout << endl; // rep(i, b.size())cout << b[i] << " "; // cout << endl; vll c(a.size() + b.size() - 1, INF); rep(i, c.size()) { rep(j, b.size()) { if (i - j >= 0 && i - j < a.size()) c[i] = min(c[i], a[i - j] + b[j]); } } return c; } void dfs(ll u, ll v) { mn[v].assign(1, A[v]); for (ll w : g[v]) { if (u == w) { continue; } dfs(v, w); vll b(mn[w].size() + 1, INF); rep(i, mn[w].size() + 1) { if (i < mn[w].size()) b[i] = mn[w][i]; if (i >= dp1[w] + 1) { b[i] = min(0, b[i]); } } mn[v] = merge(mn[v], b); } if (A[v] < 0) { dp1[v] = INF; } else { dp1[v] = 0; for (ll w : g[v]) { if (u == w) { continue; } dp1[v] += min(dp1[w], dp2[w] + 1); } } dp2[v] = INF; rep(i, mn[v].size()) { if (mn[v][i] < 0) { dp2[v] = i; break; } } // cout << v << " " << dp1[v] << " " << dp2[v] << endl; // for (ll x : mn[v]) { cout << x << " "; } // cout << endl; return; } int main() { cin >> N; A.assign(N + 1, 0); repn(i, N) cin >> A[i]; g.resize(N + 1); repn(i, N - 1) { ll u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } mn.resize(N + 1); dp1.resize(N + 1); dp2.resize(N + 1); dfs(0, 1); cout << min(dp1[1], dp2[1]) << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define _USE_MATH_DEFINES #include <complex> #include <functional> #include <math.h> using namespace std; #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 1000000007; const ll MAX = 4000001; const double eps = 1E-18; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint &a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b >> 1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// ll N; vll A; vvll g; vvll mn; vll dp1; vll dp2; vll merge(vll a, vll b) { // rep(i, a.size())cout << a[i] << " "; // cout << endl; // rep(i, b.size())cout << b[i] << " "; // cout << endl; vll c(a.size() + b.size() - 1, INF); rep(i, a.size()) rep(j, b.size()) { c[i + j] = min(c[i + j], a[i] + b[j]); } return c; } void dfs(ll u, ll v) { mn[v].assign(1, A[v]); for (ll w : g[v]) { if (u == w) { continue; } dfs(v, w); vll b(mn[w].size() + 1, INF); rep(i, mn[w].size() + 1) { if (i < mn[w].size()) b[i] = mn[w][i]; if (i >= dp1[w] + 1) { b[i] = min(0, b[i]); } } mn[v] = merge(mn[v], b); } if (A[v] < 0) { dp1[v] = INF; } else { dp1[v] = 0; for (ll w : g[v]) { if (u == w) { continue; } dp1[v] += min(dp1[w], dp2[w] + 1); } } dp2[v] = INF; rep(i, mn[v].size()) { if (mn[v][i] < 0) { dp2[v] = i; break; } } // cout << v << " " << dp1[v] << " " << dp2[v] << endl; // for (ll x : mn[v]) { cout << x << " "; } // cout << endl; return; } int main() { cin >> N; A.assign(N + 1, 0); repn(i, N) cin >> A[i]; g.resize(N + 1); repn(i, N - 1) { ll u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } mn.resize(N + 1); dp1.resize(N + 1); dp2.resize(N + 1); dfs(0, 1); cout << min(dp1[1], dp2[1]) << endl; }
replace
150
157
150
151
TLE
p03159
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define f first #define s second #define cbr cerr << "hi\n" #define mmst(x, v) memset((x), v, sizeof((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x, y) (lower_bound(all(x), y) - x.begin()) #define ubd(x, y) (upper_bound(all(x), y) - x.begin()) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline long long rand(long long x, long long y) { return (rng() % (y + 1 - x)) + x; } // inclusivesss string inline to_string(char c) { string s(1, c); return s; } template <typename T> inline T gcd(T a, T b) { return a == 0 ? llabs(b) : gcd(b % a, a); } typedef long long ll; typedef long double ld; #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; #define LLINF ((long long)1e18) #define INF int(1e9 + 1e6) #define MAXN (5006) ll n, A[MAXN], dp[MAXN][MAXN], sz[MAXN]; // if 0 then max, 1 then min bitset<MAXN> dp2[MAXN]; vector<int> v[MAXN]; void dfs(ll x, ll p) { sz[x] = 1; dp[x][0] = A[x]; for (auto i : v[x]) if (i ^ p) dfs(i, x), sz[x] += sz[i]; FOR(k, 1, sz[x]) dp[x][k] = LLINF; ll cur = 1; if (A[x] > 0) dp2[x][0] = 1; for (auto i : v[x]) if (i ^ p) { DEC(j, cur + sz[i], 0) { if (dp[x][j] < LLINF) dp[x][j] += dp[i][0]; dp2[x][j] = dp2[x][j] & dp2[i][0]; FOR(k, 1ll, min(j, sz[i])) { dp[x][j] = min(dp[x][j], dp[x][j - k] + dp[i][k]); if (dp2[i][k - 1]) dp[x][j] = min(dp[x][j], dp[x][j - k]); dp2[x][j] = dp2[x][j] | (dp2[x][j - k] & dp2[i][k]); if (dp[i][k - 1] < 0) dp2[x][j] = dp2[x][j] | dp2[x][j - k]; } } cur += sz[i]; } } int main() { FAST cin >> n; FOR(i, 1, n) cin >> A[i]; FOR(i, 2, n) { ll a, b; cin >> a >> b; v[a].eb(b), v[b].eb(a); } dfs(1, 1); ll ans = n - 1; FOR(k, 0, n - 1) if (dp[1][k] < 0) ans = min(ans, k); FOR(k, 0, n - 1) if (dp2[1][k]) ans = min(ans, k); cout << ans << '\n'; }
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define f first #define s second #define cbr cerr << "hi\n" #define mmst(x, v) memset((x), v, sizeof((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x, y) (lower_bound(all(x), y) - x.begin()) #define ubd(x, y) (upper_bound(all(x), y) - x.begin()) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) inline long long rand(long long x, long long y) { return (rng() % (y + 1 - x)) + x; } // inclusivesss string inline to_string(char c) { string s(1, c); return s; } template <typename T> inline T gcd(T a, T b) { return a == 0 ? llabs(b) : gcd(b % a, a); } typedef long long ll; typedef long double ld; #define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i) #define DEC(i, s, e) for (ll i = s; i >= ll(e); --i) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; #define LLINF ((long long)1e18) #define INF int(1e9 + 1e6) #define MAXN (5006) ll n, A[MAXN], dp[MAXN][MAXN], sz[MAXN]; // if 0 then max, 1 then min bitset<MAXN> dp2[MAXN]; vector<int> v[MAXN]; void dfs(ll x, ll p) { sz[x] = 1; dp[x][0] = A[x]; for (auto i : v[x]) if (i ^ p) dfs(i, x), sz[x] += sz[i]; FOR(k, 1, sz[x]) dp[x][k] = LLINF; ll cur = 1; if (A[x] > 0) dp2[x][0] = 1; for (auto i : v[x]) if (i ^ p) { DEC(j, cur + sz[i], 0) { if (dp[x][j] < LLINF) dp[x][j] += dp[i][0]; dp2[x][j] = dp2[x][j] & dp2[i][0]; FOR(k, max(j - cur, 1ll), min(j, sz[i])) { dp[x][j] = min(dp[x][j], dp[x][j - k] + dp[i][k]); if (dp2[i][k - 1]) dp[x][j] = min(dp[x][j], dp[x][j - k]); dp2[x][j] = dp2[x][j] | (dp2[x][j - k] & dp2[i][k]); if (dp[i][k - 1] < 0) dp2[x][j] = dp2[x][j] | dp2[x][j - k]; } } cur += sz[i]; } } int main() { FAST cin >> n; FOR(i, 1, n) cin >> A[i]; FOR(i, 2, n) { ll a, b; cin >> a >> b; v[a].eb(b), v[b].eb(a); } dfs(1, 1); ll ans = n - 1; FOR(k, 0, n - 1) if (dp[1][k] < 0) ans = min(ans, k); FOR(k, 0, n - 1) if (dp2[1][k]) ans = min(ans, k); cout << ans << '\n'; }
replace
62
63
62
63
TLE
p03159
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define int long long #define INF 1000000000000000000 using namespace std; #define ANS(f) \ if (f) \ cout << "YES" << endl; \ else \ cout << "NO" << endl; typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> Pii; template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; } void readi(vector<int> &a) { REP(i, a.size()) { cin >> a[i]; a[i]--; } } void debug(mat m) { REP(i, m.size()) { REP(j, m[i].size()) { cout << m[i][j] << ","; } cout << endl; } } struct edge { int to, cost; }; class Graph { public: int V; vector<vector<edge>> G; vec gen, par; mat chi, genlist; int depth; Graph(int V) : V(V) { G = vector<vector<edge>>(V, vector<edge>(0)); } void add_edge(int from, int to, int cost) { G[from].push_back(edge({to, cost})); } void add_edge2(int v1, int v2, int cost) { add_edge(v1, v2, cost); add_edge(v2, v1, cost); } void dfs(int v, int d) { gen[v] = d; depth = max(depth, d + 1); REP(k, G[v].size()) { if (G[v][k].to == par[v]) continue; par[G[v][k].to] = v; chi[v].push_back(G[v][k].to); dfs(G[v][k].to, d + 1); } } mat makegenlist() { REP(v, V) genlist[gen[v]].push_back(v); return genlist; } void analyzeTree(int root) { gen = vec(V); par = vec(V); chi = mat(V, vec(0)); depth = 0; par[root] = -1; dfs(root, 0); genlist = mat(depth, vec(0)); makegenlist(); } }; signed main() { int N; cin >> N; vec A(N); readv(A); Graph T(N); int U, V; REP(i, N - 1) { cin >> U >> V; T.add_edge2(U - 1, V - 1, 0); } T.analyzeTree(0); vector<mat> dp(N); vec sz(N, 0); IREP(d, T.depth) { for (int v : T.genlist[d]) { vector<mat> dq(1 + T.chi[v].size()); sz[v] = 1; dq[0] = mat(2, vec(1, INF)); if (A[v] < 0) dq[0][1][0] = A[v]; else dq[0][0][0] = A[v]; int i0 = 1; for (int chi : T.chi[v]) { sz[v] += sz[chi]; dq[i0] = mat(2, vec(sz[v], INF)); // 切る int m = INF; REP(k, sz[chi]) { if (dp[chi][0][k] < INF || dp[chi][1][k] < 0) { m = k; break; } } if (m < INF) { REP(k, dq[i0 - 1][0].size()) { dq[i0][0][k + 1 + m] = dq[i0 - 1][0][k]; dq[i0][1][k + 1 + m] = dq[i0 - 1][1][k]; } } // 繫げる REP(k, sz[v]) { REP(l, min(sz[chi], k + 1)) { if (!(k - l >= 0 && k - l < dq[i0 - 1][0].size())) continue; if (dp[chi][0][l] < INF && dq[i0 - 1][0][k - l] < INF) dq[i0][0][k] = min(dq[i0][0][k], dp[chi][0][l] + dq[i0 - 1][0][k - l]); if (dp[chi][1][l] < INF && dq[i0 - 1][1][k - l] < INF) dq[i0][1][k] = min(dq[i0][1][k], dp[chi][1][l] + dq[i0 - 1][1][k - l]); if (dp[chi][0][l] < INF && dq[i0 - 1][1][k - l] < INF) dq[i0][1][k] = min(dq[i0][1][k], dp[chi][0][l] + dq[i0 - 1][1][k - l]); if (dp[chi][1][l] < INF && dq[i0 - 1][0][k - l] < INF) dq[i0][1][k] = min(dq[i0][1][k], dp[chi][1][l] + dq[i0 - 1][0][k - l]); } } i0++; } dp[v] = dq[T.chi[v].size()]; /* cout << v + 1 << endl; REP(i, sz[v]) cout << dp[v][0][i] << ","; cout << endl; REP(i, sz[v]) cout << dp[v][1][i] << ","; cout << endl << endl;*/ } } int ans = INF; REP(k, sz[0]) { if (dp[0][0][k] < INF || dp[0][1][k] < 0) { ans = k; break; } } cout << ans; return 0; }
#include <bits/stdc++.h> #define FOR(i, begin, end) for (int i = (begin); i < (end); i++) #define REP(i, n) FOR(i, 0, n) #define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--) #define IREP(i, n) IFOR(i, 0, n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) #define Lower_bound(v, x) \ distance(v.begin(), lower_bound(v.begin(), v.end(), x)) #define Upper_bound(v, x) \ distance(v.begin(), upper_bound(v.begin(), v.end(), x)) #define int long long #define INF 1000000000000000000 using namespace std; #define ANS(f) \ if (f) \ cout << "YES" << endl; \ else \ cout << "NO" << endl; typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> Pii; template <typename T> void readv(vector<T> &a) { REP(i, a.size()) cin >> a[i]; } void readi(vector<int> &a) { REP(i, a.size()) { cin >> a[i]; a[i]--; } } void debug(mat m) { REP(i, m.size()) { REP(j, m[i].size()) { cout << m[i][j] << ","; } cout << endl; } } struct edge { int to, cost; }; class Graph { public: int V; vector<vector<edge>> G; vec gen, par; mat chi, genlist; int depth; Graph(int V) : V(V) { G = vector<vector<edge>>(V, vector<edge>(0)); } void add_edge(int from, int to, int cost) { G[from].push_back(edge({to, cost})); } void add_edge2(int v1, int v2, int cost) { add_edge(v1, v2, cost); add_edge(v2, v1, cost); } void dfs(int v, int d) { gen[v] = d; depth = max(depth, d + 1); REP(k, G[v].size()) { if (G[v][k].to == par[v]) continue; par[G[v][k].to] = v; chi[v].push_back(G[v][k].to); dfs(G[v][k].to, d + 1); } } mat makegenlist() { REP(v, V) genlist[gen[v]].push_back(v); return genlist; } void analyzeTree(int root) { gen = vec(V); par = vec(V); chi = mat(V, vec(0)); depth = 0; par[root] = -1; dfs(root, 0); genlist = mat(depth, vec(0)); makegenlist(); } }; signed main() { int N; cin >> N; vec A(N); readv(A); Graph T(N); int U, V; REP(i, N - 1) { cin >> U >> V; T.add_edge2(U - 1, V - 1, 0); } T.analyzeTree(0); vector<mat> dp(N); vec sz(N, 0); IREP(d, T.depth) { for (int v : T.genlist[d]) { vector<mat> dq(1 + T.chi[v].size()); sz[v] = 1; dq[0] = mat(2, vec(1, INF)); if (A[v] < 0) dq[0][1][0] = A[v]; else dq[0][0][0] = A[v]; int i0 = 1; for (int chi : T.chi[v]) { sz[v] += sz[chi]; dq[i0] = mat(2, vec(sz[v], INF)); // 切る int m = INF; REP(k, sz[chi]) { if (dp[chi][0][k] < INF || dp[chi][1][k] < 0) { m = k; break; } } if (m < INF) { REP(k, dq[i0 - 1][0].size()) { dq[i0][0][k + 1 + m] = dq[i0 - 1][0][k]; dq[i0][1][k + 1 + m] = dq[i0 - 1][1][k]; } } // 繫げる REP(k, sz[chi]) { REP(l, dq[i0 - 1][0].size()) { if (dp[chi][0][k] < INF && dq[i0 - 1][0][l] < INF) dq[i0][0][k + l] = min(dq[i0][0][k + l], dp[chi][0][k] + dq[i0 - 1][0][l]); if (dp[chi][1][k] < INF && dq[i0 - 1][1][l] < INF) dq[i0][1][k + l] = min(dq[i0][1][k + l], dp[chi][1][k] + dq[i0 - 1][1][l]); if (dp[chi][0][k] < INF && dq[i0 - 1][1][l] < INF) dq[i0][1][k + l] = min(dq[i0][1][k + l], dp[chi][0][k] + dq[i0 - 1][1][l]); if (dp[chi][1][k] < INF && dq[i0 - 1][0][l] < INF) dq[i0][1][k + l] = min(dq[i0][1][k + l], dp[chi][1][k] + dq[i0 - 1][0][l]); } } i0++; } dp[v] = dq[T.chi[v].size()]; /* cout << v + 1 << endl; REP(i, sz[v]) cout << dp[v][0][i] << ","; cout << endl; REP(i, sz[v]) cout << dp[v][1][i] << ","; cout << endl << endl;*/ } } int ans = INF; REP(k, sz[0]) { if (dp[0][0][k] < INF || dp[0][1][k] < 0) { ans = k; break; } } cout << ans; return 0; }
replace
139
155
139
153
TLE
p03159
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; const long long MAXN = 3e3 + 5; vector<long long> v1[MAXN]; long long dp[MAXN][MAXN][3]; long long temp[MAXN][3]; long long sz[MAXN]; long long arr[MAXN]; void dfs(long long curr, long long par) { for (long long x : v1[curr]) { if (x != par) { dfs(x, curr); } } long long sum = 1; // current subtree sz dp[curr][0][arr[curr] < 0] = arr[curr]; // 0 edges cut, is a computer = 1, not a computer = 0 for (long long x : v1[curr]) { if (x != par) { for (long long i = 0; i < sum; i++) { for (long long j = 0; j < sz[x]; j++) { for (long long k1 = 0; k1 < 2; k1++) { for (long long k2 = 0; k2 < 2; k2++) { // merge components together(all possible cuts) temp[i + j][k1 | k2] = min(temp[i + j][k1 | k2], dp[curr][i][k1] + dp[x][j][k2]); if ((k2 == 0 && dp[x][j][k2] < 1e18) || (k2 == 1 && dp[x][j][k2] < 0)) { // cut edge between curr and x, cuts++, whether or not a // computer exists depends on the current component itself. You // can only do this if x's subtree has a value less than 0 when // it has a computer, or if x's subtree doesn't currently have a // computer. temp[i + j + 1][k1] = min(temp[i + j + 1][k1], dp[curr][i][k1]); } } } } } //(actually O(n)) sum += sz[x]; for (long long i = 0; i < sum; i++) { for (long long k = 0; k < 2; k++) { dp[curr][i][k] = temp[i][k]; temp[i][k] = 1e18; // update all values to dp } } } } sz[curr] = sum; } int main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) { cin >> arr[i]; } for (long long i = 1; i < n; i++) { long long u, v; cin >> u >> v; v1[u].push_back(v); v1[v].push_back(u); } for (long long i = 1; i <= n; i++) { for (long long j = 0; j <= n; j++) { for (long long k = 0; k <= 1; k++) { dp[i][j][k] = 1e18; temp[j][k] = 1e18; } } } dfs(1, 1); long long ans = n - 1; for (long long i = 0; i < n; i++) { if (dp[1][i][0] < 1e18) { ans = min(ans, i); } if (dp[1][i][1] < 0) { ans = min(ans, i); } } cout << ans << endl; }
#include <iostream> #include <vector> using namespace std; const long long MAXN = 5e3 + 5; vector<long long> v1[MAXN]; long long dp[MAXN][MAXN][3]; long long temp[MAXN][3]; long long sz[MAXN]; long long arr[MAXN]; void dfs(long long curr, long long par) { for (long long x : v1[curr]) { if (x != par) { dfs(x, curr); } } long long sum = 1; // current subtree sz dp[curr][0][arr[curr] < 0] = arr[curr]; // 0 edges cut, is a computer = 1, not a computer = 0 for (long long x : v1[curr]) { if (x != par) { for (long long i = 0; i < sum; i++) { for (long long j = 0; j < sz[x]; j++) { for (long long k1 = 0; k1 < 2; k1++) { for (long long k2 = 0; k2 < 2; k2++) { // merge components together(all possible cuts) temp[i + j][k1 | k2] = min(temp[i + j][k1 | k2], dp[curr][i][k1] + dp[x][j][k2]); if ((k2 == 0 && dp[x][j][k2] < 1e18) || (k2 == 1 && dp[x][j][k2] < 0)) { // cut edge between curr and x, cuts++, whether or not a // computer exists depends on the current component itself. You // can only do this if x's subtree has a value less than 0 when // it has a computer, or if x's subtree doesn't currently have a // computer. temp[i + j + 1][k1] = min(temp[i + j + 1][k1], dp[curr][i][k1]); } } } } } //(actually O(n)) sum += sz[x]; for (long long i = 0; i < sum; i++) { for (long long k = 0; k < 2; k++) { dp[curr][i][k] = temp[i][k]; temp[i][k] = 1e18; // update all values to dp } } } } sz[curr] = sum; } int main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) { cin >> arr[i]; } for (long long i = 1; i < n; i++) { long long u, v; cin >> u >> v; v1[u].push_back(v); v1[v].push_back(u); } for (long long i = 1; i <= n; i++) { for (long long j = 0; j <= n; j++) { for (long long k = 0; k <= 1; k++) { dp[i][j][k] = 1e18; temp[j][k] = 1e18; } } } dfs(1, 1); long long ans = n - 1; for (long long i = 0; i < n; i++) { if (dp[1][i][0] < 1e18) { ans = min(ans, i); } if (dp[1][i][1] < 0) { ans = min(ans, i); } } cout << ans << endl; }
replace
5
6
5
6
-11
p03159
C++
Time Limit Exceeded
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int MAX = 5100; int n; vector<int> a; vector<vector<int>> g; int num[MAX]; int dp[MAX][MAX][2]; int sdp[MAX][2]; void rec(int v, int p) { num[v] = 1; for (int nv : g[v]) { if (nv == p) continue; rec(nv, v); num[v] += num[nv]; } for (int i = 0; i <= num[v]; ++i) { sdp[i][0] = sdp[i][1] = INF; } sdp[0][0] = a[v]; if (a[v] > 0) sdp[0][1] = a[v]; int cur = 0; for (int nv : g[v]) { if (nv == p) continue; for (int i = cur; i >= 0; --i) { int tmp0 = sdp[i][0], tmp1 = sdp[i][1]; sdp[i][0] = INF, sdp[i][1] = INF; for (int j = 0; j <= num[nv]; ++j) { chmin(sdp[i + j][0], tmp0 + dp[nv][j][0]); if (dp[nv][j][0] < 0 || dp[nv][j][1] < INF) chmin(sdp[i + j + 1][0], tmp0); if (a[v] > 0) { chmin(sdp[i + j][1], tmp1 + dp[nv][j][1]); if (dp[nv][j][0] < 0 || dp[nv][j][1] < INF) chmin(sdp[i + j + 1][1], tmp1); } } } cur += num[v]; } for (int i = 0; i <= num[v]; ++i) { dp[v][i][0] = sdp[i][0]; dp[v][i][1] = sdp[i][1]; } } void solve() { cin >> n; a.resize(n); rep(i, n) { cin >> a[i]; } g.assign(n, vector<int>()); rep(i, n - 1) { int u, v; cin >> u >> v; --u, --v; g[u].pb(v); g[v].pb(u); } for (int i = 0; i < n; ++i) { for (int j = 0; j <= MAX; ++j) { dp[i][j][0] = dp[i][j][1] = INF; } } rec(0, -1); int ans = n; for (int j = n; j >= 0; --j) { if (dp[0][j][0] < 0) ans = j; if (dp[0][j][1] < INF) ans = j; } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int MAX = 5100; int n; vector<int> a; vector<vector<int>> g; int num[MAX]; int dp[MAX][MAX][2]; int sdp[MAX][2]; void rec(int v, int p) { num[v] = 1; for (int nv : g[v]) { if (nv == p) continue; rec(nv, v); num[v] += num[nv]; } for (int i = 0; i <= num[v]; ++i) { sdp[i][0] = sdp[i][1] = INF; } sdp[0][0] = a[v]; if (a[v] > 0) sdp[0][1] = a[v]; int cur = 0; for (int nv : g[v]) { if (nv == p) continue; for (int i = cur; i >= 0; --i) { int tmp0 = sdp[i][0], tmp1 = sdp[i][1]; sdp[i][0] = INF, sdp[i][1] = INF; for (int j = 0; j <= num[nv]; ++j) { chmin(sdp[i + j][0], tmp0 + dp[nv][j][0]); if (dp[nv][j][0] < 0 || dp[nv][j][1] < INF) chmin(sdp[i + j + 1][0], tmp0); if (a[v] > 0) { chmin(sdp[i + j][1], tmp1 + dp[nv][j][1]); if (dp[nv][j][0] < 0 || dp[nv][j][1] < INF) chmin(sdp[i + j + 1][1], tmp1); } } } cur += num[nv]; } for (int i = 0; i <= num[v]; ++i) { dp[v][i][0] = sdp[i][0]; dp[v][i][1] = sdp[i][1]; } } void solve() { cin >> n; a.resize(n); rep(i, n) { cin >> a[i]; } g.assign(n, vector<int>()); rep(i, n - 1) { int u, v; cin >> u >> v; --u, --v; g[u].pb(v); g[v].pb(u); } for (int i = 0; i < n; ++i) { for (int j = 0; j <= MAX; ++j) { dp[i][j][0] = dp[i][j][1] = INF; } } rec(0, -1); int ans = n; for (int j = n; j >= 0; --j) { if (dp[0][j][0] < 0) ans = j; if (dp[0][j][1] < INF) ans = j; } cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
replace
111
112
111
112
TLE
p03159
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[5010][5010]; long long tmp[5010]; bool pos[5010][5010]; bool ptmp[5010]; int sz[5010]; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<int>> e(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; e[u].emplace_back(v); e[v].emplace_back(u); } for (int i = 0; i < n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 1e16; } } auto f = [&](auto f, int curr, int prev) -> void { sz[curr] = 1; pos[curr][0] = (a[curr] > 0); dp[curr][0] = a[curr]; for (auto &to : e[curr]) { if (to == prev) continue; f(f, to, curr); for (int i = 0; i < n; i++) tmp[i] = 1e16, ptmp[i] = 0; for (int i = 0; i < sz[curr]; i--) { for (int j = 0; j < sz[to]; j++) { if (pos[to][j] || dp[to][j] < 0) { tmp[i + j + 1] = min(tmp[i + j + 1], dp[curr][i]); ptmp[i + j + 1] |= pos[curr][i]; } ptmp[i + j] |= pos[curr][i] & pos[to][j]; tmp[i + j] = min(tmp[i + j], dp[curr][i] + dp[to][j]); } } sz[curr] += sz[to]; for (int i = 0; i < sz[curr]; i++) { dp[curr][i] = tmp[i]; pos[curr][i] = ptmp[i]; } } }; f(f, 0, -1); for (int i = 0; i < n; i++) { if (dp[0][i] < 0 || pos[0][i]) { cout << i << "\n"; return 0; } } assert(false); return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[5010][5010]; long long tmp[5010]; bool pos[5010][5010]; bool ptmp[5010]; int sz[5010]; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<int>> e(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; e[u].emplace_back(v); e[v].emplace_back(u); } for (int i = 0; i < n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 1e16; } } auto f = [&](auto f, int curr, int prev) -> void { sz[curr] = 1; pos[curr][0] = (a[curr] > 0); dp[curr][0] = a[curr]; for (auto &to : e[curr]) { if (to == prev) continue; f(f, to, curr); for (int i = 0; i < n; i++) tmp[i] = 1e16, ptmp[i] = 0; for (int i = 0; i < sz[curr]; i++) { for (int j = 0; j < sz[to]; j++) { if (pos[to][j] || dp[to][j] < 0) { tmp[i + j + 1] = min(tmp[i + j + 1], dp[curr][i]); ptmp[i + j + 1] |= pos[curr][i]; } ptmp[i + j] |= pos[curr][i] & pos[to][j]; tmp[i + j] = min(tmp[i + j], dp[curr][i] + dp[to][j]); } } sz[curr] += sz[to]; for (int i = 0; i < sz[curr]; i++) { dp[curr][i] = tmp[i]; pos[curr][i] = ptmp[i]; } } }; f(f, 0, -1); for (int i = 0; i < n; i++) { if (dp[0][i] < 0 || pos[0][i]) { cout << i << "\n"; return 0; } } assert(false); return 0; }
replace
40
41
40
41
-11
p03159
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v.begin(), v.end()) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } const long long INF = 1LL << 60; const int MAX = 5100; int N; vector<long long> A; vector<vector<int>> G; int num[MAX]; long long dp[MAX][MAX][2]; // vの部分木、vを含めず何個か、(全部正かどうか) long long sdp[2][MAX][2]; void rec(int v, int p) { int numc = 0; num[v] = 1; for (auto ch : G[v]) { if (ch == p) continue; rec(ch, v); ++numc; num[v] += num[ch]; } for (int i = 0; i < 2; ++i) for (int j = 0; j <= num[v]; ++j) sdp[i][j][0] = sdp[i][j][1] = INF; sdp[0][0][0] = A[v]; if (A[v] > 0) sdp[0][0][1] = A[v]; int i = 0; for (auto ch : G[v]) { if (ch == p) continue; for (int j = 0; j <= num[v]; ++j) { for (int k = 0; k <= num[ch] && j + k <= num[v]; ++k) { chmin(sdp[(i + 1) % 2][j + k][0], sdp[i % 2][j][0] + dp[ch][k][0]); chmin(sdp[(i + 1) % 2][j + k][0], sdp[i % 2][j][0] + dp[ch][k][1]); if (dp[ch][k][0] < 0) chmin(sdp[(i + 1) % 2][j + k + 1][0], sdp[i % 2][j][0]); if (dp[ch][k][1] < INF / 2) chmin(sdp[(i + 1) % 2][j + k + 1][0], sdp[i % 2][j][0]); if (A[v] > 0) { chmin(sdp[(i + 1) % 2][j + k][1], sdp[i % 2][j][1] + dp[ch][k][1]); if (dp[ch][k][0] < 0) chmin(sdp[(i + 1) % 2][j + k + 1][1], sdp[i % 2][j][1]); if (dp[ch][k][1] < INF / 2) chmin(sdp[(i + 1) % 2][j + k + 1][1], sdp[i % 2][j][1]); } } } for (int j = 0; j <= num[v]; ++j) sdp[i % 2][j][0] = sdp[i % 2][j][1] = INF; ++i; } for (int j = 0; j <= num[v]; ++j) { dp[v][j][0] = sdp[numc % 2][j][0]; dp[v][j][1] = sdp[numc % 2][j][1]; // cout << v << ", " << j << ": " << make_pair(dp[v][j][0], dp[v][j][1]) << // endl; } } int main() { while (cin >> N) { A.resize(N); for (int i = 0; i < N; ++i) cin >> A[i]; G.assign(N, vector<int>()); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; --u, --v; G[u].push_back(v); G[v].push_back(u); } for (int i = 0; i < MAX; ++i) for (int j = 0; j < MAX; ++j) dp[i][j][0] = dp[i][j][1] = INF; rec(0, -1); int res = N; for (int i = 0; i <= N; ++i) { if (dp[0][i][0] < 0) chmin(res, i); if (dp[0][i][1] < INF / 2) chmin(res, i); } cout << res << endl; } }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v.begin(), v.end()) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) \ for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template <class T> ostream &operator<<(ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template <class T> ostream &operator<<(ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } const long long INF = 1LL << 60; const int MAX = 5100; int N; vector<long long> A; vector<vector<int>> G; int num[MAX]; long long dp[MAX][MAX][2]; // vの部分木、vを含めず何個か、(全部正かどうか) long long sdp[2][MAX][2]; void rec(int v, int p) { int numc = 0; num[v] = 1; for (auto ch : G[v]) { if (ch == p) continue; rec(ch, v); ++numc; num[v] += num[ch]; } for (int i = 0; i < 2; ++i) for (int j = 0; j <= num[v]; ++j) sdp[i][j][0] = sdp[i][j][1] = INF; sdp[0][0][0] = A[v]; if (A[v] > 0) sdp[0][0][1] = A[v]; int i = 0; for (auto ch : G[v]) { if (ch == p) continue; for (int j = 0; j <= num[v]; ++j) { if (sdp[i % 2][j][0] > INF / 2) break; for (int k = 0; k <= num[ch] && j + k <= num[v]; ++k) { chmin(sdp[(i + 1) % 2][j + k][0], sdp[i % 2][j][0] + dp[ch][k][0]); chmin(sdp[(i + 1) % 2][j + k][0], sdp[i % 2][j][0] + dp[ch][k][1]); if (dp[ch][k][0] < 0) chmin(sdp[(i + 1) % 2][j + k + 1][0], sdp[i % 2][j][0]); if (dp[ch][k][1] < INF / 2) chmin(sdp[(i + 1) % 2][j + k + 1][0], sdp[i % 2][j][0]); if (A[v] > 0) { chmin(sdp[(i + 1) % 2][j + k][1], sdp[i % 2][j][1] + dp[ch][k][1]); if (dp[ch][k][0] < 0) chmin(sdp[(i + 1) % 2][j + k + 1][1], sdp[i % 2][j][1]); if (dp[ch][k][1] < INF / 2) chmin(sdp[(i + 1) % 2][j + k + 1][1], sdp[i % 2][j][1]); } } } for (int j = 0; j <= num[v]; ++j) sdp[i % 2][j][0] = sdp[i % 2][j][1] = INF; ++i; } for (int j = 0; j <= num[v]; ++j) { dp[v][j][0] = sdp[numc % 2][j][0]; dp[v][j][1] = sdp[numc % 2][j][1]; // cout << v << ", " << j << ": " << make_pair(dp[v][j][0], dp[v][j][1]) << // endl; } } int main() { while (cin >> N) { A.resize(N); for (int i = 0; i < N; ++i) cin >> A[i]; G.assign(N, vector<int>()); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; --u, --v; G[u].push_back(v); G[v].push_back(u); } for (int i = 0; i < MAX; ++i) for (int j = 0; j < MAX; ++j) dp[i][j][0] = dp[i][j][1] = INF; rec(0, -1); int res = N; for (int i = 0; i <= N; ++i) { if (dp[0][i][0] < 0) chmin(res, i); if (dp[0][i][1] < INF / 2) chmin(res, i); } cout << res << endl; } }
insert
105
105
105
107
TLE
p03159
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1e18; signed main() { int n; cin >> n; vector<int> a(n); for (auto &y : a) cin >> y; vector<int> u(n - 1); vector<int> v(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> u[i] >> v[i]; --u[i]; --v[i]; } vector<vector<int>> edges(n); for (int i = 0; i < n - 1; ++i) { edges[u[i]].push_back(v[i]); edges[v[i]].push_back(u[i]); } vector<int> size(n, 1); bitset<5000> flag; function<void(int)> siz = [&](int x) { for (auto &y : edges[x]) if (!flag[y]) { flag[y] = 1; siz(y); size[x] += size[y]; } }; flag[0] = 1; siz(0); flag.reset(); function<vector<vector<ll>>(int)> f = [&](int x) { vector<vector<ll>> dp(1, vector<ll>(2, INF)); dp[0][0] = a[x]; if (a[x] > 0) dp[0][1] = a[x]; int sizesum = 0; for (auto &y : edges[x]) { if (flag[y]) continue; flag[y] = 1; auto from = f(y); vector<vector<ll>> next(sizesum + size[y] + 1, vector<ll>(2, INF)); for (int i = 0; i <= sizesum; ++i) { for (int j = 0; j < size[y]; ++j) { next[i + j][0] = min(next[i + j][0], dp[i][0] + from[j][0]); next[i + j][0] = min(next[i + j][0], dp[i][0] + from[j][1]); next[i + j][0] = min(next[i + j][0], dp[i][1] + from[j][0]); next[i + j][0] = min(next[i + j][0], dp[i][1] + from[j][1]); if (a[x] > 0 && from[j][1] != INF) next[i + j][1] = min(next[i + j][1], dp[i][1] + from[j][1]); // 分断が可能なら if (from[j][0] < 0 || from[j][1] != INF) { next[i + j + 1][0] = min(next[i + j + 1][0], dp[i][0]); if (a[x] > 0) next[i + j + 1][1] = min(next[i + j + 1][1], dp[i][1]); } } } dp = move(next); sizesum += size[y]; } return dp; }; flag[0] = 1; auto dp = f(0); int ans = 1e9; for (int j = 0; j <= n; ++j) { if (dp[j][0] < 0) ans = min(ans, j); if (dp[j][1] != INF) ans = min(ans, j); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1e18; signed main() { int n; cin >> n; vector<int> a(n); for (auto &y : a) cin >> y; vector<int> u(n - 1); vector<int> v(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> u[i] >> v[i]; --u[i]; --v[i]; } vector<vector<int>> edges(n); for (int i = 0; i < n - 1; ++i) { edges[u[i]].push_back(v[i]); edges[v[i]].push_back(u[i]); } vector<int> size(n, 1); bitset<5000> flag; function<void(int)> siz = [&](int x) { for (auto &y : edges[x]) if (!flag[y]) { flag[y] = 1; siz(y); size[x] += size[y]; } }; flag[0] = 1; siz(0); flag.reset(); function<vector<vector<ll>>(int)> f = [&](int x) { vector<vector<ll>> dp(1, vector<ll>(2, INF)); dp[0][0] = a[x]; if (a[x] > 0) dp[0][1] = a[x]; int sizesum = 0; for (auto &y : edges[x]) { if (flag[y]) continue; flag[y] = 1; auto from = f(y); vector<vector<ll>> next(sizesum + size[y] + 1, vector<ll>(2, INF)); for (int i = 0; i <= sizesum; ++i) { for (int j = 0; j < size[y]; ++j) { next[i + j][0] = min(next[i + j][0], dp[i][0] + from[j][0]); next[i + j][0] = min(next[i + j][0], dp[i][0] + from[j][1]); next[i + j][0] = min(next[i + j][0], dp[i][1] + from[j][0]); next[i + j][0] = min(next[i + j][0], dp[i][1] + from[j][1]); if (a[x] > 0 && from[j][1] != INF) next[i + j][1] = min(next[i + j][1], dp[i][1] + from[j][1]); // 分断が可能なら if (from[j][0] < 0 || from[j][1] != INF) { next[i + j + 1][0] = min(next[i + j + 1][0], dp[i][0]); if (a[x] > 0) next[i + j + 1][1] = min(next[i + j + 1][1], dp[i][1]); } } } dp = move(next); sizesum += size[y]; } return dp; }; flag[0] = 1; auto dp = f(0); int ans = 1e9; for (int j = 0; j < (int)dp.size(); ++j) { if (dp[j][0] < 0) ans = min(ans, j); if (dp[j][1] != INF) ans = min(ans, j); } cout << ans << endl; }
replace
81
82
81
82
-11
p03159
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using std::cerr; using std::cin; using std::cout; using std::endl; using std::bitset; using std::deque; using std::iterator; using std::map; using std::multimap; using std::multiset; using std::pair; using std::queue; using std::set; using std::stack; using std::string; using std::vector; using std::fill; using std::ios_base; using std::max_element; using std::min_element; using std::reverse; using std::sort; using std::stable_sort; using std::swap; using std::unique; using std::fixed; using std::setprecision; long long min(long long a, long long b) { return a < b ? a : b; } long long min(int a, long long b) { return a < b ? a : b; } long long min(long long a, int b) { return a < b ? a : b; } long long min(int a, int b) { return a < b ? a : b; } long long max(long long a, long long b) { return a > b ? a : b; } long long max(int a, long long b) { return a > b ? a : b; } long long max(long long a, int b) { return a > b ? a : b; } long long max(int a, int b) { return a > b ? a : b; } #define int long long typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<long long> vl; typedef string S; #define F(i, n) for (int(i) = 0; (i) != (n); (i)++) #define fi first #define se second #define re return #define all(x) (x).begin(), (x).end() const long long INF = 1e18; const int N = 10100; const int MOD = 1e9 + 7; const double eps = 1e-8; int n; int a[N]; vector<int> graph[N]; int dp[2][N][N / 2]; int last_dinamics[N]; int last_state[N]; int tmp = 0; void Merge(int p1, int p2) { for (int a = 0; a <= last_state[p1]; a++) { for (int b = 0; b <= last_state[p2]; b++) { dp[0][tmp][a + b] = min(dp[0][tmp][a + b], dp[0][p1][a] + dp[0][p2][b]); if (dp[0][p2][b] < INF) dp[0][tmp][a + b + 1] = min(dp[0][tmp][a + b + 1], dp[0][p1][a]); if (dp[1][p2][b] < 0) dp[0][tmp][a + b + 1] = min(dp[0][tmp][a + b + 1], dp[0][p1][a]); dp[1][tmp][a + b] = min(dp[1][tmp][a + b], min(dp[0][p1][a], dp[1][p1][a]) + min(dp[0][p2][b], dp[1][p2][b])); if (dp[0][p2][b] < INF) dp[1][tmp][a + b + 1] = min(dp[1][tmp][a + b + 1], min(dp[0][p1][a], dp[1][p1][a])); if (dp[1][p2][b] < 0) dp[1][tmp][a + b + 1] = min(dp[1][tmp][a + b + 1], min(dp[0][p1][a], dp[1][p1][a])); } } last_state[tmp] = last_state[p1] + last_state[p2] + 1; } void dfs(int v, int parent) { int merge1 = tmp; dp[0][tmp][0] = 0, dp[1][tmp][0] = 0; tmp++; for (auto u : graph[v]) if (u != parent) { dfs(u, v); int merge2 = last_dinamics[u]; Merge(merge1, merge2); merge1 = tmp; tmp++; } tmp--; for (int i = 0; i < n + 100; i++) { dp[1][tmp][i] = min(dp[1][tmp][i] + a[v], INF); dp[0][tmp][i] = min(dp[0][tmp][i] + a[v], INF); } if (a[v] < 0) { for (int i = 0; i < n + 100; i++) dp[0][tmp][i] = INF; } last_dinamics[v] = tmp; tmp++; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < 2 * n + 100; i++) { for (int j = 0; j < n + 100; j++) { dp[0][i][j] = INF, dp[1][i][j] = INF; } } for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i + 1 < n; i++) { int v, u; cin >> v >> u; v--, u--; graph[v].push_back(u); graph[u].push_back(v); } dfs(0, -1); long long ans = n - 1; for (int i = 0; i < n - 1; i++) { // cout << dp[1][tmp - 1][i] << " " << dp[0][tmp - 1][i] << "\n"; if (dp[1][tmp - 1][i] < 0) ans = min(ans, i); if (dp[0][tmp - 1][i] < INF) ans = min(ans, i); } cout << ans; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using std::cerr; using std::cin; using std::cout; using std::endl; using std::bitset; using std::deque; using std::iterator; using std::map; using std::multimap; using std::multiset; using std::pair; using std::queue; using std::set; using std::stack; using std::string; using std::vector; using std::fill; using std::ios_base; using std::max_element; using std::min_element; using std::reverse; using std::sort; using std::stable_sort; using std::swap; using std::unique; using std::fixed; using std::setprecision; long long min(long long a, long long b) { return a < b ? a : b; } long long min(int a, long long b) { return a < b ? a : b; } long long min(long long a, int b) { return a < b ? a : b; } long long min(int a, int b) { return a < b ? a : b; } long long max(long long a, long long b) { return a > b ? a : b; } long long max(int a, long long b) { return a > b ? a : b; } long long max(long long a, int b) { return a > b ? a : b; } long long max(int a, int b) { return a > b ? a : b; } #define int long long typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<long long> vl; typedef string S; #define F(i, n) for (int(i) = 0; (i) != (n); (i)++) #define fi first #define se second #define re return #define all(x) (x).begin(), (x).end() const long long INF = 1e18; const int N = 10110; const int MOD = 1e9 + 7; const double eps = 1e-8; int n; int a[N]; vector<int> graph[N]; int dp[2][N][N / 2]; int last_dinamics[N]; int last_state[N]; int tmp = 0; void Merge(int p1, int p2) { for (int a = 0; a <= last_state[p1]; a++) { for (int b = 0; b <= last_state[p2]; b++) { dp[0][tmp][a + b] = min(dp[0][tmp][a + b], dp[0][p1][a] + dp[0][p2][b]); if (dp[0][p2][b] < INF) dp[0][tmp][a + b + 1] = min(dp[0][tmp][a + b + 1], dp[0][p1][a]); if (dp[1][p2][b] < 0) dp[0][tmp][a + b + 1] = min(dp[0][tmp][a + b + 1], dp[0][p1][a]); dp[1][tmp][a + b] = min(dp[1][tmp][a + b], min(dp[0][p1][a], dp[1][p1][a]) + min(dp[0][p2][b], dp[1][p2][b])); if (dp[0][p2][b] < INF) dp[1][tmp][a + b + 1] = min(dp[1][tmp][a + b + 1], min(dp[0][p1][a], dp[1][p1][a])); if (dp[1][p2][b] < 0) dp[1][tmp][a + b + 1] = min(dp[1][tmp][a + b + 1], min(dp[0][p1][a], dp[1][p1][a])); } } last_state[tmp] = last_state[p1] + last_state[p2] + 1; } void dfs(int v, int parent) { int merge1 = tmp; dp[0][tmp][0] = 0, dp[1][tmp][0] = 0; tmp++; for (auto u : graph[v]) if (u != parent) { dfs(u, v); int merge2 = last_dinamics[u]; Merge(merge1, merge2); merge1 = tmp; tmp++; } tmp--; for (int i = 0; i < n + 100; i++) { dp[1][tmp][i] = min(dp[1][tmp][i] + a[v], INF); dp[0][tmp][i] = min(dp[0][tmp][i] + a[v], INF); } if (a[v] < 0) { for (int i = 0; i < n + 100; i++) dp[0][tmp][i] = INF; } last_dinamics[v] = tmp; tmp++; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < 2 * n + 100; i++) { for (int j = 0; j < n + 100; j++) { dp[0][i][j] = INF, dp[1][i][j] = INF; } } for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i + 1 < n; i++) { int v, u; cin >> v >> u; v--, u--; graph[v].push_back(u); graph[u].push_back(v); } dfs(0, -1); long long ans = n - 1; for (int i = 0; i < n - 1; i++) { // cout << dp[1][tmp - 1][i] << " " << dp[0][tmp - 1][i] << "\n"; if (dp[1][tmp - 1][i] < 0) ans = min(ans, i); if (dp[0][tmp - 1][i] < INF) ans = min(ans, i); } cout << ans; }
replace
76
77
76
77
-11
p03160
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; int *h; int *dp; bool *v; int solve(int n, const int &max) { if (n == max - 1) return 0; if (n == max) { return 1e9; } if (v[n]) return dp[n]; int cost = dp[n] = min(solve(n + 1, max) + abs(h[n] - h[n + 1]), solve(n + 2, max) + abs(h[n] - h[n + 2])); return cost; } int main() { int N; cin >> N; h = new int[N]; dp = new int[N]; v = new bool[N]; fill(dp, dp + N, 0); fill(v, v + N, 0); for (int i = 0; i < N; i++) { cin >> h[i]; } cout << solve(0, N) << endl; return 0; }
#include "bits/stdc++.h" using namespace std; int *h; int *dp; bool *v; int solve(int n, const int &max) { if (n == max - 1) return 0; if (n == max) { return 1e9; } if (v[n]) return dp[n]; int cost = dp[n] = min(solve(n + 1, max) + abs(h[n] - h[n + 1]), solve(n + 2, max) + abs(h[n] - h[n + 2])); v[n] = 1; return cost; } int main() { int N; cin >> N; h = new int[N]; dp = new int[N]; v = new bool[N]; fill(dp, dp + N, 0); fill(v, v + N, 0); for (int i = 0; i < N; i++) { cin >> h[i]; } cout << solve(0, N) << endl; return 0; }
insert
19
19
19
20
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define frst first #define sec second #define fast cin.tie(0), ios_base ::sync_with_stdio(0) #define db double #define mod 1000000007 void online_judge() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } ll lpp(string s) { string rev = s; reverse(rev.begin(), rev.end()); s = s + '#' + rev; ////cout << s << " "; vector<ll> z(s.size(), 0); ll l = 0, r = 0; for (int i = 1; i < s.size(); ++i) { if (i > r) { l = r = i; while (s[r] == s[r - l]) { r++; } z[i] = (r - l); r--; } else { if (z[i - l] + i <= r) z[i] = z[i - l]; else { l = i; while (s[r] == s[r - l]) r++; z[i] = (r - l); r--; } } } ll ans = 0; for (int i = 0; i < s.size(); ++i) { if (z[i] + i == s.size()) { ans = max(ans, z[i]); } } return ans; } string binary(ll n) { string s; while (n) { if ((n & 1)) s += '1'; else s += '0'; n >>= 1; } reverse(s.begin(), s.end()); return s; } bool pos(vector<ll> &v, ll val, ll painters) { ll cur = 0; ll k = 1; for (int i = 0; i < v.size(); ++i) { if (v[i] > val) return 0; if (cur + v[i] <= val) cur += v[i]; else { k++; cur = v[i]; } } cout << k << endl; return (k <= painters); } class node { node *next; node *random; ll val; node(ll x) { next = random = NULL; val = x; } }; ll dp[101][101][1001]; ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) { if (n == 0) { /// cout << cur << " " << sum << " " << c << endl; if (cur == tot / 2) return abs((sum - c) - c); return 1e8; } if (dp[n][cur][c] != 1e8) { /// cout << dp[n][cur][c] << " "; return dp[n][cur][c]; } /// cout << dp[n][cur][c] << " "; dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot); dp[n][cur][c] = min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot)); // cout << ans << endl; return dp[n][cur][c]; } void go() { ll n; cin >> n; ll a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; ll dp[n]; // memset(dp , 0, sizeof dp); for (int i = 0; i < n; ++i) dp[i] = 1e8; dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), (i >= 2) ? dp[i - 2] + abs(a[i - 2] - a[i]) : (ll)1e8); } cout << dp[n - 1]; } int main() { fast; online_judge(); go(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define frst first #define sec second #define fast cin.tie(0), ios_base ::sync_with_stdio(0) #define db double #define mod 1000000007 void online_judge() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } ll lpp(string s) { string rev = s; reverse(rev.begin(), rev.end()); s = s + '#' + rev; ////cout << s << " "; vector<ll> z(s.size(), 0); ll l = 0, r = 0; for (int i = 1; i < s.size(); ++i) { if (i > r) { l = r = i; while (s[r] == s[r - l]) { r++; } z[i] = (r - l); r--; } else { if (z[i - l] + i <= r) z[i] = z[i - l]; else { l = i; while (s[r] == s[r - l]) r++; z[i] = (r - l); r--; } } } ll ans = 0; for (int i = 0; i < s.size(); ++i) { if (z[i] + i == s.size()) { ans = max(ans, z[i]); } } return ans; } string binary(ll n) { string s; while (n) { if ((n & 1)) s += '1'; else s += '0'; n >>= 1; } reverse(s.begin(), s.end()); return s; } bool pos(vector<ll> &v, ll val, ll painters) { ll cur = 0; ll k = 1; for (int i = 0; i < v.size(); ++i) { if (v[i] > val) return 0; if (cur + v[i] <= val) cur += v[i]; else { k++; cur = v[i]; } } cout << k << endl; return (k <= painters); } class node { node *next; node *random; ll val; node(ll x) { next = random = NULL; val = x; } }; ll dp[101][101][1001]; ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) { if (n == 0) { /// cout << cur << " " << sum << " " << c << endl; if (cur == tot / 2) return abs((sum - c) - c); return 1e8; } if (dp[n][cur][c] != 1e8) { /// cout << dp[n][cur][c] << " "; return dp[n][cur][c]; } /// cout << dp[n][cur][c] << " "; dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot); dp[n][cur][c] = min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot)); // cout << ans << endl; return dp[n][cur][c]; } void go() { ll n; cin >> n; ll a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; ll dp[n]; // memset(dp , 0, sizeof dp); for (int i = 0; i < n; ++i) dp[i] = 1e8; dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), (i >= 2) ? dp[i - 2] + abs(a[i - 2] - a[i]) : (ll)1e8); } cout << dp[n - 1]; } int main() { fast; // online_judge(); go(); }
replace
133
134
133
134
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define frst first #define sec second #define fast cin.tie(0), ios_base ::sync_with_stdio(0) #define db double #define mod 1000000007 void online_judge() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } ll lpp(string s) { string rev = s; reverse(rev.begin(), rev.end()); s = s + '#' + rev; ////cout << s << " "; vector<ll> z(s.size(), 0); ll l = 0, r = 0; for (int i = 1; i < s.size(); ++i) { if (i > r) { l = r = i; while (s[r] == s[r - l]) { r++; } z[i] = (r - l); r--; } else { if (z[i - l] + i <= r) z[i] = z[i - l]; else { l = i; while (s[r] == s[r - l]) r++; z[i] = (r - l); r--; } } } ll ans = 0; for (int i = 0; i < s.size(); ++i) { if (z[i] + i == s.size()) { ans = max(ans, z[i]); } } return ans; } string binary(ll n) { string s; while (n) { if ((n & 1)) s += '1'; else s += '0'; n >>= 1; } reverse(s.begin(), s.end()); return s; } bool pos(vector<ll> &v, ll val, ll painters) { ll cur = 0; ll k = 1; for (int i = 0; i < v.size(); ++i) { if (v[i] > val) return 0; if (cur + v[i] <= val) cur += v[i]; else { k++; cur = v[i]; } } cout << k << endl; return (k <= painters); } class node { node *next; node *random; ll val; node(ll x) { next = random = NULL; val = x; } }; ll dp[101][101][1001]; ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) { if (n == 0) { /// cout << cur << " " << sum << " " << c << endl; if (cur == tot / 2) return abs((sum - c) - c); return 1e8; } if (dp[n][cur][c] != 1e8) { /// cout << dp[n][cur][c] << " "; return dp[n][cur][c]; } /// cout << dp[n][cur][c] << " "; dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot); dp[n][cur][c] = min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot)); // cout << ans << endl; return dp[n][cur][c]; } void go() { ll n; cin >> n; ll a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; ll dp[3]; // memset(dp , 0, sizeof dp); for (int i = 0; i < 3; ++i) dp[i] = 1e8; dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i % 3] = min(dp[(i - 1 + 3) % 3] + abs(a[i] - a[i - 1]), (i >= 2) ? dp[(i - 2 + 3) % 3] + abs(a[i - 2] - a[i]) : (ll)1e8); } cout << dp[(n - 1) % 3]; } int main() { fast; online_judge(); go(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define frst first #define sec second #define fast cin.tie(0), ios_base ::sync_with_stdio(0) #define db double #define mod 1000000007 void online_judge() { #ifndef ONLINE_JUDGE freopen("Input.txt", "r", stdin); freopen("Output.txt", "w", stdout); #endif } ll lpp(string s) { string rev = s; reverse(rev.begin(), rev.end()); s = s + '#' + rev; ////cout << s << " "; vector<ll> z(s.size(), 0); ll l = 0, r = 0; for (int i = 1; i < s.size(); ++i) { if (i > r) { l = r = i; while (s[r] == s[r - l]) { r++; } z[i] = (r - l); r--; } else { if (z[i - l] + i <= r) z[i] = z[i - l]; else { l = i; while (s[r] == s[r - l]) r++; z[i] = (r - l); r--; } } } ll ans = 0; for (int i = 0; i < s.size(); ++i) { if (z[i] + i == s.size()) { ans = max(ans, z[i]); } } return ans; } string binary(ll n) { string s; while (n) { if ((n & 1)) s += '1'; else s += '0'; n >>= 1; } reverse(s.begin(), s.end()); return s; } bool pos(vector<ll> &v, ll val, ll painters) { ll cur = 0; ll k = 1; for (int i = 0; i < v.size(); ++i) { if (v[i] > val) return 0; if (cur + v[i] <= val) cur += v[i]; else { k++; cur = v[i]; } } cout << k << endl; return (k <= painters); } class node { node *next; node *random; ll val; node(ll x) { next = random = NULL; val = x; } }; ll dp[101][101][1001]; ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) { if (n == 0) { /// cout << cur << " " << sum << " " << c << endl; if (cur == tot / 2) return abs((sum - c) - c); return 1e8; } if (dp[n][cur][c] != 1e8) { /// cout << dp[n][cur][c] << " "; return dp[n][cur][c]; } /// cout << dp[n][cur][c] << " "; dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot); dp[n][cur][c] = min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot)); // cout << ans << endl; return dp[n][cur][c]; } void go() { ll n; cin >> n; ll a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; ll dp[3]; // memset(dp , 0, sizeof dp); for (int i = 0; i < 3; ++i) dp[i] = 1e8; dp[0] = 0; for (int i = 1; i < n; ++i) { dp[i % 3] = min(dp[(i - 1 + 3) % 3] + abs(a[i] - a[i - 1]), (i >= 2) ? dp[(i - 2 + 3) % 3] + abs(a[i - 2] - a[i]) : (ll)1e8); } cout << dp[(n - 1) % 3]; } int main() { fast; // online_judge(); go(); }
replace
134
135
134
135
-11
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; vector<int64_t> dp(100010, INT64_MAX); int64_t dfs(vector<int> &arr, int i) { if (dp[i] != INT64_MAX) { return dp[i]; } if (i <= 0) { return 0; } // // i - 1 と、i - 2 への遷移を再帰的に集めてきて // それの min を返り値とする // // これは、同じ i であれば、返り値が同じになる // ことがメモ化の鍵となる。 // // i - 1 からジャンプ // abs(arr[i] - arr[i - 1]) だけコストが増えていく int64_t result = INT64_MAX; result = min(result, dfs(arr, i - 1) + abs(arr[i] - arr[i - 1])); // i - 2 からジャンプ if (i > 1) { result = min(result, dfs(arr, i - 2) + abs(arr[i] - arr[i - 2])); } return result; } int main(int argc, char *argv[]) { int N; cin >> N; vector<int> arr; int cur; for (int i = 0; i < N; i++) { cin >> cur; arr.push_back(cur); } cout << dfs(arr, N - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int64_t> dp(100010, INT64_MAX); int64_t dfs(vector<int> &arr, int i) { if (dp[i] != INT64_MAX) { return dp[i]; } if (i <= 0) { return 0; } // // i - 1 と、i - 2 への遷移を再帰的に集めてきて // それの min を返り値とする // // これは、同じ i であれば、返り値が同じになる // ことがメモ化の鍵となる。 // // i - 1 からジャンプ // abs(arr[i] - arr[i - 1]) だけコストが増えていく int64_t result = INT64_MAX; result = min(result, dfs(arr, i - 1) + abs(arr[i] - arr[i - 1])); // i - 2 からジャンプ if (i > 1) { result = min(result, dfs(arr, i - 2) + abs(arr[i] - arr[i - 2])); } dp[i] = result; return result; } int main(int argc, char *argv[]) { int N; cin >> N; vector<int> arr; int cur; for (int i = 0; i < N; i++) { cin >> cur; arr.push_back(cur); } cout << dfs(arr, N - 1) << endl; return 0; }
insert
33
33
33
34
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef int ll; typedef vector<ll> vl; typedef vector<vl> vv; #define pb push_back #define MOD 1000000007 #define INF 1000000000 #define endl '\n' void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fast(); #ifndef ONLINE_JUDGE freopen("F:\\Codes\\input.txt", "r", stdin); freopen("F:\\Codes\\output.txt", "w", stdout); #endif ll n, i; cin >> n; vl hgt(n), dp(n); for (i = 0; i < n; i++) { cin >> hgt[i]; } dp[1] = abs(hgt[1] - hgt[0]); for (i = 2; i < n; i++) { dp[i] = min((dp[i - 1] + abs(hgt[i] - hgt[i - 1])), (dp[i - 2] + abs(hgt[i] - hgt[i - 2]))); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef int ll; typedef vector<ll> vl; typedef vector<vl> vv; #define pb push_back #define MOD 1000000007 #define INF 1000000000 #define endl '\n' void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fast(); // #ifndef ONLINE_JUDGE // freopen("F:\\Codes\\input.txt", "r", stdin); // freopen("F:\\Codes\\output.txt", "w", stdout); // #endif ll n, i; cin >> n; vl hgt(n), dp(n); for (i = 0; i < n; i++) { cin >> hgt[i]; } dp[1] = abs(hgt[1] - hgt[0]); for (i = 2; i < n; i++) { dp[i] = min((dp[i - 1] + abs(hgt[i] - hgt[i - 1])), (dp[i - 2] + abs(hgt[i] - hgt[i - 2]))); } cout << dp[n - 1] << endl; return 0; }
replace
18
22
18
22
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define erep(i, n) for (ll i = 1; i <= (n)i++) #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) using namespace std; using ll = long long; const ll INF = 1LL << 60; // input ll h[10010]; // dp tabel ll dp[100010]; int main(void) { ll n; cin >> n; rep(i, n) cin >> h[i]; rep(i, 100010) dp[i] = 1e9; dp[0] = 0; rep(i, n) { mins(dp[i + 1], dp[i] + abs(h[i + 1] - h[i])); mins(dp[i + 2], dp[i] + abs(h[i + 2] - h[i])); } cout << dp[n - 1] << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); i++) #define erep(i, n) for (ll i = 1; i <= (n)i++) #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) using namespace std; using ll = long long; const ll INF = 1LL << 60; // input ll h[100010]; // dp tabel ll dp[100010]; int main(void) { ll n; cin >> n; rep(i, n) cin >> h[i]; rep(i, 100010) dp[i] = 1e9; dp[0] = 0; rep(i, n) { mins(dp[i + 1], dp[i] + abs(h[i + 1] - h[i])); mins(dp[i + 2], dp[i] + abs(h[i + 2] - h[i])); } cout << dp[n - 1] << '\n'; return 0; }
replace
9
10
9
10
0
p03160
C++
Runtime Error
////////////////////////////////////// //// ABHINASH GIRI //// ///////////////////////////////////// #include <bits/stdc++.h> using namespace std; #define pr 4294967296 // 2^32 #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define high 1000000001 #define F first #define S second #define PB push_back #define MP make_pair #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf -1e18 #define pinf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define e 1000000000 #define w(x) \ int t; \ cin >> t; \ while (t--) #define rep(i, a, b) for (int i = a; i <= b; i++) #define co(x) cout << (x) << "\n" #define endl "\n" typedef unsigned long int ul; typedef unsigned long long int ull; typedef unsigned long long int li; typedef vector<int> vi; typedef vector<long long int> vll; typedef vector<long int> vl; typedef vector<string> vs; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpi; typedef vector<pair<long int, long int>> vpl; typedef vector<pair<long long int, long long int>> vpll; typedef long long ll; bool cmp(pair<char, int> a, pair<char, int> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } ll power(ll a, ll b) { if (b == 0) return 1; if (b == 1) return a; else { ll res = power(a, b / 2); if (b % 2) { return (res * res * (a)); } else { return (res * res); } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll cost(vl &v, int n, vll dp) { // cout<< " solving for n="<<n<<endl; if (n == 1) { return 0; } if (n == 2) { return abs(v[2] - v[1]); } ll a, b; if (!dp[n - 1]) { a = cost(v, n - 1, dp); dp[n - 1] = a; } else a = dp[n - 1]; if (!dp[n - 2]) { b = cost(v, n - 2, dp); dp[n - 2] = b; } else b = dp[n - 2]; return min(a + abs(v[n] - v[n - 1]), b + abs(v[n] - v[n - 2])); } int main() { std::chrono::time_point<std::chrono::high_resolution_clock> start_timer, end_timer; start_timer = std::chrono::high_resolution_clock::now(); bool show_time = false; // change it to true when needed; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif /* Cook your Code */ fast; int n; cin >> n; vl v(n + 1, 0); v[0] = 0; for (int i = 1; i <= n; ++i) { cin >> v[i]; } vll dp(n + 1, 0); cout << cost(v, n, dp) << endl; end_timer = std::chrono::high_resolution_clock::now(); ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>( end_timer - start_timer) .count(); if (show_time) { cout << "\\nElapsed Time: " << elapsed_time << "ms\\n"; } return 0; }
////////////////////////////////////// //// ABHINASH GIRI //// ///////////////////////////////////// #include <bits/stdc++.h> using namespace std; #define pr 4294967296 // 2^32 #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define high 1000000001 #define F first #define S second #define PB push_back #define MP make_pair #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf -1e18 #define pinf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define e 1000000000 #define w(x) \ int t; \ cin >> t; \ while (t--) #define rep(i, a, b) for (int i = a; i <= b; i++) #define co(x) cout << (x) << "\n" #define endl "\n" typedef unsigned long int ul; typedef unsigned long long int ull; typedef unsigned long long int li; typedef vector<int> vi; typedef vector<long long int> vll; typedef vector<long int> vl; typedef vector<string> vs; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpi; typedef vector<pair<long int, long int>> vpl; typedef vector<pair<long long int, long long int>> vpll; typedef long long ll; bool cmp(pair<char, int> a, pair<char, int> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } ll power(ll a, ll b) { if (b == 0) return 1; if (b == 1) return a; else { ll res = power(a, b / 2); if (b % 2) { return (res * res * (a)); } else { return (res * res); } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll cost(vl &v, int n, vll dp) { // cout<< " solving for n="<<n<<endl; if (n == 1) { return 0; } if (n == 2) { return abs(v[2] - v[1]); } ll a, b; if (!dp[n - 1]) { a = cost(v, n - 1, dp); dp[n - 1] = a; } else a = dp[n - 1]; if (!dp[n - 2]) { b = cost(v, n - 2, dp); dp[n - 2] = b; } else b = dp[n - 2]; return min(a + abs(v[n] - v[n - 1]), b + abs(v[n] - v[n - 2])); } int main() { std::chrono::time_point<std::chrono::high_resolution_clock> start_timer, end_timer; start_timer = std::chrono::high_resolution_clock::now(); bool show_time = false; // change it to true when needed; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif /* Cook your Code */ fast; int n; cin >> n; vl v(n + 1, 0); v[0] = 0; for (int i = 1; i <= n; ++i) { cin >> v[i]; } vll dp(n + 1, 0); dp[2] = abs(v[2] - v[1]); for (int j = 3; j <= n; ++j) { dp[j] = min(dp[j - 1] + abs(v[j] - v[j - 1]), dp[j - 2] + abs(v[j] - v[j - 2])); } cout << dp[n] << endl; end_timer = std::chrono::high_resolution_clock::now(); ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>( end_timer - start_timer) .count(); if (show_time) { cout << "\\nElapsed Time: " << elapsed_time << "ms\\n"; } return 0; }
replace
109
111
109
115
0
p03160
C++
Runtime Error
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; int main() { quick; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; int h[n]; fab(0, n, i) cin >> h[i]; vector<int> dp(n); dp[n - 1] = 0; dp[n - 2] = abs(h[n - 1] - h[n - 2]); for (int i = n - 3; i >= 0; i--) { dp[i] = min(dp[i + 1] + abs(h[i + 1] - h[i]), dp[i + 2] + abs(h[i + 2] - h[i])); } cout << dp[0] << endl; return 0; }
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; int main() { quick; int n; cin >> n; int h[n]; fab(0, n, i) cin >> h[i]; vector<int> dp(n); dp[n - 1] = 0; dp[n - 2] = abs(h[n - 1] - h[n - 2]); for (int i = n - 3; i >= 0; i--) { dp[i] = min(dp[i + 1] + abs(h[i + 1] - h[i]), dp[i + 2] + abs(h[i + 2] - h[i])); } cout << dp[0] << endl; return 0; }
replace
19
23
19
20
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define F(i, a, b) for (ll i = a; i < b; i++) #define RF(i, a, b) for (ll i = a; i > b; i--) #define pii pair<ll, ll> #define ff first #define ss second #define pb(x) push_back(x) #define pob pop_back #define mp(x, y) make_pair(x, y) #define mod 1000000007 #define w(t) \ int t; \ cin >> t; \ while (t--) using namespace std; const int inf = (int)1e9; char flip_bits(char c) { return (c == '0') ? '1' : '0'; } int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } int main() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ll a[100000]; ll dp[100000]; ll n; cin >> n; for (ll i = 0; i < n; i++) { cin >> a[i]; } ll i, j, prev; dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (i = 2; i < n; i++) { dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])), (dp[i - 2] + abs(a[i] - a[i - 2]))); } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> #define ll long long #define F(i, a, b) for (ll i = a; i < b; i++) #define RF(i, a, b) for (ll i = a; i > b; i--) #define pii pair<ll, ll> #define ff first #define ss second #define pb(x) push_back(x) #define pob pop_back #define mp(x, y) make_pair(x, y) #define mod 1000000007 #define w(t) \ int t; \ cin >> t; \ while (t--) using namespace std; const int inf = (int)1e9; char flip_bits(char c) { return (c == '0') ? '1' : '0'; } int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll a[100000]; ll dp[100000]; ll n; cin >> n; for (ll i = 0; i < n; i++) { cin >> a[i]; } ll i, j, prev; dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (i = 2; i < n; i++) { dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])), (dp[i - 2] + abs(a[i] - a[i - 2]))); } cout << dp[n - 1]; return 0; }
delete
36
41
36
36
0
p03160
C++
Runtime Error
#include <iostream> #define ll long long using namespace std; ll int min(ll int a, ll int b) { if (a > b) return b; return a; } ll int abs(ll int a) { if (a > 0) return a; return -a; } ll int x[10000]; int main() { int a; cin >> a; int b[a + 6]; for (int i = 0; i < a; i++) cin >> b[i]; x[1] = abs(b[1] - b[0]); for (int i = 2; i < a; i++) x[i] = min(x[i - 2] + abs(b[i] - b[i - 2]), x[i - 1] + abs(b[i] - b[i - 1])); cout << x[a - 1]; }
#include <iostream> #define ll long long using namespace std; ll int min(ll int a, ll int b) { if (a > b) return b; return a; } ll int abs(ll int a) { if (a > 0) return a; return -a; } ll int x[200000]; int main() { int a; cin >> a; int b[a + 6]; for (int i = 0; i < a; i++) cin >> b[i]; x[1] = abs(b[1] - b[0]); for (int i = 2; i < a; i++) x[i] = min(x[i - 2] + abs(b[i] - b[i - 2]), x[i - 1] + abs(b[i] - b[i - 1])); cout << x[a - 1]; }
replace
13
14
13
14
0
p03160
C++
Runtime Error
// DP1 #include <bits/stdc++.h> #include <cfloat> #include <climits> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <stdio.h> #include <string> using namespace std; int main() { int n; cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h.at(i); } vector<int> cost(n); cost.at(0) = 0; cost.at(1) = abs(h.at(1) - h.at(0)); for (int i = 0; i < n; i++) { cost.at(i) = min(abs(h.at(i) - h.at(i - 1)) + abs(h.at(i - 1) - h.at(i - 2)) + cost.at(i - 2), abs(h.at(i) - h.at(i - 2)) + cost.at(i - 1)); } cout << cost.at(n - 1) << endl; return 0; }
// DP1 #include <bits/stdc++.h> #include <cfloat> #include <climits> #include <iomanip> #include <iostream> #include <math.h> #include <numeric> #include <stdio.h> #include <string> using namespace std; int main() { int n; cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) { cin >> h.at(i); } vector<int> cost(n); cost.at(0) = 0; cost.at(1) = abs(h.at(0) - h.at(1)); for (int i = 2; i < n; i++) { cost.at(i) = min(cost.at(i - 2) + abs(h.at(i) - h.at(i - 2)), cost.at(i - 1) + abs(h.at(i) - h.at(i - 1))); } cout << cost.at(n - 1) << endl; return 0; }
replace
21
26
21
25
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 18446744073709551614) >= this->size() (which is 4)
p03160
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> // #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; int main() { int n = 0; cin >> n; vector<long long> dp(10); vector<long long> h(n); rep(i, n) cin >> h.at(i); rep(i, dp.size()) dp.at(i) = INF; dp.at(0) = 0; for (int i = 1; i < n; i++) { chmin(dp.at(i), dp.at(i - 1) + abs(h.at(i) - h.at(i - 1))); if (i > 1) { chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2])); } } cout << dp[n - 1]; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> // #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; int main() { int n = 0; cin >> n; vector<long long> dp(n); vector<long long> h(n); rep(i, n) cin >> h.at(i); rep(i, dp.size()) dp.at(i) = INF; dp.at(0) = 0; for (int i = 1; i < n; i++) { chmin(dp.at(i), dp.at(i - 1) + abs(h.at(i) - h.at(i - 1))); if (i > 1) { chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2])); } } cout << dp[n - 1]; }
replace
23
24
23
24
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" #define int long long #define watch(x) cout << (#x) << " is " << (x) << endl #define pb push_back const int N = 1e18; int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS; int n; cin >> n; int mincost[n + 1]; int A[n + 1]; int cost1, cost2; for (int i = 1; i <= n; i++) { cin >> A[i]; } mincost[1] = 0; if (n == 1) return cout << 0 << endl, 0; mincost[2] = abs(A[2] - A[1]); for (int i = 3; i <= n; i++) { cost1 = mincost[i - 2] + abs(A[i] - A[i - 2]); cost2 = mincost[i - 1] + abs(A[i] - A[i - 1]); mincost[i] = min(cost1, cost2); } cout << mincost[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" #define int long long #define watch(x) cout << (#x) << " is " << (x) << endl #define pb push_back const int N = 1e18; int32_t main() { IOS; int n; cin >> n; int mincost[n + 1]; int A[n + 1]; int cost1, cost2; for (int i = 1; i <= n; i++) { cin >> A[i]; } mincost[1] = 0; if (n == 1) return cout << 0 << endl, 0; mincost[2] = abs(A[2] - A[1]); for (int i = 3; i <= n; i++) { cost1 = mincost[i - 2] + abs(A[i] - A[i - 2]); cost2 = mincost[i - 1] + abs(A[i] - A[i - 1]); mincost[i] = min(cost1, cost2); } cout << mincost[n] << endl; return 0; }
delete
13
18
13
13
-11
p03160
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> // #include <bits/stdc++.h> #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 const int N = 5e4 + 100; using namespace std; int a[N]; int dp[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } dp[1] = 0; dp[2] = abs(a[1] - a[2]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } // for(int i=1;i<=n;i++) cout<<dp[i]<<" "; // cout<<endl; printf("%d\n", dp[n]); return 0; }
#include <algorithm> #include <cstdio> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string.h> #include <vector> // #include <bits/stdc++.h> #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 const int N = 5e5 + 100; using namespace std; int a[N]; int dp[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } dp[1] = 0; dp[2] = abs(a[1] - a[2]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } // for(int i=1;i<=n;i++) cout<<dp[i]<<" "; // cout<<endl; printf("%d\n", dp[n]); return 0; }
replace
16
17
16
17
0
p03160
C++
Runtime Error
// https://atcoder.jp/contests/dp/tasks/dp_a // 29-02-2020 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; vector<int> h(n), dp(n, 0); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[n - 1] << '\n'; }
// https://atcoder.jp/contests/dp/tasks/dp_a // 29-02-2020 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); // #ifndef ONLINE_JUDGE // freopen ("input.txt", "r", stdin); // freopen ("output.txt", "w", stdout); // #endif int n; cin >> n; vector<int> h(n), dp(n, 0); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[n - 1] << '\n'; }
replace
8
12
8
12
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define lc 2 * v #define rc 2 * v + 1 #define mid (s + e) / 2 #define ll long long #define int long long #define ld long double #define pii pair<int, int> #define pll pair<long long, long long> #define FAST \ ios::sync_with_stdio(false); \ cin.tie(0); // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") const int maxn = 130, N = 1e5 + 5, SQ = 600, base = 1999, mod = 1e9 + 7, INF = 1e18, lg = 17; int n, a[maxn], dp[maxn]; int32_t main() { FAST cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } fill(dp, dp + maxn, INF); dp[0] = 0; for (int i = 0; i < n; ++i) { if (i - 1 >= 0) { dp[i] = min(dp[i], dp[i - 1] + abs(a[i] - a[i - 1])); } if (i - 2 >= 0) { dp[i] = min(dp[i], dp[i - 2] + abs(a[i] - a[i - 2])); } } cout << dp[n - 1] << '\n'; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define lc 2 * v #define rc 2 * v + 1 #define mid (s + e) / 2 #define ll long long #define int long long #define ld long double #define pii pair<int, int> #define pll pair<long long, long long> #define FAST \ ios::sync_with_stdio(false); \ cin.tie(0); // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") const int maxn = 3e5, N = 1e5 + 5, SQ = 600, base = 1999, mod = 1e9 + 7, INF = 1e18, lg = 17; int n, a[maxn], dp[maxn]; int32_t main() { FAST cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } fill(dp, dp + maxn, INF); dp[0] = 0; for (int i = 0; i < n; ++i) { if (i - 1 >= 0) { dp[i] = min(dp[i], dp[i - 1] + abs(a[i] - a[i - 1])); } if (i - 2 >= 0) { dp[i] = min(dp[i], dp[i - 2] + abs(a[i] - a[i - 2])); } } cout << dp[n - 1] << '\n'; }
replace
20
21
20
21
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define rep(n) for (int i = 0; i < n; i++) #define rrep(n) for (int i = n; i >= 0; i--) #define Rep(a, b) for (int i = a; i < b; i++) #define Rrep(a, b) for (int i = a; i >= b; i--) #define gcd(x, y) __gcd(x, y) #define mp make_pair #define fi first #define se second #define fixed(x) fixed << setprecision(x) const ll MOD = (1e9) + 7; const ll mod = 998244353; const ll inf = (1LL << 62); template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); } using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; ll h[100000], dp[100000]; cin >> n; rep(n) { cin >> h[i]; dp[i] = inf; } dp[0] = 0; rep(n) { chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[n - 1] << "\n"; return (0); }
#include <bits/stdc++.h> #define ll long long #define rep(n) for (int i = 0; i < n; i++) #define rrep(n) for (int i = n; i >= 0; i--) #define Rep(a, b) for (int i = a; i < b; i++) #define Rrep(a, b) for (int i = a; i >= b; i--) #define gcd(x, y) __gcd(x, y) #define mp make_pair #define fi first #define se second #define fixed(x) fixed << setprecision(x) const ll MOD = (1e9) + 7; const ll mod = 998244353; const ll inf = (1LL << 62); template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); } using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n; ll h[1000000], dp[1000000]; cin >> n; rep(n) { cin >> h[i]; dp[i] = inf; } dp[0] = 0; rep(n) { chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[n - 1] << "\n"; return (0); }
replace
26
27
26
27
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int dp[1100]; for (int i = 0; i < 110000; i++) dp[i] = 1000000000; int h[N]; for (int i = 0; i < N; i++) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); // cout<<dp[0]<<endl<<dp[1]<<endl; for (int i = 2; i < N; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout<<dp[i]<<endl; } cout << dp[N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int dp[110000]; for (int i = 0; i < 110000; i++) dp[i] = 1000000000; int h[N]; for (int i = 0; i < N; i++) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); // cout<<dp[0]<<endl<<dp[1]<<endl; for (int i = 2; i < N; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout<<dp[i]<<endl; } cout << dp[N - 1] << endl; }
replace
6
7
6
7
-11
p03160
C++
Time Limit Exceeded
#include <iostream> using namespace std; const int mxn = 1e5 + 100; int a[mxn], dp[mxn], n; bool cal[mxn]; int abs(int x) { return (x > 0 ? x : -x); } int dpf(int x) { if (cal[x] == true) return dp[x]; dp[x] = dpf(x - 1) + abs(a[x] - a[x - 1]); if (x > 1) dp[x] = min(dp[x], dpf(x - 2) + abs(a[x] - a[x - 2])); return dp[x]; } int main() { cin >> n; cal[0] = true; for (int i = 0; i < n; i++) cin >> a[i]; cout << dpf(n - 1); return 0; }
#include <iostream> using namespace std; const int mxn = 1e5 + 100; int a[mxn], dp[mxn], n; bool cal[mxn]; int abs(int x) { return (x > 0 ? x : -x); } int dpf(int x) { if (cal[x] == true) return dp[x]; cal[x] = true; dp[x] = dpf(x - 1) + abs(a[x] - a[x - 1]); if (x > 1) dp[x] = min(dp[x], dpf(x - 2) + abs(a[x] - a[x - 2])); return dp[x]; } int main() { cin >> n; cal[0] = true; for (int i = 0; i < n; i++) cin >> a[i]; cout << dpf(n - 1); return 0; }
insert
11
11
11
12
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> /*#include <ext/pb_ds/assoc_container.hpp> // Common file using namespace __gnu_pbds; //policy-based data structures (like set with adttional functionality) some operations //(*name.find_by_order(x) it givesyou value of x index in sorted set and name.find_by_key(no) it gives you the number at index no before sorting) typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds_s; /* OHH..... BHAI MAARO... MUJHE MAARO.... JO MEIN ISS.. MAADHERCHOD CONTEST MEIN AAYA...*/ using namespace std; #define ull unsigned long long #define ld long double #define PI 3.1415926535897932384626433832795 #define substring \ s.substr(form_index, to_which_index) // last index is not included #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define deque \ deque<int> d; // dynamic array with push_front() ans pop_front functionality #define pq priority_queue<int> p; // max_heap stored data in decresing order #define rpq \ priority_queue<int, vector<int>, greater<int>> \ p; // min_heap in increasing order #define decimalupto(no, y) fixed << setprecision(y) << no #define b_to_d(no) \ bitset<32> ans("no"); \ ans.to_ullong(); // when you have to make an array of binary array #define d_to_b(no, upto) \ bitset<upto> ans(no); \ ans.to_string(); // then you use bitset<int> name.And you can use operations // (a&b),(a|b),(a^b) #define inf 1e9 + 5 #define mod 1e9 + 7 // 1e9+7 #define count1(no) \ __builtin_popcountll(no) // tells us the number of setbits means the total no. // of 1 in a binary no. #define count0_front(no) \ __builtin_clz(no) // tells us the number of 0s in a binary no. from begiining #define count0_back(no) \ __builtin_ctz(no) // tells us the number of 0s from the count0_back #define parity(no) \ __builtin_parity( \ no) // tells the number is even or odd by counting the number of 1s #define Erase(i) \ myvector.erase(myvector.begin() + i); // erase a specific element from a // vector #define grp_Erase(i) \ myvector.erase(myvector.begin(), \ myvector.begin() + \ i); // erase the first i elemnts from a vector #define copy array copy(arr1.begin(), arr1.begin() + n, arr2.begin()); #define to_uppercase(s) transform(s.begin(), s.end(), s.begin(), ::toupper); #define to_lowercase(s) transform(s.begin(), s.end(), s.begin(), ::tolower); #define gcd(a, b) __gcd(a, b) #define array_to_string to_string(i) or string.push_back(arr[i] + '0') #define string_to_int s[i] - 'a' ll power(ll x, ll y, ll md = mod) { ll res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x *= x; if (x >= md) x %= md; y >>= 1; } return res; } // #define left_operand(x<<y) is equivalent to (x*2^y) // #define right_operand(x>>y) is equivalent to (x/2^y) // int arr[100][100]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } vector<int> dp(n, inf); int sum = 0; dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j : {i + 1, i + 2}) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(arr[j] - arr[i])); } } } cout << dp[n - 1]; }
#include <bits/stdc++.h> /*#include <ext/pb_ds/assoc_container.hpp> // Common file using namespace __gnu_pbds; //policy-based data structures (like set with adttional functionality) some operations //(*name.find_by_order(x) it givesyou value of x index in sorted set and name.find_by_key(no) it gives you the number at index no before sorting) typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds_s; /* OHH..... BHAI MAARO... MUJHE MAARO.... JO MEIN ISS.. MAADHERCHOD CONTEST MEIN AAYA...*/ using namespace std; #define ull unsigned long long #define ld long double #define PI 3.1415926535897932384626433832795 #define substring \ s.substr(form_index, to_which_index) // last index is not included #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define deque \ deque<int> d; // dynamic array with push_front() ans pop_front functionality #define pq priority_queue<int> p; // max_heap stored data in decresing order #define rpq \ priority_queue<int, vector<int>, greater<int>> \ p; // min_heap in increasing order #define decimalupto(no, y) fixed << setprecision(y) << no #define b_to_d(no) \ bitset<32> ans("no"); \ ans.to_ullong(); // when you have to make an array of binary array #define d_to_b(no, upto) \ bitset<upto> ans(no); \ ans.to_string(); // then you use bitset<int> name.And you can use operations // (a&b),(a|b),(a^b) #define inf 1e9 + 5 #define mod 1e9 + 7 // 1e9+7 #define count1(no) \ __builtin_popcountll(no) // tells us the number of setbits means the total no. // of 1 in a binary no. #define count0_front(no) \ __builtin_clz(no) // tells us the number of 0s in a binary no. from begiining #define count0_back(no) \ __builtin_ctz(no) // tells us the number of 0s from the count0_back #define parity(no) \ __builtin_parity( \ no) // tells the number is even or odd by counting the number of 1s #define Erase(i) \ myvector.erase(myvector.begin() + i); // erase a specific element from a // vector #define grp_Erase(i) \ myvector.erase(myvector.begin(), \ myvector.begin() + \ i); // erase the first i elemnts from a vector #define copy array copy(arr1.begin(), arr1.begin() + n, arr2.begin()); #define to_uppercase(s) transform(s.begin(), s.end(), s.begin(), ::toupper); #define to_lowercase(s) transform(s.begin(), s.end(), s.begin(), ::tolower); #define gcd(a, b) __gcd(a, b) #define array_to_string to_string(i) or string.push_back(arr[i] + '0') #define string_to_int s[i] - 'a' ll power(ll x, ll y, ll md = mod) { ll res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x *= x; if (x >= md) x %= md; y >>= 1; } return res; } // #define left_operand(x<<y) is equivalent to (x*2^y) // #define right_operand(x>>y) is equivalent to (x/2^y) // int arr[100][100]; int main() { fastio int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } vector<int> dp(n, inf); int sum = 0; dp[0] = 0; for (int i = 0; i < n; ++i) { for (int j : {i + 1, i + 2}) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(arr[j] - arr[i])); } } } cout << dp[n - 1]; }
replace
90
94
90
91
-11
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pii; const LL INF = 1e15; LL memo[100005]; LL datas[100005]; int N; LL solve(int ix) { if (ix == N) return 0; if (ix > N) return INF; if (memo[ix] != -1) return memo[ix]; return min(abs(datas[ix] - datas[ix + 1]) + solve(ix + 1), abs(datas[ix] - datas[ix + 2]) + solve(ix + 2)); } int main() { scanf("%d", &N); for (int a = 1; a <= N; a++) { scanf("%lld", &datas[a]); } memset(memo, -1, sizeof(memo)); printf("%lld\n", solve(1)); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pii; const LL INF = 1e15; LL memo[100005]; LL datas[100005]; int N; LL solve(int ix) { if (ix == N) return 0; if (ix > N) return INF; if (memo[ix] != -1) return memo[ix]; return memo[ix] = min(abs(datas[ix] - datas[ix + 1]) + solve(ix + 1), abs(datas[ix] - datas[ix + 2]) + solve(ix + 2)); } int main() { scanf("%d", &N); for (int a = 1; a <= N; a++) { scanf("%lld", &datas[a]); } memset(memo, -1, sizeof(memo)); printf("%lld\n", solve(1)); return 0; }
replace
18
20
18
20
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n; scanf("%d", &n); vector<int> h(n); for (int &x : h) { scanf("%d", &x); } vector<int> ans(n, 0); ans[n - 2] = abs(h[n - 1] - h[n - 2]); ans[n - 3] = abs(h[n - 3] - h[n - 1]); for (int i = n - 4; i >= 0; i--) { ans[i] = min(ans[i + 1] + abs(h[i] - h[i + 1]), ans[i + 2] + abs(h[i] - h[i + 2])); } printf("%d", ans[0]); }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; int main() { int n; scanf("%d", &n); vector<int> h(n); for (int &x : h) { scanf("%d", &x); } vector<int> ans(n, 0); ans[n - 2] = abs(h[n - 1] - h[n - 2]); if (n >= 3) ans[n - 3] = abs(h[n - 3] - h[n - 1]); for (int i = n - 4; i >= 0; i--) { ans[i] = min(ans[i + 1] + abs(h[i] - h[i + 1]), ans[i + 2] + abs(h[i] - h[i + 2])); } printf("%d", ans[0]); }
replace
13
14
13
15
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define FAST_IO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define INF (1e10 + 5) #define F first #define S second #define pb push_back #define MP make_pair #define FRL(i, a, n) for (i = a; i < n; i++) #define FR(i, n) FRL(i, 0, n) #define RFRL(i, n) for (i = n; i >= 0; i--) #define MOD 1000000007 #define fill(a) memset(dp, a, sizeof(dp)); using namespace std; typedef long long int ll; typedef vector<ll> vi; typedef pair<ll, ll> pi; typedef vector<pair<ll, ll>> vpi; typedef set<ll> st; typedef map<ll, ll> mp; typedef unordered_map<ll, ll> ump; template <class T> T minn(T a, T b) { return (a > b) ? b : a; } template <class T> T maxx(T a, T b) { return (a > b) ? a : b; } void FILE_OP() { #ifndef ONLINE_JUDGE freopen("inputf.txt", "r", stdin); freopen("output2.txt", "w", stdout); #endif } ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll n; vi v(100005); ll dp[100005]; /*ll frog(ll i,ll c) { if(i>n) return 0; c=c+minn(frog(i+1,abs(v[i-1]-v[i])),frog(i+2,abs(v[i-2]-v[i]))); return c; }*/ void SOLVE() { ll tt, m, i, j, k, a, b, c, d, l, r; // cin>>tt; tt = 1; fill(0) while (tt--) { cin >> n; FR(i, n) cin >> v[i]; dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (i = 2; i < n; i++) { dp[i] += minn(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2])); } cout << dp[n - 1]; } } int main() { FAST_IO FILE_OP(); SOLVE(); return 0; }
#include <bits/stdc++.h> #define FAST_IO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define INF (1e10 + 5) #define F first #define S second #define pb push_back #define MP make_pair #define FRL(i, a, n) for (i = a; i < n; i++) #define FR(i, n) FRL(i, 0, n) #define RFRL(i, n) for (i = n; i >= 0; i--) #define MOD 1000000007 #define fill(a) memset(dp, a, sizeof(dp)); using namespace std; typedef long long int ll; typedef vector<ll> vi; typedef pair<ll, ll> pi; typedef vector<pair<ll, ll>> vpi; typedef set<ll> st; typedef map<ll, ll> mp; typedef unordered_map<ll, ll> ump; template <class T> T minn(T a, T b) { return (a > b) ? b : a; } template <class T> T maxx(T a, T b) { return (a > b) ? a : b; } void FILE_OP() { #ifndef ONLINE_JUDGE freopen("inputf.txt", "r", stdin); freopen("output2.txt", "w", stdout); #endif } ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll n; vi v(100005); ll dp[100005]; /*ll frog(ll i,ll c) { if(i>n) return 0; c=c+minn(frog(i+1,abs(v[i-1]-v[i])),frog(i+2,abs(v[i-2]-v[i]))); return c; }*/ void SOLVE() { ll tt, m, i, j, k, a, b, c, d, l, r; // cin>>tt; tt = 1; fill(0) while (tt--) { cin >> n; FR(i, n) cin >> v[i]; dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (i = 2; i < n; i++) { dp[i] += minn(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2])); } cout << dp[n - 1]; } } int main() { FAST_IO // FILE_OP(); SOLVE(); return 0; }
replace
81
82
81
82
0
p03160
C++
Runtime Error
#include <cmath> #include <iostream> #include <vector> using namespace std; const int maxN = 1e4 + 5, INF = 1e9 + 5; vector<int> dp(maxN, INF), a(maxN); int rc(int in) { if (dp[in] != INF) return dp[in]; if (in == 0) { return dp[0] = 0; } if (in == 1) { return dp[1] = abs(a[1] - a[0]); } return dp[in] = min(rc(in - 1) + abs(a[in] - a[in - 1]), rc(in - 2) + abs(a[in] - a[in - 2])); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << rc(n - 1); return 0; }
#include <cmath> #include <iostream> #include <vector> using namespace std; const int maxN = 2e5 + 5, INF = 1e9 + 5; vector<int> dp(maxN, INF), a(maxN); int rc(int in) { if (dp[in] != INF) return dp[in]; if (in == 0) { return dp[0] = 0; } if (in == 1) { return dp[1] = abs(a[1] - a[0]); } return dp[in] = min(rc(in - 1) + abs(a[in] - a[in - 1]), rc(in - 2) + abs(a[in] - a[in - 2])); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << rc(n - 1); return 0; }
replace
6
7
6
7
0
p03160
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef pair<ll, P> PP; ll N; vector<ll> h; ll memo[10001]; ll func(ll pos) { if (memo[pos] != -1) return memo[pos]; if (pos == 0) return 0; if (pos == 1) return abs(h[1] - h[0]); memo[pos] = min(func(pos - 1) + abs(h[pos - 1] - h[pos]), func(pos - 2) + abs(h[pos - 2] - h[pos])); return memo[pos]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; h.resize(N); for (ll i = 0; i < N; ++i) cin >> h[i]; for (auto &e : memo) e = -1; cout << func(N - 1) << endl; // system("pause"); }
#include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef pair<ll, P> PP; ll N; vector<ll> h; ll memo[100001]; ll func(ll pos) { if (memo[pos] != -1) return memo[pos]; if (pos == 0) return 0; if (pos == 1) return abs(h[1] - h[0]); memo[pos] = min(func(pos - 1) + abs(h[pos - 1] - h[pos]), func(pos - 2) + abs(h[pos - 2] - h[pos])); return memo[pos]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; h.resize(N); for (ll i = 0; i < N; ++i) cin >> h[i]; for (auto &e : memo) e = -1; cout << func(N - 1) << endl; // system("pause"); }
replace
26
27
26
27
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define Phuong // <3 typedef pair<int, int> ii; const int inf = 1e9 + 1; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif // ONLINE_JUDGE ios_base::sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; vector<int> dp(n, inf); vector<int> h(n); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i])); } cout << dp[n - 1]; return (0); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define Phuong // <3 typedef pair<int, int> ii; const int inf = 1e9 + 1; int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); #endif // ONLINE_JUDGE ios_base::sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; vector<int> dp(n, inf); vector<int> h(n); for (int i = 0; i < n; ++i) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i])); } cout << dp[n - 1]; return (0); }
replace
11
12
11
12
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1000000000 using namespace std; int n, h[100005], mem[100005]; int dp(int i) { if (i == 1) return 0; if (i < 1) return INF; int a1 = dp(i - 1) + abs(h[i - 1] - h[i]); int a2 = dp(i - 2) + abs(h[i - 2] - h[i]); return mem[i] = min(a1, a2); } int main() { cin >> n; memset(mem, -1, sizeof(mem)); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dp(n) << '\n'; return (0); }
#include <bits/stdc++.h> #define INF 1000000000 using namespace std; int n, h[100005], mem[100005]; int dp(int i) { if (i == 1) return 0; if (i < 1) return INF; if (mem[i] != -1) return mem[i]; int a1 = dp(i - 1) + abs(h[i - 1] - h[i]); int a2 = dp(i - 2) + abs(h[i - 2] - h[i]); return mem[i] = min(a1, a2); } int main() { cin >> n; memset(mem, -1, sizeof(mem)); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dp(n) << '\n'; return (0); }
insert
9
9
9
11
TLE
p03160
C++
Runtime Error
// Inbornhandsome #include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define FIO \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout) #define endl '\n' #define int long long const int N = 1e5 + 7; int stones[N], cost[N], n; int find_cost(int stone) { if (stone > n) return 1e9; if (stone == n) return 0; int &ans = cost[stone]; if (ans != -1) return ans; ans = min(abs(stones[stone] - stones[stone + 1]) + find_cost(stone + 1), abs(stones[stone] - stones[stone + 2]) + find_cost(stone + 2)); return ans; } int32_t main() { IOS; FIO; cin >> n; memset(cost, -1, sizeof(cost)); for (int i = 1; i <= n; ++i) cin >> stones[i]; cout << find_cost(1); return 0; }
// Inbornhandsome #include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define FIO \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout) #define endl '\n' #define int long long const int N = 1e5 + 7; int stones[N], cost[N], n; int find_cost(int stone) { if (stone > n) return 1e9; if (stone == n) return 0; int &ans = cost[stone]; if (ans != -1) return ans; ans = min(abs(stones[stone] - stones[stone + 1]) + find_cost(stone + 1), abs(stones[stone] - stones[stone + 2]) + find_cost(stone + 2)); return ans; } int32_t main() { IOS; // FIO; cin >> n; memset(cost, -1, sizeof(cost)); for (int i = 1; i <= n; ++i) cin >> stones[i]; cout << find_cost(1); return 0; }
replace
34
36
34
35
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; long double PI = 3.141592653589793238463; const int N = 1e5 + 5; const int MOD = 1e9 + 7; ll a[100005]; ll dp[100005][4]; int n; int D(int x) { for (int i = x; i < n; i++) { dp[i][0] = (abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1])); dp[i][1] = (abs(a[i] - a[i - 2]) + min(dp[i - 2][0], dp[i - 2][1])); } } int main() { // int t; cin>>t; // while(t--){ cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { dp[i][0] = N * N; dp[i][1] = N * N; } dp[0][0] = 0; dp[1][0] = (abs(a[0] - a[1])); D(2); cout << min(dp[n - 1][0], dp[n - 1][1]); //} return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; long double PI = 3.141592653589793238463; const int N = 1e5 + 5; const int MOD = 1e9 + 7; ll a[100005]; ll dp[100005][4]; int n; void D(int x) { for (int i = x; i < n; i++) { dp[i][0] = (abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1])); dp[i][1] = (abs(a[i] - a[i - 2]) + min(dp[i - 2][0], dp[i - 2][1])); } } int main() { // int t; cin>>t; // while(t--){ cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { dp[i][0] = N * N; dp[i][1] = N * N; } dp[0][0] = 0; dp[1][0] = (abs(a[0] - a[1])); D(2); cout << min(dp[n - 1][0], dp[n - 1][1]); //} return 0; }
replace
11
12
11
12
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define f(n) for (int i = 0; i < n; i++) #define fo(i, n) for (i = 0; i < n; i++) #define Fo(k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define ll long long #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define eb emplace_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define OJ \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; const int mod = 1'000'000'007; const int N = 3e5, M = N; //======================= void solve() { int n, e; cin >> n; vi a, r = {0}; for (int i = 0; i < n; i++) cin >> e, a.emplace_back(e); r.emplace_back(abs(a[0] - a[1])); for (int i = 2; i < n; i++) r.emplace_back( min(r[i - 2] + abs(a[i] - a[i - 2]), r[i - 1] + abs(a[i - 1] - a[i]))); cout << r[n - 1]; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); OJ; // clock_t start,stop; // start=clock(); int t = 1; // cin >> t; while (t--) { solve(); } // stop=clock(); // cout<<endl<<stop-start<<"ms"; return 0; }
#include <bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define f(n) for (int i = 0; i < n; i++) #define fo(i, n) for (i = 0; i < n; i++) #define Fo(k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) #define ll long long #define si(x) scanf("%d", &x) #define sl(x) scanf("%lld", &x) #define ss(s) scanf("%s", s) #define pi(x) printf("%d\n", x) #define pl(x) printf("%lld\n", x) #define ps(s) printf("%s\n", s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define eb emplace_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 #define OJ \ freopen("input.txt", "r", stdin); \ freopen("output.txt", "w", stdout); typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; const int mod = 1'000'000'007; const int N = 3e5, M = N; //======================= void solve() { int n, e; cin >> n; vi a, r = {0}; for (int i = 0; i < n; i++) cin >> e, a.emplace_back(e); r.emplace_back(abs(a[0] - a[1])); for (int i = 2; i < n; i++) r.emplace_back( min(r[i - 2] + abs(a[i] - a[i - 2]), r[i - 1] + abs(a[i - 1] - a[i]))); cout << r[n - 1]; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // OJ; // clock_t start,stop; // start=clock(); int t = 1; // cin >> t; while (t--) { solve(); } // stop=clock(); // cout<<endl<<stop-start<<"ms"; return 0; }
replace
57
58
57
58
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define EACH(i, a, b) for (int i = (a); i <= (b); i++) #define REACH(i, a, b) for (int i = (b); i >= (a); i--) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() const int MOD = 1e9 + 7; const int INF = 1 << 29; const double EPS = 1e-10; using ll = long long; using P = pair<int, int>; template <class T = int> using V = vector<T>; template <class T> bool inline chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool inline chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T = int> T inline input() { T x; cin >> x; return (x); } template <class T> void inline print(T &x) { cout << x << '\n'; } #define debug(x) cerr << #x << ": " << x << endl; int dp[10010]; signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; int h[N]; REP(i, N) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); FOR(i, 2, N) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } print(dp[N - 1]); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define EACH(i, a, b) for (int i = (a); i <= (b); i++) #define REACH(i, a, b) for (int i = (b); i >= (a); i--) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() const int MOD = 1e9 + 7; const int INF = 1 << 29; const double EPS = 1e-10; using ll = long long; using P = pair<int, int>; template <class T = int> using V = vector<T>; template <class T> bool inline chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool inline chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T = int> T inline input() { T x; cin >> x; return (x); } template <class T> void inline print(T &x) { cout << x << '\n'; } #define debug(x) cerr << #x << ": " << x << endl; int dp[100100]; signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; int h[N]; REP(i, N) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); FOR(i, 2, N) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } print(dp[N - 1]); return 0; }
replace
42
43
42
43
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> const int MAX_N = 100005; const int MAX = 2000000005; int N; int mem[MAX_N]; int heights[MAX_N]; int dp(int stone) { if (stone == N) return 0; if (stone > N) return MAX; int best = MAX; if (stone + 1 <= N) best = std::min( dp(stone + 1) + std::abs(heights[stone] - heights[stone + 1]), best); if (stone + 2 <= N) best = std::min( dp(stone + 2) + std::abs(heights[stone] - heights[stone + 2]), best); mem[stone] = best; return best; } int main() { memset(mem, -1, sizeof(mem)); scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", heights + i + 1); } printf("%d\n", dp(1)); }
#include <bits/stdc++.h> const int MAX_N = 100005; const int MAX = 2000000005; int N; int mem[MAX_N]; int heights[MAX_N]; int dp(int stone) { if (stone == N) return 0; if (stone > N) return MAX; if (mem[stone] != -1) return mem[stone]; int best = MAX; if (stone + 1 <= N) best = std::min( dp(stone + 1) + std::abs(heights[stone] - heights[stone + 1]), best); if (stone + 2 <= N) best = std::min( dp(stone + 2) + std::abs(heights[stone] - heights[stone + 2]), best); mem[stone] = best; return best; } int main() { memset(mem, -1, sizeof(mem)); scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", heights + i + 1); } printf("%d\n", dp(1)); }
insert
14
14
14
16
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int getCost(int *arr, int index, vector<int> mem) { if (mem[index] != -1) return mem[index]; int cost = INT_MAX; if (index + 1 < mem.size()) cost = min(cost, abs(arr[index] - arr[index + 1]) + getCost(arr, index + 1, mem)); if (index + 2 < mem.size()) cost = min(cost, abs(arr[index] - arr[index + 2]) + getCost(arr, index + 2, mem)); if (cost == INT_MAX) cost = 0; mem[index] = cost; return cost; } int main() { int N; cin >> N; int *arr = new int[N]; for (int i = 0; i < N; i++) cin >> arr[i]; vector<int> mem(N, -1); int ans = getCost(arr, 0, mem); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int getCost(int *arr, int index, vector<int> &mem) { if (mem[index] != -1) return mem[index]; int cost = INT_MAX; if (index + 1 < mem.size()) cost = min(cost, abs(arr[index] - arr[index + 1]) + getCost(arr, index + 1, mem)); if (index + 2 < mem.size()) cost = min(cost, abs(arr[index] - arr[index + 2]) + getCost(arr, index + 2, mem)); if (cost == INT_MAX) cost = 0; mem[index] = cost; return cost; } int main() { int N; cin >> N; int *arr = new int[N]; for (int i = 0; i < N; i++) cin >> arr[i]; vector<int> mem(N, -1); int ans = getCost(arr, 0, mem); cout << ans << endl; }
replace
2
3
2
3
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define ld long double #define FOR(i, a, n) for (int i = (a); i <= (n); ++i) #define RFOR(i, a, n) for (int i = (n); i >= (a); --i) #define FI(i, n) for (int i = 0; i < (n); ++i) #define ZERO(a) memset((a), 0, sizeof((a))) #define f first // #define s second #define pb push_back #define mk make_pair #define all(g) g.begin(), g.end() int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll, // null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // ordered_set; // I am questioning life and universe and // everything else after looking at this const ll MAXN = 1e5 + 5; ll a[MAXN], dp[MAXN]; ll n; ll rec(ll idx) { if (idx == n) return 0; ll &ans = dp[idx]; if (ans != -1) return ans; ans = 1e10; if (idx + 2 <= n) ans = min(ans, rec(idx + 2) + abs(a[idx + 2] - a[idx])); ans = min(ans, rec(idx + 1) + abs(a[idx + 1] - a[idx])); return ans; } void solve() { cin >> n; FOR(i, 1, n) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll ans = rec(1); cout << ans << endl; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif FastRead; ll t; t = 1; // cin>>t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long int #define ld long double #define FOR(i, a, n) for (int i = (a); i <= (n); ++i) #define RFOR(i, a, n) for (int i = (n); i >= (a); --i) #define FI(i, n) for (int i = 0; i < (n); ++i) #define ZERO(a) memset((a), 0, sizeof((a))) #define f first // #define s second #define pb push_back #define mk make_pair #define all(g) g.begin(), g.end() int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } // #include <ext/pb_ds/assoc_container.hpp> // Common file // #include <ext/pb_ds/tree_policy.hpp> // Including // tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll, // null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // ordered_set; // I am questioning life and universe and // everything else after looking at this const ll MAXN = 1e5 + 5; ll a[MAXN], dp[MAXN]; ll n; ll rec(ll idx) { if (idx == n) return 0; ll &ans = dp[idx]; if (ans != -1) return ans; ans = 1e10; if (idx + 2 <= n) ans = min(ans, rec(idx + 2) + abs(a[idx + 2] - a[idx])); ans = min(ans, rec(idx + 1) + abs(a[idx + 1] - a[idx])); return ans; } void solve() { cin >> n; FOR(i, 1, n) cin >> a[i]; memset(dp, -1, sizeof(dp)); ll ans = rec(1); cout << ans << endl; } signed main() { FastRead; ll t; t = 1; // cin>>t; while (t--) solve(); }
delete
63
68
63
63
0
p03160
C++
Runtime Error
/*** ** AUTHOR::ASHUTOSH MOUDGIL ***/ #include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second // #define int long long #define ll long long #define all(x) x.begin(), x.end() #define endl '\n' #define vi vector<int> #define vlli vector<long long> #define pii pair<int, int> #define mod 1000000007 #define LMAX 1e18 int h[10005]; int dp[10005]; int solve(int n, int i) { if (i == n) return 0; int a = INT_MIN, b = INT_MIN; if (i + 1 <= n) a = solve(n, i + 1); if (i + 2 <= n) b = solve(n, i + 2); return min(a + abs(h[i] - h[i - 1]), b + abs(h[i - 1] - h[i + 1])); } signed main() { IOS; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> h[i]; dp[n] = 0; for (int i = n - 1; i >= 1; i--) { dp[i] = dp[i + 1] + abs(h[i] - h[i + 1]); if (i + 2 <= n) { dp[i] = min(dp[i], dp[i + 2] + abs(h[i] - h[i + 2])); } } cout << dp[1] << endl; // cout << solve(n,1); return 0; }
/*** ** AUTHOR::ASHUTOSH MOUDGIL ***/ #include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second // #define int long long #define ll long long #define all(x) x.begin(), x.end() #define endl '\n' #define vi vector<int> #define vlli vector<long long> #define pii pair<int, int> #define mod 1000000007 #define LMAX 1e18 int h[100005]; int dp[100005]; int solve(int n, int i) { if (i == n) return 0; int a = INT_MIN, b = INT_MIN; if (i + 1 <= n) a = solve(n, i + 1); if (i + 2 <= n) b = solve(n, i + 2); return min(a + abs(h[i] - h[i - 1]), b + abs(h[i - 1] - h[i + 1])); } signed main() { IOS; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> h[i]; dp[n] = 0; for (int i = n - 1; i >= 1; i--) { dp[i] = dp[i + 1] + abs(h[i] - h[i + 1]); if (i + 2 <= n) { dp[i] = min(dp[i], dp[i + 2] + abs(h[i] - h[i + 2])); } } cout << dp[1] << endl; // cout << solve(n,1); return 0; }
replace
24
26
24
26
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long #define dd double #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vll vector<long long> #define vdd vector<double> #define vpii vector<pair<int, int>> #define vpll vector<pair<long long, long long>> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define mii map<int, int> #define mll map<long long, long long> #define umii unordered_map<int, int> #define umll unordered_map<long long, long long> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, b) for (int i = a; i < b; i++) #define rev(i, n) for (int i = n; i >= 0; i--) #define rev2(i, a, b) for (int i = a; i >= b; i--) #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 9e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define all(x) x.begin(), x.end() #define d0(x) cout << x << " " #define d1(x) cout << x << endl #define d2(x, y) cout << x << " " << y << endl #define d3(x, y, z) cout << x << " " << y << " " << z << endl #define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl #define read2(x, y) cin >> x >> y #define read3(x, y, z) cin >> x >> y >> z #define read4(x, y, z, w) cin >> x >> y >> z >> w #define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a #define sz(a) (int)a.size() const ll mod = 1e9 + 7; const double PI = 3.14159265359; const ll MOD = 998244353; void i_o() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int help(vi &A) { int n = sz(A); vi dp(n); dp[0] = 0; rep2(i, 1, n) { int x = INT_MAX; if (i >= 2) x = min(x, dp[i - 2] + abs(A[i] - A[i - 2])); x = min(x, dp[i - 1] + abs(A[i] - A[i - 1])); dp[i] = x; } return dp[n - 1]; } int main() { fio i_o(); int n; cin >> n; vi h(n); rep(i, n) cin >> h[i]; d1(help(h)); return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long #define dd double #define pb push_back #define mp make_pair #define pii pair<int, int> #define vi vector<int> #define vll vector<long long> #define vdd vector<double> #define vpii vector<pair<int, int>> #define vpll vector<pair<long long, long long>> #define vvi vector<vector<int>> #define vvl vector<vector<long long>> #define mii map<int, int> #define mll map<long long, long long> #define umii unordered_map<int, int> #define umll unordered_map<long long, long long> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, a, b) for (int i = a; i < b; i++) #define rev(i, n) for (int i = n; i >= 0; i--) #define rev2(i, a, b) for (int i = a; i >= b; i--) #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define inf 9e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define all(x) x.begin(), x.end() #define d0(x) cout << x << " " #define d1(x) cout << x << endl #define d2(x, y) cout << x << " " << y << endl #define d3(x, y, z) cout << x << " " << y << " " << z << endl #define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl #define read2(x, y) cin >> x >> y #define read3(x, y, z) cin >> x >> y >> z #define read4(x, y, z, w) cin >> x >> y >> z >> w #define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a #define sz(a) (int)a.size() const ll mod = 1e9 + 7; const double PI = 3.14159265359; const ll MOD = 998244353; void i_o() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int help(vi &A) { int n = sz(A); vi dp(n); dp[0] = 0; rep2(i, 1, n) { int x = INT_MAX; if (i >= 2) x = min(x, dp[i - 2] + abs(A[i] - A[i - 2])); x = min(x, dp[i - 1] + abs(A[i] - A[i - 1])); dp[i] = x; } return dp[n - 1]; } int main() { fio // i_o(); int n; cin >> n; vi h(n); rep(i, n) cin >> h[i]; d1(help(h)); return 0; }
replace
76
78
76
79
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
// Created by Tanuj Jain #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> pii; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int recursive_min_cost(vector<int> &v, int end, int &ans) { // cout<<"call:"<<end<<endl; if (end == 0) return 0; if (end < 0) return 1e6; // find left child ans // int ans=0; int left_child = recursive_min_cost(v, end - 1, ans); int right_child = recursive_min_cost(v, end - 2, ans); // find the current answer ans = min(left_child + abs(v[end] - v[end - 1]), right_child + abs(v[end] - v[end - 2])); // cout<<ans<<endl; return ans; } int top_down(vector<int> &v, int end, int &ans, unordered_map<int, int> &memo) { // cout<<end<<endl; if (end == 0) return 0; if (end < 0) return 1e6; // find left child ans // int ans=0; if (memo.find(end) == memo.end()) { int left_child = top_down(v, end - 1, ans, memo); int right_child = top_down(v, end - 2, ans, memo); // find the current answer ans = min(left_child + abs(v[end] - v[end - 1]), right_child + abs(v[end] - v[end - 2])); memo[end] = ans; // cout<<ans<<endl; return ans; } else return memo[end]; } int bottom_up(vector<int> &v) { int n = v.size(); vector<int> dp(n + 1, 1e6); dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2])); } return dp[n - 1]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("inputf.in", "r", stdin); freopen("outputf.in", "w", stdout); #endif int t; cin >> t; vector<int> v(t); for (int i = 0; i < t; i++) cin >> v[i]; // int ans=0; // recursive_min_cost(v,t-1,ans); // top down dp with memoization // unordered_map<int,int>memo; // top_down(v,t-1,ans,memo); // for(auto it:memo) // cout<<it.first<<" "<<it.second<<endl; // cout<<ans; // bottom up dp cout << bottom_up(v) << endl; }
// Created by Tanuj Jain #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int, int> pii; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int recursive_min_cost(vector<int> &v, int end, int &ans) { // cout<<"call:"<<end<<endl; if (end == 0) return 0; if (end < 0) return 1e6; // find left child ans // int ans=0; int left_child = recursive_min_cost(v, end - 1, ans); int right_child = recursive_min_cost(v, end - 2, ans); // find the current answer ans = min(left_child + abs(v[end] - v[end - 1]), right_child + abs(v[end] - v[end - 2])); // cout<<ans<<endl; return ans; } int top_down(vector<int> &v, int end, int &ans, unordered_map<int, int> &memo) { // cout<<end<<endl; if (end == 0) return 0; if (end < 0) return 1e6; // find left child ans // int ans=0; if (memo.find(end) == memo.end()) { int left_child = top_down(v, end - 1, ans, memo); int right_child = top_down(v, end - 2, ans, memo); // find the current answer ans = min(left_child + abs(v[end] - v[end - 1]), right_child + abs(v[end] - v[end - 2])); memo[end] = ans; // cout<<ans<<endl; return ans; } else return memo[end]; } int bottom_up(vector<int> &v) { int n = v.size(); vector<int> dp(n + 1, 1e6); dp[0] = 0; dp[1] = abs(v[1] - v[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2])); } return dp[n - 1]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("inputf.in","r",stdin); // freopen("outputf.in","w",stdout); // #endif int t; cin >> t; vector<int> v(t); for (int i = 0; i < t; i++) cin >> v[i]; // int ans=0; // recursive_min_cost(v,t-1,ans); // top down dp with memoization // unordered_map<int,int>memo; // top_down(v,t-1,ans,memo); // for(auto it:memo) // cout<<it.first<<" "<<it.second<<endl; // cout<<ans; // bottom up dp cout << bottom_up(v) << endl; }
replace
66
70
66
70
-6
terminate called after throwing an instance of 'std::length_error' what(): cannot create std::vector larger than max_size()
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int minCost(int n, int cost[], int dp[]) { if (n == 1) { return 0; } if (n == 2) { return abs(cost[1] - cost[0]); } if (dp[n] != -1) { return dp[n]; } int cost1 = abs(cost[n - 1] - cost[n - 2]) + minCost(n - 1, cost, dp); int cost2 = abs(cost[n - 1] - cost[n - 3]) + minCost(n - 2, cost, dp); dp[n] = min(cost1, cost2); return dp[n]; } int main() { int n; cin >> n; int cost[n]; for (int i = 0; i < n; i++) { cin >> cost[i]; } int dp[10000]; for (int i = 0; i < 10000; i++) { dp[i] = -1; } cout << minCost(n, cost, dp); }
#include <bits/stdc++.h> using namespace std; int minCost(int n, int cost[], int dp[]) { if (n == 1) { return 0; } if (n == 2) { return abs(cost[1] - cost[0]); } if (dp[n] != -1) { return dp[n]; } int cost1 = abs(cost[n - 1] - cost[n - 2]) + minCost(n - 1, cost, dp); int cost2 = abs(cost[n - 1] - cost[n - 3]) + minCost(n - 2, cost, dp); dp[n] = min(cost1, cost2); return dp[n]; } int main() { int n; cin >> n; int cost[n]; for (int i = 0; i < n; i++) { cin >> cost[i]; } int dp[n + 1]; for (int i = 0; i <= n; i++) { dp[i] = -1; } cout << minCost(n, cost, dp); }
replace
26
28
26
28
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define oo int(1e9) using namespace std; int n; int main() { cin.tie(0); ios::sync_with_stdio(0); cout.tie(0); cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; vector<int> dp(n); for (int i = 1; i <= n; i++) dp[i] = oo; for (int i = 0; i < n; i++) if (dp[i] < oo) { if (i + 1 < n) dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); if (i + 2 < n) dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[n - 1]; return 0; }
#include <bits/stdc++.h> #define oo int(1e9) using namespace std; int n; int main() { cin.tie(0); ios::sync_with_stdio(0); cout.tie(0); cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; vector<int> dp(n); for (int i = 1; i < n; i++) dp[i] = oo; for (int i = 0; i < n; i++) if (dp[i] < oo) { if (i + 1 < n) dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); if (i + 2 < n) dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[n - 1]; return 0; }
replace
14
15
14
15
0
p03160
C++
Runtime Error
// I'm a f*cking looser #include <bits/stdc++.h> using namespace std; #define ll long long #define fasino \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define asc(A) sort(A.begin(), A.end()) #define dsc(A) sort(A.begin(), A.end(), greater<ll>()) #define input(A, N) \ for (ll i = 0; i < N; i++) \ cin >> A[i]; int main() { fasino #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n; cin >> n; vector<ll> a(n); input(a, n); int dp[n]; dp[0] = 0; dp[1] = abs(a[1] - a[0]); // Because At step 1 : Only i+1 jump is possible. for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } cout << dp[n - 1]; return 0; }
// I'm a f*cking looser #include <bits/stdc++.h> using namespace std; #define ll long long #define fasino \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define asc(A) sort(A.begin(), A.end()) #define dsc(A) sort(A.begin(), A.end(), greater<ll>()) #define input(A, N) \ for (ll i = 0; i < N; i++) \ cin >> A[i]; int main() { fasino int n; cin >> n; vector<ll> a(n); input(a, n); int dp[n]; dp[0] = 0; dp[1] = abs(a[1] - a[0]); // Because At step 1 : Only i+1 jump is possible. for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } cout << dp[n - 1]; return 0; }
replace
13
18
13
14
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 1000000000000000000 #define fr first #define sc second #define pb push_back #define pf push_front #define rep(i, a, n) for (ll i = a; i < n; i++) #define per(i, a, n) for (ll i = n - 1; i >= a; i--) #define pll pair<ll, ll> #define vll vector<ll> #define umap unordered_map<ll, ll> #define lpq priority_queue<ll, vector<ll>, greater<ll>> ll powe(ll n, ll r) { if (!r) return 1; else if (r % 2 == 0) return powe(n * n, r / 2); else return n * powe(n * n, (r - 1) / 2); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t = 1; // cin>>t; while (t--) { ll n; cin >> n; vector<ll> h(n); vector<ll> dp(n); for (ll i = 0; i < n; i++) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (ll i = 2; i < n; i++) dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); cout << dp[n - 1] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 1000000000000000000 #define fr first #define sc second #define pb push_back #define pf push_front #define rep(i, a, n) for (ll i = a; i < n; i++) #define per(i, a, n) for (ll i = n - 1; i >= a; i--) #define pll pair<ll, ll> #define vll vector<ll> #define umap unordered_map<ll, ll> #define lpq priority_queue<ll, vector<ll>, greater<ll>> ll powe(ll n, ll r) { if (!r) return 1; else if (r % 2 == 0) return powe(n * n, r / 2); else return n * powe(n * n, (r - 1) / 2); } int main() { /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif*/ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t = 1; // cin>>t; while (t--) { ll n; cin >> n; vector<ll> h(n); vector<ll> dp(n); for (ll i = 0; i < n; i++) cin >> h[i]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (ll i = 2; i < n; i++) dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); cout << dp[n - 1] << endl; } return 0; }
replace
26
31
26
31
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define int long long #define rep(i, a, b) for (int i = a; i < b; i++) #define repn(i, a, b) for (int i = a; i >= b; i--) #define F first #define S second #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define pb push_back #define mp make_pair #define all(v) (v).begin(), (v).end() #define mod 1000000007 #define mod2 998244353 int solve() { int n; cin >> n; int a[n], b[n]; rep(i, 0, n) { cin >> a[i]; if (i == 0) b[i] = 0; if (i == 1) b[i] = abs(a[i] - a[i - 1]); if (i > 1) b[i] = min(abs(a[i] - a[i - 1]) + b[i - 1], abs(a[i] - a[i - 2]) + b[i - 2]); } cout << b[n - 1]; return 0; } signed main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output4.txt", "w", stdout); #endif int t = 1; // cin>>t; while (t--) { solve(); cout << "\n"; // cout<<" * "<<t<<"\n"; } // return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define int long long #define rep(i, a, b) for (int i = a; i < b; i++) #define repn(i, a, b) for (int i = a; i >= b; i--) #define F first #define S second #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define pb push_back #define mp make_pair #define all(v) (v).begin(), (v).end() #define mod 1000000007 #define mod2 998244353 int solve() { int n; cin >> n; int a[n], b[n]; rep(i, 0, n) { cin >> a[i]; if (i == 0) b[i] = 0; if (i == 1) b[i] = abs(a[i] - a[i - 1]); if (i > 1) b[i] = min(abs(a[i] - a[i - 1]) + b[i - 1], abs(a[i] - a[i - 2]) + b[i - 2]); } cout << b[n - 1]; return 0; } signed main() { IOS; int t = 1; // cin>>t; while (t--) { solve(); cout << "\n"; // cout<<" * "<<t<<"\n"; } // return 0; }
delete
42
46
42
42
-11
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int64_t min_cost(int step, vector<int> &h, vector<int64_t> &memo) { if (step == 1) return 0; if (step == 2) return abs(h.at(0) - h.at(1)); int elem = step - 1; int64_t near = abs(h.at(elem) - h.at(elem - 1)); if (memo.at(elem - 1) != -1) { near += memo.at(elem - 1); } else { near += min_cost(step - 1, h, memo); } int64_t far = abs(h.at(elem) - h.at(elem - 2)); if (memo.at(elem - 2) != -1) { far += memo.at(elem - 2); } else { far += min_cost(step - 2, h, memo); } return min(near, far); } int main() { int N; cin >> N; vector<int64_t> memo(N, -1); vector<int> h(N); for (int i = 0; i < N; i++) cin >> h.at(i); cout << min_cost(N, h, memo) << endl; }
#include <bits/stdc++.h> using namespace std; int64_t min_cost(int step, vector<int> &h, vector<int64_t> &memo) { if (step == 1) return 0; if (step == 2) return abs(h.at(0) - h.at(1)); int elem = step - 1; int64_t near = abs(h.at(elem) - h.at(elem - 1)); if (memo.at(elem - 1) != -1) { near += memo.at(elem - 1); } else { near += min_cost(step - 1, h, memo); } int64_t far = abs(h.at(elem) - h.at(elem - 2)); if (memo.at(elem - 2) != -1) { far += memo.at(elem - 2); } else { far += min_cost(step - 2, h, memo); } return memo.at(elem) = min(near, far); } int main() { int N; cin >> N; vector<int64_t> memo(N, -1); vector<int> h(N); for (int i = 0; i < N; i++) cin >> h.at(i); cout << min_cost(N, h, memo) << endl; }
replace
22
23
22
23
TLE
p03160
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long int ll; const ll INF = 1000000000; const double PI = acos(-1); const int mod = 1000000007; int dp[100001] = {0}; int main() { int n; cin >> n; vector<int> h(n + 1, 0); for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 2; i <= n; i++) { if (i = 2) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); } else { dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1])); } } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long int ll; const ll INF = 1000000000; const double PI = acos(-1); const int mod = 1000000007; int dp[100001] = {0}; int main() { int n; cin >> n; vector<int> h(n + 1, 0); for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 2; i <= n; i++) { if (i == 2) { dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]); } else { dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1])); } } cout << dp[n] << endl; return 0; }
replace
30
31
30
31
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { int N, dp[10020], h[10020]; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < N; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[N - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N, dp[100020], h[100020]; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < N; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[N - 1] << endl; return 0; }
replace
4
5
4
5
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; void solve() { int n; cin >> n; vector<int> vec(n), dp(n, INT_MAX); for (int i = 0; i < n; ++i) { cin >> vec[i]; } dp[0] = 0, dp[1] = abs(vec[0] - vec[1]); for (int i = 2; i < n; ++i) { dp[i] = min(abs(vec[i] - vec[i - 1]) + dp[i - 1], abs(vec[i] - vec[i - 2]) + dp[i - 2]); } cout << dp[n - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; void solve() { int n; cin >> n; vector<int> vec(n), dp(n, INT_MAX); for (int i = 0; i < n; ++i) { cin >> vec[i]; } dp[0] = 0, dp[1] = abs(vec[0] - vec[1]); for (int i = 2; i < n; ++i) { dp[i] = min(abs(vec[i] - vec[i - 1]) + dp[i - 1], abs(vec[i] - vec[i - 2]) + dp[i - 2]); } cout << dp[n - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
delete
23
27
23
23
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; /* #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") */ //************** StrAnge.R ********************* #define ll long long int #define ull unsigned long long #define ld long double #define lll __int128 #define vi vector<int> #define vl vector<ll> #define vvi vector<vector<int>> #define pii pair<int, int> #define piii pair<int, pair<int, int>> #define pll pair<ll, ll> #define vii vector<pii> #define min_pq priority_queue<int, vector<int>, greater<int>> #define sz(v) ((int)(v).size()) #define all(s) s.begin(), s.end() #define allr(s) s.rbegin(), s.rend() #define unq(c) (sort(all(c)), c.resize(distance(c.begin(), unique(all(c))))) #define get_pos(c, x) (lower_bound(all(c), x) - c.begin()) #define MS0(v) memset((v), 0, sizeof((v))) #define MS1(v) memset((v), -1, sizeof((v))) #define LEN(v) strlen(v) #define MP make_pair #define pb push_back #define pob pop_back #define ff first #define ss second #define sc scanf #define pf printf #define endl "\n" #define intmx INT_MAX #define llmx 1llu << 61 #define PI 3.14159265358979323846264338327950L #define MOD 1000000007 #define MAX 100010 #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a) * ((b) / gcd(a, b))) #define shuffle(v) (random_shuffle(all(v))) #define min_ele(v) (*min_element(all(v))) #define max_ele(v) (*max_element(all(v))) #define is_equal(x, y) (abs(x - y) < eps) #define cnt_ele(v, x) (count(all(v), x)) #define sum_ele(v) (accumulate(all(v), 0)) #define pro_ele(v) (accumulate(all(v), 1, multiplies<int>())) #define init_range(v, x) (iota(all(v), x)) #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a)) #define TEST_CASE \ int ___T; \ cin >> ___T; \ for (int cs = 1; cs <= ___T; cs++) #define PRINT_CASE cout << "Case " << cs << ": "; #define PRINT_CASEN cout << "Case " << cs << ": \n"; #define vpf(v) \ for (int i = 0; i < v.size(); i++) { \ cout << v[i]; \ if (i != v.size() - 1) \ cout << " "; \ else \ cout << "\n"; \ } #define vsc(v) \ for (int i = 0; i < v.size(); i++) \ cin >> v[i]; #define FOR(i, a, b, stp) for (int i = (a); i <= (b); i += stp) #define ROF(i, a, b, stp) for (int i = (a); i >= (b); i -= stp) template <class T> inline bool read(T &x) { int c = getchar(); int sgn = 1; while (~c && c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } inline ll exp(ll a, ll p) { if (p == 0) return 1; ll x = exp(a, p / 2) % MOD; x = (x * x) % MOD; if (p & 1) x = (x * (a % MOD)) % MOD; return x; } inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < MOD) a += MOD; return a; } inline int multi(ll a, ll b) { a *= b; if (a >= MOD) a %= MOD; return a; } inline int on_bit(int N, int pos) { return N = N | (1 << pos); } inline int off_bit(int N, int pos) { return N = N & ~(1 << pos); } inline bool check_bit(ll N, int pos) { return (bool)(N & (1 << pos)); } #define on_bit_cnt(x) (__builtin_popcount(x)) #define start_clock clock_t tStart = clock() #define end_clock \ printf("\n>>Runtime: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC) #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fileio \ freopen("in.txt", "r", stdin); \ freopen("out.txt", "w", stdout) //******************* my code starts here ********************************** int n, ar[100005]; int dp[100005]; int solve(int pos) { if (pos == n) return 0; if (dp[pos] != -1) return dp[pos]; int x = solve(pos + 1) + abs(ar[pos] - ar[pos + 1]); int y = 1e9; if (pos + 2 <= n) y = solve(pos + 2) + abs(ar[pos] - ar[pos + 2]); return min(x, y); } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> ar[i]; MS1(dp); cout << solve(1) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; /* #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") */ //************** StrAnge.R ********************* #define ll long long int #define ull unsigned long long #define ld long double #define lll __int128 #define vi vector<int> #define vl vector<ll> #define vvi vector<vector<int>> #define pii pair<int, int> #define piii pair<int, pair<int, int>> #define pll pair<ll, ll> #define vii vector<pii> #define min_pq priority_queue<int, vector<int>, greater<int>> #define sz(v) ((int)(v).size()) #define all(s) s.begin(), s.end() #define allr(s) s.rbegin(), s.rend() #define unq(c) (sort(all(c)), c.resize(distance(c.begin(), unique(all(c))))) #define get_pos(c, x) (lower_bound(all(c), x) - c.begin()) #define MS0(v) memset((v), 0, sizeof((v))) #define MS1(v) memset((v), -1, sizeof((v))) #define LEN(v) strlen(v) #define MP make_pair #define pb push_back #define pob pop_back #define ff first #define ss second #define sc scanf #define pf printf #define endl "\n" #define intmx INT_MAX #define llmx 1llu << 61 #define PI 3.14159265358979323846264338327950L #define MOD 1000000007 #define MAX 100010 #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a) * ((b) / gcd(a, b))) #define shuffle(v) (random_shuffle(all(v))) #define min_ele(v) (*min_element(all(v))) #define max_ele(v) (*max_element(all(v))) #define is_equal(x, y) (abs(x - y) < eps) #define cnt_ele(v, x) (count(all(v), x)) #define sum_ele(v) (accumulate(all(v), 0)) #define pro_ele(v) (accumulate(all(v), 1, multiplies<int>())) #define init_range(v, x) (iota(all(v), x)) #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a)) #define TEST_CASE \ int ___T; \ cin >> ___T; \ for (int cs = 1; cs <= ___T; cs++) #define PRINT_CASE cout << "Case " << cs << ": "; #define PRINT_CASEN cout << "Case " << cs << ": \n"; #define vpf(v) \ for (int i = 0; i < v.size(); i++) { \ cout << v[i]; \ if (i != v.size() - 1) \ cout << " "; \ else \ cout << "\n"; \ } #define vsc(v) \ for (int i = 0; i < v.size(); i++) \ cin >> v[i]; #define FOR(i, a, b, stp) for (int i = (a); i <= (b); i += stp) #define ROF(i, a, b, stp) for (int i = (a); i >= (b); i -= stp) template <class T> inline bool read(T &x) { int c = getchar(); int sgn = 1; while (~c && c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } inline ll exp(ll a, ll p) { if (p == 0) return 1; ll x = exp(a, p / 2) % MOD; x = (x * x) % MOD; if (p & 1) x = (x * (a % MOD)) % MOD; return x; } inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < MOD) a += MOD; return a; } inline int multi(ll a, ll b) { a *= b; if (a >= MOD) a %= MOD; return a; } inline int on_bit(int N, int pos) { return N = N | (1 << pos); } inline int off_bit(int N, int pos) { return N = N & ~(1 << pos); } inline bool check_bit(ll N, int pos) { return (bool)(N & (1 << pos)); } #define on_bit_cnt(x) (__builtin_popcount(x)) #define start_clock clock_t tStart = clock() #define end_clock \ printf("\n>>Runtime: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC) #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define fileio \ freopen("in.txt", "r", stdin); \ freopen("out.txt", "w", stdout) //******************* my code starts here ********************************** int n, ar[100005]; int dp[100005]; int solve(int pos) { if (pos == n) return 0; if (dp[pos] != -1) return dp[pos]; int x = solve(pos + 1) + abs(ar[pos] - ar[pos + 1]); int y = 1e9; if (pos + 2 <= n) y = solve(pos + 2) + abs(ar[pos] - ar[pos + 2]); return dp[pos] = min(x, y); } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> ar[i]; MS1(dp); cout << solve(1) << "\n"; return 0; }
replace
159
160
159
160
TLE
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 10000000; ll h[100010]; ll dps[100010]; // n番目のジャンプ台まで到達するための最小コストを返す ll dp(int n) { if (n == 0) { return 0; } if (dps[n] < MAX) { return dps[n]; } ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]); ll n2 = MAX; if (n > 1) { n2 = dp(n - 2) + abs(h[n] - h[n - 2]); } dps[n] = min(n1, n2); return dps[n]; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } for (int i = 0; i < 100010; i++) { dps[i] = MAX; } cout << dp(N - 1) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll MAX = 1LL << 60; ll h[100010]; ll dps[100010]; // n番目のジャンプ台まで到達するための最小コストを返す ll dp(int n) { if (n == 0) { return 0; } if (dps[n] < MAX) { return dps[n]; } ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]); ll n2 = MAX; if (n > 1) { n2 = dp(n - 2) + abs(h[n] - h[n - 2]); } dps[n] = min(n1, n2); return dps[n]; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } for (int i = 0; i < 100010; i++) { dps[i] = MAX; } cout << dp(N - 1) << endl; }
replace
4
5
4
5
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /// find_by_order() /// order_of_key() template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T> T Abs(T x) { return (x < (T)0 ? -x : x); } template <class T> T safe_sqrt(T x) { return sqrt(max(x, (T)0)); } template <class T, class U, class V> T addmod(T x, U k, V MOD) { return ((x + k) % MOD + MOD) % MOD; } template <class T, class U, class V> T submod(T x, U k, V MOD) { return ((x - k) % MOD + MOD) % MOD; } template <class T, class U, class V> T mul(T x, U y, V MOD) { return ((x % MOD) * (y % MOD)) % MOD; } #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> #define fir first #define sec second #define mp make_pair #define pb push_back #define emp emplace_back #define MASK(i) ((1LL) << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define all(c) (c).begin(), (c).end() #define sz(c) (int)((c).size()) #define fn "test" /// FILE_NAME_HERE /*------------------------------------------END_OF_TEMPLATE------------------------------------------*/ namespace task { const int N = 1e5 + 5; int n; int h[N], f[N]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> h[i]; f[i] = 2e9; } f[1] = 0, f[2] = Abs(h[2] - h[1]); for (int i = 3; i <= n; ++i) { minimize(f[i], min(f[i - 1] + Abs(h[i] - h[i - 1]), f[i - 2] + Abs(h[i] - h[i - 2]))); } cout << f[n]; } } // namespace task int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen(fn ".inp", "r", stdin); freopen(fn ".out", "w", stdout); #endif // ONLINE_JUDGE task::solve(); }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /// find_by_order() /// order_of_key() template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T> T Abs(T x) { return (x < (T)0 ? -x : x); } template <class T> T safe_sqrt(T x) { return sqrt(max(x, (T)0)); } template <class T, class U, class V> T addmod(T x, U k, V MOD) { return ((x + k) % MOD + MOD) % MOD; } template <class T, class U, class V> T submod(T x, U k, V MOD) { return ((x - k) % MOD + MOD) % MOD; } template <class T, class U, class V> T mul(T x, U y, V MOD) { return ((x % MOD) * (y % MOD)) % MOD; } #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> #define fir first #define sec second #define mp make_pair #define pb push_back #define emp emplace_back #define MASK(i) ((1LL) << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define all(c) (c).begin(), (c).end() #define sz(c) (int)((c).size()) #define fn "test" /// FILE_NAME_HERE /*------------------------------------------END_OF_TEMPLATE------------------------------------------*/ namespace task { const int N = 1e5 + 5; int n; int h[N], f[N]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> h[i]; f[i] = 2e9; } f[1] = 0, f[2] = Abs(h[2] - h[1]); for (int i = 3; i <= n; ++i) { minimize(f[i], min(f[i - 1] + Abs(h[i] - h[i - 1]), f[i - 2] + Abs(h[i] - h[i - 2]))); } cout << f[n]; } } // namespace task int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); task::solve(); }
delete
74
78
74
74
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int h[10005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i += 1) cin >> h[i]; long long int dp[n + 1]; dp[1] = 0; dp[2] = abs(h[2] - h[1]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout<<dp[i]<<endl; } cout << dp[n] << endl; }
#include <bits/stdc++.h> using namespace std; int h[100005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i += 1) cin >> h[i]; long long int dp[n + 1]; dp[1] = 0; dp[2] = abs(h[2] - h[1]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout<<dp[i]<<endl; } cout << dp[n] << endl; }
replace
2
3
2
3
0
p03160
C++
Runtime Error
//<============ Author --> @venom ============>// #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define fo(i, n) for (int i = 0; i < n; i++) #define int long long #define pb emplace_back #define mp make_pair #define ff first #define ss second #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define sortrev(x) sort(all(x), greater<int>()) #define w(x) \ int x; \ cin >> x; \ while (x--) #define trunc(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define PI 3.1415926535897932384626 #define mod 1000000007 #define inf 1e18 #define FIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; const int N = 3e5, M = N; int mpow(int base, int exp); int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } //<----------------------------------------------------------------------------->// void fastIO() { FIO; #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif } int32_t main() { fastIO(); int n; cin >> n; int x; vi h; fo(i, n) { cin >> x; h.pb(x); } vi dp(n, inf); dp[0] = 0; fo(i, n) { for (int j : {i + 1, i + 2}) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } } cout << dp[n - 1] << "\n"; return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((int)result * base) % mod; base = ((int)base * base) % mod; exp >>= 1; } return result; }
//<============ Author --> @venom ============>// #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define fo(i, n) for (int i = 0; i < n; i++) #define int long long #define pb emplace_back #define mp make_pair #define ff first #define ss second #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define sortrev(x) sort(all(x), greater<int>()) #define w(x) \ int x; \ cin >> x; \ while (x--) #define trunc(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define PI 3.1415926535897932384626 #define mod 1000000007 #define inf 1e18 #define FIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; const int N = 3e5, M = N; int mpow(int base, int exp); int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } //<----------------------------------------------------------------------------->// void fastIO() { FIO; #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif } int32_t main() { FIO; int n; cin >> n; int x; vi h; fo(i, n) { cin >> x; h.pb(x); } vi dp(n, inf); dp[0] = 0; fo(i, n) { for (int j : {i + 1, i + 2}) { if (j < n) { dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j])); } } } cout << dp[n - 1] << "\n"; return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((int)result * base) % mod; base = ((int)base * base) % mod; exp >>= 1; } return result; }
replace
55
56
55
56
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> #include <fstream> #include <string> using namespace std; int main() { int n; cin >> n; vector<int> stones(n); for (int i = 0; i < n; i++) { cin >> stones[i]; } vector<int> cost(10001, 1e9 + 5); cost[0] = 0; for (int i = 0; i < n; i++) { for (int j : {i + 1, i + 2}) { if (j < n) { cost[j] = min(cost[j], cost[i] + abs(stones[i] - stones[j])); } } } cout << cost[n - 1]; return 0; }
#include <bits/stdc++.h> #include <fstream> #include <string> using namespace std; int main() { int n; cin >> n; vector<int> stones(n); for (int i = 0; i < n; i++) { cin >> stones[i]; } vector<int> cost(n, INT_MAX); cost[0] = 0; for (int i = 0; i < n; i++) { for (int j : {i + 1, i + 2}) { if (j < n) { cost[j] = min(cost[j], cost[i] + abs(stones[i] - stones[j])); } } } cout << cost[n - 1]; return 0; }
replace
11
12
11
12
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fast \ ios::sync_with_stdio(false); \ cin.tie(0) #define pb push_back #define digit(x) floor(log10(x)) + 1 #define mod 1000000007 #define L_MAX 1e18 using ll = long long; using ld = long double; using ii = pair<ll, ll>; using iii = tuple<ll, ll, ll>; using matrix = vector<vector<ll>>; using arr = vector<ll>; using vs = vector<string>; using pv = vector<ii>; using umap = unordered_map<ll, ll>; #define in1(x) scanf("%lld", &x) #define in2(x, y) scanf("%lld %lld", &x, &y) #define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z) #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " is " << x << endl; void showArr(arr a) { for (auto i : a) cout << i << " "; cout << endl; } ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1}; ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1}; ll Solve(ll index, arr a, ll n, ll dp[]) { if (index >= n - 1) return 0; if (dp[index] != -1) return dp[index]; ll left = L_MAX, right = L_MAX; if (index + 1 < n) { if (dp[index + 1] == -1) dp[index + 1] = Solve(index + 1, a, n, dp); left = dp[index + 1] + abs(a[index] - a[index + 1]); } if (index + 2 < n) { if (dp[index + 2] == -1) dp[index + 2] = Solve(index + 2, a, n, dp); right = dp[index + 2] + abs(a[index] - a[index + 2]); } return dp[index] = min(left, right); } int main() { fast; ll n; cin >> n; arr a; ll dp[100009]; for (int i = 0; i < n; i++) { ll x; cin >> x; a.pb(x); } memset(dp, -1, sizeof(dp)); cout << Solve(0, a, n, dp) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fast \ ios::sync_with_stdio(false); \ cin.tie(0) #define pb push_back #define digit(x) floor(log10(x)) + 1 #define mod 1000000007 #define L_MAX 1e18 using ll = long long; using ld = long double; using ii = pair<ll, ll>; using iii = tuple<ll, ll, ll>; using matrix = vector<vector<ll>>; using arr = vector<ll>; using vs = vector<string>; using pv = vector<ii>; using umap = unordered_map<ll, ll>; #define in1(x) scanf("%lld", &x) #define in2(x, y) scanf("%lld %lld", &x, &y) #define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z) #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " is " << x << endl; void showArr(arr a) { for (auto i : a) cout << i << " "; cout << endl; } ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1}; ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1}; ll Solve(ll index, arr a, ll n, ll dp[]) { if (index >= n - 1) return 0; if (dp[index] != -1) return dp[index]; ll left = L_MAX, right = L_MAX; if (index + 1 < n) { if (dp[index + 1] == -1) dp[index + 1] = Solve(index + 1, a, n, dp); left = dp[index + 1] + abs(a[index] - a[index + 1]); } if (index + 2 < n) { if (dp[index + 2] == -1) dp[index + 2] = Solve(index + 2, a, n, dp); right = dp[index + 2] + abs(a[index] - a[index + 2]); } return dp[index] = min(left, right); } int main() { fast; ll n; cin >> n; arr a; ll dp[100009]; for (int i = 0; i < n; i++) { ll x; cin >> x; a.pb(x); } // memset(dp,-1,sizeof(dp)); // cout<<Solve(0,a,n,dp)<<endl; dp[n - 1] = 0; dp[n - 2] = abs(a[n - 1] - a[n - 2]); for (int i = n - 3; i >= 0; i--) { dp[i] = min(dp[i + 2] + abs(a[i] - a[i + 2]), abs(a[i] - a[i + 1]) + dp[i + 1]); } cout << dp[0] << endl; return 0; }
replace
59
62
59
68
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool cmp(pair<int, int> x, pair<int, int> y) { return (abs(x.first - x.second) > abs(y.first - y.second)); } int main(int argc, char **argv) { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; int dp[n]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout << dp[i] << endl; } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool cmp(pair<int, int> x, pair<int, int> y) { return (abs(x.first - x.second) > abs(y.first - y.second)); } int main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; int dp[n]; dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); // cout << dp[i] << endl; } cout << dp[n - 1] << endl; return 0; }
delete
7
11
7
7
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; const int INF = (1 << 30); template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; int h[N]; rep(i, N) cin >> h[i]; int dp[10010]; rep(i, 10010) dp[i] = INF; dp[0] = 0; dp[1] = abs(h[0] - h[1]); rep(i, N) { chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[N - 1] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; const int INF = (1 << 30); template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N; cin >> N; int h[N]; rep(i, N) cin >> h[i]; int dp[100100]; rep(i, 100100) dp[i] = INF; dp[0] = 0; dp[1] = abs(h[0] - h[1]); rep(i, N) { chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1])); chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2])); } cout << dp[N - 1] << endl; return 0; }
replace
25
27
25
27
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define ll long long #define P pair<int, int> #define M map<int, int> int n; vector<int> h(10000); vector<int> dp(10000); int main() { cin >> n; rep(i, n) cin >> h.at(i); dp.at(0) = 0; dp.at(1) = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define ll long long #define P pair<int, int> #define M map<int, int> int n; vector<int> h(100000); vector<int> dp(100000); int main() { cin >> n; rep(i, n) cin >> h.at(i); dp.at(0) = 0; dp.at(1) = abs(h[1] - h[0]); for (int i = 2; i < n; ++i) { dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[n - 1] << endl; return 0; }
replace
8
10
8
10
0
p03160
C++
Time Limit Exceeded
#include <iostream> #include <math.h> #include <vector> using namespace std; #define MAX 1e9 + 7 int dp[100000] = {}; int h[100000] = {}; int n; int dfs(int k) { if (k < n - 2) dp[k + 2] = min(dp[k + 2], abs(h[k] - h[k + 2]) + dp[k]); if (k < n - 1) dp[k + 1] = min(dp[k + 1], abs(h[k] - h[k + 1]) + dp[k]); if (k < n - 2) dfs(k + 2); if (k < n - 1) dfs(k + 1); return 0; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> h[i]; } for (int i = 0; i < n; i++) { dp[i] = MAX; } dp[0] = 0; dfs(0); cout << dp[n - 1] << endl; }
#include <iostream> #include <math.h> #include <vector> using namespace std; #define MAX 1e9 + 7 int dp[100000] = {}; int h[100000] = {}; int n; int dfs(int k) { if (k < n - 2) dp[k + 2] = min(dp[k + 2], abs(h[k] - h[k + 2]) + dp[k]); if (k < n - 1) dp[k + 1] = min(dp[k + 1], abs(h[k] - h[k + 1]) + dp[k]); if (k < n - 1) dfs(k + 1); return 0; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> h[i]; } for (int i = 0; i < n; i++) { dp[i] = MAX; } dp[0] = 0; dfs(0); cout << dp[n - 1] << endl; }
delete
16
18
16
16
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define sf scanf #define pf printf #define pb push_back #define llu unsigned long long #define U unsigned int #define SIZE 1000002 #define pie 3.14159265358979323 #define minuss 1e-6 #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define BIG 100000000 ll lcm(ll a, ll b) { ll GCD = __gcd(a, b); ll lcm = (a * b) / GCD; return lcm; } ll dp[1000]; ll weight[10000], value[10000]; ll total, val; ll call(ll n) { if (n >= total) return 0; if (dp[n] != -1) return dp[n]; ll res1 = BIG, res2 = BIG; // if(weight[n]+c<=val) res1 = abs(weight[n] - weight[n + 1]) + call(n + 1); if (n + 2 <= total) res2 = abs(weight[n] - weight[n + 2]) + call(n + 2); // cout<<n<<" "<<res1<<" "<<res2<<endl; dp[n] = min(res1, res2); return dp[n]; } int main() { #ifdef forthright48 freopen("input.txt", "r", stdin); // freopen ( "output.txt", "w", stdout ); #endif // forthright48 cin >> total; for (int i = 1; i <= total; ++i) cin >> weight[i]; memset(dp, -1, sizeof(dp)); ll ret = call(1); cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define sf scanf #define pf printf #define pb push_back #define llu unsigned long long #define U unsigned int #define SIZE 1000002 #define pie 3.14159265358979323 #define minuss 1e-6 #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define BIG 100000000 ll lcm(ll a, ll b) { ll GCD = __gcd(a, b); ll lcm = (a * b) / GCD; return lcm; } ll dp[100005]; ll weight[1000005], value[1000005]; ll total, val; ll call(ll n) { if (n >= total) return 0; if (dp[n] != -1) return dp[n]; ll res1 = BIG, res2 = BIG; // if(weight[n]+c<=val) res1 = abs(weight[n] - weight[n + 1]) + call(n + 1); if (n + 2 <= total) res2 = abs(weight[n] - weight[n + 2]) + call(n + 2); // cout<<n<<" "<<res1<<" "<<res2<<endl; dp[n] = min(res1, res2); return dp[n]; } int main() { #ifdef forthright48 freopen("input.txt", "r", stdin); // freopen ( "output.txt", "w", stdout ); #endif // forthright48 cin >> total; for (int i = 1; i <= total; ++i) cin >> weight[i]; memset(dp, -1, sizeof(dp)); ll ret = call(1); cout << ret << endl; return 0; }
replace
24
26
24
26
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define fr(i, l, r) for (int i = l; i <= r; i++) vector<int> dp(100001, -1); int fun(int n, int h[]) { if (dp[n] != -1) return dp[n]; if (n == 0) { dp[0] = 0; return 0; } if (n == 1) { dp[1] = abs(h[1] - h[0]); return dp[1]; } return min(abs(h[n] - h[n - 1]) + fun(n - 1, h), abs(h[n] - h[n - 2]) + fun(n - 2, h)); } signed main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; cout << fun(n - 1, h); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define fr(i, l, r) for (int i = l; i <= r; i++) vector<int> dp(100001, -1); int fun(int n, int h[]) { if (dp[n] != -1) return dp[n]; if (n == 0) { dp[0] = 0; return 0; } if (n == 1) { dp[1] = abs(h[1] - h[0]); return dp[1]; } dp[n] = min(abs(h[n] - h[n - 1]) + fun(n - 1, h), abs(h[n] - h[n - 2]) + fun(n - 2, h)); return dp[n]; } signed main() { // freopen("input.txt", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) cin >> h[i]; cout << fun(n - 1, h); return 0; }
replace
22
24
22
25
TLE
p03160
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #define INF 2100000000 using namespace std; int absint(int a) { return max(a, -a); } int main() { int n, h[10010], i, j; int cost[10010]; cin >> n; for (i = 1; i <= n; i++) { cin >> h[i]; cost[i] = INF; } cost[1] = 0; for (i = 1; i <= n - 2; i++) { cost[i + 1] = min(cost[i + 1], cost[i] + absint(h[i + 1] - h[i])); cost[i + 2] = min(cost[i + 2], cost[i] + absint(h[i + 2] - h[i])); } cout << min(cost[n], cost[n - 1] + absint(h[n] - h[n - 1])) << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #define INF 2100000000 using namespace std; int absint(int a) { return max(a, -a); } int main() { int n, h[100010], i, j; int cost[100010]; cin >> n; for (i = 1; i <= n; i++) { cin >> h[i]; cost[i] = INF; } cost[1] = 0; for (i = 1; i <= n - 2; i++) { cost[i + 1] = min(cost[i + 1], cost[i] + absint(h[i + 1] - h[i])); cost[i + 2] = min(cost[i + 2], cost[i] + absint(h[i + 2] - h[i])); } cout << min(cost[n], cost[n - 1] + absint(h[n] - h[n - 1])) << endl; return 0; }
replace
9
11
9
11
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lli long long int int main() { lli n, i; lli dp[10001]; dp[0] = 0; dp[1] = 0; cin >> n; lli a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; } dp[2] = abs(a[2] - a[1]); for (i = 3; i <= n; i++) { dp[i] = min(abs(a[i] - a[i - 2]) + dp[i - 2], dp[i - 1] + abs(a[i] - a[i - 1])); } cout << dp[n] << "\n"; }
#include <bits/stdc++.h> using namespace std; #define lli long long int int main() { lli n, i; lli dp[100001]; dp[0] = 0; dp[1] = 0; cin >> n; lli a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; } dp[2] = abs(a[2] - a[1]); for (i = 3; i <= n; i++) { dp[i] = min(abs(a[i] - a[i - 2]) + dp[i - 2], dp[i - 1] + abs(a[i] - a[i - 1])); } cout << dp[n] << "\n"; }
replace
5
6
5
6
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; int MAX = 10010010; ll h[100010]; ll dps[100010]; // n番目のジャンプ台まで到達するための最小コストを返す ll dp(int n) { if (n == 0) { return 0; } if (dps[n] < MAX) { return dps[n]; } ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]); ll n2 = MAX; if (n > 1) { n2 = dp(n - 2) + abs(h[n] - h[n - 2]); } dps[n] = min(n1, n2); return dps[n]; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } for (int i = 0; i < 100010; i++) { dps[i] = MAX; } cout << dp(N - 1) << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 1LL << 60; ll h[100010]; ll dps[100010]; // n番目のジャンプ台まで到達するための最小コストを返す ll dp(int n) { if (n == 0) { return 0; } if (dps[n] < MAX) { return dps[n]; } ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]); ll n2 = MAX; if (n > 1) { n2 = dp(n - 2) + abs(h[n] - h[n - 2]); } dps[n] = min(n1, n2); return dps[n]; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> h[i]; } for (int i = 0; i < 100010; i++) { dps[i] = MAX; } cout << dp(N - 1) << endl; }
replace
4
5
4
5
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ld long double #define int long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define MOD 1000000007 #define sd(t) scanf("%d", &(t)) #define rep(i, a, b) for (int i = a; i < b; i++) #define hii cout << "hii" << endl #define okay cout << "okay" << endl #define hey cout << "hey" << endl template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("jjj.txt", "w", stdout); #endif } clock_t time_p = clock(); void starboy1299() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } const int N = 3e5 + 5; int arr[N]; int dp[N]; int n; int f(int idx) { if (idx == n - 1) { return 0; } if (idx >= n) return 1e10; if (dp[idx] != -1) return dp[idx]; int ans = 1e10; if (idx + 1 < n) { ans = min(ans, abs(arr[idx + 1] - arr[idx]) + f(idx + 1)); } if (idx + 2 < n) { ans = min(ans, abs(arr[idx + 2] - arr[idx]) + f(idx + 2)); } return dp[idx] = ans; } void solve() { // int n; cin >> n; memset(dp, -1, sizeof(dp)); rep(i, 0, n) cin >> arr[i]; cout << f(0) << endl; } int32_t main() { fast(); int t = 1; // cin>> t; while (t--) { solve(); } starboy1299(); }
#include <bits/stdc++.h> using namespace std; #define ld long double #define int long long #define pb push_back #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define mi map<int, int> #define mii map<pii, int> #define all(a) (a).begin(), (a).end() #define x first #define y second #define sz(x) (int)x.size() #define endl '\n' #define MOD 1000000007 #define sd(t) scanf("%d", &(t)) #define rep(i, a, b) for (int i = a; i < b; i++) #define hii cout << "hii" << endl #define okay cout << "okay" << endl #define hey cout << "hey" << endl template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; } template <class T, class S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; } #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <class Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <class Arg1, class... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *sep = strchr(names + 1, ','); cerr.write(names, sep - names) << " : " << arg1 << " "; __f(sep + 1, args...); } #else #define trace(...) 0 #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target( \ "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif // ifndef ONLINE_JUDGE void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } clock_t time_p = clock(); void starboy1299() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } const int N = 3e5 + 5; int arr[N]; int dp[N]; int n; int f(int idx) { if (idx == n - 1) { return 0; } if (idx >= n) return 1e10; if (dp[idx] != -1) return dp[idx]; int ans = 1e10; if (idx + 1 < n) { ans = min(ans, abs(arr[idx + 1] - arr[idx]) + f(idx + 1)); } if (idx + 2 < n) { ans = min(ans, abs(arr[idx + 2] - arr[idx]) + f(idx + 2)); } return dp[idx] = ans; } void solve() { // int n; cin >> n; memset(dp, -1, sizeof(dp)); rep(i, 0, n) cin >> arr[i]; cout << f(0) << endl; } int32_t main() { fast(); int t = 1; // cin>> t; while (t--) { solve(); } starboy1299(); }
delete
69
73
69
69
0
Time Taken : 0.002513
p03160
C++
Runtime Error
/************************************************************ / AUTHOR : DEVANSHU SINGLA / / NICK : DSINGLA / / INSTITUTE : IITK / ************************************************************/ // TEMPLATE // #include <bits/stdc++.h> #define ll long long #define N 100005 #define MOD 1000000007 #define pb push_back #define mp make_pair #define clr(x) x.clear() #define F first #define S second #define MAX(x, y) (x) > (y) ? (x) : (y) #define MIN(x, y) (x) < (y) ? (x) : (y) // use cin.ignore() after cin statement. using namespace std; void solve() { long n; cin >> n; long h[n]; for (long i = 0; i < n; i++) { cin >> h[i]; } ll p[n]; p[0] = 0; p[1] = abs(h[1] - h[0]); for (long i = 2; i < n; i++) { p[i] = MIN(p[i - 1] + abs(h[i] - h[i - 1]), p[i - 2] + abs(h[i] - h[i - 2])); } cout << p[n - 1]; } int main() { #ifndef ONLINE_JUDGE freopen("/home/devanshu/Desktop/cp/input.txt", "r", stdin); freopen("/home/devanshu/Desktop/cp/output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
/************************************************************ / AUTHOR : DEVANSHU SINGLA / / NICK : DSINGLA / / INSTITUTE : IITK / ************************************************************/ // TEMPLATE // #include <bits/stdc++.h> #define ll long long #define N 100005 #define MOD 1000000007 #define pb push_back #define mp make_pair #define clr(x) x.clear() #define F first #define S second #define MAX(x, y) (x) > (y) ? (x) : (y) #define MIN(x, y) (x) < (y) ? (x) : (y) // use cin.ignore() after cin statement. using namespace std; void solve() { long n; cin >> n; long h[n]; for (long i = 0; i < n; i++) { cin >> h[i]; } ll p[n]; p[0] = 0; p[1] = abs(h[1] - h[0]); for (long i = 2; i < n; i++) { p[i] = MIN(p[i - 1] + abs(h[i] - h[i - 1]), p[i - 2] + abs(h[i] - h[i - 2])); } cout << p[n - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
delete
40
46
40
40
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; ifstream fin("darb.in"); ofstream fout("darb.out"); int minCost[10001], v[10001]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; minCost[1] = 0, minCost[2] = abs(v[2] - v[1]); for (int i = 3; i <= n; i++) minCost[i] = min(minCost[i - 1] + abs(v[i] - v[i - 1]), minCost[i - 2] + abs(v[i] - v[i - 2])); cout << minCost[n] << " "; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; ifstream fin("darb.in"); ofstream fout("darb.out"); int minCost[100001], v[100001]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; minCost[1] = 0, minCost[2] = abs(v[2] - v[1]); for (int i = 3; i <= n; i++) minCost[i] = min(minCost[i - 1] + abs(v[i] - v[i - 1]), minCost[i - 2] + abs(v[i] - v[i - 2])); cout << minCost[n] << " "; return 0; }
replace
6
7
6
7
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; vector<int> dp(10001, -1); int main() { int n; cin >> n; vector<int> h; for (int i = 0; i < n; i++) { int h1; cin >> h1; h.push_back(h1); } dp[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) dp[i] = abs(h[i - 1] - h[i]); else dp[i] = min(abs(h[i - 2] - h[i]) + dp[i - 2], abs(h[i - 1] - h[i]) + dp[i - 1]); } cout << dp[n - 1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> dp(100000001, -1); int main() { int n; cin >> n; vector<int> h; for (int i = 0; i < n; i++) { int h1; cin >> h1; h.push_back(h1); } dp[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) dp[i] = abs(h[i - 1] - h[i]); else dp[i] = min(abs(h[i - 2] - h[i]) + dp[i - 2], abs(h[i - 1] - h[i]) + dp[i - 1]); } cout << dp[n - 1] << "\n"; return 0; }
replace
2
3
2
3
0
p03160
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; int dp[10000] = {}; int main() { int N; cin >> N; int h[N]; for (int i = 0; i < N; i++) cin >> h[i]; for (int i = 1; i < N; i++) { if (i == 1) dp[1] = abs(h[1] - h[0]); else dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[N - 1] << endl; return 0; }
#include <cmath> #include <iostream> using namespace std; int dp[100000] = {}; int main() { int N; cin >> N; int h[N]; for (int i = 0; i < N; i++) cin >> h[i]; for (int i = 1; i < N; i++) { if (i == 1) dp[1] = abs(h[1] - h[0]); else dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2])); } cout << dp[N - 1] << endl; return 0; }
replace
4
5
4
5
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define deb(x) cout << '>' << #x << ':' << x << endl; #define REP(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i <= (b); i++) #define FORD(i, a, b) for (ll i = (a); i >= (b); i--) typedef long long int ll; ll go(ll *h, ll n, ll index, ll *dp) { if (dp[index] != 0) { return dp[index]; } if (n < 0) { return 0; } if (n == 0) { return 0; } if (n == 1) { return 0; } if (n == 2) { return abs(h[1] - h[0]); } ll ans1 = go(h + 1, n - 1, index + 1, dp) + abs(h[1] - h[0]); ll ans2 = go(h + 2, n - 2, index + 2, dp) + abs(h[2] - h[0]); return dp[index] = min(ans1, ans2); } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); ll n; cin >> n; ll *h = new ll[n]; ll *dp = new ll[n]; REP(i, n) { cin >> h[i]; dp[i] = 0; } int x = go(h, n, 0, dp); cout << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define deb(x) cout << '>' << #x << ':' << x << endl; #define REP(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i <= (b); i++) #define FORD(i, a, b) for (ll i = (a); i >= (b); i--) typedef long long int ll; ll go(ll *h, ll n, ll index, ll *dp) { if (dp[index] != 0) { return dp[index]; } if (n < 0) { return 0; } if (n == 0) { return 0; } if (n == 1) { return 0; } if (n == 2) { return abs(h[1] - h[0]); } ll ans1 = go(h + 1, n - 1, index + 1, dp) + abs(h[1] - h[0]); ll ans2 = go(h + 2, n - 2, index + 2, dp) + abs(h[2] - h[0]); return dp[index] = min(ans1, ans2); } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); ll n; cin >> n; ll *h = new ll[n]; ll *dp = new ll[n]; REP(i, n) { cin >> h[i]; dp[i] = 0; } dp[0] = 0; dp[1] = abs(h[1] - h[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i])); } cout << dp[n - 1] << endl; return 0; }
replace
42
44
42
50
TLE
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define why(n, x) \ int n; \ while (cin >> n, n != x) #define iFOR(i, x, n) for (int i = x; i < n; i++) #define unless(flg) if (!(flg)) #define read cin << #define echo cout << #define fin << '\n' #define __ << " " << #define ___ << " " #define bash push_back #define ALL(x) x.begin(), x.end() #define SWAP(a, b) ((a != b) && (a += b, b = a - b, a -= b)) #define cinf(n, x) \ for (int UNCH = 0; UNCH < (n); UNCH++) \ cin >> x[UNCH]; #define fcin(n, x) \ for (int UNCH = 1; UNCH <= (n); UNCH++) \ cin >> x[UNCH]; #define memmin(x) memset((x), -1, sizeof((x))) #define memzer(x) memset((x), 0, sizeof((x))) #define meminf(x) memset((x), 0x3f, sizeof((x))) typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vit; typedef map<string, int> mstit; typedef vector<pii> vpi; typedef greater<pii> gpi; typedef priority_queue<pii, vpi, gpi> dijk; static const signed int INF = 0x3f3f3f3f; static const signed long long LINF = 0x3f3f3f3f3f3f3f3fLL; static const signed int SMOD = 1000000007; static const signed int NMOD = 1000000009; static const signed int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; static const signed int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; bool inside(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } template <class T> T abs(T &x) { return x < 0 ? -x : x; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } // #define int long long int h[100005]; int mem[100005]; int dp(int n) { if (mem[n] != -1) return mem[n]; if (n < 1) return INF; if (n == 1) return 0; int a1 = dp(n - 1) + abs(h[n] - h[n - 1]); int a2 = dp(n - 2) + abs(h[n] - h[n - 2]); return min(a1, a2); } inline void solve() { int n; cin >> n; memset(mem, -1, sizeof(mem)); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dp(n) << endl; } struct xyz { xyz() { cin.tie(0), ios::sync_with_stdio(false); cout << fixed << setprecision(12); }; } xyzzy; signed main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define why(n, x) \ int n; \ while (cin >> n, n != x) #define iFOR(i, x, n) for (int i = x; i < n; i++) #define unless(flg) if (!(flg)) #define read cin << #define echo cout << #define fin << '\n' #define __ << " " << #define ___ << " " #define bash push_back #define ALL(x) x.begin(), x.end() #define SWAP(a, b) ((a != b) && (a += b, b = a - b, a -= b)) #define cinf(n, x) \ for (int UNCH = 0; UNCH < (n); UNCH++) \ cin >> x[UNCH]; #define fcin(n, x) \ for (int UNCH = 1; UNCH <= (n); UNCH++) \ cin >> x[UNCH]; #define memmin(x) memset((x), -1, sizeof((x))) #define memzer(x) memset((x), 0, sizeof((x))) #define meminf(x) memset((x), 0x3f, sizeof((x))) typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vit; typedef map<string, int> mstit; typedef vector<pii> vpi; typedef greater<pii> gpi; typedef priority_queue<pii, vpi, gpi> dijk; static const signed int INF = 0x3f3f3f3f; static const signed long long LINF = 0x3f3f3f3f3f3f3f3fLL; static const signed int SMOD = 1000000007; static const signed int NMOD = 1000000009; static const signed int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; static const signed int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; bool inside(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } template <class T> T abs(T &x) { return x < 0 ? -x : x; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } // #define int long long int h[100005]; int mem[100005]; int dp(int n) { if (mem[n] != -1) return mem[n]; if (n < 1) return INF; if (n == 1) return 0; int a1 = dp(n - 1) + abs(h[n] - h[n - 1]); int a2 = dp(n - 2) + abs(h[n] - h[n - 2]); return mem[n] = min(a1, a2); } inline void solve() { int n; cin >> n; memset(mem, -1, sizeof(mem)); for (int i = 1; i <= n; i++) { cin >> h[i]; } cout << dp(n) << endl; } struct xyz { xyz() { cin.tie(0), ios::sync_with_stdio(false); cout << fixed << setprecision(12); }; } xyzzy; signed main() { solve(); return 0; }
replace
83
84
83
84
TLE
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vi vector<int> #define pii pair<int, int> #define mii map<int, int> #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (int i = (a); i < (b); i++) #define mod 1000000007 int dp[100001]; void c() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int Mincost(vi &h, int n) { if (n == 0) return 0; if (dp[n] != -1) return dp[n]; return dp[n] = min(Mincost(h, n - 1) + abs(h[n] - h[n - 1]), (n >= 2 ? Mincost(h, n - 2) + abs(h[n] - h[n - 2]) : INT_MAX)); } int32_t main() { c(); memset(dp, -1, sizeof(dp)); int n; cin >> n; vi a(n); rep(i, 0, n) cin >> a[i]; cout << Mincost(a, n - 1); return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vi vector<int> #define pii pair<int, int> #define mii map<int, int> #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (int i = (a); i < (b); i++) #define mod 1000000007 int dp[100001]; void c() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int Mincost(vi &h, int n) { if (n == 0) return 0; if (dp[n] != -1) return dp[n]; return dp[n] = min(Mincost(h, n - 1) + abs(h[n] - h[n - 1]), (n >= 2 ? Mincost(h, n - 2) + abs(h[n] - h[n - 2]) : INT_MAX)); } int32_t main() { // c(); memset(dp, -1, sizeof(dp)); int n; cin >> n; vi a(n); rep(i, 0, n) cin >> a[i]; cout << Mincost(a, n - 1); return 0; }
replace
42
43
42
43
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int h[10001]; int dp[10001]; int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) { cin >> h[i]; } dp[1] = 0; dp[2] = abs(h[2] - h[1]); for (int i = 3; i <= N; ++i) { dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[N] << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> using namespace std; int h[100001]; int dp[100001]; int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) { cin >> h[i]; } dp[1] = 0; dp[2] = abs(h[2] - h[1]); for (int i = 3; i <= N; ++i) { dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[N] << endl; return 0; }
replace
5
7
5
7
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int h[10000]; for (long long int i = 0; i < n; i++) cin >> h[i]; vector<long long int> dp(n); dp[0] = 0; for (long long int i = 1; i < n; i++) { if (i - 2 < 0) { dp[i] = abs(h[i] - h[i - 1]) + dp[i - 1]; } else dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[n - 1]; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int h[100000]; for (long long int i = 0; i < n; i++) cin >> h[i]; vector<long long int> dp(n); dp[0] = 0; for (long long int i = 1; i < n; i++) { if (i - 2 < 0) { dp[i] = abs(h[i] - h[i - 1]) + dp[i - 1]; } else dp[i] = min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]); } cout << dp[n - 1]; }
replace
5
6
5
6
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl '\n' #define ll long long #define inf 1000000007 int main() { fastio; #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ll n; cin >> n; ll arr[n]; ll dp[n]; for (ll i = 0; i < n; i++) { cin >> arr[i]; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (ll i = 2; i < n; i++) { dp[i] = min(abs(arr[i] - arr[i - 1]) + dp[i - 1], abs(arr[i] - arr[i - 2]) + dp[i - 2]); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl '\n' #define ll long long #define inf 1000000007 int main() { fastio; #ifdef APNA_IO freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ll n; cin >> n; ll arr[n]; ll dp[n]; for (ll i = 0; i < n; i++) { cin >> arr[i]; } dp[0] = 0; dp[1] = abs(arr[1] - arr[0]); for (ll i = 2; i < n; i++) { dp[i] = min(abs(arr[i] - arr[i - 1]) + dp[i - 1], abs(arr[i] - arr[i - 2]) + dp[i - 2]); } cout << dp[n - 1] << endl; return 0; }
replace
11
12
11
12
-11
p03160
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; vector<int> stones; vector<int> costs; int hops(int stone) { if (stone < 1) { return 0; } if (stone == 1) { return abs(costs[stone] - costs[stone - 1]); } if (stones[stone] != -1) { return stones[stone]; } int cost1 = hops(stone - 1) + abs(costs[stone] - costs[stone - 1]); int cost2 = hops(stone - 2) + abs(costs[stone] - costs[stone - 2]); int preferable = min(cost1, cost2); stones[stone] = preferable; return preferable; } int main() { int n; cin >> n; stones.assign(1000000000, -1); for (int i = 0; i < n; i++) { int cost; cin >> cost; costs.push_back(cost); } cout << hops(n - 1) << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; vector<int> stones; vector<int> costs; int hops(int stone) { if (stone < 1) { return 0; } if (stone == 1) { return abs(costs[stone] - costs[stone - 1]); } if (stones[stone] != -1) { return stones[stone]; } int cost1 = hops(stone - 1) + abs(costs[stone] - costs[stone - 1]); int cost2 = hops(stone - 2) + abs(costs[stone] - costs[stone - 2]); int preferable = min(cost1, cost2); stones[stone] = preferable; return preferable; } int main() { int n; cin >> n; stones.assign(n, -1); for (int i = 0; i < n; i++) { int cost; cin >> cost; costs.push_back(cost); } cout << hops(n - 1) << endl; }
replace
26
27
26
27
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define LL long long #define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX) #define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX) vector<LL> InputVec(LL size) { vector<LL> vec((size_t)size); REP(i, size) { cin >> vec[(size_t)i]; } return vec; } vector<LL> tbl; LL CalcCost(vector<LL> const &hs, int i) { if (i == hs.size() - 1) { return 0; } if (tbl[i] >= 0) { // return tbl[i]; } LL cost = 0; { LL c = abs(hs[i] - hs[i + 1]); c += CalcCost(hs, i + 1); cost = c; } if (i + 2 < hs.size()) { LL c = abs(hs[i] - hs[i + 2]); c += CalcCost(hs, i + 2); cost = min(cost, c); } tbl[i] = cost; return cost; } int main() { LL N; cin >> N; vector<LL> hs = InputVec(N); tbl.resize(N, -1); LL cost = CalcCost(hs, 0); cout << cost << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define LL long long #define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX) #define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX) vector<LL> InputVec(LL size) { vector<LL> vec((size_t)size); REP(i, size) { cin >> vec[(size_t)i]; } return vec; } vector<LL> tbl; LL CalcCost(vector<LL> const &hs, int i) { if (i == hs.size() - 1) { return 0; } if (tbl[i] >= 0) { return tbl[i]; } LL cost = 0; { LL c = abs(hs[i] - hs[i + 1]); c += CalcCost(hs, i + 1); cost = c; } if (i + 2 < hs.size()) { LL c = abs(hs[i] - hs[i + 2]); c += CalcCost(hs, i + 2); cost = min(cost, c); } tbl[i] = cost; return cost; } int main() { LL N; cin >> N; vector<LL> hs = InputVec(N); tbl.resize(N, -1); LL cost = CalcCost(hs, 0); cout << cost << endl; return 0; }
replace
22
23
22
23
TLE
p03160
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; int n; int dp[10010]; int height[10010]; int main() { cin >> n; dp[0] = 9999999; for (int i = 1; i <= n; i++) { dp[i] = 9999999; int a; cin >> a; height[i] = a; } dp[1] = 0; dp[2] = abs(height[2] - height[1]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]), dp[i - 2] + abs(height[i - 2] - height[i])); } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; int n; int dp[100100]; int height[100100]; int main() { cin >> n; dp[0] = 9999999; for (int i = 1; i <= n; i++) { dp[i] = 9999999; int a; cin >> a; height[i] = a; } dp[1] = 0; dp[2] = abs(height[2] - height[1]); for (int i = 3; i <= n; i++) { dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]), dp[i - 2] + abs(height[i - 2] - height[i])); } cout << dp[n] << endl; return 0; }
replace
10
12
10
12
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> vec; for (int i = 0; i < n; i++) cin >> vec[i]; int dp[n + 1]; dp[0] = 0; dp[1] = 0; for (int j = 2; j <= n; j++) { if (j > 2) dp[j] = min(dp[j - 1] + abs(vec[j - 1] - vec[j - 2]), dp[j - 2] + abs(vec[j - 1] - vec[j - 3])); else dp[j] = dp[j - 1] + abs(vec[j - 1] - vec[j - 2]); } cout << dp[n] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; int dp[n + 1]; dp[0] = 0; dp[1] = 0; for (int j = 2; j <= n; j++) { if (j > 2) dp[j] = min(dp[j - 1] + abs(vec[j - 1] - vec[j - 2]), dp[j - 2] + abs(vec[j - 1] - vec[j - 3])); else dp[j] = dp[j - 1] + abs(vec[j - 1] - vec[j - 2]); } cout << dp[n] << endl; }
replace
5
6
5
6
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int using namespace std; int ans1 = 0, ans2 = 0; const int N = 1e4 + 5; int arr[N]; int recur(int n) { int dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) { dp[i] = abs(arr[0] - arr[1]); } else { dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]), dp[i - 2] + abs(arr[i] - arr[i - 2])); } } return dp[n - 1]; } signed main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; cout << recur(n) << endl; return 0; }
#include <bits/stdc++.h> #define int long long int using namespace std; int ans1 = 0, ans2 = 0; const int N = 1e5 + 5; int arr[N]; int recur(int n) { int dp[n]; dp[0] = 0; for (int i = 1; i < n; i++) { if (i == 1) { dp[i] = abs(arr[0] - arr[1]); } else { dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]), dp[i - 2] + abs(arr[i] - arr[i - 2])); } } return dp[n - 1]; } signed main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; cout << recur(n) << endl; return 0; }
replace
4
5
4
5
0
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define mod 1000000007 #define pii pair<int, int> #define F first #define S second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define forn(i, s, e) for (int i = s; i <= e; i++) #define fornd(i, s, e) for (int i = s; i >= e; i--) #define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++) #define print(x) \ for (auto el : x) \ cout << el << " "; \ cout << "\n" #define PSET(x, y) fixed << setprecision(y) << x // #define bits(x) __builtin_popcount(x) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) void fileio() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } template <typename T> void swap(T &a, T &b) { T temp = a; a = b; b = temp; } const int N = 1000001; vector<int> h; vector<int> dp; int rec(int i) { if (i == 0) return 0; if (i < 0) return INT_MAX; if (dp[i] != -1) return dp[i]; return dp[i] = min((i - 1) >= 0 ? rec(i - 1) + abs(h[i] - h[i - 1]) : INT_MAX, (i - 2) >= 0 ? rec(i - 2) + abs(h[i] - h[i - 2]) : INT_MAX); } void solve() { int n; cin >> n; h = vector<int>(n); dp = vector<int>(n, -1); forn(i, 0, n - 1) cin >> h[i]; cout << rec(n - 1) << endl; } int32_t main() { fileio(); fast; // int t; // cin>>t; // for(int y=1;y<=t;y++) { // cout<<"Case #"<<y<<": "; solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define mod 1000000007 #define pii pair<int, int> #define F first #define S second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define forn(i, s, e) for (int i = s; i <= e; i++) #define fornd(i, s, e) for (int i = s; i >= e; i--) #define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++) #define print(x) \ for (auto el : x) \ cout << el << " "; \ cout << "\n" #define PSET(x, y) fixed << setprecision(y) << x // #define bits(x) __builtin_popcount(x) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) void fileio() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } template <typename T> void swap(T &a, T &b) { T temp = a; a = b; b = temp; } const int N = 1000001; vector<int> h; vector<int> dp; int rec(int i) { if (i == 0) return 0; if (i < 0) return INT_MAX; if (dp[i] != -1) return dp[i]; return dp[i] = min((i - 1) >= 0 ? rec(i - 1) + abs(h[i] - h[i - 1]) : INT_MAX, (i - 2) >= 0 ? rec(i - 2) + abs(h[i] - h[i - 2]) : INT_MAX); } void solve() { int n; cin >> n; h = vector<int>(n); dp = vector<int>(n, -1); forn(i, 0, n - 1) cin >> h[i]; cout << rec(n - 1) << endl; } int32_t main() { fast; // int t; // cin>>t; // for(int y=1;y<=t;y++) { // cout<<"Case #"<<y<<": "; solve(); } return 0; }
replace
71
72
71
72
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; int a[n], dp[n]; for (int i = 0; i < n; i++) { cin >> a[i]; dp[i] = INT_MAX; } dp[0] = 0; for (int i = 0; i < n; i++) { if (i + 1 < n) dp[i + 1] = min(dp[i + 1], abs(a[i + 1] - a[i]) + dp[i]); if (i + 2 < n) dp[i + 2] = min(dp[i + 2], abs(a[i + 2] - a[i]) + dp[i]); } cout << dp[n - 1]; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int n; cin >> n; int a[n], dp[n]; for (int i = 0; i < n; i++) { cin >> a[i]; dp[i] = INT_MAX; } dp[0] = 0; for (int i = 0; i < n; i++) { if (i + 1 < n) dp[i + 1] = min(dp[i + 1], abs(a[i + 1] - a[i]) + dp[i]); if (i + 2 < n) dp[i + 2] = min(dp[i + 2], abs(a[i + 2] - a[i]) + dp[i]); } cout << dp[n - 1]; }
replace
8
12
8
12
-11
p03160
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define fi first #define se second #define pb push_back #define mp make_pair ll mod = 1e9 + 7; ll a[200005]; ll b[200005]; ll idx[100005][2]; // ll cnt[200005]; ll pre[200005]; // ll end[200005]; map<ll, ll> m; bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi - a.se) > (b.fi - b.se); } bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi) > (b.fi); } ll ceil1(ll a, ll b) { if (a % b != 0) return a / b + 1; else return a / b; } ll power(ll a, ll b, ll n) { ll ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % n; a = (a * a) % n; b /= 2; } return ans; } ll count1 = 0; ll gcd(ll a, ll b) { // if(a<b)swap(a,b); if (b == 0) return a; return gcd(b, a % b); } ll primepos(ll fact, ll p) { ll res = 0; while (fact > 0) { res += fact / p; fact /= p; } return res; } // void dfs(ll n, ll p){ // if(vis[n]!=0){ // return; // } // else{ // vis[n]=1; // b[n]=p; // for(int i=0;i<v[n].size();i++){ // dfs(v[n][i],p); // } // } // } int main() { ios::sync_with_stdio(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int dp[n + 1]; memset(dp, 0, sizeof dp); dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define fi first #define se second #define pb push_back #define mp make_pair ll mod = 1e9 + 7; ll a[200005]; ll b[200005]; ll idx[100005][2]; // ll cnt[200005]; ll pre[200005]; // ll end[200005]; map<ll, ll> m; bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi - a.se) > (b.fi - b.se); } bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) { return (a.fi) > (b.fi); } ll ceil1(ll a, ll b) { if (a % b != 0) return a / b + 1; else return a / b; } ll power(ll a, ll b, ll n) { ll ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % n; a = (a * a) % n; b /= 2; } return ans; } ll count1 = 0; ll gcd(ll a, ll b) { // if(a<b)swap(a,b); if (b == 0) return a; return gcd(b, a % b); } ll primepos(ll fact, ll p) { ll res = 0; while (fact > 0) { res += fact / p; fact /= p; } return res; } // void dfs(ll n, ll p){ // if(vis[n]!=0){ // return; // } // else{ // vis[n]=1; // b[n]=p; // for(int i=0;i<v[n].size();i++){ // dfs(v[n][i],p); // } // } // } int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int dp[n + 1]; memset(dp, 0, sizeof dp); dp[0] = 0; dp[1] = abs(a[1] - a[0]); for (int i = 2; i < n; i++) { dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2])); } cout << dp[n - 1] << endl; return 0; }
replace
80
83
80
81
0
p03160
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lint long long #define P pair<int, int> #define LLP pair<long long, long long> #define REP(i, x, n) for (int i = x; i < n; ++i) #define rep(i, n) for (int i = 0; i < n; ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define SORT(x) sort((x).begin(), (x).end()) const int IINF = 1e9 + 10; const long long LLINF = (long long)1e18 + 10; const long long MOD = (long long)1e9 + 7; const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1}; const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1}; const double EPS = 1e-8; int n; vector<int> h; int memo[100010]; int solve(int i) { if (i == 0) { return 0; } if (i == 1) { return abs(h[1] - h[0]); } if (memo[i] >= 0) { return memo[i]; } return min(solve(i - 1) + abs(h[i] - h[i - 1]), solve(i - 2) + abs(h[i] - h[i - 2])); } int main() { cin >> n; h.resize(n); rep(i, n) { cin >> h[i]; } fill(memo, memo + n, -1); int ans = solve(n - 1); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define lint long long #define P pair<int, int> #define LLP pair<long long, long long> #define REP(i, x, n) for (int i = x; i < n; ++i) #define rep(i, n) for (int i = 0; i < n; ++i) #define repr(i, n) for (int i = n - 1; i >= 0; --i) #define SORT(x) sort((x).begin(), (x).end()) const int IINF = 1e9 + 10; const long long LLINF = (long long)1e18 + 10; const long long MOD = (long long)1e9 + 7; const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1}; const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1}; const double EPS = 1e-8; int n; vector<int> h; int memo[100010]; int solve(int i) { if (i == 0) { return 0; } if (i == 1) { return abs(h[1] - h[0]); } if (memo[i] >= 0) { return memo[i]; } return memo[i] = min(solve(i - 1) + abs(h[i] - h[i - 1]), solve(i - 2) + abs(h[i] - h[i - 2])); } int main() { cin >> n; h.resize(n); rep(i, n) { cin >> h[i]; } fill(memo, memo + n, -1); int ans = solve(n - 1); cout << ans << endl; return 0; }
replace
33
35
33
35
TLE