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
p00603
C++
Time Limit Exceeded
#include <algorithm> #include <queue> #include <stdio.h> using namespace std; int now[60]; int main() { int a, b; while (~scanf("%d%d", &a, &b), a) { queue<int> A; queue<int> B; queue<int> C; for (int i = 0; i < a; i++) now[i] = i; while (b--) { int c; scanf("%d", &c); for (int i = 0; i < a / 2; i++) B.push(now[i]); for (int i = a / 2; i < a; i++) A.push(now[i]); while (C.size() != a) { for (int i = 0; i < c; i++) if (A.size()) { C.push(A.front()); A.pop(); } for (int i = 0; i < c; i++) if (B.size()) { C.push(B.front()); B.pop(); } } for (int i = 0; i < a; i++) { now[i] = C.front(); C.pop(); } } printf("%d\n", now[a - 1]); } }
#include <algorithm> #include <queue> #include <stdio.h> using namespace std; int now[60]; int main() { int a, b; while (~scanf("%d%d", &a, &b)) { queue<int> A; queue<int> B; queue<int> C; for (int i = 0; i < a; i++) now[i] = i; while (b--) { int c; scanf("%d", &c); for (int i = 0; i < a / 2; i++) B.push(now[i]); for (int i = a / 2; i < a; i++) A.push(now[i]); while (C.size() != a) { for (int i = 0; i < c; i++) if (A.size()) { C.push(A.front()); A.pop(); } for (int i = 0; i < c; i++) if (B.size()) { C.push(B.front()); B.pop(); } } for (int i = 0; i < a; i++) { now[i] = C.front(); C.pop(); } } printf("%d\n", now[a - 1]); } }
replace
7
8
7
8
TLE
p00603
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<int> RiffleShuffle(int, vector<int>); int main() { int i, n, r, c; vector<int> data; while (cin >> n >> r) { for (i = 0; i < n; ++i) data.push_back(i); for (i = 0; i < r; ++i) { cin >> c; data = RiffleShuffle(c, data); } cout << data[data.size() - 1] << endl; data.clear(); } return 0; } vector<int> RiffleShuffle(int c, vector<int> data) { vector<int> A, B, C; copy(data.begin() + data.size() / 2, data.end(), back_inserter(A)); copy(data.begin(), data.begin() + data.size() / 2, back_inserter(B)); copy(A.begin(), A.begin() + c, back_inserter(C)); A.erase(A.begin(), A.begin() + c); copy(B.begin(), B.begin() + c, back_inserter(C)); B.erase(B.begin(), B.begin() + c); copy(A.begin(), A.end(), back_inserter(C)); copy(B.begin(), B.end(), back_inserter(C)); return C; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<int> RiffleShuffle(int, vector<int>); int main() { int i, n, r, c; vector<int> data; while (cin >> n >> r) { for (i = 0; i < n; ++i) data.push_back(i); for (i = 0; i < r; ++i) { cin >> c; data = RiffleShuffle(c, data); } cout << data[data.size() - 1] << endl; data.clear(); } return 0; } vector<int> RiffleShuffle(int c, vector<int> data) { vector<int> A, B, C; copy(data.begin() + data.size() / 2, data.end(), back_inserter(A)); copy(data.begin(), data.begin() + data.size() / 2, back_inserter(B)); while (A.size() > c || B.size() > c) { copy(A.begin(), A.begin() + c, back_inserter(C)); A.erase(A.begin(), A.begin() + c); copy(B.begin(), B.begin() + c, back_inserter(C)); B.erase(B.begin(), B.begin() + c); } copy(A.begin(), A.end(), back_inserter(C)); copy(B.begin(), B.end(), back_inserter(C)); return C; }
replace
34
38
34
40
0
p00604
C++
Memory Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; vector<int> time(n); while (cin >> n) { time.clear(); for (int i = 0; i < n; i++) { int t; cin >> t; time.push_back(t); } sort(time.begin(), time.end()); int sum = 0; for (int i = 0; i < n; i++) { sum += time[i] * (n - i); } cout << sum << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; vector<int> time; while (cin >> n) { time.clear(); for (int i = 0; i < n; i++) { int t; cin >> t; time.push_back(t); } sort(time.begin(), time.end()); int sum = 0; for (int i = 0; i < n; i++) { sum += time[i] * (n - i); } cout << sum << endl; } return 0; }
replace
7
8
7
8
MLE
p00605
C++
Runtime Error
#include <iostream> using namespace std; int main() { int N, K; while (cin >> N >> K, N || K) { int s[N]; for (int i = 0; i < K; i++) { cin >> s[i]; } int temp; bool flag = true; for (int j = 0; j < N; j++) { for (int i = 0; i < K; i++) { cin >> temp; s[i] -= temp; if (s[i] < 0) flag = false; } } if (flag) cout << "Yes" << endl; else cout << "No" << endl; } }
#include <iostream> using namespace std; int main() { int N, K; while (cin >> N >> K, N || K) { int s[K]; for (int i = 0; i < K; i++) { cin >> s[i]; } int temp; bool flag = true; for (int j = 0; j < N; j++) { for (int i = 0; i < K; i++) { cin >> temp; s[i] -= temp; if (s[i] < 0) flag = false; } } if (flag) cout << "Yes" << endl; else cout << "No" << endl; } }
replace
6
7
6
7
0
p00605
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pb(n) push_back(n) #define mp(n, m) make_pair(n, m) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define REP(i, s, e) for (int i = (s); i < (e); i++) #define rep(i, n) REP(i, 0, n) #define REPE(i, s, e) for (int i = s; i > e; i--) #define repe(i, n) REPE(i, n, -1) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<vi> vvi; const int IMAX = ((1 << 30) - 1) * 2 + 1; const int INF = 100000000; const double EPS = 1e-10; // int mod=1000000007 const int MAX_V = 10000; int main() { int n, m; while (cin >> n >> m) { if (n == 0 && m == 0) break; vi s(n, 0); rep(i, m) cin >> s[i]; int t; rep(i, n) { rep(j, m) { cin >> t; s[j] -= t; } } sort(all(s)); cout << (s[0] < 0 ? "No" : "Yes") << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pb(n) push_back(n) #define mp(n, m) make_pair(n, m) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define REP(i, s, e) for (int i = (s); i < (e); i++) #define rep(i, n) REP(i, 0, n) #define REPE(i, s, e) for (int i = s; i > e; i--) #define repe(i, n) REPE(i, n, -1) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<vi> vvi; const int IMAX = ((1 << 30) - 1) * 2 + 1; const int INF = 100000000; const double EPS = 1e-10; // int mod=1000000007 const int MAX_V = 10000; int main() { int n, m; while (cin >> n >> m) { if (n == 0 && m == 0) break; vi s(m, 0); rep(i, m) cin >> s[i]; int t; rep(i, n) { rep(j, m) { cin >> t; s[j] -= t; } } sort(all(s)); cout << (s[0] < 0 ? "No" : "Yes") << endl; } }
replace
49
50
49
50
0
p00605
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { while (true) { int N, K; string ans = "Yes"; cin >> N >> K; int S[N]; if (N == 0 && K == 0) break; for (int i = 0; i < K; ++i) { cin >> S[i]; } for (int i = 0; i < N; ++i) { for (int j = 0; j < K; ++j) { int B; cin >> B; S[j] -= B; } } for (int i = 0; i < K; ++i) { if (S[i] < 0) { ans = "No"; } } cout << ans << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { while (true) { int N, K; string ans = "Yes"; cin >> N >> K; int S[K]; if (N == 0 && K == 0) break; for (int i = 0; i < K; ++i) { cin >> S[i]; } for (int i = 0; i < N; ++i) { for (int j = 0; j < K; ++j) { int B; cin >> B; S[j] -= B; } } for (int i = 0; i < K; ++i) { if (S[i] < 0) { ans = "No"; } } cout << ans << endl; } return 0; }
replace
10
11
10
11
0
p00605
C++
Runtime Error
#include <iostream> using namespace std; int n, m, x, s[100]; int main() { while (cin >> n >> m, n + m) { for (int i = 0; i < m; i++) cin >> s[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> x, s[x]--; } int f = 1; for (int i = 0; i < m; i++) { if (s[i] < 0) f = 0; } cout << (f ? "Yes" : "No") << endl; } }
#include <iostream> using namespace std; int n, m, x, s[100]; int main() { while (cin >> n >> m, n + m) { for (int i = 0; i < m; i++) cin >> s[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> x, s[j] -= x; } int f = 1; for (int i = 0; i < m; i++) { if (s[i] < 0) f = 0; } cout << (f ? "Yes" : "No") << endl; } }
replace
9
10
9
10
0
p00606
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100000000; string m = "012345678"; P getP(char c) { int p = m[c - 'A'] - '0'; return MP(p % 3, p / 3); } int dx[] = {0, 0, 1, -1}; int dy[] = {-1, 1, 0, 0}; int main() { while (1) { int n; char s, t, b; cin >> n; if (!n) break; cin >> s >> t >> b; P sp = getP(s); P tp = getP(t); P bp = getP(b); double p[15][3][3] = {}; p[0][sp.Y][sp.X] = 1.0; for (int i = 1; i <= n; i++) { rep(j, 3) { rep(k, 3) { if (MP(k, j) == bp) continue; int wallcnt = 0; rep(l, 4) { int x = k + dx[l]; int y = j + dy[l]; if (x < 0 || x > 2 || y < 0 || y > 2 || MP(x, y) == bp) { wallcnt++; continue; } p[i][j][k] += p[i - 1][y][x] / 4; } p[i][j][k] += p[i - 1][j][k] * wallcnt / 4; } } } printf("%.8lf\n", p[n][tp.Y][tp.X]); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100000000; string m = "012345678"; P getP(char c) { int p = m[c - 'A'] - '0'; return MP(p % 3, p / 3); } int dx[] = {0, 0, 1, -1}; int dy[] = {-1, 1, 0, 0}; int main() { while (1) { int n; char s, t, b; cin >> n; if (!n) break; cin >> s >> t >> b; P sp = getP(s); P tp = getP(t); P bp = getP(b); double p[16][3][3] = {}; p[0][sp.Y][sp.X] = 1.0; for (int i = 1; i <= n; i++) { rep(j, 3) { rep(k, 3) { if (MP(k, j) == bp) continue; int wallcnt = 0; rep(l, 4) { int x = k + dx[l]; int y = j + dy[l]; if (x < 0 || x > 2 || y < 0 || y > 2 || MP(x, y) == bp) { wallcnt++; continue; } p[i][j][k] += p[i - 1][y][x] / 4; } p[i][j][k] += p[i - 1][j][k] * wallcnt / 4; } } } printf("%.8lf\n", p[n][tp.Y][tp.X]); } return 0; }
replace
48
49
48
49
0
p00606
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, j) FOR(i, 0, j) #define mp std::make_pair typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> P; typedef std::pair<int, P> State; const int INF = 1001001001; // S N E W(南北東西) const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; double dp[3][3][16]; int n, s, t, b; int toIndex(char l) { return l - 'A'; } double rec(int x, int y, int battery) { if (dp[y][x][battery] > -1e-6) { return dp[y][x][battery]; } if (x == t % 3 && y == t / 3 && battery == n) { return 1.0; } if (battery >= n) { return 0.0; } double res = 0.0; REP(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 && !(nx == b % 3 && ny == b / 3)) { res += 0.25 * rec(nx, ny, battery + 1); } else { res += 0.25 * rec(x, y, battery + 1); } } return res; } int main() { while (std::cin >> n, n) { std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0); char sl, tl, bl; std::cin >> sl >> tl >> bl; s = toIndex(sl), t = toIndex(tl), b = toIndex(bl); printf("%.7f\n", rec(s % 3, s / 3, 0)); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, j) FOR(i, 0, j) #define mp std::make_pair typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> P; typedef std::pair<int, P> State; const int INF = 1001001001; // S N E W(南北東西) const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; double dp[3][3][16]; int n, s, t, b; int toIndex(char l) { return l - 'A'; } double rec(int x, int y, int battery) { if (dp[y][x][battery] > -1e-6) { return dp[y][x][battery]; } if (x == t % 3 && y == t / 3 && battery == n) { return 1.0; } if (battery >= n) { return 0.0; } double res = 0.0; REP(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 && !(nx == b % 3 && ny == b / 3)) { res += 0.25 * rec(nx, ny, battery + 1); } else { res += 0.25 * rec(x, y, battery + 1); } } return dp[y][x][battery] = res; } int main() { while (std::cin >> n, n) { std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0); char sl, tl, bl; std::cin >> sl >> tl >> bl; s = toIndex(sl), t = toIndex(tl), b = toIndex(bl); printf("%.7f\n", rec(s % 3, s / 3, 0)); } }
replace
52
53
52
53
TLE
p00608
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cstdint> #include <cstdio> #include <string> #include <vector> #define fprintf(...) (void)0 int parse_int(std::string &s, size_t &i) { assert(isdigit(s[i])); int res = s[i++] - '0'; while (isdigit(s[i])) { res = res * 10 + s[i++] - '0'; } return res; } int parse(std::string &s, size_t &i, bool &succ) { if (i == s.length()) { succ = false; return -1; } if (s[i] == '+' || s[i] == '-') { succ = false; return -1; } int res = 0; char sgn = '+'; while (i < s.length()) { assert(isdigit(s[i])); int cur = parse_int(s, i); while (true) { if (s[i] == '*') { cur *= parse_int(s, ++i); } else if (s[i] == '/') { int tmp = parse_int(s, ++i); if (cur % tmp != 0) { succ = false; return -1; } cur /= tmp; } else { break; } } if (sgn == '+') { res += cur; } else if (sgn == '-') { res -= cur; } if (i == s.length() || s[i] == '=') return res; sgn = s[i++]; } return res; } bool solved(const std::vector<std::string> &eqs, const std::vector<char> &ch) { fprintf(stderr, ": "); for (size_t i = 0; i < ch.size(); ++i) fprintf(stderr, "%c%c", ch[i], i + 1 < ch.size() ? ' ' : '\n'); for (auto eq : eqs) { size_t i = 0; for (char &c : eq) if (c >= 'A' && c <= 'Z') c = ch[c - 'A']; fprintf(stderr, "# %s\n", eq.c_str()); if (eq[0] == '=' || eq.back() == '=') return false; if (!isdigit(eq[0]) || !isdigit(eq.back())) return false; for (size_t i = 0; i + 1 < eq.length(); ++i) { if (isdigit(eq[i])) continue; if (!isdigit(eq[i + 1])) return false; } for (size_t i = 0; i < eq.length(); ++i) { if (!isdigit(eq[i])) continue; if (eq[i] == '0') { if (i + 1 < eq.length() && isdigit(eq[i + 1])) return false; } while (i + 1 < eq.length() && isdigit(eq[i + 1])) { ++i; } } if (std::count(eq.begin(), eq.end(), '=') != 1) return false; bool succ = true; int lhs = parse(eq, i, succ); if (!succ) return false; assert(eq[i] == '='); ++i; succ = true; int rhs = parse(eq, i, succ); if (!succ) return false; fprintf(stderr, "%s: %d %d\n", eq.c_str(), lhs, rhs); if (lhs != rhs) return false; } return true; } int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; std::vector<std::string> g(H); for (size_t i = 0; i < H; ++i) { char buf[16]; scanf("%s", buf); g[i] = buf; } size_t n; scanf("%zu", &n); std::vector<char> ch(n); for (size_t i = 0; i < n; ++i) { scanf(" %c", &ch[i]); } char ph = 'A'; for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) if (g[i][j] == '.') g[i][j] = ph++; std::vector<std::string> eqs; for (size_t i = 0; i < H; ++i) { std::string eq = ""; for (size_t j = 0; j < W; ++j) { if (g[i][j] == '#') { if (eq.size() >= 3) { eqs.emplace_back(eq); } eq = ""; continue; } eq += g[i][j]; } if (eq.size() >= 3) eqs.emplace_back(eq); } for (size_t j = 0; j < W; ++j) { std::string eq = ""; for (size_t i = 0; i < H; ++i) { if (g[i][j] == '#') { if (eq.size() >= 3) { eqs.emplace_back(eq); } eq = ""; continue; } eq += g[i][j]; } if (eq.size() >= 3) eqs.emplace_back(eq); } std::sort(ch.begin(), ch.end()); do { if (solved(eqs, ch)) { printf("Yes\n"); return 0; } } while (std::next_permutation(ch.begin(), ch.end())); printf("No\n"); return 0; } int main() { while (!testcase_ends()) { } }
#include <algorithm> #include <cassert> #include <cctype> #include <cstdint> #include <cstdio> #include <string> #include <vector> #define fprintf(...) (void)0 int parse_int(std::string &s, size_t &i) { assert(isdigit(s[i])); int res = s[i++] - '0'; while (isdigit(s[i])) { res = res * 10 + s[i++] - '0'; } return res; } int parse(std::string &s, size_t &i, bool &succ) { if (i == s.length()) { succ = false; return -1; } if (s[i] == '+' || s[i] == '-') { succ = false; return -1; } int res = 0; char sgn = '+'; while (i < s.length()) { assert(isdigit(s[i])); int cur = parse_int(s, i); while (true) { if (s[i] == '*') { cur *= parse_int(s, ++i); } else if (s[i] == '/') { int tmp = parse_int(s, ++i); if (tmp == 0 || cur % tmp != 0) { succ = false; return -1; } cur /= tmp; } else { break; } } if (sgn == '+') { res += cur; } else if (sgn == '-') { res -= cur; } if (i == s.length() || s[i] == '=') return res; sgn = s[i++]; } return res; } bool solved(const std::vector<std::string> &eqs, const std::vector<char> &ch) { fprintf(stderr, ": "); for (size_t i = 0; i < ch.size(); ++i) fprintf(stderr, "%c%c", ch[i], i + 1 < ch.size() ? ' ' : '\n'); for (auto eq : eqs) { size_t i = 0; for (char &c : eq) if (c >= 'A' && c <= 'Z') c = ch[c - 'A']; fprintf(stderr, "# %s\n", eq.c_str()); if (eq[0] == '=' || eq.back() == '=') return false; if (!isdigit(eq[0]) || !isdigit(eq.back())) return false; for (size_t i = 0; i + 1 < eq.length(); ++i) { if (isdigit(eq[i])) continue; if (!isdigit(eq[i + 1])) return false; } for (size_t i = 0; i < eq.length(); ++i) { if (!isdigit(eq[i])) continue; if (eq[i] == '0') { if (i + 1 < eq.length() && isdigit(eq[i + 1])) return false; } while (i + 1 < eq.length() && isdigit(eq[i + 1])) { ++i; } } if (std::count(eq.begin(), eq.end(), '=') != 1) return false; bool succ = true; int lhs = parse(eq, i, succ); if (!succ) return false; assert(eq[i] == '='); ++i; succ = true; int rhs = parse(eq, i, succ); if (!succ) return false; fprintf(stderr, "%s: %d %d\n", eq.c_str(), lhs, rhs); if (lhs != rhs) return false; } return true; } int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; std::vector<std::string> g(H); for (size_t i = 0; i < H; ++i) { char buf[16]; scanf("%s", buf); g[i] = buf; } size_t n; scanf("%zu", &n); std::vector<char> ch(n); for (size_t i = 0; i < n; ++i) { scanf(" %c", &ch[i]); } char ph = 'A'; for (size_t i = 0; i < H; ++i) for (size_t j = 0; j < W; ++j) if (g[i][j] == '.') g[i][j] = ph++; std::vector<std::string> eqs; for (size_t i = 0; i < H; ++i) { std::string eq = ""; for (size_t j = 0; j < W; ++j) { if (g[i][j] == '#') { if (eq.size() >= 3) { eqs.emplace_back(eq); } eq = ""; continue; } eq += g[i][j]; } if (eq.size() >= 3) eqs.emplace_back(eq); } for (size_t j = 0; j < W; ++j) { std::string eq = ""; for (size_t i = 0; i < H; ++i) { if (g[i][j] == '#') { if (eq.size() >= 3) { eqs.emplace_back(eq); } eq = ""; continue; } eq += g[i][j]; } if (eq.size() >= 3) eqs.emplace_back(eq); } std::sort(ch.begin(), ch.end()); do { if (solved(eqs, ch)) { printf("Yes\n"); return 0; } } while (std::next_permutation(ch.begin(), ch.end())); printf("No\n"); return 0; } int main() { while (!testcase_ends()) { } }
replace
41
42
41
42
0
p00608
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; const ll inf = 1e15; int h, w, n; string g[10]; char c[10]; bool use[10]; int l[10][10], r[10][10], u[10][10], d[10][10]; inline bool valid(const string &s) { rep(i, s.size()) { if (s[i] == '0' && (i == 0 || !isdigit(s[i - 1])) && (i + 1 < (int)s.size() && isdigit(s[i + 1]))) { return false; } if (i > 0 && !isdigit(s[i]) && !isdigit(s[i - 1])) { return false; } } return true; } ll parse(int L, int R, const string &s) { assert(R != L); for (int i = R - 1; i >= L; i--) { if (s[i] == '+' || s[i] == '-') { ll a = parse(L, i, s), b = parse(i + 1, R, s); if (a == inf || b == inf) return inf; if (s[i] == '+') return a + b; if (s[i] == '-') return a - b; } } for (int i = R - 1; i >= L; i--) { if (s[i] == '*' || s[i] == '/') { ll a = parse(L, i, s), b = parse(i + 1, R, s); if (a == inf || b == inf) return inf; if (s[i] == '*') return a * b; if (s[i] == '/') { if (b == 0 || a % b) return inf; return a / b; } } } ll res = 0; for (int i = L; i < R; i++) res = res * 10LL + (ll)(s[i] - '0'); return res; } inline bool check(const string &s) { if (!valid(s)) return false; int p = 0; while (s[p] != '=') p++; assert(p != (int)s.size()); ll a = parse(0, p, s); ll b = parse(p + 1, s.size(), s); if (a == inf || b == inf) return false; return a == b; } bool solve(int y, int x) { if (y == h) return true; int ny = y, nx = x + 1; if (nx == w) ny++, nx = 0; if (g[y][x] != '.') return solve(ny, nx); rep(i, n) { if (!use[i]) { use[i] = 1; g[y][x] = c[i]; bool f = true; if (l[y][x] >= 0) { string s; for (int j = l[y][x]; j < r[y][x]; j++) s += g[y][j]; if (!check(s)) f = false; } if (f && u[y][x] >= 0) { string s; for (int j = u[y][x]; j < d[y][x]; j++) s += g[j][x]; if (!check(s)) f = false; } if (f && solve(ny, nx)) return true; g[y][x] = '.'; use[i] = 0; } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (cin >> h >> w, h) { rep(i, h) cin >> g[i]; cin >> n; rep(i, n) cin >> c[i]; bool f = true; rep(i, h) rep(j, w) { if (j == 0 || g[i][j - 1] == '#') { string tmp; rep(k, w) { if (g[i][k] == '#') break; tmp += g[i][k]; } if (tmp.size() >= 3 && tmp.find('.') == string::npos && !check(tmp)) f = false; } if (i == 0 || g[i - 1][j] == '#') { string tmp; rep(k, h) { if (g[k][j] == '#') break; tmp += g[k][j]; } if (tmp.size() >= 3 && tmp.find('.') == string::npos && !check(tmp)) f = false; } } if (!f) { cout << "No" << endl; continue; } memset(l, -1, sizeof(l)); memset(r, -1, sizeof(r)); memset(u, -1, sizeof(u)); memset(d, -1, sizeof(d)); rep(i, h) rep(j, w) { if (g[i][j] == '.') { int cnt = 0; for (int k = j + 1; k < w; k++) { if (g[i][k] == '.') cnt++; if (g[i][k] == '#') break; } if (cnt == 0) { l[i][j] = 0; r[i][j] = w; for (int k = j - 1; k >= 0; k--) { if (g[i][k] == '#') { l[i][j] = k + 1; break; } } for (int k = j + 1; k < w; k++) { if (g[i][k] == '#') { r[i][j] = k; break; } } if (r[i][j] - l[i][j] < 3) r[i][j] = l[i][j] = -1; } cnt = 0; for (int k = i + 1; k < h; k++) { if (g[k][j] == '.') cnt++; if (g[k][j] == '#') break; } if (cnt == 0) { u[i][j] = 0; d[i][j] = h; for (int k = i - 1; k >= 0; k--) { if (g[k][j] == '#') { u[i][j] = k + 1; break; } } for (int k = i + 1; k < h; k++) { if (g[k][j] == '#') { d[i][j] = k; break; } } if (d[i][j] - u[i][j] < 3) d[i][j] = u[i][j] = -1; } } } memset(use, 0, sizeof(use)); if (solve(0, 0)) cout << "Yes" << endl; else cout << "No" << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; const ll inf = 1e15; int h, w, n; string g[10]; char c[10]; bool use[10]; int l[10][10], r[10][10], u[10][10], d[10][10]; inline bool valid(const string &s) { rep(i, s.size()) { if (s[i] == '0' && (i == 0 || !isdigit(s[i - 1])) && (i + 1 < (int)s.size() && isdigit(s[i + 1]))) { return false; } if (i > 0 && !isdigit(s[i]) && !isdigit(s[i - 1])) { return false; } } if (!isdigit(s[0]) || !isdigit(s.back())) return false; return true; } ll parse(int L, int R, const string &s) { assert(R != L); for (int i = R - 1; i >= L; i--) { if (s[i] == '+' || s[i] == '-') { ll a = parse(L, i, s), b = parse(i + 1, R, s); if (a == inf || b == inf) return inf; if (s[i] == '+') return a + b; if (s[i] == '-') return a - b; } } for (int i = R - 1; i >= L; i--) { if (s[i] == '*' || s[i] == '/') { ll a = parse(L, i, s), b = parse(i + 1, R, s); if (a == inf || b == inf) return inf; if (s[i] == '*') return a * b; if (s[i] == '/') { if (b == 0 || a % b) return inf; return a / b; } } } ll res = 0; for (int i = L; i < R; i++) res = res * 10LL + (ll)(s[i] - '0'); return res; } inline bool check(const string &s) { if (!valid(s)) return false; int p = 0; while (s[p] != '=') p++; assert(p != (int)s.size()); ll a = parse(0, p, s); ll b = parse(p + 1, s.size(), s); if (a == inf || b == inf) return false; return a == b; } bool solve(int y, int x) { if (y == h) return true; int ny = y, nx = x + 1; if (nx == w) ny++, nx = 0; if (g[y][x] != '.') return solve(ny, nx); rep(i, n) { if (!use[i]) { use[i] = 1; g[y][x] = c[i]; bool f = true; if (l[y][x] >= 0) { string s; for (int j = l[y][x]; j < r[y][x]; j++) s += g[y][j]; if (!check(s)) f = false; } if (f && u[y][x] >= 0) { string s; for (int j = u[y][x]; j < d[y][x]; j++) s += g[j][x]; if (!check(s)) f = false; } if (f && solve(ny, nx)) return true; g[y][x] = '.'; use[i] = 0; } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(0); while (cin >> h >> w, h) { rep(i, h) cin >> g[i]; cin >> n; rep(i, n) cin >> c[i]; bool f = true; rep(i, h) rep(j, w) { if (j == 0 || g[i][j - 1] == '#') { string tmp; rep(k, w) { if (g[i][k] == '#') break; tmp += g[i][k]; } if (tmp.size() >= 3 && tmp.find('.') == string::npos && !check(tmp)) f = false; } if (i == 0 || g[i - 1][j] == '#') { string tmp; rep(k, h) { if (g[k][j] == '#') break; tmp += g[k][j]; } if (tmp.size() >= 3 && tmp.find('.') == string::npos && !check(tmp)) f = false; } } if (!f) { cout << "No" << endl; continue; } memset(l, -1, sizeof(l)); memset(r, -1, sizeof(r)); memset(u, -1, sizeof(u)); memset(d, -1, sizeof(d)); rep(i, h) rep(j, w) { if (g[i][j] == '.') { int cnt = 0; for (int k = j + 1; k < w; k++) { if (g[i][k] == '.') cnt++; if (g[i][k] == '#') break; } if (cnt == 0) { l[i][j] = 0; r[i][j] = w; for (int k = j - 1; k >= 0; k--) { if (g[i][k] == '#') { l[i][j] = k + 1; break; } } for (int k = j + 1; k < w; k++) { if (g[i][k] == '#') { r[i][j] = k; break; } } if (r[i][j] - l[i][j] < 3) r[i][j] = l[i][j] = -1; } cnt = 0; for (int k = i + 1; k < h; k++) { if (g[k][j] == '.') cnt++; if (g[k][j] == '#') break; } if (cnt == 0) { u[i][j] = 0; d[i][j] = h; for (int k = i - 1; k >= 0; k--) { if (g[k][j] == '#') { u[i][j] = k + 1; break; } } for (int k = i + 1; k < h; k++) { if (g[k][j] == '#') { d[i][j] = k; break; } } if (d[i][j] - u[i][j] < 3) d[i][j] = u[i][j] = -1; } } } memset(use, 0, sizeof(use)); if (solve(0, 0)) cout << "Yes" << endl; else cout << "No" << endl; } }
insert
23
23
23
25
0
p00608
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cstdint> #include <cstdio> #include <string> #include <vector> constexpr int tsurai = 123456789; int parse_int(const std::string &s, size_t &i) { assert(isdigit(s[i])); int res = s[i] - '0'; while (++i < s.length() && isdigit(s[i])) { res = res * 10 + s[i] - '0'; } return res; } int fourt(const std::string &s, size_t &i) { int res = 0; char sgn = '+'; while (i < s.length() && isdigit(s[i])) { int cur = parse_int(s, i); while (i < s.length() && s[i] != '=') { if (s[i] == '*') { cur *= parse_int(s, ++i); } else if (s[i] == '/') { int tmp = parse_int(s, ++i); if (tmp == 0 || cur % tmp) return tsurai; cur /= tmp; } else { break; } } if (sgn == '+') { res += cur; } else { res -= cur; } if (i < s.length() && s[i] != '=') { sgn = s[i++]; } } return res; } bool valid(const std::vector<std::string> &eqs, const std::vector<char> &cs) { for (/* not const */ std::string eq : eqs) { for (char &ch : eq) if (ch >= 'A' && ch <= 'Z') ch = cs[ch - 'A']; if (eq[0] == '=' || eq.back() == '=') return false; if (std::count(eq.begin(), eq.end(), '=') != 1) return false; for (size_t i = 0; i + 1 < eq.length(); ++i) { if (isdigit(eq[i])) continue; if (!isdigit(eq[i + 1])) return false; } for (size_t i = 0; i < eq.length(); ++i) { if (!isdigit(eq[i])) continue; if (eq[i] != '0') { do { ++i; } while (i < eq.length() && isdigit(eq[i])); } else { if (i + 1 < eq.length() && isdigit(eq[i + 1])) return false; } } size_t i = 0; if (!isdigit(eq[i])) return false; int lhs = fourt(eq, i); if (lhs == tsurai) return false; assert(eq[i] == '='); ++i; if (!isdigit(eq[i])) return false; int rhs = fourt(eq, i); if (rhs == tsurai) return false; assert(i == eq.length()); if (lhs != rhs) return false; } return true; } int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; size_t ph = 'A'; // placeholder std::vector<std::string> s(H); for (size_t i = 0; i < H; ++i) { char buf[16]; scanf("%s", buf); s[i] = buf; for (size_t j = 0; j < W; ++j) { if (s[i][j] == '.') s[i][j] = ph++; } } std::vector<std::string> eqs; for (size_t i = 0; i < H; ++i) { std::string eq = ""; for (size_t j = 0; j < W; ++j) { if (s[i][j] != '#') { eq += s[i][j]; } else { if (eq.size() >= 3) eqs.emplace_back(eq); eq = ""; } } if (eq.size() >= 3) eqs.emplace_back(eq); } for (size_t j = 0; j < W; ++j) { std::string eq = ""; for (size_t i = 0; i < H; ++i) { if (s[i][j] != '#') { eq += s[i][j]; } else { if (eq.size() >= 3) eqs.emplace_back(eq); eq = ""; } } if (eq.size() >= 3) eqs.emplace_back(eq); } size_t n; scanf("%zu", &n); std::vector<char> cs(n); for (size_t i = 0; i < n; ++i) scanf(" %c", &cs[i]); std::sort(cs.begin(), cs.end()); do { if (valid(eqs, cs)) return !printf("Yes\n"); } while (std::next_permutation(cs.begin(), cs.end())); printf("No\n"); return 0; } int main() { while (!testcase_ends()) { } }
#include <algorithm> #include <cassert> #include <cctype> #include <cstdint> #include <cstdio> #include <string> #include <vector> constexpr int tsurai = 123456789; int parse_int(const std::string &s, size_t &i) { assert(isdigit(s[i])); int res = s[i] - '0'; while (++i < s.length() && isdigit(s[i])) { res = res * 10 + s[i] - '0'; } return res; } int fourt(const std::string &s, size_t &i) { int res = 0; char sgn = '+'; while (i < s.length() && isdigit(s[i])) { int cur = parse_int(s, i); while (i < s.length() && s[i] != '=') { if (s[i] == '*') { cur *= parse_int(s, ++i); } else if (s[i] == '/') { int tmp = parse_int(s, ++i); if (tmp == 0 || cur % tmp) return tsurai; cur /= tmp; } else { break; } } if (sgn == '+') { res += cur; } else { res -= cur; } if (i < s.length() && s[i] != '=') { sgn = s[i++]; } } return res; } bool valid(const std::vector<std::string> &eqs, const std::vector<char> &cs) { for (/* not const */ std::string eq : eqs) { for (char &ch : eq) if (ch >= 'A' && ch <= 'Z') ch = cs[ch - 'A']; if (eq[0] == '=' || !isdigit(eq.back())) return false; if (std::count(eq.begin(), eq.end(), '=') != 1) return false; for (size_t i = 0; i + 1 < eq.length(); ++i) { if (isdigit(eq[i])) continue; if (!isdigit(eq[i + 1])) return false; } for (size_t i = 0; i < eq.length(); ++i) { if (!isdigit(eq[i])) continue; if (eq[i] != '0') { do { ++i; } while (i < eq.length() && isdigit(eq[i])); } else { if (i + 1 < eq.length() && isdigit(eq[i + 1])) return false; } } size_t i = 0; if (!isdigit(eq[i])) return false; int lhs = fourt(eq, i); if (lhs == tsurai) return false; assert(eq[i] == '='); ++i; if (!isdigit(eq[i])) return false; int rhs = fourt(eq, i); if (rhs == tsurai) return false; assert(i == eq.length()); if (lhs != rhs) return false; } return true; } int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; size_t ph = 'A'; // placeholder std::vector<std::string> s(H); for (size_t i = 0; i < H; ++i) { char buf[16]; scanf("%s", buf); s[i] = buf; for (size_t j = 0; j < W; ++j) { if (s[i][j] == '.') s[i][j] = ph++; } } std::vector<std::string> eqs; for (size_t i = 0; i < H; ++i) { std::string eq = ""; for (size_t j = 0; j < W; ++j) { if (s[i][j] != '#') { eq += s[i][j]; } else { if (eq.size() >= 3) eqs.emplace_back(eq); eq = ""; } } if (eq.size() >= 3) eqs.emplace_back(eq); } for (size_t j = 0; j < W; ++j) { std::string eq = ""; for (size_t i = 0; i < H; ++i) { if (s[i][j] != '#') { eq += s[i][j]; } else { if (eq.size() >= 3) eqs.emplace_back(eq); eq = ""; } } if (eq.size() >= 3) eqs.emplace_back(eq); } size_t n; scanf("%zu", &n); std::vector<char> cs(n); for (size_t i = 0; i < n; ++i) scanf(" %c", &cs[i]); std::sort(cs.begin(), cs.end()); do { if (valid(eqs, cs)) return !printf("Yes\n"); } while (std::next_permutation(cs.begin(), cs.end())); printf("No\n"); return 0; } int main() { while (!testcase_ends()) { } }
replace
54
55
54
55
0
p00609
C++
Time Limit Exceeded
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #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) < (int)(n); ++(i)) #define foreach(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) const int N = 100002, kUnit = 250, kB = 10000 / kUnit; int AN, BN, R; int xa[N], ya[N], xb[N], yb[N]; typedef pair<int, int> P; vector<P> sep[kB + 2][kB + 2]; bool input() { scanf("%d%d%d", &AN, &BN, &R); if (AN == 0) return false; rep(i, AN) scanf("%d%d", xa + i, ya + i); rep(i, BN) scanf("%d%d", xb + i, yb + i); return true; } int solve() { // バケット初期化 rep(i, kB) rep(j, kB) sep[i][j].clear(); // 弾をバケットに放り込む rep(i, BN) { int x = xb[i] / kUnit, y = yb[i] / kUnit; sep[y][x].push_back(P(xb[i], yb[i])); } int ans = 0; rep(i, AN) { int x = xa[i], y = ya[i]; int ix = x / kUnit, iy = y / kUnit; for (int dy = -1; dy <= 1; ++dy) { for (int dx = -1; dx <= 1; ++dx) { int tix = ix + dx, tiy = iy + dy; if (0 > tix || 0 > tiy || tix >= kB || tiy >= kB) continue; // 同じバケット内の弾と比較 rep(j, sep[tiy][tix].size()) { int dist_x = sep[tiy][tix][j].first - x, dist_y = sep[tiy][tix][j].second - y; int dist_pow = (dist_x * dist_x) + (dist_y * dist_y); if (16 * R * R >= dist_pow) ++ans; } } } } return ans; } int main() { while (input()) { printf("%d\n", solve()); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <numeric> #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) < (int)(n); ++(i)) #define foreach(c, i) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) const int N = 100002, kUnit = 50, kB = 10000 / kUnit; int AN, BN, R; int xa[N], ya[N], xb[N], yb[N]; typedef pair<int, int> P; vector<P> sep[kB + 2][kB + 2]; bool input() { scanf("%d%d%d", &AN, &BN, &R); if (AN == 0) return false; rep(i, AN) scanf("%d%d", xa + i, ya + i); rep(i, BN) scanf("%d%d", xb + i, yb + i); return true; } int solve() { // バケット初期化 rep(i, kB) rep(j, kB) sep[i][j].clear(); // 弾をバケットに放り込む rep(i, BN) { int x = xb[i] / kUnit, y = yb[i] / kUnit; sep[y][x].push_back(P(xb[i], yb[i])); } int ans = 0; rep(i, AN) { int x = xa[i], y = ya[i]; int ix = x / kUnit, iy = y / kUnit; for (int dy = -1; dy <= 1; ++dy) { for (int dx = -1; dx <= 1; ++dx) { int tix = ix + dx, tiy = iy + dy; if (0 > tix || 0 > tiy || tix >= kB || tiy >= kB) continue; // 同じバケット内の弾と比較 rep(j, sep[tiy][tix].size()) { int dist_x = sep[tiy][tix][j].first - x, dist_y = sep[tiy][tix][j].second - y; int dist_pow = (dist_x * dist_x) + (dist_y * dist_y); if (16 * R * R >= dist_pow) ++ans; } } } } return ans; } int main() { while (input()) { printf("%d\n", solve()); } return 0; }
replace
23
24
23
24
TLE
p00609
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <set> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) struct Point { int x; int y; char type; Point() { ; } Point(double x, double y, int type) : x(x), y(y), type(type) { ; } bool operator<(const Point &rhs) const { if (y != rhs.y) { return y < rhs.y; } return x < rhs.x; } }; struct Event { int x; Point p; char in; Event() { ; } Event(int x, Point p, int in) : x(x), p(p), in(in) { ; } bool operator<(const Event &rhs) const { if (x != rhs.x) { return x < rhs.x; } return in > rhs.in; } }; inline int square(int x) { return x * x; } inline int dist2(const Point &a, const Point &b) { return square(a.x - b.x) + square(a.y - b.y); } int an, bn, n; int r; Event event[500000]; int PlaneSweep() { int ans = 0; int r4 = 4 * r; int r2 = 16 * r * r; set<Point> open[2]; REP(iter, n) { Event &e = event[iter]; Point &p = e.p; if (e.in) { set<Point>::iterator lower = open[(int)p.type ^ 1].lower_bound(Point(0, p.y - r4, 0)); set<Point>::iterator upper = upper_bound( lower, open[(int)p.type ^ 1].end(), Point(1000000000, p.y + r4, 0)); for (set<Point>::iterator it = lower; it != upper; it++) { if (dist2(*it, p) <= r2) { ans++; } } open[(int)p.type].insert(p); } else { open[(int)p.type].erase(open[(int)p.type].find(p)); } } return ans; } int main() { while (scanf("%d %d %d", &an, &bn, &r), an | bn) { n = (an + bn) * 2; REP(i, an) { int x, y; scanf("%d %d", &x, &y); event[2 * i + 0] = Event(x - 2 * r, Point(x, y, 0), 1); event[2 * i + 1] = Event(x + 2 * r, Point(x, y, 0), 0); } REP(i, bn) { int x, y; scanf("%d %d", &x, &y); event[2 * an + 2 * i + 0] = Event(x - 2 * r, Point(x, y, 1), 1); event[2 * an + 2 * i + 1] = Event(x + 2 * r, Point(x, y, 1), 0); } sort(event, event + n); int ans = PlaneSweep(); printf("%d\n", ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <set> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) struct Point { int x; int y; char type; Point() { ; } Point(double x, double y, int type) : x(x), y(y), type(type) { ; } bool operator<(const Point &rhs) const { if (y != rhs.y) { return y < rhs.y; } return x < rhs.x; } }; struct Event { int x; Point p; char in; Event() { ; } Event(int x, Point p, int in) : x(x), p(p), in(in) { ; } bool operator<(const Event &rhs) const { if (x != rhs.x) { return x < rhs.x; } return in > rhs.in; } }; inline int square(int x) { return x * x; } inline int dist2(const Point &a, const Point &b) { return square(a.x - b.x) + square(a.y - b.y); } int an, bn, n; int r; Event event[500000]; int PlaneSweep() { int ans = 0; int r4 = 4 * r; int r2 = 16 * r * r; set<Point> open[2]; REP(iter, n) { Event &e = event[iter]; Point &p = e.p; if (e.in) { set<Point>::iterator lower = open[(int)p.type ^ 1].lower_bound(Point(0, p.y - r4, 0)); set<Point>::iterator upper = open[(int)p.type ^ 1].upper_bound(Point(1000000000, p.y + r4, 0)); for (set<Point>::iterator it = lower; it != upper; it++) { if (dist2(*it, p) <= r2) { ans++; } } open[(int)p.type].insert(p); } else { open[(int)p.type].erase(open[(int)p.type].find(p)); } } return ans; } int main() { while (scanf("%d %d %d", &an, &bn, &r), an | bn) { n = (an + bn) * 2; REP(i, an) { int x, y; scanf("%d %d", &x, &y); event[2 * i + 0] = Event(x - 2 * r, Point(x, y, 0), 1); event[2 * i + 1] = Event(x + 2 * r, Point(x, y, 0), 0); } REP(i, bn) { int x, y; scanf("%d %d", &x, &y); event[2 * an + 2 * i + 0] = Event(x - 2 * r, Point(x, y, 1), 1); event[2 * an + 2 * i + 1] = Event(x + 2 * r, Point(x, y, 1), 0); } sort(event, event + n); int ans = PlaneSweep(); printf("%d\n", ans); } }
replace
70
72
70
72
TLE
p00609
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <vector> using namespace std; int main() { int a, b, R; while (cin >> a >> b >> R, a || b) { vector<long long int> A_x(a), A_y(a); for (int i = 0; i < a; ++i) cin >> A_x[i] >> A_y[i]; vector<vector<long long int>> M(100010); for (int i = 0; i < b; ++i) { long long int x, y; cin >> x >> y; M[x].push_back(y); } for (auto &x : M) sort(x.begin(), x.end()); long long int ans = 0; for (int i = 0; i < a; ++i) { long long int x = A_x[i], y = A_y[i]; for (long long int j = x - 4 * R; j <= x + 4 * R; ++j) { long long int d = floor(sqrt(16 * R * R - (x - j) * (x - j)) + 1e-8); ans += upper_bound(M[j].begin(), M[j].end(), y + d) - lower_bound(M[j].begin(), M[j].end(), y - d); } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <vector> using namespace std; int main() { int a, b, R; while (cin >> a >> b >> R, a || b) { vector<long long int> A_x(a), A_y(a); for (int i = 0; i < a; ++i) cin >> A_x[i] >> A_y[i]; vector<vector<long long int>> M(100010); for (int i = 0; i < b; ++i) { long long int x, y; cin >> x >> y; M[x].push_back(y); } for (auto &x : M) sort(x.begin(), x.end()); long long int ans = 0; for (int i = 0; i < a; ++i) { long long int x = A_x[i], y = A_y[i]; for (long long int j = max(0LL, x - 4 * R); j <= min(100000LL, x + 4 * R); ++j) { long long int d = floor(sqrt(16 * R * R - (x - j) * (x - j)) + 1e-8); ans += upper_bound(M[j].begin(), M[j].end(), y + d) - lower_bound(M[j].begin(), M[j].end(), y - d); } } cout << ans << endl; } return 0; }
replace
23
24
23
25
-11
p00609
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 10005 set<int> G[MAX]; int n, m, x, y, R; int main() { while (1) { scanf("%d %d %d", &n, &m, &R); if (n == 0 && m == 0) break; for (int i = 0; i < MAX; i++) G[i].clear(); for (int i = 0; i < n; i++) { scanf("%d %d", &x, &y); G[x].insert(y); } int ans = 0; while (m--) { scanf("%d %d", &x, &y); set<int>::iterator it, it2; for (int px = x - R * 4; px <= x + R * 4; px++) { int py = y - R * 4; it = G[px].lower_bound(py); it2 = G[px].end(); while (it != it2 && (*it) <= y + R * 4) { int X = px - x; int Y = (*it) - y; if (16 * R * R >= X * X + Y * Y) { ans++; } it++; } } } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 10005 set<int> G[MAX]; int n, m, x, y, R; int main() { while (1) { scanf("%d %d %d", &n, &m, &R); if (n == 0 && m == 0) break; for (int i = 0; i < MAX; i++) G[i].clear(); for (int i = 0; i < n; i++) { scanf("%d %d", &x, &y); G[x].insert(y); } int ans = 0; while (m--) { scanf("%d %d", &x, &y); set<int>::iterator it, it2; for (int px = x - R * 4; px <= x + R * 4; px++) { if (px < 0 || MAX <= px) continue; int py = y - R * 4; it = G[px].lower_bound(py); it2 = G[px].end(); while (it != it2 && (*it) <= y + R * 4) { int X = px - x; int Y = (*it) - y; if (16 * R * R >= X * X + Y * Y) { ans++; } it++; } } } printf("%d\n", ans); } return 0; }
insert
23
23
23
26
0
p00609
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int A, B, R; vector<int> P[11000]; bool check(int x, int y, int x1, int y1) { if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R) return false; return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> A >> B >> R && (A || B || R)) { for (int i = 0; i <= 10000; i++) P[i].clear(); for (int i = 0; i < A; i++) { int x, y; cin >> x >> y; P[x].push_back(y); } for (int i = 0; i <= 10000; i++) sort(P[i].begin(), P[i].end()); int res = 0; for (int i = 0; i < B; i++) { int x, y; cin >> x >> y; // cout << x << " " << y << " : " << endl; for (int nx = x - 4 * R; nx <= x + 4 * R; nx++) { int id = lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin(); for (int j = id; j < P[nx].size(); j++) { // cout << nx << " "<< P[nx][j] << endl; if (P[nx][j] > y + 4 * R) break; if (check(nx, P[nx][j], x, y)) res++; } } } cout << res << endl; } }
#include <bits/stdc++.h> using namespace std; int A, B, R; vector<int> P[11000]; bool check(int x, int y, int x1, int y1) { if ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y) > 16 * R * R) return false; return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> A >> B >> R && (A || B || R)) { for (int i = 0; i <= 10000; i++) P[i].clear(); for (int i = 0; i < A; i++) { int x, y; cin >> x >> y; P[x].push_back(y); } for (int i = 0; i <= 10000; i++) sort(P[i].begin(), P[i].end()); int res = 0; for (int i = 0; i < B; i++) { int x, y; cin >> x >> y; // cout << x << " " << y << " : " << endl; for (int nx = max(0, x - 4 * R); nx <= x + 4 * R; nx++) { int id = lower_bound(P[nx].begin(), P[nx].end(), y - 4 * R) - P[nx].begin(); for (int j = id; j < P[nx].size(); j++) { // cout << nx << " "<< P[nx][j] << endl; if (P[nx][j] > y + 4 * R) break; if (check(nx, P[nx][j], x, y)) res++; } } } cout << res << endl; } }
replace
34
35
34
35
-11
p00612
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; typedef long long ll; int main() { ll n; while (cin >> n, n) { if (n == 3) { cout << 42 << endl; continue; } ll hoge = (int)(sqrt((double)n / 2) - 1); ll num = 2 * n - 4 * hoge; cout << num << endl; for (ll i = 1; 2 * i * i <= n; ++i) { num += 4 * ceil((double)n / i / 2) - 2 * hoge; cout << i << " " << num << endl; } cout << num * 2 + 8 * n << endl; } }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1 << 29; typedef long long ll; int main() { ll n; while (scanf("%lld", &n), n) { ll corner = 1; while (corner * corner < n / 2) ++corner; ll side = n / 2; for (ll i = 1; i * i < n / 2; ++i) side += (n / 2 + i - 1) / i; side -= corner * corner; // cout << corner << " " << side << endl; ll ans = side * 8 + corner * corner * 4; ans *= 2; ans += n * 2 * 4; printf("%lld\n", ans); } }
replace
24
37
24
37
TLE
p00613
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int K, c[10], sum = 0; cin >> K; if (0 == K) break; for (int i = 0; i < K * (K - 1) / 2; i++) { cin >> c[i]; sum += c[i]; } sum = sum / (K - 1); cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { while (1) { int K, c[10000], sum = 0; cin >> K; if (0 == K) break; for (int i = 0; i < K * (K - 1) / 2; i++) { cin >> c[i]; sum += c[i]; } sum = sum / (K - 1); cout << sum << endl; } return 0; }
replace
5
6
5
6
0
p00613
C++
Runtime Error
#include <iostream> using namespace std; int main() { int k, l; int p = 0; int i = 0; int a[101]; while (1) { cin >> k; l = k * (k - 1) / 2; if (k == 0) { break; } for (i = 0; i < l; i++) { cin >> a[i]; p += a[i]; } cout << p / (k - 1) << endl; p = 0; } return 0; }
#include <iostream> using namespace std; int main() { int k, l; int p = 0; int i = 0; int a[10000]; while (1) { cin >> k; l = k * (k - 1) / 2; if (k == 0) { break; } for (i = 0; i < l; i++) { cin >> a[i]; p += a[i]; } cout << p / (k - 1) << endl; p = 0; } return 0; }
replace
7
8
7
8
0
p00614
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) int main() { // cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl; while (1) { int P, N[7] = {}; scanf("%d", &P); if (P == 0) break; REP(i, 1, 7) scanf("%d", &N[i]); int ans = 1000000000; REP(d, P, 666001) { int D = d; int U = 0; int k, m; m = D / 500; k = min(m, N[6]); D %= 500; D += (m - k) * 500; U += k; m = D / 100; k = min(m, N[5]); D %= 100; D += (m - k) * 100; U += k; m = D / 50; k = min(m, N[4]); D %= 50; D += (m - k) * 50; U += k; m = D / 10; k = min(m, N[3]); D %= 10; D += (m - k) * 10; U += k; m = D / 5; k = min(m, N[2]); D %= 5; D += (m - k) * 5; U += k; m = D; k = min(D, N[1]); D += (m - k); U += k; int T = d - P; int S = 0; S += T / 500; T %= 500; S += T / 100; T %= 100; S += T / 50; T %= 50; S += T / 10; T %= 10; S += T / 5; T %= 5; S += T; ans = min(ans, U + S); } printf("%d\n", ans); } 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) int main() { // cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl; while (1) { int P, N[7] = {}; scanf("%d", &P); if (P == 0) break; REP(i, 1, 7) scanf("%d", &N[i]); int ans = 1000000000; REP(d, P, P + 100000) { int D = d; int U = 0; int k, m; m = D / 500; k = min(m, N[6]); D %= 500; D += (m - k) * 500; U += k; m = D / 100; k = min(m, N[5]); D %= 100; D += (m - k) * 100; U += k; m = D / 50; k = min(m, N[4]); D %= 50; D += (m - k) * 50; U += k; m = D / 10; k = min(m, N[3]); D %= 10; D += (m - k) * 10; U += k; m = D / 5; k = min(m, N[2]); D %= 5; D += (m - k) * 5; U += k; m = D; k = min(D, N[1]); D += (m - k); U += k; int T = d - P; int S = 0; S += T / 500; T %= 500; S += T / 100; T %= 100; S += T / 50; T %= 50; S += T / 10; T %= 10; S += T / 5; T %= 5; S += T; ans = min(ans, U + S); } printf("%d\n", ans); } return 0; }
replace
20
21
20
21
TLE
p00614
C++
Time Limit Exceeded
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define popcount __builtin_popcount #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> pint; const int coin[] = {500, 100, 50, 10, 5, 1}; int min_coin(int change) { int res = 0; rep(i, 6) { int c = change / coin[i]; res += c; change -= c * coin[i]; } return res; } bool pint_eq(const pint &a, const pint &b) { cout << "hoge" << endl; return a.first == b.first; } int main() { int p, n[6]; while (scanf("%d", &p), p) { rep(i, 6) scanf("%d", n + i); reverse(n, n + 6); vector<pint> d[7]; d[0].push_back(pint(p, 0)); int res = 1 << 25; rep(i, 6) { rep(j, d[i].size()) { int left = d[i][j].first; int use = d[i][j].second; for (int c = min(n[i], max(0, left / coin[i])); c <= n[i] && c * left - c * coin[i] >= -500; ++c) { d[i + 1].push_back(pint(left - c * coin[i], use + c)); } } sort(all(d[i + 1])); d[i + 1].erase(unique(all(d[i + 1]), pint_eq), d[i + 1].end()); rep(j, d[i + 1].size()) { int left = d[i + 1][j].first; int use = d[i + 1][j].second; if (left <= 0) min_swap(res, use + min_coin(-left)); } } printf("%d\n", res); } }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define popcount __builtin_popcount #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T &a, const T &b) { a = max(a, b); } template <class T> void min_swap(T &a, const T &b) { a = min(a, b); } const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> pint; const int coin[] = {500, 100, 50, 10, 5, 1}; int min_coin(int change) { int res = 0; rep(i, 6) { int c = change / coin[i]; res += c; change -= c * coin[i]; } return res; } bool pint_eq(const pint &a, const pint &b) { return a.first == b.first; } int main() { int p, n[6]; while (scanf("%d", &p), p) { rep(i, 6) scanf("%d", n + i); reverse(n, n + 6); vector<pint> d[7]; d[0].push_back(pint(p, 0)); int res = 1 << 25; rep(i, 6) { rep(j, d[i].size()) { int left = d[i][j].first; int use = d[i][j].second; for (int c = min(n[i], max(0, left / coin[i])); c <= n[i] && c * left - c * coin[i] >= -500; ++c) { d[i + 1].push_back(pint(left - c * coin[i], use + c)); } } sort(all(d[i + 1])); d[i + 1].erase(unique(all(d[i + 1]), pint_eq), d[i + 1].end()); rep(j, d[i + 1].size()) { int left = d[i + 1][j].first; int use = d[i + 1][j].second; if (left <= 0) min_swap(res, use + min_coin(-left)); } } printf("%d\n", res); } }
replace
57
61
57
58
TLE
p00614
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i) #define FOREACH(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) const int INF = 1000000000; int value[] = {500, 100, 50, 10, 5, 1}; int opt(int p, int num[]) { int ret = 0; REP(i, 6) { int use = min(num[i], p / value[i]); p -= value[i] * use; ret += use; } return p ? INF : ret; } int main() { int p; while (scanf("%d", &p), p) { int num[6], sum = 0; REP(i, 6) { scanf("%d", &num[5 - i]); sum += num[5 - i] * value[5 - i]; } int big[] = {INF, INF, INF, INF, INF, INF}; int ans = INF; FOREQ(i, p, sum) { ans = min(ans, opt(i, num) + opt(i - p, big)); } printf("%d\n", ans); } }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i) #define FOREACH(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) const int INF = 1000000000; int value[] = {500, 100, 50, 10, 5, 1}; int opt(int p, int num[]) { int ret = 0; REP(i, 6) { int use = min(num[i], p / value[i]); p -= value[i] * use; ret += use; } return p ? INF : ret; } int main() { int p; while (scanf("%d", &p), p) { int num[6], sum = 0; REP(i, 6) { scanf("%d", &num[5 - i]); sum += num[5 - i] * value[5 - i]; } int big[] = {INF, INF, INF, INF, INF, INF}; int ans = INF; FOREQ(i, p, min(sum, p + 10000)) { ans = min(ans, opt(i, num) + opt(i - p, big)); } printf("%d\n", ans); } }
replace
40
41
40
43
TLE
p00614
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; const int MAXN = 666 * 1000 + 1; int dp2[MAXN]; int p[6] = {1, 5, 10, 50, 100, 500}; int count_c(int Y, int c[]) { int res = 0; for (int i = 5; i >= 0; i--) { REP(j, c[i]) { if (Y >= p[i]) { Y -= p[i]; res++; } } } if (Y == 0) return res; else return INF; } int main() { int Y; fill(dp2, dp2 + MAXN, INF); dp2[0] = 0; REP(i, 6) { FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); } } while (cin >> Y, Y) { int c[6]; REP(i, 6) cin >> c[i]; int MY = 0; REP(i, 6) MY += c[i] * p[i]; int ans = INF; for (int i = MY; i >= Y; i--) { ans = min(ans, count_c(i, c) + dp2[i - Y]); } cout << ans << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; const int MAXN = 666 * 1000 + 1; int dp2[MAXN]; int p[6] = {1, 5, 10, 50, 100, 500}; int count_c(int Y, int c[]) { int res = 0; for (int i = 5; i >= 0; i--) { int t = min(c[i], Y / p[i]); Y -= t * p[i]; res += t; } if (Y == 0) return res; else return INF; } int main() { int Y; fill(dp2, dp2 + MAXN, INF); dp2[0] = 0; REP(i, 6) { FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); } } while (cin >> Y, Y) { int c[6]; REP(i, 6) cin >> c[i]; int MY = 0; REP(i, 6) MY += c[i] * p[i]; int ans = INF; for (int i = MY; i >= Y; i--) { ans = min(ans, count_c(i, c) + dp2[i - Y]); } cout << ans << endl; } return 0; }
replace
39
45
39
42
TLE
p00615
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; // #define int long long #define DEBUG 0 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ if (DEBUG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DEBUG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; static const int MOD = 1e9 + 7; signed main() { int a[10010] = {}; for (int n, m; cin >> n >> m && (n || m);) { rep(i, 0, n + m) cin >> a[i]; a[n + m] = 0; sort(a, a + n + m + 1); int M = 0; rep(i, 1, n + m + 1) { M = max(M, a[i] - a[i - 1]); } cout << M << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; // #define int long long #define DEBUG 0 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(a) (a).begin(), (a).end() #define dump(o) \ if (DEBUG) { \ cerr << #o << " " << o << endl; \ } #define dumpc(o) \ if (DEBUG) { \ cerr << #o; \ for (auto &e : (o)) \ cerr << " " << e; \ cerr << endl; \ } using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; static const int MOD = 1e9 + 7; signed main() { int a[20010] = {}; for (int n, m; cin >> n >> m && (n || m);) { rep(i, 0, n + m) cin >> a[i]; a[n + m] = 0; sort(a, a + n + m + 1); int M = 0; rep(i, 1, n + m + 1) { M = max(M, a[i] - a[i - 1]); } cout << M << endl; } return 0; }
replace
27
28
27
28
0
p00615
C++
Memory Limit Exceeded
#include <cstdio> #include <iostream> #define N 10000000 int tl[N]; using namespace std; void sort(int, int); int syori(int, int); int main() { int n, m, i, ans; while (1) { for (i = 0; i < N; i++) { tl[i] = 0; } cin >> n; cin >> m; if (m == 0 && n == 0) break; for (i = 1; i <= n; i++) { cin >> tl[i]; } for (i = n + 1; i <= (m + n); i++) { cin >> tl[i]; } sort(m, n); ans = syori(n, m); cout << ans << endl; } return 0; } void sort(int m, int n) { int i, j, max, hi; for (i = 0; i <= (m + n); i++) { max = i; for (j = i; j <= (m + n); j++) { if (tl[j] < tl[max]) max = j; } hi = tl[max]; tl[max] = tl[i]; tl[i] = hi; } // for(i=0;i<=(m+n);i++){ // cout << tl[i] << endl;; //} } int syori(int n, int m) { int i, max = 0; for (i = 0; i < (m + n); i++) { if (tl[i + 1] - tl[i] >= max) max = tl[i + 1] - tl[i]; } return max; }
#include <cstdio> #include <iostream> #define N 1000000 int tl[N]; using namespace std; void sort(int, int); int syori(int, int); int main() { int n, m, i, ans; while (1) { for (i = 0; i < N; i++) { tl[i] = 0; } cin >> n; cin >> m; if (m == 0 && n == 0) break; for (i = 1; i <= n; i++) { cin >> tl[i]; } for (i = n + 1; i <= (m + n); i++) { cin >> tl[i]; } sort(m, n); ans = syori(n, m); cout << ans << endl; } return 0; } void sort(int m, int n) { int i, j, max, hi; for (i = 0; i <= (m + n); i++) { max = i; for (j = i; j <= (m + n); j++) { if (tl[j] < tl[max]) max = j; } hi = tl[max]; tl[max] = tl[i]; tl[i] = hi; } // for(i=0;i<=(m+n);i++){ // cout << tl[i] << endl;; //} } int syori(int n, int m) { int i, max = 0; for (i = 0; i < (m + n); i++) { if (tl[i + 1] - tl[i] >= max) max = tl[i + 1] - tl[i]; } return max; }
replace
2
3
2
3
MLE
p00615
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; #define FOR(i, b, n) for (int i = (b); i < (n); ++i) #define rep(i, n) FOR(i, 0, n) int main() { int a, b, data[10000]; while (cin >> a >> b, a || b) { rep(i, a + b) cin >> data[i]; sort(data, data + a + b); int ans = data[0]; FOR(i, 1, a + b) ans = max(ans, data[i] - data[i - 1]); cout << ans << endl; } }
#include <algorithm> #include <iostream> using namespace std; #define FOR(i, b, n) for (int i = (b); i < (n); ++i) #define rep(i, n) FOR(i, 0, n) int main() { int a, b, data[20000]; while (cin >> a >> b, a || b) { rep(i, a + b) cin >> data[i]; sort(data, data + a + b); int ans = data[0]; FOR(i, 1, a + b) ans = max(ans, data[i] - data[i - 1]); cout << ans << endl; } }
replace
6
7
6
7
0
p00615
C++
Runtime Error
#include <cstring> #include <iostream> int max(int a, int b) { if (a > b) return a; else return b; } void bsort(int a[], int lim) { int tmp; for (int i = 0; i <= lim - 1; i++) for (int j = lim; i < j; j--) { if (a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; } } } int main() { int n, m, i; int out; int t[1000]; while (std::cin >> n >> m, (n || m)) { std::memset(t, 0, sizeof(t)); out = 0; for (i = 1; i <= n + m; i++) { std::cin >> t[i]; } bsort(t, n + m); for (i = 0; i <= m + n; i++) { t[i] = t[i + 1] - t[i]; } for (i = 0; i <= m + n - 1; i++) { out = max(out, t[i]); } std::cout << out << std::endl; } return 0; }
#include <cstring> #include <iostream> int max(int a, int b) { if (a > b) return a; else return b; } void bsort(int a[], int lim) { int tmp; for (int i = 0; i <= lim - 1; i++) for (int j = lim; i < j; j--) { if (a[j - 1] > a[j]) { tmp = a[j]; a[j] = a[j - 1]; a[j - 1] = tmp; } } } int main() { int n, m, i; int out; int t[20002]; while (std::cin >> n >> m, (n || m)) { std::memset(t, 0, sizeof(t)); out = 0; for (i = 1; i <= n + m; i++) { std::cin >> t[i]; } bsort(t, n + m); for (i = 0; i <= m + n; i++) { t[i] = t[i + 1] - t[i]; } for (i = 0; i <= m + n - 1; i++) { out = max(out, t[i]); } std::cout << out << std::endl; } return 0; }
replace
26
27
26
27
0
p00616
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; bool t[3][200][200]; int n, h, x, y; string s; int main() { while (cin >> n >> h && n + h) { int cnt, ans = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { t[i][j][k] = 0; } } for (int i = 0; i < h; i++) { cin >> s >> x >> y; x--, y--; if (s == "xy") { t[0][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[1][x][i]) cnt++; if (t[2][y][i]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } if (s == "xz") { t[1][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[0][x][i]) cnt++; if (t[2][i][y]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } if (s == "yz") { t[2][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[0][i][x]) cnt++; if (t[1][i][y]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } } cout << n * (n * n - h) + ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; bool t[3][500][500]; int n, h, x, y; string s; int main() { while (cin >> n >> h && n + h) { int cnt, ans = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { t[i][j][k] = 0; } } for (int i = 0; i < h; i++) { cin >> s >> x >> y; x--, y--; if (s == "xy") { t[0][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[1][x][i]) cnt++; if (t[2][y][i]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } if (s == "xz") { t[1][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[0][x][i]) cnt++; if (t[2][i][y]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } if (s == "yz") { t[2][x][y] = 1; for (int i = 0; i < n; i++) { cnt = 0; if (t[0][i][x]) cnt++; if (t[1][i][y]) cnt++; if (cnt == 2) cnt--; ans += cnt; } } } cout << n * (n * n - h) + ans << endl; } return 0; }
replace
4
5
4
5
0
p00618
C++
Time Limit Exceeded
// 22:45~ #include <bits/stdc++.h> using namespace std; #define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define all(c) begin(c), end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { print_tuple<Cdr...>(os, t); os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { print_tuple<Args...>(os << '(', t); return os << ')'; } template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))> basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) { os << '['; for (auto i = begin(c); i != end(c); ++i) os << (i == begin(c) ? "" : " ") << *i; return os << ']'; } constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr double EPS = 1e-9; int dfs(const vvi &g, int u) { int res = 1 << u; for (int v : g[u]) res |= dfs(g, v); return res; } int main() { // clock_t t0=clock(); for (int n, m; ~scanf("%d%d", &n, &m) && n | m;) { array<int, 20> cs; vvi g(n); rep(i, n) { scanf("%d", &cs[i]); int k; scanf("%d", &k); rep(j, k) { int r; scanf("%d", &r); g[i].push_back(r); } } array<int, 20> bs; rep(i, n) bs[i] = dfs(g, i); vi sums(1 << n); rep(i, n) sums[1 << i] = cs[i]; rep(i, 1 << n) sums[i] = sums[i & -i] + sums[i - (i & -i)]; int res = INF; per(i, 1 << n) if (__builtin_popcount(i) < res) { int b = 0; rep(j, n) b |= (i >> j & 1) * bs[j]; if (sums[b] >= m) res = min(res, __builtin_popcount(b)); } printf("%d\n", res); } // clock_t t1=clock(); // printf("%.3f[s]\n",1.*(t1-t0)/CLOCKS_PER_SEC); }
// 22:45~ #include <bits/stdc++.h> using namespace std; #define dump(...) cout << "# " << #__VA_ARGS__ << '=' << (__VA_ARGS__) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define all(c) begin(c), end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { print_tuple<Cdr...>(os, t); os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { print_tuple<Args...>(os << '(', t); return os << ')'; } template <typename Ch, typename Tr, typename C, typename = decltype(begin(C()))> basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, const C &c) { os << '['; for (auto i = begin(c); i != end(c); ++i) os << (i == begin(c) ? "" : " ") << *i; return os << ']'; } constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr double EPS = 1e-9; int dfs(const vvi &g, int u) { int res = 1 << u; for (int v : g[u]) res |= dfs(g, v); return res; } int main() { // clock_t t0=clock(); for (int n, m; ~scanf("%d%d", &n, &m) && n | m;) { array<int, 20> cs; vvi g(n); rep(i, n) { scanf("%d", &cs[i]); int k; scanf("%d", &k); rep(j, k) { int r; scanf("%d", &r); g[i].push_back(r); } } array<int, 20> bs; rep(i, n) bs[i] = dfs(g, i); static array<int, 1 << 20> sums; sums.fill(0); rep(i, n) sums[1 << i] = cs[i]; rep(i, 1 << n) sums[i] = sums[i & -i] + sums[i - (i & -i)]; int res = INF; per(i, 1 << n) if (__builtin_popcount(i) < res) { int b = 0; rep(j, n) b |= (i >> j & 1) * bs[j]; if (sums[b] >= m) res = min(res, __builtin_popcount(b)); } printf("%d\n", res); } // clock_t t1=clock(); // printf("%.3f[s]\n",1.*(t1-t0)/CLOCKS_PER_SEC); }
replace
81
82
81
84
TLE
p00618
C++
Memory Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct NODE { int a, b, c; NODE(int x, int y, int z) { a = x, b = y, c = z; } }; #define INF (1 << 21) int n, U, unit[20]; char done[1 << 20]; int main() { while (cin >> n >> U, n) { int graph[21] = {0}; memset(done, 0, sizeof(done)); for (int i = 0; i < n; i++) { int k; cin >> unit[i] >> k; for (int j = 0; j < k; j++) { int a; cin >> a; graph[i] |= (1 << a); } } queue<NODE> Q; Q.push(NODE(0, 0, 0)); done[0] = 1; while (Q.size()) { NODE q = Q.front(); Q.pop(); if (q.b >= U) { cout << q.c << endl; break; } for (int i = 0; i < n; i++) { if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i]) if (!done[q.a | (1 << i)]) Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1)); } } } }
#include <algorithm> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct NODE { int a, b, c; NODE(int x, int y, int z) { a = x, b = y, c = z; } }; #define INF (1 << 21) int n, U, unit[20]; char done[1 << 20]; int main() { while (cin >> n >> U, n) { int graph[21] = {0}; memset(done, 0, sizeof(done)); for (int i = 0; i < n; i++) { int k; cin >> unit[i] >> k; for (int j = 0; j < k; j++) { int a; cin >> a; graph[i] |= (1 << a); } } queue<NODE> Q; Q.push(NODE(0, 0, 0)); done[0] = 1; while (Q.size()) { NODE q = Q.front(); Q.pop(); if (q.b >= U) { cout << q.c << endl; break; } for (int i = 0; i < n; i++) { if (!(q.a >> i & 1) && (q.a & graph[i]) == graph[i]) if (!done[q.a | (1 << i)]) Q.push(NODE(q.a | (1 << i), q.b + unit[i], q.c + 1)), done[q.a | (1 << i)] = true; } } } }
replace
42
43
42
44
MLE
p00618
C++
Time Limit Exceeded
#include <iostream> #include <queue> using namespace std; struct Data { int used; int credit; }; int n, U, c, k, r; int credit[20], required[20]; int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; } int BFS() { bool visited[1 << 20] = {0}; queue<Data> q; Data now = {0, 0}; q.push(now); while (!q.empty()) { now = q.front(); q.pop(); if (visited[now.used]) continue; visited[now.used] = true; if (now.credit >= U) return bitcount(now.used); for (int i = 0; i < n; i++) { if (now.used & (1 << i) || (now.used & required[i]) != required[i]) continue; Data next = now; next.used ^= (1 << i); next.credit += credit[i]; q.push(next); } } return -1; } int main() { while (cin >> n >> U, n | U) { for (int i = 0; i < n; i++) { cin >> c >> k; credit[i] = c; required[i] = 0; for (int j = 0; j < k; j++) { cin >> r; required[i] ^= (1 << r); } } cout << BFS() << endl; } }
#include <iostream> #include <queue> using namespace std; struct Data { int used; int credit; }; int n, U, c, k, r; int credit[20], required[20]; int bitcount(int n) { return n ? (n & 1) + bitcount(n >> 1) : 0; } int BFS() { bool visited[1 << 20] = {0}; queue<Data> q; Data now = {0, 0}; q.push(now); while (!q.empty()) { now = q.front(); q.pop(); if (visited[now.used]) continue; visited[now.used] = true; if (now.credit >= U) return bitcount(now.used); for (int i = 0; i < n; i++) { if (now.used & (1 << i) || (now.used & required[i]) != required[i]) continue; Data next = now; next.used ^= (1 << i); next.credit += credit[i]; q.push(next); } } return -1; } int main() { while (cin >> n >> U, n | U) { for (int i = 0; i < n; i++) { cin >> c >> k; credit[i] = c; required[i] = 0; for (int j = 0; j < k; j++) { cin >> r; required[i] ^= (1 << r); } } cout << BFS() << endl; } }
insert
0
0
0
1
TLE
p00618
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int tani[21]; int needs[21]; // i‚̏ó‘Ô‚©‚çUˆÈã‚Ì’PˆÊ‚É‚½‚ǂ蒅‚­Å¬—šC” int dp[1 << 20]; // s‚«æ int U; int n; int dfs(int s, int sum) { if (dp[s] != -1) return dp[s]; else if (sum >= U) { dp[s] = 0; return dp[s]; } else { int minCost = 1000000; // bool used[21]; // for(int i = 0; i < n; i++){ // if((s>>i)&1) // used[i]=true; // } // ‚‚¬‚ɂǂ±‚Ö‘JˆÚ‚·‚é‚© // for(int i = 0; i < tos[s].size(); i++){ // minCost=min(minCost,dfs(s|(1<<(tos[s][i])),sum+tani[tos[s][i]])+1); // if(minCost==1) // break; // } for (int i = 0; i < n; i++) { // ‚Ü‚¾‘JˆÚ‚µ‚ĂȂ©‚Á‚½‚ç if (!((s >> i) & 1)) { if (((needs[i]) & s) == needs[i]) { minCost = min(minCost, dfs(s | (1 << i), sum + tani[i]) + 1); if (minCost == 1) break; } // for(int j = 0; j < vpii[i].second.size(); j++){ // if(!used[vpii[i].second[j]]){ // f=true; // break; // } // } // if(!f) // minCost=min(minCost,dfs(s|(1<<i),sum+vpii[i].first)+1); } } dp[s] = minCost; return dp[s]; } } int main() { int c, k; int t; int tn; while (cin >> n >> U && !(n == 0 && U == 0)) { fill(dp, dp + (1 << 20), -1); for (int i = 0; i < n; i++) { cin >> tani[i] >> k; needs[i] = 0; for (int j = 0; j < k; j++) { cin >> t; needs[i] |= (1 << t); } // needs[i]=tn; } // for(int s = 0; s < (1<<n); s++){ // for(int i = 0; i < n; i++){ // if(!((s>>i)&1)){ // if(((needs[i])&s)==needs[i]){ // tos[s].push_back(i); // } // } // } // } cout << dfs(0, 0) << endl; // for(int i = 0; i < (1<<n); i++) // tos[i].clear(); } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int tani[21]; int needs[21]; // i‚̏ó‘Ô‚©‚çUˆÈã‚Ì’PˆÊ‚É‚½‚ǂ蒅‚­Å¬—šC” int dp[1 << 20]; // s‚«æ int U; int n; int dfs(int s, int sum) { if (dp[s] != -1) return dp[s]; else if (sum >= U) { dp[s] = 0; return dp[s]; } else { int minCost = 1000000; // bool used[21]; // for(int i = 0; i < n; i++){ // if((s>>i)&1) // used[i]=true; // } // ‚‚¬‚ɂǂ±‚Ö‘JˆÚ‚·‚é‚© // for(int i = 0; i < tos[s].size(); i++){ // minCost=min(minCost,dfs(s|(1<<(tos[s][i])),sum+tani[tos[s][i]])+1); // if(minCost==1) // break; // } for (int i = 0; i < n; i++) { // ‚Ü‚¾‘JˆÚ‚µ‚ĂȂ©‚Á‚½‚ç if (!((s >> i) & 1)) { if (((needs[i]) & s) == needs[i]) { minCost = min(minCost, dfs(s | (1 << i), sum + tani[i]) + 1); if (minCost == 1) break; } // for(int j = 0; j < vpii[i].second.size(); j++){ // if(!used[vpii[i].second[j]]){ // f=true; // break; // } // } // if(!f) // minCost=min(minCost,dfs(s|(1<<i),sum+vpii[i].first)+1); } } dp[s] = minCost; return dp[s]; } } int main() { int c, k; int t; int tn; while (cin >> n >> U && !(n == 0 && U == 0)) { fill(dp, dp + (1 << 20), -1); for (int i = 0; i < n; i++) { cin >> tani[i] >> k; needs[i] = 0; for (int j = 0; j < k; j++) { cin >> t; needs[i] |= (1 << t); } } // for(int s = 0; s < (1<<n); s++){ // for(int i = 0; i < n; i++){ // if(!((s>>i)&1)){ // if(((needs[i])&s)==needs[i]){ // tos[s].push_back(i); // } // } // } // } cout << dfs(0, 0) << endl; // for(int i = 0; i < (1<<n); i++) // tos[i].clear(); } return 0; }
delete
72
73
72
72
TLE
p00619
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define EPS (1e-5) #define equal(a, b) (fabs(a - b) < EPS) #define lt(a, b) (a - b < -EPS) #define MAX 252 #define INF (1e9) #define PI acos(-1) struct Point { long double x, y; Point() {} Point(long double x, long double y) : x(x), y(y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point operator*(const long double &k) const { return Point(x * k, y * k); } Point operator/(const long double &k) const { return Point(x / k, y / k); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return (x == p.x && y == p.y); } }; long double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } long double cross(const Point &a, const Point &b) { return a.x * b.y - b.x * a.y; } long double norm(const Point &p) { return dot(p, p); } long double abs(const Point &p) { return sqrt(norm(p)); } long double dist(const Point &a, const Point &b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } long double toRad(long double ang) { return ang * PI / 180; } long double toAng(long double rad) { return rad * 180 / PI; } #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 typedef Point Vector; int ccw(const Point &p0, const Point &p1, const Point &p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } long double getAngle(const Point &a, const Point &b, const Point &c) { Vector v1 = b - a, v2 = c - b; long double aa = atan2(v1.x, v1.y), ba = atan2(v2.x, v2.y); if (aa > ba) swap(aa, ba); long double ang = toAng(ba - aa); return min(ang, 180 - ang); } struct Segment { Point s, t; Segment() {} Segment(Point s, Point t) : s(s), t(t) {} }; bool isIntersectSP(const Segment &s, const Point &p) { return (ccw(s.s, s.t, p) == 0); } bool isIntersectSS(const Segment &a, const Segment &b) { Point s[2] = {a.s, a.t}, t[2] = {b.s, b.t}; return (ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 && ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0); } Point crosspointSS(const Segment &a, const Segment &b) { Vector va = a.t - a.s, vb = b.t - b.s; long double d = cross(vb, va); if (abs(d) < EPS) return b.s; return a.s + va * cross(vb, b.t - a.s) * (1.0 / d); } typedef vector<int> Edges; typedef vector<Edges> Graph; Graph segmentArrangement(vector<Segment> &segs, vector<Point> &ps) { Graph G; int N = segs.size(); ps.clear(); for (int i = 0; i < N; i++) { ps.push_back(segs[i].s); ps.push_back(segs[i].t); for (int j = i + 1; j < N; j++) { if (isIntersectSS(segs[i], segs[j])) { ps.push_back(crosspointSS(segs[i], segs[j])); } } } sort(ps.begin(), ps.end()); ps.erase(unique(ps.begin(), ps.end()), ps.end()); int N2 = ps.size(); G.resize(N2); for (int i = 0; i < N; i++) { vector<int> vec; for (int j = 0; j < N2; j++) { if (isIntersectSP(segs[i], ps[j])) { vec.push_back(j); } } sort(vec.begin(), vec.end()); for (int j = 0; j < (int)vec.size() - 1; j++) { int u = vec[j], v = vec[j + 1]; G[u].push_back(v); G[v].push_back(u); } } return G; } struct State { long double w; int p, n; State() {} State(long double w, int p, int n) : w(w), p(p), n(n) {} bool operator<(const State &s) const { return w > s.w; } }; long double dijkstra(Graph &G, Point &s, Point &t, vector<Point> &ps) { int idx = -1; for (int i = 0; i < (int)ps.size(); i++) { if (ps[i] == s) { idx = i; } } long double d[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { d[i][j] = INF; } } priority_queue<State> Q; for (int i = 0; i < (int)G[idx].size(); i++) { int to = G[idx][i]; Q.push(State(0, idx, to)); d[idx][to] = 0; } while (!Q.empty()) { State st = Q.top(); Q.pop(); int p = st.p, n = st.n; long double w = st.w; if (lt(d[p][n], w)) continue; Point p1 = ps[p], p2 = ps[n]; if (p2 == t) return d[p][n]; for (int i = 0; i < (int)G[n].size(); i++) { int to = G[n][i]; if (p == to) continue; Point p3 = ps[to]; long double ncost = w + getAngle(p1, p2, p3); if (ncost < d[n][to] && !equal(ncost, d[n][to])) { d[n][to] = ncost; Q.push(State(d[n][to], n, to)); } } } return INF; } int main() { int N; while (cin >> N, N) { Point s, t; vector<Segment> segs(N); for (int i = 0; i < N; i++) { cin >> segs[i].s.x >> segs[i].s.y; cin >> segs[i].t.x >> segs[i].t.y; } cin >> s.x >> s.y >> t.x >> t.y; vector<Point> ps; Graph G = segmentArrangement(segs, ps); long double res = dijkstra(G, s, t, ps); if (res == INF) { cout << -1 << endl; } else { printf("%.12Lf\n", res); } } return 0; }
#include <bits/stdc++.h> using namespace std; #define EPS (1e-5) #define equal(a, b) (fabs(a - b) < EPS) #define lt(a, b) (a - b < -EPS) #define MAX 252 #define INF (1e9) #define PI acos(-1) struct Point { long double x, y; Point() {} Point(long double x, long double y) : x(x), y(y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point operator*(const long double &k) const { return Point(x * k, y * k); } Point operator/(const long double &k) const { return Point(x / k, y / k); } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return (x == p.x && y == p.y); } }; long double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } long double cross(const Point &a, const Point &b) { return a.x * b.y - b.x * a.y; } long double norm(const Point &p) { return dot(p, p); } long double abs(const Point &p) { return sqrt(norm(p)); } long double dist(const Point &a, const Point &b) { return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); } long double toRad(long double ang) { return ang * PI / 180; } long double toAng(long double rad) { return rad * 180 / PI; } #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 typedef Point Vector; int ccw(const Point &p0, const Point &p1, const Point &p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS) return CLOCKWISE; if (dot(a, b) < -EPS) return ONLINE_BACK; if (norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } long double getAngle(const Point &a, const Point &b, const Point &c) { Vector v1 = b - a, v2 = c - b; long double aa = atan2(v1.x, v1.y), ba = atan2(v2.x, v2.y); if (aa > ba) swap(aa, ba); long double ang = toAng(ba - aa); return min(ang, 360 - ang); } struct Segment { Point s, t; Segment() {} Segment(Point s, Point t) : s(s), t(t) {} }; bool isIntersectSP(const Segment &s, const Point &p) { return (ccw(s.s, s.t, p) == 0); } bool isIntersectSS(const Segment &a, const Segment &b) { Point s[2] = {a.s, a.t}, t[2] = {b.s, b.t}; return (ccw(s[0], s[1], t[0]) * ccw(s[0], s[1], t[1]) <= 0 && ccw(t[0], t[1], s[0]) * ccw(t[0], t[1], s[1]) <= 0); } Point crosspointSS(const Segment &a, const Segment &b) { Vector va = a.t - a.s, vb = b.t - b.s; long double d = cross(vb, va); if (abs(d) < EPS) return b.s; return a.s + va * cross(vb, b.t - a.s) * (1.0 / d); } typedef vector<int> Edges; typedef vector<Edges> Graph; Graph segmentArrangement(vector<Segment> &segs, vector<Point> &ps) { Graph G; int N = segs.size(); ps.clear(); for (int i = 0; i < N; i++) { ps.push_back(segs[i].s); ps.push_back(segs[i].t); for (int j = i + 1; j < N; j++) { if (isIntersectSS(segs[i], segs[j])) { ps.push_back(crosspointSS(segs[i], segs[j])); } } } sort(ps.begin(), ps.end()); ps.erase(unique(ps.begin(), ps.end()), ps.end()); int N2 = ps.size(); G.resize(N2); for (int i = 0; i < N; i++) { vector<int> vec; for (int j = 0; j < N2; j++) { if (isIntersectSP(segs[i], ps[j])) { vec.push_back(j); } } sort(vec.begin(), vec.end()); for (int j = 0; j < (int)vec.size() - 1; j++) { int u = vec[j], v = vec[j + 1]; G[u].push_back(v); G[v].push_back(u); } } return G; } struct State { long double w; int p, n; State() {} State(long double w, int p, int n) : w(w), p(p), n(n) {} bool operator<(const State &s) const { return w > s.w; } }; long double dijkstra(Graph &G, Point &s, Point &t, vector<Point> &ps) { int idx = -1; for (int i = 0; i < (int)ps.size(); i++) { if (ps[i] == s) { idx = i; } } long double d[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { d[i][j] = INF; } } priority_queue<State> Q; for (int i = 0; i < (int)G[idx].size(); i++) { int to = G[idx][i]; Q.push(State(0, idx, to)); d[idx][to] = 0; } while (!Q.empty()) { State st = Q.top(); Q.pop(); int p = st.p, n = st.n; long double w = st.w; if (lt(d[p][n], w)) continue; Point p1 = ps[p], p2 = ps[n]; if (p2 == t) return d[p][n]; for (int i = 0; i < (int)G[n].size(); i++) { int to = G[n][i]; if (p == to) continue; Point p3 = ps[to]; long double ncost = w + getAngle(p1, p2, p3); if (ncost < d[n][to] && !equal(ncost, d[n][to])) { d[n][to] = ncost; Q.push(State(d[n][to], n, to)); } } } return INF; } int main() { int N; while (cin >> N, N) { Point s, t; vector<Segment> segs(N); for (int i = 0; i < N; i++) { cin >> segs[i].s.x >> segs[i].s.y; cin >> segs[i].t.x >> segs[i].t.y; } cin >> s.x >> s.y >> t.x >> t.y; vector<Point> ps; Graph G = segmentArrangement(segs, ps); long double res = dijkstra(G, s, t, ps); if (res == INF) { cout << -1 << endl; } else { printf("%.12Lf\n", res); } } return 0; }
replace
67
68
67
68
MLE
p00620
C++
Time Limit Exceeded
#include <iostream> #include <map> #include <string> #include <vector> #define pb push_back using namespace std; typedef pair<int, int> P; int n; vector<P> rt; // map<vector<vector<int> >,bool> mp; int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; bool inf(int y, int x) { if (y >= 0 && y < n && x >= 0 && x < n) return true; return false; } bool cal(vector<vector<int>> &, int); bool f(vector<vector<int>> v, int y, int x, int left, int now) { if (!left) { return cal(v, now + 1); } for (int i = 0; i < 4; i++) { int ny = dy[i] + y, nx = dx[i] + x; if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) { int temp = v[ny][nx]; v[ny][nx] = 0; if (f(v, ny, nx, left - temp, now)) { return true; } v[ny][nx] = temp; } } return false; } bool cal(vector<vector<int>> &v, int now) { if (now == rt.size()) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (v[i][j] != 0) return false; return true; } int y = rt[now].first; int x = rt[now].second; int temp = v[y][x]; v[y][x] = 0; if (f(v, y, x, -temp, now)) return true; v[y][x] = temp; return false; } int main() { while (cin >> n, n) { vector<vector<int>> v(n, vector<int>(n)); rt.clear(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> v[i][j]; if (v[i][j] < 0) rt.pb(P(i, j)); } if (cal(v, 0)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <iostream> #include <map> #include <string> #include <vector> #define pb push_back using namespace std; typedef pair<int, int> P; int n; vector<P> rt; // map<vector<vector<int> >,bool> mp; int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; bool inf(int y, int x) { if (y >= 0 && y < n && x >= 0 && x < n) return true; return false; } bool cal(vector<vector<int>> &, int); bool f(vector<vector<int>> &v, int y, int x, int left, int now) { if (!left) { return cal(v, now + 1); } for (int i = 0; i < 4; i++) { int ny = dy[i] + y, nx = dx[i] + x; if (inf(ny, nx) && v[ny][nx] > 0 && left - v[ny][nx] >= 0) { int temp = v[ny][nx]; v[ny][nx] = 0; if (f(v, ny, nx, left - temp, now)) { return true; } v[ny][nx] = temp; } } return false; } bool cal(vector<vector<int>> &v, int now) { if (now == rt.size()) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (v[i][j] != 0) return false; return true; } int y = rt[now].first; int x = rt[now].second; int temp = v[y][x]; v[y][x] = 0; if (f(v, y, x, -temp, now)) return true; v[y][x] = temp; return false; } int main() { while (cin >> n, n) { vector<vector<int>> v(n, vector<int>(n)); rt.clear(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> v[i][j]; if (v[i][j] < 0) rt.pb(P(i, j)); } if (cal(v, 0)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
replace
17
18
17
18
TLE
p00620
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int field[8][8] = {{0}}; bool used[8][8] = {{false}}; vector<int> startX, startY; bool dfs(int n, int x, int y, int r, int k) { if (r == 0) { if (k == startX.size()) { return true; } x = startX[k]; y = startY[k]; ++k; } else if (used[y][x] || field[y][x] < 0) { return false; } r -= field[y][x]; if (r < 0) { return false; } used[y][x] = true; for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (x >= 0 && x < n && y >= 0 && y < n) { if (dfs(n, nx, ny, r, k)) { return true; } } } used[y][x] = false; return false; } int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> field[i][j]; sum += field[i][j]; if (field[i][j] < 0) { startX.push_back(j); startY.push_back(i); } used[i][j] = false; } } cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl; startX.clear(); startY.clear(); } return 0; }
#include <iostream> #include <vector> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int field[8][8] = {{0}}; bool used[8][8] = {{false}}; vector<int> startX, startY; bool dfs(int n, int x, int y, int r, int k) { if (r == 0) { if (k == startX.size()) { return true; } x = startX[k]; y = startY[k]; ++k; } else if (used[y][x] || field[y][x] < 0) { return false; } r -= field[y][x]; if (r < 0) { return false; } used[y][x] = true; if (r == 0) { if (dfs(n, 0, 0, 0, k)) { return true; } } else { for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (x >= 0 && x < n && y >= 0 && y < n) { if (dfs(n, nx, ny, r, k)) { return true; } } } } used[y][x] = false; return false; } int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> field[i][j]; sum += field[i][j]; if (field[i][j] < 0) { startX.push_back(j); startY.push_back(i); } used[i][j] = false; } } cout << (sum == 0 && dfs(n, 0, 0, 0, 0) ? "YES" : "NO") << endl; startX.clear(); startY.clear(); } return 0; }
replace
27
32
27
38
TLE
p00620
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) typedef pair<int, int> Point; typedef pair<int, Point> SP; typedef vector<vector<int>> Mat; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; int n; Mat mat; vector<vector<bool>> used; vector<SP> starts(0); bool dfs(int cur, int sum, int x, int y) { // cout << x << ", "<< y << ", " << sum << endl; if (starts[cur].first == sum) { // cout << "ok " << cur << endl; cur++; sum = 0; if (cur >= starts.size()) { return true; } x = starts[cur].second.second; y = starts[cur].second.first; } else if (starts[cur].first < sum) { return false; } for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) { used[ny][nx] = true; if (dfs(cur, sum + mat[ny][nx], nx, ny)) return true; used[ny][nx] = false; } } return false; } void solve() { mat = Mat(n, vector<int>(n)); used = vector<vector<bool>>(n, vector<bool>(n, false)); starts.clear(); int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; cin >> a; sum += a; if (a < 0) { a *= -1; used[i][j] = true; starts.push_back(SP(a, Point(i, j))); } mat[i][j] = a; } } bool ret = false; ; if (sum != 0) { ret = false; } else if (starts.size() == 1) { ret = true; } else { sort(starts.begin(), starts.end()); ret = dfs(0, 0, starts[0].second.second, starts[0].second.first); } cout << (ret ? "YES" : "NO") << endl; return; } int main() { while (true) { cin >> n; if (n == 0) break; solve(); } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) typedef pair<int, int> Point; typedef pair<int, Point> SP; typedef vector<vector<int>> Mat; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; int n; Mat mat; vector<vector<bool>> used; vector<SP> starts(0); bool dfs(int cur, int sum, int x, int y) { // cout << x << ", "<< y << ", " << sum << endl; if (starts[cur].first == sum) { // cout << "ok " << cur << endl; cur++; sum = 0; if (cur >= starts.size()) { return true; } x = starts[cur].second.second; y = starts[cur].second.first; } else if (starts[cur].first < sum) { return false; } for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < n && !used[ny][nx]) { used[ny][nx] = true; if (dfs(cur, sum + mat[ny][nx], nx, ny)) return true; used[ny][nx] = false; } } return false; } void solve() { mat = Mat(n, vector<int>(n)); used = vector<vector<bool>>(n, vector<bool>(n, false)); starts.clear(); int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; cin >> a; sum += a; if (a < 0) { a *= -1; used[i][j] = true; starts.push_back(SP(a, Point(i, j))); } mat[i][j] = a; } } bool ret = false; ; if (sum != 0) { ret = false; } else if (starts.size() == 1) { ret = true; } else { ret = dfs(0, 0, starts[0].second.second, starts[0].second.first); } cout << (ret ? "YES" : "NO") << endl; return; } int main() { while (true) { cin >> n; if (n == 0) break; solve(); } return 0; }
delete
73
75
73
73
TLE
p00621
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int d[110], w, q; int check(int wide) { int cnt = 0; rep(i, 0, w + 1) { if (d[i]) { cnt = 0; } else { cnt++; } if (cnt = wide) return i + 1 - cnt; } return -1; } signed main() { while (1) { cin >> w >> q; if (w + q == 0) break; char c; int id, wide; rep(i, 0, w) d[i] = 0; d[w] = 1; map<int, pii> mp; rep(i, 0, q) { cin >> c; if (c == 's') { cin >> id >> wide; mp[id].first = wide; int ind = check(wide); if (ind == -1) o("impossible"); else { o(ind); mp[id].second = ind; rep(j, 0, wide) { d[ind + j] = 1; } } } else { cin >> id; rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; } } } o("END"); } }
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; int d[110], w, q; int check(int wide) { int cnt = 0; rep(i, 0, w + 1) { if (d[i]) { cnt = 0; } else { cnt++; } if (cnt == wide) return i + 1 - cnt; } return -1; } signed main() { while (1) { cin >> w >> q; if (w + q == 0) break; char c; int id, wide; rep(i, 0, w) d[i] = 0; d[w] = 1; map<int, pii> mp; rep(i, 0, q) { cin >> c; if (c == 's') { cin >> id >> wide; mp[id].first = wide; int ind = check(wide); if (ind == -1) o("impossible"); else { o(ind); mp[id].second = ind; rep(j, 0, wide) { d[ind + j] = 1; } } } else { cin >> id; rep(j, 0, mp[id].first) { d[mp[id].second + j] = 0; } } } o("END"); } }
replace
20
21
20
21
TLE
p00622
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; string t, l, ad, ar, d, r; int ct, cl; char cc; void push_right() { r += cc; cc = l[cl++]; } void push_down() { d += cc; cc = t[ct++]; } bool rec(int s) { char tmpc; if (s == t.length() + l.length() - 1) { int f; if (d.length() != t.length()) { d += cc; f = 0; } else { r += cc; f = 1; } if (d == ad) { ar = r; return true; } if (f == 0) d = d.substr(0, d.length() - 1); else r = r.substr(0, r.length() - 1); return false; } if (cl < l.length()) { tmpc = cc; push_right(); if (rec(s + 1)) return true; r = r.substr(0, r.length() - 1); cl--; cc = tmpc; } if (ct < t.length()) { tmpc = cc; push_down(); if (rec(s + 1)) return true; d = d.substr(0, d.length() - 1); ct--; cc = tmpc; } return false; } string solve() { cc = l[0]; r = d = ""; ct = 0; cl = 1; if (rec(0)) { return ar; } else { return "NA"; } } int main() { while (1) { cin >> t; if (t == "-") break; cin >> l; cin >> ad; cout << solve() << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string t, l, ad, ar, d, r; int ct, cl; char cc; void push_right() { r += cc; cc = l[cl++]; } void push_down() { d += cc; cc = t[ct++]; } bool rec(int s) { char tmpc; if (s == t.length() + l.length() - 1) { int f; if (d.length() != t.length()) { d += cc; f = 0; } else { r += cc; f = 1; } if (d == ad) { ar = r; return true; } if (f == 0) d = d.substr(0, d.length() - 1); else r = r.substr(0, r.length() - 1); return false; } if (d.length() != 0 && d[d.length() - 1] != ad[d.length() - 1]) return false; if (cl < l.length()) { tmpc = cc; push_right(); if (rec(s + 1)) return true; r = r.substr(0, r.length() - 1); cl--; cc = tmpc; } if (ct < t.length()) { tmpc = cc; push_down(); if (rec(s + 1)) return true; d = d.substr(0, d.length() - 1); ct--; cc = tmpc; } return false; } string solve() { cc = l[0]; r = d = ""; ct = 0; cl = 1; if (rec(0)) { return ar; } else { return "NA"; } } int main() { while (1) { cin >> t; if (t == "-") break; cin >> l; cin >> ad; cout << solve() << endl; } return 0; }
insert
39
39
39
41
TLE
p00622
C++
Runtime Error
/* 2012-09-03T10:37:02 */ #define DEBUG_ON #define CONDITION true using namespace std; /*{{{*/ #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 <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <vector> #define INF (1e9) static const double PI = acos(-1.0); static const double EPS = 1e-10; typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<string> VS; typedef pair<int, int> PII; typedef complex<double> P; #define FOR(i, b, e) for (typeof(b) i = (b); i != (e); i < (e) ? ++i : --i) #define REP(i, n) FOR(i, 0, n) #define IFC(c) \ if (c) \ continue; #define IFB(c) \ if (c) \ break; #define IFR(c, r) \ if (c) \ return r; #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define PQ priority_queue #define F first #define S second #define MP(a, b) make_pair(a, b) #define SZ(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 EXIST(s, e) ((s).find(e) != (s).end()) #define BIT(n) (1ULL << (n)) #define BITOF(n, m) ((ULL)(n) >> (m)&1) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #ifdef DEBUG_ON #define dprt(fmt, ...) \ if (CONDITION) \ fprintf(stderr, fmt, __VA_ARGS__) #define darr(a) \ if (CONDITION) \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr_range(a, f, t) \ if (CONDITION) \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define dvec(v) \ if (CONDITION) \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr2(a, n, m) \ if (CONDITION) \ FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); } #define dvec2(v) \ if (CONDITION) \ FOR(i, 0, SZ(v)) { dvec((v)[i]); } #define WAIT() \ if (CONDITION) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (CONDITION) \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; #define dumpf() \ if (CONDITION) \ cerr << __PRETTY_FUNCTION__ << endl; #define dumpv(x) \ if (CONDITION) \ cerr << " [L:" << __LINE__ << "] " << #x << " = "; \ REP(q, (x).size()) cerr << (x)[q] << " "; \ cerr << endl; #define where() \ if (CONDITION) \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; #define show_bits(b, s) \ if (CONDITION) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } #else #define cerr \ if (0) \ cerr #define dprt(fmt, ...) #define darr(a) #define darr_range(a, f, t) #define dvec(v) #define darr2(a, n, m) #define dvec2(v) #define WAIT() #define dump(x) #define dumpf() #define dumpv(x) #define where() #define show_bits(b, s) #endif inline int onbits_count(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int bits_count(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c; } 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(); } inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { IFC(s[i] == ' '); if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } /*}}}*/ typedef vector<char> VC; int main() { string hor, ver, down; while (cin >> ver, ver != "-") { cin >> hor >> down; string ans = ""; int hi = 0, vi = 0, nex = 0; bool hmv = true; while (nex < down.length()) { dump(down[nex]); if (hmv) { dump(hi); dump(hor[hi]); while (hor[hi] != down[nex]) { ans.PB(hor[hi]); ++hi; } ++nex; ++hi; hmv = false; } else { dump(vi); dump(ver[vi]); while (ver[vi] == down[nex]) { ++nex; ++vi; } ans.PB(ver[vi]); ++vi; hmv = true; } } dump(ans); while (vi < ver.length()) { ans.PB(ver[vi]); ++vi; } dump(ans); while (hi < hor.length()) { ans.PB(hor[hi]); ++hi; } dump(ans); cout << ans << endl; } }
/* 2012-09-03T10:37:02 */ #define DEBUG_ON_ #define CONDITION true using namespace std; /*{{{*/ #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 <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <vector> #define INF (1e9) static const double PI = acos(-1.0); static const double EPS = 1e-10; typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<string> VS; typedef pair<int, int> PII; typedef complex<double> P; #define FOR(i, b, e) for (typeof(b) i = (b); i != (e); i < (e) ? ++i : --i) #define REP(i, n) FOR(i, 0, n) #define IFC(c) \ if (c) \ continue; #define IFB(c) \ if (c) \ break; #define IFR(c, r) \ if (c) \ return r; #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define PQ priority_queue #define F first #define S second #define MP(a, b) make_pair(a, b) #define SZ(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 EXIST(s, e) ((s).find(e) != (s).end()) #define BIT(n) (1ULL << (n)) #define BITOF(n, m) ((ULL)(n) >> (m)&1) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #ifdef DEBUG_ON #define dprt(fmt, ...) \ if (CONDITION) \ fprintf(stderr, fmt, __VA_ARGS__) #define darr(a) \ if (CONDITION) \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr_range(a, f, t) \ if (CONDITION) \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define dvec(v) \ if (CONDITION) \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl #define darr2(a, n, m) \ if (CONDITION) \ FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); } #define dvec2(v) \ if (CONDITION) \ FOR(i, 0, SZ(v)) { dvec((v)[i]); } #define WAIT() \ if (CONDITION) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (CONDITION) \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; #define dumpf() \ if (CONDITION) \ cerr << __PRETTY_FUNCTION__ << endl; #define dumpv(x) \ if (CONDITION) \ cerr << " [L:" << __LINE__ << "] " << #x << " = "; \ REP(q, (x).size()) cerr << (x)[q] << " "; \ cerr << endl; #define where() \ if (CONDITION) \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; #define show_bits(b, s) \ if (CONDITION) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } #else #define cerr \ if (0) \ cerr #define dprt(fmt, ...) #define darr(a) #define darr_range(a, f, t) #define dvec(v) #define darr2(a, n, m) #define dvec2(v) #define WAIT() #define dump(x) #define dumpf() #define dumpv(x) #define where() #define show_bits(b, s) #endif inline int onbits_count(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int bits_count(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c; } 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(); } inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { IFC(s[i] == ' '); if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } /*}}}*/ typedef vector<char> VC; int main() { string hor, ver, down; while (cin >> ver, ver != "-") { cin >> hor >> down; string ans = ""; int hi = 0, vi = 0, nex = 0; bool hmv = true; while (nex < down.length()) { dump(down[nex]); if (hmv) { dump(hi); dump(hor[hi]); while (hor[hi] != down[nex]) { ans.PB(hor[hi]); ++hi; } ++nex; ++hi; hmv = false; } else { dump(vi); dump(ver[vi]); while (ver[vi] == down[nex]) { ++nex; ++vi; } ans.PB(ver[vi]); ++vi; hmv = true; } } dump(ans); while (vi < ver.length()) { ans.PB(ver[vi]); ++vi; } dump(ans); while (hi < hor.length()) { ans.PB(hor[hi]); ++hi; } dump(ans); cout << ans << endl; } }
replace
1
2
1
2
0
[L127] down[nex] = c [L129] hi = 0 [L130] hor[hi] = c [L127] down[nex] = C [L140] vi = 0 [L141] ver[vi] = C [L127] down[nex] = a [L129] hi = 1 [L130] hor[hi] = b [L151] ans = Bb [L156] ans = BbA [L161] ans = BbA [L127] down[nex] = Z [L129] hi = 0 [L130] hor[hi] = Z [L151] ans = [L156] ans = X [L161] ans = XY
p00622
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; queue<char> to_queue(string s) { queue<char> q; for (int i = 0; i < (int)s.size(); ++i) { q.push(s[i]); } return q; } int main() { string red, green, down; while (cin >> red, red != "-") { cin >> green >> down; queue<char> r, g, d; r = to_queue(red); g = to_queue(green); d = to_queue(down); string res = ""; char c = g.front(); g.pop(); while (!r.empty() || !g.empty()) { cout << c << endl; if (c == d.front()) { d.pop(); c = r.front(); r.pop(); } else { res += c; c = g.front(); g.pop(); } } res += c; cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; queue<char> to_queue(string s) { queue<char> q; for (int i = 0; i < (int)s.size(); ++i) { q.push(s[i]); } return q; } int main() { string red, green, down; while (cin >> red, red != "-") { cin >> green >> down; queue<char> r, g, d; r = to_queue(red); g = to_queue(green); d = to_queue(down); string res = ""; char c = g.front(); g.pop(); while (!r.empty() || !g.empty()) { if (c == d.front()) { d.pop(); c = r.front(); r.pop(); } else { res += c; c = g.front(); g.pop(); } } res += c; cout << res << endl; } return 0; }
delete
25
26
25
25
0
p00623
C++
Runtime Error
#include <iostream> #include <string> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; string expr; int n, sweet[4], s, ans; int eval(int b, int e) { if (b + 1 == e) return sweet[expr[b] - '1']; int l, o, r, d; for (o = b, d = 0; o < e; o++) { char c = expr[o]; if (c == '(') d++; if (c == ')') d--; if (d == 0 && (c == '&' || c == '|' || c == '^')) break; } if (o == e && expr[b] == '(') return eval(b + 1, e - 1); l = eval(b, o), r = eval(o + 1, e); char c = expr[o]; return c != '&' ? c == '^' ? l ^ r : l | r : l & r; } void op(int c) { for (; c < s; c++) if (expr[c] == ' ') break; if (c < s) rep(i, 3) { expr[c] = "&|^"[i]; op(c + 1); expr[c] = ' '; } else if (eval(0, s) == 15) ans++; } int main() { while (getline(cin, expr), expr != "END") { s = expr.size(), ans = 0; cin >> n; rep(i, n) { int t; sweet[i] = 0; rep(j, 4) cin >> t, sweet[i] <<= 1, sweet[i] += t; } op(0); cout << ans << endl; cin.ignore(); } return 0; }
#include <iostream> #include <string> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; string expr; int n, sweet[10], s, ans; int eval(int b, int e) { if (b + 1 == e) return sweet[expr[b] - '1']; int l, o, r, d; for (o = b, d = 0; o < e; o++) { char c = expr[o]; if (c == '(') d++; if (c == ')') d--; if (d == 0 && (c == '&' || c == '|' || c == '^')) break; } if (o == e && expr[b] == '(') return eval(b + 1, e - 1); l = eval(b, o), r = eval(o + 1, e); char c = expr[o]; return c != '&' ? c == '^' ? l ^ r : l | r : l & r; } void op(int c) { for (; c < s; c++) if (expr[c] == ' ') break; if (c < s) rep(i, 3) { expr[c] = "&|^"[i]; op(c + 1); expr[c] = ' '; } else if (eval(0, s) == 15) ans++; } int main() { while (getline(cin, expr), expr != "END") { s = expr.size(), ans = 0; cin >> n; rep(i, n) { int t; sweet[i] = 0; rep(j, 4) cin >> t, sweet[i] <<= 1, sweet[i] += t; } op(0); cout << ans << endl; cin.ignore(); } return 0; }
replace
5
6
5
6
0
p00623
C++
Runtime Error
#include <ctype.h> #include <map> #include <stdio.h> #include <vector> typedef std::map<int, int> M; typedef M::iterator I; typedef std::pair<int, int> P; std::vector<P> v; int x[10]; int F(char *&p, int n) { if (isdigit(*p)) return -*p + '0'; v.push_back(P(0, 0)); int s = v.size(); v[n].first = F(++p, s); s = v.size(); v[n].second = F(p += 2, s); ++p; return n; } M G(int i) { M m; if (i < 0) m[x[-i]] = 1; else { M a(G(v[i].first)), b(G(v[i].second)); for (I p = a.begin(); p != a.end(); ++p) for (I q = b.begin(); q != b.end(); ++q) { int a = p->first, b = q->first, c = p->second * q->second; m[a & b] += c; m[a | b] += c; m[a ^ b] += c; } } return m; } int main() { int n, i, j, t; char *p, s[512]; while (fgets(s, 512, stdin), *s - 'E') { v.clear(); scanf("%d", &n); for (i = 1; i <= n; ++i) for (x[i] = j = 0; j < 4; ++j) scanf("%d", &t), x[i] = x[i] * 2 + t; t = F(p = s, 0); printf("%d\n", t ? x[-t] == 15 : G(0)[15]); fgets(s, 512, stdin); } return 0; }
#include <ctype.h> #include <map> #include <stdio.h> #include <vector> typedef std::map<int, int> M; typedef M::iterator I; typedef std::pair<int, int> P; std::vector<P> v; int x[10]; int F(char *&p, int n) { if (isdigit(*p)) return -*p + '0'; v.push_back(P(0, 0)); int s = v.size(); v[n].first = F(++p, s); s = v.size(); v[n].second = F(p += 2, s); ++p; return n; } M G(int i) { M m; if (i < 0) m[x[-i]] = 1; else { M a(G(v[i].first)), b(G(v[i].second)); for (I p = a.begin(); p != a.end(); ++p) for (I q = b.begin(); q != b.end(); ++q) { int a = p->first, b = q->first, c = p->second * q->second; m[a & b] += c; m[a | b] += c; m[a ^ b] += c; } } return m; } int main() { int n, i, j, t; char *p, s[512]; v.reserve(64); while (fgets(s, 512, stdin), *s - 'E') { v.clear(); scanf("%d", &n); for (i = 1; i <= n; ++i) for (x[i] = j = 0; j < 4; ++j) scanf("%d", &t), x[i] = x[i] * 2 + t; t = F(p = s, 0); printf("%d\n", t ? x[-t] == 15 : G(0)[15]); fgets(s, 512, stdin); } return 0; }
insert
39
39
39
40
0
p00623
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; int x[15]; int type[15]; string s; int num; int parse(int l, int r) { if (r - l == 1) return x[s[l] - '1']; int sp = -1; for (int i = l; i < r; ++i) { if (s[i] == ' ') sp = i; } int res1 = parse(l + 1, sp); int res2 = parse(sp + 1, r - 1); int ret = -1; if (type[num] == 0) ret = res1 | res2; if (type[num] == 1) ret = res1 & res2; if (type[num] == 2) ret = res1 ^ res2; ++num; return ret; } int ans; int m; void rec(int n) { if (n == m) { num = 0; // cout << parse(0, s.size()) << endl; ans += parse(0, s.size()) == 15; return; } rep(i, 3) { type[n] = i; rec(n + 1); } } int main() { while (getline(cin, s), s != "END") { int n; cin >> n; MEMSET(x, 0); rep(i, n) rep(j, 4) { int t; cin >> t; x[i] |= (t << j); } ans = 0; m = count(ALL(s), ' '); rec(0); cout << ans << endl; cin.ignore(); } return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; int x[15]; int type[15]; string s; int num; int parse(int l, int r) { if (r - l == 1) return x[s[l] - '1']; int sp = -1, nest = 0; for (int i = l + 1; i < r - 1; ++i) { if (s[i] == '(') ++nest; if (s[i] == ')') --nest; if (!nest && s[i] == ' ') sp = i; } int res1 = parse(l + 1, sp); int res2 = parse(sp + 1, r - 1); int ret = -1; if (type[num] == 0) ret = res1 | res2; if (type[num] == 1) ret = res1 & res2; if (type[num] == 2) ret = res1 ^ res2; ++num; return ret; } int ans; int m; void rec(int n) { if (n == m) { num = 0; // cout << parse(0, s.size()) << endl; ans += parse(0, s.size()) == 15; return; } rep(i, 3) { type[n] = i; rec(n + 1); } } int main() { while (getline(cin, s), s != "END") { int n; cin >> n; MEMSET(x, 0); rep(i, n) rep(j, 4) { int t; cin >> t; x[i] |= (t << j); } ans = 0; m = count(ALL(s), ' '); rec(0); cout << ans << endl; cin.ignore(); } return 0; }
replace
43
46
43
50
0
p00625
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define EQ(a, b) (abs((a) - (b)) < EPS) using namespace std; typedef long double D; const D EPS = 1e-8; D getTime(D dx, D dy, D v, D fpx, D fpy, D fvx, D fvy) { D px = fpx - dx, py = fpy - dy; D a = fvx * fvx + fvy * fvy - v * v; D b = 2 * fvx * px + 2 * fvy * py; D c = px * px + py * py; if (EQ(a, 0)) { if (EQ(b, 0)) return EQ(c, 0) ? 0 : -1; else return (-c / b < -EPS) ? -1 : -c / b; } else { D d = b * b - 4 * a * c; if (d < -EPS) return -1; if (EQ(d, 0)) return (-b / 2 / a < -EPS) ? -1 : -b / 2 / a; D t1 = (-b - sqrt(d)) / 2 / a, t2 = (-b + sqrt(d)) / 2 / a; if (t1 < -EPS) return (t2 < -EPS) ? -1 : t2; return t1; } } int main() { int n, m; while (scanf("%d%d", &n, &m), n) { vector<D> dx(n), dy(n), v(n); rep(i, n) scanf("%Lf%Lf%Lf", &dx[i], &dy[i], &v[i]); vector<int> cnt(n, 0); rep(i, m) { D fpx, fpy, fvx, fvy; scanf("%Lf%Lf%Lf%Lf", &fpx, &fpy, &fvx, &fvy); D minT = 1e10, minID = -1; vector<D> t(n); rep(i, n) { t[i] = getTime(dx[i], dy[i], v[i], fpx, fpy, fvx, fvy); if (t[i] > -EPS && minT > t[i] + EPS) { minT = t[i]; minID = i; } } cnt[minID]++; rep(i, n) { if (t[i] > -EPS) { dx[i] += v[i] * minT; dy[i] += v[i] * minT; } } } rep(i, n) printf("%d%c", cnt[i], i == n - 1 ? '\n' : ' '); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define EQ(a, b) (abs((a) - (b)) < EPS) using namespace std; typedef long double D; const D EPS = 1e-8; D getTime(D dx, D dy, D v, D fpx, D fpy, D fvx, D fvy) { D px = fpx - dx, py = fpy - dy; D a = fvx * fvx + fvy * fvy - v * v; D b = 2 * fvx * px + 2 * fvy * py; D c = px * px + py * py; if (EQ(a, 0)) { if (EQ(b, 0)) return EQ(c, 0) ? 0 : -1; else return (-c / b < -EPS) ? -1 : -c / b; } else { D d = b * b - 4 * a * c; if (d < -EPS) return -1; if (EQ(d, 0)) return (-b / 2 / a < -EPS) ? -1 : -b / 2 / a; D t1 = (-b - sqrt(d)) / 2 / a, t2 = (-b + sqrt(d)) / 2 / a; if (t1 < -EPS) return (t2 < -EPS) ? -1 : t2; return t1; } } int main() { int n, m; while (scanf("%d%d", &n, &m), n) { vector<D> dx(n), dy(n), v(n); rep(i, n) scanf("%Lf%Lf%Lf", &dx[i], &dy[i], &v[i]); vector<int> cnt(n, 0); rep(i, m) { D fpx, fpy, fvx, fvy; scanf("%Lf%Lf%Lf%Lf", &fpx, &fpy, &fvx, &fvy); D minT = 1e10, minID = -1; vector<D> t(n); rep(i, n) { t[i] = getTime(dx[i], dy[i], v[i], fpx, fpy, fvx, fvy); if (t[i] > -EPS && minT > t[i] + EPS) { minT = t[i]; minID = i; } } cnt[minID]++; rep(i, n) { if (t[i] > -EPS) { D sx = fpx + fvx * t[i] - dx[i], sy = fpy + fvy * t[i] - dy[i]; D absS = sqrt(sx * sx + sy * sy); if (EQ(absS, 0)) continue; sx /= absS; sy /= absS; dx[i] += sx * v[i] * minT; dy[i] += sy * v[i] * minT; } } } rep(i, n) printf("%d%c", cnt[i], i == n - 1 ? '\n' : ' '); } }
replace
55
57
55
63
0
p00626
C++
Memory Limit Exceeded
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define watch(a) \ { cout << #a << " = " << a << endl; } template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; typedef vector<int> vec; typedef vector<vec> matrix; typedef int weight; weight OPT[1 << 20][40]; weight minimum_steiner_tree(const vector<int> &T, const matrix &g) { const int n = g.size(); const int numT = T.size(); if (numT <= 1) return 0; matrix d(g); // all-pair shortest for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); for (int S = 0; S < (1 << numT); ++S) for (int x = 0; x < n; ++x) OPT[S][x] = inf; for (int p = 0; p < numT; ++p) // trivial case for (int q = 0; q < n; ++q) OPT[1 << p][q] = d[T[p]][q]; for (int S = 1; S < (1 << numT); ++S) { // DP step if (!(S & (S - 1))) continue; for (int p = 0; p < n; ++p) for (int E = 0; E < S; ++E) if ((E | S) == S) OPT[S][p] = min(OPT[S][p], OPT[E][p] + OPT[S - E][p]); for (int p = 0; p < n; ++p) for (int q = 0; q < n; ++q) OPT[S][p] = min(OPT[S][p], OPT[S][q] + d[p][q]); } weight ans = inf; for (int S = 0; S < (1 << numT); ++S) for (int q = 0; q < n; ++q) ans = min(ans, OPT[S][q] + OPT[((1 << numT) - 1) - S][q]); return ans; } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; template <class T> constexpr bool in_range(T y, T x, T H, T W) { return 0 <= y && y < H && 0 <= x && x < W; } int main() { for (int H, W; cin >> H >> W && (H | W);) { zero(OPT); matrix mat(H, vec(W)); rep(i, H) rep(j, W) cin >> mat[i][j]; vector<int> T; rep(i, H) rep(j, W) if (mat[i][j]) T.push_back(i * W + j); matrix mat2(H * W, vec(H * W, inf)); rep(i, H) { rep(j, W) { mat2[i * W + j][i * W + j] = 0; rep(k, 4) { const int ni = i + dy[k], nj = j + dx[k]; if (in_range(ni, nj, H, W)) { mat2[i * W + j][ni * W + nj] /*= mat2[ni*W+nj][i*W+j]*/ = 1; } } } } // rep(i, H*W) rep(j, W) printf("%10d%c", mat2[i][j], j < W-1 ? ' ' : // '\n'); int ms = minimum_steiner_tree(T, mat2); cout << H * W - (1 + ms) << endl; } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define REP(i, a, b) for (int i = a; i < (int)b; i++) #define rep(i, n) REP(i, 0, n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define watch(a) \ { cout << #a << " = " << a << endl; } template <class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1 << 29; typedef vector<int> vec; typedef vector<vec> matrix; typedef int weight; weight OPT[1 << 7][12 * 12 + 10]; weight minimum_steiner_tree(const vector<int> &T, const matrix &g) { const int n = g.size(); const int numT = T.size(); if (numT <= 1) return 0; matrix d(g); // all-pair shortest for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); for (int S = 0; S < (1 << numT); ++S) for (int x = 0; x < n; ++x) OPT[S][x] = inf; for (int p = 0; p < numT; ++p) // trivial case for (int q = 0; q < n; ++q) OPT[1 << p][q] = d[T[p]][q]; for (int S = 1; S < (1 << numT); ++S) { // DP step if (!(S & (S - 1))) continue; for (int p = 0; p < n; ++p) for (int E = 0; E < S; ++E) if ((E | S) == S) OPT[S][p] = min(OPT[S][p], OPT[E][p] + OPT[S - E][p]); for (int p = 0; p < n; ++p) for (int q = 0; q < n; ++q) OPT[S][p] = min(OPT[S][p], OPT[S][q] + d[p][q]); } weight ans = inf; for (int S = 0; S < (1 << numT); ++S) for (int q = 0; q < n; ++q) ans = min(ans, OPT[S][q] + OPT[((1 << numT) - 1) - S][q]); return ans; } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; template <class T> constexpr bool in_range(T y, T x, T H, T W) { return 0 <= y && y < H && 0 <= x && x < W; } int main() { for (int H, W; cin >> H >> W && (H | W);) { zero(OPT); matrix mat(H, vec(W)); rep(i, H) rep(j, W) cin >> mat[i][j]; vector<int> T; rep(i, H) rep(j, W) if (mat[i][j]) T.push_back(i * W + j); matrix mat2(H * W, vec(H * W, inf)); rep(i, H) { rep(j, W) { mat2[i * W + j][i * W + j] = 0; rep(k, 4) { const int ni = i + dy[k], nj = j + dx[k]; if (in_range(ni, nj, H, W)) { mat2[i * W + j][ni * W + nj] /*= mat2[ni*W+nj][i*W+j]*/ = 1; } } } } // rep(i, H*W) rep(j, W) printf("%10d%c", mat2[i][j], j < W-1 ? ' ' : // '\n'); int ms = minimum_steiner_tree(T, mat2); cout << H * W - (1 + ms) << endl; } return 0; }
replace
46
47
46
47
MLE
p00626
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> PI; #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, n) rep(i, n) #define F first #define S second #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define min3(a, b, c) min((a), min((b), (c))) #define min4(a, b, c, d) min((a), min3((b), (c), (d))) #define SZ(a) (int)((a).size()) #define ALL(a) a.begin(), a.end() #define FLL(a, b) memset((a), b, sizeof(a)) #define CLR(a) memset((a), 0, sizeof(a)) #define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) template <typename T, typename U> ostream &operator<<(ostream &out, const pair<T, U> &val) { return out << "(" << val.F << ", " << val.S << ")"; } template <class T> ostream &operator<<(ostream &out, const vector<T> &val) { out << "{"; rep(i, SZ(val)) out << (i ? ", " : "") << val[i]; return out << "}"; } typedef double FP; typedef complex<FP> pt; typedef pt P; typedef pair<pt, pt> line; FP dot(P a, P b) { return real(conj(a) * b); } FP crs(P a, P b) { return imag(conj(a) * b); } P ortho(P a) { return a * P(0, 1); } P ortho(line a) { return ortho(a.S - a.F); } P crspt(P a, P b, P c, P d) { b -= a, d -= c; return a + b * crs(d, c - a) / crs(d, b); } P crspt(line a, line b) { return crspt(a.F, a.S, b.F, b.S); } bool onl(P a1, P a2, P b) { return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS; } bool onl(line a, P b) { return onl(a.F, a.S, b); } bool iscrs(line a, line b) { P c = crspt(a, b); return onl(a, c) && onl(b, c); } void pkuassert(bool t) { t = 1 / t; }; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; enum { TOP, BTM, LFT, RGT, FRT, BCK }; int dxdy2ce[] = {RGT, FRT, LFT, BCK}; template <class T> T shift(T a, int b, int c, int d, int e) { __typeof(a[0]) t = a[b]; a[b] = a[c]; a[c] = a[d]; a[d] = a[e]; a[e] = t; return a; } template <class T> T rgt(T a) { return shift(a, TOP, LFT, BTM, RGT); } template <class T> T lft(T a) { return shift(a, TOP, RGT, BTM, LFT); } template <class T> T frt(T a) { return shift(a, TOP, BCK, BTM, FRT); } template <class T> T bck(T a) { return shift(a, TOP, FRT, BTM, BCK); } line mkl(P a, P v) { return line(a, a + v); } FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); } FP spdist(line a, P b) { P c(crspt(a, mkl(b, ortho(a)))); return onl(a, c) ? abs(b - c) : min(abs(a.F - b), abs(a.S - b)); } FP ssdist(line a, line b) { return iscrs(a, b) ? 0. : min4(spdist(a, b.F), spdist(a, b.S), spdist(b, a.F), spdist(b, a.S)); } namespace std { bool operator<(const P &a, const P &b) { return mp(real(a), imag(a)) < mp(real(b), imag(b)); } } // namespace std int h, w; int in[12][12]; PI rinsetu[12 * 12]; int rsz; bool isrinsetu[12][12]; bool issh[12][12]; vector<PI> choco; PI tree[12 * 12]; int tsz = 0; int row[12], col[12]; int maxdist[6]; bool iddfs(int depth, int ne, int ch) { if (depth == 0) return ch == SZ(choco); if (ne) rep(j, SZ(choco)) if (depth < maxdist[j]) return false; if (ne && ch == SZ(choco) - 1) return true; int crsz = rsz; int back[6]; rep(i, SZ(choco)) back[i] = maxdist[i]; for (int i = ne; i < crsz; ++i) { PI node = rinsetu[i]; if (!row[node.F] && !col[node.S]) continue; bool flag[4] = {}; int nu = 0; rep(k, 4) { int nx = node.F + dx[k]; int ny = node.S + dy[k]; if (min(nx, ny) < 0 || nx >= h || ny >= w) continue; nu += issh[nx][ny]; if (isrinsetu[nx][ny]) continue; isrinsetu[nx][ny] = true; flag[k] = true; rinsetu[rsz++] = PI(nx, ny); } issh[node.F][node.S] = true; tree[tsz++] = node; int ok = 0; // bool ok=false; rep(j, SZ(choco)) { int b = maxdist[j]; maxdist[j] = min(maxdist[j], abs(node.F - choco[j].F) + abs(node.S - choco[j].S)); ok |= (b != maxdist[j]) << j; } if ((nu < 2 || in[node.F][node.S]) && ok && iddfs(depth - 1, i + 1, ch + in[node.F][node.S])) return true; rep(j, SZ(choco)) maxdist[j] = back[j]; --tsz; rep(k, 4) if (flag[k]) { int nx = node.F + dx[k]; int ny = node.S + dy[k]; isrinsetu[nx][ny] = false; } rsz = crsz; issh[node.F][node.S] = false; if (in[node.F][node.S]) return false; } return false; } void solve() { CLR(row); CLR(col); int low = 0; choco.clear(); rep(i, h) rep(j, w) { cin >> in[i][j]; low += in[i][j]; row[i] += in[i][j]; col[j] += in[i][j]; if (in[i][j]) choco.pb(mp(i, j)); } if (SZ(choco) < 1) { cout << h * w << endl; return; } int maxdist = 0; rep(i, SZ(choco)) rep(j, i) maxdist = max(maxdist, abs(choco[i].F - choco[j].F) + abs(choco[i].S - choco[j].S)); low = max(low, maxdist); for (int depth = low; depth <= h * w; ++depth) { cerr << depth << endl; CLR(isrinsetu); CLR(issh); rsz = 0; int idx = 0; rand() % SZ(choco); rinsetu[rsz++] = choco[idx]; // cerr << choco[idx] << endl; isrinsetu[choco[idx].F][choco[idx].S] = 1; tsz = 0; rep(i, SZ(choco))::maxdist[i] = 140; if (iddfs(depth, 0, 0)) { cout << h * w - depth << endl; return; } } } int main(int argc, char *argv[]) { while (cin >> h >> w && h) solve(); }
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<int, int> PI; #define EPS (1e-10) #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define REP(i, n) rep(i, n) #define F first #define S second #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define min3(a, b, c) min((a), min((b), (c))) #define min4(a, b, c, d) min((a), min3((b), (c), (d))) #define SZ(a) (int)((a).size()) #define ALL(a) a.begin(), a.end() #define FLL(a, b) memset((a), b, sizeof(a)) #define CLR(a) memset((a), 0, sizeof(a)) #define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) template <typename T, typename U> ostream &operator<<(ostream &out, const pair<T, U> &val) { return out << "(" << val.F << ", " << val.S << ")"; } template <class T> ostream &operator<<(ostream &out, const vector<T> &val) { out << "{"; rep(i, SZ(val)) out << (i ? ", " : "") << val[i]; return out << "}"; } typedef double FP; typedef complex<FP> pt; typedef pt P; typedef pair<pt, pt> line; FP dot(P a, P b) { return real(conj(a) * b); } FP crs(P a, P b) { return imag(conj(a) * b); } P ortho(P a) { return a * P(0, 1); } P ortho(line a) { return ortho(a.S - a.F); } P crspt(P a, P b, P c, P d) { b -= a, d -= c; return a + b * crs(d, c - a) / crs(d, b); } P crspt(line a, line b) { return crspt(a.F, a.S, b.F, b.S); } bool onl(P a1, P a2, P b) { return abs(b - a1) + abs(b - a2) < abs(a1 - a2) + EPS; } bool onl(line a, P b) { return onl(a.F, a.S, b); } bool iscrs(line a, line b) { P c = crspt(a, b); return onl(a, c) && onl(b, c); } void pkuassert(bool t) { t = 1 / t; }; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; enum { TOP, BTM, LFT, RGT, FRT, BCK }; int dxdy2ce[] = {RGT, FRT, LFT, BCK}; template <class T> T shift(T a, int b, int c, int d, int e) { __typeof(a[0]) t = a[b]; a[b] = a[c]; a[c] = a[d]; a[d] = a[e]; a[e] = t; return a; } template <class T> T rgt(T a) { return shift(a, TOP, LFT, BTM, RGT); } template <class T> T lft(T a) { return shift(a, TOP, RGT, BTM, LFT); } template <class T> T frt(T a) { return shift(a, TOP, BCK, BTM, FRT); } template <class T> T bck(T a) { return shift(a, TOP, FRT, BTM, BCK); } line mkl(P a, P v) { return line(a, a + v); } FP lpdist(line a, P b) { return abs(b - crspt(a, mkl(b, ortho(a)))); } FP spdist(line a, P b) { P c(crspt(a, mkl(b, ortho(a)))); return onl(a, c) ? abs(b - c) : min(abs(a.F - b), abs(a.S - b)); } FP ssdist(line a, line b) { return iscrs(a, b) ? 0. : min4(spdist(a, b.F), spdist(a, b.S), spdist(b, a.F), spdist(b, a.S)); } namespace std { bool operator<(const P &a, const P &b) { return mp(real(a), imag(a)) < mp(real(b), imag(b)); } } // namespace std int h, w; int in[12][12]; PI rinsetu[12 * 12]; int rsz; bool isrinsetu[12][12]; bool issh[12][12]; vector<PI> choco; PI tree[12 * 12]; int tsz = 0; int row[12], col[12]; int maxdist[6]; bool iddfs(int depth, int ne, int ch) { if (depth == 0) return ch == SZ(choco); if (ne) rep(j, SZ(choco)) if (depth < maxdist[j]) return false; if (ne && ch == SZ(choco) - 1) return true; int crsz = rsz; int back[6]; rep(i, SZ(choco)) back[i] = maxdist[i]; for (int i = ne; i < crsz; ++i) { PI node = rinsetu[i]; if (!row[node.F] && !col[node.S]) continue; bool flag[4] = {}; int nu = 0; rep(k, 4) { int nx = node.F + dx[k]; int ny = node.S + dy[k]; if (min(nx, ny) < 0 || nx >= h || ny >= w) continue; nu += issh[nx][ny]; if (isrinsetu[nx][ny]) continue; isrinsetu[nx][ny] = true; flag[k] = true; rinsetu[rsz++] = PI(nx, ny); } issh[node.F][node.S] = true; tree[tsz++] = node; int ok = 0; // bool ok=false; rep(j, SZ(choco)) { int b = maxdist[j]; maxdist[j] = min(maxdist[j], abs(node.F - choco[j].F) + abs(node.S - choco[j].S)); ok |= (b != maxdist[j]) << j; } if ((nu < 2 || in[node.F][node.S]) && ok && iddfs(depth - 1, i + 1, ch + in[node.F][node.S])) return true; rep(j, SZ(choco)) maxdist[j] = back[j]; --tsz; rep(k, 4) if (flag[k]) { int nx = node.F + dx[k]; int ny = node.S + dy[k]; isrinsetu[nx][ny] = false; } rsz = crsz; issh[node.F][node.S] = false; if (in[node.F][node.S]) return false; } return false; } void solve() { CLR(row); CLR(col); int low = 0; choco.clear(); rep(i, h) rep(j, w) { cin >> in[i][j]; low += in[i][j]; row[i] += in[i][j]; col[j] += in[i][j]; if (in[i][j]) choco.pb(mp(i, j)); } if (SZ(choco) < 1) { cout << h * w << endl; return; } int maxdist = 0; rep(i, SZ(choco)) rep(j, i) maxdist = max(maxdist, abs(choco[i].F - choco[j].F) + abs(choco[i].S - choco[j].S)); low = max(low, maxdist); for (int depth = low; depth <= h * w; ++depth) { // cerr << depth << endl; CLR(isrinsetu); CLR(issh); rsz = 0; int idx = 0; rand() % SZ(choco); rinsetu[rsz++] = choco[idx]; // cerr << choco[idx] << endl; isrinsetu[choco[idx].F][choco[idx].S] = 1; tsz = 0; rep(i, SZ(choco))::maxdist[i] = 140; if (iddfs(depth, 0, 0)) { cout << h * w - depth << endl; return; } } } int main(int argc, char *argv[]) { while (cin >> h >> w && h) solve(); }
replace
199
200
199
200
0
6 7 8 9 1 3 4
p00627
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int s = 0, a; while (n--) { cin >> a; s += a; } cout << s << endl; } }
#include <iostream> using namespace std; int main() { int n; while (cin >> n, n) { int s = 0, a; while (n) { n -= 4; cin >> a; s += a; } cout << s << endl; } }
replace
6
7
6
8
TLE
p00627
C++
Runtime Error
#include <iostream> using namespace std; int main() { while (1) { int n, k[5], ans = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < n / 4; ++i) { cin >> k[i]; ans += k[i]; } cout << ans << endl; } }
#include <iostream> using namespace std; int main() { while (1) { int n, k[100005], ans = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < n / 4; ++i) { cin >> k[i]; ans += k[i]; } cout << ans << endl; } }
replace
4
5
4
5
0
p00627
C++
Time Limit Exceeded
#include <iostream> using namespace std; int n, a, sum; int main() { while (true) { sum = 0; cin >> n; n /= 4; for (int i = 0; i < n; i++) { cin >> a; sum += a; } cout << sum << endl; } return 0; }
#include <iostream> using namespace std; int n, a, sum; int main() { while (true) { sum = 0; cin >> n; if (!n) { break; } n /= 4; for (int i = 0; i < n; i++) { cin >> a; sum += a; } cout << sum << endl; } return 0; }
insert
7
7
7
10
TLE
p00627
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <string.h> #include <vector> using namespace std; const int mod = 1000000007; int main() { int n; while (cin >> n, n) { int a, b; while (n--) { cin >> a; b += a; } cout << b << endl; } }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <string.h> #include <vector> using namespace std; const int mod = 1000000007; int main() { int n; while (cin >> n, n) { int a = 0, b = 0; n /= 4; while (n--) { cin >> a; b += a; } cout << b << endl; } }
replace
16
17
16
18
TLE
p00627
C++
Time Limit Exceeded
#include <stdio.h> int main(void) { int s, n, i, x; while (1) { scanf("%d", &n); s = 0; for (i = 0; i < n / 4; i++) { scanf("%d", &x); s += x; } printf("%d\n", s); } return 0; }
#include <stdio.h> int main(void) { int s, n, i, x; while (1) { scanf("%d", &n); if (!n) break; s = 0; for (i = 0; i < n / 4; i++) { scanf("%d", &x); s += x; } printf("%d\n", s); } return 0; }
insert
6
6
6
8
TLE
p00627
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int n; for (; cin >> n, n;) { int sum = 0; for (;; n = n / 5) { int tmp; cin >> tmp; sum += tmp; } cout << sum << endl; } }
#include <iostream> using namespace std; int main() { int n; for (; cin >> n, n;) { int sum = 0; for (; n > 0; n = n - 4) { int tmp; cin >> tmp; sum += tmp; } cout << sum << endl; } }
replace
6
7
6
7
TLE
p00628
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; string S, T; int chain; int main() { while (true) { getline(cin, S); S += ' '; T = ""; if (S == "END OF INPUT") { break; } for (int i = 0; i < S.size(); i++) { if (S[i] != ' ') { chain++; } else { T += to_string(chain); chain = 0; } } cout << T << endl; } }
#include <iostream> #include <string> using namespace std; string S, T; int chain; int main() { while (true) { getline(cin, S); S += ' '; T = ""; if (S == "END OF INPUT ") { break; } for (int i = 0; i < S.size(); i++) { if (S[i] != ' ') { chain++; } else { T += to_string(chain); chain = 0; } } cout << T << endl; } }
replace
10
11
10
11
TLE
p00629
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> I, U, A, P, C; while (true) { cin >> N; if (N == 0) { break; } for (int i = 0; i < N; i++) { cin >> I[i] >> U[i] >> A[i] >> P[i]; } C = vector<int>(1001, 0); for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (A[i] < A[j] || (A[i] == A[j] && P[i] > P[j])) { swap(A[i], A[j]); swap(P[i], P[j]); swap(I[i], I[j]); swap(U[i], U[j]); } } } int count_ = 0; for (int i = 0; i < N; i++) { if ((count_ < 10 && C[U[i]] < 3) || (count_ < 20 && C[U[i]] < 2) || (count_ < 26 && C[U[i]] == 0)) { cout << I[i] << endl; C[U[i]]++; count_++; } } } return 0; }
#include <iostream> #include <vector> using namespace std; int main() { int N; vector<int> I, U, A, P, C; while (true) { cin >> N; if (N == 0) { break; } I = vector<int>(N); U = vector<int>(N); A = vector<int>(N); P = vector<int>(N); for (int i = 0; i < N; i++) { cin >> I[i] >> U[i] >> A[i] >> P[i]; } C = vector<int>(1001, 0); for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (A[i] < A[j] || (A[i] == A[j] && P[i] > P[j])) { swap(A[i], A[j]); swap(P[i], P[j]); swap(I[i], I[j]); swap(U[i], U[j]); } } } int count_ = 0; for (int i = 0; i < N; i++) { if ((count_ < 10 && C[U[i]] < 3) || (count_ < 20 && C[U[i]] < 2) || (count_ < 26 && C[U[i]] == 0)) { cout << I[i] << endl; C[U[i]]++; count_++; } } } return 0; }
insert
15
15
15
20
-11
p00630
C++
Runtime Error
#include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; void UpperCamelCase(string &); void LowerCamelCase(string &); void UnderScoreCase(string &); bool IsUpperCase(char); int main() { char c; string str; while (1) { cin >> str >> c; if (c == 'X') break; else if (c == 'U') UpperCamelCase(str); else if (c == 'L') LowerCamelCase(str); else if (c == 'D') UnderScoreCase(str); cout << str << endl; } return 0; } void UpperCamelCase(string &s) { string::iterator i; if (s[0] >= 'a' && s[0] <= 'z') s[0] = toupper(s[0]); i = s.begin(); while (1) { i = find(i, s.end(), '_'); if (i == s.end()) break; i = s.erase(i); *i = toupper(*i); } } void LowerCamelCase(string &s) { string::iterator i; if (s[0] >= 'A' && s[0] <= 'Z') s[0] = tolower(s[0]); i = s.begin(); while (1) { i = find(i, s.end(), '_'); if (i == s.end()) break; i = s.erase(i); *i = toupper(*i); } } void UnderScoreCase(string &s) { string::iterator i; if (s[0] >= 'A' && s[0] <= 'Z') s[0] = tolower(s[0]); i = s.begin(); while (1) { i = find_if(i, s.end(), IsUpperCase); if (i == s.end()) break; *i = tolower(*i); s.insert(i, '_'); } } bool IsUpperCase(char c) { if (c >= 'A' && c <= 'Z') return true; else return false; }
#include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; void UpperCamelCase(string &); void LowerCamelCase(string &); void UnderScoreCase(string &); bool IsUpperCase(char); int main() { char c; string str; while (1) { cin >> str >> c; if (c == 'X') break; else if (c == 'U') UpperCamelCase(str); else if (c == 'L') LowerCamelCase(str); else if (c == 'D') UnderScoreCase(str); cout << str << endl; } return 0; } void UpperCamelCase(string &s) { string::iterator i; if (s[0] >= 'a' && s[0] <= 'z') s[0] = toupper(s[0]); i = s.begin(); while (1) { i = find(i, s.end(), '_'); if (i == s.end()) break; i = s.erase(i); *i = toupper(*i); } } void LowerCamelCase(string &s) { string::iterator i; if (s[0] >= 'A' && s[0] <= 'Z') s[0] = tolower(s[0]); i = s.begin(); while (1) { i = find(i, s.end(), '_'); if (i == s.end()) break; i = s.erase(i); *i = toupper(*i); } } void UnderScoreCase(string &s) { string::iterator i; if (s[0] >= 'A' && s[0] <= 'Z') s[0] = tolower(s[0]); i = s.begin(); while (1) { i = find_if(i, s.end(), IsUpperCase); if (i == s.end()) break; *i = tolower(*i); i = s.insert(i, '_'); ++i; } } bool IsUpperCase(char c) { if (c >= 'A' && c <= 'Z') return true; else return false; }
replace
78
79
78
80
0
p00630
C++
Runtime Error
#include <algorithm> #include <cctype> #include <functional> #include <iostream> #include <string> using namespace std; bool C(char c) { return isupper(c); } int main() { string s; string::iterator i; char c; int x; while (cin >> s >> c, c - 'X') { s[0] = toupper(s[0]); while (x = s.find('_'), x != string::npos) s.erase(x, 1), s[x] = toupper(s[x]); if (c - 'U') s[0] = tolower(s[0]); if (c == 'D') for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i) *i = tolower(*i), s.insert(i, '_'); cout << s << endl; } return 0; }
#include <algorithm> #include <cctype> #include <functional> #include <iostream> #include <string> using namespace std; bool C(char c) { return isupper(c); } int main() { string s; string::iterator i; char c; int x; while (cin >> s >> c, c - 'X') { s[0] = toupper(s[0]); while (x = s.find('_'), x != string::npos) s.erase(x, 1), s[x] = toupper(s[x]); if (c - 'U') s[0] = tolower(s[0]); if (c == 'D') for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i) *i = tolower(*i), i = s.insert(i, '_'); cout << s << endl; } return 0; }
replace
20
21
20
21
0
p00631
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, sum; vector<int> a; while (true) { cin >> n; if (n == 0) { break; } a = vector<int>(n); sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } int min_ = (1 << 29); for (int i = 0; i < (1 << n); i++) { int sum2 = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum2 += a[j]; } } cout << sum2 << endl; min_ = min(abs(2 * sum2 - sum), min_); } cout << min_ << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> using namespace std; int main() { int n, sum; vector<int> a; while (true) { cin >> n; if (n == 0) { break; } a = vector<int>(n); sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } int min_ = (1 << 29); for (int i = 0; i < (1 << n); i++) { int sum2 = 0; for (int j = 0; j < n; j++) { if (i & (1 << j)) { sum2 += a[j]; } } min_ = min(abs(2 * sum2 - sum), min_); } cout << min_ << endl; } return 0; }
delete
37
39
37
37
TLE
p00632
C++
Memory Limit Exceeded
// 35 #include <algorithm> #include <iostream> #include <queue> #include <string> #include <utility> using namespace std; char g[20][20]; int h, w; int t[20][20]; struct S { int t, x, y; }; int main() { while (cin >> h >> w, h | w) { int ax, ay, bx, by; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> g[y][x]; if (g[y][x] == 'A') { ax = x; ay = y; } else if (g[y][x] == 'B') { bx = x; by = y; } } } fill(t[0], t[20], 1 << 30); queue<S> que; S is = {0, ax, ay}; que.push(is); while (!que.empty()) { int x = que.front().x; int y = que.front().y; int ct = que.front().t; que.pop(); t[y][x] = ct; for (int i = 0; i < 4; i++) { int d[] = {0, 1, 0, -1, 0}; int nx = x + d[i]; int ny = y + d[i + 1]; if (nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] <= ct + 1 || g[ny][nx] == '#') continue; S ns = {ct + 1, nx, ny}; que.push(ns); } } string p; cin >> p; int e; for (e = 0; e < 10000; e++) { if (t[by][bx] <= e) break; char o = p[e % p.size()]; int ny = by + (o == '2') - (o == '8'); int nx = bx + (o == '6') - (o == '4'); if (0 <= ny && ny < h && 0 <= nx && nx < w) { by = ny; bx = nx; } } if (e < 10000) { cout << e << ' ' << by << ' ' << bx << endl; } else { cout << "impossible" << endl; } } return 0; }
// 35 #include <algorithm> #include <iostream> #include <queue> #include <string> #include <utility> using namespace std; char g[20][20]; int h, w; int t[20][20]; struct S { int t, x, y; }; int main() { while (cin >> h >> w, h | w) { int ax, ay, bx, by; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> g[y][x]; if (g[y][x] == 'A') { ax = x; ay = y; } else if (g[y][x] == 'B') { bx = x; by = y; } } } fill(t[0], t[20], 1 << 30); queue<S> que; S is = {0, ax, ay}; que.push(is); while (!que.empty()) { int x = que.front().x; int y = que.front().y; int ct = que.front().t; que.pop(); if (t[y][x] <= ct) continue; t[y][x] = ct; for (int i = 0; i < 4; i++) { int d[] = {0, 1, 0, -1, 0}; int nx = x + d[i]; int ny = y + d[i + 1]; if (nx < 0 || w <= nx || ny < 0 || h <= ny || t[ny][nx] <= ct + 1 || g[ny][nx] == '#') continue; S ns = {ct + 1, nx, ny}; que.push(ns); } } string p; cin >> p; int e; for (e = 0; e < 10000; e++) { if (t[by][bx] <= e) break; char o = p[e % p.size()]; int ny = by + (o == '2') - (o == '8'); int nx = bx + (o == '6') - (o == '4'); if (0 <= ny && ny < h && 0 <= nx && nx < w) { by = ny; bx = nx; } } if (e < 10000) { cout << e << ' ' << by << ' ' << bx << endl; } else { cout << "impossible" << endl; } } return 0; }
insert
41
41
41
43
MLE
p00632
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; 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(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) const double EPS = 1e-10; const double PI = acos(-1.0); struct node { int y, x, t, cost; node(int y, int x, int t, int cost) : y(y), x(x), t(t), cost(cost) {} bool operator<(const node &other) const { return cost > other.cost; } }; const int MAX = INT_MAX / 10; typedef vector<vvi> vvvi; int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, -1, 0, 1, 0}; int main() { int n, m; while (cin >> n >> m, n) { vvi field(n, vi(m)); pii girl, ghost; REP(i, n) { string s; cin >> s; REP(j, s.size()) { switch (s[j]) { case 'A': girl = make_pair(i, j); break; case 'B': ghost = make_pair(i, j); break; case '.': field[i][j] = 0; break; case '#': field[i][j] = 1; break; default: assert(0); } } } string pattern; cin >> pattern; int len = pattern.size() * 20 * 20; vector<pii> glocs; REP(i, len) { glocs.push_back(ghost); switch (pattern[i % pattern.size()]) { case '2': if (ghost.first < n - 1) ghost.first++; break; case '4': if (ghost.second > 0) ghost.second--; break; case '8': if (ghost.first > 0) ghost.first--; break; case '6': if (ghost.second < m - 1) ghost.second++; break; case '5': break; default: assert(0); } } priority_queue<node> q; q.push(node(girl.first, girl.second, 0, 0)); vvvi cost(n, vvi(m, vi(len, MAX))); cost[girl.first][girl.second][0] = 0; while (!q.empty()) { node cnode = q.top(); q.pop(); if (cost[cnode.y][cnode.x][cnode.t] < cnode.cost) { continue; } REP(i, 5) { int x = cnode.x + dx[i]; int y = cnode.y + dy[i]; if (x >= 0 && y >= 0 && x < m && y < n && field[y][x] == 0) { int newcost = cnode.cost + 1; int t = cnode.t + 1; if (t < len && cost[y][x][t] > newcost) { cost[y][x][t] = newcost; q.push(node(y, x, t, newcost)); } } } } int ans = MAX; pii ansloc; REP(i, len) { int c = cost[glocs[i].first][glocs[i].second][i]; if (c < ans) { ans = c; ansloc = glocs[i]; } } if (ans == MAX) { cout << "impossible" << endl; } else { cout << ans << " " << ansloc.first << " " << ansloc.second << endl; } } }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; 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(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) const double EPS = 1e-10; const double PI = acos(-1.0); struct node { int y, x, t, cost; node(int y, int x, int t, int cost) : y(y), x(x), t(t), cost(cost) {} bool operator<(const node &other) const { return cost > other.cost; } }; const int MAX = INT_MAX / 10; typedef vector<vvi> vvvi; int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, -1, 0, 1, 0}; int main() { int n, m; while (cin >> n >> m, n) { vvi field(n, vi(m)); pii girl, ghost; REP(i, n) { string s; cin >> s; REP(j, s.size()) { switch (s[j]) { case 'A': girl = make_pair(i, j); break; case 'B': ghost = make_pair(i, j); break; case '.': field[i][j] = 0; break; case '#': field[i][j] = 1; break; default: assert(0); } } } string pattern; cin >> pattern; int len = pattern.size() * 50; vector<pii> glocs; REP(i, len) { glocs.push_back(ghost); switch (pattern[i % pattern.size()]) { case '2': if (ghost.first < n - 1) ghost.first++; break; case '4': if (ghost.second > 0) ghost.second--; break; case '8': if (ghost.first > 0) ghost.first--; break; case '6': if (ghost.second < m - 1) ghost.second++; break; case '5': break; default: assert(0); } } priority_queue<node> q; q.push(node(girl.first, girl.second, 0, 0)); vvvi cost(n, vvi(m, vi(len, MAX))); cost[girl.first][girl.second][0] = 0; while (!q.empty()) { node cnode = q.top(); q.pop(); if (cost[cnode.y][cnode.x][cnode.t] < cnode.cost) { continue; } REP(i, 5) { int x = cnode.x + dx[i]; int y = cnode.y + dy[i]; if (x >= 0 && y >= 0 && x < m && y < n && field[y][x] == 0) { int newcost = cnode.cost + 1; int t = cnode.t + 1; if (t < len && cost[y][x][t] > newcost) { cost[y][x][t] = newcost; q.push(node(y, x, t, newcost)); } } } } int ans = MAX; pii ansloc; REP(i, len) { int c = cost[glocs[i].first][glocs[i].second][i]; if (c < ans) { ans = c; ansloc = glocs[i]; } } if (ans == MAX) { cout << "impossible" << endl; } else { cout << ans << " " << ansloc.first << " " << ansloc.second << endl; } } }
replace
86
87
86
87
TLE
p00632
C++
Memory Limit Exceeded
#include <algorithm> #include <cstdint> #include <cstdio> #include <queue> #include <string> #include <vector> const size_t di[] = {size_t(-1), 0, 1, 0}; const size_t dj[] = {0, size_t(-1), 0, 1}; const int INF = 1e9; const size_t dti[] = {0, 0, 1, 0, 0, 0, 0, 0, size_t(-1)}; const size_t dtj[] = {0, 0, 0, 0, size_t(-1), 0, 1, 0, 0}; int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; std::vector<std::string> s(H); size_t si = -1, sj = -1; size_t ti = -1, tj = -1; for (size_t i = 0; i < H; ++i) { char buf[128]; scanf("%s", buf); s[i] = buf; for (size_t j = 0; j < W; ++j) { if (s[i][j] == 'A') { si = i; sj = j; } else if (s[i][j] == 'B') { ti = i; tj = j; } } } char buf[128]; scanf("%s", buf); std::string t = buf; std::queue<std::pair<size_t, size_t>> q; q.emplace(si, sj); std::vector<std::vector<int>> dp(H, std::vector<int>(W, INF)); dp[si][sj] = 0; while (!q.empty()) { std::pair<size_t, size_t> p = q.front(); q.pop(); size_t i = p.first, j = p.second; for (size_t k = 0; k < 4; ++k) { size_t ni = i + di[k], nj = j + dj[k]; if (!(ni < H && nj < W)) continue; if (s[ni][nj] == '#') continue; if (dp[ni][nj] < dp[i][j] + 1) continue; dp[ni][nj] = dp[i][j] + 1; q.emplace(ni, nj); } } // for (size_t i=0; i<H; ++i) // for (size_t j=0; j<W; ++j) // if (dp[i][j] < INF) // printf("%d%c", dp[i][j], j+1<W? ' ':'\n'); // else // printf("#%c", j+1<W? ' ':'\n'); size_t tlen = t.length(); std::pair<size_t, size_t> res(-1, -1); int tt = INF; for (int i = 0; i < 1000; ++i) { size_t nti = ti + dti[t[i % tlen] - '0']; size_t ntj = tj + dtj[t[i % tlen] - '0']; if (nti < H) ti = nti; if (ntj < W) tj = ntj; if (dp[ti][tj] < INF) { if (dp[ti][tj] > i + 1) continue; if (i + 1 < tt) { tt = i + 1; res = {ti, tj}; } } } if (tt < INF) return !printf("%d %zu %zu\n", tt, res.first, res.second); printf("impossible\n"); return 0; } int main() { while (!testcase_ends()) { } }
#include <algorithm> #include <cstdint> #include <cstdio> #include <queue> #include <string> #include <vector> const size_t di[] = {size_t(-1), 0, 1, 0}; const size_t dj[] = {0, size_t(-1), 0, 1}; const int INF = 1e9; const size_t dti[] = {0, 0, 1, 0, 0, 0, 0, 0, size_t(-1)}; const size_t dtj[] = {0, 0, 0, 0, size_t(-1), 0, 1, 0, 0}; int testcase_ends() { size_t H, W; scanf("%zu %zu", &H, &W); if (H == 0 && W == 0) return 1; std::vector<std::string> s(H); size_t si = -1, sj = -1; size_t ti = -1, tj = -1; for (size_t i = 0; i < H; ++i) { char buf[128]; scanf("%s", buf); s[i] = buf; for (size_t j = 0; j < W; ++j) { if (s[i][j] == 'A') { si = i; sj = j; } else if (s[i][j] == 'B') { ti = i; tj = j; } } } char buf[128]; scanf("%s", buf); std::string t = buf; std::queue<std::pair<size_t, size_t>> q; q.emplace(si, sj); std::vector<std::vector<int>> dp(H, std::vector<int>(W, INF)); dp[si][sj] = 0; while (!q.empty()) { std::pair<size_t, size_t> p = q.front(); q.pop(); size_t i = p.first, j = p.second; for (size_t k = 0; k < 4; ++k) { size_t ni = i + di[k], nj = j + dj[k]; if (!(ni < H && nj < W)) continue; if (s[ni][nj] == '#') continue; if (dp[ni][nj] <= dp[i][j] + 1) continue; dp[ni][nj] = dp[i][j] + 1; q.emplace(ni, nj); } } // for (size_t i=0; i<H; ++i) // for (size_t j=0; j<W; ++j) // if (dp[i][j] < INF) // printf("%d%c", dp[i][j], j+1<W? ' ':'\n'); // else // printf("#%c", j+1<W? ' ':'\n'); size_t tlen = t.length(); std::pair<size_t, size_t> res(-1, -1); int tt = INF; for (int i = 0; i < 1000; ++i) { size_t nti = ti + dti[t[i % tlen] - '0']; size_t ntj = tj + dtj[t[i % tlen] - '0']; if (nti < H) ti = nti; if (ntj < W) tj = ntj; if (dp[ti][tj] < INF) { if (dp[ti][tj] > i + 1) continue; if (i + 1 < tt) { tt = i + 1; res = {ti, tj}; } } } if (tt < INF) return !printf("%d %zu %zu\n", tt, res.first, res.second); printf("impossible\n"); return 0; } int main() { while (!testcase_ends()) { } }
replace
56
57
56
57
MLE
p00632
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; i++) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int h, w, ax, ay, bx, by, t, flag; char field[30][30]; char pattern[20]; int main() { while (true) { scanf("%d%d", &h, &w); if (h == 0 && w == 0) break; REP(y, h) { scanf("%s", field[y]); REP(x, w) { if (field[y][x] == 'A') { ax = x; ay = y; } else if (field[y][x] == 'B') { bx = x; by = y; } } } scanf("%s", pattern); int leng = strlen(pattern); t = 0, flag = 0; queue<pii> q, nq; q.push(pii(ax, ay)); REP(i, 200) { t++; // girl while (!q.empty()) { pii p = q.front(); q.pop(); REP(j, 4) { int nx = p.first + dx[j]; int ny = p.second + dy[j]; if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != '#') { field[ny][nx] = 'A'; nq.push(pii(nx, ny)); } } } while (!nq.empty()) { q.push(nq.front()); nq.pop(); } // ghost switch (pattern[i % leng]) { case '5': break; case '8': by--; if (by < 0) by = 0; break; case '6': bx++; if (bx >= w) bx = w - 1; break; case '4': bx--; if (bx < 0) bx = 0; break; case '2': by++; if (by >= h) by = h - 1; break; } if (field[by][bx] == 'A') { flag = 1; break; } } if (flag == 0) { printf("impossible\n"); } else { printf("%d %d %d\n", t, by, bx); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define REP(i, n) for (ll i = 0; i < n; i++) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int h, w, ax, ay, bx, by, t, flag; char field[30][30]; char pattern[20]; int main() { while (true) { scanf("%d%d", &h, &w); if (h == 0 && w == 0) break; REP(y, h) { scanf("%s", field[y]); REP(x, w) { if (field[y][x] == 'A') { ax = x; ay = y; } else if (field[y][x] == 'B') { bx = x; by = y; } } } scanf("%s", pattern); int leng = strlen(pattern); t = 0, flag = 0; queue<pii> q, nq; q.push(pii(ax, ay)); REP(i, 200) { t++; // girl while (!q.empty()) { pii p = q.front(); q.pop(); REP(j, 4) { int nx = p.first + dx[j]; int ny = p.second + dy[j]; if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] != '#' && field[ny][nx] != 'A') { field[ny][nx] = 'A'; nq.push(pii(nx, ny)); } } } while (!nq.empty()) { q.push(nq.front()); nq.pop(); } // ghost switch (pattern[i % leng]) { case '5': break; case '8': by--; if (by < 0) by = 0; break; case '6': bx++; if (bx >= w) bx = w - 1; break; case '4': bx--; if (bx < 0) bx = 0; break; case '2': by++; if (by >= h) by = h - 1; break; } if (field[by][bx] == 'A') { flag = 1; break; } } if (flag == 0) { printf("impossible\n"); } else { printf("%d %d %d\n", t, by, bx); } } return 0; }
replace
57
58
57
59
0
p00632
C++
Time Limit Exceeded
#import <queue> #import <stdio.h> int main() { std::queue<int> q; int w, h, f[484], i, j, a, b, t, x, z; char p[15]; for (; scanf("%d%d ", &h, &w), h;) { for (j = 0; j < w + 2; j++) f[j] = f[j - ~h * 22] = -1; for (i = 1; i <= h; i++) { for (j = 1; j <= w; j++) f[x = i * 22 + j] = -2, z = getchar(), z - 35 ? z > 65 ? b = x : z > 64 ? a = x : 1 : --f[x]; f[i * 22] = f[i * 22 - ~w] = -9; getchar(); } f[a] = t = 0; q.push(0); for (q.push(a); a = q.front(), q.size() > 1; q.pop()) if (!a) ++t, q.push(0); else for (i = 4; i--;) if (f[x = a + " 57L"[i] - 54] == -2) f[x] = t, q.push(x); q.pop(); scanf("%s", p); for (a = t = 0; !a;) { x = b; z = 0; for (i = 0; !a && p[i]; f[b] >= 0 && ++z) ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0, f[b] >= 0 & t >= f[b] ? a = b : 0; !z & !a & !p[i] & b == x & f[b] < 0 && --a; } printf(~a ? "%d %d %d\n" : "impossible\n", t, a / 22 - 1, a % 22 - 1); } }
#import <queue> #import <stdio.h> int main() { std::queue<int> q; int w, h, f[484], i, j, a, b, t, x, z; char p[15]; for (; scanf("%d%d ", &h, &w), h;) { for (j = 0; j < w + 2; j++) f[j] = f[j - ~h * 22] = -1; for (i = 1; i <= h; i++) { for (j = 1; j <= w; j++) f[x = i * 22 + j] = -2, z = getchar(), z - 35 ? z > 65 ? b = x : z > 64 ? a = x : 1 : --f[x]; f[i * 22] = f[i * 22 - ~w] = -1; getchar(); } f[a] = t = 0; q.push(0); for (q.push(a); a = q.front(), q.size() > 1; q.pop()) if (!a) ++t, q.push(0); else for (i = 4; i--;) if (f[x = a + " 57L"[i] - 54] == -2) f[x] = t, q.push(x); q.pop(); scanf("%s", p); for (a = t = 0; !a;) { x = b; z = 0; for (i = 0; !a && p[i]; f[b] >= 0 && ++z) ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0, f[b] >= 0 & t >= f[b] ? a = b : 0; !z & !a & !p[i] & b == x & f[b] < 0 && --a; } printf(~a ? "%d %d %d\n" : "impossible\n", t, a / 22 - 1, a % 22 - 1); } }
replace
16
17
16
17
TLE
p00633
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; typedef complex<double> P; namespace std { bool operator<(const P &a, const P &b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } // namespace std double cross(const P &a, const P &b) { return imag(conj(a) * b); } double dot(const P &a, const P &b) { return real(conj(a) * b); } struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } }; typedef vector<P> G; #define curr(P, i) P[i] #define next(P, i) P[(i + 1) % P.size()] struct C { P p; double r; C(const P &p, double r) : p(p), r(r) {} C() {} }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } P projection(const L &l, const P &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } vector<P> crosspointLC(const L &l, const C &c) { vector<P> ret; P center = projection(l, c.p); double d = abs(center - c.p); double t = sqrt(c.r * c.r - d * d); if (isnan(t)) { return ret; } P vect = (l[1] - l[0]); vect /= abs(vect); ret.push_back(center - vect * t); if (t > EPS) { ret.push_back(center + vect * t); } return ret; } bool contain(const C &c1, const C &c2) { double d = abs(c1.p - c2.p); return c1.r - c2.r - d > -EPS; } vector<P> crosspointCC(const C &c1, const C &c2) { vector<P> ret; double d = abs(c1.p - c2.p); if (max(c1.r, c2.r) - min(c1.r, c2.r) - d > -EPS) { return ret; } double x = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); P start = c1.p + (c2.p - c1.p) / d * x; P vect = (c1.p - c2.p) * P(0.0, 1.0); return crosspointLC(L(start, start + vect), c1); } double angle( const P &a, const P & b) { // ツベツクツトツδ仰つつゥツづァツづ敖つスツベツクツトツδ仰つつづ個角ツ度ツづ個計ツ算[0,2pi] double ret = arg(b) - arg(a); return (ret >= 0) ? ret : ret + 2 * PI; } typedef pair<double, double> pdd; double func(C c, const vector<pdd> &v) { if (v.size() == 0) return 0; double x, y; double ans = 0; REP(i, v.size()) { double a = v[i].first, b = v[i].second; if (i == 0) { x = a; y = b; continue; } if (y >= a) { y = max(y, b); } else { ans += c.r * (y - x); x = a; y = b; } } ans += c.r * (y - x); return ans; } int main() { int n; while (cin >> n, n) { C c[n]; REP(i, n) { int x, y, r; cin >> x >> y >> r; c[i] = C(P(x, y), r); } vector<pdd> ngang[n]; REP(i, n) { // cout << i << endl; REP(j, n) { if (i == j) continue; double d = abs(c[i].p - c[j].p); if (c[j].r - c[i].r - d > -EPS) { ngang[i].push_back(pdd(0, 2 * PI)); continue; } if (c[i].r - c[j].r - d > -EPS) continue; if (d + EPS >= c[i].r + c[j].r) continue; vector<P> vp = crosspointCC(c[i], c[j]); assert(vp.size() == 2); if (vp.size() != 2) continue; // cout << vp[0] << " " << vp[1] << endl; double a1 = arg(vp[1] - c[i].p); double a2 = arg(vp[0] - c[i].p); if (a1 < 0) a1 += 2 * PI; if (a2 < 0) a2 += 2 * PI; if (a2 - a1 < 0) { ngang[i].push_back(pdd(a1, 2 * PI)); ngang[i].push_back(pdd(0, a2)); } else { ngang[i].push_back(pdd(a1, a2)); } } } double ans = 0; REP(i, n) { // cout << "i = " << i << endl; // cout << ngang[i].size() << endl; sort(ALL(ngang[i])); double tmp = func(c[i], ngang[i]); ans += c[i].r * 2 * PI - tmp; } printf("%.10f\n", ans); } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; typedef complex<double> P; namespace std { bool operator<(const P &a, const P &b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } // namespace std double cross(const P &a, const P &b) { return imag(conj(a) * b); } double dot(const P &a, const P &b) { return real(conj(a) * b); } struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } }; typedef vector<P> G; #define curr(P, i) P[i] #define next(P, i) P[(i + 1) % P.size()] struct C { P p; double r; C(const P &p, double r) : p(p), r(r) {} C() {} }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } P projection(const L &l, const P &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } vector<P> crosspointLC(const L &l, const C &c) { vector<P> ret; P center = projection(l, c.p); double d = abs(center - c.p); double t = sqrt(c.r * c.r - d * d); if (isnan(t)) { return ret; } P vect = (l[1] - l[0]); vect /= abs(vect); ret.push_back(center - vect * t); if (t > EPS) { ret.push_back(center + vect * t); } return ret; } bool contain(const C &c1, const C &c2) { double d = abs(c1.p - c2.p); return c1.r - c2.r - d > -EPS; } vector<P> crosspointCC(const C &c1, const C &c2) { vector<P> ret; double d = abs(c1.p - c2.p); if (max(c1.r, c2.r) - min(c1.r, c2.r) - d > -EPS) { return ret; } double x = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); P start = c1.p + (c2.p - c1.p) / d * x; P vect = (c1.p - c2.p) * P(0.0, 1.0); return crosspointLC(L(start, start + vect), c1); } double angle( const P &a, const P & b) { // ツベツクツトツδ仰つつゥツづァツづ敖つスツベツクツトツδ仰つつづ個角ツ度ツづ個計ツ算[0,2pi] double ret = arg(b) - arg(a); return (ret >= 0) ? ret : ret + 2 * PI; } typedef pair<double, double> pdd; double func(C c, const vector<pdd> &v) { if (v.size() == 0) return 0; double x, y; double ans = 0; REP(i, v.size()) { double a = v[i].first, b = v[i].second; if (i == 0) { x = a; y = b; continue; } if (y >= a) { y = max(y, b); } else { ans += c.r * (y - x); x = a; y = b; } } ans += c.r * (y - x); return ans; } int main() { int n; while (cin >> n, n) { C c[n]; REP(i, n) { double x, y, r; cin >> x >> y >> r; c[i] = C(P(x, y), r); } vector<pdd> ngang[n]; REP(i, n) { // cout << i << endl; REP(j, n) { if (i == j) continue; double d = abs(c[i].p - c[j].p); if (c[j].r - c[i].r - d > -EPS) { ngang[i].push_back(pdd(0, 2 * PI)); continue; } if (c[i].r - c[j].r - d > -EPS) continue; if (d + EPS >= c[i].r + c[j].r) continue; vector<P> vp = crosspointCC(c[i], c[j]); assert(vp.size() == 2); if (vp.size() != 2) continue; // cout << vp[0] << " " << vp[1] << endl; double a1 = arg(vp[1] - c[i].p); double a2 = arg(vp[0] - c[i].p); if (a1 < 0) a1 += 2 * PI; if (a2 < 0) a2 += 2 * PI; if (a2 - a1 < 0) { ngang[i].push_back(pdd(a1, 2 * PI)); ngang[i].push_back(pdd(0, a2)); } else { ngang[i].push_back(pdd(a1, a2)); } } } double ans = 0; REP(i, n) { // cout << "i = " << i << endl; // cout << ngang[i].size() << endl; sort(ALL(ngang[i])); double tmp = func(c[i], ngang[i]); ans += c[i].r * 2 * PI - tmp; } printf("%.10f\n", ans); } }
replace
141
142
141
142
TLE
p00633
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; const double EPS = 1e-10; const double INF = 1e12; const double PI = acos(-1); #define EQ(n, m) (abs((n) - (m)) < EPS) #define X real() #define Y imag() typedef complex<double> P; typedef vector<P> VP; struct C { P p; double r; C(const P &p, const double &r) : p(p), r(r) {} C() {} }; P unit(const P &p) { return p / abs(p); } P rotate(const P &p, double rad) { return p * P(cos(rad), sin(rad)); } VP crosspointCC(C a, C b) { VP ret; if (a.r < b.r) swap(a, b); double dist = abs(b.p - a.p); P dir = a.r * unit(b.p - a.p); if (EQ(dist, a.r + b.r) || EQ(dist, a.r - b.r)) { ret.push_back(a.p + dir); } else if (a.r - b.r < dist && dist < a.r + b.r) { double cos = (a.r * a.r + dist * dist - b.r * b.r) / (2 * a.r * dist); double sin = sqrt(1 - cos * cos); ret.push_back(a.p + dir * P(cos, sin)); ret.push_back(a.p + dir * P(cos, -sin)); } return ret; } int main() { while (1) { int n; cin >> n; if (n == 0) break; vector<C> c(n); for (int i = 0; i < n; i++) { int x, y, r; cin >> x >> y >> r; c[i] = C(P(x, y), r); } double ans = 0; for (int i = 0; i < n; i++) { vector<double> ar{-PI, PI}; for (int j = 0; j < n; j++) { if (i == j) continue; VP ret = crosspointCC(c[i], c[j]); for (int k = 0; k < (int)ret.size(); k++) { ar.push_back(arg(ret[k] - c[i].p)); } } sort(ar.begin(), ar.end()); ar.erase(unique(ar.begin(), ar.end()), ar.end()); for (int j = 0; j < (int)ar.size() - 1; j++) { double mid = (ar[j] + ar[j + 1]) / 2; P p = c[i].p + rotate(P(c[i].r, 0), mid); bool out = true; for (int k = 0; k < n; k++) { if (abs(p - c[k].p) + EPS < c[k].r) { out = false; } } if (out) { ans += (ar[j + 1] - ar[j]) * c[i].r; } } } cout << fixed << setprecision(10); cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; const double EPS = 1e-10; const double INF = 1e12; const double PI = acos(-1); #define EQ(n, m) (abs((n) - (m)) < EPS) #define X real() #define Y imag() typedef complex<double> P; typedef vector<P> VP; struct C { P p; double r; C(const P &p, const double &r) : p(p), r(r) {} C() {} }; P unit(const P &p) { return p / abs(p); } P rotate(const P &p, double rad) { return p * P(cos(rad), sin(rad)); } VP crosspointCC(C a, C b) { VP ret; if (a.r < b.r) swap(a, b); double dist = abs(b.p - a.p); P dir = a.r * unit(b.p - a.p); if (EQ(dist, a.r + b.r) || EQ(dist, a.r - b.r)) { ret.push_back(a.p + dir); } else if (a.r - b.r < dist && dist < a.r + b.r) { double cos = (a.r * a.r + dist * dist - b.r * b.r) / (2 * a.r * dist); double sin = sqrt(1 - cos * cos); ret.push_back(a.p + dir * P(cos, sin)); ret.push_back(a.p + dir * P(cos, -sin)); } return ret; } int main() { while (1) { int n; cin >> n; if (n == 0) break; vector<C> c(n); for (int i = 0; i < n; i++) { double x, y, r; cin >> x >> y >> r; c[i] = C(P(x, y), r); } double ans = 0; for (int i = 0; i < n; i++) { vector<double> ar{-PI, PI}; for (int j = 0; j < n; j++) { if (i == j) continue; VP ret = crosspointCC(c[i], c[j]); for (int k = 0; k < (int)ret.size(); k++) { ar.push_back(arg(ret[k] - c[i].p)); } } sort(ar.begin(), ar.end()); ar.erase(unique(ar.begin(), ar.end()), ar.end()); for (int j = 0; j < (int)ar.size() - 1; j++) { double mid = (ar[j] + ar[j + 1]) / 2; P p = c[i].p + rotate(P(c[i].r, 0), mid); bool out = true; for (int k = 0; k < n; k++) { if (abs(p - c[k].p) + EPS < c[k].r) { out = false; } } if (out) { ans += (ar[j + 1] - ar[j]) * c[i].r; } } } cout << fixed << setprecision(10); cout << ans << endl; } return 0; }
replace
51
52
51
52
TLE
p00638
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n; while (1) { vector<pair<int, int>> v; cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> v[i].second >> v[i].first; } sort(v.begin(), v.end()); int ans = 0; int judge = 0; for (int i = 0; i < n; i++) { ans += v[i].second; if (ans > v[i].first) { judge = 1; break; } } if (judge == 0) cout << "Yes" << endl; else cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (1) { vector<pair<int, int>> v; cin >> n; if (n == 0) break; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].second >> v[i].first; } sort(v.begin(), v.end()); int ans = 0; int judge = 0; for (int i = 0; i < n; i++) { ans += v[i].second; if (ans > v[i].first) { judge = 1; break; } } if (judge == 0) cout << "Yes" << endl; else cout << "No" << endl; } }
replace
11
12
11
12
-11
p00638
C++
Runtime Error
#include <algorithm> #include <iostream> #include <numeric> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; int main() { for (int N; cin >> N, N;) { vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(v.begin(), v.end()); vector<int> w1, w2; for (int i = 0; i < N; i++) { w1.push_back(v[i].first); w2.push_back(v[i].second); } partial_sum(w2.begin(), w2.end(), w2.begin()); cerr << "-----" << endl; for (int i : w2) cerr << i << endl; cerr << "-----" << endl; bool f = true; for (int i = 0; i < N; i++) if (w1[i] < w2[i]) f = false; cout << (f ? "Yes" : "No") << endl; } }
#include <algorithm> #include <iostream> #include <numeric> #include <utility> #include <vector> using namespace std; typedef pair<int, int> P; int main() { for (int N; cin >> N, N;) { vector<P> v; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(b, a)); } sort(v.begin(), v.end()); vector<int> w1, w2; for (int i = 0; i < N; i++) { w1.push_back(v[i].first); w2.push_back(v[i].second); } partial_sum(w2.begin(), w2.end(), w2.begin()); bool f = true; for (int i = 0; i < N; i++) if (w1[i] < w2[i]) f = false; cout << (f ? "Yes" : "No") << endl; } }
delete
26
31
26
26
0
----- 1 3 6 ----- ----- 1 3 6 -----
p00640
C++
Runtime Error
// AOJ1054 Distorted Love #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main() { int n; int buf[100]; string name[100]; int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100], ptr[100][100]; string link[100][100]; while (cin >> n, n) { int x, y; cin >> x >> y; int cur = 0, size = 0; buf[cur] = 0; for (int i = 0; i < n; i++) { cin >> name[i] >> b[i]; for (int j = 0; j < b[i]; j++) cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < b[j]; k++) if (link[j][k] == name[i]) ptr[j][k] = i; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "click") { cin >> x >> y; for (int j = 0; j < b[buf[cur]]; j++) { if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] && y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) { cur++; size = cur; buf[cur] = ptr[buf[cur - 1]][j]; break; } } } if (s == "forward" && cur != size) cur++; if (s == "back" && cur != 0) cur--; if (s == "show") cout << name[buf[cur]] << endl; } } }
// AOJ1054 Distorted Love #include <iostream> #include <map> #include <string> #include <vector> using namespace std; int main() { int n; int buf[1000000]; string name[100]; int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100], ptr[100][100]; string link[100][100]; while (cin >> n, n) { int x, y; cin >> x >> y; int cur = 0, size = 0; buf[cur] = 0; for (int i = 0; i < n; i++) { cin >> name[i] >> b[i]; for (int j = 0; j < b[i]; j++) cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < b[j]; k++) if (link[j][k] == name[i]) ptr[j][k] = i; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "click") { cin >> x >> y; for (int j = 0; j < b[buf[cur]]; j++) { if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] && y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) { cur++; size = cur; buf[cur] = ptr[buf[cur - 1]][j]; break; } } } if (s == "forward" && cur != size) cur++; if (s == "back" && cur != 0) cur--; if (s == "show") cout << name[buf[cur]] << endl; } } }
replace
11
12
11
12
0
p00640
C++
Runtime Error
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109], yb[109][109]; string s[109], t[109][109], tp; int main() { while (cin >> N, N) { cin >> W >> H; for (int i = 0; i < N; i++) { cin >> s[i] >> c[i]; for (int j = 0; j < c[i]; j++) { cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j]; } } cin >> Q; vector<int> res; res.push_back(0); int pos = 0; for (int i = 0; i < Q; i++) { cin >> tp; if (tp == "click") { cin >> x >> y; for (int j = 0; j < c[res[pos]]; j++) { if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] && ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) { int ptr = -1; for (int k = 0; k < c[res[pos]]; k++) { if (s[k] == t[res[pos]][j]) ptr = k; } res.resize(pos + 2); res[++pos] = ptr; break; } } } if (tp == "show") cout << s[res[pos]] << endl; if (tp == "forward" && pos + 1 != res.size()) pos++; if (tp == "back" && pos != 0) pos--; } } return 0; }
#include <iostream> #include <map> #include <string> #include <vector> using namespace std; int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109], yb[109][109]; string s[109], t[109][109], tp; int main() { while (cin >> N, N) { cin >> W >> H; for (int i = 0; i < N; i++) { cin >> s[i] >> c[i]; for (int j = 0; j < c[i]; j++) { cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j]; } } cin >> Q; vector<int> res; res.push_back(0); int pos = 0; for (int i = 0; i < Q; i++) { cin >> tp; if (tp == "click") { cin >> x >> y; for (int j = 0; j < c[res[pos]]; j++) { if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] && ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) { int ptr = -1; for (int k = 0; k < N; k++) { if (s[k] == t[res[pos]][j]) ptr = k; } res.resize(pos + 2); res[++pos] = ptr; break; } } } if (tp == "show") cout << s[res[pos]] << endl; if (tp == "forward" && pos + 1 != res.size()) pos++; if (tp == "back" && pos != 0) pos--; } } return 0; }
replace
29
30
29
30
0
p00640
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back #define rp(i, c) rep(i, (c).size()) #define fr(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define dbg(x) cerr << #x << " = " << (x) << endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; struct S { int x1, y1, x2, y2; string name; }; int main() { int n; while (cin >> n, n) { int h, w; cin >> h >> w; vector<string> name(n); map<string, vector<S>> button; cin.ignore(); rep(i, n) { int k; string str; getline(cin, str); int p = str.rfind(" "); name[i] = str.substr(0, p); stringstream(str.substr(p)) >> k; rep(j, k) { S s; string str; getline(cin, str); int p = 0; rep(it, 4) p = str.find(" ", p + 1); s.name = str.substr(p + 1); stringstream(str.substr(0, p)) >> s.x1 >> s.y1 >> s.x2 >> s.y2; button[name[i]].pb(s); } } int m; cin >> m; vector<string> buffer; int cur = 0; buffer.pb(name[0]); rep(i, m) { string op; cin >> op; if (op == "show") { cout << buffer[cur] << endl; } else if (op == "click") { int x, y; cin >> x >> y; fr(it, button[name[cur]]) if (it->x1 <= x && x <= it->x2 && it->y1 <= y && y <= it->y2) { buffer.erase(buffer.begin() + cur + 1, buffer.end()); buffer.pb(it->name); cur++; break; } } else if (op == "back") { if (cur > 0) cur--; } else if (op == "forward") { if (cur < buffer.size() - 1) cur++; } } } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back #define rp(i, c) rep(i, (c).size()) #define fr(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define dbg(x) cerr << #x << " = " << (x) << endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; struct S { int x1, y1, x2, y2; string name; }; int main() { int n; while (cin >> n, n) { int h, w; cin >> h >> w; vector<string> name(n); map<string, vector<S>> button; cin.ignore(); rep(i, n) { int k; string str; getline(cin, str); int p = str.rfind(" "); name[i] = str.substr(0, p); stringstream(str.substr(p)) >> k; rep(j, k) { S s; string str; getline(cin, str); int p = 0; rep(it, 4) p = str.find(" ", p + 1); s.name = str.substr(p + 1); stringstream(str.substr(0, p)) >> s.x1 >> s.y1 >> s.x2 >> s.y2; button[name[i]].pb(s); } } int m; cin >> m; vector<string> buffer; int cur = 0; buffer.pb(name[0]); rep(i, m) { string op; cin >> op; if (op == "show") { cout << buffer[cur] << endl; } else if (op == "click") { int x, y; cin >> x >> y; fr(it, button[buffer[cur]]) if (it->x1 <= x && x <= it->x2 && it->y1 <= y && y <= it->y2) { buffer.erase(buffer.begin() + cur + 1, buffer.end()); buffer.pb(it->name); cur++; break; } } else if (op == "back") { if (cur > 0) cur--; } else if (op == "forward") { if (cur < buffer.size() - 1) cur++; } } } return 0; }
replace
83
85
83
85
0
p00642
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; 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.txt" int main() { cout << setprecision(7) << fixed; vector<vector<ld>> dp(100005, vector<ld>(50)); dp[0][0] = 1; for (int n = 0; n < 100004; ++n) { for (int i = 0; i < 48; ++i) { dp[n + 1][i + 1] += dp[n][i] / pow(2, i); dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i)); } } vector<ld> anss(100001); for (int i = 0; i < 100001; ++i) { anss[i + 1] = anss[i]; for (int j = 1; j < 50; ++j) { anss[i + 1] += dp[i + 1][j]; } } while (1) { int N; cin >> N; if (!N) break; cout << anss[N] << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = double; 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.txt" int main() { cout << setprecision(7) << fixed; vector<vector<ld>> dp(100005, vector<ld>(50)); dp[0][0] = 1; for (int n = 0; n < 100004; ++n) { for (int i = 0; i < 48; ++i) { dp[n + 1][i + 1] += dp[n][i] / pow(2, i); dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i)); } } vector<ld> anss(100001); for (int i = 0; i < 100001; ++i) { anss[i + 1] = anss[i]; for (int j = 1; j < 50; ++j) { anss[i + 1] += dp[i + 1][j]; } } while (1) { int N; cin >> N; if (!N) break; cout << anss[N] << endl; } return 0; }
replace
5
6
5
6
MLE
p00642
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> using namespace std; int main() { int n; while (cin >> n, n) { double memo1[40] = {}; double memo2[40] = {}; double ans = 0; memo1[0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < 39; j++) { if (memo1[j] == 0) continue; double getexp = memo1[j] * pow(2, -j); memo2[j + 1] += getexp; memo2[0] += memo1[j] - getexp; ans += getexp; memo1[j] = 0; } swap(memo1, memo2); } printf("%f\n", ans); } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> using namespace std; int main() { int n; while (cin >> n, n) { double memo1[40] = {}; double memo2[40] = {}; double ans = 0; memo1[0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < 35; j++) { if (memo1[j] == 0) continue; double getexp = memo1[j] * pow(2, -j); memo2[j + 1] += getexp; memo2[0] += memo1[j] - getexp; ans += getexp; memo1[j] = 0; } swap(memo1, memo2); } printf("%f\n", ans); } return 0; }
replace
15
16
15
16
TLE
p00642
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { long long int foo = 1; int bar = 1; for (int j = i + 1; j < n + 1 and j < n + 30; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { long long int foo = 1; long long int bar = 1; for (int j = i + 1; j < n + 1 and foo < 1e08; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
replace
14
16
14
16
TLE
p00642
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { int foo = 1; int bar = 1; for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
#include <cstdio> #include <iostream> using namespace std; #define NMAX 100000 double dp[NMAX + 1]; int main() { int n = NMAX; for (int i = 0; i < n + 1; i++) { dp[i] = 0.0; } for (int i = 0; i < n; i++) { long long int foo = 1; int bar = 1; for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) { dp[j] += (1.0 - dp[i]) / foo; bar *= 2; foo *= bar; } } while (cin >> n and n > 0) { double ans = 0.0; for (int i = 1; i < n + 1; i++) { ans += dp[i]; } printf("%.8f\n", ans); } return 0; }
replace
13
14
13
14
TLE
p00642
C++
Runtime Error
#include <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) const int N = 10, ONE_KUURU = 12; vector<double> E; void init() { E.assign(N + 1, 0.0); vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1)); dp[1][0] = 1.0; for (int i = 1; i <= N; ++i) { E[i] += E[i - 1]; rep(j, ONE_KUURU) { dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j)); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); E[i] += dp[i][j] * (1.0 / (1 << j)); } } } int main() { init(); for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n') ; return 0; }
#include <iomanip> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) const int N = 100000, ONE_KUURU = 12; vector<double> E; void init() { E.assign(N + 1, 0.0); vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1)); dp[1][0] = 1.0; for (int i = 1; i <= N; ++i) { E[i] += E[i - 1]; rep(j, ONE_KUURU) { dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j)); dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j)); E[i] += dp[i][j] * (1.0 / (1 << j)); } } } int main() { init(); for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\n') ; return 0; }
replace
7
8
7
8
0
p00642
C++
Memory Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS // #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; // #define int ll typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) begin(c), end(c) #define range(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) range(i, 0, b) #define rangei(i, a, b) for (ll a = a; i <= ll(b); i++) #define repi(i, b) rangei(i, 1, b) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple template <class T> ostream &operator<<(ostream &os, vector<T> const &); template <int n, class... T> typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &, tuple<T...> const &) {} template <int n, class... T> typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os, tuple<T...> const &t) { os << (n == 0 ? "" : " ") << get<n>(t); _ot<n + 1>(os, t); } template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) { _ot<0>(os, t); return os; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> const &p) { return os << "(" << p.first << ", " << p.second << ") "; } template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " "); return os; } #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \ << endl) #else #define dump(...) #endif void fastios() { ios_base::sync_with_stdio(0); cin.tie(0); // #define endl '\n' } template <class T> size_t uniq(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v.size(); } template <class T> size_t uniq(T *l, size_t n) { sort(l, l + n); return unique(l, l + n) - l; } #define mems(arr, val) memset(arr, val, sizeof(arr)); int const mod = 1000000007; int const inf = numeric_limits<int>::max() / 8; const int MAX_D = 1000010; const int MAX_C = 30; double dp[MAX_D][MAX_C]; // i日目にj回連続で得る確率 double ans[MAX_D]; int main() { memset(dp, 0, sizeof(dp)); memset(ans, 0, sizeof(ans)); dp[1][1] = 1; double cur = 0; for (int i = 1; i < MAX_D; i++) { double p = 1; for (int j = 1; j < MAX_C; j++) { cur += dp[i][j] * p; dp[i + 1][j + 1] += dp[i][j] * p; dp[i + 1][1] += dp[i][j] * (1 - p); p /= 2; } // rep(j,MAX_C) printf("%2.2lf ", dp[i][j]); // puts(""); ans[i] = cur; } int n; while (scanf("%d", &n), n != 0) { printf("%.10lf\n", ans[n]); } }
#define _CRT_SECURE_NO_WARNINGS // #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; // #define int ll typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; #define all(c) begin(c), end(c) #define range(i, a, b) for (ll i = a; i < ll(b); i++) #define rep(i, b) range(i, 0, b) #define rangei(i, a, b) for (ll a = a; i <= ll(b); i++) #define repi(i, b) rangei(i, 1, b) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple template <class T> ostream &operator<<(ostream &os, vector<T> const &); template <int n, class... T> typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &, tuple<T...> const &) {} template <int n, class... T> typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os, tuple<T...> const &t) { os << (n == 0 ? "" : " ") << get<n>(t); _ot<n + 1>(os, t); } template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) { _ot<0>(os, t); return os; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> const &p) { return os << "(" << p.first << ", " << p.second << ") "; } template <class T> ostream &operator<<(ostream &os, vector<T> const &v) { rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " "); return os; } #ifdef DEBUG #define dump(...) \ (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \ << endl) #else #define dump(...) #endif void fastios() { ios_base::sync_with_stdio(0); cin.tie(0); // #define endl '\n' } template <class T> size_t uniq(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v.size(); } template <class T> size_t uniq(T *l, size_t n) { sort(l, l + n); return unique(l, l + n) - l; } #define mems(arr, val) memset(arr, val, sizeof(arr)); int const mod = 1000000007; int const inf = numeric_limits<int>::max() / 8; const int MAX_D = 100010; const int MAX_C = 30; double dp[MAX_D][MAX_C]; // i日目にj回連続で得る確率 double ans[MAX_D]; int main() { memset(dp, 0, sizeof(dp)); memset(ans, 0, sizeof(ans)); dp[1][1] = 1; double cur = 0; for (int i = 1; i < MAX_D; i++) { double p = 1; for (int j = 1; j < MAX_C; j++) { cur += dp[i][j] * p; dp[i + 1][j + 1] += dp[i][j] * p; dp[i + 1][1] += dp[i][j] * (1 - p); p /= 2; } // rep(j,MAX_C) printf("%2.2lf ", dp[i][j]); // puts(""); ans[i] = cur; } int n; while (scanf("%d", &n), n != 0) { printf("%.10lf\n", ans[n]); } }
replace
65
66
65
66
MLE
p00642
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef unsigned long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<string, map<string, int>> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; double getb[200000]; double dp[100001][30]; int main() { int n; fill(&dp[0][0], &dp[199999][49] + 1, 0.0); dp[1][1] = 1.0; for (int i = 1; i < 100000; i++) { for (int j = 0; j < 29; j++) { dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j); dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j)); } } getb[0] = 0; for (int i = 1; i <= 100000; i++) { getb[i] = 0.0; for (int j = 1; j < 30; j++) { getb[i] += dp[i][j]; } getb[i] += getb[i - 1]; } while (cin >> n && n) { cout << fixed << setprecision(9) << getb[n] << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> #define _USE_MATH_DEFINES using namespace std; typedef unsigned long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<string, map<string, int>> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; double getb[200000]; double dp[100001][30]; int main() { int n; fill(&dp[0][0], &dp[100000][29] + 1, 0.0); dp[1][1] = 1.0; for (int i = 1; i < 100000; i++) { for (int j = 0; j < 29; j++) { dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j); dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j)); } } getb[0] = 0; for (int i = 1; i <= 100000; i++) { getb[i] = 0.0; for (int j = 1; j < 30; j++) { getb[i] += dp[i][j]; } getb[i] += getb[i - 1]; } while (cin >> n && n) { cout << fixed << setprecision(9) << getb[n] << endl; } return 0; }
replace
39
40
39
40
-11
p00643
C++
Memory Limit Exceeded
#include <iostream> #include <queue> #include <string.h> using namespace std; int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}; struct Dice { int t, b, n, s, e, w; int y, x; int c; Dice(int top, int bottom, int north, int south, int east, int west, int sx, int sy, int cost) { t = top, b = bottom, n = north, s = south, e = east, w = west; x = sx, y = sy, c = cost; } bool operator<(const Dice &r) const { return c > r.c; } void roll(int d) { int tmp; y += dy[d], x += dx[d]; if (d == 0) { tmp = t; t = s; s = b; b = n; n = tmp; } else if (d == 1) { tmp = t; t = e; e = b; b = w; w = tmp; } else if (d == 2) { tmp = t; t = n; n = b; b = s; s = tmp; } else { tmp = t; t = w; w = b; b = e; e = tmp; } } }; int w, h; bool in(int x, int y) { if (x < 0 || y < 0 || x >= w || y >= h) return false; return true; } int main() { int p[10][10], memo[10][10][100]; while (cin >> h >> w && h != 0) { priority_queue<Dice> que; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { cin >> p[y][x]; } memset(memo, -1, sizeof(memo)); int gx, gy, sx, sy; cin >> sy >> sx >> gy >> gx; que.push(Dice(1, 6, 5, 2, 3, 4, sx, sy, 0)); while (!que.empty()) { Dice now = que.top(); que.pop(); // cout<<now.x<<" "<<now.y<<" "<<now.b<<" //"<<now.c<<endl; if (now.x == gx && now.y == gy) { cout << now.c << endl; break; } if (memo[now.y][now.x][now.e * 7 + now.b] >= 0 && memo[now.y][now.x][now.e * 7 + now.b] <= now.c) continue; for (int r = 0; r < 4; r++) { Dice copy = now; copy.roll(r); if (in(copy.x, copy.y)) { copy.c += copy.b * p[copy.y][copy.x]; que.push(copy); } } } } return 0; }
#include <iostream> #include <queue> #include <string.h> using namespace std; int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}; struct Dice { int t, b, n, s, e, w; int y, x; int c; Dice(int top, int bottom, int north, int south, int east, int west, int sx, int sy, int cost) { t = top, b = bottom, n = north, s = south, e = east, w = west; x = sx, y = sy, c = cost; } bool operator<(const Dice &r) const { return c > r.c; } void roll(int d) { int tmp; y += dy[d], x += dx[d]; if (d == 0) { tmp = t; t = s; s = b; b = n; n = tmp; } else if (d == 1) { tmp = t; t = e; e = b; b = w; w = tmp; } else if (d == 2) { tmp = t; t = n; n = b; b = s; s = tmp; } else { tmp = t; t = w; w = b; b = e; e = tmp; } } }; int w, h; bool in(int x, int y) { if (x < 0 || y < 0 || x >= w || y >= h) return false; return true; } int main() { int p[10][10], memo[10][10][100]; while (cin >> h >> w && h != 0) { priority_queue<Dice> que; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { cin >> p[y][x]; } memset(memo, -1, sizeof(memo)); int gx, gy, sx, sy; cin >> sy >> sx >> gy >> gx; que.push(Dice(1, 6, 5, 2, 3, 4, sx, sy, 0)); while (!que.empty()) { Dice now = que.top(); que.pop(); // cout<<now.x<<" "<<now.y<<" "<<now.b<<" //"<<now.c<<endl; if (now.x == gx && now.y == gy) { cout << now.c << endl; break; } if (memo[now.y][now.x][now.e * 7 + now.b] >= 0 && memo[now.y][now.x][now.e * 7 + now.b] <= now.c) continue; memo[now.y][now.x][now.e * 7 + now.b] = now.c; for (int r = 0; r < 4; r++) { Dice copy = now; copy.roll(r); if (in(copy.x, copy.y)) { copy.c += copy.b * p[copy.y][copy.x]; que.push(copy); } } } } return 0; }
insert
82
82
82
83
MLE
p00643
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define dump(...) \ cout << 'L' << __LINE__ << ": " << #__VA_ARGS__ << '=' << (__VA_ARGS__) \ << endl template <typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { print_tuple<Cdr...>(os, t); os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { os << '('; print_tuple<Args...>(os, t); return os << ')'; } struct Die { array<int, 6> faces; Die() { faces[0] = 2; faces[1] = 5; faces[2] = 3; faces[3] = 4; faces[4] = 1; faces[5] = 6; } void Rotate(int a, int b, int c, int d) { swap(faces[a], faces[b]); swap(faces[b], faces[c]); swap(faces[c], faces[d]); } void TurnU() { Rotate(4, 0, 5, 1); } void TurnD() { Rotate(4, 1, 5, 0); } void TurnL() { Rotate(4, 2, 5, 3); } void TurnR() { Rotate(4, 3, 5, 2); } }; bool operator<(const Die &a, const Die &b) { return lexicographical_compare(begin(a.faces), end(a.faces), begin(b.faces), end(b.faces)); } bool operator>(const Die &a, const Die &b) { return lexicographical_compare(begin(b.faces), end(b.faces), begin(a.faces), end(a.faces)); } template <typename Functor> struct functor_traits { template <typename C, typename Ret, typename Arg, typename... Args> static Arg helper(Ret (C::*)(Arg, Args...)); template <typename C, typename Ret, typename Arg, typename... Args> static Arg helper(Ret (C::*)(Arg, Args...) const); typedef decltype(helper(&Functor::operator())) first_argument_type; }; template <typename Compare, typename T = typename functor_traits<Compare>::first_argument_type> priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) { return priority_queue<T, vector<T>, Compare>(move(comp)); } int main() { for (int h, w; cin >> h >> w && h | w;) { vector<vector<int>> grid(h, vector<int>(w)); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> grid[i][j]; int si, sj, gi, gj; cin >> si >> sj >> gi >> gj; auto pq = make_priority_queue( [](tuple<Die, int, int, int> a, tuple<Die, int, int, int> b) { return get<3>(a) > get<3>(b); }); pq.emplace(Die(), si, sj, 0); set<tuple<Die, int, int>> vis; while (pq.size()) { Die d; int i, j, c; tie(d, i, j, c) = pq.top(); pq.pop(); if (vis.count(make_tuple(d, i, j))) continue; if (i == gi && j == gj) { cout << c << endl; break; } function<void(Die &)> turns[] = {&Die::TurnU, &Die::TurnD, &Die::TurnL, &Die::TurnR}; for (int k = 0; k < 4; k++) { int ni = i + "\xff\x1\0\0"[k], nj = j + "\0\0\xff\x1"[k]; if (0 <= ni && ni < h && 0 <= nj && nj < w) { Die t = d; turns[k](t); pq.emplace(t, ni, nj, c + t.faces[5] * grid[ni][nj]); } } } } }
#include <bits/stdc++.h> using namespace std; #define dump(...) \ cout << 'L' << __LINE__ << ": " << #__VA_ARGS__ << '=' << (__VA_ARGS__) \ << endl template <typename Tuple> void print_tuple(ostream &, const Tuple &) {} template <typename Car, typename... Cdr, typename Tuple> void print_tuple(ostream &os, const Tuple &t) { print_tuple<Cdr...>(os, t); os << (sizeof...(Cdr) ? "," : "") << get<sizeof...(Cdr)>(t); } template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { os << '('; print_tuple<Args...>(os, t); return os << ')'; } struct Die { array<int, 6> faces; Die() { faces[0] = 2; faces[1] = 5; faces[2] = 3; faces[3] = 4; faces[4] = 1; faces[5] = 6; } void Rotate(int a, int b, int c, int d) { swap(faces[a], faces[b]); swap(faces[b], faces[c]); swap(faces[c], faces[d]); } void TurnU() { Rotate(4, 0, 5, 1); } void TurnD() { Rotate(4, 1, 5, 0); } void TurnL() { Rotate(4, 2, 5, 3); } void TurnR() { Rotate(4, 3, 5, 2); } }; bool operator<(const Die &a, const Die &b) { return lexicographical_compare(begin(a.faces), end(a.faces), begin(b.faces), end(b.faces)); } bool operator>(const Die &a, const Die &b) { return lexicographical_compare(begin(b.faces), end(b.faces), begin(a.faces), end(a.faces)); } template <typename Functor> struct functor_traits { template <typename C, typename Ret, typename Arg, typename... Args> static Arg helper(Ret (C::*)(Arg, Args...)); template <typename C, typename Ret, typename Arg, typename... Args> static Arg helper(Ret (C::*)(Arg, Args...) const); typedef decltype(helper(&Functor::operator())) first_argument_type; }; template <typename Compare, typename T = typename functor_traits<Compare>::first_argument_type> priority_queue<T, vector<T>, Compare> make_priority_queue(Compare comp) { return priority_queue<T, vector<T>, Compare>(move(comp)); } int main() { for (int h, w; cin >> h >> w && h | w;) { vector<vector<int>> grid(h, vector<int>(w)); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> grid[i][j]; int si, sj, gi, gj; cin >> si >> sj >> gi >> gj; auto pq = make_priority_queue( [](tuple<Die, int, int, int> a, tuple<Die, int, int, int> b) { return get<3>(a) > get<3>(b); }); pq.emplace(Die(), si, sj, 0); set<tuple<Die, int, int>> vis; while (pq.size()) { Die d; int i, j, c; tie(d, i, j, c) = pq.top(); pq.pop(); if (vis.count(make_tuple(d, i, j))) continue; vis.insert(make_tuple(d, i, j)); if (i == gi && j == gj) { cout << c << endl; break; } function<void(Die &)> turns[] = {&Die::TurnU, &Die::TurnD, &Die::TurnL, &Die::TurnR}; for (int k = 0; k < 4; k++) { int ni = i + "\xff\x1\0\0"[k], nj = j + "\0\0\xff\x1"[k]; if (0 <= ni && ni < h && 0 <= nj && nj < w) { Die t = d; turns[k](t); pq.emplace(t, ni, nj, c + t.faces[5] * grid[ni][nj]); } } } } }
insert
83
83
83
84
0
p00646
C++
Time Limit Exceeded
// 00 #include <iostream> #include <vector> using namespace std; int main() { bool cmp[1000001] = {}; vector<int> prm; for (int i = 2; i <= 1000000; i++) { if (!cmp[i]) { prm.push_back(i); for (int j = 2; j * i <= 1000000; j++) { cmp[i * j] = true; } } } for (long long l; cin >> l, l;) { int a = 1; for (int i = 0; i < prm.size(); i++) { int c = 0; while (l % prm[i] == 0) { c++; l /= prm[i]; } a *= c * 2 + 1; } if (l != 1) { a *= 3; } cout << (a + 1) / 2 << endl; } return 0; }
// 00 #include <iostream> #include <vector> using namespace std; int main() { bool cmp[1000001] = {}; vector<int> prm; for (int i = 2; i <= 1000000; i++) { if (!cmp[i]) { prm.push_back(i); for (int j = 2; j * i <= 1000000; j++) { cmp[i * j] = true; } } } for (long long l; cin >> l, l;) { int a = 1; for (int i = 0; l != 1 && i < prm.size(); i++) { int c = 0; while (l % prm[i] == 0) { c++; l /= prm[i]; } a *= c * 2 + 1; } if (l != 1) { a *= 3; } cout << (a + 1) / 2 << endl; } return 0; }
replace
19
20
19
20
TLE
p00646
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second #define MP make_pair #define umap unordered_map using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> P; typedef unsigned int uint; vector<pair<ll, int>> v; ll _pow(ll x, ll n) { ll res = 1; while (n) { if (n & 1) res *= x; x *= x; n /= 2; } return res; } int ans; void dfs(int n, ll a, ll b) { if (n == v.size()) { if (a <= b) { // cout << a << ' ' << b << endl; ++ans; } return; } int x = v[n].first; int m = v[n].second; for (int i = 0; i < m; ++i) { dfs(n + 1, a * _pow(x, i), b * _pow(x, m)); dfs(n + 1, a * _pow(x, m), b * _pow(x, i)); } dfs(n + 1, a * _pow(x, m), b * _pow(x, m)); } int main() { ll L; while (cin >> L, L) { ans = 0; v.clear(); for (int i = 2; i * i <= L; ++i) { int cnt = 0; while (L % i == 0) { ++cnt; L /= i; } if (cnt) v.push_back(MP(i, cnt)); } if (L != 1) v.push_back(MP(L, 1)); dfs(0, 1, 1); cout << ans << endl; } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second #define MP make_pair #define umap unordered_map using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> P; typedef unsigned int uint; vector<pair<ll, int>> v; ll _pow(ll x, ll n) { ll res = 1; while (n) { if (n & 1) res *= x; x *= x; n /= 2; } return res; } int ans; void dfs(int n, ll a, ll b) { if (n == v.size()) { if (a <= b) { // cout << a << ' ' << b << endl; ++ans; } return; } int x = v[n].first; int m = v[n].second; for (int i = 0; i < m; ++i) { dfs(n + 1, a * _pow(x, i), b * _pow(x, m)); dfs(n + 1, a * _pow(x, m), b * _pow(x, i)); } dfs(n + 1, a * _pow(x, m), b * _pow(x, m)); } int main() { ll L; while (cin >> L, L) { ans = 0; v.clear(); for (ll i = 2; i * i <= L; ++i) { int cnt = 0; while (L % i == 0) { ++cnt; L /= i; } if (cnt) v.push_back(MP(i, cnt)); } if (L != 1) v.push_back(MP(L, 1)); dfs(0, 1, 1); cout << ans << endl; } return 0; }
replace
82
83
82
83
TLE
p00646
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); vector<ll> divisors; divisors.reserve(4096); while (true) { ll L; cin >> L; if (L == 0) { break; } divisors.clear(); for (ll i = 1; i * i <= L; ++i) { if (L % i != 0) { continue; } divisors.push_back(i); if (i * i != L) { divisors.push_back(L / i); } } sort(divisors.begin(), divisors.end()); int answer = 0; for (int i = 0; i < divisors.size(); ++i) { ll a = divisors[i]; for (int j = i; j < divisors.size(); ++j) { ll b = divisors[j]; ll lcm = a * (b / __gcd(a, b)); if (lcm == L) { ++answer; } } } cout << answer << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); vector<ll> divisors; divisors.reserve(4096); while (true) { ll L; cin >> L; if (L == 0) { break; } divisors.clear(); for (ll i = 1; i * i <= L; ++i) { if (L % i != 0) { continue; } divisors.push_back(i); if (i * i != L) { divisors.push_back(L / i); } } sort(divisors.begin(), divisors.end()); int answer = 0; for (int i = 0; i < divisors.size(); ++i) { ll b = divisors[i]; vector<ll>::iterator it = lower_bound(divisors.begin(), divisors.end(), L / b); for (int j = it - divisors.begin(); j <= i; ++j) { ll a = divisors[j]; if (a * (b / __gcd(a, b)) == L) { ++answer; } } } cout << answer << endl; } return 0; }
replace
30
35
30
36
TLE
p00646
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; const long long int MAX_ = 10000000; vector<long long int> prime; bool is_prime[MAX_]; void sieve() { fill(is_prime, is_prime + MAX_, true); is_prime[0] = is_prime[1] = false; for (long long int i = 2; i < MAX_; ++i) { if (is_prime[i]) { prime.push_back(i); for (long long int j = i * 2; j < MAX_; j += i) { is_prime[j] = false; } } } } long long int gcd(long long int a, long long int b) { while (b != 0) { long long int tmp = a % b; a = b; b = tmp; } return a; } vector<long long int> *divisor(long long int x) { static vector<long long int> result; result.clear(); for (long long int i = 1; i * i <= x; ++i) { if (x % i == 0) { result.push_back(i); long long int tmp = x / i; if (i != tmp) result.push_back(tmp); } } sort(result.begin(), result.end()); return &result; } map<long long int, long long int> *prime_factor(long long int n) { static map<long long int, long long int> result; result.clear(); for (auto it = prime.begin(); (*it) * (*it) <= n && it != prime.end(); ++it) { while (n % (*it) == 0) { ++result[(*it)]; n /= (*it); } } if (n != 1) result[n] = 1; return &result; } int solve(long long int l) { int ans = 0; vector<long long int> v = (*divisor(l)); map<long long int, long long int> table = (*prime_factor(l)); for (auto it = v.begin(); it != v.end(); ++it) { /* for(auto it_=v.begin(); (*it_)<=(*it)&&it_!=v.end(); ++it_){ if((*it)*(*it_)/gcd((*it),(*it_))==l) ++ans; } */ map<long long int, long long int> table_ = (*prime_factor(*it)); long long int tmp = 1; for (auto it_ = table.begin(); it_ != table.end(); ++it_) { if (table_[it_->first] == it_->second) tmp *= it_->second + 1; // cout << it_->second << endl; } // cout << tmp << endl; ans += tmp; } ans = (ans + 1) / 2; return ans; } int main() { sieve(); long long int L; while (cin >> L && L) { cout << solve(L) << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; const long long int MAX_ = 1000000; vector<long long int> prime; bool is_prime[MAX_]; void sieve() { fill(is_prime, is_prime + MAX_, true); is_prime[0] = is_prime[1] = false; for (long long int i = 2; i < MAX_; ++i) { if (is_prime[i]) { prime.push_back(i); for (long long int j = i * 2; j < MAX_; j += i) { is_prime[j] = false; } } } } long long int gcd(long long int a, long long int b) { while (b != 0) { long long int tmp = a % b; a = b; b = tmp; } return a; } vector<long long int> *divisor(long long int x) { static vector<long long int> result; result.clear(); for (long long int i = 1; i * i <= x; ++i) { if (x % i == 0) { result.push_back(i); long long int tmp = x / i; if (i != tmp) result.push_back(tmp); } } sort(result.begin(), result.end()); return &result; } map<long long int, long long int> *prime_factor(long long int n) { static map<long long int, long long int> result; result.clear(); for (auto it = prime.begin(); (*it) * (*it) <= n && it != prime.end(); ++it) { while (n % (*it) == 0) { ++result[(*it)]; n /= (*it); } } if (n != 1) result[n] = 1; return &result; } int solve(long long int l) { int ans = 0; vector<long long int> v = (*divisor(l)); map<long long int, long long int> table = (*prime_factor(l)); for (auto it = v.begin(); it != v.end(); ++it) { /* for(auto it_=v.begin(); (*it_)<=(*it)&&it_!=v.end(); ++it_){ if((*it)*(*it_)/gcd((*it),(*it_))==l) ++ans; } */ map<long long int, long long int> table_ = (*prime_factor(*it)); long long int tmp = 1; for (auto it_ = table.begin(); it_ != table.end(); ++it_) { if (table_[it_->first] == it_->second) tmp *= it_->second + 1; // cout << it_->second << endl; } // cout << tmp << endl; ans += tmp; } ans = (ans + 1) / 2; return ans; } int main() { sieve(); long long int L; while (cin >> L && L) { cout << solve(L) << endl; } return 0; }
replace
6
7
6
7
TLE
p00646
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; vector<int> fact(ll L) { ll N = L; vector<int> res; for (ll i = 2; i * i <= N; i++) if (L % i == 0) { int cnt = 0; while (L % i == 0) { L /= i; cnt++; } res.push_back(cnt); } if (L != 1) res.push_back(1); return res; } int main() { ll L; while (cin >> L && L) { vector<int> v = fact(L); int dp[2] = {}; dp[1] = v[0]; for (int i = 1; i < v.size(); i++) { dp[(i + 1) & 1] = dp[i & 1] * 2 * v[i] + v[i] + dp[i & 1]; } cout << dp[v.size() & 1] + 1 << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; vector<int> fact(ll L) { ll N = L; vector<int> res; for (ll i = 2; i * i <= N; i++) if (L % i == 0) { int cnt = 0; while (L % i == 0) { L /= i; cnt++; } res.push_back(cnt); } if (L != 1) res.push_back(1); return res; } int main() { ll L; while (cin >> L && L) { if (L == 1) { cout << 1 << endl; continue; } vector<int> v = fact(L); int dp[2] = {}; dp[1] = v[0]; for (int i = 1; i < v.size(); i++) { dp[(i + 1) & 1] = dp[i & 1] * 2 * v[i] + v[i] + dp[i & 1]; } cout << dp[v.size() & 1] + 1 << endl; } return 0; }
insert
57
57
57
61
0
p00646
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define f first #define s second #define mp make_pair #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(c) (c).begin(), (c).end() using namespace std; typedef unsigned int uint; typedef long long ll; template <int m> class Prime { std::vector<bool> p; std::vector<int> ps; public: Prime() { p = std::vector<bool>(m, true); assert(m > 1); p[0] = p[1] = false; for (int i = 4; i < m; i += 2) p[i] = false; ps.push_back(2); for (int i = 3; i < m; i += 2) { if (p[i]) { ps.push_back(i); for (int j = i + i; j < m; j += i) p[j] = false; } } } bool isPrime(int n) { assert(m > n); return p[n]; } int operator[](int n) { return ps[n]; } int size() { return ps.size(); } }; int main() { Prime<1000000 + 1000> p; ll n; while (cin >> n, n) { long long ans = 1; long long org = n; for (int i = 0; p[i] <= n && p[i] * p[i] <= org; i++) { int cc = 0; while (n % p[i] == 0) { cc++; n /= p[i]; } if (cc != 0) ans *= (cc + 1) + (cc + 1) - 1; } if (n != 1) ans *= 3; cout << (ans - 1) / 2 + 1 << endl; } return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> #define f first #define s second #define mp make_pair #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(c) (c).begin(), (c).end() using namespace std; typedef unsigned int uint; typedef long long ll; template <int m> class Prime { std::vector<bool> p; std::vector<int> ps; public: Prime() { p = std::vector<bool>(m, true); assert(m > 1); p[0] = p[1] = false; for (int i = 4; i < m; i += 2) p[i] = false; ps.push_back(2); for (int i = 3; i < m; i += 2) { if (p[i]) { ps.push_back(i); for (int j = i + i; j < m; j += i) p[j] = false; } } } bool isPrime(int n) { assert(m > n); return p[n]; } int operator[](int n) { return ps[n]; } int size() { return ps.size(); } }; int main() { Prime<1000000 + 1000> p; ll n; while (cin >> n, n) { long long ans = 1; long long org = n; for (int i = 0; p[i] <= n && (ll)p[i] * p[i] <= org; i++) { int cc = 0; while (n % p[i] == 0) { cc++; n /= p[i]; } if (cc != 0) ans *= (cc + 1) + (cc + 1) - 1; } if (n != 1) ans *= 3; cout << (ans - 1) / 2 + 1 << endl; } return 0; }
replace
64
65
64
65
0
p00646
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<ll> so, v; ll ans; void dfs(int d, int t) { if (d == v.size()) { ans += t; return; } for (int i = 0; i <= v[d]; i++) { int nt = t; if (v[d] == i) nt *= i + 1; dfs(d + 1, nt); } } int main() { ll n; while (cin >> n && n) { ans = 1; v.clear(); ll tmp = n; for (int i = 2; i * i <= n; i++) if (tmp % i == 0) { v.push_back(0); while (tmp % i == 0) tmp /= i, v[v.size() - 1]++; } if (tmp > 1) v.push_back(1); dfs(0, 1); cout << ans / 2 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<ll> so, v; ll ans; void dfs(int d, int t) { if (d == v.size()) { ans += t; return; } for (int i = 0; i <= v[d]; i++) { int nt = t; if (v[d] == i) nt *= i + 1; dfs(d + 1, nt); } } int main() { ll n; while (cin >> n && n) { ans = 1; v.clear(); ll tmp = n; for (ll i = 2; i * i <= n; i++) if (tmp % i == 0) { v.push_back(0); while (tmp % i == 0) tmp /= i, v[v.size() - 1]++; } if (tmp > 1) v.push_back(1); dfs(0, 1); cout << ans / 2 << endl; } return 0; }
replace
24
25
24
25
TLE
p00650
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; void add_edge(Graph &G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, c, G[a].size() - 1)); } int dfs(Graph &G, int u, int t, int f, vector<bool> &used) { if (u == t) return f; used[u] = true; FORIT(e, G[u]) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(Graph G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { Graph G(N); int minus = 0; REP(i, M) { int a, b, c; cin >> a >> b >> c; if (c <= 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; REP(s, N) REP(t, N) if (s != t) { ans = min(ans, max_flow(G, s, t)); } cout << ans + minus << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; void add_edge(Graph &G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, c, G[a].size() - 1)); } int dfs(Graph &G, int u, int t, int f, vector<bool> &used) { if (u == t) return f; used[u] = true; FORIT(e, G[u]) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(Graph G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { Graph G(N); int minus = 0; REP(i, M) { int a, b, c; cin >> a >> b >> c; if (c <= 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; FOR(t, 1, N) { ans = min(ans, max_flow(G, 0, t)); } cout << ans + minus << endl; } return 0; }
replace
88
89
88
89
TLE
p00650
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #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 <vector> using namespace std; #define INF (1 << 29) // math #define Sq(x) ((x) * (x)) // container utility #define ALL(x) (x).begin(), (x).end() #define MP make_pair #define PB push_back #define EACH(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) // rep #define REP(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef long long ll; // conversion template <class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); } inline int toInt(string s) { return atoi(s.c_str()); } // prime bool isPrime(int a) { for (int i = 2; i * i <= a; i++) if (a % i == 0) return false; return true; } // !!! set MAX_L number !!! #define MAX_L (1000001) #define MAX_SQRT_B ((int)sqrt(INT_MAX) + 1) bool is_prime[MAX_L]; vector<bool> is_prime_small(MAX_SQRT_B); void segment_sieve(ll a, ll b) { for (int i = 0; (ll)i * i < b; i++) is_prime_small[i] = true; for (int i = 0; i < b - a; i++) is_prime[i] = true; if (a == 1) { is_prime[0] = false; } is_prime_small[0] = is_prime_small[1] = false; for (int i = 2; (ll)i * i < b; i++) { if (is_prime_small[i]) { for (int j = 2 * i; (ll)j * j < b; j += i) is_prime_small[j] = false; for (ll j = max(2LL, (a + i - 1) / i) * i; j < b; j += i) is_prime[j - a] = false; } } } ////////////////////////////////////////////////////////////// struct Edge { int to, cost, id; Edge(int t, int c, int i) : to(t), cost(c), id(i) {} }; #define MAX_E (98) vector<Edge> es[MAX_E]; int N, M; bool isConnected(int x) { vector<bool> used(N); vector<int> VEC; VEC.PB(0); used[0] = 1; int v = 0; while (!VEC.empty()) { vector<int> NEXT; EACH(i, VEC) { EACH(e, es[*i]) { if (e->id != x && !used[e->to]) { used[e->to] = 1; NEXT.PB(e->to); } } v++; } VEC = NEXT; } return N == v; } int main() { while (cin >> N >> M && (N | M)) { for (int i = 0; i < MAX_E; i++) { es[i].clear(); } int base = 0; int id = 0; vector<int> cost; for (int i = 0; i < M; i++) { int s, t, c; cin >> s >> t >> c; if (c <= 0) base += c; else { es[s].PB(Edge(t, c, id)); es[t].PB(Edge(s, c, id)); cost.PB(c); id++; } } int E = id; int ans = INF; if (!isConnected(-1)) ans = 0; for (int i = 0; i < E; i++) { if (!isConnected(i)) ans = min(ans, cost[i]); } for (int i = 0; i < E - 1; i++) for (int j = i + 1; j < E; j++) ans = min(ans, cost[i] + cost[j]); cout << base + ans << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #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 <vector> using namespace std; #define INF (1 << 29) // math #define Sq(x) ((x) * (x)) // container utility #define ALL(x) (x).begin(), (x).end() #define MP make_pair #define PB push_back #define EACH(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) // rep #define REP(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) // debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef long long ll; // conversion template <class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); } inline int toInt(string s) { return atoi(s.c_str()); } // prime bool isPrime(int a) { for (int i = 2; i * i <= a; i++) if (a % i == 0) return false; return true; } // !!! set MAX_L number !!! #define MAX_L (1000001) #define MAX_SQRT_B ((int)sqrt(INT_MAX) + 1) bool is_prime[MAX_L]; vector<bool> is_prime_small(MAX_SQRT_B); void segment_sieve(ll a, ll b) { for (int i = 0; (ll)i * i < b; i++) is_prime_small[i] = true; for (int i = 0; i < b - a; i++) is_prime[i] = true; if (a == 1) { is_prime[0] = false; } is_prime_small[0] = is_prime_small[1] = false; for (int i = 2; (ll)i * i < b; i++) { if (is_prime_small[i]) { for (int j = 2 * i; (ll)j * j < b; j += i) is_prime_small[j] = false; for (ll j = max(2LL, (a + i - 1) / i) * i; j < b; j += i) is_prime[j - a] = false; } } } ////////////////////////////////////////////////////////////// struct Edge { int to, cost, id; Edge(int t, int c, int i) : to(t), cost(c), id(i) {} }; #define MAX_E (99) vector<Edge> es[MAX_E]; int N, M; bool isConnected(int x) { vector<bool> used(N); vector<int> VEC; VEC.PB(0); used[0] = 1; int v = 0; while (!VEC.empty()) { vector<int> NEXT; EACH(i, VEC) { EACH(e, es[*i]) { if (e->id != x && !used[e->to]) { used[e->to] = 1; NEXT.PB(e->to); } } v++; } VEC = NEXT; } return N == v; } int main() { while (cin >> N >> M && (N | M)) { for (int i = 0; i < MAX_E; i++) { es[i].clear(); } int base = 0; int id = 0; vector<int> cost; for (int i = 0; i < M; i++) { int s, t, c; cin >> s >> t >> c; if (c <= 0) base += c; else { es[s].PB(Edge(t, c, id)); es[t].PB(Edge(s, c, id)); cost.PB(c); id++; } } int E = id; int ans = INF; if (!isConnected(-1)) ans = 0; for (int i = 0; i < E; i++) { if (!isConnected(i)) ans = min(ans, cost[i]); } for (int i = 0; i < E - 1; i++) for (int j = i + 1; j < E; j++) ans = min(ans, cost[i] + cost[j]); cout << base + ans << endl; } return 0; }
replace
108
109
108
109
0
p00651
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; #include <complex> #define CURR(P, i) (P[(i) % P.size()]) #define NEXT(P, i) (P[((i) + 1) % P.size()]) typedef complex<double> Point; typedef vector<Point> Polygon; struct Line : public vector<Point> { Line() { ; } Line(Point a, Point b) { push_back(a); push_back(b); } }; struct Circle { Point p; double r; Circle() { ; } Circle(Point p, double r) : p(p), r(r) { ; } }; namespace std { bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } // namespace std inline double cross(const Point &a, const Point &b) { return imag(conj(a) * b); } inline double dot(const Point &a, const Point &b) { return real(conj(a) * b); } int ccw(Point a, Point b, Point c) { b -= a; c -= a; double len = abs(b) * abs(c); if (cross(b, c) > +EPS * len) return +1; // counter-clockwise if (cross(b, c) < -EPS * len) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b if (norm(b) < norm(c)) return -2; // a--b--c return 0; // a--c--b } enum { OUT, ON, IN }; int contains(const Circle &C, Point p) { p -= C.p; if (abs(C.r - abs(p)) < EPS) return ON; if (C.r > abs(p)) return IN; if (C.r < abs(p)) return OUT; assert(false); } // pをm中心にtだけ回転 Point rotate(Point p, Point m, double t) { p -= m; p = Point(p.real() * cos(t) - p.imag() * sin(t), p.real() * sin(t) + p.imag() * cos(t)); return p + m; } Polygon rotate(Polygon P, Point m, double t) { REP(i, P.size()) { P[i] = rotate(P[i], m, t); } return P; } int main() { int N, R, Q; while (cin >> N >> R >> Q && N) { Polygon object; REP(i, N) { int x, y; cin >> x >> y; object.push_back(Point(x, y)); } Circle sphere(Point(0, 0), (double)R); int touchindex = -1; REP(i, N) { if (contains(sphere, object[i]) == ON) touchindex = i; } assert(touchindex != -1); REP(iter, Q) { double lower = 0, upper = 2.0 * M_PI; REP(iter2, 60) { double mid = (lower + upper) / 2.0; Polygon rotated = rotate(object, object[touchindex], mid); bool pointisout = false; REP(i, N) { if (i == touchindex) continue; int judge = contains(sphere, rotated[i]); if (judge == OUT) pointisout = true; } if (pointisout) { upper = mid; } else { lower = mid; } } int newtouchindex = -1; Polygon newobject = rotate(object, object[touchindex], lower); REP(i, N) { if (i != touchindex && contains(sphere, newobject[i]) == ON) { if (newtouchindex == -1 || abs(newobject[newtouchindex] - object[touchindex]) < abs(newobject[i] - object[touchindex])) { newtouchindex = i; } } } assert(newtouchindex != -1); printf("%.12f %.12f\n", newobject[newtouchindex].real(), newobject[newtouchindex].imag()); object = newobject; touchindex = newtouchindex; } } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cout << *i << " "; cout << endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; #include <complex> #define CURR(P, i) (P[(i) % P.size()]) #define NEXT(P, i) (P[((i) + 1) % P.size()]) typedef complex<double> Point; typedef vector<Point> Polygon; struct Line : public vector<Point> { Line() { ; } Line(Point a, Point b) { push_back(a); push_back(b); } }; struct Circle { Point p; double r; Circle() { ; } Circle(Point p, double r) : p(p), r(r) { ; } }; namespace std { bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } // namespace std inline double cross(const Point &a, const Point &b) { return imag(conj(a) * b); } inline double dot(const Point &a, const Point &b) { return real(conj(a) * b); } int ccw(Point a, Point b, Point c) { b -= a; c -= a; double len = abs(b) * abs(c); if (cross(b, c) > +EPS * len) return +1; // counter-clockwise if (cross(b, c) < -EPS * len) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b if (norm(b) < norm(c)) return -2; // a--b--c return 0; // a--c--b } enum { OUT, ON, IN }; int contains(const Circle &C, Point p) { p -= C.p; if (abs(C.r - abs(p)) < EPS) return ON; if (C.r > abs(p)) return IN; if (C.r < abs(p)) return OUT; assert(false); } // pをm中心にtだけ回転 Point rotate(Point p, Point m, double t) { p -= m; p = Point(p.real() * cos(t) - p.imag() * sin(t), p.real() * sin(t) + p.imag() * cos(t)); return p + m; } Polygon rotate(Polygon P, Point m, double t) { REP(i, P.size()) { P[i] = rotate(P[i], m, t); } return P; } int main() { int N, R, Q; while (cin >> N >> R >> Q && N) { Polygon object; REP(i, N) { int x, y; cin >> x >> y; object.push_back(Point(x, y)); } Circle sphere(Point(0, 0), (double)R); int touchindex = -1; REP(i, N) { if (contains(sphere, object[i]) == ON) touchindex = i; } assert(touchindex != -1); REP(iter, Q) { double lower = 0, upper = 2.0 * M_PI; REP(iter2, 40) { double mid = (lower + upper) / 2.0; Polygon rotated = rotate(object, object[touchindex], mid); bool pointisout = false; REP(i, N) { if (i == touchindex) continue; int judge = contains(sphere, rotated[i]); if (judge == OUT) pointisout = true; } if (pointisout) { upper = mid; } else { lower = mid; } } int newtouchindex = -1; Polygon newobject = rotate(object, object[touchindex], lower); REP(i, N) { if (i != touchindex && contains(sphere, newobject[i]) == ON) { if (newtouchindex == -1 || abs(newobject[newtouchindex] - object[touchindex]) < abs(newobject[i] - object[touchindex])) { newtouchindex = i; } } } assert(newtouchindex != -1); printf("%.12f %.12f\n", newobject[newtouchindex].real(), newobject[newtouchindex].imag()); object = newobject; touchindex = newtouchindex; } } return 0; }
replace
120
121
120
121
TLE
p00651
C++
Time Limit Exceeded
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define FOR(i, b, n) for (int i = b; i < n; i++) #define RFOR(i, b, n) for (int i = n - 1; i >= b; i--) #define CLR(mat) memset(mat, 0, sizeof(mat)) #define NCLR(mat) memset(mat, -1, sizeof(mat)) #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define PI (acos(-1.0)) #define POSARG(a) (arg((a)) > 0.0 ? arg((a)) : 2.0 * PI + arg((a))) using namespace std; typedef complex<double> P; int n, r, q, center; // center...回転の中心となる点のある添字 vector<P> vc; P rotate_p(P a, P c, double t) { a = a - c; a = P(a.real() * cos(t) - a.imag() * sin(t), a.real() * sin(t) + a.imag() * cos(t)); return a + c; } vector<P> rotate_vc(vector<P> vc, P c, double t) { for (int i = 0; i < (int)vc.size(); i++) { vc[i] = rotate_p(vc[i], c, t); } return vc; } void solve() { FOR(k, 0, q) { double upper = 2.0 * PI, lower = 0; FOR(j, 0, 100) { double mid = (upper + lower) / 2; vector<P> vc_r = rotate_vc(vc, vc[center], mid); bool out = false; FOR(i, 0, n) { if (abs(vc_r[i]) > r) { out = true; } } if (out) { upper = mid; } else { lower = mid; } } vector<P> vc_r = rotate_vc(vc, vc[center], lower); int newcenter = -1; FOR(i, 0, n) { if (i != center && EQ(abs(vc_r[i]), r)) { if (newcenter < 0 || abs(vc_r[i] - vc[center]) > abs(vc_r[newcenter] - vc[center])) { newcenter = i; } } } printf("%.12lf %.12lf\n", vc_r[newcenter].real(), vc_r[newcenter].imag()); vc = vc_r; center = newcenter; } return; } int main() { while (cin >> n >> r >> q, (n || r || q)) { vc.clear(); FOR(i, 0, n) { double x, y; cin >> x >> y; vc.push_back(P(x, y)); if (EQ(x * x + y * y, r * r)) center = i; } solve(); } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define FOR(i, b, n) for (int i = b; i < n; i++) #define RFOR(i, b, n) for (int i = n - 1; i >= b; i--) #define CLR(mat) memset(mat, 0, sizeof(mat)) #define NCLR(mat) memset(mat, -1, sizeof(mat)) #define EPS (1e-10) #define EQ(a, b) (abs((a) - (b)) < EPS) #define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag())) #define PI (acos(-1.0)) #define POSARG(a) (arg((a)) > 0.0 ? arg((a)) : 2.0 * PI + arg((a))) using namespace std; typedef complex<double> P; int n, r, q, center; // center...回転の中心となる点のある添字 vector<P> vc; P rotate_p(P a, P c, double t) { a = a - c; a = P(a.real() * cos(t) - a.imag() * sin(t), a.real() * sin(t) + a.imag() * cos(t)); return a + c; } vector<P> rotate_vc(vector<P> vc, P c, double t) { for (int i = 0; i < (int)vc.size(); i++) { vc[i] = rotate_p(vc[i], c, t); } return vc; } void solve() { FOR(k, 0, q) { double upper = 2.0 * PI, lower = 0; FOR(j, 0, 50) { double mid = (upper + lower) / 2; vector<P> vc_r = rotate_vc(vc, vc[center], mid); bool out = false; FOR(i, 0, n) { if (abs(vc_r[i]) > r) { out = true; } } if (out) { upper = mid; } else { lower = mid; } } vector<P> vc_r = rotate_vc(vc, vc[center], lower); int newcenter = -1; FOR(i, 0, n) { if (i != center && EQ(abs(vc_r[i]), r)) { if (newcenter < 0 || abs(vc_r[i] - vc[center]) > abs(vc_r[newcenter] - vc[center])) { newcenter = i; } } } printf("%.12lf %.12lf\n", vc_r[newcenter].real(), vc_r[newcenter].imag()); vc = vc_r; center = newcenter; } return; } int main() { while (cin >> n >> r >> q, (n || r || q)) { vc.clear(); FOR(i, 0, n) { double x, y; cin >> x >> y; vc.push_back(P(x, y)); if (EQ(x * x + y * y, r * r)) center = i; } solve(); } return 0; }
replace
59
60
59
60
TLE
p00653
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair template <class T, class S> inline bool minup(T &m, const S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, const S x) { return m < (T)x ? (m = (T)x, true) : false; } static const unsigned INF = 0xFFFFFFFF; static const ll MOD = 1000000007LL; static const double EPS = 1E-10; int r, c, q; int _r, _c; int g; unsigned int _seg[11111111]; vector<unsigned int *> seg; int sx, sy, tx, ty; void init() { _r = 1; _c = 1; while (_r < r) _r *= 2; while (_c < c) _c *= 2; seg.resize(2 * _c - 1); rep(i, 2 * _r - 1) seg[i] = _seg + i * (2 * _c - 1); rep(i, 2 * _r - 1) rep(j, 2 * _c - 1) seg[i][j] = INF; } void update(int a, int b, unsigned int x) { a += _r - 1; b += _c - 1; seg[a][b] = x; while (a >= 0) { int tmp = b; while (b > 0) { b = (b - 1) / 2; seg[a][b] = min(seg[a][2 * b + 1], seg[a][2 * b + 2]); } b = tmp; if (a == 0) break; a = (a - 1) / 2; seg[a][b] = min(seg[2 * a + 1][b], seg[2 * a + 2][b]); } } unsigned int _query(int sy, int ty, int kk, int k = 0, int l = 0, int r = _c) { if (r <= sy || ty <= l) return INF; if (sy <= l && r <= ty) return seg[kk][k]; return min(_query(sy, ty, kk, 2 * k + 1, l, (l + r) / 2), _query(sy, ty, kk, 2 * k + 2, (l + r) / 2, r)); } unsigned int query(int sx, int sy, int tx, int ty, int k = 0, int l = 0, int r = _r) { if (r <= sx || tx <= l) return INF; if (sx <= l && r <= tx) return _query(sy, ty, k); return min(query(sx, sy, tx, ty, 2 * k + 1, l, (l + r) / 2), query(sx, sy, tx, ty, 2 * k + 2, (l + r) / 2, r)); } int main(int argc, char *argv[]) { while (cin >> r >> c >> q, r) { init(); rep(i, r) rep(j, c) { cin >> g; update(i, j, g); } rep(i, q) { cin >> sx >> sy >> tx >> ty; cout << query(sx, sy, tx + 1, ty + 1) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vii; #define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++) #define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++) #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--) #define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--) #define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--) #define vrep(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair template <class T, class S> inline bool minup(T &m, const S x) { return m > (T)x ? (m = (T)x, true) : false; } template <class T, class S> inline bool maxup(T &m, const S x) { return m < (T)x ? (m = (T)x, true) : false; } static const unsigned INF = 0xFFFFFFFF; static const ll MOD = 1000000007LL; static const double EPS = 1E-10; int r, c, q; int _r, _c; int g; unsigned int _seg[11111111]; vector<unsigned int *> seg; int sx, sy, tx, ty; void init() { _r = 1; _c = 1; while (_r < r) _r *= 2; while (_c < c) _c *= 2; seg.resize(2 * _r - 1); rep(i, 2 * _r - 1) seg[i] = _seg + i * (2 * _c - 1); rep(i, 2 * _r - 1) rep(j, 2 * _c - 1) seg[i][j] = INF; } void update(int a, int b, unsigned int x) { a += _r - 1; b += _c - 1; seg[a][b] = x; while (a >= 0) { int tmp = b; while (b > 0) { b = (b - 1) / 2; seg[a][b] = min(seg[a][2 * b + 1], seg[a][2 * b + 2]); } b = tmp; if (a == 0) break; a = (a - 1) / 2; seg[a][b] = min(seg[2 * a + 1][b], seg[2 * a + 2][b]); } } unsigned int _query(int sy, int ty, int kk, int k = 0, int l = 0, int r = _c) { if (r <= sy || ty <= l) return INF; if (sy <= l && r <= ty) return seg[kk][k]; return min(_query(sy, ty, kk, 2 * k + 1, l, (l + r) / 2), _query(sy, ty, kk, 2 * k + 2, (l + r) / 2, r)); } unsigned int query(int sx, int sy, int tx, int ty, int k = 0, int l = 0, int r = _r) { if (r <= sx || tx <= l) return INF; if (sx <= l && r <= tx) return _query(sy, ty, k); return min(query(sx, sy, tx, ty, 2 * k + 1, l, (l + r) / 2), query(sx, sy, tx, ty, 2 * k + 2, (l + r) / 2, r)); } int main(int argc, char *argv[]) { while (cin >> r >> c >> q, r) { init(); rep(i, r) rep(j, c) { cin >> g; update(i, j, g); } rep(i, q) { cin >> sx >> sy >> tx >> ty; cout << query(sx, sy, tx + 1, ty + 1) << endl; } } return 0; }
replace
43
44
43
44
0
p00653
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> using namespace std; #define dump(n) cout << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; const int INFTY = INT_MAX; const double EPS = 1e-9; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '['; rep(i, a.size()) os << (i ? " " : "") << a[i]; return os << ']'; } struct SegTree { int size; vi data; SegTree(int s) : size(Need(s)), data(size * 2, INFTY) {} void Update(int i, int x) { for (i += size; i; i >>= 1) data[i] = min(data[i], x); } int Query(int a, int b, int i, int l, int r) { if (b <= l || r <= a) return INFTY; if (a <= l && r <= b) return data[i]; int res = INFTY, m = (l + r) / 2; if (a < m) res = min(res, Query(a, b, i * 2 + 0, l, m)); if (m < b) res = min(res, Query(a, b, i * 2 + 1, m, r)); return res; } int Query(int a, int b) { return Query(a, b, 1, 0, size); } int Need(int size) { size--; rep(i, 5) size |= size >> (1 << i); return size + 1; } }; int main() { for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) { vvi grid; if (r <= c) { grid.assign(r, vi(c)); rep(i, r) rep(j, c) scanf("%d", &grid[i][j]); } else { grid.assign(c, vi(r)); rep(i, r) rep(j, c) scanf("%d", &grid[j][i]); } vector<SegTree> sts(r, SegTree(c)); rep(i, r) rep(j, c) sts[i].Update(j, grid[i][j]); while (q--) { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int res = INFTY; if (r <= c) repi(i, r1, r2 + 1) res = min(res, sts[i].Query(c1, c2 + 1)); else repi(i, c1, c2 + 1) res = min(res, sts[i].Query(r1, r2 + 1)); printf("%d\n", res); } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> using namespace std; #define dump(n) cout << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; const int INFTY = INT_MAX; const double EPS = 1e-9; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << '(' << p.first << ',' << p.second << ')'; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '['; rep(i, a.size()) os << (i ? " " : "") << a[i]; return os << ']'; } struct SegTree { int size; vi data; SegTree(int s) : size(Need(s)), data(size * 2, INFTY) {} void Update(int i, int x) { for (i += size; i; i >>= 1) data[i] = min(data[i], x); } int Query(int a, int b, int i, int l, int r) { if (b <= l || r <= a) return INFTY; if (a <= l && r <= b) return data[i]; int res = INFTY, m = (l + r) / 2; if (a < m) res = min(res, Query(a, b, i * 2 + 0, l, m)); if (m < b) res = min(res, Query(a, b, i * 2 + 1, m, r)); return res; } int Query(int a, int b) { return Query(a, b, 1, 0, size); } int Need(int size) { size--; rep(i, 5) size |= size >> (1 << i); return size + 1; } }; int main() { for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) { vvi grid; if (r <= c) { grid.assign(r, vi(c)); rep(i, r) rep(j, c) scanf("%d", &grid[i][j]); } else { grid.assign(c, vi(r)); rep(i, r) rep(j, c) scanf("%d", &grid[j][i]); } vector<SegTree> sts; if (r <= c) { sts.assign(r, SegTree(c)); rep(i, r) rep(j, c) sts[i].Update(j, grid[i][j]); } else { sts.assign(c, SegTree(r)); rep(i, c) rep(j, r) sts[i].Update(j, grid[i][j]); } while (q--) { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int res = INFTY; if (r <= c) repi(i, r1, r2 + 1) res = min(res, sts[i].Query(c1, c2 + 1)); else repi(i, c1, c2 + 1) res = min(res, sts[i].Query(r1, r2 + 1)); printf("%d\n", res); } } return 0; }
replace
94
96
94
102
0
p00653
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; typedef int ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> ipair; typedef tuple<int, int, int> ituple; // const int MOD = (int)1e9 + 7; // const double EPS = 1e-10; #define PI acosl(-1) #define MAX_N 8000005 ll h, w, H, W, q; ll dat[MAX_N]; class SegmentTree2D { protected: // ??¨????????????????????????????????¢??° virtual ll _func(ll a, ll b) = 0; ll query_h(int li, int lj, int ri, int rj, int si, int ti, int k) { if (ri <= si or ti <= li) return _getDefaultValue(); if (li <= si and ti <= ri) return query_w(lj, rj, 0, W, k, 0); const int mi = (si + ti) / 2; return _func(query_h(li, lj, ri, rj, si, mi, 2 * k + 1), query_h(li, lj, ri, rj, mi, ti, 2 * k + 2)); } ll query_w(int lj, int rj, int sj, int tj, int i, int k) { if (rj <= sj or tj <= lj) return _getDefaultValue(); if (lj <= sj and tj <= rj) return get(i, k); const int mj = (sj + tj) / 2; return _func(query_w(lj, rj, sj, mj, i, 2 * k + 1), query_w(lj, rj, mj, tj, i, 2 * k + 2)); } public: // ??¨??????????????????????????????????????????????????????????????? virtual ll _getDefaultValue() = 0; SegmentTree2D() {} ll get(int hh, int ww) { // printf("get (%d, %d)[%lld] = %lld\n", hh, ww, hh*W+ww, dat[hh * W + ww]); return dat[hh * 2 * W + ww]; } void set(int hh, int ww, ll v) { // printf("set (%d, %d)[%lld] = %lld\n", hh, ww, hh*W+ww, v); dat[hh * 2 * W + ww] = v; } void init() { // H = W = 1; // while(H < (int)f.size()) H <<= 1; // while(W < (int)f[0].size()) W <<= 1; // dat.assign(2*H-1,VL(2*W-1, _getDefaultValue())); for (int i = 2 * H - 2; i > H - 2; i--) for (int j = W - 2; j >= 0; j--) { set(i, j, _func(get(i, 2 * j + 1), get(i, 2 * j + 2))); } for (int i = H - 2; i >= 0; i--) for (int j = 0; j < 2 * W - 1; j++) { set(i, j, _func(get(2 * i + 1, j), get(2 * i + 2, j))); } } // [(li, ri), (lj, rj)]??????????????¨???????±??????? ll query(int li, int lj, int ri, int rj) { return query_h(li, lj, ri + 1, rj + 1, 0, H, 0); } }; class SegmentTree2DMin : public SegmentTree2D { using SegmentTree2D::SegmentTree2D; protected: // ??¨????????????????????????????????¢??° ll _func(ll a, ll b) { return min(a, b); } public: // ??¨??????????????????????????????????????????????????????????????? ll _getDefaultValue() { return INT_MAX; } }; void exec() { int tmp, r1, r2, c1, c2; SegmentTree2DMin st2 = SegmentTree2DMin(); for (int i = 0; i < MAX_N; i++) { dat[i] = st2._getDefaultValue(); } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { scanf("%d", &tmp); st2.set(i + H - 1, j + W - 1, tmp); } st2.init(); for (int i = 0; i < q; i++) { scanf("%d%d%d%d", &r1, &c1, &r2, &c2); printf("%d\n", st2.query(r1, c1, r2, c2)); } } void solve() { while (scanf("%d%d%d", &h, &w, &q) != EOF) { if (h + w + q <= 0) { break; } H = W = 1; while (H < h) H <<= 1; while (W < w) W <<= 1; exec(); } } int main() { solve(); return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <iostream> #include <map> #include <queue> #include <string> #include <vector> using namespace std; typedef int ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> ipair; typedef tuple<int, int, int> ituple; // const int MOD = (int)1e9 + 7; // const double EPS = 1e-10; #define PI acosl(-1) #define MAX_N 9000005 ll h, w, H, W, q; ll dat[MAX_N]; class SegmentTree2D { protected: // ??¨????????????????????????????????¢??° virtual ll _func(ll a, ll b) = 0; ll query_h(int li, int lj, int ri, int rj, int si, int ti, int k) { if (ri <= si or ti <= li) return _getDefaultValue(); if (li <= si and ti <= ri) return query_w(lj, rj, 0, W, k, 0); const int mi = (si + ti) / 2; return _func(query_h(li, lj, ri, rj, si, mi, 2 * k + 1), query_h(li, lj, ri, rj, mi, ti, 2 * k + 2)); } ll query_w(int lj, int rj, int sj, int tj, int i, int k) { if (rj <= sj or tj <= lj) return _getDefaultValue(); if (lj <= sj and tj <= rj) return get(i, k); const int mj = (sj + tj) / 2; return _func(query_w(lj, rj, sj, mj, i, 2 * k + 1), query_w(lj, rj, mj, tj, i, 2 * k + 2)); } public: // ??¨??????????????????????????????????????????????????????????????? virtual ll _getDefaultValue() = 0; SegmentTree2D() {} ll get(int hh, int ww) { // printf("get (%d, %d)[%lld] = %lld\n", hh, ww, hh*W+ww, dat[hh * W + ww]); return dat[hh * 2 * W + ww]; } void set(int hh, int ww, ll v) { // printf("set (%d, %d)[%lld] = %lld\n", hh, ww, hh*W+ww, v); dat[hh * 2 * W + ww] = v; } void init() { // H = W = 1; // while(H < (int)f.size()) H <<= 1; // while(W < (int)f[0].size()) W <<= 1; // dat.assign(2*H-1,VL(2*W-1, _getDefaultValue())); for (int i = 2 * H - 2; i > H - 2; i--) for (int j = W - 2; j >= 0; j--) { set(i, j, _func(get(i, 2 * j + 1), get(i, 2 * j + 2))); } for (int i = H - 2; i >= 0; i--) for (int j = 0; j < 2 * W - 1; j++) { set(i, j, _func(get(2 * i + 1, j), get(2 * i + 2, j))); } } // [(li, ri), (lj, rj)]??????????????¨???????±??????? ll query(int li, int lj, int ri, int rj) { return query_h(li, lj, ri + 1, rj + 1, 0, H, 0); } }; class SegmentTree2DMin : public SegmentTree2D { using SegmentTree2D::SegmentTree2D; protected: // ??¨????????????????????????????????¢??° ll _func(ll a, ll b) { return min(a, b); } public: // ??¨??????????????????????????????????????????????????????????????? ll _getDefaultValue() { return INT_MAX; } }; void exec() { int tmp, r1, r2, c1, c2; SegmentTree2DMin st2 = SegmentTree2DMin(); for (int i = 0; i < MAX_N; i++) { dat[i] = st2._getDefaultValue(); } for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { scanf("%d", &tmp); st2.set(i + H - 1, j + W - 1, tmp); } st2.init(); for (int i = 0; i < q; i++) { scanf("%d%d%d%d", &r1, &c1, &r2, &c2); printf("%d\n", st2.query(r1, c1, r2, c2)); } } void solve() { while (scanf("%d%d%d", &h, &w, &q) != EOF) { if (h + w + q <= 0) { break; } H = W = 1; while (H < h) H <<= 1; while (W < w) W <<= 1; exec(); } } int main() { solve(); return 0; }
replace
23
24
23
24
0
p00653
C++
Runtime Error
// AOJ 1068 #include <algorithm> #include <cstdio> #include <vector> #define rep(i, a) for (int i = 0; i != (a); ++i) #define repi(i, a, b) for (int i = (a); i != (b); ++i) #define all(a) (a).begin(), (a).end() const int INF = (1 << 31) - 1; int r, c, q; std::vector<std::vector<int>> segR, segC; void init(std::vector<std::vector<int>> &seg, int n) { int sz = 1; while (sz < c) sz <<= 1; rep(i, seg.size()) { seg[i].resize(2 * sz); std::fill(all(seg[i]), INF); } return; } void update(std::vector<int> &seg, int k, int x) { k += (seg.size() >> 1) - 1; seg[k] = x; while (k) { k = (k - 1) >> 1; seg[k] = std::min(seg[(k << 1) + 1], seg[(k << 1) + 2]); } return; } int query(const std::vector<int> &seg, int a, int b, int k, int l, int r) { if (r <= a || b <= l) return INF; else if (a <= l && r <= b) return seg[k]; return std::min(query(seg, a, b, (k << 1) + 1, l, (l + r) >> 1), query(seg, a, b, (k << 1) + 2, (l + r) >> 1, r)); } int main() { while (scanf("%d%d%d", &r, &c, &q), r | c | q) { segR.resize(r); segC.resize(c); init(segR, c); init(segC, r); rep(i, r) { rep(j, c) { int grid; scanf("%d", &grid); update(segR[i], j, grid); update(segC[j], i, grid); } } rep(i, q) { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int ans = INF; if (r2 - r1 <= c2 - c1) repi(j, r1, r2 + 1) ans = std::min( ans, query(segR[j], c1, c2 + 1, 0, 0, segR[j].size() >> 1)); else repi(j, c1, c2 + 1) ans = std::min( ans, query(segC[j], r1, r2 + 1, 0, 0, segC[j].size() >> 1)); printf("%d\n", ans); } } return 0; }
// AOJ 1068 #include <algorithm> #include <cstdio> #include <vector> #define rep(i, a) for (int i = 0; i != (a); ++i) #define repi(i, a, b) for (int i = (a); i != (b); ++i) #define all(a) (a).begin(), (a).end() const int INF = (1 << 31) - 1; int r, c, q; std::vector<std::vector<int>> segR, segC; void init(std::vector<std::vector<int>> &seg, int n) { int sz = 1; while (sz < n) sz <<= 1; rep(i, seg.size()) { seg[i].resize(2 * sz); std::fill(all(seg[i]), INF); } return; } void update(std::vector<int> &seg, int k, int x) { k += (seg.size() >> 1) - 1; seg[k] = x; while (k) { k = (k - 1) >> 1; seg[k] = std::min(seg[(k << 1) + 1], seg[(k << 1) + 2]); } return; } int query(const std::vector<int> &seg, int a, int b, int k, int l, int r) { if (r <= a || b <= l) return INF; else if (a <= l && r <= b) return seg[k]; return std::min(query(seg, a, b, (k << 1) + 1, l, (l + r) >> 1), query(seg, a, b, (k << 1) + 2, (l + r) >> 1, r)); } int main() { while (scanf("%d%d%d", &r, &c, &q), r | c | q) { segR.resize(r); segC.resize(c); init(segR, c); init(segC, r); rep(i, r) { rep(j, c) { int grid; scanf("%d", &grid); update(segR[i], j, grid); update(segC[j], i, grid); } } rep(i, q) { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); int ans = INF; if (r2 - r1 <= c2 - c1) repi(j, r1, r2 + 1) ans = std::min( ans, query(segR[j], c1, c2 + 1, 0, 0, segR[j].size() >> 1)); else repi(j, c1, c2 + 1) ans = std::min( ans, query(segC[j], r1, r2 + 1, 0, 0, segC[j].size() >> 1)); printf("%d\n", ans); } } return 0; }
replace
15
16
15
16
0
p00653
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF ((1LL << 31) - 1) #define D 6 struct SegTree { int segSize, realSegSize, nodeSize; vector<int> seg; void init(int n) { realSegSize = n; nodeSize = 0; for (segSize = 1; segSize < n; segSize *= D) nodeSize += segSize; seg = vector<int>(nodeSize + n, INF); } int get(int n) { if (n < (int)seg.size()) return seg[n]; else return INF; } void set(int n, int v) { if (n < (int)seg.size()) seg[n] = v; } void update(int pos, int val) { int n = nodeSize + pos; set(n, val); while (n) { n = (n - 1) / D; int r = INF; for (int i = 0; i < D; ++i) { r = min(r, seg[n * D + i + 1]); } set(n, r); } } int get(int n, int l, int r, int L, int R) { // cout << n << " " << l << " " << r << endl; if (L <= l && r <= R) return get(n); else if (R <= l || r <= L) return INF; else { int ret = INF; int prev = l; for (int i = 0; i < D; ++i) { int next = i == D - 1 ? r : prev + (r - l) / D; ret = min(ret, get(n * D + i + 1, prev, next, L, R)); prev = next; } return ret; } } int get(int L, int R) { return get(0, 0, segSize, L, R); } }; struct Query { int y1, x1, y2, x2; }; int main() { int H, W, Q; while (cin >> H >> W >> Q, H || W || Q) { vector<vector<int>> m(H, vector<int>(W, 0)); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> m[y][x]; } } bool isSegRow = H < W; vector<SegTree> seg; if (isSegRow) { seg.resize(H); for (int y = 0; y < H; ++y) { seg[y].init(W); } } else { seg.resize(W); for (int x = 0; x < W; ++x) { seg[x].init(H); } } for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { if (isSegRow) seg[y].update(x, m[y][x]); else seg[x].update(y, m[y][x]); } } for (int t = 0; t < Q; ++t) { int y1, x1, y2, x2; cin >> y1 >> x1 >> y2 >> x2; int ans = INF; if (isSegRow) { for (int y = y1; y <= y2; ++y) { ans = min(ans, seg[y].get(x1, x2 + 1)); } } else { for (int x = x1; x <= x2; ++x) { ans = min(ans, seg[x].get(y1, y2 + 1)); } } cout << ans << endl; } } }
#include <bits/stdc++.h> using namespace std; #define INF ((1LL << 31) - 1) #define D 16 struct SegTree { int segSize, realSegSize, nodeSize; vector<int> seg; void init(int n) { realSegSize = n; nodeSize = 0; for (segSize = 1; segSize < n; segSize *= D) nodeSize += segSize; seg = vector<int>(nodeSize + n, INF); } int get(int n) { if (n < (int)seg.size()) return seg[n]; else return INF; } void set(int n, int v) { if (n < (int)seg.size()) seg[n] = v; } void update(int pos, int val) { int n = nodeSize + pos; set(n, val); while (n) { n = (n - 1) / D; int r = INF; for (int i = 0; i < D; ++i) { r = min(r, seg[n * D + i + 1]); } set(n, r); } } int get(int n, int l, int r, int L, int R) { // cout << n << " " << l << " " << r << endl; if (L <= l && r <= R) return get(n); else if (R <= l || r <= L) return INF; else { int ret = INF; int prev = l; for (int i = 0; i < D; ++i) { int next = i == D - 1 ? r : prev + (r - l) / D; ret = min(ret, get(n * D + i + 1, prev, next, L, R)); prev = next; } return ret; } } int get(int L, int R) { return get(0, 0, segSize, L, R); } }; struct Query { int y1, x1, y2, x2; }; int main() { int H, W, Q; while (cin >> H >> W >> Q, H || W || Q) { vector<vector<int>> m(H, vector<int>(W, 0)); for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> m[y][x]; } } bool isSegRow = H < W; vector<SegTree> seg; if (isSegRow) { seg.resize(H); for (int y = 0; y < H; ++y) { seg[y].init(W); } } else { seg.resize(W); for (int x = 0; x < W; ++x) { seg[x].init(H); } } for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { if (isSegRow) seg[y].update(x, m[y][x]); else seg[x].update(y, m[y][x]); } } for (int t = 0; t < Q; ++t) { int y1, x1, y2, x2; cin >> y1 >> x1 >> y2 >> x2; int ans = INF; if (isSegRow) { for (int y = y1; y <= y2; ++y) { ans = min(ans, seg[y].get(x1, x2 + 1)); } } else { for (int x = x1; x <= x2; ++x) { ans = min(ans, seg[x].get(y1, y2 + 1)); } } cout << ans << endl; } } }
replace
5
6
5
6
MLE
p00653
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> using namespace std; #define dump(n) cerr << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; struct SegTree { vi data; int size; SegTree(int _size) { for (int i = 1;; i <<= 1) if (i >= _size) { size = i; break; } data.assign(2 * size, INT_MAX); } void Update(int i, int n) { data[size + i] = n; for (int j = size + i; j; j >>= 1) data[j] = min(data[j], n); } int Query(int a, int b, int i, int l, int r) { if (b <= l || r <= a) return INT_MAX; if (a <= l && r <= b) return data[i]; int res = INT_MAX, m = (l + r) / 2; if (a < m) res = min(res, Query(a, b, i * 2 + 0, l, m)); if (m < b) res = min(res, Query(a, b, i * 2 + 1, m, r)); return res; } int Query(int a, int b) { return Query(a, b, 1, 0, size); } }; int main() { for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) { bool flg = r > c; // グリッドが縦長 if (r > c) swap(r, c); vvi grid(r, vi(c)); rep(i, r) rep(j, c) scanf("%d", &(flg ? grid[j][i] : grid[i][j])); vi r1(q), c1(q), r2(q), c2(q); rep(i, q) scanf("%d%d%d%d", &r1[i], &c1[i], &r2[i], &c2[i]); if (flg) { swap(r1, c1); swap(r2, c2); } vector<SegTree> st(r, SegTree(c)); rep(i, r) rep(j, c) st[i].Update(j, grid[i][j]); rep(i, q) { int mn = INT_MAX; repi(j, r1[i], r2[i] + 1) mn = min(mn, st[j].Query(c1[i], c2[i] + 1)); printf("%d\n", mn); } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> using namespace std; #define dump(n) cerr << "# " << #n << "=" << (n) << endl #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define peri(i, a, b) for (int i = int(b); i-- > int(a);) #define rep(i, n) repi(i, 0, n) #define per(i, n) peri(i, 0, n) #define iter(c) __typeof__((c).begin()) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define all(c) (c).begin(), (c).end() #define mp make_pair typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; typedef pair<int, int> pii; struct SegTree { vi data; int size; SegTree(int _size) { for (int i = 1;; i <<= 1) if (i >= _size) { size = i; break; } data.assign(2 * size, INT_MAX); } void Update(int i, int n) { data[size + i] = n; for (int j = size + i; j; j >>= 1) data[j] = min(data[j], n); } int Query(int a, int b, int i, int l, int r) { if (b <= l || r <= a) return INT_MAX; if (a <= l && r <= b) return data[i]; int res = INT_MAX, m = (l + r) / 2; if (a < m) res = min(res, Query(a, b, i * 2 + 0, l, m)); if (m < b) res = min(res, Query(a, b, i * 2 + 1, m, r)); return res; } int Query(int a, int b) { return Query(a, b, 1, 0, size); } }; int main() { for (int r, c, q; scanf("%d%d%d", &r, &c, &q), r | c | q;) { bool flg = r > c; // グリッドが縦長 if (r > c) swap(r, c); vvi grid(r, vi(c)); if (flg) rep(j, c) rep(i, r) scanf("%d", &grid[i][j]); else rep(i, r) rep(j, c) scanf("%d", &grid[i][j]); vi r1(q), c1(q), r2(q), c2(q); rep(i, q) scanf("%d%d%d%d", &r1[i], &c1[i], &r2[i], &c2[i]); if (flg) { swap(r1, c1); swap(r2, c2); } vector<SegTree> st(r, SegTree(c)); rep(i, r) rep(j, c) st[i].Update(j, grid[i][j]); rep(i, q) { int mn = INT_MAX; repi(j, r1[i], r2[i] + 1) mn = min(mn, st[j].Query(c1[i], c2[i] + 1)); printf("%d\n", mn); } } return 0; }
replace
81
82
81
85
0
p00653
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = (1LL << 31) - 1; struct segtree { int H, W, H_, W_; int dat[4000000]; segtree() {} segtree(vector<vector<int>> &f) { init(f); } int x(int i, int j) { return i * W_ + j; } void init(vector<vector<int>> &f) { H = W = 1; while (H < (int)f.size()) H <<= 1; while (W < (int)f[0].size()) W <<= 1; H_ = 2 * H - 1, W_ = 2 * W - 1; for (int i = 0; i < (int)f.size(); i++) for (int j = 0; j < (int)f[0].size(); j++) dat[x(i + H - 1, j + W - 1)] = f[i][j]; for (int i = 2 * H - 2; i > H - 2; i--) for (int j = W - 2; j >= 0; j--) dat[x(i, j)] = min(dat[x(i, 2 * j + 1)], dat[x(i, 2 * j + 2)]); for (int i = H - 2; i >= 0; i--) for (int j = 0; j < 2 * W - 1; j++) dat[x(i, j)] = min(dat[x(2 * i + 1, j)], dat[x(2 * i + 2, j)]); } int query(int li, int lj, int ri, int rj) { return query_h(li, lj, ri, rj, 0, H, 0); } int query_h(int li, int lj, int ri, int rj, int si, int ti, int k) { if (ri <= si || ti <= li) return INF; if (li <= si && ti <= ri) return query_w(lj, rj, 0, W, k, 0); const int mi = (si + ti) / 2; return min(query_h(li, lj, ri, rj, si, mi, 2 * k + 1), query_h(li, lj, ri, rj, mi, ti, 2 * k + 2)); } int query_w(int lj, int rj, int sj, int tj, int i, int k) { if (rj <= sj || tj <= lj) return INF; if (lj <= sj && tj <= rj) return dat[x(i, k)]; const int mj = (sj + tj) / 2; return min(query_w(lj, rj, sj, mj, i, 2 * k + 1), query_w(lj, rj, mj, tj, i, 2 * k + 2)); } }; vector<vector<int>> f; segtree st; int main() { cin.tie(0); ios::sync_with_stdio(false); int r, c, q; while (cin >> r >> c >> q, r | c | q) { f.assign(r, vector<int>(c, 0)); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> f[i][j]; } } st.init(f); while (q--) { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; cout << st.query(r1, c1, r2 + 1, c2 + 1) << endl; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = (1LL << 31) - 1; struct segtree { int H, W, H_, W_; int dat[9000000]; segtree() {} segtree(vector<vector<int>> &f) { init(f); } int x(int i, int j) { return i * W_ + j; } void init(vector<vector<int>> &f) { H = W = 1; while (H < (int)f.size()) H <<= 1; while (W < (int)f[0].size()) W <<= 1; H_ = 2 * H - 1, W_ = 2 * W - 1; for (int i = 0; i < (int)f.size(); i++) for (int j = 0; j < (int)f[0].size(); j++) dat[x(i + H - 1, j + W - 1)] = f[i][j]; for (int i = 2 * H - 2; i > H - 2; i--) for (int j = W - 2; j >= 0; j--) dat[x(i, j)] = min(dat[x(i, 2 * j + 1)], dat[x(i, 2 * j + 2)]); for (int i = H - 2; i >= 0; i--) for (int j = 0; j < 2 * W - 1; j++) dat[x(i, j)] = min(dat[x(2 * i + 1, j)], dat[x(2 * i + 2, j)]); } int query(int li, int lj, int ri, int rj) { return query_h(li, lj, ri, rj, 0, H, 0); } int query_h(int li, int lj, int ri, int rj, int si, int ti, int k) { if (ri <= si || ti <= li) return INF; if (li <= si && ti <= ri) return query_w(lj, rj, 0, W, k, 0); const int mi = (si + ti) / 2; return min(query_h(li, lj, ri, rj, si, mi, 2 * k + 1), query_h(li, lj, ri, rj, mi, ti, 2 * k + 2)); } int query_w(int lj, int rj, int sj, int tj, int i, int k) { if (rj <= sj || tj <= lj) return INF; if (lj <= sj && tj <= rj) return dat[x(i, k)]; const int mj = (sj + tj) / 2; return min(query_w(lj, rj, sj, mj, i, 2 * k + 1), query_w(lj, rj, mj, tj, i, 2 * k + 2)); } }; vector<vector<int>> f; segtree st; int main() { cin.tie(0); ios::sync_with_stdio(false); int r, c, q; while (cin >> r >> c >> q, r | c | q) { f.assign(r, vector<int>(c, 0)); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> f[i][j]; } } st.init(f); while (q--) { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; cout << st.query(r1, c1, r2 + 1, c2 + 1) << endl; } } }
replace
8
9
8
9
0
p00653
C++
Runtime Error
#include <algorithm> #include <climits> #include <cstdio> #include <iostream> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int rc[1000 * 1000]; int w, h, q; int mm; bool sw; int buff[1024 * 1024]; void gen() { const int n = w * h; int m = 1; while (m < n) m *= 2; mm = m; REP(i, m + m) buff[i] = INT_MAX; REP(i, n) buff[i + m - 1] = rc[i]; while (m - 1 >= 0) { m /= 2; /* for(int i = 1; i <= mm; i *= 2){ for(int j = i - 1; j < i - 1 + i; j++){ printf("%d ", buff[j]); } puts(""); } */ for (int i = m - 1; i < m - 1 + m; i++) { buff[i] = min(buff[i * 2 + 1], buff[i * 2 + 2]); } } } int iter(int l, int r, int pos, int width) { // printf("iter: %d %d %d %d\n", l, r, pos, width); int w2 = width / 2; if (l + width == r) return buff[pos]; if (w2 <= l) return iter(l - w2, r - w2, pos * 2 + 2, w2); if (w2 >= r) return iter(l, r, pos * 2 + 1, w2); return min(iter(l, w2, pos * 2 + 1, w2), iter(0, r - w2, pos * 2 + 2, w2)); } int query(int l, int r) { return iter(l, r, 0, mm); } int main() { while (scanf("%d%d%d", &h, &w, &q), w + h + q) { sw = h > w; REP(i, h) REP(j, w) { if (!sw) { scanf("%d", rc + i * w + j); } else { scanf("%d", rc + j * h + i); } } if (sw) swap(h, w); gen(); REP(qq, q) { int h1, h2, w1, w2; int ans = INT_MAX; scanf("%d%d%d%d", &h1, &w1, &h2, &w2); if (sw) { swap(h1, w1); swap(h2, w2); } for (int hh = h1; hh < h2 + 1; hh++) { int p1 = hh * w + w1; int p2 = hh * w + w2; // REP(i, h*w) printf("%d ", rc[i]); puts(""); // printf("query(%d, %d) = %d\n", p1, p2 + 1, query(p1, p2 + 1)); ans = min(ans, query(p1, p2 + 1)); } cout << ans << endl; } } return 0; }
#include <algorithm> #include <climits> #include <cstdio> #include <iostream> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; int rc[1000 * 1000]; int w, h, q; int mm; bool sw; int buff[2 * 1024 * 1024 + 10]; void gen() { const int n = w * h; int m = 1; while (m < n) m *= 2; mm = m; REP(i, m + m) buff[i] = INT_MAX; REP(i, n) buff[i + m - 1] = rc[i]; while (m - 1 >= 0) { m /= 2; /* for(int i = 1; i <= mm; i *= 2){ for(int j = i - 1; j < i - 1 + i; j++){ printf("%d ", buff[j]); } puts(""); } */ for (int i = m - 1; i < m - 1 + m; i++) { buff[i] = min(buff[i * 2 + 1], buff[i * 2 + 2]); } } } int iter(int l, int r, int pos, int width) { // printf("iter: %d %d %d %d\n", l, r, pos, width); int w2 = width / 2; if (l + width == r) return buff[pos]; if (w2 <= l) return iter(l - w2, r - w2, pos * 2 + 2, w2); if (w2 >= r) return iter(l, r, pos * 2 + 1, w2); return min(iter(l, w2, pos * 2 + 1, w2), iter(0, r - w2, pos * 2 + 2, w2)); } int query(int l, int r) { return iter(l, r, 0, mm); } int main() { while (scanf("%d%d%d", &h, &w, &q), w + h + q) { sw = h > w; REP(i, h) REP(j, w) { if (!sw) { scanf("%d", rc + i * w + j); } else { scanf("%d", rc + j * h + i); } } if (sw) swap(h, w); gen(); REP(qq, q) { int h1, h2, w1, w2; int ans = INT_MAX; scanf("%d%d%d%d", &h1, &w1, &h2, &w2); if (sw) { swap(h1, w1); swap(h2, w2); } for (int hh = h1; hh < h2 + 1; hh++) { int p1 = hh * w + w1; int p2 = hh * w + w2; // REP(i, h*w) printf("%d ", rc[i]); puts(""); // printf("query(%d, %d) = %d\n", p1, p2 + 1, query(p1, p2 + 1)); ans = min(ans, query(p1, p2 + 1)); } cout << ans << endl; } } return 0; }
replace
14
15
14
15
0
p00653
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1 << 24; const int INF = 1 << 28; int r, c; int w, h; int seg[8 * MAX_N]; void init() { for (w = 1; w < r; w <<= 1) ; for (h = 1; h < c; h <<= 1) ; fill_n(seg, 8 * MAX_N, INF); } void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (x < ax || bx <= x) return; if (y < ay || by <= y) return; if (bx - ax == 1 && by - ay == 1) { seg[k] = v; return; } if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; if (nxt == 0) { int m = (ax + bx) / 2; update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1); update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]); // printf("seg[%d] = %d\n", k, seg[k]); // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); } int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (rx <= ax || bx <= lx) return INF; if (ry <= ay || by <= ly) return INF; if (lx <= ax && bx <= rx && ly <= ay && by <= ry) return seg[k]; // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; int vl, vr; if (nxt == 0) { int m = (ax + bx) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } return min(vl, vr); } int main() { int q; while (scanf("%d %d %d", &r, &c, &q), r | c | q) { init(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int v; scanf("%d", &v); update(i, j, v); } } while (q--) { int r1, c1, r2, c2; scanf("%d %d %d %d", &r1, &c1, &r2, &c2); printf("%d\n", getMin(r1, c1, ++r2, ++c2)); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1 << 20; const int INF = INT_MAX; int r, c; int w, h; int seg[8 * MAX_N]; void init() { for (w = 1; w < r; w <<= 1) ; for (h = 1; h < c; h <<= 1) ; fill_n(seg, 8 * MAX_N, INF); } void update(int x, int y, int v, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (x < ax || bx <= x) return; if (y < ay || by <= y) return; if (bx - ax == 1 && by - ay == 1) { seg[k] = v; return; } if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; if (nxt == 0) { int m = (ax + bx) / 2; update(x, y, v, k * 2 + 1, ax, ay, m, by, nxt ^ 1); update(x, y, v, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; update(x, y, v, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); update(x, y, v, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } seg[k] = min(seg[k * 2 + 1], seg[k * 2 + 2]); // printf("seg[%d] = %d\n", k, seg[k]); // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); } int getMin(int lx, int ly, int rx, int ry, int k = 0, int ax = 0, int ay = 0, int bx = w, int by = h, int nxt = 0) { if (rx <= ax || bx <= lx) return INF; if (ry <= ay || by <= ly) return INF; if (lx <= ax && bx <= rx && ly <= ay && by <= ry) return seg[k]; // printf("%d %d %d %d %d\n", k, ax, ay, bx, by); if (nxt == 0 && bx - ax == 1) nxt = 1; else if (nxt == 1 && by - ay == 1) nxt = 0; int vl, vr; if (nxt == 0) { int m = (ax + bx) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, m, by, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, m, ay, bx, by, nxt ^ 1); } else if (nxt == 1) { int m = (ay + by) / 2; vl = getMin(lx, ly, rx, ry, k * 2 + 1, ax, ay, bx, m, nxt ^ 1); vr = getMin(lx, ly, rx, ry, k * 2 + 2, ax, m, bx, by, nxt ^ 1); } return min(vl, vr); } int main() { int q; while (scanf("%d %d %d", &r, &c, &q), r | c | q) { init(); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int v; scanf("%d", &v); update(i, j, v); } } while (q--) { int r1, c1, r2, c2; scanf("%d %d %d %d", &r1, &c1, &r2, &c2); printf("%d\n", getMin(r1, c1, ++r2, ++c2)); } } return 0; }
replace
2
4
2
4
MLE
p00654
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <vector> #define all(a) (a).begin(), (a).end() #define EPS 1e-8 using namespace std; typedef long long ll; int main() { ll n, b; vector<ll> odd, even; while (cin >> n, n) { odd.clear(); even.clear(); for (int i = 0; i < n * (n + 1) / 2; i++) { cin >> b; if (b & 1) odd.push_back(b); else even.push_back(b); } sort(all(odd)); sort(all(even)); ll tmp = __gcd(even[0], even[1]); for (ll i = 2; i < n; i++) tmp = __gcd(tmp, even[i]); ll t0 = even[0] / tmp, t1 = even[1] / tmp; ll hoge = odd[0] / (t0 * t1); ll sq; for (sq = 0; sq * sq != hoge; sq++) ; tmp /= sq; cout << tmp << endl; for (int i = 0; i < n - 1; i++) cout << even[i] / tmp << " "; cout << even[n - 1] / tmp << endl; } }
#include <algorithm> #include <cmath> #include <iostream> #include <vector> #define all(a) (a).begin(), (a).end() #define EPS 1e-8 using namespace std; typedef long long ll; int main() { ll n, b; vector<ll> odd, even; while (cin >> n, n) { odd.clear(); even.clear(); for (int i = 0; i < n * (n + 1) / 2; i++) { cin >> b; if (b & 1) odd.push_back(b); else even.push_back(b); } sort(all(odd)); sort(all(even)); ll tmp = __gcd(even[0], even[1]); for (ll i = 2; i < n; i++) tmp = __gcd(tmp, even[i]); ll t0 = even[0] / tmp, t1 = even[1] / tmp; ll hoge = odd[0] / (t0 * t1); ll sq = (ll)(sqrt(hoge) + EPS); // for(sq=0;sq*sq!=hoge;sq++); tmp /= sq; cout << tmp << endl; for (int i = 0; i < n - 1; i++) cout << even[i] / tmp << " "; cout << even[n - 1] / tmp << endl; } }
replace
35
38
35
37
TLE