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
p01681
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <set> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const double EPS = 1e-8; const double PI = acos(-1); struct point { double x, y; point() : x(0), y(0) {} point(double x, double y) : x(x), y(y) {} point operator+(const point &a) const { return point(x + a.x, y + a.y); } point operator-(const point &a) const { return point(x - a.x, y - a.y); } bool operator==(const point &a) const { return abs(x - a.x) < EPS && abs(y - a.y) < EPS; } bool operator!=(const point &a) const { return abs(x - a.x) > EPS || abs(y - a.y) > EPS; } }; point operator*(double c, const point &a) { return point(c * a.x, c * a.y); } inline double dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } inline double cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } inline point rot(const point &a, double theta) { return point(a.x * cos(theta) - a.y * sin(theta), a.x * sin(theta) + a.y * cos(theta)); } inline double arg(const point &a) { double t = atan2(a.y, a.x); return t < 0 ? t + 2 * PI : t; } struct segment { point a, b; segment() {} segment(const point &a, const point &b) : a(a), b(b) {} }; typedef vector<point> polygon; enum { CCW = 1, CW = -1, ON = 0 }; inline int ccw(const point &a, const point &b, const point &c) { double rdir = cross(b - a, c - a); if (rdir > EPS) return CCW; if (rdir < -EPS) return CW; return ON; } inline bool intersect(const segment &S1, const segment &S2) { if (max(S1.a.x, S1.b.x) + EPS < min(S2.a.x, S2.b.x) || max(S1.a.y, S1.b.y) + EPS < min(S2.a.y, S2.b.y) || max(S2.a.x, S2.b.x) + EPS < min(S1.a.x, S1.b.x) || max(S2.a.y, S2.b.y) + EPS < min(S1.a.y, S1.b.y)) return false; return ccw(S1.a, S1.b, S2.a) * ccw(S1.a, S1.b, S2.b) <= 0 && ccw(S2.a, S2.b, S1.a) * ccw(S2.a, S2.b, S1.b) <= 0; } inline bool cover(const segment &S, const point &p) { return abs(cross(S.a - p, S.b - p)) < EPS && dot(S.a - p, S.b - p) < EPS; } inline point get_intersect(const segment &S1, const segment &S2) { double a1 = cross(S1.b - S1.a, S2.b - S2.a); double a2 = cross(S1.b - S1.a, S1.b - S2.a); if (abs(a1) < EPS) { if (cover(S1, S2.a)) return S2.a; if (cover(S1, S2.b)) return S2.b; if (cover(S2, S1.a)) return S1.a; return S1.b; } return S2.a + a2 / a1 * (S2.b - S2.a); } bool is_perm(vector<int> a) { sort(a.begin(), a.end()); return unique(a.begin(), a.end()) - a.begin() == a.size(); } vector<int> simulate(point p, const polygon &F, double theta) { int n = F.size(); vector<int> res; bool used[8] = {}; while (res.size() < n) { point v(cos(theta), sin(theta)); segment S(p, p + 200 * v); int i0 = -1; segment e; // 反射する辺 point q; // 反射する点 rep(i, n) { e = segment(F[i], F[(i + 1) % n]); if (intersect(S, e)) { q = get_intersect(S, e); if (q != p) { i0 = i; break; } } } assert(i0 != -1); if (used[i0]) return res; // 同じ辺に二回反射したらダメ used[i0] = true; if (q == e.a || q == e.b) return res; // 角に当たったらダメ res.push_back(i0); // 反射角を求める double phi = arg(e.b - e.a); point dir = rot(q - p, -phi); dir.y *= -1; dir = rot(dir, phi); theta = arg(dir); p = q; } return res; } void dfs(point s, int cnt, int used, polygon G, vector<double> &theta) { int n = G.size(); if (cnt == n - 1) { int i; for (i = 0; used & (1 << i); i++) ; theta.push_back(arg(G[i] - s)); theta.push_back(arg(G[(i + 1) % n] - s)); return; } rep(i, n) if (!(used & (1 << i))) { theta.push_back(arg(G[i] - s)); theta.push_back(arg(G[(i + 1) % n] - s)); segment e(G[i], G[(i + 1) % n]); polygon H(n); rep(j, n) { point p = G[j]; // p を e に対称な位置に移す double phi = arg(e.b - e.a); point tmp = rot(p - e.a, -phi); tmp.y *= -1; H[j] = rot(tmp, phi) + e.a; } dfs(s, cnt + 1, used | (1 << i), H, theta); } } bool eq(double a, double b) { return abs(a - b) < EPS; } int main() { for (int n; scanf("%d", &n), n;) { point s; scanf("%lf%lf", &s.x, &s.y); polygon F(n); rep(i, n) scanf("%lf%lf", &F[i].x, &F[i].y); vector<double> theta; dfs(s, 0, 0, F, theta); sort(theta.begin(), theta.end()); theta.erase(unique(theta.begin(), theta.end(), eq), theta.end()); theta.push_back(theta[0] + 2 * PI); vector<double> phi; rep(i, (int)theta.size() - 1) phi.push_back((theta[i] + theta[i + 1]) / 2); set<vector<int>> ans; // TLE が厳しいので 1 つ置きにシミュレーション. 2 // つが同じ結果ならスキップしたものも同じ結果 vector<int> prev; static bool skip[200000]; rep(i, phi.size()) skip[i] = false; rep(i, phi.size() / 2) { vector<int> hit = simulate(s, F, phi[2 * i]); if (hit.size() == n && is_perm(hit)) { ans.insert(hit); if (hit == prev && phi[2 * i] - phi[2 * i - 2] < 1e-5) skip[2 * i - 1] = true; } prev = hit; } rep(i, phi.size() / 2 + 1) { if (2 * i + 1 < phi.size() && !skip[2 * i + 1]) { vector<int> hit = simulate(s, F, phi[2 * i + 1]); if (hit.size() == n && is_perm(hit)) { ans.insert(hit); } } } printf("%d\n", ans.size()); } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <set> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; const double EPS = 1e-8; const double PI = acos(-1); struct point { double x, y; point() : x(0), y(0) {} point(double x, double y) : x(x), y(y) {} point operator+(const point &a) const { return point(x + a.x, y + a.y); } point operator-(const point &a) const { return point(x - a.x, y - a.y); } bool operator==(const point &a) const { return abs(x - a.x) < EPS && abs(y - a.y) < EPS; } bool operator!=(const point &a) const { return abs(x - a.x) > EPS || abs(y - a.y) > EPS; } }; point operator*(double c, const point &a) { return point(c * a.x, c * a.y); } inline double dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } inline double cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } inline point rot(const point &a, double theta) { return point(a.x * cos(theta) - a.y * sin(theta), a.x * sin(theta) + a.y * cos(theta)); } inline double arg(const point &a) { double t = atan2(a.y, a.x); return t < 0 ? t + 2 * PI : t; } struct segment { point a, b; segment() {} segment(const point &a, const point &b) : a(a), b(b) {} }; typedef vector<point> polygon; enum { CCW = 1, CW = -1, ON = 0 }; inline int ccw(const point &a, const point &b, const point &c) { double rdir = cross(b - a, c - a); if (rdir > EPS) return CCW; if (rdir < -EPS) return CW; return ON; } inline bool intersect(const segment &S1, const segment &S2) { if (max(S1.a.x, S1.b.x) + EPS < min(S2.a.x, S2.b.x) || max(S1.a.y, S1.b.y) + EPS < min(S2.a.y, S2.b.y) || max(S2.a.x, S2.b.x) + EPS < min(S1.a.x, S1.b.x) || max(S2.a.y, S2.b.y) + EPS < min(S1.a.y, S1.b.y)) return false; return ccw(S1.a, S1.b, S2.a) * ccw(S1.a, S1.b, S2.b) <= 0 && ccw(S2.a, S2.b, S1.a) * ccw(S2.a, S2.b, S1.b) <= 0; } inline bool cover(const segment &S, const point &p) { return abs(cross(S.a - p, S.b - p)) < EPS && dot(S.a - p, S.b - p) < EPS; } inline point get_intersect(const segment &S1, const segment &S2) { double a1 = cross(S1.b - S1.a, S2.b - S2.a); double a2 = cross(S1.b - S1.a, S1.b - S2.a); if (abs(a1) < EPS) { if (cover(S1, S2.a)) return S2.a; if (cover(S1, S2.b)) return S2.b; if (cover(S2, S1.a)) return S1.a; return S1.b; } return S2.a + a2 / a1 * (S2.b - S2.a); } bool is_perm(vector<int> a) { sort(a.begin(), a.end()); return unique(a.begin(), a.end()) - a.begin() == a.size(); } vector<int> simulate(point p, const polygon &F, double theta) { int n = F.size(); vector<int> res; bool used[8] = {}; while (res.size() < n) { point v(cos(theta), sin(theta)); segment S(p, p + 200 * v); int i0 = -1; segment e; // 反射する辺 point q; // 反射する点 rep(i, n) { e = segment(F[i], F[(i + 1) % n]); if (intersect(S, e)) { q = get_intersect(S, e); if (q != p) { i0 = i; break; } } } assert(i0 != -1); if (used[i0]) return res; // 同じ辺に二回反射したらダメ used[i0] = true; if (q == e.a || q == e.b) return res; // 角に当たったらダメ res.push_back(i0); // 反射角を求める double phi = arg(e.b - e.a); point dir = rot(q - p, -phi); dir.y *= -1; dir = rot(dir, phi); theta = arg(dir); p = q; } return res; } void dfs(point s, int cnt, int used, polygon G, vector<double> &theta) { int n = G.size(); if (cnt == n - 1) { int i; for (i = 0; used & (1 << i); i++) ; theta.push_back(arg(G[i] - s)); theta.push_back(arg(G[(i + 1) % n] - s)); return; } rep(i, n) if (!(used & (1 << i))) { theta.push_back(arg(G[i] - s)); theta.push_back(arg(G[(i + 1) % n] - s)); segment e(G[i], G[(i + 1) % n]); polygon H(n); rep(j, n) { point p = G[j]; // p を e に対称な位置に移す double phi = arg(e.b - e.a); point tmp = rot(p - e.a, -phi); tmp.y *= -1; H[j] = rot(tmp, phi) + e.a; } dfs(s, cnt + 1, used | (1 << i), H, theta); } } bool eq(double a, double b) { return abs(a - b) < EPS; } int main() { for (int n; scanf("%d", &n), n;) { point s; scanf("%lf%lf", &s.x, &s.y); polygon F(n); rep(i, n) scanf("%lf%lf", &F[i].x, &F[i].y); vector<double> theta; dfs(s, 0, 0, F, theta); sort(theta.begin(), theta.end()); theta.erase(unique(theta.begin(), theta.end(), eq), theta.end()); theta.push_back(theta[0] + 2 * PI); vector<double> phi; rep(i, (int)theta.size() - 1) phi.push_back((theta[i] + theta[i + 1]) / 2); set<vector<int>> ans; // TLE が厳しいので 1 つ置きにシミュレーション. 2 // つが同じ結果ならスキップしたものも同じ結果 vector<int> prev; static bool skip[200000]; rep(i, phi.size()) skip[i] = false; rep(i, phi.size() / 2) { vector<int> hit = simulate(s, F, phi[2 * i]); if (hit.size() == n && is_perm(hit)) { ans.insert(hit); if (hit == prev) skip[2 * i - 1] = true; } prev = hit; } rep(i, phi.size() / 2 + 1) { if (2 * i + 1 < phi.size() && !skip[2 * i + 1]) { vector<int> hit = simulate(s, F, phi[2 * i + 1]); if (hit.size() == n && is_perm(hit)) { ans.insert(hit); } } } printf("%d\n", ans.size()); } return 0; }
replace
203
204
203
204
TLE
p01682
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) typedef long long ll; int const MOD = 1e9 + 7; string line; string::iterator iter; inline void debug() { cout << "Remain: \"" << string(iter, line.end()) << '"' << endl; } void consume(char e) { if (*iter == e) { iter++; } else { assert(0); } } bool ifconsume(char e) { if (*iter == e) { consume(e); return 1; } return 0; } bool strcons(string s) { string::iterator is = s.begin(); string::iterator tmpiter = iter; while (is != s.end() && tmpiter != line.end() && *is == *tmpiter) { tmpiter++; is++; } if (is == s.end()) { iter = tmpiter; return true; } return false; } ll get_num() { ll ret = 0; while (isdigit(*iter)) { ret *= 10; ret += (*iter) - '0'; consume(*iter); } return ret; } ll expr(); ll term() { if (isdigit(*iter)) { return get_num(); } else if (ifconsume('S')) { consume('<'); ll r = expr(); (r *= r) %= MOD; consume('>'); return r; } else { debug(); assert(0 && "term"); } } ll expr() { ll t1 = term(); while (strcons(">>")) { if (*iter != 'S' && !isdigit(*iter)) { iter -= 2; break; } int k = term(); while (k--) { t1 >>= 1; } } return t1; } int main() { for (string s; getline(cin, s);) { if (s == "#") break; line.clear(); int sz = s.size(); rep(i, sz) if (s[i] != ' ') { line.push_back(s[i]); } iter = line.begin(); cout << expr() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) typedef long long ll; int const MOD = 1e9 + 7; string line; string::iterator iter; inline void debug() { cout << "Remain: \"" << string(iter, line.end()) << '"' << endl; } void consume(char e) { if (*iter == e) { iter++; } else { assert(0); } } bool ifconsume(char e) { if (*iter == e) { consume(e); return 1; } return 0; } bool strcons(string s) { string::iterator is = s.begin(); string::iterator tmpiter = iter; while (is != s.end() && tmpiter != line.end() && *is == *tmpiter) { tmpiter++; is++; } if (is == s.end()) { iter = tmpiter; return true; } return false; } ll get_num() { ll ret = 0; while (isdigit(*iter)) { ret *= 10; ret += (*iter) - '0'; consume(*iter); } return ret; } ll expr(); ll term() { if (isdigit(*iter)) { return get_num(); } else if (ifconsume('S')) { consume('<'); ll r = expr(); (r *= r) %= MOD; consume('>'); return r; } else { debug(); assert(0 && "term"); } } ll expr() { ll t1 = term(); while (strcons(">>")) { if (*iter != 'S' && !isdigit(*iter)) { iter -= 2; break; } int k = term(); while (k--) { t1 >>= 1; if (t1 == 0) break; } } return t1; } int main() { for (string s; getline(cin, s);) { if (s == "#") break; line.clear(); int sz = s.size(); rep(i, sz) if (s[i] != ' ') { line.push_back(s[i]); } iter = line.begin(); cout << expr() << endl; } return 0; }
insert
84
84
84
86
TLE
p01682
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).*begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; const int INF = 150000000; const long long M = 1000000007; typedef string::const_iterator State; int number(int &); int term(int &); int expression(int &); string s; // ??°??????????????????????????????????????°???????????? int number(int &i) { int ret = 0; while (isdigit(s[i])) { ret *= 10; ret += s[i] - '0'; i++; } return ret; } // ????????????????????????????????????????????????????????????????????? int term(int &i) { if (s[i] == 'S') { i += 2; long long ret = expression(i); i++; return ret * ret % M; } else { return number(i); } return INF; } // ????????????????????????????????????????????????????????????????????? int expression(int &i) { int ret = term(i); while (i < s.size() - 2) { if (s[i] == '>' && s[i + 1] == '>' && s[i + 2] != '>') { i += 2; ret = ret >> min(term(i), 31); } else { return ret; } } return ret; } inline void deleteSpace(string &a) { size_t pos; while ((pos = a.find_first_of(" ")) != string::npos) { a.erase(pos, 1); } } int main() { std::ifstream ifs("/Users/noy/Downloads/2570_in1.txt.html"); while (getline(cin, s), s != "#") { deleteSpace(s); int i = 0; cout << expression(i) << endl; } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).*begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; const int INF = 150000000; const long long M = 1000000007; typedef string::const_iterator State; int number(int &); int term(int &); int expression(int &); string s; // ??°??????????????????????????????????????°???????????? int number(int &i) { int ret = 0; while (isdigit(s[i])) { ret *= 10; ret += s[i] - '0'; i++; } return ret; } // ????????????????????????????????????????????????????????????????????? int term(int &i) { if (s[i] == 'S') { i += 2; long long ret = expression(i); i++; return ret * ret % M; } else { return number(i); } return INF; } // ????????????????????????????????????????????????????????????????????? int expression(int &i) { int ret = term(i); while (i < s.size() - 2) { if (s[i] == '>' && s[i + 1] == '>' && s[i + 2] != '>') { i += 2; ret = ret >> min(term(i), 31); } else { return ret; } } return ret; } inline void deleteSpace(string &a) { size_t pos; while ((pos = a.find_first_of(" ")) != string::npos) { a.erase(pos, 1); } } int main() { std::ifstream ifs("/Users/noy/Downloads/2570_in1.txt.html"); while (getline(cin, s), s != "#") { // deleteSpace(s); string tmp; for (auto it : s) { if (it != ' ') tmp += it; } s = tmp; int i = 0; cout << expression(i) << endl; } }
replace
69
70
69
76
TLE
p01682
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int MOD = 1000000007LL; string s; int expr(int &p); int term(int &p); void sp(int &p); int number(int &p); int expr(int &p) { int res = term(p); sp(p); int q = p; while (q + 1 < (int)s.size() && s[q] == '>' && s[q + 1] == '>') { q += 2; sp(q); if (q >= (int)s.size() || !isdigit(s[q])) break; p += 2; sp(p); int y = term(p); if (y > 31) res = 0; else res >>= y; sp(p); q = p; } return res; } int term(int &p) { int res; if (s[p] == 'S') { p++; sp(p); assert(s[p] == '<'); p++; sp(p); int x = expr(p); sp(p); assert(s[p] == '>'); p++; res = (x * x) % MOD; } else { res = number(p); } return res; } void sp(int &p) { while (p < (int)s.size() && s[p] == ' ') p++; } int number(int &p) { int res = 0; while (p < (int)s.size() && isdigit(s[p])) res = (res * 10 + s[p++] - '0') % MOD; return res; } signed main() { while (getline(cin, s), s != "#") { int p = 0; cout << expr(p) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int MOD = 1000000007LL; string s; int expr(int &p); int term(int &p); void sp(int &p); int number(int &p); int expr(int &p) { int res = term(p); sp(p); int q = p; while (q + 1 < (int)s.size() && s[q] == '>' && s[q + 1] == '>') { q += 2; sp(q); if (q >= (int)s.size() || s[q] == '>') break; p += 2; sp(p); int y = term(p); if (y > 31) res = 0; else res >>= y; sp(p); q = p; } return res; } int term(int &p) { int res; if (s[p] == 'S') { p++; sp(p); assert(s[p] == '<'); p++; sp(p); int x = expr(p); sp(p); assert(s[p] == '>'); p++; res = (x * x) % MOD; } else { res = number(p); } return res; } void sp(int &p) { while (p < (int)s.size() && s[p] == ' ') p++; } int number(int &p) { int res = 0; while (p < (int)s.size() && isdigit(s[p])) res = (res * 10 + s[p++] - '0') % MOD; return res; } signed main() { while (getline(cin, s), s != "#") { int p = 0; cout << expr(p) << endl; } return 0; }
replace
16
17
16
17
0
p01683
C++
Runtime Error
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define pb push_back typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<int, ll> i_ll; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; typedef vector<int> vi; struct edge { int v; ll w; }; ll _MOD = 1000000009; double EPS = 1e-10; ll INF = LLONG_MAX / 2; int main() { for (;;) { int n; cin >> n; if (!n) break; vector<int> a(n), b(n); rep(u, n) scanf("%d%d", &a[u], &b[u]); if (n == 2) { cout << abs(a[0] - a[1]) << endl; continue; } vector<i_i> ab; vector<int> c; rep(u, n) if (b[u] == 1) c.pb(a[u]); else ab.pb(i_i(a[u], b[u] - 2)); int N = ab.size(), M = c.size(); sort(ab.begin(), ab.end()); ab.front().second++; ab.back().second++; sort(c.begin(), c.end()); vector<ll> dp(M + 1, INF); dp[0] = 0; rep(i, N) { int a = ab[i].first, b = ab[i].second; vector<ll> _dp(M + 1, INF); deque<i_ll> d; ll sum = 0; rep(j, M + 1) { if (!d.empty() && j - d.front().first > b) d.pop_front(); while (!d.empty() && d.back().second >= dp[j] - sum) d.pop_back(); d.push_back(i_ll(j, dp[j] - sum)); if (!d.empty()) _dp[j] = d.front().second + sum; if (j < M) sum += abs(c[j] - a); } dp = _dp; } ll ans = dp[M] + ab.back().first - ab.front().first; if (ans >= INF) ans = -1; cout << ans << endl; } }
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define pb push_back typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<int, ll> i_ll; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; typedef vector<int> vi; struct edge { int v; ll w; }; ll _MOD = 1000000009; double EPS = 1e-10; ll INF = LLONG_MAX / 2; int main() { for (;;) { int n; cin >> n; if (!n) break; vector<int> a(n), b(n); rep(u, n) scanf("%d%d", &a[u], &b[u]); if (n == 2) { cout << abs(a[0] - a[1]) << endl; continue; } vector<i_i> ab; vector<int> c; rep(u, n) if (b[u] == 1) c.pb(a[u]); else ab.pb(i_i(a[u], b[u] - 2)); int N = ab.size(), M = c.size(); if (!N) { cout << -1 << endl; continue; } sort(ab.begin(), ab.end()); ab.front().second++; ab.back().second++; sort(c.begin(), c.end()); vector<ll> dp(M + 1, INF); dp[0] = 0; rep(i, N) { int a = ab[i].first, b = ab[i].second; vector<ll> _dp(M + 1, INF); deque<i_ll> d; ll sum = 0; rep(j, M + 1) { if (!d.empty() && j - d.front().first > b) d.pop_front(); while (!d.empty() && d.back().second >= dp[j] - sum) d.pop_back(); d.push_back(i_ll(j, dp[j] - sum)); if (!d.empty()) _dp[j] = d.front().second + sum; if (j < M) sum += abs(c[j] - a); } dp = _dp; } ll ans = dp[M] + ab.back().first - ab.front().first; if (ans >= INF) ans = -1; cout << ans << endl; } }
insert
60
60
60
64
0
p01683
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef pair<int, int> P; int main() { int n; while (cin >> n && n > 0) { if (n == 1) { cout << 0 << endl; continue; } if (n == 2) { int p1, t1, p2, t2; cin >> p1 >> t1 >> p2 >> t2; cout << abs(p1 - p2) << endl; continue; } vector<long long> p1; vector<long long> p2; vector<P> pv; REP(i, n) { int p, t; cin >> p >> t; if (t == 1) { p1.push_back(p); } else { pv.push_back(P(p, t)); } } sort(pv.begin(), pv.end()); REP(i, pv.size() - 1) pv[i].second--; REP(i, pv.size() - 1) pv[i + 1].second--; REP(i, pv.size()) { REP(_, pv[i].second) { p2.push_back(pv[i].first); } } int n1 = p1.size(); int n2 = p2.size(); if (n1 > n2) { cout << -1 << endl; continue; } sort(p1.begin(), p1.end()); sort(p2.begin(), p2.end()); long long ans = *max_element(p2.begin(), p2.end()) - *min_element(p2.begin(), p2.end()); // cout << ans << endl; // cout << "p1 : "; REP(i, p1.size()) cout << p1[i] << " "; cout << endl; // cout << "p2 : "; REP(i, p2.size()) cout << p2[i] << " "; cout << endl; vector<long long> dp(2 * n1, 0); int last_base2 = -1; for (int i1 = 0; i1 < n1; i1++) { vector<long long> next(2 * n1, LLONG_MAX); int base2 = lower_bound(p2.begin(), p2.end(), p1[i1]) - p2.begin(); for (int di = 0; di < 2 * n1; di++) { int i2 = di - n1 + base2; if (i2 >= 0 && i2 < n2) { if (i1 == 0) { next[di] = abs(p1[i1] - p2[i2]); continue; } int lastdi = di + (base2 - last_base2) - 1; if (lastdi < 0) continue; long long last = (lastdi < 2 * n1 ? dp[lastdi] : dp.back()); if (last == LLONG_MAX) continue; next[di] = last + abs(p1[i1] - p2[i2]); // printf("i1 = %d i2 = %d di = %d base = %d lastdi = %d last = %lld // next[%d] <- %lld + %lld = %lld\n", i1, i2, di, base2, lastdi, last, // di, last, abs(p1[i1] - p2[i2]), next[di]); } } for (int j = 1; j < 2 * n1; j++) { next[j] = min(next[j], next[j - 1]); } last_base2 = base2; dp.swap(next); } cout << ans + (n1 != 0 ? dp.back() : 0) << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef pair<int, int> P; int main() { int n; while (cin >> n && n > 0) { if (n == 1) { cout << 0 << endl; continue; } if (n == 2) { int p1, t1, p2, t2; cin >> p1 >> t1 >> p2 >> t2; cout << abs(p1 - p2) << endl; continue; } vector<int> p1; vector<int> p2; p2.reserve(n * n); vector<P> pv; REP(i, n) { int p, t; cin >> p >> t; if (t == 1) { p1.push_back(p); } else { pv.push_back(P(p, t)); } } sort(pv.begin(), pv.end()); REP(i, pv.size() - 1) pv[i].second--; REP(i, pv.size() - 1) pv[i + 1].second--; REP(i, pv.size()) { REP(_, pv[i].second) { p2.push_back(pv[i].first); } } int n1 = p1.size(); int n2 = p2.size(); if (n1 > n2) { cout << -1 << endl; continue; } sort(p1.begin(), p1.end()); sort(p2.begin(), p2.end()); long long ans = *max_element(p2.begin(), p2.end()) - *min_element(p2.begin(), p2.end()); // cout << ans << endl; // cout << "p1 : "; REP(i, p1.size()) cout << p1[i] << " "; cout << endl; // cout << "p2 : "; REP(i, p2.size()) cout << p2[i] << " "; cout << endl; vector<long long> dp(2 * n1, 0); int last_base2 = -1; for (int i1 = 0; i1 < n1; i1++) { vector<long long> next(2 * n1, LLONG_MAX); int base2 = lower_bound(p2.begin(), p2.end(), p1[i1]) - p2.begin(); for (int di = 0; di < 2 * n1; di++) { int i2 = di - n1 + base2; if (i2 >= 0 && i2 < n2) { if (i1 == 0) { next[di] = abs(p1[i1] - p2[i2]); continue; } int lastdi = di + (base2 - last_base2) - 1; if (lastdi < 0) continue; long long last = (lastdi < 2 * n1 ? dp[lastdi] : dp.back()); if (last == LLONG_MAX) continue; next[di] = last + abs(p1[i1] - p2[i2]); // printf("i1 = %d i2 = %d di = %d base = %d lastdi = %d last = %lld // next[%d] <- %lld + %lld = %lld\n", i1, i2, di, base2, lastdi, last, // di, last, abs(p1[i1] - p2[i2]), next[di]); } } for (int j = 1; j < 2 * n1; j++) { next[j] = min(next[j], next[j - 1]); } last_base2 = base2; dp.swap(next); } cout << ans + (n1 != 0 ? dp.back() : 0) << endl; } return 0; }
replace
19
21
19
22
TLE
p01687
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, e) for (int i = (s); i < (e); i++) #define pb(n) push_back((n)) #define mp(n, m) make_pair((n), (m)) #define all(r) r.begin(), r.end() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 10000000; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; string s; string t = "AIZUNYAN", u = "AIDUNYAN"; vi w(26, 0); bool calc(int n) { vi v(26, 0); for (int i = 0; i < 8 && i < s.size(); i++) v[s[i + n] - 'A']++; for (int i = 0; i < 26; i++) { if (v[i] != w[i]) return false; ; } for (int i = 0; i < 8; i++) { s[i + n] = t[i]; } return true; } int main() { for (int i = 0; i < u.size(); i++) w[u[i] - 'A']++; cin >> s; for (int i = 0; i < s.size(); i++) { bool f = calc(i); if (f) i += 7; } cout << s << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, e) for (int i = (s); i < (e); i++) #define pb(n) push_back((n)) #define mp(n, m) make_pair((n), (m)) #define all(r) r.begin(), r.end() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 10000000; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; string s; string t = "AIZUNYAN", u = "AIDUNYAN"; vi w(26, 0); bool calc(int n) { vi v(26, 0); for (int i = 0; i < 8 && i + n < s.size(); i++) v[s[i + n] - 'A']++; for (int i = 0; i < 26; i++) { if (v[i] != w[i]) return false; ; } for (int i = 0; i < 8; i++) { s[i + n] = t[i]; } return true; } int main() { for (int i = 0; i < u.size(); i++) w[u[i] - 'A']++; cin >> s; for (int i = 0; i < s.size(); i++) { bool f = calc(i); if (f) i += 7; } cout << s << endl; }
replace
44
45
44
45
0
p01694
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; int n; void solve() { vector<string> f(n); for (int i = 0; i < n; i++) cin >> f[i]; int l = 0, r = 0; int ans = 0; for (int i = 0; i < n; i++) { if (f[i] == "lu" and f[i + 1] == "ru") ans++; else if (f[i] == "ru" and f[i + 1] == "lu") ans++; else if (f[i] == "ld" and f[i + 1] == "rd") ans++; else if (f[i] == "rd" and f[i + 1] == "ld") ans++; } cout << ans << endl; } int main() { while (1) { cin >> n; if (n == 0) break; solve(); } }
#include <iostream> #include <string> #include <vector> using namespace std; int n; void solve() { vector<string> f(n); for (int i = 0; i < n; i++) cin >> f[i]; int l = 0, r = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i + 1 >= n) break; if (f[i] == "lu" and f[i + 1] == "ru") ans++; else if (f[i] == "ru" and f[i + 1] == "lu") ans++; else if (f[i] == "ld" and f[i + 1] == "rd") ans++; else if (f[i] == "rd" and f[i + 1] == "ld") ans++; } cout << ans << endl; } int main() { while (1) { cin >> n; if (n == 0) break; solve(); } }
insert
15
15
15
17
-11
p01694
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /*{{{*/ // template #define rep(i, n) for (int i = 0; i < n; i++) constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long LINF = numeric_limits<long long>::max() / 3; #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define all(v) (v).begin(), (v).end() #define sz(x) (int)(x).size() #define debug(x) cerr << #x << ":" << x << endl #define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl // struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_; struct Double { double d; explicit Double(double x) : d(x) {} }; ostream &operator<<(ostream &os, const Double x) { os << fixed << setprecision(20) << x.d; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; for (T v : st) os << v << ","; os << "}"; return os; } template <typename T, typename U> inline void chmax(T &x, U y) { if (y > x) x = y; } template <typename T, typename U> inline void chmin(T &x, U y) { if (y < x) x = y; } typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } // constexpr double eps = 1e-14; constexpr double eps = 1e-10; constexpr ll mod = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; /*}}}*/ int n; void solve() { int ans = 0; vector<string> vs(n); rep(i, n) cin >> vs[i]; for (int i = 0; i < n; i += 2) { string s1 = vs[i]; string s2 = vs[i + 1]; if (s1 == "lu" and s2 == "ru") ans++; else if (s1 == "ru" and s2 == "lu") ans++; else if (s1 == "ld" and s2 == "rd") ans++; else if (s1 == "rd" and s2 == "ld") ans++; } cout << ans << endl; } int main() { while (1) { cin >> n; if (n == 0) break; solve(); } }
#include <bits/stdc++.h> using namespace std; /*{{{*/ // template #define rep(i, n) for (int i = 0; i < n; i++) constexpr int INF = numeric_limits<int>::max() / 2; constexpr long long LINF = numeric_limits<long long>::max() / 3; #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define all(v) (v).begin(), (v).end() #define sz(x) (int)(x).size() #define debug(x) cerr << #x << ":" << x << endl #define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl // struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_; struct Double { double d; explicit Double(double x) : d(x) {} }; ostream &operator<<(ostream &os, const Double x) { os << fixed << setprecision(20) << x.d; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { os << "{"; for (T v : st) os << v << ","; os << "}"; return os; } template <typename T, typename U> inline void chmax(T &x, U y) { if (y > x) x = y; } template <typename T, typename U> inline void chmin(T &x, U y) { if (y < x) x = y; } typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; ll gcd(ll a, ll b) { if (b == 0) return a; else return gcd(b, a % b); } // constexpr double eps = 1e-14; constexpr double eps = 1e-10; constexpr ll mod = 1e9 + 7; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; /*}}}*/ int n; void solve() { int ans = 0; vector<string> vs(n); rep(i, n) cin >> vs[i]; for (int i = 0; i + 1 < n; i += 2) { string s1 = vs[i]; string s2 = vs[i + 1]; if (s1 == "lu" and s2 == "ru") ans++; else if (s1 == "ru" and s2 == "lu") ans++; else if (s1 == "ld" and s2 == "rd") ans++; else if (s1 == "rd" and s2 == "ld") ans++; } cout << ans << endl; } int main() { while (1) { cin >> n; if (n == 0) break; solve(); } }
replace
76
77
76
77
-11
p01695
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { vector<string> s(n); for (auto &e : s) { cin >> e; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; s[i][j] == '.'; j++) { if (j + 1 < s[i].size() && s[i][j + 1] != '.') { s[i][j] = '+'; } else if (i + 1 < n && s[i + 1][j] == '+' || s[i + 1][j] == '|') { s[i][j] = '|'; } else { s[i][j] = ' '; } } } for (auto e : s) { cout << e << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { vector<string> s(n); for (auto &e : s) { cin >> e; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; s[i][j] == '.'; j++) { if (j + 1 < s[i].size() && s[i][j + 1] != '.') { s[i][j] = '+'; } else if (i + 1 < n && j < s[i + 1].size() && (s[i + 1][j] == '+' || s[i + 1][j] == '|')) { s[i][j] = '|'; } else { s[i][j] = ' '; } } } for (auto e : s) { cout << e << endl; } } }
replace
14
15
14
16
-11
p01695
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define all(c) c.begin(), c.end() #define sz(c) ((int)c.size()) using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; signed main() { while (1) { int n; cin >> n; if (n == 0) break; vector<string> s(n); rep(i, 0, n) { cin >> s[i]; } rep(i, 0, n) { if (s[i][0] != '.') continue; rep(j, 0, sz(s[i]) - 1) { if (s[i][j + 1] != '.') { int r = i - 1; while (r >= 0 && s[r][j] == '.') { s[r][j] = '|'; r--; } s[i][j] = '+'; break; } } } rep(i, 0, n) { rep(j, 0, sz(s)) { if (s[i][j] == '.') s[i][j] = ' '; } cout << s[i] << endl; } } }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define all(c) c.begin(), c.end() #define sz(c) ((int)c.size()) using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; signed main() { while (1) { int n; cin >> n; if (n == 0) break; vector<string> s(n); rep(i, 0, n) { cin >> s[i]; } rep(i, 0, n) { if (s[i][0] != '.') continue; rep(j, 0, sz(s[i]) - 1) { if (s[i][j + 1] != '.') { int r = i - 1; while (r >= 0 && s[r][j] == '.') { s[r][j] = '|'; r--; } s[i][j] = '+'; break; } } } rep(i, 0, n) { rep(j, 0, sz(s[i])) { if (s[i][j] == '.') s[i][j] = ' '; } cout << s[i] << endl; } } }
replace
35
36
35
36
0
p01695
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = m; i < (int)(n); i++) #define if_range(x, y, w, h) \ if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \ (int)(y) < (int)(h)) const int INF = 1e9; const double EPS = 1e-8; const double PI = 3.14159; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; typedef long long ll; // typedef pair<int, int> P; /* struct P { int x, y, n; P(int n, int x, int y):n(n), x(x), y(y){} P(){} }; */ /** Problem2583 : JAG-cannel **/ int main() { int N; while (cin >> N, N) { vector<string> s(N); rep(i, N) cin >> s[i]; REP(i, 1, N) { int j = 0; for (; j < s[i].size(); j++) { if (s[i][j + 1] != '.') { s[i][j] = '+'; break; } } for (int k = i; k > 0; k--) { if (s[k - 1][j] == '.') { s[k - 1][j] = '|'; } } } rep(i, N) { rep(j, s[i].size()) { if (s[i][j] == '.') s[i][j] = ' '; } } rep(i, N) { cout << s[i] << endl; } } }
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = m; i < (int)(n); i++) #define if_range(x, y, w, h) \ if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \ (int)(y) < (int)(h)) const int INF = 1e9; const double EPS = 1e-8; const double PI = 3.14159; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; typedef long long ll; // typedef pair<int, int> P; /* struct P { int x, y, n; P(int n, int x, int y):n(n), x(x), y(y){} P(){} }; */ /** Problem2583 : JAG-cannel **/ int main() { int N; while (cin >> N, N) { vector<string> s(N); rep(i, N) cin >> s[i]; REP(i, 1, N) { int j = 0; for (; j < s[i].size(); j++) { if (s[i][j + 1] != '.') { s[i][j] = '+'; break; } } for (int k = i - 1; k > 0; k--) { if (s[k][j] == '.') { s[k][j] = '|'; } else { break; } } } rep(i, N) { rep(j, s[i].size()) { if (s[i][j] == '.') s[i][j] = ' '; } } rep(i, N) { cout << s[i] << endl; } } }
replace
53
56
53
58
0
p01695
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)(a.size())) int n; string in[100]; void solve() { rep(i, n) cin >> in[i]; rep(i, n) { int idx = -1; rep(j, SZ(in[i])) { if (in[i][j] != '.') { if (j) { in[i][j - 1] = '+'; idx = j - 1; } break; } } if (idx >= 0) for (int k = i - 1; k; --k) { if (isalpha(in[k][idx]) || in[k][idx] == '+') break; in[k][idx] = '|'; } } rep(i, n) for (auto &c : in[i]) if (c == '.') c = ' '; rep(i, n) cout << in[i] << endl; } int main(int argc, char *argv[]) { while (cin >> n && n) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) #define SZ(a) ((int)(a.size())) int n; string in[10000]; void solve() { rep(i, n) cin >> in[i]; rep(i, n) { int idx = -1; rep(j, SZ(in[i])) { if (in[i][j] != '.') { if (j) { in[i][j - 1] = '+'; idx = j - 1; } break; } } if (idx >= 0) for (int k = i - 1; k; --k) { if (isalpha(in[k][idx]) || in[k][idx] == '+') break; in[k][idx] = '|'; } } rep(i, n) for (auto &c : in[i]) if (c == '.') c = ' '; rep(i, n) cout << in[i] << endl; } int main(int argc, char *argv[]) { while (cin >> n && n) solve(); return 0; }
replace
7
8
7
8
0
p01696
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <queue> #include <stack> #include <string.h> #define MAX_N 1000 using namespace std; char str[100], res[100]; void reverse(char *str, char *res) { for (int i = 0; i < strlen(str); i++) { res[i] = str[strlen(str) - i - 1]; } res[strlen(str)] = '\0'; } void dfs(int l, int r, char *res) { int prel = l; char tmp1[100], tmp2[100], count = 0; res[0] = '\0'; if (l + 1 == r) { res[0] = str[l]; if (res[0] == '?') res[0] = 'A'; res[1] = '\0'; return; } for (int i = l; i < r;) { if (str[i] == '+' || str[i] == '-' || (str[i] >= 'A' && str[i] <= 'Z')) { int j = i; while (str[j] == '+' || str[j] == '-') j++; if (i == l && j == r - 1) { res[0] = str[r - 1]; if (res[0] == '?') { res[0] = 'A'; } else { for (int k = l; k < r - 1; k++) { if (str[k] == '+') { res[0]++; if (res[0] > 'Z') { res[0] = 'A'; } } else { res[0]--; if (res[0] < 'A') { res[0] = 'Z'; } } } } res[1] = '\0'; break; } else { dfs(i, j + 1, tmp1); strcat(res, tmp1); i = j + 1; } } if (str[i] == '[') { int j, count = 0; for (j = i; j < r; j++) { if (str[j] == '[') { count++; } else if (str[j] == ']') { count--; } if (count == 0) break; } dfs(i + 1, j, tmp1); reverse(tmp1, tmp2); strcat(res, tmp2); i = j + 1; } } } int main() { while (1) { cin >> str; if (str[0] == '.') break; dfs(0, strlen(str), res); cout << res << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <queue> #include <stack> #include <string.h> #define MAX_N 1000 using namespace std; char str[100], res[100]; void reverse(char *str, char *res) { for (int i = 0; i < strlen(str); i++) { res[i] = str[strlen(str) - i - 1]; } res[strlen(str)] = '\0'; } void dfs(int l, int r, char *res) { int prel = l; char tmp1[100], tmp2[100], count = 0; res[0] = '\0'; if (l + 1 == r) { res[0] = str[l]; if (res[0] == '?') res[0] = 'A'; res[1] = '\0'; return; } for (int i = l; i < r;) { if (str[i] == '+' || str[i] == '-' || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '?') { int j = i; while (str[j] == '+' || str[j] == '-') j++; if (i == l && j == r - 1) { res[0] = str[r - 1]; if (res[0] == '?') { res[0] = 'A'; } else { for (int k = l; k < r - 1; k++) { if (str[k] == '+') { res[0]++; if (res[0] > 'Z') { res[0] = 'A'; } } else { res[0]--; if (res[0] < 'A') { res[0] = 'Z'; } } } } res[1] = '\0'; break; } else { dfs(i, j + 1, tmp1); strcat(res, tmp1); i = j + 1; } } if (str[i] == '[') { int j, count = 0; for (j = i; j < r; j++) { if (str[j] == '[') { count++; } else if (str[j] == ']') { count--; } if (count == 0) break; } dfs(i + 1, j, tmp1); reverse(tmp1, tmp2); strcat(res, tmp2); i = j + 1; } } } int main() { while (1) { cin >> str; if (str[0] == '.') break; dfs(0, strlen(str), res); cout << res << endl; } return 0; }
replace
35
36
35
37
TLE
p01697
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define abs(a) max((a), -(a)) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repe(i, n) rep(i, (n) + 1) #define per(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define pere(i, n) rep(i, (n) + 1) #define all(x) (x).begin(), (x).end() #define SP << " " << #define RET return 0 #define MOD 1000000007 #define INF 1000000000 typedef long long LL; typedef long double LD; struct edge { int t; int h; int c; int r; }; int main() { while (1) { int n, m, h, k; cin >> n >> m >> h >> k; if (n == 0) return 0; vector<list<edge>> e(n); int a, b, c, hh, r; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> hh >> r; e[a - 1].push_back({b - 1, hh, c, r - 1}); e[b - 1].push_back({a - 1, hh, c, r - 1}); } int s, t; cin >> s >> t; s--, t--; int p; cin >> p; vector<int> val(1 << k, INF); val[0] = 0; int l, x, d, kk; for (int i = 0; i < p; i++) { cin >> l >> d; x = 0; for (int j = 0; j < l; j++) { cin >> kk; x |= (1 << (kk - 1)); } for (int j = (1 << k) - 1; j >= 0; j--) { val[j | x] = min(val[j | x], val[j] + d); } } vector<vector<int>> cost(n, vector<int>(h + 1, INF)); int ans = INF; for (int i = 0; i < (1 << k); i++) { for (int j = 0; j < n; j++) { for (int k = 0; k <= h; k++) cost[j][k] = INF; } priority_queue<pair<int, pair<int, int>>> dijk; dijk.push({0, {s, 0}}); pair<int, pair<int, int>> now; while (!dijk.empty()) { now = dijk.top(); dijk.pop(); cost[now.second.first][now.second.second] = -now.first; for (auto ee : e[now.second.first]) { if (now.second.second + ee.h <= h) { if (cost[ee.t][now.second.second + ee.h] == INF) { if (i & (1 << ee.r)) { dijk.push({now.first, {ee.t, now.second.second + ee.h}}); } else { dijk.push({now.first - ee.c, {ee.t, now.second.second + ee.h}}); } } } } } for (int j = 0; j <= h; j++) { ans = min(ans, val[i] + cost[t][j]); } } if (ans != INF) cout << ans << endl; else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define abs(a) max((a), -(a)) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repe(i, n) rep(i, (n) + 1) #define per(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define pere(i, n) rep(i, (n) + 1) #define all(x) (x).begin(), (x).end() #define SP << " " << #define RET return 0 #define MOD 1000000007 #define INF 1000000000 typedef long long LL; typedef long double LD; struct edge { int t; int h; int c; int r; }; int main() { while (1) { int n, m, h, k; cin >> n >> m >> h >> k; if (n == 0) return 0; vector<list<edge>> e(n); int a, b, c, hh, r; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> hh >> r; e[a - 1].push_back({b - 1, hh, c, r - 1}); e[b - 1].push_back({a - 1, hh, c, r - 1}); } int s, t; cin >> s >> t; s--, t--; int p; cin >> p; vector<int> val(1 << k, INF); val[0] = 0; int l, x, d, kk; for (int i = 0; i < p; i++) { cin >> l >> d; x = 0; for (int j = 0; j < l; j++) { cin >> kk; x |= (1 << (kk - 1)); } for (int j = (1 << k) - 1; j >= 0; j--) { val[j | x] = min(val[j | x], val[j] + d); } } vector<vector<int>> cost(n, vector<int>(h + 1, INF)); int ans = INF; for (int i = 0; i < (1 << k); i++) { for (int j = 0; j < n; j++) { for (int k = 0; k <= h; k++) cost[j][k] = INF; } priority_queue<pair<int, pair<int, int>>> dijk; dijk.push({0, {s, 0}}); pair<int, pair<int, int>> now; while (!dijk.empty()) { now = dijk.top(); dijk.pop(); if (cost[now.second.first][now.second.second] == INF) { cost[now.second.first][now.second.second] = -now.first; for (auto ee : e[now.second.first]) { if (now.second.second + ee.h <= h) { if (cost[ee.t][now.second.second + ee.h] == INF) { if (i & (1 << ee.r)) { dijk.push({now.first, {ee.t, now.second.second + ee.h}}); } else { dijk.push( {now.first - ee.c, {ee.t, now.second.second + ee.h}}); } } } } } } for (int j = 0; j <= h; j++) { ans = min(ans, val[i] + cost[t][j]); } } if (ans != INF) cout << ans << endl; else cout << -1 << endl; } return 0; }
replace
71
79
71
82
MLE
p01697
C++
Memory Limit Exceeded
//================================= // Created on: 2018/07/04 16:12:54 //================================= #include <bits/stdc++.h> using namespace std; struct CostGraph { CostGraph(const int n) : V{n}, edge(n) {} void addEdge(const int from, const int to, const int cost, const int tim, const int label) { edge[from].push_back(Edge{from, to, cost, tim, label}); } struct Edge { Edge(const int from, const int to, const int cost, const int tim, const int label) : from{from}, to{to}, cost{cost}, tim{tim}, label{label} {} const int from, to, cost, tim, label; }; const int V; vector<vector<Edge>> edge; }; int main() { for (int N, M, H, K, S, T, P;;) { cin >> N >> M >> H >> K; if (N == 0 and M == 0 and H == 0 and K == 0) { break; } CostGraph g(N); for (int i = 0, a, b, c, h, r; i < M; i++) { cin >> a >> b >> c >> h >> r; a--, b--, r--; g.addEdge(a, b, c, h, r), g.addEdge(b, a, c, h, r); } cin >> S >> T >> P; S--, T--; using pii = pair<int, int>; vector<pii> pass(P); for (int i = 0, l; i < P; i++) { cin >> l >> pass[i].first; for (int j = 0, k; j < l; j++) { cin >> k, pass[i].second |= (1 << (k - 1)); } } constexpr int INF = 1 << 28; const int MASK = 1 << K; vector<int> ans(MASK, INF); for (int mask = 0; mask < MASK; mask++) { using PP = pair<int, pii>; vector<vector<int>> d(N, vector<int>(H + 1, INF)); priority_queue<PP, vector<PP>, greater<PP>> q; d[S][0] = 0, q.push({0, {S, 0}}); while (not q.empty()) { const auto p = q.top(); q.pop(); const int cost = p.first, s = p.second.first, t = p.second.second; if (cost > d[s][t]) { continue; } for (const auto &e : g.edge[s]) { const int dh = e.tim, l = e.label, dc = ((mask & (1 << l)) ? 0 : e.cost); if (t + dh > H) { continue; } if (d[e.to][t + dh] < cost + dc) { continue; } d[e.to][t + dh] = cost + dc, q.push({cost + dc, {e.to, t + dh}}); } } for (int i = 0; i <= H; i++) { ans[mask] = min(ans[mask], d[T][i]); } } vector<int> dp(MASK, INF); dp[0] = 0; for (int i = 0; i < P; i++) { auto tmp = dp; const int mask = pass[i].second; for (int num = mask; num > 0; num = (num - 1) & mask) { for (int j = 0; j < MASK; j++) { if (dp[j] == INF) { continue; } int newmask = j; for (int k = 1; k < MASK; k <<= 1) { if ((num & k) == 0) { continue; } newmask |= k; } tmp[newmask] = min(tmp[newmask], dp[j] + pass[i].first); } } dp = tmp; } int answer = INF; for (int i = 0; i < MASK; i++) { answer = min(answer, ans[i] + dp[i]); } cout << (answer == INF ? -1 : answer) << endl; } return 0; }
//================================= // Created on: 2018/07/04 16:12:54 //================================= #include <bits/stdc++.h> using namespace std; struct CostGraph { CostGraph(const int n) : V{n}, edge(n) {} void addEdge(const int from, const int to, const int cost, const int tim, const int label) { edge[from].push_back(Edge{from, to, cost, tim, label}); } struct Edge { Edge(const int from, const int to, const int cost, const int tim, const int label) : from{from}, to{to}, cost{cost}, tim{tim}, label{label} {} const int from, to, cost, tim, label; }; const int V; vector<vector<Edge>> edge; }; int main() { for (int N, M, H, K, S, T, P;;) { cin >> N >> M >> H >> K; if (N == 0 and M == 0 and H == 0 and K == 0) { break; } CostGraph g(N); for (int i = 0, a, b, c, h, r; i < M; i++) { cin >> a >> b >> c >> h >> r; a--, b--, r--; g.addEdge(a, b, c, h, r), g.addEdge(b, a, c, h, r); } cin >> S >> T >> P; S--, T--; using pii = pair<int, int>; vector<pii> pass(P); for (int i = 0, l; i < P; i++) { cin >> l >> pass[i].first; for (int j = 0, k; j < l; j++) { cin >> k, pass[i].second |= (1 << (k - 1)); } } constexpr int INF = 1 << 28; const int MASK = 1 << K; vector<int> ans(MASK, INF); for (int mask = 0; mask < MASK; mask++) { using PP = pair<int, pii>; vector<vector<int>> d(N, vector<int>(H + 1, INF)); priority_queue<PP, vector<PP>, greater<PP>> q; d[S][0] = 0, q.push({0, {S, 0}}); while (not q.empty()) { const auto p = q.top(); q.pop(); const int cost = p.first, s = p.second.first, t = p.second.second; if (cost > d[s][t]) { continue; } for (const auto &e : g.edge[s]) { const int dh = e.tim, l = e.label, dc = ((mask & (1 << l)) ? 0 : e.cost); if (t + dh > H) { continue; } if (d[e.to][t + dh] <= cost + dc) { continue; } d[e.to][t + dh] = cost + dc, q.push({cost + dc, {e.to, t + dh}}); } } for (int i = 0; i <= H; i++) { ans[mask] = min(ans[mask], d[T][i]); } } vector<int> dp(MASK, INF); dp[0] = 0; for (int i = 0; i < P; i++) { auto tmp = dp; const int mask = pass[i].second; for (int num = mask; num > 0; num = (num - 1) & mask) { for (int j = 0; j < MASK; j++) { if (dp[j] == INF) { continue; } int newmask = j; for (int k = 1; k < MASK; k <<= 1) { if ((num & k) == 0) { continue; } newmask |= k; } tmp[newmask] = min(tmp[newmask], dp[j] + pass[i].first); } } dp = tmp; } int answer = INF; for (int i = 0; i < MASK; i++) { answer = min(answer, ans[i] + dp[i]); } cout << (answer == INF ? -1 : answer) << endl; } return 0; }
replace
63
64
63
64
MLE
p01697
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using pi = tuple<int, int, int>; const int INF = 100000000; int main() { int N, M, H, K, S, T, P; while (cin >> N >> M >> H >> K, N | M | H | K) { vector<int> a(M), b(M), c(M), h(M), r(M); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i] >> c[i] >> h[i] >> r[i]; a[i]--; b[i]--; r[i]--; } cin >> S >> T >> P; S--; T--; vector<int> dp(1 << K, INF); dp[0] = 0; for (int i = 0, l, d; i < P; i++) { cin >> l >> d; int val = 0; for (int j = 0, k; j < l; j++) { cin >> k; val += 1 << (k - 1); } for (int j = 0; j < (1 << K); j++) { if (dp[j] != INF) { dp[j | val] = min(dp[j | val], dp[j] + d); } } } int res = INF; for (int i = 0; i < (1 << K); i++) { if (dp[i] == INF) continue; vector<vector<int>> d(N, vector<int>(H + 1, INF)); vector<vector<pi>> G(N); for (int j = 0; j < M; j++) { if (i & (1 << r[j])) { G[a[j]].push_back(pi(0, h[j], b[j])); G[b[j]].push_back(pi(0, h[j], a[j])); } else { G[a[j]].push_back(pi(c[j], h[j], b[j])); G[b[j]].push_back(pi(c[j], h[j], a[j])); } } priority_queue<pi, vector<pi>, greater<pi>> pq; pq.push(pi(0, 0, S)); while (!pq.empty()) { auto p = pq.top(); pq.pop(); if (d[get<2>(p)][get<1>(p)] != INF) { continue; } d[get<2>(p)][get<1>(p)] = get<0>(p); for (auto v : G[get<2>(p)]) { if (get<1>(p) + get<1>(v) <= H) { pq.push( pi(get<0>(p) + get<0>(v), get<1>(p) + get<1>(v), get<2>(v))); } } } int mid = INF; for (int i = 0; i <= H; i++) { mid = min(mid, d[T][i]); } res = min(res, dp[i] + mid); } cout << (res != INF ? res : -1) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using pi = tuple<int, int, int>; const int INF = 100000000; int main() { int N, M, H, K, S, T, P; while (cin >> N >> M >> H >> K, N | M | H | K) { vector<int> a(M), b(M), c(M), h(M), r(M); for (int i = 0; i < M; i++) { cin >> a[i] >> b[i] >> c[i] >> h[i] >> r[i]; a[i]--; b[i]--; r[i]--; } cin >> S >> T >> P; S--; T--; vector<int> dp(1 << K, INF); dp[0] = 0; for (int i = 0, l, d; i < P; i++) { cin >> l >> d; int val = 0; for (int j = 0, k; j < l; j++) { cin >> k; val += 1 << (k - 1); } for (int j = 0; j < (1 << K); j++) { if (dp[j] != INF) { dp[j | val] = min(dp[j | val], dp[j] + d); } } } int res = INF; for (int i = 0; i < (1 << K); i++) { if (dp[i] == INF) continue; vector<vector<int>> d(N, vector<int>(H + 1, INF)); vector<vector<pi>> G(N); for (int j = 0; j < M; j++) { if (i & (1 << r[j])) { G[a[j]].push_back(pi(0, h[j], b[j])); G[b[j]].push_back(pi(0, h[j], a[j])); } else { G[a[j]].push_back(pi(c[j], h[j], b[j])); G[b[j]].push_back(pi(c[j], h[j], a[j])); } } priority_queue<pi, vector<pi>, greater<pi>> pq; pq.push(pi(0, 0, S)); while (!pq.empty()) { auto p = pq.top(); pq.pop(); if (d[get<2>(p)][get<1>(p)] != INF) { continue; } d[get<2>(p)][get<1>(p)] = get<0>(p); for (auto v : G[get<2>(p)]) { if (get<1>(p) + get<1>(v) <= H && d[get<2>(v)][get<1>(p) + get<1>(v)] == INF) { pq.push( pi(get<0>(p) + get<0>(v), get<1>(p) + get<1>(v), get<2>(v))); } } } int mid = INF; for (int i = 0; i <= H; i++) { mid = min(mid, d[T][i]); } res = min(res, dp[i] + mid); } cout << (res != INF ? res : -1) << endl; } return 0; }
replace
58
59
58
60
TLE
p01697
C++
Memory Limit Exceeded
#include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define each(i, n) for (auto i : n) #define clr(n) memset(n, 0, sizeof(n)) #define mclr(n) memset(n, -1, sizeof(n)) // 2^8 * 100 * 500 * 24 int INF = 1e9; int n, m, H, K; int c[105][105]; int h[105][105]; int r[105][105]; int l[300]; int d[300]; int k[300]; int s, t, p; int me[(1 << 8) + 1][(1 << 8) + 1]; int ct[105][30]; int solve(int i, int bit, int tg) { if (me[i][bit] != -1) return me[i][bit]; if (i == p) { if ((bit & tg) == tg) { return 0; } else { return INF; } } return me[i][bit] = min(solve(i + 1, bit, tg), solve(i + 1, bit | k[i], tg) + d[i]); } vector<vector<int>> ce; int main() { while (cin >> n >> m >> H >> K && n) { mclr(c); mclr(h); mclr(r); mclr(l); mclr(d); clr(k); ce.clear(); ce.resize(n); rep(i, m) { int a, b, e, f, g; cin >> a >> b >> e >> f >> g; a--; b--; g--; ce[a].push_back(b); ce[b].push_back(a); c[a][b] = c[b][a] = e; h[a][b] = h[b][a] = f; r[a][b] = r[b][a] = g; } cin >> s >> t >> p; s--; t--; rep(i, p) { cin >> l[i] >> d[i]; rep(j, l[i]) { int tmp; cin >> tmp; tmp--; k[i] |= (1 << tmp); } } int ans = INF; rep(bit, 1 << K) { mclr(me); int cost = solve(0, 0, bit); if (cost == INF) continue; rep(i, 105) rep(j, 30) { ct[i][j] = INF; } ct[s][0] = cost; typedef tuple<int, int, int> T; priority_queue<T, vector<T>, greater<T>> qu; qu.push(T(cost, s, 0)); while (qu.size()) { int mc = get<0>(qu.top()); int nw = get<1>(qu.top()); int tm = get<2>(qu.top()); qu.pop(); if (ct[nw][tm] < mc) continue; each(i, ce[nw]) { if (c[nw][i] != -1) { int nt = tm + h[nw][i]; if (nt <= H) { int nc = ct[nw][tm]; if (!(bit & (1 << r[nw][i]))) { nc += c[nw][i]; } if (nc <= ct[i][nt]) { ct[i][nt] = nc; qu.push(T(nc, i, nt)); } } } } } rep(i, H + 1) { ans = min(ans, ct[t][i]); } } if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
#include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define each(i, n) for (auto i : n) #define clr(n) memset(n, 0, sizeof(n)) #define mclr(n) memset(n, -1, sizeof(n)) // 2^8 * 100 * 500 * 24 int INF = 1e9; int n, m, H, K; int c[105][105]; int h[105][105]; int r[105][105]; int l[300]; int d[300]; int k[300]; int s, t, p; int me[(1 << 8) + 1][(1 << 8) + 1]; int ct[105][30]; int solve(int i, int bit, int tg) { if (me[i][bit] != -1) return me[i][bit]; if (i == p) { if ((bit & tg) == tg) { return 0; } else { return INF; } } return me[i][bit] = min(solve(i + 1, bit, tg), solve(i + 1, bit | k[i], tg) + d[i]); } vector<vector<int>> ce; int main() { while (cin >> n >> m >> H >> K && n) { mclr(c); mclr(h); mclr(r); mclr(l); mclr(d); clr(k); ce.clear(); ce.resize(n); rep(i, m) { int a, b, e, f, g; cin >> a >> b >> e >> f >> g; a--; b--; g--; ce[a].push_back(b); ce[b].push_back(a); c[a][b] = c[b][a] = e; h[a][b] = h[b][a] = f; r[a][b] = r[b][a] = g; } cin >> s >> t >> p; s--; t--; rep(i, p) { cin >> l[i] >> d[i]; rep(j, l[i]) { int tmp; cin >> tmp; tmp--; k[i] |= (1 << tmp); } } int ans = INF; rep(bit, 1 << K) { mclr(me); int cost = solve(0, 0, bit); if (cost == INF) continue; rep(i, 105) rep(j, 30) { ct[i][j] = INF; } ct[s][0] = cost; typedef tuple<int, int, int> T; priority_queue<T, vector<T>, greater<T>> qu; qu.push(T(cost, s, 0)); while (qu.size()) { int mc = get<0>(qu.top()); int nw = get<1>(qu.top()); int tm = get<2>(qu.top()); qu.pop(); if (ct[nw][tm] < mc) continue; each(i, ce[nw]) { if (c[nw][i] != -1) { int nt = tm + h[nw][i]; if (nt <= H) { int nc = ct[nw][tm]; if (!(bit & (1 << r[nw][i]))) { nc += c[nw][i]; } if (nc < ct[i][nt]) { ct[i][nt] = nc; qu.push(T(nc, i, nt)); } } } } } rep(i, H + 1) { ans = min(ans, ct[t][i]); } } if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
replace
109
110
109
110
MLE
p01697
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define N_MAX 100 #define INF (1e9) using namespace std; typedef pair<int, int> P; typedef pair<P, P> P1; typedef pair<int, P> P2; int a, b, c, h, r, s, t, p, l, d, k, ans; int N, M, H, K, dp[300][(1 << 8)]; vector<P1> v[N_MAX]; P L[300]; void dijkstra(int S, int C) { int d[25][N_MAX]; priority_queue<P2> q; for (int i = 0; i <= H; i++) for (int j = 0; j < N; j++) d[i][j] = INF; d[0][s - 1] = 0; q.push(P2(0, P(0, s - 1))); while (!q.empty()) { P2 A = q.top(); q.pop(); int cost = A.first; int tim = A.second.first; int x = A.second.second; if (d[tim][x] < cost) continue; for (int i = 0; i < v[x].size(); i++) { int nx = v[x][i].first.first; int pc = v[x][i].first.second; int ph = v[x][i].second.first; int R = v[x][i].second.second; if (tim + ph <= H) { if (!(S & (1 << R)) && d[tim + ph][nx] > cost + pc) { d[tim + ph][nx] = cost + pc; q.push(P2(cost + pc, P(tim + ph, nx))); } if ((S & (1 << R)) && d[tim + ph][nx] > cost) { d[tim + ph][nx] = cost; q.push(P2(cost, P(tim + ph, nx))); } } } } int mind = INF; for (int i = 0; i <= H; i++) mind = min(mind, C + d[i][t - 1]); ans = min(ans, mind); } int main() { while (1) { cin >> N >> M >> H >> K; if (!N && !M && !H && !K) break; for (int i = 0; i < M; i++) { cin >> a >> b >> c >> h >> r; v[a - 1].push_back(P1(P(b - 1, c), P(h, r - 1))); v[b - 1].push_back(P1(P(a - 1, c), P(h, r - 1))); } cin >> s >> t; cin >> p; for (int i = 0; i <= p; i++) for (int j = 0; j < (1 << K); j++) dp[i][j] = INF; for (int i = 0; i < p; i++) { cin >> l >> d; L[i] = P(d, 0); for (int j = 0; j < l; j++) cin >> k, L[i].second |= (1 << (k - 1)); dp[i][L[i].second] = d; } for (int i = 0; i < p; i++) { for (int j = 0; j < (1 << K); j++) { if (dp[i][j] == INF) continue; dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j | L[i].second] = min(dp[i + 1][j | L[i].second], dp[i][j] + L[i].first); } } ans = INF; dijkstra(0, 0); for (int i = 0; i < (1 << K); i++) { if (dp[p][i] == INF) continue; dijkstra(i, dp[p][i]); } if (ans != INF) cout << ans << endl; else cout << -1 << endl; for (int i = 0; i < N; i++) v[i].clear(); } return 0; }
#include <bits/stdc++.h> #define N_MAX 100 #define INF (1e9) using namespace std; typedef pair<int, int> P; typedef pair<P, P> P1; typedef pair<int, P> P2; int a, b, c, h, r, s, t, p, l, d, k, ans; int N, M, H, K, dp[300][(1 << 8)]; vector<P1> v[N_MAX]; P L[300]; void dijkstra(int S, int C) { int d[25][N_MAX]; priority_queue<P2, vector<P2>, greater<P2>> q; for (int i = 0; i <= H; i++) for (int j = 0; j < N; j++) d[i][j] = INF; d[0][s - 1] = 0; q.push(P2(0, P(0, s - 1))); while (!q.empty()) { P2 A = q.top(); q.pop(); int cost = A.first; int tim = A.second.first; int x = A.second.second; if (d[tim][x] < cost) continue; for (int i = 0; i < v[x].size(); i++) { int nx = v[x][i].first.first; int pc = v[x][i].first.second; int ph = v[x][i].second.first; int R = v[x][i].second.second; if (tim + ph <= H) { if (!(S & (1 << R)) && d[tim + ph][nx] > cost + pc) { d[tim + ph][nx] = cost + pc; q.push(P2(cost + pc, P(tim + ph, nx))); } if ((S & (1 << R)) && d[tim + ph][nx] > cost) { d[tim + ph][nx] = cost; q.push(P2(cost, P(tim + ph, nx))); } } } } int mind = INF; for (int i = 0; i <= H; i++) mind = min(mind, C + d[i][t - 1]); ans = min(ans, mind); } int main() { while (1) { cin >> N >> M >> H >> K; if (!N && !M && !H && !K) break; for (int i = 0; i < M; i++) { cin >> a >> b >> c >> h >> r; v[a - 1].push_back(P1(P(b - 1, c), P(h, r - 1))); v[b - 1].push_back(P1(P(a - 1, c), P(h, r - 1))); } cin >> s >> t; cin >> p; for (int i = 0; i <= p; i++) for (int j = 0; j < (1 << K); j++) dp[i][j] = INF; for (int i = 0; i < p; i++) { cin >> l >> d; L[i] = P(d, 0); for (int j = 0; j < l; j++) cin >> k, L[i].second |= (1 << (k - 1)); dp[i][L[i].second] = d; } for (int i = 0; i < p; i++) { for (int j = 0; j < (1 << K); j++) { if (dp[i][j] == INF) continue; dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j | L[i].second] = min(dp[i + 1][j | L[i].second], dp[i][j] + L[i].first); } } ans = INF; dijkstra(0, 0); for (int i = 0; i < (1 << K); i++) { if (dp[p][i] == INF) continue; dijkstra(i, dp[p][i]); } if (ans != INF) cout << ans << endl; else cout << -1 << endl; for (int i = 0; i < N; i++) v[i].clear(); } return 0; }
replace
14
15
14
15
TLE
p01697
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define X first #define Y second using namespace std; typedef pair<int, int> pii; const int INF = 1e9; int main() { int N, M, H, K; while (cin >> N >> M >> H >> K, N | M | H | K) { vector<pii> edge[105]; int cost[505], in_h[505], in_r[505]; int S, T, P, pd[1 << 8], pk[1 << 8]; fill(pd, pd + (1 << 8), INF); rep(i, M) { int a, b, c, h, r; cin >> a >> b >> cost[i] >> in_h[i] >> in_r[i]; in_r[i]--; edge[a].emplace_back(b, i); edge[b].emplace_back(a, i); } cin >> S >> T >> P; rep(i, P) { int l, d, k, s = 0; cin >> l >> d; rep(j, l) { cin >> k; k--; s |= (1 << k); } pk[i] = s; pd[i] = d; } int pass[1 << 8]; fill(pass, pass + (1 << 8), INF); pass[0] = 0; rep(i, 1 << K) rep(j, P) { pass[i | pk[j]] = min(pass[i | pk[j]], pass[i] + pd[j]); } // rep(i,1<<K) cout << pass[i] << " "; cout << endl; int ans = INF; rep(k, 1 << K) { if (pass[k] == INF) continue; int d[105][30]; fill(d[0], d[0] + 105 * 30, ans); priority_queue<pair<int, pii>> que; d[S][0] = 0; que.push({0, {S, 0}}); while (!que.empty()) { int dd = que.top().X; int dn = que.top().Y.X; int dh = que.top().Y.Y; que.pop(); if (dd > d[dn][dh]) continue; for (auto t : edge[dn]) { int nx = t.X; int nh = dh + in_h[t.Y]; int ct = d[dn][dh]; int r = in_r[t.Y]; if (!((1 << r) & k)) ct += cost[t.Y]; if (nh <= H && ct < d[nx][nh]) { for (int i = nh; i <= H; i++) d[nx][i] = min(d[nx][i], ct); que.push({ct, {nx, nh}}); } } } int ret = INF; rep(h, H + 1) ret = min(ret, d[T][h]); ans = min(ans, pass[k] + ret); } cout << (ans == INF ? -1 : ans) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define X first #define Y second using namespace std; typedef pair<int, int> pii; const int INF = 1e9; int main() { int N, M, H, K; while (cin >> N >> M >> H >> K, N | M | H | K) { vector<pii> edge[105]; int cost[505], in_h[505], in_r[505]; int S, T, P, pd[1 << 8], pk[1 << 8]; fill(pd, pd + (1 << 8), INF); rep(i, M) { int a, b, c, h, r; cin >> a >> b >> cost[i] >> in_h[i] >> in_r[i]; in_r[i]--; edge[a].emplace_back(b, i); edge[b].emplace_back(a, i); } cin >> S >> T >> P; rep(i, P) { int l, d, k, s = 0; cin >> l >> d; rep(j, l) { cin >> k; k--; s |= (1 << k); } pk[i] = s; pd[i] = d; } int pass[1 << 8]; fill(pass, pass + (1 << 8), INF); pass[0] = 0; rep(i, 1 << K) rep(j, P) { pass[i | pk[j]] = min(pass[i | pk[j]], pass[i] + pd[j]); } // rep(i,1<<K) cout << pass[i] << " "; cout << endl; int ans = INF; rep(k, 1 << K) { if (pass[k] == INF) continue; int d[105][30]; fill(d[0], d[0] + 105 * 30, ans - pass[k]); priority_queue<pair<int, pii>> que; d[S][0] = 0; que.push({0, {S, 0}}); while (!que.empty()) { int dd = que.top().X; int dn = que.top().Y.X; int dh = que.top().Y.Y; que.pop(); if (dd > d[dn][dh]) continue; for (auto t : edge[dn]) { int nx = t.X; int nh = dh + in_h[t.Y]; int ct = d[dn][dh]; int r = in_r[t.Y]; if (!((1 << r) & k)) ct += cost[t.Y]; if (nh <= H && ct < d[nx][nh]) { for (int i = nh; i <= H; i++) d[nx][i] = min(d[nx][i], ct); que.push({ct, {nx, nh}}); } } } int ret = INF; rep(h, H + 1) ret = min(ret, d[T][h]); ans = min(ans, pass[k] + ret); } cout << (ans == INF ? -1 : ans) << endl; } return 0; }
replace
52
53
52
53
TLE
p01698
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define FF first #define SS second template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.FF >> p.SS; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << p.FF << " " << p.SS; } template <class T> void maxi(T &x, T y) { if (x < y) x = y; } template <class T> void mini(T &x, T y) { if (x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9 + 7; struct P { double x, y, z; P(double x_ = 0., double y_ = 0., double z_ = 0.) : x(x_), y(y_), z(z_) {} double norm() const { return sqrt(x * x + y * y + z * z); } P operator+(const P &p2) const { return P(x + p2.x, y + p2.y, z + p2.z); } P operator-(const P &p2) const { return P(x - p2.x, y - p2.y, z - p2.z); } P operator*(double sc) const { return P(x * sc, y * sc, z * sc); } }; double solve(const P &p1, const P &p2, const P &dv1, const P &dv2, double r1, double r2, double dr1, double dr2, double ub) { double lb = 0., md = 1e9; REP(i, 100) { double ts[2] = {(lb * 2 + ub) / 3., (lb + ub * 2) / 3.}; double ys[2]; REP(j, 2) { P c1 = p1 + dv1 * ts[j]; P c2 = p2 + dv2 * ts[j]; double dist = (c1 - c2).norm(); double tr1 = r1 - dr1 * ts[j]; double tr2 = r2 - dr2 * ts[j]; ys[j] = dist - tr1 - tr2; maxi(ys[j], 0.); } if (ys[0] <= ys[1]) { ub = ts[1]; md = ys[0]; } else { lb = ts[0]; md = ys[1]; } } if (md > EPS) return 1e9; return lb; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; while (cin >> N, N) { vector<P> ps(N), dv(N); vector<double> rs(N), dr(N); REP(i, N) { cin >> ps[i].x >> ps[i].y >> ps[i].z >> dv[i].x >> dv[i].y >> dv[i].z >> rs[i] >> dr[i]; } vector<double> ans(N); vector<bool> col(N); REP(i, N) ans[i] = rs[i] / dr[i]; while (true) { int i1 = -1, i2; double at = 1e9; REP(i, N) REP(j, i) { if (col[i] || col[j]) continue; double t = solve(ps[i], ps[j], dv[i], dv[j], rs[i], rs[j], dr[i], dr[j], min(ans[i], ans[j])); if (at > t && t < ans[i] - EPS && t < ans[j] - EPS) { i1 = i; i2 = j; at = t; } } if (i1 < 0) break; col[i1] = col[i2] = true; mini(ans[i1], at); mini(ans[i2], at); } REP(i, N) cout << fixed << setprecision(9) << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define FF first #define SS second template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) { return is >> p.FF >> p.SS; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &p) { return os << p.FF << " " << p.SS; } template <class T> void maxi(T &x, T y) { if (x < y) x = y; } template <class T> void mini(T &x, T y) { if (x > y) x = y; } const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9 + 7; struct P { double x, y, z; P(double x_ = 0., double y_ = 0., double z_ = 0.) : x(x_), y(y_), z(z_) {} double norm() const { return sqrt(x * x + y * y + z * z); } P operator+(const P &p2) const { return P(x + p2.x, y + p2.y, z + p2.z); } P operator-(const P &p2) const { return P(x - p2.x, y - p2.y, z - p2.z); } P operator*(double sc) const { return P(x * sc, y * sc, z * sc); } }; double solve(const P &p1, const P &p2, const P &dv1, const P &dv2, double r1, double r2, double dr1, double dr2, double ub) { double lb = 0., md = 1e9; REP(i, 100) { double ts[2] = {(lb * 2 + ub) / 3., (lb + ub * 2) / 3.}; double ys[2]; REP(j, 2) { P c1 = p1 + dv1 * ts[j]; P c2 = p2 + dv2 * ts[j]; double dist = (c1 - c2).norm(); double tr1 = r1 - dr1 * ts[j]; double tr2 = r2 - dr2 * ts[j]; ys[j] = dist - tr1 - tr2; maxi(ys[j], 0.); } if (ys[0] <= ys[1]) { ub = ts[1]; md = ys[0]; } else { lb = ts[0]; md = ys[1]; } } if (md > EPS) return 1e9; return lb; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; while (cin >> N, N) { vector<P> ps(N), dv(N); vector<double> rs(N), dr(N); REP(i, N) { cin >> ps[i].x >> ps[i].y >> ps[i].z >> dv[i].x >> dv[i].y >> dv[i].z >> rs[i] >> dr[i]; } vector<double> ans(N); vector<bool> col(N); REP(i, N) ans[i] = rs[i] / dr[i]; priority_queue<pair<double, PII>> pq; REP(i, N) REP(j, i) { double t = solve(ps[i], ps[j], dv[i], dv[j], rs[i], rs[j], dr[i], dr[j], min(ans[i], ans[j])); pq.push(MP(-t, MP(i, j))); } while (!pq.empty()) { double at = -pq.top().FF; PII p = pq.top().SS; pq.pop(); if (col[p.FF] || col[p.SS]) continue; col[p.FF] = col[p.SS] = true; mini(ans[p.FF], at); mini(ans[p.SS], at); } REP(i, N) cout << fixed << setprecision(9) << ans[i] << endl; } return 0; }
replace
103
122
103
119
TLE
p01703
C++
Time Limit Exceeded
#include <bits/stdc++.h> double ans[100010]; void MAIN(int n) { std::vector<double> prob; for (int i = 0; i < n; ++i) { double p; scanf("%lf", &p); if (p < 0.995) { prob.push_back(p); } else if (prob.empty() || prob.back() < 0.995) { prob.push_back(p); } else { prob[prob.size() - 1] += 1; } ans[i] = 1e100; } n = prob.size(); ans[n] = 0; for (int i = n - 1; i >= 0; --i) { double now = 0; int upper = std::min(n, i + 1000); for (int j = i + 1; j <= upper; ++j) { if (prob[j - 1] >= 0.995) { now += prob[j - 1]; } else { now = (now + 1) / prob[j - 1]; } ans[i] = std::min(ans[i], now + ans[j] + 1); } } printf("%.11f\n", ans[0] - 1); } int main() { int n; while (scanf("%d", &n) == 1 && n > 0) { MAIN(n); } return 0; }
#include <bits/stdc++.h> double ans[100010]; void MAIN(int n) { std::vector<double> prob; for (int i = 0; i < n; ++i) { double p; scanf("%lf", &p); if (p < 0.995) { prob.push_back(p); } else if (prob.empty() || prob.back() < 0.995) { prob.push_back(p); } else { prob[prob.size() - 1] += 1; } ans[i] = 1e100; } n = prob.size(); ans[n] = 0; for (int i = n - 1; i >= 0; --i) { double now = 0; int upper = std::min(n, i + 700); for (int j = i + 1; j <= upper; ++j) { if (prob[j - 1] >= 0.995) { now += prob[j - 1]; } else { now = (now + 1) / prob[j - 1]; } ans[i] = std::min(ans[i], now + ans[j] + 1); } } printf("%.11f\n", ans[0] - 1); } int main() { int n; while (scanf("%d", &n) == 1 && n > 0) { MAIN(n); } return 0; }
replace
22
23
22
23
TLE
p01704
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int n; cin >> n; if (!n) break; double pw; cin >> pw; vector<double> vw(n); vector<double> pf(n); vector<double> vf(n); vector<double> th(n); for (int i = 0; i < n; i++) { cin >> vw[i] >> pf[i] >> vf[i] >> th[i]; } double ans; double l = 0.0, r = 1000.0; while (r - l >= 0.000000000000001) { double p1 = (r + 2 * l) / 3; double p2 = (2 * r + l) / 3; // cout << l << " " << r << " " << p1 << " " << p2 << endl; double cost1 = (double)(p1 * pw); double cost2 = (double)(p2 * pw); for (int i = 0; i < n; i++) { double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]); double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]); F1 = max(F1, 0.0); F2 = max(F2, 0.0); cost1 += (double)(F1 * pf[i]); cost2 += (double)(F2 * pf[i]); } if (cost1 < cost2) r = p2; else l = p1; ans = cost1; } printf("%.4f\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int n; cin >> n; if (!n) break; double pw; cin >> pw; vector<double> vw(n); vector<double> pf(n); vector<double> vf(n); vector<double> th(n); for (int i = 0; i < n; i++) { cin >> vw[i] >> pf[i] >> vf[i] >> th[i]; } double ans; double l = 0.0, r = 1000.0; while (r - l >= 0.00000000001) { double p1 = (r + 2 * l) / 3; double p2 = (2 * r + l) / 3; // cout << l << " " << r << " " << p1 << " " << p2 << endl; double cost1 = (double)(p1 * pw); double cost2 = (double)(p2 * pw); for (int i = 0; i < n; i++) { double F1 = (double)((th[i] - p1 * vw[i]) / vf[i]); double F2 = (double)((th[i] - p2 * vw[i]) / vf[i]); F1 = max(F1, 0.0); F2 = max(F2, 0.0); cost1 += (double)(F1 * pf[i]); cost2 += (double)(F2 * pf[i]); } if (cost1 < cost2) r = p2; else l = p1; ans = cost1; } printf("%.4f\n", ans); } return 0; }
replace
23
24
23
24
TLE
p01705
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <map> using namespace std; int main() { for (int N; cin >> N, N;) { int X[51234], R[51234]; for (int i = 0; i < N; i++) { cin >> X[i] >> R[i]; } double l = 0, h = 1e9; for (int i = 0; i < 88; i++) { double m = (l + h) / 2; map<double, int> mp; for (int j = 0; j < N; j++) { double inr = 1. * R[j] * R[j] - m * m; if (inr >= 0) { mp[-sqrt(inr) + X[j]]++; mp[sqrt(inr) + X[j]]--; } } int c = 0; double start; bool f = false; for (auto e : mp) { if (c == 0) { start = e.first; } c += e.second; if (c == 0) { f |= e.first - start >= m * 2; } } (f ? l : h) = m; } cout << fixed << l * 2 << endl; } }
#include <cmath> #include <iostream> #include <map> using namespace std; int main() { for (int N; cin >> N, N;) { int X[51234], R[51234]; for (int i = 0; i < N; i++) { cin >> X[i] >> R[i]; } double l = 0, h = 1e9; for (int i = 0; i < 66; i++) { double m = (l + h) / 2; map<double, int> mp; for (int j = 0; j < N; j++) { double inr = 1. * R[j] * R[j] - m * m; if (inr >= 0) { mp[-sqrt(inr) + X[j]]++; mp[sqrt(inr) + X[j]]--; } } int c = 0; double start; bool f = false; for (auto e : mp) { if (c == 0) { start = e.first; } c += e.second; if (c == 0) { f |= e.first - start >= m * 2; } } (f ? l : h) = m; } cout << fixed << l * 2 << endl; } }
replace
13
14
13
14
TLE
p01706
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define fs first #define sc second #define pb push_back #define show(x) cout << #x << " " << x << endl struct edge { int to, cap, rev; }; const int MAX_V = 1000, inf = 1e8; vector<edge> G[MAX_V]; int level[MAX_V], iter[MAX_V], ord[MAX_V]; int N, M, S, T, a[10000], b[10000]; void add_edge(int from, int to, int cap, int i) { edge e1 = {to, cap, (int)G[to].size()}, e2 = {from, 0, (int)G[from].size()}; G[from].pb(e1); G[to].pb(e2); ord[i] = G[from].size() - 1; } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); rep(i, G[v].size()) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (true) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, inf)) > 0) flow += f; } } bool froms[MAX_V], tot[MAX_V]; void dfs1(int v) { // from s froms[v] = 1; for (auto e : G[v]) { if (e.cap <= 0) continue; if (froms[e.to]) continue; dfs1(e.to); } } void dfs2(int v) { // to t tot[v] = 1; for (auto e : G[v]) { if (e.cap > 0) continue; if (tot[e.to]) continue; dfs2(e.to); } } int main() { while (true) { cin >> N >> M >> S >> T; if (N == 0) break; rep(i, N) G[i].clear(); S--, T--; rep(i, M) cin >> a[i] >> b[i]; rep(i, M) { a[i]--, b[i]--; add_edge(a[i], b[i], 1, i); } int f = max_flow(S, T); int cnt = 0; rep(i, N) froms[i] = 0, tot[i] = 0; dfs1(S); dfs2(T); rep(i, M) { if (G[a[i]][ord[i]].cap < 1) continue; if (froms[b[i]] && tot[a[i]]) cnt++; } if (cnt > 0) f++; cout << f << " " << cnt << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define fs first #define sc second #define pb push_back #define show(x) cout << #x << " " << x << endl struct edge { int to, cap, rev; }; const int MAX_V = 1000, inf = 1e8; vector<edge> G[MAX_V]; int level[MAX_V], iter[MAX_V], ord[10000]; int N, M, S, T, a[10000], b[10000]; void add_edge(int from, int to, int cap, int i) { edge e1 = {to, cap, (int)G[to].size()}, e2 = {from, 0, (int)G[from].size()}; G[from].pb(e1); G[to].pb(e2); ord[i] = G[from].size() - 1; } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); rep(i, G[v].size()) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (true) { bfs(s); if (level[t] < 0) return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, inf)) > 0) flow += f; } } bool froms[MAX_V], tot[MAX_V]; void dfs1(int v) { // from s froms[v] = 1; for (auto e : G[v]) { if (e.cap <= 0) continue; if (froms[e.to]) continue; dfs1(e.to); } } void dfs2(int v) { // to t tot[v] = 1; for (auto e : G[v]) { if (e.cap > 0) continue; if (tot[e.to]) continue; dfs2(e.to); } } int main() { while (true) { cin >> N >> M >> S >> T; if (N == 0) break; rep(i, N) G[i].clear(); S--, T--; rep(i, M) cin >> a[i] >> b[i]; rep(i, M) { a[i]--, b[i]--; add_edge(a[i], b[i], 1, i); } int f = max_flow(S, T); int cnt = 0; rep(i, N) froms[i] = 0, tot[i] = 0; dfs1(S); dfs2(T); rep(i, M) { if (G[a[i]][ord[i]].cap < 1) continue; if (froms[b[i]] && tot[a[i]]) cnt++; } if (cnt > 0) f++; cout << f << " " << cnt << endl; } }
replace
26
27
26
27
0
p01707
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define FR first #define SC second #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++) #define each(a, b) for (auto &a : b) typedef pair<int, int> P; const int inf = 1LL << 55; const int mod = 1e9 + 7LL; int extgcd(int a, int b, int &x, int &y) { int d = a; if (b != 0) d = extgcd(b, a % b, y, x), y -= (a / b) * x; else x = 1, y = 0; return d; } int modinv(int a, int m) { int x, y; extgcd(a, m, x, y); return (m + x % m) % m; } int f[200020]; void fact() { f[0] = 1; for (int i = 1; i < 200020; i++) { f[i] = f[i - 1] * i % mod; } } int combi(int n, int k, int m) { if (k < 0 || n < k) return 0; return f[n] * modinv(f[k] * f[n - k] % m, m) % m; } int dp[2002][2002]; signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); fact(); int N, D, X; while (cin >> N >> D >> X, N) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 0; i < min(D, N); i++) { for (int j = 0; j < N; j++) { dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j]) % mod; if (j + 1 >= X) dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j + 1 - X] + mod) % mod; } } int ans = 0; for (int i = 1; i <= min(N, D); i++) ans = (ans + dp[i][N] * combi(D, i, mod)) % mod; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define FR first #define SC second #define all(v) (v).begin(), (v).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, f, n) for (int i = (int)(f); i < (int)(n); i++) #define each(a, b) for (auto &a : b) typedef pair<int, int> P; const int inf = 1LL << 55; const int mod = 1e9 + 7LL; int extgcd(int a, int b, int &x, int &y) { int d = a; if (b != 0) d = extgcd(b, a % b, y, x), y -= (a / b) * x; else x = 1, y = 0; return d; } int modinv(int a, int m) { int x, y; extgcd(a, m, x, y); return (m + x % m) % m; } int f[200020]; void fact() { f[0] = 1; for (int i = 1; i < 200020; i++) { f[i] = f[i - 1] * i % mod; } } int combi(int n, int k, int m) { if (k < 0 || n < k) return 0; return f[n] * modinv(f[k] * f[n - k] % m, m) % m; } int dp[2002][2002]; signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); fact(); int N, D, X; while (cin >> N >> D >> X, N) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 0; i < min(D, N); i++) { for (int j = 0; j < N; j++) { dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j]) % mod; if (j + 1 >= X) dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j + 1 - X] + mod) % mod; } } int ans = 0, nCk = 1; for (int i = 1; i <= min(N, D); i++) { // ans = (ans + dp[i][N] * combi(D, i, mod)) % mod; nCk = (nCk * ((D - i + 1) % mod * modinv(i, mod) % mod) % mod) % mod; ans = (ans + dp[i][N] * nCk) % mod; } cout << ans << endl; } return 0; }
replace
67
70
67
73
0
p01707
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve(int d, int x, vector<long long> &dp) { if (d == 1) { for (int i = 0; i < dp.size() && i < x; i++) dp[i] = 1; return; } vector<long long> A(dp.size()); if (d & 1) { solve(d - 1, x, A); for (int i = 0; i < x; i++) { for (int j = 0; j + i < dp.size(); j++) { dp[i + j] += A[j]; if (dp[i + j] >= mod) dp[i + j] -= mod; } } } else { solve(d >> 1, x, A); for (int i = 0; i < dp.size(); i++) { for (int j = 0; j + i < dp.size(); j++) { dp[i + j] += A[j] * A[i] % mod; if (dp[i + j] >= mod) dp[i + j] -= mod; } } } } int main() { int x, n; long long d; while (~scanf("%d%lld%d", &n, &d, &x)) { if (!n && !d && !x) break; vector<long long> dp(n + 1); solve(d, x, dp); printf("%lld\n", dp[n]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve(long long d, int x, vector<long long> &dp) { if (d == 1) { for (int i = 0; i < dp.size() && i < x; i++) dp[i] = 1; return; } vector<long long> A(dp.size()); if (d & 1) { solve(d - 1, x, A); for (int i = 0; i < x; i++) { for (int j = 0; j + i < dp.size(); j++) { dp[i + j] += A[j]; if (dp[i + j] >= mod) dp[i + j] -= mod; } } } else { solve(d >> 1, x, A); for (int i = 0; i < dp.size(); i++) { for (int j = 0; j + i < dp.size(); j++) { dp[i + j] += A[j] * A[i] % mod; if (dp[i + j] >= mod) dp[i + j] -= mod; } } } } int main() { int x, n; long long d; while (~scanf("%d%lld%d", &n, &d, &x)) { if (!n && !d && !x) break; vector<long long> dp(n + 1); solve(d, x, dp); printf("%lld\n", dp[n]); } return 0; }
replace
3
4
3
4
MLE
p01707
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) const int MOD = 1000000007; typedef long long ll; ll N, D, X; typedef vector<int> Vec; typedef vector<Vec> Mat; void mat_mul(Vec &A, const Vec &B) { const int n = A.size(); Vec col(n); REP(j, n) { ll s = 0; REP(k, n) { ll a = (j - k >= 0) ? A[j - k] : 0; ll b = (k - 0 >= 0) ? B[k - 0] : 0; s += a * b; s %= MOD; } col[j] = s; } A = move(col); } Mat mat_pow(Mat A, ll n) { Vec col(A.size(), 0); col[0] = 1; Vec a(A.size()); REP(i, A.size()) a[i] = A[i][0]; for (; n > 0; n >>= 1) { if (n & 1) mat_mul(col, a); mat_mul(a, a); } Mat res(A.size(), Vec(A.size(), 0)); REP(i, A.size()) REP(j, A.size()) { if (i - j >= 0) res[i][j] = col[i - j]; } return res; } void mat_print(Mat A) { const int n = A.size(); REP(i, n) { REP(j, n) { cerr << ' ' << A[i][j]; } cerr << endl; } } signed main() { ios::sync_with_stdio(false); while (true) { cin >> N >> D >> X; if (N == 0 && D == 0 && X == 0) break; Mat A(N + 1, Vec(N + 1, 0)); REP(i, N + 1) { for (int j = max<int>(i - X + 1, 0); j <= i; ++j) { A[i][j] = 1; } } Mat B = mat_pow(A, D); cout << B[N][0] << endl; } }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) const int MOD = 1000000007; typedef long long ll; ll N, D, X; typedef vector<int> Vec; typedef vector<Vec> Mat; void mat_mul(Vec &A, const Vec &B) { const int n = A.size(); Vec col(n); REP(j, n) { ll s = 0; for (int k = 0; k <= j; ++k) { ll a = A[j - k]; ll b = B[k]; s += a * b; s %= MOD; } col[j] = s; } A = move(col); } Mat mat_pow(Mat A, ll n) { Vec col(A.size(), 0); col[0] = 1; Vec a(A.size()); REP(i, A.size()) a[i] = A[i][0]; for (; n > 0; n >>= 1) { if (n & 1) mat_mul(col, a); mat_mul(a, a); } Mat res(A.size(), Vec(A.size(), 0)); REP(i, A.size()) REP(j, A.size()) { if (i - j >= 0) res[i][j] = col[i - j]; } return res; } void mat_print(Mat A) { const int n = A.size(); REP(i, n) { REP(j, n) { cerr << ' ' << A[i][j]; } cerr << endl; } } signed main() { ios::sync_with_stdio(false); while (true) { cin >> N >> D >> X; if (N == 0 && D == 0 && X == 0) break; Mat A(N + 1, Vec(N + 1, 0)); REP(i, N + 1) { for (int j = max<int>(i - X + 1, 0); j <= i; ++j) { A[i][j] = 1; } } Mat B = mat_pow(A, D); cout << B[N][0] << endl; } }
replace
13
16
13
16
TLE
p01708
C++
Runtime Error
#include <cassert> #include <complex> #include <iomanip> #include <iostream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef complex<double> vec; typedef pair<vec, vec> line; double ip(vec u, vec v) { return real(u * conj(v)); } double cross(vec u, vec v) { return imag(conj(u) * v); } vec projection(line l, vec p) { vec s = l.first, t = l.second; double k = ip(t - s, p - s) / ip(t - s, t - s); return (1.0 - k) * s + k * t; } vec reflection(line l, vec p) { return 2.0 * projection(l, p) - p; } vec intersection(line l0, line l1) { vec s0, t0, s1, t1; s0 = l0.first, t0 = l0.second; s1 = l1.first, t1 = l1.second; double k = cross(t1 - s1, s1 - s0) / cross(t1 - s1, t0 - s0); return s0 + (t0 - s0) * k; } string s; int c, N; struct V { vector<vec> ps; V() {} }; V f(V v1, V v2) { if (v1.ps.size() + v2.ps.size() == 2) { V ret; ret.ps.push_back(v1.ps[0]); ret.ps.push_back(v2.ps[0]); return ret; } if (v1.ps.size() + v2.ps.size() == 3) { if (v1.ps.size() == 2) swap(v1, v2); vec v = v1.ps[0]; line l = make_pair(v2.ps[0], v2.ps[1]); v = reflection(l, v); V ret; ret.ps.push_back(v); return ret; } if (v1.ps.size() + v2.ps.size() == 4) { line l1 = make_pair(v1.ps[0], v1.ps[1]); line l2 = make_pair(v2.ps[0], v2.ps[1]); vec v = intersection(l1, l2); V ret; ret.ps.push_back(v); return ret; } } V expr(); double num(); V expr() { if (s[c] == '(') { c++; if (isdigit(s[c]) || s[c] == '-') { double x = num(); assert(s[c] == ','); c++; double y = num(); assert(s[c] == ')'); c++; V v; v.ps.push_back(vec(x, y)); return v; } V v1 = expr(); while (c < N && s[c] == '@') { c++; V v2 = expr(); assert(s[c] == ')'); v1 = f(v1, v2); } assert(s[c] == ')'); c++; return v1; } } double num() { double ret = 0; bool m = false; if (s[c] == '-') { m = true; c++; } while (isdigit(s[c])) { ret = ret * 10 + s[c] - '0'; c++; } if (m) { ret *= -1; } return ret; } int main() { while (cin >> s, s != "#") { c = 0; N = s.size(); V v1 = expr(); while (c < N && s[c] == '@') { c++; V v2 = expr(); v1 = f(v1, v2); } vec p = v1.ps[0]; cout << fixed << setprecision(15) << p.real() << " " << p.imag() << endl; } }
#include <cassert> #include <complex> #include <iomanip> #include <iostream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; typedef complex<double> vec; typedef pair<vec, vec> line; double ip(vec u, vec v) { return real(u * conj(v)); } double cross(vec u, vec v) { return imag(conj(u) * v); } vec projection(line l, vec p) { vec s = l.first, t = l.second; double k = ip(t - s, p - s) / ip(t - s, t - s); return (1.0 - k) * s + k * t; } vec reflection(line l, vec p) { return 2.0 * projection(l, p) - p; } vec intersection(line l0, line l1) { vec s0, t0, s1, t1; s0 = l0.first, t0 = l0.second; s1 = l1.first, t1 = l1.second; double k = cross(t1 - s1, s1 - s0) / cross(t1 - s1, t0 - s0); return s0 + (t0 - s0) * k; } string s; int c, N; struct V { vector<vec> ps; V() {} }; V f(V v1, V v2) { if (v1.ps.size() + v2.ps.size() == 2) { V ret; ret.ps.push_back(v1.ps[0]); ret.ps.push_back(v2.ps[0]); return ret; } if (v1.ps.size() + v2.ps.size() == 3) { if (v1.ps.size() == 2) swap(v1, v2); vec v = v1.ps[0]; line l = make_pair(v2.ps[0], v2.ps[1]); v = reflection(l, v); V ret; ret.ps.push_back(v); return ret; } if (v1.ps.size() + v2.ps.size() == 4) { line l1 = make_pair(v1.ps[0], v1.ps[1]); line l2 = make_pair(v2.ps[0], v2.ps[1]); vec v = intersection(l1, l2); V ret; ret.ps.push_back(v); return ret; } } V expr(); double num(); V expr() { if (s[c] == '(') { c++; if (isdigit(s[c]) || s[c] == '-') { double x = num(); assert(s[c] == ','); c++; double y = num(); assert(s[c] == ')'); c++; V v; v.ps.push_back(vec(x, y)); return v; } V v1 = expr(); while (c < N && s[c] == '@') { c++; V v2 = expr(); // assert(s[c] == ')'); v1 = f(v1, v2); } assert(s[c] == ')'); c++; return v1; } } double num() { double ret = 0; bool m = false; if (s[c] == '-') { m = true; c++; } while (isdigit(s[c])) { ret = ret * 10 + s[c] - '0'; c++; } if (m) { ret *= -1; } return ret; } int main() { while (cin >> s, s != "#") { c = 0; N = s.size(); V v1 = expr(); while (c < N && s[c] == '@') { c++; V v2 = expr(); v1 = f(v1, v2); } vec p = v1.ps[0]; cout << fixed << setprecision(15) << p.real() << " " << p.imag() << endl; } }
replace
86
87
86
87
0
p01710
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } namespace SCC { void visit(const vector<vector<int>> &G, vector<int> &vs, vector<int> &used, int v) { used[v] = true; for (auto u : G[v]) { if (!used[u]) visit(G, vs, used, u); } vs.push_back(v); } void visit2(const vector<vector<int>> &T, vector<int> &used, vector<int> &comp, vector<int> &vec, int k, int v) { comp[v] = k; used[v] = true; vec.push_back(v); for (auto u : T[v]) { if (!used[u]) visit2(T, used, comp, vec, k, u); } } void decompose(const vector<vector<int>> &G, vector<vector<int>> &H, vector<int> &comp) { vector<vector<int>> T(G.size()); for (int i = 0; i < G.size(); i++) { for (auto v : G[i]) { T[v].push_back(i); } } comp.resize(G.size()); vector<int> vs(G.size()); vector<int> used(G.size()); for (int i = 0; i < G.size(); i++) { if (!used[i]) visit(G, vs, used, i); } reverse(vs.begin(), vs.end()); fill(used.begin(), used.end(), 0); int K = 0; vector<vector<int>> S; for (auto v : vs) { if (!used[v]) { S.push_back(vector<int>()); visit2(T, used, comp, S.back(), K++, v); } } H.resize(K); fill(used.begin(), used.end(), 0); for (int i = 0; i < K; i++) { for (auto v : S[i]) { for (auto u : G[v]) { if (used[comp[u]] || comp[v] == comp[u]) continue; used[comp[u]] = true; H[comp[v]].push_back(comp[u]); } } for (auto v : H[i]) used[v] = false; } } } // namespace SCC int N, M, T; void solve() { vint p(N), t(N), k(N); rep(i, N) cin >> p[i] >> t[i] >> k[i]; vector<vint> G(N); vector<int> sel(N); rep(i, M) { int a, b; cin >> a >> b; a--; b--; G[a].pb(b); if (a == b) sel[i] = true; } vector<vint> H; vint comp; SCC::decompose(G, H, comp); vector<vpint> lis(H.size()); vint cnt(H.size()); for (int i = 0; i < N; i++) cnt[comp[i]]++; for (int i = 0; i < N; i++) { if (cnt[comp[i]] == 1 && !sel[i]) { lis[comp[i]].pb({p[i], t[i]}); } else { for (int j = 1;; j <<= 1) { if (k[i] >= j) { lis[comp[i]].pb({p[i] * j, t[i] * j}); k[i] -= j; } else { if (k[i]) lis[comp[i]].pb({p[i] * k[i], t[i] * k[i]}); break; } } } } vector<vint> dp(H.size(), vint(T + 1)); for (int i = 0; i < H.size(); i++) { for (auto &w : lis[i]) { for (int j = T; j >= 0; j--) { if (j + w.se <= T) chmax(dp[i][j + w.se], dp[i][j] + w.fi); } } for (auto &u : H[i]) rep(j, T + 1) chmax(dp[u][j], dp[i][j]); } int ans = 0; rep(i, H.size()) rep(j, T + 1) chmax(ans, dp[i][j]); cout << ans << endl; } signed main() { while (cin >> N >> M >> T, N || M || T) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } namespace SCC { void visit(const vector<vector<int>> &G, vector<int> &vs, vector<int> &used, int v) { used[v] = true; for (auto u : G[v]) { if (!used[u]) visit(G, vs, used, u); } vs.push_back(v); } void visit2(const vector<vector<int>> &T, vector<int> &used, vector<int> &comp, vector<int> &vec, int k, int v) { comp[v] = k; used[v] = true; vec.push_back(v); for (auto u : T[v]) { if (!used[u]) visit2(T, used, comp, vec, k, u); } } void decompose(const vector<vector<int>> &G, vector<vector<int>> &H, vector<int> &comp) { vector<vector<int>> T(G.size()); for (int i = 0; i < G.size(); i++) { for (auto v : G[i]) { T[v].push_back(i); } } comp.resize(G.size()); vector<int> vs(G.size()); vector<int> used(G.size()); for (int i = 0; i < G.size(); i++) { if (!used[i]) visit(G, vs, used, i); } reverse(vs.begin(), vs.end()); fill(used.begin(), used.end(), 0); int K = 0; vector<vector<int>> S; for (auto v : vs) { if (!used[v]) { S.push_back(vector<int>()); visit2(T, used, comp, S.back(), K++, v); } } H.resize(K); fill(used.begin(), used.end(), 0); for (int i = 0; i < K; i++) { for (auto v : S[i]) { for (auto u : G[v]) { if (used[comp[u]] || comp[v] == comp[u]) continue; used[comp[u]] = true; H[comp[v]].push_back(comp[u]); } } for (auto v : H[i]) used[v] = false; } } } // namespace SCC int N, M, T; void solve() { vint p(N), t(N), k(N); rep(i, N) cin >> p[i] >> t[i] >> k[i]; vector<vint> G(N); vector<int> sel(N); rep(i, M) { int a, b; cin >> a >> b; a--; b--; G[a].pb(b); if (a == b) sel[a] = true; } vector<vint> H; vint comp; SCC::decompose(G, H, comp); vector<vpint> lis(H.size()); vint cnt(H.size()); for (int i = 0; i < N; i++) cnt[comp[i]]++; for (int i = 0; i < N; i++) { if (cnt[comp[i]] == 1 && !sel[i]) { lis[comp[i]].pb({p[i], t[i]}); } else { for (int j = 1;; j <<= 1) { if (k[i] >= j) { lis[comp[i]].pb({p[i] * j, t[i] * j}); k[i] -= j; } else { if (k[i]) lis[comp[i]].pb({p[i] * k[i], t[i] * k[i]}); break; } } } } vector<vint> dp(H.size(), vint(T + 1)); for (int i = 0; i < H.size(); i++) { for (auto &w : lis[i]) { for (int j = T; j >= 0; j--) { if (j + w.se <= T) chmax(dp[i][j + w.se], dp[i][j] + w.fi); } } for (auto &u : H[i]) rep(j, T + 1) chmax(dp[u][j], dp[i][j]); } int ans = 0; rep(i, H.size()) rep(j, T + 1) chmax(ans, dp[i][j]); cout << ans << endl; } signed main() { while (cin >> N >> M >> T, N || M || T) solve(); return 0; }
replace
108
109
108
109
0
p01712
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; // #define int long long #define DBG 1 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #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 each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); signed main() { int N, W, H; cin >> N >> W >> H; vector<int> w(W + 1, 0); vector<int> h(H + 1, 0); rep(i, 0, N) { int x, y, k; cin >> x >> y >> k; w[max(0, x - k)]++; w[min(W, x + k)]--; h[max(0, y - k)]++; h[min(H, y + k)]--; } dumpc(h); dumpc(w); rep(i, 1, W + 1) { w[i] += w[i - 1]; } rep(i, 1, H + 1) { h[i] += h[i - 1]; } dumpc(h); dumpc(w); int a = max(*min_element(w.begin(), w.end() - 1), *min_element(h.begin(), h.end() - 1)); if (a == 0) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
#include "bits/stdc++.h" using namespace std; // #define int long long #define DBG 0 #define dump(o) \ if (DBG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DBG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } #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 each(it, c) for (auto it = (c).begin(); it != (c).end(); it++) #define all(c) c.begin(), c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); signed main() { int N, W, H; cin >> N >> W >> H; vector<int> w(W + 1, 0); vector<int> h(H + 1, 0); rep(i, 0, N) { int x, y, k; cin >> x >> y >> k; w[max(0, x - k)]++; w[min(W, x + k)]--; h[max(0, y - k)]++; h[min(H, y + k)]--; } dumpc(h); dumpc(w); rep(i, 1, W + 1) { w[i] += w[i - 1]; } rep(i, 1, H + 1) { h[i] += h[i - 1]; } dumpc(h); dumpc(w); int a = max(*min_element(w.begin(), w.end() - 1), *min_element(h.begin(), h.end() - 1)); if (a == 0) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
replace
4
5
4
5
0
h 1 0 0 1 -1 0 1 -1 0 -1 w 1 0 0 1 -1 0 1 -1 0 -1 h 1 1 1 2 1 1 2 1 1 0 w 1 1 1 2 1 1 2 1 1 0
p01712
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int main() { int N, W, H; cin >> N >> W >> H; int vertical[W + 1]; int horizon[H + 1]; for (int i = 0; i < W + 1; i++) { vertical[i] = 0; } for (int i = 0; i < H + 1; i++) { horizon[i] = 0; } for (int times = 0; times < N; times++) { int x, y, w; cin >> x >> y >> w; int temp; temp = (x - w >= 0) ? x - w : 0; vertical[temp]++; temp = (x + w <= W) ? x + w : W; vertical[temp]--; temp = (y - w >= 0) ? y - w : 0; horizon[temp]++; temp = (y + w <= H) ? y + w : H; vertical[temp]--; } for (int i = 1; i < W + 1; i++) { vertical[i] += vertical[i - 1]; } for (int i = 1; i < H + 1; i++) { horizon[i] += horizon[i - 1]; } bool flagv = true; bool flagh = true; for (int i = 0; i < W; i++) { if (vertical[i] == 0) { flagv = false; break; } } for (int i = 0; i < H; i++) { if (horizon[i] == 0) { flagh = false; break; } } if (!flagv && !flagh) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int N, W, H; cin >> N >> W >> H; int vertical[W + 1]; int horizon[H + 1]; for (int i = 0; i < W + 1; i++) { vertical[i] = 0; } for (int i = 0; i < H + 1; i++) { horizon[i] = 0; } for (int times = 0; times < N; times++) { int x, y, w; cin >> x >> y >> w; int temp; temp = (x - w >= 0) ? x - w : 0; vertical[temp]++; temp = (x + w <= W) ? x + w : W; vertical[temp]--; temp = (y - w >= 0) ? y - w : 0; horizon[temp]++; temp = (y + w <= H) ? y + w : H; horizon[temp]--; } for (int i = 1; i < W + 1; i++) { vertical[i] += vertical[i - 1]; } for (int i = 1; i < H + 1; i++) { horizon[i] += horizon[i - 1]; } bool flagv = true; bool flagh = true; for (int i = 0; i < W; i++) { if (vertical[i] == 0) { flagv = false; break; } } for (int i = 0; i < H; i++) { if (horizon[i] == 0) { flagh = false; break; } } if (!flagv && !flagh) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
replace
29
30
29
30
0
p01712
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int X[10005], Y[100005]; int main() { int n, W, H; cin >> n >> W >> H; memset(X, 0, sizeof(X)); memset(Y, 0, sizeof(Y)); rep(i, n) { int x, y, w; cin >> x >> y >> w; X[max(0, x - w)]++; X[min(W, x + w)]--; Y[max(0, y - w)]++; Y[min(H, y + w)]--; } REP(i, 1, 100005) { X[i] += X[i - 1]; Y[i] += Y[i - 1]; } bool a = true, b = true; REP(i, 0, W) { if (X[i] == 0) a = false; } REP(i, 0, H) { if (Y[i] == 0) b = false; } if (a || b) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int X[100005], Y[100005]; int main() { int n, W, H; cin >> n >> W >> H; memset(X, 0, sizeof(X)); memset(Y, 0, sizeof(Y)); rep(i, n) { int x, y, w; cin >> x >> y >> w; X[max(0, x - w)]++; X[min(W, x + w)]--; Y[max(0, y - w)]++; Y[min(H, y + w)]--; } REP(i, 1, 100005) { X[i] += X[i - 1]; Y[i] += Y[i - 1]; } bool a = true, b = true; REP(i, 0, W) { if (X[i] == 0) a = false; } REP(i, 0, H) { if (Y[i] == 0) b = false; } if (a || b) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
replace
19
20
19
20
0
p01712
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <utility> using namespace std; class S { public: S(int length) : length(length) { part.insert(make_pair(length + 1, length + 1)); } void add_ap(int pos, int power) { int start = max(pos - power, 0), end = min(pos + power, length); auto it = part.lower_bound(start); if (it != part.begin()) it++; while (true) { if (end < it->first) { // (A)(B) part.insert(make_pair(start, end)); break; } else if (start < it->first) { if (end <= it->second) { // (A()B) part.insert(make_pair(start, it->second)); part.erase(it); break; } else { // (A(B)... part.erase(it++); } } else if (start <= it->second) { if (end <= it->second) { // (B(A)) break; } else { // (B()A... start = it->first; part.erase(it++); } } else { // (B)(A... it++; } } } bool covered() { auto f = part.begin(); return f != part.end() & f->first == 0 & f->second == length; } private: int length; map<int, int> part; }; int main() { int n, w, h; cin >> n >> w >> h; S xs = S(w), ys = S(h); bool covered = false; int x, y, p; for (int i = 0; i < n; i++) { cin >> x >> y >> p; xs.add_ap(x, p); ys.add_ap(y, p); if (xs.covered() | ys.covered()) { covered = true; break; } } cout << (covered ? "Yes" : "No") << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <utility> using namespace std; class S { public: S(int length) : length(length) { part.insert(make_pair(length + 1, length + 1)); } void add_ap(int pos, int power) { int start = max(pos - power, 0), end = min(pos + power, length); auto it = part.lower_bound(start); if (it != part.begin()) it--; while (true) { if (end < it->first) { // (A)(B) part.insert(make_pair(start, end)); break; } else if (start < it->first) { if (end <= it->second) { // (A()B) part.insert(make_pair(start, it->second)); part.erase(it); break; } else { // (A(B)... part.erase(it++); } } else if (start <= it->second) { if (end <= it->second) { // (B(A)) break; } else { // (B()A... start = it->first; part.erase(it++); } } else { // (B)(A... it++; } } } bool covered() { auto f = part.begin(); return f != part.end() & f->first == 0 & f->second == length; } private: int length; map<int, int> part; }; int main() { int n, w, h; cin >> n >> w >> h; S xs = S(w), ys = S(h); bool covered = false; int x, y, p; for (int i = 0; i < n; i++) { cin >> x >> y >> p; xs.add_ap(x, p); ys.add_ap(y, p); if (xs.covered() | ys.covered()) { covered = true; break; } } cout << (covered ? "Yes" : "No") << endl; return 0; }
replace
15
16
15
16
0
p01712
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int mx[10020], my[10020]; int main() { int n, h, w; cin >> n >> w >> h; while (n--) { int x, y, c; cin >> x >> y >> c; mx[max(0, x - c)]++; mx[min(w + 1, x + c)]--; my[max(0, y - c)]++; my[min(h + 1, y + c)]--; } int ans = 0; for (int i = 0; i < w - 1; i++) mx[i + 1] += mx[i]; for (int i = 0; i < h - 1; i++) my[i + 1] += my[i]; for (int i = 0; i < w; i++) if (mx[i] == 0) { ans++; break; } for (int i = 0; i < h; i++) if (my[i] == 0) { ans++; break; } if (ans != 2) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int mx[100020], my[100020]; int main() { int n, h, w; cin >> n >> w >> h; while (n--) { int x, y, c; cin >> x >> y >> c; mx[max(0, x - c)]++; mx[min(w + 1, x + c)]--; my[max(0, y - c)]++; my[min(h + 1, y + c)]--; } int ans = 0; for (int i = 0; i < w - 1; i++) mx[i + 1] += mx[i]; for (int i = 0; i < h - 1; i++) my[i + 1] += my[i]; for (int i = 0; i < w; i++) if (mx[i] == 0) { ans++; break; } for (int i = 0; i < h; i++) if (my[i] == 0) { ans++; break; } if (ans != 2) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
replace
3
4
3
4
0
p01712
C++
Runtime Error
#include <cstdio> #include <iostream> #include <vector> using namespace std; int n, w, h; int r(int a, int b) { int c = a + b; if (c < 0) return 0; else if (c > h) return h; else return c; } int main(void) { cin >> n >> w >> h; vector<int> x(n), y(n), z(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i]; vector<vector<int>> g(h, vector<int>(w, 0)); for (int i = 0; i < h; i++) { } vector<int> gh(h + 1, 0); vector<int> gw(w + 1, 0); for (int i = 0; i < n; i++) { gh[r(y[i], z[i] * -1)]++; gh[r(y[i], z[i])]--; } for (int i = 0; i < n; i++) { gw[r(x[i], z[i] * -1)]++; gw[r(x[i], z[i])]--; } for (int i = 0; i < h; i++) gh[i + 1] += gh[i]; for (int i = 0; i < w; i++) gw[i + 1] += gw[i]; bool f1 = true, f2 = true; for (int i = 0; i < h; i++) if (gh[i] == 0) f1 = false; for (int i = 0; i < w; i++) if (gw[i] == 0) f2 = false; if (f1 || f2) cout << "Yes" << endl; else cout << "No" << endl; }
#include <cstdio> #include <iostream> #include <vector> using namespace std; int n, w, h; int r(int a, int b) { int c = a + b; if (c < 0) return 0; else if (c > h) return h; else return c; } int main(void) { cin >> n >> w >> h; vector<int> x(n), y(n), z(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> z[i]; vector<int> gh(h + 1, 0); vector<int> gw(w + 1, 0); for (int i = 0; i < n; i++) { gh[r(y[i], z[i] * -1)]++; gh[r(y[i], z[i])]--; } for (int i = 0; i < n; i++) { gw[r(x[i], z[i] * -1)]++; gw[r(x[i], z[i])]--; } for (int i = 0; i < h; i++) gh[i + 1] += gh[i]; for (int i = 0; i < w; i++) gw[i + 1] += gw[i]; bool f1 = true, f2 = true; for (int i = 0; i < h; i++) if (gh[i] == 0) f1 = false; for (int i = 0; i < w; i++) if (gw[i] == 0) f2 = false; if (f1 || f2) cout << "Yes" << endl; else cout << "No" << endl; }
delete
23
27
23
23
0
p01712
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dx[] = {1, -1, 0, 0, 1, -1, -1, 1}; int miti[10020]; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define fi first #define sc second int main() { int n, w, h; cin >> n >> w >> h; int wifi[n][3]; REP(i, n) { cin >> wifi[i][0] >> wifi[i][1] >> wifi[i][2]; } // x軸を調べる REP(i, 10020) miti[i] = 0; REP(i, n) { int a = wifi[i][0]; int b = wifi[i][2]; int minx = max(0, a - b); int maxx = a + b; miti[minx]++; miti[maxx]--; } int total = 0; bool flag = true; REP(i, w) { total += miti[i]; // cout << miti[i] << i <<endl; if (total <= 0) { flag = false; break; } } if (flag) { cout << "Yes" << endl; return 0; } REP(i, 10020) miti[i] = 0; REP(i, n) { int a = wifi[i][1]; int b = wifi[i][2]; int miny = max(0, a - b); int maxy = a + b; miti[miny]++; miti[maxy]--; } total = 0; flag = true; REP(i, h) { total += miti[i]; // cout << miti[i] <<' ' <<i<<endl; if (total <= 0) { flag = false; break; } } if (flag) { cout << "Yes" << endl; return 0; } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dx[] = {1, -1, 0, 0, 1, -1, -1, 1}; int miti[300000]; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define mp make_pair #define fi first #define sc second int main() { int n, w, h; cin >> n >> w >> h; int wifi[n][3]; REP(i, n) { cin >> wifi[i][0] >> wifi[i][1] >> wifi[i][2]; } // x軸を調べる REP(i, 10020) miti[i] = 0; REP(i, n) { int a = wifi[i][0]; int b = wifi[i][2]; int minx = max(0, a - b); int maxx = a + b; miti[minx]++; miti[maxx]--; } int total = 0; bool flag = true; REP(i, w) { total += miti[i]; // cout << miti[i] << i <<endl; if (total <= 0) { flag = false; break; } } if (flag) { cout << "Yes" << endl; return 0; } REP(i, 10020) miti[i] = 0; REP(i, n) { int a = wifi[i][1]; int b = wifi[i][2]; int miny = max(0, a - b); int maxy = a + b; miti[miny]++; miti[maxy]--; } total = 0; flag = true; REP(i, h) { total += miti[i]; // cout << miti[i] <<' ' <<i<<endl; if (total <= 0) { flag = false; break; } } if (flag) { cout << "Yes" << endl; return 0; } cout << "No" << endl; return 0; }
replace
7
8
7
8
0
p01712
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n, w, h, ans = 0; pair<int, int> han[2][10001]; cin >> n >> w >> h; for (int i = 0, x, y, z; i < n; i++) { cin >> x >> y >> z; han[0][i] = P(x - z, x + z); han[1][i] = P(y - z, y + z); } han[0][n] = P(w, w); han[1][n++] = P(h, h); for (int i = 0; i < 2; i++) sort(han[i], han[i] + n); for (int i = 0; i < 2; i++) { int mx = 0, f = 1; for (int j = 0; j < n; j++) { if (han[i][j].first > mx) f = 0; mx = max(mx, han[i][j].second); } if (f) ans = 1; } if (ans) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n, w, h, ans = 0; pair<int, int> han[2][100001]; cin >> n >> w >> h; for (int i = 0, x, y, z; i < n; i++) { cin >> x >> y >> z; han[0][i] = P(x - z, x + z); han[1][i] = P(y - z, y + z); } han[0][n] = P(w, w); han[1][n++] = P(h, h); for (int i = 0; i < 2; i++) sort(han[i], han[i] + n); for (int i = 0; i < 2; i++) { int mx = 0, f = 1; for (int j = 0; j < n; j++) { if (han[i][j].first > mx) f = 0; mx = max(mx, han[i][j].second); } if (f) ans = 1; } if (ans) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
replace
5
6
5
6
0
p01712
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int xdp[108000], ydp[108000]; int n, W, H; int x, y, w; int main() { cin >> n >> W >> H; for (int i = 0; i < n; i++) { cin >> x >> y >> w; xdp[max(x - w, 0)]++; xdp[x + w]--; ydp[max(y - w, 0)]++; ydp[y + w]--; } int ok = 2; for (int i = 0; i < W; i++) { if (i) xdp[i] += xdp[i - 1]; if (xdp[i] == 0) { ok--; break; } } for (int i = 0; i < H; i++) { if (i) ydp[i] += ydp[i - 1]; if (ydp[i] == 0) { ok--; break; } } if (ok) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int xdp[216000], ydp[216000]; int n, W, H; int x, y, w; int main() { cin >> n >> W >> H; for (int i = 0; i < n; i++) { cin >> x >> y >> w; xdp[max(x - w, 0)]++; xdp[x + w]--; ydp[max(y - w, 0)]++; ydp[y + w]--; } int ok = 2; for (int i = 0; i < W; i++) { if (i) xdp[i] += xdp[i - 1]; if (xdp[i] == 0) { ok--; break; } } for (int i = 0; i < H; i++) { if (i) ydp[i] += ydp[i - 1]; if (ydp[i] == 0) { ok--; break; } } if (ok) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
replace
5
6
5
6
0
p01713
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int W; cin >> W; vector<int> a(W), dp1(W + 2, -1e9), dp2(W + 2, -1e9); for (int i = 1; i <= W; i++) cin >> a[i]; for (int i = 1; i <= W; i++) { if (a[i] == 0) dp1[i] = 1e9; else if (a[i] < 0) dp1[i] = min(dp1[i - 1] - 1, -a[i]); else dp1[i] = dp1[i - 1] - 1; } for (int i = W; i >= 1; i--) { if (a[i] == 0) dp2[i] = 1e9; else if (a[i] < 0) dp2[i] = min(dp2[i + 1] - 1, -a[i]); else dp2[i] = dp2[i + 1] - 1; } int ans = 0; for (int i = 1; i <= W; i++) ans += max(0, min(max(dp1[i], dp2[i]), a[i])); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int W; cin >> W; vector<int> a(W + 2), dp1(W + 2, -1e9), dp2(W + 2, -1e9); for (int i = 1; i <= W; i++) cin >> a[i]; for (int i = 1; i <= W; i++) { if (a[i] == 0) dp1[i] = 1e9; else if (a[i] < 0) dp1[i] = min(dp1[i - 1] - 1, -a[i]); else dp1[i] = dp1[i - 1] - 1; } for (int i = W; i >= 1; i--) { if (a[i] == 0) dp2[i] = 1e9; else if (a[i] < 0) dp2[i] = min(dp2[i + 1] - 1, -a[i]); else dp2[i] = dp2[i + 1] - 1; } int ans = 0; for (int i = 1; i <= W; i++) ans += max(0, min(max(dp1[i], dp2[i]), a[i])); cout << ans << endl; }
replace
6
7
6
7
0
p01715
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[2222][2222]; int R[2222][2222]; int S, N, M; const int INF = (1000000009); bool update(int &a, int b) { if (a > b) { a = b; return true; } return false; } int solvek() { fill(dp[0], dp[M + 1], INF); dp[0][0] = 0; for (int i = 1; i <= M; i++) { int kid = 0; for (int j = 1; j <= N; j++) { while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid])) kid++; kid--; cout << kid << " "; } cout << endl; for (int j = 0; dp[i][j] != 0; j++) dp[i][j] = 0; } return dp[M][N]; } int solve() { fill(dp[0], dp[M + 1], INF); dp[0][0] = 0; for (int i = 1; i <= M; i++) { int minik = 0; for (int j = 1; j <= N; j++) { for (int kid = minik; kid < N; kid++) { if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) { minik = min(minik, kid); } } } for (int j = 0; dp[i][j] != 0; j++) dp[i][j] = 0; } return dp[M][N]; } int main() { cin >> S >> N >> M; int X[2222], Y[2222]; for (int i = 0; i < S; i++) cin >> X[i]; for (int i = 0; i < N; i++) { int t, p; cin >> t >> p; p--; Y[i] = t - X[p]; } sort(Y, Y + N); for (int i = 0; i < N; i++) { int sum = 0; // cout << " y " << Y[i] << endl; for (int j = i; j > -1; j--) { sum += Y[i] - Y[j]; R[i + 1][j] = sum; // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl; } } cout << solve() << endl; }
#include <bits/stdc++.h> using namespace std; int dp[2222][2222]; int R[2222][2222]; int S, N, M; const int INF = (1000000009); bool update(int &a, int b) { if (a > b) { a = b; return true; } return false; } int solvek() { fill(dp[0], dp[M + 1], INF); dp[0][0] = 0; for (int i = 1; i <= M; i++) { int kid = 0; for (int j = 1; j <= N; j++) { while (kid < j && update(dp[i][j], dp[i - 1][kid] + R[j][kid])) kid++; kid--; cout << kid << " "; } cout << endl; for (int j = 0; dp[i][j] != 0; j++) dp[i][j] = 0; } return dp[M][N]; } int solve() { fill(dp[0], dp[M + 1], INF); dp[0][0] = 0; for (int i = 1; i <= M; i++) { int minik = 0; for (int j = 1; j <= N; j++) { for (int kid = minik; kid < N; kid++) { if (update(dp[i][j], dp[i - 1][kid] + R[j][kid])) { minik = max(minik, kid); } } } for (int j = 0; dp[i][j] != 0; j++) dp[i][j] = 0; } return dp[M][N]; } int main() { cin >> S >> N >> M; int X[2222], Y[2222]; for (int i = 0; i < S; i++) cin >> X[i]; for (int i = 0; i < N; i++) { int t, p; cin >> t >> p; p--; Y[i] = t - X[p]; } sort(Y, Y + N); for (int i = 0; i < N; i++) { int sum = 0; // cout << " y " << Y[i] << endl; for (int j = i; j > -1; j--) { sum += Y[i] - Y[j]; R[i + 1][j] = sum; // cout << "[ " << j << ", " << i+1 << " ) = " << R[i+1][j] << endl; } } cout << solve() << endl; }
replace
42
43
42
43
TLE
p01717
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; struct Edge { int v, w; }; typedef vector<vector<Edge>> Graph; const int INF = 1 << 28; const int MAXN = 2020; int N, M, K; Graph G; bool vis[MAXN]; int size; int cost[MAXN]; int dp[MAXN][MAXN][2]; int C; int Size[MAXN]; int V[MAXN][MAXN]; int dp2[MAXN][MAXN]; void dfs(int v, int src, int prev) { vis[v] = true; for (int i = 0; i < G[v].size(); ++i) { int nv = G[v][i].v; if (nv == prev) continue; cost[size++] = G[v][i].w; if (nv == src) continue; dfs(nv, src, v); break; } } void calc(int r, int v[]) { for (int s = 0; s < 2; ++s) { fill(dp[0][0], dp[MAXN][0], -INF); dp[1][s][s] = 0; for (int i = 1; i < size; ++i) { for (int j = 0; j < size; ++j) { for (int k = 0; k < 2; ++k) { if (dp[i][j][k] == -INF) continue; dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]); int add = 0; if (k) { if (!cost[i - 1]) continue; else add += cost[i - 1]; } if (r && i + 1 == size && s) { if (!cost[i]) continue; else add += cost[i]; } dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j][k] + add); } } } for (int j = 0; j <= size; ++j) { v[j] = max(v[j], max(dp[size][j][0], dp[size][j][1])); } } } int main() { while (cin >> N >> M >> K) { G = Graph(N); for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; G[a].push_back((Edge){b, c}); G[b].push_back((Edge){a, c}); } memset(vis, 0, sizeof(vis)); fill(V[0], V[MAXN], -INF); C = 0; for (int r = 0; r < 2; ++r) { for (int i = 0; i < N; ++i) { if (vis[i]) continue; if (!r && G[i].size() == 2) continue; size = 0; dfs(i, i, -1); size += !r; Size[C] = size; calc(r, V[C++]); } } fill(dp2[0], dp2[MAXN], -INF); dp2[0][0] = 0; for (int i = 0; i < C; ++i) { for (int j = 0; j <= K; ++j) { for (int k = 0; k <= Size[i] && j + k <= K; ++k) { if (dp2[i][j] == -INF) continue; dp2[i + 1][j + k] = max(dp2[i + 1][j + k], dp2[i][j] + V[i][k]); } } } if (dp2[C][K] == -INF) cout << "Impossible" << endl; else cout << dp2[C][K] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int v, w; }; typedef vector<vector<Edge>> Graph; const int INF = 1 << 28; const int MAXN = 2020; int N, M, K; Graph G; bool vis[MAXN]; int size; int cost[MAXN]; int dp[MAXN][MAXN][2]; int C; int Size[MAXN]; int V[MAXN][MAXN]; int dp2[MAXN][MAXN]; void dfs(int v, int src, int prev) { vis[v] = true; for (int i = 0; i < G[v].size(); ++i) { int nv = G[v][i].v; if (nv == prev) continue; cost[size++] = G[v][i].w; if (nv == src) continue; dfs(nv, src, v); break; } } void calc(int r, int v[]) { for (int s = 0; s < 2; ++s) { fill(dp[0][0], dp[size + 1][0], -INF); dp[1][s][s] = 0; for (int i = 1; i < size; ++i) { for (int j = 0; j < size; ++j) { for (int k = 0; k < 2; ++k) { if (dp[i][j][k] == -INF) continue; dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]); int add = 0; if (k) { if (!cost[i - 1]) continue; else add += cost[i - 1]; } if (r && i + 1 == size && s) { if (!cost[i]) continue; else add += cost[i]; } dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j][k] + add); } } } for (int j = 0; j <= size; ++j) { v[j] = max(v[j], max(dp[size][j][0], dp[size][j][1])); } } } int main() { while (cin >> N >> M >> K) { G = Graph(N); for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; G[a].push_back((Edge){b, c}); G[b].push_back((Edge){a, c}); } memset(vis, 0, sizeof(vis)); fill(V[0], V[MAXN], -INF); C = 0; for (int r = 0; r < 2; ++r) { for (int i = 0; i < N; ++i) { if (vis[i]) continue; if (!r && G[i].size() == 2) continue; size = 0; dfs(i, i, -1); size += !r; Size[C] = size; calc(r, V[C++]); } } fill(dp2[0], dp2[MAXN], -INF); dp2[0][0] = 0; for (int i = 0; i < C; ++i) { for (int j = 0; j <= K; ++j) { for (int k = 0; k <= Size[i] && j + k <= K; ++k) { if (dp2[i][j] == -INF) continue; dp2[i + 1][j + k] = max(dp2[i + 1][j + k], dp2[i][j] + V[i][k]); } } } if (dp2[C][K] == -INF) cout << "Impossible" << endl; else cout << dp2[C][K] << endl; } return 0; }
replace
38
39
38
39
TLE
p01717
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; struct Edge { int from; int to; int cost; }; vector<vector<vector<int>>> *dp; int main() { int N, M, K; cin >> N >> M >> K; vector<vector<Edge>> edges(N); vector<vector<int>> haits(N); /*for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; if (c) { edges[a].push_back(Edge{ a,b,c }); edges[b].push_back(Edge{ b,a,c }); } else { haits[a].push_back(b); haits[b].push_back(a); } } vector<vector<int>>hlines; int num = -1; vector<bool>oks(N); for (int i = 0; i < M; ++i) { if (haits[i].size() == 1&&!oks[i]) { oks[i] = true; num++; hlines.push_back(vector<int>()); int next = haits[i][0]; while (1) { hlines[num].push_back(next); int anext = -1; for (auto e : haits[next]) { if (e != next) { anext = e; } } if (anext == -1)break; next = 1; } } }*/ for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(Edge{a, b, c}); edges[b].push_back(Edge{b, a, c}); } vector<bool> oks(N); vector<vector<int>> line_loves; { vector<vector<int>> lines; int num = -1; for (int i = 0; i < N; ++i) { if (edges[i].size() == 1 && !oks[i]) { oks[i] = true; num++; lines.push_back(vector<int>()); line_loves.push_back(vector<int>()); lines[num].push_back(i); line_loves[num].push_back(edges[i][0].cost); int next = edges[i][0].to; while (1) { oks[next] = true; int anext = -1; for (auto e : edges[next]) { if (!oks[e.to]) { anext = e.to; line_loves[num].push_back(e.cost); } } if (anext == -1) break; next = anext; } } } for (int i = 0; i < N; ++i) { if (edges[i].size() == 0) { oks[i] = true; lines.push_back(vector<int>()); line_loves.push_back(vector<int>()); lines[num].push_back(i); } } } vector<vector<int>> circle_loves; { vector<vector<int>> circles; int num = -1; for (int i = 0; i < N; ++i) { if (edges[i].size() == 2 && !oks[i]) { oks[i] = true; num++; circles.push_back(vector<int>()); circle_loves.push_back(vector<int>()); circles[num].push_back(i); circle_loves[num].push_back(edges[i][0].cost); int next = edges[i][0].to; while (1) { oks[next] = true; circles[num].push_back(next); int anext = -1; for (auto e : edges[next]) { if (!oks[e.to]) { anext = e.to; circle_loves[num].push_back(e.cost); } } if (anext == -1 || oks[anext]) break; next = anext; } if (edges[next][0].to == circles[num][0]) { circle_loves[num].push_back(edges[next][0].cost); } else { circle_loves[num].push_back(edges[next][1].cost); } } } } vector<int> ans(2001, -9e8); ans[0] = 0; for (auto l : line_loves) { const int size_ = l.size() + 1; dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8))); (*dp)[0][0][0] = 0; (*dp)[0][1][1] = 0; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } vector<int> nowans(size_ + 1, -9e8); for (int j = 0; j <= size_; ++j) { nowans[j] = max((*dp)[(size_ - 1) & 1][j][0], (*dp)[(size_ - 1) & 1][j][1]); } vector<int> nans(2001, -9e8); for (int anew = 0; anew <= 2000; ++anew) { for (int from = anew - size_; from <= anew; ++from) { if (from < 0) continue; nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]); } } for (int j = 0; j <= 2000; ++j) { ans[j] = max(ans[j], nans[j]); } free(dp); } for (auto l : circle_loves) { const int size_ = l.size() + 1; vector<int> nowans(size_ + 1, -9e8); { dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8))); (*dp)[0][0][0] = 0; (*dp)[0][1][1] = -9e8; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } for (int j = 0; j <= size_; ++j) { nowans[j] = (*dp)[(size_ - 1) & 1][j][0]; } } { dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8)));(*dp)[0][0][0] = -9e8; (*dp)[0][1][1] = 0; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } for (int j = 1; j <= size_; ++j) { nowans[j - 1] = max(nowans[j - 1], (*dp)[(size_ - 1) & 1][j][1]); } } vector<int> nans(2001, -9e8); for (int anew = 0; anew <= 2000; ++anew) { for (int from = anew - size_; from <= anew; ++from) { if (from < 0) continue; nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]); } } for (int j = 0; j <= 2000; ++j) { ans[j] = max(ans[j], nans[j]); } } int finans; for (int i = K; i <= K; ++i) { finans = ans[i]; } if (finans > -4e8) { cout << finans << endl; } else { cout << "Impossible" << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; struct Edge { int from; int to; int cost; }; vector<vector<vector<int>>> *dp; int main() { int N, M, K; cin >> N >> M >> K; vector<vector<Edge>> edges(N); vector<vector<int>> haits(N); /*for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; if (c) { edges[a].push_back(Edge{ a,b,c }); edges[b].push_back(Edge{ b,a,c }); } else { haits[a].push_back(b); haits[b].push_back(a); } } vector<vector<int>>hlines; int num = -1; vector<bool>oks(N); for (int i = 0; i < M; ++i) { if (haits[i].size() == 1&&!oks[i]) { oks[i] = true; num++; hlines.push_back(vector<int>()); int next = haits[i][0]; while (1) { hlines[num].push_back(next); int anext = -1; for (auto e : haits[next]) { if (e != next) { anext = e; } } if (anext == -1)break; next = 1; } } }*/ for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(Edge{a, b, c}); edges[b].push_back(Edge{b, a, c}); } vector<bool> oks(N); vector<vector<int>> line_loves; { vector<vector<int>> lines; int num = -1; for (int i = 0; i < N; ++i) { if (edges[i].size() == 1 && !oks[i]) { oks[i] = true; num++; lines.push_back(vector<int>()); line_loves.push_back(vector<int>()); lines[num].push_back(i); line_loves[num].push_back(edges[i][0].cost); int next = edges[i][0].to; while (1) { oks[next] = true; int anext = -1; for (auto e : edges[next]) { if (!oks[e.to]) { anext = e.to; line_loves[num].push_back(e.cost); } } if (anext == -1) break; next = anext; } } } for (int i = 0; i < N; ++i) { if (edges[i].size() == 0) { oks[i] = true; num++; lines.push_back(vector<int>()); line_loves.push_back(vector<int>()); lines[num].push_back(i); } } } vector<vector<int>> circle_loves; { vector<vector<int>> circles; int num = -1; for (int i = 0; i < N; ++i) { if (edges[i].size() == 2 && !oks[i]) { oks[i] = true; num++; circles.push_back(vector<int>()); circle_loves.push_back(vector<int>()); circles[num].push_back(i); circle_loves[num].push_back(edges[i][0].cost); int next = edges[i][0].to; while (1) { oks[next] = true; circles[num].push_back(next); int anext = -1; for (auto e : edges[next]) { if (!oks[e.to]) { anext = e.to; circle_loves[num].push_back(e.cost); } } if (anext == -1 || oks[anext]) break; next = anext; } if (edges[next][0].to == circles[num][0]) { circle_loves[num].push_back(edges[next][0].cost); } else { circle_loves[num].push_back(edges[next][1].cost); } } } } vector<int> ans(2001, -9e8); ans[0] = 0; for (auto l : line_loves) { const int size_ = l.size() + 1; dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8))); (*dp)[0][0][0] = 0; (*dp)[0][1][1] = 0; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } vector<int> nowans(size_ + 1, -9e8); for (int j = 0; j <= size_; ++j) { nowans[j] = max((*dp)[(size_ - 1) & 1][j][0], (*dp)[(size_ - 1) & 1][j][1]); } vector<int> nans(2001, -9e8); for (int anew = 0; anew <= 2000; ++anew) { for (int from = anew - size_; from <= anew; ++from) { if (from < 0) continue; nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]); } } for (int j = 0; j <= 2000; ++j) { ans[j] = max(ans[j], nans[j]); } free(dp); } for (auto l : circle_loves) { const int size_ = l.size() + 1; vector<int> nowans(size_ + 1, -9e8); { dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8))); (*dp)[0][0][0] = 0; (*dp)[0][1][1] = -9e8; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } for (int j = 0; j <= size_; ++j) { nowans[j] = (*dp)[(size_ - 1) & 1][j][0]; } } { dp = new vector<vector<vector<int>>>( 2, vector<vector<int>>(size_ + 1, vector<int>(2, -9e8))); // vector<vector<vector<int>>>(*dp)(size_, vector<vector<int>>(size_+1, // vector<int>(2, -9e8)));(*dp)[0][0][0] = -9e8; (*dp)[0][1][1] = 0; for (int i = 0; i < size_ - 1; ++i) { int tar = i & 1, cur = tar ^ 1; for (int k = 0; k < size_ + 1; ++k) { for (int l = 0; l < 2; ++l) { (*dp)[cur][k][l] = -9e8; } } for (int j = 0; j < size_; ++j) { if (i == 0) assert(tar == 0 && cur == 1); if (l[i]) { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], max((*dp)[tar][j][0], (*dp)[tar][j][1] + l[i])); } else { (*dp)[cur][j][0] = max((*dp)[cur][j][0], max((*dp)[tar][j][0], (*dp)[tar][j][1])); (*dp)[cur][j + 1][1] = max((*dp)[cur][j + 1][1], (*dp)[tar][j][0]); } } } for (int j = 1; j <= size_; ++j) { nowans[j - 1] = max(nowans[j - 1], (*dp)[(size_ - 1) & 1][j][1]); } } vector<int> nans(2001, -9e8); for (int anew = 0; anew <= 2000; ++anew) { for (int from = anew - size_; from <= anew; ++from) { if (from < 0) continue; nans[anew] = max(nans[anew], ans[from] + nowans[anew - from]); } } for (int j = 0; j <= 2000; ++j) { ans[j] = max(ans[j], nans[j]); } } int finans; for (int i = K; i <= K; ++i) { finans = ans[i]; } if (finans > -4e8) { cout << finans << endl; } else { cout << "Impossible" << endl; } return 0; }
insert
93
93
93
94
0
p01719
C++
Runtime Error
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); int dp[11][100010]; int solve(int N, VI &d, VI &p, int X) { fill((int *)dp, (int *)dp + 11 * 100010, -1); dp[0][0] = 0; REP(i, N) { REP(x, X + 1) { if (dp[i][x] < 0) continue; dp[i + 1][x] = max(dp[i + 1][x], dp[i][x]); for (int tx = x, j = 0; tx <= X; tx += p[i], ++j) { dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][x] + d[i] * j); } } } return dp[N][X]; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, D, X; cin >> N >> D >> X; VVI p(D, VI(N)); REP(d, D) REP(i, N) cin >> p[d][i]; REP(d, D) p[d].PB(1); REP(d, D - 1) { X = solve(N + 1, p[d + 1], p[d], X); } cout << X << endl; return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); int dp[12][100010]; int solve(int N, VI &d, VI &p, int X) { fill((int *)dp, (int *)dp + 11 * 100010, -1); dp[0][0] = 0; REP(i, N) { REP(x, X + 1) { if (dp[i][x] < 0) continue; dp[i + 1][x] = max(dp[i + 1][x], dp[i][x]); for (int tx = x, j = 0; tx <= X; tx += p[i], ++j) { dp[i + 1][tx] = max(dp[i + 1][tx], dp[i][x] + d[i] * j); } } } return dp[N][X]; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, D, X; cin >> N >> D >> X; VVI p(D, VI(N)); REP(d, D) REP(i, N) cin >> p[d][i]; REP(d, D) p[d].PB(1); REP(d, D - 1) { X = solve(N + 1, p[d + 1], p[d], X); } cout << X << endl; return 0; }
replace
58
59
58
59
0
p01719
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Item { int value, weight; }; int dp[11][10001]; int knapsack(vector<Item> &items, int W) { fill(dp[0], dp[11], -1); dp[0][0] = 0; int N = items.size(); for (int i = 0; i < N; ++i) { for (int j = 0; j <= W; ++j) { if (dp[i][j] < 0) continue; int nj = j + items[i].weight; if (nj <= W) { dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } int res = 0; for (int i = 0; i <= W; ++i) { res = max(res, dp[N][i]); } return res; } int main() { int N, D, X; cin >> N >> D >> X; vector<vector<int>> P(D, vector<int>(N)); for (int i = 0; i < D; ++i) { for (int j = 0; j < N; ++j) { cin >> P[i][j]; } } vector<vector<Item>> items(D - 1, vector<Item>(N)); for (int i = 0; i < D - 1; ++i) { for (int j = 0; j < N; ++j) { items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]}; } } for (int i = 0; i < items.size(); ++i) { X += knapsack(items[i], X); } cout << X << endl; }
#include <bits/stdc++.h> using namespace std; struct Item { int value, weight; }; int dp[11][100001]; int knapsack(vector<Item> &items, int W) { fill(dp[0], dp[11], -1); dp[0][0] = 0; int N = items.size(); for (int i = 0; i < N; ++i) { for (int j = 0; j <= W; ++j) { if (dp[i][j] < 0) continue; int nj = j + items[i].weight; if (nj <= W) { dp[i][nj] = max(dp[i][nj], dp[i][j] + items[i].value); } dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); } } int res = 0; for (int i = 0; i <= W; ++i) { res = max(res, dp[N][i]); } return res; } int main() { int N, D, X; cin >> N >> D >> X; vector<vector<int>> P(D, vector<int>(N)); for (int i = 0; i < D; ++i) { for (int j = 0; j < N; ++j) { cin >> P[i][j]; } } vector<vector<Item>> items(D - 1, vector<Item>(N)); for (int i = 0; i < D - 1; ++i) { for (int j = 0; j < N; ++j) { items[i][j] = (Item){P[i + 1][j] - P[i][j], P[i][j]}; } } for (int i = 0; i < items.size(); ++i) { X += knapsack(items[i], X); } cout << X << endl; }
replace
8
9
8
9
0
p01719
C++
Runtime Error
#include <iostream> using namespace std; int n, d, p[10][10], ans; void saiki(int sum, int x, int day, int I) { int f = 1; for (int i = I; i < n; i++) if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) { f = 0; saiki(sum + p[day][i], x - p[day - 1][i], day, i); } if (f) ans = max(ans, sum + x); } int main() { cin >> n >> d >> ans; for (int i = 0; i < d; i++) for (int j = 0; j < n; j++) cin >> p[i][j]; for (int i = 0; i < d - 1; i++) saiki(0, ans, i + 1, -1); cout << ans << endl; return 0; }
#include <iostream> using namespace std; int n, d, p[10][10], ans; void saiki(int sum, int x, int day, int I) { int f = 1; for (int i = I; i < n; i++) if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) { f = 0; saiki(sum + p[day][i], x - p[day - 1][i], day, i); } if (f) ans = max(ans, sum + x); } int main() { cin >> n >> d >> ans; for (int i = 0; i < d; i++) for (int j = 0; j < n; j++) cin >> p[i][j]; for (int i = 0; i < d - 1; i++) saiki(0, ans, i + 1, 0); cout << ans << endl; return 0; }
replace
22
23
22
23
-8
p01719
C++
Runtime Error
class in { struct my_iterator { int it; const bool rev; explicit constexpr my_iterator(int it_, bool rev = false) : it(it_), rev(rev) {} int operator*() { return it; } bool operator!=(my_iterator &r) { return it != r.it; } void operator++() { rev ? --it : ++it; } }; const my_iterator i, n; public: explicit constexpr in(int n) : i(0), n(n) {} explicit constexpr in(int i, int n) : i(i, n < i), n(n) {} const my_iterator &begin() { return i; } const my_iterator &end() { return n; } }; #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, d, x; cin >> n >> d >> x; vector<vector<int>> p(d, vector<int>(n)); for (auto &v : p) for (auto &a : v) cin >> a; for (int i : in(d - 1)) { vector<int> dp(x + 1, 0); for (int j : in(n)) for (int k : in(p[i][j], x + 1)) dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]); x += dp[x]; } cout << x << endl; return 0; }
class in { struct my_iterator { int it; const bool rev; explicit constexpr my_iterator(int it_, bool rev = false) : it(it_), rev(rev) {} int operator*() { return it; } bool operator!=(my_iterator &r) { return it != r.it; } void operator++() { rev ? --it : ++it; } }; const my_iterator i, n; public: explicit constexpr in(int n) : i(0), n(n) {} explicit constexpr in(int i, int n) : i(i, n < i), n(n) {} const my_iterator &begin() { return i; } const my_iterator &end() { return n; } }; #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, d, x; cin >> n >> d >> x; vector<vector<int>> p(d, vector<int>(n)); for (auto &v : p) for (auto &a : v) cin >> a; for (int i : in(d - 1)) { vector<int> dp(x + 1, 0); for (int j : in(n)) if (p[i][j] <= x) for (int k : in(p[i][j], x + 1)) dp[k] = max(dp[k], dp[k - p[i][j]] + p[i + 1][j] - p[i][j]); x += dp[x]; } cout << x << endl; return 0; }
replace
34
36
34
37
0
p01719
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; int main() { int n, d, x; cin >> n >> d >> x; vvi val(d, vi(n)); REP(i, d) REP(j, n) cin >> val[i][j]; vvi dp(d, vi(10001)); REP(i, x + 1) { dp[0][i] = i; } int ma = x; int tmp; FOR(i, 1, d) { tmp = 0; REP(k, ma + 1) dp[i][k] = k; REP(k, ma + 1) { REP(j, n) if (k - val[i - 1][j] >= 0) { dp[i][k] = max(dp[i][k], dp[i][k - val[i - 1][j]] + val[i][j]); tmp = max(tmp, dp[i][k]); } } ma = max(ma, tmp); } int ans = 0; REP(i, dp[d - 1].size()) ans = max(ans, dp[d - 1][i]); cout << ans << endl; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define LOG(...) printf(__VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define RSORT(c) sort((c).rbegin(), (c).rend()) #define CLR(a) memset((a), 0, sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; struct UnionFind { vector<int> v; UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; } int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); } void unite(int x, int y) { v[find(x)] = find(y); } }; int main() { int n, d, x; cin >> n >> d >> x; vvi val(d, vi(n)); REP(i, d) REP(j, n) cin >> val[i][j]; vvi dp(d, vi(100001)); REP(i, x + 1) { dp[0][i] = i; } int ma = x; int tmp; FOR(i, 1, d) { tmp = 0; REP(k, ma + 1) dp[i][k] = k; REP(k, ma + 1) { REP(j, n) if (k - val[i - 1][j] >= 0) { dp[i][k] = max(dp[i][k], dp[i][k - val[i - 1][j]] + val[i][j]); tmp = max(tmp, dp[i][k]); } } ma = max(ma, tmp); } int ans = 0; REP(i, dp[d - 1].size()) ans = max(ans, dp[d - 1][i]); cout << ans << endl; }
replace
63
64
63
64
0
p01719
C++
Time Limit Exceeded
#include <iostream> using namespace std; int n, p[10][10], ans; void saiki(int sum, int x, int day, int I) { int f = 1; for (int i = I + 1; i < n; i++) for (int j = 1; x / (p[day - 1][i] * j); j++) f = 0, saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i); if (f) ans = max(ans, sum + x); } int main() { int d, x; cin >> n >> d >> x; for (int i = 0; i < d; i++) for (int j = 0; j < n; j++) cin >> p[i][j]; ans = x; for (int i = 0; i < d - 1; i++) saiki(0, ans, i + 1, -1); cout << ans << endl; return 0; }
#include <iostream> using namespace std; int n, p[10][10], ans; void saiki(int sum, int x, int day, int I) { int f = 1; for (int i = I + 1; i < n; i++) for (int j = 1; (p[day][i] / p[day - 1][i]) && (x / (p[day - 1][i] * j)); j++) { f = 0; saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i); } if (f) ans = max(ans, sum + x); } int main() { int d, x; cin >> n >> d >> x; for (int i = 0; i < d; i++) for (int j = 0; j < n; j++) cin >> p[i][j]; ans = x; for (int i = 0; i < d - 1; i++) saiki(0, ans, i + 1, -1); cout << ans << endl; return 0; }
replace
7
10
7
12
TLE
p01719
C++
Memory Limit Exceeded
#include <iostream> using namespace std; #define MAX_N 20 #define MAX_M 20000000 #define MAX_P 300000 int d[MAX_N][MAX_N]; int dp[MAX_M]; int dp2[MAX_M]; int DP[MAX_N]; int main() { int n, m, p; cin >> m >> n >> p; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> d[i][j]; } } for (int i = 0; i < MAX_N; i++) { DP[i] = 0; } DP[0] = p; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < MAX_M; j++) { dp[j] = 0; dp2[j] = 0; } for (int j = 0; j < m; j++) { for (int k = 0; k < MAX_M - MAX_P; k++) { dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]); } } for (int j = 1; j < MAX_M; j++) { dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1])); } DP[i + 1] = DP[i] + dp2[DP[i]]; } cout << DP[n - 1] << endl; return 0; }
#include <iostream> using namespace std; #define MAX_N 20 #define MAX_M 6000000 #define MAX_P 300000 int d[MAX_N][MAX_N]; int dp[MAX_M]; int dp2[MAX_M]; int DP[MAX_N]; int main() { int n, m, p; cin >> m >> n >> p; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> d[i][j]; } } for (int i = 0; i < MAX_N; i++) { DP[i] = 0; } DP[0] = p; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < MAX_M; j++) { dp[j] = 0; dp2[j] = 0; } for (int j = 0; j < m; j++) { for (int k = 0; k < MAX_M - MAX_P; k++) { dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]); } } for (int j = 1; j < MAX_M; j++) { dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1])); } DP[i + 1] = DP[i] + dp2[DP[i]]; } cout << DP[n - 1] << endl; return 0; }
replace
4
5
4
5
MLE
p01723
C++
Memory Limit Exceeded
// Template {{{ #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; #ifdef LOCAL #include "contest.h" #else #define dump(x) #endif class range { struct Iterator { int val, inc; int operator*() { return val; } bool operator!=(Iterator &rhs) { return val < rhs.val; } void operator++() { val += inc; } }; Iterator i, n; public: range(int e) : i({0, 1}), n({e, 1}) {} range(int b, int e) : i({b, 1}), n({e, 1}) {} range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {} Iterator &begin() { return i; } Iterator &end() { return n; } }; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; inline bool valid(int x, int w) { return 0 <= x && x < w; } void iostream_init() { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(12); } //}}} typedef vector<int> Node; typedef vector<Node> Graph; int dp[202][202]; int size[202]; const int MOD = 1e9 + 7; int memo[402][402][402]; int ways(int a, int b, int c) { assert(a <= c); assert(b <= c); assert(c <= a + b); if (memo[a][b][c] != -1) return memo[a][b][c]; if (c == 0) return 1; int res = 0; if (a > 0 && b <= c - 1) { res += ways(a - 1, b, c - 1); res %= MOD; } if (b > 0 && a <= c - 1) { res += ways(a, b - 1, c - 1); res %= MOD; } if (a > 0 && b > 0 && c - 1 <= a - 1 + b - 1) { res += ways(a - 1, b - 1, c - 1); res %= MOD; } return memo[a][b][c] = res; } void rec(const Graph &G, int u, int p) { size[u] = 1; for (int v : G[u]) if (v != p) { rec(G, v, u); size[u] += size[v]; } int cur[202] = {}; cur[0] = 1; for (int v : G[u]) if (v != p) { int next[202] = {}; for (int c = 0; c < size[u]; c++) { for (int d = 1; d <= size[v]; d++) { for (int e = max(c, d); e <= c + d; e++) { if (cur[c] > 0 && dp[v][d] > 0) { next[e] += (LL)ways(c, d, e) * cur[c] % MOD * dp[v][d] % MOD; next[e] %= MOD; } } } } memcpy(cur, next, sizeof(next)); } for (int c = 0; c < size[u]; c++) { dump(u); dump(c + 1); dump(cur[c]); dp[u][c + 1] = cur[c]; } } int main() { iostream_init(); memset(memo, -1, sizeof(memo)); assert(ways(1, 1, 1) == 1); assert(ways(1, 1, 2) == 2); int N; while (cin >> N) { Graph G(N); int deg[200] = {}; REP(i, N - 1) { int a, b; cin >> a >> b; G[a].push_back(b); deg[b]++; } int root = -1; REP(i, N) if (deg[i] == 0) root = i; rec(G, root, -1); int ans = 0; for (int c = 1; c <= N; c++) { ans += dp[root][c]; ans %= MOD; } cout << ans << endl; } return 0; } /* vim:set foldmethod=marker: */
// Template {{{ #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; #ifdef LOCAL #include "contest.h" #else #define dump(x) #endif class range { struct Iterator { int val, inc; int operator*() { return val; } bool operator!=(Iterator &rhs) { return val < rhs.val; } void operator++() { val += inc; } }; Iterator i, n; public: range(int e) : i({0, 1}), n({e, 1}) {} range(int b, int e) : i({b, 1}), n({e, 1}) {} range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {} Iterator &begin() { return i; } Iterator &end() { return n; } }; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; inline bool valid(int x, int w) { return 0 <= x && x < w; } void iostream_init() { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(12); } //}}} typedef vector<int> Node; typedef vector<Node> Graph; int dp[202][202]; int size[202]; const int MOD = 1e9 + 7; int memo[202][202][402]; int ways(int a, int b, int c) { assert(a <= c); assert(b <= c); assert(c <= a + b); if (memo[a][b][c] != -1) return memo[a][b][c]; if (c == 0) return 1; int res = 0; if (a > 0 && b <= c - 1) { res += ways(a - 1, b, c - 1); res %= MOD; } if (b > 0 && a <= c - 1) { res += ways(a, b - 1, c - 1); res %= MOD; } if (a > 0 && b > 0 && c - 1 <= a - 1 + b - 1) { res += ways(a - 1, b - 1, c - 1); res %= MOD; } return memo[a][b][c] = res; } void rec(const Graph &G, int u, int p) { size[u] = 1; for (int v : G[u]) if (v != p) { rec(G, v, u); size[u] += size[v]; } int cur[202] = {}; cur[0] = 1; for (int v : G[u]) if (v != p) { int next[202] = {}; for (int c = 0; c < size[u]; c++) { for (int d = 1; d <= size[v]; d++) { for (int e = max(c, d); e <= c + d; e++) { if (cur[c] > 0 && dp[v][d] > 0) { next[e] += (LL)ways(c, d, e) * cur[c] % MOD * dp[v][d] % MOD; next[e] %= MOD; } } } } memcpy(cur, next, sizeof(next)); } for (int c = 0; c < size[u]; c++) { dump(u); dump(c + 1); dump(cur[c]); dp[u][c + 1] = cur[c]; } } int main() { iostream_init(); memset(memo, -1, sizeof(memo)); assert(ways(1, 1, 1) == 1); assert(ways(1, 1, 2) == 2); int N; while (cin >> N) { Graph G(N); int deg[200] = {}; REP(i, N - 1) { int a, b; cin >> a >> b; G[a].push_back(b); deg[b]++; } int root = -1; REP(i, N) if (deg[i] == 0) root = i; rec(G, root, -1); int ans = 0; for (int c = 1; c <= N; c++) { ans += dp[root][c]; ans %= MOD; } cout << ans << endl; } return 0; } /* vim:set foldmethod=marker: */
replace
46
47
46
47
MLE
p01724
C++
Runtime Error
#include <iostream> #define inf 1000000000 using namespace std; const int W = 15, H = 20; char map[W][H]; int sx, sy; const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1}, vy[] = {0, -1, -1, -1, 0, 1, 1, 1}; int dfs(int x, int y) { int nx, ny; int ret = inf; if (y >= H - 2) return 0; for (int d = 0; d < 8; d++) { nx = x + vx[d], ny = y + vy[d]; if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue; if (map[nx][ny] == '.') continue; while (1) { nx += vx[d], ny += vy[d]; if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue; if (map[nx][ny] == '.') { for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++) map[x + k * vx[d]][y + k * vy[d]] = '.'; ret = min(ret, dfs(nx, ny)); for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++) map[x + k * vx[d]][y + k * vy[d]] = 'X'; break; } } } return ret + 1; } int main(void) { for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { cin >> map[x][y]; if (map[x][y] == 'O') { sx = x, sy = y; map[x][y] = '.'; } } } for (int x = 0; x < W; x++) map[x][H - 1] = '.'; int ans = dfs(sx, sy); if (ans >= inf) ans = -1; cout << ans << endl; return 0; }
#include <iostream> #define inf 1000000000 using namespace std; const int W = 15, H = 20; char map[W][H]; int sx, sy; const int vx[] = {1, 1, 0, -1, -1, -1, 0, 1}, vy[] = {0, -1, -1, -1, 0, 1, 1, 1}; int dfs(int x, int y) { int nx, ny; int ret = inf; if (y >= H - 2) return 0; for (int d = 0; d < 8; d++) { nx = x + vx[d], ny = y + vy[d]; if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue; if (map[nx][ny] == '.') continue; while (1) { nx += vx[d], ny += vy[d]; if (ny >= H - 1) { ret = 0; break; } if (nx < 0 || nx >= W || ny < 0) break; if (map[nx][ny] == '.') { for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++) map[x + k * vx[d]][y + k * vy[d]] = '.'; ret = min(ret, dfs(nx, ny)); for (int k = 1; x + k * vx[d] != nx || y + k * vy[d] != ny; k++) map[x + k * vx[d]][y + k * vy[d]] = 'X'; break; } } } return ret + 1; } int main(void) { for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { cin >> map[x][y]; if (map[x][y] == 'O') { sx = x, sy = y; map[x][y] = '.'; } } } for (int x = 0; x < W; x++) map[x][H - 1] = '.'; int ans = dfs(sx, sy); if (ans >= inf) ans = -1; cout << ans << endl; return 0; }
replace
25
27
25
31
0
p01724
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2612&lang=jp #define INF 1 << 30 #define H 19 #define W 15 typedef long long ll; typedef pair<int, int> pii; int ans = INF; int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1}; bool check(int x, int y, vector<vector<char>> &masu, int &dir) { int nx = x + dx[dir], ny = y + dy[dir]; if (masu[nx][ny] != 'X') return false; while (true) { nx += dx[dir]; ny += dy[dir]; if (masu[nx][ny] == 'X') continue; if (masu[nx][ny] == '#') return false; if (masu[nx][ny] == '.') return true; } return false; } void erase_move(int &x, int &y, vector<vector<char>> &masu, int &dir) { while (true) { x += dx[dir]; y += dy[dir]; if (masu[x][y] == 'X') { masu[x][y] = '.'; continue; } if (masu[x][y] == '.') return; } } void dfs(int x, int y, int cnt, vector<vector<char>> &masu) { if (cnt + 1 >= ans) return; for (int dir = 0; dir < 8; dir++) { if (!check(x, y, masu, dir)) continue; auto new_masu = masu; int new_x = x, new_y = y; erase_move(new_x, new_y, new_masu, dir); if (new_x >= 19) { ans = min(ans, cnt + 1); return; } dfs(new_x, new_y, cnt + 1, new_masu); } } int main() { cin.tie(0); ios::sync_with_stdio(false); vector<vector<char>> masu(H + 2, vector<char>(W + 2, '#')); int sx, sy; /* input */ masu[H + 1][0] = masu[H + 1][W + 1] = '.'; for (int i = 1; i <= H + 1; i++) { for (int j = 1; j <= W; j++) { if (i == H + 1) masu[i][j] = '.'; else { cin >> masu[i][j]; if (masu[i][j] == 'O') { sx = i; sy = j; masu[i][j] = '.'; } } } } dfs(sx, sy, 0, masu); cout << (ans == INF ? -1 : ans) << endl; return 0; }
#include "bits/stdc++.h" using namespace std; // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2612&lang=jp #define INF 1 << 30 #define H 19 #define W 15 typedef long long ll; typedef pair<int, int> pii; int ans = INF; int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1}; bool check(int x, int y, vector<vector<char>> &masu, int &dir) { int nx = x + dx[dir], ny = y + dy[dir]; if (masu[nx][ny] != 'X') return false; while (true) { nx += dx[dir]; ny += dy[dir]; if (masu[nx][ny] == 'X') continue; if (masu[nx][ny] == '#') return false; if (masu[nx][ny] == '.') return true; } return false; } void erase_move(int &x, int &y, vector<vector<char>> &masu, int &dir) { while (true) { x += dx[dir]; y += dy[dir]; if (masu[x][y] == 'X') { masu[x][y] = '.'; continue; } if (masu[x][y] == '.') return; } } void dfs(int x, int y, int cnt, vector<vector<char>> &masu) { if (cnt + 1 >= ans) return; for (int dir = 0; dir < 8; dir++) { if (!check(x, y, masu, dir)) continue; auto new_masu = masu; int new_x = x, new_y = y; erase_move(new_x, new_y, new_masu, dir); if (new_x >= 19) { ans = min(ans, cnt + 1); return; } dfs(new_x, new_y, cnt + 1, new_masu); } } int main() { cin.tie(0); ios::sync_with_stdio(false); vector<vector<char>> masu(H + 2, vector<char>(W + 2, '#')); int sx, sy; /* input */ masu[H + 1][0] = masu[H + 1][W + 1] = '.'; for (int i = 1; i <= H + 1; i++) { for (int j = 1; j <= W; j++) { if (i == H + 1) masu[i][j] = '.'; else { cin >> masu[i][j]; if (masu[i][j] == 'O') { sx = i; sy = j; masu[i][j] = '.'; } } } } int f = 0; for (int i = sx + 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (masu[i][j] == 'X') { f = 0; break; } if (j == W) f++; } if (f == 2) { cout << -1 << endl; return 0; } } dfs(sx, sy, 0, masu); cout << (ans == INF ? -1 : ans) << endl; return 0; }
insert
82
82
82
99
TLE
p01724
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) { is >> x; } return is; } typedef vector<vector<bool>> Map; typedef tuple<pair<int, int>, Map, int> T; const int h = 20, w = 17; const int dy[16] = {0, -1, 0, 1, 1, -1, 1, -1, 0, -2, 0, 2}; const int dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0}; int ans; Map c; bool impossible(int y, Map &c) { vector<bool> no(h, 0); range(i, y + 1, 18) { bool f = false; rep(j, w) { if (c[i][j]) f = true; } if (not f) no[i] = true; } rep(i, h - 1) { if (no[i] and no[i + 1]) return true; } return false; } map<Map, map<pair<int, int>, int>> memo; void dfs(int y, int x, int cost) { if (memo.count(c) and memo[c].count(make_pair(y, x)) and memo[c][make_pair(y, x)] <= cost) return; memo[c][make_pair(y, x)] = cost; if (y == 18 or y == 19) { ans = min(ans, cost); return; } if (impossible(y, c)) return; rep(i, 8) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if ((nx == 0 && ny <= 18) or (nx == w - 1 && ny <= 18)) continue; if (c[ny][nx] == 1) { vector<pair<int, int>> tmp; while (true) { if (ny < 0 || ny >= h || nx < 0 || nx >= w) { ny = -1; break; } if ((nx == 0 && ny <= 18) or (nx == w - 1 && ny <= 18)) { ny = -1; break; } if (c[ny][nx] == 0) break; tmp.emplace_back(ny, nx); // pc[ny][nx] = 0; ny = ny + dy[i]; nx = nx + dx[i]; } if (ny == -1) continue; for (auto i : tmp) c[i.first][i.second] = 0; dfs(ny, nx, cost + 1); for (auto i : tmp) c[i.first][i.second] = 1; } } } int main() { pair<int, int> s; c = Map(h, vector<bool>(w)); rep(i, h - 1) { rep(j, w - 2) { char a; cin >> a; if (a == 'X') c[i][j + 1] = 1; else c[i][j + 1] = 0; if (a == 'O') { s = make_pair(i, j + 1); } } } ans = 1e9; dfs(s.first, s.second, 0); cout << (ans == 1e9 ? -1 : ans) << endl; }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) for (int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; using namespace std; template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { os << v[i] << (i == v.size() - 1 ? "" : " "); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) { is >> x; } return is; } typedef vector<vector<bool>> Map; typedef tuple<pair<int, int>, Map, int> T; const int h = 20, w = 17; const int dy[16] = {0, -1, 0, 1, 1, -1, 1, -1, 0, -2, 0, 2}; const int dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0}; int ans; Map c; bool impossible(int y, Map &c) { vector<bool> no(h, 0); range(i, y + 1, 18) { bool f = false; rep(j, w) { if (c[i][j]) f = true; } if (not f) no[i] = true; } rep(i, h - 1) { if (no[i] and no[i + 1]) return true; } return false; } map<Map, map<pair<int, int>, int>> memo; void dfs(int y, int x, int cost) { if (ans <= cost) return; if (memo.count(c) and memo[c].count(make_pair(y, x)) and memo[c][make_pair(y, x)] <= cost) return; memo[c][make_pair(y, x)] = cost; if (y == 18 or y == 19) { ans = min(ans, cost); return; } if (impossible(y, c)) return; rep(i, 8) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue; if ((nx == 0 && ny <= 18) or (nx == w - 1 && ny <= 18)) continue; if (c[ny][nx] == 1) { vector<pair<int, int>> tmp; while (true) { if (ny < 0 || ny >= h || nx < 0 || nx >= w) { ny = -1; break; } if ((nx == 0 && ny <= 18) or (nx == w - 1 && ny <= 18)) { ny = -1; break; } if (c[ny][nx] == 0) break; tmp.emplace_back(ny, nx); // pc[ny][nx] = 0; ny = ny + dy[i]; nx = nx + dx[i]; } if (ny == -1) continue; for (auto i : tmp) c[i.first][i.second] = 0; dfs(ny, nx, cost + 1); for (auto i : tmp) c[i.first][i.second] = 1; } } } int main() { pair<int, int> s; c = Map(h, vector<bool>(w)); rep(i, h - 1) { rep(j, w - 2) { char a; cin >> a; if (a == 'X') c[i][j + 1] = 1; else c[i][j + 1] = 0; if (a == 'O') { s = make_pair(i, j + 1); } } } ans = 1e9; dfs(s.first, s.second, 0); cout << (ans == 1e9 ? -1 : ans) << endl; }
insert
49
49
49
51
TLE
p01724
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; struct State { int x[19][15]; }; State S; int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int solve(State p) { int cx = 0, cy = 0; for (int i = 0; i < 285; i++) { if (p.x[i / 15][i % 15] == 2) { cx = i / 15; cy = i % 15; } } vector<int> vec; for (int i = 0; i < 8; i++) { int ex = cx + dx[i], ey = cy + dy[i]; if (ex < 0 || ey < 0 || ex >= 19 || ey >= 15) continue; if (p.x[ex][ey] == 0) continue; vec.push_back(i); } if (vec.size() == 0) return 999999; int minx = 999999; for (int i = 0; i < 8; i++) { bool OK2 = false; for (int j = 0; j < vec.size(); j++) { if (vec[j] == i) OK2 = true; } if (OK2 == false) continue; State T = p; int ex = cx, ey = cy; bool flag = true; while (true) { ex += dx[i]; ey += dy[i]; if (ex >= 19) break; if (ex < 0 || ey < 0 || ey >= 15) { flag = false; break; } if (T.x[ex][ey] == 0) break; T.x[ex][ey] = 0; } if (flag == false) continue; if (abs(ex - cx) <= 1 && abs(ey - cy) <= 1) continue; if (ex == 18 || ex == 19) minx = min(minx, 1); else { T.x[cx][cy] = 0; T.x[ex][ey] = 2; minx = min(minx, solve(T) + 1); } } return minx; } int main() { for (int i = 0; i < 19; i++) { for (int j = 0; j < 15; j++) { char p; cin >> p; if (p == 'O') S.x[i][j] = 2; if (p == 'X') S.x[i][j] = 1; if (p == '.') S.x[i][j] = 0; } } int R = solve(S); if (R >= 100000) R = -1; cout << R << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; struct State { int x[19][15]; }; State S; int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int solve(State p) { bool OK3 = false; for (int i = 0; i < 15; i++) { if (p.x[18][i] == 1 || p.x[17][i] == 1) OK3 = true; } if (OK3 == false) return 999999; int cx = 0, cy = 0; for (int i = 0; i < 285; i++) { if (p.x[i / 15][i % 15] == 2) { cx = i / 15; cy = i % 15; } } vector<int> vec; for (int i = 0; i < 8; i++) { int ex = cx + dx[i], ey = cy + dy[i]; if (ex < 0 || ey < 0 || ex >= 19 || ey >= 15) continue; if (p.x[ex][ey] == 0) continue; vec.push_back(i); } if (vec.size() == 0) return 999999; int minx = 999999; for (int i = 0; i < 8; i++) { bool OK2 = false; for (int j = 0; j < vec.size(); j++) { if (vec[j] == i) OK2 = true; } if (OK2 == false) continue; State T = p; int ex = cx, ey = cy; bool flag = true; while (true) { ex += dx[i]; ey += dy[i]; if (ex >= 19) break; if (ex < 0 || ey < 0 || ey >= 15) { flag = false; break; } if (T.x[ex][ey] == 0) break; T.x[ex][ey] = 0; } if (flag == false) continue; if (abs(ex - cx) <= 1 && abs(ey - cy) <= 1) continue; if (ex == 18 || ex == 19) minx = min(minx, 1); else { T.x[cx][cy] = 0; T.x[ex][ey] = 2; minx = min(minx, solve(T) + 1); } } return minx; } int main() { for (int i = 0; i < 19; i++) { for (int j = 0; j < 15; j++) { char p; cin >> p; if (p == 'O') S.x[i][j] = 2; if (p == 'X') S.x[i][j] = 1; if (p == '.') S.x[i][j] = 0; } } int R = solve(S); if (R >= 100000) R = -1; cout << R << endl; return 0; }
insert
11
11
11
18
TLE
p01725
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef string::const_iterator State; map<char, int> op; ll term(State &, int); ll number(State &); ll calc(ll x, char c, ll y) { if (c == '+') x += y; if (c == '-') x -= y; if (c == '*') x *= y; return x; } ll term(State &begin, int d) { ll ret = term(begin, d + 1); while (1) { if (d == 4) { if (*begin == '(') { begin++; ll ret = term(begin, 1); begin++; return ret; } else { return number(begin); } } else if (op[*begin] == d) { char c = *begin; begin++; ret = calc(ret, c, term(begin, d + 1)); } else { break; } } return ret; } ll number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } int main() { ll res = 0; bool f = 1; string s; cin >> s; REP(i, 3) REP(j, 3) REP(k, 3) { op['+'] = i + 1; op['-'] = j + 1; op['*'] = k + 1; State begin = s.begin(); if (f) res = term(begin, 1), f = 0; else res = max(res, term(begin, 1)); } cout << res << endl; return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; typedef string::const_iterator State; map<char, int> op; ll term(State &, int); ll number(State &); ll calc(ll x, char c, ll y) { if (c == '+') x += y; if (c == '-') x -= y; if (c == '*') x *= y; return x; } ll term(State &begin, int d) { ll ret; if (d < 4) ret = term(begin, d + 1); while (1) { if (d == 4) { if (*begin == '(') { begin++; ll ret = term(begin, 1); begin++; return ret; } else { return number(begin); } } else if (op[*begin] == d) { char c = *begin; begin++; ret = calc(ret, c, term(begin, d + 1)); } else { break; } } return ret; } ll number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } int main() { ll res = 0; bool f = 1; string s; cin >> s; REP(i, 3) REP(j, 3) REP(k, 3) { op['+'] = i + 1; op['-'] = j + 1; op['*'] = k + 1; State begin = s.begin(); if (f) res = term(begin, 1), f = 0; else res = max(res, term(begin, 1)); } cout << res << endl; return 0; }
replace
43
44
43
46
-11
p01726
C++
Memory Limit Exceeded
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual // Studio 2015\Projects\programing_contest_c++\Debug\b.answer" using ll = long long int; using ha = pair<ll, int>; struct RollingHash { static const ll mul0 = 10009, mul1 = 10007; static const ll add0 = 1000010007, add1 = 1003333331; size_t len; // vector<ll> hash_, hash2_; vector<ha> hash_; // static vector<ll> pmo_, pmo2_; static vector<ha> pmo_; void init(const vector<ll> &s) { len = s.size(); hash_.resize(len + 1); hash_[0] = make_pair(0, 0); if (pmo_.empty()) { pmo_.push_back(make_pair(1, 1)); } while (pmo_.size() <= len) { pmo_.push_back( make_pair(pmo_.back().first * mul0, pmo_.back().second * mul1)); } for (unsigned int i = 0; i < len; ++i) { hash_[i + 1] = make_pair((hash_[i].first * mul0 + s[i]), (hash_[i].second * mul1 + s[i])); } return; } void init(const string &s) { vector<ll> v; for (char c : s) { v.push_back(c); } init(v); } ha hash(const int l, const int r) const { // s[l..r] return make_pair( hash_[r + 1].first - hash_[l].first * pmo_[r + 1 - l].first, hash_[r + 1].second - hash_[l].second * pmo_[r + 1 - l].second); } ha hash() const { // s[all] return hash(0, len - 1); } // hash???????????? static ha concat(const ha L, const ha R, const int RLength) { // hash(L+R) return make_pair((R.first + L.first * pmo_[RLength].first), (R.second + L.second * pmo_[RLength].second)); } // index????????????from??????to????????????????????????????????\???????????? ha get_changed_hash(const int index, const ll from, const ll to) const { const ha p(hash()); return make_pair(p.first + (to - from) * pmo_[len - index - 1].first, p.second + (to - from) * pmo_[len - index - 1].second); } }; vector<ha> RollingHash::pmo_; int main() { string s, t; cin >> s >> t; RollingHash base_rh; base_rh.init(t); vector<ha> hs; for (int i = 0; i < t.size(); ++i) { for (int c = 0; c < 26; ++c) { char ch = 'a' + c; if (ch == t[i]) continue; else { ha ahash = base_rh.get_changed_hash(i, t[i], ch); hs.emplace_back(ahash); } } for (int c = 0; c < 26; ++c) { char ch = 'A' + c; if (ch == t[i]) continue; else { ha ahash = base_rh.get_changed_hash(i, t[i], ch); hs.emplace_back(ahash); } } } sort(hs.begin(), hs.end()); hs.erase(unique(hs.begin(), hs.end()), hs.end()); RollingHash s_rh; s_rh.init(s); long long int ans = 0; for (int i = 0; i < s.size() - t.size() + 1; ++i) { ha h = s_rh.hash(i, i + t.size() - 1); if (binary_search(hs.begin(), hs.end(), h)) { ans++; } } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; // < "D:\D_Download\Visual Studio // 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual // Studio 2015\Projects\programing_contest_c++\Debug\b.answer" using ll = long long int; using ha = pair<int, int>; struct RollingHash { static const ll mul0 = 10009, mul1 = 10007; static const ll add0 = 1000010007, add1 = 1003333331; size_t len; // vector<ll> hash_, hash2_; vector<ha> hash_; // static vector<ll> pmo_, pmo2_; static vector<ha> pmo_; void init(const vector<ll> &s) { len = s.size(); hash_.resize(len + 1); hash_[0] = make_pair(0, 0); if (pmo_.empty()) { pmo_.push_back(make_pair(1, 1)); } while (pmo_.size() <= len) { pmo_.push_back( make_pair(pmo_.back().first * mul0, pmo_.back().second * mul1)); } for (unsigned int i = 0; i < len; ++i) { hash_[i + 1] = make_pair((hash_[i].first * mul0 + s[i]), (hash_[i].second * mul1 + s[i])); } return; } void init(const string &s) { vector<ll> v; for (char c : s) { v.push_back(c); } init(v); } ha hash(const int l, const int r) const { // s[l..r] return make_pair( hash_[r + 1].first - hash_[l].first * pmo_[r + 1 - l].first, hash_[r + 1].second - hash_[l].second * pmo_[r + 1 - l].second); } ha hash() const { // s[all] return hash(0, len - 1); } // hash???????????? static ha concat(const ha L, const ha R, const int RLength) { // hash(L+R) return make_pair((R.first + L.first * pmo_[RLength].first), (R.second + L.second * pmo_[RLength].second)); } // index????????????from??????to????????????????????????????????\???????????? ha get_changed_hash(const int index, const ll from, const ll to) const { const ha p(hash()); return make_pair(p.first + (to - from) * pmo_[len - index - 1].first, p.second + (to - from) * pmo_[len - index - 1].second); } }; vector<ha> RollingHash::pmo_; int main() { string s, t; cin >> s >> t; RollingHash base_rh; base_rh.init(t); vector<ha> hs; for (int i = 0; i < t.size(); ++i) { for (int c = 0; c < 26; ++c) { char ch = 'a' + c; if (ch == t[i]) continue; else { ha ahash = base_rh.get_changed_hash(i, t[i], ch); hs.emplace_back(ahash); } } for (int c = 0; c < 26; ++c) { char ch = 'A' + c; if (ch == t[i]) continue; else { ha ahash = base_rh.get_changed_hash(i, t[i], ch); hs.emplace_back(ahash); } } } sort(hs.begin(), hs.end()); hs.erase(unique(hs.begin(), hs.end()), hs.end()); RollingHash s_rh; s_rh.init(s); long long int ans = 0; for (int i = 0; i < s.size() - t.size() + 1; ++i) { ha h = s_rh.hash(i, i + t.size() - 1); if (binary_search(hs.begin(), hs.end(), h)) { ans++; } } cout << ans << endl; return 0; }
replace
14
15
14
15
MLE
p01726
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; #define MOD 1000000007ll #define INF 1000000000ll #define EPS 1e-10 #define FOR(i, n, m) for (ll i = n; i < (ll)m; i++) #define REP(i, n) FOR(i, 0, n) #define DUMP(a) \ REP(d, a.size()) { \ cout << a[d]; \ if (d != a.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define ALL(v) v.begin(), v.end() #define UNIQUE(v) \ sort(ALL(v)); \ v.erase(unique(ALL(v)), v.end()); #define pb push_back string s, t; vector<int> _s; vector<int> _t; ll m, n; vector<pair<int, int>> hsh(15600000, P(-1, -1)); ll idx = 0; const ll base = 100007LL; const ll mod1 = 1000000007LL, mod2 = 1000000009LL; vector<ll> fact1(300030, 0); vector<ll> fact2(300030, 0); ll rolling1 = 0, rolling2 = 0; void fill_fact() { fact1[0] = 1; fact2[0] = 1; FOR(i, 1, 300030) { fact1[i] = (fact1[i - 1] * base); fact1[i] %= mod1; fact2[i] = (fact2[i - 1] * base); fact2[i] %= mod2; } } void fill_hash() { REP(i, n) { rolling1 *= base; rolling2 *= base; rolling1 += _t[i]; rolling2 += _t[i]; rolling1 %= mod1; rolling2 %= mod2; } REP(i, n) { rolling1 -= fact1[n - 1 - i] * _t[i]; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 -= fact2[n - 1 - i] * _t[i]; rolling2 = (rolling2 % mod2 + mod2) % mod2; for (ll j = 1; j <= 52; j++) { rolling1 += fact1[n - 1 - i] * j; rolling1 %= mod1; rolling2 += fact2[n - 1 - i] * j; rolling2 %= mod2; hsh[idx] = pair<int, int>(rolling1, rolling2); idx++; rolling1 -= fact1[n - 1 - i] * j; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 -= fact2[n - 1 - i] * j; rolling2 = (rolling2 % mod2 + mod2) % mod2; } rolling1 += fact1[n - 1 - i] * _t[i]; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 += fact2[n - 1 - i] * _t[i]; rolling2 = (rolling2 % mod2 + mod2) % mod2; } UNIQUE(hsh); auto ite = lower_bound(ALL(hsh), pair<int, int>(rolling1, rolling2)); hsh.erase(ite); } ll solve() { fill_fact(); fill_hash(); ll r1 = 0, r2 = 0; ll ret = 0; for (ll i = 0; i < m - n + 1; i++) { if (i == 0) { REP(j, n) { r1 *= base; r2 *= base; r1 += _s[j]; r2 += _s[j]; r1 %= mod1; r2 %= mod2; } } else { r1 *= base; r2 *= base; r1 += _s[n - 1 + i]; r2 += _s[n - 1 + i]; r1 -= fact1[n] * _s[i - 1]; r2 -= fact2[n] * _s[i - 1]; r1 = (r1 % mod1 + mod1) % mod1; r2 = (r2 % mod2 + mod2) % mod2; } auto ite = lower_bound(ALL(hsh), pair<int, int>(r1, r2)); if (*ite == pair<int, int>(r1, r2)) ret++; } return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> s >> t; m = s.size(); n = t.size(); _s.assign(m, 0); _t.assign(n, 0); REP(i, m) { if (s[i] >= 'A' && s[i] <= 'Z') _s[i] = 27 + (s[i] - 'A'); else _s[i] = 1 + (s[i] - 'a'); } REP(i, n) { if (t[i] >= 'A' && t[i] <= 'Z') _t[i] = 27 + (t[i] - 'A'); else _t[i] = 1 + (t[i] - 'a'); } cout << solve() << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; #define MOD 1000000007ll #define INF 1000000000ll #define EPS 1e-10 #define FOR(i, n, m) for (ll i = n; i < (ll)m; i++) #define REP(i, n) FOR(i, 0, n) #define DUMP(a) \ REP(d, a.size()) { \ cout << a[d]; \ if (d != a.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define ALL(v) v.begin(), v.end() #define UNIQUE(v) \ sort(ALL(v)); \ v.erase(unique(ALL(v)), v.end()); #define pb push_back string s, t; vector<char> _s; vector<char> _t; ll m, n; vector<pair<int, int>> hsh(15600000, P(-1, -1)); ll idx = 0; const ll base = 100007LL; const ll mod1 = 1000000007LL, mod2 = 1000000009LL; vector<ll> fact1(300030, 0); vector<ll> fact2(300030, 0); ll rolling1 = 0, rolling2 = 0; void fill_fact() { fact1[0] = 1; fact2[0] = 1; FOR(i, 1, 300030) { fact1[i] = (fact1[i - 1] * base); fact1[i] %= mod1; fact2[i] = (fact2[i - 1] * base); fact2[i] %= mod2; } } void fill_hash() { REP(i, n) { rolling1 *= base; rolling2 *= base; rolling1 += _t[i]; rolling2 += _t[i]; rolling1 %= mod1; rolling2 %= mod2; } REP(i, n) { rolling1 -= fact1[n - 1 - i] * _t[i]; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 -= fact2[n - 1 - i] * _t[i]; rolling2 = (rolling2 % mod2 + mod2) % mod2; for (ll j = 1; j <= 52; j++) { rolling1 += fact1[n - 1 - i] * j; rolling1 %= mod1; rolling2 += fact2[n - 1 - i] * j; rolling2 %= mod2; hsh[idx] = pair<int, int>(rolling1, rolling2); idx++; rolling1 -= fact1[n - 1 - i] * j; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 -= fact2[n - 1 - i] * j; rolling2 = (rolling2 % mod2 + mod2) % mod2; } rolling1 += fact1[n - 1 - i] * _t[i]; rolling1 = (rolling1 % mod1 + mod1) % mod1; rolling2 += fact2[n - 1 - i] * _t[i]; rolling2 = (rolling2 % mod2 + mod2) % mod2; } UNIQUE(hsh); auto ite = lower_bound(ALL(hsh), pair<int, int>(rolling1, rolling2)); hsh.erase(ite); } ll solve() { fill_fact(); fill_hash(); ll r1 = 0, r2 = 0; ll ret = 0; for (ll i = 0; i < m - n + 1; i++) { if (i == 0) { REP(j, n) { r1 *= base; r2 *= base; r1 += _s[j]; r2 += _s[j]; r1 %= mod1; r2 %= mod2; } } else { r1 *= base; r2 *= base; r1 += _s[n - 1 + i]; r2 += _s[n - 1 + i]; r1 -= fact1[n] * _s[i - 1]; r2 -= fact2[n] * _s[i - 1]; r1 = (r1 % mod1 + mod1) % mod1; r2 = (r2 % mod2 + mod2) % mod2; } auto ite = lower_bound(ALL(hsh), pair<int, int>(r1, r2)); if (*ite == pair<int, int>(r1, r2)) ret++; } return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> s >> t; m = s.size(); n = t.size(); _s.assign(m, 0); _t.assign(n, 0); REP(i, m) { if (s[i] >= 'A' && s[i] <= 'Z') _s[i] = 27 + (s[i] - 'A'); else _s[i] = 1 + (s[i] - 'a'); } REP(i, n) { if (t[i] >= 'A' && t[i] <= 'Z') _t[i] = 27 + (t[i] - 'A'); else _t[i] = 1 + (t[i] - 'a'); } cout << solve() << endl; }
replace
25
27
25
27
MLE
p01726
C++
Runtime Error
#include <algorithm> #include <cassert> #include <iostream> #include <string> #include <vector> using namespace std; int n, k; int rk[300100]; int tmp[300100]; bool compare_sa(int i, int j) { if (rk[i] != rk[j]) return rk[i] < rk[j]; int ri, rj; ri = i + k <= n ? rk[i + k] : -1; rj = j + k <= n ? rk[j + k] : -1; return ri < rj; } void constract_sa(const string &s, vector<int> &v) { v.resize(s.size() + 1); n = s.size() + 1; for (int i = 0; i < v.size(); i++) v[i] = i; for (int i = 0; i < s.size(); i++) rk[i] = s[i] - 'a' + 1; rk[s.size()] = 0; for (k = 1; k <= n; k *= 2) { sort(v.begin(), v.end(), compare_sa); tmp[v[0]] = 0; for (int i = 1; i < v.size(); i++) { if (compare_sa(v[i - 1], v[i])) tmp[v[i]] = tmp[v[i - 1]] + 1; else tmp[v[i]] = tmp[v[i - 1]]; } for (int i = 0; i <= n; i++) { rk[i] = tmp[i]; } } } void construct_lcp(const string &s, const vector<int> &sa, vector<int> &lcp) { int n = s.size(); lcp.resize(n); for (int i = 0; i <= n; i++) rk[sa[i]] = i; int h = 0; lcp[0] = 0; for (int i = 0; i < n; i++) { assert(rk[i] - 1 >= 0); int j = sa[rk[i] - 1]; if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rk[i] - 1] = h; } } int main() { string S, T; vector<int> sa[2]; vector<int> same[2]; cin >> S >> T; for (int i = 0; i < 2; i++) { int len = 0; constract_sa(S, sa[i]); same[i].resize(sa[i].size()); vector<int> lcp; construct_lcp(S, sa[i], lcp); for (int j = 0; j < sa[i].size(); j++) { if (j && lcp[j - 1] < len) len = lcp[j - 1]; while (len > 0 && S[sa[i][j] + len - 1] != T[len - 1]) len--; for (int k = sa[i][j] + len; k < S.size() && len < T.size(); k++) { if (S[k] == T[len]) len++; else break; } same[i][sa[i][j]] = len; } reverse(T.begin(), T.end()); reverse(S.begin(), S.end()); } /* for(int i = 0; i < 2; i++) { for(int j = 0; j < sa[i].size(); j++) { cout << j << ":" << sa[i][j] << endl; } cout << endl; } for(int i = 0; i < 2; i++) { for(int j = 0; j < same[i].size(); j++) { cout << same[i][j] << " "; } cout << endl; } // */ int res = 0; for (int i = 0; i < same[0].size(); i++) { if (0 <= (int)S.size() - (int)(T.size() + i) && same[0][i] + same[1][S.size() - (T.size() + i)] == T.size() - 1) { res++; } } cout << res << endl; }
#include <algorithm> #include <cassert> #include <iostream> #include <string> #include <vector> using namespace std; int n, k; int rk[300100]; int tmp[300100]; bool compare_sa(int i, int j) { if (rk[i] != rk[j]) return rk[i] < rk[j]; int ri, rj; ri = i + k <= n ? rk[i + k] : -1; rj = j + k <= n ? rk[j + k] : -1; return ri < rj; } void constract_sa(const string &s, vector<int> &v) { v.resize(s.size() + 1); n = s.size() + 1; for (int i = 0; i < v.size(); i++) v[i] = i; for (int i = 0; i < s.size(); i++) rk[i] = s[i] + 1; rk[s.size()] = 0; for (k = 1; k <= n; k *= 2) { sort(v.begin(), v.end(), compare_sa); tmp[v[0]] = 0; for (int i = 1; i < v.size(); i++) { if (compare_sa(v[i - 1], v[i])) tmp[v[i]] = tmp[v[i - 1]] + 1; else tmp[v[i]] = tmp[v[i - 1]]; } for (int i = 0; i <= n; i++) { rk[i] = tmp[i]; } } } void construct_lcp(const string &s, const vector<int> &sa, vector<int> &lcp) { int n = s.size(); lcp.resize(n); for (int i = 0; i <= n; i++) rk[sa[i]] = i; int h = 0; lcp[0] = 0; for (int i = 0; i < n; i++) { assert(rk[i] - 1 >= 0); int j = sa[rk[i] - 1]; if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rk[i] - 1] = h; } } int main() { string S, T; vector<int> sa[2]; vector<int> same[2]; cin >> S >> T; for (int i = 0; i < 2; i++) { int len = 0; constract_sa(S, sa[i]); same[i].resize(sa[i].size()); vector<int> lcp; construct_lcp(S, sa[i], lcp); for (int j = 0; j < sa[i].size(); j++) { if (j && lcp[j - 1] < len) len = lcp[j - 1]; while (len > 0 && S[sa[i][j] + len - 1] != T[len - 1]) len--; for (int k = sa[i][j] + len; k < S.size() && len < T.size(); k++) { if (S[k] == T[len]) len++; else break; } same[i][sa[i][j]] = len; } reverse(T.begin(), T.end()); reverse(S.begin(), S.end()); } /* for(int i = 0; i < 2; i++) { for(int j = 0; j < sa[i].size(); j++) { cout << j << ":" << sa[i][j] << endl; } cout << endl; } for(int i = 0; i < 2; i++) { for(int j = 0; j < same[i].size(); j++) { cout << same[i][j] << " "; } cout << endl; } // */ int res = 0; for (int i = 0; i < same[0].size(); i++) { if (0 <= (int)S.size() - (int)(T.size() + i) && same[0][i] + same[1][S.size() - (T.size() + i)] == T.size() - 1) { res++; } } cout << res << endl; }
replace
26
27
26
27
0
p01730
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef pair<double, double> P; double D(P a, P b) { int x1 = floor(a.F / 10) * 10, x2 = floor(b.F / 10) * 10, y1 = floor(a.S / 10) * 10, y2 = floor(b.S / 10) * 10; if (x1 != a.F && x2 != b.F && x1 == x2 && y1 != y2) { double d1 = fabs(a.F - x1), d2 = fabs(b.F - x2); return fabs(a.S - b.S) + min(d1 + d2, 20 - d1 - d2); } else if (y1 != a.S && y2 != b.S && y1 == y2 && x1 != x2) { double d1 = fabs(a.S - y1), d2 = fabs(b.S - y2); return fabs(a.F - b.F) + min(d1 + d2, 20 - d1 - d2); } return fabs(a.F - b.F) + fabs(a.S - b.S); } int n, M = 1 << 29; P a[11111]; double solve(int x, int y, double s, double t) { double ans = M; for (double l = x - 100; l <= x + 100; l += s) { for (double r = y - 100; r <= y + 100; r += t) { double d = 0; for (int i = 0; i < n; i++) d = max(d, D(a[i], P(l, r))); ans = min(ans, d); } } return ans; } int main() { cin >> n; double Max = -M, Mix = M, May = -M, Miy = M; for (int i = 0; i < n; i++) { cin >> a[i].F >> a[i].S; Max = max(Max, a[i].F + a[i].S); Mix = min(Mix, a[i].F + a[i].S); May = max(May, a[i].F - a[i].S); Miy = min(Miy, a[i].F - a[i].S); } int xx = (Max + Mix) / 2, yy = (May + Miy) / 2; int x = floor((double)(xx + yy) / 20) * 10, y = floor((double)(xx - yy) / 20) * 10; printf("%.10f\n", min(solve(x, y, 10, 0.5), solve(x, y, 0.5, 10))); return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef pair<double, double> P; double D(P a, P b) { int x1 = floor(a.F / 10) * 10, x2 = floor(b.F / 10) * 10, y1 = floor(a.S / 10) * 10, y2 = floor(b.S / 10) * 10; if (x1 != a.F && x2 != b.F && x1 == x2 && y1 != y2) { double d1 = fabs(a.F - x1), d2 = fabs(b.F - x2); return fabs(a.S - b.S) + min(d1 + d2, 20 - d1 - d2); } else if (y1 != a.S && y2 != b.S && y1 == y2 && x1 != x2) { double d1 = fabs(a.S - y1), d2 = fabs(b.S - y2); return fabs(a.F - b.F) + min(d1 + d2, 20 - d1 - d2); } return fabs(a.F - b.F) + fabs(a.S - b.S); } int n, M = 1 << 29; P a[11111]; double solve(int x, int y, double s, double t) { double ans = M; for (double l = x - 30; l <= x + 30; l += s) { for (double r = y - 30; r <= y + 30; r += t) { double d = 0; for (int i = 0; i < n; i++) d = max(d, D(a[i], P(l, r))); ans = min(ans, d); } } return ans; } int main() { cin >> n; double Max = -M, Mix = M, May = -M, Miy = M; for (int i = 0; i < n; i++) { cin >> a[i].F >> a[i].S; Max = max(Max, a[i].F + a[i].S); Mix = min(Mix, a[i].F + a[i].S); May = max(May, a[i].F - a[i].S); Miy = min(Miy, a[i].F - a[i].S); } int xx = (Max + Mix) / 2, yy = (May + Miy) / 2; int x = floor((double)(xx + yy) / 20) * 10, y = floor((double)(xx - yy) / 20) * 10; printf("%.10f\n", min(solve(x, y, 10, 0.5), solve(x, y, 0.5, 10))); return 0; }
replace
23
25
23
25
TLE
p01731
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; #define INF INT_MAX / 3 #define MAX_N 1000 int n; int k[55]; string s[55]; void dfs(int d, int dd) { while (1) { int ni = -1; rep(i, n) { if (k[i] == d) { ni = i; break; } } if (ni == -1) return; rep(i, dd) cout << "."; cout << s[ni] << endl; k[ni] = -1; dfs(ni + 1, dd + 1); } } int main() { cin.sync_with_stdio(false); cin >> n; rep(i, n) cin >> k[i] >> s[i]; dfs(0, 0); return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define each(itr, v) for (auto itr : v) #define pb(s) push_back(s) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define maxch(x, y) x = max(x, y) #define minch(x, y) x = min(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt(x) bitset<32>(x).count() typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<P, int> PPI; typedef pair<ll, ll> PL; typedef pair<P, ll> PPL; #define INF INT_MAX / 3 #define MAX_N 1000 int n; int k[1111]; string s[1111]; void dfs(int d, int dd) { while (1) { int ni = -1; rep(i, n) { if (k[i] == d) { ni = i; break; } } if (ni == -1) return; rep(i, dd) cout << "."; cout << s[ni] << endl; k[ni] = -1; dfs(ni + 1, dd + 1); } } int main() { cin.sync_with_stdio(false); cin >> n; rep(i, n) cin >> k[i] >> s[i]; dfs(0, 0); return 0; }
replace
30
32
30
32
0
p01732
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int w, h, n; int sx, sy, gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool can(int y, int x) { if (0 <= y && y < h && 0 <= x && x < w) return true; return false; } struct edge { int from, to; ll cost; edge(int t, int c) : to(t), cost(c) {} edge(int f, int t, int c) : from(f), to(t), cost(c) {} bool operator<(const edge &e) const { return cost < e.cost; } }; vector<edge> G[500 * 500 + 5]; ll d[500 * 500 + 5]; void dijkstra(int s, int n) { priority_queue<P, vector<P>, greater<P>> que; fill(d, d + n, INF); d[s] = 0; que.push(P(0, s)); while (que.size()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; rep(i, G[v].size()) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { cin >> w >> h >> n; cin >> sx >> sy >> gx >> gy; rep(i, h * w) { G[i].clear(); } w++; h++; rep(i, h) { rep(j, w) { rep(k, 4) { int y = i + dy[k]; int x = j + dx[k]; if (can(y, x)) { G[i * w + j].push_back(edge(y * w + x, 0)); } } } } rep(i, n) { int y, x, t; cin >> x >> y >> t; string s; cin >> s; rep(k, t) { rep(j, s.size()) { int from = -1, to = -1; if (s[j] == 'R' && x + 1 < w - 1) { from = y * w + (x + 1); to = (y + 1) * w + (x + 1); x++; } else if (s[j] == 'L' && 0 <= x - 1) { from = y * w + x; to = (y + 1) * w + x; x--; } else if (s[j] == 'U' && 0 <= y - 1) { from = y * w + x; to = y * w + (x + 1); y--; } else if (s[j] == 'D' && y + 1 < h - 1) { from = (y + 1) * w + x; to = (y + 1) * w + (x + 1); y++; } if (from == -1 && to == -1) continue; rep(k, G[from].size()) { if (G[from][k].to == to) { G[from][k].cost++; } } rep(k, G[to].size()) { if (G[to][k].to == from) { G[to][k].cost++; } } } } } // rep(i, h) { // rep(j, w) { // cout << " --------- " << i << ", " << j << " :" << i * w + j << " // |"; rep(k, G[i*w + j].size()) { cout << "(" << G[i*w + j][k].to << ", " << // G[i*w + j][k].cost << ") "; // } // cout << endl; // } // } dijkstra(sy * w + sx, h * w); // rep(i, h) { // rep(j, w) { // cout << d[i*w + j] << " "; // } // cout << endl; // } cout << d[gy * w + gx] << endl; return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int w, h, n; int sx, sy, gx, gy; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool can(int y, int x) { if (0 <= y && y < h && 0 <= x && x < w) return true; return false; } struct edge { int from, to; ll cost; edge(int t, int c) : to(t), cost(c) {} edge(int f, int t, int c) : from(f), to(t), cost(c) {} bool operator<(const edge &e) const { return cost < e.cost; } }; vector<edge> G[505 * 505 + 5]; ll d[505 * 505 + 5]; void dijkstra(int s, int n) { priority_queue<P, vector<P>, greater<P>> que; fill(d, d + n, INF); d[s] = 0; que.push(P(0, s)); while (que.size()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; rep(i, G[v].size()) { edge e = G[v][i]; if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int main() { cin >> w >> h >> n; cin >> sx >> sy >> gx >> gy; rep(i, h * w) { G[i].clear(); } w++; h++; rep(i, h) { rep(j, w) { rep(k, 4) { int y = i + dy[k]; int x = j + dx[k]; if (can(y, x)) { G[i * w + j].push_back(edge(y * w + x, 0)); } } } } rep(i, n) { int y, x, t; cin >> x >> y >> t; string s; cin >> s; rep(k, t) { rep(j, s.size()) { int from = -1, to = -1; if (s[j] == 'R' && x + 1 < w - 1) { from = y * w + (x + 1); to = (y + 1) * w + (x + 1); x++; } else if (s[j] == 'L' && 0 <= x - 1) { from = y * w + x; to = (y + 1) * w + x; x--; } else if (s[j] == 'U' && 0 <= y - 1) { from = y * w + x; to = y * w + (x + 1); y--; } else if (s[j] == 'D' && y + 1 < h - 1) { from = (y + 1) * w + x; to = (y + 1) * w + (x + 1); y++; } if (from == -1 && to == -1) continue; rep(k, G[from].size()) { if (G[from][k].to == to) { G[from][k].cost++; } } rep(k, G[to].size()) { if (G[to][k].to == from) { G[to][k].cost++; } } } } } // rep(i, h) { // rep(j, w) { // cout << " --------- " << i << ", " << j << " :" << i * w + j << " // |"; rep(k, G[i*w + j].size()) { cout << "(" << G[i*w + j][k].to << ", " << // G[i*w + j][k].cost << ") "; // } // cout << endl; // } // } dijkstra(sy * w + sx, h * w); // rep(i, h) { // rep(j, w) { // cout << d[i*w + j] << " "; // } // cout << endl; // } cout << d[gy * w + gx] << endl; return 0; }
replace
41
43
41
43
0
p01732
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> Pair; struct Edge { int to, w; bool operator<(const Edge &e) const { return w > e.w; } }; typedef vector<vector<Edge>> Graph; const int INF = 1 << 28; const int di[] = {0, 1, 0, -1}; const int dj[] = {1, 0, -1, 0}; int dijkstra(int src, int dst, const Graph &g) { int n = g.size(); vector<int> cost(n, INF); priority_queue<Edge> que; cost[src] = 0; que.push((Edge){src, 0}); while (que.size()) { const Edge s = que.top(); que.pop(); if (cost[s.to] < s.w) continue; if (s.to == dst) return s.w; for (int i = 0; i < g[s.to].size(); ++i) { const Edge &e = g[s.to][i]; const Edge t = {e.to, s.w + e.w}; if (cost[t.to] <= t.w) continue; cost[t.to] = t.w; que.push(t); } } return -1; } int main() { for (int W, H, N; cin >> W >> H >> N;) { Pair s, t; cin >> s.first >> s.second >> t.first >> t.second; int cnt[500][500][4] = {}; for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; int T; string S; cin >> T >> S; while (T--) { for (int j = 0; j < S.size(); ++j) { int k; if (S[j] == 'R') k = 0; if (S[j] == 'D') k = 1; if (S[j] == 'L') k = 2; if (S[j] == 'U') k = 3; int nx = x + dj[k]; int ny = y + di[k]; if (nx < 0 || nx >= W || ny < 0 || ny >= H) { continue; } if (ny == y) { int p = max(x, nx); cnt[y][p][1] += 1; cnt[y + 1][p][3] += 1; } else { int p = max(y, ny); cnt[p][x][0] += 1; cnt[p][x + 1][2] += 1; } x = nx; y = ny; } } } Graph G((H + 1) * (W + 1)); for (int i = 0; i <= H; ++i) { for (int j = 0; j <= W; ++j) { for (int k = 0; k < 4; ++k) { int ni = i + di[k]; int nj = j + dj[k]; if (ni < 0 || ni >= H + 1) continue; if (nj < 0 || nj >= W + 1) continue; int a = i * (W + 1) + j; int b = ni * (W + 1) + nj; G[a].push_back((Edge){b, cnt[i][j][k]}); } } } int src = s.second * (W + 1) + s.first; int dst = t.second * (W + 1) + t.first; cout << dijkstra(src, dst, G) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> Pair; struct Edge { int to, w; bool operator<(const Edge &e) const { return w > e.w; } }; typedef vector<vector<Edge>> Graph; const int INF = 1 << 28; const int di[] = {0, 1, 0, -1}; const int dj[] = {1, 0, -1, 0}; int dijkstra(int src, int dst, const Graph &g) { int n = g.size(); vector<int> cost(n, INF); priority_queue<Edge> que; cost[src] = 0; que.push((Edge){src, 0}); while (que.size()) { const Edge s = que.top(); que.pop(); if (cost[s.to] < s.w) continue; if (s.to == dst) return s.w; for (int i = 0; i < g[s.to].size(); ++i) { const Edge &e = g[s.to][i]; const Edge t = {e.to, s.w + e.w}; if (cost[t.to] <= t.w) continue; cost[t.to] = t.w; que.push(t); } } return -1; } int main() { for (int W, H, N; cin >> W >> H >> N;) { Pair s, t; cin >> s.first >> s.second >> t.first >> t.second; int cnt[501][501][4] = {}; for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; int T; string S; cin >> T >> S; while (T--) { for (int j = 0; j < S.size(); ++j) { int k; if (S[j] == 'R') k = 0; if (S[j] == 'D') k = 1; if (S[j] == 'L') k = 2; if (S[j] == 'U') k = 3; int nx = x + dj[k]; int ny = y + di[k]; if (nx < 0 || nx >= W || ny < 0 || ny >= H) { continue; } if (ny == y) { int p = max(x, nx); cnt[y][p][1] += 1; cnt[y + 1][p][3] += 1; } else { int p = max(y, ny); cnt[p][x][0] += 1; cnt[p][x + 1][2] += 1; } x = nx; y = ny; } } } Graph G((H + 1) * (W + 1)); for (int i = 0; i <= H; ++i) { for (int j = 0; j <= W; ++j) { for (int k = 0; k < 4; ++k) { int ni = i + di[k]; int nj = j + dj[k]; if (ni < 0 || ni >= H + 1) continue; if (nj < 0 || nj >= W + 1) continue; int a = i * (W + 1) + j; int b = ni * (W + 1) + nj; G[a].push_back((Edge){b, cnt[i][j][k]}); } } } int src = s.second * (W + 1) + s.first; int dst = t.second * (W + 1) + t.first; cout << dijkstra(src, dst, G) << endl; } return 0; }
replace
43
44
43
44
0
p01732
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <unordered_map> using namespace std; #define INF 99999999 typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef unordered_map<int, Edges> Graph; void shortestPath(Graph &g, int s, unordered_map<int, Weight> &dist, unordered_map<int, int> &prev) { for (auto &f : g) dist[f.first] = INF; dist[s] = 0; priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight" for (Q.push(Edge(-2, s, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (prev.find(e.dst) != prev.end()) continue; prev[e.dst] = e.src; for (auto &f : g[e.dst]) { if (dist[f.dst] > e.weight + f.weight) { dist[f.dst] = e.weight + f.weight; Q.push(Edge(f.src, f.dst, e.weight + f.weight)); } } } } char buf[101]; int W; int coor(int x, int y) { return (y) * (W * 2 + 2) + x; } int main() { int H, N, X1, Y1, X2, Y2; scanf("%d%d%d%d%d%d%d", &W, &H, &N, &X1, &Y1, &X2, &Y2); X1 = coor(X1 * 2, Y1 * 2), X2 = coor(X2 * 2, Y2 * 2); vector<vector<int>> m(H * 2 + 1); for (auto &&e : m) e.resize(W * 2 + 1); for (int i = 0; i < N; i++) { int x, y, t, l; scanf("%d%d%d%s", &x, &y, &t, buf); l = strlen(buf); x = 2 * x + 1, y = 2 * y + 1; for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { int c = buf[k]; if (c == 'L' && 1 < x) m[y][x - 1]++, x -= 2; if (c == 'U' && 1 < y) m[y - 1][x]++, y -= 2; if (c == 'R' && x < W * 2 - 1) m[y][x + 1]++, x += 2; if (c == 'D' && y < H * 2 - 1) m[y + 1][x]++, y += 2; } } } Graph g; for (int h = 0; h <= H * 2; h += 2) for (int w = 0; w <= W * 2; w += 2) { int a = coor(w, h), b; if (h < H * 2 - 1) b = coor(w, h + 2), g[a].push_back(Edge(a, b, m[h + 1][w])), g[b].push_back(Edge(b, a, m[h + 1][w])); if (w < W * 2 - 1) b = coor(w + 2, h), g[a].push_back(Edge(a, b, m[h][w + 1])), g[b].push_back(Edge(b, a, m[h][w + 1])); } unordered_map<int, Weight> dist; unordered_map<int, int> prev; shortestPath(g, X1, dist, prev); printf("%d\n", dist[X2]); }
#include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <unordered_map> using namespace std; #define INF 99999999 typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef unordered_map<int, Edges> Graph; void shortestPath(Graph &g, int s, unordered_map<int, Weight> &dist, unordered_map<int, int> &prev) { for (auto &f : g) dist[f.first] = INF; dist[s] = 0; priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight" for (Q.push(Edge(-2, s, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (prev.find(e.dst) != prev.end()) continue; prev[e.dst] = e.src; for (auto &f : g[e.dst]) { if (dist[f.dst] > e.weight + f.weight) { dist[f.dst] = e.weight + f.weight; Q.push(Edge(f.src, f.dst, e.weight + f.weight)); } } } } char buf[1001]; int W; int coor(int x, int y) { return (y) * (W * 2 + 2) + x; } int main() { int H, N, X1, Y1, X2, Y2; scanf("%d%d%d%d%d%d%d", &W, &H, &N, &X1, &Y1, &X2, &Y2); X1 = coor(X1 * 2, Y1 * 2), X2 = coor(X2 * 2, Y2 * 2); vector<vector<int>> m(H * 2 + 1); for (auto &&e : m) e.resize(W * 2 + 1); for (int i = 0; i < N; i++) { int x, y, t, l; scanf("%d%d%d%s", &x, &y, &t, buf); l = strlen(buf); x = 2 * x + 1, y = 2 * y + 1; for (int j = 0; j < t; j++) { for (int k = 0; k < l; k++) { int c = buf[k]; if (c == 'L' && 1 < x) m[y][x - 1]++, x -= 2; if (c == 'U' && 1 < y) m[y - 1][x]++, y -= 2; if (c == 'R' && x < W * 2 - 1) m[y][x + 1]++, x += 2; if (c == 'D' && y < H * 2 - 1) m[y + 1][x]++, y += 2; } } } Graph g; for (int h = 0; h <= H * 2; h += 2) for (int w = 0; w <= W * 2; w += 2) { int a = coor(w, h), b; if (h < H * 2 - 1) b = coor(w, h + 2), g[a].push_back(Edge(a, b, m[h + 1][w])), g[b].push_back(Edge(b, a, m[h + 1][w])); if (w < W * 2 - 1) b = coor(w + 2, h), g[a].push_back(Edge(a, b, m[h][w + 1])), g[b].push_back(Edge(b, a, m[h][w + 1])); } unordered_map<int, Weight> dist; unordered_map<int, int> prev; shortestPath(g, X1, dist, prev); printf("%d\n", dist[X2]); }
replace
43
44
43
44
0
p01732
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; typedef priority_queue<PP, vector<PP>, greater<PP>> QUE; int d[501][501][4]; int main() { int W, H, N; cin >> W >> H >> N; int x1, y1, x2, y2; cin >> y1 >> x1 >> y2 >> x2; map<char, int> dx, dy; dx['U'] = -1, dy['U'] = 0; dx['D'] = 1, dy['D'] = 0; dx['L'] = 0, dy['L'] = -1; dx['R'] = 0, dy['R'] = 1; for (int i = 0; i < N; i++) { int sx, sy; cin >> sy >> sx; int times; cin >> times; string code; cin >> code; for (int time = 0; time < times; time++) { for (int j = 0; j < code.size(); j++) { int nx = sx + dx[code[j]]; int ny = sy + dy[code[j]]; if (!(0 <= nx && nx < H && 0 <= ny && ny < W)) continue; if (code[j] == 'R') { d[nx][ny][1] += 1; d[nx + 1][ny][3] += 1; } else if (code[j] == 'L') { d[sx][sy][1] += 1; d[sx + 1][sy][3] += 1; } else if (code[j] == 'U') { d[sx][sy][0] += 1; d[sx][sy + 1][2] += 1; } else { d[nx][ny][0] += 1; d[nx][ny + 1][2] += 1; } sx = nx; sy = ny; } } } QUE que; que.push(PP(0, P(x1, y1))); int D[101][101]; int res = 1 << 28; for (int i = 0; i <= H; i++) for (int j = 0; j <= W; j++) D[i][j] = 1 << 28; while (que.size()) { PP pp = que.top(); que.pop(); int cost = pp.first; int x = pp.second.first; int y = pp.second.second; if (x == x2 && y == y2) res = min(res, cost); if (D[x][y] < cost) continue; int ddx[] = {0, 1, 0, -1}; int ddy[] = {1, 0, -1, 0}; for (int i = 0; i < 4; i++) { int nx = x + ddx[i]; int ny = y + ddy[i]; int cost_ = cost + d[x][y][i]; if (0 <= nx && nx <= H && 0 <= ny && ny <= W && D[nx][ny] > cost_) { D[nx][ny] = cost_; que.push(PP(cost_, P(nx, ny))); } } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; typedef priority_queue<PP, vector<PP>, greater<PP>> QUE; int d[501][501][4]; int main() { int W, H, N; cin >> W >> H >> N; int x1, y1, x2, y2; cin >> y1 >> x1 >> y2 >> x2; map<char, int> dx, dy; dx['U'] = -1, dy['U'] = 0; dx['D'] = 1, dy['D'] = 0; dx['L'] = 0, dy['L'] = -1; dx['R'] = 0, dy['R'] = 1; for (int i = 0; i < N; i++) { int sx, sy; cin >> sy >> sx; int times; cin >> times; string code; cin >> code; for (int time = 0; time < times; time++) { for (int j = 0; j < code.size(); j++) { int nx = sx + dx[code[j]]; int ny = sy + dy[code[j]]; if (!(0 <= nx && nx < H && 0 <= ny && ny < W)) continue; if (code[j] == 'R') { d[nx][ny][1] += 1; d[nx + 1][ny][3] += 1; } else if (code[j] == 'L') { d[sx][sy][1] += 1; d[sx + 1][sy][3] += 1; } else if (code[j] == 'U') { d[sx][sy][0] += 1; d[sx][sy + 1][2] += 1; } else { d[nx][ny][0] += 1; d[nx][ny + 1][2] += 1; } sx = nx; sy = ny; } } } QUE que; que.push(PP(0, P(x1, y1))); int D[501][501]; int res = 1 << 28; for (int i = 0; i <= H; i++) for (int j = 0; j <= W; j++) D[i][j] = 1 << 28; while (que.size()) { PP pp = que.top(); que.pop(); int cost = pp.first; int x = pp.second.first; int y = pp.second.second; if (x == x2 && y == y2) res = min(res, cost); if (D[x][y] < cost) continue; int ddx[] = {0, 1, 0, -1}; int ddy[] = {1, 0, -1, 0}; for (int i = 0; i < 4; i++) { int nx = x + ddx[i]; int ny = y + ddy[i]; int cost_ = cost + d[x][y][i]; if (0 <= nx && nx <= H && 0 <= ny && ny <= W && D[nx][ny] > cost_) { D[nx][ny] = cost_; que.push(PP(cost_, P(nx, ny))); } } } cout << res << endl; return 0; }
replace
56
57
56
57
0
p01733
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; map<int, int> pop[123456]; int x[123456], y[123456]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { int w; cin >> x[i] >> y[i] >> w; pop[y[i]][x[i]] = w; } int ans = 0; for (int i = 0; i < N; i++) { for (int j = -1; j <= 0; j++) { for (int k = -1; k <= 0; k++) { int c = 0; for (int l = 0; l < 2; l++) { for (int m = 0; m < 2; m++) { c += pop[y[i] + j + l][x[i] + k + m]; } } ans = max(ans, c); } } } cout << ans << " / 1" << endl; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; map<int, map<int, int>> pop; int x[123456], y[123456]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { int w; cin >> x[i] >> y[i] >> w; pop[y[i]][x[i]] = w; } int ans = 0; for (int i = 0; i < N; i++) { for (int j = -1; j <= 0; j++) { for (int k = -1; k <= 0; k++) { int c = 0; for (int l = 0; l < 2; l++) { for (int m = 0; m < 2; m++) { c += pop[y[i] + j + l][x[i] + k + m]; } } ans = max(ans, c); } } } cout << ans << " / 1" << endl; }
replace
7
8
7
8
0
p01733
C++
Runtime Error
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < n; ++i) #define RREP(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define DUMP(v) \ REP(aa, (v).size()) { \ cout << v[aa]; \ if (aa != v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; typedef long long 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; ll max(ll a, int b) { return max(a, ll(b)); } ll max(int a, ll b) { return max(ll(a), b); } ll min(ll a, int b) { return min(a, ll(b)); } ll min(int a, ll b) { return min(ll(a), b); } ///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)/// int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; map<pii, int> mp; vi x(n), y(n); REP(i, n) { int z; cin >> x[i] >> y[i] >> z; mp[{x[i], y[i]}] += z; } int ans = -1; REP(i, n) { REP(j, 4) { ans = max(ans, mp[{x[i], y[i]}] + mp[{x[i] + dx[i * 2], y[i] + dy[i * 2]}] + mp[{x[i] + dx[i * 2 + 1], y[i] + dy[i * 2 + 1]}] + mp[{x[i] + dx[(i + 1) * 2 % 8], y[i] + dy[(i + 1) * 2 % 8]}]); } } cout << ans << " / 1" << endl; }
#include "bits/stdc++.h" #define REP(i, n) for (ll i = 0; i < n; ++i) #define RREP(i, n) for (ll i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()); #define DUMP(v) \ REP(aa, (v).size()) { \ cout << v[aa]; \ if (aa != v.size() - 1) \ cout << " "; \ else \ cout << endl; \ } #define INF 1000000001ll #define MOD 1000000007ll #define EPS 1e-9 const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; typedef long long 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; ll max(ll a, int b) { return max(a, ll(b)); } ll max(int a, ll b) { return max(ll(a), b); } ll min(ll a, int b) { return min(a, ll(b)); } ll min(int a, ll b) { return min(ll(a), b); } ///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)/// int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; map<pii, int> mp; vi x(n), y(n); REP(i, n) { int z; cin >> x[i] >> y[i] >> z; mp[{x[i], y[i]}] += z; } int ans = -1; REP(i, n) { REP(j, 4) { ans = max(ans, mp[{x[i], y[i]}] + mp[{x[i] + dx[j * 2], y[i] + dy[j * 2]}] + mp[{x[i] + dx[j * 2 + 1], y[i] + dy[j * 2 + 1]}] + mp[{x[i] + dx[(j + 1) * 2 % 8], y[i] + dy[(j + 1) * 2 % 8]}]); } } cout << ans << " / 1" << endl; }
replace
54
57
54
57
0
p01733
C++
Memory Limit Exceeded
#include <algorithm> #include <assert.h> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back // #define mt make_tuple #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; // typedef tuple<int,int,int> tp; // typedef vector<tp> vt; typedef vector<vd> vvd; typedef pair<Def, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-7; const int inf = 1e9; const ll INF = 2e18; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } map<ll, int> ma; vvi in; int main() { int n; cin >> n; in = vvi(n); rep(i, n) { int a, b, c; scanf("%d %d %d", &a, &b, &c); in[i] = vi{a, b, c}; ma[a * 1000000001ll + b] = c; } ll a = 0, b = 1, sum, sx, sy, gx, gy, area, gc_d = 1; rep(q, n) { loop(x, -4, 5) loop(y, -4, 5) if (x != 0 && y != 0) { sum = 0; sx = min(in[q][0], in[q][0] + x); gx = max(in[q][0], in[q][0] + x); sy = min(in[q][1], in[q][1] + y); gy = max(in[q][1], in[q][1] + y); loop(i, sx, gx + 1) loop(j, sy, gy + 1) sum += ma[i * 1000000001ll + j]; area = abs(x * y); gc_d = gcd(sum, area); sum /= gc_d; area /= gc_d; if (a * area < b * sum) { a = sum; b = area; } } } cout << a << " / " << b << endl; }
#include <algorithm> #include <assert.h> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back // #define mt make_tuple #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef int Def; typedef pair<Def, Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; // typedef tuple<int,int,int> tp; // typedef vector<tp> vt; typedef vector<vd> vvd; typedef pair<Def, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-7; const int inf = 1e9; const ll INF = 2e18; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); } map<ll, int> ma; vvi in; int main() { int n; cin >> n; in = vvi(n); rep(i, n) { int a, b, c; scanf("%d %d %d", &a, &b, &c); in[i] = vi{a, b, c}; ma[a * 1000000001ll + b] = c; } ll a = 0, b = 1, sum, sx, sy, gx, gy, area, gc_d = 1; rep(q, n) { loop(x, -4, 5) loop(y, -4, 5) if (x != 0 && y != 0) { sum = 0; sx = min(in[q][0], in[q][0] + x); gx = max(in[q][0], in[q][0] + x); sy = min(in[q][1], in[q][1] + y); gy = max(in[q][1], in[q][1] + y); loop(i, sx, gx + 1) loop(j, sy, gy + 1) if (ma.count(i * 1000000001ll + j)) sum += ma[i * 1000000001ll + j]; area = abs(x * y); gc_d = gcd(sum, area); sum /= gc_d; area /= gc_d; if (a * area < b * sum) { a = sum; b = area; } } } cout << a << " / " << b << endl; }
replace
64
65
64
67
MLE
p01736
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "sz=" << vc.size() << endl << "["; for (T &v : vc) o << v << ","; cout << "]" << endl; return o; } // Z/pZ typedef int ll; struct mint { const static ll mod = 2; ll a; mint() : a(0) {} mint(ll a) : a((a % mod + mod) % mod) {} mint operator+=(const mint &b) { if ((a += b.a) >= mod) a -= mod; return *this; } mint operator-=(const mint &b) { if ((a += mod - b.a) >= mod) a -= mod; return *this; } mint operator*=(const mint &b) { (a *= b.a) %= mod; return *this; } mint operator+(const mint &b) { return mint(*this) += b; } mint operator-(const mint &b) { return mint(*this) -= b; } mint operator*(const mint &b) { return mint(*this) *= b; } mint operator-() { return mint(-a); } int extgcd(int a, int b, int &x, int &y) const { int u[] = {a, 1, 0}, v[] = {b, 0, 1}; while (*v) { int t = *u / *v; rep(i, 3) swap(u[i] -= t * v[i], v[i]); } if (u[0] < 0) rep(i, 3) u[i] = -u[i]; x = u[1], y = u[2]; return u[0]; } mint inv() const { int x, y; extgcd(a, mod, x, y); if (x < 0) x += mod; return mint(x); } mint operator/=(const mint &b) { (*this) *= b.inv(); return *this; } mint operator/(const mint &b) { return mint(*this) /= b; } bool operator==(const mint &b) const { return a == b.a; } friend ostream &operator<<(ostream &o, const mint &x) { return o << x.a; } }; const mint zero(0), one(1); bool iszero(mint x) { return x.a == 0; } template <class T> struct Matrix { int H, W; vector<vector<T>> m; Matrix() : H(0), W(0) {} Matrix(int H, int W) : H(H), W(W), m(vector<vector<T>>(H, vector<T>(W, zero))) {} //???0*W?????????????????¨?£????????????§??±????????????????????????????????§??????????????? // Matrix(vector< vector<T> > m):m(m),H(m.size()),W(m[0].size()){} static Matrix E(int N) { Matrix a(N, N); rep(i, N) a.m[i][i] = one; return a; } Matrix operator+=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] += b.m[i][j]; return *this; } Matrix operator-=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] -= b.m[i][j]; return *this; } Matrix operator+(const Matrix &b) { return Matrix(*this) += b; } Matrix operator-(const Matrix &b) { return Matrix(*this) -= b; } Matrix operator*(const Matrix &b) { assert(W == b.H); Matrix c(H, b.W); rep(i, H) rep(k, W) rep(j, b.W) c.m[i][j] += m[i][k] * b.m[k][j]; return c; } Matrix operator*=(const Matrix &b) { return (*this) = (*this) * b; } vector<T> operator*(const vector<T> &b) { assert(W == b.size()); vector<T> c(H); rep(i, H) rep(j, W) c[i] += m[i][j] * b[j]; return c; } int sweep(int var = -1) { // return rank ????????¨ if (var < 0) var = W; int r = 0; vector<bool> used(H); rep(j, var) { int i = 0; while (i < H && (used[i] || iszero(m[i][j]))) i++; if (i == H) continue; used[i] = true; r++; T t = m[i][j]; rep(k, W) m[i][k] = m[i][k] / t; rep(k, H) if (k != i) { t = m[k][j]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return r; } Matrix inv() { assert(H == W); Matrix a(H, 2 * H); rep(i, H) rep(j, H) a.m[i][j] = m[i][j]; rep(i, H) a.m[i][i + H] = one; int r = a.sweep(H); // show(r); if (r < H) return Matrix(); Matrix b(H, H); rep(i_, H) { int i = -1; rep(j, H) if (a.m[i_][j] == one) i = j; assert(i >= 0); rep(j, H) b.m[i][j] = a.m[i_][j + H]; } return b; } pair<int, vector<T>> sle(vector<T> b) { // return pair( ?§£????¬????(?????????????????????-1) ,x // s.t.Ax=b) assert(H == b.size()); Matrix a(H, W + 1); rep(i, H) rep(j, W) a.m[i][j] = m[i][j]; rep(i, H) a.m[i][W] = b[i]; a.sweep(W); int d = 0; rep(i, H) { bool all0 = 1; rep(j, W) if (!iszero(a.m[i][j])) all0 = 0; if (all0) { if (!iszero(a.m[i][W])) { return pair<int, vector<T>>(-1, vector<T>()); } d++; } } rep(i_, H) { int i = -1; rep(j, W) if (a.m[i_][j] == one) i = j; assert(i >= 0); b[i] = a.m[i_][W]; // show(b[i]); } return pair<int, vector<T>>(d, b); } friend ostream &operator<<(ostream &o, const Matrix &m) { o << m.H << " * " << m.W << endl; rep(i, m.H) { o << "["; rep(j, m.W) o << m.m[i][j] << " "; o << "]" << endl; } return o; } }; template <class T> Matrix<T> ex(Matrix<T> a, int t) { int N = a.H; Matrix<mint> x = Matrix<mint>::E(N); while (t) { if (t % 2) x *= a; a *= a; t /= 2; } return x; } int main() { int N, T; cin >> N; Matrix<mint> a(N, N); rep(i, N) rep(j, N) { int x; cin >> x; a.m[i][j] = mint(x); } vector<mint> b(N); rep(i, N) { int x; cin >> x; b[i] = mint(x); } cin >> T; a = ex(a, T); auto p = a.sle(b); // assert(a*p.sc==b); // show(p.fs); // vector<mint> c=a*p.sc; // assert(c==b); // rep(i,N) assert(c[i]==b[i]); if (p.fs == -1) { puts("none"); } else if (p.fs > 0) { puts("ambiguous"); } else { rep(i, N) cout << p.sc[i] << (i == N - 1 ? "\n" : " "); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "sz=" << vc.size() << endl << "["; for (T &v : vc) o << v << ","; cout << "]" << endl; return o; } // Z/pZ typedef int ll; struct mint { const static ll mod = 2; ll a; mint() : a(0) {} mint(ll a) : a((a % mod + mod) % mod) {} mint operator+=(const mint &b) { if ((a += b.a) >= mod) a -= mod; return *this; } mint operator-=(const mint &b) { if ((a += mod - b.a) >= mod) a -= mod; return *this; } mint operator*=(const mint &b) { (a *= b.a) %= mod; return *this; } mint operator+(const mint &b) { return mint(*this) += b; } mint operator-(const mint &b) { return mint(*this) -= b; } mint operator*(const mint &b) { return mint(*this) *= b; } mint operator-() { return mint(-a); } int extgcd(int a, int b, int &x, int &y) const { int u[] = {a, 1, 0}, v[] = {b, 0, 1}; while (*v) { int t = *u / *v; rep(i, 3) swap(u[i] -= t * v[i], v[i]); } if (u[0] < 0) rep(i, 3) u[i] = -u[i]; x = u[1], y = u[2]; return u[0]; } mint inv() const { int x, y; extgcd(a, mod, x, y); if (x < 0) x += mod; return mint(x); } mint operator/=(const mint &b) { (*this) *= b.inv(); return *this; } mint operator/(const mint &b) { return mint(*this) /= b; } bool operator==(const mint &b) const { return a == b.a; } friend ostream &operator<<(ostream &o, const mint &x) { return o << x.a; } }; const mint zero(0), one(1); bool iszero(mint x) { return x.a == 0; } template <class T> struct Matrix { int H, W; vector<vector<T>> m; Matrix() : H(0), W(0) {} Matrix(int H, int W) : H(H), W(W), m(vector<vector<T>>(H, vector<T>(W, zero))) {} //???0*W?????????????????¨?£????????????§??±????????????????????????????????§??????????????? // Matrix(vector< vector<T> > m):m(m),H(m.size()),W(m[0].size()){} static Matrix E(int N) { Matrix a(N, N); rep(i, N) a.m[i][i] = one; return a; } Matrix operator+=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] += b.m[i][j]; return *this; } Matrix operator-=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] -= b.m[i][j]; return *this; } Matrix operator+(const Matrix &b) { return Matrix(*this) += b; } Matrix operator-(const Matrix &b) { return Matrix(*this) -= b; } Matrix operator*(const Matrix &b) { assert(W == b.H); Matrix c(H, b.W); rep(i, H) rep(k, W) rep(j, b.W) c.m[i][j] += m[i][k] * b.m[k][j]; return c; } Matrix operator*=(const Matrix &b) { return (*this) = (*this) * b; } vector<T> operator*(const vector<T> &b) { assert(W == b.size()); vector<T> c(H); rep(i, H) rep(j, W) c[i] += m[i][j] * b[j]; return c; } int sweep(int var = -1) { // return rank ????????¨ if (var < 0) var = W; int r = 0; vector<bool> used(H); rep(j, var) { int i = 0; while (i < H && (used[i] || iszero(m[i][j]))) i++; if (i == H) continue; used[i] = true; r++; T t = m[i][j]; rep(k, W) m[i][k] = m[i][k] / t; rep(k, H) if (k != i) { t = m[k][j]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return r; } Matrix inv() { assert(H == W); Matrix a(H, 2 * H); rep(i, H) rep(j, H) a.m[i][j] = m[i][j]; rep(i, H) a.m[i][i + H] = one; int r = a.sweep(H); // show(r); if (r < H) return Matrix(); Matrix b(H, H); rep(i_, H) { int i = -1; rep(j, H) if (a.m[i_][j] == one) i = j; assert(i >= 0); rep(j, H) b.m[i][j] = a.m[i_][j + H]; } return b; } pair<int, vector<T>> sle(vector<T> b) { // return pair( ?§£????¬????(?????????????????????-1) ,x // s.t.Ax=b) assert(H == b.size()); Matrix a(H, W + 1); rep(i, H) rep(j, W) a.m[i][j] = m[i][j]; rep(i, H) a.m[i][W] = b[i]; a.sweep(W); int d = 0; rep(i, H) { bool all0 = 1; rep(j, W) if (!iszero(a.m[i][j])) all0 = 0; if (all0) { if (!iszero(a.m[i][W])) { return pair<int, vector<T>>(-1, vector<T>()); } d++; } } rep(i_, H) { int i = -1; rep(j, W) if (a.m[i_][j] == one) i = j; if (i < 0) continue; b[i] = a.m[i_][W]; // show(b[i]); } return pair<int, vector<T>>(d, b); } friend ostream &operator<<(ostream &o, const Matrix &m) { o << m.H << " * " << m.W << endl; rep(i, m.H) { o << "["; rep(j, m.W) o << m.m[i][j] << " "; o << "]" << endl; } return o; } }; template <class T> Matrix<T> ex(Matrix<T> a, int t) { int N = a.H; Matrix<mint> x = Matrix<mint>::E(N); while (t) { if (t % 2) x *= a; a *= a; t /= 2; } return x; } int main() { int N, T; cin >> N; Matrix<mint> a(N, N); rep(i, N) rep(j, N) { int x; cin >> x; a.m[i][j] = mint(x); } vector<mint> b(N); rep(i, N) { int x; cin >> x; b[i] = mint(x); } cin >> T; a = ex(a, T); auto p = a.sle(b); // assert(a*p.sc==b); // show(p.fs); // vector<mint> c=a*p.sc; // assert(c==b); // rep(i,N) assert(c[i]==b[i]); if (p.fs == -1) { puts("none"); } else if (p.fs > 0) { puts("ambiguous"); } else { rep(i, N) cout << p.sc[i] << (i == N - 1 ? "\n" : " "); } }
replace
172
173
172
174
0
p01736
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "sz=" << vc.size() << endl << "["; for (T &v : vc) o << v << ","; cout << "]" << endl; return o; } // Z/pZ typedef int ll; struct mint { const static ll mod = 2; ll a; mint() : a(0) {} mint(ll a) : a((a % mod + mod) % mod) {} mint operator+=(const mint &b) { if ((a += b.a) >= mod) a -= mod; return *this; } mint operator-=(const mint &b) { if ((a += mod - b.a) >= mod) a -= mod; return *this; } mint operator*=(const mint &b) { (a *= b.a) %= mod; return *this; } mint operator+(const mint &b) { return mint(*this) += b; } mint operator-(const mint &b) { return mint(*this) -= b; } mint operator*(const mint &b) { return mint(*this) *= b; } mint operator-() { return mint(-a); } int extgcd(int a, int b, int &x, int &y) const { int u[] = {a, 1, 0}, v[] = {b, 0, 1}; while (*v) { int t = *u / *v; rep(i, 3) swap(u[i] -= t * v[i], v[i]); } if (u[0] < 0) rep(i, 3) u[i] = -u[i]; x = u[1], y = u[2]; return u[0]; } mint inv() const { int x, y; extgcd(a, mod, x, y); if (x < 0) x += mod; return mint(x); } mint operator/=(const mint &b) { (*this) *= b.inv(); return *this; } mint operator/(const mint &b) { return mint(*this) /= b; } bool operator==(const mint &b) const { return a == b.a; } friend ostream &operator<<(ostream &o, const mint &x) { return o << x.a; } }; const mint zero(0), one(1); bool iszero(mint x) { return x.a == 0; } template <class T> struct Matrix { int H, W; vector<vector<T>> m; Matrix() : H(0), W(0) {} Matrix(int H, int W) : H(H), W(W), m(vector<vector<T>>(H, vector<T>(W, zero))) {} //???0*W?????????????????¨?£????????????§??±????????????????????????????????§??????????????? // Matrix(vector< vector<T> > m):m(m),H(m.size()),W(m[0].size()){} vector<T> operator[](const int &i) const { return m[i]; } vector<T> &operator[](const int &i) { return m[i]; } Matrix operator+=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] += b[i][j]; return *this; } Matrix operator-=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] -= b[i][j]; return *this; } Matrix operator+(const Matrix &b) { return Matrix(*this) += b; } Matrix operator-(const Matrix &b) { return Matrix(*this) -= b; } Matrix operator*(const Matrix &b) { assert(W == b.H); Matrix c(H, b.W); rep(i, H) rep(k, W) rep(j, b.W) c[i][j] += m[i][k] * b[k][j]; return c; } Matrix operator*=(const Matrix &b) { return (*this) = (*this) * b; } vector<T> operator*(const vector<T> &b) { assert(W == b.size()); vector<T> c(H); rep(i, H) rep(j, W) c[i] += m[i][j] * b[j]; return c; } static Matrix E(int N) { Matrix a(N, N); rep(i, N) a[i][i] = one; return a; } /* Matrix ex(int t) { puts("here"); // assert(H==W); Matrix x=Matrix::E(H); while(t){ show(t); if(t%2) x*=(*this); (*this)=(*this)*(*this); t/=2; } return x; }*/ int sweep(int var = -1) { // return rank ????????¨ swap!!! if (var < 0) var = W; int r = 0; rep(i, var) { if (iszero(m[i][i])) { for (int j = i + 1; j < H; j++) if (!iszero(m[j][i])) { for (int k = i; k < W; k++) swap(m[i][k], m[j][k]); break; } if (iszero(m[i][i])) continue; } T t = m[i][i]; rep(k, W) m[i][k] /= t; rep(k, H) if (k != i) { t = m[k][i]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return r; } T det() { //????????¨??????! T d = one; assert(H == W); rep(i, H) { if (iszero(m[i][i])) { for (int j = i + 1; j < H; j++) if (!iszero(m[j][i])) { for (int k = i; k < W; k++) swap(m[i][k], m[j][k]); d = -d; break; } if (iszero(m[i][i])) return zero; } T t = m[i][i]; d *= t; rep(k, W) m[i][k] /= t; rep(k, H) if (k != i) { t = m[k][i]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return d; } Matrix inv() { assert(H == W); Matrix a(H, 2 * H); rep(i, H) rep(j, H) a[i][j] = m[i][j]; rep(i, H) a[i][i + H] = one; int r = a.sweep(H); if (r < H) return Matrix(); Matrix b(H, H); rep(i, H) { rep(j, H) b[i][j] = a[i][j + H]; } return b; } pair<int, vector<T>> sle(vector<T> b) { // return pair( ?§£????¬????(?????????????????????-1) ,x // s.t.Ax=b) assert(H == b.size()); Matrix a(H, W + 1); rep(i, H) rep(j, W) a[i][j] = m[i][j]; rep(i, H) a[i][W] = b[i]; a.sweep(W); int d = 0; rep(i, H) { bool all0 = 1; rep(j, W) if (!iszero(a[i][j])) all0 = 0; if (all0) { if (!iszero(a[i][W])) { return pair<int, vector<T>>(-1, vector<T>()); } d++; } } rep(i, H) b[i] = a[i][W]; return pair<int, vector<T>>(d, b); } friend ostream &operator<<(ostream &o, const Matrix &m) { o << m.H << " * " << m.W << endl; rep(i, m.H) { o << "["; rep(j, m.W) o << m[i][j] << " "; o << "]" << endl; } return o; } }; template <class T> Matrix<T> ex(Matrix<T> a, int t) { int N = a.H; Matrix<mint> x = Matrix<mint>::E(N); while (t) { // show(t); if (t % 2) x *= a; a = a * a; t /= 2; } return x; } int main() { int N, T; cin >> N; Matrix<mint> a(N, N); rep(i, N) rep(j, N) { int x; cin >> x; a[i][j] = mint(x); } vector<mint> b(N); rep(i, N) { int x; cin >> x; b[i] = mint(x); } cin >> T; a = ex(a, T); auto p = a.sle(b); if (p.fs == -1) { puts("none"); } else if (p.fs > 0) { puts("ambiguous"); } else { rep(i, N) cout << p.sc[i] << (i == N - 1 ? "\n" : " "); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "sz=" << vc.size() << endl << "["; for (T &v : vc) o << v << ","; cout << "]" << endl; return o; } // Z/pZ typedef int ll; struct mint { const static ll mod = 2; ll a; mint() : a(0) {} mint(ll a) : a((a % mod + mod) % mod) {} mint operator+=(const mint &b) { if ((a += b.a) >= mod) a -= mod; return *this; } mint operator-=(const mint &b) { if ((a += mod - b.a) >= mod) a -= mod; return *this; } mint operator*=(const mint &b) { (a *= b.a) %= mod; return *this; } mint operator+(const mint &b) { return mint(*this) += b; } mint operator-(const mint &b) { return mint(*this) -= b; } mint operator*(const mint &b) { return mint(*this) *= b; } mint operator-() { return mint(-a); } int extgcd(int a, int b, int &x, int &y) const { int u[] = {a, 1, 0}, v[] = {b, 0, 1}; while (*v) { int t = *u / *v; rep(i, 3) swap(u[i] -= t * v[i], v[i]); } if (u[0] < 0) rep(i, 3) u[i] = -u[i]; x = u[1], y = u[2]; return u[0]; } mint inv() const { int x, y; extgcd(a, mod, x, y); if (x < 0) x += mod; return mint(x); } mint operator/=(const mint &b) { (*this) *= b.inv(); return *this; } mint operator/(const mint &b) { return mint(*this) /= b; } bool operator==(const mint &b) const { return a == b.a; } friend ostream &operator<<(ostream &o, const mint &x) { return o << x.a; } }; const mint zero(0), one(1); bool iszero(mint x) { return x.a == 0; } template <class T> struct Matrix { int H, W; vector<vector<T>> m; Matrix() : H(0), W(0) {} Matrix(int H, int W) : H(H), W(W), m(vector<vector<T>>(H, vector<T>(W, zero))) {} //???0*W?????????????????¨?£????????????§??±????????????????????????????????§??????????????? // Matrix(vector< vector<T> > m):m(m),H(m.size()),W(m[0].size()){} vector<T> const &operator[](const int &i) const { return m[i]; } vector<T> &operator[](const int &i) { return m[i]; } Matrix operator+=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] += b[i][j]; return *this; } Matrix operator-=(const Matrix &b) { assert(H == b.H && W == b.W); rep(i, H) rep(j, H) m[i][j] -= b[i][j]; return *this; } Matrix operator+(const Matrix &b) { return Matrix(*this) += b; } Matrix operator-(const Matrix &b) { return Matrix(*this) -= b; } Matrix operator*(const Matrix &b) { assert(W == b.H); Matrix c(H, b.W); rep(i, H) rep(k, W) rep(j, b.W) c[i][j] += m[i][k] * b[k][j]; return c; } Matrix operator*=(const Matrix &b) { return (*this) = (*this) * b; } vector<T> operator*(const vector<T> &b) { assert(W == b.size()); vector<T> c(H); rep(i, H) rep(j, W) c[i] += m[i][j] * b[j]; return c; } static Matrix E(int N) { Matrix a(N, N); rep(i, N) a[i][i] = one; return a; } /* Matrix ex(int t) { puts("here"); // assert(H==W); Matrix x=Matrix::E(H); while(t){ show(t); if(t%2) x*=(*this); (*this)=(*this)*(*this); t/=2; } return x; }*/ int sweep(int var = -1) { // return rank ????????¨ swap!!! if (var < 0) var = W; int r = 0; rep(i, var) { if (iszero(m[i][i])) { for (int j = i + 1; j < H; j++) if (!iszero(m[j][i])) { for (int k = i; k < W; k++) swap(m[i][k], m[j][k]); break; } if (iszero(m[i][i])) continue; } T t = m[i][i]; rep(k, W) m[i][k] /= t; rep(k, H) if (k != i) { t = m[k][i]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return r; } T det() { //????????¨??????! T d = one; assert(H == W); rep(i, H) { if (iszero(m[i][i])) { for (int j = i + 1; j < H; j++) if (!iszero(m[j][i])) { for (int k = i; k < W; k++) swap(m[i][k], m[j][k]); d = -d; break; } if (iszero(m[i][i])) return zero; } T t = m[i][i]; d *= t; rep(k, W) m[i][k] /= t; rep(k, H) if (k != i) { t = m[k][i]; rep(l, W) m[k][l] = m[k][l] - m[i][l] * t; } // show((*this)); } return d; } Matrix inv() { assert(H == W); Matrix a(H, 2 * H); rep(i, H) rep(j, H) a[i][j] = m[i][j]; rep(i, H) a[i][i + H] = one; int r = a.sweep(H); if (r < H) return Matrix(); Matrix b(H, H); rep(i, H) { rep(j, H) b[i][j] = a[i][j + H]; } return b; } pair<int, vector<T>> sle(vector<T> b) { // return pair( ?§£????¬????(?????????????????????-1) ,x // s.t.Ax=b) assert(H == b.size()); Matrix a(H, W + 1); rep(i, H) rep(j, W) a[i][j] = m[i][j]; rep(i, H) a[i][W] = b[i]; a.sweep(W); int d = 0; rep(i, H) { bool all0 = 1; rep(j, W) if (!iszero(a[i][j])) all0 = 0; if (all0) { if (!iszero(a[i][W])) { return pair<int, vector<T>>(-1, vector<T>()); } d++; } } rep(i, H) b[i] = a[i][W]; return pair<int, vector<T>>(d, b); } friend ostream &operator<<(ostream &o, const Matrix &m) { o << m.H << " * " << m.W << endl; rep(i, m.H) { o << "["; rep(j, m.W) o << m[i][j] << " "; o << "]" << endl; } return o; } }; template <class T> Matrix<T> ex(Matrix<T> a, int t) { int N = a.H; Matrix<mint> x = Matrix<mint>::E(N); while (t) { // show(t); if (t % 2) x *= a; a = a * a; t /= 2; } return x; } int main() { int N, T; cin >> N; Matrix<mint> a(N, N); rep(i, N) rep(j, N) { int x; cin >> x; a[i][j] = mint(x); } vector<mint> b(N); rep(i, N) { int x; cin >> x; b[i] = mint(x); } cin >> T; a = ex(a, T); auto p = a.sle(b); if (p.fs == -1) { puts("none"); } else if (p.fs > 0) { puts("ambiguous"); } else { rep(i, N) cout << p.sc[i] << (i == N - 1 ? "\n" : " "); } }
replace
79
80
79
80
TLE
p01739
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }; template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }; template <typename T> struct PrimalDual { struct edge { Int to; T cap, cost; Int rev; edge() {} edge(Int to, T cap, T cost, Int rev) : to(to), cap(cap), cost(cost), rev(rev) {} }; T INF; vector<vector<edge>> G; vector<T> h, dist; vector<Int> prevv, preve; PrimalDual() {} PrimalDual(Int n, T INF) : INF(INF), G(n), h(n), dist(n), prevv(n), preve(n) {} void add_edge(Int from, Int to, T cap, T cost) { G[from].emplace_back(to, cap, cost, G[to].size()); G[to].emplace_back(from, 0, -cost, G[from].size() - 1); } T flow(Int s, Int t, T f, Int &ok) { T res = 0; fill(h.begin(), h.end(), 0); while (f > 0) { using P = pair<T, Int>; priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), INF); dist[s] = 0; que.emplace(dist[s], s); while (!que.empty()) { P p = que.top(); que.pop(); Int v = p.second; if (dist[v] < p.first) continue; for (Int i = 0; i < (Int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap == 0) continue; if (dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.emplace(dist[e.to], e.to); } } } if (dist[t] == INF) return ok = 0; for (Int v = 0; v < (Int)h.size(); v++) h[v] += dist[v]; T d = f; for (Int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap); f -= d; res += d * h[t]; for (Int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } ok = 1; return res; } }; // INSERT ABOVE HERE signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; cin >> n; const Int INF2 = 1e9; const Int INF3 = 1e15; PrimalDual<Int> G(n + 1, INF3); Int num = 0, ans = 0; for (Int i = 0; i < n; i++) { Int k; cin >> k; num += k; map<Int, Int> cnt, dst; for (Int j = 0; j < k; j++) { Int t, c; cin >> t >> c; t--; cnt[t]++; ans += c + INF2; if (!dst.count(t)) dst[t] = c; chmin(dst[t], c); } for (auto p : cnt) { G.add_edge(i, p.first, cnt[p.first], -INF2); G.add_edge(i, p.first, INF2, dst[p.first]); } G.add_edge(i, n, INF2, 0); } Int ok = 1; ans += G.flow(0, n, INF2, ok); cout << ans << endl; assert(ok); return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }; template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }; template <typename T> struct PrimalDual { struct edge { Int to; T cap, cost; Int rev; edge() {} edge(Int to, T cap, T cost, Int rev) : to(to), cap(cap), cost(cost), rev(rev) {} }; T INF; vector<vector<edge>> G; vector<T> h, dist; vector<Int> prevv, preve; PrimalDual() {} PrimalDual(Int n, T INF) : INF(INF), G(n), h(n), dist(n), prevv(n), preve(n) {} void add_edge(Int from, Int to, T cap, T cost) { G[from].emplace_back(to, cap, cost, G[to].size()); G[to].emplace_back(from, 0, -cost, G[from].size() - 1); } T flow(Int s, Int t, T f, Int &ok) { T res = 0; fill(h.begin(), h.end(), 0); while (f > 0) { struct P { T first; Int second; P(T first, Int second) : first(first), second(second) {} bool operator<(const P &a) const { return first > a.first; } }; priority_queue<P> que; fill(dist.begin(), dist.end(), INF); dist[s] = 0; que.emplace(dist[s], s); while (!que.empty()) { P p = que.top(); que.pop(); Int v = p.second; if (dist[v] < p.first) continue; for (Int i = 0; i < (Int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap == 0) continue; if (dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.emplace(dist[e.to], e.to); } } } if (dist[t] == INF) return ok = 0; for (Int v = 0; v < (Int)h.size(); v++) h[v] += dist[v]; T d = f; for (Int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap); f -= d; res += d * h[t]; for (Int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } ok = 1; return res; } }; // INSERT ABOVE HERE signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; cin >> n; const Int INF2 = 1e9; const Int INF3 = 1e15; PrimalDual<Int> G(n + 1, INF3); Int num = 0, ans = 0; for (Int i = 0; i < n; i++) { Int k; cin >> k; num += k; map<Int, Int> cnt, dst; for (Int j = 0; j < k; j++) { Int t, c; cin >> t >> c; t--; cnt[t]++; ans += c + INF2; if (!dst.count(t)) dst[t] = c; chmin(dst[t], c); } for (auto p : cnt) { G.add_edge(i, p.first, cnt[p.first], -INF2); G.add_edge(i, p.first, INF2, dst[p.first]); } G.add_edge(i, n, INF2, 0); } Int ok = 1; ans += G.flow(0, n, INF2, ok); cout << ans << endl; assert(ok); return 0; }
replace
41
43
41
50
TLE
p01743
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define show(x) #endif typedef long long int ll; typedef pair<int, int> pii; template <typename T> using vec = std::vector<T>; const int inf = 1 << 30; const long long int infll = 1LL << 62; const double eps = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { int n; cin >> n; vector<ll> a; ll ans = 0; int sum = 0; rep(i, 0, n) { ll in; cin >> in; if (in > 4) { ans += in - 4; in = 4; } sum += in; a.push_back(in); } sort(a.rbegin(), a.rend()); int tmp = min(sum - (n - 1), 11); int mini = tmp; vector<vector<int>> vs(tmp + 1); rep(i, 0, 1 << tmp) vs[__builtin_popcount(i)].push_back(i); ; function<void(int, vector<int>, int)> dfs = [&](int idx, vector<int> v, int x) { if (idx == n) { mini = __builtin_popcount(x); return; } for (int i : vs[a[idx]]) { int x_ = x | i; if (__builtin_popcount(x_) >= mini) continue; bool ok = true; rep(j, 0, idx) { if (__builtin_popcount(i & v[j]) < 2) continue; ok = false; break; } if (!ok) continue; vector<int> w = v; w.push_back(i); dfs(idx + 1, w, x_); } }; dfs(0, vector<int>(), 0); ans += mini; cout << ans << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define show(x) #endif typedef long long int ll; typedef pair<int, int> pii; template <typename T> using vec = std::vector<T>; const int inf = 1 << 30; const long long int infll = 1LL << 62; const double eps = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { int n; cin >> n; vector<ll> a; ll ans = 0; int sum = 0; rep(i, 0, n) { ll in; cin >> in; if (in > 4) { ans += in - 4; in = 4; } sum += in; a.push_back(in); } sort(a.rbegin(), a.rend()); int tmp = min(sum - (n - 1), 10); int mini = tmp; vector<vector<int>> vs(tmp + 1); rep(i, 0, 1 << tmp) vs[__builtin_popcount(i)].push_back(i); ; function<void(int, vector<int>, int)> dfs = [&](int idx, vector<int> v, int x) { if (idx == n) { mini = __builtin_popcount(x); return; } for (int i : vs[a[idx]]) { int x_ = x | i; if (__builtin_popcount(x_) >= mini) continue; bool ok = true; rep(j, 0, idx) { if (__builtin_popcount(i & v[j]) < 2) continue; ok = false; break; } if (!ok) continue; vector<int> w = v; w.push_back(i); dfs(idx + 1, w, x_); } }; dfs(0, vector<int>(), 0); ans += mini; cout << ans << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
replace
49
50
49
50
TLE
p01744
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int leftAns[SIZE], rightAns[SIZE]; void init(int w) { int idL = 0; int idR = 0; if (w % 2 == 0) { for (int i = 1; i < w; i += 2) { leftAns[idL++] = i; } for (int i = w - 2; i >= 0; i -= 2) { leftAns[idL++] = i; } for (int i = w - 2; i >= 0; i -= 2) { rightAns[idR++] = i; } for (int i = 1; i < w; i += 2) { rightAns[idR++] = i; } } else { for (int i = 1; i < w; i += 2) { leftAns[idL++] = i; } for (int i = w - 1; i >= 0; i -= 2) { leftAns[idL++] = i; } for (int i = w - 1; i >= 0; i -= 2) { rightAns[idR++] = i; } for (int i = 1; i < w; i += 2) { rightAns[idR++] = i; } } } int getID(int h, int w, int y, int x) { if ((y + x) % 2 == 0) { // left y -= x; y = (y / 2) + (w - 1); return leftAns[y % w]; } else { // right y -= (w - x - 1); if (w % 2 == 0) y = (y / 2) + (w - 1); else y = (y / 2); return rightAns[y % w]; } } int main() { int s[SIZE], ans[SIZE]; int h, w, n; pair<int, int> query[SIZE]; scanf("%d%d%d", &h, &w, &n); init(w); for (int i = 0; i < w; i++) s[i] = i; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; query[i] = {a, b}; } sort(query, query + n); for (int i = 0; i < n; i++) { swap(s[getID(h, w, query[i].first, query[i].second)], s[getID(h, w, query[i].first, query[i].second + 1)]); } for (int i = 0; i < w; i++) { debug(s[i]); ans[s[getID(h, w, h, i)]] = i + 1; } for (int i = 0; i < w; i++) { printf("%d\n", ans[i]); } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 200010 int leftAns[SIZE], rightAns[SIZE]; void init(int w) { int idL = 0; int idR = 0; if (w % 2 == 0) { for (int i = 1; i < w; i += 2) { leftAns[idL++] = i; } for (int i = w - 2; i >= 0; i -= 2) { leftAns[idL++] = i; } for (int i = w - 2; i >= 0; i -= 2) { rightAns[idR++] = i; } for (int i = 1; i < w; i += 2) { rightAns[idR++] = i; } } else { for (int i = 1; i < w; i += 2) { leftAns[idL++] = i; } for (int i = w - 1; i >= 0; i -= 2) { leftAns[idL++] = i; } for (int i = w - 1; i >= 0; i -= 2) { rightAns[idR++] = i; } for (int i = 1; i < w; i += 2) { rightAns[idR++] = i; } } } int getID(int h, int w, int y, int x) { if ((y + x) % 2 == 0) { // left y -= x; y = (y / 2) + (w - 1); return leftAns[y % w]; } else { // right y -= (w - x - 1); if (w % 2 == 0) y = (y / 2) + (w - 1); else y = (y / 2); return rightAns[y % w]; } } int main() { int s[SIZE], ans[SIZE]; int h, w, n; pair<int, int> query[SIZE]; scanf("%d%d%d", &h, &w, &n); init(w); for (int i = 0; i < w; i++) s[i] = i; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; query[i] = {a, b}; } sort(query, query + n); for (int i = 0; i < n; i++) { swap(s[getID(h, w, query[i].first, query[i].second)], s[getID(h, w, query[i].first, query[i].second + 1)]); } for (int i = 0; i < w; i++) { ans[s[getID(h, w, h, i)]] = i + 1; } for (int i = 0; i < w; i++) { printf("%d\n", ans[i]); } return 0; }
delete
119
120
119
119
0
s[i] = 2 s[i] = 1 s[i] = 0 s[i] = 3
p01749
C++
Memory Limit Exceeded
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; constexpr int TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } /// x^n template <class T> T pow(T x, ll n) { T r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <uint MD> struct ModInt { uint v; ModInt() : v(0) {} ModInt(ll v) : v(normS(v % MD + MD)) {} uint value() const { return v; } static uint normS(const uint &x) { return (x < MD) ? x : x - MD; }; static ModInt make(const uint &x) { ModInt m; m.v = x; return m; } const ModInt operator+(const ModInt &r) const { return make(normS(v + r.v)); } const ModInt operator-(const ModInt &r) const { return make(normS(v + normS(MD - r.v))); } const ModInt operator*(const ModInt &r) const { return make((ull)v * r.v % MD); } ModInt &operator+=(const ModInt &r) { return *this = *this + r; } ModInt &operator-=(const ModInt &r) { return *this = *this - r; } ModInt &operator*=(const ModInt &r) { return *this = *this * r; } static ModInt inv(const ModInt &x) { return pow(ModInt(x), MD - 2); } }; struct Union { int B; vector<int> g; Union(int B) : B(B) { g.resize(B); iota(g.begin(), g.end(), 0); } void compress() { int co = 0; map<int, int> mp; for (int i = 0; i < B; i++) { if (!mp.count(g[i])) { mp[g[i]] = co++; } g[i] = mp[g[i]]; } } void merge(int a, int b) { int c = g[b]; for (int i = 0; i < B; i++) { if (g[i] == c) g[i] = g[a]; } } int gc() { int ma = 0; for (int i = 0; i < B; i++) { ma = max(ma, g[i] + 1); } return ma; } const bool operator<(const Union &r) const { for (int i = 0; i < B; i++) { if (g[i] != r.g[i]) return g[i] < r.g[i]; } return false; } const bool operator==(const Union &r) const { for (int i = 0; i < B; i++) { if (g[i] != r.g[i]) return false; } return true; } void print() { for (int i = 0; i < B; i++) { if (g[i] < 26) { printf("%c", g[i] + 'a'); } else { printf("%c", g[i] + 'A' - 26); } } printf("\n"); } }; const int MD = 1e9 + 7; using Mint = ModInt<MD>; const int MA = 1100; const int MB = 55; const int MS = 2300; int A, B, C; vector<Mint> co; vector<array<int, MB>> nex; bool used[MA][MS][MB][2]; Mint dp[MA][MS][MB][2]; Mint solve(int P, int S, int K, int F) { K = min(K, B); if (P == 0) { assert(K == B); if (F) return 0; return co[S]; } if (used[P][S][K][F]) return dp[P][S][K][F]; used[P][S][K][F] = true; Mint ans = solve(P - 1, S, K + 1, F); if (K == B) ans *= C; if (P >= B) ans += solve(P - 1, nex[S][K], 1, 1 - F); // printf("solve %d %d %d %d %d\n", P, S, K, F, ans.value()); return dp[P][S][K][F] = ans; } int main() { cin >> A >> B >> C; Mint powC[MB]; powC[0] = 1; for (int i = 1; i < MB; i++) { powC[i] = powC[i - 1] * C; } vector<Union> v; v.push_back(Union(B)); for (int d = 1; d < B; d++) { vector<Union> v2(v.begin(), v.end()); for (Union u : v) { for (int i = 0; i < B - d; i++) { u.merge(i, i + d); } u.compress(); v2.push_back(u); } sort(v2.begin(), v2.end()); v2.erase(unique(v2.begin(), v2.end()), v2.end()); v = v2; } int m = (int)v.size(); map<Union, int> u2id; for (int i = 0; i < m; i++) { u2id[v[i]] = i; } co.resize(m); nex.resize(m); for (int i = 0; i < m; i++) { co[i] = powC[v[i].gc()]; for (int d = 1; d <= B; d++) { Union u = v[i]; for (int j = 0; j < B - d; j++) { u.merge(j, j + d); } u.compress(); nex[i][d] = u2id[u]; } } /* for (Union u: v) { u.print(); } for (int i = 0; i < m; i++) { printf("%d co(%d) : ", i, co[i].value()); for (int j = 0; j <= B; j++) { printf("%d ", nex[i][j]); } printf("\n"); } cout << "size:" << v.size() << endl;*/ cout << (solve(A, m - 1, B, 1) - solve(A, m - 1, B, 0) + pow(Mint(C), B) * pow(Mint(C), A)) .value() << endl; return 0; }
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; constexpr int TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } /// x^n template <class T> T pow(T x, ll n) { T r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <uint MD> struct ModInt { uint v; ModInt() : v(0) {} ModInt(ll v) : v(normS(v % MD + MD)) {} uint value() const { return v; } static uint normS(const uint &x) { return (x < MD) ? x : x - MD; }; static ModInt make(const uint &x) { ModInt m; m.v = x; return m; } const ModInt operator+(const ModInt &r) const { return make(normS(v + r.v)); } const ModInt operator-(const ModInt &r) const { return make(normS(v + normS(MD - r.v))); } const ModInt operator*(const ModInt &r) const { return make((ull)v * r.v % MD); } ModInt &operator+=(const ModInt &r) { return *this = *this + r; } ModInt &operator-=(const ModInt &r) { return *this = *this - r; } ModInt &operator*=(const ModInt &r) { return *this = *this * r; } static ModInt inv(const ModInt &x) { return pow(ModInt(x), MD - 2); } }; struct Union { int B; vector<int> g; Union(int B) : B(B) { g.resize(B); iota(g.begin(), g.end(), 0); } void compress() { int co = 0; map<int, int> mp; for (int i = 0; i < B; i++) { if (!mp.count(g[i])) { mp[g[i]] = co++; } g[i] = mp[g[i]]; } } void merge(int a, int b) { int c = g[b]; for (int i = 0; i < B; i++) { if (g[i] == c) g[i] = g[a]; } } int gc() { int ma = 0; for (int i = 0; i < B; i++) { ma = max(ma, g[i] + 1); } return ma; } const bool operator<(const Union &r) const { for (int i = 0; i < B; i++) { if (g[i] != r.g[i]) return g[i] < r.g[i]; } return false; } const bool operator==(const Union &r) const { for (int i = 0; i < B; i++) { if (g[i] != r.g[i]) return false; } return true; } void print() { for (int i = 0; i < B; i++) { if (g[i] < 26) { printf("%c", g[i] + 'a'); } else { printf("%c", g[i] + 'A' - 26); } } printf("\n"); } }; const int MD = 1e9 + 7; using Mint = ModInt<MD>; const int MA = 210; const int MB = 55; const int MS = 2300; int A, B, C; vector<Mint> co; vector<array<int, MB>> nex; bool used[MA][MS][MB][2]; Mint dp[MA][MS][MB][2]; Mint solve(int P, int S, int K, int F) { K = min(K, B); if (P == 0) { assert(K == B); if (F) return 0; return co[S]; } if (used[P][S][K][F]) return dp[P][S][K][F]; used[P][S][K][F] = true; Mint ans = solve(P - 1, S, K + 1, F); if (K == B) ans *= C; if (P >= B) ans += solve(P - 1, nex[S][K], 1, 1 - F); // printf("solve %d %d %d %d %d\n", P, S, K, F, ans.value()); return dp[P][S][K][F] = ans; } int main() { cin >> A >> B >> C; Mint powC[MB]; powC[0] = 1; for (int i = 1; i < MB; i++) { powC[i] = powC[i - 1] * C; } vector<Union> v; v.push_back(Union(B)); for (int d = 1; d < B; d++) { vector<Union> v2(v.begin(), v.end()); for (Union u : v) { for (int i = 0; i < B - d; i++) { u.merge(i, i + d); } u.compress(); v2.push_back(u); } sort(v2.begin(), v2.end()); v2.erase(unique(v2.begin(), v2.end()), v2.end()); v = v2; } int m = (int)v.size(); map<Union, int> u2id; for (int i = 0; i < m; i++) { u2id[v[i]] = i; } co.resize(m); nex.resize(m); for (int i = 0; i < m; i++) { co[i] = powC[v[i].gc()]; for (int d = 1; d <= B; d++) { Union u = v[i]; for (int j = 0; j < B - d; j++) { u.merge(j, j + d); } u.compress(); nex[i][d] = u2id[u]; } } /* for (Union u: v) { u.print(); } for (int i = 0; i < m; i++) { printf("%d co(%d) : ", i, co[i].value()); for (int j = 0; j <= B; j++) { printf("%d ", nex[i][j]); } printf("\n"); } cout << "size:" << v.size() << endl;*/ cout << (solve(A, m - 1, B, 1) - solve(A, m - 1, B, 0) + pow(Mint(C), B) * pow(Mint(C), A)) .value() << endl; return 0; }
replace
117
118
117
118
MLE
p01750
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcount #define INF 1e16 #define mod 1000000007 ll mod_pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll n; ll a[310]; ll dp[301][90001][2]; ll dfs(ll i, ll s, bool f) { if (dp[i][s][f] != -1) return dp[i][s][f]; if (i == n) { if (!f) return dp[n][s][f] = mod_pow(s, n); else return dp[n][s][f] = (mod - mod_pow(s, n)) % mod; } ll res = 0; if (s >= a[i]) { (res += dfs(i + 1, s - a[i], !f)) %= mod; } (res += dfs(i + 1, s, f)) %= mod; return dp[i][s][f] = res; } int main() { cin >> n; rep(i, n) cin >> a[i]; ll S; cin >> S; memset(dp, -1, sizeof(dp)); cout << dfs(0, S, false) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) #define rep(i, n) repl(i, 0, n) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define mmax(x, y) (x > y ? x : y) #define mmin(x, y) (x < y ? x : y) #define maxch(x, y) x = mmax(x, y) #define minch(x, y) x = mmin(x, y) #define uni(x) x.erase(unique(all(x)), x.end()) #define exist(x, y) (find(all(x), y) != x.end()) #define bcnt __builtin_popcount #define INF 1e16 #define mod 1000000007 ll mod_pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll n; ll a[310]; int dp[301][90001][2]; ll dfs(ll i, ll s, bool f) { if (dp[i][s][f] != -1) return dp[i][s][f]; if (i == n) { if (!f) return dp[n][s][f] = mod_pow(s, n); else return dp[n][s][f] = (mod - mod_pow(s, n)) % mod; } ll res = 0; if (s >= a[i]) { (res += dfs(i + 1, s - a[i], !f)) %= mod; } (res += dfs(i + 1, s, f)) %= mod; return dp[i][s][f] = res; } int main() { cin >> n; rep(i, n) cin >> a[i]; ll S; cin >> S; memset(dp, -1, sizeof(dp)); cout << dfs(0, S, false) << endl; return 0; }
replace
37
38
37
38
MLE
p01750
C++
Runtime Error
#include <bits/stdc++.h> #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) #define _all(arg) begin(arg), end(arg) #define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg)) #define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary) #define clr(a, b) memset((a), (b), sizeof(a)) #define bit(n) (1LL << (n)) #define popcount(n) (__builtin_popcountll(n)) template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } using namespace std; using ll = long long; const ll mod = 1000000007LL; int l[310]; ll dp[310]; int main(void) { int d, s; cin >> d; rep(i, d) cin >> l[i]; cin >> s; dp[s] = 1LL; rep(i, d) rep(j, s + 1) { if (j - l[i] >= 0) dp[j - l[i]] += mod - dp[j]; dp[j] %= mod; } ll cut = 0LL; rep(i, s + 1) { ll tmp = dp[i]; rep(j, d) tmp = (tmp * i) % mod; cut = (cut + tmp) % mod; } cout << cut << endl; return 0; }
#include <bits/stdc++.h> #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) #define _all(arg) begin(arg), end(arg) #define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg)) #define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary) #define clr(a, b) memset((a), (b), sizeof(a)) #define bit(n) (1LL << (n)) #define popcount(n) (__builtin_popcountll(n)) template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } using namespace std; using ll = long long; const ll mod = 1000000007LL; int l[90010]; ll dp[90010]; int main(void) { int d, s; cin >> d; rep(i, d) cin >> l[i]; cin >> s; dp[s] = 1LL; rep(i, d) rep(j, s + 1) { if (j - l[i] >= 0) dp[j - l[i]] += mod - dp[j]; dp[j] %= mod; } ll cut = 0LL; rep(i, s + 1) { ll tmp = dp[i]; rep(j, d) tmp = (tmp * i) % mod; cut = (cut + tmp) % mod; } cout << cut << endl; return 0; }
replace
30
32
30
32
0
p01750
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define MOD 1000000007 int dp[2][303][90909]; int d, s; int l[303]; int mod_pow(int x, int n, int m) { int res = 1; while (n) { if (n & 1) (res *= x) %= m; x = (x * x) % m; n >>= 1; } return res; } int dfs(int f, int x, int y) { if (~dp[f][x][y]) return dp[f][x][y]; int res = 0; if (x == d) { res = mod_pow(y, d, MOD); if (f) res = (MOD - res) % MOD; return dp[f][x][y] = res; } (res += dfs(f, x + 1, y)) %= MOD; if (l[x] < y) (res += dfs(!f, x + 1, y - l[x])) %= MOD; return dp[f][x][y] = res; } signed main() { cin >> d; for (int i = 0; i < d; i++) cin >> l[i]; cin >> s; memset(dp, -1, sizeof(dp)); cout << dfs(0, 0, s) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define MOD 1000000007 signed dp[2][303][90909]; int d, s; int l[303]; int mod_pow(int x, int n, int m) { int res = 1; while (n) { if (n & 1) (res *= x) %= m; x = (x * x) % m; n >>= 1; } return res; } int dfs(int f, int x, int y) { if (~dp[f][x][y]) return dp[f][x][y]; int res = 0; if (x == d) { res = mod_pow(y, d, MOD); if (f) res = (MOD - res) % MOD; return dp[f][x][y] = res; } (res += dfs(f, x + 1, y)) %= MOD; if (l[x] < y) (res += dfs(!f, x + 1, y - l[x])) %= MOD; return dp[f][x][y] = res; } signed main() { cin >> d; for (int i = 0; i < d; i++) cin >> l[i]; cin >> s; memset(dp, -1, sizeof(dp)); cout << dfs(0, 0, s) << endl; return 0; }
replace
4
5
4
5
MLE
p01750
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define pb push_back #define all(x) (x).begin(), (x).end() #define fi first #define se second #define mt make_tuple #define mp make_pair template <class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; using vi = vector<int>; using vvi = vector<vi>; vll conv(vi &v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; } using P = pair<ll, ll>; template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; } template <size_t...> struct seq {}; template <size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, size_t... Is> void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using s = int[]; (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...}; } template <class Ch, class Tr, class... Args> auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t) -> basic_ostream<Ch, Tr> & { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; } ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; } void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; } #define ldout fixed << setprecision(40) static const double EPS = 1e-14; static const long long INF = 1e18; static const long long mo = 1e9 + 7; class Mod { public: int num; int mod; Mod() : Mod(0) {} Mod(long long int n) : Mod(n, 1000000007) {} Mod(long long int n, int m) { mod = m; num = (n % mod + mod) % mod; } Mod(const string &s) { long long int tmp = 0; for (auto &c : s) tmp = (c - '0' + tmp * 10) % mod; num = tmp; } Mod(int n) : Mod(static_cast<long long int>(n)) {} operator int() { return num; } void setmod(const int mod) { this->mod = mod; } }; istream &operator>>(istream &is, Mod &x) { long long int n; is >> n; x = n; return is; } ostream &operator<<(ostream &o, const Mod &x) { o << x.num; return o; } Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % a.mod); } Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; } Mod operator+(const Mod a, const long long int b) { return b + a; } Mod operator++(Mod &a) { return a + Mod(1); } Mod operator-(const Mod a, const Mod b) { return Mod((a.mod + a.num - b.num) % a.mod); } Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; } Mod operator--(Mod &a) { return a - Mod(1); } Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % a.mod); } Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; } Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; } Mod operator*(const Mod a, const int b) { return Mod(b) * a; } Mod operator+=(Mod &a, const Mod b) { return a = a + b; } Mod operator+=(long long int &a, const Mod b) { return a = a + b; } Mod operator-=(Mod &a, const Mod b) { return a = a - b; } Mod operator-=(long long int &a, const Mod b) { return a = a - b; } Mod operator*=(Mod &a, const Mod b) { return a = a * b; } Mod operator*=(long long int &a, const Mod b) { return a = a * b; } Mod operator*=(Mod &a, const long long int &b) { return a = a * b; } Mod factorial(const long long n) { if (n < 0) return 0; Mod ret = 1; for (int i = 1; i <= n; i++) { ret *= i; } return ret; } Mod operator^(const Mod a, const long long n) { if (n == 0) return Mod(1); Mod res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } Mod modpowsum(const Mod a, const long long b) { if (b == 0) return 0; if (b % 2 == 1) return modpowsum(a, b - 1) * a + Mod(1); Mod result = modpowsum(a, b / 2); return result * (a ^ (b / 2)) + result; } /*************************************/ // ??\??????mod????´???°??§??????????????????????????? /*************************************/ Mod inv(const Mod a) { return a ^ (a.mod - 2); } Mod operator/(const Mod a, const Mod b) { assert(b.num != 0); return a * inv(b); } Mod operator/(const long long int a, const Mod b) { assert(b.num != 0); return Mod(a) * inv(b); } Mod operator/=(Mod &a, const Mod b) { assert(b.num != 0); return a = a * inv(b); } // n!??¨1/n!??????????????????????????? // nCr?????????????¨???????????????????????????? // // assert??§n????¶?????????????????????????????????¨????????????????????¨??? // // O(n log mo) vector<Mod> fact, rfact; void constructFactorial(const long long n) { fact.resize(n); rfact.resize(n); fact[0] = rfact[0] = 1; for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i; rfact[i] = Mod(1) / fact[i]; } } // O(1) Mod nCr(const long long n, const long long r) { // assert(n < (long long)fact.size()); if (n < 0 || r < 0) return 0; return fact[n] * rfact[r] * rfact[n - r]; } // O(k log mo) Mod nCrWithoutConstruction(const long long n, const long long k) { if (n < 0) return 0; if (k < 0) return 0; Mod ret = 1; for (int i = 0; i < k; i++) { ret *= n - (Mod)i; ret /= Mod(i + 1); } return ret; } // n*m????????¢????????????????????????????????´????????° // O(1) Mod nBm(const long long n, const long long m) { if (n < 0 || m < 0) return 0; return nCr(n + m, n); } ll d, r; vll l; static ll dp[301][100000][2] = {}; Mod f(ll i, ll s, bool b) { if (dp[i][s][b] >= 0) return dp[i][s][b]; if (s < 0) return 0; if (i == d) return dp[i][s][b] = (Mod(s) ^ d) * (b ? -1 : 1); return dp[i][s][b] = f(i + 1, s, b) + f(i + 1, s - l[i], !b); } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> d; l.resize(d); rep(i, l.size()) cin >> l[i]; cin >> r; rep(i, 301) rep(j, 100000) rep(h, 2) dp[i][j][h] = -1; cout << f(0, r, 0) << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define pb push_back #define all(x) (x).begin(), (x).end() #define fi first #define se second #define mt make_tuple #define mp make_pair template <class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; using vi = vector<int>; using vvi = vector<vi>; vll conv(vi &v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; } using P = pair<ll, ll>; template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; } template <size_t...> struct seq {}; template <size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {}; template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...> {}; template <class Ch, class Tr, class Tuple, size_t... Is> void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) { using s = int[]; (void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...}; } template <class Ch, class Tr, class... Args> auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t) -> basic_ostream<Ch, Tr> & { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; } ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size() - 1 ? ", " : ""); o << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; } template <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; } void printbits(ll mask, ll n) { rep(i, n) { cout << !!(mask & (1ll << i)); } cout << endl; } #define ldout fixed << setprecision(40) static const double EPS = 1e-14; static const long long INF = 1e18; static const long long mo = 1e9 + 7; class Mod { public: int num; int mod; Mod() : Mod(0) {} Mod(long long int n) : Mod(n, 1000000007) {} Mod(long long int n, int m) { mod = m; num = (n % mod + mod) % mod; } Mod(const string &s) { long long int tmp = 0; for (auto &c : s) tmp = (c - '0' + tmp * 10) % mod; num = tmp; } Mod(int n) : Mod(static_cast<long long int>(n)) {} operator int() { return num; } void setmod(const int mod) { this->mod = mod; } }; istream &operator>>(istream &is, Mod &x) { long long int n; is >> n; x = n; return is; } ostream &operator<<(ostream &o, const Mod &x) { o << x.num; return o; } Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % a.mod); } Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; } Mod operator+(const Mod a, const long long int b) { return b + a; } Mod operator++(Mod &a) { return a + Mod(1); } Mod operator-(const Mod a, const Mod b) { return Mod((a.mod + a.num - b.num) % a.mod); } Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; } Mod operator--(Mod &a) { return a - Mod(1); } Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % a.mod); } Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; } Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; } Mod operator*(const Mod a, const int b) { return Mod(b) * a; } Mod operator+=(Mod &a, const Mod b) { return a = a + b; } Mod operator+=(long long int &a, const Mod b) { return a = a + b; } Mod operator-=(Mod &a, const Mod b) { return a = a - b; } Mod operator-=(long long int &a, const Mod b) { return a = a - b; } Mod operator*=(Mod &a, const Mod b) { return a = a * b; } Mod operator*=(long long int &a, const Mod b) { return a = a * b; } Mod operator*=(Mod &a, const long long int &b) { return a = a * b; } Mod factorial(const long long n) { if (n < 0) return 0; Mod ret = 1; for (int i = 1; i <= n; i++) { ret *= i; } return ret; } Mod operator^(const Mod a, const long long n) { if (n == 0) return Mod(1); Mod res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } Mod modpowsum(const Mod a, const long long b) { if (b == 0) return 0; if (b % 2 == 1) return modpowsum(a, b - 1) * a + Mod(1); Mod result = modpowsum(a, b / 2); return result * (a ^ (b / 2)) + result; } /*************************************/ // ??\??????mod????´???°??§??????????????????????????? /*************************************/ Mod inv(const Mod a) { return a ^ (a.mod - 2); } Mod operator/(const Mod a, const Mod b) { assert(b.num != 0); return a * inv(b); } Mod operator/(const long long int a, const Mod b) { assert(b.num != 0); return Mod(a) * inv(b); } Mod operator/=(Mod &a, const Mod b) { assert(b.num != 0); return a = a * inv(b); } // n!??¨1/n!??????????????????????????? // nCr?????????????¨???????????????????????????? // // assert??§n????¶?????????????????????????????????¨????????????????????¨??? // // O(n log mo) vector<Mod> fact, rfact; void constructFactorial(const long long n) { fact.resize(n); rfact.resize(n); fact[0] = rfact[0] = 1; for (int i = 1; i < n; i++) { fact[i] = fact[i - 1] * i; rfact[i] = Mod(1) / fact[i]; } } // O(1) Mod nCr(const long long n, const long long r) { // assert(n < (long long)fact.size()); if (n < 0 || r < 0) return 0; return fact[n] * rfact[r] * rfact[n - r]; } // O(k log mo) Mod nCrWithoutConstruction(const long long n, const long long k) { if (n < 0) return 0; if (k < 0) return 0; Mod ret = 1; for (int i = 0; i < k; i++) { ret *= n - (Mod)i; ret /= Mod(i + 1); } return ret; } // n*m????????¢????????????????????????????????´????????° // O(1) Mod nBm(const long long n, const long long m) { if (n < 0 || m < 0) return 0; return nCr(n + m, n); } ll d, r; vll l; static int dp[301][100000][2] = {}; Mod f(ll i, ll s, bool b) { if (dp[i][s][b] >= 0) return dp[i][s][b]; if (s < 0) return 0; if (i == d) return dp[i][s][b] = (Mod(s) ^ d) * (b ? -1 : 1); return dp[i][s][b] = f(i + 1, s, b) + f(i + 1, s - l[i], !b); } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> d; l.resize(d); rep(i, l.size()) cin >> l[i]; cin >> r; rep(i, 301) rep(j, 100000) rep(h, 2) dp[i][j][h] = -1; cout << f(0, r, 0) << endl; }
replace
241
242
241
242
MLE
p01750
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <thread> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned long long int ull; typedef long long int ll; typedef pair<ll, ll> pll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<double, ll> pdl; #define F first #define S second const ll E = 1e18 + 7; const ll MOD = 1000000007; ll mod_pow(ll a, ll x) { ll ret = 1; while (x > 0) { if (x & 1) { (ret *= a) %= MOD; } (a *= a) %= MOD; x >>= 1; } return ret; } int main() { ll d; cin >> d; vector<ll> a(d); for (auto &I : a) { cin >> I; } ll s; cin >> s; vector<ll> dp(s, 0); dp[0] = 1; for (auto &I : a) { for (ll t = s - 1; t >= 0; t--) { if (t + I < s) { dp[t + I] -= dp[t]; dp[t + I] %= MOD; } } } ll ans = 0; for (int i = 0; i < s; i++) { ans += dp[i] * mod_pow(s - i, d) % MOD; ans %= MOD; } if (ans < 0) { ans += MOD; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <thread> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; typedef unsigned long long int ull; typedef long long int ll; typedef pair<ll, ll> pll; typedef pair<int, int> pi; typedef pair<double, double> pd; typedef pair<double, ll> pdl; #define F first #define S second const ll E = 1e18 + 7; const ll MOD = 1000000007; ll mod_pow(ll a, ll x) { ll ret = 1; while (x > 0) { if (x & 1) { (ret *= a) %= MOD; } (a *= a) %= MOD; x >>= 1; } return ret; } int main() { ll d; cin >> d; vector<ll> a(d); for (auto &I : a) { cin >> I; } ll s; cin >> s; if (s == 0) { cout << 0 << endl; return 0; } vector<ll> dp(s + 10, 0); dp[0] = 1; for (auto &I : a) { for (ll t = s - 1; t >= 0; t--) { if (t + I < s) { dp[t + I] -= dp[t]; dp[t + I] %= MOD; } } } ll ans = 0; for (int i = 0; i < s; i++) { ans += dp[i] * mod_pow(s - i, d) % MOD; ans %= MOD; } if (ans < 0) { ans += MOD; } cout << ans << endl; return 0; }
replace
58
59
58
63
0
p01750
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end()) #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(kbrni, v.size()) cout << " " << v[kbrni]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(kbrni, s) cout << " " << kbrni; \ cout << endl #define smap(m) \ cout << #m << ":"; \ each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \ cout << endl using namespace std; typedef pair<int, int> P; ll dp[301][90001][2]; ll mod_pow(ll a, ll b) { a %= MOD; ll res = 1; while (b) { if (b & 1) { res = res * a % MOD; } a = a * a % MOD; b >>= 1; } return res; } ll add(ll x, ll y) { return (x + y) % MOD; } ll sub(ll x, ll y) { return (x + MOD - y) % MOD; } ll mult(ll x, ll y) { return x * y % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); int d; cin >> d; vector<int> l(d); rep(i, d) { cin >> l[i]; } int s; cin >> s; dp[0][s][0] = 1; rep(i, d) { rep(j, s + 1) { if (j >= l[i]) { dp[i + 1][j - l[i]][1] = add(dp[i + 1][j - l[i]][1], dp[i][j][0]); dp[i + 1][j - l[i]][0] = add(dp[i + 1][j - l[i]][0], dp[i][j][1]); } dp[i + 1][j][0] = add(dp[i + 1][j][0], dp[i][j][0]); dp[i + 1][j][1] = add(dp[i + 1][j][1], dp[i][j][1]); } } ll ans = 0; for (int i = 1; i <= s; i++) { ans = add(ans, mult(mod_pow(i, d), sub(dp[d][i][0], dp[d][i][1]))); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end()) #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(kbrni, v.size()) cout << " " << v[kbrni]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(kbrni, s) cout << " " << kbrni; \ cout << endl #define smap(m) \ cout << #m << ":"; \ each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \ cout << endl using namespace std; typedef pair<int, int> P; int dp[301][90001][2]; ll mod_pow(ll a, ll b) { a %= MOD; ll res = 1; while (b) { if (b & 1) { res = res * a % MOD; } a = a * a % MOD; b >>= 1; } return res; } ll add(ll x, ll y) { return (x + y) % MOD; } ll sub(ll x, ll y) { return (x + MOD - y) % MOD; } ll mult(ll x, ll y) { return x * y % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); int d; cin >> d; vector<int> l(d); rep(i, d) { cin >> l[i]; } int s; cin >> s; dp[0][s][0] = 1; rep(i, d) { rep(j, s + 1) { if (j >= l[i]) { dp[i + 1][j - l[i]][1] = add(dp[i + 1][j - l[i]][1], dp[i][j][0]); dp[i + 1][j - l[i]][0] = add(dp[i + 1][j - l[i]][0], dp[i][j][1]); } dp[i + 1][j][0] = add(dp[i + 1][j][0], dp[i][j][0]); dp[i + 1][j][1] = add(dp[i + 1][j][1], dp[i][j][1]); } } ll ans = 0; for (int i = 1; i <= s; i++) { ans = add(ans, mult(mod_pow(i, d), sub(dp[d][i][0], dp[d][i][1]))); } cout << ans << endl; return 0; }
replace
31
32
31
32
MLE
p01751
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int n, m, b, d; long long c; bool a = false; cin >> n >> m >> b; c = b; for (int i = 0; i < 250000000; i++) { if (c % (n + m) <= n && a == false) { a = true; d = i; } c += 60; } if (a == false) { cout << "-1" << endl; } else { c = b + d * 60; cout << c << endl; } return 0; }
#include <iostream> using namespace std; int main() { int n, m, b, d; long long c; bool a = false; cin >> n >> m >> b; c = b; for (int i = 0; i < 150000000; i++) { if (c % (n + m) <= n && a == false) { a = true; d = i; } c += 60; } if (a == false) { cout << "-1" << endl; } else { c = b + d * 60; cout << c << endl; } return 0; }
replace
8
9
8
9
TLE
p01751
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; while (c < 1000000) { if (0 <= c % (a + b) && c % (a + b) <= a) { cout << c << endl; return 0; } } cout << -1 << endl; return 0; }
#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; while (c < 1000000) { if (0 <= c % (a + b) && c % (a + b) <= a) { cout << c << endl; return 0; } c += 60; } cout << -1 << endl; return 0; }
insert
14
14
14
16
TLE
p01751
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int main(void) { int a, b, c; cin >> a >> b >> c; for (int i = 0;; i += a + b) { if (i <= c && c <= i + a) { cout << i + c % 60 - i % 60 << endl; break; } if (60 < i && 60 % (a + b) == 0) { cout << -1 << endl; break; } if (i / 60 != (i + a + b) / 60) c += 60; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { int a, b, c; cin >> a >> b >> c; for (int i = 0;; i += a + b) { if (i <= c && c <= i + a) { cout << i + c % 60 - i % 60 << endl; break; } // if(60<i && 60%(a+b)==0){ if (100000 < i) { cout << -1 << endl; break; } if (i / 60 != (i + a + b) / 60) c += 60; } return 0; }
replace
14
15
14
16
TLE
p01751
C++
Time Limit Exceeded
// Header {{{ // includes {{{ #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <unistd.h> #include <vector> // }}} using namespace std; // consts {{{ static const int INF = 1e9; static const double PI = acos(-1.0); static const double EPS = 1e-10; // }}} // typedefs {{{ typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<ULL> VULL; typedef vector<VULL> VVULL; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef vector<VS> VVS; typedef pair<int, int> PII; typedef complex<int> P; #define PQ(type) priority_queue<type> // priority queue reverse #define PQR(type) priority_queue<type, vector<type>, greater<type>> // }}} // macros & inline functions {{{ // syntax sugars {{{ #define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i) #define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i) #define REP(i, n) FOR(i, 0, n) #define REPI(i, n) FORI(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define ASSIGN_MAX(var, val) ((var) = max((var), (val))) // }}} // conversion {{{ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // }}} // array and STL {{{ #define ARRSIZE(a) (sizeof(a) / sizeof(a[0])) #define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a))) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SIZE(a) ((LL)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(c, it) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define REACH(c, it) \ for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(s, e) ((s).find(e) != (s).end()) // }}} // bit manipulation {{{ // singed integers are not for bitwise operations, specifically arithmetic // shifts ('>>', and maybe not good for '<<' too) #define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed) #define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n))) #define BITOF(n, m) \ (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1)) inline int BITS_COUNT(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int MSB(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c - 1; } inline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); } // }}} // for readable code {{{ #define EVEN(n) (n % 2 == 0) #define ODD(n) (!EVEN(n)) // }}} // debug {{{ #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define dprt(fmt, ...) \ if (opt_debug) { \ fprintf(stderr, fmt, ##__VA_ARGS__); \ } #define darr(a) \ if (opt_debug) { \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr_range(a, f, t) \ if (opt_debug) { \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define dvec(v) \ if (opt_debug) { \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr2(a) \ if (opt_debug) { \ FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \ } #define WAIT() \ if (opt_debug) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \ } // dump vector elements in [s, e) #define dumpv(v, s, e) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #v << " = "; \ FOR(__i, s, e) { cerr << v[__i] << "\t"; } \ cerr << endl; \ } #define dumpl(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \ } #define dumpf() \ if (opt_debug) { \ cerr << __PRETTY_FUNCTION__ << endl; \ } #define where() \ if (opt_debug) { \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; \ } #define show_bits(b, s) \ if (opt_debug) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } // ostreams {{{ // complex template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) { return s << "(" << d.real() << ", " << d.imag() << ")"; } // pair template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) { int len = d.size(); REP(i, len) { s << d[i]; if (i < len - 1) s << "\t"; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &d) { int len = d.size(); REP(i, len) { s << d[i] << endl; } return s; } // map template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } // }}} // }}} // }}} // time {{{ inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } // }}} // string manipulation {{{ inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } inline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; } // }}} // geometry {{{ #define Y real() #define X imag() // }}} // 2 dimentional array {{{ P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)}; P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)}; bool in_field(int H, int W, P p) { return (0 <= p.Y && p.Y < H) && (0 <= p.X && p.X < W); } // }}} // input and output {{{ inline void input(string filename) { freopen(filename.c_str(), "r", stdin); } inline void output(string filename) { freopen(filename.c_str(), "w", stdout); } // }}} // }}} // Header under development {{{ int LCM(int a, int b) { // FIXME return a * b; } // Fraction class {{{ // ref: http://martin-thoma.com/fractions-in-cpp/ class Fraction { public: ULL numerator; ULL denominator; Fraction(ULL _numerator, ULL _denominator) { assert(_denominator > 0); numerator = _numerator; denominator = _denominator; }; Fraction operator*(const ULL rhs) { return Fraction(this->numerator * rhs, this->denominator); }; Fraction operator*(const Fraction &rhs) { return Fraction(this->numerator * rhs.numerator, this->denominator * rhs.denominator); } Fraction operator+(const Fraction &rhs) { ULL lcm = LCM(this->denominator, rhs.denominator); ULL numer_lhs = this->numerator * (this->denominator / lcm); ULL numer_rhs = rhs.numerator * (rhs.numerator / lcm); return Fraction(numer_lhs + numer_rhs, lcm); } Fraction &operator+=(const Fraction &rhs) { Fraction result = (*this) + rhs; this->numerator = result.numerator; this->denominator = result.denominator; return *this; } }; std::ostream &operator<<(std::ostream &s, const Fraction &a) { if (a.denominator == 1) { s << a.numerator; } else { s << a.numerator << "/" << a.denominator; } return s; } // }}} // }}} bool opt_debug = false; int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); // set options {{{ int __c; while ((__c = getopt(argc, argv, "d")) != -1) { switch (__c) { case 'd': opt_debug = true; break; default: abort(); } } // }}} // input("./inputs/0.txt"); // output("./outputs/0.txt"); int a, b, c; cin >> a >> b >> c; if (60 % (a + b) == 0 && a < (c % (a + b)) && (c % (a + b)) < a + b) { cout << -1 << endl; return 0; } int ans = c; while (a < c) { if (c > a + b) { c %= (a + b); } else { c = (c + 60) % (a + b); ans += 60; } } cout << ans << endl; return 0; } // vim: foldmethod=marker
// Header {{{ // includes {{{ #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <unistd.h> #include <vector> // }}} using namespace std; // consts {{{ static const int INF = 1e9; static const double PI = acos(-1.0); static const double EPS = 1e-10; // }}} // typedefs {{{ typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<ULL> VULL; typedef vector<VULL> VVULL; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef vector<VS> VVS; typedef pair<int, int> PII; typedef complex<int> P; #define PQ(type) priority_queue<type> // priority queue reverse #define PQR(type) priority_queue<type, vector<type>, greater<type>> // }}} // macros & inline functions {{{ // syntax sugars {{{ #define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i) #define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i) #define REP(i, n) FOR(i, 0, n) #define REPI(i, n) FORI(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define ASSIGN_MAX(var, val) ((var) = max((var), (val))) // }}} // conversion {{{ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // }}} // array and STL {{{ #define ARRSIZE(a) (sizeof(a) / sizeof(a[0])) #define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a))) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SIZE(a) ((LL)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(c, it) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define REACH(c, it) \ for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(s, e) ((s).find(e) != (s).end()) // }}} // bit manipulation {{{ // singed integers are not for bitwise operations, specifically arithmetic // shifts ('>>', and maybe not good for '<<' too) #define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed) #define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n))) #define BITOF(n, m) \ (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1)) inline int BITS_COUNT(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int MSB(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c - 1; } inline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); } // }}} // for readable code {{{ #define EVEN(n) (n % 2 == 0) #define ODD(n) (!EVEN(n)) // }}} // debug {{{ #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define dprt(fmt, ...) \ if (opt_debug) { \ fprintf(stderr, fmt, ##__VA_ARGS__); \ } #define darr(a) \ if (opt_debug) { \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr_range(a, f, t) \ if (opt_debug) { \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define dvec(v) \ if (opt_debug) { \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr2(a) \ if (opt_debug) { \ FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \ } #define WAIT() \ if (opt_debug) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \ } // dump vector elements in [s, e) #define dumpv(v, s, e) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #v << " = "; \ FOR(__i, s, e) { cerr << v[__i] << "\t"; } \ cerr << endl; \ } #define dumpl(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \ } #define dumpf() \ if (opt_debug) { \ cerr << __PRETTY_FUNCTION__ << endl; \ } #define where() \ if (opt_debug) { \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; \ } #define show_bits(b, s) \ if (opt_debug) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } // ostreams {{{ // complex template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) { return s << "(" << d.real() << ", " << d.imag() << ")"; } // pair template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) { int len = d.size(); REP(i, len) { s << d[i]; if (i < len - 1) s << "\t"; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &d) { int len = d.size(); REP(i, len) { s << d[i] << endl; } return s; } // map template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } // }}} // }}} // }}} // time {{{ inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } // }}} // string manipulation {{{ inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } inline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; } // }}} // geometry {{{ #define Y real() #define X imag() // }}} // 2 dimentional array {{{ P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)}; P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)}; bool in_field(int H, int W, P p) { return (0 <= p.Y && p.Y < H) && (0 <= p.X && p.X < W); } // }}} // input and output {{{ inline void input(string filename) { freopen(filename.c_str(), "r", stdin); } inline void output(string filename) { freopen(filename.c_str(), "w", stdout); } // }}} // }}} // Header under development {{{ int LCM(int a, int b) { // FIXME return a * b; } // Fraction class {{{ // ref: http://martin-thoma.com/fractions-in-cpp/ class Fraction { public: ULL numerator; ULL denominator; Fraction(ULL _numerator, ULL _denominator) { assert(_denominator > 0); numerator = _numerator; denominator = _denominator; }; Fraction operator*(const ULL rhs) { return Fraction(this->numerator * rhs, this->denominator); }; Fraction operator*(const Fraction &rhs) { return Fraction(this->numerator * rhs.numerator, this->denominator * rhs.denominator); } Fraction operator+(const Fraction &rhs) { ULL lcm = LCM(this->denominator, rhs.denominator); ULL numer_lhs = this->numerator * (this->denominator / lcm); ULL numer_rhs = rhs.numerator * (rhs.numerator / lcm); return Fraction(numer_lhs + numer_rhs, lcm); } Fraction &operator+=(const Fraction &rhs) { Fraction result = (*this) + rhs; this->numerator = result.numerator; this->denominator = result.denominator; return *this; } }; std::ostream &operator<<(std::ostream &s, const Fraction &a) { if (a.denominator == 1) { s << a.numerator; } else { s << a.numerator << "/" << a.denominator; } return s; } // }}} // }}} bool opt_debug = false; int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); // set options {{{ int __c; while ((__c = getopt(argc, argv, "d")) != -1) { switch (__c) { case 'd': opt_debug = true; break; default: abort(); } } // }}} // input("./inputs/0.txt"); // output("./outputs/0.txt"); int a, b, c; cin >> a >> b >> c; if (60 % (a + b) == 0 && a < (c % (a + b)) && (c % (a + b)) < a + b) { cout << -1 << endl; return 0; } int ans = c; while (a < c) { if (c > a + b) { c %= (a + b); } else { c = (c + 60) % (a + b); ans += 60; if (ans > INF) { ans = -1; break; } } } cout << ans << endl; return 0; } // vim: foldmethod=marker
insert
372
372
372
376
TLE
p01751
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int a, b, c; int d = 0; int n = 0; cin >> a >> b >> c; while (true) { if (d <= c && c <= d + a) { cout << n * 60 + c << endl; break; } d += a + b; if (d > 60) { n += d / 60; d %= 60; } if (d == 0) { cout << -1 << endl; break; } } }
#include <iostream> using namespace std; int main() { int a, b, c; int d = 0; int n = 0; cin >> a >> b >> c; while (true) { if (d <= c && c <= d + a) { cout << n * 60 + c << endl; break; } d += a + b; if (d >= 60) { n += d / 60; d %= 60; } if (d == 0) { cout << -1 << endl; break; } } }
replace
14
15
14
15
TLE
p01751
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define reep(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) reep((i), 0, (n)) typedef long long ll; int main() { ll a, b, c; cin >> a >> b >> c; ll t = 0; while (1) { if (c < (a + b) * t) { c += 60; } else if (c <= (a + b) * t + a) { cout << c << endl; return 0; } else { t++; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; #define reep(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) reep((i), 0, (n)) typedef long long ll; int main() { ll a, b, c; cin >> a >> b >> c; ll t = 0; while (1) { if (c < (a + b) * t) { c += 60; } else if (c <= (a + b) * t + a) { cout << c << endl; return 0; } else { t++; } if (t > 1000) break; } cout << -1 << endl; }
insert
22
22
22
24
TLE
p01751
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int st, et, r; int d[60] = {}; for (st = r = 0, et = a;; st = et + b, et = st + a, d[st] = et) { if (st >= 60) { st -= 60; et -= 60; r++; } if (c >= st && c <= et) { cout << r * 60 + c << endl; break; } if (d[st] == et) { cout << "-1" << endl; break; } } }
#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int st, et, r; int d[60] = {}; for (st = r = 0, et = a;; d[st] = et, st = et + b, et = st + a) { if (st >= 60) { st -= 60; et -= 60; r++; } if (c >= st && c <= et) { cout << r * 60 + c << endl; break; } if (d[st] == et) { cout << "-1" << endl; break; } } }
replace
8
9
8
9
TLE
p01751
C++
Runtime Error
#include <bits/stdc++.h> #define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; int main(void) { int a, b, c; cin >> a >> b >> c; int t = 0; int res = 0; vector<vector<int>> visited(a + b + 1, vector<int>(2, 0)); visited[0][0] = 1; bool ok = false; while (1) { int nt = t; nt += a; res += a; if ((t <= c and c <= nt) or (t <= c + 60 and c + 60 <= nt)) { res -= (nt - c) % 60; ok = true; break; } nt %= 60; if (visited[nt][1] != 0) break; visited[nt][1] = 1; t = nt; nt += b; res += b; nt %= 60; if (visited[nt][0] != 0) break; visited[nt][0] = 1; t = nt; } if (not ok) res = -1; cout << res << endl; return 0; }
#include <bits/stdc++.h> #define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) using namespace std; int main(void) { int a, b, c; cin >> a >> b >> c; int t = 0; int res = 0; vector<vector<int>> visited(121, vector<int>(2, 0)); visited[0][0] = 1; bool ok = false; while (1) { int nt = t; nt += a; res += a; if ((t <= c and c <= nt) or (t <= c + 60 and c + 60 <= nt)) { res -= (nt - c) % 60; ok = true; break; } nt %= 60; if (visited[nt][1] != 0) break; visited[nt][1] = 1; t = nt; nt += b; res += b; nt %= 60; if (visited[nt][0] != 0) break; visited[nt][0] = 1; t = nt; } if (not ok) res = -1; cout << res << endl; return 0; }
replace
9
10
9
10
0
p01752
C++
Runtime Error
#include <cmath> #include <iostream> #include <queue> #define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++) using namespace std; struct state { int y, x, d, hy, hx, n; bool operator<(const state &s) const { return n > s.n; } }; int N, M, D, SY, SX, GY, GX; char S[52][52]; int dd[5] = {0, 1, 0, -1, 0}; bool visited[52][52][52][52][4]; bool step[52][52]; bool isin(int y, int x) { return 0 <= y && y < N + 2 && 0 <= x && x < M + 2; } bool ok(int y, int x, int d, int hy, int hx) { if (S[y][x] == '#') return false; if (S[hy][hx] != '#') return false; bool f = false; if (y + dd[d] == hy && x + dd[d + 1] == hx) f = true; if (y + dd[d] + dd[(d + 1) % 4] == hy && x + dd[d + 1] + dd[(d + 1) % 4 + 1] == hx) f = true; if (y + dd[(d + 1) % 4] == hy && x + dd[(d + 1) % 4 + 1] == hx) f = true; if (y + dd[(d + 1) % 4] + dd[(d + 2) % 4] == hy && x + dd[(d + 1) % 4 + 1] + dd[(d + 2) % 4 + 1] == hx) f = true; return f; } int main(void) { cin >> N >> M; REP(i, 0, N + 2) REP(j, 0, N + 2) S[i][j] = '#'; REP(i, 1, N + 1) REP(j, 1, M + 1) { cin >> S[i][j]; if (S[i][j] == '>') SY = i, SX = j, D = 0; if (S[i][j] == 'v') SY = i, SX = j, D = 1; if (S[i][j] == '<') SY = i, SX = j, D = 2; if (S[i][j] == '^') SY = i, SX = j, D = 3; if (S[i][j] == 'G') GY = i, GX = j; } REP(i, 0, 52) REP(j, 0, 52) REP(k, 0, 52) REP(l, 0, 52) REP(m, 0, 4) visited[i][j][k][l][m] = false; REP(i, 0, 52) REP(j, 0, 52) step[i][j] = false; priority_queue<state> q; q.push((state){SY, SX, D, SY + dd[(D + 1) % 4], SX + dd[(D + 1) % 4 + 1], 1}); int ans = -1; while (q.size()) { state s = q.top(); q.pop(); if (visited[s.y][s.x][s.hy][s.hx][s.d]) continue; visited[s.y][s.x][s.hy][s.hx][s.d] = true; step[s.y][s.x] = true; if (s.y == GY && s.x == GX) { ans = s.n; break; } REP(i, -1, 2) { int nd = (s.d + i + 4) % 4; int ny = s.y + (i == 0 ? dd[nd] : 0); int nx = s.x + (i == 0 ? dd[nd + 1] : 0); int nhy = s.hy; int nhx = s.hx; int nn = s.n + (i == 0 && !step[ny][nx] ? 1 : 0); if (ok(ny, nx, nd, nhy, nhx)) { q.push((state){ny, nx, nd, nhy, nhx, nn}); } } REP(i, -1, 2) REP(j, -1, 2) { if (i == 0 && j == 0) continue; int nd = s.d; int ny = s.y; int nx = s.x; int nhy = s.hy + i; int nhx = s.hx + j; int nn = s.n; if (isin(nhy, nhx) && ok(ny, nx, nd, nhy, nhx)) { q.push((state){ny, nx, nd, nhy, nhx, nn}); } } } cout << ans << endl; return 0; }
#include <cmath> #include <iostream> #include <queue> #define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++) using namespace std; struct state { int y, x, d, hy, hx, n; bool operator<(const state &s) const { return n > s.n; } }; int N, M, D, SY, SX, GY, GX; char S[52][52]; int dd[5] = {0, 1, 0, -1, 0}; bool visited[52][52][52][52][4]; bool step[52][52]; bool isin(int y, int x) { return 0 <= y && y < N + 2 && 0 <= x && x < M + 2; } bool ok(int y, int x, int d, int hy, int hx) { if (S[y][x] == '#') return false; if (S[hy][hx] != '#') return false; bool f = false; if (y + dd[d] == hy && x + dd[d + 1] == hx) f = true; if (y + dd[d] + dd[(d + 1) % 4] == hy && x + dd[d + 1] + dd[(d + 1) % 4 + 1] == hx) f = true; if (y + dd[(d + 1) % 4] == hy && x + dd[(d + 1) % 4 + 1] == hx) f = true; if (y + dd[(d + 1) % 4] + dd[(d + 2) % 4] == hy && x + dd[(d + 1) % 4 + 1] + dd[(d + 2) % 4 + 1] == hx) f = true; return f; } int main(void) { cin >> N >> M; REP(i, 0, N + 2) REP(j, 0, M + 2) S[i][j] = '#'; REP(i, 1, N + 1) REP(j, 1, M + 1) { cin >> S[i][j]; if (S[i][j] == '>') SY = i, SX = j, D = 0; if (S[i][j] == 'v') SY = i, SX = j, D = 1; if (S[i][j] == '<') SY = i, SX = j, D = 2; if (S[i][j] == '^') SY = i, SX = j, D = 3; if (S[i][j] == 'G') GY = i, GX = j; } REP(i, 0, 52) REP(j, 0, 52) REP(k, 0, 52) REP(l, 0, 52) REP(m, 0, 4) visited[i][j][k][l][m] = false; REP(i, 0, 52) REP(j, 0, 52) step[i][j] = false; priority_queue<state> q; q.push((state){SY, SX, D, SY + dd[(D + 1) % 4], SX + dd[(D + 1) % 4 + 1], 1}); int ans = -1; while (q.size()) { state s = q.top(); q.pop(); if (visited[s.y][s.x][s.hy][s.hx][s.d]) continue; visited[s.y][s.x][s.hy][s.hx][s.d] = true; step[s.y][s.x] = true; if (s.y == GY && s.x == GX) { ans = s.n; break; } REP(i, -1, 2) { int nd = (s.d + i + 4) % 4; int ny = s.y + (i == 0 ? dd[nd] : 0); int nx = s.x + (i == 0 ? dd[nd + 1] : 0); int nhy = s.hy; int nhx = s.hx; int nn = s.n + (i == 0 && !step[ny][nx] ? 1 : 0); if (ok(ny, nx, nd, nhy, nhx)) { q.push((state){ny, nx, nd, nhy, nhx, nn}); } } REP(i, -1, 2) REP(j, -1, 2) { if (i == 0 && j == 0) continue; int nd = s.d; int ny = s.y; int nx = s.x; int nhy = s.hy + i; int nhx = s.hx + j; int nn = s.n; if (isin(nhy, nhx) && ok(ny, nx, nd, nhy, nhx)) { q.push((state){ny, nx, nd, nhy, nhx, nn}); } } } cout << ans << endl; return 0; }
replace
40
41
40
41
0
p01752
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; int Dx[4] = {0, 1, 0, -1}; int Dy[4] = {1, 0, -1, 0}; int dhx[4][4] = { {0, 1, 1, 1}, {1, 1, 0, -1}, {0, -1, -1, -1}, {-1, -1, 0, 1}, }; int dhy[4][4] = { {1, 1, 0, -1}, {0, -1, -1, -1}, {-1, -1, 0, 1}, {0, 1, 1, 1}, }; struct Node { int x, y, hx, hy, dir; }; int H, W; int wall[100][100] = {0}; bool used[100][100] = {0}; bool reach[100][100][100][100][4] = {0}; bool inRange(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; } bool inRange2(int x, int y) { return -1 <= x && x <= W && -1 <= y && y <= H; } bool Wall(int x, int y) { if (inRange(x, y)) return wall[y][x]; return true; } bool isInvalidHand(int px, int py, int hx, int hy, int dir) { // cout << ":" << px << " " << py << " " << hx << " " << hy << endl; if (!Wall(hx, hy)) return true; for (int i = 0; i < 4; ++i) { int x = px + dhx[dir][i]; int y = py + dhy[dir][i]; if (inRange2(x, y) && hx == x && hy == y) { return false; } } return true; } int main() { cin >> H >> W; for (int i = 0; i < 4; ++i) { Dy[i] = -Dy[i]; for (int j = 0; j < 4; ++j) { dhy[i][j] = -dhy[i][j]; } } int sx = -1, sy = -1, sd = -1; int gx = -1, gy = -1; for (int y = 0; y < H; ++y) { string str; cin >> str; for (int x = 0; x < W; ++x) { switch (str[x]) { case '#': wall[y][x] = true; break; case 'G': gx = x, gy = y; break; case '^': sx = x, sy = y; sd = 0; break; case '>': sx = x, sy = y; sd = 1; break; case 'v': sx = x, sy = y; sd = 2; break; case '<': sx = x, sy = y; sd = 3; break; } } } int ans = -1; queue<Node> Q; // fill(used[0], used[50], false); // fill(reach[0][0][0][0], reach[49][49][50][51], false); Q.push((Node){sx, sy, sx + Dx[(sd + 1) % 4], sy + Dy[(sd + 1) % 4], sd}); while (!Q.empty()) { Node node = Q.front(); Q.pop(); int px = node.x, py = node.y, hx = node.hx, hy = node.hy, dir = node.dir; if (!inRange(px, py)) continue; if (wall[py][px]) continue; if (!inRange2(hx, hy)) continue; if (reach[py][px][hy + 1][hx + 1][dir]) continue; if (isInvalidHand(px, py, hx, hy, dir)) continue; reach[py][px][hy + 1][hx + 1][dir] = true; used[py][px] = true; if (px == gx && py == gy) { break; } // cout << px << " " << py << " " << hx << " " << hy << " " << dir << //endl; // move player { int npx = px + Dx[dir]; int npy = py + Dy[dir]; Q.push((Node){npx, npy, hx, hy, dir}); } // move hand for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) { int nhx = hx + dx; int nhy = hy + dy; if (inRange2(nhx, nhy) && Wall(hx, hy)) { Q.push((Node){px, py, nhx, nhy, dir}); } } // change dir / turn Q.push((Node){px, py, hx, hy, (dir + 1) % 4}); Q.push((Node){px, py, hx, hy, (dir - 1 + 4) % 4}); } int s = 0; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { s += used[y][x]; } } if (used[gy][gx] && (ans < 0 || s < ans)) { ans = s; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int Dx[4] = {0, 1, 0, -1}; int Dy[4] = {1, 0, -1, 0}; int dhx[4][4] = { {0, 1, 1, 1}, {1, 1, 0, -1}, {0, -1, -1, -1}, {-1, -1, 0, 1}, }; int dhy[4][4] = { {1, 1, 0, -1}, {0, -1, -1, -1}, {-1, -1, 0, 1}, {0, 1, 1, 1}, }; struct Node { int x, y, hx, hy, dir; }; int H, W; int wall[60][60] = {0}; bool used[60][60] = {0}; bool reach[60][60][60][60][4] = {0}; bool inRange(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; } bool inRange2(int x, int y) { return -1 <= x && x <= W && -1 <= y && y <= H; } bool Wall(int x, int y) { if (inRange(x, y)) return wall[y][x]; return true; } bool isInvalidHand(int px, int py, int hx, int hy, int dir) { // cout << ":" << px << " " << py << " " << hx << " " << hy << endl; if (!Wall(hx, hy)) return true; for (int i = 0; i < 4; ++i) { int x = px + dhx[dir][i]; int y = py + dhy[dir][i]; if (inRange2(x, y) && hx == x && hy == y) { return false; } } return true; } int main() { cin >> H >> W; for (int i = 0; i < 4; ++i) { Dy[i] = -Dy[i]; for (int j = 0; j < 4; ++j) { dhy[i][j] = -dhy[i][j]; } } int sx = -1, sy = -1, sd = -1; int gx = -1, gy = -1; for (int y = 0; y < H; ++y) { string str; cin >> str; for (int x = 0; x < W; ++x) { switch (str[x]) { case '#': wall[y][x] = true; break; case 'G': gx = x, gy = y; break; case '^': sx = x, sy = y; sd = 0; break; case '>': sx = x, sy = y; sd = 1; break; case 'v': sx = x, sy = y; sd = 2; break; case '<': sx = x, sy = y; sd = 3; break; } } } int ans = -1; queue<Node> Q; // fill(used[0], used[50], false); // fill(reach[0][0][0][0], reach[49][49][50][51], false); Q.push((Node){sx, sy, sx + Dx[(sd + 1) % 4], sy + Dy[(sd + 1) % 4], sd}); while (!Q.empty()) { Node node = Q.front(); Q.pop(); int px = node.x, py = node.y, hx = node.hx, hy = node.hy, dir = node.dir; if (!inRange(px, py)) continue; if (wall[py][px]) continue; if (!inRange2(hx, hy)) continue; if (reach[py][px][hy + 1][hx + 1][dir]) continue; if (isInvalidHand(px, py, hx, hy, dir)) continue; reach[py][px][hy + 1][hx + 1][dir] = true; used[py][px] = true; if (px == gx && py == gy) { break; } // cout << px << " " << py << " " << hx << " " << hy << " " << dir << //endl; // move player { int npx = px + Dx[dir]; int npy = py + Dy[dir]; Q.push((Node){npx, npy, hx, hy, dir}); } // move hand for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) { int nhx = hx + dx; int nhy = hy + dy; if (inRange2(nhx, nhy) && Wall(hx, hy)) { Q.push((Node){px, py, nhx, nhy, dir}); } } // change dir / turn Q.push((Node){px, py, hx, hy, (dir + 1) % 4}); Q.push((Node){px, py, hx, hy, (dir - 1 + 4) % 4}); } int s = 0; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { s += used[y][x]; } } if (used[gy][gx] && (ans < 0 || s < ans)) { ans = s; } cout << ans << endl; }
replace
25
28
25
28
MLE
p01752
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; namespace { typedef double real; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) { if (vs.empty()) return os << "[]"; os << "[" << vs[0]; for (int i = 1; i < vs.size(); i++) os << " " << vs[i]; return os << "]"; } int H, W; vector<string> F; void input() { cin >> H >> W; H += 2; W += 2; F.resize(H); F[0] = string(W, '#'); for (int i = 1; i < H - 1; i++) { string s; cin >> s; F[i] = string("#") + s + "#"; } F[H - 1] = string(W, '#'); } const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; int sy, sx, sdir; void init() { string s = "^>v<"; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (find(s.begin(), s.end(), F[i][j]) != s.end()) { sy = i; sx = j; sdir = find(s.begin(), s.end(), F[i][j]) - s.begin(); return; } } } } struct State { int y, x; int dir; State(int y, int x, int dir) : y(y), x(x), dir(dir) {} }; const int INF = 1 << 28; void solve() { init(); bool V[H][W]; bool D[H][W][4]; memset(V, false, sizeof(V)); memset(D, false, sizeof(D)); queue<State> Q; Q.push(State(sy, sx, sdir)); V[sy][sx] = true; D[sy][sx][sdir] = true; while (not Q.empty()) { State c = Q.front(); Q.pop(); if (F[c.y][c.x] == 'G') { break; } for (int i = 0; i < 4; i++) { int ndir = (c.dir + 5 - i) % 4; int ny = c.y + dy[ndir]; int nx = c.x + dx[ndir]; if (F[ny][nx] == '#') continue; if (D[ny][nx][ndir]) { cout << -1 << endl; return; } V[ny][nx] = true; Q.push(State(ny, nx, ndir)); break; } } int ans = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (V[i][j]) ans++; // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << V[i][j] // << " "; } cout << endl; } cout << ans << endl; } } // namespace int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; namespace { typedef double real; typedef long long ll; template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) { if (vs.empty()) return os << "[]"; os << "[" << vs[0]; for (int i = 1; i < vs.size(); i++) os << " " << vs[i]; return os << "]"; } int H, W; vector<string> F; void input() { cin >> H >> W; H += 2; W += 2; F.resize(H); F[0] = string(W, '#'); for (int i = 1; i < H - 1; i++) { string s; cin >> s; F[i] = string("#") + s + "#"; } F[H - 1] = string(W, '#'); } const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; int sy, sx, sdir; void init() { string s = "^>v<"; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (find(s.begin(), s.end(), F[i][j]) != s.end()) { sy = i; sx = j; sdir = find(s.begin(), s.end(), F[i][j]) - s.begin(); return; } } } } struct State { int y, x; int dir; State(int y, int x, int dir) : y(y), x(x), dir(dir) {} }; const int INF = 1 << 28; void solve() { init(); bool V[H][W]; bool D[H][W][4]; memset(V, false, sizeof(V)); memset(D, false, sizeof(D)); queue<State> Q; Q.push(State(sy, sx, sdir)); V[sy][sx] = true; D[sy][sx][sdir] = true; while (not Q.empty()) { State c = Q.front(); Q.pop(); if (F[c.y][c.x] == 'G') { break; } for (int i = 0; i < 4; i++) { int ndir = (c.dir + 5 - i) % 4; int ny = c.y + dy[ndir]; int nx = c.x + dx[ndir]; if (F[ny][nx] == '#') continue; if (D[ny][nx][ndir]) { cout << -1 << endl; return; } V[ny][nx] = true; D[ny][nx][ndir] = true; Q.push(State(ny, nx, ndir)); break; } } int ans = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) if (V[i][j]) ans++; // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << V[i][j] // << " "; } cout << endl; } cout << ans << endl; } } // namespace int main() { input(); solve(); return 0; }
insert
87
87
87
88
TLE
p01755
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) const int H = 55, W = 55; const int L = 1111; string board[H]; int h, w; string in; bool isClear; int walk = 0; int memo_if[L]; int memo_while[L]; int visit[H][W][4][L]; int x; int y; int dir; int dx[4] = {0, 1, 0, -1}; int dy[4] = {-1, 0, 1, 0}; void input() { cin >> h >> w; rep(i, h) cin >> board[i]; cin >> in; in += "*"; rep(i, L) memo_if[i] = memo_while[i]; rep(i, h) { rep(j, w) { if (board[i][j] == 's') { board[i][j] = '.'; x = j; y = i; dir = 0; } } } } int sim_program(int); bool sim_check(int st) { char c = in[st]; if (c == 'N') return dir == 0; if (c == 'E') return dir == 1; if (c == 'S') return dir == 2; if (c == 'W') return dir == 3; int nx = x + dx[dir]; int ny = y + dy[dir]; if (c == 'C') return board[ny][nx] == '#'; return true; } int sim_if(int st) { bool rev = false; if (in[st] == '~') { rev = true; st++; } bool res = rev ^ sim_check(st); st++; if (res) { int res = sim_program(st); if (res == -1) return res; } return 0; } int sim_while(int st) { int first = st; while (1) { bool rev = false; if (in[st] == '~') { rev = true; st++; } bool res = rev ^ sim_check(st); st++; if (res) { int res = sim_program(st); if (res == -1) return res; } else { break; } st = first; } return 0; } int sim_simple(int st) { char c = in[st]; int nx = x; int ny = y; if (c == '^') { nx = x + dx[dir]; ny = y + dy[dir]; } if (c == 'v') { nx = x - dx[dir]; ny = y - dy[dir]; } if (board[ny][nx] == '#') { nx = x; ny = y; } x = nx; y = ny; if (c == '<') dir = (dir + 3) % 4; if (c == '>') dir = (dir + 1) % 4; // printf("aa %d %d %d %d\n",st,x,y,dir); walk++; if (board[y][x] == 'g') { isClear = true; return -1; } if (visit[y][x][dir][st] == 1) return -1; visit[y][x][dir][st] = 1; return 0; } bool isSimple(char c) { if (c == '^') return true; if (c == 'v') return true; if (c == '>') return true; if (c == '<') return true; return false; } int end_if(int st) { if (memo_if[st] != 0) return memo_if[st]; int cnt = 0; int pos = st; while (1) { if (in[pos] == '[') cnt++; if (in[pos] == ']') cnt--; if (cnt == 0) { return memo_while[st] = pos; } pos++; } return -1; } int end_while(int st) { if (memo_while[st] != 0) return memo_while[st]; int cnt = 0; int pos = st; while (1) { if (in[pos] == '{') cnt++; if (in[pos] == '}') cnt--; if (cnt == 0) { return memo_if[st] = pos; } pos++; } return -1; } int sim_program(int st) { while (1) { if (in[st] == '*' || in[st] == '}' || in[st] == ']') return 0; if (in[st] == '[') { int res = sim_if(st + 1); if (res == -1) return res; st = end_if(st) + 1; continue; } if (in[st] == '{') { int res = sim_while(st + 1); if (res == -1) return res; st = end_while(st) + 1; continue; } if (isSimple(in[st])) { int res = sim_simple(st); if (res == -1) return res; st++; continue; } } } int solve() { sim_program(0); if (isClear) return walk; return -1; } int main() { input(); cout << solve() << endl; }
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; #define reps(i, f, n) for (int i = f; i < int(n); i++) #define rep(i, n) reps(i, 0, n) const int H = 55, W = 55; const int L = 1111; string board[H]; int h, w; string in; bool isClear; int walk = 0; int memo_if[L]; int memo_while[L]; int visit[H][W][4][L]; int x; int y; int dir; int dx[4] = {0, 1, 0, -1}; int dy[4] = {-1, 0, 1, 0}; void input() { cin >> h >> w; rep(i, h) cin >> board[i]; cin >> in; in += "*"; rep(i, L) memo_if[i] = memo_while[i]; rep(i, h) { rep(j, w) { if (board[i][j] == 's') { board[i][j] = '.'; x = j; y = i; dir = 0; } } } } int sim_program(int); bool sim_check(int st) { char c = in[st]; if (c == 'N') return dir == 0; if (c == 'E') return dir == 1; if (c == 'S') return dir == 2; if (c == 'W') return dir == 3; int nx = x + dx[dir]; int ny = y + dy[dir]; if (c == 'C') return board[ny][nx] == '#'; return true; } int sim_if(int st) { bool rev = false; if (in[st] == '~') { rev = true; st++; } bool res = rev ^ sim_check(st); st++; if (res) { int res = sim_program(st); if (res == -1) return res; } return 0; } int sim_while(int st) { int first = st; while (1) { if (visit[y][x][dir][st] == 1) return -1; visit[y][x][dir][st] = 1; bool rev = false; if (in[st] == '~') { rev = true; st++; } bool res = rev ^ sim_check(st); st++; if (res) { int res = sim_program(st); if (res == -1) return res; } else { break; } st = first; } return 0; } int sim_simple(int st) { char c = in[st]; int nx = x; int ny = y; if (c == '^') { nx = x + dx[dir]; ny = y + dy[dir]; } if (c == 'v') { nx = x - dx[dir]; ny = y - dy[dir]; } if (board[ny][nx] == '#') { nx = x; ny = y; } x = nx; y = ny; if (c == '<') dir = (dir + 3) % 4; if (c == '>') dir = (dir + 1) % 4; // printf("aa %d %d %d %d\n",st,x,y,dir); walk++; if (board[y][x] == 'g') { isClear = true; return -1; } if (visit[y][x][dir][st] == 1) return -1; visit[y][x][dir][st] = 1; return 0; } bool isSimple(char c) { if (c == '^') return true; if (c == 'v') return true; if (c == '>') return true; if (c == '<') return true; return false; } int end_if(int st) { if (memo_if[st] != 0) return memo_if[st]; int cnt = 0; int pos = st; while (1) { if (in[pos] == '[') cnt++; if (in[pos] == ']') cnt--; if (cnt == 0) { return memo_while[st] = pos; } pos++; } return -1; } int end_while(int st) { if (memo_while[st] != 0) return memo_while[st]; int cnt = 0; int pos = st; while (1) { if (in[pos] == '{') cnt++; if (in[pos] == '}') cnt--; if (cnt == 0) { return memo_if[st] = pos; } pos++; } return -1; } int sim_program(int st) { while (1) { if (in[st] == '*' || in[st] == '}' || in[st] == ']') return 0; if (in[st] == '[') { int res = sim_if(st + 1); if (res == -1) return res; st = end_if(st) + 1; continue; } if (in[st] == '{') { int res = sim_while(st + 1); if (res == -1) return res; st = end_while(st) + 1; continue; } if (isSimple(in[st])) { int res = sim_simple(st); if (res == -1) return res; st++; continue; } } } int solve() { sim_program(0); if (isClear) return walk; return -1; } int main() { input(); cout << solve() << endl; }
insert
97
97
97
101
TLE
p01755
C++
Time Limit Exceeded
#include <iostream> #include <stack> #include <string> #include <vector> using namespace std; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int main() { // ------ Variable, Input ------ // int H, W, ptr, cnt, sec, x, y, d; bool clear; vector<int> Par; vector<string> MAP; string S; cin >> H >> W; MAP = vector<string>(H); for (int i = 0; i < H; i++) { cin >> MAP[i]; } cin >> S; // ------ Prepare ------ // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (MAP[i][j] == 's') { x = j; y = i; } } } Par = vector<int>(S.size(), -1); stack<int> Par2; for (int i = 0; i < S.size(); i++) { if (S[i] == '[' || S[i] == '{') { Par2.push(i); } if (S[i] == ']' || S[i] == '}') { Par[i] = Par2.top(); Par[Par2.top()] = i; Par2.pop(); } } d = 0; ptr = 0; cnt = 0, sec = 0; clear = false; // ------ Move ------ // while (ptr < S.size()) { if (sec++ > 100000000) { break; } if (S[ptr] == '^') { if (MAP[y + dy[d]][x + dx[d]] != '#') { x += dx[d]; y += dy[d]; } cnt++; } else if (S[ptr] == 'v') { if (MAP[y - dy[d]][x - dx[d]] != '#') { x -= dx[d]; y -= dy[d]; } cnt++; } else if (S[ptr] == '<') { d -= 1; if (d < 0) { d += 4; } cnt++; } else if (S[ptr] == '>') { d += 1; if (d > 3) { d -= 4; } cnt++; } else if (S[ptr] == '[' || S[ptr] == '{') { string S2; S2 = S[ptr + 1]; if (S2 == "~") { S2 += S[ptr + 2]; } bool ok = false; if (S2 == "T") { ok = true; } else if (S2 == "N") { ok = (d == 0); } else if (S2 == "E") { ok = (d == 1); } else if (S2 == "S") { ok = (d == 2); } else if (S2 == "W") { ok = (d == 3); } else if (S2 == "~N") { ok = (d != 0); } else if (S2 == "~E") { ok = (d != 1); } else if (S2 == "~S") { ok = (d != 2); } else if (S2 == "~W") { ok = (d != 3); } else if (S2 == "C") { ok = (MAP[y + dy[d]][x + dx[d]] == '#'); } else if (S2 == "~C") { ok = (MAP[y + dy[d]][x + dx[d]] != '#'); } if (ok == true) { ptr += S2.size(); } else { ptr = Par[ptr]; } } else if (S[ptr] == '}') { ptr = Par[ptr] - 1; } if (MAP[y][x] == 'g') { clear = true; break; } ptr++; } if (clear == false) { cout << -1 << endl; } else { cout << cnt << endl; } return 0; }
#include <iostream> #include <stack> #include <string> #include <vector> using namespace std; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int main() { // ------ Variable, Input ------ // int H, W, ptr, cnt, sec, x, y, d; bool clear; vector<int> Par; vector<string> MAP; string S; cin >> H >> W; MAP = vector<string>(H); for (int i = 0; i < H; i++) { cin >> MAP[i]; } cin >> S; // ------ Prepare ------ // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (MAP[i][j] == 's') { x = j; y = i; } } } Par = vector<int>(S.size(), -1); stack<int> Par2; for (int i = 0; i < S.size(); i++) { if (S[i] == '[' || S[i] == '{') { Par2.push(i); } if (S[i] == ']' || S[i] == '}') { Par[i] = Par2.top(); Par[Par2.top()] = i; Par2.pop(); } } d = 0; ptr = 0; cnt = 0, sec = 0; clear = false; // ------ Move ------ // while (ptr < S.size()) { if (sec++ > 30000000) { break; } if (S[ptr] == '^') { if (MAP[y + dy[d]][x + dx[d]] != '#') { x += dx[d]; y += dy[d]; } cnt++; } else if (S[ptr] == 'v') { if (MAP[y - dy[d]][x - dx[d]] != '#') { x -= dx[d]; y -= dy[d]; } cnt++; } else if (S[ptr] == '<') { d -= 1; if (d < 0) { d += 4; } cnt++; } else if (S[ptr] == '>') { d += 1; if (d > 3) { d -= 4; } cnt++; } else if (S[ptr] == '[' || S[ptr] == '{') { string S2; S2 = S[ptr + 1]; if (S2 == "~") { S2 += S[ptr + 2]; } bool ok = false; if (S2 == "T") { ok = true; } else if (S2 == "N") { ok = (d == 0); } else if (S2 == "E") { ok = (d == 1); } else if (S2 == "S") { ok = (d == 2); } else if (S2 == "W") { ok = (d == 3); } else if (S2 == "~N") { ok = (d != 0); } else if (S2 == "~E") { ok = (d != 1); } else if (S2 == "~S") { ok = (d != 2); } else if (S2 == "~W") { ok = (d != 3); } else if (S2 == "C") { ok = (MAP[y + dy[d]][x + dx[d]] == '#'); } else if (S2 == "~C") { ok = (MAP[y + dy[d]][x + dx[d]] != '#'); } if (ok == true) { ptr += S2.size(); } else { ptr = Par[ptr]; } } else if (S[ptr] == '}') { ptr = Par[ptr] - 1; } if (MAP[y][x] == 'g') { clear = true; break; } ptr++; } if (clear == false) { cout << -1 << endl; } else { cout << cnt << endl; } return 0; }
replace
67
68
67
68
TLE
p01756
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct SuffixArray { string s; vector<int> sa; SuffixArray(const string &S) { s = S; int n = S.length(); sa.assign(n + 1, 0); vector<int> rnk(n + 1), tmp(n + 1); for (int i = 0; i <= n; i++) { sa[i] = i; rnk[i] = i < n ? S[i] : -1; } int k = 0; auto compare_sa = [&](int i, int j) { if (rnk[i] != rnk[j]) return rnk[i] < rnk[j]; else { int ri = i + k <= n ? rnk[i + k] : -1; int rj = j + k <= n ? rnk[j + k] : -1; return ri < rj; } }; for (k = 1; k <= n; k *= 2) { sort(sa.begin(), sa.end(), compare_sa); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { rnk[i] = tmp[i]; } } } bool contain(const string &T) { int a = 0, b = s.size(); while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return s.compare(sa[b], T.length(), T) == 0; } int lower_bound(const string &T) { int a = 0, b = s.size(); while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return b; } int upper_bound(const string &T) { int a = 0, b = s.size() + 1; while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) <= 0) a = c; else b = c; } return b; } int get(int idx) { return sa[idx]; } }; const int MAX_N = 1 << 17; const int INF = 1 << 25; class MinSegTree { int n, dat[2 * MAX_N - 1]; public: void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; i++) { dat[i] = INF; } } void update(int k, int a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]); } } int query(int a, int b) { return query(a, b, 0, 0, n); } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return dat[k]; else { int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } } void show() { for (int i = 0; i < n; i++) { cout << dat[i + n - 1] << " "; } cout << endl; } }; MinSegTree min_seg; MinSegTree min_segr; int main() { cin.tie(0); ios::sync_with_stdio(false); string S, Sr; int m; cin >> S >> m; Sr = S; reverse(Sr.begin(), Sr.end()); SuffixArray sa1(S), sa2(Sr); min_seg.init(S.size() + 1); min_segr.init(S.size() + 1); for (int i = 0; i < S.size() + 1; i++) { min_seg.update(i, sa1.get(i)); } for (int i = 0; i < S.size() + 1; i++) { min_segr.update(i, sa2.get(i)); } while (m--) { string x, y; cin >> x >> y; reverse(y.begin(), y.end()); if (!sa1.contain(x) || !sa2.contain(y)) { cout << 0 << endl; continue; } int lb = sa1.lower_bound(x); int ub = sa1.upper_bound(x); int a = min_seg.query(lb, ub); lb = sa2.lower_bound(y); ub = sa2.upper_bound(y); int b = min_segr.query(lb, ub); if (a + x.size() <= S.size() - b && (S.size() - b) - y.size() + 1 >= a) { cout << S.size() - a - b << endl; } else { cout << 0 << endl; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct SuffixArray { string s; vector<int> sa; SuffixArray(const string &S) { s = S; int n = S.length(); sa.assign(n + 1, 0); vector<int> rnk(n + 1), tmp(n + 1); for (int i = 0; i <= n; i++) { sa[i] = i; rnk[i] = i < n ? S[i] : -1; } int k = 0; auto compare_sa = [&](int i, int j) { if (rnk[i] != rnk[j]) return rnk[i] < rnk[j]; else { int ri = i + k <= n ? rnk[i + k] : -1; int rj = j + k <= n ? rnk[j + k] : -1; return ri < rj; } }; for (k = 1; k <= n; k *= 2) { sort(sa.begin(), sa.end(), compare_sa); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { rnk[i] = tmp[i]; } } } bool contain(const string &T) { int a = 0, b = s.size(); while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return s.compare(sa[b], T.length(), T) == 0; } int lower_bound(const string &T) { int a = 0, b = s.size(); while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return b; } int upper_bound(const string &T) { int a = 0, b = s.size() + 1; while (b - a > 1) { int c = (a + b) / 2; if (s.compare(sa[c], T.length(), T) <= 0) a = c; else b = c; } return b; } int get(int idx) { return sa[idx]; } }; const int MAX_N = 1 << 20; const int INF = 1 << 25; class MinSegTree { int n, dat[2 * MAX_N - 1]; public: void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; i++) { dat[i] = INF; } } void update(int k, int a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]); } } int query(int a, int b) { return query(a, b, 0, 0, n); } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return dat[k]; else { int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } } void show() { for (int i = 0; i < n; i++) { cout << dat[i + n - 1] << " "; } cout << endl; } }; MinSegTree min_seg; MinSegTree min_segr; int main() { cin.tie(0); ios::sync_with_stdio(false); string S, Sr; int m; cin >> S >> m; Sr = S; reverse(Sr.begin(), Sr.end()); SuffixArray sa1(S), sa2(Sr); min_seg.init(S.size() + 1); min_segr.init(S.size() + 1); for (int i = 0; i < S.size() + 1; i++) { min_seg.update(i, sa1.get(i)); } for (int i = 0; i < S.size() + 1; i++) { min_segr.update(i, sa2.get(i)); } while (m--) { string x, y; cin >> x >> y; reverse(y.begin(), y.end()); if (!sa1.contain(x) || !sa2.contain(y)) { cout << 0 << endl; continue; } int lb = sa1.lower_bound(x); int ub = sa1.upper_bound(x); int a = min_seg.query(lb, ub); lb = sa2.lower_bound(y); ub = sa2.upper_bound(y); int b = min_segr.query(lb, ub); if (a + x.size() <= S.size() - b && (S.size() - b) - y.size() + 1 >= a) { cout << S.size() - a - b << endl; } else { cout << 0 << endl; } } }
replace
78
79
78
79
0
p01756
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define N 100001 using namespace std; typedef pair<string, int> P; typedef set<P>::iterator It; void solve(string str, set<P> A, vector<int> &L) { for (int i = 0; i < str.size(); i++) { string t; t += str[i]; It it; while ((it = A.lower_bound(P(t, -1))) != A.end()) { while (it != A.end() && it->first == t) L[it->second] = i, A.erase(it++); if (i + t.size() >= str.size()) break; t += str[i + t.size()]; } } } int main() { string str; cin >> str; int len = str.size(), n; cin >> n; set<P> A, B; string a[N], b[N]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; A.insert(P(a[i], i)); reverse(b[i].begin(), b[i].end()); B.insert(P(b[i], i)); } vector<int> L(N, -1), R(N, -1); solve(str, A, L); reverse(str.begin(), str.end()); solve(str, B, R); for (int i = 0; i < n; i++) { int cnt = 0, alen = a[i].size(), blen = b[i].size(); while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt]) cnt++; if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt) printf("0\n"); else printf("%d\n", len - R[i] - L[i]); } return 0; }
#include <bits/stdc++.h> #define N 100001 using namespace std; typedef pair<string, int> P; typedef set<P>::iterator It; void solve(string str, set<P> A, vector<int> &L) { for (int i = 0; i < str.size(); i++) { string t; t += str[i]; It it; while ((it = A.lower_bound(P(t, -1))) != A.end()) { if (it->first.substr(0, t.size()) != t) break; while (it != A.end() && it->first == t) L[it->second] = i, A.erase(it++); if (i + t.size() >= str.size()) break; t += str[i + t.size()]; } } } int main() { string str; cin >> str; int len = str.size(), n; cin >> n; set<P> A, B; string a[N], b[N]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; A.insert(P(a[i], i)); reverse(b[i].begin(), b[i].end()); B.insert(P(b[i], i)); } vector<int> L(N, -1), R(N, -1); solve(str, A, L); reverse(str.begin(), str.end()); solve(str, B, R); for (int i = 0; i < n; i++) { int cnt = 0, alen = a[i].size(), blen = b[i].size(); while (cnt < min(alen, blen) && a[i][alen - cnt] == b[i][cnt]) cnt++; if (L[i] == -1 || R[i] == -1 || len - R[i] < L[i] + alen - cnt) printf("0\n"); else printf("%d\n", len - R[i] - L[i]); } return 0; }
insert
12
12
12
14
TLE
p01758
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int INF = 1e9 + 7; using Weight = int; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; auto operator<(const Edge &e, const Edge &f) { if (e.weight != f.weight) return e.weight > f.weight; else if (e.src != f.src) return e.src < f.src; else return e.dst < f.dst; } using Edges = vector<Edge>; using Graph = vector<Edges>; long long ChuLiu_Edmonds(Graph &g, int root) { const int V = g.size(); Edges mins(V, Edge(-1, -1, INF)); //????????????????????\??£?????????????°???????????????¶ for (auto es : g) { for (auto e : es) { if (e.weight < mins[e.dst].weight) { mins[e.dst] = e; } } } mins[root] = Edge(-1, -1, -1); /*for(auto e:mins){ if(e.weight==INF)return -1; }*/ vector<int> group(V, 0); vector<bool> isCycle(V, false); int count = 0; vector<bool> used(V, false); //??°??????????????? for (int i = 0; i < V; ++i) { if (used[i]) continue; vector<int> chain; int cursor = i; //??????????????£????????? while (cursor != -1 && !used[cursor]) { used[cursor] = true; chain.push_back(cursor); cursor = mins[cursor].src; } //?????????????????§?????? if (cursor != -1) { bool inCycle = false; for (int j = 0; j < chain.size(); ++j) { group[chain[j]] = count; if (chain[j] == cursor) { isCycle[count] = true; inCycle = true; } if (!inCycle) ++count; } if (inCycle) ++count; } else { //????????§?????? for (int j = 0; j < chain.size(); ++j) { group[chain[j]] = count++; } } } //??°????????????????????? //?????????????????¨??? if (count == V) { long long ans = 1; for (int i = 0; i < V; ++i) { ans += mins[i].weight; } return ans; } //????´?????????????????????????????¨? long long res = 0; for (int i = 0; i < V; ++i) { if (i != root && isCycle[group[i]]) { res += mins[i].weight; } } //???????????????????????????????????°???????????????????????? Graph ng(count); for (auto es : g) { for (auto e : es) { int to = e.dst; int gfrom = group[e.src]; int gto = group[e.dst]; if (gfrom == gto) { continue; } else if (isCycle[gto]) { ng[gfrom].emplace_back(gfrom, gto, e.weight - mins[to].weight); } else { ng[gfrom].emplace_back(gfrom, gto, e.weight); } } } return res + ChuLiu_Edmonds(ng, group[root]); } int dijkstra(const Graph &g, int s, int t) { const int V = g.size(); priority_queue<Edge> q; vector<Weight> dist(V, INF); //???????????§????????¢?????\??? dist[s] = 0; vector<int> prev(V, -1); //???????????????????????\??? for (q.emplace(-1, s, 0); !q.empty();) { Edge e = q.top(); q.pop(); if (prev[e.dst] != -1) continue; prev[e.dst] = e.src; for (auto ge : g[e.dst]) { if (dist[ge.dst] > e.weight + ge.weight) { dist[ge.dst] = e.weight + ge.weight; q.emplace(ge.src, ge.dst, e.weight + ge.weight); } } } return dist[t]; } int main() { int N, M; cin >> N >> M; vector<int> in_e(N, 0), zero, to; Graph g(N); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; g[a].emplace_back(a, b, 0); g[b].emplace_back(b, a, 1); in_e[b]++; } for (int i = 0; i < N; ++i) { if (!in_e[i]) zero.push_back(i); } Graph ng(zero.size()); for (int i = 0; i < zero.size(); ++i) { for (int j = 0; j < zero.size(); ++j) { if (i != j) { ng[i].emplace_back(i, j, dijkstra(g, zero[i], zero[j])); } } } vector<pair<ll, int>> hu; for (int i = 0; i < zero.size(); ++i) { hu.emplace_back(ChuLiu_Edmonds(ng, i), zero[i]); } stable_sort(hu.begin(), hu.end()); ll ans = hu[0].first; for (auto x : hu) { if (x.first == ans) to.push_back(x.second); else break; } cout << to.size() << " " << ans << endl; for (int i = 0; i < to.size(); ++i) { if (!i) cout << to[i]; else cout << " " << to[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int INF = 1e9 + 7; using Weight = int; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; auto operator<(const Edge &e, const Edge &f) { if (e.weight != f.weight) return e.weight > f.weight; else if (e.src != f.src) return e.src < f.src; else return e.dst < f.dst; } using Edges = vector<Edge>; using Graph = vector<Edges>; long long ChuLiu_Edmonds(Graph &g, int root) { const int V = g.size(); Edges mins(V, Edge(-1, -1, INF)); //????????????????????\??£?????????????°???????????????¶ for (auto es : g) { for (auto e : es) { if (e.weight < mins[e.dst].weight) { mins[e.dst] = e; } } } mins[root] = Edge(-1, -1, -1); /*for(auto e:mins){ if(e.weight==INF)return -1; }*/ vector<int> group(V, 0); vector<bool> isCycle(V, false); int count = 0; vector<bool> used(V, false); //??°??????????????? for (int i = 0; i < V; ++i) { if (used[i]) continue; vector<int> chain; int cursor = i; //??????????????£????????? while (cursor != -1 && !used[cursor]) { used[cursor] = true; chain.push_back(cursor); cursor = mins[cursor].src; } //?????????????????§?????? if (cursor != -1) { bool inCycle = false; for (int j = 0; j < chain.size(); ++j) { group[chain[j]] = count; if (chain[j] == cursor) { isCycle[count] = true; inCycle = true; } if (!inCycle) ++count; } if (inCycle) ++count; } else { //????????§?????? for (int j = 0; j < chain.size(); ++j) { group[chain[j]] = count++; } } } //??°????????????????????? //?????????????????¨??? if (count == V) { long long ans = 1; for (int i = 0; i < V; ++i) { ans += mins[i].weight; } return ans; } //????´?????????????????????????????¨? long long res = 0; for (int i = 0; i < V; ++i) { if (i != root && isCycle[group[i]]) { res += mins[i].weight; } } //???????????????????????????????????°???????????????????????? Graph ng(count); for (auto es : g) { for (auto e : es) { int to = e.dst; int gfrom = group[e.src]; int gto = group[e.dst]; if (gfrom == gto) { continue; } else if (isCycle[gto]) { ng[gfrom].emplace_back(gfrom, gto, e.weight - mins[to].weight); } else { ng[gfrom].emplace_back(gfrom, gto, e.weight); } } } return res + ChuLiu_Edmonds(ng, group[root]); } int dijkstra(const Graph &g, int s, int t) { const int V = g.size(); priority_queue<Edge> q; vector<Weight> dist(V, INF); //???????????§????????¢?????\??? dist[s] = 0; vector<int> prev(V, -1); //???????????????????????\??? for (q.emplace(-1, s, 0); !q.empty();) { Edge e = q.top(); q.pop(); if (prev[e.dst] != -1) continue; prev[e.dst] = e.src; for (auto ge : g[e.dst]) { if (dist[ge.dst] > e.weight + ge.weight) { dist[ge.dst] = e.weight + ge.weight; q.emplace(ge.src, ge.dst, e.weight + ge.weight); } } if (prev[t] != -1) return dist[t]; } return dist[t]; } int main() { int N, M; cin >> N >> M; vector<int> in_e(N, 0), zero, to; Graph g(N); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; g[a].emplace_back(a, b, 0); g[b].emplace_back(b, a, 1); in_e[b]++; } for (int i = 0; i < N; ++i) { if (!in_e[i]) zero.push_back(i); } Graph ng(zero.size()); for (int i = 0; i < zero.size(); ++i) { for (int j = 0; j < zero.size(); ++j) { if (i != j) { ng[i].emplace_back(i, j, dijkstra(g, zero[i], zero[j])); } } } vector<pair<ll, int>> hu; for (int i = 0; i < zero.size(); ++i) { hu.emplace_back(ChuLiu_Edmonds(ng, i), zero[i]); } stable_sort(hu.begin(), hu.end()); ll ans = hu[0].first; for (auto x : hu) { if (x.first == ans) to.push_back(x.second); else break; } cout << to.size() << " " << ans << endl; for (int i = 0; i < to.size(); ++i) { if (!i) cout << to[i]; else cout << " " << to[i]; } cout << endl; }
insert
131
131
131
133
TLE
p01764
C++
Time Limit Exceeded
#include <algorithm> #include <stdio.h> using namespace std; int b[310]; long long dp[310][310]; int sum[310]; int pow[10]; long long solve(int l, int r) { if (l == r) return 0; if (~dp[l][r]) return dp[l][r]; long long ret = 99999999999999LL; for (int i = l; i < r; i++) { int L = sum[i + 1] - sum[l]; int R = sum[r + 1] - sum[i + 1]; int c = 0; long long cost = solve(l, i) + solve(i + 1, r); while (L + R + c) { cost += (L % 10) * (R % 10); if (c + L % 10 + R % 10 >= 10) { c = 1; cost++; } L /= 10; R /= 10; } ret = min(ret, cost); } return dp[l][r] = ret; } int main() { int a; scanf("%d", &a); for (int i = 0; i < a; i++) scanf("%d", b + i); for (int i = 0; i < a; i++) sum[i + 1] = sum[i] + b[i]; for (int i = 0; i < 310; i++) { for (int j = 0; j < 310; j++) { dp[i][j] = -1; } } pow[0] = 1; for (int i = 1; i < 10; i++) pow[i] = pow[i - 1] * 10; printf("%lld\n", solve(0, a - 1)); }
#include <algorithm> #include <stdio.h> using namespace std; int b[310]; long long dp[310][310]; int sum[310]; int pow[10]; long long solve(int l, int r) { if (l == r) return 0; if (~dp[l][r]) return dp[l][r]; long long ret = 99999999999999LL; for (int i = l; i < r; i++) { int L = sum[i + 1] - sum[l]; int R = sum[r + 1] - sum[i + 1]; int c = 0; long long cost = solve(l, i) + solve(i + 1, r); while (L + R + c) { cost += (L % 10) * (R % 10); if (c + L % 10 + R % 10 >= 10) { c = 1; cost++; } else c = 0; L /= 10; R /= 10; } ret = min(ret, cost); } return dp[l][r] = ret; } int main() { int a; scanf("%d", &a); for (int i = 0; i < a; i++) scanf("%d", b + i); for (int i = 0; i < a; i++) sum[i + 1] = sum[i] + b[i]; for (int i = 0; i < 310; i++) { for (int j = 0; j < 310; j++) { dp[i][j] = -1; } } pow[0] = 1; for (int i = 1; i < 10; i++) pow[i] = pow[i - 1] * 10; printf("%lld\n", solve(0, a - 1)); }
replace
23
24
23
25
TLE
p01764
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; #define INF 131211109876543210LL #define MAX_N 350 #define MAX_K 10 #define MAX_NUM 10 long long wa[MAX_N][MAX_N]; long long dp[MAX_N][MAX_N]; long long x[MAX_N], y[MAX_N]; long long power[MAX_K]; long long a1[MAX_K], a2[MAX_K], a3[MAX_K]; long long N; long long keisan(long long v, long long w) { for (int i = 0; i < MAX_K; i++) { power[i] = 0LL; a1[i] = 0LL; a2[i] = 0LL; a3[i] = 0LL; } power[0] = 1; for (int i = 1; i < MAX_K; i++) { power[i] = power[i - 1] * MAX_NUM; } for (int i = 0; i < MAX_K; i++) { a1[i] = (v / power[i]) % MAX_NUM; } for (int i = 0; i < MAX_K; i++) { a2[i] = (w / power[i]) % MAX_NUM; } long long res1 = 0; for (int i = 0; i < MAX_K; i++) { res1 += a1[i] * a2[i]; } a3[0] = (a1[0] + a2[0]) / MAX_NUM; for (int i = 1; i < MAX_K; i++) { long long a4 = a1[i] + a2[i] + a3[i - 1]; if (a4 >= MAX_NUM) { a3[i] = 1; } else { a3[i] = 0; } } for (int i = 0; i < MAX_K; i++) { res1 += a3[i]; } return res1; } void WA() { y[0] = x[0]; for (int i = 1; i < N; i++) { y[i] = y[i - 1] + x[i]; } for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { wa[i][j] = y[j] - y[i - 1]; } } } int DP() { WA(); for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { dp[i][j] = INF; } } for (int i = 0; i < N; i++) { dp[i][i] = 0; } for (int i = 1; i < N; i++) { for (int j = 0; j <= N - i; j++) { long long a5 = j, a6 = j + i; for (int k = a5; k < a6; k++) { long long a7 = dp[a5][k]; long long a8 = dp[k + 1][a6]; long long a9 = wa[a5][k]; long long a10 = wa[k + 1][a6]; long long a11 = keisan(a9, a10); long long a12 = a7 + a8 + a11; dp[a5][a6] = min(dp[a5][a6], a12); } } } return dp[0][N - 1]; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> x[i]; } cout << DP() << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; #define INF 131211109876543210LL #define MAX_N 301 #define MAX_K 9 #define MAX_NUM 10 long long wa[MAX_N][MAX_N]; long long dp[MAX_N][MAX_N]; long long x[MAX_N], y[MAX_N]; long long power[MAX_K]; long long a1[MAX_K], a2[MAX_K], a3[MAX_K]; long long N; long long keisan(long long v, long long w) { for (int i = 0; i < MAX_K; i++) { power[i] = 0LL; a1[i] = 0LL; a2[i] = 0LL; a3[i] = 0LL; } power[0] = 1; for (int i = 1; i < MAX_K; i++) { power[i] = power[i - 1] * MAX_NUM; } for (int i = 0; i < MAX_K; i++) { a1[i] = (v / power[i]) % MAX_NUM; } for (int i = 0; i < MAX_K; i++) { a2[i] = (w / power[i]) % MAX_NUM; } long long res1 = 0; for (int i = 0; i < MAX_K; i++) { res1 += a1[i] * a2[i]; } a3[0] = (a1[0] + a2[0]) / MAX_NUM; for (int i = 1; i < MAX_K; i++) { long long a4 = a1[i] + a2[i] + a3[i - 1]; if (a4 >= MAX_NUM) { a3[i] = 1; } else { a3[i] = 0; } } for (int i = 0; i < MAX_K; i++) { res1 += a3[i]; } return res1; } void WA() { y[0] = x[0]; for (int i = 1; i < N; i++) { y[i] = y[i - 1] + x[i]; } for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { wa[i][j] = y[j] - y[i - 1]; } } } int DP() { WA(); for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { dp[i][j] = INF; } } for (int i = 0; i < N; i++) { dp[i][i] = 0; } for (int i = 1; i < N; i++) { for (int j = 0; j <= N - i; j++) { long long a5 = j, a6 = j + i; for (int k = a5; k < a6; k++) { long long a7 = dp[a5][k]; long long a8 = dp[k + 1][a6]; long long a9 = wa[a5][k]; long long a10 = wa[k + 1][a6]; long long a11 = keisan(a9, a10); long long a12 = a7 + a8 + a11; dp[a5][a6] = min(dp[a5][a6], a12); } } } return dp[0][N - 1]; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> x[i]; } cout << DP() << endl; return 0; }
replace
5
7
5
7
TLE
p01767
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 300001 typedef long long ll; int main() { ll N, M, a; vector<ll> d(MAX, 0), e(MAX + 1, 0); cin >> N; for (int i = 0; i < N; i++) { cin >> a; d[a]++; } for (int i = 0; i < MAX; i++) { d[i] *= (ll)i; } for (int i = 0; i < MAX; i++) { e[i + 1] = e[i] + d[i + 1]; } cin >> M; vector<ll> b(M); for (int i = 0; i < M; i++) { cin >> a; b[i] = e[a]; } for (int i = 0; i < M; i++) { cin >> a; cout << (b[i] >= a ? "Yes" : "No") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 1000001 typedef long long ll; int main() { ll N, M, a; vector<ll> d(MAX, 0), e(MAX + 1, 0); cin >> N; for (int i = 0; i < N; i++) { cin >> a; d[a]++; } for (int i = 0; i < MAX; i++) { d[i] *= (ll)i; } for (int i = 0; i < MAX; i++) { e[i + 1] = e[i] + d[i + 1]; } cin >> M; vector<ll> b(M); for (int i = 0; i < M; i++) { cin >> a; b[i] = e[a]; } for (int i = 0; i < M; i++) { cin >> a; cout << (b[i] >= a ? "Yes" : "No") << endl; } return 0; }
replace
4
5
4
5
0
p01767
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, b) FOR(i, 0, b) #define PB push_back #define BE(c) c.begin(), c.end() using namespace std; typedef long long LL; typedef LL ut; typedef vector<ut> VI; const int SIZE = 1 + 1e+7; LL getly[SIZE]; VI nums; LL b[SIZE]; int main() { LL N, M, a, c; cin >> N; REP(i, N) { scanf("%lld", &a); nums.PB(a); } sort(BE(nums)); int now = 0; while (now != nums.size() && nums[now] == 0) now++; FOR(i, 1, SIZE) { getly[i] = getly[i - 1]; while (now != nums.size() && nums[now] == i) { getly[i] += (LL)i; now++; } } cin >> M; REP(i, M) scanf("%lld", &b[i]); REP(i, M) { scanf("%lld", &c); if (getly[b[i]] >= c) printf("Yes\n"); else printf("No\n"); } }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, b) FOR(i, 0, b) #define PB push_back #define BE(c) c.begin(), c.end() using namespace std; typedef long long LL; typedef LL ut; typedef vector<ut> VI; const int SIZE = 1 + 1e+6; LL getly[SIZE]; VI nums; LL b[SIZE]; int main() { LL N, M, a, c; cin >> N; REP(i, N) { scanf("%lld", &a); nums.PB(a); } sort(BE(nums)); int now = 0; while (now != nums.size() && nums[now] == 0) now++; FOR(i, 1, SIZE) { getly[i] = getly[i - 1]; while (now != nums.size() && nums[now] == i) { getly[i] += (LL)i; now++; } } cin >> M; REP(i, M) scanf("%lld", &b[i]); REP(i, M) { scanf("%lld", &c); if (getly[b[i]] >= c) printf("Yes\n"); else printf("No\n"); } }
replace
9
10
9
10
MLE