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
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int M = 20010; const long long M2 = 1e18; long long n, k, l, r, mid, ll, rr, mmid, cnt, i, a[M]; int main() { cin >> n >> k; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); l = -M2; r = M2; while (l + 1 < r) { mid = l + r >> 1; cnt = 0; for (i = 0; i < n; i++) { if (a[i] < 0) { ll = i; rr = n; while (ll + 1 < rr) { mmid = ll + rr >> 1; if (a[i] * a[mmid] <= mid) rr = mmid; else ll = mmid; } cnt += n - rr; } else { ll = i; rr = n; while (ll + 1 < rr) { mmid = ll + rr >> 1; if (a[i] * a[mmid] <= mid) ll = mmid; else rr = mmid; } cnt += ll - i; } } if (cnt < k) l = mid; else r = mid; } cout << r; return 0; }
#include <bits/stdc++.h> using namespace std; const long long M = 2e5 + 10; const long long M2 = 1e18; long long n, k, l, r, mid, ll, rr, mmid, cnt, i, a[M]; int main() { cin >> n >> k; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); l = -M2; r = M2; while (l + 1 < r) { mid = l + r >> 1; cnt = 0; for (i = 0; i < n; i++) { if (a[i] < 0) { ll = i; rr = n; while (ll + 1 < rr) { mmid = ll + rr >> 1; if (a[i] * a[mmid] <= mid) rr = mmid; else ll = mmid; } cnt += n - rr; } else { ll = i; rr = n; while (ll + 1 < rr) { mmid = ll + rr >> 1; if (a[i] * a[mmid] <= mid) ll = mmid; else rr = mmid; } cnt += ll - i; } } if (cnt < k) l = mid; else r = mid; } cout << r; return 0; }
replace
4
5
4
5
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define ALL(obj) (obj).begin(), (obj).end() #define SPEED \ cin.tie(0); \ ios::sync_with_stdio(false); template <class T> using PQ = priority_queue<T>; template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>; constexpr long long MOD = (long long)1e9 + 7; constexpr long long MOD2 = 998244353; constexpr long long HIGHINF = (long long)1e18; constexpr long long LOWINF = (long long)1e15; constexpr long double PI = 3.1415926535897932384626433; template <class T> vector<T> multivector(size_t N, T init) { return vector<T>(N, init); } template <class... T> auto multivector(size_t N, T... t) { return vector<decltype(multivector(t...))>(N, multivector(t...)); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; exit(0); } } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &obj) { o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &obj) { o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) { o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) { o << "{"; for (int i = 0; i < (int)obj.size(); ++i) o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &obj) { o << "{" << obj.first << ", " << obj.second << "}"; return o; } void print(void) { cout << endl; } template <class Head> void print(Head &&head) { cout << head; print(); } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); } template <class T> void chmax(T &a, const T b) { a = max(a, b); } template <class T> void chmin(T &a, const T b) { a = min(a, b); } void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; } void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; } void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; } int main() { ll N, K; cin >> N >> K; vector<ll> A, B; for (int i = 0; i < N; ++i) { ll C; cin >> C; (C >= 0 ? A : B).push_back(C); } sort(ALL(A)); sort(ALL(B)); ll X = A.size(); ll Y = B.size(); ll ok = HIGHINF, ng = -HIGHINF, md; while (ok - ng > 1) { md = (ok + ng) / 2; ll cnt = 0; for (int i = 0; i + 1 < X; ++i) { if (A[i] * A[i + 1] > md) continue; int ok2 = i + 1, ng2 = X, md2; while (ng2 - ok2 > 1) { md2 = (ok2 + ng2) / 2; (A[i] * A[md2] <= md ? ok2 : ng2) = md2; } cnt += ok2 - i; } for (int i = Y - 1; 0 <= i - 1; --i) { if (B[i - 1] * B[i] > md) continue; int ok2 = i - 1, ng2 = -1, md2; while (ok2 - ng2 > 1) { md2 = (ok2 + ng2) / 2; (B[i] * B[md2] <= md ? ok2 : ng2) = md2; } cnt += i - ok2; } for (int i = 0; i < X; ++i) { if (A[i] * B[0] > md) continue; int ok2 = 0, ng2 = Y, md2; while (ng2 - ok2 > 1) { md2 = (ok2 + ng2) / 2; (A[i] * B[md2] <= md ? ok2 : ng2) = md2; } cnt += ok2 + 1; } (cnt >= K ? ok : ng) = md; } cout << ok << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define ALL(obj) (obj).begin(), (obj).end() #define SPEED \ cin.tie(0); \ ios::sync_with_stdio(false); template <class T> using PQ = priority_queue<T>; template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>; constexpr long long MOD = (long long)1e9 + 7; constexpr long long MOD2 = 998244353; constexpr long long HIGHINF = (long long)1e18; constexpr long long LOWINF = (long long)1e15; constexpr long double PI = 3.1415926535897932384626433; template <class T> vector<T> multivector(size_t N, T init) { return vector<T>(N, init); } template <class... T> auto multivector(size_t N, T... t) { return vector<decltype(multivector(t...))>(N, multivector(t...)); } template <class T> void corner(bool flg, T hoge) { if (flg) { cout << hoge << endl; exit(0); } } template <class T, class U> ostream &operator<<(ostream &o, const map<T, U> &obj) { o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &obj) { o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const multiset<T> &obj) { o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) { o << "{"; for (int i = 0; i < (int)obj.size(); ++i) o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o; } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &obj) { o << "{" << obj.first << ", " << obj.second << "}"; return o; } void print(void) { cout << endl; } template <class Head> void print(Head &&head) { cout << head; print(); } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head << " "; print(forward<Tail>(tail)...); } template <class T> void chmax(T &a, const T b) { a = max(a, b); } template <class T> void chmin(T &a, const T b) { a = min(a, b); } void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; } void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; } void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; } int main() { ll N, K; cin >> N >> K; vector<ll> A, B; for (int i = 0; i < N; ++i) { ll C; cin >> C; (C >= 0 ? A : B).push_back(C); } sort(ALL(A)); sort(ALL(B)); ll X = A.size(); ll Y = B.size(); ll ok = HIGHINF, ng = -HIGHINF, md; while (ok - ng > 1) { md = (ok + ng) / 2; ll cnt = 0; for (int i = 0; i + 1 < X; ++i) { if (A[i] * A[i + 1] > md) continue; int ok2 = i + 1, ng2 = X, md2; while (ng2 - ok2 > 1) { md2 = (ok2 + ng2) / 2; (A[i] * A[md2] <= md ? ok2 : ng2) = md2; } cnt += ok2 - i; } for (int i = Y - 1; 0 <= i - 1; --i) { if (B[i - 1] * B[i] > md) continue; int ok2 = i - 1, ng2 = -1, md2; while (ok2 - ng2 > 1) { md2 = (ok2 + ng2) / 2; (B[i] * B[md2] <= md ? ok2 : ng2) = md2; } cnt += i - ok2; } for (int i = 0; i < X; ++i) { if (B.empty()) continue; if (A[i] * B[0] > md) continue; int ok2 = 0, ng2 = Y, md2; while (ng2 - ok2 > 1) { md2 = (ok2 + ng2) / 2; (A[i] * B[md2] <= md ? ok2 : ng2) = md2; } cnt += ok2 + 1; } (cnt >= K ? ok : ng) = md; } cout << ok << endl; return 0; }
insert
120
120
120
122
0
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; const ll INF = ll(1e18) + 1; ll binary_search(ll x, ll ai, vector<ll> a) { int n = a.size(); int l = -1; int r = n; if (ai < 0) { while (l + 1 < r) { int c = (l + r) / 2; if (ai * a[c] < x) r = c; else l = c; } return n - r; } else { while (l + 1 < r) { int c = (l + r) / 2; if (ai * a[c] < x) l = c; else r = c; } return r; } } int main() { int n; ll k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a.at(i); sort(a.begin(), a.end()); ll l = -INF, r = INF; while (l + 1 < r) { ll x = (l + r) / 2; bool ok = [&] { ll tot = 0; rep(i, n) { tot += binary_search(x, a[i], a); if (a[i] * a[i] < x) tot--; } tot /= 2; return tot < k; }(); if (ok) l = x; else r = x; } cout << l << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; const ll INF = ll(1e18) + 1; ll binary_search(ll x, ll ai, vector<ll> a) { int n = a.size(); int l = -1; int r = n; if (ai < 0) { while (l + 1 < r) { int c = (l + r) / 2; if (ai * a[c] < x) r = c; else l = c; } return n - r; } else { while (l + 1 < r) { int c = (l + r) / 2; if (ai * a[c] < x) l = c; else r = c; } return r; } } int main() { int n; ll k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a.at(i); sort(a.begin(), a.end()); ll l = -INF, r = INF; while (l + 1 < r) { ll x = (l + r) / 2; bool ok = [&] { ll tot = 0; rep(i, n) { if (a[i] < 0) { int l = -1, r = n; while (l + 1 < r) { int c = (l + r) / 2; if (a[c] * a[i] < x) r = c; else l = c; } tot += n - r; } else { int l = -1, r = n; while (l + 1 < r) { int c = (l + r) / 2; if (a[c] * a[i] < x) l = c; else r = c; } tot += r; } if (a[i] * a[i] < x) tot--; } tot /= 2; return tot < k; }(); if (ok) l = x; else r = x; } cout << l << endl; return 0; }
replace
45
46
45
66
TLE
p02774
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; long long tot1, tot2, a[200010], b[200010], c[200010], kk; long long check1(long long ys) { long long ans = 0; for (long long i = 1; i <= tot1; i++) { long long k = 1, s = tot2 + 1; while (k) { if (s > 0 && b[i] * c[s - k] < ys) s -= k, k = k * 2; else k = k / 2; } ans += tot2 - s + 1; } return ans; } long long calc1() { long long l = -2e18, r = -1, aa = 0; while (l <= r) { long long mid = (l + r) / 2, x = check1(mid), y = check1(mid + 1); if (x < kk && kk <= y) return mid; else if (x < kk) l = mid + 1; else r = mid - 1; } } long long check2(long long ys) { long long ans = 0; for (long long i = 1; i <= tot1; i++) { long long k = 1, s = i; while (k) { if (s + k <= tot1 && b[i] * b[s + k] < ys) s += k, k = k * 2; else k = k / 2; } ans += s - i; } for (long long i = 1; i <= tot2; i++) { long long k = 1, s = i; while (k) { if (s + k <= tot2 && c[i] * c[s + k] < ys) s += k, k = k * 2; else k = k / 2; } ans += s - i; } return ans; } long long calc2() { long long l = 1, r = 2e18, aa = 0; while (l <= r) { long long mid = (l + r) / 2, x = check2(mid), y = check2(mid + 1); if (x < kk && kk <= y) return mid; else if (x < kk) l = mid + 1; else r = mid - 1; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, ans1, ans2, ans3, s1 = 0, s2 = 0, s3 = 0; cin >> n >> kk; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) if (a[i] == 0) s1++; else if (a[i] < 0) s2++, b[++tot1] = a[i]; else s3++, c[++tot2] = a[i]; ans1 = s2 * s3; ans2 = s1 * (n - s1) + s1 * (s1 - 1) / 2; ans3 = s2 * (s2 - 1) / 2 + s3 * (s3 - 1) / 2; sort(b + 1, b + tot1 + 1); sort(c + 1, c + tot2 + 1); reverse(b + 1, b + tot1 + 1); if (ans1 < kk && kk <= ans1 + ans2) { cout << 0 << "\n"; return 0; } else if (kk <= ans1) { long long ans = calc1(); cout << ans << "\n"; return 0; } else if (kk > ans1 + ans2) { kk = kk - ans1 - ans2; long long ans = calc2(); cout << ans << "\n"; return 0; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; long long tot1, tot2, a[200010], b[200010], c[200010], kk; long long check1(long long ys) { long long ans = 0; for (long long i = 1; i <= tot1; i++) { long long k = 1, s = tot2 + 1; while (k) { if (s - k > 0 && b[i] * c[s - k] < ys) s -= k, k = k * 2; else k = k / 2; } ans += tot2 - s + 1; } return ans; } long long calc1() { long long l = -2e18, r = -1, aa = 0; while (l <= r) { long long mid = (l + r) / 2, x = check1(mid), y = check1(mid + 1); if (x < kk && kk <= y) return mid; else if (x < kk) l = mid + 1; else r = mid - 1; } } long long check2(long long ys) { long long ans = 0; for (long long i = 1; i <= tot1; i++) { long long k = 1, s = i; while (k) { if (s + k <= tot1 && b[i] * b[s + k] < ys) s += k, k = k * 2; else k = k / 2; } ans += s - i; } for (long long i = 1; i <= tot2; i++) { long long k = 1, s = i; while (k) { if (s + k <= tot2 && c[i] * c[s + k] < ys) s += k, k = k * 2; else k = k / 2; } ans += s - i; } return ans; } long long calc2() { long long l = 1, r = 2e18, aa = 0; while (l <= r) { long long mid = (l + r) / 2, x = check2(mid), y = check2(mid + 1); if (x < kk && kk <= y) return mid; else if (x < kk) l = mid + 1; else r = mid - 1; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, ans1, ans2, ans3, s1 = 0, s2 = 0, s3 = 0; cin >> n >> kk; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) if (a[i] == 0) s1++; else if (a[i] < 0) s2++, b[++tot1] = a[i]; else s3++, c[++tot2] = a[i]; ans1 = s2 * s3; ans2 = s1 * (n - s1) + s1 * (s1 - 1) / 2; ans3 = s2 * (s2 - 1) / 2 + s3 * (s3 - 1) / 2; sort(b + 1, b + tot1 + 1); sort(c + 1, c + tot2 + 1); reverse(b + 1, b + tot1 + 1); if (ans1 < kk && kk <= ans1 + ans2) { cout << 0 << "\n"; return 0; } else if (kk <= ans1) { long long ans = calc1(); cout << ans << "\n"; return 0; } else if (kk > ans1 + ans2) { kk = kk - ans1 - ans2; long long ans = calc2(); cout << ans << "\n"; return 0; } return 0; }
replace
15
16
15
16
0
p02774
C++
Time Limit Exceeded
//////////////////////////////////////// /// tu3 pro-con template /// //////////////////////////////////////// #include "bits/stdc++.h" using namespace std; // -- typedefs -- // #define EPS 1e-9 typedef long long llong; // -- loop macros -- // #define LOOP_TYPE(s, n) \ std::remove_reference_t<std::remove_cv_t<decltype((n) - (s))>> #define FOR(i, s, n) \ for (auto i = LOOP_TYPE(s, n)(s); i < LOOP_TYPE(s, n)(n); i++) #define FORR(i, s, n) \ for (auto i = LOOP_TYPE(s, n)(n) - 1; i != LOOP_TYPE(s, n)(s) - 1; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) FORR(i, 0, n) #define FORE(exp) for (auto &&exp) #define allof(c) c.begin(), c.end() #define partof(c, s, e) c.begin() + (s), c.begin() + (e) // -- functors -- // #define PREDICATE(t, a, ...) [&](const t &a) -> bool { return __VA_ARGS__; } #define PRED(a, ...) PREDICATE(auto, a, __VA_ARGS__) #define COMPARISON(t, a, b, ...) \ [&](const t &a, const t &b) -> bool { return __VA_ARGS__; } #define COMP(a, b, ...) COMPARISON(auto, a, b, __VA_ARGS__) #define CONV1(a, ...) \ [&](const auto &a) -> auto{ return __VA_ARGS__; } #define CONV2(a, b, ...) \ [&](const auto &a, const auto &b) -> auto{ return __VA_ARGS__; } #define CONV3(a, b, c, ...) \ [&](const auto &a, const auto &b, const auto &c) -> auto{ \ return __VA_ARGS__; \ } #define TIE(...) \ auto tie() const { return std::tie(__VA_ARGS__); } #define LESS(t) \ bool operator<(const t &rhs) const { return tie() < rhs.tie(); } #define GREATER(t) \ bool operator>(const t &rhs) const { return tie() > rhs.tie(); } // -- I/O Helper -- // struct _Reader { istream &cin; template <class T> _Reader operator,(T &rhs) { cin >> rhs; return *this; } }; struct _Writer { ostream &cout; const char *d{" "}; bool f{}; template <class T> _Writer operator,(const T &rhs) { cout << (f ? d : "") << rhs; f = 1; return *this; } }; #define READ(t, ...) \ t __VA_ARGS__; \ (_Reader{cin}), __VA_ARGS__ #define WRITE(...) \ do { \ (_Writer{cout}), __VA_ARGS__; \ cout << endl; \ } while (false) #define WRITELN(...) \ do { \ (_Writer{cout, "\n"}), __VA_ARGS__; \ cout << endl; \ } while (false) #define WRITE2D(vevector) FORE(row : vevector) WRITE(row) #ifdef _DEBUG #define DEBUG(...) (_Writer{cerr}), "DEBUG -> ", __VA_ARGS__, "\n" #else #define DEBUG(...) #endif // -- vevector -- // template <class T> struct vevector : vector<vector<T>> { vevector(size_t n = 0, size_t m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) {} }; template <class T> struct vevevector : vector<vevector<T>> { vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {} }; template <class T> struct vevevevector : vector<vevevector<T>> { vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {} }; namespace std { template <class T1, class T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T1, class T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << p.first << " " << p.second; return out; } } // namespace std template <class T> T read() { T t; cin >> t; return t; } template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; } template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; } template <class T> vector<T> readjag() { return read<T>(read<int>()); } template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; } template <class T> struct iter_range_t { T beg, end; }; template <class T> iter_range_t<T> iter_range(T beg, T end) { return iter_range_t<T>{beg, end}; } template <class T> ostream &operator<<(ostream &out, iter_range_t<T> v) { if (v.beg != v.end) { out << *v.beg++; while (v.beg != v.end) { out << " " << *v.beg++; } } return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { return out << iter_range(allof(v)); } // -- etc -- // template <class T> T infinity_value(); template <> int infinity_value<int>() { return int(1) << 30; } template <> llong infinity_value<llong>() { return llong(1) << 60; } template <> double infinity_value<double>() { return 1e+300 * 1e+300; } #define INF(T) infinity_value<T>() inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } template <class TInt> bool in_range(TInt val, TInt min, TInt max) { return val >= min && val < max; } template <> bool in_range<double>(double val, double min, double max) { return val - min > -EPS && val - max < EPS; } template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) { return x >= 0 && x < w && y >= 0 && y < h; } vector<int> iotavn(int start, int count) { vector<int> r(count); iota(allof(r), start); return r; } //// start up //// void solve(); int32_t main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed; cout << setprecision(std::numeric_limits<double>::max_digits10); solve(); return 0; } struct Fixed { template <class F> struct FixedPoint : F { template <class... T> auto operator()(T &&...p) { return F::operator()(*this, std::forward<T>(p)...); } FixedPoint(F &&f) : F(std::forward<F>(f)) {} }; template <class F> static auto fixed(F &&f) { return FixedPoint<F>(std::forward<F>(f)); } }; // にぶたん for 整数。 // lower_bound を求めたければ ok = min, ng = max + 1 とし、 // upper_bound を求めたければ ok = max, ng = min - 1 とするとよいと思う template <class TPred> llong binary_search(TPred pred, llong ok = 0, llong ng = 1000000000000000000 + 1) { while (abs(ok - ng) > 1) { llong m = (ok + ng) / 2; if (pred(m)) ok = m; else ng = m; } return ok; } template <class TPred, class RandomAccessIt> pair<RandomAccessIt, RandomAccessIt> binary_count_if(TPred pred, RandomAccessIt first, RandomAccessIt last) { if (first == last) { return {first, last}; } assert(is_sorted(first, last)); int N = last - first; if (pred(*first)) { return {first, first + binary_search( [&](llong index) { return pred(*(first + index)); }, 0, N) + 1}; } else if (pred(*(last - 1))) { return {first + binary_search( [&](llong index) { return pred(*(first + index)); }, N - 1, -1), last}; } else { return {last, last}; } } //////////////////// //// template end //////////////////// void solve() { READ(llong, N, K); auto A = read<llong>(N); std::sort(allof(A)); llong neg = lower_bound(allof(A), 0) - A.begin(); llong pos = N - neg; if (K <= neg * pos) { auto val = binary_search( [&](llong x) { llong count = 0; REP(i, neg) // negpos { llong a = A[i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, neg, N)); llong posCount = range.second - range.first; count += posCount; } return count >= K; }, 0, -INF(llong)); WRITE(val); } else { K -= neg * pos; auto val = binary_search( [&](llong x) { llong count = 0; REP(i, neg) // negneg { llong a = A[i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, i + 1, neg)); llong posCount = range.second - range.first; count += posCount; } REP(i, pos) // pospos { llong a = A[neg + i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, neg + i + 1, N)); llong posCount = range.second - range.first; count += posCount; } return count >= K; }, INF(llong), -1); WRITE(val); } }
//////////////////////////////////////// /// tu3 pro-con template /// //////////////////////////////////////// #include "bits/stdc++.h" using namespace std; // -- typedefs -- // #define EPS 1e-9 typedef long long llong; // -- loop macros -- // #define LOOP_TYPE(s, n) \ std::remove_reference_t<std::remove_cv_t<decltype((n) - (s))>> #define FOR(i, s, n) \ for (auto i = LOOP_TYPE(s, n)(s); i < LOOP_TYPE(s, n)(n); i++) #define FORR(i, s, n) \ for (auto i = LOOP_TYPE(s, n)(n) - 1; i != LOOP_TYPE(s, n)(s) - 1; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) FORR(i, 0, n) #define FORE(exp) for (auto &&exp) #define allof(c) c.begin(), c.end() #define partof(c, s, e) c.begin() + (s), c.begin() + (e) // -- functors -- // #define PREDICATE(t, a, ...) [&](const t &a) -> bool { return __VA_ARGS__; } #define PRED(a, ...) PREDICATE(auto, a, __VA_ARGS__) #define COMPARISON(t, a, b, ...) \ [&](const t &a, const t &b) -> bool { return __VA_ARGS__; } #define COMP(a, b, ...) COMPARISON(auto, a, b, __VA_ARGS__) #define CONV1(a, ...) \ [&](const auto &a) -> auto{ return __VA_ARGS__; } #define CONV2(a, b, ...) \ [&](const auto &a, const auto &b) -> auto{ return __VA_ARGS__; } #define CONV3(a, b, c, ...) \ [&](const auto &a, const auto &b, const auto &c) -> auto{ \ return __VA_ARGS__; \ } #define TIE(...) \ auto tie() const { return std::tie(__VA_ARGS__); } #define LESS(t) \ bool operator<(const t &rhs) const { return tie() < rhs.tie(); } #define GREATER(t) \ bool operator>(const t &rhs) const { return tie() > rhs.tie(); } // -- I/O Helper -- // struct _Reader { istream &cin; template <class T> _Reader operator,(T &rhs) { cin >> rhs; return *this; } }; struct _Writer { ostream &cout; const char *d{" "}; bool f{}; template <class T> _Writer operator,(const T &rhs) { cout << (f ? d : "") << rhs; f = 1; return *this; } }; #define READ(t, ...) \ t __VA_ARGS__; \ (_Reader{cin}), __VA_ARGS__ #define WRITE(...) \ do { \ (_Writer{cout}), __VA_ARGS__; \ cout << endl; \ } while (false) #define WRITELN(...) \ do { \ (_Writer{cout, "\n"}), __VA_ARGS__; \ cout << endl; \ } while (false) #define WRITE2D(vevector) FORE(row : vevector) WRITE(row) #ifdef _DEBUG #define DEBUG(...) (_Writer{cerr}), "DEBUG -> ", __VA_ARGS__, "\n" #else #define DEBUG(...) #endif // -- vevector -- // template <class T> struct vevector : vector<vector<T>> { vevector(size_t n = 0, size_t m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) {} }; template <class T> struct vevevector : vector<vevector<T>> { vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {} }; template <class T> struct vevevevector : vector<vevevector<T>> { vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {} }; namespace std { template <class T1, class T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T1, class T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << p.first << " " << p.second; return out; } } // namespace std template <class T> T read() { T t; cin >> t; return t; } template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; } template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; } template <class T> vector<T> readjag() { return read<T>(read<int>()); } template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; } template <class T> struct iter_range_t { T beg, end; }; template <class T> iter_range_t<T> iter_range(T beg, T end) { return iter_range_t<T>{beg, end}; } template <class T> ostream &operator<<(ostream &out, iter_range_t<T> v) { if (v.beg != v.end) { out << *v.beg++; while (v.beg != v.end) { out << " " << *v.beg++; } } return out; } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { return out << iter_range(allof(v)); } // -- etc -- // template <class T> T infinity_value(); template <> int infinity_value<int>() { return int(1) << 30; } template <> llong infinity_value<llong>() { return llong(1) << 60; } template <> double infinity_value<double>() { return 1e+300 * 1e+300; } #define INF(T) infinity_value<T>() inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } template <class TInt> bool in_range(TInt val, TInt min, TInt max) { return val >= min && val < max; } template <> bool in_range<double>(double val, double min, double max) { return val - min > -EPS && val - max < EPS; } template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) { return x >= 0 && x < w && y >= 0 && y < h; } vector<int> iotavn(int start, int count) { vector<int> r(count); iota(allof(r), start); return r; } //// start up //// void solve(); int32_t main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed; cout << setprecision(std::numeric_limits<double>::max_digits10); solve(); return 0; } struct Fixed { template <class F> struct FixedPoint : F { template <class... T> auto operator()(T &&...p) { return F::operator()(*this, std::forward<T>(p)...); } FixedPoint(F &&f) : F(std::forward<F>(f)) {} }; template <class F> static auto fixed(F &&f) { return FixedPoint<F>(std::forward<F>(f)); } }; // にぶたん for 整数。 // lower_bound を求めたければ ok = min, ng = max + 1 とし、 // upper_bound を求めたければ ok = max, ng = min - 1 とするとよいと思う template <class TPred> llong binary_search(TPred pred, llong ok = 0, llong ng = 1000000000000000000 + 1) { while (abs(ok - ng) > 1) { llong m = (ok + ng) / 2; if (pred(m)) ok = m; else ng = m; } return ok; } template <class TPred, class RandomAccessIt> pair<RandomAccessIt, RandomAccessIt> binary_count_if(TPred pred, RandomAccessIt first, RandomAccessIt last) { if (first == last) { return {first, last}; } int N = last - first; if (pred(*first)) { return {first, first + binary_search( [&](llong index) { return pred(*(first + index)); }, 0, N) + 1}; } else if (pred(*(last - 1))) { return {first + binary_search( [&](llong index) { return pred(*(first + index)); }, N - 1, -1), last}; } else { return {last, last}; } } //////////////////// //// template end //////////////////// void solve() { READ(llong, N, K); auto A = read<llong>(N); std::sort(allof(A)); llong neg = lower_bound(allof(A), 0) - A.begin(); llong pos = N - neg; if (K <= neg * pos) { auto val = binary_search( [&](llong x) { llong count = 0; REP(i, neg) // negpos { llong a = A[i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, neg, N)); llong posCount = range.second - range.first; count += posCount; } return count >= K; }, 0, -INF(llong)); WRITE(val); } else { K -= neg * pos; auto val = binary_search( [&](llong x) { llong count = 0; REP(i, neg) // negneg { llong a = A[i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, i + 1, neg)); llong posCount = range.second - range.first; count += posCount; } REP(i, pos) // pospos { llong a = A[neg + i]; auto range = binary_count_if(PRED(b, a * b <= x), partof(A, neg + i + 1, N)); llong posCount = range.second - range.first; count += posCount; } return count >= K; }, INF(llong), -1); WRITE(val); } }
replace
223
224
223
224
TLE
p02774
C++
Runtime Error
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, k, ai; int zero; vi posi, nega; signed main() { I(n); I(k); REP(i, n) { I(ai); if (ai == 0) zero++; if (ai < 0) nega.pb(ai); if (ai > 0) posi.pb(ai); } sort(all(posi)); // 1, 2, 3, ... sort(all(nega), greater<int>()); // -1,-2,-3, ... int nposi = sz(posi); int nnega = sz(nega); int npnega = sz(posi) * sz(nega); int npzero = zero * (sz(posi) + sz(nega)) + zero * (zero - 1) / 2; int npposi = nposi * (nposi - 1) / 2 + nnega * (nnega - 1) / 2; assert(n * (n - 1) / 2 == npnega + npzero + npposi); cerr << nposi << " " << nnega << endl; cerr << npnega << " " << npzero << " " << npposi << endl; if (k <= npnega) { int lo = posi[nposi - 1] * nega[nnega - 1] - 1; int hi = posi[0] * nega[0] + 1; while (abs(lo - hi) > 1) { int mid = abs(lo + hi) / 2 * -1; int hit = 0; for (auto i : nega) { hit += posi.end() - lower_bound(all(posi), (mid - abs(i) + 1) / i); } cerr << "mid " << mid << " , hit " << hit << endl; (hit < k ? lo : hi) = mid; } cout << hi << endl; return 0; } k -= npnega; if (k <= npzero) { cout << 0 << endl; return 0; } k -= npzero; REP(i, nnega) nega[i] *= -1; // 1, 2, 3 ... int lo = 0; int hi = 1; if (nposi) hi += posi[nposi - 1]; if (nnega) hi += posi[nnega - 1]; hi *= hi; while (hi - lo > 1) { int mid = (lo + hi) / 2; int hit = 0; if (nposi > 1) REP(ind, nposi) { int i = posi[ind]; int cand = upper_bound(posi.begin(), posi.begin() + ind, mid / i) - posi.begin(); hit += cand; } if (nnega > 1) REP(ind, nnega) { int i = nega[ind]; int cand = upper_bound(nega.begin(), nega.begin() + ind, mid / i) - nega.begin(); hit += cand; } cerr << "mid " << mid << " , hit " << hit << endl; (hit < k ? lo : hi) = mid; } cout << hi << endl; }
//----------------------------templates #pragma GCC optimize("Ofast") #pragma GCC target("tune=native") #pragma GCC target("avx") //---------------------------- #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int ll #define FOR(i, j, n) for (int i = (int)(j); i < (n); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPS(i, n) for (int i = 1; i <= (int)(n); i++) #define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define REPNS(i, n) for (int i = (int)(n); i > 0; i--) #define I(n) scanf("%lld", &(n)) #define LL(n) scanf("%lld", &(n)) #define pb(n) push_back((n)) #define mp(i, j) make_pair((i), (j)) #define eb(i, j) emplace_back((i), (j)) #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef pair<int, int> pi; typedef vector<pi> vpi; typedef vector<vi> vvi; typedef vector<vpi> vvpi; typedef vector<vvi> vvvi; const int mod = 1000000007; //-------------------------------------------- int n, k, ai; int zero; vi posi, nega; signed main() { I(n); I(k); REP(i, n) { I(ai); if (ai == 0) zero++; if (ai < 0) nega.pb(ai); if (ai > 0) posi.pb(ai); } sort(all(posi)); // 1, 2, 3, ... sort(all(nega), greater<int>()); // -1,-2,-3, ... int nposi = sz(posi); int nnega = sz(nega); int npnega = sz(posi) * sz(nega); int npzero = zero * (sz(posi) + sz(nega)) + zero * (zero - 1) / 2; int npposi = nposi * (nposi - 1) / 2 + nnega * (nnega - 1) / 2; assert(n * (n - 1) / 2 == npnega + npzero + npposi); cerr << nposi << " " << nnega << endl; cerr << npnega << " " << npzero << " " << npposi << endl; if (k <= npnega) { int lo = posi[nposi - 1] * nega[nnega - 1] - 1; int hi = posi[0] * nega[0] + 1; while (abs(lo - hi) > 1) { int mid = abs(lo + hi) / 2 * -1; int hit = 0; for (auto i : nega) { hit += posi.end() - lower_bound(all(posi), (mid - abs(i) + 1) / i); } cerr << "mid " << mid << " , hit " << hit << endl; (hit < k ? lo : hi) = mid; } cout << hi << endl; return 0; } k -= npnega; if (k <= npzero) { cout << 0 << endl; return 0; } k -= npzero; REP(i, nnega) nega[i] *= -1; // 1, 2, 3 ... int lo = 0; int hi = 1; int adder = 1; if (nposi > 1) adder = max(adder, posi[nposi - 1]); if (nnega > 1) adder = max(adder, nega[nnega - 1]); hi += adder; hi *= hi; while (hi - lo > 1) { int mid = (lo + hi) / 2; int hit = 0; if (nposi > 1) REP(ind, nposi) { int i = posi[ind]; int cand = upper_bound(posi.begin(), posi.begin() + ind, mid / i) - posi.begin(); hit += cand; } if (nnega > 1) REP(ind, nnega) { int i = nega[ind]; int cand = upper_bound(nega.begin(), nega.begin() + ind, mid / i) - nega.begin(); hit += cand; } cerr << "mid " << mid << " , hit " << hit << endl; (hit < k ? lo : hi) = mid; } cout << hi << endl; }
replace
97
101
97
103
0
2 2 4 0 2 mid -9 , hit 2 mid -7 , hit 2 mid -6 , hit 4
p02774
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repr(i, n) for (int i = (n); i >= 0; --i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define FORR(i, m, n) for (int i = (m); i >= (n); --i) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long ll; typedef long double ld; const ll mod = 1000000007; const ll mod2 = 998244353; const int inf = 2e9; const ll INF = 1e18; const ld EPS = 1e-10; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); vector<ll> sgn(3); sgn[0] = lower_bound(a.begin(), a.end(), (ll)0) - a.begin(); sgn[1] = upper_bound(a.begin(), a.end(), (ll)0) - lower_bound(a.begin(), a.end(), (ll)0); sgn[2] = a.end() - upper_bound(a.begin(), a.end(), (ll)0); vector<ll> cnt(3); cnt[0] = sgn[0] * sgn[2]; cnt[2] = sgn[0] * (sgn[0] - 1) / 2 + sgn[2] * (sgn[2] - 1) / 2; cnt[1] = n * (n - 1) / 2 - cnt[0] - cnt[2]; if (cnt[0] >= k) { vector<ll> aneg(sgn[0]), apos(sgn[2]); rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i]; rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]]; ll l = 0, r = INF; while (r - l > 1) { ll m = (l + r) / 2, res = cnt[0]; rep(i, sgn[0]) { ll d = m / aneg[i]; res -= upper_bound(apos.begin(), apos.end(), d) - apos.begin(); } if (res >= k) l = m; else r = m; } cout << -r << endl; } else if (cnt[0] < k && cnt[0] + cnt[1] >= k) { cout << 0 << endl; } else { vector<ll> aneg(sgn[0]), apos(sgn[2]); rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i]; rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]]; ll l = 0, r = INF; while (r - l > 1) { ll m = (l + r) / 2, res = cnt[0] + cnt[1]; rep(i, sgn[0]) { ll d = m / aneg[i]; ll now = upper_bound(aneg.begin(), aneg.end(), d) - aneg.begin() - i; if (d >= aneg[i]) now--; if (now > 0) res += now; } rep(i, sgn[2]) { ll d = m / apos[i]; ll now = upper_bound(apos.begin(), apos.end(), d) - apos.begin() - i; if (d >= aneg[i]) now--; if (now > 0) res += now; } if (res >= k) r = m; else l = m; } cout << r << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repr(i, n) for (int i = (n); i >= 0; --i) #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define FORR(i, m, n) for (int i = (m); i >= (n); --i) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long ll; typedef long double ld; const ll mod = 1000000007; const ll mod2 = 998244353; const int inf = 2e9; const ll INF = 1e18; const ld EPS = 1e-10; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); vector<ll> sgn(3); sgn[0] = lower_bound(a.begin(), a.end(), (ll)0) - a.begin(); sgn[1] = upper_bound(a.begin(), a.end(), (ll)0) - lower_bound(a.begin(), a.end(), (ll)0); sgn[2] = a.end() - upper_bound(a.begin(), a.end(), (ll)0); vector<ll> cnt(3); cnt[0] = sgn[0] * sgn[2]; cnt[2] = sgn[0] * (sgn[0] - 1) / 2 + sgn[2] * (sgn[2] - 1) / 2; cnt[1] = n * (n - 1) / 2 - cnt[0] - cnt[2]; if (cnt[0] >= k) { vector<ll> aneg(sgn[0]), apos(sgn[2]); rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i]; rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]]; ll l = 0, r = INF; while (r - l > 1) { ll m = (l + r) / 2, res = cnt[0]; rep(i, sgn[0]) { ll d = m / aneg[i]; res -= upper_bound(apos.begin(), apos.end(), d) - apos.begin(); } if (res >= k) l = m; else r = m; } cout << -r << endl; } else if (cnt[0] < k && cnt[0] + cnt[1] >= k) { cout << 0 << endl; } else { vector<ll> aneg(sgn[0]), apos(sgn[2]); rep(i, sgn[0]) aneg[i] = -a[sgn[0] - 1 - i]; rep(i, sgn[2]) apos[i] = a[i + sgn[0] + sgn[1]]; ll l = 0, r = INF; while (r - l > 1) { ll m = (l + r) / 2, res = cnt[0] + cnt[1]; rep(i, sgn[0]) { ll d = m / aneg[i]; ll now = upper_bound(aneg.begin(), aneg.end(), d) - aneg.begin() - i; if (d >= aneg[i]) now--; if (now > 0) res += now; } rep(i, sgn[2]) { ll d = m / apos[i]; ll now = upper_bound(apos.begin(), apos.end(), d) - apos.begin() - i; if (d >= apos[i]) now--; if (now > 0) res += now; } if (res >= k) r = m; else l = m; } cout << r << endl; } return 0; }
replace
70
71
70
71
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll count_neg(vector<ll> &neg, vector<ll> &pos, ll x) { // x以下の組み合わせを数え上げる ll cnt = 0; for (const auto &val : pos) { ll ng = -1; ll ok = neg.size(); auto isOK = [&](ll mid) { return val * neg[mid] > x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } cnt += ng + 1; } return cnt; } void negative_routine(vector<ll> &neg, vector<ll> &pos, ll K) { ll right = 0; ll left = neg.front() * pos.back() - 1; while (left + 1 < right) { ll cand = (left + right) / 2; ll ret = count_neg(neg, pos, cand); // cand以下の個数がK個より大きければ  if (ret >= K) { right = cand; } else { left = cand; } } // x 以下の組み合わせがK個以上である を始めてみたすxがright // x 以下の組み合わせが1個以上であるを 初めて満たすのは -12 printf("%lld\n", right); } ll count_pos(vector<ll> &neg, vector<ll> &pos, ll x) { // x以下のものをカウントする ll cnt = 0; for (const auto &val : pos) { // ng側に val*pos <= xを集めたい ll ng = -1; ll ok = pos.size(); auto isOK = [&](ll key) { return val * pos[key] > x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } if (ng >= 0 && pos[ng] >= val) cnt -= 1; cnt += ng + 1; } for (const auto &val : neg) { ll ng = -1; ll ok = neg.size(); auto isOK = [&](ll key) { return val * neg[key] <= x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } if (ok < neg.size() && neg[ok] <= val) cnt -= 1; // okなものが存在して、val以下のモノがあったら重複が出ている -3, -2, // -1のならびで neg[ok]が-2 valが-1ならと考えると // ok は -2,-1で -1 含んでおり neg[ok]<= val = -1 cnt += neg.size() - ok; } return cnt / 2; } void positive_routine(vector<ll> &neg, vector<ll> &pos, ll K) { ll cand2 = (neg.size() > 0) ? 0 : neg.front() * neg.front() + 1; ll cand1 = (pos.size() > 0) ? 0 : pos.back() * pos.back() + 1; ll right = max(cand1, cand2); ll left = 0; while (left + 1 < right) { ll cand = (left + right) / 2; ll ret = count_pos(neg, pos, cand); if (ret >= K) { right = cand; } else { left = cand; } } printf("%lld\n", right); } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { ll N, K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } sort(A.begin(), A.end()); vector<ll> neg; vector<ll> zeros; vector<ll> pos; for (const auto &val : A) { if (val < 0) neg.push_back(val); else if (val == 0) zeros.push_back(0); else pos.push_back(val); } ll negative_count = neg.size() * pos.size(); ll zero_count = zeros.size() * (N - zeros.size()) + (zeros.size() - 1) * zeros.size() / 2; if (K <= negative_count) { negative_routine(neg, pos, K); } else if (K <= negative_count + zero_count) { printf("0\n"); } else { positive_routine(neg, pos, K - negative_count - zero_count); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll count_neg(vector<ll> &neg, vector<ll> &pos, ll x) { // x以下の組み合わせを数え上げる ll cnt = 0; for (const auto &val : pos) { ll ng = -1; ll ok = neg.size(); auto isOK = [&](ll mid) { return val * neg[mid] > x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } cnt += ng + 1; } return cnt; } void negative_routine(vector<ll> &neg, vector<ll> &pos, ll K) { ll right = 0; ll left = neg.front() * pos.back() - 1; while (left + 1 < right) { ll cand = (left + right) / 2; ll ret = count_neg(neg, pos, cand); // cand以下の個数がK個より大きければ  if (ret >= K) { right = cand; } else { left = cand; } } // x 以下の組み合わせがK個以上である を始めてみたすxがright // x 以下の組み合わせが1個以上であるを 初めて満たすのは -12 printf("%lld\n", right); } ll count_pos(vector<ll> &neg, vector<ll> &pos, ll x) { // x以下のものをカウントする ll cnt = 0; for (const auto &val : pos) { // ng側に val*pos <= xを集めたい ll ng = -1; ll ok = pos.size(); auto isOK = [&](ll key) { return val * pos[key] > x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } if (ng >= 0 && pos[ng] >= val) cnt -= 1; cnt += ng + 1; } for (const auto &val : neg) { ll ng = -1; ll ok = neg.size(); auto isOK = [&](ll key) { return val * neg[key] <= x; }; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } if (ok < neg.size() && neg[ok] <= val) cnt -= 1; // okなものが存在して、val以下のモノがあったら重複が出ている -3, -2, // -1のならびで neg[ok]が-2 valが-1ならと考えると // ok は -2,-1で -1 含んでおり neg[ok]<= val = -1 cnt += neg.size() - ok; } return cnt / 2; } void positive_routine(vector<ll> &neg, vector<ll> &pos, ll K) { ll cand2 = (neg.size() > 0) ? neg.front() * neg.front() + 1 : 1; ll cand1 = (pos.size() > 0) ? pos.back() * pos.back() + 1 : 1; ll right = max(cand1, cand2); ll left = 0; while (left + 1 < right) { ll cand = (left + right) / 2; ll ret = count_pos(neg, pos, cand); if (ret >= K) { right = cand; } else { left = cand; } } printf("%lld\n", right); } // Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You // use the default template now. You can remove this line by using your custom // template) int main() { ll N, K; cin >> N >> K; vector<int> A(N); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } sort(A.begin(), A.end()); vector<ll> neg; vector<ll> zeros; vector<ll> pos; for (const auto &val : A) { if (val < 0) neg.push_back(val); else if (val == 0) zeros.push_back(0); else pos.push_back(val); } ll negative_count = neg.size() * pos.size(); ll zero_count = zeros.size() * (N - zeros.size()) + (zeros.size() - 1) * zeros.size() / 2; if (K <= negative_count) { negative_routine(neg, pos, K); } else if (K <= negative_count + zero_count) { printf("0\n"); } else { positive_routine(neg, pos, K - negative_count - zero_count); } return 0; }
replace
83
85
83
85
0
p02774
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <clocale> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); #define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i) #define FORS(i, s, n) for (int i = (s), i##_len = (n); i <= i##_len; ++i) #define VFOR(i, s, n) for (int i = (s); i < (n); ++i) #define VFORS(i, s, n) for (int i = (s); i <= (n); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FORS(i, 0, n) #define VREP(i, n) VFOR(i, 0, n) #define VREPS(i, n) VFORS(i, 0, n) #define RFOR(i, s, n) for (int i = (s), i##_len = (n); i >= i##_len; --i) #define RFORS(i, s, n) for (int i = (s), i##_len = (n); i > i##_len; --i) #define RREP(i, n) RFOR(i, n, 0) #define RREPS(i, n) RFORS(i, n, 0) #define ALL(v) (v).begin(), (v).end() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(ALL(v), greater<decltype(v[0])>()) #define SZ(x) ((int)(x).size()) #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()) using ld = long double; using ll = long long; using ui = unsigned int; using ull = unsigned long long; using Pi_i = pair<int, int>; using Pll_ll = pair<ll, ll>; using VB = vector<bool>; using VC = vector<char>; using VD = vector<double>; using VI = vector<int>; using VLL = vector<ll>; using VS = vector<string>; using VSH = vector<short>; using VULL = vector<ull>; const int MOD = 1000000007; const int INF = 1000000000; const int NIL = -1; const double EPS = 1E-10; template <class T, class S> bool chmax(T &a, const S &b) { if (a < b) { a = b; return true; } return false; } template <class T, class S> bool chmin(T &a, const S &b) { if (b < a) { a = b; return true; } return false; } int main() { ll N, K; cin >> N >> K; ll n, z = 0, p; VLL neg, pos; REP(i, N) { int A; cin >> A; if (A < 0) neg.PB(A); if (A == 0) ++z; if (A > 0) pos.PB(A); } n = neg.size(); p = pos.size(); SORT(neg); SORT(pos); if (n * p >= K) { // 答えは負の数, [lt, rt[ ll lt = neg[0] * pos[p - 1], rt = 0; while (lt + 1 < rt) { ll mid = (lt + rt) / 2; int j = 0; ll cnt = 0; REP(i, n) { while (j < p && neg[i] * pos[j] >= mid) ++j; if (j == p) break; cnt += p - j; } if (cnt < K) lt = mid; else rt = mid; } cout << lt << endl; return 0; } else if (n * p + z * (n + p) + z * (z - 1) / 2 >= K) { cout << 0 << endl; return 0; } else { // 答えは正の数, ]lt, rt] ll more = N * (N - 1) / 2 - K + 1; ll lt = 0, rt = 1; if (!n) rt = rt = neg[0] * neg[0] + 1; if (!p) chmax(rt, pos[p - 1] * pos[p - 1] + 1); while (lt + 1 < rt) { ll mid = (lt + rt) / 2; ll cnt = 0, sqcnt = 0; int j = 0; RREP(i, p - 1) { if (pos[i] * pos[i] > mid) ++sqcnt; while (j < p && pos[i] * pos[j] <= mid) ++j; if (j == p) break; cnt += p - j; } j = n - 1; REP(i, n) { if (neg[i] * neg[i] > mid) ++sqcnt; while (j >= 0 && neg[i] * neg[j] <= mid) --j; if (j == -1) break; cnt += j + 1; } cnt = (cnt - sqcnt) / 2; if (cnt < more) rt = mid; else lt = mid; } cout << rt << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <clocale> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); #define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i) #define FORS(i, s, n) for (int i = (s), i##_len = (n); i <= i##_len; ++i) #define VFOR(i, s, n) for (int i = (s); i < (n); ++i) #define VFORS(i, s, n) for (int i = (s); i <= (n); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FORS(i, 0, n) #define VREP(i, n) VFOR(i, 0, n) #define VREPS(i, n) VFORS(i, 0, n) #define RFOR(i, s, n) for (int i = (s), i##_len = (n); i >= i##_len; --i) #define RFORS(i, s, n) for (int i = (s), i##_len = (n); i > i##_len; --i) #define RREP(i, n) RFOR(i, n, 0) #define RREPS(i, n) RFORS(i, n, 0) #define ALL(v) (v).begin(), (v).end() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(ALL(v), greater<decltype(v[0])>()) #define SZ(x) ((int)(x).size()) #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define BIT(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()) using ld = long double; using ll = long long; using ui = unsigned int; using ull = unsigned long long; using Pi_i = pair<int, int>; using Pll_ll = pair<ll, ll>; using VB = vector<bool>; using VC = vector<char>; using VD = vector<double>; using VI = vector<int>; using VLL = vector<ll>; using VS = vector<string>; using VSH = vector<short>; using VULL = vector<ull>; const int MOD = 1000000007; const int INF = 1000000000; const int NIL = -1; const double EPS = 1E-10; template <class T, class S> bool chmax(T &a, const S &b) { if (a < b) { a = b; return true; } return false; } template <class T, class S> bool chmin(T &a, const S &b) { if (b < a) { a = b; return true; } return false; } int main() { ll N, K; cin >> N >> K; ll n, z = 0, p; VLL neg, pos; REP(i, N) { int A; cin >> A; if (A < 0) neg.PB(A); if (A == 0) ++z; if (A > 0) pos.PB(A); } n = neg.size(); p = pos.size(); SORT(neg); SORT(pos); if (n * p >= K) { // 答えは負の数, [lt, rt[ ll lt = neg[0] * pos[p - 1], rt = 0; while (lt + 1 < rt) { ll mid = (lt + rt) / 2; int j = 0; ll cnt = 0; REP(i, n) { while (j < p && neg[i] * pos[j] >= mid) ++j; if (j == p) break; cnt += p - j; } if (cnt < K) lt = mid; else rt = mid; } cout << lt << endl; return 0; } else if (n * p + z * (n + p) + z * (z - 1) / 2 >= K) { cout << 0 << endl; return 0; } else { // 答えは正の数, ]lt, rt] ll more = N * (N - 1) / 2 - K + 1; ll lt = 0, rt = 1; if (n) rt = neg[0] * neg[0] + 1; if (p) chmax(rt, pos[p - 1] * pos[p - 1] + 1); while (lt + 1 < rt) { ll mid = (lt + rt) / 2; ll cnt = 0, sqcnt = 0; int j = 0; RREP(i, p - 1) { if (pos[i] * pos[i] > mid) ++sqcnt; while (j < p && pos[i] * pos[j] <= mid) ++j; if (j == p) break; cnt += p - j; } j = n - 1; REP(i, n) { if (neg[i] * neg[i] > mid) ++sqcnt; while (j >= 0 && neg[i] * neg[j] <= mid) --j; if (j == -1) break; cnt += j + 1; } cnt = (cnt - sqcnt) / 2; if (cnt < more) rt = mid; else lt = mid; } cout << rt << endl; } return 0; }
replace
137
140
137
140
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200005; int n; ll k; int a[N]; ll f(ll lim) { ll ans = 0; int neg_ptr = 0, pos_ptr = n; for (int i = 0; i < n; i++) { if (a[i] == 0) { if (lim >= 0) ans += n - 1; } else if (a[i] > 0) { while (pos_ptr - 1 >= 0 and 1LL * a[i] * a[pos_ptr - 1] > lim) { pos_ptr--; } if (i < pos_ptr) ans += pos_ptr - 1; else ans += pos_ptr; } else { // a[i] < 0 while (neg_ptr < n and 1LL * a[i] * a[neg_ptr] > lim) { neg_ptr++; } while (neg_ptr - 1 >= 0 and 1LL * a[i] * a[neg_ptr - 1] <= lim) neg_ptr--; if (i >= neg_ptr) ans += n - neg_ptr - 1; else ans += n - neg_ptr; } } assert(ans % 2 == 0); return ans / 2; } int main() { scanf("%d %lld", &n, &k); for (int i = 0; i < n; i++) scanf("%d", a + i); sort(a, a + n); ll lo = -1e18, hi = 1e18, ans = 0; while (lo <= hi) { ll mid = (lo + hi) / 2; if (f(mid) >= k) ans = mid, hi = mid - 1; else lo = mid + 1; } printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 200005; int n; ll k; int a[N]; ll f(ll lim) { ll ans = 0; int neg_ptr = 0, pos_ptr = n; for (int i = 0; i < n; i++) { if (a[i] == 0) { if (lim >= 0) ans += n - 1; } else if (a[i] > 0) { while (pos_ptr - 1 >= 0 and 1LL * a[i] * a[pos_ptr - 1] > lim) { pos_ptr--; } while (pos_ptr < n and 1LL * a[i] * a[pos_ptr] <= lim) pos_ptr++; if (i < pos_ptr) ans += pos_ptr - 1; else ans += pos_ptr; } else { // a[i] < 0 while (neg_ptr < n and 1LL * a[i] * a[neg_ptr] > lim) { neg_ptr++; } while (neg_ptr - 1 >= 0 and 1LL * a[i] * a[neg_ptr - 1] <= lim) neg_ptr--; if (i >= neg_ptr) ans += n - neg_ptr - 1; else ans += n - neg_ptr; } } assert(ans % 2 == 0); return ans / 2; } int main() { scanf("%d %lld", &n, &k); for (int i = 0; i < n; i++) scanf("%d", a + i); sort(a, a + n); ll lo = -1e18, hi = 1e18, ans = 0; while (lo <= hi) { ll mid = (lo + hi) / 2; if (f(mid) >= k) ans = mid, hi = mid - 1; else lo = mid + 1; } printf("%lld\n", ans); }
insert
25
25
25
27
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i) const int MAX_N = 200; int N; ll K; ll A[MAX_N]; const ll INF = (ll)1e18 + 1LL; bool cmp(ll a, ll b, ll X) { return a * b < X; } int b_search(ll key, ll X, bool right_true, bool (*isOK)(ll, ll, ll)) { int ng = right_true ? N : -1; int ok = right_true ? -1 : N; while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isOK(key, A[mid], X)) ok = mid; else ng = mid; } return ok; } ll order_ascending(ll X) { // 積がX未満になるのが何個あるか ll res = 0; for (int i = 0; i < N; ++i) { if (A[i] >= 0) { int candidate = b_search(A[i], X, true, cmp); if (i <= candidate) --candidate; res += (ll)candidate + 1; } else { int candidate = b_search(A[i], X, false, cmp); if (candidate <= i) ++candidate; res += (ll)N - candidate; } } return res / 2LL; } int main() { scanf("%d %lld", &N, &K); for (int i = 0; i < N; ++i) scanf("%lld", &A[i]); sort(A, A + N); ll low = -INF, high = INF; while (abs(high - low) > 1) { ll mid = (low + high) / 2; if (order_ascending(mid) < K) low = mid; else high = mid; } printf("%lld\n", low); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, a, b) for (int i = a, i##_len = (b); i < i##_len; ++i) const int MAX_N = 200100; int N; ll K; ll A[MAX_N]; const ll INF = (ll)1e18 + 1LL; bool cmp(ll a, ll b, ll X) { return a * b < X; } int b_search(ll key, ll X, bool right_true, bool (*isOK)(ll, ll, ll)) { int ng = right_true ? N : -1; int ok = right_true ? -1 : N; while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isOK(key, A[mid], X)) ok = mid; else ng = mid; } return ok; } ll order_ascending(ll X) { // 積がX未満になるのが何個あるか ll res = 0; for (int i = 0; i < N; ++i) { if (A[i] >= 0) { int candidate = b_search(A[i], X, true, cmp); if (i <= candidate) --candidate; res += (ll)candidate + 1; } else { int candidate = b_search(A[i], X, false, cmp); if (candidate <= i) ++candidate; res += (ll)N - candidate; } } return res / 2LL; } int main() { scanf("%d %lld", &N, &K); for (int i = 0; i < N; ++i) scanf("%lld", &A[i]); sort(A, A + N); ll low = -INF, high = INF; while (abs(high - low) > 1) { ll mid = (low + high) / 2; if (order_ascending(mid) < K) low = mid; else high = mid; } printf("%lld\n", low); return 0; }
replace
5
6
5
6
0
p02774
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include <conio.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define rep(i, n) for (ll i = 0; i < (n); i++) #define SZ(x) ((ll)x.size()) #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define F first #define S second #define int ll #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const ll MOD = INT_MAX; const double PI = acos(-1); void openfile() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } const int N = 2005; // vector<int> a; int a[N]; int n, k; int count(int x) { int cnt = 0; rep(i, n) { if (a[i] * a[i] <= x) cnt--; if (a[i] == 0 && x >= 0) cnt += n; else if (a[i] < 0) { // cnt += n - (lower_bound(a.begin(), a.end(), (long double)x / a[i]) - // a.begin()); cnt += n - (lower_bound(a, a + n, (long double)x / a[i]) - a); } else if (a[i] > 0) { // int pos = upper_bound(a.begin(), a.end(), (long double)x / a[i]) - // a.begin(); cnt += pos; cnt += upper_bound(a, a + n, (long double)x / a[i]) - a; } // if(a[i] * a[i] < x) cnt--; // if(a[i] <= 0){ // negative or zero // int l = -1,r = n; // while(r-l > 1){ // int mid = (l+r) / 2; // if(a[i] * a[mid] < x) r = mid; // else l = mid; // } // cnt += n-r; // } // else{ // positive // int l = -1,r = n; // while(r-l > 1){ // int mid = (l+r) / 2; // if(a[i] * a[mid] < x) l = mid; // else r = mid; // } // cnt += r; // } // cout << cnt << ' '; } // cout << endl << x << ' ' << cnt << endl; return cnt / 2; } // O(n * log(n) * log(C)), C = 1e18 signed main() { IOS cin >> n >> k; // a.resize(n); rep(i, n) cin >> a[i]; // sort(a.begin(), a.end()); sort(a, a + n); int l = -1e18 + 7, r = 1e18 + 7; while (r - l > 1) { int mid = (l + r) / 2; if (count(mid) < k) l = mid; else r = mid; } cout << r << endl; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include <conio.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define rep(i, n) for (ll i = 0; i < (n); i++) #define SZ(x) ((ll)x.size()) #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define F first #define S second #define int ll #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); const ll MOD = INT_MAX; const double PI = acos(-1); void openfile() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } const int N = 2e5 + 5; // vector<int> a; int a[N]; int n, k; int count(int x) { int cnt = 0; rep(i, n) { if (a[i] * a[i] <= x) cnt--; if (a[i] == 0 && x >= 0) cnt += n; else if (a[i] < 0) { // cnt += n - (lower_bound(a.begin(), a.end(), (long double)x / a[i]) - // a.begin()); cnt += n - (lower_bound(a, a + n, (long double)x / a[i]) - a); } else if (a[i] > 0) { // int pos = upper_bound(a.begin(), a.end(), (long double)x / a[i]) - // a.begin(); cnt += pos; cnt += upper_bound(a, a + n, (long double)x / a[i]) - a; } // if(a[i] * a[i] < x) cnt--; // if(a[i] <= 0){ // negative or zero // int l = -1,r = n; // while(r-l > 1){ // int mid = (l+r) / 2; // if(a[i] * a[mid] < x) r = mid; // else l = mid; // } // cnt += n-r; // } // else{ // positive // int l = -1,r = n; // while(r-l > 1){ // int mid = (l+r) / 2; // if(a[i] * a[mid] < x) l = mid; // else r = mid; // } // cnt += r; // } // cout << cnt << ' '; } // cout << endl << x << ' ' << cnt << endl; return cnt / 2; } // O(n * log(n) * log(C)), C = 1e18 signed main() { IOS cin >> n >> k; // a.resize(n); rep(i, n) cin >> a[i]; // sort(a.begin(), a.end()); sort(a, a + n); int l = -1e18 + 7, r = 1e18 + 7; while (r - l > 1) { int mid = (l + r) / 2; if (count(mid) < k) l = mid; else r = mid; } cout << r << endl; }
replace
28
29
28
29
0
p02774
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; #define INF 100000000; ll n, k; int64_t intceil(int64_t a, int64_t b) { if (a >= 0) { return (a + b - 1) / b; } else { return a / b; } } int64_t intfloor(int64_t a, int64_t b) { if (a < 0) { return (a - b + 1) / b; } else { return a / b; } } int main() { cin >> n >> k; vector<int> a(n); vector<ll> b(n); for (int i = 0; i < n; i++) { ll z; cin >> z; a[i] = z; b[i] = z * z; } sort(a.begin(), a.end()); ll lb = -1e18 - 1, ub = 1e18; int i = 0; while (lb + 1 < ub) { bool fl = lb < ub; ll mid = (ub + lb) / 2; ll cnt = 0; // mid以下の個数 for (int i = 0; i < n; i++) { if (a[i] < 0) { cnt += a.begin() + i - lower_bound(a.begin(), a.begin() + i, intceil(-mid, -a[i])); } else if (a[i] > 0) { // cnt+=upper_bound(a.begin(),a.end(),intfloor(mid,a[i]))-a.begin(); cnt += upper_bound(a.begin(), a.begin() + i, intfloor(mid, a[i])) - a.begin(); } else { if (mid >= 0) { cnt += i; } } // if (b[i]<=mid) cnt--; } // cnt/=2; if (cnt >= k) { ub = mid; } else { lb = mid; } } cout << ub << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define INF 100000000; ll n, k; int64_t intceil(int64_t a, int64_t b) { if (a >= 0) { return (a + b - 1) / b; } else { return a / b; } } int64_t intfloor(int64_t a, int64_t b) { if (a < 0) { return (a - b + 1) / b; } else { return a / b; } } int main() { cin >> n >> k; vector<int> a(n); vector<ll> b(n); for (int i = 0; i < n; i++) { ll z; cin >> z; a[i] = z; b[i] = z * z; } sort(a.begin(), a.end()); ll lb = -1e18 - 1, ub = 1e18; int i = 0; while (lb + 1 < ub) { bool fl = lb < ub; ll mid = (ub + lb) / 2; ll cnt = 0; // mid以下の個数 for (int i = 0; i < n; i++) { if (a[i] < 0) { cnt += a.begin() + i - lower_bound(a.begin(), a.begin() + i, intceil(-mid, -a[i])); } else if (a[i] > 0) { // cnt+=upper_bound(a.begin(),a.end(),intfloor(mid,a[i]))-a.begin(); cnt += upper_bound(a.begin(), a.begin() + i, intfloor(mid, a[i])) - a.begin(); } else { if (mid >= 0) { cnt += i; } } // if (b[i]<=mid) cnt--; } // cnt/=2; if (cnt >= k) { ub = mid; } else { lb = mid; } } cout << ub << endl; }
delete
0
1
0
0
TLE
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // template {{{ #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #define f first #define s second #define resz resize #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto &a : x) #define sort_by(x, y) \ sort(all(x), [&](const auto &a, const auto &b) { return y; }) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); } template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2> &p); template <class T> void re(vector<T> &a); template <class T, size_t SZ> void re(array<T, SZ> &a); template <class T> void re(T &x) { cin >> x; } void re(double &x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2> &p) { re(p.f, p.s); } template <class T> void re(vector<T> &a) { F0R(i, sz(a)) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ> &a) { F0R(i, SZ) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2> &x); template <class T, size_t SZ> void pr(const array<T, SZ> &x); template <class T> void pr(const vector<T> &x); template <class T> void pr(const deque<T> &x); template <class T> void pr(const set<T> &x); template <class T1, class T2> void pr(const map<T1, T2> &x); template <class T> void pr(const T &x) { cout << x; } template <class Arg, class... Args> void pr(const Arg &first, const Args &...rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); } template <class T, bool pretty = true> void prContain(const T &x) { if (pretty) pr("{"); bool fst = 1; for (const auto &a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0; if (pretty) pr("}"); } template <class T> void pc(const T &x) { prContain<T, false>(x); pr("\n"); } template <class T, size_t SZ> void pr(const array<T, SZ> &x) { prContain(x); } template <class T> void pr(const vector<T> &x) { prContain(x); } template <class T> void pr(const deque<T> &x) { prContain(x); } template <class T> void pr(const set<T> &x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2> &x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg &first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg &first, const Args &...rest) { pr(first, " "); ps(rest...); } } // namespace __output using namespace __output; #define TRACE(x) x #define __pn(x) pr(#x, " = ") #define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush namespace __algorithm { template <typename T> void dedup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T> &v, const T &x) { auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T> &v, const T &x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C &v, T id, OP op) { vector<T> r(sz(v) + 1, id); F0R(i, sz(v)) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C &v, T id, OP op) { vector<T> r(sz(v) + 1, id); F0Rd(i, sz(v)) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" struct monostate { friend istream &operator>>(istream &is, const monostate &ms) { return is; } friend ostream &operator<<(ostream &os, const monostate &ms) { return os; } friend monostate operator+(const monostate &a, const monostate &b) { return a; } } ms; #pragma GCC diagnostic pop namespace __io { void setIn(string s) { freopen(s.c_str(), "r", stdin); } void setOut(string s) { freopen(s.c_str(), "w", stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); if (sz(s)) { setIn(s + ".in"), setOut(s + ".out"); } } } // namespace __io using namespace __io; // }}} // integer division in C++ rounds towards 0 ll floor(ll top, ll bot) { assert(bot > 0); if (top < 0) { return (top - bot + 1) / bot; } else { return top / bot; } } ll ceil(ll top, ll bot) { assert(bot > 0); if (top < 0) { return top / bot; } else { return (top + bot - 1) / bot; } } int main() { setIO(); int N; ll K; re(N, K); vi a(N); re(a); sort(all(a)); auto count_geq = [&](ll v) { ll ans = 0; trav(_fv, a) { ll fv = _fv; if (fv == 0) { if (v <= 0) ans += N - 1; continue; } if (fv < 0) { // < 0 ll minv = floor(-v, -fv); ans += ub(all(a), '0', [&](char _, int pos) { // return fv * pos < v; // want pos > v/fv return pos > minv; }) - a.begin(); } else { // >= 0 ll minv = ceil(v, fv); ans += a.end() - ub(all(a), '0', [&](char _, int pos) { // return fv * pos >= v; // want pos >= v / fv return pos >= minv; }); } if (fv * fv >= v) ans--; } return ans / 2; }; auto count_lt = [&](ll v) { return ll(N) * (N - 1) / 2 - count_geq(v); }; --K; ll lo = -1e18, hi = 1e18 + 1; while (hi - lo > 1) { ll mi = (lo + 1 + hi) / 2; if (count_lt(mi) <= K) lo = mi; else hi = mi; } ps(lo); // did you check N=1? did you mix up N,M? // check your "infinity" and "null" values against the bounds return 0; }
#include <bits/stdc++.h> using namespace std; // template {{{ #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #define f first #define s second #define resz resize #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto &a : x) #define sort_by(x, y) \ sort(all(x), [&](const auto &a, const auto &b) { return y; }) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); } template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2> &p); template <class T> void re(vector<T> &a); template <class T, size_t SZ> void re(array<T, SZ> &a); template <class T> void re(T &x) { cin >> x; } void re(double &x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2> &p) { re(p.f, p.s); } template <class T> void re(vector<T> &a) { F0R(i, sz(a)) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ> &a) { F0R(i, SZ) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2> &x); template <class T, size_t SZ> void pr(const array<T, SZ> &x); template <class T> void pr(const vector<T> &x); template <class T> void pr(const deque<T> &x); template <class T> void pr(const set<T> &x); template <class T1, class T2> void pr(const map<T1, T2> &x); template <class T> void pr(const T &x) { cout << x; } template <class Arg, class... Args> void pr(const Arg &first, const Args &...rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); } template <class T, bool pretty = true> void prContain(const T &x) { if (pretty) pr("{"); bool fst = 1; for (const auto &a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0; if (pretty) pr("}"); } template <class T> void pc(const T &x) { prContain<T, false>(x); pr("\n"); } template <class T, size_t SZ> void pr(const array<T, SZ> &x) { prContain(x); } template <class T> void pr(const vector<T> &x) { prContain(x); } template <class T> void pr(const deque<T> &x) { prContain(x); } template <class T> void pr(const set<T> &x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2> &x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg &first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg &first, const Args &...rest) { pr(first, " "); ps(rest...); } } // namespace __output using namespace __output; #define TRACE(x) x #define __pn(x) pr(#x, " = ") #define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush namespace __algorithm { template <typename T> void dedup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T> &v, const T &x) { auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T> &v, const T &x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C &v, T id, OP op) { vector<T> r(sz(v) + 1, id); F0R(i, sz(v)) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C &v, T id, OP op) { vector<T> r(sz(v) + 1, id); F0Rd(i, sz(v)) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" struct monostate { friend istream &operator>>(istream &is, const monostate &ms) { return is; } friend ostream &operator<<(ostream &os, const monostate &ms) { return os; } friend monostate operator+(const monostate &a, const monostate &b) { return a; } } ms; #pragma GCC diagnostic pop namespace __io { void setIn(string s) { freopen(s.c_str(), "r", stdin); } void setOut(string s) { freopen(s.c_str(), "w", stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); if (sz(s)) { setIn(s + ".in"), setOut(s + ".out"); } } } // namespace __io using namespace __io; // }}} // integer division in C++ rounds towards 0 ll floor(ll top, ll bot) { assert(bot > 0); if (top < 0) { return (top - bot + 1) / bot; } else { return top / bot; } } ll ceil(ll top, ll bot) { assert(bot > 0); if (top < 0) { return top / bot; } else { return (top + bot - 1) / bot; } } int main() { setIO(); int N; ll K; re(N, K); vi a(N); re(a); sort(all(a)); auto count_geq = [&](ll v) { ll ans = 0; trav(_fv, a) { ll fv = _fv; if (fv == 0) { if (v <= 0) ans += N - 1; continue; } if (fv < 0) { // < 0 ll minv = floor(-v, -fv); ans += ub(all(a), '0', [&](char _, int pos) { // return fv * pos < v; // want pos > v/fv return pos > minv; }) - a.begin(); } else { // >= 0 ll minv = ceil(v, fv); ans += a.end() - ub(all(a), '0', [&](char _, int pos) { // return fv * pos >= v; // want pos >= v / fv return pos >= minv; }); } if (fv * fv >= v) ans--; } return ans / 2; }; auto count_lt = [&](ll v) { return ll(N) * (N - 1) / 2 - count_geq(v); }; --K; ll lo = -1e18, hi = 1e18 + 1; while (hi - lo > 1) { ll mi = lo + (hi - lo) / 2; if (count_lt(mi) <= K) lo = mi; else hi = mi; } ps(lo); // did you check N=1? did you mix up N,M? // check your "infinity" and "null" values against the bounds return 0; }
replace
254
255
254
255
TLE
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define de(c) cout << #c << " = " << c << endl #define dd(c) cout << #c << " = " << c << " " typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; mt19937 mrand(random_device{}()); const ll mod = 1000000007; int rnd(int x) { return mrand() % x; } ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const int maxn = 1e5 + 100; ll n, m; ll k; ll a[maxn], b[maxn]; ll a2[maxn], b2[maxn]; ll tmp, tmp2, tmp3, tmp4; ll s[maxn]; bool check(ll m, ll x, ll y) { if (m * y <= x) { return true; } return false; } ll gao1(ll x, ll y) { ll l = 0, r = tmp3 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check(b[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check(b[l], x, y)) return l; return -1; } ll gao2(ll x, ll y) { ll l = 0, r = tmp4 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check(b2[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check(b2[l], x, y)) return l; return -1; } bool check2(ll m, ll x, ll y) { if (m * y < x) { return true; } return false; } ll gao3(ll x, ll y) { ll l = 0, r = tmp3 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check2(b[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check2(b[l], x, y)) return l; return -1; } ll gao4(ll x, ll y) { ll l = 0, r = tmp4 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check2(b2[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check2(b2[l], x, y)) return l; return -1; } bool gao(ll x) { ll cnt = 0; rep(i, 0, tmp) { if (tmp3) { // + * + if (x >= 0) { ll ps = gao1(x, a[i]) + 1; cnt += ps; // dd("1"),de(ps); } } if (tmp4) { if (x >= 0) cnt += tmp4; else { ll ps = gao4(-x, a[i]); ps = tmp4 - ps - 1; cnt += ps; } } } rep(j, 0, tmp2) { if (tmp3) { // - * + if (x >= 0) { cnt += tmp3; } else { ll ps = gao3(-x, -a2[j]); ps = tmp3 - ps - 1; cnt += ps; } } if (tmp4) { // - * - => + if (x >= 0) { ll ps = gao2(x, -a2[j]) + 1; cnt += ps; // dd("4"),de(ps); } } } // dd(x),de(cnt); cnt -= (upper_bound(s, s + n, x) - s); cnt /= 2; // dd(x),de(cnt); return cnt >= k; } int main(int argc, char const *argv[]) { #ifdef indiewar freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; ll x; tmp = tmp2 = 0; tmp3 = tmp4 = 0; m = n; rep(i, 0, n) { cin >> x; s[i] = x * x; if (x >= 0) { a[tmp++] = x; } else { a2[tmp2++] = x; } if (x >= 0) { b[tmp3++] = x; } else { b2[tmp4++] = -x; } } sort(s, s + n); sort(a, a + tmp); sort(a2, a2 + tmp2); sort(b, b + tmp3); sort(b2, b2 + tmp4); ll l = -1e18, r = 1e18; while (l < r) { ll mid = (l + r) >> 1; // dd(l),de(r); if (gao(mid)) { r = mid; } else { l = mid + 1; } } cout << l << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define de(c) cout << #c << " = " << c << endl #define dd(c) cout << #c << " = " << c << " " typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; mt19937 mrand(random_device{}()); const ll mod = 1000000007; int rnd(int x) { return mrand() % x; } ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } const int maxn = 2e5 + 100; ll n, m; ll k; ll a[maxn], b[maxn]; ll a2[maxn], b2[maxn]; ll tmp, tmp2, tmp3, tmp4; ll s[maxn]; bool check(ll m, ll x, ll y) { if (m * y <= x) { return true; } return false; } ll gao1(ll x, ll y) { ll l = 0, r = tmp3 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check(b[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check(b[l], x, y)) return l; return -1; } ll gao2(ll x, ll y) { ll l = 0, r = tmp4 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check(b2[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check(b2[l], x, y)) return l; return -1; } bool check2(ll m, ll x, ll y) { if (m * y < x) { return true; } return false; } ll gao3(ll x, ll y) { ll l = 0, r = tmp3 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check2(b[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check2(b[l], x, y)) return l; return -1; } ll gao4(ll x, ll y) { ll l = 0, r = tmp4 - 1; while (l < r) { ll mid = (l + r + 1) >> 1; if (check2(b2[mid], x, y)) { l = mid; } else { r = mid - 1; } } if (check2(b2[l], x, y)) return l; return -1; } bool gao(ll x) { ll cnt = 0; rep(i, 0, tmp) { if (tmp3) { // + * + if (x >= 0) { ll ps = gao1(x, a[i]) + 1; cnt += ps; // dd("1"),de(ps); } } if (tmp4) { if (x >= 0) cnt += tmp4; else { ll ps = gao4(-x, a[i]); ps = tmp4 - ps - 1; cnt += ps; } } } rep(j, 0, tmp2) { if (tmp3) { // - * + if (x >= 0) { cnt += tmp3; } else { ll ps = gao3(-x, -a2[j]); ps = tmp3 - ps - 1; cnt += ps; } } if (tmp4) { // - * - => + if (x >= 0) { ll ps = gao2(x, -a2[j]) + 1; cnt += ps; // dd("4"),de(ps); } } } // dd(x),de(cnt); cnt -= (upper_bound(s, s + n, x) - s); cnt /= 2; // dd(x),de(cnt); return cnt >= k; } int main(int argc, char const *argv[]) { #ifdef indiewar freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; ll x; tmp = tmp2 = 0; tmp3 = tmp4 = 0; m = n; rep(i, 0, n) { cin >> x; s[i] = x * x; if (x >= 0) { a[tmp++] = x; } else { a2[tmp2++] = x; } if (x >= 0) { b[tmp3++] = x; } else { b2[tmp4++] = -x; } } sort(s, s + n); sort(a, a + tmp); sort(a2, a2 + tmp2); sort(b, b + tmp3); sort(b2, b2 + tmp4); ll l = -1e18, r = 1e18; while (l < r) { ll mid = (l + r) >> 1; // dd(l),de(r); if (gao(mid)) { r = mid; } else { l = mid + 1; } } cout << l << endl; return 0; }
replace
31
32
31
32
0
p02774
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; lint K; cin >> K; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(A.begin(), A.end()); auto judge = [&](auto idx) { lint cnt = 0; if (idx >= 0) { for (int i = 0; i < N; i++) { if (A[i] == 0) cnt += N - i - 1; else if (A[i] < 0) cnt += A.end() - lower_bound(A.begin() + i + 1, A.end(), idx / A[i]); else cnt += upper_bound(A.begin() + i + 1, A.end(), idx / A[i]) - (A.begin() + i + 1); } } else { for (int i = 0; i < N; i++) { if (A[i] < 0) cnt += A.end() - lower_bound(A.begin() + i + 1, A.end(), (idx + 1) / A[i] + 1); else cnt += upper_bound(A.begin() + i + 1, A.end(), (idx + 1) / A[i] + 1) - (A.begin() + i + 1); } } return cnt >= K; }; auto binary_search = [&](auto f) { auto ng = -LINF; auto ok = LINF; while (abs(ok - ng) > 1) { auto mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; }; lint ans = binary_search(judge); cout << ans << "\n"; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; lint K; cin >> K; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(A.begin(), A.end()); auto judge = [&](auto idx) { lint cnt = 0; if (idx >= 0) { for (int i = 0; i < N; i++) { if (A[i] == 0) cnt += N - i - 1; else if (A[i] < 0) cnt += A.end() - lower_bound(A.begin() + i + 1, A.end(), idx / A[i]); else cnt += upper_bound(A.begin() + i + 1, A.end(), idx / A[i]) - (A.begin() + i + 1); } } else { for (int i = 0; i < N; i++) { if (A[i] < 0) cnt += A.end() - lower_bound(A.begin() + i + 1, A.end(), (idx + 1) / A[i] + 1); else if (A[i] != 0) cnt += upper_bound(A.begin() + i + 1, A.end(), (idx + 1) / A[i] + 1) - (A.begin() + i + 1); } } return cnt >= K; }; auto binary_search = [&](auto f) { auto ng = -LINF; auto ok = LINF; while (abs(ok - ng) > 1) { auto mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; }; lint ans = binary_search(judge); cout << ans << "\n"; return 0; }
replace
74
75
74
75
0
p02774
C++
Runtime Error
/** * author: qodjf * created: 04.21.2020 23:13:45 */ #include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) 42 #endif #define mst(x, a) memset(x, a, sizeof(x)) using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll MOD = 1e9 + 7; ll N, K; vector<ll> negative, positive; ll zeroCnt, negCnt, posCnt; ll cntOfProductLessThan(const vector<ll> &A, ll x) { ll cnt = 0; int rb = 0; for (int i = A.size() - 1; i >= 0; i--) { rb = min(rb, i); while (A[rb] * A[i] <= x && rb < i) rb++; cnt += rb; } return cnt; } ll test(ll x) { if (x == 0) { return zeroCnt * (posCnt + negCnt) + zeroCnt * (zeroCnt - 1) / 2 + posCnt * negCnt; } if (x > 0) { return cntOfProductLessThan(positive, x) + cntOfProductLessThan(negative, x) + test(0); } ll cnt = 0; int lb = negCnt - 1; for (int i = positive.size() - 1; i >= 0; i--) { while (positive[i] * negative[lb] <= x && lb >= 0) lb--; cnt += negCnt - 1 - lb; } return cnt; } void solve() { sort(positive.begin(), positive.end()); sort(negative.rbegin(), negative.rend()); ll lo = -1e18 - 10, hi = 1e18 + 10; while (lo + 1 < hi) { ll mi = lo + (hi - lo) / 2; // product <= x is >= K if (test(mi) >= K) { hi = mi; } else { lo = mi; } } cout << hi << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> K; for (int i = 0; i < N; i++) { int a; cin >> a; if (a > 0) { positive.push_back(a); } else if (a < 0) { negative.push_back(a); } } posCnt = positive.size(); negCnt = negative.size(); zeroCnt = N - posCnt - negCnt; solve(); }
/** * author: qodjf * created: 04.21.2020 23:13:45 */ #include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define dbg(...) 42 #endif #define mst(x, a) memset(x, a, sizeof(x)) using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const ll MOD = 1e9 + 7; ll N, K; vector<ll> negative, positive; ll zeroCnt, negCnt, posCnt; ll cntOfProductLessThan(const vector<ll> &A, ll x) { ll cnt = 0; int rb = 0; for (int i = A.size() - 1; i >= 0; i--) { rb = min(rb, i); while (A[rb] * A[i] <= x && rb < i) rb++; cnt += rb; } return cnt; } ll test(ll x) { if (x == 0) { return zeroCnt * (posCnt + negCnt) + zeroCnt * (zeroCnt - 1) / 2 + posCnt * negCnt; } if (x > 0) { return cntOfProductLessThan(positive, x) + cntOfProductLessThan(negative, x) + test(0); } ll cnt = 0; int lb = negCnt - 1; for (int i = 0; i < posCnt; i++) { while (lb >= 0 && positive[i] * negative[lb] <= x) lb--; cnt += negCnt - 1 - lb; } return cnt; } void solve() { sort(positive.begin(), positive.end()); sort(negative.rbegin(), negative.rend()); ll lo = -1e18 - 10, hi = 1e18 + 10; while (lo + 1 < hi) { ll mi = lo + (hi - lo) / 2; // product <= x is >= K if (test(mi) >= K) { hi = mi; } else { lo = mi; } } cout << hi << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> K; for (int i = 0; i < N; i++) { int a; cin >> a; if (a > 0) { positive.push_back(a); } else if (a < 0) { negative.push_back(a); } } posCnt = positive.size(); negCnt = negative.size(); zeroCnt = N - posCnt - negCnt; solve(); }
replace
81
83
81
83
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) // // Notes // // When specified less than N, try full scan. // RE: empty vector // Check Qs // long/int // xor -> count each bit // Check values // 1,000,000 -> Loop // 100,000 -> vector(int:400KB, long:800KB) #define INF (1 << 30) // 1,073,741,824 //= 536,870,912 *2 #define MOD 1000000007 #define Rep0(i, n) for (ll i = 0; i < n; i++) #define Rep1(i, n) for (ll i = 1; i <= n; i++) #define Sort(P) sort(P.begin(), P.end()) #define Rev(P) reverse(P.begin(), P.end()) ll FP(const vector<ll> &P, const vector<ll> &M, const ll K) { ll r = 0; if (0 != M.size()) // No positive number r = max(r, M.back() * M.back() + 1); if (0 != P.size()) // No negative number r = max(r, P.back() * P.back() + 1); ll l = 0; // Less than minimum value while (r - l > 1) { // Loop ll m = (l + r) / 2; ll s = 0; // Count pairs equal or more then m Rep0(i, P.size() - 1) { ll d = (m + P.at(i) - 1) / P.at(i); // minimum value for the other for m ll c = lower_bound(P.begin(), P.end(), d) - P.begin(); // index for the minimum P c = max(c, i + 1); // count only the ones more than P[i] s += P.size() - c; } Rep0(i, M.size() - 1) { ll d = (m + M.at(i) - 1) / M.at(i); // minimum value for the other for m ll c = lower_bound(M.begin(), M.end(), d) - M.begin(); // index for the minimum M c = max(c, i + 1); // count only the ones more than M[i] s += M.size() - c; } if (s >= K) l = m; else r = m; } return l; } ll FM(const vector<ll> &P, const vector<ll> &M, const ll K) { ll r = P.back() * M.back() + 1; // More then maximum value ll l = 0; // Less than minimum value while (r - l > 1) { // Loop ll m = (l + r) / 2; ll s = 0; // Count pairs equal or more then m Rep0(i, P.size()) { ll d = (m + P.at(i) - 1) / P.at(i); // minimum value for the other for m ll c = lower_bound(M.begin(), M.end(), d) - M.begin(); // index for the minimum M s += M.size() - c; } if (s >= K) l = m; else r = m; } return l; } int main() { ll N, K; cin >> N >> K; vector<ll> P, M, Z; Rep0(i, N) { ll A; cin >> A; if (A < 0) M.push_back(-A); else if (A == 0) Z.push_back(A); else P.push_back(A); } Sort(P); Sort(M); ll NM = P.size() * M.size(); ll NZ = Z.size() * (P.size() + M.size()) + Z.size() * (Z.size() - 1) / 2; if (K <= NM) { // Minus cout << -FM(P, M, K) << endl; } else if (K <= NM + NZ) { // Zero cout << 0 << endl; } else { // Plus cout << FP(P, M, N * (N - 1) / 2 - K + 1) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 // (-2^63 <= long < 2^63) // lower_bound(A.begin(), A.end(), N) // upper_bound(... // A.erase(unique(A.begin(), A.end()), A.end()) // bit: &/and, |/or, ^/xor, ~/not // getline(cin, String) // while (getline(cin, S)) {} // cout <<fixed <<setprecision(10) // priority_queue<int> q; // priority_queue<int, vector<int>, less<int>> q; // Default: vector, less // priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q; // Graph // Warshall-Floyd: Distance between each node, N^3 // Dijkstra: Distance from the start node, N^2 // DFS(Depth-First Search) // // Notes // // When specified less than N, try full scan. // RE: empty vector // Check Qs // long/int // xor -> count each bit // Check values // 1,000,000 -> Loop // 100,000 -> vector(int:400KB, long:800KB) #define INF (1 << 30) // 1,073,741,824 //= 536,870,912 *2 #define MOD 1000000007 #define Rep0(i, n) for (ll i = 0; i < n; i++) #define Rep1(i, n) for (ll i = 1; i <= n; i++) #define Sort(P) sort(P.begin(), P.end()) #define Rev(P) reverse(P.begin(), P.end()) ll FP(const vector<ll> &P, const vector<ll> &M, const ll K) { ll r = 0; if (0 != M.size()) // No positive number r = max(r, M.back() * M.back() + 1); if (0 != P.size()) // No negative number r = max(r, P.back() * P.back() + 1); ll l = 0; // Less than minimum value while (r - l > 1) { // Loop ll m = (l + r) / 2; ll s = 0; // Count pairs equal or more then m if (0 < P.size()) Rep0(i, P.size() - 1) { ll d = (m + P.at(i) - 1) / P.at(i); // minimum value for the other for m ll c = lower_bound(P.begin(), P.end(), d) - P.begin(); // index for the minimum P c = max(c, i + 1); // count only the ones more than P[i] s += P.size() - c; } if (0 < M.size()) Rep0(i, M.size() - 1) { ll d = (m + M.at(i) - 1) / M.at(i); // minimum value for the other for m ll c = lower_bound(M.begin(), M.end(), d) - M.begin(); // index for the minimum M c = max(c, i + 1); // count only the ones more than M[i] s += M.size() - c; } if (s >= K) l = m; else r = m; } return l; } ll FM(const vector<ll> &P, const vector<ll> &M, const ll K) { ll r = P.back() * M.back() + 1; // More then maximum value ll l = 0; // Less than minimum value while (r - l > 1) { // Loop ll m = (l + r) / 2; ll s = 0; // Count pairs equal or more then m Rep0(i, P.size()) { ll d = (m + P.at(i) - 1) / P.at(i); // minimum value for the other for m ll c = lower_bound(M.begin(), M.end(), d) - M.begin(); // index for the minimum M s += M.size() - c; } if (s >= K) l = m; else r = m; } return l; } int main() { ll N, K; cin >> N >> K; vector<ll> P, M, Z; Rep0(i, N) { ll A; cin >> A; if (A < 0) M.push_back(-A); else if (A == 0) Z.push_back(A); else P.push_back(A); } Sort(P); Sort(M); ll NM = P.size() * M.size(); ll NZ = Z.size() * (P.size() + M.size()) + Z.size() * (Z.size() - 1) / 2; if (K <= NM) { // Minus cout << -FM(P, M, K) << endl; } else if (K <= NM + NZ) { // Zero cout << 0 << endl; } else { // Plus cout << FP(P, M, N * (N - 1) / 2 - K + 1) << endl; } return 0; }
replace
62
78
62
80
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, o, n) for (long long i = o; i < n; i++) #define oneforall \ ios::sync_with_stdio(false); \ cin.tie(0); #define all(v) (v).begin(), (v).end() #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) const long long INF = 1e18; void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } typedef vector<int> vi; typedef vector<long long> vl; typedef long long ll; typedef vector<pair<long, long>> vpll; typedef vector<pair<int, int>> vpii; int main() { oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall inl( n); inl(k); ll a[10000]; FOR(i, 0, n) { in(a[i]); } sort(a, a + n); ll l = -INF; ll r = INF; while (r - l > 1) { ll mid = (l + r) >> 1; ll tmp = 0; FOR(i, 0, n) { if (a[i] >= 0) { if (a[i] * a[0] > mid) continue; ll l1 = i; ll r1 = n; while (r1 - l1 > 1) { ll mid1 = (r1 + l1) >> 1; if (a[i] * a[mid1] > mid) r1 = mid1; else l1 = mid1; } tmp += l1 - i; } if (a[i] < 0) { if (a[i] * a[n - 1] > mid) continue; ll l1 = i; ll r1 = n; while (r1 - l1 > 1) { ll mid1 = (r1 + l1) >> 1; if (a[i] * a[mid1] > mid) l1 = mid1; else r1 = mid1; } tmp += n - r1; } } if (tmp >= k) r = mid; else l = mid; } out(r); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, o, n) for (long long i = o; i < n; i++) #define oneforall \ ios::sync_with_stdio(false); \ cin.tie(0); #define all(v) (v).begin(), (v).end() #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) const long long INF = 1e18; void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } typedef vector<int> vi; typedef vector<long long> vl; typedef long long ll; typedef vector<pair<long, long>> vpll; typedef vector<pair<int, int>> vpii; int main() { oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall inl( n); inl(k); ll a[10000010]; FOR(i, 0, n) { in(a[i]); } sort(a, a + n); ll l = -INF; ll r = INF; while (r - l > 1) { ll mid = (l + r) >> 1; ll tmp = 0; FOR(i, 0, n) { if (a[i] >= 0) { if (a[i] * a[0] > mid) continue; ll l1 = i; ll r1 = n; while (r1 - l1 > 1) { ll mid1 = (r1 + l1) >> 1; if (a[i] * a[mid1] > mid) r1 = mid1; else l1 = mid1; } tmp += l1 - i; } if (a[i] < 0) { if (a[i] * a[n - 1] > mid) continue; ll l1 = i; ll r1 = n; while (r1 - l1 > 1) { ll mid1 = (r1 + l1) >> 1; if (a[i] * a[mid1] > mid) l1 = mid1; else r1 = mid1; } tmp += n - r1; } } if (tmp >= k) r = mid; else l = mid; } out(r); return 0; }
replace
39
40
39
40
0
p02774
C++
Runtime Error
#include <algorithm> #include <bitset> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, i, j, k, x, a, b, c, ans, m, ct, aa, bb, cc; cin >> n >> k; vector<vector<long long>> v(3); for (i = 0; i < n; i++) { cin >> x; if (x >= 0) v[0].push_back(x); else v[1].push_back(x); } if (!v[0].empty()) sort(v[0].begin(), v[0].end()); if (!v[1].empty()) sort(v[1].rbegin(), v[1].rend()); v[2] = v[1]; for (i = 0; i < v[1].size(); i++) v[1][i] = -v[1][i]; sort(v[1].begin(), v[1].end()); a = -(1LL << 60); b = 1LL << 60; while (a < b) { if (b - a == 1) c = a; else c = (a + b) / 2; ct = 0; for (i = 0; i < 2; i++) { // same sign for (j = 0; j < v[i].size() - 1; j++) { if (v[i][j] * v[i][j + 1] > c) continue; aa = j + 1; bb = v[i].size() - 1; while (aa < bb) { cc = (aa + bb + 1) / 2; if (v[i][j] * v[i][cc] > c) bb = cc - 1; else aa = cc; } ct += aa - j; } // different sign if (i == 1) continue; // avoid duplication m = v[2].size(); if (m == 0) continue; for (j = 0; j < v[i].size(); j++) { if (v[i][j] * v[2][m - 1] > c) continue; aa = 0; bb = m - 1; while (aa < bb) { cc = (aa + bb) / 2; if (v[i][j] * v[2][cc] > c) aa = cc + 1; else bb = cc; } ct += m - aa; } } if (ct < k) a = c + 1; else b = c; } cout << a << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, i, j, k, x, a, b, c, ans, m, ct, aa, bb, cc; cin >> n >> k; vector<vector<long long>> v(3); for (i = 0; i < n; i++) { cin >> x; if (x >= 0) v[0].push_back(x); else v[1].push_back(x); } if (!v[0].empty()) sort(v[0].begin(), v[0].end()); if (!v[1].empty()) sort(v[1].rbegin(), v[1].rend()); v[2] = v[1]; for (i = 0; i < v[1].size(); i++) v[1][i] = -v[1][i]; sort(v[1].begin(), v[1].end()); a = -(1LL << 60); b = 1LL << 60; while (a < b) { if (b - a == 1) c = a; else c = (a + b) / 2; ct = 0; for (i = 0; i < 2; i++) { if (v[i].empty()) continue; // same sign for (j = 0; j < v[i].size() - 1; j++) { if (v[i][j] * v[i][j + 1] > c) continue; aa = j + 1; bb = v[i].size() - 1; while (aa < bb) { cc = (aa + bb + 1) / 2; if (v[i][j] * v[i][cc] > c) bb = cc - 1; else aa = cc; } ct += aa - j; } // different sign if (i == 1) continue; // avoid duplication m = v[2].size(); if (m == 0) continue; for (j = 0; j < v[i].size(); j++) { if (v[i][j] * v[2][m - 1] > c) continue; aa = 0; bb = m - 1; while (aa < bb) { cc = (aa + bb) / 2; if (v[i][j] * v[2][cc] > c) aa = cc + 1; else bb = cc; } ct += m - aa; } } if (ct < k) a = c + 1; else b = c; } cout << a << "\n"; return 0; }
insert
54
54
54
56
0
p02774
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define for0(i, N) for (int i = 0; i < (N); ++i) #define for1n(i, N) for (int i = 1; i <= (N); ++i) #define for1(i, N) for (int i = 1; i < (N); ++i) #define forn(i, N) for (int i = 0; i <= (N); ++i) #define forx(i, N, x) for (int i = (x); i < (N); ++i) #define forl(i, N, x, a) \ for (int i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a) template <size_t i> std::bitset<i> operator++(std::bitset<i> b, int) { static_assert(i <= 64, "Cannot cast 64bit int."); return b = (unsigned long long)b + 1; } template <size_t i> std::bitset<i> operator++(std::bitset<i> b) { static_assert(i <= 64, "Cannot cast 64bit int."); auto temp = b; b++; return temp; } template <size_t i> std::bitset<i> operator--(std::bitset<i> b, int) { static_assert(i <= 64, "Cannot cast 64bit int."); return b = (unsigned long long)b - 1; } template <size_t i> std::bitset<i> operator--(std::bitset<i> b) { static_assert(i <= 64, "Cannot cast 64bit int."); auto temp = b; b--; return temp; } using namespace std; typedef long long ll; typedef long double ld; typedef priority_queue<ll> pq; typedef priority_queue<ll, vector<ll>, greater<ll>> pql; typedef stack<ll> stk; typedef queue<ll> qu; typedef pair<ll, ll> pll; typedef map<ll, ll> mll; typedef unordered_map<ll, ll> umll; typedef set<ll> sll; typedef unordered_set<ll> usll; typedef vector<ll> vll; typedef vector<ld> vld; // right is true, left is false. template <class T, class F> T bSearch(T l, T r, F func, T diff = 1) { while (std::abs(r - l) > diff) { T m = (l + r) / 2; (func(m) ? r : l) = m; } return r; } template <class Iterator1, class IteratorDynamic, class Condition, class Func2> void shakutori(Iterator1 bg1, Iterator1 ed1, IteratorDynamic bg2, IteratorDynamic ed2, Condition cond, Func2 func2) { auto it2 = bg2; for (auto it1 = bg1; it1 != ed1; ++it1) { while (it2 != ed2 && cond(it1, it2)) ++it2; func2(it1, it2); } } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); ll n, k; cin >> n >> k; vll minus, plus; ll zero = 0; for0(i, n) { ll a; cin >> a; if (a > 0) { plus.push_back(a); } else if (a < 0) { minus.push_back(-a); } else { ++zero; assert(a == 0); } } assert(plus.size() + minus.size() + zero == n); ll ms = minus.size(); ll ps = plus.size(); assert(n * (n - 1) / 2 == ms * ps + ps * zero + zero * ms + zero * (zero - 1) / 2 + ps * (ps - 1) / 2 + ms * (ms - 1) / 2); // cerr << "all: " // << ms * ps + ps * zero + zero * ms + // zero * (zero - 1) / 2 + ps *(ps - 1) / 2 + ms *(ms - 1) / 2 << "\n"; ll m = ms * ps; ll z = zero * ps + zero * ms + zero * (zero - 1) / 2; ll p = ms * (ms - 1) / 2 + ps * (ps - 1) / 2; assert(m + z + p == n * (n - 1) / 2); // cerr << "p: " << p << "\n"; // cerr << "m: " << m << "\n"; // cerr << "z: " << z << "\n"; // cerr << "p + m + z: " << p + m + z << "\n"; for (auto &&x : {&minus, &plus}) sort(x->begin(), x->end()); if (m < k && k <= m + z) { cout << 0; } else if (k <= m) { reverse(plus.begin(), plus.end()); auto r = bSearch(ll(1e18 + 1), ll(0), [&](ll x) { assert(!plus.empty()); ll result = 0; shakutori( plus.begin(), plus.end(), minus.begin(), minus.end(), [&](auto i, auto j) { return *i * *j < x; }, [&](auto, auto j) { result += distance(minus.begin(), j); }); return m - result >= k; }); cout << -r; } else { auto r = bSearch(ll(0), ll(1e18 + 1), [&](ll x) { ll result = 0; for (auto con : {&plus, &minus}) { shakutori( con->rbegin(), con->rend(), con->begin(), con->end(), [&](auto i, auto j) { return *i * *j <= x; }, [&](auto i, auto j) { result += distance(con->begin(), j); if (*i * *i < x) --result; }); } assert(result % 2 == 0); return result / 2 >= k - z - m; }); cout << r; } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define for0(i, N) for (int i = 0; i < (N); ++i) #define for1n(i, N) for (int i = 1; i <= (N); ++i) #define for1(i, N) for (int i = 1; i < (N); ++i) #define forn(i, N) for (int i = 0; i <= (N); ++i) #define forx(i, N, x) for (int i = (x); i < (N); ++i) #define forl(i, N, x, a) \ for (int i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a) template <size_t i> std::bitset<i> operator++(std::bitset<i> b, int) { static_assert(i <= 64, "Cannot cast 64bit int."); return b = (unsigned long long)b + 1; } template <size_t i> std::bitset<i> operator++(std::bitset<i> b) { static_assert(i <= 64, "Cannot cast 64bit int."); auto temp = b; b++; return temp; } template <size_t i> std::bitset<i> operator--(std::bitset<i> b, int) { static_assert(i <= 64, "Cannot cast 64bit int."); return b = (unsigned long long)b - 1; } template <size_t i> std::bitset<i> operator--(std::bitset<i> b) { static_assert(i <= 64, "Cannot cast 64bit int."); auto temp = b; b--; return temp; } using namespace std; typedef long long ll; typedef long double ld; typedef priority_queue<ll> pq; typedef priority_queue<ll, vector<ll>, greater<ll>> pql; typedef stack<ll> stk; typedef queue<ll> qu; typedef pair<ll, ll> pll; typedef map<ll, ll> mll; typedef unordered_map<ll, ll> umll; typedef set<ll> sll; typedef unordered_set<ll> usll; typedef vector<ll> vll; typedef vector<ld> vld; // right is true, left is false. template <class T, class F> T bSearch(T l, T r, F func, T diff = 1) { while (std::abs(r - l) > diff) { T m = (l + r) / 2; (func(m) ? r : l) = m; } return r; } template <class Iterator1, class IteratorDynamic, class Condition, class Func2> void shakutori(Iterator1 bg1, Iterator1 ed1, IteratorDynamic bg2, IteratorDynamic ed2, Condition cond, Func2 func2) { auto it2 = bg2; for (auto it1 = bg1; it1 != ed1; ++it1) { while (it2 != ed2 && cond(it1, it2)) ++it2; func2(it1, it2); } } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); ll n, k; cin >> n >> k; vll minus, plus; ll zero = 0; for0(i, n) { ll a; cin >> a; if (a > 0) { plus.push_back(a); } else if (a < 0) { minus.push_back(-a); } else { ++zero; assert(a == 0); } } assert(plus.size() + minus.size() + zero == n); ll ms = minus.size(); ll ps = plus.size(); assert(n * (n - 1) / 2 == ms * ps + ps * zero + zero * ms + zero * (zero - 1) / 2 + ps * (ps - 1) / 2 + ms * (ms - 1) / 2); // cerr << "all: " // << ms * ps + ps * zero + zero * ms + // zero * (zero - 1) / 2 + ps *(ps - 1) / 2 + ms *(ms - 1) / 2 << "\n"; ll m = ms * ps; ll z = zero * ps + zero * ms + zero * (zero - 1) / 2; ll p = ms * (ms - 1) / 2 + ps * (ps - 1) / 2; assert(m + z + p == n * (n - 1) / 2); // cerr << "p: " << p << "\n"; // cerr << "m: " << m << "\n"; // cerr << "z: " << z << "\n"; // cerr << "p + m + z: " << p + m + z << "\n"; for (auto &&x : {&minus, &plus}) sort(x->begin(), x->end()); if (m < k && k <= m + z) { cout << 0; } else if (k <= m) { reverse(plus.begin(), plus.end()); auto r = bSearch(ll(1e18 + 1), ll(0), [&](ll x) { assert(!plus.empty()); ll result = 0; shakutori( plus.begin(), plus.end(), minus.begin(), minus.end(), [&](auto i, auto j) { return *i * *j < x; }, [&](auto, auto j) { result += distance(minus.begin(), j); }); return m - result >= k; }); cout << -r; } else { auto r = bSearch(ll(0), ll(1e18 + 1), [&](ll x) { ll result = 0; for (auto con : {&plus, &minus}) { shakutori( con->rbegin(), con->rend(), con->begin(), con->end(), [&](auto i, auto j) { return *i * *j <= x; }, [&](auto i, auto j) { result += distance(con->begin(), j); if (*i * *i <= x) --result; }); } assert(result % 2 == 0); return result / 2 >= k - z - m; }); cout << r; } return 0; }
replace
162
163
162
163
0
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (ll i = x; i <= n; i++) #define rep3(i, x, n) for (ll i = x; i >= n; i--) #define elif else if #define sp setprecision #define pb(x) push_back(x) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ld, ld> pdd; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const int inf = 1e8; const ll INF = 1e16; const string alpha = "abcdefghijklmnopqrstuvwxyz"; int main() { ll N, K; cin >> N >> K; ll A[N]; rep(i, N) cin >> A[i]; sort(A, A + N); // 解は[l,r]に存在 ll l = -1e18, r = 1e18; while (l < r) { ll n = (l + r) / 2; // n以下になるものが何個あるか判定 ll sum = 0; rep(i, N) { if (A[i] < 0) { ll x = n / A[i]; rep2(j, x - 1, x + 1) { if (A[i] * j <= n) { sum += A + N - lower_bound(A, A + N, j); break; } } } elif (A[i] == 0) { if (n >= 0) sum += N; } else { ll x = n / A[i]; rep3(j, x + 1, x - 1) { if (A[i] * j <= n) { sum += N - (A + N - lower_bound(A, A + N, j + 1)); break; } } } if (A[i] * A[i] <= n) sum--; } sum /= 2; if (sum < K) l = n + 1; else r = n; } cout << l << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (ll i = x; i <= n; i++) #define rep3(i, x, n) for (ll i = x; i >= n; i--) #define elif else if #define sp setprecision #define pb(x) push_back(x) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ld, ld> pdd; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const int inf = 1e8; const ll INF = 1e16; const string alpha = "abcdefghijklmnopqrstuvwxyz"; int main() { ll N, K; cin >> N >> K; ll A[N]; rep(i, N) cin >> A[i]; sort(A, A + N); // 解は[l,r]に存在 ll l = -1e18, r = 1e18; while (l < r) { ll n = (l + r) / 2; if (l + r < 0 && 2 * n != l + r) n--; // n以下になるものが何個あるか判定 ll sum = 0; rep(i, N) { if (A[i] < 0) { ll x = n / A[i]; rep2(j, x - 1, x + 1) { if (A[i] * j <= n) { sum += A + N - lower_bound(A, A + N, j); break; } } } elif (A[i] == 0) { if (n >= 0) sum += N; } else { ll x = n / A[i]; rep3(j, x + 1, x - 1) { if (A[i] * j <= n) { sum += N - (A + N - lower_bound(A, A + N, j + 1)); break; } } } if (A[i] * A[i] <= n) sum--; } sum /= 2; if (sum < K) l = n + 1; else r = n; } cout << l << endl; }
insert
31
31
31
33
TLE
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define pb push_back #define pf push_front #define mp make_pair #define in insert #define ld long double #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) 1 #endif template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; const ll mod = 1e9 + 7; const ll MAX_NODES = 200001; const ll MAX_LOG = 50; ll power(ll x, ll n) { ll res = 1; for (; n > 0; n >>= 1) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; } return res; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif IOS ll n; ll k; cin >> n >> k; ll a[n]; vl pos, neg; ll z = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 0) pos.pb(a[i]); else if (a[i] == 0) z++; else { neg.pb(-a[i]); } } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); if (k <= (pos.size() * neg.size())) { ll l = -1e18, r = -1; ll fin = 0; auto check = [&](ll temp) { ll ans = 0; for (ll i = 0; i < neg.size(); i++) { ll pp = ((temp % neg[i]) ? upper_bound(pos.begin(), pos.end(), temp / neg[i]) : lower_bound(pos.begin(), pos.end(), temp / neg[i])) - pos.begin(); ans += (pos.size() - pp); } return ans; }; while (r - l > 1) { ll mid = (r + l) / 2; ll temp = -mid; ll ans = 0; ans = check(temp); if (ans >= k) { r = mid; } else { l = mid; } } if (check(-l) == k) cout << l << endl; else { cout << r << endl; } return 0; } else { k -= pos.size() * neg.size(); } if (k <= (z * (n - z) + (z * (z - 1)) / 2)) { cout << 0 << endl; return 0; } else { k -= (z * (n - z) + (z * (z - 1)) / 2); ll l = 1, r = 1e18; // trace(k); auto check = [&](ll temp) { ll ans = 0; for (ll i = 0; i < neg.size(); i++) { ll pp = upper_bound(neg.begin(), neg.end(), temp / neg[i]) - neg.begin(); if (pp > i) ans += (pp - i - 1); } for (ll i = 0; i < pos.size(); i++) { ll pp = upper_bound(pos.begin(), pos.end(), temp / pos[i]) - pos.begin(); if (pp > i) ans += (pp - i - 1); } return ans; }; while (r - l > 1) { ll mid = (r + l) / 2; ll temp = mid; ll ans = check(temp); if (ans >= k) { r = mid; } else { l = mid; } } if (check(l) == k) cout << l << endl; else { cout << r << endl; } return 0; } }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define pb push_back #define pf push_front #define mp make_pair #define in insert #define ld long double #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) 1 #endif template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; const ll mod = 1e9 + 7; const ll MAX_NODES = 200001; const ll MAX_LOG = 50; ll power(ll x, ll n) { ll res = 1; for (; n > 0; n >>= 1) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; } return res; } int main() { IOS ll n; ll k; cin >> n >> k; ll a[n]; vl pos, neg; ll z = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 0) pos.pb(a[i]); else if (a[i] == 0) z++; else { neg.pb(-a[i]); } } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); if (k <= (pos.size() * neg.size())) { ll l = -1e18, r = -1; ll fin = 0; auto check = [&](ll temp) { ll ans = 0; for (ll i = 0; i < neg.size(); i++) { ll pp = ((temp % neg[i]) ? upper_bound(pos.begin(), pos.end(), temp / neg[i]) : lower_bound(pos.begin(), pos.end(), temp / neg[i])) - pos.begin(); ans += (pos.size() - pp); } return ans; }; while (r - l > 1) { ll mid = (r + l) / 2; ll temp = -mid; ll ans = 0; ans = check(temp); if (ans >= k) { r = mid; } else { l = mid; } } if (check(-l) == k) cout << l << endl; else { cout << r << endl; } return 0; } else { k -= pos.size() * neg.size(); } if (k <= (z * (n - z) + (z * (z - 1)) / 2)) { cout << 0 << endl; return 0; } else { k -= (z * (n - z) + (z * (z - 1)) / 2); ll l = 1, r = 1e18; // trace(k); auto check = [&](ll temp) { ll ans = 0; for (ll i = 0; i < neg.size(); i++) { ll pp = upper_bound(neg.begin(), neg.end(), temp / neg[i]) - neg.begin(); if (pp > i) ans += (pp - i - 1); } for (ll i = 0; i < pos.size(); i++) { ll pp = upper_bound(pos.begin(), pos.end(), temp / pos[i]) - pos.begin(); if (pp > i) ans += (pp - i - 1); } return ans; }; while (r - l > 1) { ll mid = (r + l) / 2; ll temp = mid; ll ans = check(temp); if (ans >= k) { r = mid; } else { l = mid; } } if (check(l) == k) cout << l << endl; else { cout << r << endl; } return 0; } }
replace
64
68
64
65
0
p02774
C++
Runtime Error
#define ENABLE_DEBUG 1 // Kana's kitchen {{{ #include <bits/stdc++.h> #define ALL(v) std::begin(v), std::end(v) #define LOOP(k) \ for (i64 ngtkana_is_a_genius = 0; ngtkana_is_a_genius < (i64)k; \ ngtkana_is_a_genius++) using i32 = std::int_least32_t; using i64 = std::int_least64_t; using u32 = std::uint_least32_t; using u64 = std::uint_least64_t; using usize = std::size_t; template <class T, class U> using pair = std::pair<U, T>; template <class T> using diag_pair = std::pair<T, T>; template <class... Args> using tuple = std::tuple<Args...>; template <class T> using vec = std::vector<T>; template <class T> using numr = std::numeric_limits<T>; #ifdef NGTKANA #include <debug.hpp> #else #define DEBUG(...) (void)0 #endif /*}}}*/ auto cmx = [](auto &x, auto y) { if (x < y) { x = y; return true; } return false; }; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::setprecision(15) << std::fixed; usize n, k; std::cin >> n >> k; k--; vec<i64> pos, neg; usize zero = 0; for (usize i = 0; i < n; i++) { i64 x; std::cin >> x; if (0 < x) pos.push_back(x); if (x < 0) neg.push_back(-x); if (x == 0) zero++; } std::sort(ALL(pos)); std::sort(ALL(neg)); auto ans = [&]() -> i64 { usize base0 = pos.size() * neg.size(); if (k < base0) { auto count_ge = [&](i64 X) { i64 ans = 0; for (i64 x : pos) { // ceil(X/x) <= y ans += neg.end() - std::lower_bound(ALL(neg), (X + x - 1) / x); } return ans; }; i64 l = 0, r = pos.back() * neg.back() + 1; while (1 < r - l) { i64 c = (l + r) / 2; (k + 1 <= count_ge(c) ? l : r) = c; } return -l; } k -= base0; usize base1 = (zero * (zero - 1) / 2 + zero * (pos.size() + neg.size())); if (k < base1) return 0; k -= base1; auto count_le = [&](i64 X) { i64 ans = 0; for (auto &&v : {pos, neg}) { for (i64 x : v) { // y <= floor(X/x); ans += std::upper_bound(ALL(v), X / x) - v.begin(); } for (i64 x : v) { ans -= x * x <= X; } } assert(ans % 2 == 0); ans /= 2; return ans; }; i64 l = 0, r = std::max(pos.back() * pos.back(), neg.back() * neg.back()) + 1; while (1 < r - l) { i64 c = (l + r) / 2; (k + 1 <= count_le(c) ? r : l) = c; } return r; }(); std::cout << ans << '\n'; }
#define ENABLE_DEBUG 1 // Kana's kitchen {{{ #include <bits/stdc++.h> #define ALL(v) std::begin(v), std::end(v) #define LOOP(k) \ for (i64 ngtkana_is_a_genius = 0; ngtkana_is_a_genius < (i64)k; \ ngtkana_is_a_genius++) using i32 = std::int_least32_t; using i64 = std::int_least64_t; using u32 = std::uint_least32_t; using u64 = std::uint_least64_t; using usize = std::size_t; template <class T, class U> using pair = std::pair<U, T>; template <class T> using diag_pair = std::pair<T, T>; template <class... Args> using tuple = std::tuple<Args...>; template <class T> using vec = std::vector<T>; template <class T> using numr = std::numeric_limits<T>; #ifdef NGTKANA #include <debug.hpp> #else #define DEBUG(...) (void)0 #endif /*}}}*/ auto cmx = [](auto &x, auto y) { if (x < y) { x = y; return true; } return false; }; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::setprecision(15) << std::fixed; usize n, k; std::cin >> n >> k; k--; vec<i64> pos, neg; usize zero = 0; for (usize i = 0; i < n; i++) { i64 x; std::cin >> x; if (0 < x) pos.push_back(x); if (x < 0) neg.push_back(-x); if (x == 0) zero++; } std::sort(ALL(pos)); std::sort(ALL(neg)); auto ans = [&]() -> i64 { usize base0 = pos.size() * neg.size(); if (k < base0) { auto count_ge = [&](i64 X) { i64 ans = 0; for (i64 x : pos) { // ceil(X/x) <= y ans += neg.end() - std::lower_bound(ALL(neg), (X + x - 1) / x); } return ans; }; i64 l = 0, r = pos.back() * neg.back() + 1; while (1 < r - l) { i64 c = (l + r) / 2; (k + 1 <= count_ge(c) ? l : r) = c; } return -l; } k -= base0; usize base1 = (zero * (zero - 1) / 2 + zero * (pos.size() + neg.size())); if (k < base1) return 0; k -= base1; auto count_le = [&](i64 X) { i64 ans = 0; for (auto &&v : {pos, neg}) { for (i64 x : v) { // y <= floor(X/x); ans += std::upper_bound(ALL(v), X / x) - v.begin(); } for (i64 x : v) { ans -= x * x <= X; } } assert(ans % 2 == 0); ans /= 2; return ans; }; i64 l = 0, r = std::max(pos.empty() ? 0ll : pos.back() * pos.back(), neg.empty() ? 0ll : neg.back() * neg.back()) + 1; while (1 < r - l) { i64 c = (l + r) / 2; (k + 1 <= count_le(c) ? r : l) = c; } return r; }(); std::cout << ans << '\n'; }
replace
96
98
96
99
0
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define LOCAL using namespace std; template <typename A, typename B> ostream &operator<<(ostream &out, const pair<A, B> &a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream &operator<<(ostream &out, const array<T, N> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream &operator<<(ostream &out, const set<T, Cmp> &a) { out << "{"; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) { out << "{"; bool first = true; for (auto &p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0; } out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; #define int long long using P = pair<int, int>; // ######################################### const int INF = (int)(1e18) + 3ll; signed main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); vector<int> b = a; reverse(b.begin(), b.end()); auto cnt = [&](int x, int y, vector<int> v) { // x*v[i] < yとなるiの個数 int bot = -1, top = n; while (top - bot > 1) { int mid = (top + bot) / 2; if (v[mid] * x < y) bot = mid; else top = mid; } return top; }; auto ok = [&](int x) { // x未満がkこ未満か int ret = 0; rep(i, n) { if (a[i] >= 0) ret += cnt(a[i], x, a); else ret += cnt(a[i], x, b); if (a[i] * a[i] < x) ret--; } ret /= 2; return ret < k; }; int top = INF; int bot = -INF; while (top - bot > 1) { int mid = (top + bot) / 2; if (ok(mid)) bot = mid; else top = mid; } cout << bot << endl; return 0; }
#include <bits/stdc++.h> #define LOCAL using namespace std; template <typename A, typename B> ostream &operator<<(ostream &out, const pair<A, B> &a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream &operator<<(ostream &out, const array<T, N> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream &operator<<(ostream &out, const set<T, Cmp> &a) { out << "{"; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) { out << "{"; bool first = true; for (auto &p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0; } out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; #define int long long using P = pair<int, int>; // ######################################### const int INF = (int)(1e18) + 3ll; signed main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; sort(a.begin(), a.end()); vector<int> b = a; reverse(b.begin(), b.end()); auto cnt = [&](int x, int y, vector<int> &v) { // x*v[i] < yとなるiの個数 int bot = -1, top = n; while (top - bot > 1) { int mid = (top + bot) / 2; if (v[mid] * x < y) bot = mid; else top = mid; } return top; }; auto ok = [&](int x) { // x未満がkこ未満か int ret = 0; rep(i, n) { if (a[i] >= 0) ret += cnt(a[i], x, a); else ret += cnt(a[i], x, b); if (a[i] * a[i] < x) ret--; } ret /= 2; return ret < k; }; int top = INF; int bot = -INF; while (top - bot > 1) { int mid = (top + bot) / 2; if (ok(mid)) bot = mid; else top = mid; } cout << bot << endl; return 0; }
replace
83
84
83
84
TLE
p02774
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; int n = 0, x = 0, k = 0; int arr[200001]; vector<int> otr; vector<int> pol; vector<int> zeroes; int otr_p = 0, pol_p = 0, zero_p = 0; int pol_pol(int a, int b) { int ll = 0, rr = pol.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * pol[mm] > b) rr = mm; else ll = mm; } if (a * pol[ll] > b) return -1; else if (a * pol[rr] > b) return ll; else return rr; } int pol_otr(int a, int b) { int ll = 0, rr = otr.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * otr[mm] > b) rr = mm; else ll = mm; } if (a * otr[ll] > b) return -1; else if (a * otr[rr] > b) return ll; else return rr; } int otr_otr(int a, int b) { int ll = 0, rr = otr.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * otr[mm] > b) ll = mm; else rr = mm; } if (a * otr[rr] > b) return -1; else if (a * otr[ll] > b) return rr; else return ll; } int cnt(int x) { int res = 0; for (int i = 0; i < pol.size(); i++) { int ii = pol_pol(pol[i], x); if (ii == -1) continue; res += max(0ll, ii - i); } for (int i = 0; i < otr.size(); i++) { int ii = otr_otr(otr[i], x); if (ii == -1) continue; ii = max(ii, i + 1); res += otr.size() - ii; } for (int i = 0; i < pol.size(); i++) { int ii = pol_otr(pol[i], x); if (ii == -1) continue; res += ii + 1; } if (x >= 0) res += (zeroes.size() * (n - zeroes.size())) + (zeroes.size() * (zeroes.size() - 1) / 2); return res; } signed main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 0; i < n; i++) { if (arr[i] < 0) otr.push_back(arr[i]); else if (arr[i] == 0) zeroes.push_back(arr[i]); else pol.push_back(arr[i]); } otr_p = otr.size() * pol.size(); zero_p = zeroes.size() * (n - zeroes.size()); pol_p = pol.size() * (pol.size() - 1) / 2 + otr.size() * (otr.size() - 1) / 2; int ll = LLONG_MIN / 3; int rr = LLONG_MAX / 3; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (cnt(mm) < k) ll = mm; else rr = mm; } if (cnt(rr) < k) cout << rr + 1; else if (cnt(ll) < k) cout << rr; else cout << ll; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int n = 0, x = 0, k = 0; int arr[200001]; vector<int> otr; vector<int> pol; vector<int> zeroes; int otr_p = 0, pol_p = 0, zero_p = 0; int pol_pol(int a, int b) { int ll = 0, rr = pol.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * pol[mm] > b) rr = mm; else ll = mm; } if (a * pol[ll] > b) return -1; else if (a * pol[rr] > b) return ll; else return rr; } int pol_otr(int a, int b) { if (otr.size() == 0) return -1; int ll = 0, rr = otr.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * otr[mm] > b) rr = mm; else ll = mm; } if (a * otr[ll] > b) return -1; else if (a * otr[rr] > b) return ll; else return rr; } int otr_otr(int a, int b) { int ll = 0, rr = otr.size() - 1; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (a * otr[mm] > b) ll = mm; else rr = mm; } if (a * otr[rr] > b) return -1; else if (a * otr[ll] > b) return rr; else return ll; } int cnt(int x) { int res = 0; for (int i = 0; i < pol.size(); i++) { int ii = pol_pol(pol[i], x); if (ii == -1) continue; res += max(0ll, ii - i); } for (int i = 0; i < otr.size(); i++) { int ii = otr_otr(otr[i], x); if (ii == -1) continue; ii = max(ii, i + 1); res += otr.size() - ii; } for (int i = 0; i < pol.size(); i++) { int ii = pol_otr(pol[i], x); if (ii == -1) continue; res += ii + 1; } if (x >= 0) res += (zeroes.size() * (n - zeroes.size())) + (zeroes.size() * (zeroes.size() - 1) / 2); return res; } signed main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = 0; i < n; i++) { if (arr[i] < 0) otr.push_back(arr[i]); else if (arr[i] == 0) zeroes.push_back(arr[i]); else pol.push_back(arr[i]); } otr_p = otr.size() * pol.size(); zero_p = zeroes.size() * (n - zeroes.size()); pol_p = pol.size() * (pol.size() - 1) / 2 + otr.size() * (otr.size() - 1) / 2; int ll = LLONG_MIN / 3; int rr = LLONG_MAX / 3; while (ll < rr - 1) { int mm = (ll + rr) / 2; if (cnt(mm) < k) ll = mm; else rr = mm; } if (cnt(rr) < k) cout << rr + 1; else if (cnt(ll) < k) cout << rr; else cout << ll; return 0; }
insert
30
30
30
32
0
p02774
C++
Time Limit Exceeded
// #define NDEBUG #include "bits/stdc++.h" #include <array> #include <iostream> #include <random> #include <string> #include <unordered_map> #include <unordered_set> #ifdef _MSC_VER #include <ppl.h> // #include <boost/multiprecision/cpp_dec_float.hpp> // #include <boost/multiprecision/cpp_int.hpp> // #include <boost/rational.hpp> // #else // #include <omp.h> #endif using namespace std; #define DUMPOUT cerr #define dump(...) \ DUMPOUT << " "; \ DUMPOUT << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) using uint = unsigned; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using pss = pair<string, string>; template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const pair<_KTy, _Ty> &m) { o << "{" << m.first << ", " << m.second << "}"; return o; } template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const map<_KTy, _Ty> &m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const unordered_map<_KTy, _Ty> &m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const vector<_Ty> &v) { if (v.empty()) { o << "{ }"; return o; } o << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const deque<_Ty> &v) { if (v.empty()) { o << "{ }"; return o; } o << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const set<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const unordered_set<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const stack<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } stack<_Ty> t(s); o << "{" << t.top(); t.pop(); while (!t.empty()) { o << ", " << t.top(); t.pop(); } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const list<_Ty> &l) { if (l.empty()) { o << "{ }"; return o; } o << "{" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { o << ", " << *itr; } o << "}"; return o; } template <typename _KTy, typename _Ty> istream &operator>>(istream &is, pair<_KTy, _Ty> &m) { is >> m.first >> m.second; return is; } template <typename _Ty> istream &operator>>(istream &is, vector<_Ty> &v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } template <typename _Ty> istream &operator>>(istream &is, deque<_Ty> &v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } namespace aux { // print tuple template <typename Ty, unsigned N, unsigned L> struct tp { static void print(ostream &os, const Ty &v) { os << get<N>(v) << ", "; tp<Ty, N + 1, L>::print(os, v); } }; template <typename Ty, unsigned N> struct tp<Ty, N, N> { static void print(ostream &os, const Ty &v) { os << get<N>(v); } }; } // namespace aux template <typename... Tys> ostream &operator<<(ostream &os, const tuple<Tys...> &t) { os << "{"; aux::tp<tuple<Tys...>, 0, sizeof...(Tys) - 1>::print(os, t); os << "}"; return os; } template <typename A, size_t N, typename T> inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(move(tail)...); } #define PI 3.14159265358979323846 #define EPS 1e-8 #define FOR(t, a, n) for (int t = (a); t < (n); ++t) #define REP(t, n) FOR(t, 0, n) #define all(j) (j).begin(), (j).end() #define SZ(j) ((int)(j).size()) #define fake false class Timer { public: double t = 0; Timer() {} static double time() { #ifdef _MSC_VER return __rdtsc() / 2.8e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.8e9; #endif } void measure() { t = time() - t; } double elapsedMs() { return (time() - t) * 1000.0; } } timer; struct Xorshift { uint64_t x = 88172645463325252LL; unsigned next_int() { x = x ^ (x << 7); return x = x ^ (x >> 9); } unsigned next_int(unsigned mod) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % mod; } unsigned next_int(unsigned l, unsigned r) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % (r - l + 1) + l; } double next_double() { return double(next_int()) / UINT_MAX; } } rnd; template <typename T> void shuffle_vector(vector<T> &v, Xorshift &rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); swap(v[i], v[r]); } } ll solve(ll N, ll K, vector<ll> A) { vector<ll> neg, pos; ll zeros = 0; REP(i, N) { if (A[i] < 0) neg.push_back(A[i]); else if (A[i] > 0) pos.push_back(A[i]); else zeros++; } ll below_zero = neg.size() * pos.size(); ll just_zero = zeros * (N - zeros) + zeros * (zeros - 1) / 2; ll upper_zero = N * (N - 1) / 2 - just_zero - below_zero; if (below_zero < K && K <= below_zero + just_zero) { return 0; } vector<ll> neg_a(neg), neg_d(neg), pos_a(pos), pos_d(pos); sort(all(neg_a)); sort(all(neg_d), greater<ll>()); sort(all(pos_a)); sort(all(pos_d), greater<ll>()); auto bin = [&](ll ng, ll ok, ll x, ll n, const vector<ll> &v) { while (abs(ok - ng) > 1) { ll mid = (ng + ok) / 2; (n * v[mid] <= x ? ok : ng) = mid; } return ok; }; auto under_x = [&](ll x) { ll ret = 0; if (x < 0) { if (neg_a.front() * pos_d.front() > x) return ret; for (ll n : neg_a) { if (n * pos_d.front() > x) continue; if (n * pos_d.back() <= x) { ret += pos_d.size(); continue; } // n * p <= x なる x の個数を求める ret += bin(pos_d.size() - 1, 0, x, n, pos_d) + 1; } } else { ret = below_zero + just_zero; // n1 * n2 <= x for (int i = 0; i < (int)neg_d.size() - 1; i++) { ll n1 = neg_d[i], n2 = neg_d[i + 1]; if (n1 * n2 > x) break; if (n1 * neg_d.back() <= x) { ret += neg_d.size() - i - 1; continue; } ret += bin(neg_d.size() - 1, i + 1, x, n1, neg_d) - i; } // p1 * p2 <= x for (int i = 0; i < (int)pos_a.size() - 1; i++) { ll p1 = pos_a[i], p2 = pos_a[i + 1]; if (p1 * p2 > x) break; if (p1 * pos_a.back() <= x) { ret += pos_a.size() - i - 1; continue; } ret += bin(pos_a.size() - 1, i + 1, x, p1, pos_a) - i; } } return ret; }; auto under_x_naive = [&](ll x) { ll ret = 0; for (int i = 0; i < (int)A.size() - 1; i++) { for (int j = i + 1; j < A.size(); j++) { if (A[i] * A[j] <= x) ret++; } } return ret; }; auto bin2 = [&](ll ng, ll ok, ll k) { // x 以下の個数が k 以上となるはじめての x を求める while (abs(ok - ng) > 1) { ll mid = (ng + ok) / 2; if (under_x_naive(mid) != under_x(mid)) { under_x_naive(mid); under_x(mid); } (under_x_naive(mid) >= k ? ok : ng) = mid; } return ok; }; return bin2(INT64_MIN / 2, INT64_MAX / 2, K); } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; vector<ll> A(N); cin >> A; cout << solve(N, K, A) << endl; return 0; }
// #define NDEBUG #include "bits/stdc++.h" #include <array> #include <iostream> #include <random> #include <string> #include <unordered_map> #include <unordered_set> #ifdef _MSC_VER #include <ppl.h> // #include <boost/multiprecision/cpp_dec_float.hpp> // #include <boost/multiprecision/cpp_int.hpp> // #include <boost/rational.hpp> // #else // #include <omp.h> #endif using namespace std; #define DUMPOUT cerr #define dump(...) \ DUMPOUT << " "; \ DUMPOUT << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) using uint = unsigned; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using pss = pair<string, string>; template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const pair<_KTy, _Ty> &m) { o << "{" << m.first << ", " << m.second << "}"; return o; } template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const map<_KTy, _Ty> &m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _KTy, typename _Ty> ostream &operator<<(ostream &o, const unordered_map<_KTy, _Ty> &m) { if (m.empty()) { o << "{ }"; return o; } o << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const vector<_Ty> &v) { if (v.empty()) { o << "{ }"; return o; } o << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const deque<_Ty> &v) { if (v.empty()) { o << "{ }"; return o; } o << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const set<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const unordered_set<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } o << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { o << ", " << *itr; } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const stack<_Ty> &s) { if (s.empty()) { o << "{ }"; return o; } stack<_Ty> t(s); o << "{" << t.top(); t.pop(); while (!t.empty()) { o << ", " << t.top(); t.pop(); } o << "}"; return o; } template <typename _Ty> ostream &operator<<(ostream &o, const list<_Ty> &l) { if (l.empty()) { o << "{ }"; return o; } o << "{" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { o << ", " << *itr; } o << "}"; return o; } template <typename _KTy, typename _Ty> istream &operator>>(istream &is, pair<_KTy, _Ty> &m) { is >> m.first >> m.second; return is; } template <typename _Ty> istream &operator>>(istream &is, vector<_Ty> &v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } template <typename _Ty> istream &operator>>(istream &is, deque<_Ty> &v) { for (size_t t = 0; t < v.size(); t++) is >> v[t]; return is; } namespace aux { // print tuple template <typename Ty, unsigned N, unsigned L> struct tp { static void print(ostream &os, const Ty &v) { os << get<N>(v) << ", "; tp<Ty, N + 1, L>::print(os, v); } }; template <typename Ty, unsigned N> struct tp<Ty, N, N> { static void print(ostream &os, const Ty &v) { os << get<N>(v); } }; } // namespace aux template <typename... Tys> ostream &operator<<(ostream &os, const tuple<Tys...> &t) { os << "{"; aux::tp<tuple<Tys...>, 0, sizeof...(Tys) - 1>::print(os, t); os << "}"; return os; } template <typename A, size_t N, typename T> inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(move(tail)...); } #define PI 3.14159265358979323846 #define EPS 1e-8 #define FOR(t, a, n) for (int t = (a); t < (n); ++t) #define REP(t, n) FOR(t, 0, n) #define all(j) (j).begin(), (j).end() #define SZ(j) ((int)(j).size()) #define fake false class Timer { public: double t = 0; Timer() {} static double time() { #ifdef _MSC_VER return __rdtsc() / 2.8e9; #else unsigned long long a, d; __asm__ volatile("rdtsc" : "=a"(a), "=d"(d)); return (d << 32 | a) / 2.8e9; #endif } void measure() { t = time() - t; } double elapsedMs() { return (time() - t) * 1000.0; } } timer; struct Xorshift { uint64_t x = 88172645463325252LL; unsigned next_int() { x = x ^ (x << 7); return x = x ^ (x >> 9); } unsigned next_int(unsigned mod) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % mod; } unsigned next_int(unsigned l, unsigned r) { x = x ^ (x << 7); x = x ^ (x >> 9); return x % (r - l + 1) + l; } double next_double() { return double(next_int()) / UINT_MAX; } } rnd; template <typename T> void shuffle_vector(vector<T> &v, Xorshift &rnd) { int n = v.size(); for (int i = n - 1; i >= 1; i--) { int r = rnd.next_int(i); swap(v[i], v[r]); } } ll solve(ll N, ll K, vector<ll> A) { vector<ll> neg, pos; ll zeros = 0; REP(i, N) { if (A[i] < 0) neg.push_back(A[i]); else if (A[i] > 0) pos.push_back(A[i]); else zeros++; } ll below_zero = neg.size() * pos.size(); ll just_zero = zeros * (N - zeros) + zeros * (zeros - 1) / 2; ll upper_zero = N * (N - 1) / 2 - just_zero - below_zero; if (below_zero < K && K <= below_zero + just_zero) { return 0; } vector<ll> neg_a(neg), neg_d(neg), pos_a(pos), pos_d(pos); sort(all(neg_a)); sort(all(neg_d), greater<ll>()); sort(all(pos_a)); sort(all(pos_d), greater<ll>()); auto bin = [&](ll ng, ll ok, ll x, ll n, const vector<ll> &v) { while (abs(ok - ng) > 1) { ll mid = (ng + ok) / 2; (n * v[mid] <= x ? ok : ng) = mid; } return ok; }; auto under_x = [&](ll x) { ll ret = 0; if (x < 0) { if (neg_a.front() * pos_d.front() > x) return ret; for (ll n : neg_a) { if (n * pos_d.front() > x) continue; if (n * pos_d.back() <= x) { ret += pos_d.size(); continue; } // n * p <= x なる x の個数を求める ret += bin(pos_d.size() - 1, 0, x, n, pos_d) + 1; } } else { ret = below_zero + just_zero; // n1 * n2 <= x for (int i = 0; i < (int)neg_d.size() - 1; i++) { ll n1 = neg_d[i], n2 = neg_d[i + 1]; if (n1 * n2 > x) break; if (n1 * neg_d.back() <= x) { ret += neg_d.size() - i - 1; continue; } ret += bin(neg_d.size() - 1, i + 1, x, n1, neg_d) - i; } // p1 * p2 <= x for (int i = 0; i < (int)pos_a.size() - 1; i++) { ll p1 = pos_a[i], p2 = pos_a[i + 1]; if (p1 * p2 > x) break; if (p1 * pos_a.back() <= x) { ret += pos_a.size() - i - 1; continue; } ret += bin(pos_a.size() - 1, i + 1, x, p1, pos_a) - i; } } return ret; }; auto under_x_naive = [&](ll x) { ll ret = 0; for (int i = 0; i < (int)A.size() - 1; i++) { for (int j = i + 1; j < A.size(); j++) { if (A[i] * A[j] <= x) ret++; } } return ret; }; auto bin2 = [&](ll ng, ll ok, ll k) { // x 以下の個数が k 以上となるはじめての x を求める while (abs(ok - ng) > 1) { ll mid = (ng + ok) / 2; (under_x(mid) >= k ? ok : ng) = mid; } return ok; }; return bin2(INT64_MIN / 2, INT64_MAX / 2, K); } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, K; cin >> N >> K; vector<ll> A(N); cin >> A; cout << solve(N, K, A) << endl; return 0; }
replace
339
344
339
340
TLE
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void) { ll n, k; cin >> n >> k; vector<ll> a(n); for (auto &e : a) cin >> e; sort(a.begin(), a.end()); ll ng = 2e18, ok = -2e18; // ok譛ェ貅€縺ョ蛟区焚 < k while (ng - ok > 1) { ll m = (ok + ng) / 2; ll s = 0; for (ll i = 0; i < n; i++) { auto chk = [&]() { ll r = 0; for (ll j = 0; j < n; j++) { if (i == j) continue; r += a[j] * a[i] < m; } return r; }; if (a[i] < 0) { // a[x] * a[i] < m ll j = [&]() { ll ok = n, ng = -1; while (abs(ng - ok) > 1) { ll mm = (ok + ng) / 2; (a[mm] * a[i] < m ? ok : ng) = mm; } return ok; }(); // ng ng [ok] ok ok if (i >= j) j++; s += max(n - j, 0ll); assert(max(n - j, 0ll) == chk()); } if (a[i] > 0) { // a[x] * a[i] < m ll j = [&]() { ll ok = 0, ng = n; if (!(a[0] * a[i] < m)) return -1ll; while (abs(ng - ok) > 1) { ll mm = (ok + ng) / 2; (a[mm] * a[i] < m ? ok : ng) = mm; } return ok; }(); // ok ok [ok] ng ng if (i <= j) j--; s += max(j + 1, 0ll); } if (a[i] == 0) { // a[x] * 0 < m s += m > 0 ? n - 1 : 0; } } assert(s % 2 == 0); s /= 2; (s < k ? ok : ng) = m; } cout << ok << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void) { ll n, k; cin >> n >> k; vector<ll> a(n); for (auto &e : a) cin >> e; sort(a.begin(), a.end()); ll ng = 2e18, ok = -2e18; // ok譛ェ貅€縺ョ蛟区焚 < k while (ng - ok > 1) { ll m = (ok + ng) / 2; ll s = 0; for (ll i = 0; i < n; i++) { auto chk = [&]() { ll r = 0; for (ll j = 0; j < n; j++) { if (i == j) continue; r += a[j] * a[i] < m; } return r; }; if (a[i] < 0) { // a[x] * a[i] < m ll j = [&]() { ll ok = n, ng = -1; while (abs(ng - ok) > 1) { ll mm = (ok + ng) / 2; (a[mm] * a[i] < m ? ok : ng) = mm; } return ok; }(); // ng ng [ok] ok ok if (i >= j) j++; s += max(n - j, 0ll); } if (a[i] > 0) { // a[x] * a[i] < m ll j = [&]() { ll ok = 0, ng = n; if (!(a[0] * a[i] < m)) return -1ll; while (abs(ng - ok) > 1) { ll mm = (ok + ng) / 2; (a[mm] * a[i] < m ? ok : ng) = mm; } return ok; }(); // ok ok [ok] ng ng if (i <= j) j--; s += max(j + 1, 0ll); } if (a[i] == 0) { // a[x] * 0 < m s += m > 0 ? n - 1 : 0; } } assert(s % 2 == 0); s /= 2; (s < k ? ok : ng) = m; } cout << ok << endl; }
delete
46
47
46
46
TLE
p02774
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define _ << " " << #define all(X) (X).begin(), (X).end() #define len(X) (X).size() #define Pii pair<int, int> #define Pll pair<ll, ll> #define Tiii tuple<int, int, int> #define Tlll tuple<ll, ll, ll> ll count_pos(ll val, vector<ll> a, vector<ll> d) { ll cnt = 0; for (int i = 0; i < a.size(); i++) { ll bd = val / a[i]; ll l = -1, r = a.size(); while (r - l > 1) { ll m = (l + r) / 2; if (a[m] <= bd) l = m; else r = m; } cnt += l + 1; if (i <= l) cnt--; // cerr << val _ bd _ i _ l _ cnt << endl; // } for (int i = 0; i < d.size(); i++) { ll bd = val / d[i]; ll l = -1, r = d.size(); while (r - l > 1) { ll m = (l + r) / 2; if (d[m] <= bd) l = m; else r = m; } cnt += l + 1; if (i <= l) cnt--; // cerr << val _ bd _ i _ l _ cnt << endl; // } return cnt / 2; } ll count_neg(ll val, vector<ll> b, vector<ll> c) { ll cnt = 0; for (int i = 0; i < c.size(); i++) { ll bd = val / c[i] + bool(val % c[i]); ll l = -1, r = b.size(); while (r - l > 1) { ll m = (l + r) / 2; if (b[m] < bd) l = m; else r = m; } cnt += b.size() - r; cerr << val _ bd _ i _ l _ cnt << endl; // } return cnt; } int main() { ll n, k, neg = 0, zero = 0; cin >> n >> k; vector<ll> a(n), b, c, d; for (int i = 0; i < n; i++) { cin >> a[i]; neg += (a[i] < 0); zero += (a[i] == 0); if (a[i] > 0) b.push_back(a[i]); if (a[i] < 0) { c.push_back(a[i]); d.push_back(-a[i]); } } sort(all(a)); sort(all(b)); sort(all(c)); sort(all(d)); neg = neg * (n - zero - neg); zero = zero * (zero - 1) / 2 + zero * (n - zero); cerr << neg _ zero << endl; if (neg >= k) { ll l = -2e18, r = 0; while (r - l > 1) { ll m = (l + r) / 2; if (count_neg(m, b, c) >= k) r = m; else l = m; } cout << r << endl; } else if (neg + zero >= k) { cout << 0 << endl; } else { k -= neg + zero; ll l = 0, r = 2e18; // cerr << k << endl; while (r - l > 1) { ll m = (l + r) / 2; // cerr << m _ count_pos(m, b, d) << endl; if (count_pos(m, b, d) >= k) r = m; else l = m; } cout << r << endl; } }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; #define _ << " " << #define all(X) (X).begin(), (X).end() #define len(X) (X).size() #define Pii pair<int, int> #define Pll pair<ll, ll> #define Tiii tuple<int, int, int> #define Tlll tuple<ll, ll, ll> ll count_pos(ll val, vector<ll> a, vector<ll> d) { ll cnt = 0; for (int i = 0; i < a.size(); i++) { ll bd = val / a[i]; ll l = -1, r = a.size(); while (r - l > 1) { ll m = (l + r) / 2; if (a[m] <= bd) l = m; else r = m; } cnt += l + 1; if (i <= l) cnt--; // cerr << val _ bd _ i _ l _ cnt << endl; // } for (int i = 0; i < d.size(); i++) { ll bd = val / d[i]; ll l = -1, r = d.size(); while (r - l > 1) { ll m = (l + r) / 2; if (d[m] <= bd) l = m; else r = m; } cnt += l + 1; if (i <= l) cnt--; // cerr << val _ bd _ i _ l _ cnt << endl; // } return cnt / 2; } ll count_neg(ll val, vector<ll> b, vector<ll> c) { ll cnt = 0; for (int i = 0; i < c.size(); i++) { ll bd = val / c[i] + bool(val % c[i]); ll l = -1, r = b.size(); while (r - l > 1) { ll m = (l + r) / 2; if (b[m] < bd) l = m; else r = m; } cnt += b.size() - r; // cerr << val _ bd _ i _ l _ cnt << endl; // } return cnt; } int main() { ll n, k, neg = 0, zero = 0; cin >> n >> k; vector<ll> a(n), b, c, d; for (int i = 0; i < n; i++) { cin >> a[i]; neg += (a[i] < 0); zero += (a[i] == 0); if (a[i] > 0) b.push_back(a[i]); if (a[i] < 0) { c.push_back(a[i]); d.push_back(-a[i]); } } sort(all(a)); sort(all(b)); sort(all(c)); sort(all(d)); neg = neg * (n - zero - neg); zero = zero * (zero - 1) / 2 + zero * (n - zero); cerr << neg _ zero << endl; if (neg >= k) { ll l = -2e18, r = 0; while (r - l > 1) { ll m = (l + r) / 2; if (count_neg(m, b, c) >= k) r = m; else l = m; } cout << r << endl; } else if (neg + zero >= k) { cout << 0 << endl; } else { k -= neg + zero; ll l = 0, r = 2e18; // cerr << k << endl; while (r - l > 1) { ll m = (l + r) / 2; // cerr << m _ count_pos(m, b, d) << endl; if (count_pos(m, b, d) >= k) r = m; else l = m; } cout << r << endl; } }
replace
75
76
75
76
TLE
p02774
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define ll long long #define mp make_pair #define pb push_back #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int maxn = 1e5 + 5; int n; ll k; ll a[maxn]; vector<ll> pos, neg, zero; int psz, nsz, zsz; ll NEG, ZERO; ll count(ll x) { if (x < 0) { ll ans = 0; rep(i, psz) { ll thr = -((-x + pos[i] - 1) / pos[i]); ans += upper_bound(neg.begin(), neg.end(), thr) - neg.begin(); } return ans; } else if (x == 0) { return NEG + ZERO; } else { ll ans = NEG + ZERO; rep(i, psz) { ll thr = x / pos[i]; ans += min(pos.begin() + i, upper_bound(pos.begin(), pos.end(), thr)) - pos.begin(); } rep(i, nsz) { ll thr = -(x / -neg[i]); ans += neg.end() - max(neg.begin() + i + 1, lower_bound(neg.begin(), neg.end(), thr)); } return ans; } } int main() { scanf("%d %lld", &n, &k); rep(i, n) scanf("%lld", &a[i]); rep(i, n) { if (a[i] > 0) pos.pb(a[i]); else if (a[i] == 0) zero.pb(a[i]); else neg.pb(a[i]); } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); sort(zero.begin(), zero.end()); psz = pos.size(), nsz = neg.size(), zsz = zero.size(); NEG = 1LL * psz * nsz; ZERO = 1LL * zsz * (zsz - 1) / 2 + 1LL * zsz * (psz + nsz); ll lb = -1e18, rb = 1e18; while (lb < rb) { ll md = lb + (rb - lb) / 2; if (count(md) < k) lb = md + 1; else rb = md; } printf("%lld\n", lb); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define ll long long #define mp make_pair #define pb push_back #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int maxn = 2e5 + 5; int n; ll k; ll a[maxn]; vector<ll> pos, neg, zero; int psz, nsz, zsz; ll NEG, ZERO; ll count(ll x) { if (x < 0) { ll ans = 0; rep(i, psz) { ll thr = -((-x + pos[i] - 1) / pos[i]); ans += upper_bound(neg.begin(), neg.end(), thr) - neg.begin(); } return ans; } else if (x == 0) { return NEG + ZERO; } else { ll ans = NEG + ZERO; rep(i, psz) { ll thr = x / pos[i]; ans += min(pos.begin() + i, upper_bound(pos.begin(), pos.end(), thr)) - pos.begin(); } rep(i, nsz) { ll thr = -(x / -neg[i]); ans += neg.end() - max(neg.begin() + i + 1, lower_bound(neg.begin(), neg.end(), thr)); } return ans; } } int main() { scanf("%d %lld", &n, &k); rep(i, n) scanf("%lld", &a[i]); rep(i, n) { if (a[i] > 0) pos.pb(a[i]); else if (a[i] == 0) zero.pb(a[i]); else neg.pb(a[i]); } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); sort(zero.begin(), zero.end()); psz = pos.size(), nsz = neg.size(), zsz = zero.size(); NEG = 1LL * psz * nsz; ZERO = 1LL * zsz * (zsz - 1) / 2 + 1LL * zsz * (psz + nsz); ll lb = -1e18, rb = 1e18; while (lb < rb) { ll md = lb + (rb - lb) / 2; if (count(md) < k) lb = md + 1; else rb = md; } printf("%lld\n", lb); return 0; }
replace
24
25
24
25
0
p02774
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second #define INF 1e9 // #define long long long long long long typedef long long ll; ll mod = 1e9 + 7; long long fast_exp(long long a, long long b) { if (b <= 0) return 1; else { long long res = 1; res = fast_exp(a, b / 2); res = (res * res) % mod; if (b % 2 == 1) res = (res * a) % mod; return res; } } typedef long double ld; const ll N = 1e5 + 5; long long num_strictly_less(vector<long long> &v, long long val) { long long n = v.size(); long long truecount = 0; for (long long i = 0; i < n - 1; i++) { long long l = i + 1, r = n - 1; long long cnt = 0; if (v[i] < 0) { while (l < r) { long long mid = (l + r) / 2; if (v[mid] * v[i] < val) r = mid; else l = mid + 1; } if (v[l] * v[i] < val) cnt += n - l; } else if (v[i] == 0) { if (val > 0) cnt += r - l + 1; } else { while (l < r) { long long mid = (l + r + 1) / 2; if (v[mid] * v[i] < val) l = mid; else r = mid - 1; } if (v[l] * v[i] < val) cnt += l - i; } truecount += cnt; } return truecount; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<long long> v; long long x, n, k; cin >> n >> k; k--; for (long long i = 0; i < n; i++) { cin >> x; v.pb(x); } sort(v.begin(), v.end()); long long l = -1e18, r = 1e18; while (l < r) { long long mid = (l + r) / 2; long long lesser = num_strictly_less(v, mid); if (lesser <= k) l = mid; else r = mid - 1; } cout << l << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second #define INF 1e9 // #define long long long long long long typedef long long ll; ll mod = 1e9 + 7; long long fast_exp(long long a, long long b) { if (b <= 0) return 1; else { long long res = 1; res = fast_exp(a, b / 2); res = (res * res) % mod; if (b % 2 == 1) res = (res * a) % mod; return res; } } typedef long double ld; const ll N = 1e5 + 5; long long num_strictly_less(vector<long long> &v, long long val) { long long n = v.size(); long long truecount = 0; for (long long i = 0; i < n - 1; i++) { long long l = i + 1, r = n - 1; long long cnt = 0; if (v[i] < 0) { while (l < r) { long long mid = (l + r) / 2; if (v[mid] * v[i] < val) r = mid; else l = mid + 1; } if (v[l] * v[i] < val) cnt += n - l; } else if (v[i] == 0) { if (val > 0) cnt += r - l + 1; } else { while (l < r) { long long mid = (l + r + 1) / 2; if (v[mid] * v[i] < val) l = mid; else r = mid - 1; } if (v[l] * v[i] < val) cnt += l - i; } truecount += cnt; } return truecount; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<long long> v; long long x, n, k; cin >> n >> k; k--; for (long long i = 0; i < n; i++) { cin >> x; v.pb(x); } sort(v.begin(), v.end()); long long l = -1e18, r = 1e18; while (l < r) { long long mid = (l + r + 1) / 2; long long lesser = num_strictly_less(v, mid); if (lesser <= k) l = mid; else r = mid - 1; } cout << l << endl; return 0; }
replace
78
79
78
79
TLE
p02774
C++
Runtime Error
// Author: Vamsi Krishna Reddy Satti // With love for Competitive Programming! #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; string to_string(const string &s) { return '"' + s + '"'; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 3 #endif #define cin_exception cin.exceptions(cin.failbit); #define cout_precision \ cout.setf(ios::fixed); \ cout.precision(15); #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr); using ll = int_fast64_t; using ld = long double; // ----------------------------------------------------------------------------- const int N = 2e5 + 5, A = 1e9; int n, k, a[N]; int x, y; ll f(ll v) { ll ret = 0; int b = 0; for (int i = 0; i < x; ++i) { while (b != n && ll(1) * a[i] * a[b] >= v) ++b; while (b != 0 && ll(1) * a[i] * a[b - 1] < v) --b; ret += max(i - b, 0); } for (int i = x; i < y; ++i) { if (v > 0) ret += i; } for (int i = y; i < n; ++i) { while (b != n && ll(1) * a[i] * a[b] < v) ++b; while (b != 0 && ll(1) * a[i] * a[b - 1] >= v) --b; ret += min(i, b); } return ret; } void solve() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); x = lower_bound(a, a + n, 0) - a; y = upper_bound(a, a + n, 0) - a; ll r = ll(1) * A * A + 1, l = -r; while (l + 1 < r) { ll m = (l + r) >> 1; ll n_less = f(m); if (n_less < k) l = m; else r = m; } cout << l << '\n'; } int main() { fast_io; cin_exception; int t = 1; // cin >> t; for (int i = 0; i < t; ++i) { solve(); } }
// Author: Vamsi Krishna Reddy Satti // With love for Competitive Programming! #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; string to_string(const string &s) { return '"' + s + '"'; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 3 #endif #define cin_exception cin.exceptions(cin.failbit); #define cout_precision \ cout.setf(ios::fixed); \ cout.precision(15); #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr); using ll = int_fast64_t; using ld = long double; // ----------------------------------------------------------------------------- const int N = 2e5 + 5, A = 1e9; int n, a[N]; ll k; int x, y; ll f(ll v) { ll ret = 0; int b = 0; for (int i = 0; i < x; ++i) { while (b != n && ll(1) * a[i] * a[b] >= v) ++b; while (b != 0 && ll(1) * a[i] * a[b - 1] < v) --b; ret += max(i - b, 0); } for (int i = x; i < y; ++i) { if (v > 0) ret += i; } for (int i = y; i < n; ++i) { while (b != n && ll(1) * a[i] * a[b] < v) ++b; while (b != 0 && ll(1) * a[i] * a[b - 1] >= v) --b; ret += min(i, b); } return ret; } void solve() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); x = lower_bound(a, a + n, 0) - a; y = upper_bound(a, a + n, 0) - a; ll r = ll(1) * A * A + 1, l = -r; while (l + 1 < r) { ll m = (l + r) >> 1; ll n_less = f(m); if (n_less < k) l = m; else r = m; } cout << l << '\n'; } int main() { fast_io; cin_exception; int t = 1; // cin >> t; for (int i = 0; i < t; ++i) { solve(); } }
replace
33
34
33
35
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct init_ { init_() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init_; template <class C> using iodef_rng_t = typename enable_if<decltype(declval<C>().begin(), true_type{})::value && !is_same<C, string>::value>::type; template <class C, class = iodef_rng_t<C>> istream &operator>>(istream &in, C &c) { for (auto &v : c) { in >> v; } return in; } template <class C, class = iodef_rng_t<C>> ostream &operator<<(ostream &out, const C &c) { int i = 0; for (const auto &v : c) { if (i++) { out << ' '; } out << v; } return out; } template <size_t N, class T, class R> using when_last = typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1, R>::type; template <size_t N, class T, class R> using when_init = typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1, R>::type; template <size_t N = 0, class T> auto operator>>(istream &in, T &&t) -> when_last<N, T, istream &> { return in >> get<N>(t); } template <size_t N = 0, class T> auto operator>>(istream &in, T &&t) -> when_init<N, T, istream &> { return operator>><N + 1>(in >> get<N>(t), forward<T>(t)); } template <size_t N = 0, class T> auto operator<<(ostream &out, const T &t) -> when_last<N, T, ostream &> { return out << get<N>(t); } template <size_t N = 0, class T> auto operator<<(ostream &out, const T &t) -> when_init<N, T, ostream &> { return operator<< <N + 1>(out << get<N>(t) << ' ', t); } #ifdef LOCAL template <class T> void trace_(const char *s, const T &v) { cerr << s << "=" << v; } template <class T, class... Ts> void trace_(const char *s, const T &v, const Ts &...vs) { for (; *s != ','; ++s) { cerr << *s; } cerr << "=" << v << ","; trace_(++s, vs...); } #define TRACE(...) \ (cerr << "\033[2m\033[33m", trace_(#__VA_ARGS__, __VA_ARGS__), \ cerr << "\033[0m\n") #else #define TRACE(...) #endif template <class Int, class Pred> Int bsearch(Int first, Int last, Pred p) noexcept { size_t len = last - first, half; Int mid; while (len > 0) { half = len >> 1; mid = first + half; if (p(mid)) { first = mid + 1; len = len - half - 1; } else { len = half; } } return first; } int main() { int64_t n, k; cin >> n >> k; vector<int64_t> p, m, z; for (int i = 0; i < n; i++) { int64_t x; cin >> x; if (x == 0) z.push_back(x); else if (x > 0) p.push_back(x); else m.push_back(-x); } int64_t num_m_ans = p.size() * m.size(); int64_t num_z_ans = z.size() * (z.size() - 1) / 2 + z.size() * (m.size() + p.size()); TRACE(num_m_ans, num_z_ans); int64_t ans; if (k <= num_m_ans) { sort(p.begin(), p.end()); sort(m.begin(), m.end()); ans = -bsearch(1L, p.back() * m.back() + 1, [&](int64_t x) { int64_t lowerx = 0; for (size_t i = 0; i < p.size(); i++) { lowerx += bsearch(0UL, m.size(), [&](size_t j) { return m[j] <= x / p[i]; }); } TRACE(x, lowerx); return lowerx < num_m_ans - k + 1; }); } else if (k <= num_m_ans + num_z_ans) { ans = 0; } else { sort(p.begin(), p.end()); sort(m.begin(), m.end()); int64_t max_abs = max(p.back(), m.back()); ans = bsearch(1L, max_abs * max_abs + 1, [&](int64_t x) { int64_t lowerx = 0; for (size_t i = 1; i < p.size(); i++) { lowerx += bsearch(0UL, i, [&](size_t j) { return p[j] <= x / p[i]; }); } for (size_t i = 1; i < m.size(); i++) { lowerx += bsearch(0UL, i, [&](size_t j) { return m[j] <= x / m[i]; }); } TRACE(x, lowerx); return lowerx < k - num_m_ans - num_z_ans; }); } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; struct init_ { init_() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } init_; template <class C> using iodef_rng_t = typename enable_if<decltype(declval<C>().begin(), true_type{})::value && !is_same<C, string>::value>::type; template <class C, class = iodef_rng_t<C>> istream &operator>>(istream &in, C &c) { for (auto &v : c) { in >> v; } return in; } template <class C, class = iodef_rng_t<C>> ostream &operator<<(ostream &out, const C &c) { int i = 0; for (const auto &v : c) { if (i++) { out << ' '; } out << v; } return out; } template <size_t N, class T, class R> using when_last = typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1, R>::type; template <size_t N, class T, class R> using when_init = typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1, R>::type; template <size_t N = 0, class T> auto operator>>(istream &in, T &&t) -> when_last<N, T, istream &> { return in >> get<N>(t); } template <size_t N = 0, class T> auto operator>>(istream &in, T &&t) -> when_init<N, T, istream &> { return operator>><N + 1>(in >> get<N>(t), forward<T>(t)); } template <size_t N = 0, class T> auto operator<<(ostream &out, const T &t) -> when_last<N, T, ostream &> { return out << get<N>(t); } template <size_t N = 0, class T> auto operator<<(ostream &out, const T &t) -> when_init<N, T, ostream &> { return operator<< <N + 1>(out << get<N>(t) << ' ', t); } #ifdef LOCAL template <class T> void trace_(const char *s, const T &v) { cerr << s << "=" << v; } template <class T, class... Ts> void trace_(const char *s, const T &v, const Ts &...vs) { for (; *s != ','; ++s) { cerr << *s; } cerr << "=" << v << ","; trace_(++s, vs...); } #define TRACE(...) \ (cerr << "\033[2m\033[33m", trace_(#__VA_ARGS__, __VA_ARGS__), \ cerr << "\033[0m\n") #else #define TRACE(...) #endif template <class Int, class Pred> Int bsearch(Int first, Int last, Pred p) noexcept { size_t len = last - first, half; Int mid; while (len > 0) { half = len >> 1; mid = first + half; if (p(mid)) { first = mid + 1; len = len - half - 1; } else { len = half; } } return first; } int main() { int64_t n, k; cin >> n >> k; vector<int64_t> p, m, z; for (int i = 0; i < n; i++) { int64_t x; cin >> x; if (x == 0) z.push_back(x); else if (x > 0) p.push_back(x); else m.push_back(-x); } int64_t num_m_ans = p.size() * m.size(); int64_t num_z_ans = z.size() * (z.size() - 1) / 2 + z.size() * (m.size() + p.size()); TRACE(num_m_ans, num_z_ans); int64_t ans; if (k <= num_m_ans) { sort(p.begin(), p.end()); sort(m.begin(), m.end()); ans = -bsearch(1L, p.back() * m.back() + 1, [&](int64_t x) { int64_t lowerx = 0; for (size_t i = 0; i < p.size(); i++) { lowerx += bsearch(0UL, m.size(), [&](size_t j) { return m[j] <= x / p[i]; }); } TRACE(x, lowerx); return lowerx < num_m_ans - k + 1; }); } else if (k <= num_m_ans + num_z_ans) { ans = 0; } else { sort(p.begin(), p.end()); sort(m.begin(), m.end()); int64_t max_abs = m.empty() ? p.back() : p.empty() ? m.back() : max(p.back(), m.back()); ans = bsearch(1L, max_abs * max_abs + 1, [&](int64_t x) { int64_t lowerx = 0; for (size_t i = 1; i < p.size(); i++) { lowerx += bsearch(0UL, i, [&](size_t j) { return p[j] <= x / p[i]; }); } for (size_t i = 1; i < m.size(); i++) { lowerx += bsearch(0UL, i, [&](size_t j) { return m[j] <= x / m[i]; }); } TRACE(x, lowerx); return lowerx < k - num_m_ans - num_z_ans; }); } cout << ans << '\n'; }
replace
130
131
130
133
0
p02774
C++
Time Limit Exceeded
// Optimize #pragma GCC optimize("Ofast") // Head File #include <bits/stdc++.h> using namespace std; #define il inline // Variable #define ll long long #define ull unsigned long long #define db double #define lb long double #define ui unsigned int // Debug #define B cerr << "Break Point" << endl; #define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl; #define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' '; #define ml(x) \ cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl; // Vector #define vc vector #define puf push_front #define pof pop_front #define pub push_back #define pob pop_back #define vbe(x) x.begin(), x.end() // Memset #define ms(x) memset(x, 0, sizeof(x)) #define MS(x) memset(x, 0x3f3f3f3f, sizeof(x)) // Pair #define fi first #define se second // File #define fin(x) freopen(x, "r", stdin) #define fou(x) freopen(x, "w", stdout) void fio() { #ifndef ONLINE_JUDGE freopen("sample.in", "r", stdin); freopen("sample.out", "w", stdout); #endif } void pti() { double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC; cerr << "Timeuse " << timeuse << "ms" << endl; } void end() { pti(); exit(0); } // Inf #define INF 0x3f3f3f3f #define LINF ((long long)(0x3f3f3f3f3f3f3f3f)) // IO #define pc(s) putchar(s) #define say(s) cout << s << endl namespace io { const int SIZ = 55; int que[SIZ], op, qr; char ch; template <class I> il void gi(I &w) { ch = getchar(), op = 1, w = 0; while (!isdigit(ch)) { if (ch == '-') op = -1; ch = getchar(); } while (isdigit(ch)) { w = w * 10 + ch - '0'; ch = getchar(); } w *= op; } template <class I> il void print(I w) { qr = 0; if (!w) putchar('0'); if (w < 0) putchar('-'), w = -w; while (w) que[++qr] = w % 10 + '0', w /= 10; while (qr) putchar(que[qr--]); } } // namespace io using io::gi; using io::print; const int N = 2e5 + 5; ll n, cnta, cntb, cntc, num, k; ll a[N], b[N], c[N]; int find(int l, int r, ll s, ll d, ll *x) { int pos = l - 1; while (l <= r) { int mid = (l + r) >> 1; if (x[mid] * s >= d) l = mid + 1, pos = mid; else r = mid - 1; } return pos; } bool check(ll v) { ll res = 0; for (int i = 1; i <= cnta; ++i) { int pos = find(i + 1, cnta, a[i], v, a); res += pos - i; } if (v <= 0) res += num * cnta + num * cntb + num * (num - 1) / 2; for (int i = 1; i <= cntb; ++i) { int pos = find(i + 1, cntb, b[i], v, b); res += pos - i; } for (int i = 1; i <= cnta; ++i) { int pos = find(1, cntc, a[i], v, c); res += pos; } res = n * (n - 1) / 2 - res; if (res < k) return true; else return false; } int main() { fio(); gi(n), gi(k); for (int i = 1; i <= n; ++i) { ll x; gi(x); if (x > 0) a[++cnta] = x; else if (x < 0) b[++cntb] = x, c[++cntc] = x; else ++num; } sort(a + 1, a + cnta + 1, [](ll x, ll y) { return x > y; }); sort(b + 1, b + cntb + 1, [](ll x, ll y) { return x < y; }); sort(c + 1, c + cntc + 1, [](ll x, ll y) { return x > y; }); ll l = -1e18 - 1, r = 1e18 + 1, ans; while (l <= r) { ll mid = (l + r) >> 1; if (check(mid)) l = mid + 1, ans = mid; else r = mid - 1; } print(ans); end(); }
// Optimize #pragma GCC optimize("Ofast") // Head File #include <bits/stdc++.h> using namespace std; #define il inline // Variable #define ll long long #define ull unsigned long long #define db double #define lb long double #define ui unsigned int // Debug #define B cerr << "Break Point" << endl; #define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl; #define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' '; #define ml(x) \ cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl; // Vector #define vc vector #define puf push_front #define pof pop_front #define pub push_back #define pob pop_back #define vbe(x) x.begin(), x.end() // Memset #define ms(x) memset(x, 0, sizeof(x)) #define MS(x) memset(x, 0x3f3f3f3f, sizeof(x)) // Pair #define fi first #define se second // File #define fin(x) freopen(x, "r", stdin) #define fou(x) freopen(x, "w", stdout) void fio() { #ifndef ONLINE_JUDGE freopen("sample.in", "r", stdin); freopen("sample.out", "w", stdout); #endif } void pti() { double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC; cerr << "Timeuse " << timeuse << "ms" << endl; } void end() { pti(); exit(0); } // Inf #define INF 0x3f3f3f3f #define LINF ((long long)(0x3f3f3f3f3f3f3f3f)) // IO #define pc(s) putchar(s) #define say(s) cout << s << endl namespace io { const int SIZ = 55; int que[SIZ], op, qr; char ch; template <class I> il void gi(I &w) { ch = getchar(), op = 1, w = 0; while (!isdigit(ch)) { if (ch == '-') op = -1; ch = getchar(); } while (isdigit(ch)) { w = w * 10 + ch - '0'; ch = getchar(); } w *= op; } template <class I> il void print(I w) { qr = 0; if (!w) putchar('0'); if (w < 0) putchar('-'), w = -w; while (w) que[++qr] = w % 10 + '0', w /= 10; while (qr) putchar(que[qr--]); } } // namespace io using io::gi; using io::print; const int N = 2e5 + 5; ll n, cnta, cntb, cntc, num, k; ll a[N], b[N], c[N]; int find(int l, int r, ll s, ll d, ll *x) { int pos = l - 1; while (l <= r) { int mid = (l + r) >> 1; if (x[mid] * s >= d) l = mid + 1, pos = mid; else r = mid - 1; } return pos; } bool check(ll v) { ll res = 0; for (int i = 1; i <= cnta; ++i) { int pos = find(i + 1, cnta, a[i], v, a); res += pos - i; } if (v <= 0) res += num * cnta + num * cntb + num * (num - 1) / 2; for (int i = 1; i <= cntb; ++i) { int pos = find(i + 1, cntb, b[i], v, b); res += pos - i; } for (int i = 1; i <= cnta; ++i) { int pos = find(1, cntc, a[i], v, c); res += pos; } res = n * (n - 1) / 2 - res; if (res < k) return true; else return false; } int main() { gi(n), gi(k); for (int i = 1; i <= n; ++i) { ll x; gi(x); if (x > 0) a[++cnta] = x; else if (x < 0) b[++cntb] = x, c[++cntc] = x; else ++num; } sort(a + 1, a + cnta + 1, [](ll x, ll y) { return x > y; }); sort(b + 1, b + cntb + 1, [](ll x, ll y) { return x < y; }); sort(c + 1, c + cntc + 1, [](ll x, ll y) { return x > y; }); ll l = -1e18 - 1, r = 1e18 + 1, ans; while (l <= r) { ll mid = (l + r) >> 1; if (check(mid)) l = mid + 1, ans = mid; else r = mid - 1; } print(ans); end(); }
delete
136
137
136
136
TLE
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const LL mod = 1e9 + 7; const int N = 100005; int n; LL m; int a[N]; vector<LL> b, c; LL zero; bool check(LL x) { LL cnt = 0; if (x < 0) { x = -x; for (auto u : b) { LL t = (x + u - 1) / u; cnt += c.end() - lower_bound(c.begin(), c.end(), t); } for (auto u : c) { LL t = (x + u - 1) / u; cnt += b.end() - lower_bound(b.begin(), b.end(), t); } } else if (x == 0) { cnt += zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2; } else { cnt += zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2; for (auto u : b) { LL t = x / u; cnt += upper_bound(b.begin(), b.end(), t) - b.begin(); if (u <= t) cnt--; } for (auto u : c) { LL t = x / u; cnt += upper_bound(c.begin(), c.end(), t) - c.begin(); if (u <= t) cnt--; } } return cnt / 2 >= m; } int main() { scanf("%d%lld", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] < 0) { b.push_back(-a[i]); } else if (a[i] > 0) { c.push_back(a[i]); } else { zero++; } } sort(b.begin(), b.end()); sort(c.begin(), c.end()); LL l = -1e18, r = 1e18; while (l < r) { LL mid = l + (r - l) / 2; if (check(mid)) { r = mid; } else { l = mid + 1; } } cout << l << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const LL mod = 1e9 + 7; const int N = 200005; int n; LL m; int a[N]; vector<LL> b, c; LL zero; bool check(LL x) { LL cnt = 0; if (x < 0) { x = -x; for (auto u : b) { LL t = (x + u - 1) / u; cnt += c.end() - lower_bound(c.begin(), c.end(), t); } for (auto u : c) { LL t = (x + u - 1) / u; cnt += b.end() - lower_bound(b.begin(), b.end(), t); } } else if (x == 0) { cnt += zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2; } else { cnt += zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2; for (auto u : b) { LL t = x / u; cnt += upper_bound(b.begin(), b.end(), t) - b.begin(); if (u <= t) cnt--; } for (auto u : c) { LL t = x / u; cnt += upper_bound(c.begin(), c.end(), t) - c.begin(); if (u <= t) cnt--; } } return cnt / 2 >= m; } int main() { scanf("%d%lld", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] < 0) { b.push_back(-a[i]); } else if (a[i] > 0) { c.push_back(a[i]); } else { zero++; } } sort(b.begin(), b.end()); sort(c.begin(), c.end()); LL l = -1e18, r = 1e18; while (l < r) { LL mid = l + (r - l) / 2; if (check(mid)) { r = mid; } else { l = mid + 1; } } cout << l << endl; return 0; }
replace
5
6
5
6
0
p02774
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (n); i++) #define revrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 61; // const int INF = INT_MAX; const ll INF = 1 << 30; // 10^9 ll MOD = 1000000007; // ll MOD = 998244353; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A, ll x = 10) { cout << fixed << setprecision(x) << A << endl; } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll inverse(ll x) { return pow_mod(x, MOD - 2); }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; // コンビネーション const int MAXcomb = 200010; ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; // facはn!,finvは1/n! // invは逆元 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAXcomb; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] % MOD * finv[n - k] % MOD; } const int MAXkai = 200010; ll kai_memo[MAXkai]; ll kai(ll N) { if (kai_memo[N] != 0) return kai_memo[N]; if (N <= 1) return 1; return kai_memo[N] = N * kai(N - 1) % MOD; } ll N, K; ll zero; vector<ll> P, M, A; bool can(ll x) { ll res = 0; if (x > 0) { // P, P rep(i, P.size()) { if (P[0] * P[i] > x) continue; ll ok = 0, ng = P.size(); while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (P[i] * P[mid] <= x) ok = mid; else ng = mid; } res += ok + 1; } // M, M rep(i, M.size()) { if (M[0] * M[i] > x) continue; ll ok = 0, ng = M.size(); while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (M[i] * M[mid] <= x) ok = mid; else ng = mid; } res += ok + 1; } rep(i, P.size()) { if (P[i] * P[i] <= x) res--; } rep(i, M.size()) { if (M[i] * M[i] <= x) res--; } res /= 2; res += P.size() * M.size(); res += zero * (P.size() + M.size()); res += zero * (zero - 1) / 2; } else if (x < 0) { // P, M rep(i, P.size()) { if (-P[i] * M.back() > x) continue; ll ok = M.size() - 1, ng = -1; while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (-P[i] * M[mid] <= x) ok = mid; else ng = mid; } res += M.size() - ok; } } else { res += P.size() * M.size(); res += zero * (P.size() + M.size()); res += zero * (zero - 1) / 2; } return res >= K; } void solve() { cin >> N >> K; rep(i, N) { ll a; cin >> a; if (a > 0) P.pb(a); else if (a < 0) M.pb(-a); else zero++; A.pb(a); } sort(A.begin(), A.end()); sort(P.begin(), P.end()); sort(M.begin(), M.end()); ll ok = INFL, ng = -INFL; while (ok - ng > 1) { ll mid = (ng + ok) / 2; if (can(mid)) ok = mid; else ng = mid; } cout << ok << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (n); i++) #define revrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 61; // const int INF = INT_MAX; const ll INF = 1 << 30; // 10^9 ll MOD = 1000000007; // ll MOD = 998244353; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A, ll x = 10) { cout << fixed << setprecision(x) << A << endl; } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll inverse(ll x) { return pow_mod(x, MOD - 2); }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; // コンビネーション const int MAXcomb = 200010; ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; // facはn!,finvは1/n! // invは逆元 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAXcomb; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] % MOD * finv[n - k] % MOD; } const int MAXkai = 200010; ll kai_memo[MAXkai]; ll kai(ll N) { if (kai_memo[N] != 0) return kai_memo[N]; if (N <= 1) return 1; return kai_memo[N] = N * kai(N - 1) % MOD; } ll N, K; ll zero; vector<ll> P, M, A; bool can(ll x) { ll res = 0; if (x > 0) { // P, P rep(i, P.size()) { if (P[0] * P[i] > x) continue; ll ok = 0, ng = P.size(); while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (P[i] * P[mid] <= x) ok = mid; else ng = mid; } res += ok + 1; } // M, M rep(i, M.size()) { if (M[0] * M[i] > x) continue; ll ok = 0, ng = M.size(); while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (M[i] * M[mid] <= x) ok = mid; else ng = mid; } res += ok + 1; } rep(i, P.size()) { if (P[i] * P[i] <= x) res--; } rep(i, M.size()) { if (M[i] * M[i] <= x) res--; } res /= 2; res += P.size() * M.size(); res += zero * (P.size() + M.size()); res += zero * (zero - 1) / 2; } else if (x < 0) { // P, M rep(i, P.size()) { if (M.size() == 0) continue; if (-P[i] * M.back() > x) continue; ll ok = M.size() - 1, ng = -1; while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (-P[i] * M[mid] <= x) ok = mid; else ng = mid; } res += M.size() - ok; } } else { res += P.size() * M.size(); res += zero * (P.size() + M.size()); res += zero * (zero - 1) / 2; } return res >= K; } void solve() { cin >> N >> K; rep(i, N) { ll a; cin >> a; if (a > 0) P.pb(a); else if (a < 0) M.pb(-a); else zero++; A.pb(a); } sort(A.begin(), A.end()); sort(P.begin(), P.end()); sort(M.begin(), M.end()); ll ok = INFL, ng = -INFL; while (ok - ng > 1) { ll mid = (ng + ok) / 2; if (can(mid)) ok = mid; else ng = mid; } cout << ok << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
insert
171
171
171
173
0
p02774
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; using ll = long long; long long find_min(function<bool(long long)> solve, long long low, long long high) { long long ng = low, ok = high; while (ok - ng > 1) { long long mid = (ok + ng) / 2; if (solve(mid)) { ok = mid; } else { ng = mid; } } return ok; } int main() { ll n, k, zero = 0; cin >> n >> k; vector<ll> A_plus, A_minus; for (int i = 0; i < n; i++) { ll a; cin >> a; if (a > 0) A_plus.push_back(a); else if (a < 0) A_minus.push_back(-a); else zero++; } sort(A_plus.begin(), A_plus.end()); sort(A_minus.begin(), A_minus.end()); ll num_minus = A_minus.size() * A_plus.size(); ll num_zero = zero * (n - zero) + zero * (zero - 1) / 2; ll ans; if (k <= num_minus) { function<ll(ll)> g = [&](ll x) { ll res = 0, j = A_minus.size(); for (ll i : A_plus) { while (j > 0 && i * A_minus[j - 1] >= -x) j--; res += A_minus.size() - j; } return res; }; function<bool(ll)> f = [&](ll x) { return g(x) >= k; }; ans = find_min(f, -(*A_minus.rbegin()) * (*A_plus.rbegin()) - 1, -1); } else if (k <= num_minus + num_zero) { ans = 0; } else { k -= num_minus + num_zero; function<ll(ll)> g = [&](ll x) { ll res = 0, j = A_minus.size() - 1; for (ll i = 0; i < A_minus.size() && i < j; i++) { ll z = x / A_minus[i]; while (j > i && A_minus[j] > z) j--; res += j - i; } j = A_plus.size() - 1; for (ll i = 0; i < A_plus.size() && i < j; i++) { ll z = x / A_plus[i]; while (j > i && A_plus[j] > z) j--; res += j - i; } return res; }; function<bool(ll)> f = [&](ll x) { return g(x) >= k; }; ans = find_min(f, 0, max((*A_plus.rbegin()) * (*A_plus.rbegin()), (*A_minus.rbegin()) * (*A_minus.rbegin())) + 1); } cout << ans << endl; return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <vector> using namespace std; using ll = long long; long long find_min(function<bool(long long)> solve, long long low, long long high) { long long ng = low, ok = high; while (ok - ng > 1) { long long mid = (ok + ng) / 2; if (solve(mid)) { ok = mid; } else { ng = mid; } } return ok; } int main() { ll n, k, zero = 0; cin >> n >> k; vector<ll> A_plus, A_minus; for (int i = 0; i < n; i++) { ll a; cin >> a; if (a > 0) A_plus.push_back(a); else if (a < 0) A_minus.push_back(-a); else zero++; } sort(A_plus.begin(), A_plus.end()); sort(A_minus.begin(), A_minus.end()); ll num_minus = A_minus.size() * A_plus.size(); ll num_zero = zero * (n - zero) + zero * (zero - 1) / 2; ll ans; if (k <= num_minus) { function<ll(ll)> g = [&](ll x) { ll res = 0, j = A_minus.size(); for (ll i : A_plus) { while (j > 0 && i * A_minus[j - 1] >= -x) j--; res += A_minus.size() - j; } return res; }; function<bool(ll)> f = [&](ll x) { return g(x) >= k; }; ans = find_min(f, -(*A_minus.rbegin()) * (*A_plus.rbegin()) - 1, -1); } else if (k <= num_minus + num_zero) { ans = 0; } else { k -= num_minus + num_zero; function<ll(ll)> g = [&](ll x) { ll res = 0, j = A_minus.size() - 1; for (ll i = 0; i < A_minus.size() && i < j; i++) { ll z = x / A_minus[i]; while (j > i && A_minus[j] > z) j--; res += j - i; } j = A_plus.size() - 1; for (ll i = 0; i < A_plus.size() && i < j; i++) { ll z = x / A_plus[i]; while (j > i && A_plus[j] > z) j--; res += j - i; } return res; }; function<bool(ll)> f = [&](ll x) { return g(x) >= k; }; ll high = 1; if (!A_plus.empty()) high = max(high, *A_plus.rbegin()); if (!A_minus.empty()) high = max(high, *A_minus.rbegin()); high = high * high + 1; ans = find_min(f, 0, high); } cout << ans << endl; return 0; }
replace
74
78
74
81
0
p02774
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define REP(i, n) for (ll i = 0; i < (n); ++i) ll binary_search(ll lo, ll hi, function<bool(ll)> is_ok, bool maximize) { while (lo < hi) { if (maximize) { const ll mid = (lo + hi + 1) / 2; if (is_ok(mid)) { lo = mid; } else { hi = mid - 1; } } else { ll mid = (lo + hi) / 2; if (mid == hi) { mid--; } if (is_ok(mid)) { hi = mid; } else { lo = mid + 1; } } } return lo; } ll ceildiv(ll x, ll y) { if (x >= 0 && y > 0) { return (x + y - 1) / y; } else if (x >= 0 && y < 0) { return -x / abs(y); } else if (x < 0 && y > 0) { return -abs(x) / y; } else { assert(x < 0 && y < 0); return (abs(x) + abs(y) - 1) / abs(y); } } ll floordiv(ll x, ll y) { if (x >= 0 && y > 0) { return x / y; } else if (x >= 0 && y < 0) { return -(x + abs(y) - 1) / abs(y); } else if (x < 0 && y > 0) { return -(abs(x) + y - 1) / y; } else { assert(x < 0 && y > 0); return abs(x) / abs(y); } } int main() { ll n, k; cin >> n >> k; vector<ll> a(n); REP(i, n) { cin >> a.at(i); } sort(a.begin(), a.end()); const ll maxval = ll(1e18) + 1; const ll minval = -maxval; ll ans = ::binary_search( minval, maxval, [&](ll x) { ll count = 0; REP(i, n) { ll tmp = 0; if (a.at(i) == 0) { if (x > 0) { tmp = n - 1 - i; } } else if (a.at(i) > 0) { const ll y = ceildiv(x, a.at(i)); const auto it_start = a.begin() + i + 1; tmp = lower_bound(it_start, a.end(), y) - it_start; } else { assert(a.at(i) < 0); const ll y = floordiv(x, a.at(i)); const auto it_start = a.begin() + i + 1; tmp = a.end() - upper_bound(it_start, a.end(), y); } count += tmp; } return count < k; }, true); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define REP(i, n) for (ll i = 0; i < (n); ++i) ll binary_search(ll lo, ll hi, function<bool(ll)> is_ok, bool maximize) { while (lo < hi) { if (maximize) { const ll mid = (lo + hi + 1) / 2; if (is_ok(mid)) { lo = mid; } else { hi = mid - 1; } } else { ll mid = (lo + hi) / 2; if (mid == hi) { mid--; } if (is_ok(mid)) { hi = mid; } else { lo = mid + 1; } } } return lo; } ll ceildiv(ll x, ll y) { if (x >= 0 && y > 0) { return (x + y - 1) / y; } else if (x >= 0 && y < 0) { return -x / abs(y); } else if (x < 0 && y > 0) { return -abs(x) / y; } else { assert(x < 0 && y < 0); return (abs(x) + abs(y) - 1) / abs(y); } } ll floordiv(ll x, ll y) { if (x >= 0 && y > 0) { return x / y; } else if (x >= 0 && y < 0) { return -(x + abs(y) - 1) / abs(y); } else if (x < 0 && y > 0) { return -(abs(x) + y - 1) / y; } else { assert(x < 0 && y < 0); return abs(x) / abs(y); } } int main() { ll n, k; cin >> n >> k; vector<ll> a(n); REP(i, n) { cin >> a.at(i); } sort(a.begin(), a.end()); const ll maxval = ll(1e18) + 1; const ll minval = -maxval; ll ans = ::binary_search( minval, maxval, [&](ll x) { ll count = 0; REP(i, n) { ll tmp = 0; if (a.at(i) == 0) { if (x > 0) { tmp = n - 1 - i; } } else if (a.at(i) > 0) { const ll y = ceildiv(x, a.at(i)); const auto it_start = a.begin() + i + 1; tmp = lower_bound(it_start, a.end(), y) - it_start; } else { assert(a.at(i) < 0); const ll y = floordiv(x, a.at(i)); const auto it_start = a.begin() + i + 1; tmp = a.end() - upper_bound(it_start, a.end(), y); } count += tmp; } return count < k; }, true); cout << ans << endl; return 0; }
replace
51
52
51
52
-6
97eaee68-a4be-479f-841f-fd73075030ce.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02774/C++/s904219173.cpp:53: ll floordiv(ll, ll): Assertion `x < 0 && y > 0' failed.
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int dp[N][2]; int main() { string s; cin >> s; dp[0][0] = s[0] - '0'; dp[0][1] = 1 + 10 - s[0] + '0'; for (int i = 1; i < s.size(); i++) { int x = s[i] - '0'; dp[i][0] = min(dp[i - 1][0] + x, dp[i - 1][1] + x); dp[i][1] = min(dp[i - 1][0] + 10 - x + 1, dp[i - 1][1] - 1 + 10 - x); } cout << min(dp[s.size() - 1][1], dp[s.size() - 1][0]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int dp[N][2]; int main() { string s; cin >> s; dp[0][0] = s[0] - '0'; dp[0][1] = 1 + 10 - s[0] + '0'; for (int i = 1; i < s.size(); i++) { int x = s[i] - '0'; dp[i][0] = min(dp[i - 1][0] + x, dp[i - 1][1] + x); dp[i][1] = min(dp[i - 1][0] + 10 - x + 1, dp[i - 1][1] - 1 + 10 - x); } cout << min(dp[s.size() - 1][1], dp[s.size() - 1][0]) << endl; return 0; }
replace
2
3
2
3
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { string s; cin >> s; int N = s.size(); ll dp[100005][2]; dp[0][1] = 1; REP(i, N) { int num = s[i] - '0'; int num2 = 10 - num; dp[i + 1][0] = min(dp[i][0] + num, dp[i][1] + num2); dp[i + 1][1] = min(dp[i][0] + num + 1, dp[i][1] + num2 - 1); } cout << dp[N][0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) typedef long long ll; int main() { string s; cin >> s; int N = s.size(); ll dp[1000005][2]; dp[0][1] = 1; REP(i, N) { int num = s[i] - '0'; int num2 = 10 - num; dp[i + 1][0] = min(dp[i][0] + num, dp[i][1] + num2); dp[i + 1][1] = min(dp[i][0] + num + 1, dp[i][1] + num2 - 1); } cout << dp[N][0] << endl; return 0; }
replace
9
10
9
10
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 // #define MOD 998244353 #define INF 1145141919810893364 // #define INF 810114514 // #define PI 3.141592653589 typedef pair<int, int> PP; typedef long long ll; #define int ll #define setdouble setprecision #define REP(i, n) for (int i = 0; i < (n); ++i) #define OREP(i, n) for (int i = 1; i <= (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define GOODBYE \ do { \ cout << "0" << endl; \ return 0; \ } while (false) #define MM << " " << #define Endl endl signed main(void) { string N; cin >> N; const int MEM = 100; int dp[MEM][2]; REP(i, MEM) { REP(j, 2) { dp[i][j] = INF; } } dp[0][0] = N[0] - '0'; dp[0][1] = 11 - (N[0] - '0'); REP(i, N.size() - 1) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + N[i + 1] - '0'); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + N[i + 1] - '0'); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 11 - (N[i + 1] - '0')); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 9 - (N[i + 1] - '0')); } cout << min(dp[N.size() - 1][0], dp[N.size() - 1][1]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 // #define MOD 998244353 #define INF 1145141919810893364 // #define INF 810114514 // #define PI 3.141592653589 typedef pair<int, int> PP; typedef long long ll; #define int ll #define setdouble setprecision #define REP(i, n) for (int i = 0; i < (n); ++i) #define OREP(i, n) for (int i = 1; i <= (n); ++i) #define RREP(i, n) for (int i = (n)-1; i >= 0; --i) #define GOODBYE \ do { \ cout << "0" << endl; \ return 0; \ } while (false) #define MM << " " << #define Endl endl signed main(void) { string N; cin >> N; const int MEM = 1114514; int dp[MEM][2]; REP(i, MEM) { REP(j, 2) { dp[i][j] = INF; } } dp[0][0] = N[0] - '0'; dp[0][1] = 11 - (N[0] - '0'); REP(i, N.size() - 1) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + N[i + 1] - '0'); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + N[i + 1] - '0'); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 11 - (N[i + 1] - '0')); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 9 - (N[i + 1] - '0')); } cout << min(dp[N.size() - 1][0], dp[N.size() - 1][1]) << endl; return 0; }
replace
25
26
25
26
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define rrep(i, x) for (long long i = x - 1; i >= 0; i--) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); ll dp[1000001][2]; int main() { string N; cin >> N; ll res = 0; dp[0][1] = INF; int n = N.size(); rep(i, n) { ll c = N[n - 1 - i] - '0'; if (c != 9) dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c); else dp[i + 1][0] = dp[i][0] + c; dp[i + 1][1] = min(dp[i][0] + 10 - c + 1, dp[i][1] + 9 - c); // cout << i+1 << ": " << dp[i+1][0] << " " << dp[i+1][1] << endl; } cout << min(dp[n][0], dp[n][1]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> P1; typedef pair<P, P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i, x) for (long long i = 0; i < x; i++) #define rrep(i, x) for (long long i = x - 1; i >= 0; i--) #define repn(i, x) for (long long i = 1; i <= x; i++) #define SORT(x) sort(x.begin(), x.end()) #define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end()) #define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin()) #define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin()) vector<pair<string, P>> vec; // vector<vector<int>> data(3, vector<int>(4)); ll dp[1000005][2]; int main() { string N; cin >> N; ll res = 0; dp[0][1] = INF; int n = N.size(); rep(i, n) { ll c = N[n - 1 - i] - '0'; if (c != 9) dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c); else dp[i + 1][0] = dp[i][0] + c; dp[i + 1][1] = min(dp[i][0] + 10 - c + 1, dp[i][1] + 9 - c); // cout << i+1 << ": " << dp[i+1][0] << " " << dp[i+1][1] << endl; } cout << min(dp[n][0], dp[n][1]) << endl; return 0; }
replace
24
25
24
25
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #include <type_traits> using namespace std; using ll = int64_t; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define ddrep(i, n) for (int i = n; i > 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define ssrep(i, s, t) for (int i = s; i <= t; ++i) #define rng(a) a.begin(), a.end() #define pb push_back #define eb emplace_back #define fi first #define se second #define chmax(x, y) (x = max(x, y)) #define chmin(x, y) (x = min(x, y)) using pi = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using ld = long double; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; rep(i, (int)v.size()) { if (i) os << ","; os << v[i]; } os << "}"; return os; } template <typename T, size_t S> void printArray(const T (&array)[S]) { for (auto val : array) std::cout << val << ", "; std::cout << "\n"; } const int mod = 1e9 + 7; const int inf = 1e9 + 5; const int MAX = 1e5 + 5; int dp[MAX][2]; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::setprecision(10); string s; std::cin >> s; reverse(rng(s)); s += '0'; int n = s.size(); rep(i, MAX) rep(j, 2) dp[i][j] = inf; dp[0][0] = 0; rep(i, n + 1) rep(j, 2) { int x = s[i] - '0', ni = i + 1; x += j; if (x < 10) chmin(dp[ni][0], dp[i][j] + x); if (x > 0) chmin(dp[ni][1], dp[i][j] + (10 - x)); } std::cout << dp[n][0] << "\n"; }
#include <bits/stdc++.h> #include <type_traits> using namespace std; using ll = int64_t; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define ddrep(i, n) for (int i = n; i > 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define ssrep(i, s, t) for (int i = s; i <= t; ++i) #define rng(a) a.begin(), a.end() #define pb push_back #define eb emplace_back #define fi first #define se second #define chmax(x, y) (x = max(x, y)) #define chmin(x, y) (x = min(x, y)) using pi = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using ld = long double; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; rep(i, (int)v.size()) { if (i) os << ","; os << v[i]; } os << "}"; return os; } template <typename T, size_t S> void printArray(const T (&array)[S]) { for (auto val : array) std::cout << val << ", "; std::cout << "\n"; } const int mod = 1e9 + 7; const int inf = 1e9 + 5; const int MAX = 1e6 + 5; int dp[MAX][2]; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::setprecision(10); string s; std::cin >> s; reverse(rng(s)); s += '0'; int n = s.size(); rep(i, MAX) rep(j, 2) dp[i][j] = inf; dp[0][0] = 0; rep(i, n + 1) rep(j, 2) { int x = s[i] - '0', ni = i + 1; x += j; if (x < 10) chmin(dp[ni][0], dp[i][j] + x); if (x > 0) chmin(dp[ni][1], dp[i][j] + (10 - x)); } std::cout << dp[n][0] << "\n"; }
replace
53
54
53
54
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; typedef long long ll; char s[N]; ll dp[N][2]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); dp[0][1] = 1000; for (int i = 1; i <= n; i++) { int x = s[i] - '0'; dp[i][0] = dp[i - 1][0] + min(x, 10 - x + 1); dp[i][0] = min(dp[i][0], dp[i - 1][1] + 10 - x); dp[i][1] = min(dp[i - 1][0] + 10 - x, dp[i - 1][1] + 10 - x - 1); } printf("%lld\n", min(dp[n][0], dp[n][1] + 1)); }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; typedef long long ll; char s[N]; ll dp[N][2]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); dp[0][1] = 1000; for (int i = 1; i <= n; i++) { int x = s[i] - '0'; dp[i][0] = dp[i - 1][0] + min(x, 10 - x + 1); dp[i][0] = min(dp[i][0], dp[i - 1][1] + 10 - x); dp[i][1] = min(dp[i - 1][0] + 10 - x, dp[i - 1][1] + 10 - x - 1); } printf("%lld\n", min(dp[n][0], dp[n][1] + 1)); }
replace
2
3
2
3
0
p02775
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 105; int num[maxn]; int main() { string ss; cin >> ss; reverse(ss.begin(), ss.end()); for (int i = 1; i <= ss.length(); i++) num[i] = (ss[i - 1] - '0'); int ans = 0; for (int i = 1; i <= ss.length() + 1; i++) { if (num[i] <= 4) ans += num[i]; else if (num[i] == 5) { if (num[i + 1] >= 5) { ans += (10 - num[i]); num[i + 1]++; } else ans += num[i]; } else { ans += (10 - num[i]); num[i + 1]++; } } printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1e6 + 5; int num[maxn]; int main() { string ss; cin >> ss; reverse(ss.begin(), ss.end()); for (int i = 1; i <= ss.length(); i++) num[i] = (ss[i - 1] - '0'); int ans = 0; for (int i = 1; i <= ss.length() + 1; i++) { if (num[i] <= 4) ans += num[i]; else if (num[i] == 5) { if (num[i + 1] >= 5) { ans += (10 - num[i]); num[i + 1]++; } else ans += num[i]; } else { ans += (10 - num[i]); num[i + 1]++; } } printf("%d\n", ans); return 0; }
replace
5
6
5
6
0
p02775
C++
Runtime Error
#define _LIBCPP_DEBUG 0 #include <bits/stdc++.h> using namespace std; typedef long long int ll; ll MOD = 1e9 + 7; int main(void) { string N; cin >> N; vector<int> vec(N.length()); for (ll i = 0; i < N.length(); i++) { vec[i + 1] = N[i] - '0'; } ll ans = 0; for (ll i = N.length(); i >= 0; --i) { if (vec[i] >= 10) { // 繰り上げ処理 vec[i - 1]++; vec[i] %= 10; } if (vec[i] < 5) { ans += vec[i]; } else if (vec[i] == 5) { if (vec[i - 1] >= 5) { ans += 10 - vec[i]; vec[i - 1]++; } else { ans += vec[i]++; } } else { ans += 10 - vec[i]; vec[i - 1]++; } } cout << ans << endl; }
#define _LIBCPP_DEBUG 0 #include <bits/stdc++.h> using namespace std; typedef long long int ll; ll MOD = 1e9 + 7; int main(void) { string N; cin >> N; vector<int> vec(N.length() + 1); for (int i = 0; i < N.length(); i++) { vec[i + 1] = N[i] - '0'; } ll ans = 0; for (ll i = N.length(); i >= 0; --i) { if (vec[i] >= 10) { // 繰り上げ処理 vec[i - 1]++; vec[i] %= 10; } if (vec[i] < 5) { ans += vec[i]; } else if (vec[i] == 5) { if (vec[i - 1] >= 5) { ans += 10 - vec[i]; vec[i - 1]++; } else { ans += vec[i]++; } } else { ans += 10 - vec[i]; vec[i - 1]++; } } cout << ans << endl; }
replace
10
12
10
12
0
p02775
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (n); i++) #define revrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 60; // const int INF = INT_MAX; const ll INF = 1 << 30; // 10^9 ll MOD = 1000000007; // ll MOD = 998244353; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A, ll x = 10) { cout << fixed << setprecision(x) << A << endl; } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll inverse(ll x) { return pow_mod(x, MOD - 2); }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; // コンビネーション const int MAXcomb = 200010; ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; // facはn!,finvは1/n! // invは逆元 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAXcomb; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] % MOD * finv[n - k] % MOD; } const int MAXkai = 200010; ll kai_memo[MAXkai]; ll kai(ll N) { if (kai_memo[N] != 0) return kai_memo[N]; if (N <= 1) return 1; return kai_memo[N] = N * kai(N - 1) % MOD; } ll N; string S; ll dp[100010][2]; void solve() { cin >> S; reverse(S.begin(), S.end()); S += '0'; N = S.size(); rep(i, N + 1) { rep(j, 2) { dp[i][j] = INFL; } } dp[0][0] = 0; rep(i, N) { rep(j, 2) { rep(k, 10) { ll sum = j + k + S[i] - '0'; ll nj = sum / 10; sum %= 10; dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + sum + k); } } } cout << dp[N][0] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <algorithm> #include <bitset> #include <climits> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef pair<ll, ll> P; #define rep(i, n) for (ll i = 0; i < (n); i++) #define revrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define pb push_back #define f first #define s second #define chmin(x, y) x = min(x, y); #define chmax(x, y) x = max(x, y); // const ll INFL = LLONG_MAX;//10^18 = 2^60 const ll INFL = 1LL << 60; // const int INF = INT_MAX; const ll INF = 1 << 30; // 10^9 ll MOD = 1000000007; // ll MOD = 998244353; vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0}; vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0}; void pres(double A, ll x = 10) { cout << fixed << setprecision(x) << A << endl; } void BinarySay(ll x, ll y = 60) { rep(i, y) cout << (x >> (y - 1 - i) & 1); cout << endl; } ll cnt_bit(ll x) { return __builtin_popcountll(x); } ll pow_long(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) res *= x; x *= x; k /= 2; } return res; } ll pow_mod(ll x, ll k) { ll res = 1; while (k > 0) { if (k % 2) { res *= x; res %= MOD; } x *= x; x %= MOD; k /= 2; } return res; } ll inverse(ll x) { return pow_mod(x, MOD - 2); }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll x, ll y) { ll res = x / gcd(x, y); res *= y; return res; }; // コンビネーション const int MAXcomb = 200010; ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb]; // facはn!,finvは1/n! // invは逆元 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAXcomb; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll comb(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] % MOD * finv[n - k] % MOD; } const int MAXkai = 200010; ll kai_memo[MAXkai]; ll kai(ll N) { if (kai_memo[N] != 0) return kai_memo[N]; if (N <= 1) return 1; return kai_memo[N] = N * kai(N - 1) % MOD; } ll N; string S; ll dp[1000010][2]; void solve() { cin >> S; reverse(S.begin(), S.end()); S += '0'; N = S.size(); rep(i, N + 1) { rep(j, 2) { dp[i][j] = INFL; } } dp[0][0] = 0; rep(i, N) { rep(j, 2) { rep(k, 10) { ll sum = j + k + S[i] - '0'; ll nj = sum / 10; sum %= 10; dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + sum + k); } } } cout << dp[N][0] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }
replace
122
123
122
123
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define LL long long using namespace std; const int N = 2e5 + 10; int d[N][2]; int main() { string s; cin >> s; d[0][0] = s[0] - '0'; d[0][1] = 11 - (s[0] - '0'); for (int i = 1; i < s.size(); i++) { d[i][0] = min(d[i - 1][0], d[i - 1][1]) + s[i] - '0'; d[i][1] = min(d[i - 1][0] + 1, d[i - 1][1] - 1) + 10 - (s[i] - '0'); } cout << min(d[s.size() - 1][0], d[s.size() - 1][1]) << endl; }
#include <bits/stdc++.h> #define LL long long using namespace std; const int N = 2e6 + 10; int d[N][2]; int main() { string s; cin >> s; d[0][0] = s[0] - '0'; d[0][1] = 11 - (s[0] - '0'); for (int i = 1; i < s.size(); i++) { d[i][0] = min(d[i - 1][0], d[i - 1][1]) + s[i] - '0'; d[i][1] = min(d[i - 1][0] + 1, d[i - 1][1] - 1) + 10 - (s[i] - '0'); } cout << min(d[s.size() - 1][0], d[s.size() - 1][1]) << endl; }
replace
3
4
3
4
0
p02775
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return 1; } return 0; } const int MAX_N = 100100; int main() { string s; cin >> s; int n = s.length(); long long dp[MAX_N][2]; for (int i = 0; i < MAX_N; i++) dp[i][0] = dp[i][1] = 0; // dp[i][0] : ぴったり払う // dp[i][1] : 1多く払う dp[1][0] = s[0] - '0'; dp[1][1] = 10 - (s[0] - '0'); for (int i = 1; i < n; i++) { const int c = s[i] - '0'; dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c + 1); dp[i + 1][1] = min(dp[i][0] + 10 - c, dp[i][1] + 10 - (c + 1)); } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; template <class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return 1; } return 0; } const int MAX_N = 1001000; int main() { string s; cin >> s; int n = s.length(); long long dp[MAX_N][2]; for (int i = 0; i < MAX_N; i++) dp[i][0] = dp[i][1] = 0; // dp[i][0] : ぴったり払う // dp[i][1] : 1多く払う dp[1][0] = s[0] - '0'; dp[1][1] = 10 - (s[0] - '0'); for (int i = 1; i < n; i++) { const int c = s[i] - '0'; dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c + 1); dp[i + 1][1] = min(dp[i][0] + 10 - c, dp[i][1] + 10 - (c + 1)); } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
replace
17
18
17
18
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[105][2]; const ll INF = 1e5; int main() { // 一桁目から桁dp、dp[i][j]:i桁目まで決めた時の最小値、繰り上がりが起きているならj=1 string S; cin >> S; reverse(S.begin(), S.end()); S += '0'; int N = sz(S); rep(i, N + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, N) rep(j, 2) { rep(a, 10) { // aはその桁で払うお金、bはお釣り int cur = S[i] - '0'; int ni = i + 1, nj = 0; cur += j; // 繰り上がり int b = a - cur; if (b < 0) { nj = 1; b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } cout << dp[N][0] << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[1000005][2]; const ll INF = 1e9; int main() { // 一桁目から桁dp、dp[i][j]:i桁目まで決めた時の最小値、繰り上がりが起きているならj=1 string S; cin >> S; reverse(S.begin(), S.end()); S += '0'; int N = sz(S); rep(i, N + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, N) rep(j, 2) { rep(a, 10) { // aはその桁で払うお金、bはお釣り int cur = S[i] - '0'; int ni = i + 1, nj = 0; cur += j; // 繰り上がり int b = a - cur; if (b < 0) { nj = 1; b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } cout << dp[N][0] << '\n'; return 0; }
replace
14
16
14
16
0
p02775
C++
Runtime Error
#include "bits/stdc++.h" #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; class DebugStream { } LOG; template <typename T> DebugStream &operator<<(DebugStream &s, const T &) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29; int n, eq[maxN], bg[maxN]; string st; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> st; n = st.size(); reverse(st.begin(), st.end()); bg[n] = 1; for (int i = n - 1; i >= 0; i--) { int elm = st[i] - '0'; eq[i] = min(eq[i + 1] + elm, bg[i + 1] + 10 - elm); if (elm == 9) bg[i] = bg[i + 1]; else bg[i] = min(eq[i + 1] + elm + 1, bg[i + 1] + 10 - (elm + 1)); } cout << eq[0] << '\n'; }
#include "bits/stdc++.h" #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; class DebugStream { } LOG; template <typename T> DebugStream &operator<<(DebugStream &s, const T &) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e6 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29; int n, eq[maxN], bg[maxN]; string st; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> st; n = st.size(); reverse(st.begin(), st.end()); bg[n] = 1; for (int i = n - 1; i >= 0; i--) { int elm = st[i] - '0'; eq[i] = min(eq[i + 1] + elm, bg[i + 1] + 10 - elm); if (elm == 9) bg[i] = bg[i + 1]; else bg[i] = min(eq[i + 1] + elm + 1, bg[i + 1] + 10 - (elm + 1)); } cout << eq[0] << '\n'; }
replace
24
25
24
25
0
p02775
C++
Runtime Error
#include "bits/stdc++.h" #include <random> using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) 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; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18; string s; lint arr[1000000]; int main() { cin >> s; IREP(i, SZ(s)) { arr[SZ(s) - i - 1] = (s[i] - '0'); } lint cnt = 0; REP(i, SZ(s) + 1) { if (arr[i] < 5 || (arr[i] == 5 && arr[i + 1] < 5)) { cnt += arr[i]; } else { cnt += (10 - arr[i]); arr[i + 1]++; } } cout << cnt << endl; }
#include "bits/stdc++.h" #include <random> using namespace std; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) 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; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } const lint MOD = 1e9 + 7, INF = 1e18; string s; lint arr[1000001]; int main() { cin >> s; IREP(i, SZ(s)) { arr[SZ(s) - i - 1] = (s[i] - '0'); } lint cnt = 0; REP(i, SZ(s) + 1) { if (arr[i] < 5 || (arr[i] == 5 && arr[i + 1] < 5)) { cnt += arr[i]; } else { cnt += (10 - arr[i]); arr[i + 1]++; } } cout << cnt << endl; }
replace
40
41
40
41
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string nd; int dp[110][2]; int main() { cin >> nd; string n = "0" + nd; int nlen = n.length(); for (int i = 0; i <= nlen + 2; ++i) { for (int j = 0; j < 2; ++j) { dp[i][j] = 0; } } int now = 0; for (int i = nlen - 1; i >= 0; --i) { int check = n.at(i) - '0'; if (now == 0) { dp[now + 1][0] = dp[now][0] + check; dp[now + 1][1] = dp[now][1] + (10 - check); } else { dp[now + 1][0] = min(dp[now][0] + check, dp[now][1] + (check + 1)); dp[now + 1][1] = min(dp[now][0] + (10 - check), dp[now][1] + (10 - (check + 1))); } now++; } cout << min(dp[nlen][0], dp[nlen][1]) << endl; /*for(int i = 0; i <= nlen; ++i) { for(int j = 0; j < 2; ++j) { cout << dp[i][j] << " "; } cout << endl; }*/ }
#include <bits/stdc++.h> using namespace std; string nd; int dp[10000100][2]; int main() { cin >> nd; string n = "0" + nd; int nlen = n.length(); for (int i = 0; i <= nlen + 2; ++i) { for (int j = 0; j < 2; ++j) { dp[i][j] = 0; } } int now = 0; for (int i = nlen - 1; i >= 0; --i) { int check = n.at(i) - '0'; if (now == 0) { dp[now + 1][0] = dp[now][0] + check; dp[now + 1][1] = dp[now][1] + (10 - check); } else { dp[now + 1][0] = min(dp[now][0] + check, dp[now][1] + (check + 1)); dp[now + 1][1] = min(dp[now][0] + (10 - check), dp[now][1] + (10 - (check + 1))); } now++; } cout << min(dp[nlen][0], dp[nlen][1]) << endl; /*for(int i = 0; i <= nlen; ++i) { for(int j = 0; j < 2; ++j) { cout << dp[i][j] << " "; } cout << endl; }*/ }
replace
3
4
3
4
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[200]; string s; int main(void) { cin >> s; for (ll i = 0; i < s.size(); i++) { a[i + 1] = s[i] - '0'; } ll cnt = 0, f = 0; a[0] = 0; for (ll i = s.size(); i > 0; i--) { a[i] = a[i] + f; cnt += min(a[i], 10 - a[i]); if (a[i] > 5 || a[i] == 5 && a[i - 1] >= 5) { f = 1; } else { f = 0; } } printf("%lld", cnt + f); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[1000005]; string s; int main(void) { cin >> s; for (ll i = 0; i < s.size(); i++) { a[i + 1] = s[i] - '0'; } ll cnt = 0, f = 0; a[0] = 0; for (ll i = s.size(); i > 0; i--) { a[i] = a[i] + f; cnt += min(a[i], 10 - a[i]); if (a[i] > 5 || a[i] == 5 && a[i - 1] >= 5) { f = 1; } else { f = 0; } } printf("%lld", cnt + f); }
replace
3
4
3
4
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; string s; int n; ll dp[1000001][2]; int main() { cin >> s; n = s.size(); reverse(s.begin(), s.end()); memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; rep(i, n) rep(j, 2) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + (s[i] - '0') + j); dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 10 - (s[i] - '0') - j); } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define pb emplace_back typedef long long ll; typedef pair<int, int> pint; string s; int n; ll dp[1000005][2]; int main() { cin >> s; n = s.size(); reverse(s.begin(), s.end()); memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; rep(i, n) rep(j, 2) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + (s[i] - '0') + j); dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 10 - (s[i] - '0') - j); } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
replace
10
11
10
11
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T = int64> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <typename F> struct FixPoint : F { FixPoint(F &&f) : F(forward<F>(f)) {} template <typename... Args> decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, forward<Args>(args)...); } }; template <typename F> inline decltype(auto) MFP(F &&f) { return FixPoint<F>{forward<F>(f)}; } int main() { string S; cin >> S; S = string("000") + S; auto dp = make_v<int>(S.size(), 2); fill_v(dp, -1); auto rec = MFP([&](auto rec, int idx, int put) -> int { if (idx == -1) return put ? inf : 0; if (~dp[idx][put]) return dp[idx][put]; int ret = inf; for (int i = 0; i <= 9; i++) { int d = i - (S[idx] - '0') - put; if (d < 0) { chmin(ret, rec(idx - 1, true) + i + (i + 10 - put - (S[idx] - '0'))); } else { chmin(ret, rec(idx - 1, false) + i + d); } } return dp[idx][put] = ret; }); cout << rec(S.size() - 1, 0) << endl; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template <typename T = int64> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template <typename F> struct FixPoint : F { FixPoint(F &&f) : F(forward<F>(f)) {} template <typename... Args> decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, forward<Args>(args)...); } }; template <typename F> inline decltype(auto) MFP(F &&f) { return FixPoint<F>{forward<F>(f)}; } int main() { string S; cin >> S; S = string("000") + S; auto dp = make_v<int>(S.size(), 2); fill_v(dp, -1); auto rec = MFP([&](auto rec, int idx, int put) -> int { if (idx == -1) return put ? inf : 0; if (~dp[idx][put]) return dp[idx][put]; int ret = inf; for (int i = 0; i <= 9; i++) { int d = i - (S[idx] - '0') - put; if (d < 0) { chmin(ret, rec(idx - 1, true) + i + (i + 10 - put - (S[idx] - '0'))); } else { chmin(ret, rec(idx - 1, false) + i + d); } } return dp[idx][put] = ret; }); // えっなにこれは...... for (int i = 0; i < S.size(); i++) rec(i, 0); for (int i = 0; i < S.size(); i++) rec(i, 1); cout << rec(S.size() - 1, 0) << endl; }
insert
105
105
105
110
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string N; int counter = 0; int count, counter1; cin >> N; int a[N.size()]; for (int i = 0; i < N.size(); i++) { a[i] = N.at(i) - '0'; } int dp[100010][2]{}; dp[0][0] = a[0]; dp[0][1] = 11 - a[0]; for (int i = 0; i < N.size() - 1; i++) { dp[i + 1][0] = min(dp[i][0] + a[i + 1], dp[i][1] + a[i + 1]); dp[i + 1][1] = min(dp[i][0] + 11 - a[i + 1], dp[i][1] + 9 - a[i + 1]); } cout << min(dp[N.size() - 1][1], dp[N.size() - 1][0]) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string N; int counter = 0; int count, counter1; cin >> N; int a[N.size()]; for (int i = 0; i < N.size(); i++) { a[i] = N.at(i) - '0'; } int dp[1000010][2]{}; dp[0][0] = a[0]; dp[0][1] = 11 - a[0]; for (int i = 0; i < N.size() - 1; i++) { dp[i + 1][0] = min(dp[i][0] + a[i + 1], dp[i][1] + a[i + 1]); dp[i + 1][1] = min(dp[i][0] + 11 - a[i + 1], dp[i][1] + 9 - a[i + 1]); } cout << min(dp[N.size() - 1][1], dp[N.size() - 1][0]) << endl; }
replace
12
13
12
13
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; int ans = 0; int temp = 0; for (int i = S.length() - 1; i >= 0; i--) { if ((S.at(i) - '0') + temp > 5) { ans += 10 - (S.at(i) - '0' + temp); temp = 1; } else if ((S.at(i) - '0') + temp == 5) { if (S.at(i - 1) - '0' > 4 && i > 0) { ans += 10 - (S.at(i) - '0' + temp); temp = 1; } else { ans += (S.at(i) - '0') + temp; temp = 0; } } else { ans += (S.at(i) - '0') + temp; temp = 0; } // cout<<i<<" "<<ans<<endl; } cout << ans + temp << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; int ans = 0; int temp = 0; for (int i = S.length() - 1; i >= 0; i--) { if ((S.at(i) - '0') + temp > 5) { ans += 10 - (S.at(i) - '0' + temp); temp = 1; } else if ((S.at(i) - '0') + temp == 5 && i > 0) { if (S.at(i - 1) - '0' > 4) { ans += 10 - (S.at(i) - '0' + temp); temp = 1; } else { ans += (S.at(i) - '0') + temp; temp = 0; } } else { ans += (S.at(i) - '0') + temp; temp = 0; } // cout<<i<<" "<<ans<<endl; } cout << ans + temp << endl; return 0; }
replace
11
13
11
13
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define tihs \ if (fopen("inp.txt", "r")) \ freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 100010 string s; ll dp[M][2]; ll f(ll pos, ll car) { if (pos < 0) return car; if (dp[pos][car] != -1) return dp[pos][car]; ll num = s[pos] - '0'; ll val = num + car, ans; if (val >= 10) { ans = f(pos - 1, 1) + val % 10; } else { ans = f(pos - 1, 0) + val; ans = min(ans, f(pos - 1, 1) + (10 - val)); } return dp[pos][car] = ans; } int main() { IOS; tihs; memset(dp, -1, sizeof(dp)); cin >> s; ll n = sz(s) - 1; cout << f(n, 0); return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define tihs \ if (fopen("inp.txt", "r")) \ freopen("inp.txt", "r", stdin), freopen("out.txt", "w", stdout); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 1000010 string s; ll dp[M][2]; ll f(ll pos, ll car) { if (pos < 0) return car; if (dp[pos][car] != -1) return dp[pos][car]; ll num = s[pos] - '0'; ll val = num + car, ans; if (val >= 10) { ans = f(pos - 1, 1) + val % 10; } else { ans = f(pos - 1, 0) + val; ans = min(ans, f(pos - 1, 1) + (10 - val)); } return dp[pos][car] = ans; } int main() { IOS; tihs; memset(dp, -1, sizeof(dp)); cin >> s; ll n = sz(s) - 1; cout << f(n, 0); return 0; }
replace
54
55
54
55
0
p02775
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; using point = struct { ll x; ll y; }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> gr; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { x = root_find(x); y = root_find(y); if (x == y) return; if (set_size[x] < set_size[y]) { parents[y] = x; set_size[x] += set_size[y]; } else { parents[x] = y; set_size[y] += set_size[x]; } } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; /* class LCA{ public: ll N, logN; vl depth, len; gr tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l){ depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()){ ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build(){ rep(k, logN - 1) rep(n, N){ // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v){ if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN){ if (parents[k][u] != parents[k][v]){ u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v){ ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; */ struct BIT { ll n; vl tree_dat; BIT(ll n) : tree_dat(n + 1, 0), n(n){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll dat) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + dat; } ll query(ll i) { /* e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, tree_dat[j]); } return ret; } ll lower_bound(ll key) { if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && tree_dat[left + i - 1] < key) { key -= tree_dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. 10101 -> 10110 itr2. 10110 -> 11000 itr3. 11000 -> 100000 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { tree_dat[j] = update_func(val, tree_dat[j]); } } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } ll combination_memo(ll n, ll r, ll pre, ll mod) { // pre = nCr-1 // return nCr if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = ll(n - r + 1) % mod; ll denomenator = modinv(r, mod) % mod; ll val = (numerator * denomenator) % mod; val *= pre; return val % mod; } ll combination_no_mod(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } bool is_cross(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3, ll x4, ll y4) { ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) { ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } ll next_combination(ll sub) { /* ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } vl z_algorithm(str s) { ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(ll xl1, ll yl1, ll xl2, ll yl2, ll xp, ll yp) { ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BIT bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } /* const ll N_VERTEX = 310; ll a, b, t; ll dist[N_VERTEX][N_VERTEX]; void warshall_floyd(ll n){ // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } ll dist[1005], vertex_pre[1005]; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool bellman_ford(ll start, ll n, ll goal){ // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[e.to] > dist[v] + e.cost){ dist[e.to] = dist[v] + e.cost; if (i == n - 1 && (e.to == goal || goal == -1)) return false; } } return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) ll depth[MAX_N]; gr tree[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) gr tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = G[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = G[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } int main(void){ in2(h, w); rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; ll a, b, n; str s; ll dp[MAX_N * 2][2]; void solve() { reverse(all(s)); s += '0'; n = s.length(); rep(i, n + 1) dp[i][0] = dp[i][1] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { ll x = s[i] - '0'; x += j; if (x < 10) chmin(dp[i + 1][0], dp[i][j] + x); if (x > 0) chmin(dp[i + 1][1], dp[i][j] + 10 - x); } prl(dp[n][0]); // check negative MOD // check index flow // check overwrite of the input variables } int main(void) { in1(s); // assert(n <= 400); solve(); return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef long double ld; using edge = struct { ll to; ll cost; }; using point = struct { ll x; ll y; }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::pair<ll, pl> pl3; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<pl> vpl; typedef std::vector<point> points; typedef std::vector<pl3> vpl3; typedef std::priority_queue<ll> pq; typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value. typedef std::vector<edge> gr; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = MOD * MOD; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define e1 first #define e2 second #define ctol(c) ll((c)) - ll('0') #define ltos(n) to_string((n)) #define items(kv, v) for (auto &(kv) : (v)) #define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)])) #define rsort(a, n) sort(a, a + n, greater<>()) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define XOR(a, b) (a) ^ (b) #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define DEBUG(x) cerr << #x << ": " << (x) << endl #define DEBUG_VEC(v) \ cerr << #v << ": "; \ rep(__i, (v).size()) cerr << ((v)[__i]) << ", "; \ cerr << endl struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; class UnionFind { vl parents, set_size; public: UnionFind() {} UnionFind(ll n) { parents = set_size = vl(n); rep(i, n) { parents[i] = i; set_size[i] = 1LL; } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { x = root_find(x); y = root_find(y); if (x == y) return; if (set_size[x] < set_size[y]) { parents[y] = x; set_size[x] += set_size[y]; } else { parents[x] = y; set_size[y] += set_size[x]; } } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } }; /* class LCA{ public: ll N, logN; vl depth, len; gr tree[200005]; // global declaration later. vector<vl> parents; LCA(ll n){ N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vector<vl>(logN, vl(N)); init(0, -1, 0, 0); build(); } void init(ll source, ll parent, ll d, ll l){ depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()){ ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build(){ rep(k, logN - 1) rep(n, N){ // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v){ if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN){ if (parents[k][u] != parents[k][v]){ u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v){ ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; */ struct BIT { ll n; vl tree_dat; BIT(ll n) : tree_dat(n + 1, 0), n(n){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll dat) { // if maximum -> max(val, dat) // return max(val, dat); // if cumulative sum return val + dat; } ll query(ll i) { /* e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, tree_dat[j]); } return ret; } ll lower_bound(ll key) { if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && tree_dat[left + i - 1] < key) { key -= tree_dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. 10101 -> 10110 itr2. 10110 -> 11000 itr3. 11000 -> 100000 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { tree_dat[j] = update_func(val, tree_dat[j]); } } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_mod(ll a, ll power, ll mod) { ll value = 1; while (power != 0) { if (power & 1) value = (value * a) % mod; a = (a * a) % mod; power = power >> 1; } return value % mod; } ll modinv(ll a, ll mod) { return power_mod(a, mod - 2, mod); } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } ll comb_memo[55][55]; ll pascal_triangle(ll n) { comb_memo[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb_memo[i][j] += comb_memo[i - 1][j]; if (j > 0) comb_memo[i][j] += comb_memo[i - 1][j - 1]; } } ll combination(ll n, ll r, ll mod) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { ll num = (n - i) % mod, den = (i + 1) % mod; (numerator *= num) %= mod; (denomenator *= modinv(den, mod)) %= mod; } return (numerator * denomenator) % mod; } ll combination_memo(ll n, ll r, ll pre, ll mod) { // pre = nCr-1 // return nCr if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = ll(n - r + 1) % mod; ll denomenator = modinv(r, mod) % mod; ll val = (numerator * denomenator) % mod; val *= pre; return val % mod; } ll combination_no_mod(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll numerator = 1; ll denomenator = 1; for (ll i = 0; i < r; i++) { numerator *= n - i; denomenator *= i + 1; ll g = gcd(numerator, denomenator); numerator /= g; denomenator /= g; } return numerator; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] >= key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] < key && key <= A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] < key && key <= A[mid]) return mid; else if (A[mid - 1] >= key) right = mid - 1; else if (A[mid] < key) left = mid; } return -1; // all the elements < key } ll inf_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value right instead of -1. if (A[left] > key) return left; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == left) { if (A[left] <= key && key < A[left + 1]) return left + 1; else return -1; } if (A[mid - 1] <= key && key < A[mid]) return mid; else if (A[mid - 1] > key) right = mid - 1; else if (A[mid] <= key) left = mid; } return -1; // all the elements <= key } ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] <= key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] <= key && key < A[right]) return right - 1; else return -1; } if (A[mid] <= key && key < A[mid + 1]) return mid; else if (A[mid] > key) right = mid; else if (A[mid + 1] <= key) left = mid + 1; } return -1; // all the elements > key } ll sup_bin_search_temp(ll key, ll A[], ll left, ll right) { // Pay attention to the return value -1. // For example, sometimes you may want the value left instead of -1. if (A[right] < key) return right; ll mid = left + (right - left) / 2; while (right >= left) { mid = left + (right - left) / 2; if (mid == right) { if (A[right - 1] < key && key <= A[right]) return right - 1; else return -1; } if (A[mid] < key && key <= A[mid + 1]) return mid; else if (A[mid] >= key) right = mid; else if (A[mid + 1] < key) left = mid + 1; } return -1; // all the elements >= key } ll bin_search_vector(ll key, vl v, ll left, ll right) { // return the index idx where v[idx] = key. // v[left] is start and v[right] is end.. // In other words, v[right], not v[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (v[mid] == key) return mid; else if (v[mid] > key) right = mid - 1; else if (v[mid] < key) left = mid + 1; } return -1; } ll lower_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] < key <= v[N] // N == -1 if all the elements < key return lower_bound(all(v), key) - v.begin(); } ll inf_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N - 1] <= key < v[N] <= key + 1 // N == -1 if all the elements <= key return lower_bound(all(v), key + 1) - v.begin(); } ll upper_bound_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key < v[N + 1] // N == -1 if all the elements > key return upper_bound(all(v), key) - v.begin(); // (- 1) } ll sup_bin_search_vector(ll key, vl v) { // the return value N satisfies // v[N] <= key - 1 < key <= v[N + 1] // N == -1 if all the elements >= key return upper_bound(all(v), key - 1) - v.begin() - 1; } ll fact(ll n) { if (n == 0) return 1; return n * fact(n - 1); } ll fact_mod(ll n, ll mod) { if (n == 0) return 1; return n * fact_mod(n - 1, mod); } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } ll bool_sum(ll a1, ll a2) { if (a1 == 1 || a2 == 1) return 1; return 0; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll char_to_idx(char c) { ll idx = 0; Forchar(cc, 'a', 'z') { if (c == cc) return idx; else idx++; } } bool is_cross(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3, ll x4, ll y4) { ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) { ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } ll next_combination(ll sub) { /* ll n, k; ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } */ ll x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } vl z_algorithm(str s) { ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = ctol(s[l - 1 - idx]); val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(ll xl1, ll yl1, ll xl2, ll yl2, ll xp, ll yp) { ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BIT bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } /* const ll N_VERTEX = 310; ll a, b, t; ll dist[N_VERTEX][N_VERTEX]; void warshall_floyd(ll n){ // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } int main(void){ in2(n, m); rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(i, m){ in3(a, b, t); a--; b--; dist[a][b] = t; dist[b][a] = t; } warshall_floyd(n); } ll dist[1005], vertex_pre[1005]; void dijkstra(ll start, ll n) { priority_queue<pl, vector<pl>, greater<pl>> edge_costs; fill(dist, dist + n, INF); // fill(vertex_pre, vertex_pre + n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { pl edge_cost = edge_costs.top(); edge_costs.pop(); ll idx = edge_cost.second; ll cost = edge_cost.first; if (dist[idx] < cost) continue; rep(i, graph[idx].size()){ edge e = graph[idx][i]; if (dist[e.to] > dist[idx] + e.cost){ dist[e.to] = dist[idx] + e.cost; // vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g){ vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } int main(void){ in2(n, m); rep(i, m){ in3(a, b, t); a--; b--; G[a].pb({b, t}); G[b].pb({a, t}); } dijkstra(0, n); } # ABC061D bool bellman_ford(ll start, ll n, ll goal){ // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != -1) fill(dist, dist + n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()){ edge e = graph[v][k]; if (dist[e.to] > dist[v] + e.cost){ dist[e.to] = dist[v] + e.cost; if (i == n - 1 && (e.to == goal || goal == -1)) return false; } } return true; } */ /* # 1. The usage of map pair map<pl, ll> cnt; cnt[{i, j}] = 0; items(kv, cnt){ prl2(kv.first, kv.second); } # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); // here, combination ll n, r; ll i1, i2, ..., ir; For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;} # 3. bit search ll n; in1(n); const ll base = 3; ll upper = power_normal(base, n); rep(i, upper){ ll tmp = i; rep(j, n){ rep(k, base) if (tmp % base == k) prl(k); tmp /= base; } } # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ r = max(r, l); if (l == r) r++; while(r < n && cond) r++; answer += r - l; l++; } prl(answer); #6. priority queue pq q; ll answer = 0; ll v; rep(i, n) q.push(a[i]); rep(i, m){ v = q.top(); q.pop(); // get the top value and dump the value from queue v /= 2; q.push(v); // add the new value } while(!q.empty()){ answer += q.top(); q.pop(); } #7. The shortest path (distance) between the k-th edge and another edge (Tree) ll depth[MAX_N]; gr tree[MAX_N]; void dfs(ll source, ll parent, ll all_cost){ depth[source] = all_cost; items(e, tree[source]){ if (e.to == parent) continue; dfs(e.to, source, all_cost + e.cost); } } ll n, k, a, b, c; in2(n, k); rep(i, n - 1){ in3(a, b, c); a--; b--; tree[a].pb({b, c}); tree[b].pb({a, c}); } k--; dfs(k, -1, 0); #10. Visiting Subtree using recurrent function (ABC138D) gr tree[MAX_N]; ll c[MAX_N]; bool visited[MAX_N]; void dfs(ll source, ll parent, ll val){ visited[source] = true; c[source] += val; rep(i, tree[source].size()){ rep(i, m){ll res = n % match[i].e1;} ll vertex = tree[source][i].to; if (vertex == parent) continue; dfs(vertex, source, c[source]); } } #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = G[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = G[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } #12. grid to distance matrix (dx, dy) ll w, h; ll pos_to_idx(ll x, ll y){ return y * w + x; } pl idx_to_pos(ll idx){ return mp(idx % w, idx / w); } int main(void){ in2(h, w); rep(y, h){ in1(s); rep(x, w){ if (s[x] == '#') wall[x][y] = true; else wall[x][y] = false; } } rep(i1, h * w)rep(i2, h * w) dist[i1][i2] = INF * (i1 != i2); rep(x, w)rep(y, h){ ll idx1 = pos_to_idx(x, y); ll idx2; if (wall[x][y]) continue; if (x != 0 && !wall[x - 1][y]){ idx2 = pos_to_idx(x - 1, y); // if warshall floyd dist[idx1][idx2] = 1; // if dijkstra // graph[idx1].pb({idx2, 1}); } if (x != w - 1 && !wall[x + 1][y]){ idx2 = pos_to_idx(x + 1, y); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != 0 && !wall[x][y - 1]){ idx2 = pos_to_idx(x, y - 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } if (y != h - 1 && !wall[x][y + 1]){ idx2 = pos_to_idx(x, y + 1); dist[idx1][idx2] = 1; // graph[idx1].pb({idx2, 1}); } } } */ /* # the operators regarding bit & (AND), | (OR), ^ (XOR) - (REVERSE), >> (SMALLER SHIFT) << (BIGGER SHIFT) x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & - x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 x << y is x * 2 ** y x >> y is rep(i, y) x = x // 2 Let S be a bit sequence and i be a non-negative integer S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & -(1 << i) -> S - {i} __builtin_popcount(S) -> the number of elements in S S = 0 -> S is an empty set S = (1 << n) - 1 -> S includes all the elements up to the n-th #Conditional Operator condition ? true : false; #iterator type declaration: auto value reference: *itr increment: itr++ decrement: itr-- substitution of value: *itr = smt */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; ll a, b, n; str s; ll dp[MAX_N * 5][2]; void solve() { reverse(all(s)); s += '0'; n = s.length(); rep(i, n + 1) dp[i][0] = dp[i][1] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { ll x = s[i] - '0'; x += j; if (x < 10) chmin(dp[i + 1][0], dp[i][j] + x); if (x > 0) chmin(dp[i + 1][1], dp[i][j] + 10 - x); } prl(dp[n][0]); // check negative MOD // check index flow // check overwrite of the input variables } int main(void) { in1(s); // assert(n <= 400); solve(); return 0; }
replace
1,203
1,204
1,203
1,204
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define pf push_front #define all(a) (a).begin(), (a).end() #define lwb lower_bound #define upb upper_bound #define vvll vector<vector<ll>> #define Reset(s, n) memset(s, n, sizeof(s)) #define Sz(a) int((a).size()) #define Max(a, b, c) max(max((ll)(a), (ll)(b)), (ll)(c)) #define Min(a, b, c) min(min((ll)(a), (ll)(b)), (ll)(c)) #define Unique(a) (a).resize(unique(all(a)) - (a).begin()) #define bit(n, i) (((n) >> (i)) & 1) #define onbit(n, i) ((n) |= (1LL << (i))) #define offbit(n, i) onbit((n), (i)), ((n) -= (1LL << (i))) #define bitcount(n) __builtin_popcountll(n) using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; const ll MOD = 1e9 + 7; const ll oo = 2e9; const ld PI = acos((ld)-1); const ld EPS = 1e-9; inline bool isPrime(ll x) { if (x <= 1) return 0; if (x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for (ll i = 5; i <= s; i += 6) { if (!(x % i) || !(x % (i + 2))) return 0; } return 1; } inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline void addmod(int &a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int &a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline ll gcd(ll a, ll b) { ll r; while (b) { r = a % b; a = b; b = r; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } const int N = 3e5 + 5; ll n, m, k; ll a[N]; ll f[N][3]; void Solve() { string s; cin >> s; for (char &c : s) c -= '0'; int n = Sz(s); ll res = 0; f[0][1] = s[0]; f[0][2] = 11 - s[0]; for (int i = 1; i < n; i++) { f[i][1] = s[i] + min(f[i - 1][2], f[i - 1][1]); f[i][2] = min(f[i - 1][2] - 1, f[i - 1][1] + 1) + 10 - s[i]; } cout << min(f[n - 1][1], f[n - 1][2]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int query = 1; // cin >> query; int start = 1000 * clock() / CLOCKS_PER_SEC; while (query--) Solve(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC - start << "ms\n"; }
#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define pf push_front #define all(a) (a).begin(), (a).end() #define lwb lower_bound #define upb upper_bound #define vvll vector<vector<ll>> #define Reset(s, n) memset(s, n, sizeof(s)) #define Sz(a) int((a).size()) #define Max(a, b, c) max(max((ll)(a), (ll)(b)), (ll)(c)) #define Min(a, b, c) min(min((ll)(a), (ll)(b)), (ll)(c)) #define Unique(a) (a).resize(unique(all(a)) - (a).begin()) #define bit(n, i) (((n) >> (i)) & 1) #define onbit(n, i) ((n) |= (1LL << (i))) #define offbit(n, i) onbit((n), (i)), ((n) -= (1LL << (i))) #define bitcount(n) __builtin_popcountll(n) using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; const ll MOD = 1e9 + 7; const ll oo = 2e9; const ld PI = acos((ld)-1); const ld EPS = 1e-9; inline bool isPrime(ll x) { if (x <= 1) return 0; if (x <= 3) return 1; if (!(x % 2) || !(x % 3)) return 0; ll s = sqrt(1.0 * x) + EPS; for (ll i = 5; i <= s; i += 6) { if (!(x % i) || !(x % (i + 2))) return 0; } return 1; } inline ll fpow(ll n, ll k, int p = MOD) { ll r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline void addmod(int &a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int &a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline ll gcd(ll a, ll b) { ll r; while (b) { r = a % b; a = b; b = r; } return a; } inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } const int N = 1e6 + 5; ll n, m, k; ll a[N]; ll f[N][3]; void Solve() { string s; cin >> s; for (char &c : s) c -= '0'; int n = Sz(s); ll res = 0; f[0][1] = s[0]; f[0][2] = 11 - s[0]; for (int i = 1; i < n; i++) { f[i][1] = s[i] + min(f[i - 1][2], f[i - 1][1]); f[i][2] = min(f[i - 1][2] - 1, f[i - 1][1] + 1) + 10 - s[i]; } cout << min(f[n - 1][1], f[n - 1][2]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int query = 1; // cin >> query; int start = 1000 * clock() / CLOCKS_PER_SEC; while (query--) Solve(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC - start << "ms\n"; }
replace
73
74
73
74
0
Time elapsed: 0ms
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = 1001001001; int dp[100005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += "0"; int n = s.size(); rep(i, n + 1) { rep(j, 2) dp[i][j] = INF; } dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; x += j; if (x < 10) dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x); if (x > 0) dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)); } cout << dp[n][0] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = 1001001001; int dp[1000005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += "0"; int n = s.size(); rep(i, n + 1) { rep(j, 2) dp[i][j] = INF; } dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; x += j; if (x < 10) dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x); if (x > 0) dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)); } cout << dp[n][0] << endl; return 0; }
replace
7
8
7
8
0
p02775
C++
Runtime Error
#pragma region cp -helper #include <bits/stdc++.h> using namespace std; #define AC \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long #define ll long long #define ull unsigned long long #define ii pair<int, int> #define lll pair<ll, ll> #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define vll vector<lll> #define vvl vector<vl> #define vii vector<ii> #define all(a) a.begin(), a.end() #define qsort(a) sort(all(a)) #define qsortd(a) sort(all(a), greater<>()) #define qsortf(a, f) sort(all(a), f) #define pb(n) push_back(n) #define eb(n) emplace_back(n) #define pp(a, b) emplace_back(a, b) #define umap unordered_map #define uset unordered_set #define nl '\n' #define fileio(in, out) \ freopen(in, "r", stdin); \ freopen(out, "w", stdout) #define qmod % mod #define pls signed #define give main() const int mod = 1000000007; #pragma endregion const int N = 200005; string a; int ans, n, dp[N][2]; int f(int i, int carry) { int digit = ((a[i] - '0')) + carry; if (i == 0) return digit; if (dp[i][carry]) return dp[i][carry]; return dp[i][carry] = (min(f(i - 1, 1) + (10 - digit), f(i - 1, 0) + digit)); } pls give { AC; cin >> a; n = a.length(); a = '0' + a; cout << f(n, 0) << nl; }
#pragma region cp -helper #include <bits/stdc++.h> using namespace std; #define AC \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define int long long #define ll long long #define ull unsigned long long #define ii pair<int, int> #define lll pair<ll, ll> #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define vll vector<lll> #define vvl vector<vl> #define vii vector<ii> #define all(a) a.begin(), a.end() #define qsort(a) sort(all(a)) #define qsortd(a) sort(all(a), greater<>()) #define qsortf(a, f) sort(all(a), f) #define pb(n) push_back(n) #define eb(n) emplace_back(n) #define pp(a, b) emplace_back(a, b) #define umap unordered_map #define uset unordered_set #define nl '\n' #define fileio(in, out) \ freopen(in, "r", stdin); \ freopen(out, "w", stdout) #define qmod % mod #define pls signed #define give main() const int mod = 1000000007; #pragma endregion const int N = 1000005; string a; int ans, n, dp[N][2]; int f(int i, int carry) { int digit = ((a[i] - '0')) + carry; if (i == 0) return digit; if (dp[i][carry]) return dp[i][carry]; return dp[i][carry] = (min(f(i - 1, 1) + (10 - digit), f(i - 1, 0) + digit)); } pls give { AC; cin >> a; n = a.length(); a = '0' + a; cout << f(n, 0) << nl; }
replace
37
38
37
38
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--) #define pb push_back #define all(r) r.begin(), r.end() #define rall(r) r.rbegin(), r.rend() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll INF = 1e18; const ll MOD = 1e9 + 7; const double EPS = 1e-8; template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); } template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); } const int MAX_N = 1e5 + 10; ll dp[MAX_N][2]; int main() { string s; cin >> s; reverse(all(s)); int n = s.size(); REP(i, 1, n + 2) rep(j, 2) dp[i][j] = INF; dp[0][1] = INF; rep(i, n) { int x = s[i] - '0'; { // ちょうど払う chmin(dp[i + 1][0], dp[i][0] + x); chmin(dp[i + 1][0], dp[i][1] + x + 1); // 繰り上げる chmin(dp[i + 1][1], dp[i][0] + 10 - x); chmin(dp[i + 1][1], dp[i][1] + 10 - (x + 1)); // cout << i << " " << dp[i+1][0] << " " << dp[i+1][1] << endl; } } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (int i = (s); i < (int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--) #define pb push_back #define all(r) r.begin(), r.end() #define rall(r) r.rbegin(), r.rend() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll INF = 1e18; const ll MOD = 1e9 + 7; const double EPS = 1e-8; template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); } template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); } const int MAX_N = 1e6 + 10; ll dp[MAX_N][2]; int main() { string s; cin >> s; reverse(all(s)); int n = s.size(); REP(i, 1, n + 2) rep(j, 2) dp[i][j] = INF; dp[0][1] = INF; rep(i, n) { int x = s[i] - '0'; { // ちょうど払う chmin(dp[i + 1][0], dp[i][0] + x); chmin(dp[i + 1][0], dp[i][1] + x + 1); // 繰り上げる chmin(dp[i + 1][1], dp[i][0] + 10 - x); chmin(dp[i + 1][1], dp[i][1] + 10 - (x + 1)); // cout << i << " " << dp[i+1][0] << " " << dp[i+1][1] << endl; } } cout << min(dp[n][0], dp[n][1] + 1) << endl; return 0; }
replace
25
26
25
26
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define lvector vector<ll> #define P pair<ll, ll> #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) #define print(a) cout << (a) << endl int main() { ios::sync_with_stdio(false); cin.tie(0); string N; cin >> N; ll ans = 0, l = N.length(); lvector v(l + 1, 0); rep(i, l + 1) v[i + 1] = N[i] - '0'; rep(i, l + 1) { ll pos = l - i; if (v[pos] < 5) ans += v[pos]; else if (pos - 1 >= 0) { ll pattern1 = 10 - v[pos] + min(v[pos - 1] + 1ll, 10ll - (v[pos - 1] + 1ll)); ll pattern2 = v[pos] + min(v[pos - 1], 10ll - v[pos - 1]); if (pattern1 <= pattern2) { ans += 10 - v[pos]; v[pos - 1] += 1; } else { ans += v[pos]; } } } print(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define lvector vector<ll> #define P pair<ll, ll> #define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i)) #define print(a) cout << (a) << endl int main() { ios::sync_with_stdio(false); cin.tie(0); string N; cin >> N; ll ans = 0, l = N.length(); lvector v(l + 1, 0); rep(i, l) v[i + 1] = N[i] - '0'; rep(i, l + 1) { ll pos = l - i; if (v[pos] < 5) ans += v[pos]; else if (pos - 1 >= 0) { ll pattern1 = 10 - v[pos] + min(v[pos - 1] + 1ll, 10ll - (v[pos - 1] + 1ll)); ll pattern2 = v[pos] + min(v[pos - 1], 10ll - v[pos - 1]); if (pattern1 <= pattern2) { ans += 10 - v[pos]; v[pos - 1] += 1; } else { ans += v[pos]; } } } print(ans); return 0; }
replace
15
16
15
16
0
p02775
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int f[105][2], n, num; char s[105]; bool check(int num) { if (num >= 0 && num <= 9) return 1; return 0; } int main() { scanf("%s", s + 1); n = strlen(s + 1); memset(f, 0x3f3f3f3f, sizeof(f)); f[0][0] = 0; f[0][1] = 1; for (register int i = 1; i <= n; ++i) { num = s[i] - '0'; for (register int j = 0; j <= 9; ++j) { if (check(j - num)) f[i][0] = min(f[i][0], f[i - 1][0] + j + j - num); if (check(j - num + 10)) f[i][0] = min(f[i][0], f[i - 1][1] + j + j - num + 10); if (check(j - 1 - num)) f[i][1] = min(f[i][1], f[i - 1][0] + j + j - 1 - num); if (check(j - 1 - num + 10)) f[i][1] = min(f[i][1], f[i - 1][1] + j + j - 1 - num + 10); } } printf("%d\n", f[n][0]); return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int f[1000005][2], n, num; char s[1000005]; bool check(int num) { if (num >= 0 && num <= 9) return 1; return 0; } int main() { scanf("%s", s + 1); n = strlen(s + 1); memset(f, 0x3f3f3f3f, sizeof(f)); f[0][0] = 0; f[0][1] = 1; for (register int i = 1; i <= n; ++i) { num = s[i] - '0'; for (register int j = 0; j <= 9; ++j) { if (check(j - num)) f[i][0] = min(f[i][0], f[i - 1][0] + j + j - num); if (check(j - num + 10)) f[i][0] = min(f[i][0], f[i - 1][1] + j + j - num + 10); if (check(j - 1 - num)) f[i][1] = min(f[i][1], f[i - 1][0] + j + j - 1 - num); if (check(j - 1 - num + 10)) f[i][1] = min(f[i][1], f[i - 1][1] + j + j - 1 - num + 10); } } printf("%d\n", f[n][0]); return 0; }
replace
4
6
4
6
0
p02775
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int n, dp[1000001][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += "0"; n = s.length(); for (int i = 0; i <= n; i++) { dp[i][0] = dp[i][1] = 1000000000; } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { int x = s[i] - '0'; x += j; for (int a = 0; a < 10; a++) { int ni = i + 1, nj = 0; int b = a - x; if (b < 0) { nj = 1; b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } } cout << min(dp[n][0], dp[n][1] + 1) << endl; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int n, dp[1000005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += "0"; n = s.length(); for (int i = 0; i <= n; i++) { dp[i][0] = dp[i][1] = 1000000000; } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { int x = s[i] - '0'; x += j; for (int a = 0; a < 10; a++) { int ni = i + 1, nj = 0; int b = a - x; if (b < 0) { nj = 1; b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } } cout << min(dp[n][0], dp[n][1] + 1) << endl; }
replace
5
6
5
6
0
p02775
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dbg(x) cerr << #x << ": " << x << endl #define all(x) (x).begin(), (x).end() int dp[100000][2]; const int INF = 1000000005; int main() { string s; cin >> s; reverse(all(s)); s += '0'; int m = s.size(); rep(i, m + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, m) rep(j, 2) { int n = s[i] - '0'; n += j; rep(a, 10) { int ni = i + 1, nj = (a - n < 0); int b = a - n >= 0 ? a - n : 10 + a - n; dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } cout << dp[m][0] << endl; return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dbg(x) cerr << #x << ": " << x << endl #define all(x) (x).begin(), (x).end() int dp[1000100][2]; const int INF = 1000000005; int main() { string s; cin >> s; reverse(all(s)); s += '0'; int m = s.size(); rep(i, m + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, m) rep(j, 2) { int n = s[i] - '0'; n += j; rep(a, 10) { int ni = i + 1, nj = (a - n < 0); int b = a - n >= 0 ? a - n : 10 + a - n; dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } cout << dp[m][0] << endl; return 0; }
replace
9
10
9
10
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = b - 1; i >= a; i--) #define ALL(a) a.begin(), a.end() #define pii pair<int, int> #pragma GCC optimize("Ofast") #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); 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 (a > b) { a = b; return 1; } return 0; }; inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } const double EPS = 1e-9; const int mod = 1e9 + 7; const int INF = 1e9; const long long LLINF = 1e18; long long lcm(ll a, ll b) { return a * b / __gcd(a, b); } struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int MAX = 1e5 + 10; int dp[MAX][2]; // dp[i+1][j] := Aを下からi桁目まで決めた時繰り下がりがjの時の最小値 int main() { iosetup; string S; cin >> S; reverse(ALL(S)); S += '0'; int N = S.size(); rep(i, 0, N + 1) rep(j, 0, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, 0, N) rep(j, 0, 2) { int x = S[i] - '0'; x += j; rep(a, 0, 10) { int ni = i + 1, nj = 0; int b = a - x; if (b < 0) { nj = 1; b += 10; } chmin(dp[ni][nj], dp[i][j] + a + b); } } int ans = dp[N][0]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = b - 1; i >= a; i--) #define ALL(a) a.begin(), a.end() #define pii pair<int, int> #pragma GCC optimize("Ofast") #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define pb push_back #define mp make_pair #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); 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 (a > b) { a = b; return 1; } return 0; }; inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } const double EPS = 1e-9; const int mod = 1e9 + 7; const int INF = 1e9; const long long LLINF = 1e18; long long lcm(ll a, ll b) { return a * b / __gcd(a, b); } struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using modint = ModInt<mod>; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int MAX = 1e6 + 10; int dp[MAX][2]; // dp[i+1][j] := Aを下からi桁目まで決めた時繰り下がりがjの時の最小値 int main() { iosetup; string S; cin >> S; reverse(ALL(S)); S += '0'; int N = S.size(); rep(i, 0, N + 1) rep(j, 0, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, 0, N) rep(j, 0, 2) { int x = S[i] - '0'; x += j; rep(a, 0, 10) { int ni = i + 1, nj = 0; int b = a - x; if (b < 0) { nj = 1; b += 10; } chmin(dp[ni][nj], dp[i][j] + a + b); } } int ans = dp[N][0]; cout << ans << endl; return 0; }
replace
118
119
118
119
0
p02775
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; #define chmin(x, y) x = min(x, y) const int INF = 1001001001; int dp[100][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; int n = s.size(); rep(i, n + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; x += j; if (x < 10) chmin(dp[i + 1][0], dp[i][j] + x); if (x > 0) chmin(dp[i + 1][1], dp[i][j] + (10 - x)); } int ans = dp[n][0]; cout << ans << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; #define chmin(x, y) x = min(x, y) const int INF = 1001001001; int dp[1000005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; int n = s.size(); rep(i, n + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; x += j; if (x < 10) chmin(dp[i + 1][0], dp[i][j] + x); if (x > 0) chmin(dp[i + 1][1], dp[i][j] + (10 - x)); } int ans = dp[n][0]; cout << ans << endl; return 0; }
replace
12
13
12
13
0
p02775
C++
Runtime Error
#include <iostream> using namespace std; #define MAX 1000001 #define ll long long const ll INF = 1e18 + 7; string s; ll num[MAX], sum[MAX], dp[MAX][2]; int main() { cin >> s; s = '0' + s; int len = s.length(); for (int i = 0; i < len; ++i) { num[i] = s[i] - '0'; sum[i + 1] = sum[i] + num[i]; } for (int i = 0; i <= len; ++i) for (int j = 0; j < 2; ++j) dp[i][j] = INF; dp[0][0] = 0; for (int i = 0; i < len; ++i) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + num[i]); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + num[i] + 1); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] - num[i] + 10); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] - num[i] + 9); } cout << dp[len][0] << '\n'; return 0; }
#include <iostream> using namespace std; #define MAX 1000002 #define ll long long const ll INF = 1e18 + 7; string s; ll num[MAX], sum[MAX], dp[MAX][2]; int main() { cin >> s; s = '0' + s; int len = s.length(); for (int i = 0; i < len; ++i) { num[i] = s[i] - '0'; sum[i + 1] = sum[i] + num[i]; } for (int i = 0; i <= len; ++i) for (int j = 0; j < 2; ++j) dp[i][j] = INF; dp[0][0] = 0; for (int i = 0; i < len; ++i) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + num[i]); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + num[i] + 1); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] - num[i] + 10); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] - num[i] + 9); } cout << dp[len][0] << '\n'; return 0; }
replace
2
3
2
3
0
p02775
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <tuple> #include <utility> #include <vector> template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } // [l, r) from l to r struct range { struct itr { int i; constexpr itr(int i_) : i(i_) {} constexpr void operator++() { ++i; } constexpr int operator*() const { return i; } constexpr bool operator!=(itr x) const { return i != x.i; } }; const itr l, r; constexpr range(int l_, int r_) : l(std::min<int>(l_, r_)), r(r_) {} constexpr itr begin() const { return l; } constexpr itr end() const { return r; } }; // [l, r) from r to l struct revrange { struct itr { int i; constexpr itr(int i_) : i(i_) {} constexpr void operator++() { --i; } constexpr int operator*() const { return i; } constexpr bool operator!=(itr x) const { return i != x.i; } }; const itr r, l; constexpr revrange(int l_, int r_) : r(std::max<int>(l_, r_) - 1), l(l_ - 1) {} constexpr itr begin() const { return r; } constexpr itr end() const { return l; } }; template <class T> inline T scan() { T res; std::cin >> res; return res; } constexpr int inf = (1 << 30) - 1; int N; int dp[1000001][2]; std::string S; int main() { std::cin >> S; N = S.size(); for (int i : range(0, N + 1)) { dp[i][0] = dp[i][1] = inf; } dp[0][0] = 0; for (int i : range(0, N)) { int d = S[N - i - 1] - '0'; chmin(dp[i + 1][0], dp[i][0] + d); chmin(dp[i + 1][0], dp[i][1] + d); chmin(dp[i + 1][1], dp[i][0] + 11 - d); chmin(dp[i + 1][1], dp[i][1] + 9 - d); } std::cout << std::min(dp[N][0], dp[N][1]) << '\n'; return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <tuple> #include <utility> #include <vector> template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } // [l, r) from l to r struct range { struct itr { int i; constexpr itr(int i_) : i(i_) {} constexpr void operator++() { ++i; } constexpr int operator*() const { return i; } constexpr bool operator!=(itr x) const { return i != x.i; } }; const itr l, r; constexpr range(int l_, int r_) : l(std::min<int>(l_, r_)), r(r_) {} constexpr itr begin() const { return l; } constexpr itr end() const { return r; } }; // [l, r) from r to l struct revrange { struct itr { int i; constexpr itr(int i_) : i(i_) {} constexpr void operator++() { --i; } constexpr int operator*() const { return i; } constexpr bool operator!=(itr x) const { return i != x.i; } }; const itr r, l; constexpr revrange(int l_, int r_) : r(std::max<int>(l_, r_) - 1), l(l_ - 1) {} constexpr itr begin() const { return r; } constexpr itr end() const { return l; } }; template <class T> inline T scan() { T res; std::cin >> res; return res; } constexpr int inf = (1 << 30) - 1; int N; int dp[1000002][2]; std::string S; int main() { std::cin >> S; N = S.size(); for (int i : range(0, N + 1)) { dp[i][0] = dp[i][1] = inf; } dp[0][0] = 0; for (int i : range(0, N)) { int d = S[N - i - 1] - '0'; chmin(dp[i + 1][0], dp[i][0] + d); chmin(dp[i + 1][0], dp[i][1] + d); chmin(dp[i + 1][1], dp[i][0] + 11 - d); chmin(dp[i + 1][1], dp[i][1] + 9 - d); } std::cout << std::min(dp[N][0], dp[N][1]) << '\n'; return 0; }
replace
65
66
65
66
0
p02775
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; 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; ll min(ll a, ll b) { return a < b ? a : b; } ll min(int a, ll b) { return a < b ? a : b; } ll min(ll a, int b) { return a < b ? a : b; } ll min(int a, int b) { return a < b ? a : b; } ll max(ll a, ll b) { return a > b ? a : b; } ll max(int a, ll b) { return a > b ? a : b; } ll max(ll a, int b) { return a > b ? a : b; } ll max(int a, int b) { return a > b ? a : b; } namespace MySpace {}; #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() #define int long long int n; string s; int dp[2][500000]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; dp[0][0] = s[0] - '0'; dp[1][0] = 11 - (s[0] - '0'); for (int i = 1; i < s.size(); i++) { dp[0][i] = min(dp[0][i - 1], dp[1][i - 1]) + s[i] - '0'; dp[1][i] = min(dp[0][i - 1] + 11 - (s[i] - '0'), dp[1][i - 1] + 9 - (s[i] - '0')); } cout << min(dp[0][s.size() - 1], dp[1][s.size() - 1]); }
#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; 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; ll min(ll a, ll b) { return a < b ? a : b; } ll min(int a, ll b) { return a < b ? a : b; } ll min(ll a, int b) { return a < b ? a : b; } ll min(int a, int b) { return a < b ? a : b; } ll max(ll a, ll b) { return a > b ? a : b; } ll max(int a, ll b) { return a > b ? a : b; } ll max(ll a, int b) { return a > b ? a : b; } ll max(int a, int b) { return a > b ? a : b; } namespace MySpace {}; #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() #define int long long int n; string s; int dp[2][1500000]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; dp[0][0] = s[0] - '0'; dp[1][0] = 11 - (s[0] - '0'); for (int i = 1; i < s.size(); i++) { dp[0][i] = min(dp[0][i - 1], dp[1][i - 1]) + s[i] - '0'; dp[1][i] = min(dp[0][i - 1] + 11 - (s[i] - '0'), dp[1][i - 1] + 9 - (s[i] - '0')); } cout << min(dp[0][s.size() - 1], dp[1][s.size() - 1]); }
replace
78
79
78
79
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; int dp[200000][2]; int main() { string s; cin >> s; memset(dp, 0x3f, sizeof(dp)); s = "0" + s; dp[s.size()][0] = 0; for (int i = s.size(); i > 0; i--) rep(j, 2) { if (dp[i][j] == INF) continue; for (int k = 0; k <= 9; k++) { int x = k - (int)(s[i - 1] - '0') - j; bool b = 0; if (x < 0) b = 1, x += 10; dp[i - 1][b] = min(dp[i - 1][b], dp[i][j] + k + x); } } cout << dp[0][0] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; int dp[2000000][2]; int main() { string s; cin >> s; memset(dp, 0x3f, sizeof(dp)); s = "0" + s; dp[s.size()][0] = 0; for (int i = s.size(); i > 0; i--) rep(j, 2) { if (dp[i][j] == INF) continue; for (int k = 0; k <= 9; k++) { int x = k - (int)(s[i - 1] - '0') - j; bool b = 0; if (x < 0) b = 1, x += 10; dp[i - 1][b] = min(dp[i - 1][b], dp[i][j] + k + x); } } cout << dp[0][0] << endl; }
replace
8
9
8
9
0
p02775
C++
Runtime Error
// atcoder/abc155/E/main.cpp // author: @___Johniel // github: https://github.com/johniel/ #include <bits/stdc++.h> #define each(i, c) for (auto &i : c) #define unless(cond) if (!(cond)) using namespace std; template <typename P, typename Q> ostream &operator<<(ostream &os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename P, typename Q> istream &operator>>(istream &is, pair<P, Q> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> v) { os << "("; for (auto &i : v) os << i << ","; os << ")"; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &i : v) is >> i; return is; } template <typename T> ostream &operator<<(ostream &os, set<T> s) { os << "#{"; for (auto &i : s) os << i << ","; os << "}"; return os; } template <typename K, typename V> ostream &operator<<(ostream &os, map<K, V> m) { os << "{"; for (auto &i : m) os << i << ","; os << "}"; return os; } template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); } template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); } template <typename P, typename Q> inline pair<Q, P> reverse(pair<P, Q> p) { return make_pair(p.second, p.first); } using lli = long long int; using ull = unsigned long long; using point = complex<double>; using str = string; template <typename T> using vec = vector<T>; constexpr lli mod = 1e9 + 7; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); str s; while (cin >> s) { vector<int> v(s.begin(), s.end()); each(i, v) i -= '0'; reverse(v.begin(), v.end()); const int N = 10000 + 100; const int M = 2; static lli dp[N][M]; const lli inf = 1LL << 60; fill(&dp[0][0], &dp[N - 1][M - 1] + 1, inf); dp[0][false] = 0; for (int i = 0; i < v.size(); ++i) { { setmin(dp[i + 1][true], dp[i][false] + (10 - v[i])); setmin(dp[i + 1][false], dp[i][false] + v[i]); } { setmin(dp[i + 1][true], dp[i][true] + (10 - v[i] - 1)); setmin(dp[i + 1][false], dp[i][true] + v[i] + 1); } } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; } return 0; }
// atcoder/abc155/E/main.cpp // author: @___Johniel // github: https://github.com/johniel/ #include <bits/stdc++.h> #define each(i, c) for (auto &i : c) #define unless(cond) if (!(cond)) using namespace std; template <typename P, typename Q> ostream &operator<<(ostream &os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename P, typename Q> istream &operator>>(istream &is, pair<P, Q> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> v) { os << "("; for (auto &i : v) os << i << ","; os << ")"; return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &i : v) is >> i; return is; } template <typename T> ostream &operator<<(ostream &os, set<T> s) { os << "#{"; for (auto &i : s) os << i << ","; os << "}"; return os; } template <typename K, typename V> ostream &operator<<(ostream &os, map<K, V> m) { os << "{"; for (auto &i : m) os << i << ","; os << "}"; return os; } template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); } template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); } template <typename P, typename Q> inline pair<Q, P> reverse(pair<P, Q> p) { return make_pair(p.second, p.first); } using lli = long long int; using ull = unsigned long long; using point = complex<double>; using str = string; template <typename T> using vec = vector<T>; constexpr lli mod = 1e9 + 7; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); str s; while (cin >> s) { vector<int> v(s.begin(), s.end()); each(i, v) i -= '0'; reverse(v.begin(), v.end()); const int N = 1e6 + 5; const int M = 2; static lli dp[N][M]; const lli inf = 1LL << 60; fill(&dp[0][0], &dp[N - 1][M - 1] + 1, inf); dp[0][false] = 0; for (int i = 0; i < v.size(); ++i) { { setmin(dp[i + 1][true], dp[i][false] + (10 - v[i])); setmin(dp[i + 1][false], dp[i][false] + v[i]); } { setmin(dp[i + 1][true], dp[i][true] + (10 - v[i] - 1)); setmin(dp[i + 1][false], dp[i][true] + v[i] + 1); } } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; } return 0; }
replace
73
74
73
74
0
p02775
C++
Runtime Error
/* AuThOr GaRyMr */ #include <bits/stdc++.h> #define rb(a, b, c) for (int a = b; a <= c; ++a) #define rl(a, b, c) for (int a = b; a >= c; --a) #define LL long long #define IT iterator #define PB push_back #define II(a, b) make_pair(a, b) #define FIR first #define SEC second #define FREO freopen("check.out", "w", stdout) #define rep(a, b) for (int a = 0; a < b; ++a) #define KEEP while (1) #define SRAND \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng() % a #define ALL(a) a.begin(), a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int, int> mp; typedef pair<mp, mp> superpair; int val[100000 + 1]; int dp[1000000 + 3][2]; // 这一位是否要借; int main() { fastio; string n; cin >> n; int len = n.length(); rep(i, len) val[i] = n[i] - '0'; reverse(val, val + len); memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; rb(i, 1, len) { rb(j, 0, 9) { rb(k, 0, 9) { // cout<<j<<"" if (j - k == val[i - 1]) { dp[i][0] = min(dp[i][0], dp[i - 1][0] + j + k); } if (10 + j - k == val[i - 1]) { dp[i][1] = min(dp[i][1], dp[i - 1][0] + j + k); } if (j - 1 - k == val[i - 1]) { dp[i][0] = min(dp[i][0], dp[i - 1][1] + j + k); } if (9 + j - k == val[i - 1]) { dp[i][1] = min(dp[i][1], dp[i - 1][1] + j + k); } } } } cout << min(dp[len][0], dp[len][1] + 1) << endl; return 0; }
/* AuThOr GaRyMr */ #include <bits/stdc++.h> #define rb(a, b, c) for (int a = b; a <= c; ++a) #define rl(a, b, c) for (int a = b; a >= c; --a) #define LL long long #define IT iterator #define PB push_back #define II(a, b) make_pair(a, b) #define FIR first #define SEC second #define FREO freopen("check.out", "w", stdout) #define rep(a, b) for (int a = 0; a < b; ++a) #define KEEP while (1) #define SRAND \ mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng() % a #define ALL(a) a.begin(), a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) using namespace std; const int INF = 0x3f3f3f3f; typedef pair<int, int> mp; typedef pair<mp, mp> superpair; int val[1000000 + 1]; int dp[1000000 + 3][2]; // 这一位是否要借; int main() { fastio; string n; cin >> n; int len = n.length(); rep(i, len) val[i] = n[i] - '0'; reverse(val, val + len); memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; rb(i, 1, len) { rb(j, 0, 9) { rb(k, 0, 9) { // cout<<j<<"" if (j - k == val[i - 1]) { dp[i][0] = min(dp[i][0], dp[i - 1][0] + j + k); } if (10 + j - k == val[i - 1]) { dp[i][1] = min(dp[i][1], dp[i - 1][0] + j + k); } if (j - 1 - k == val[i - 1]) { dp[i][0] = min(dp[i][0], dp[i - 1][1] + j + k); } if (9 + j - k == val[i - 1]) { dp[i][1] = min(dp[i][1], dp[i - 1][1] + j + k); } } } } cout << min(dp[len][0], dp[len][1] + 1) << endl; return 0; }
replace
26
27
26
27
0
p02775
C++
Runtime Error
#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, s, f) for (int i = s; i < f; i++) #define INF 1000000000000000000 #define MOD 1000000007 typedef pair<int, int> P; 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; } bool prime(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int mod_pow(int n, int p, int m) { if (p == 0) return 1; if (p % 2 == 0) { int t = mod_pow(n, p / 2, m); return (t * t) % m; } return n * mod_pow(n, p - 1, m) % m; } int extGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } int modinv(int a, int m) { int b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int digit(int x) { int cnt = 0; while (x > 0) { cnt += x % 10; x /= 10; } return cnt; } int read() { int tmp; cin >> tmp; return tmp; } int xor128() { static int x = 123456789, y = 362436069, z = 521288629, w = 88675123; int t = (x xor (x << 11)); x = y; y = z; z = w; return (w = (w xor (w >> 19)) xor (t xor (t >> 8))); } map<int, int> factaring(int x) { map<int, int> ans; int now = 2; while (now * now <= x) { if (x % now == 0) { x /= now; ans[now]++; } else now++; } if (x != 1) ans[x]++; return ans; } int comb(int n, int k) { k = min(k, n - k); int x = 1, y = 1; rep(i, k) { y *= i + 1; y %= MOD; } for (int i = n - k + 1; i <= n; i++) { x *= i; x %= MOD; } return x * modinv(y, MOD) % MOD; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); string s; int dp[100005][2]; cin >> s; reverse(s.begin(), s.end()); rep(i, s.size() + 1) { rep(j, 2) { dp[i][j] = INF; } } dp[0][0] = 0, dp[0][1] = -1; rep(i, s.size()) { if (dp[i][0] != -1) { chmin(dp[i + 1][0], dp[i][0] + (int)(s[i] - '0')); chmin(dp[i + 1][1], dp[i][0] + ((int)10 - (int)(s[i] - '0')) + 1); } if (dp[i][1] != -1) { chmin(dp[i + 1][0], dp[i][1] + (int)(s[i] - '0')); chmin(dp[i + 1][1], dp[i][1] + ((int)10 - (int)(s[i] - '0')) - 1); } } /*rep(i, s.size() + 1) { rep(j, 2) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << min(dp[s.size()][0], dp[s.size()][1]) << endl; return 0; }
#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, s, f) for (int i = s; i < f; i++) #define INF 1000000000000000000 #define MOD 1000000007 typedef pair<int, int> P; 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; } bool prime(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int mod_pow(int n, int p, int m) { if (p == 0) return 1; if (p % 2 == 0) { int t = mod_pow(n, p / 2, m); return (t * t) % m; } return n * mod_pow(n, p - 1, m) % m; } int extGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } int modinv(int a, int m) { int b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int digit(int x) { int cnt = 0; while (x > 0) { cnt += x % 10; x /= 10; } return cnt; } int read() { int tmp; cin >> tmp; return tmp; } int xor128() { static int x = 123456789, y = 362436069, z = 521288629, w = 88675123; int t = (x xor (x << 11)); x = y; y = z; z = w; return (w = (w xor (w >> 19)) xor (t xor (t >> 8))); } map<int, int> factaring(int x) { map<int, int> ans; int now = 2; while (now * now <= x) { if (x % now == 0) { x /= now; ans[now]++; } else now++; } if (x != 1) ans[x]++; return ans; } int comb(int n, int k) { k = min(k, n - k); int x = 1, y = 1; rep(i, k) { y *= i + 1; y %= MOD; } for (int i = n - k + 1; i <= n; i++) { x *= i; x %= MOD; } return x * modinv(y, MOD) % MOD; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); string s; int dp[1000005][2]; cin >> s; reverse(s.begin(), s.end()); rep(i, s.size() + 1) { rep(j, 2) { dp[i][j] = INF; } } dp[0][0] = 0, dp[0][1] = -1; rep(i, s.size()) { if (dp[i][0] != -1) { chmin(dp[i + 1][0], dp[i][0] + (int)(s[i] - '0')); chmin(dp[i + 1][1], dp[i][0] + ((int)10 - (int)(s[i] - '0')) + 1); } if (dp[i][1] != -1) { chmin(dp[i + 1][0], dp[i][1] + (int)(s[i] - '0')); chmin(dp[i + 1][1], dp[i][1] + ((int)10 - (int)(s[i] - '0')) - 1); } } /*rep(i, s.size() + 1) { rep(j, 2) { cout << dp[i][j] << " "; } cout << endl; }*/ cout << min(dp[s.size()][0], dp[s.size()][1]) << endl; return 0; }
replace
129
130
129
130
0
p02775
Python
Time Limit Exceeded
s = input() keta = len(s) n = int(s) infi = 10**10 dp = [[infi] * (2) for _ in range(keta + 1)] dp[0][0] = 0 s = s[::-1] for i in range(keta): for j in range(2): num = int(s[i]) if j == 1: num += 1 ni = i + 1 if num == 10: nj = 1 num = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) elif num < 5: nj = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) elif num > 5: nj = 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) else: nj = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) nj = 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) print(min(dp[keta][0], dp[keta][1] + 1))
s = input() keta = len(s) infi = 10**10 dp = [[infi] * (2) for _ in range(keta + 1)] dp[0][0] = 0 s = s[::-1] for i in range(keta): for j in range(2): num = int(s[i]) if j == 1: num += 1 ni = i + 1 if num == 10: nj = 1 num = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) elif num < 5: nj = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) elif num > 5: nj = 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) else: nj = 0 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num) nj = 1 dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num) print(min(dp[keta][0], dp[keta][1] + 1))
delete
2
3
2
2
TLE
p02775
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #define int long long #define endl '\n' #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define bug1(x) \ { cerr << (#x) << "=" << x << endl; } #define bug2(x, y) \ { cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << endl; } #define bug3(x, y, z) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << endl; \ } #define bug4(x, y, z, w) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << endl; \ } #define bug5(x, y, z, w, p) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << " " << (#p) \ << "=" << p << endl; \ } #define bug6(x, y, z, w, p, q) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << " " << (#p) \ << "=" << p << " " << (#q) << "=" << q << endl; \ } #define bugn(x, n) \ { \ cerr << (#x) << ":"; \ for (int i = 0; i < n; i++) \ cerr << x[i] << " "; \ cerr << endl; \ } #define bugnm(x, n, m) \ { \ cerr << (#x) << endl; \ for (int i = 0; i < n; i++) { \ cerr << "Row #" << i << ":"; \ for (int j = 0; j < m; j++) \ cerr << x[i][j] << " "; \ cerr << endl; \ } \ } typedef long long ll; typedef long double ld; using namespace std; const int maxn = 1e6 + 10; int dp[maxn][10]; string s; int n; int solve(int i, int carry) { bug2(i, carry); if (i >= n) return carry; int &ans = dp[i][carry % 10]; if (ans != -1) return carry / 10 + ans; ans = 1e8; carry += (i < n ? s[i] - '0' : 0); ans = min(ans, carry % 10 + solve(i + 1, carry / 10)); if (carry % 10) { ans = min(ans, 10 - (carry % 10) + solve(i + 1, carry / 10 + 1)); } return ans; } int32_t main() { IOS memset(dp, -1, sizeof(dp)); cin >> s; reverse(s.begin(), s.end()); n = s.length(); cout << solve(0, 0); } /* * long long or int? * index out of bound? * Tested on own test case?corner? * Make more general solution. * Read Read Read Read .... */
#include <bits/stdc++.h> // #define int long long #define endl '\n' #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define bug1(x) \ { cerr << (#x) << "=" << x << endl; } #define bug2(x, y) \ { cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << endl; } #define bug3(x, y, z) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << endl; \ } #define bug4(x, y, z, w) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << endl; \ } #define bug5(x, y, z, w, p) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << " " << (#p) \ << "=" << p << endl; \ } #define bug6(x, y, z, w, p, q) \ { \ cerr << (#x) << "=" << (x) << " " << (#y) << "=" << (y) << " " \ << (#z) << "=" << (z) << " " << (#w) << "=" << w << " " << (#p) \ << "=" << p << " " << (#q) << "=" << q << endl; \ } #define bugn(x, n) \ { \ cerr << (#x) << ":"; \ for (int i = 0; i < n; i++) \ cerr << x[i] << " "; \ cerr << endl; \ } #define bugnm(x, n, m) \ { \ cerr << (#x) << endl; \ for (int i = 0; i < n; i++) { \ cerr << "Row #" << i << ":"; \ for (int j = 0; j < m; j++) \ cerr << x[i][j] << " "; \ cerr << endl; \ } \ } typedef long long ll; typedef long double ld; using namespace std; const int maxn = 1e6 + 10; int dp[maxn][10]; string s; int n; int solve(int i, int carry) { // bug2(i,carry); if (i >= n) return carry; int &ans = dp[i][carry % 10]; if (ans != -1) return carry / 10 + ans; ans = 1e8; carry += (i < n ? s[i] - '0' : 0); ans = min(ans, carry % 10 + solve(i + 1, carry / 10)); if (carry % 10) { ans = min(ans, 10 - (carry % 10) + solve(i + 1, carry / 10 + 1)); } return ans; } int32_t main() { IOS memset(dp, -1, sizeof(dp)); cin >> s; reverse(s.begin(), s.end()); n = s.length(); cout << solve(0, 0); } /* * long long or int? * index out of bound? * Tested on own test case?corner? * Make more general solution. * Read Read Read Read .... */
replace
59
60
59
60
TLE
p02775
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int dp[SIZE][2]; int main() { char S[SIZE]; scanf("%s", S); int N = strlen(S); for (int i = 0; i <= N; i++) dp[i][0] = dp[i][1] = INF; dp[0][0] = 0; dp[0][1] = 1; for (int i = 0; i < N; i++) { int p = S[i] - '0'; dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + p); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 10 - p); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + 1); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 10 - p - 1); } cout << dp[N][0] << endl; return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template <typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 1000010 int dp[SIZE][2]; int main() { char S[SIZE]; scanf("%s", S); int N = strlen(S); for (int i = 0; i <= N; i++) dp[i][0] = dp[i][1] = INF; dp[0][0] = 0; dp[0][1] = 1; for (int i = 0; i < N; i++) { int p = S[i] - '0'; dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + p); dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 10 - p); dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + 1); dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 10 - p - 1); } cout << dp[N][0] << endl; return 0; }
replace
48
49
48
49
0
p02775
C++
Runtime Error
// agrawal117 // chahatagrawal117 #pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx2") #include <bits/stdc++.h> #define endl '\n' using namespace std; typedef long long int ll; #define MAX 1005 ll dp[1000009][2]; string s; ll solve(int i, int carry) { if (i < 0) return carry; if (dp[i][carry] != -1) return dp[i][carry]; if (carry == 1) { int n = s[i] - '0'; ll y; if (n == 9) { n = 0; y = solve(i - 1, 1); } else { n++; y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1)); } dp[i][carry] = y; return y; } else { int n = s[i] - '0'; ll y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1)); dp[i][carry] = y; return y; } } int main() { cin >> s; for (int i = 0; i < s.size() + 1; i++) { dp[i][0] = -1; dp[i][1] = -1; } ll ans = solve(s.size() - 1, 0); cout << ans << endl; }
// agrawal117 // chahatagrawal117 #pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx2") #include <bits/stdc++.h> #define endl '\n' using namespace std; typedef long long int ll; #define MAX 1005 ll dp[1000009][4]; string s; ll solve(int i, int carry) { if (i < 0) return carry; if (dp[i][carry] != -1) return dp[i][carry]; if (carry == 1) { int n = s[i] - '0'; ll y; if (n == 9) { n = 0; y = solve(i - 1, 1); } else { n++; y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1)); } dp[i][carry] = y; return y; } else { int n = s[i] - '0'; ll y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1)); dp[i][carry] = y; return y; } } int main() { cin >> s; for (int i = 0; i < s.size() + 1; i++) { dp[i][0] = -1; dp[i][1] = -1; } ll ans = solve(s.size() - 1, 0); cout << ans << endl; }
replace
9
10
9
10
0
p02775
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; const ll NCK_MAX = 510000; /**** General Functions ****/ ll ketawa(ll n) { ll a = 0; while (n != 0) { a += n % 10; n /= 10; } return a; } ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return (t % M) * (t % M) % M; } return (N * RepeatSquaring(N, P - 1, M)) % M; } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll *fac, *finv, *inv; void nCk_init(ll mod) { fac = new ll[NCK_MAX]; finv = new ll[NCK_MAX]; inv = new ll[NCK_MAX]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < NCK_MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } ll nCk(ll n, ll k, ll mod) { if (fac == NULL) nCk_init(mod); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ class SegmentTree { public: vector<pair<double, double>> node; // node[0]は使用しない ll n; // データの個数(nodeの最下層には何個並んでいるか) pair<double, double> initial_value; // 初期値 public: void Init(ll n_, pair<double, double> initial_value_) { n = 1; while (n < n_) n *= 2; node.resize(2 * n); for (ll i = 0; i < 2 * n; i++) { node[i] = initial_value_; } initial_value = initial_value_; } void Update(ll k, pair<double, double> a) { // node[k]をaにする // それに従って先祖も変わっていく k += n; node[k] = a; while (k > 1) { k = k / 2; node[k] = pair<double, double>( node[k * 2].first * node[k * 2 + 1].first, node[k * 2].second * node[k * 2 + 1].first + node[k * 2 + 1].second); } } /*void Watch(){ for(ll i=0;i<2*n;i++){ cout<<node[i]<<endl; } }*/ double Query() { //[a,b) return node[1].first + node[1].second; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll k, n; ll m, d; ll ans = -9999999999999; string s, t; vector<ll> a; vector<ll> pluss, minuss; ll keta[1000000]; ll dp[1000000][2]; int main() { cin >> s; for (ll i = 0; i < s.size(); i++) { keta[i] = s[s.size() - 1 - i] - '0'; } dp[0][0] = keta[0]; dp[0][1] = 10 - keta[0] + 1; // cout<<dp[0][0]<<" "<<dp[0][1]<<endl; for (ll i = 1; i < s.size(); i++) { // cout<<dp[i-1][0]+keta[i]<<" "<<dp[i-1][1]+keta[i]<<endl; // cout<<dp[i-1][0]+(10-keta[i])+1<<" "<<dp[i-1][1]+(10-keta[i]+1)<<endl; dp[i][0] = lmin(dp[i - 1][0] + keta[i], dp[i - 1][1] + keta[i]); dp[i][1] = lmin(dp[i - 1][0] + (10 - keta[i]) + 1, dp[i - 1][1] + (10 - keta[i]) + 1 - 2); // cout<<dp[i][0]<<" "<<dp[i][1]<<endl; } cout << lmin(dp[s.size() - 1][0], dp[s.size() - 1][1]) << endl; }
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; const ll NCK_MAX = 510000; /**** General Functions ****/ ll ketawa(ll n) { ll a = 0; while (n != 0) { a += n % 10; n /= 10; } return a; } ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return (t % M) * (t % M) % M; } return (N * RepeatSquaring(N, P - 1, M)) % M; } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll *fac, *finv, *inv; void nCk_init(ll mod) { fac = new ll[NCK_MAX]; finv = new ll[NCK_MAX]; inv = new ll[NCK_MAX]; fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < NCK_MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } ll nCk(ll n, ll k, ll mod) { if (fac == NULL) nCk_init(mod); if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ class SegmentTree { public: vector<pair<double, double>> node; // node[0]は使用しない ll n; // データの個数(nodeの最下層には何個並んでいるか) pair<double, double> initial_value; // 初期値 public: void Init(ll n_, pair<double, double> initial_value_) { n = 1; while (n < n_) n *= 2; node.resize(2 * n); for (ll i = 0; i < 2 * n; i++) { node[i] = initial_value_; } initial_value = initial_value_; } void Update(ll k, pair<double, double> a) { // node[k]をaにする // それに従って先祖も変わっていく k += n; node[k] = a; while (k > 1) { k = k / 2; node[k] = pair<double, double>( node[k * 2].first * node[k * 2 + 1].first, node[k * 2].second * node[k * 2 + 1].first + node[k * 2 + 1].second); } } /*void Watch(){ for(ll i=0;i<2*n;i++){ cout<<node[i]<<endl; } }*/ double Query() { //[a,b) return node[1].first + node[1].second; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll k, n; ll m, d; ll ans = -9999999999999; string s, t; vector<ll> a; vector<ll> pluss, minuss; ll keta[1000011]; ll dp[1000011][2]; int main() { cin >> s; for (ll i = 0; i < s.size(); i++) { keta[i] = s[s.size() - 1 - i] - '0'; } dp[0][0] = keta[0]; dp[0][1] = 10 - keta[0] + 1; // cout<<dp[0][0]<<" "<<dp[0][1]<<endl; for (ll i = 1; i < s.size(); i++) { // cout<<dp[i-1][0]+keta[i]<<" "<<dp[i-1][1]+keta[i]<<endl; // cout<<dp[i-1][0]+(10-keta[i])+1<<" "<<dp[i-1][1]+(10-keta[i]+1)<<endl; dp[i][0] = lmin(dp[i - 1][0] + keta[i], dp[i - 1][1] + keta[i]); dp[i][1] = lmin(dp[i - 1][0] + (10 - keta[i]) + 1, dp[i - 1][1] + (10 - keta[i]) + 1 - 2); // cout<<dp[i][0]<<" "<<dp[i][1]<<endl; } cout << lmin(dp[s.size() - 1][0], dp[s.size() - 1][1]) << endl; }
replace
234
236
234
236
0
p02775
C++
Runtime Error
#include <bits/extc++.h> #include <bits/stdc++.h> using namespace std; #define _rep(n, a, b) for (ll n = (a); n <= (b); ++n) #define _rev(n, a, b) for (ll n = (a); n >= (b); --n) #define _for(n, a, b) for (ll n = (a); n < (b); ++n) #define _rof(n, a, b) for (ll n = (a); n > (b); --n) #define oo 0x3f3f3f3f3f3f #define ll long long #define db double #define eps 1e-6 #define bin(x) cout << bitset<10>(x) << endl; #define what_is(x) cerr << #x << " is " << x << endl #define met(a, b) memset(a, b, sizeof(a)) #define mp(a, b) make_pair(a, b) #define all(x) x.begin(), x.end() #define pii pair<ll, ll> #define pdd pair<db, db> #define pi acos(-1.0) const ll maxn = 2e5 + 10; ll dp[maxn][2]; signed main() { string s; cin >> s; dp[0][1] = 1; _for(i, 0, s.size()) { int x = s[i] - '0'; dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + 10 - x); dp[i + 1][1] = min(dp[i][0] + x + 1, dp[i][1] + 10 - x - 1); } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; }
#include <bits/extc++.h> #include <bits/stdc++.h> using namespace std; #define _rep(n, a, b) for (ll n = (a); n <= (b); ++n) #define _rev(n, a, b) for (ll n = (a); n >= (b); --n) #define _for(n, a, b) for (ll n = (a); n < (b); ++n) #define _rof(n, a, b) for (ll n = (a); n > (b); --n) #define oo 0x3f3f3f3f3f3f #define ll long long #define db double #define eps 1e-6 #define bin(x) cout << bitset<10>(x) << endl; #define what_is(x) cerr << #x << " is " << x << endl #define met(a, b) memset(a, b, sizeof(a)) #define mp(a, b) make_pair(a, b) #define all(x) x.begin(), x.end() #define pii pair<ll, ll> #define pdd pair<db, db> #define pi acos(-1.0) const ll maxn = 1e6 + 10; ll dp[maxn][2]; signed main() { string s; cin >> s; dp[0][1] = 1; _for(i, 0, s.size()) { int x = s[i] - '0'; dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + 10 - x); dp[i + 1][1] = min(dp[i][0] + x + 1, dp[i][1] + 10 - x - 1); } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; }
replace
20
21
20
21
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, m, n) for (int i = (int)m; i < (int)n; i++) #define ALL(vec) vec.begin(), vec.end() typedef long long ll; typedef long double ld; int main() { string N; cin >> N; ll n = N.size(); ll inc = 0; vector<ll> f(n), g(n); f[0] = 0; g[0] = 1; rep(i, n) { int a = N[i] - '0'; f[i + 1] = min(f[i] + a, g[i] + 10 - a); g[i + 1] = min(f[i] + a + 1, g[i] + 9 - a); } cout << f[n] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define rep2(i, m, n) for (int i = (int)m; i < (int)n; i++) #define ALL(vec) vec.begin(), vec.end() typedef long long ll; typedef long double ld; int main() { string N; cin >> N; ll n = N.size(); ll inc = 0; vector<ll> f(n + 1), g(n + 1); f[0] = 0; g[0] = 1; rep(i, n) { int a = N[i] - '0'; f[i + 1] = min(f[i] + a, g[i] + 10 - a); g[i + 1] = min(f[i] + a + 1, g[i] + 9 - a); } cout << f[n] << endl; }
replace
13
14
13
14
0
p02775
C++
Time Limit Exceeded
#include <iostream> #include <map> #include <string> #include <list> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> #include <cinttypes> #include <cmath> #include <cstdlib> using namespace std; int main() { string n; cin >> n; typedef pair<int, int> answer; vector<answer> que; que.push_back(answer(0, 0)); vector<answer> nextQue; for (int index = n.size() - 1; index >= 0; --index) { int d = n[index] - '0'; while (!que.empty()) { answer ans = que.back(); que.pop_back(); int c = d + ans.second; int nextCarry = 0; if (c >= 10) { c -= 10; ++nextCarry; } if (c <= 5) nextQue.push_back(answer(ans.first + c, nextCarry)); if (c >= 5) nextQue.push_back(answer(ans.first + 10 - c, nextCarry + 1)); } que.swap(nextQue); nextQue.clear(); sort(que.begin(), que.end()); int min = que[0].first + que[1].second; for (int i = 1; i < que.size(); ++i) { if (min + 2 <= que[i].first + que[i].second) que.erase(que.begin() + i); } } int ans = 1 << 30; for (auto a : que) { ans = min(a.first + a.second, ans); } cout << ans << endl; return 0; }
#include <iostream> #include <map> #include <string> #include <list> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <algorithm> #include <cinttypes> #include <cmath> #include <cstdlib> using namespace std; int main() { string n; cin >> n; typedef pair<int, int> answer; vector<answer> que; que.push_back(answer(0, 0)); vector<answer> nextQue; for (int index = n.size() - 1; index >= 0; --index) { int d = n[index] - '0'; while (!que.empty()) { answer ans = que.back(); que.pop_back(); int c = d + ans.second; int nextCarry = 0; if (c >= 10) { c -= 10; ++nextCarry; } if (c <= 5) nextQue.push_back(answer(ans.first + c, nextCarry)); if (c >= 5) nextQue.push_back(answer(ans.first + 10 - c, nextCarry + 1)); } que.swap(nextQue); nextQue.clear(); sort(que.begin(), que.end()); que.erase(unique(que.begin(), que.end()), que.end()); int min = que[0].first + que[1].second; for (int i = 1; i < que.size(); ++i) { if (min + 2 <= que[i].first + que[i].second) que.erase(que.begin() + i); } } int ans = 1 << 30; for (auto a : que) { ans = min(a.first + a.second, ans); } cout << ans << endl; return 0; }
insert
56
56
56
57
TLE
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef unsigned long long int uli; typedef long double Lf; typedef pair<int, int> pii; typedef vector<int> vi; #define fastIO \ std::ios::sync_with_stdio(false); \ cin.tie(NULL) #define mod 1000000007 #define N 100005 #define NN 25 #define SZ(x) ((lli)(x).size()) #define loop(i, s, n) for (int(i) = (s); (i) < (n); (i)++) #define loopr(i, n, s) for (int(i) = (n)-1; (i) >= (s); (i)--) #define pb push_back #define o2(a, b) cout << (a) << " " << (b) << endl #define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define o4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define cl cout << endl #define r0 return 0 #define x first #define y second inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; } inline lli modsub(lli n, lli m, lli p = mod) { return ((n - m + p) % p + p) % p; } inline lli modpro(lli n, lli m, lli p = mod) { return (((n % p) * (m % p)) % p + p) % p; } uli powe(lli x, lli y) { uli res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } lli modpow(lli x, lli y, lli p = mod) { lli res = 1; while (y > 0) { if (y & 1) res = modpro(res, x, p); y = y >> 1; x = modpro(x, x, p); } return res; } inline lli modInverse(lli n, lli p = mod) { if (n == 1) return 1; return modpow(n, p - 2, p); } inline lli moddiv(lli n, lli m, lli p = mod) { return modpro(n, modInverse(m, p), p); } inline lli modadd3(lli x, lli y, lli z, lli p = mod) { return modadd(modadd(x, y, p), z, p); } inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) { return modadd(modadd(x, y, p), modadd(z, w, p), p); } inline lli modnCr(lli fac[], int n, int r, lli p = mod) { if (r == 0) return 1; return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p); } template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); } template <typename T> inline T max4(T x, T y, T z, T w) { return max(max3(x, y, w), z); } template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); } template <typename T> inline T min4(T x, T y, T z, T w) { return min(min3(x, y, w), z); } template <typename T> void printArr(T *arr, int s, int n) { for (int i = s; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } // template<class X, class Y, class Z> // struct triple { // X x; // Y y; // Z z; // // friend bool operator<(triple a, triple b) // { // if(a.x!=b.x) return a.x<b.x; // else if(a.y!=b.y) return a.y<b.y; // else return a.z<=b.z; // } // }; // // // template<class X, class Y, class Z, class W> // class quad { // public: // X x; // Y y; // Z z; // W w; // // friend bool operator<(quad a, quad b) // { // if(a.x!=b.x) return a.x<b.x; // else if(a.y!=b.y) return a.y<b.y; // else if(a.z!=b.z)return a.z<b.z; // else return a.w<b.w; // } // }; // template <typename T> // T gcd(T a, T b) //{ // if (a == 0) // return b; // if(b==0) // return a; // T t; // while((a>0)&&(b>0)){ // t = a; // a=b%a; // b=t; // } // // return max(a,b); // } // // uli choose(uli n, uli k){ // uli res = 1; // // // Since C(n, k) = C(n, n-k) // if ( k > n - k ) // k = n - k; // // // Calculate value of // // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] // for (uli i = 0; i < k; ++i) // { // res *= (n - i); // res /= (i + 1); // } // // return res; //} // // // vector<vector<int>> adj; ////vector<bool> visited; // vector<int> pa; ////queue<int> q; // // class Graph{ // // public: // int V; // // // // Graph(int V) //{ // this->V = V; // // adj.resize(V); // pa.resize(V); //} // // void addEdge(int a, int b) //{ // adj[a].pb(b); // adj[b].pb(a); //} // ////void dfs(int x){ //// visited[x]=1; //// v.pb(x); //// v2.pb(a[x]); //// for(auto u: adj[x]){ //// if(visited[u]) continue; //// dfs(u); //// } ////} // // // // // // void dfsTree(int x, int p){ // // pa[x]=p; // for(auto u: adj[x]){ // if(u==p) {continue;} // dfsTree(u,x); // } // //} // // // ////void bfs(int x){ //// visited[x]=1; //// q.push(x); //// dist[x]=0; //// v.pb(x); //// while(!q.empty()){ //// int s = q.front(); //// q.pop(); //// for(int u : adj[s]){ //// if(visited[u]==1) continue; //// visited[u]=1; //// dist[u]=dist[s]+1; //// v.pb(u); //// //cout<<u<<endl; //// q.push(u); //// } //// } ////} // ////int connectedcomponents(int n){ //// int ans=0; //// loop(i,1,n+1){ //// if(!visited[i]){ //// ans++; //// dfs(i); //// } //// //// } //// return ans; ////} // //}; // template <typename T> // T findpowerfactorial(T n,T p) //{ // T x = 0; // while (n) // { // n /= p; // x += n; // } // return x; // } // // template <typename T> // int getibit(T n, int i){ // //cout<<(n&(1LL<<i))<<endl; // return (n&(1LL<<i))?1:0; // } // // template <typename T> // int findbits(T n, T p){ // int x=0; // while(n>0){ // n/=p; // x++; // } // return x; // } // // //// // vector<pair<lli,int>> v; // void primeFactors(lli n) //{ // // int c=0; // while (n % 2 == 0) // { // n = n/2; // c++; // } // if(c>0) v.pb({2,c}); // // // for (lli i = 3; i*i <= n; i = i + 2) // { c=0; // //if(n%i==0) {v.pb(i);} // while (n % i == 0) // { // n = n/i; // c++; // } // if(c>0)v.pb({i,c}); // // } // if (n > 2) // v.pb({n,1}); // } // lli fac_[N]; // void fac_init(int n){ // fac_[0]=1; // loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i); // } // /*BITMASK for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) { for ( int pos = 0; pos < n ; ++pos ) { if ( inum & ( 1 << pos ) ){ results[inum] += s [pos] ; //DO SOMETHING } } } */ int a[N]; lli dp[N][2]; int main() { fastIO; int erer = 1; // cin>>erer; loop(erer2, 1, erer + 1) { string s; cin >> s; int n = SZ(s); loop(i, 0, SZ(s)) a[i] = (int)(s[i] - '0'); dp[0][0] = min(a[0], (10 - a[0]) + 1); if (a[0] + 1 == 10) { dp[0][1] = 1; } else { dp[0][1] = min(a[0] + 1, (10 - a[0])); } loop(i, 1, n) { dp[i][0] = min(dp[i - 1][0] + a[i], dp[i - 1][1] + (10 - a[i])); if (a[i] + 1 == 10) { dp[i][1] = dp[i - 1][1]; } else { dp[i][1] = min(dp[i - 1][0] + a[i] + 1, dp[i - 1][1] + (10 - a[i] - 1)); } } cout << dp[n - 1][0] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef unsigned long long int uli; typedef long double Lf; typedef pair<int, int> pii; typedef vector<int> vi; #define fastIO \ std::ios::sync_with_stdio(false); \ cin.tie(NULL) #define mod 1000000007 #define N 10000005 #define NN 25 #define SZ(x) ((lli)(x).size()) #define loop(i, s, n) for (int(i) = (s); (i) < (n); (i)++) #define loopr(i, n, s) for (int(i) = (n)-1; (i) >= (s); (i)--) #define pb push_back #define o2(a, b) cout << (a) << " " << (b) << endl #define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define o4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define cl cout << endl #define r0 return 0 #define x first #define y second inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; } inline lli modsub(lli n, lli m, lli p = mod) { return ((n - m + p) % p + p) % p; } inline lli modpro(lli n, lli m, lli p = mod) { return (((n % p) * (m % p)) % p + p) % p; } uli powe(lli x, lli y) { uli res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } lli modpow(lli x, lli y, lli p = mod) { lli res = 1; while (y > 0) { if (y & 1) res = modpro(res, x, p); y = y >> 1; x = modpro(x, x, p); } return res; } inline lli modInverse(lli n, lli p = mod) { if (n == 1) return 1; return modpow(n, p - 2, p); } inline lli moddiv(lli n, lli m, lli p = mod) { return modpro(n, modInverse(m, p), p); } inline lli modadd3(lli x, lli y, lli z, lli p = mod) { return modadd(modadd(x, y, p), z, p); } inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) { return modadd(modadd(x, y, p), modadd(z, w, p), p); } inline lli modnCr(lli fac[], int n, int r, lli p = mod) { if (r == 0) return 1; return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p); } template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); } template <typename T> inline T max4(T x, T y, T z, T w) { return max(max3(x, y, w), z); } template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); } template <typename T> inline T min4(T x, T y, T z, T w) { return min(min3(x, y, w), z); } template <typename T> void printArr(T *arr, int s, int n) { for (int i = s; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } // template<class X, class Y, class Z> // struct triple { // X x; // Y y; // Z z; // // friend bool operator<(triple a, triple b) // { // if(a.x!=b.x) return a.x<b.x; // else if(a.y!=b.y) return a.y<b.y; // else return a.z<=b.z; // } // }; // // // template<class X, class Y, class Z, class W> // class quad { // public: // X x; // Y y; // Z z; // W w; // // friend bool operator<(quad a, quad b) // { // if(a.x!=b.x) return a.x<b.x; // else if(a.y!=b.y) return a.y<b.y; // else if(a.z!=b.z)return a.z<b.z; // else return a.w<b.w; // } // }; // template <typename T> // T gcd(T a, T b) //{ // if (a == 0) // return b; // if(b==0) // return a; // T t; // while((a>0)&&(b>0)){ // t = a; // a=b%a; // b=t; // } // // return max(a,b); // } // // uli choose(uli n, uli k){ // uli res = 1; // // // Since C(n, k) = C(n, n-k) // if ( k > n - k ) // k = n - k; // // // Calculate value of // // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] // for (uli i = 0; i < k; ++i) // { // res *= (n - i); // res /= (i + 1); // } // // return res; //} // // // vector<vector<int>> adj; ////vector<bool> visited; // vector<int> pa; ////queue<int> q; // // class Graph{ // // public: // int V; // // // // Graph(int V) //{ // this->V = V; // // adj.resize(V); // pa.resize(V); //} // // void addEdge(int a, int b) //{ // adj[a].pb(b); // adj[b].pb(a); //} // ////void dfs(int x){ //// visited[x]=1; //// v.pb(x); //// v2.pb(a[x]); //// for(auto u: adj[x]){ //// if(visited[u]) continue; //// dfs(u); //// } ////} // // // // // // void dfsTree(int x, int p){ // // pa[x]=p; // for(auto u: adj[x]){ // if(u==p) {continue;} // dfsTree(u,x); // } // //} // // // ////void bfs(int x){ //// visited[x]=1; //// q.push(x); //// dist[x]=0; //// v.pb(x); //// while(!q.empty()){ //// int s = q.front(); //// q.pop(); //// for(int u : adj[s]){ //// if(visited[u]==1) continue; //// visited[u]=1; //// dist[u]=dist[s]+1; //// v.pb(u); //// //cout<<u<<endl; //// q.push(u); //// } //// } ////} // ////int connectedcomponents(int n){ //// int ans=0; //// loop(i,1,n+1){ //// if(!visited[i]){ //// ans++; //// dfs(i); //// } //// //// } //// return ans; ////} // //}; // template <typename T> // T findpowerfactorial(T n,T p) //{ // T x = 0; // while (n) // { // n /= p; // x += n; // } // return x; // } // // template <typename T> // int getibit(T n, int i){ // //cout<<(n&(1LL<<i))<<endl; // return (n&(1LL<<i))?1:0; // } // // template <typename T> // int findbits(T n, T p){ // int x=0; // while(n>0){ // n/=p; // x++; // } // return x; // } // // //// // vector<pair<lli,int>> v; // void primeFactors(lli n) //{ // // int c=0; // while (n % 2 == 0) // { // n = n/2; // c++; // } // if(c>0) v.pb({2,c}); // // // for (lli i = 3; i*i <= n; i = i + 2) // { c=0; // //if(n%i==0) {v.pb(i);} // while (n % i == 0) // { // n = n/i; // c++; // } // if(c>0)v.pb({i,c}); // // } // if (n > 2) // v.pb({n,1}); // } // lli fac_[N]; // void fac_init(int n){ // fac_[0]=1; // loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i); // } // /*BITMASK for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) { for ( int pos = 0; pos < n ; ++pos ) { if ( inum & ( 1 << pos ) ){ results[inum] += s [pos] ; //DO SOMETHING } } } */ int a[N]; lli dp[N][2]; int main() { fastIO; int erer = 1; // cin>>erer; loop(erer2, 1, erer + 1) { string s; cin >> s; int n = SZ(s); loop(i, 0, SZ(s)) a[i] = (int)(s[i] - '0'); dp[0][0] = min(a[0], (10 - a[0]) + 1); if (a[0] + 1 == 10) { dp[0][1] = 1; } else { dp[0][1] = min(a[0] + 1, (10 - a[0])); } loop(i, 1, n) { dp[i][0] = min(dp[i - 1][0] + a[i], dp[i - 1][1] + (10 - a[i])); if (a[i] + 1 == 10) { dp[i][1] = dp[i - 1][1]; } else { dp[i][1] = min(dp[i - 1][0] + a[i] + 1, dp[i - 1][1] + (10 - a[i] - 1)); } } cout << dp[n - 1][0] << endl; } return 0; }
replace
11
12
11
12
0
p02775
C++
Runtime Error
#include <memory.h> #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; #define MOD 998244353 #define INIT -1 int main() { string n; cin >> n; // result[i][0] : i - 1桁めまで払ってる 繰り下がりはない // result[i][1] : i - 1桁目まで払ってる 繰り下がりあり int result[1000001][2] = {}; result[0][0] = 0; result[0][1] = 1; for (int i = 1; i <= n.length(); i++) { result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'), result[i - 1][1] + (10 - (n[i - 1] - '0'))); result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1), result[i - 1][0] + (n[i - 1] - '0' + 1)); } cout << result[n.length()][0] << endl; }
#include <memory.h> #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; #define MOD 998244353 #define INIT -1 int main() { string n; cin >> n; // result[i][0] : i - 1桁めまで払ってる 繰り下がりはない // result[i][1] : i - 1桁目まで払ってる 繰り下がりあり int result[1000010][2] = {}; result[0][0] = 0; result[0][1] = 1; for (int i = 1; i <= n.length(); i++) { result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'), result[i - 1][1] + (10 - (n[i - 1] - '0'))); result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1), result[i - 1][0] + (n[i - 1] - '0' + 1)); } cout << result[n.length()][0] << endl; }
replace
31
32
31
32
0
p02775
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll, ll> P; typedef vector<P> vpl; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 510000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; vvl dp(100005, vl(2, linf)); int main() { string s; cin >> s; ll n = s.size(); reverse(all(s)); dp[0][0] = 0; dp[0][1] = 1; rep(i, n) { ll t = s[i] - '0'; chmin(dp[i + 1][0], dp[i][0] + t); chmin(dp[i + 1][1], dp[i][0] + 10 - t); chmin(dp[i + 1][0], dp[i][1] + t + 1); chmin(dp[i + 1][1], dp[i][1] + 10 - t - 1); } // print2(dp[n][0],dp[n][1]); cout << min(dp[n][0], dp[n][1] + 1) << endl; // print2(dp[1][0],dp[1][1]); }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll, ll> P; typedef vector<P> vpl; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 510000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; vvl dp(1000005, vl(2, linf)); int main() { string s; cin >> s; ll n = s.size(); reverse(all(s)); dp[0][0] = 0; dp[0][1] = 1; rep(i, n) { ll t = s[i] - '0'; chmin(dp[i + 1][0], dp[i][0] + t); chmin(dp[i + 1][1], dp[i][0] + 10 - t); chmin(dp[i + 1][0], dp[i][1] + t + 1); chmin(dp[i + 1][1], dp[i][1] + 10 - t - 1); } // print2(dp[n][0],dp[n][1]); cout << min(dp[n][0], dp[n][1] + 1) << endl; // print2(dp[1][0],dp[1][1]); }
replace
56
57
56
57
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; string s; const int N = 1e5; int dp[2][N + 5]; int calc(int st, int pos) { if (pos == -1) return st; int &res = dp[st][pos]; if (res != -1) return res; res = 1e9; int val = s[pos] - '0'; if (st == 0) { res = min(calc(0, pos - 1) + val, calc(1, pos - 1) + 10 - val); } else { int nval = val + 1; if (nval != 10) res = min(calc(0, pos - 1) + nval, calc(1, pos - 1) + 10 - nval); else res = calc(1, pos - 1) + 10 - nval; } return res; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> s; memset(dp, -1, sizeof dp); cout << calc(0, s.size() - 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; string s; const int N = 1e6; int dp[2][N + 5]; int calc(int st, int pos) { if (pos == -1) return st; int &res = dp[st][pos]; if (res != -1) return res; res = 1e9; int val = s[pos] - '0'; if (st == 0) { res = min(calc(0, pos - 1) + val, calc(1, pos - 1) + 10 - val); } else { int nval = val + 1; if (nval != 10) res = min(calc(0, pos - 1) + nval, calc(1, pos - 1) + 10 - nval); else res = calc(1, pos - 1) + 10 - nval; } return res; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> s; memset(dp, -1, sizeof dp); cout << calc(0, s.size() - 1) << endl; return 0; }
replace
7
8
7
8
0
p02775
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int64_t min(int64_t a, int64_t b) { if (a > b) { return b; } else { return a; } } int main() { string str; cin >> str; while (1) { if (str.at(str.size() - 1) == '0') { str.pop_back(); } else { break; } } if (str.size() == 1) { int a = str.at(0) - '0'; cout << min(a, 11 - a) << endl; } vector<int> n(str.size()); for (size_t i = 0; i < n.size(); i++) { n.at(i) = str.at(i) - '0'; } vector<int> m(str.size()); for (size_t i = 0; i < m.size() - 1; i++) { m.at(i) = 9 - n.at(i); } m.at(m.size() - 1) = 10 - n.at(m.size() - 1); int64_t ans = 0; vector<int> s1(str.size()); vector<int> s2(str.size()); s1.at(n.size() - 1) = n.at(n.size() - 1); s2.at(m.size() - 1) = m.at(m.size() - 1); for (size_t i = n.size() - 2; 1; i--) { s1.at(i) = min(s1.at(i + 1), s2.at(i + 1) + 1) + n.at(i); s2.at(i) = min(s2.at(i + 1), s1.at(i + 1) + 1) + m.at(i); if (i == 0) { break; } } ans = min(s1.at(0), s2.at(0) + 1); cout << ans << endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> #include <vector> using namespace std; int64_t min(int64_t a, int64_t b) { if (a > b) { return b; } else { return a; } } int main() { string str; cin >> str; while (1) { if (str.at(str.size() - 1) == '0') { str.pop_back(); } else { break; } } if (str.size() == 1) { int a = str.at(0) - '0'; cout << min(a, 11 - a) << endl; return 0; } vector<int> n(str.size()); for (size_t i = 0; i < n.size(); i++) { n.at(i) = str.at(i) - '0'; } vector<int> m(str.size()); for (size_t i = 0; i < m.size() - 1; i++) { m.at(i) = 9 - n.at(i); } m.at(m.size() - 1) = 10 - n.at(m.size() - 1); int64_t ans = 0; vector<int> s1(str.size()); vector<int> s2(str.size()); s1.at(n.size() - 1) = n.at(n.size() - 1); s2.at(m.size() - 1) = m.at(m.size() - 1); for (size_t i = n.size() - 2; 1; i--) { s1.at(i) = min(s1.at(i + 1), s2.at(i + 1) + 1) + n.at(i); s2.at(i) = min(s2.at(i + 1), s1.at(i + 1) + 1) + m.at(i); if (i == 0) { break; } } ans = min(s1.at(0), s2.at(0) + 1); cout << ans << endl; }
insert
29
29
29
30
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < N; i++) #define rep2(i, N) for (int i = 1; i <= N; i++) using namespace std; long long INF = 1e18; long long mod = 1e9 + 7; long long dp[100010][2]; // status unsolved long long solve(string s) { fill((long long *)dp, (long long *)dp + sizeof(dp) / sizeof(long long), INF); int L = s.size(); // dp[i][k]:=i桁目において、状態kになるための最小枚数 // i:=桁数  // k=0 繰り上下がりなし  // k=1 繰り下がりあり dp[0][0] = 0; for (int i = 0; i < L; i++) { // 繰り下がり for (int j = 0; j < 2; j++) { int D = s[i] - '0'; D += j; for (int d = 0; d < 10; d++) { int b = d - D; int nj = 0; if (b < 0) { nj = 1; b += 10; } // 次の桁の繰り下がり状態nj //=min(現在の値,今の桁の状態j+お客が払うコイン+お店が払うコイン) dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + b + d); } } } return dp[L][0]; } int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; cout << solve(s) << endl; ; return 0; } /* */
#include <bits/stdc++.h> #define rep(i, N) for (int i = 0; i < N; i++) #define rep2(i, N) for (int i = 1; i <= N; i++) using namespace std; long long INF = 1e18; long long mod = 1e9 + 7; long long dp[1000010][2]; // status unsolved long long solve(string s) { fill((long long *)dp, (long long *)dp + sizeof(dp) / sizeof(long long), INF); int L = s.size(); // dp[i][k]:=i桁目において、状態kになるための最小枚数 // i:=桁数  // k=0 繰り上下がりなし  // k=1 繰り下がりあり dp[0][0] = 0; for (int i = 0; i < L; i++) { // 繰り下がり for (int j = 0; j < 2; j++) { int D = s[i] - '0'; D += j; for (int d = 0; d < 10; d++) { int b = d - D; int nj = 0; if (b < 0) { nj = 1; b += 10; } // 次の桁の繰り下がり状態nj //=min(現在の値,今の桁の状態j+お客が払うコイン+お店が払うコイン) dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + b + d); } } } return dp[L][0]; } int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; cout << solve(s) << endl; ; return 0; } /* */
replace
7
8
7
8
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 2e5 + 5; string s; int dp[N][2]; int solve(int idx, int c) { if (idx < 0) return c; int &ret = dp[idx][c]; if (~ret) return ret; int cur = s[idx] - '0' + c; ret = solve(idx - 1, 0) + cur; ret = min(ret, solve(idx - 1, 1) + 10 - cur); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; memset(dp, -1, sizeof dp); cout << solve((int)s.size() - 1, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 2e6 + 5; string s; int dp[N][2]; int solve(int idx, int c) { if (idx < 0) return c; int &ret = dp[idx][c]; if (~ret) return ret; int cur = s[idx] - '0' + c; ret = solve(idx - 1, 0) + cur; ret = min(ret, solve(idx - 1, 1) + 10 - cur); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; memset(dp, -1, sizeof dp); cout << solve((int)s.size() - 1, 0) << endl; return 0; }
replace
4
5
4
5
0
p02775
C++
Runtime Error
// GYM problem J ArabellaCPC 2019 #include <bits/stdc++.h> #include <set> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define pb push_back #define ff first #define ss second #define sz size() #define mp make_pair #define int long long int #define double long double #define bp __builtin_popcountll #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROR(i, a, b) for (int i = a; i >= b; i--) const int M = 1e9 + 7; const int N = 1e5 + 5; const int inf = 1e18; string s; int n; int dp[N][2]; int solve(int idx, int vaddi) { if (idx == 0) { if (vaddi == 1) return 1; return 0; } int &ans = dp[idx][vaddi]; if (ans != -1) return ans; ans = 0; char pp; if (vaddi == 1) { pp = s[idx] + 1; if (s[idx] + 1 > '9') ans = solve(idx - 1, 1); else ans = min((pp - '0') + solve(idx - 1, 0), (10 + '0' - pp) + solve(idx - 1, 1)); } else ans = min((s[idx] - '0') + solve(idx - 1, 0), (10 + '0' - s[idx]) + solve(idx - 1, 1)); return ans; } int32_t main() { FAST cin >> s; s = '0' + s; n = s.size(); // cout<<s; memset(dp, -1, sizeof(dp)); cout << (solve(n, 0) + 48); }
// GYM problem J ArabellaCPC 2019 #include <bits/stdc++.h> #include <set> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define pb push_back #define ff first #define ss second #define sz size() #define mp make_pair #define int long long int #define double long double #define bp __builtin_popcountll #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROR(i, a, b) for (int i = a; i >= b; i--) const int M = 1e9 + 7; const int N = 1e6 + 5; const int inf = 1e18; string s; int n; int dp[N][2]; int solve(int idx, int vaddi) { if (idx == 0) { if (vaddi == 1) return 1; return 0; } int &ans = dp[idx][vaddi]; if (ans != -1) return ans; ans = 0; char pp; if (vaddi == 1) { pp = s[idx] + 1; if (s[idx] + 1 > '9') ans = solve(idx - 1, 1); else ans = min((pp - '0') + solve(idx - 1, 0), (10 + '0' - pp) + solve(idx - 1, 1)); } else ans = min((s[idx] - '0') + solve(idx - 1, 0), (10 + '0' - s[idx]) + solve(idx - 1, 1)); return ans; } int32_t main() { FAST cin >> s; s = '0' + s; n = s.size(); // cout<<s; memset(dp, -1, sizeof(dp)); cout << (solve(n, 0) + 48); }
replace
23
24
23
24
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) using P = pair<int, int>; const int INF = 1000100100; /* 大文字を小文字に変換 */ char tolower(char c) { return (c + 0x20); } /* 小文字を大文字に変換 */ char toupr(char c) { return (c - 0x20); } // if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A'; /* string s = "abcdefg" s.substr(4) "efg" s.substr(0,3) "abc" s.substr(2,4) "cdef" // イテレータ要素のインデックス distance(A.begin(), itr); */ int dp[100005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; // 桁が1つだけ上る可能性があるので int n = s.size(); rep(i, n + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; // 今の桁、nのi桁目 x += j; // 繰り下がりを引く数をプラス1することで対応している rep(a, 10) { // ni:次の桁に行くだけなのでi+1 // nj:繰り下がりが遷移先で起こる場合1、起こらない場合0、デフォルトは0 int ni = i + 1, nj = 0; // 次の遷移先 int b = a - x; if (b < 0) { nj = 1; // 繰り下がりを1にして、次の桁で繰り下がりが起こるようにする b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } int ans = dp[n][0]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) using P = pair<int, int>; const int INF = 1000100100; /* 大文字を小文字に変換 */ char tolower(char c) { return (c + 0x20); } /* 小文字を大文字に変換 */ char toupr(char c) { return (c - 0x20); } // if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A'; /* string s = "abcdefg" s.substr(4) "efg" s.substr(0,3) "abc" s.substr(2,4) "cdef" // イテレータ要素のインデックス distance(A.begin(), itr); */ int dp[1000005][2]; int main() { string s; cin >> s; reverse(s.begin(), s.end()); s += '0'; // 桁が1つだけ上る可能性があるので int n = s.size(); rep(i, n + 1) rep(j, 2) dp[i][j] = INF; dp[0][0] = 0; rep(i, n) rep(j, 2) { int x = s[i] - '0'; // 今の桁、nのi桁目 x += j; // 繰り下がりを引く数をプラス1することで対応している rep(a, 10) { // ni:次の桁に行くだけなのでi+1 // nj:繰り下がりが遷移先で起こる場合1、起こらない場合0、デフォルトは0 int ni = i + 1, nj = 0; // 次の遷移先 int b = a - x; if (b < 0) { nj = 1; // 繰り下がりを1にして、次の桁で繰り下がりが起こるようにする b += 10; } dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b); } } int ans = dp[n][0]; cout << ans << endl; return 0; }
replace
25
26
25
26
0
p02775
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define all(x) x.begin(), x.end() #define fi first #define se second #define pb push_back #define umax(x, y) x = max(x, (y)) #define umin(x, y) x = min(x, (y)) #define set multiset #define null NULL #define ort (b + s) / 2 using namespace std; typedef long long Lint; typedef long double db; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef pair<db, db> dd; typedef pair<int, string> is; const int maxn = 600020; const int K = 320; const int MOd = 1e9 + 7; Lint ar[maxn], a, k; vector<Lint> v; Lint f(Lint x) { Lint ans = 0; for (int i = 1; i <= a; i++) { if (ar[i] == 0) { if (0 <= x) ans += a; } else if (ar[i] > 0) { Lint h = x / ar[i]; // -3/4 || -4/4 if (x < 0 && x % ar[i] != 0) h--; int t = upper_bound(all(v), h) - v.begin(); ans += t; // printf("asdasd %lld\n",h); } else { // 4, -3 Lint h = x / ar[i]; // -3, -2 if (x < 0 && (-x) % (-ar[i]) != 0) h++; int t = v.end() - lower_bound(all(v), h); // printf("asd %lld\n",h); ans += t; } if (ar[i] * ar[i] <= x) ans--; // printf("%d --> %d (%lld)\n",i,ans,ar[i]); } // printf("x=%lld --> ans=%lld\n",x,ans); return ans / 2; } int dn[maxn][2]; void solve() { string s; cin >> s; dn[s.size()][1] = 1; for (int i = s.size() - 1; i >= 0; i--) { s[i] -= '0'; dn[i][0] = min(dn[i + 1][1] + 1 + s[i], dn[i + 1][0] + s[i]); dn[i][1] = min(dn[i + 1][1] + 10 - s[i] - 1, dn[i + 1][0] + 10 - s[i]); // printf("asd %d %d\n",dn[i][0],dn[i][1]); } cout << min(dn[0][0], dn[0][1] + 1) << endl; } int main() { int n = 1; // scanf("%d",&n); while (n--) solve(); return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define all(x) x.begin(), x.end() #define fi first #define se second #define pb push_back #define umax(x, y) x = max(x, (y)) #define umin(x, y) x = min(x, (y)) #define set multiset #define null NULL #define ort (b + s) / 2 using namespace std; typedef long long Lint; typedef long double db; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef pair<db, db> dd; typedef pair<int, string> is; const int maxn = 1000020; const int K = 320; const int MOd = 1e9 + 7; Lint ar[maxn], a, k; vector<Lint> v; Lint f(Lint x) { Lint ans = 0; for (int i = 1; i <= a; i++) { if (ar[i] == 0) { if (0 <= x) ans += a; } else if (ar[i] > 0) { Lint h = x / ar[i]; // -3/4 || -4/4 if (x < 0 && x % ar[i] != 0) h--; int t = upper_bound(all(v), h) - v.begin(); ans += t; // printf("asdasd %lld\n",h); } else { // 4, -3 Lint h = x / ar[i]; // -3, -2 if (x < 0 && (-x) % (-ar[i]) != 0) h++; int t = v.end() - lower_bound(all(v), h); // printf("asd %lld\n",h); ans += t; } if (ar[i] * ar[i] <= x) ans--; // printf("%d --> %d (%lld)\n",i,ans,ar[i]); } // printf("x=%lld --> ans=%lld\n",x,ans); return ans / 2; } int dn[maxn][2]; void solve() { string s; cin >> s; dn[s.size()][1] = 1; for (int i = s.size() - 1; i >= 0; i--) { s[i] -= '0'; dn[i][0] = min(dn[i + 1][1] + 1 + s[i], dn[i + 1][0] + s[i]); dn[i][1] = min(dn[i + 1][1] + 10 - s[i] - 1, dn[i + 1][0] + 10 - s[i]); // printf("asd %d %d\n",dn[i][0],dn[i][1]); } cout << min(dn[0][0], dn[0][1] + 1) << endl; } int main() { int n = 1; // scanf("%d",&n); while (n--) solve(); return 0; }
replace
33
34
33
34
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, o, n) for (long long i = o; i < n; i++) #define oneforall \ ios::sync_with_stdio(false); \ cin.tie(0); #define all(v) (v).begin(), (v).end() #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } typedef vector<int> vi; typedef vector<long long> vl; typedef long long ll; typedef vector<pair<long, long>> vpll; typedef vector<pair<int, int>> vpii; int main() { oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall int dp[2][10000]; ins(a); dp[0][0] = a[0] - '0'; dp[1][0] = 11 - (a[0] - '0'); FOR(i, 1, a.size()) { dp[0][i] = min(dp[0][i - 1], dp[1][i - 1]) + a[i] - '0'; dp[1][i] = min(dp[0][i - 1] + 11 - (a[i] - '0'), dp[1][i - 1] + 9 - (a[i] - '0')); } out(min(dp[0][a.size() - 1], dp[1][a.size() - 1])); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, o, n) for (long long i = o; i < n; i++) #define oneforall \ ios::sync_with_stdio(false); \ cin.tie(0); #define all(v) (v).begin(), (v).end() #define ini(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define inl(...) \ long long __VA_ARGS__; \ in(__VA_ARGS__) #define ins(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) void in() {} template <typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template <typename T, class... U> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; out(u...); } typedef vector<int> vi; typedef vector<long long> vl; typedef long long ll; typedef vector<pair<long, long>> vpll; typedef vector<pair<int, int>> vpii; int main() { oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall oneforall int dp[2][10000010]; ins(a); dp[0][0] = a[0] - '0'; dp[1][0] = 11 - (a[0] - '0'); FOR(i, 1, a.size()) { dp[0][i] = min(dp[0][i - 1], dp[1][i - 1]) + a[i] - '0'; dp[1][i] = min(dp[0][i - 1] + 11 - (a[i] - '0'), dp[1][i - 1] + 9 - (a[i] - '0')); } out(min(dp[0][a.size() - 1], dp[1][a.size() - 1])); return 0; }
replace
36
37
36
37
0
p02775
C++
Runtime Error
#pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #include <algorithm> #include <assert.h> #include <bitset> #include <cfloat> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define int long long #define ll long long #define mod (int)1000000007 #define P pair<int, int> #define prique(T) priority_queue<T, vector<T>, greater<T>> #define all(V) V.begin(), V.end() #ifdef int constexpr int INF = LLONG_MAX / 5; #else constexpr int INF = INT_MAX / 10; #endif constexpr double eps = DBL_EPSILON; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } using namespace std; inline int gcd(int a, int b) { while (b) { int c = a; a = b; b = c % b; } return a; } inline int lcm(int a, int b) { return a / gcd(a, b) * b; } bool isprime(int n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int mypow(int a, int b) { if (!b) return 1; if (b & 1) return mypow(a, b - 1) * a; int memo = mypow(a, b >> 1); return memo * memo; } int modpow(int a, int b, int m = mod) { if (!b) return 1; if (b & 1) return modpow(a, b - 1, m) * a % m; int memo = modpow(a, b >> 1, m); return memo * memo % m; } string s; int dp[1000000][2]; signed main() { cin >> s; reverse(all(s)); rep(i, s.size() + 1) { rep(j, 2) dp[i][j] = INF; } dp[0][0] = 0; rep(i, s.size()) { rep(j, 2) { if (j == 1 && s[i] == '9') { chmin(dp[i + 1][1], dp[i][j]); } int d = s[i] - '0'; d += j; chmin(dp[i + 1][0], dp[i][j] + d); chmin(dp[i + 1][1], dp[i][j] + (10 - d)); } } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; return 0; }
#pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #include <algorithm> #include <assert.h> #include <bitset> #include <cfloat> #include <complex> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define int long long #define ll long long #define mod (int)1000000007 #define P pair<int, int> #define prique(T) priority_queue<T, vector<T>, greater<T>> #define all(V) V.begin(), V.end() #ifdef int constexpr int INF = LLONG_MAX / 5; #else constexpr int INF = INT_MAX / 10; #endif constexpr double eps = DBL_EPSILON; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } using namespace std; inline int gcd(int a, int b) { while (b) { int c = a; a = b; b = c % b; } return a; } inline int lcm(int a, int b) { return a / gcd(a, b) * b; } bool isprime(int n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int mypow(int a, int b) { if (!b) return 1; if (b & 1) return mypow(a, b - 1) * a; int memo = mypow(a, b >> 1); return memo * memo; } int modpow(int a, int b, int m = mod) { if (!b) return 1; if (b & 1) return modpow(a, b - 1, m) * a % m; int memo = modpow(a, b >> 1, m); return memo * memo % m; } string s; int dp[1000010][2]; signed main() { cin >> s; reverse(all(s)); rep(i, s.size() + 1) { rep(j, 2) dp[i][j] = INF; } dp[0][0] = 0; rep(i, s.size()) { rep(j, 2) { if (j == 1 && s[i] == '9') { chmin(dp[i + 1][1], dp[i][j]); } int d = s[i] - '0'; d += j; chmin(dp[i + 1][0], dp[i][j] + d); chmin(dp[i + 1][1], dp[i][j] + (10 - d)); } } cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl; return 0; }
replace
91
92
91
92
0
p02775
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define pb push_back #define ff first #define ss second #define sz size() #define mp make_pair #define int long long int #define double long double #define bp __builtin_popcountll #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROR(i, a, b) for (int i = a; i >= b; i--) const int M = 1e9 + 7; const int N = 1e5 + 5; const int inf = 1e18; string s; int n; int dp[N][2]; int solve(int idx, int vaddi) { if (idx == 0) { if (vaddi == 1) return 1; return 0; } int &ans = dp[idx][vaddi]; if (ans != -1) return ans; ans = 0; char pp; if (vaddi == 1) { pp = s[idx] + 1; if (s[idx] + 1 > '9') ans = solve(idx - 1, 1); else ans = min((pp - '0') + solve(idx - 1, 0), (10 + '0' - pp) + solve(idx - 1, 1)); } else ans = min((s[idx] - '0') + solve(idx - 1, 0), (10 + '0' - s[idx]) + solve(idx - 1, 1)); return ans; } int32_t main() { FAST cin >> s; s = '0' + s; n = s.size(); // cout<<s; memset(dp, -1, sizeof(dp)); cout << (solve(n, 0) + 48); }
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #define pb push_back #define ff first #define ss second #define sz size() #define mp make_pair #define int long long int #define double long double #define bp __builtin_popcountll #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROR(i, a, b) for (int i = a; i >= b; i--) const int M = 1e9 + 7; const int N = 1e6 + 5; const int inf = 1e18; string s; int n; int dp[N][2]; int solve(int idx, int vaddi) { if (idx == 0) { if (vaddi == 1) return 1; return 0; } int &ans = dp[idx][vaddi]; if (ans != -1) return ans; ans = 0; char pp; if (vaddi == 1) { pp = s[idx] + 1; if (s[idx] + 1 > '9') ans = solve(idx - 1, 1); else ans = min((pp - '0') + solve(idx - 1, 0), (10 + '0' - pp) + solve(idx - 1, 1)); } else ans = min((s[idx] - '0') + solve(idx - 1, 0), (10 + '0' - s[idx]) + solve(idx - 1, 1)); return ans; } int32_t main() { FAST cin >> s; s = '0' + s; n = s.size(); // cout<<s; memset(dp, -1, sizeof(dp)); cout << (solve(n, 0) + 48); }
replace
15
16
15
16
0
p02775
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <unordered_map> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <complex> #include <iostream> #include <math.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, double> pld; typedef pair<double, double> pdd; typedef pair<double, ll> pdl; typedef pair<int, char> pic; typedef vector<ll> vl; typedef vector<int> vi; typedef complex<double> Point; typedef priority_queue<ll, vector<ll>, greater<ll>> llgreaterq; typedef priority_queue<pll, vector<pll>, greater<pll>> pllgreaterq; typedef priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> plpllgreaterq; typedef priority_queue<vi, vector<vi>, greater<vi>> vigreaterq; typedef priority_queue<vl, vector<vl>, greater<vl>> vlgreaterq; #define bit(x, v) ((ll)x << v) #define rep(x, v) for (int x = 0; x < v; x++) #define rep2(x, f, v) for (int x = f; x < v; x++) // 許容する誤差ε #define EPS (1e-10) // 2つのスカラーが等しいかどうか #define EQ(a, b) (abs((a) - (b)) < EPS) // 2つのベクトルが等しいかどうか #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) const ll INF = 1000000007; const int MAX = 2000010; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int pr[200010]; int lank[200010]; void uini(int n) { for (size_t i = 0; i <= n; i++) { pr[i] = i; } } int parent(int x) { if (x == pr[x]) return x; return pr[x] = parent(pr[x]); } int same(int x, int y) { return parent(x) == parent(y); } bool unit(int x, int y) { int px = parent(x); int py = parent(y); if (px == py) return false; if (lank[px] < lank[py]) { pr[py] = px; lank[px] += lank[py] + 1; } else { pr[px] = py; lank[py] += lank[px] + 1; } return true; } ll bit[200010]; int max_n = 200000; int pm = 0; void add(int x) { while (max_n >= x) { bit[x]++; x += x & -x; } } void sub(int x) { while (max_n >= x) { bit[x]--; x += x & -x; } } ll merge(ll *a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector<int> L(n1 + 1); vector<int> R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = INF; R[n2] = INF; ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll merge2(pair<int, char> *a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector<pair<int, char>> L(n1 + 1); vector<pair<int, char>> R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = make_pair(INF, ' '); R[n2] = make_pair(INF, ' '); ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i].first <= R[j].first) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll mergeSort2(pair<int, char> *a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort2(a, left, mid); res += mergeSort2(a, mid, right); res += merge2(a, left, mid, right); } return res; } ll mergeSort(ll *a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort(a, left, mid); res += mergeSort(a, mid, right); res += merge(a, left, mid, right); } return res; } int partition(pair<int, char> *a, int p, int r) { pair<int, char> x = a[r]; int i = p - 1; for (size_t j = p; j < r; j++) { if (a[j].first <= x.first) { i++; swap(a[i], a[j]); } } swap(a[i + 1], a[r]); return i + 1; } void quick(pair<int, char> *a, int p, int r) { if (p < r) { int q = partition(a, p, r); quick(a, p, q - 1); quick(a, q + 1, r); } } ll n; int ci = 0; ll P[1000010]; struct Node { int key; int priority; Node *parent, *left, *right; Node(int key, int priority); Node() {} }; Node NIL; Node::Node(int key, int priority) : key(key), priority(priority) { left = &NIL; right = &NIL; } Node *root = new Node(); void cenrec(Node *k) { if (k->key == NIL.key) return; cenrec(k->left); cout << " " << k->key; cenrec(k->right); } void fastrec(Node *k) { if (k->key == NIL.key) return; cout << " " << k->key; fastrec(k->left); fastrec(k->right); } void insert(Node *v) { Node *y = &NIL; Node *x = root; while (x->key != NIL.key) { y = x; if (v->key < x->key) { x = x->left; } else { x = x->right; } } v->parent = y; if (y->key == NIL.key) { root = v; } else if (v->key < y->key) { y->left = v; } else { y->right = v; } } Node *find(Node *k, ll v) { if (k->key == NIL.key) return &NIL; if (k->key == v) return k; if (v < k->key) return find(k->left, v); return find(k->right, v); } void delp12(Node *x) { if (x->key == NIL.key) return; Node *l = x->left; Node *r = x->right; Node *pr = x->parent; if (l->key == NIL.key && r->key == NIL.key) { if (pr->left == x) { pr->left = &NIL; } else pr->right = &NIL; } else if (l->key != NIL.key) { if (pr->left == x) { pr->left = l; } else pr->right = l; l->parent = pr; } else if (r->key != NIL.key) { if (pr->left == x) { pr->left = r; } else pr->right = r; r->parent = pr; } } Node *get_next(Node *k) { if (k->key == NIL.key) return &NIL; Node *res = get_next(k->left); if (res->key != NIL.key) return res; return k; } void del(Node *x) { if (x->key == NIL.key) return; Node *l = x->left; Node *r = x->right; Node *pr = x->parent; if (l->key != NIL.key && r->key != NIL.key) { Node *nex = get_next(r); x->key = nex->key; delp12(nex); } else { delp12(x); } } Node *rightRotate(Node *t) { Node *s = t->left; t->left = s->right; s->right = t; return s; } Node *leftRotate(Node *t) { Node *s = t->right; t->right = s->left; s->left = t; return s; } Node *_insert(Node *t, int key, int priority) { if (t->key == NIL.key) { return new Node(key, priority); } if (key == t->key) { return t; } if (key < t->key) { t->left = _insert(t->left, key, priority); if (t->priority < t->left->priority) { t = rightRotate(t); } } else { t->right = _insert(t->right, key, priority); if (t->priority < t->right->priority) { t = leftRotate(t); } } return t; } Node *delete1(Node *t, int key); Node *_delete(Node *t, int key) { if (t->left->key == NIL.key && t->right->key == NIL.key) { return &NIL; } else if (t->left->key == NIL.key) { t = leftRotate(t); } else if (t->right->key == NIL.key) { t = rightRotate(t); } else { if (t->left->priority > t->right->priority) { t = rightRotate(t); } else t = leftRotate(t); } return delete1(t, key); } Node *delete1(Node *t, int key) { if (t->key == NIL.key) { return &NIL; } if (key < t->key) { t->left = delete1(t->left, key); } else if (key > t->key) { t->right = delete1(t->right, key); } else return _delete(t, key); return t; } int H; int left(int i) { return i * 2 + 1; } int right(int i) { return i * 2 + 2; } // 内積 (dot product) : a・b = |a||b|cosΘ double dot(Point a, Point b) { return (a.real() * b.real() + a.imag() * b.imag()); } // 外積 (cross product) : a×b = |a||b|sinΘ double cross(Point a, Point b) { return (a.real() * b.imag() - a.imag() * b.real()); } // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0 int is_orthogonal(Point a1, Point a2, Point b1, Point b2) { return EQ(dot(a1 - a2, b1 - b2), 0.0); } // 2直線の平行判定 : a//b <=> cross(a, b) = 0 int is_parallel(Point a1, Point a2, Point b1, Point b2) { return EQ(cross(a1 - a2, b1 - b2), 0.0); } // 点cが直線a,b上にあるかないか int is_point_on_line(Point a, Point b, Point c) { return EQ(cross(b - a, c - a), 0.0); } // 点cが線分a,b上にあるかないか(1) int is_point_on_line1(Point a, Point b, Point c) { return EQ(cross(b - a, c - a), 0.0) && (dot(b - a, c - a) > -EPS) && (dot(a - b, c - b) > -EPS); } // 点cが線分a,b上にあるかないか(2) int is_point_on_line2(Point a, Point b, Point c) { // |a-c| + |c-b| <= |a-b| なら線分上 return (abs(a - c) + abs(c - b) < abs(a - b) + EPS); } // 点a,bを通る直線と点cとの距離 double distance_l_p(Point a, Point b, Point c) { return abs(cross(b - a, c - a)) / abs(b - a); } // 点a,bを端点とする線分と点cとの距離 double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } // a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定 int is_intersected_ls(Point a1, Point a2, Point b1, Point b2) { return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) && (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS); } // a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算 Point intersection_ls(Point a1, Point a2, Point b1, Point b2) { Point b = b2 - b1; double d1 = abs(cross(b, a1 - b1)); double d2 = abs(cross(b, a2 - b1)); double t = d1 / (d1 + d2); return a1 + (a2 - a1) * t; } // a1,a2を通る直線とb1,b2を通る直線の交差判定 int is_intersected_l(Point a1, Point a2, Point b1, Point b2) { return !EQ(cross(a1 - a2, b1 - b2), 0.0); } // a1,a2を通る直線とb1,b2を通る直線の交点計算 Point intersection_l(Point a1, Point a2, Point b1, Point b2) { Point a = a2 - a1; Point b = b2 - b1; return a1 + a * cross(b, b1 - a1) / cross(b, a); } // 円の交点 pair<Point, Point> intersection_circle(Point p1, Point p2, double r1, double r2) { double d = abs(p2 - p1); double th = acos((d * d + r1 * r1 - r2 * r2) / (2 * d * r1)); return make_pair(p1 + polar(r1, arg(p2 - p1) + th), polar(r1, arg(p2 - p1) - th)); } ll heap[2000010]; void maxHeapify(int i) { int l = left(i); int r = right(i); int largest = 0; if (l < H && heap[l] > heap[i]) largest = l; else largest = i; if (r < H && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(largest); } } int pare(int i) { return (i - 1) / 2; } void raise(int i) { int l = pare(i); if (l < 0) return; if (heap[l] < heap[i]) { swap(heap[i], heap[l]); raise(l); } } void minHeapify(int i) { int l = left(i); int r = right(i); int minimam = 0; if (l < H && heap[l] < heap[i]) minimam = l; else minimam = i; if (r < H && heap[r] < heap[minimam]) minimam = r; if (minimam != i) { swap(heap[i], heap[minimam]); minHeapify(minimam); } } void buildMaxHeap() { for (int i = H / 2; i >= 0; i--) { maxHeapify(i); } } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; std::vector<int> find_all(const std::string str, const std::string subStr) { std::vector<int> result; int subStrSize = subStr.size(); int pos = str.find(subStr); while (pos != std::string::npos) { result.push_back(pos); pos = str.find(subStr, pos + 1); } return result; } // ll memo[100010]; // ll next[100010]; // ll dm[100010]; // int f[100010]; // ll rec(int x) { // // if (~memo[x]) return memo[x]; // if (x == n) { // dm[n] = 1; // return 1; // } // ll *res = &memo[x]; // *res = 0; // set<int> st; // st.insert(f[x]); // for (int i = x + 1; i <= n; i++) // { // if (~memo[i]) { // *res += memo[i] + 1; // *res %= INF; // break; // } // // *res += rec(i); // *res %= INF; // if (st.find(f[i]) != st.end()) {break; } // st.insert(f[i]); // } // // return *res; // } #define bit(x, v) ((ll)x << v) class BIT { static const int MAX_N = 1000010; public: BIT() { memset(bit, 0, sizeof(bit)); } int bit[MAX_N + 1], n; int sum(int i) { int s = 0; while (i > 0) { s += bit[i]; i -= i & -i; } return s; } void add(int i, int x) { while (i <= n) { bit[i] += x; i += i & -i; } } void clear() { memset(bit, 0, sizeof(bit)); } int a[MAX_N]; void bable_swap_count() { ll ans = 0; for (size_t j = 0; j < n; j++) { ans += j - sum(a[j]); add(a[j], 1); } printf("%lld\n", ans); } int search(int s, int x) { ll half = (s + x) / 2; ll sh = sum(x); ll sl = sum(half); ll st = sum(s); if (sh - sl == 0) { return x; } if (sh - sl < x - half) { return search(half, x); } if (sl - st == 0) { return half; } if (sl - st < half - s) { return search(s, half); } return -1; } int lankSearch(int lank) { return lankSearch(lank, 0, MAX_N); } int lankSearch(int lank, int s, int t) { ll half = (s + t) / 2; ll v = sum(half); ll v1 = sum(t); ll v2 = sum(s); if (lank == 1) { if (s + 1 >= t) return t; else if (v - v2 > 0) { return lankSearch(lank, s, half); } else return lankSearch(lank, half, t); } if ((v - v2) < lank) { return lankSearch(lank - (v - v2), half, t); } if ((v - v2) >= lank) { return lankSearch(lank, s, half); } return -1; } }; vector<ll> getp(ll n) { vector<ll> res; ll a = 2; if (n % 2 == 0) { res.push_back(2); while (n % 2 == 0) n /= 2; } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { res.push_back(i); while (n % i == 0) n /= i; } } if (n != 1) res.push_back(n); return res; } vector<ll> getp2(ll n) { vector<ll> res; ll a = 2; if (n % 2 == 0) { while (n % 2 == 0) { n /= 2; res.push_back(2); } } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { while (n % i == 0) { n /= i; res.push_back(i); } } } if (n != 1) res.push_back(n); return res; } vector<pll> getp3(ll n) { vector<pll> res; ll a = 2; int cnt = 0; if (n % 2 == 0) { res.push_back(make_pair(2, 0)); while (n % 2 == 0) { n /= 2; res[cnt].second++; } cnt++; } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { res.push_back(make_pair(n, 0)); while (n % i == 0) { n /= i; res[cnt].second++; } cnt++; } } if (n != 1) res.push_back(make_pair(n, 1)); return res; } vector<ll> getDivisors(ll n) { vector<ll> res; ll a = 2; res.push_back(1); for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (n / i != i) res.push_back(n / i); } } return res; } struct ve { public: vector<ve> child; int _t = INF; ve(int t) : _t(t) {} ve(ve _left, ve _right) { _t = _left._t + _right._t; child.push_back(_left); child.push_back(_right); } bool operator<(const ve &t) const { return _t > t._t; } }; vector<bool> elas(ll n) { vector<bool> r(n); for (ll i = 3; i < n; i += 2) { r[i] = 1; } r[0] = 0; r[1] = 0; r[2] = 1; for (ll i = 3; i * i < n; i += 2) { if (!r[i]) continue; ll ti = i * 2; while (ti < n) { r[ti] = false; ti += i; } } return r; } bool isprime(ll v) { for (ll i = 2; i * i <= v; i++) { if (v % i == 0) return false; } return true; } ll lcm(vector<ll> v) { if (v.size() == 0) return 0; ll t = v[0]; for (size_t i = 1; i < v.size(); i++) { t = v[i] * t / gcd(v[i], t); } return t; } ll eulerphi(ll n) { auto p = getp(n); double u = n; for (auto v : p) { u *= (double)(v - 1) / (double)v; } return u; } double revs(double x) { ll dig = 0; stringstream st; st << std::fixed << setprecision(0) << x; string v = st.str(); reverse(v.begin(), v.end()); return stod(v); } bool chkparindrome(double x) { stringstream st; st << std::fixed << setprecision(0) << x; string p = st.str(); for (size_t i = 0; i < p.size() / 2; i++) { if (p[i] != p[p.size() - i - 1]) { return false; } } return true; } ll digitC(double x) { stringstream st; st << fixed << setprecision(0) << x; return st.str().size(); } ll digitSum(double x) { stringstream st; st << std::fixed << x; string p = st.str(); ll rs = 0; for (size_t i = 0; i < p.size(); i++) { if (p[i] == '.') break; rs += p[i] - '0'; } return rs; } pdd recs(int x) { if (x == 0) return make_pair(1, 2); pdd d = recs(x - 1); auto nu = d.second * 2.0 + d.first; auto de = d.second; return make_pair(de, nu); } ll caldig(ll a) { ll r = 0; while (a > 0) { a /= 10; r++; } return r; } int chav(char v) { if (v <= 'Z') return v - 'A'; return v - 'a' + 26; } char itoch(int i) { if (i < 26) return i + 'A'; return (i - 26) + 'a'; } int crmp[1000][1000]; int countR(ll base, ll x, ll y, int deep) { if (~crmp[x][y]) { return deep - crmp[x][y]; } crmp[x][y] = deep; double nu = sqrt(base) + x; double de = (base - (x * x)) / y; ll u = nu / de; ll nx = x - (u * de); return countR(base, -nx, de, deep + 1); } bool isPermutation(ll x, ll y) { int c1[10]; int c2[10]; memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); while (x > 0) { c1[x % 10]++; x /= 10; } while (y > 0) { c2[y % 10]++; y /= 10; } for (size_t i = 0; i < 10; i++) { if (c1[i] != c2[i]) return false; } return true; } bool fnd[1000]; double cl4(double a, int t, double b) { switch (t) { case 0: return a + b; case 1: return a - b; case 2: return a * b; case 3: if (b == 0) { return INF; } else return a / b; default: break; } } void clc5(double a, int t1, double b) { double ab = cl4(a, t1, b); if (ab != INF && ab > 0 && ab < 1000 && (int)ab == ab) { fnd[(int)ab] = 1; } } void clc4(double a, int t1, double b, int t2, double c) { double ab = cl4(a, t1, b); double bc = cl4(b, t2, c); if (ab != INF) { clc5(ab, t2, c); } if (bc != INF) { clc5(a, t1, bc); } } void clc3(double a, int t1, double b, int t2, double c, int t3, double d) { double ab = cl4(a, t1, b); double bc = cl4(b, t2, c); double cd = cl4(c, t3, d); if (ab != INF) { clc4(ab, t2, c, t3, d); } if (bc != INF) { clc4(a, t1, bc, t3, d); } if (cd != INF) { clc4(a, t1, b, t2, cd); } } void clc2(ll a, ll b, ll c, ll d) { for (size_t i = 0; i < 4; i++) { for (size_t j = 0; j < 4; j++) { for (size_t k = 0; k < 4; k++) { clc3(a, i, b, j, c, k, d); } } } } void clc(ll a, ll b, ll c, ll d) { ll t[] = {a, b, c, d}; do { clc2(t[0], t[1], t[2], t[3]); } while (next_permutation(t, t + 4)); } double heron(ll a, ll b, ll c) { double s = (double)(a + b + c) / 2.0; return sqrt(s * (s - a) * (s - b) * (s - c)); } double calcThreePS(double x1, double y1, double x2, double y2, double x3, double y3) { return abs((x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1) / 2.0); } vector<ll> p; int tp[21]; int evp[26]; int oddp[26]; ll rec(int i, int dig) { // 全てセットされたためチェック if (dig <= i * 2) { for (int j = i; j < dig; j++) { int ad = 0; if (p[j - 1] >= 10) { ad = 1; } if ((p[j] + ad) % 2 == 0) return 0; } return 1; } int *t; if (p[i - 1] > 10) { t = evp; } else if ((dig % 2 == 1 && dig / 2 == i)) { return 0; } else { t = oddp; } ll res = 0; if ((dig % 2 == 1 && dig / 2 == i)) { for (size_t j = 0; j < 10; j++) { p[i] = j * 2; res += rec(i + 1, dig); } } else { for (size_t j = 0; j < 25; j++) { p[i] = t[j]; p[dig - i - 1] = t[j]; res += rec(i + 1, dig); } } return res; } void solv() { string s; cin >> s; ll dp[100010][10]; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; dp[0][1] = 1; int bef = 0; for (size_t i = 1; i <= s.size(); i++) { int v = s[i - 1] - '0'; // ピッタリ払うとき dp[i][v] = min(dp[i - 1][bef] + v, dp[i - 1][bef + 1] + (10 - v)); // 1余分に払うとき dp[i][v + 1] = min(dp[i - 1][bef] + v + 1, dp[i - 1][bef + 1] + (10 - (v + 1))); bef = v; } cout << dp[s.size()][bef] << endl; } int main() { // COMinit(); solv(); return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctype.h> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <unordered_map> #include <utility> #include <vector> #define _USE_MATH_DEFINES #include <complex> #include <iostream> #include <math.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, double> pld; typedef pair<double, double> pdd; typedef pair<double, ll> pdl; typedef pair<int, char> pic; typedef vector<ll> vl; typedef vector<int> vi; typedef complex<double> Point; typedef priority_queue<ll, vector<ll>, greater<ll>> llgreaterq; typedef priority_queue<pll, vector<pll>, greater<pll>> pllgreaterq; typedef priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> plpllgreaterq; typedef priority_queue<vi, vector<vi>, greater<vi>> vigreaterq; typedef priority_queue<vl, vector<vl>, greater<vl>> vlgreaterq; #define bit(x, v) ((ll)x << v) #define rep(x, v) for (int x = 0; x < v; x++) #define rep2(x, f, v) for (int x = f; x < v; x++) // 許容する誤差ε #define EPS (1e-10) // 2つのスカラーが等しいかどうか #define EQ(a, b) (abs((a) - (b)) < EPS) // 2つのベクトルが等しいかどうか #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) const ll INF = 1000000007; const int MAX = 2000010; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int pr[200010]; int lank[200010]; void uini(int n) { for (size_t i = 0; i <= n; i++) { pr[i] = i; } } int parent(int x) { if (x == pr[x]) return x; return pr[x] = parent(pr[x]); } int same(int x, int y) { return parent(x) == parent(y); } bool unit(int x, int y) { int px = parent(x); int py = parent(y); if (px == py) return false; if (lank[px] < lank[py]) { pr[py] = px; lank[px] += lank[py] + 1; } else { pr[px] = py; lank[py] += lank[px] + 1; } return true; } ll bit[200010]; int max_n = 200000; int pm = 0; void add(int x) { while (max_n >= x) { bit[x]++; x += x & -x; } } void sub(int x) { while (max_n >= x) { bit[x]--; x += x & -x; } } ll merge(ll *a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector<int> L(n1 + 1); vector<int> R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = INF; R[n2] = INF; ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll merge2(pair<int, char> *a, int left, int mid, int right) { ll n1 = mid - left; ll n2 = right - mid; vector<pair<int, char>> L(n1 + 1); vector<pair<int, char>> R(n2 + 1); for (size_t i = 0; i < n1; i++) { L[i] = a[left + i]; } for (size_t i = 0; i < n2; i++) { R[i] = a[mid + i]; } L[n1] = make_pair(INF, ' '); R[n2] = make_pair(INF, ' '); ll i = 0; ll j = 0; ll r = 0; for (size_t k = left; k < right; k++) { if (L[i].first <= R[j].first) { a[k] = L[i]; i++; } else { a[k] = R[j]; r += n1 - i; j++; } } return r; } ll mergeSort2(pair<int, char> *a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort2(a, left, mid); res += mergeSort2(a, mid, right); res += merge2(a, left, mid, right); } return res; } ll mergeSort(ll *a, int left, int right) { ll res = 0; if (left + 1 < right) { int mid = (left + right) / 2; res = mergeSort(a, left, mid); res += mergeSort(a, mid, right); res += merge(a, left, mid, right); } return res; } int partition(pair<int, char> *a, int p, int r) { pair<int, char> x = a[r]; int i = p - 1; for (size_t j = p; j < r; j++) { if (a[j].first <= x.first) { i++; swap(a[i], a[j]); } } swap(a[i + 1], a[r]); return i + 1; } void quick(pair<int, char> *a, int p, int r) { if (p < r) { int q = partition(a, p, r); quick(a, p, q - 1); quick(a, q + 1, r); } } ll n; int ci = 0; ll P[1000010]; struct Node { int key; int priority; Node *parent, *left, *right; Node(int key, int priority); Node() {} }; Node NIL; Node::Node(int key, int priority) : key(key), priority(priority) { left = &NIL; right = &NIL; } Node *root = new Node(); void cenrec(Node *k) { if (k->key == NIL.key) return; cenrec(k->left); cout << " " << k->key; cenrec(k->right); } void fastrec(Node *k) { if (k->key == NIL.key) return; cout << " " << k->key; fastrec(k->left); fastrec(k->right); } void insert(Node *v) { Node *y = &NIL; Node *x = root; while (x->key != NIL.key) { y = x; if (v->key < x->key) { x = x->left; } else { x = x->right; } } v->parent = y; if (y->key == NIL.key) { root = v; } else if (v->key < y->key) { y->left = v; } else { y->right = v; } } Node *find(Node *k, ll v) { if (k->key == NIL.key) return &NIL; if (k->key == v) return k; if (v < k->key) return find(k->left, v); return find(k->right, v); } void delp12(Node *x) { if (x->key == NIL.key) return; Node *l = x->left; Node *r = x->right; Node *pr = x->parent; if (l->key == NIL.key && r->key == NIL.key) { if (pr->left == x) { pr->left = &NIL; } else pr->right = &NIL; } else if (l->key != NIL.key) { if (pr->left == x) { pr->left = l; } else pr->right = l; l->parent = pr; } else if (r->key != NIL.key) { if (pr->left == x) { pr->left = r; } else pr->right = r; r->parent = pr; } } Node *get_next(Node *k) { if (k->key == NIL.key) return &NIL; Node *res = get_next(k->left); if (res->key != NIL.key) return res; return k; } void del(Node *x) { if (x->key == NIL.key) return; Node *l = x->left; Node *r = x->right; Node *pr = x->parent; if (l->key != NIL.key && r->key != NIL.key) { Node *nex = get_next(r); x->key = nex->key; delp12(nex); } else { delp12(x); } } Node *rightRotate(Node *t) { Node *s = t->left; t->left = s->right; s->right = t; return s; } Node *leftRotate(Node *t) { Node *s = t->right; t->right = s->left; s->left = t; return s; } Node *_insert(Node *t, int key, int priority) { if (t->key == NIL.key) { return new Node(key, priority); } if (key == t->key) { return t; } if (key < t->key) { t->left = _insert(t->left, key, priority); if (t->priority < t->left->priority) { t = rightRotate(t); } } else { t->right = _insert(t->right, key, priority); if (t->priority < t->right->priority) { t = leftRotate(t); } } return t; } Node *delete1(Node *t, int key); Node *_delete(Node *t, int key) { if (t->left->key == NIL.key && t->right->key == NIL.key) { return &NIL; } else if (t->left->key == NIL.key) { t = leftRotate(t); } else if (t->right->key == NIL.key) { t = rightRotate(t); } else { if (t->left->priority > t->right->priority) { t = rightRotate(t); } else t = leftRotate(t); } return delete1(t, key); } Node *delete1(Node *t, int key) { if (t->key == NIL.key) { return &NIL; } if (key < t->key) { t->left = delete1(t->left, key); } else if (key > t->key) { t->right = delete1(t->right, key); } else return _delete(t, key); return t; } int H; int left(int i) { return i * 2 + 1; } int right(int i) { return i * 2 + 2; } // 内積 (dot product) : a・b = |a||b|cosΘ double dot(Point a, Point b) { return (a.real() * b.real() + a.imag() * b.imag()); } // 外積 (cross product) : a×b = |a||b|sinΘ double cross(Point a, Point b) { return (a.real() * b.imag() - a.imag() * b.real()); } // 2直線の直交判定 : a⊥b <=> dot(a, b) = 0 int is_orthogonal(Point a1, Point a2, Point b1, Point b2) { return EQ(dot(a1 - a2, b1 - b2), 0.0); } // 2直線の平行判定 : a//b <=> cross(a, b) = 0 int is_parallel(Point a1, Point a2, Point b1, Point b2) { return EQ(cross(a1 - a2, b1 - b2), 0.0); } // 点cが直線a,b上にあるかないか int is_point_on_line(Point a, Point b, Point c) { return EQ(cross(b - a, c - a), 0.0); } // 点cが線分a,b上にあるかないか(1) int is_point_on_line1(Point a, Point b, Point c) { return EQ(cross(b - a, c - a), 0.0) && (dot(b - a, c - a) > -EPS) && (dot(a - b, c - b) > -EPS); } // 点cが線分a,b上にあるかないか(2) int is_point_on_line2(Point a, Point b, Point c) { // |a-c| + |c-b| <= |a-b| なら線分上 return (abs(a - c) + abs(c - b) < abs(a - b) + EPS); } // 点a,bを通る直線と点cとの距離 double distance_l_p(Point a, Point b, Point c) { return abs(cross(b - a, c - a)) / abs(b - a); } // 点a,bを端点とする線分と点cとの距離 double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return abs(c - a); if (dot(a - b, c - b) < EPS) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } // a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定 int is_intersected_ls(Point a1, Point a2, Point b1, Point b2) { return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS) && (cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < EPS); } // a1,a2を端点とする線分とb1,b2を端点とする線分の交点計算 Point intersection_ls(Point a1, Point a2, Point b1, Point b2) { Point b = b2 - b1; double d1 = abs(cross(b, a1 - b1)); double d2 = abs(cross(b, a2 - b1)); double t = d1 / (d1 + d2); return a1 + (a2 - a1) * t; } // a1,a2を通る直線とb1,b2を通る直線の交差判定 int is_intersected_l(Point a1, Point a2, Point b1, Point b2) { return !EQ(cross(a1 - a2, b1 - b2), 0.0); } // a1,a2を通る直線とb1,b2を通る直線の交点計算 Point intersection_l(Point a1, Point a2, Point b1, Point b2) { Point a = a2 - a1; Point b = b2 - b1; return a1 + a * cross(b, b1 - a1) / cross(b, a); } // 円の交点 pair<Point, Point> intersection_circle(Point p1, Point p2, double r1, double r2) { double d = abs(p2 - p1); double th = acos((d * d + r1 * r1 - r2 * r2) / (2 * d * r1)); return make_pair(p1 + polar(r1, arg(p2 - p1) + th), polar(r1, arg(p2 - p1) - th)); } ll heap[2000010]; void maxHeapify(int i) { int l = left(i); int r = right(i); int largest = 0; if (l < H && heap[l] > heap[i]) largest = l; else largest = i; if (r < H && heap[r] > heap[largest]) largest = r; if (largest != i) { swap(heap[i], heap[largest]); maxHeapify(largest); } } int pare(int i) { return (i - 1) / 2; } void raise(int i) { int l = pare(i); if (l < 0) return; if (heap[l] < heap[i]) { swap(heap[i], heap[l]); raise(l); } } void minHeapify(int i) { int l = left(i); int r = right(i); int minimam = 0; if (l < H && heap[l] < heap[i]) minimam = l; else minimam = i; if (r < H && heap[r] < heap[minimam]) minimam = r; if (minimam != i) { swap(heap[i], heap[minimam]); minHeapify(minimam); } } void buildMaxHeap() { for (int i = H / 2; i >= 0; i--) { maxHeapify(i); } } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; std::vector<int> find_all(const std::string str, const std::string subStr) { std::vector<int> result; int subStrSize = subStr.size(); int pos = str.find(subStr); while (pos != std::string::npos) { result.push_back(pos); pos = str.find(subStr, pos + 1); } return result; } // ll memo[100010]; // ll next[100010]; // ll dm[100010]; // int f[100010]; // ll rec(int x) { // // if (~memo[x]) return memo[x]; // if (x == n) { // dm[n] = 1; // return 1; // } // ll *res = &memo[x]; // *res = 0; // set<int> st; // st.insert(f[x]); // for (int i = x + 1; i <= n; i++) // { // if (~memo[i]) { // *res += memo[i] + 1; // *res %= INF; // break; // } // // *res += rec(i); // *res %= INF; // if (st.find(f[i]) != st.end()) {break; } // st.insert(f[i]); // } // // return *res; // } #define bit(x, v) ((ll)x << v) class BIT { static const int MAX_N = 1000010; public: BIT() { memset(bit, 0, sizeof(bit)); } int bit[MAX_N + 1], n; int sum(int i) { int s = 0; while (i > 0) { s += bit[i]; i -= i & -i; } return s; } void add(int i, int x) { while (i <= n) { bit[i] += x; i += i & -i; } } void clear() { memset(bit, 0, sizeof(bit)); } int a[MAX_N]; void bable_swap_count() { ll ans = 0; for (size_t j = 0; j < n; j++) { ans += j - sum(a[j]); add(a[j], 1); } printf("%lld\n", ans); } int search(int s, int x) { ll half = (s + x) / 2; ll sh = sum(x); ll sl = sum(half); ll st = sum(s); if (sh - sl == 0) { return x; } if (sh - sl < x - half) { return search(half, x); } if (sl - st == 0) { return half; } if (sl - st < half - s) { return search(s, half); } return -1; } int lankSearch(int lank) { return lankSearch(lank, 0, MAX_N); } int lankSearch(int lank, int s, int t) { ll half = (s + t) / 2; ll v = sum(half); ll v1 = sum(t); ll v2 = sum(s); if (lank == 1) { if (s + 1 >= t) return t; else if (v - v2 > 0) { return lankSearch(lank, s, half); } else return lankSearch(lank, half, t); } if ((v - v2) < lank) { return lankSearch(lank - (v - v2), half, t); } if ((v - v2) >= lank) { return lankSearch(lank, s, half); } return -1; } }; vector<ll> getp(ll n) { vector<ll> res; ll a = 2; if (n % 2 == 0) { res.push_back(2); while (n % 2 == 0) n /= 2; } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { res.push_back(i); while (n % i == 0) n /= i; } } if (n != 1) res.push_back(n); return res; } vector<ll> getp2(ll n) { vector<ll> res; ll a = 2; if (n % 2 == 0) { while (n % 2 == 0) { n /= 2; res.push_back(2); } } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { while (n % i == 0) { n /= i; res.push_back(i); } } } if (n != 1) res.push_back(n); return res; } vector<pll> getp3(ll n) { vector<pll> res; ll a = 2; int cnt = 0; if (n % 2 == 0) { res.push_back(make_pair(2, 0)); while (n % 2 == 0) { n /= 2; res[cnt].second++; } cnt++; } for (ll i = 3; i * i <= n; i += 2) { if (n % i == 0) { res.push_back(make_pair(n, 0)); while (n % i == 0) { n /= i; res[cnt].second++; } cnt++; } } if (n != 1) res.push_back(make_pair(n, 1)); return res; } vector<ll> getDivisors(ll n) { vector<ll> res; ll a = 2; res.push_back(1); for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (n / i != i) res.push_back(n / i); } } return res; } struct ve { public: vector<ve> child; int _t = INF; ve(int t) : _t(t) {} ve(ve _left, ve _right) { _t = _left._t + _right._t; child.push_back(_left); child.push_back(_right); } bool operator<(const ve &t) const { return _t > t._t; } }; vector<bool> elas(ll n) { vector<bool> r(n); for (ll i = 3; i < n; i += 2) { r[i] = 1; } r[0] = 0; r[1] = 0; r[2] = 1; for (ll i = 3; i * i < n; i += 2) { if (!r[i]) continue; ll ti = i * 2; while (ti < n) { r[ti] = false; ti += i; } } return r; } bool isprime(ll v) { for (ll i = 2; i * i <= v; i++) { if (v % i == 0) return false; } return true; } ll lcm(vector<ll> v) { if (v.size() == 0) return 0; ll t = v[0]; for (size_t i = 1; i < v.size(); i++) { t = v[i] * t / gcd(v[i], t); } return t; } ll eulerphi(ll n) { auto p = getp(n); double u = n; for (auto v : p) { u *= (double)(v - 1) / (double)v; } return u; } double revs(double x) { ll dig = 0; stringstream st; st << std::fixed << setprecision(0) << x; string v = st.str(); reverse(v.begin(), v.end()); return stod(v); } bool chkparindrome(double x) { stringstream st; st << std::fixed << setprecision(0) << x; string p = st.str(); for (size_t i = 0; i < p.size() / 2; i++) { if (p[i] != p[p.size() - i - 1]) { return false; } } return true; } ll digitC(double x) { stringstream st; st << fixed << setprecision(0) << x; return st.str().size(); } ll digitSum(double x) { stringstream st; st << std::fixed << x; string p = st.str(); ll rs = 0; for (size_t i = 0; i < p.size(); i++) { if (p[i] == '.') break; rs += p[i] - '0'; } return rs; } pdd recs(int x) { if (x == 0) return make_pair(1, 2); pdd d = recs(x - 1); auto nu = d.second * 2.0 + d.first; auto de = d.second; return make_pair(de, nu); } ll caldig(ll a) { ll r = 0; while (a > 0) { a /= 10; r++; } return r; } int chav(char v) { if (v <= 'Z') return v - 'A'; return v - 'a' + 26; } char itoch(int i) { if (i < 26) return i + 'A'; return (i - 26) + 'a'; } int crmp[1000][1000]; int countR(ll base, ll x, ll y, int deep) { if (~crmp[x][y]) { return deep - crmp[x][y]; } crmp[x][y] = deep; double nu = sqrt(base) + x; double de = (base - (x * x)) / y; ll u = nu / de; ll nx = x - (u * de); return countR(base, -nx, de, deep + 1); } bool isPermutation(ll x, ll y) { int c1[10]; int c2[10]; memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); while (x > 0) { c1[x % 10]++; x /= 10; } while (y > 0) { c2[y % 10]++; y /= 10; } for (size_t i = 0; i < 10; i++) { if (c1[i] != c2[i]) return false; } return true; } bool fnd[1000]; double cl4(double a, int t, double b) { switch (t) { case 0: return a + b; case 1: return a - b; case 2: return a * b; case 3: if (b == 0) { return INF; } else return a / b; default: break; } } void clc5(double a, int t1, double b) { double ab = cl4(a, t1, b); if (ab != INF && ab > 0 && ab < 1000 && (int)ab == ab) { fnd[(int)ab] = 1; } } void clc4(double a, int t1, double b, int t2, double c) { double ab = cl4(a, t1, b); double bc = cl4(b, t2, c); if (ab != INF) { clc5(ab, t2, c); } if (bc != INF) { clc5(a, t1, bc); } } void clc3(double a, int t1, double b, int t2, double c, int t3, double d) { double ab = cl4(a, t1, b); double bc = cl4(b, t2, c); double cd = cl4(c, t3, d); if (ab != INF) { clc4(ab, t2, c, t3, d); } if (bc != INF) { clc4(a, t1, bc, t3, d); } if (cd != INF) { clc4(a, t1, b, t2, cd); } } void clc2(ll a, ll b, ll c, ll d) { for (size_t i = 0; i < 4; i++) { for (size_t j = 0; j < 4; j++) { for (size_t k = 0; k < 4; k++) { clc3(a, i, b, j, c, k, d); } } } } void clc(ll a, ll b, ll c, ll d) { ll t[] = {a, b, c, d}; do { clc2(t[0], t[1], t[2], t[3]); } while (next_permutation(t, t + 4)); } double heron(ll a, ll b, ll c) { double s = (double)(a + b + c) / 2.0; return sqrt(s * (s - a) * (s - b) * (s - c)); } double calcThreePS(double x1, double y1, double x2, double y2, double x3, double y3) { return abs((x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1) / 2.0); } vector<ll> p; int tp[21]; int evp[26]; int oddp[26]; ll rec(int i, int dig) { // 全てセットされたためチェック if (dig <= i * 2) { for (int j = i; j < dig; j++) { int ad = 0; if (p[j - 1] >= 10) { ad = 1; } if ((p[j] + ad) % 2 == 0) return 0; } return 1; } int *t; if (p[i - 1] > 10) { t = evp; } else if ((dig % 2 == 1 && dig / 2 == i)) { return 0; } else { t = oddp; } ll res = 0; if ((dig % 2 == 1 && dig / 2 == i)) { for (size_t j = 0; j < 10; j++) { p[i] = j * 2; res += rec(i + 1, dig); } } else { for (size_t j = 0; j < 25; j++) { p[i] = t[j]; p[dig - i - 1] = t[j]; res += rec(i + 1, dig); } } return res; } void solv() { string s; cin >> s; ll dp[1000100][11]; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; dp[0][1] = 1; int bef = 0; for (size_t i = 1; i <= s.size(); i++) { int v = s[i - 1] - '0'; // ピッタリ払うとき dp[i][v] = min(dp[i - 1][bef] + v, dp[i - 1][bef + 1] + (10 - v)); // 1余分に払うとき dp[i][v + 1] = min(dp[i - 1][bef] + v + 1, dp[i - 1][bef + 1] + (10 - (v + 1))); bef = v; } cout << dp[s.size()][bef] << endl; } int main() { // COMinit(); solv(); return 0; }
replace
1,047
1,048
1,047
1,048
0
p02775
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdlib> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define SAY_YES cout << "YES" << endl; #define SAY_Yes cout << "Yes" << endl; #define SAY_NO cout << "NO" << endl; #define SAY_No cout << "No" << endl; #define IFYES(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "YES" << endl; \ } else { \ cout << "NO" << endl; \ } #define IFYes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "Yes" << endl; \ } else { \ cout << "No" << endl; \ } #define IFyes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "yes" << endl; \ } else { \ cout << "no" << endl; \ } const long long int mod = 1000000007; const long long int INF = 99999999999999999; long long int N, M, K, A[200050] = {}, pl = 0, zero = 0, mi = 0, tmp = 0, res = 0; long long int dp[1000020] = {}, dp2[1000020] = {}; vector<long long int> minus_num, plus_num; string str; map<string, long long int> mp; int main() { cout << fixed << setprecision(18); cin >> str; for (int i = 0; i < str.length(); i++) { A[str.length() - i - 1] = str[i] - '0'; dp[i] = INF; dp2[i] = INF; } // dpはより大きなお金を払う状態 dp[0] = 10 - A[0]; dp2[0] = A[0]; for (int i = 1; i < str.length(); i++) { dp[i] = min(dp[i - 1] + 9 - A[i], dp2[i - 1] + 10 - A[i]); if (A[i] != 9) { dp2[i] = min(dp[i - 1] + A[i] + 1, dp2[i - 1] + A[i]); } else { dp2[i] = dp2[i - 1] + A[i]; } tmp = i; // cout<<dp[i]<<" "<<dp2[i]<<endl; } cout << min(dp[str.length() - 1] + 1, dp2[str.length() - 1]) << endl; }
#include <algorithm> #include <complex> #include <cstdlib> #include <ctime> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define SAY_YES cout << "YES" << endl; #define SAY_Yes cout << "Yes" << endl; #define SAY_NO cout << "NO" << endl; #define SAY_No cout << "No" << endl; #define IFYES(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "YES" << endl; \ } else { \ cout << "NO" << endl; \ } #define IFYes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "Yes" << endl; \ } else { \ cout << "No" << endl; \ } #define IFyes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) { \ cout << "yes" << endl; \ } else { \ cout << "no" << endl; \ } const long long int mod = 1000000007; const long long int INF = 99999999999999999; long long int N, M, K, A[1100050] = {}, pl = 0, zero = 0, mi = 0, tmp = 0, res = 0; long long int dp[1000020] = {}, dp2[1000020] = {}; vector<long long int> minus_num, plus_num; string str; map<string, long long int> mp; int main() { cout << fixed << setprecision(18); cin >> str; for (int i = 0; i < str.length(); i++) { A[str.length() - i - 1] = str[i] - '0'; dp[i] = INF; dp2[i] = INF; } // dpはより大きなお金を払う状態 dp[0] = 10 - A[0]; dp2[0] = A[0]; for (int i = 1; i < str.length(); i++) { dp[i] = min(dp[i - 1] + 9 - A[i], dp2[i - 1] + 10 - A[i]); if (A[i] != 9) { dp2[i] = min(dp[i - 1] + A[i] + 1, dp2[i - 1] + A[i]); } else { dp2[i] = dp2[i - 1] + A[i]; } tmp = i; // cout<<dp[i]<<" "<<dp2[i]<<endl; } cout << min(dp[str.length() - 1] + 1, dp2[str.length() - 1]) << endl; }
replace
42
43
42
43
0