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
p01269
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; typedef struct { int to; int D; int E; } Edge; class State { public: int now; int money; int damaged; State(int now, int money, int damaged) { this->now = now; this->money = money; this->damaged = damaged; } }; const int INF = 999999; vector<Edge> G[101]; int d[101][101]; int N, M, L; int toV(int n, int l) { return n * L + l; } int main() { while (cin >> N >> M >> L, N + M + L != 0) { queue<State> q; for (int i = 0; i < 101; i++) { G[i].clear(); for (int j = 0; j < 101; j++) { d[i][j] = INF; } } for (int i = 0; i < M; i++) { int A, B, D, E; cin >> A >> B >> D >> E; Edge e{B, D, E}; Edge e2{A, D, E}; G[A].push_back(e); G[B].push_back(e2); } q.push(State(1, L, 0)); while (!q.empty()) { State s = q.front(); q.pop(); if (s.damaged > d[s.now][s.money]) continue; d[s.now][s.money] = s.damaged; for (Edge e : G[s.now]) { q.push(State(e.to, s.money, s.damaged + e.E)); if (s.money - e.D >= 0) { q.push(State(e.to, s.money - e.D, s.damaged)); } } } int m = INF; for (int i = 0; i <= L; i++) { m = min(m, d[N][i]); } cout << m << endl; } return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; typedef struct { int to; int D; int E; } Edge; class State { public: int now; int money; int damaged; State(int now, int money, int damaged) { this->now = now; this->money = money; this->damaged = damaged; } }; const int INF = 999999; vector<Edge> G[101]; int d[101][101]; int N, M, L; int toV(int n, int l) { return n * L + l; } int main() { while (cin >> N >> M >> L, N + M + L != 0) { queue<State> q; for (int i = 0; i < 101; i++) { G[i].clear(); for (int j = 0; j < 101; j++) { d[i][j] = INF; } } for (int i = 0; i < M; i++) { int A, B, D, E; cin >> A >> B >> D >> E; Edge e{B, D, E}; Edge e2{A, D, E}; G[A].push_back(e); G[B].push_back(e2); } q.push(State(1, L, 0)); while (!q.empty()) { State s = q.front(); q.pop(); if (s.damaged >= d[s.now][s.money]) continue; d[s.now][s.money] = s.damaged; for (Edge e : G[s.now]) { q.push(State(e.to, s.money, s.damaged + e.E)); if (s.money - e.D >= 0) { q.push(State(e.to, s.money - e.D, s.damaged)); } } } int m = INF; for (int i = 0; i <= L; i++) { m = min(m, d[N][i]); } cout << m << endl; } return 0; }
replace
57
58
57
58
TLE
p01269
C++
Time Limit Exceeded
#include <iostream> #include <map> #include <queue> #define LEN 100 #define BASE 25 using namespace std; int maps[LEN][LEN]; int off[LEN][LEN]; // int checker[4]; // int ncheck[4]; map<int, int> points[LEN]; class State { public: char where; int enemy; int budget; // int check[4]; State() {} // State() {check[0]=0;check[1]=0;check[2]=0;check[3]=0;} State(char w, int e, int b) { where = w; enemy = e; budget = b; /* check[0] = ncheck[0]; check[1] = ncheck[1]; check[2] = ncheck[2]; check[3] = ncheck[3]; */ } }; inline bool operator<(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget > b.budget; return a.enemy < b.enemy; } inline bool operator>(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget < b.budget; return a.enemy > b.enemy; } priority_queue<State, vector<State>, greater<State>> status; /* inline void copy ( State now ) { checker[0] = now.check[0]; checker[1] = now.check[1]; checker[2] = now.check[2]; checker[3] = now.check[3]; } inline void ncopy ( ) { ncheck[0] = checker[0]; ncheck[1] = checker[1]; ncheck[2] = checker[2]; ncheck[3] = checker[3]; } inline bool check ( int i ) { return 0 == ((1<<(i%25)) & checker[i/BASE]); } inline void add ( int i ) { ncheck[i/BASE] = ((1<<(i%25)) | checker[i/BASE]); } */ inline void insert(int i, int e, int b) { map<int, int>::iterator itr = points[i].find(e); if (itr == points[i].end()) { points[i][e] = b; status.push(State(i, e, b)); } else if ((*itr).second < b) { (*itr).second = b; status.push(State(i, e, b)); int min = b; while (itr != points[i].end()) { if ((*itr).second > min) { min = (*itr).second; } else if ((*itr).second <= min) { (*itr).second = min; } } } } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { maps[i][j] = -1; off[i][j] = 0; } points[i].clear(); } for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; maps[a - 1][b - 1] = maps[b - 1][a - 1] = d; off[a - 1][b - 1] = off[b - 1][a - 1] = e; } /* for ( int i=0; i<4; i++ ) { ncheck[i] = 0; checker[i] = 0; } */ priority_queue<State, vector<State>, greater<State>> empty; status = empty; // ncheck[0] = 1; status.push(State(0, 0, l)); int count = 0; while (!status.empty()) { State now = status.top(); status.pop(); if (now.where == n - 1) { cout << now.enemy << endl; break; } // copy(now); for (int i = 0; i < n; i++) { if (maps[now.where][i] > 0) { /* if ( check( i ) ) { ncopy(); add(i); */ int e, b; if (off[now.where][i] == 0) { e = now.enemy; b = now.budget; insert(i, e, b); } else { if (now.budget - maps[now.where][i] >= 0) { e = now.enemy; b = now.budget - maps[now.where][i]; insert(i, e, b); } e = now.enemy + off[now.where][i]; b = now.budget; insert(i, e, b); } // } } } } } }
#include <iostream> #include <map> #include <queue> #define LEN 100 #define BASE 25 using namespace std; int maps[LEN][LEN]; int off[LEN][LEN]; // int checker[4]; // int ncheck[4]; map<int, int> points[LEN]; class State { public: char where; int enemy; int budget; // int check[4]; State() {} // State() {check[0]=0;check[1]=0;check[2]=0;check[3]=0;} State(char w, int e, int b) { where = w; enemy = e; budget = b; /* check[0] = ncheck[0]; check[1] = ncheck[1]; check[2] = ncheck[2]; check[3] = ncheck[3]; */ } }; inline bool operator<(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget > b.budget; return a.enemy < b.enemy; } inline bool operator>(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget < b.budget; return a.enemy > b.enemy; } priority_queue<State, vector<State>, greater<State>> status; /* inline void copy ( State now ) { checker[0] = now.check[0]; checker[1] = now.check[1]; checker[2] = now.check[2]; checker[3] = now.check[3]; } inline void ncopy ( ) { ncheck[0] = checker[0]; ncheck[1] = checker[1]; ncheck[2] = checker[2]; ncheck[3] = checker[3]; } inline bool check ( int i ) { return 0 == ((1<<(i%25)) & checker[i/BASE]); } inline void add ( int i ) { ncheck[i/BASE] = ((1<<(i%25)) | checker[i/BASE]); } */ inline void insert(int i, int e, int b) { map<int, int>::iterator itr = points[i].find(e); if (itr == points[i].end()) { points[i][e] = b; status.push(State(i, e, b)); } else if ((*itr).second < b) { (*itr).second = b; status.push(State(i, e, b)); int min = b; while (itr != points[i].end()) { if ((*itr).second > min) { min = (*itr).second; } else if ((*itr).second <= min) { (*itr).second = min; } itr++; } } } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { maps[i][j] = -1; off[i][j] = 0; } points[i].clear(); } for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; maps[a - 1][b - 1] = maps[b - 1][a - 1] = d; off[a - 1][b - 1] = off[b - 1][a - 1] = e; } /* for ( int i=0; i<4; i++ ) { ncheck[i] = 0; checker[i] = 0; } */ priority_queue<State, vector<State>, greater<State>> empty; status = empty; // ncheck[0] = 1; status.push(State(0, 0, l)); int count = 0; while (!status.empty()) { State now = status.top(); status.pop(); if (now.where == n - 1) { cout << now.enemy << endl; break; } // copy(now); for (int i = 0; i < n; i++) { if (maps[now.where][i] > 0) { /* if ( check( i ) ) { ncopy(); add(i); */ int e, b; if (off[now.where][i] == 0) { e = now.enemy; b = now.budget; insert(i, e, b); } else { if (now.budget - maps[now.where][i] >= 0) { e = now.enemy; b = now.budget - maps[now.where][i]; insert(i, e, b); } e = now.enemy + off[now.where][i]; b = now.budget; insert(i, e, b); } // } } } } } }
insert
89
89
89
90
TLE
p01269
C++
Memory Limit Exceeded
#include <iostream> #include <map> #include <queue> #define LEN 100 #define BASE 25 using namespace std; int maps[LEN][LEN]; int off[LEN][LEN]; int checker[4]; int ncheck[4]; map<int, int> points[LEN]; class State { public: char where; int enemy; int budget; int check[4]; State() { check[0] = 0; check[1] = 0; check[2] = 0; check[3] = 0; } State(char w, int e, int b) { where = w; enemy = e; budget = b; check[0] = ncheck[0]; check[1] = ncheck[1]; check[2] = ncheck[2]; check[3] = ncheck[3]; } }; inline bool operator<(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget > b.budget; return a.enemy < b.enemy; } inline bool operator>(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget < b.budget; return a.enemy > b.enemy; } priority_queue<State, deque<State>, greater<State>> status; inline void copy(State now) { checker[0] = now.check[0]; checker[1] = now.check[1]; checker[2] = now.check[2]; checker[3] = now.check[3]; } inline void ncopy() { ncheck[0] = checker[0]; ncheck[1] = checker[1]; ncheck[2] = checker[2]; ncheck[3] = checker[3]; } inline bool check(int i) { return 0 == ((1 << (i % 25)) & checker[i / BASE]); } inline void add(int i) { ncheck[i / BASE] = ((1 << (i % 25)) | checker[i / BASE]); } inline void insert(int i, int e, int b) { map<int, int>::iterator itr = points[i].find(e); if (itr == points[i].end()) { points[i][e] = b; status.push(State(i, e, b)); } else if ((*itr).second < b) { (*itr).second = b; status.push(State(i, e, b)); } int min = b + 1; while (itr != points[i].end()) { if ((*itr).second > min) { (*itr).second = min; } else { min = (*itr).second + 1; } itr++; } } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { maps[i][j] = -1; off[i][j] = 0; } points[i].clear(); } for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; maps[a - 1][b - 1] = maps[b - 1][a - 1] = d; off[a - 1][b - 1] = off[b - 1][a - 1] = e; } for (int i = 0; i < 4; i++) { ncheck[i] = 0; checker[i] = 0; } priority_queue<State, deque<State>, greater<State>> empty; status = empty; ncheck[0] = 1; status.push(State(0, 0, l)); int count = 0; while (!status.empty()) { State now = status.top(); status.pop(); if (now.where == n - 1) { cout << now.enemy << endl; break; } copy(now); for (int i = 0; i < n; i++) { if (maps[now.where][i] > 0) { if (check(i)) { ncopy(); add(i); int e, b; if (off[now.where][i] == 0) { e = now.enemy; b = now.budget; insert(i, e, b); } else { if (now.budget - maps[now.where][i] >= 0) { e = now.enemy; b = now.budget - maps[now.where][i]; insert(i, e, b); } e = now.enemy + off[now.where][i]; b = now.budget; insert(i, e, b); } } } } } } }
#include <iostream> #include <map> #include <queue> #define LEN 100 #define BASE 25 using namespace std; int maps[LEN][LEN]; int off[LEN][LEN]; int checker[4]; int ncheck[4]; map<int, int> points[LEN]; class State { public: char where; int enemy; int budget; int check[4]; State() { check[0] = 0; check[1] = 0; check[2] = 0; check[3] = 0; } State(char w, int e, int b) { where = w; enemy = e; budget = b; check[0] = ncheck[0]; check[1] = ncheck[1]; check[2] = ncheck[2]; check[3] = ncheck[3]; } }; inline bool operator<(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget > b.budget; return a.enemy < b.enemy; } inline bool operator>(const State &a, const State &b) { if (a.enemy == b.enemy) return a.budget < b.budget; return a.enemy > b.enemy; } priority_queue<State, deque<State>, greater<State>> status; inline void copy(State now) { checker[0] = now.check[0]; checker[1] = now.check[1]; checker[2] = now.check[2]; checker[3] = now.check[3]; } inline void ncopy() { ncheck[0] = checker[0]; ncheck[1] = checker[1]; ncheck[2] = checker[2]; ncheck[3] = checker[3]; } inline bool check(int i) { return 0 == ((1 << (i % 25)) & checker[i / BASE]); } inline void add(int i) { ncheck[i / BASE] = ((1 << (i % 25)) | checker[i / BASE]); } inline void insert(int i, int e, int b) { map<int, int>::iterator itr = points[i].find(e); if (itr == points[i].end()) { points[i][e] = b; status.push(State(i, e, b)); } else if ((*itr).second < b) { (*itr).second = b; status.push(State(i, e, b)); } itr++; int min = b + 1; while (itr != points[i].end()) { if ((*itr).second > min) { (*itr).second = min; } else { min = (*itr).second + 1; } itr++; } } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { maps[i][j] = -1; off[i][j] = 0; } points[i].clear(); } for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; maps[a - 1][b - 1] = maps[b - 1][a - 1] = d; off[a - 1][b - 1] = off[b - 1][a - 1] = e; } for (int i = 0; i < 4; i++) { ncheck[i] = 0; checker[i] = 0; } priority_queue<State, deque<State>, greater<State>> empty; status = empty; ncheck[0] = 1; status.push(State(0, 0, l)); int count = 0; while (!status.empty()) { State now = status.top(); status.pop(); if (now.where == n - 1) { cout << now.enemy << endl; break; } copy(now); for (int i = 0; i < n; i++) { if (maps[now.where][i] > 0) { if (check(i)) { ncopy(); add(i); int e, b; if (off[now.where][i] == 0) { e = now.enemy; b = now.budget; insert(i, e, b); } else { if (now.budget - maps[now.where][i] >= 0) { e = now.enemy; b = now.budget - maps[now.where][i]; insert(i, e, b); } e = now.enemy + off[now.where][i]; b = now.budget; insert(i, e, b); } } } } } } }
insert
84
84
84
85
MLE
p01269
C++
Memory Limit Exceeded
#include <iostream> #include <map> #include <queue> #include <vector> #define fr first #define sc second #define INF (1 << 25) using namespace std; typedef pair<int, int> F; typedef pair<F, int> P; struct edge { int to, dis, enemy; edge() {} edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {} }; int n, m, l, costed[128][128]; vector<vector<edge>> info(128); void init() { for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) costed[i][j] = INF; } info.resize(0); info.resize(128); } void add_info() { int a, b, c, d; cin >> a >> b >> c >> d; info[a].push_back(edge(b, c, d)); info[b].push_back(edge(a, c, d)); } int Dijkstra() { priority_queue<P, vector<P>, greater<P>> que; que.push(P(F(0, 1), l)); costed[1][l] = 0; while (!que.empty()) { P p = que.top(); que.pop(); int now = p.fr.sc, en = p.fr.fr, mo = p.sc; if (now == n) return en; for (int i = 0; i < info[now].size(); i++) { edge e = info[now][i]; if (e.enemy + en < costed[e.to][mo]) { que.push(P(F(e.enemy + en, e.to), mo)); costed[e.to][mo] = e.enemy + en; } if (e.enemy == 0) { if (en < costed[e.to][mo]) { que.push(P(F(en, e.to), mo)); costed[e.to][mo] = en; } } if (e.dis <= mo) { if (en < costed[e.to][mo - e.dis]) { que.push(P(F(en, e.to), mo - e.dis)); costed[e.to][mo] = en; } } } } } int main() { while (cin >> n >> m >> l, n || m || l) { init(); for (int i = 0; i < m; i++) add_info(); cout << Dijkstra() << endl; } }
#include <iostream> #include <map> #include <queue> #include <vector> #define fr first #define sc second #define INF (1 << 25) using namespace std; typedef pair<int, int> F; typedef pair<F, int> P; struct edge { int to, dis, enemy; edge() {} edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {} }; int n, m, l, costed[128][128]; vector<vector<edge>> info(128); void init() { for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) costed[i][j] = INF; } info.resize(0); info.resize(128); } void add_info() { int a, b, c, d; cin >> a >> b >> c >> d; info[a].push_back(edge(b, c, d)); info[b].push_back(edge(a, c, d)); } int Dijkstra() { priority_queue<P, vector<P>, greater<P>> que; que.push(P(F(0, 1), l)); costed[1][l] = 0; while (!que.empty()) { P p = que.top(); que.pop(); int now = p.fr.sc, en = p.fr.fr, mo = p.sc; if (now == n) return en; for (int i = 0; i < info[now].size(); i++) { edge e = info[now][i]; if (e.enemy + en < costed[e.to][mo]) { que.push(P(F(e.enemy + en, e.to), mo)); costed[e.to][mo] = e.enemy + en; } if (e.enemy == 0) { if (en < costed[e.to][mo]) { que.push(P(F(en, e.to), mo)); costed[e.to][mo] = en; } } if (e.dis <= mo) { if (en < costed[e.to][mo - e.dis]) { que.push(P(F(en, e.to), mo - e.dis)); costed[e.to][mo - e.dis] = en; } } } } } int main() { while (cin >> n >> m >> l, n || m || l) { init(); for (int i = 0; i < m; i++) add_info(); cout << Dijkstra() << endl; } }
replace
61
62
61
62
MLE
p01269
C++
Runtime Error
#include <queue> #include <stdio.h> #include <string.h> struct S { int i, c, m; S(int i, int c, int m) : i(i), c(c), m(m) {} bool operator<(const S &r) const { return c > r.c; } }; #define C(d) memset(d, X, sizeof(d)) int main() { int N, M, L, A, B, D, E, X = 0x7f7f7f7f; int d[100][100], e[100][100], x[100][100], i, j; while (scanf("%d%d%d", &N, &M, &L), N) { C(d), C(e), C(x); while (M--) { scanf("%d%d%d%d", &A, &B, &D, &E); A--, B--; d[A][B] = d[B][A] = D; e[A][B] = e[B][A] = E; } std::priority_queue<S> q; q.push(S(0, 0, L)); for (;;) { S p(q.top()); q.pop(); if (x[p.i][p.m] <= p.c) continue; if (p.i == N - 1) { printf("%d\n", p.c); break; } x[p.i][p.m] = p.c; for (i = 0; i < N; ++i) { if (d[p.i][i] == X) continue; if (d[p.i][i] <= p.m) q.push(S(i, p.c, p.m - d[p.i][i])); q.push(S(i, p.c + e[p.i][i], p.m)); } } } return 0; }
#include <queue> #include <stdio.h> #include <string.h> struct S { int i, c, m; S(int i, int c, int m) : i(i), c(c), m(m) {} bool operator<(const S &r) const { return c > r.c; } }; #define C(d) memset(d, X, sizeof(d)) int main() { int N, M, L, A, B, D, E, X = 0x7f7f7f7f; int d[100][100], e[100][100], x[100][101], i; while (scanf("%d%d%d", &N, &M, &L), N) { C(d), C(e), C(x); while (M--) { scanf("%d%d%d%d", &A, &B, &D, &E); A--, B--; d[A][B] = d[B][A] = D; e[A][B] = e[B][A] = E; } std::priority_queue<S> q; q.push(S(0, 0, L)); for (;;) { S p(q.top()); q.pop(); if (x[p.i][p.m] <= p.c) continue; if (p.i == N - 1) { printf("%d\n", p.c); break; } x[p.i][p.m] = p.c; for (i = 0; i < N; ++i) { if (d[p.i][i] == X) continue; if (d[p.i][i] <= p.m) q.push(S(i, p.c, p.m - d[p.i][i])); q.push(S(i, p.c + e[p.i][i], p.m)); } } } return 0; }
replace
11
12
11
12
0
p01269
C++
Memory Limit Exceeded
#include <iostream> #include <map> #include <queue> #include <vector> #define fr first #define sc second #define INF (1 << 25) using namespace std; typedef pair<int, int> P; typedef pair<P, int> iP; struct edge { int to, dis, enemy; edge() {} edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {} }; int n, m, money; int costed[128][128]; vector<vector<edge>> info(128); void init() { for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) costed[i][j] = INF; } info.resize(0); info.resize(128); } void add_info() { int a, b, c, d; cin >> a >> b >> c >> d; info[a].push_back(edge(b, c, d)); info[b].push_back(edge(a, c, d)); } int Dijkstra() { priority_queue<iP, vector<iP>, greater<iP>> que; que.push(iP(P(0, 1), money)); costed[money][1] = 0; while (!que.empty()) { iP p = que.top(); que.pop(); int now = p.fr.sc, att = p.fr.fr, mo = p.sc; if (now == n) return att; for (int i = 0; i < info[now].size(); i++) { edge e = info[now][i]; if (e.enemy + att < costed[mo][e.to]) { que.push(iP(P(att + e.enemy, e.to), mo)); costed[mo][e.to] = e.enemy + att; } if (mo >= e.dis) { que.push(iP(P(att, e.to), mo - e.dis)); costed[mo - e.dis][e.to] = att; } } } } int main() { while (cin >> n >> m >> money, n || m || money) { init(); while (m--) add_info(); cout << Dijkstra() << endl; } }
#include <iostream> #include <map> #include <queue> #include <vector> #define fr first #define sc second #define INF (1 << 25) using namespace std; typedef pair<int, int> P; typedef pair<P, int> iP; struct edge { int to, dis, enemy; edge() {} edge(int to, int dis, int enemy) : to(to), dis(dis), enemy(enemy) {} }; int n, m, money; int costed[128][128]; vector<vector<edge>> info(128); void init() { for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) costed[i][j] = INF; } info.resize(0); info.resize(128); } void add_info() { int a, b, c, d; cin >> a >> b >> c >> d; info[a].push_back(edge(b, c, d)); info[b].push_back(edge(a, c, d)); } int Dijkstra() { priority_queue<iP, vector<iP>, greater<iP>> que; que.push(iP(P(0, 1), money)); costed[money][1] = 0; while (!que.empty()) { iP p = que.top(); que.pop(); int now = p.fr.sc, att = p.fr.fr, mo = p.sc; if (now == n) return att; for (int i = 0; i < info[now].size(); i++) { edge e = info[now][i]; if (e.enemy + att < costed[mo][e.to]) { que.push(iP(P(att + e.enemy, e.to), mo)); costed[mo][e.to] = e.enemy + att; } if (mo >= e.dis && att < costed[mo - e.dis][e.to]) { que.push(iP(P(att, e.to), mo - e.dis)); costed[mo - e.dis][e.to] = att; } } } } int main() { while (cin >> n >> m >> money, n || m || money) { init(); while (m--) add_info(); cout << Dijkstra() << endl; } }
replace
53
54
53
54
MLE
p01269
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define PB push_back #define F first #define S second #define mkp make_pair static const int INF = 1 << 24; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> piii; struct E { int v, l, thief; }; int N, M, L; vector<vector<E>> a; priority_queue<E> pq; int dp[110][110]; bool operator<(E a, E b) { return a.thief > b.thief; } int dij() { rep(i, 110) rep(j, 110) dp[i][j] = INF; E tmp; tmp.v = 0; tmp.l = 0; tmp.thief = 0; pq.push(tmp); dp[0][0] = 0; int ans = INF; while (!pq.empty()) { E now = pq.top(); // cout<<now.v<<" "<<now.l<<" "<<now.thief<<endl; pq.pop(); if (now.v == N - 1) { if (now.l <= L) ans = min(ans, now.thief); continue; } rep(i, a[now.v].size()) { E next = a[now.v][i]; // 雇う if (now.thief < dp[next.l + now.l][next.v]) { if (next.l + now.l > 105) continue; // if(next.l+now.l>110) cout<<"hoge "<<dp[next.l+now.l][next.v]<<" // "<<next.l+now.l<<endl; dp[next.l + now.l][next.v] = now.thief; // cout<<"dp "<<dp[next.l+now.l][next.v]<<" "<<next.l+now.l<<" // "<<next.v<<endl; E t1 = next; t1.l += now.l; t1.thief = now.thief; pq.push(t1); } // 雇わない if (now.thief + next.thief < dp[now.l][next.v]) { dp[now.l][next.v] = now.thief + next.thief; // cout<<"dp "<<dp[now.l][next.v]<<" "<<now.l<<" "<<next.v<<endl; E t1 = next; t1.l = now.l; t1.thief += now.thief; pq.push(t1); } } } return ans; } int main() { while (cin >> N >> M >> L, N || M || L) { a.resize(N); rep(i, N) a.clear(); rep(i, M) { int b, c, l, thi; cin >> b >> c >> l >> thi; b--; c--; E t1, t2; t1.v = b; t1.l = l; t1.thief = thi; t2.v = c; t2.l = l; t2.thief = thi; a[c].PB(t1); a[b].PB(t2); } cout << dij() << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define PB push_back #define F first #define S second #define mkp make_pair static const int INF = 1 << 24; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> piii; struct E { int v, l, thief; }; int N, M, L; vector<vector<E>> a; priority_queue<E> pq; int dp[110][110]; bool operator<(E a, E b) { return a.thief > b.thief; } int dij() { rep(i, 110) rep(j, 110) dp[i][j] = INF; E tmp; tmp.v = 0; tmp.l = 0; tmp.thief = 0; pq.push(tmp); dp[0][0] = 0; int ans = INF; while (!pq.empty()) { E now = pq.top(); // cout<<now.v<<" "<<now.l<<" "<<now.thief<<endl; pq.pop(); if (now.v == N - 1) { if (now.l <= L) ans = min(ans, now.thief); continue; } rep(i, a[now.v].size()) { E next = a[now.v][i]; // 雇う if (next.l + now.l <= 105) { if (now.thief < dp[next.l + now.l][next.v]) { // if(next.l+now.l>110) cout<<"hoge "<<dp[next.l+now.l][next.v]<<" // "<<next.l+now.l<<endl; dp[next.l + now.l][next.v] = now.thief; // cout<<"dp "<<dp[next.l+now.l][next.v]<<" "<<next.l+now.l<<" // "<<next.v<<endl; E t1 = next; t1.l += now.l; t1.thief = now.thief; pq.push(t1); } } // 雇わない if (now.thief + next.thief < dp[now.l][next.v]) { dp[now.l][next.v] = now.thief + next.thief; // cout<<"dp "<<dp[now.l][next.v]<<" "<<now.l<<" "<<next.v<<endl; E t1 = next; t1.l = now.l; t1.thief += now.thief; pq.push(t1); } } } return ans; } int main() { while (cin >> N >> M >> L, N || M || L) { a.resize(N); rep(i, N) a.clear(); rep(i, M) { int b, c, l, thi; cin >> b >> c >> l >> thi; b--; c--; E t1, t2; t1.v = b; t1.l = l; t1.thief = thi; t2.v = c; t2.l = l; t2.thief = thi; a[c].PB(t1); a[b].PB(t2); } cout << dij() << endl; } return 0; }
replace
50
62
50
62
-11
p01269
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define rep(i, n) for (int i = 0; i < (n); i++) #define tr(it, container) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); ++it) #define mp(a, b) make_pair((a), (b)) typedef long long ll; typedef complex<double> point; // up right down left const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; const double EPS = 1e-9; const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int INF = 500000000; struct Edge { int from, to, cost, danger; Edge(int from, int to, int cost, int danger) : from(from), to(to), cost(cost), danger(danger) {} }; int solve(vector<vector<Edge>> graph, int L) { int n = graph.size(); vector<vector<int>> dp(n, vector<int>(L + 1, INF)); dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < graph[i].size(); k++) { const Edge &edge = graph[j][k]; for (int l = 0; l <= L; l++) { if (dp[j][l] >= INF) continue; if (l + edge.cost <= L) { int new_cost = l + edge.cost; int to = edge.to; int here = dp[j][l]; dp[to][new_cost] = min(dp[to][new_cost], dp[j][l]); } dp[edge.to][l] = min(dp[edge.to][l], dp[j][l] + edge.danger); } } } } int ret = INF; for (int i = 0; i <= L; i++) { ret = min(ret, dp.back()[i]); } return ret; } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; vector<vector<Edge>> edges(n); for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; a--; b--; edges[a].push_back(Edge(a, b, d, e)); edges[b].push_back(Edge(b, a, d, e)); } cout << solve(edges, l) << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define rep(i, n) for (int i = 0; i < (n); i++) #define tr(it, container) \ for (typeof(container.begin()) it = container.begin(); \ it != container.end(); ++it) #define mp(a, b) make_pair((a), (b)) typedef long long ll; typedef complex<double> point; // up right down left const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; const double EPS = 1e-9; const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int INF = 500000000; struct Edge { int from, to, cost, danger; Edge(int from, int to, int cost, int danger) : from(from), to(to), cost(cost), danger(danger) {} }; int solve(vector<vector<Edge>> graph, int L) { int n = graph.size(); vector<vector<int>> dp(n, vector<int>(L + 1, INF)); dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < graph[j].size(); k++) { const Edge &edge = graph[j][k]; for (int l = 0; l <= L; l++) { if (dp[j][l] >= INF) continue; if (l + edge.cost <= L) { int new_cost = l + edge.cost; int to = edge.to; int here = dp[j][l]; dp[to][new_cost] = min(dp[to][new_cost], dp[j][l]); } dp[edge.to][l] = min(dp[edge.to][l], dp[j][l] + edge.danger); } } } } int ret = INF; for (int i = 0; i <= L; i++) { ret = min(ret, dp.back()[i]); } return ret; } int main() { while (true) { int n, m, l; cin >> n >> m >> l; if (n == 0) break; vector<vector<Edge>> edges(n); for (int i = 0; i < m; i++) { int a, b, d, e; cin >> a >> b >> d >> e; a--; b--; edges[a].push_back(Edge(a, b, d, e)); edges[b].push_back(Edge(b, a, d, e)); } cout << solve(edges, l) << endl; } return 0; }
replace
53
54
53
54
0
p01269
C++
Runtime Error
#include <iostream> #include <queue> #include <vector> using namespace std; int num[101][101]; int dist[101][101]; #define INF 100000000 class state { public: int now, money, cost; state(int now, int money, int cost) : now(now), cost(cost), money(money) {} bool operator<(const state &a) { return cost > a.cost; } }; bool operator<(state a, state b) { return a.cost > b.cost; } int main() { int n, m, l; while (cin >> n >> m >> l && n != 0) { int C[101][101]; for (int i = 0; i < 101; i++) for (int j = 0; j < 101; j++) num[i][j] = dist[i][j] = C[i][j] = INF; for (int i = 0; i < m; i++) { int in, out, d, e; cin >> in >> out >> d >> e; num[in][out] = num[out][in] = e; dist[in][out] = dist[out][in] = d; } priority_queue<state> Q; Q.push(state(1, l, 0)); while (!Q.empty()) { state t = Q.top(); Q.pop(); if (C[t.now][t.money] <= t.cost) continue; if (t.now == n) { cout << t.cost << endl; break; } for (int i = 1; i <= n; i++) { if (dist[t.now][i] < INF) { if (t.money - dist[t.now][i] >= 0) Q.push(state(i, t.money - dist[t.now][i], t.cost)); Q.push(state(i, t.money, t.cost + num[t.now][i])); } } } } return 0; }
#include <iostream> #include <queue> #include <vector> using namespace std; int num[101][101]; int dist[101][101]; #define INF 100000000 class state { public: int now, money, cost; state(int now, int money, int cost) : now(now), cost(cost), money(money) {} bool operator<(const state &a) { return cost > a.cost; } }; bool operator<(state a, state b) { return a.cost > b.cost; } int main() { int n, m, l; while (cin >> n >> m >> l && n != 0) { int C[101][101]; for (int i = 0; i < 101; i++) for (int j = 0; j < 101; j++) num[i][j] = dist[i][j] = C[i][j] = INF; for (int i = 0; i < m; i++) { int in, out, d, e; cin >> in >> out >> d >> e; num[in][out] = num[out][in] = e; dist[in][out] = dist[out][in] = d; } priority_queue<state> Q; Q.push(state(1, l, 0)); while (!Q.empty()) { state t = Q.top(); Q.pop(); if (C[t.now][t.money] <= t.cost) continue; C[t.now][t.money] = t.cost; if (t.now == n) { cout << t.cost << endl; break; } for (int i = 1; i <= n; i++) { if (dist[t.now][i] < INF) { if (t.money - dist[t.now][i] >= 0) Q.push(state(i, t.money - dist[t.now][i], t.cost)); Q.push(state(i, t.money, t.cost + num[t.now][i])); } } } } return 0; }
insert
41
41
41
42
0
p01270
C++
Time Limit Exceeded
/* */ #include <algorithm> #include <iostream> #include <list> using namespace std; typedef pair<int, int> P; // init // input int iN; char cC; // 0 <= N <= 100,000 int iI; // 1 <= P <= 100 int iS; // 1 <= P <= 100 int iP; // 1 <= P <= 100 // manage // ‰Šú‰» void init() {} // “ü—Í bool input() { scanf("%d", &iN); if (iN == 0) return false; return true; } // ˆ— void manage() { int i; list<P> lis; list<P>::iterator it; // ƒCƒeƒŒ[ƒ^ P p; for (i = 0; i < iN; i++) { it = lis.begin(); while (it != lis.end()) { // list‚Ì––”ö‚܂Šprintf("(%d, %d) ", it->first, it->second); // —v‘f‚ðo—Í ++it; // ƒCƒeƒŒ[ƒ^‚ð‚P‚i‚ß‚é } printf("\n"); // —v‘f‚ðo—Í scanf(" %c", &cC); if (cC == 'W') { scanf("%d %d", &iI, &iS); it = lis.begin(); while (it != lis.end()) { // list‚Ì––”ö‚܂Šif (it->first == -1) { it->first = iI; if (iS >= it->second) { iS -= it->second; } else { int d = it->second - iS; it->second = iS; iS = 0; it++; lis.insert(it, P(-1, d)); } if (iS == 0) break; } it++; } if (it == lis.end()) { lis.push_back(P(iI, iS)); } continue; } if (cC == 'D') { scanf("%d", &iI); it = lis.begin(); while (it != lis.end()) { // list‚Ì––”ö‚܂Šif (it->first == iI) { it->first = -1; } it++; } continue; } if (cC == 'R') { scanf("%d", &iP); it = lis.begin(); int sum = 0; while (it != lis.end()) { // list‚Ì––”ö‚܂Š// printf("(%d, %d) %d %d %d\n", it->first, it->second, sum + // it->second, iP, sum + it->second > iP); // —v‘f‚ðo—Í if (sum + it->second > iP) break; sum += it->second; it++; } if (it != lis.end()) { printf("%d\n", it->first); } else { printf("-1\n"); } continue; } } } // o—Í void output() { printf("\n"); } // mizo‘S–⋤’Ê int main() { init(); // ‰Šú‰» while (1) { if (!input()) break; // “ü—Í + I—¹”»’è manage(); // ˆ— output(); // o—Í } return 0; }
/* */ #include <algorithm> #include <iostream> #include <list> using namespace std; typedef pair<int, int> P; // init // input int iN; char cC; // 0 <= N <= 100,000 int iI; // 1 <= P <= 100 int iS; // 1 <= P <= 100 int iP; // 1 <= P <= 100 // manage // ‰Šú‰» void init() {} // “ü—Í bool input() { scanf("%d", &iN); if (iN == 0) return false; return true; } // ˆ— void manage() { int i; list<P> lis; list<P>::iterator it; // ƒCƒeƒŒ[ƒ^ P p; for (i = 0; i < iN; i++) { it = lis.begin(); /* while (it != lis.end()) { // list‚Ì––”ö‚܂Šprintf("(%d, %d) ", it->first, it->second); // —v‘f‚ðo—Í ++it; // ƒCƒeƒŒ[ƒ^‚ð‚P‚i‚ß‚é } printf("\n"); // —v‘f‚ðo—Í */ scanf(" %c", &cC); if (cC == 'W') { scanf("%d %d", &iI, &iS); it = lis.begin(); while (it != lis.end()) { // list‚Ì––”ö‚܂Šif (it->first == -1) { it->first = iI; if (iS >= it->second) { iS -= it->second; } else { int d = it->second - iS; it->second = iS; iS = 0; it++; lis.insert(it, P(-1, d)); } if (iS == 0) break; } it++; } if (it == lis.end()) { lis.push_back(P(iI, iS)); } continue; } if (cC == 'D') { scanf("%d", &iI); it = lis.begin(); while (it != lis.end()) { // list‚Ì––”ö‚܂Šif (it->first == iI) { it->first = -1; } it++; } continue; } if (cC == 'R') { scanf("%d", &iP); it = lis.begin(); int sum = 0; while (it != lis.end()) { // list‚Ì––”ö‚܂Š// printf("(%d, %d) %d %d %d\n", it->first, it->second, sum + // it->second, iP, sum + it->second > iP); // —v‘f‚ðo—Í if (sum + it->second > iP) break; sum += it->second; it++; } if (it != lis.end()) { printf("%d\n", it->first); } else { printf("-1\n"); } continue; } } } // o—Í void output() { printf("\n"); } // mizo‘S–⋤’Ê int main() { init(); // ‰Šú‰» while (1) { if (!input()) break; // “ü—Í + I—¹”»’è manage(); // ˆ— output(); // o—Í } return 0; }
replace
40
47
40
47
TLE
p01270
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #define rep(i, n) for (int i = 0; i < n; i++) #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define mp make_pair using namespace std; typedef map<pair<int, int>, int> S; int main() { int n, t1, t2; char c; while (cin >> n, n) { S s; rep(i, n) { cin >> c; if (c == 'W') { cin >> t1 >> t2; int p = 0, q; if (s.empty()) s.insert(mp(mp(0, t2), t1)), t2 = 0; else { foreach (j, s) { if (j->first.first > p) { q = min(p + t2, j->first.first); s.insert(mp(mp(p, q), t1)); t2 -= q - p; p = q; j--; } else p = j->first.second; } if (t2 > 0) s.insert(mp(mp(p, p + t2), t1)); } } else if (c == 'D') { cin >> t1; foreach (j, s) if (j->second == t1) s.erase(j); } else { cin >> t1; foreach (j, s) if (j->first.first <= t1 && t1 < j->first.second) { cout << j->second << endl; goto END; } cout << -1 << endl; END:; } } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #define rep(i, n) for (int i = 0; i < n; i++) #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define mp make_pair using namespace std; typedef map<pair<int, int>, int> S; int main() { int n, t1, t2; char c; while (cin >> n, n) { S s; rep(i, n) { cin >> c; if (c == 'W') { cin >> t1 >> t2; int p = 0, q; if (s.empty()) s.insert(mp(mp(0, t2), t1)), t2 = 0; else { foreach (j, s) { if (j->first.first > p) { q = min(p + t2, j->first.first); s.insert(mp(mp(p, q), t1)); t2 -= q - p; p = q; j--; } else p = j->first.second; if (t2 == 0) break; } if (t2 > 0) s.insert(mp(mp(p, p + t2), t1)); } } else if (c == 'D') { cin >> t1; foreach (j, s) if (j->second == t1) s.erase(j); } else { cin >> t1; foreach (j, s) if (j->first.first <= t1 && t1 < j->first.second) { cout << j->second << endl; goto END; } cout << -1 << endl; END:; } } cout << endl; } return 0; }
insert
30
30
30
32
TLE
p01270
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <cstdio> #include <iostream> #include <vector> using namespace std; const int LIMIT = 1000000000; struct Data { int id; int st; int num; Data(int id, int st, int num) : id(id), st(st), num(num) {} bool operator<(const Data &rhs) const { return st < rhs.st; } }; int N; vector<Data> storage; void dump() { cout << "-----------------------------" << endl; for (int i = 0; i < (int)storage.size(); i++) { cout << "storage[" << i << "]:" << endl; cout << "id: " << storage[i].id << " st: " << storage[i].st << " num: " << storage[i].num << endl; } cout << "-----------------------------" << endl; } void write(const int id, int remain) { int st = 0; while (remain > 0) { int num = -1; int into = -1; for (int i = 0; i < (int)storage.size(); i++) { if (st < storage[i].st) { num = storage[i].st - st; into = i; break; } else if (st < storage[i].st + storage[i].num) { st = st + storage[i].num; } if (st > LIMIT) return; } if (num == -1) { // insert data back num = remain; into = storage.size(); } storage.insert(storage.begin() + into, Data(id, st, num)); remain -= num; assert(remain >= 0); } } class EqualsID { private: int id; public: EqualsID(int id) : id(id) {} bool operator()(const Data &d) { return d.id == id; } }; void del(int id) { storage.erase(remove_if(storage.begin(), storage.end(), EqualsID(id)), storage.end()); } int refer(int sect) { for (int i = 0; i < (int)storage.size(); i++) { if (storage[i].st <= sect && sect < storage[i].st + storage[i].num) { return storage[i].id; } } return -1; } int main() { while (scanf("%d", &N) && N) { storage.clear(); for (int i = 0; i < N; i++) { char ch; cin >> ch; // scanf("%c", &ch); if (ch == 'W') { int id, num; scanf("%d%d", &id, &num); write(id, num); // dump(); } if (ch == 'D') { int id; scanf("%d", &id); del(id); } if (ch == 'R') { int sect; scanf("%d", &sect); printf("%d\n", refer(sect)); } } puts(""); } return 0; }
#include <algorithm> #include <assert.h> #include <cstdio> #include <iostream> #include <vector> using namespace std; const int LIMIT = 1000000000; struct Data { int id; int st; int num; Data(int id, int st, int num) : id(id), st(st), num(num) {} bool operator<(const Data &rhs) const { return st < rhs.st; } }; int N; vector<Data> storage; void dump() { cout << "-----------------------------" << endl; for (int i = 0; i < (int)storage.size(); i++) { cout << "storage[" << i << "]:" << endl; cout << "id: " << storage[i].id << " st: " << storage[i].st << " num: " << storage[i].num << endl; } cout << "-----------------------------" << endl; } void write(const int id, int remain) { int st = 0; while (remain > 0) { int num = -1; int into = -1; for (int i = 0; i < (int)storage.size(); i++) { if (st < storage[i].st) { num = min(remain, storage[i].st - st); into = i; break; } else if (st < storage[i].st + storage[i].num) { st = st + storage[i].num; } if (st > LIMIT) return; } if (num == -1) { // insert data back num = remain; into = storage.size(); } storage.insert(storage.begin() + into, Data(id, st, num)); remain -= num; assert(remain >= 0); } } class EqualsID { private: int id; public: EqualsID(int id) : id(id) {} bool operator()(const Data &d) { return d.id == id; } }; void del(int id) { storage.erase(remove_if(storage.begin(), storage.end(), EqualsID(id)), storage.end()); } int refer(int sect) { for (int i = 0; i < (int)storage.size(); i++) { if (storage[i].st <= sect && sect < storage[i].st + storage[i].num) { return storage[i].id; } } return -1; } int main() { while (scanf("%d", &N) && N) { storage.clear(); for (int i = 0; i < N; i++) { char ch; cin >> ch; // scanf("%c", &ch); if (ch == 'W') { int id, num; scanf("%d%d", &id, &num); write(id, num); // dump(); } if (ch == 'D') { int id; scanf("%d", &id); del(id); } if (ch == 'R') { int sect; scanf("%d", &sect); printf("%d\n", refer(sect)); } } puts(""); } return 0; }
replace
39
40
39
40
0
p01270
C++
Runtime Error
#include <iostream> #define REP(i, a, n) for (int i = (a); i < (n); i++) using namespace std; typedef long long ll; struct sector { ll l, r, f; }; ll N, I, S, P; char C; sector a[1000]; int k; void write() { cin >> I >> S; REP(i, 0, k) if (a[i].f == -1 && S > 0) { if (S >= a[i].r - a[i].l) { a[i].f = I; S -= a[i].r - a[i].l; } else { for (int j = k; j > i; j--) { a[j + 1].l = a[j].l; a[j + 1].r = a[j].r; a[j + 1].f = a[j].f; } a[i + 1].r = a[i].r; a[i + 1].f = -1; a[i].f = I; a[i].r = a[i].l + S; a[i + 1].l = a[i].r; S = 0; k++; break; } } if (S > 0) { int l = k > 0 ? a[k - 1].r : 0; a[k++] = (sector){l, l + S, I}; } } void destroy() { cin >> I; REP(i, 0, k) if (a[i].f == I) a[i].f = -1; } ll read() { cin >> P; REP(i, 0, k) if (a[i].l <= P && P < a[i].r) return a[i].f; return -1; } int main(void) { while (cin >> N, N) { k = 0; REP(i, 0, N) { cin >> C; if (C == 'W') write(); if (C == 'D') destroy(); if (C == 'R') cout << read() << endl; // cout << i << ": " << C << " " << k << endl; // REP(i, 0, k) { // cout << a[i].l << " - " << a[i].r << ": " << a[i].f << endl; // } // cout << endl; } cout << endl; } }
#include <iostream> #define REP(i, a, n) for (int i = (a); i < (n); i++) using namespace std; typedef long long ll; struct sector { ll l, r, f; }; ll N, I, S, P; char C; sector a[20000]; int k; void write() { cin >> I >> S; REP(i, 0, k) if (a[i].f == -1 && S > 0) { if (S >= a[i].r - a[i].l) { a[i].f = I; S -= a[i].r - a[i].l; } else { for (int j = k; j > i; j--) { a[j + 1].l = a[j].l; a[j + 1].r = a[j].r; a[j + 1].f = a[j].f; } a[i + 1].r = a[i].r; a[i + 1].f = -1; a[i].f = I; a[i].r = a[i].l + S; a[i + 1].l = a[i].r; S = 0; k++; break; } } if (S > 0) { int l = k > 0 ? a[k - 1].r : 0; a[k++] = (sector){l, l + S, I}; } } void destroy() { cin >> I; REP(i, 0, k) if (a[i].f == I) a[i].f = -1; } ll read() { cin >> P; REP(i, 0, k) if (a[i].l <= P && P < a[i].r) return a[i].f; return -1; } int main(void) { while (cin >> N, N) { k = 0; REP(i, 0, N) { cin >> C; if (C == 'W') write(); if (C == 'D') destroy(); if (C == 'R') cout << read() << endl; // cout << i << ": " << C << " " << k << endl; // REP(i, 0, k) { // cout << a[i].l << " - " << a[i].r << ": " << a[i].f << endl; // } // cout << endl; } cout << endl; } }
replace
11
12
11
12
0
p01271
C++
Memory Limit Exceeded
#include <iostream> #include <queue> #include <string> #include <tuple> using namespace std; #define MAX_N 70 #define INF 1 << 30 int dist[MAX_N][MAX_N][MAX_N][MAX_N]; int dy1[4] = {0, 1, 0, -1}; int dy2[4] = {0, 1, 0, -1}; int dx1[4] = {1, 0, -1, 0}; int dx2[4] = {-1, 0, 1, 0}; int H, W, map1[MAX_N][MAX_N], map2[MAX_N][MAX_N]; int Lsy, Lsx, Lgy, Lgx; int Rsy, Rsx, Rgy, Rgx; string S, T; queue<tuple<int, int, int, int>> Q; void _memset() { for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { for (int l = 0; l < MAX_N; l++) { dist[i][j][k][l] = INF; } } } } for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { map1[i][j] = 1; map2[i][j] = 1; } } } int main() { while (true) { // memset. _memset(); // cin. cin >> W >> H; if (H == 0 && W == 0) { break; } for (int i = 1; i <= H; i++) { cin >> S >> T; for (int j = 0; j < S.size(); j++) { if (S[j] == 'L') { Lsx = j + 1; Lsy = i; map1[i][j + 1] = 0; } if (S[j] == '#') { map1[i][j + 1] = 1; } if (S[j] == '.') { map1[i][j + 1] = 0; } if (S[j] == '%') { Lgy = i; Lgx = j + 1; map1[i][j + 1] = 0; } } for (int j = 0; j < T.size(); j++) { if (T[j] == 'R') { Rsx = j + 1; Rsy = i; map2[i][j + 1] = 0; } if (T[j] == '#') { map2[i][j + 1] = 1; } if (T[j] == '.') { map2[i][j + 1] = 0; } if (T[j] == '%') { Rgy = i; Rgx = j + 1; map2[i][j + 1] = 0; } } } // bfs. Q.push(make_tuple(Lsy, Lsx, Rsy, Rsx)); dist[Lsy][Lsx][Rsy][Rsx] = 0; while (!Q.empty()) { tuple<int, int, int, int> tup = Q.front(); Q.pop(); bool Q1, Q2; int ay = get<0>(tup); int ax = get<1>(tup); int by = get<2>(tup); int bx = get<3>(tup); for (int i = 0; i < 4; i++) { int ey = ay + dy1[i], ex = ax + dx1[i]; int fy = by + dy2[i], fx = bx + dx2[i]; if (map1[ey][ex] == 1) { ey = ay; ex = ax; } if (map2[fy][fx] == 1) { fy = by; fx = bx; } if (ey == Lgy && ex == Lgx) { Q1 = true; } else { Q1 = false; } if (fy == Rgy && fx == Rgx) { Q2 = true; } else { Q2 = false; } if (Q1 == true && Q2 == false) { goto G; } if (Q1 == false && Q2 == true) { goto G; } if (dist[ey][ex][fy][fx] == INF) { dist[ey][ex][fy][fx] = dist[ay][ax][by][bx] + 1; Q.push(make_tuple(ey, ex, fy, fx)); } G:; } } // hantei. if (dist[Lgy][Lgx][Rgy][Rgx] == INF) { cout << "No" << endl; } else { cout << "Yes" << endl; } } return 0; }
#include <iostream> #include <queue> #include <string> #include <tuple> using namespace std; #define MAX_N 63 #define INF 1 << 30 int dist[MAX_N][MAX_N][MAX_N][MAX_N]; int dy1[4] = {0, 1, 0, -1}; int dy2[4] = {0, 1, 0, -1}; int dx1[4] = {1, 0, -1, 0}; int dx2[4] = {-1, 0, 1, 0}; int H, W, map1[MAX_N][MAX_N], map2[MAX_N][MAX_N]; int Lsy, Lsx, Lgy, Lgx; int Rsy, Rsx, Rgy, Rgx; string S, T; queue<tuple<int, int, int, int>> Q; void _memset() { for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { for (int k = 0; k < MAX_N; k++) { for (int l = 0; l < MAX_N; l++) { dist[i][j][k][l] = INF; } } } } for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { map1[i][j] = 1; map2[i][j] = 1; } } } int main() { while (true) { // memset. _memset(); // cin. cin >> W >> H; if (H == 0 && W == 0) { break; } for (int i = 1; i <= H; i++) { cin >> S >> T; for (int j = 0; j < S.size(); j++) { if (S[j] == 'L') { Lsx = j + 1; Lsy = i; map1[i][j + 1] = 0; } if (S[j] == '#') { map1[i][j + 1] = 1; } if (S[j] == '.') { map1[i][j + 1] = 0; } if (S[j] == '%') { Lgy = i; Lgx = j + 1; map1[i][j + 1] = 0; } } for (int j = 0; j < T.size(); j++) { if (T[j] == 'R') { Rsx = j + 1; Rsy = i; map2[i][j + 1] = 0; } if (T[j] == '#') { map2[i][j + 1] = 1; } if (T[j] == '.') { map2[i][j + 1] = 0; } if (T[j] == '%') { Rgy = i; Rgx = j + 1; map2[i][j + 1] = 0; } } } // bfs. Q.push(make_tuple(Lsy, Lsx, Rsy, Rsx)); dist[Lsy][Lsx][Rsy][Rsx] = 0; while (!Q.empty()) { tuple<int, int, int, int> tup = Q.front(); Q.pop(); bool Q1, Q2; int ay = get<0>(tup); int ax = get<1>(tup); int by = get<2>(tup); int bx = get<3>(tup); for (int i = 0; i < 4; i++) { int ey = ay + dy1[i], ex = ax + dx1[i]; int fy = by + dy2[i], fx = bx + dx2[i]; if (map1[ey][ex] == 1) { ey = ay; ex = ax; } if (map2[fy][fx] == 1) { fy = by; fx = bx; } if (ey == Lgy && ex == Lgx) { Q1 = true; } else { Q1 = false; } if (fy == Rgy && fx == Rgx) { Q2 = true; } else { Q2 = false; } if (Q1 == true && Q2 == false) { goto G; } if (Q1 == false && Q2 == true) { goto G; } if (dist[ey][ex][fy][fx] == INF) { dist[ey][ex][fy][fx] = dist[ay][ax][by][bx] + 1; Q.push(make_tuple(ey, ex, fy, fx)); } G:; } } // hantei. if (dist[Lgy][Lgx][Rgy][Rgx] == INF) { cout << "No" << endl; } else { cout << "Yes" << endl; } } return 0; }
replace
6
7
6
7
MLE
p01271
C++
Memory Limit Exceeded
#include <cstring> #include <iostream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; vector<string> field; int W, H; unsigned char memo[52][52][52][52]; struct State { int rx, ry, lx, ly; int index; }; int main() { while (true) { cin >> W >> H; if (W == 0 && H == 0) { break; } memset(memo, 0, sizeof(memo)); field.resize(H + 2); field[0] = field[H + 1] = string(W * 2 + 3, '#'); State init = {0}; for (int i = 1; i <= H; ++i) { string l1, l2; cin >> l1 >> l2; string line = "#" + l1 + "#" + l2 + "#"; for (int j = 0; j < line.size(); ++j) { if (line[j] == 'R') { init.rx = j; init.ry = i; } else if (line[j] == 'L') { init.lx = j; init.ly = i; } } field[i] = line; } stack<State> stk; stk.push(init); bool ok = false; while (!stk.empty()) { State cur = stk.top(); stk.pop(); int rx = cur.rx, ry = cur.ry, lx = cur.lx, ly = cur.ly; if (cur.index == 0) { if (memo[rx - W - 1][ry][lx][ly]) { continue; } memo[rx - W - 1][ry][lx][ly] = 1; if (field[ry][rx] == '%' && field[ly][lx] == '%') { ok = true; break; } else if (field[ry][rx] == '%' || field[ly][lx] == '%') { continue; } } int nrx = rx + dx[cur.index], nry = ry + dy[cur.index]; int nlx = lx - dx[cur.index], nly = ly + dy[cur.index]; if (field[nry][nrx] == '#') { nrx -= dx[cur.index]; nry -= dy[cur.index]; } if (field[nly][nlx] == '#') { nlx += dx[cur.index]; nly -= dy[cur.index]; } if (cur.index != 3) { ++cur.index; stk.push(cur); } State next = {nrx, nry, nlx, nly, 0}; stk.push(next); } cout << (ok ? "Yes" : "No") << endl; } return 0; }
#include <cstring> #include <iostream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; vector<string> field; int W, H; unsigned char memo[52][52][52][52]; struct State { unsigned char rx, ry, lx, ly; unsigned char index; }; int main() { while (true) { cin >> W >> H; if (W == 0 && H == 0) { break; } memset(memo, 0, sizeof(memo)); field.resize(H + 2); field[0] = field[H + 1] = string(W * 2 + 3, '#'); State init = {0}; for (int i = 1; i <= H; ++i) { string l1, l2; cin >> l1 >> l2; string line = "#" + l1 + "#" + l2 + "#"; for (int j = 0; j < line.size(); ++j) { if (line[j] == 'R') { init.rx = j; init.ry = i; } else if (line[j] == 'L') { init.lx = j; init.ly = i; } } field[i] = line; } stack<State> stk; stk.push(init); bool ok = false; while (!stk.empty()) { State cur = stk.top(); stk.pop(); int rx = cur.rx, ry = cur.ry, lx = cur.lx, ly = cur.ly; if (cur.index == 0) { if (memo[rx - W - 1][ry][lx][ly]) { continue; } memo[rx - W - 1][ry][lx][ly] = 1; if (field[ry][rx] == '%' && field[ly][lx] == '%') { ok = true; break; } else if (field[ry][rx] == '%' || field[ly][lx] == '%') { continue; } } int nrx = rx + dx[cur.index], nry = ry + dy[cur.index]; int nlx = lx - dx[cur.index], nly = ly + dy[cur.index]; if (field[nry][nrx] == '#') { nrx -= dx[cur.index]; nry -= dy[cur.index]; } if (field[nly][nlx] == '#') { nlx += dx[cur.index]; nly -= dy[cur.index]; } if (cur.index != 3) { ++cur.index; stk.push(cur); } State next = {nrx, nry, nlx, nly, 0}; stk.push(next); } cout << (ok ? "Yes" : "No") << endl; } return 0; }
replace
17
19
17
19
MLE
p01271
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) char a[52][52], b[52][52]; set<tuple<int, int, int, int>> dp; int ans; void calc(int lx, int ly, int rx, int ry) { if (ans == 1) return; if (a[lx][ly] == '%' && b[rx][ry] == '%') { ans = 1; return; } else if (a[lx][ly] == '%' || b[rx][ry] == '%') return; tuple<int, int, int, int> now; now = make_tuple(lx, ly, rx, ry); decltype(dp)::iterator it = dp.find(now); if (it != dp.end()) return; dp.insert(now); int nxl = lx, nyl = ly, nxr = rx, nyr = ry; if (a[lx + 1][ly] != '#') nxl++; if (b[rx - 1][ry] != '#') nxr--; calc(nxl, nyl, nxr, nyr); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx - 1][ly] != '#') nxl--; if (b[rx + 1][ry] != '#') nxr++; calc(nxl, nyl, nxr, nyr); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx][ly + 1] != '#') nyl++; if (b[rx][ry + 1] != '#') nyr++; calc(nxl, nyl, nxr, nyr); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx][ly - 1] != '#') nyl--; if (b[rx][ry - 1] != '#') nyr--; calc(nxl, nyl, nxr, nyr); return; } int main() { while (1) { int w, h; cin >> w >> h; int stxl, styl, stxr, styr; if (w == 0 && h == 0) break; lp(i, 52) { lp(j, 52) { a[i][j] = '#'; b[i][j] = '#'; } } lp(i, h + 1) { if (i == 0) continue; lp(j, w + 1) { if (j == 0) continue; cin >> a[j][i]; if (a[j][i] == 'L') { stxl = j; styl = i; } } lp(j, w + 1) { if (j == 0) continue; cin >> b[j][i]; if (b[j][i] == 'R') { stxr = j; styr = i; } } } dp.clear(); ans = 0; calc(stxl, styl, stxr, styr); if (ans == 0) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define lp(i, n) for (int i = 0; i < n; i++) char a[52][52], b[52][52]; set<tuple<int, int, int, int>> dp; int ans; void calc(int lx, int ly, int rx, int ry) { queue<tuple<int, int, int, int>> Q; Q.push(make_tuple(lx, ly, rx, ry)); while (!Q.empty()) { lx = get<0>(Q.front()); ly = get<1>(Q.front()); rx = get<2>(Q.front()); ry = get<3>(Q.front()); Q.pop(); if (a[lx][ly] == '%' && b[rx][ry] == '%') { ans = 1; return; } else if (a[lx][ly] == '%' || b[rx][ry] == '%') continue; tuple<int, int, int, int> now; now = make_tuple(lx, ly, rx, ry); decltype(dp)::iterator it = dp.find(now); if (it != dp.end()) continue; dp.insert(now); int nxl = lx, nyl = ly, nxr = rx, nyr = ry; if (a[lx + 1][ly] != '#') nxl++; if (b[rx - 1][ry] != '#') nxr--; Q.push(make_tuple(nxl, nyl, nxr, nyr)); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx - 1][ly] != '#') nxl--; if (b[rx + 1][ry] != '#') nxr++; Q.push(make_tuple(nxl, nyl, nxr, nyr)); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx][ly + 1] != '#') nyl++; if (b[rx][ry + 1] != '#') nyr++; Q.push(make_tuple(nxl, nyl, nxr, nyr)); nxl = lx; nyl = ly; nxr = rx; nyr = ry; if (a[lx][ly - 1] != '#') nyl--; if (b[rx][ry - 1] != '#') nyr--; Q.push(make_tuple(nxl, nyl, nxr, nyr)); } return; } int main() { while (1) { int w, h; cin >> w >> h; int stxl, styl, stxr, styr; if (w == 0 && h == 0) break; lp(i, 52) { lp(j, 52) { a[i][j] = '#'; b[i][j] = '#'; } } lp(i, h + 1) { if (i == 0) continue; lp(j, w + 1) { if (j == 0) continue; cin >> a[j][i]; if (a[j][i] == 'L') { stxl = j; styl = i; } } lp(j, w + 1) { if (j == 0) continue; cin >> b[j][i]; if (b[j][i] == 'R') { stxr = j; styr = i; } } } dp.clear(); ans = 0; calc(stxl, styl, stxr, styr); if (ans == 0) cout << "No" << endl; else cout << "Yes" << endl; } return 0; }
replace
8
54
8
61
0
p01271
C++
Runtime Error
// C++11 #include "bits/stdc++.h" #include <bitset> #include <emmintrin.h> #include <fstream> #include <random> #include <sstream> #include <string> #include <sys/time.h> using namespace std; inline long long GetTSC() { long long lo, hi; asm volatile("rdtsc" : "=a"(lo), "=d"(hi)); return lo + (hi << 32); } inline double GetSeconds() { return GetTSC() / 2.8e9; } const long inf = pow(10, 15); int di[] = {-1, 0, 1, 0}; int dj[] = {0, 1, 0, -1}; tuple<int, int> q[50 * 50 * 50 * 50]; void solve() { double starttime = GetSeconds(); while (1) { int w, h; cin >> w >> h; // cerr << w << ", " << h << endl; if (w == 0 && h == 0) break; int lp, ls, rp, rs; bool lb[2500] = {}; bool rb[2500] = {}; bool f[2500][2500] = {}; for (int i = 0; i < h; i++) { string s; cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') lb[i * w + j] = 1; if (s[j] == '%') lp = i * w + j; if (s[j] == 'L') ls = i * w + j; } cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') rb[i * w + j] = 1; if (s[j] == '%') rp = i * w + j; if (s[j] == 'R') rs = i * w + j; } } f[ls][rs] = 1; int qi = 0; int qe = 1; q[0] = make_tuple(ls, rs); bool ff = 0; while (qi < qe) { int lt = get<0>(q[qi]); int rt = get<1>(q[qi]); int li = lt / w; int lj = lt % w; int ri = rt / w; int rj = rt % w; qi++; // cerr << "qie: " << qi << ", " << qe << ", lij: " << li << ", " << lj << // ", rij: " << ri << ", " << rj << endl; for (int i = 0; i < 4; i++) { int nli = li + di[i]; int nlj = lj + dj[i]; int nri = ri + di[i]; int nrj = rj - dj[i]; if (nli < 0 || nli >= h || nlj < 0 || nlj >= w || lb[nli * w + nlj]) { nli = li; nlj = lj; } if (nri < 0 || nri >= h || nrj < 0 || nrj >= w || rb[nri * w + nrj]) { nri = ri; nrj = rj; } int nlt = nli * w + nlj; int nrt = nri * w + nrj; if (nlt == lp && nrt == rp) { ff = 1; break; } if (nlt == lp || nrt == rp) continue; if (f[nlt][nrt]) continue; f[nlt][nrt] = 1; q[qe++] = make_tuple(nlt, nrt); } if (ff) break; } if (ff) { cout << "Yes" << endl; } else { cout << "No" << endl; } } cerr << "time: " << GetSeconds() - starttime << endl; return; } int main() { solve(); return 0; }
// C++11 #include "bits/stdc++.h" #include <bitset> #include <emmintrin.h> #include <fstream> #include <random> #include <sstream> #include <string> #include <sys/time.h> using namespace std; inline long long GetTSC() { long long lo, hi; asm volatile("rdtsc" : "=a"(lo), "=d"(hi)); return lo + (hi << 32); } inline double GetSeconds() { return GetTSC() / 2.8e9; } const long inf = pow(10, 15); int di[] = {-1, 0, 1, 0}; int dj[] = {0, 1, 0, -1}; tuple<int, int> q[50 * 50 * 50 * 50]; void solve() { double starttime = GetSeconds(); while (1) { int w, h; cin >> w >> h; // cerr << w << ", " << h << endl; if (w == 0 && h == 0) break; int lp, ls, rp, rs; bool lb[2500] = {}; bool rb[2500] = {}; bool f[2500][2500] = {}; for (int i = 0; i < h; i++) { string s; cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') lb[i * w + j] = 1; if (s[j] == '%') lp = i * w + j; if (s[j] == 'L') ls = i * w + j; } cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') rb[i * w + j] = 1; if (s[j] == '%') rp = i * w + j; if (s[j] == 'R') rs = i * w + j; } } f[ls][rs] = 1; int qi = 0; int qe = 1; q[0] = make_tuple(ls, rs); bool ff = 0; while (qi < qe) { int lt = get<0>(q[qi]); int rt = get<1>(q[qi]); int li = lt / w; int lj = lt % w; int ri = rt / w; int rj = rt % w; qi++; // cerr << "qie: " << qi << ", " << qe << ", lij: " << li << ", " << lj << // ", rij: " << ri << ", " << rj << endl; for (int i = 0; i < 4; i++) { int nli = li + di[i]; int nlj = lj + dj[i]; int nri = ri + di[i]; int nrj = rj - dj[i]; if (nli < 0 || nli >= h || nlj < 0 || nlj >= w || lb[nli * w + nlj]) { nli = li; nlj = lj; } if (nri < 0 || nri >= h || nrj < 0 || nrj >= w || rb[nri * w + nrj]) { nri = ri; nrj = rj; } int nlt = nli * w + nlj; int nrt = nri * w + nrj; if (nlt == lp && nrt == rp) { ff = 1; break; } if (nlt == lp || nrt == rp) continue; if (f[nlt][nrt]) continue; f[nlt][nrt] = 1; q[qe++] = make_tuple(nlt, nrt); } if (ff) break; } if (ff) { cout << "Yes" << endl; } else { cout << "No" << endl; } } // cerr << "time: " << GetSeconds() - starttime << endl; return; } int main() { solve(); return 0; }
replace
107
108
107
108
0
time: 0.00168785
p01271
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define mp(a, b) make_pair((a), (b)) #define eq == typedef long long ll; typedef complex<double> point; typedef pair<int, int> pii; // →↑←↓ const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, -1, 0, 1}; const double EPS = 1e-9; int main() { while (true) { int W, H; cin >> W >> H; if (W == 0 and H == 0) break; vector<vector<string>> field(2, vector<string>(H + 2, string(W + 2, '#'))); for (int i = 1; i <= H; i++) { for (int j = 0; j < 2; j++) { cin >> field[j][i]; field[j][i] = '#' + field[j][i] + '#'; } } pair<pii, pii> start; pair<pii, pii> goal; for (int i = 0; i < 2; i++) { for (int j = 1; j <= H; j++) { for (int k = 1; k <= W; k++) { if (field[i][j][k] == 'L') { start.first = make_pair(j, k); } else if (field[i][j][k] == 'R') { start.second = make_pair(j, k); } else if (field[i][j][k] == '%') { if (i == 0) { goal.first = make_pair(j, k); } else { goal.second = make_pair(j, k); } } } } } stack<pair<pii, pii>> sta; sta.push(start); bool used[H + 2][W + 2][H + 2][W + 2]; for (int i = 0; i < H + 2; i++) { for (int j = 0; j < W + 2; j++) { for (int k = 0; k < H + 2; k++) { for (int l = 0; l < W + 2; l++) { used[i][j][k][l] = false; } } } } bool ok = false; while (not sta.empty()) { pair<pii, pii> now = sta.top(); sta.pop(); if (used[now.first.first][now.first.second][now.second.first] [now.second.second]) { continue; } used[now.first.first][now.first.second][now.second.first] [now.second.second] = true; if (now == goal) { ok = true; break; } if (now.first == goal.first) { continue; } if (now.second == goal.second) { continue; } for (int i = 0; i < 4; i++) { pair<pii, pii> next = now; next.first.first += dy[i]; next.first.second += dx[i]; next.second.first += dy[i]; next.second.second -= dx[i]; if (field[0][next.first.first][next.first.second] == '#') { next.first = now.first; } if (field[1][next.second.first][next.second.second] == '#') { next.second = now.second; } sta.push(next); } } if (ok) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(), c.rend() #define mp(a, b) make_pair((a), (b)) #define eq == typedef long long ll; typedef complex<double> point; typedef pair<char, char> pii; // →↑←↓ const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, -1, 0, 1}; const double EPS = 1e-9; int main() { while (true) { int W, H; cin >> W >> H; if (W == 0 and H == 0) break; vector<vector<string>> field(2, vector<string>(H + 2, string(W + 2, '#'))); for (int i = 1; i <= H; i++) { for (int j = 0; j < 2; j++) { cin >> field[j][i]; field[j][i] = '#' + field[j][i] + '#'; } } pair<pii, pii> start; pair<pii, pii> goal; for (int i = 0; i < 2; i++) { for (int j = 1; j <= H; j++) { for (int k = 1; k <= W; k++) { if (field[i][j][k] == 'L') { start.first = make_pair(j, k); } else if (field[i][j][k] == 'R') { start.second = make_pair(j, k); } else if (field[i][j][k] == '%') { if (i == 0) { goal.first = make_pair(j, k); } else { goal.second = make_pair(j, k); } } } } } stack<pair<pii, pii>> sta; sta.push(start); bool used[H + 2][W + 2][H + 2][W + 2]; for (int i = 0; i < H + 2; i++) { for (int j = 0; j < W + 2; j++) { for (int k = 0; k < H + 2; k++) { for (int l = 0; l < W + 2; l++) { used[i][j][k][l] = false; } } } } bool ok = false; while (not sta.empty()) { pair<pii, pii> now = sta.top(); sta.pop(); if (used[now.first.first][now.first.second][now.second.first] [now.second.second]) { continue; } used[now.first.first][now.first.second][now.second.first] [now.second.second] = true; if (now == goal) { ok = true; break; } if (now.first == goal.first) { continue; } if (now.second == goal.second) { continue; } for (int i = 0; i < 4; i++) { pair<pii, pii> next = now; next.first.first += dy[i]; next.first.second += dx[i]; next.second.first += dy[i]; next.second.second -= dx[i]; if (field[0][next.first.first][next.first.second] == '#') { next.first = now.first; } if (field[1][next.second.first][next.second.second] == '#') { next.second = now.second; } sta.push(next); } } if (ok) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
replace
27
28
27
28
MLE
p01271
C++
Memory Limit Exceeded
#include <cstring> #include <iostream> #include <memory.h> #include <queue> #include <vector> using namespace std; int maxw, maxh; int lsx, lsy; int rsx, rsy; const int dxl[] = {1, 0, -1, 0}; const int dxr[] = {-1, 0, 1, 0}; const int dyl[] = {0, 1, 0, -1}; const int dyr[] = {0, 1, 0, -1}; typedef pair<int, int> PP; typedef pair<PP, PP> P; bool visited[55][55][55][55] = {}; int main(void) { while (1) { char fieldl[55][55]; char fieldr[55][55]; memset(visited, 0, sizeof(visited)); memset(fieldl, 0, sizeof(fieldl)); memset(fieldr, 0, sizeof(fieldr)); cin >> maxw >> maxh; if (maxw == 0 && maxh == 0) return 0; for (int y = 1; y <= maxh; ++y) { for (int x = 1; x <= maxw; ++x) { cin >> fieldl[y][x]; if (fieldl[y][x] == 'L') { lsy = y; lsx = x; fieldl[y][x] = '.'; } if (fieldl[y][x] == 'R') { rsy = y; rsx = x; fieldl[y][x] = '.'; } } for (int x = 1; x <= maxw; ++x) { cin >> fieldr[y][x]; if (fieldr[y][x] == 'L') { lsy = y; lsx = x; fieldr[y][x] = '.'; } if (fieldr[y][x] == 'R') { rsy = y; rsx = x; fieldr[y][x] = '.'; } } } queue<P> q; bool isGoal = false; q.push(P(PP(lsy, lsx), PP(rsy, rsx))); visited[lsy][lsx][rsy][rsx] = true; while (!q.empty()) { P p = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { bool f = false; PP tol = PP(p.first.first + dyl[i], p.first.second + dxl[i]); // y,x PP tor = PP(p.second.first + dyr[i], p.second.second + dxr[i]); // y,x if (visited[tol.first][tol.second][tor.first][tor.second]) continue; if (fieldl[tol.first][tol.second] == '%' && fieldr[tor.first][tor.second] == '%') { isGoal = true; break; } else if (fieldl[tol.first][tol.second] == '%' || fieldr[tor.first][tor.second] == '%') continue; PP l = PP(p.first.first, p.first.second); PP r = PP(p.second.first, p.second.second); if (fieldl[tol.first][tol.second] == '.') { l = tol; f = true; } if (fieldr[tor.first][tor.second] == '.') { r = tor; f = true; } if (!f) continue; visited[l.first][l.second][r.first][r.second] = true; q.push(P(l, r)); } if (isGoal) break; } (isGoal ? cout << "Yes" << endl : cout << "No" << endl); } }
#include <cstring> #include <iostream> #include <memory.h> #include <queue> #include <vector> using namespace std; int maxw, maxh; int lsx, lsy; int rsx, rsy; const int dxl[] = {1, 0, -1, 0}; const int dxr[] = {-1, 0, 1, 0}; const int dyl[] = {0, 1, 0, -1}; const int dyr[] = {0, 1, 0, -1}; typedef pair<int, int> PP; typedef pair<PP, PP> P; bool visited[55][55][55][55] = {}; int main(void) { while (1) { char fieldl[55][55]; char fieldr[55][55]; memset(visited, 0, sizeof(visited)); memset(fieldl, 0, sizeof(fieldl)); memset(fieldr, 0, sizeof(fieldr)); cin >> maxw >> maxh; if (maxw == 0 && maxh == 0) return 0; for (int y = 1; y <= maxh; ++y) { for (int x = 1; x <= maxw; ++x) { cin >> fieldl[y][x]; if (fieldl[y][x] == 'L') { lsy = y; lsx = x; fieldl[y][x] = '.'; } if (fieldl[y][x] == 'R') { rsy = y; rsx = x; fieldl[y][x] = '.'; } } for (int x = 1; x <= maxw; ++x) { cin >> fieldr[y][x]; if (fieldr[y][x] == 'L') { lsy = y; lsx = x; fieldr[y][x] = '.'; } if (fieldr[y][x] == 'R') { rsy = y; rsx = x; fieldr[y][x] = '.'; } } } queue<P> q; bool isGoal = false; q.push(P(PP(lsy, lsx), PP(rsy, rsx))); visited[lsy][lsx][rsy][rsx] = true; while (!q.empty()) { P p = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { bool f = false; PP tol = PP(p.first.first + dyl[i], p.first.second + dxl[i]); // y,x PP tor = PP(p.second.first + dyr[i], p.second.second + dxr[i]); // y,x if (visited[tol.first][tol.second][tor.first][tor.second]) continue; if (fieldl[tol.first][tol.second] == '%' && fieldr[tor.first][tor.second] == '%') { isGoal = true; break; } else if (fieldl[tol.first][tol.second] == '%' || fieldr[tor.first][tor.second] == '%') continue; PP l = PP(p.first.first, p.first.second); PP r = PP(p.second.first, p.second.second); if (fieldl[tol.first][tol.second] == '.') { l = tol; f = true; } if (fieldr[tor.first][tor.second] == '.') { r = tor; f = true; } if (!f) continue; if (visited[l.first][l.second][r.first][r.second]) continue; visited[l.first][l.second][r.first][r.second] = true; q.push(P(l, r)); } if (isGoal) break; } (isGoal ? cout << "Yes" << endl : cout << "No" << endl); } }
insert
93
93
93
95
MLE
p01271
C++
Runtime Error
#include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct P { int rx, ry, lx, ly; P() {} P(int rx, int ry, int lx, int ly) : rx(rx), ry(ry), lx(lx), ly(ly){}; }; int w, h, dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1}; bool used[50][50][50][50]; char Rin[50][50], Len[50][50]; bool bfs(int, int, int, int); int main() { int rx, ry, lx, ly; while (cin >> w >> h && w || h) { memset(used, false, sizeof(used)); for (int i = 0; i < h; i++) { cin >> Len[i] >> Rin[i]; for (int j = 0; j < w; j++) { if (Rin[i][j] == 'R') rx = i, ry = j; if (Len[i][j] == 'L') lx = i, ly = j; } } cout << (bfs(rx, ry, lx, ly) ? "Yes" : "No") << endl; } } bool bfs(int rx, int ry, int lx, int ly) { queue<P> que; que.push(P(rx, ry, lx, ly)); while (!que.empty()) { P p = que.front(); que.pop(); if (Rin[p.rx][p.ry] == '%' && Len[p.lx][p.ly] == '%') return true; if (Rin[p.rx][p.ry] == '%' || Len[p.lx][p.ly] == '%') continue; used[p.rx][p.ry][p.lx][p.ly] = true; for (int i = 0; i < 4; i++) { int nrx = p.rx + dx[i], nry = p.ry + dy[i]; int nlx = p.lx + dx[i], nly = p.ly - dy[i]; if (nrx < 0 || nry < 0 || nrx >= h || nry >= w || Rin[nrx][nry] == '#') nrx = p.rx, nry = p.ry; if (nlx < 0 || nly < 0 || nlx >= h || nly >= w || Len[nlx][nly] == '#') nlx = p.lx, nly = p.ly; if (used[nrx][nry][nlx][nly]) continue; que.push(P(nrx, nry, nlx, nly)); } } return false; }
#include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; struct P { int rx, ry, lx, ly; P() {} P(int rx, int ry, int lx, int ly) : rx(rx), ry(ry), lx(lx), ly(ly){}; }; int w, h, dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1}; bool used[50][50][50][50]; char Rin[50][50], Len[50][50]; bool bfs(int, int, int, int); int main() { int rx, ry, lx, ly; while (cin >> w >> h && w || h) { memset(used, false, sizeof(used)); for (int i = 0; i < h; i++) { cin >> Len[i] >> Rin[i]; for (int j = 0; j < w; j++) { if (Rin[i][j] == 'R') rx = i, ry = j; if (Len[i][j] == 'L') lx = i, ly = j; } } cout << (bfs(rx, ry, lx, ly) ? "Yes" : "No") << endl; } } bool bfs(int rx, int ry, int lx, int ly) { queue<P> que; que.push(P(rx, ry, lx, ly)); while (!que.empty()) { P p = que.front(); que.pop(); if (used[p.rx][p.ry][p.lx][p.ly]) continue; if (Rin[p.rx][p.ry] == '%' && Len[p.lx][p.ly] == '%') return true; if (Rin[p.rx][p.ry] == '%' || Len[p.lx][p.ly] == '%') continue; used[p.rx][p.ry][p.lx][p.ly] = true; for (int i = 0; i < 4; i++) { int nrx = p.rx + dx[i], nry = p.ry + dy[i]; int nlx = p.lx + dx[i], nly = p.ly - dy[i]; if (nrx < 0 || nry < 0 || nrx >= h || nry >= w || Rin[nrx][nry] == '#') nrx = p.rx, nry = p.ry; if (nlx < 0 || nly < 0 || nlx >= h || nly >= w || Len[nlx][nly] == '#') nlx = p.lx, nly = p.ly; if (used[nrx][nry][nlx][nly]) continue; que.push(P(nrx, nry, nlx, nly)); } } return false; }
insert
36
36
36
38
0
p01271
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <class T, class U, class V> inline T clamp(const T &x, const U &lo, const V &hi) { return x < lo ? lo : x > hi ? hi : x; } bool solve(int W, int H, const vector<string> &RoomL, const vector<string> &RoomR) { int sxL, syL, sxR, syR; rep(i, H) rep(j, W) { if (RoomL[i][j] == 'L') sxL = i, syL = j; if (RoomR[i][j] == 'R') sxR = i, syR = j; } bool vis[W][H][W][H]; memset(vis, 0, W * H * W * H); vis[sxL][syL][sxR][syR] = true; queue<tuple<int, int, int, int>> Q; for (Q.emplace(sxL, syL, sxR, syR); !Q.empty();) { int xL, yL, xR, yR; tie(xL, yL, xR, yR) = Q.front(); Q.pop(); rep(k, 4) { int nxL = clamp(xL + dx[k], 0, H - 1), nyL = clamp(yL + dy[k], 0, W - 1), nxR = clamp(xR + dx[k], 0, H - 1), nyR = clamp(yR - dy[k], 0, W - 1); if (RoomL[nxL][nyL] == '#' and RoomR[nxR][nyR] == '#') continue; if (RoomL[nxL][nyL] == '#') nxL -= dx[k], nyL -= dy[k]; if (RoomR[nxR][nyR] == '#') nxR -= dx[k], nyR += dy[k]; if (RoomL[nxL][nyL] == '%' and RoomR[nxR][nyR] == '%') return true; if (RoomL[nxL][nyL] == '%' or RoomR[nxR][nyR] == '%' or vis[nxL][nyL][nxR][nyR]) continue; vis[nxL][nyL][nxR][nyR] = true; Q.emplace(nxL, nyL, nxR, nyR); } } return false; } int main() { for (int W, H; cin >> W >> H, W | H;) { vector<string> RoomL(H), RoomR(H); rep(i, H) cin >> RoomL[i] >> RoomR[i]; cout << (solve(W, H, RoomL, RoomR) ? "Yes" : "No") << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <class T, class U, class V> inline T clamp(const T &x, const U &lo, const V &hi) { return x < lo ? lo : x > hi ? hi : x; } bool solve(int W, int H, const vector<string> &RoomL, const vector<string> &RoomR) { int sxL, syL, sxR, syR; rep(i, H) rep(j, W) { if (RoomL[i][j] == 'L') sxL = i, syL = j; if (RoomR[i][j] == 'R') sxR = i, syR = j; } bool vis[H][W][H][W]; memset(vis, 0, W * H * W * H); vis[sxL][syL][sxR][syR] = true; queue<tuple<int, int, int, int>> Q; for (Q.emplace(sxL, syL, sxR, syR); !Q.empty();) { int xL, yL, xR, yR; tie(xL, yL, xR, yR) = Q.front(); Q.pop(); rep(k, 4) { int nxL = clamp(xL + dx[k], 0, H - 1), nyL = clamp(yL + dy[k], 0, W - 1), nxR = clamp(xR + dx[k], 0, H - 1), nyR = clamp(yR - dy[k], 0, W - 1); if (RoomL[nxL][nyL] == '#' and RoomR[nxR][nyR] == '#') continue; if (RoomL[nxL][nyL] == '#') nxL -= dx[k], nyL -= dy[k]; if (RoomR[nxR][nyR] == '#') nxR -= dx[k], nyR += dy[k]; if (RoomL[nxL][nyL] == '%' and RoomR[nxR][nyR] == '%') return true; if (RoomL[nxL][nyL] == '%' or RoomR[nxR][nyR] == '%' or vis[nxL][nyL][nxR][nyR]) continue; vis[nxL][nyL][nxR][nyR] = true; Q.emplace(nxL, nyL, nxR, nyR); } } return false; } int main() { for (int W, H; cin >> W >> H, W | H;) { vector<string> RoomL(H), RoomR(H); rep(i, H) cin >> RoomL[i] >> RoomR[i]; cout << (solve(W, H, RoomL, RoomR) ? "Yes" : "No") << endl; } }
replace
20
21
20
21
0
p01273
C++
Runtime Error
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) using namespace std; int main() { int n, m; while (cin >> n >> m, n || m) { bool infect[n + 1]; rep(i, n + 1) infect[i] = false; infect[1] = true; long long ans = 1; long long t[m]; int s[m], d[m]; rep(i, m) cin >> t[i] >> s[i] >> d[i]; for (int i = m; i >= 0; i--) { for (int j = 0; j < i - 1; j++) { if (t[j] > t[j + 1]) { long long alt = t[j]; t[j] = t[j + 1]; t[j + 1] = alt; alt = s[j]; s[j] = s[j + 1]; s[j + 1] = alt; alt = d[j]; d[j] = d[j + 1]; d[j + 1] = alt; } } } rep(i, n) { if (infect[s[i]]) { if (!infect[d[i]]) ans++; infect[d[i]] = true; } } cout << ans << endl; } return 0; }
#include <iostream> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) using namespace std; int main() { int n, m; while (cin >> n >> m, n || m) { bool infect[n + 1]; rep(i, n + 1) infect[i] = false; infect[1] = true; long long ans = 1; long long t[m]; int s[m], d[m]; rep(i, m) cin >> t[i] >> s[i] >> d[i]; for (int i = m; i >= 0; i--) { for (int j = 0; j < i - 1; j++) { if (t[j] > t[j + 1]) { long long alt = t[j]; t[j] = t[j + 1]; t[j + 1] = alt; alt = s[j]; s[j] = s[j + 1]; s[j + 1] = alt; alt = d[j]; d[j] = d[j + 1]; d[j + 1] = alt; } } } rep(i, m) { if (infect[s[i]]) { if (!infect[d[i]]) ans++; infect[d[i]] = true; } } cout << ans << endl; } return 0; }
replace
30
31
30
31
-11
p01273
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct Packet { int t, s, d; Packet() = default; Packet(int t, int s, int d) : t(t), s(s), d(d) {} bool operator<(const Packet &p) const { return t < p.t; } }; int main() { while (true) { int N, M; cin >> N >> M; if (N + M == 0) { break; } bool infected[20001] = {true}; vector<Packet> packet(M); for (int i = 0; i < M; i++) { int t, s, d; cin >> t >> s >> d; packet[i] = Packet(t, s - 1, d - 1); } sort(packet.begin(), packet.end()); for (int i = 0; i < M; i++) { if (infected[packet[i].s]) { infected[packet[i].t] = true; } } int ans = 0; for (int i = 0; i < N; i++) { if (infected[i]) { ans++; } } cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct Packet { int t, s, d; Packet() = default; Packet(int t, int s, int d) : t(t), s(s), d(d) {} bool operator<(const Packet &p) const { return t < p.t; } }; int main() { while (true) { int N, M; cin >> N >> M; if (N + M == 0) { break; } bool infected[20001] = {true}; vector<Packet> packet(M); for (int i = 0; i < M; i++) { int t, s, d; cin >> t >> s >> d; packet[i] = Packet(t, s - 1, d - 1); } sort(packet.begin(), packet.end()); for (int i = 0; i < M; i++) { if (infected[packet[i].s]) { infected[packet[i].d] = true; } } int ans = 0; for (int i = 0; i < N; i++) { if (infected[i]) { ans++; } } cout << ans << endl; } return 0; }
replace
36
37
36
37
0
p01273
C++
Runtime Error
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; class Packet { public: int time, src, dst; Packet(int _t, int _s, int _d) : time(_t), src(_s), dst(_d) {} bool operator<(const Packet &p) const { return time < p.time; } bool operator>(const Packet &p) const { return time > p.time; } }; int main() { int computer_num, send_list_num; while (~scanf("%d %d", &computer_num, &send_list_num)) { if (computer_num == 0 && send_list_num == 0) break; vector<Packet> packets; for (int i = 0; i < send_list_num; i++) { int time, src, dst; scanf("%d %d %d", &time, &src, &dst); packets.push_back(Packet(time, src, dst)); } sort(packets.begin(), packets.end()); bool infected[21]; memset(infected, 0, sizeof(infected)); infected[1] = true; for (int i = 0; i < packets.size(); i++) { if (infected[packets[i].src]) { infected[packets[i].dst] = true; } } int res = 0; for (int i = 1; i <= computer_num; i++) { if (infected[i]) res++; } printf("%d\n", res); } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; class Packet { public: int time, src, dst; Packet(int _t, int _s, int _d) : time(_t), src(_s), dst(_d) {} bool operator<(const Packet &p) const { return time < p.time; } bool operator>(const Packet &p) const { return time > p.time; } }; int main() { int computer_num, send_list_num; while (~scanf("%d %d", &computer_num, &send_list_num)) { if (computer_num == 0 && send_list_num == 0) break; vector<Packet> packets; for (int i = 0; i < send_list_num; i++) { int time, src, dst; scanf("%d %d %d", &time, &src, &dst); packets.push_back(Packet(time, src, dst)); } sort(packets.begin(), packets.end()); bool infected[20001]; memset(infected, 0, sizeof(infected)); infected[1] = true; for (int i = 0; i < packets.size(); i++) { if (infected[packets[i].src]) { infected[packets[i].dst] = true; } } int res = 0; for (int i = 1; i <= computer_num; i++) { if (infected[i]) res++; } printf("%d\n", res); } }
replace
56
57
56
57
0
p01273
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define len(v) (int)(v).size() #define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end()) #define cmx(x, y) x = max(x, y) #define cmn(x, y) x = min(x, y) #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(kbrni, v.size()) cout << " " << v[kbrni]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(kbrni, s) cout << " " << kbrni; \ cout << endl #define smap(m) \ cout << #m << ":"; \ each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \ cout << endl using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<double> vd; typedef vector<P> vp; const int MAX_N = 100005; struct tri { int t, s, d; bool operator<(const tri &another) const { return t < another.t; } }; class UF { private: int sz; vector<int> par, nrank, size; public: UF() {} UF(int node_size) { sz = node_size; par.resize(sz), nrank.resize(sz), size.resize(sz); rep(i, sz) { par[i] = i; nrank[i] = 0; size[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (nrank[x] < nrank[y]) swap(x, y); par[y] = x; size[x] += size[y]; if (nrank[x] == nrank[y]) nrank[x]++; } int query(int x) { x = find(x); return size[x]; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); while (1) { int n, m; cin >> n >> m; if (n == 0) { break; } vector<tri> vec(m); rep(i, m) { int t, s, d; cin >> t >> s >> d; s--, d--; vec[i] = (tri){t, s, d}; } sort(all(vec)); UF uf(n); rep(i, m) { if (uf.same(0, vec[i].s)) { uf.unite(0, vec[i].t); } } cout << uf.query(0) << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define len(v) (int)(v).size() #define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end()) #define cmx(x, y) x = max(x, y) #define cmn(x, y) x = min(x, y) #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(kbrni, v.size()) cout << " " << v[kbrni]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(kbrni, s) cout << " " << kbrni; \ cout << endl #define smap(m) \ cout << #m << ":"; \ each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \ cout << endl using namespace std; typedef pair<int, int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<double> vd; typedef vector<P> vp; const int MAX_N = 100005; struct tri { int t, s, d; bool operator<(const tri &another) const { return t < another.t; } }; class UF { private: int sz; vector<int> par, nrank, size; public: UF() {} UF(int node_size) { sz = node_size; par.resize(sz), nrank.resize(sz), size.resize(sz); rep(i, sz) { par[i] = i; nrank[i] = 0; size[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (nrank[x] < nrank[y]) swap(x, y); par[y] = x; size[x] += size[y]; if (nrank[x] == nrank[y]) nrank[x]++; } int query(int x) { x = find(x); return size[x]; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { cin.tie(0); ios::sync_with_stdio(false); while (1) { int n, m; cin >> n >> m; if (n == 0) { break; } vector<tri> vec(m); rep(i, m) { int t, s, d; cin >> t >> s >> d; s--, d--; vec[i] = (tri){t, s, d}; } sort(all(vec)); UF uf(n); rep(i, m) { if (uf.same(0, vec[i].s)) { uf.unite(0, vec[i].d); } } cout << uf.query(0) << endl; } return 0; }
replace
109
110
109
110
0
p01274
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int HP[100], M; long long dp1[100001], dp2[100001]; int solve(int N) { memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); for (int i = 0; i < N; i++) { cin >> HP[i]; } cin >> M; bool end = false; while (M--) { string Name, Target; int MP, Damage; cin >> Name >> MP >> Target >> Damage; if (MP == 0) { if (Damage > 0) end = true; continue; } long long *dp = Target.size() == 3 ? dp1 : dp2; for (int i = MP; i <= 100000; i++) { dp[i] = max(dp[i], dp[i - MP] + Damage); } } if (end) return (0); int best = 1 << 30, pos = *max_element(HP, HP + N); for (int i = 0; i <= 100000 && HP[i] <= pos; i++) { int ret = 0; for (int j = 0; j < N; j++) { ret += lower_bound(dp2, dp2 + 100001, HP[j] - dp1[i]) - dp2; } best = min(best, i + ret); } return (best); } int main() { int N; while (cin >> N, N) { cout << solve(N) << endl; } }
#include <bits/stdc++.h> using namespace std; int HP[100], M; long long dp1[100001], dp2[100001]; int solve(int N) { memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); for (int i = 0; i < N; i++) { cin >> HP[i]; } cin >> M; bool end = false; while (M--) { string Name, Target; int MP, Damage; cin >> Name >> MP >> Target >> Damage; if (MP == 0) { if (Damage > 0) end = true; continue; } long long *dp = Target.size() == 3 ? dp1 : dp2; for (int i = MP; i <= 100000; i++) { dp[i] = max(dp[i], dp[i - MP] + Damage); } } if (end) return (0); int best = 1 << 30; for (int i = 0; i <= 100000; i++) { int ret = 0; for (int j = 0; j < N; j++) { ret += lower_bound(dp2, dp2 + 100001, HP[j] - dp1[i]) - dp2; } best = min(best, i + ret); } return (best); } int main() { int N; while (cin >> N, N) { cout << solve(N) << endl; } }
replace
33
35
33
35
0
p01274
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int multi[100010], single[100010], hp[110]; int main() { int N; while (cin >> N && N) { for (int i = 0; i < 100010; i++) { multi[i] = 1e9; single[i] = 1e9; } multi[0] = 0; single[0] = 0; for (int i = 0; i < N; i++) cin >> hp[i]; int M; cin >> M; for (int i = 0; i < M; i++) { string name; int mp; string command; int dmp; cin >> name >> mp >> command >> dmp; if (command == "Single") { for (int j = 0; j <= 100000; j++) { int s = min(100000, j + dmp); single[s] = min(single[s], single[j] + mp); } } else { for (int j = 0; j <= 100000; j++) { int s = min(100000, j + dmp); multi[s] = min(multi[s], multi[j] + mp); } } } for (int i = 100000; i >= 0; i--) { multi[i] = min(multi[i], multi[i + 1]); single[i] = min(single[i], single[i + 1]); } long long ans = 1e9; for (int i = 0; i <= 100000; i++) { long long u = multi[i]; for (int j = 0; j < N; j++) { int v = min(0, hp[j] - i); u += single[v]; } ans = min(u, ans); } cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int multi[100010], single[100010], hp[110]; int main() { int N; while (cin >> N && N) { for (int i = 0; i < 100010; i++) { multi[i] = 1e9; single[i] = 1e9; } multi[0] = 0; single[0] = 0; for (int i = 0; i < N; i++) cin >> hp[i]; int M; cin >> M; for (int i = 0; i < M; i++) { string name; int mp; string command; int dmp; cin >> name >> mp >> command >> dmp; if (command == "Single") { for (int j = 0; j <= 100000; j++) { int s = min(100000, j + dmp); single[s] = min(single[s], single[j] + mp); } } else { for (int j = 0; j <= 100000; j++) { int s = min(100000, j + dmp); multi[s] = min(multi[s], multi[j] + mp); } } } for (int i = 100000; i >= 0; i--) { multi[i] = min(multi[i], multi[i + 1]); single[i] = min(single[i], single[i + 1]); } long long ans = 1e9; for (int i = 0; i <= 100000; i++) { long long u = multi[i]; for (int j = 0; j < N; j++) { int v = max(0, hp[j] - i); u += single[v]; } ans = min(u, ans); } cout << ans << endl; } return 0; }
replace
46
47
46
47
0
p01275
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define INF INT_MAX / 2 typedef vector<int> vi; typedef vector<vi> vvi; typedef string Node; typedef pair<Node, int> pni; struct Comp { bool operator()(pni a, pni b) { return a.second > b.second; } }; string rot(string a, int from, int to, int n) { rep(i, from, to + 1) { int j = a[i] - '0'; j = (j + n + 10) % 10; a[i] = '0' + j; } return a; } int solve(int k) { string start, end; cin >> start >> end; priority_queue<pni, vector<pni>, Comp> que; map<Node, int> dist; set<Node> done; que.push(pni(start, 0)); while (!que.empty()) { Node n = que.top().first; int c = que.top().second; que.pop(); if (done.find(n) != done.end()) continue; done.insert(n); if (n == end) return c; int from = 0; while (n[from] == end[from]) from++; rep(i, from, k) rep(j, i, k) rep(jj, -9, 10) { if (jj == 0) continue; string nnode = rot(n, i, j, jj); int nc = c + 1; if (dist.find(nnode) == dist.end()) dist[nnode] = INF; if (nc < dist[nnode]) { dist[nnode] = nc; que.push(pni(nnode, nc)); } } } } int main() { while (1) { int k; cin >> k; if (k == 0) return 0; cout << solve(k) << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) #define INF INT_MAX / 2 typedef vector<int> vi; typedef vector<vi> vvi; typedef string Node; typedef pair<Node, int> pni; struct Comp { bool operator()(pni a, pni b) { return a.second > b.second; } }; string rot(string a, int from, int to, int n) { rep(i, from, to + 1) { int j = a[i] - '0'; j = (j + n + 10) % 10; a[i] = '0' + j; } return a; } int solve(int k) { string start, end; cin >> start >> end; priority_queue<pni, vector<pni>, Comp> que; map<Node, int> dist; set<Node> done; que.push(pni(start, 0)); while (!que.empty()) { Node n = que.top().first; int c = que.top().second; que.pop(); if (done.find(n) != done.end()) continue; done.insert(n); if (n == end) return c; int from = 0; while (n[from] == end[from]) from++; int delta = (end[from] - '0') - (n[from] - '0'); rep(to, from, k) { Node nnode = rot(n, from, to, delta); int nc = c + 1; if (dist.find(nnode) == dist.end()) dist[nnode] = INF; if (nc < dist[nnode]) { dist[nnode] = nc; que.push(pni(nnode, nc)); } } } } int main() { while (1) { int k; cin >> k; if (k == 0) return 0; cout << solve(k) << endl; } }
replace
48
53
48
51
MLE
p01275
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int solve(vector<int> D, vector<int> imos, int i, int d) { int n = D.size(); if (i == n) return 0; d += imos[i]; D[i] += 10 - d; D[i] %= 10; int ret = 1e9; if (D[i]) { for (int j = i + 1; j <= n; ++j) { imos[j] -= D[i]; ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10)); imos[j] += D[i]; } } else { ret = min(ret, solve(D, imos, i + 1, d)); } return ret; } int main() { int N; while (cin >> N, N) { string S, T; cin >> S >> T; vector<int> D(N); for (int i = 0; i < N; ++i) { D[i] = (T[i] - S[i] + 10) % 10; } cout << solve(D, vector<int>(N, 0), 0, 0) << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int solve(vector<int> D, vector<int> imos, int i, int d) { int n = D.size(); if (i == n) return 0; d += imos[i]; D[i] += 10 - d; D[i] %= 10; int ret = 1e9; if (D[i]) { for (int j = i + 1; j <= n; ++j) { imos[j] -= D[i]; ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10)); imos[j] += D[i]; } } else { ret = min(ret, solve(D, imos, i + 1, d)); } return ret; } int main() { int N; while (cin >> N, N) { string S, T; cin >> S >> T; vector<int> D(N); for (int i = 0; i < N; ++i) { D[i] = (T[i] - S[i] + 10) % 10; } cout << solve(D, vector<int>(N + 1, 0), 0, 0) << endl; } return 0; }
replace
33
34
33
34
-6
munmap_chunk(): invalid pointer
p01275
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; int N; int solve(string &s, string &t) { queue<string> Q; map<string, int> dist; Q.push(s); dist[s] = 0; while (!Q.empty()) { string v = Q.front(); Q.pop(); if (v == t) return dist[v]; for (int i = 0; i < N; i++) { if (v[i] == t[i]) continue; string next = v; int diff = (t[i] - v[i] + 10) % 10; for (int j = i; j < N; j++) { next[j] = ((next[j] - '0' + diff) % 10) + '0'; if (dist.find(next) == dist.end()) { dist[next] = dist[v] + 1; Q.push(next); } } } } } int main() { string s, t; while (cin >> N, N) { cin >> s >> t; cout << solve(s, t) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int N; int solve(string &s, string &t) { queue<string> Q; map<string, int> dist; Q.push(s); dist[s] = 0; while (!Q.empty()) { string v = Q.front(); Q.pop(); if (v == t) return dist[v]; int i; for (i = 0; i < N; i++) { if (v[i] != t[i]) break; } string next = v; int diff = (t[i] - v[i] + 10) % 10; for (int j = i; j < N; j++) { next[j] = ((next[j] - '0' + diff) % 10) + '0'; if (dist.find(next) == dist.end()) { dist[next] = dist[v] + 1; Q.push(next); } } } } int main() { string s, t; while (cin >> N, N) { cin >> s >> t; cout << solve(s, t) << endl; } return 0; }
replace
16
27
16
28
MLE
p01275
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int main() { int n; while (cin >> n, n) { string s, t; cin >> s >> t; map<string, int> m; m[s] = 0; queue<string> q; q.push(s); while (!s.empty()) { string a = q.front(); q.pop(); int g = 0; while (a[g] == t[g]) g++; int r = (10 + t[g] - a[g]) % 10; string b = a; loop(i, g, n) { b[i] = (b[i] + r - '0') % 10 + '0'; // cout<<b<<" "<<r<<endl; if (m[b] == 0 || m[a] + 1 < m[b]) { m[b] = m[a] + 1; q.push(b); if (b == t) { cout << m[b] << endl; goto end; } } } } end:; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() #define shosu(x) fixed << setprecision(x) using namespace std; // kaewasuretyuui typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef pair<int, pii> pip; typedef vector<pip> vip; const double PI = acos(-1); const double EPS = 1e-8; const int inf = 1e8; int main() { int n; while (cin >> n, n) { string s, t; cin >> s >> t; if (s == t) { cout << 0 << endl; continue; } map<string, int> m; m[s] = 0; queue<string> q; q.push(s); while (!s.empty()) { string a = q.front(); q.pop(); int g = 0; while (a[g] == t[g]) g++; int r = (10 + t[g] - a[g]) % 10; string b = a; loop(i, g, n) { b[i] = (b[i] + r - '0') % 10 + '0'; // cout<<b<<" "<<r<<endl; if (m[b] == 0 || m[a] + 1 < m[b]) { m[b] = m[a] + 1; q.push(b); if (b == t) { cout << m[b] << endl; goto end; } } } } end:; } }
insert
37
37
37
41
0
p01275
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, ans; string A, B; string cal(string a, int x, int l, int r) { for (int i = l; i < r; i++) { int p = a[i] - '0'; p = (p + x) % 10; a[i] = '0' + p; } return a; } void dfs(string a, string b, int d, int sum) { // cout<<a<<' '<<b<<' '<<d<<endl; if (a == b) { ans = min(ans, sum); return; } if (a[d] != b[d]) { for (int i = 1; i <= 9; i++) { string nex = cal(a, i, d, d + 1); if (nex[d] != b[d]) continue; for (int r = d + 1; r <= n; r++) { nex = cal(a, i, d, r); dfs(nex, b, d + 1, sum + 1); } } } else { dfs(a, b, d + 1, sum); } } signed main() { while (cin >> n, n) { cin >> A >> B; ans = 1e9; dfs(A, B, 0, 0); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int n, ans; string A, B; string cal(string a, int x, int l, int r) { for (int i = l; i < r; i++) { int p = a[i] - '0'; p = (p + x) % 10; a[i] = '0' + p; } return a; } void dfs(string a, string b, int d, int sum) { // cout<<a<<' '<<b<<' '<<d<<endl; if (ans <= sum) return; if (a == b) { ans = min(ans, sum); return; } if (a[d] != b[d]) { for (int i = 1; i <= 9; i++) { string nex = cal(a, i, d, d + 1); if (nex[d] != b[d]) continue; for (int r = d + 1; r <= n; r++) { nex = cal(a, i, d, r); dfs(nex, b, d + 1, sum + 1); } } } else { dfs(a, b, d + 1, sum); } } signed main() { while (cin >> n, n) { cin >> A >> B; ans = 1e9; dfs(A, B, 0, 0); cout << ans << endl; } }
insert
18
18
18
21
TLE
p01275
C++
Time Limit Exceeded
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> // #include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> // #include<list> #include <algorithm> #include <deque> #include <queue> // #include<numeric> #include <utility> // #include<memory> #include <cassert> #include <functional> #include <random> #include <set> #include <stack> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, -1, 0, 1}; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; int N; vi now, target; bool dfs(int d, int maxDepth) { if (d == maxDepth) { if (now == target) return true; else return false; } int exist = 0; for (int i = 0; i < N; i++) { int tmp = (10 + target[i] - now[i]) % 10; exist |= 1 << tmp; } int cnt = __builtin_popcount(exist); if (max(cnt * 2 / 3, min(2, cnt)) + d > maxDepth + 1) return false; for (int i = 0; i < N; i++) if (now[i] != target[i]) { int diff = (10 + target[i] - now[i]) % 10; for (int j = i; j < N; j++) { for (int k = i; k <= j; k++) { now[k] = (now[k] + diff) % 10; } if (dfs(d + 1, maxDepth)) return true; for (int k = i; k <= j; k++) { now[k] = (now[k] + 10 - diff) % 10; } } break; } for (int i = N - 1; i >= 0; i--) if (now[i] != target[i]) { int diff = (10 + target[i] - now[i]) % 10; for (int j = 0; j <= i; j++) { for (int k = j; k <= i; k++) { now[k] = (now[k] + diff) % 10; } if (dfs(d + 1, maxDepth)) return true; for (int k = j; k <= i; k++) { now[k] = (now[k] + 10 - diff) % 10; } } break; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> N) { if (N == 0) break; now.clear(); target.clear(); now.resize(N); target.resize(N); string s, t; cin >> s >> t; for (int i = 0; i < N; i++) { now[i] = (s[i] - '0'); target[i] = (t[i] - '0'); } int ans = N; for (int i = 0; i < ans; i++) { if (dfs(0, i)) { ans = i; break; } } cout << ans << endl; } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> // #include<cctype> #include <climits> #include <iostream> #include <map> #include <string> #include <vector> // #include<list> #include <algorithm> #include <deque> #include <queue> // #include<numeric> #include <utility> // #include<memory> #include <cassert> #include <functional> #include <random> #include <set> #include <stack> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, -1, 0, 1}; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; int N; vi now, target; bool dfs(int d, int maxDepth) { if (d == maxDepth) { if (now == target) return true; else return false; } int exist = 0; for (int i = 0; i < N; i++) { int tmp = (10 + target[i] - now[i]) % 10; exist |= 1 << tmp; } int cnt = __builtin_popcount(exist); if (cnt / 3 * 3 + (cnt % 3) + d > maxDepth + 1) return false; for (int i = 0; i < N; i++) if (now[i] != target[i]) { int diff = (10 + target[i] - now[i]) % 10; for (int j = i; j < N; j++) { for (int k = i; k <= j; k++) { now[k] = (now[k] + diff) % 10; } if (dfs(d + 1, maxDepth)) return true; for (int k = i; k <= j; k++) { now[k] = (now[k] + 10 - diff) % 10; } } break; } for (int i = N - 1; i >= 0; i--) if (now[i] != target[i]) { int diff = (10 + target[i] - now[i]) % 10; for (int j = 0; j <= i; j++) { for (int k = j; k <= i; k++) { now[k] = (now[k] + diff) % 10; } if (dfs(d + 1, maxDepth)) return true; for (int k = j; k <= i; k++) { now[k] = (now[k] + 10 - diff) % 10; } } break; } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> N) { if (N == 0) break; now.clear(); target.clear(); now.resize(N); target.resize(N); string s, t; cin >> s >> t; for (int i = 0; i < N; i++) { now[i] = (s[i] - '0'); target[i] = (t[i] - '0'); } int ans = N; for (int i = 0; i < ans; i++) { if (dfs(0, i)) { ans = i; break; } } cout << ans << endl; } return 0; }
replace
48
49
48
49
TLE
p01277
C++
Time Limit Exceeded
#include <iostream> #include <random> #include <set> #include <vector> using namespace std; using ll = long long; struct P { ll x; ll y; bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; } }; vector<P> pos; int N; bool ok(const int i, const int j) { const ll x = (pos[i].x + pos[j].x) / 2; const ll y = (pos[i].y + pos[j].y) / 2; const ll dx = pos[j].x - x; const ll dy = pos[j].y - y; set<P> p; int cnt = 0; ll h = 0; for (int k = 0; k < N; k++) { if (k == i or k == j) { continue; } const ll X = pos[k].x - x; const ll Y = pos[k].y - y; const ll S = X * dx + Y * dy; const ll T = -X * dy + Y * dx; h = max(h, abs(T)); if (S == 0) { cnt++; } p.insert(P{S, T}); } if (cnt >= 3 or h == 0) { return false; } for (const auto &pp : p) { if (p.find(P{-pp.x, pp.y}) == p.end()) { return false; } } return true; } int main() { cin >> N; for (int i = 0; i < N; i++) { ll x, y; cin >> x >> y; pos.push_back(P{2 * x, 2 * y}); } mt19937 mt{random_device{}()}; uniform_int_distribution<int> dist{0, N - 1}; for (int i = 0; i < 100 * N; i++) { int u = 0; int v = 0; while (u == v) { u = dist(mt); v = dist(mt); } if (ok(u, v)) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
#include <iostream> #include <random> #include <set> #include <vector> using namespace std; using ll = long long; struct P { ll x; ll y; bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; } }; vector<P> pos; int N; bool ok(const int i, const int j) { const ll x = (pos[i].x + pos[j].x) / 2; const ll y = (pos[i].y + pos[j].y) / 2; const ll dx = pos[j].x - x; const ll dy = pos[j].y - y; set<P> p; int cnt = 0; ll h = 0; for (int k = 0; k < N; k++) { if (k == i or k == j) { continue; } const ll X = pos[k].x - x; const ll Y = pos[k].y - y; const ll S = X * dx + Y * dy; const ll T = -X * dy + Y * dx; h = max(h, abs(T)); if (S == 0) { cnt++; } p.insert(P{S, T}); } if (cnt >= 3 or h == 0) { return false; } for (const auto &pp : p) { if (p.find(P{-pp.x, pp.y}) == p.end()) { return false; } } return true; } int main() { cin >> N; for (int i = 0; i < N; i++) { ll x, y; cin >> x >> y; pos.push_back(P{2 * x, 2 * y}); } mt19937 mt{random_device{}()}; uniform_int_distribution<int> dist{0, N - 1}; for (int i = 0; i < 30 * N; i++) { int u = 0; int v = 0; while (u == v) { u = dist(mt); v = dist(mt); } if (ok(u, v)) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
replace
56
57
56
57
TLE
p01277
C++
Time Limit Exceeded
#include <cmath> #include <iostream> #include <limits> #include <set> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; namespace libcomp { namespace geometry { const double EPS = 1e-9; bool tolerant_eq(double a, double b) { return abs(a - b) < EPS; } struct Point { double x; double y; explicit Point(const double &x = 0.0, const double &y = 0.0) : x(x), y(y) {} static Point invalid() { double qnan = numeric_limits<double>::quiet_NaN(); return Point(qnan, qnan); } bool is_valid() const { return !(isnan(x) || isnan(y)); } Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point &operator+=(const Point &p) { return *this = *this + p; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point &operator-=(const Point &p) { return *this = *this - p; } Point operator*(double s) const { return Point(x * s, y * s); } Point &operator*=(double s) { return *this = *this * s; } Point operator/(double s) const { return Point(x / s, y / s); } Point &operator/=(double s) { return *this = *this / s; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } bool operator!=(const Point &p) const { return x != p.x || y != p.y; } bool operator<(const Point &p) const { return (x == p.x) ? (y < p.y) : (x < p.x); } }; Point operator*(double s, const Point &p) { return p * s; } bool tolerant_eq(const Point &a, const Point &b) { return tolerant_eq(a.x, b.x) && tolerant_eq(a.y, b.y); } double abs(const Point &p) { return sqrt(p.x * p.x + p.y * p.y); } double norm(const Point &p) { return p.x * p.x + p.y * p.y; } Point unit(const Point &p) { return p / abs(p); } Point ortho(const Point &p) { return Point(-p.y, p.x); } double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } int ccw(const Point &a, const Point &b, const Point &c) { Point d = b - a, e = c - a; if (cross(d, e) > 0.0) { return 1; } if (cross(d, e) < 0.0) { return -1; } if (dot(d, e) < 0.0) { return 2; } if (abs(d) < abs(e)) { return -2; } return 0; } struct Line { Point a; Point b; explicit Line(const Point &a = Point(), const Point &b = Point()) : a(a), b(b) {} static Line invalid() { Point inv = Point::invalid(); return Line(inv, inv); } bool is_valid() const { return a.is_valid() && b.is_valid(); } bool operator<(const Line &l) const { return (a == l.a) ? (b < l.b) : (a < l.a); } }; bool tolerant_eq(const Line &a, const Line &b) { return ::std::abs(cross(a.b - a.a, b.a - a.a)) < EPS; } Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a); return l.a + t * (l.b - l.a); } Point reflection(const Line &l, const Point &p) { return p + 2.0 * (projection(l, p) - p); } } // namespace geometry } // namespace libcomp using namespace libcomp::geometry; typedef pair<int, int> pii; inline ll pow2(ll x) { return x * x; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<Point> p(n); set<pii> ps; int xsum = 0, ysum = 0; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; p[i] = Point(x, y); ps.insert(pii(x, y)); xsum += x; ysum += y; } const Point c(static_cast<double>(xsum) / n, static_cast<double>(ysum) / n); bool answer = false; if (n % 2 == 0) { for (int i = 0; i < n; ++i) { if (tolerant_eq(p[i], c)) { continue; } const Line l(p[i], c); bool accept = true; int count = 0; for (int j = 0; accept && j < n; ++j) { const Point a = p[j]; const Point b = reflection(l, a); if (tolerant_eq(a, b) && ++count > 2) { accept = false; } const int ibx = static_cast<int>(floor(b.x + 0.5)); const int iby = static_cast<int>(floor(b.y + 0.5)); const Point d(ibx, iby); if (!tolerant_eq(b, d)) { accept = false; } if (ps.find(pii(ibx, iby)) == ps.end()) { accept = false; } } if (accept) { answer = true; break; } } } for (int i = 0; !answer && i < n; ++i) { for (int j = i + 1; !answer && j < n; ++j) { const Point q = projection(Line(p[i], p[j]), c); if (tolerant_eq(q, c)) { continue; } const Line l((p[i] + p[j]) / 2, c); bool accept = true; int count = 0; for (int k = 0; accept && k < n; ++k) { const Point a = p[k]; const Point b = reflection(l, a); if (tolerant_eq(a, b) && ++count > n % 2) { accept = false; } const int ibx = static_cast<int>(floor(b.x + 0.5)); const int iby = static_cast<int>(floor(b.y + 0.5)); const Point d(ibx, iby); if (!tolerant_eq(b, d)) { accept = false; } if (ps.find(pii(ibx, iby)) == ps.end()) { accept = false; } } if (accept) { answer = true; } } } cout << (answer ? "Yes" : "No") << endl; return 0; }
#include <cmath> #include <iostream> #include <limits> #include <set> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; namespace libcomp { namespace geometry { const double EPS = 1e-9; bool tolerant_eq(double a, double b) { return abs(a - b) < EPS; } struct Point { double x; double y; explicit Point(const double &x = 0.0, const double &y = 0.0) : x(x), y(y) {} static Point invalid() { double qnan = numeric_limits<double>::quiet_NaN(); return Point(qnan, qnan); } bool is_valid() const { return !(isnan(x) || isnan(y)); } Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point &operator+=(const Point &p) { return *this = *this + p; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point &operator-=(const Point &p) { return *this = *this - p; } Point operator*(double s) const { return Point(x * s, y * s); } Point &operator*=(double s) { return *this = *this * s; } Point operator/(double s) const { return Point(x / s, y / s); } Point &operator/=(double s) { return *this = *this / s; } bool operator==(const Point &p) const { return x == p.x && y == p.y; } bool operator!=(const Point &p) const { return x != p.x || y != p.y; } bool operator<(const Point &p) const { return (x == p.x) ? (y < p.y) : (x < p.x); } }; Point operator*(double s, const Point &p) { return p * s; } bool tolerant_eq(const Point &a, const Point &b) { return tolerant_eq(a.x, b.x) && tolerant_eq(a.y, b.y); } double abs(const Point &p) { return sqrt(p.x * p.x + p.y * p.y); } double norm(const Point &p) { return p.x * p.x + p.y * p.y; } Point unit(const Point &p) { return p / abs(p); } Point ortho(const Point &p) { return Point(-p.y, p.x); } double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } int ccw(const Point &a, const Point &b, const Point &c) { Point d = b - a, e = c - a; if (cross(d, e) > 0.0) { return 1; } if (cross(d, e) < 0.0) { return -1; } if (dot(d, e) < 0.0) { return 2; } if (abs(d) < abs(e)) { return -2; } return 0; } struct Line { Point a; Point b; explicit Line(const Point &a = Point(), const Point &b = Point()) : a(a), b(b) {} static Line invalid() { Point inv = Point::invalid(); return Line(inv, inv); } bool is_valid() const { return a.is_valid() && b.is_valid(); } bool operator<(const Line &l) const { return (a == l.a) ? (b < l.b) : (a < l.a); } }; bool tolerant_eq(const Line &a, const Line &b) { return ::std::abs(cross(a.b - a.a, b.a - a.a)) < EPS; } Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.b - l.a) / norm(l.b - l.a); return l.a + t * (l.b - l.a); } Point reflection(const Line &l, const Point &p) { return p + 2.0 * (projection(l, p) - p); } } // namespace geometry } // namespace libcomp using namespace libcomp::geometry; typedef pair<int, int> pii; inline ll pow2(ll x) { return x * x; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<Point> p(n); set<pii> ps; int xsum = 0, ysum = 0; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; p[i] = Point(x, y); ps.insert(pii(x, y)); xsum += x; ysum += y; } const Point c(static_cast<double>(xsum) / n, static_cast<double>(ysum) / n); bool answer = false; if (n % 2 == 0) { for (int i = 0; i < n; ++i) { if (tolerant_eq(p[i], c)) { continue; } const Line l(p[i], c); bool accept = true; int count = 0; for (int j = 0; accept && j < n; ++j) { const Point a = p[j]; const Point b = reflection(l, a); if (tolerant_eq(a, b) && ++count > 2) { accept = false; } const int ibx = static_cast<int>(floor(b.x + 0.5)); const int iby = static_cast<int>(floor(b.y + 0.5)); const Point d(ibx, iby); if (!tolerant_eq(b, d)) { accept = false; } if (ps.find(pii(ibx, iby)) == ps.end()) { accept = false; } } if (accept) { answer = true; break; } } } for (int i = 0; !answer && i < 4; ++i) { for (int j = i + 1; !answer && j < n; ++j) { const Point q = projection(Line(p[i], p[j]), c); if (tolerant_eq(q, c)) { continue; } const Line l((p[i] + p[j]) / 2, c); bool accept = true; int count = 0; for (int k = 0; accept && k < n; ++k) { const Point a = p[k]; const Point b = reflection(l, a); if (tolerant_eq(a, b) && ++count > n % 2) { accept = false; } const int ibx = static_cast<int>(floor(b.x + 0.5)); const int iby = static_cast<int>(floor(b.y + 0.5)); const Point d(ibx, iby); if (!tolerant_eq(b, d)) { accept = false; } if (ps.find(pii(ibx, iby)) == ps.end()) { accept = false; } } if (accept) { answer = true; } } } cout << (answer ? "Yes" : "No") << endl; return 0; }
replace
162
163
162
163
TLE
p01278
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define EPS 1e-8 typedef complex<double> P; typedef vector<P> G; 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); } }; bool intersectLS(const L &l, const L &s) { return cross(l[1] - l[0], s[0] - l[0]) * // s[0] is left of l cross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l } P crosspoint(const L &l, const L &m) { double A = cross(l[1] - l[0], m[1] - m[0]); double B = cross(l[1] - l[0], l[1] - m[0]); if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > +EPS) return +1; if (cross(b, c) < -EPS) return -1; if (dot(b, c) < -EPS) return +2; if (norm(b) < norm(c)) return -2; return 0; } bool notleft(P a, P b, P c) { return (ccw(a, b, c) != +1); } bool isIntoG(const G &g, const P &p) { size_t sz = g.size(); for (size_t i = 0; i < sz; i++) { if (notleft(p, g[i], g[(i + 1) % sz])) return false; } return true; } bool isfar(G &g, P &p) { for (int i = 0; i < g.size(); i++) if (abs(g[i] - p) < EPS) return false; return true; } #define CURR(P, i) P[i] #define NEXT(P, i) P[(i + 1) % P.size()] bool ConvexCut(G &g, L &l, vector<G> &ret) { int sp, inp = 0; for (sp = 0; sp < g.size(); sp++) if (intersectLS(l, L(g[sp], g[(sp + 1) % g.size()]))) break; if (sp == g.size()) return false; ret.resize(2); P xp = crosspoint(l, L(g[sp], g[(sp + 1) % g.size()])); sp++; for (int i = sp; i < sp + g.size(); i++) { if (isfar(ret[inp], g[i % g.size()])) ret[inp].push_back(g[i % g.size()]); if (intersectLS(l, L(g[i % g.size()], g[(i + 1) % g.size()]))) { P tx = crosspoint(l, L(g[i % g.size()], g[(i + 1) % g.size()])); if (abs(tx - xp) < EPS) continue; xp = tx; for (int j = 0; j < 2; j++) { if (isfar(ret[j], xp)) ret[j].push_back(xp); } inp = (inp + 1) % 2; } } return true; } L trans(P a, P b) { P med = (a + b); med = P(med.real() / 2, med.imag() / 2); P hoge = (a - b); hoge *= P(0, 1); return L(med + hoge * P(1000, 0), med - hoge * P(1000, 0)); } double Area(G &g) { double ret = 0; for (int i = 0; i < g.size(); i++) ret += cross(CURR(g, i), NEXT(g, i)) / 2; return ret; } int main() { cout << setiosflags(ios::fixed) << setprecision(7); int N, M; while (cin >> N >> M, (N || M)) { cin >> N >> M; G g, pts; for (int i = 0; i < N; i++) { double x, y; cin >> x >> y; g.push_back(P(x, y)); } for (int i = 0; i < M; i++) { double x, y; cin >> x >> y; pts.push_back(P(x, y)); } for (int i = 0; i < M; i++) { G f = g; for (int j = 0; j < M; j++) { if (i == j) continue; L divide = trans(pts[i], pts[j]); vector<G> cut; if (!ConvexCut(f, divide, cut)) continue; if (isIntoG(cut[0], pts[i])) f = cut[0]; else f = cut[1]; } cout << Area(f) << endl; } } }
#include <algorithm> #include <cassert> #include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define EPS 1e-8 typedef complex<double> P; typedef vector<P> G; 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); } }; bool intersectLS(const L &l, const L &s) { return cross(l[1] - l[0], s[0] - l[0]) * // s[0] is left of l cross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l } P crosspoint(const L &l, const L &m) { double A = cross(l[1] - l[0], m[1] - m[0]); double B = cross(l[1] - l[0], l[1] - m[0]); if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > +EPS) return +1; if (cross(b, c) < -EPS) return -1; if (dot(b, c) < -EPS) return +2; if (norm(b) < norm(c)) return -2; return 0; } bool notleft(P a, P b, P c) { return (ccw(a, b, c) != +1); } bool isIntoG(const G &g, const P &p) { size_t sz = g.size(); for (size_t i = 0; i < sz; i++) { if (notleft(p, g[i], g[(i + 1) % sz])) return false; } return true; } bool isfar(G &g, P &p) { for (int i = 0; i < g.size(); i++) if (abs(g[i] - p) < EPS) return false; return true; } #define CURR(P, i) P[i] #define NEXT(P, i) P[(i + 1) % P.size()] bool ConvexCut(G &g, L &l, vector<G> &ret) { int sp, inp = 0; for (sp = 0; sp < g.size(); sp++) if (intersectLS(l, L(g[sp], g[(sp + 1) % g.size()]))) break; if (sp == g.size()) return false; ret.resize(2); P xp = crosspoint(l, L(g[sp], g[(sp + 1) % g.size()])); sp++; for (int i = sp; i < sp + g.size(); i++) { if (isfar(ret[inp], g[i % g.size()])) ret[inp].push_back(g[i % g.size()]); if (intersectLS(l, L(g[i % g.size()], g[(i + 1) % g.size()]))) { P tx = crosspoint(l, L(g[i % g.size()], g[(i + 1) % g.size()])); if (abs(tx - xp) < EPS) continue; xp = tx; for (int j = 0; j < 2; j++) { if (isfar(ret[j], xp)) ret[j].push_back(xp); } inp = (inp + 1) % 2; } } return true; } L trans(P a, P b) { P med = (a + b); med = P(med.real() / 2, med.imag() / 2); P hoge = (a - b); hoge *= P(0, 1); return L(med + hoge * P(1000, 0), med - hoge * P(1000, 0)); } double Area(G &g) { double ret = 0; for (int i = 0; i < g.size(); i++) ret += cross(CURR(g, i), NEXT(g, i)) / 2; return ret; } int main() { cout << setiosflags(ios::fixed) << setprecision(7); int N, M; while (cin >> N >> M, (N || M)) { G g, pts; for (int i = 0; i < N; i++) { double x, y; cin >> x >> y; g.push_back(P(x, y)); } for (int i = 0; i < M; i++) { double x, y; cin >> x >> y; pts.push_back(P(x, y)); } for (int i = 0; i < M; i++) { G f = g; for (int j = 0; j < M; j++) { if (i == j) continue; L divide = trans(pts[i], pts[j]); vector<G> cut; if (!ConvexCut(f, divide, cut)) continue; if (isIntoG(cut[0], pts[i])) f = cut[0]; else f = cut[1]; } cout << Area(f) << endl; } } }
delete
131
132
131
131
TLE
p01279
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define INF 10000000 #define SOUR 200 #define SINK 201 int N, M; class Node { public: vector<int> to; }; class Edge { public: int s, d; double c; Edge(int s, int d, double c) : s(s), d(d), c(c) {} bool operator<(const Edge &e) const { return c < e.c; } }; int flow[202][202], capa[202][202]; int dfs(int p, int T, int mf, vector<Node> &graph, vector<int> &level, vector<bool> &finished) { if (p == T) return mf; if (finished[p]) return 0; finished[p] = true; for (int i = 0; i < graph[p].to.size(); i++) { int next = graph[p].to[i], fw = capa[p][next] - flow[p][next]; if (level[p] >= level[next]) continue; if (fw <= 0) continue; int f = dfs(next, T, min(mf, fw), graph, level, finished); if (f > 0) { finished[p] = false; flow[p][next] += f; flow[next][p] -= f; return f; } } return 0; } int dinic(int S, int T, vector<Node> &graph) { bool end = false; int total = 0; while (!end) { end = true; vector<int> level(graph.size(), -1); level[S] = 0; queue<int> q; q.push(S); while (!q.empty()) { int n = q.front(); q.pop(); for (int i = 0; i < graph[n].to.size(); i++) { int next = graph[n].to[i]; if (level[next] != -1) continue; if (capa[n][next] - flow[n][next] <= 0) continue; level[next] = level[n] + 1; q.push(next); } } if (level[T] == -1) break; vector<bool> finished(graph.size()); while (1) { int fw = dfs(S, T, INF, graph, level, finished); if (fw <= 0) break; total += fw; end = false; } } return total; } bool calc(int &hi, int &lo, vector<Edge> &G, double &ans) { if (hi < lo) return false; int mi = (hi + lo) / 2; memset(flow, 0, sizeof(flow)); memset(capa, 0, sizeof(capa)); vector<Node> graph(205); for (int i = 0; i <= mi; i++) { graph[G[i].s].to.push_back(G[i].d); graph[G[i].d].to.push_back(G[i].s); graph[SOUR].to.push_back(G[i].s); graph[G[i].d].to.push_back(SINK); capa[G[i].s][G[i].d] = 1; capa[G[i].d][G[i].s] = 1; flow[G[i].d][G[i].s] = 1; capa[SOUR][G[i].s] = 1; capa[G[i].s][SOUR] = 1; flow[G[i].s][SOUR] = 1; capa[G[i].d][SINK] = 1; capa[SINK][G[i].d] = 1; flow[SINK][G[i].d] = 1; } if (dinic(SOUR, SINK, graph) == M) { hi = mi - 1; ans = min(G[mi].c, ans); } else { lo = mi + 1; } return true; } int main() { while (scanf("%d%d", &N, &M), (N || M)) { scanf("%d%d", &N, &M); double x[100], y[100], v[100]; for (int i = 0; i < N; i++) scanf("%lf%lf%lf", &x[i], &y[i], &v[i]); vector<Edge> e; for (int j = 0; j < M; j++) { double xx, yy; scanf("%lf%lf", &xx, &yy); for (int i = 0; i < N; i++) { double dist = (x[i] - xx) * (x[i] - xx) + (y[i] - yy) * (y[i] - yy); e.push_back(Edge(i, j + 100, sqrt(dist) / v[i])); } } sort(e.begin(), e.end()); int hi = e.size() - 1, lo = 0; double ans = INF; while (1) { if (!calc(hi, lo, e, ans)) break; } printf("%.20lf\n", ans); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; #define INF 10000000 #define SOUR 200 #define SINK 201 int N, M; class Node { public: vector<int> to; }; class Edge { public: int s, d; double c; Edge(int s, int d, double c) : s(s), d(d), c(c) {} bool operator<(const Edge &e) const { return c < e.c; } }; int flow[202][202], capa[202][202]; int dfs(int p, int T, int mf, vector<Node> &graph, vector<int> &level, vector<bool> &finished) { if (p == T) return mf; if (finished[p]) return 0; finished[p] = true; for (int i = 0; i < graph[p].to.size(); i++) { int next = graph[p].to[i], fw = capa[p][next] - flow[p][next]; if (level[p] >= level[next]) continue; if (fw <= 0) continue; int f = dfs(next, T, min(mf, fw), graph, level, finished); if (f > 0) { finished[p] = false; flow[p][next] += f; flow[next][p] -= f; return f; } } return 0; } int dinic(int S, int T, vector<Node> &graph) { bool end = false; int total = 0; while (!end) { end = true; vector<int> level(graph.size(), -1); level[S] = 0; queue<int> q; q.push(S); while (!q.empty()) { int n = q.front(); q.pop(); for (int i = 0; i < graph[n].to.size(); i++) { int next = graph[n].to[i]; if (level[next] != -1) continue; if (capa[n][next] - flow[n][next] <= 0) continue; level[next] = level[n] + 1; q.push(next); } } if (level[T] == -1) break; vector<bool> finished(graph.size()); while (1) { int fw = dfs(S, T, INF, graph, level, finished); if (fw <= 0) break; total += fw; end = false; } } return total; } bool calc(int &hi, int &lo, vector<Edge> &G, double &ans) { if (hi < lo) return false; int mi = (hi + lo) / 2; memset(flow, 0, sizeof(flow)); memset(capa, 0, sizeof(capa)); vector<Node> graph(205); for (int i = 0; i <= mi; i++) { graph[G[i].s].to.push_back(G[i].d); graph[G[i].d].to.push_back(G[i].s); graph[SOUR].to.push_back(G[i].s); graph[G[i].d].to.push_back(SINK); capa[G[i].s][G[i].d] = 1; capa[G[i].d][G[i].s] = 1; flow[G[i].d][G[i].s] = 1; capa[SOUR][G[i].s] = 1; capa[G[i].s][SOUR] = 1; flow[G[i].s][SOUR] = 1; capa[G[i].d][SINK] = 1; capa[SINK][G[i].d] = 1; flow[SINK][G[i].d] = 1; } if (dinic(SOUR, SINK, graph) == M) { hi = mi - 1; ans = min(G[mi].c, ans); } else { lo = mi + 1; } return true; } int main() { while (scanf("%d%d", &N, &M), (N || M)) { double x[100], y[100], v[100]; for (int i = 0; i < N; i++) scanf("%lf%lf%lf", &x[i], &y[i], &v[i]); vector<Edge> e; for (int j = 0; j < M; j++) { double xx, yy; scanf("%lf%lf", &xx, &yy); for (int i = 0; i < N; i++) { double dist = (x[i] - xx) * (x[i] - xx) + (y[i] - yy) * (y[i] - yy); e.push_back(Edge(i, j + 100, sqrt(dist) / v[i])); } } sort(e.begin(), e.end()); int hi = e.size() - 1, lo = 0; double ans = INF; while (1) { if (!calc(hi, lo, e, ans)) break; } printf("%.20lf\n", ans); } }
delete
138
139
138
138
TLE
p01279
C++
Runtime Error
/* 16:50 - 17:23 */ #include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define IINF (INT_MAX) #define EPS (1e-9) #define equals(a, b) (fabs((a) - (b)) < EPS) #define MAX 500 #define pow2(a) ((a) * (a)) using namespace std; // Library - maxflow - begin 17:01 struct edge { int to, cap, rev; edge(int to = IINF, int cap = IINF, int rev = IINF) : to(to), cap(cap), rev(rev) {} }; vector<vector<edge>> G; bool used[MAX]; void add_edge(int from, int to, int cap) { G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, IINF); if (f == 0) return flow; flow += f; } } // Library - maxflow - end 17:07 struct Point { double x, y, v; Point(double x = IINF, double y = IINF, double v = IINF) : x(x), y(y), v(v) {} }; struct Data { double cost; int from, to; Data(double cost = IINF, int from = IINF, int to = IINF) : cost(cost), from(from), to(to) {} bool operator<(const Data &a) const { return cost < a.cost; } }; int N, M, source, sink; Point troops[MAX]; Point base[MAX]; bool check[MAX]; vector<Data> info; inline double getDist(Point a, Point b) { return sqrt(pow2(a.x - b.x) + pow2(b.y - a.y)); } int main() { while (cin >> N >> M, N | M) { info.clear(); rep(i, N) cin >> troops[i].x >> troops[i].y >> troops[i].v; rep(i, M) { cin >> base[i].x >> base[i].y; check[i] = false; rep(j, N) info.push_back( Data(getDist(base[i], troops[j]) / troops[j].v, j, N + i)); } sort(info.begin(), info.end()); source = N + M; sink = source + 1; G.resize(sink + 1); // rep(i,sink+1)G[i].clear(); rep(i, N) add_edge(source, i, 1); rep(i, M) add_edge(N + i, sink, 1); int finish = 0; /* int sp = 0; rep(i,info.size()){ add_edge(info[i].from,info[i].to,1); if(!check[info[i].to-N]){ finish++; check[info[i].to-N] = true; } sp = i + 1; if(finish == M)break; } */ vector<vector<edge>> tmp = G; double ans = IINF; double L = 0, R = (1 << 27), H; rep(_, 100) { H = (L + R) * 0.5; G = tmp; rep(i, info.size()) { if (equals(info[i].cost, H) || info[i].cost < H) add_edge(info[i].from, info[i].to, 1); else break; } if (max_flow(source, sink) == M) { ans = min(ans, H); R = H; } else L = H; } cout << setiosflags(ios::fixed) << setprecision(8) << ans << endl; } return 0; }
/* 16:50 - 17:23 */ #include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define IINF (INT_MAX) #define EPS (1e-9) #define equals(a, b) (fabs((a) - (b)) < EPS) #define MAX 500 #define pow2(a) ((a) * (a)) using namespace std; // Library - maxflow - begin 17:01 struct edge { int to, cap, rev; edge(int to = IINF, int cap = IINF, int rev = IINF) : to(to), cap(cap), rev(rev) {} }; vector<vector<edge>> G; bool used[MAX]; void add_edge(int from, int to, int cap) { G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; for (;;) { memset(used, 0, sizeof(used)); int f = dfs(s, t, IINF); if (f == 0) return flow; flow += f; } } // Library - maxflow - end 17:07 struct Point { double x, y, v; Point(double x = IINF, double y = IINF, double v = IINF) : x(x), y(y), v(v) {} }; struct Data { double cost; int from, to; Data(double cost = IINF, int from = IINF, int to = IINF) : cost(cost), from(from), to(to) {} bool operator<(const Data &a) const { return cost < a.cost; } }; int N, M, source, sink; Point troops[MAX]; Point base[MAX]; bool check[MAX]; vector<Data> info; inline double getDist(Point a, Point b) { return sqrt(pow2(a.x - b.x) + pow2(b.y - a.y)); } int main() { while (cin >> N >> M, N | M) { info.clear(); rep(i, N) cin >> troops[i].x >> troops[i].y >> troops[i].v; rep(i, M) { cin >> base[i].x >> base[i].y; check[i] = false; rep(j, N) info.push_back( Data(getDist(base[i], troops[j]) / troops[j].v, j, N + i)); } sort(info.begin(), info.end()); source = N + M; sink = source + 1; G.resize(sink + 1); rep(i, sink + 1) G[i].clear(); rep(i, N) add_edge(source, i, 1); rep(i, M) add_edge(N + i, sink, 1); int finish = 0; /* int sp = 0; rep(i,info.size()){ add_edge(info[i].from,info[i].to,1); if(!check[info[i].to-N]){ finish++; check[info[i].to-N] = true; } sp = i + 1; if(finish == M)break; } */ vector<vector<edge>> tmp = G; double ans = IINF; double L = 0, R = (1 << 27), H; rep(_, 100) { H = (L + R) * 0.5; G = tmp; rep(i, info.size()) { if (equals(info[i].cost, H) || info[i].cost < H) add_edge(info[i].from, info[i].to, 1); else break; } if (max_flow(source, sink) == M) { ans = min(ans, H); R = H; } else L = H; } cout << setiosflags(ios::fixed) << setprecision(8) << ans << endl; } return 0; }
replace
109
110
109
110
0
p01279
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #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; class Edge { public: int to, cap, rev; Edge(){}; Edge(int to0, int cap0) { to = to0; cap = cap0; } Edge(int to0, int cap0, int rev0) { to = to0; cap = cap0; rev = rev0; } }; int maxFlow(const vector<vector<Edge>> &edges0, int source, int sink) { static vector<vector<Edge>> edges; static vector<bool> used; class Func { public: static int dfs(int s, int t, int f) { if (s == t) return f; used[s] = true; for (unsigned i = 0; i < edges[s].size(); ++i) { Edge &e = edges[s][i]; if (!used[e.to] && e.cap > 0) { int g = dfs(e.to, t, min(f, e.cap)); if (g > 0) { e.cap -= g; edges[e.to][e.rev].cap += g; return g; } } } return 0; } }; int n = edges0.size(); edges.assign(n, vector<Edge>()); for (int i = 0; i < n; ++i) { for (unsigned j = 0; j < edges0[i].size(); ++j) { const Edge &e = edges0[i][j]; edges[i].push_back(Edge(e.to, e.cap, edges[e.to].size())); edges[e.to].push_back(Edge(i, 0, edges[i].size() - 1)); } } int ret = 0; for (;;) { used.assign(n, false); int f = Func::dfs(source, sink, INT_MAX); if (f == 0) return ret; ret += f; } } int main() { for (;;) { int n, m; cin >> n >> m; if (n == 0) return 0; vector<int> x1(n), y1(n), v(n); for (int i = 0; i < n; ++i) cin >> x1[i] >> y1[i] >> v[i]; vector<int> x2(m), y2(m); for (int i = 0; i < m; ++i) cin >> x2[i] >> y2[i]; vector<pair<double, pair<int, int>>> a; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { double d = sqrt(pow(x1[i] - x2[j], 2.0) + pow(y1[i] - y2[j], 2.0)) / v[i]; a.push_back(make_pair(d, make_pair(i, j))); } } sort(a.begin(), a.end()); int left = -1; int right = n * m - 1; while (left < right) { int mid = (left + right) / 2; vector<vector<Edge>> edges(n + m + 2); for (int i = 0; i < n; ++i) edges[0].push_back(Edge(i + 1, 1)); for (int i = 0; i < m; ++i) edges[n + 1 + i].push_back(Edge(n + m + 1, 1)); for (int i = 0; i <= mid; ++i) edges[a[i].second.first + 1].push_back( Edge(a[i].second.second + n + 1, 1)); if (maxFlow(edges, 0, n + m + 1) == m) right = mid; else left = mid + 1; } printf("%.10f\n", a[left].first); } }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #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; class Edge { public: int to, cap, rev; Edge(){}; Edge(int to0, int cap0) { to = to0; cap = cap0; } Edge(int to0, int cap0, int rev0) { to = to0; cap = cap0; rev = rev0; } }; int maxFlow(const vector<vector<Edge>> &edges0, int source, int sink) { static vector<vector<Edge>> edges; static vector<bool> used; class Func { public: static int dfs(int s, int t, int f) { if (s == t) return f; used[s] = true; for (unsigned i = 0; i < edges[s].size(); ++i) { Edge &e = edges[s][i]; if (!used[e.to] && e.cap > 0) { int g = dfs(e.to, t, min(f, e.cap)); if (g > 0) { e.cap -= g; edges[e.to][e.rev].cap += g; return g; } } } return 0; } }; int n = edges0.size(); edges.assign(n, vector<Edge>()); for (int i = 0; i < n; ++i) { for (unsigned j = 0; j < edges0[i].size(); ++j) { const Edge &e = edges0[i][j]; edges[i].push_back(Edge(e.to, e.cap, edges[e.to].size())); edges[e.to].push_back(Edge(i, 0, edges[i].size() - 1)); } } int ret = 0; for (;;) { used.assign(n, false); int f = Func::dfs(source, sink, INT_MAX); if (f == 0) return ret; ret += f; } } int main() { for (;;) { int n, m; cin >> n >> m; if (n == 0) return 0; vector<int> x1(n), y1(n), v(n); for (int i = 0; i < n; ++i) cin >> x1[i] >> y1[i] >> v[i]; vector<int> x2(m), y2(m); for (int i = 0; i < m; ++i) cin >> x2[i] >> y2[i]; vector<pair<double, pair<int, int>>> a; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { double d = sqrt(pow(x1[i] - x2[j], 2.0) + pow(y1[i] - y2[j], 2.0)) / v[i]; a.push_back(make_pair(d, make_pair(i, j))); } } sort(a.begin(), a.end()); int left = 0; int right = n * m - 1; while (left < right) { int mid = (left + right) / 2; vector<vector<Edge>> edges(n + m + 2); for (int i = 0; i < n; ++i) edges[0].push_back(Edge(i + 1, 1)); for (int i = 0; i < m; ++i) edges[n + 1 + i].push_back(Edge(n + m + 1, 1)); for (int i = 0; i <= mid; ++i) edges[a[i].second.first + 1].push_back( Edge(a[i].second.second + n + 1, 1)); if (maxFlow(edges, 0, n + m + 1) == m) right = mid; else left = mid + 1; } printf("%.10f\n", a[left].first); } }
replace
105
106
105
106
TLE
p01279
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef complex<double> Point; int n, m; Point p[100]; double v[100]; vector<vector<int>> g; vector<int> match; vector<bool> used; bool dfs(int v) { used[v] = true; for (int u : g[v]) { int w = match[u]; if (w < 0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; } } return false; } int solve() { int res = 0; match.assign(n + m, -1); for (int v = 0; v < m + n; v++) { if (match[v] < 0) { used.assign(n + m, false); if (dfs(v)) res++; } } return res; } bool check(double x) { g.assign(n + m, vector<int>()); for (int i = 0; i < n; i++) { for (int j = n; j < n + m; j++) { double dist = abs(p[i] - p[j]); if (dist <= x * v[i]) { g[i].push_back(j); g[j].push_back(i); } } } return solve() == m; } int main() { while (scanf("%d %d", &n, &m), n) { for (int i = 0; i < n + m; i++) { double x, y; scanf("%lf %lf", &x, &y); p[i] = Point(x, y); if (i < n) scanf("%lf", v + i); } double l = 0.0, r = 20000.0; for (int i = 0; i < 100; i++) { double m = (l + r) / 2; if (check(m)) r = m; else l = m; } printf("%.20f\n", l); } }
#include <bits/stdc++.h> using namespace std; typedef complex<double> Point; int n, m; Point p[200]; double v[200]; vector<vector<int>> g; vector<int> match; vector<bool> used; bool dfs(int v) { used[v] = true; for (int u : g[v]) { int w = match[u]; if (w < 0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; } } return false; } int solve() { int res = 0; match.assign(n + m, -1); for (int v = 0; v < m + n; v++) { if (match[v] < 0) { used.assign(n + m, false); if (dfs(v)) res++; } } return res; } bool check(double x) { g.assign(n + m, vector<int>()); for (int i = 0; i < n; i++) { for (int j = n; j < n + m; j++) { double dist = abs(p[i] - p[j]); if (dist <= x * v[i]) { g[i].push_back(j); g[j].push_back(i); } } } return solve() == m; } int main() { while (scanf("%d %d", &n, &m), n) { for (int i = 0; i < n + m; i++) { double x, y; scanf("%lf %lf", &x, &y); p[i] = Point(x, y); if (i < n) scanf("%lf", v + i); } double l = 0.0, r = 20000.0; for (int i = 0; i < 100; i++) { double m = (l + r) / 2; if (check(m)) r = m; else l = m; } printf("%.20f\n", l); } }
replace
6
8
6
8
0
p01279
C++
Time Limit Exceeded
#include <cstdio> #include <vector> using namespace std; bool match(int x, const vector<vector<int>> &xg, vector<bool> &visited, vector<int> &p) { if (x < 0) return true; if (visited[x]) return false; visited[x] = true; for (int i = 0; i < xg[x].size(); ++i) { int y = xg[x][i]; if (match(p[y], xg, visited, p)) { p[y] = x; return true; } } return false; } int max_matching(const vector<vector<int>> &xg, const int ys) { vector<int> p(ys, -1); vector<bool> visited(xg.size()); int count = 0; for (int x = 0; x < xg.size(); ++x) { fill(visited.begin(), visited.end(), false); if (match(x, xg, visited, p)) count += 1; } return count; } int main() { while (true) { int n, m; vector<pair<int, pair<int, int>>> troop; vector<pair<int, int>> base; scanf("%d%d", &n, &m); if (n == 0) break; troop.resize(n); base.resize(m); for (int i = 0; i < n; ++i) { int t1, t2, t3; scanf("%d%d%d", &t1, &t2, &t3); troop[i] = make_pair(t3, make_pair(t1, t2)); } for (int i = 0; i < m; ++i) { int t1, t2; scanf("%d%d", &t1, &t2); base[i] = make_pair(t1, t2); } double lb = 0, ub = 1e6; for (int step = 0; step < 10000; ++step) { double mid = (lb + ub) / 2; vector<vector<int>> xg(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int dx = troop[i].second.first - base[j].first; int dy = troop[i].second.second - base[j].second; int speed = troop[i].first; int d2 = dx * dx + dy * dy; if (d2 <= speed * speed * mid * mid) xg[i].push_back(j); } } if (max_matching(xg, m) == m) ub = mid; else lb = mid; } double ans = lb; printf("%.8lf\n", ans); } return 0; }
#include <cstdio> #include <vector> using namespace std; bool match(int x, const vector<vector<int>> &xg, vector<bool> &visited, vector<int> &p) { if (x < 0) return true; if (visited[x]) return false; visited[x] = true; for (int i = 0; i < xg[x].size(); ++i) { int y = xg[x][i]; if (match(p[y], xg, visited, p)) { p[y] = x; return true; } } return false; } int max_matching(const vector<vector<int>> &xg, const int ys) { vector<int> p(ys, -1); vector<bool> visited(xg.size()); int count = 0; for (int x = 0; x < xg.size(); ++x) { fill(visited.begin(), visited.end(), false); if (match(x, xg, visited, p)) count += 1; } return count; } int main() { while (true) { int n, m; vector<pair<int, pair<int, int>>> troop; vector<pair<int, int>> base; scanf("%d%d", &n, &m); if (n == 0) break; troop.resize(n); base.resize(m); for (int i = 0; i < n; ++i) { int t1, t2, t3; scanf("%d%d%d", &t1, &t2, &t3); troop[i] = make_pair(t3, make_pair(t1, t2)); } for (int i = 0; i < m; ++i) { int t1, t2; scanf("%d%d", &t1, &t2); base[i] = make_pair(t1, t2); } double lb = 0, ub = 1e6; for (int step = 0; step < 100; ++step) { double mid = (lb + ub) / 2; vector<vector<int>> xg(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int dx = troop[i].second.first - base[j].first; int dy = troop[i].second.second - base[j].second; int speed = troop[i].first; int d2 = dx * dx + dy * dy; if (d2 <= speed * speed * mid * mid) xg[i].push_back(j); } } if (max_matching(xg, m) == m) ub = mid; else lb = mid; } double ans = lb; printf("%.8lf\n", ans); } return 0; }
replace
59
60
59
60
TLE
p01281
C++
Runtime Error
#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<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef pair<int, int> PII; typedef complex<double> P; #define FOR(i, b, e) for (typeof(e) 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 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) (assert(n < 64), (1ULL << (n))) #define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1)) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #define PQ priority_queue #define SC static_cast #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 functions */ 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()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } /* Tweaks */ template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } /* Frequent stuffs */ int n_dir = 4; int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */ enum direction { UP, RIGHT, DOWN, LEFT }; // int n_dir = 8; // int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1}; // enum direction { // UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT // } #define FORDIR(d) REP(d, n_dir) /*}}}*/ enum state { EMPTY, HL, HR, VT, VB }; vector<vector<state>> field; int H, W; int rec(int i, int j) { if (i == H) { return 1; } if (j >= W) { return rec(i + 1, 0); } if (field[i][j] != EMPTY) { return rec(i, j + 1); } REP(y, H) { REP(x, W) { cerr << field[y][x] << " "; } cerr << endl; } int ret = 0; // horizontal if (i - 1 < 0 || j - 1 < 0 || field[i - 1][j - 1] == HL || field[i - 1][j - 1] == VT) { if (i - 1 < 0 || j + 2 == W || field[i - 1][j + 2] == HR || field[i - 1][j + 2] == VT) { if (j + 1 < W && field[i][j + 1] == EMPTY) { field[i][j] = HL, field[i][j + 1] = HR; ret += rec(i, j + 1); field[i][j] = EMPTY, field[i][j + 1] = EMPTY; } } } // vertical if (i - 1 < 0 || j - 1 < 0 || field[i - 1][j - 1] == HL || field[i - 1][j - 1] == VT) { if (i - 1 < 0 || j + 1 == W || field[i - 1][j + 1] == HR || field[i - 1][j + 1] == VT) { if (i + 1 < H) { field[i][j] = VT, field[i + 1][j] = VB; ret += rec(i, j + 1); field[i][j] = EMPTY, field[i + 1][j] = EMPTY; } } } return ret; } int main() { std::ios_base::sync_with_stdio(false); while (cin >> H >> W, H | W) { field = vector<vector<state>>(H, vector<state>(W, EMPTY)); cout << rec(0, 0) << endl; } } // vim: foldmethod=marker
#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<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef pair<int, int> PII; typedef complex<double> P; #define FOR(i, b, e) for (typeof(e) 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 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) (assert(n < 64), (1ULL << (n))) #define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1)) #define RANGE(a, b, c) ((a) <= (b) && (b) <= (c)) #define PQ priority_queue #define SC static_cast #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 functions */ 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()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } /* Tweaks */ template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } /* Frequent stuffs */ int n_dir = 4; int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */ enum direction { UP, RIGHT, DOWN, LEFT }; // int n_dir = 8; // int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1}; // enum direction { // UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT // } #define FORDIR(d) REP(d, n_dir) /*}}}*/ enum state { EMPTY, HL, HR, VT, VB }; vector<vector<state>> field; int H, W; int rec(int i, int j) { if (i == H) { return 1; } if (j >= W) { return rec(i + 1, 0); } if (field[i][j] != EMPTY) { return rec(i, j + 1); } // REP (y, H) { // REP (x, W) { // cerr << field[y][x] << " "; // } // cerr << endl; // } int ret = 0; // horizontal if (i - 1 < 0 || j - 1 < 0 || field[i - 1][j - 1] == HL || field[i - 1][j - 1] == VT) { if (i - 1 < 0 || j + 2 == W || field[i - 1][j + 2] == HR || field[i - 1][j + 2] == VT) { if (j + 1 < W && field[i][j + 1] == EMPTY) { field[i][j] = HL, field[i][j + 1] = HR; ret += rec(i, j + 1); field[i][j] = EMPTY, field[i][j + 1] = EMPTY; } } } // vertical if (i - 1 < 0 || j - 1 < 0 || field[i - 1][j - 1] == HL || field[i - 1][j - 1] == VT) { if (i - 1 < 0 || j + 1 == W || field[i - 1][j + 1] == HR || field[i - 1][j + 1] == VT) { if (i + 1 < H) { field[i][j] = VT, field[i + 1][j] = VB; ret += rec(i, j + 1); field[i][j] = EMPTY, field[i + 1][j] = EMPTY; } } } return ret; } int main() { std::ios_base::sync_with_stdio(false); while (cin >> H >> W, H | W) { field = vector<vector<state>>(H, vector<state>(W, EMPTY)); cout << rec(0, 0) << endl; } } // vim: foldmethod=marker
replace
234
238
234
240
0
0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 1 2 1 2 0 0 0 0 0 0 0 0 1 2 1 2 3 0 0 0 4 0 0 0 1 2 1 2 3 1 2 0 4 0 0 0 1 2 1 2 3 1 2 3 4 0 0 4 1 2 3 0 0 0 4 0 0 0 0 0 1 2 3 3 0 0 4 4 0 0 0 0 1 2 3 3 1 2 4 4 0 0 0 0 1 2 3 3 3 0 4 4 4 0 0 0 1 2 3 3 3 3 4 4 4 4 0 0 3 0 0 0 4 0 0 0 0 0 0 0 3 1 2 0 4 0 0 0 0 0 0 0 3 1 2 3 4 0 0 4 0 0 0 0 3 1 2 3 4 1 2 4 0 0 0 0 3 1 2 3 4 1 2 4 1 2 0 0 3 1 2 3 4 3 0 4 0 4 0 0 3 1 2 3 4 3 3 4 0 4 4 0 3 3 0 0 4 4 0 0 0 0 0 0 3 3 1 2 4 4 0 0 0 0 0 0 3 3 1 2 4 4 1 2 0 0 0 0 3 3 1 2 4 4 3 0 0 0 4 0 3 3 1 2 4 4 3 3 0 0 4 4 3 3 3 0 4 4 4 0 0 0 0 0 3 3 3 3 4 4 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 2 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 2 3 0 0 0 4 0 0 0 0 0 0 0 1 2 1 2 3 1 2 0 4 0 0 0 0 0 0 0 1 2 1 2 3 1 2 3 4 0 0 4 0 0 0 0 1 2 1 2 3 1 2 3 4 1 2 4 0 0 0 0 1 2 1 2 3 1 2 3 4 1 2 4 1 2 0 0 1 2 1 2 3 1 2 3 4 3 0 4 0 4 0 0 1 2 1 2 3 1 2 3 4 3 3 4 0 4 4 0 1 2 3 0 0 0 4 0 0 0 0 0 0 0 0 0 1 2 3 3 0 0 4 4 0 0 0 0 0 0 0 0 1 2 3 3 1 2 4 4 0 0 0 0 0 0 0 0 1 2 3 3 1 2 4 4 3 0 0 0 4 0 0 0 1 2 3 3 3 0 4 4 4 0 0 0 0 0 0 0 1 2 3 3 3 3 4 4 4 4 0 0 0 0 0 0 1 2 3 3 3 3 4 4 4 4 1 2 0 0 0 0 3 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 3 1 2 0 4 0 0 0 0 0 0 0 0 0 0 0 3 1 2 3 4 0 0 4 0 0 0 0 0 0 0 0 3 1 2 3 4 1 2 4 0 0 0 0 0 0 0 0 3 1 2 3 4 1 2 4 1 2 0 0 0 0 0 0 3 1 2 3 4 1 2 4 1 2 1 2 0 0 0 0 3 1 2 3 4 3 0 4 0 4 0 0 0 0 0 0 3 1 2 3 4 3 3 4 0 4 4 0 0 0 0 0 3 1 2 3 4 3 3 4 3 4 4 0 4 0 0 0 3 1 2 3 4 3 3 4 3 4 4 3 4 0 0 4 3 3 0 0 4 4 0 0 0 0 0 0 0 0 0 0 3 3 1 2 4 4 0 0 0 0 0 0 0 0 0 0 3 3 1 2 4 4 1 2 0 0 0 0 0 0 0 0 3 3 1 2 4 4 3 0 0 0 4 0 0 0 0 0 3 3 1 2 4 4 3 3 0 0 4 4 0 0 0 0 3 3 1 2 4 4 3 3 1 2 4 4 0 0 0 0 3 3 3 0 4 4 4 0 0 0 0 0 0 0 0 0 3 3 3 3 4 4 4 4 0 0 0 0 0 0 0 0
p01283
C++
Time Limit Exceeded
#include <cmath> #include <iostream> using namespace std; int n, a[256]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> a[i]; int mi, mj, mk; long double v = 1e+300L; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { int b[257] = {0}, c[257] = {0}; b[0] = i; for (int l = 1; l <= n; l++) b[l] = (j * b[l - 1] + k) % 256; for (int l = 0; l < n; l++) c[(a[l] + b[l + 1]) % 256]++; long double res = 0; for (int l = 0; l < 256; l++) { if (c[l]) res -= c[l] * log(1.0L * c[l] / n); } if (v - 1e-13L > res) v = res, mi = i, mj = j, mk = k; } } } cout << mi << ' ' << mj << ' ' << mk << endl; } }
#include <cmath> #include <iostream> using namespace std; int n, a[256]; int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) cin >> a[i]; int mi, mj, mk; long double v = 1e+300L; for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { for (int k = 0; k < 16; k++) { int b[257] = {0}, c[257] = {0}; b[0] = i; for (int l = 1; l <= n; l++) b[l] = (j * b[l - 1] + k) % 256; for (int l = 0; l < n; l++) c[(a[l] + b[l + 1]) % 256]++; long double res = 0; for (int l = 0; l < 256; l++) { if (c[l]) res -= c[l] * log(1.0L * c[l] / n); } if (v - 1e-13L > res) v = res, mi = i, mj = j, mk = k; } } } cout << mi << ' ' << mj << ' ' << mk << endl; } }
replace
10
13
10
13
TLE
p01284
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; const int My_Inf = 2147483647; const long long int My_LInf = 9223372036854775807; int main() { while (1) { int T; cin >> T; if (!T) break; vector<int> sleeps; for (int i = 0; i < T; ++i) { int t; cin >> t; sleeps.push_back(t); } int N; cin >> N; vector<int> needtimes(100, 25); for (int i = 0; i < N; ++i) { int d, m; cin >> d >> m; d--; needtimes[d] = min(needtimes[d], m); } vector<vector<int>> dp(100 + 1, vector<int>(T, 999)); dp[0][0] = 0; for (int i = 0; i < 100; ++i) { for (int j = 0; j < T; ++j) { if (needtimes[i] >= sleeps[j]) { dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]); } dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1); } } int ans = 99999999; for (int i = 0; i < T; ++i) { ans = min(dp[101][i], ans); } cout << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #pragma warning(disable : 4996) using namespace std; const int My_Inf = 2147483647; const long long int My_LInf = 9223372036854775807; int main() { while (1) { int T; cin >> T; if (!T) break; vector<int> sleeps; for (int i = 0; i < T; ++i) { int t; cin >> t; sleeps.push_back(t); } int N; cin >> N; vector<int> needtimes(100, 25); for (int i = 0; i < N; ++i) { int d, m; cin >> d >> m; d--; needtimes[d] = min(needtimes[d], m); } vector<vector<int>> dp(100 + 1, vector<int>(T, 999)); dp[0][0] = 0; for (int i = 0; i < 100; ++i) { for (int j = 0; j < T; ++j) { if (needtimes[i] >= sleeps[j]) { dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]); } dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1); } } int ans = 99999999; for (int i = 0; i < T; ++i) { ans = min(dp[100][i], ans); } cout << ans << endl; } return 0; }
replace
42
43
42
43
-11
p01285
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define EPS (1e-6) #define equals(a, b) (fabs((a) - (b)) < EPS) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } Point operator*(Point a) { return Point(x * a.x - y * a.y, x * a.y + y * a.x); } bool operator<(const Point &p) const { return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y); } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; struct Segment { Point p1, p2; int index; Segment(Point p1 = Point(), Point p2 = Point(), int index = -1) : p1(p1), p2(p2), index(index) {} bool operator<(const Segment &s) const { return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2; } bool operator==(const Segment &s) const { return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1); } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream &operator<<(ostream &os, const Point &a) { os << "(" << a.x << "," << a.y << ")"; } ostream &operator<<(ostream &os, const Segment &a) { os << "( " << a.p1 << " , " << a.p2 << " )"; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } double abs(Point a) { return sqrt(norm(a)); } // rad ??????§?????????????????¢???????§???????????????????¨ Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } // ????????????????¢???????????? double toRad(double agl) { return agl * M_PI / 180.0; } // a => prev, b => cur, c=> next // prev ?????? cur ??????????£??? next ??????????????????§??????????±??????? double getArg(Point a, Point b, Point c) { double arg1 = atan2(b.y - a.y, b.x - a.x); double arg2 = atan2(c.y - b.y, c.x - b.x); double arg = fabs(arg1 - arg2); while (arg > M_PI) arg -= 2.0 * M_PI; return fabs(arg); } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point 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; } bool intersectLL(Line l, Line m) { return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line } bool intersectLS(Line l, Line s) { return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l cross(l.p2 - l.p1, s.p2 - l.p1) < EPS; // s[1] is right of l } bool intersectLP(Line l, Point p) { return abs(cross(l.p2 - p, l.p1 - p)) < EPS; } bool intersectSS(Line s, Line t) { return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 && ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0; } bool intersectSP(Line s, Point p) { return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) < EPS; // triangle inequality } Point projection(Line l, Point p) { double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2); return l.p1 + (l.p1 - l.p2) * t; } Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; } double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); } double distanceLL(Line l, Line m) { return intersectLL(l, m) ? 0 : distanceLP(l, m.p1); } double distanceLS(Line l, Line s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Line s, Point p) { Point r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s.p1 - p), abs(s.p2 - p)); } double distanceSS(Line s, Line t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)), min(distanceSP(t, s.p1), distanceSP(t, s.p2))); } Point crosspoint(Line l, Line m) { double A = cross(l.p2 - l.p1, m.p2 - m.p1); double B = cross(l.p2 - l.p1, l.p2 - m.p1); if (abs(A) < EPS && abs(B) < EPS) { vector<Point> vec; vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1), vec.push_back(m.p2); sort(vec.begin(), vec.end()); assert(vec[1] == vec[2]); //?????????????°?????????????????? return vec[1]; // return m.p1; } if (abs(A) < EPS) assert(false); return m.p1 + (m.p2 - m.p1) * (B / A); } // cross product of pq and pr double cross3p(Point p, Point q, Point r) { return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x); } // returns true if point r is on the same line as the line pq bool collinear(Point p, Point q, Point r) { return fabs(cross3p(p, q, r)) < EPS; } // returns true if point t is on the left side of line pq bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; // can be modified to accept collinear points } bool onSegment(Point p, Point q, Point r) { return collinear(p, q, r) && equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) + sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)), sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2))); } // ------------------ double heron(Point A, Point B, Point C) { double a = abs(B - C); double b = abs(A - C); double c = abs(A - B); double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } Line calcLine(Line line1, Line line2, Point p1, Point p2) { Point cp = crosspoint(line1, line2); double S = heron(p1, cp, p2); double a = abs(p1 - cp); double b = abs(p2 - cp); double arg_a = asin((2.0 * S) / (a * b)); if (equals(2 * S, a * b)) arg_a = toRad(90); // arg_a = getArg(p1,cp,p2); assert(!(cp == p1 || cp == p2 || p1 == p2)); int res = ccw(cp, p1, p2); // assert( ( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); // while( !( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); Point base; if (res == COUNTER_CLOCKWISE) base = p1; else base = p2; Point not_base = (base == p1) ? p2 : p1; // cout << base << " -> " << cp << " -> " << not_base << endl; arg_a = (toRad(180.0) - getArg(base, cp, not_base)); // puts("^^^^^"); // cout << "base = " << base << " | " << (arg_a*180/M_PI)<< endl; // cout << line1 << " and " << line2 << endl; // cout << cp << " | " << p1 << " | " << p2 << endl; Vector e = (base - cp) / abs(base - cp); e = rotate(e, arg_a / 2.0); Line tmp = Line(cp, cp + e * 100); // cout << "return = " << tmp << endl; // puts("_____"); return tmp; } const string MANY = "Many"; const string NONE = "None"; #define all(x) (x.begin(), x.end()) void compute(vector<Line> &vec) { if (vec.size() <= 2) { cout << MANY << endl; return; } vector<Line> candidateLines; int n = vec.size(); rep(i, n) REP(j, i + 1, n) { if (equals(cross(vec[i].p1 - vec[i].p2, vec[j].p1 - vec[j].p2), 0.0)) { Vector e = (vec[i].p2 - vec[i].p1) / abs(vec[i].p2 - vec[i].p1); e = rotate(e, toRad(90)); Line line = Line(vec[i].p1, vec[i].p1 + e * 100); Point cp1 = crosspoint(line, vec[i]); Point cp2 = crosspoint(line, vec[j]); Point mp = (cp1 + cp2) / 2.0; e = (vec[i].p2 - vec[i].p1) / abs(vec[i].p2 - vec[i].p1); line = Line(mp, mp + e * 100); line.index = candidateLines.size(); candidateLines.push_back(line); } else { // cout << vec[i] << " x " << vec[j] << endl; Point cp = crosspoint(vec[i], vec[j]); Point I = (vec[i].p1 == cp) ? vec[i].p2 : vec[i].p1; Point J = (vec[j].p1 == cp) ? vec[j].p2 : vec[j].p1; Vector e1 = (I - cp) / abs(I - cp); Vector e2 = (J - cp) / abs(J - cp); Line tmp = calcLine(vec[i], vec[j], cp + e1 * 100, cp + e2 * 100); // cout << "tmp = " << tmp << endl; int Index = candidateLines.size(); tmp.index = Index; candidateLines.push_back(tmp); tmp = calcLine(vec[i], vec[j], cp + e1 * 100, cp - e2 * 100); // cout << "tmp = " << tmp << endl; tmp.index = Index; candidateLines.push_back(tmp); } if (candidateLines.size() >= 50) break; } vector<Point> candidatePoints; rep(i, candidateLines.size()) REP(j, i + 1, candidateLines.size()) { Line line1 = candidateLines[i]; Line line2 = candidateLines[j]; if (equals(cross(line1.p1 - line1.p2, line2.p1 - line2.p2), 0.0)) continue; Point cp = crosspoint(line1, line2); candidatePoints.push_back(cp); } vector<Point> &v = candidatePoints; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); vector<Point> answer; rep(i, candidatePoints.size()) { Point p = candidatePoints[i]; // puts(""); // cout << "p = " << p << endl; double dist = -1; bool success = true; rep(j, vec.size()) { double tmp = distanceLP(vec[j], p); if (equals(dist, -1)) dist = tmp; else if (!equals(dist, tmp)) { success = false; /*break;*/ } // cout << dist << " ?? " << tmp << endl; } // cout << "success ?= " << success << endl; if (success) answer.push_back(p); if (answer.size() >= 2) break; } if (answer.size() == 1) printf("%.10f %.10f\n", answer[0].x, answer[0].y); else if (answer.empty()) cout << NONE << endl; else cout << MANY << endl; } int main() { /* Point p = Point(0,1); cout << rotate(p,toRad(90)) << endl; */ int n; while (cin >> n, n) { vector<Line> vec(n); rep(i, n) cin >> vec[i].p1.x >> vec[i].p1.y >> vec[i].p2.x >> vec[i].p2.y; compute(vec); } return 0; }
#include <bits/stdc++.h> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define EPS (1e-6) #define equals(a, b) (fabs((a) - (b)) < EPS) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(a * x, a * y); } Point operator/(double a) { return Point(x / a, y / a); } Point operator*(Point a) { return Point(x * a.x - y * a.y, x * a.y + y * a.x); } bool operator<(const Point &p) const { return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y); } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; struct Segment { Point p1, p2; int index; Segment(Point p1 = Point(), Point p2 = Point(), int index = -1) : p1(p1), p2(p2), index(index) {} bool operator<(const Segment &s) const { return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2; } bool operator==(const Segment &s) const { return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1); } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream &operator<<(ostream &os, const Point &a) { os << "(" << a.x << "," << a.y << ")"; } ostream &operator<<(ostream &os, const Segment &a) { os << "( " << a.p1 << " , " << a.p2 << " )"; } double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double norm(Point a) { return a.x * a.x + a.y * a.y; } double abs(Point a) { return sqrt(norm(a)); } // rad ??????§?????????????????¢???????§???????????????????¨ Point rotate(Point a, double rad) { return Point(cos(rad) * a.x - sin(rad) * a.y, sin(rad) * a.x + cos(rad) * a.y); } // ????????????????¢???????????? double toRad(double agl) { return agl * M_PI / 180.0; } // a => prev, b => cur, c=> next // prev ?????? cur ??????????£??? next ??????????????????§??????????±??????? double getArg(Point a, Point b, Point c) { double arg1 = atan2(b.y - a.y, b.x - a.x); double arg2 = atan2(c.y - b.y, c.x - b.x); double arg = fabs(arg1 - arg2); while (arg > M_PI) arg -= 2.0 * M_PI; return fabs(arg); } int ccw(Point p0, Point p1, Point p2) { Point a = p1 - p0; Point 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; } bool intersectLL(Line l, Line m) { return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line } bool intersectLS(Line l, Line s) { return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l cross(l.p2 - l.p1, s.p2 - l.p1) < EPS; // s[1] is right of l } bool intersectLP(Line l, Point p) { return abs(cross(l.p2 - p, l.p1 - p)) < EPS; } bool intersectSS(Line s, Line t) { return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 && ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0; } bool intersectSP(Line s, Point p) { return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) < EPS; // triangle inequality } Point projection(Line l, Point p) { double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2); return l.p1 + (l.p1 - l.p2) * t; } Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; } double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); } double distanceLL(Line l, Line m) { return intersectLL(l, m) ? 0 : distanceLP(l, m.p1); } double distanceLS(Line l, Line s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Line s, Point p) { Point r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s.p1 - p), abs(s.p2 - p)); } double distanceSS(Line s, Line t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)), min(distanceSP(t, s.p1), distanceSP(t, s.p2))); } Point crosspoint(Line l, Line m) { double A = cross(l.p2 - l.p1, m.p2 - m.p1); double B = cross(l.p2 - l.p1, l.p2 - m.p1); if (abs(A) < EPS && abs(B) < EPS) { vector<Point> vec; vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1), vec.push_back(m.p2); sort(vec.begin(), vec.end()); assert(vec[1] == vec[2]); //?????????????°?????????????????? return vec[1]; // return m.p1; } if (abs(A) < EPS) assert(false); return m.p1 + (m.p2 - m.p1) * (B / A); } // cross product of pq and pr double cross3p(Point p, Point q, Point r) { return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x); } // returns true if point r is on the same line as the line pq bool collinear(Point p, Point q, Point r) { return fabs(cross3p(p, q, r)) < EPS; } // returns true if point t is on the left side of line pq bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; // can be modified to accept collinear points } bool onSegment(Point p, Point q, Point r) { return collinear(p, q, r) && equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) + sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)), sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2))); } // ------------------ double heron(Point A, Point B, Point C) { double a = abs(B - C); double b = abs(A - C); double c = abs(A - B); double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } Line calcLine(Line line1, Line line2, Point p1, Point p2) { Point cp = crosspoint(line1, line2); double S = heron(p1, cp, p2); double a = abs(p1 - cp); double b = abs(p2 - cp); double arg_a = asin((2.0 * S) / (a * b)); if (equals(2 * S, a * b)) arg_a = toRad(90); // arg_a = getArg(p1,cp,p2); // assert( !( cp == p1 || cp == p2 || p1 == p2 ) ); int res = ccw(cp, p1, p2); // assert( ( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); // while( !( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); Point base; if (res == COUNTER_CLOCKWISE) base = p1; else base = p2; Point not_base = (base == p1) ? p2 : p1; // cout << base << " -> " << cp << " -> " << not_base << endl; arg_a = (toRad(180.0) - getArg(base, cp, not_base)); // puts("^^^^^"); // cout << "base = " << base << " | " << (arg_a*180/M_PI)<< endl; // cout << line1 << " and " << line2 << endl; // cout << cp << " | " << p1 << " | " << p2 << endl; Vector e = (base - cp) / abs(base - cp); e = rotate(e, arg_a / 2.0); Line tmp = Line(cp, cp + e * 100); // cout << "return = " << tmp << endl; // puts("_____"); return tmp; } const string MANY = "Many"; const string NONE = "None"; #define all(x) (x.begin(), x.end()) void compute(vector<Line> &vec) { if (vec.size() <= 2) { cout << MANY << endl; return; } vector<Line> candidateLines; int n = vec.size(); rep(i, n) REP(j, i + 1, n) { if (equals(cross(vec[i].p1 - vec[i].p2, vec[j].p1 - vec[j].p2), 0.0)) { Vector e = (vec[i].p2 - vec[i].p1) / abs(vec[i].p2 - vec[i].p1); e = rotate(e, toRad(90)); Line line = Line(vec[i].p1, vec[i].p1 + e * 100); Point cp1 = crosspoint(line, vec[i]); Point cp2 = crosspoint(line, vec[j]); Point mp = (cp1 + cp2) / 2.0; e = (vec[i].p2 - vec[i].p1) / abs(vec[i].p2 - vec[i].p1); line = Line(mp, mp + e * 100); line.index = candidateLines.size(); candidateLines.push_back(line); } else { // cout << vec[i] << " x " << vec[j] << endl; Point cp = crosspoint(vec[i], vec[j]); Point I = (vec[i].p1 == cp) ? vec[i].p2 : vec[i].p1; Point J = (vec[j].p1 == cp) ? vec[j].p2 : vec[j].p1; Vector e1 = (I - cp) / abs(I - cp); Vector e2 = (J - cp) / abs(J - cp); Line tmp = calcLine(vec[i], vec[j], cp + e1 * 100, cp + e2 * 100); // cout << "tmp = " << tmp << endl; int Index = candidateLines.size(); tmp.index = Index; candidateLines.push_back(tmp); tmp = calcLine(vec[i], vec[j], cp + e1 * 100, cp - e2 * 100); // cout << "tmp = " << tmp << endl; tmp.index = Index; candidateLines.push_back(tmp); } if (candidateLines.size() >= 50) break; } vector<Point> candidatePoints; rep(i, candidateLines.size()) REP(j, i + 1, candidateLines.size()) { Line line1 = candidateLines[i]; Line line2 = candidateLines[j]; if (equals(cross(line1.p1 - line1.p2, line2.p1 - line2.p2), 0.0)) continue; Point cp = crosspoint(line1, line2); candidatePoints.push_back(cp); } vector<Point> &v = candidatePoints; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); vector<Point> answer; rep(i, candidatePoints.size()) { Point p = candidatePoints[i]; // puts(""); // cout << "p = " << p << endl; double dist = -1; bool success = true; rep(j, vec.size()) { double tmp = distanceLP(vec[j], p); if (equals(dist, -1)) dist = tmp; else if (!equals(dist, tmp)) { success = false; /*break;*/ } // cout << dist << " ?? " << tmp << endl; } // cout << "success ?= " << success << endl; if (success) answer.push_back(p); if (answer.size() >= 2) break; } if (answer.size() == 1) printf("%.10f %.10f\n", answer[0].x, answer[0].y); else if (answer.empty()) cout << NONE << endl; else cout << MANY << endl; } int main() { /* Point p = Point(0,1); cout << rotate(p,toRad(90)) << endl; */ int n; while (cin >> n, n) { vector<Line> vec(n); rep(i, n) cin >> vec[i].p1.x >> vec[i].p1.y >> vec[i].p2.x >> vec[i].p2.y; compute(vec); } return 0; }
replace
213
214
213
214
0
p01287
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define dump(x) #endif typedef long long int ll; typedef pair<int, int> pii; // template<typename T> using vec=std::vector<T>; const int inf = 1 << 30; const long long int infll = 1LL << 58; const double eps = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { const int idx1[] = {0, 4, 5, 1, 3, 7, 6, 2}; const int idx2[] = {0, 3, 7, 4, 1, 2, 6, 5}; auto equal = [&](const vector<int> &x, const vector<int> &y) { auto equal_ = [](vector<int> a, vector<int> b) { auto equal__ = [&] { rep(i, 0, 4) { bool ok = true; rep(j, 0, 4) if (a[j] != b[(i + j) % 4] or a[4 + j] != b[(4 + i + j >= 8 ? i + j : 4 + i + j)]) { ok = false; break; } if (ok) return true; } return false; }; if (equal__()) return true; reverse(b.begin(), b.end()); return equal__(); }; if (equal_(x, y)) return true; vector<int> tmp(8); rep(i, 0, 8) tmp[i] = y[idx1[i]]; if (equal_(x, tmp)) return true; rep(i, 0, 8) tmp[i] = y[idx2[i]]; return equal_(x, tmp); }; string tmp; while (cin >> tmp) { vector<string> color(8); color[0] = tmp; rep(i, 1, 8) cin >> color[i]; map<string, int> mp; int cnt = 0; rep(i, 0, 8) if (mp.find(color[i]) == mp.end()) { mp[color[i]] = cnt; ++cnt; } vector<int> v(8); rep(i, 0, 8) v[i] = mp[color[i]]; sort(v.begin(), v.end()); vector<vector<int>> all; do { bool ok = true; for (const auto &a : all) if (equal(v, a)) { ok = false; break; } if (ok) all.emplace_back(v); } while (next_permutation(v.begin(), v.end())); cout << all.size() << endl; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cout << fixed << setprecision(8); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define dump(x) #endif typedef long long int ll; typedef pair<int, int> pii; // template<typename T> using vec=std::vector<T>; const int inf = 1 << 30; const long long int infll = 1LL << 58; const double eps = 1e-9; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { const int idx1[] = {0, 4, 5, 1, 3, 7, 6, 2}; const int idx2[] = {0, 3, 7, 4, 1, 2, 6, 5}; auto equal = [&](const vector<int> &x, const vector<int> &y) { auto equal_ = [](const vector<int> &a, vector<int> b) { auto equal__ = [&] { rep(i, 0, 4) { bool ok = true; rep(j, 0, 4) if (a[j] != b[(i + j) % 4] or a[4 + j] != b[(4 + i + j >= 8 ? i + j : 4 + i + j)]) { ok = false; break; } if (ok) return true; } return false; }; if (equal__()) return true; reverse(b.begin(), b.end()); return equal__(); }; if (equal_(x, y)) return true; vector<int> tmp(8); rep(i, 0, 8) tmp[i] = y[idx1[i]]; if (equal_(x, tmp)) return true; rep(i, 0, 8) tmp[i] = y[idx2[i]]; return equal_(x, tmp); }; string tmp; while (cin >> tmp) { vector<string> color(8); color[0] = tmp; rep(i, 1, 8) cin >> color[i]; map<string, int> mp; int cnt = 0; rep(i, 0, 8) if (mp.find(color[i]) == mp.end()) { mp[color[i]] = cnt; ++cnt; } vector<int> v(8); rep(i, 0, 8) v[i] = mp[color[i]]; sort(v.begin(), v.end()); vector<vector<int>> all; do { bool ok = true; for (const auto &a : all) if (equal(v, a)) { ok = false; break; } if (ok) all.emplace_back(v); } while (next_permutation(v.begin(), v.end())); cout << all.size() << endl; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cout << fixed << setprecision(8); solve(); return 0; }
replace
35
36
35
36
TLE
p01288
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string.h> #include <string> #include <vector> // #pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-8; const double PI = acos(-1.0); typedef long long ll; const int maxn = 100010; int tree[maxn]; int Find(int x) { if (tree[x] == x) return x; return Find(tree[x]); } int main() { int n, m, id, i; char com[10]; ll ans; while (scanf("%d%d", &n, &m), n || m) { tree[1] = 1; ans = 0; for (i = 1; i < n; i++) scanf("%d", &tree[i]); for (i = 0; i < m; i++) { scanf("%s%d", com, &id); if (com[0] == 'M') tree[id] = id; else ans += Find(id); } printf("%lld\n", ans); } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string.h> #include <string> #include <vector> // #pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-8; const double PI = acos(-1.0); typedef long long ll; const int maxn = 100010; int tree[maxn]; int Find(int x) { if (tree[x] == x) return x; return Find(tree[x]); } int main() { int n, m, id, i; char com[10]; ll ans; while (scanf("%d%d", &n, &m), n || m) { tree[1] = 1; ans = 0; for (i = 2; i <= n; i++) scanf("%d", &tree[i]); for (i = 0; i < m; i++) { scanf("%s%d", com, &id); if (com[0] == 'M') tree[id] = id; else ans += Find(id); } printf("%lld\n", ans); } return 0; }
replace
32
33
32
33
TLE
p01288
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; 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 UnionFind { int parent[110000]; void unionSet(int x, int y) { x = root(x); y = root(y); if (x == y) { return; } parent[y] = x; } int findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); } }; int n, q; UnionFind ufind; int parent[110000]; bool mark[110000]; int query[11000]; int main() { while (scanf("%d %d", &n, &q), n | q) { memset(&ufind, -1, sizeof(ufind)); MEMSET(mark, false); parent[0] = 0; REP(i, n - 1) { int p; scanf("%d", &p); parent[i + 1] = p - 1; } int m = 0; REP(i, q) { char c; int v; scanf(" %c %d", &c, &v); if (c == 'Q') { query[m++] = v; } else if (c == 'M' && !mark[v - 1]) { mark[v - 1] = true; query[m++] = -v; } } REP(i, n) { if (mark[i]) { continue; } ufind.unionSet(parent[i], i); } reverse(query, query + m); ll ans = 0; REP(i, m) { int index = abs(query[i]) - 1; if (query[i] > 0) { ans += ufind.root(index) + 1; } else { ufind.unionSet(parent[index], index); } } printf("%lld\n", ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; 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 UnionFind { int parent[110000]; void unionSet(int x, int y) { x = root(x); y = root(y); if (x == y) { return; } parent[y] = x; } int findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); } }; int n, q; UnionFind ufind; int parent[110000]; bool mark[110000]; int query[110000]; int main() { while (scanf("%d %d", &n, &q), n | q) { memset(&ufind, -1, sizeof(ufind)); MEMSET(mark, false); parent[0] = 0; REP(i, n - 1) { int p; scanf("%d", &p); parent[i + 1] = p - 1; } int m = 0; REP(i, q) { char c; int v; scanf(" %c %d", &c, &v); if (c == 'Q') { query[m++] = v; } else if (c == 'M' && !mark[v - 1]) { mark[v - 1] = true; query[m++] = -v; } } REP(i, n) { if (mark[i]) { continue; } ufind.unionSet(parent[i], i); } reverse(query, query + m); ll ans = 0; REP(i, m) { int index = abs(query[i]) - 1; if (query[i] > 0) { ans += ufind.root(index) + 1; } else { ufind.unionSet(parent[index], index); } } printf("%lld\n", ans); } }
replace
40
41
40
41
0
p01288
C++
Time Limit Exceeded
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef pair<char, int> P; const int maxn = 100005; int tree[maxn]; int ar[maxn]; P query[maxn]; int get(int i) { int par = tree[i]; while (!ar[par]) { par = tree[par]; } return par; } int main() { int n, q; while (scanf("%d%d", &n, &q) && (q + n) != 0) { memset(ar, 0, sizeof(ar)); ar[1] = true; for (int i = 2; i <= n; i++) scanf("%d", &tree[i]); for (int i = 0; i < q; i++) scanf(" %c%d", &query[i].first, &query[i].second); long long sum = 0; for (int i = 0; i < q; i++) if (query[i].first == 'M') ar[query[i].second] += 1; for (int i = q - 1; i >= 0; i--) if (query[i].first == 'M') ar[query[i].second] -= 1; else sum += get(query[i].second); printf("%lld\n", sum); } return 0; }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef pair<char, int> P; const int maxn = 100005; int tree[maxn]; int ar[maxn]; P query[maxn]; int get(int i) { if (ar[i]) return i; return tree[i] = get(tree[i]); } int main() { int n, q; while (scanf("%d%d", &n, &q) && (q + n) != 0) { memset(ar, 0, sizeof(ar)); ar[1] = true; for (int i = 2; i <= n; i++) scanf("%d", &tree[i]); for (int i = 0; i < q; i++) scanf(" %c%d", &query[i].first, &query[i].second); long long sum = 0; for (int i = 0; i < q; i++) if (query[i].first == 'M') ar[query[i].second] += 1; for (int i = q - 1; i >= 0; i--) if (query[i].first == 'M') ar[query[i].second] -= 1; else sum += get(query[i].second); printf("%lld\n", sum); } return 0; }
replace
10
15
10
13
TLE
p01288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cout << (#v) << ":"; \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; #define printVS(vs) \ cout << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cout << (s) << endl; \ } #define printVV(vv) \ cout << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; \ } #define printP(p) cout << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cout << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cout << (p).first << " " << (p).second << endl; \ } inline void output() { cout << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cout << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using TUPLE = tuple<int, int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 1ll << 60; const int mod = 1e9 + 7; using Graph = vector<vector<int>>; int n, q; vi par; vector<char> t; vi v; vi cnt; int lma(int x) { if (cnt[x]) return x; return par[x] = lma(par[x]); } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); while (cin >> n >> q, n) { par.assign(n, -1); rep(i, n - 1) { int p; cin >> p; p--; par[i] = p; } t.clear(); t.resize(n); v.clear(); v.resize(n); cnt.assign(n, 0); cnt[0] = 1; rep(i, q) { cin >> t[i] >> v[i]; v[i]--; if (t[i] == 'M') { cnt[v[i]]++; } } int ans = 0; rrep(i, q) { if (t[i] == 'M') { cnt[v[i]]--; } else { ans += lma(v[i]) + 1; } } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--) #define chmin(a, b) (a) = min((a), (b)); #define chmax(a, b) (a) = max((a), (b)); #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define printV(v) \ cout << (#v) << ":"; \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; #define printVS(vs) \ cout << (#vs) << ":" << endl; \ for (auto(s) : (vs)) { \ cout << (s) << endl; \ } #define printVV(vv) \ cout << (#vv) << ":" << endl; \ for (auto(v) : (vv)) { \ for (auto(x) : (v)) { \ cout << " " << (x); \ } \ cout << endl; \ } #define printP(p) cout << (#p) << (p).first << " " << (p).second << endl; #define printVP(vp) \ cout << (#vp) << ":" << endl; \ for (auto(p) : (vp)) { \ cout << (p).first << " " << (p).second << endl; \ } inline void output() { cout << endl; } template <typename First, typename... Rest> inline void output(const First &first, const Rest &...rest) { cout << first << " "; output(rest...); } using ll = long long; using Pii = pair<int, int>; using TUPLE = tuple<int, int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; const int inf = 1ll << 60; const int mod = 1e9 + 7; using Graph = vector<vector<int>>; int n, q; vi par; vector<char> t; vi v; vi cnt; int lma(int x) { if (cnt[x]) return x; return par[x] = lma(par[x]); } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); while (cin >> n >> q, n) { par.assign(n, -1); rep2(i, 1, n) { int p; cin >> p; p--; par[i] = p; } t.clear(); t.resize(n); v.clear(); v.resize(n); cnt.assign(n, 0); cnt[0] = 1; rep(i, q) { cin >> t[i] >> v[i]; v[i]--; if (t[i] == 'M') { cnt[v[i]]++; } } int ans = 0; rrep(i, q) { if (t[i] == 'M') { cnt[v[i]]--; } else { ans += lma(v[i]) + 1; } } cout << ans << endl; } }
replace
73
74
73
74
0
p01288
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; int n, q, root[105000]; long long res; int marked[105000]; pair<char, int> query[105000]; int r(int x) { if (marked[x]) return x; return root[x] = r(root[x]); } int main() { while (true) { scanf("%d%d", &n, &q); // cin >> n >> q; if (n == 0 && q == 0) return 0; res = 0; fill(marked, marked + n + 2, 0); marked[1] = true; for (int i = 1; i < n; i++) { cin >> root[i]; root[i]--; } for (int i = 0; i < q; i++) { cin >> query[i].first >> query[i].second; query[i].second--; marked[query[i].second] += (query[i].first == 'M'); } for (int i = q; i-- > 0;) { if (query[i].first == 'M') { marked[query[i].second]--; } else { res += r(query[i].second) + 1; } } printf("%lld\n", res); // cout << res << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; int n, q, root[105000]; long long res; int marked[105000]; pair<char, int> query[105000]; int r(int x) { if (marked[x]) return x; return root[x] = r(root[x]); } int main() { while (true) { scanf("%d%d", &n, &q); // cin >> n >> q; if (n == 0 && q == 0) return 0; res = 0; fill(marked, marked + n + 2, 0); marked[0] = true; for (int i = 1; i < n; i++) { cin >> root[i]; root[i]--; } for (int i = 0; i < q; i++) { cin >> query[i].first >> query[i].second; query[i].second--; marked[query[i].second] += (query[i].first == 'M'); } for (int i = q; i-- > 0;) { if (query[i].first == 'M') { marked[query[i].second]--; } else { res += r(query[i].second) + 1; } } printf("%lld\n", res); // cout << res << endl; } }
replace
25
26
25
26
-11
p01288
C++
Runtime Error
#include <bits/stdc++.h> 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(); } template <class T> inline T sqr(T x) { return x * x; } 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 pb push_back #define mp make_pair #define each(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define exist(s, e) ((s).find(e) != (s).end()) #define range(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) range(i, 0, n) #define clr(a, b) memset((a), (b), sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF = 1LL << 62; const int inf = 1 << 29; const int nmax = 100010; const int qmax = 100010; vi trees[nmax]; int p[nmax]; int last[nmax]; void add_edge(int a, int b) { trees[a].pb(b); trees[b].pb(a); } int uf[nmax]; int find(int i) { if (uf[i] == i) return i; return uf[i] = find(uf[i]); } void unite(int p, int c) { uf[c] = p; return; } void dfs(int v, int p) { if (last[v] == -1) unite(p, v); for (auto &e : trees[v]) { if (e != p) dfs(e, v); } } char type[qmax]; int num[qmax]; int main(void) { int n, q; while (cin >> n >> q) { if (n == 0) break; for (int i = 1; i <= n; ++i) { trees[i].clear(); uf[i] = i; last[i] = -1; } for (int i = 2; i <= n; ++i) { cin >> p[i]; add_edge(p[i], i); } last[1] = -2; rep(i, q) { cin >> type[i] >> num[i]; if (type[i] == 'M' && last[num[i]] == -1) last[num[i]] = i; } dfs(1, -1); ll ans = 0; for (int i = q - 1; i >= 0; --i) { dump(i) dump(ans) if (type[i] == 'Q') ans += find(num[i]); else { if (last[num[i]] == i) unite(p[num[i]], num[i]); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> 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(); } template <class T> inline T sqr(T x) { return x * x; } 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 pb push_back #define mp make_pair #define each(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define exist(s, e) ((s).find(e) != (s).end()) #define range(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) range(i, 0, n) #define clr(a, b) memset((a), (b), sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF = 1LL << 62; const int inf = 1 << 29; const int nmax = 100010; const int qmax = 100010; vi trees[nmax]; int p[nmax]; int last[nmax]; void add_edge(int a, int b) { trees[a].pb(b); trees[b].pb(a); } int uf[nmax]; int find(int i) { if (uf[i] == i) return i; return uf[i] = find(uf[i]); } void unite(int p, int c) { uf[c] = p; return; } void dfs(int v, int p) { if (last[v] == -1) unite(p, v); for (auto &e : trees[v]) { if (e != p) dfs(e, v); } } char type[qmax]; int num[qmax]; int main(void) { int n, q; while (cin >> n >> q) { if (n == 0) break; for (int i = 1; i <= n; ++i) { trees[i].clear(); uf[i] = i; last[i] = -1; } for (int i = 2; i <= n; ++i) { cin >> p[i]; add_edge(p[i], i); } last[1] = -2; rep(i, q) { cin >> type[i] >> num[i]; if (type[i] == 'M' && last[num[i]] == -1) last[num[i]] = i; } dfs(1, -1); ll ans = 0; for (int i = q - 1; i >= 0; --i) { if (type[i] == 'Q') ans += find(num[i]); else { if (last[num[i]] == i) unite(p[num[i]], num[i]); } } cout << ans << endl; } return 0; }
replace
102
103
102
104
0
i = 2 ans = 0 i = 1 ans = 3 i = 0 ans = 3
p01288
C++
Runtime Error
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define FI first #define SE second #define int long long using namespace std; static const int MAX_N = 100000; static const int inf = 1ll << 60; static const int MAX_SEG = 1 << 17; typedef pair<int, int> pii; int dat[2 * MAX_SEG]; class segment { public: int n; void init(int n_) { for (int i = 0; i < 2 * MAX_SEG - 1; ++i) dat[i] = 0; } void update(int a, int b, int x, int k = 0, int l = 0, int r = MAX_SEG) { if (r <= a || b <= l) return; if (a <= l && r <= b) { dat[k] = max(dat[k], x); return; } update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); } int query(int k) { k += MAX_SEG - 1; int ret = dat[k]; while (k) { k = (k - 1) / 2; ret = max(ret, dat[k]); } return ret; } }; int N, M; vector<int> g[MAX_N + 5]; int depth[MAX_N + 5]; int par[20][MAX_N]; int tin[2 * MAX_N + 5], tout[2 * MAX_N + 5]; int k = 0; void dfs(int v, int p, int d) { par[0][v] = p; depth[v] = d; tin[v] = k++; for (int u = 0; u < g[v].size(); ++u) { dfs(g[v][u], v, d + 1); k++; } tout[v] = k; } void fill_table() { for (int i = 0; i < 19; i++) { for (int j = 0; j < N; j++) { if (par[i][j] == -1) par[i + 1][j] = -1; else par[i + 1][j] = par[i][par[i][j]]; } } } signed main() { while (1) { int ans = 0; cin >> N >> M; if (N == 0 && M == 0) break; for (int i = 0; i < N; ++i) g[i].clear(); memset(par, 0, sizeof(par)); for (int i = 1; i < N; ++i) { int p; cin >> p; p--; g[p].PB(i); } k = 0; dfs(0, -1, 0); fill_table(); segment seg; seg.init(N); for (int t = 0; t < M; ++t) { char que; int v; cin >> que; cin >> v; v--; // cout<<tin[v]<<" "<<tout[v]<<" "<<depth[v]<<endl; if (que == 'M') { seg.update(tin[v], tout[v], depth[v]); } else { int q = seg.query(tin[v]); for (int i = 19; i >= 0; --i) { if ((depth[v] - q) >> i & 1) v = par[i][v]; } ans += v + 1; } } cout << ans << endl; } }
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define FI first #define SE second #define int long long using namespace std; static const int MAX_N = 100000; static const int inf = 1ll << 60; static const int MAX_SEG = 1 << 17; typedef pair<int, int> pii; int dat[2 * MAX_SEG]; class segment { public: int n; void init(int n_) { for (int i = 0; i < 2 * MAX_SEG - 1; ++i) dat[i] = 0; } void update(int a, int b, int x, int k = 0, int l = 0, int r = MAX_SEG) { if (r <= a || b <= l) return; if (a <= l && r <= b) { dat[k] = max(dat[k], x); return; } update(a, b, x, k * 2 + 1, l, (l + r) / 2); update(a, b, x, k * 2 + 2, (l + r) / 2, r); } int query(int k) { k += MAX_SEG - 1; int ret = dat[k]; while (k) { k = (k - 1) / 2; ret = max(ret, dat[k]); } return ret; } }; int N, M; vector<int> g[MAX_N + 5]; int depth[MAX_N + 5]; int par[20][MAX_N]; int tin[2 * MAX_N + 5], tout[2 * MAX_N + 5]; int k = 0; void dfs(int v, int p, int d) { par[0][v] = p; depth[v] = d; tin[v] = k++; for (int u = 0; u < g[v].size(); ++u) { dfs(g[v][u], v, d + 1); // k++; } tout[v] = k; } void fill_table() { for (int i = 0; i < 19; i++) { for (int j = 0; j < N; j++) { if (par[i][j] == -1) par[i + 1][j] = -1; else par[i + 1][j] = par[i][par[i][j]]; } } } signed main() { while (1) { int ans = 0; cin >> N >> M; if (N == 0 && M == 0) break; for (int i = 0; i < N; ++i) g[i].clear(); memset(par, 0, sizeof(par)); for (int i = 1; i < N; ++i) { int p; cin >> p; p--; g[p].PB(i); } k = 0; dfs(0, -1, 0); fill_table(); segment seg; seg.init(N); for (int t = 0; t < M; ++t) { char que; int v; cin >> que; cin >> v; v--; // cout<<tin[v]<<" "<<tout[v]<<" "<<depth[v]<<endl; if (que == 'M') { seg.update(tin[v], tout[v], depth[v]); } else { int q = seg.query(tin[v]); for (int i = 19; i >= 0; --i) { if ((depth[v] - q) >> i & 1) v = par[i][v]; } ans += v + 1; } } cout << ans << endl; } }
replace
54
55
54
55
0
p01288
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX (1 << 17) #define INF (1LL << 55) typedef long long ll; typedef pair<ll, ll> P; struct SegmentTree { int n; P dat[2 * MAX - 1]; SegmentTree(int n_) { n = 1; while (n < n_) { n *= 2; } for (int i = 0; i < 2 * n - 1; i++) { dat[i] = P(0, 1); } } ll get_nearest_marked_node(int k) { k += n - 1; P p = dat[k]; while (k > 0) { k = (k - 1) / 2; p = max(p, dat[k]); } return p.second; } void mark(int a, int b, int k, int l, int r, P x) { if (r <= a || b <= l) return; if (a <= l && r <= b) { dat[k] = max(dat[k], x); } else { mark(a, b, k * 2 + 1, l, (l + r) / 2, x); mark(a, b, k * 2 + 2, (l + r) / 2, r, x); } } }; ll L[MAX], R[MAX], D[MAX], p; vector<int> G[MAX]; void init(int N) { p = 0; for (int i = 0; i < N; i++) { G[i].clear(); } } void dfs(int v, int depth) { L[v] = p++; D[v] = depth; for (int i = 0; i < (int)G[v].size(); i++) { dfs(G[v][i], depth + 1); } R[v] = p; } int main() { int N, Q, x; while (scanf("%d %d", &N, &Q), N) { init(N); for (int i = 1; i < N; i++) { scanf("%d", &x); cin >> x; x--; G[x].push_back(i); } dfs(0, 0); SegmentTree st(N); char m; ll v, res = 0; while (Q--) { cin >> m >> v; v--; if (m == 'M') { st.mark(L[v], R[v], 0, 0, st.n, P(D[v], v + 1)); } else { res += st.get_nearest_marked_node(L[v]); } } printf("%lld\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX (1 << 17) #define INF (1LL << 55) typedef long long ll; typedef pair<ll, ll> P; struct SegmentTree { int n; P dat[2 * MAX - 1]; SegmentTree(int n_) { n = 1; while (n < n_) { n *= 2; } for (int i = 0; i < 2 * n - 1; i++) { dat[i] = P(0, 1); } } ll get_nearest_marked_node(int k) { k += n - 1; P p = dat[k]; while (k > 0) { k = (k - 1) / 2; p = max(p, dat[k]); } return p.second; } void mark(int a, int b, int k, int l, int r, P x) { if (r <= a || b <= l) return; if (a <= l && r <= b) { dat[k] = max(dat[k], x); } else { mark(a, b, k * 2 + 1, l, (l + r) / 2, x); mark(a, b, k * 2 + 2, (l + r) / 2, r, x); } } }; ll L[MAX], R[MAX], D[MAX], p; vector<int> G[MAX]; void init(int N) { p = 0; for (int i = 0; i < N; i++) { G[i].clear(); } } void dfs(int v, int depth) { L[v] = p++; D[v] = depth; for (int i = 0; i < (int)G[v].size(); i++) { dfs(G[v][i], depth + 1); } R[v] = p; } int main() { int N, Q, x; while (scanf("%d %d", &N, &Q), N) { init(N); for (int i = 1; i < N; i++) { scanf("%d", &x); x--; G[x].push_back(i); } dfs(0, 0); SegmentTree st(N); char m; ll v, res = 0; while (Q--) { cin >> m >> v; v--; if (m == 'M') { st.mark(L[v], R[v], 0, 0, st.n, P(D[v], v + 1)); } else { res += st.get_nearest_marked_node(L[v]); } } printf("%lld\n", res); } return 0; }
delete
71
72
71
71
-11
p01290
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl; typedef pair<int, int> P; int h, w, qx, qy, ax, ay; string s[30]; short int dp[100][100][100][100][2]; // 1->Qwin,-1->Awin.0->? // qx,qy,ax,ay,turn=0->queen int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}; vector<P> exits; bool is(int x, int y) { if (x < 0 || h <= x || y < 0 || w <= y || s[x][y] == '#') return false; return true; } void init() { exits.clear(); rep(i, h) rep(j, w) rep(k, h) rep(l, w) rep(turn, 2) dp[i][j][k][l][turn] = 0; } void showdp(int i, int j, int k, int l, int turn, int value) { printf("dp[%d][%d][%d][%d][%d]=%d\n", i, j, k, l, turn, value); } int main() { while (true) { cin >> w >> h; if (w == 0) break; init(); rep(i, h) cin >> s[i]; rep(i, h) rep(j, w) { if (s[i][j] == 'Q') qx = i, qy = j; if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'E') exits.pb(P(i, j)); } bool update = true; for (P ex : exits) { int x = ex.fs, y = ex.sc; rep(i, h) rep(j, w) dp[x][y][i][j][0] = 1; } rep(i, h) rep(j, w) rep(turn, 2) dp[i][j][i][j][turn] = -1; while (update) { update = false; // turn=0 rep(i, h) rep(j, w) rep(k, h) rep(l, w) { if (dp[i][j][k][l][0] != 0) continue; bool allcant = true, canwin = false; rep(di, 5) { int nx = i + dx[di], ny = j + dy[di]; if (!is(nx, ny)) continue; if (dp[nx][ny][k][l][1] == 1) canwin = true; if (dp[nx][ny][k][l][1] != -1) allcant = false; } if (canwin) { dp[i][j][k][l][0] = 1; // showdp(i,j,k,l,0,1); update = true; continue; } if (allcant) { dp[i][j][k][l][0] = -1; // showdp(i,j,k,l,0,-1); update = true; continue; } } // turn=1 rep(i, h) rep(j, w) rep(k, h) rep(l, w) { if (dp[i][j][k][l][1] != 0) continue; bool allcant = true, canwin = false; rep(di, 5) { int nx = k + dx[di], ny = l + dy[di]; if (!is(nx, ny)) continue; if (dp[i][j][nx][ny][0] == -1) canwin = true; if (dp[i][j][nx][ny][0] != 1) allcant = false; } if (canwin) { dp[i][j][k][l][1] = -1; // showdp(i,j,k,l,1,-1); update = true; continue; } if (allcant) { dp[i][j][k][l][1] = 1; // showdp(i,j,k,l,1,1); update = true; continue; } } } // rep(i,h) rep(j,w) rep(k,h) rep(l,w) rep(turn,2) //showdp(i,j,k,l,turn,dp[i][j][k][l][turn]); switch (dp[qx][qy][ax][ay][0]) { case -1: puts("Army can catch Queen."); break; case 0: puts("Queen can not escape and Army can not catch Queen."); break; case 1: puts("Queen can escape."); break; } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i <= (n); ++i) #define all(c) (c).begin(), (c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl; typedef pair<int, int> P; int h, w, qx, qy, ax, ay; string s[30]; short int dp[30][30][30][30][2]; // 1->Qwin,-1->Awin.0->? // qx,qy,ax,ay,turn=0->queen int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}; vector<P> exits; bool is(int x, int y) { if (x < 0 || h <= x || y < 0 || w <= y || s[x][y] == '#') return false; return true; } void init() { exits.clear(); rep(i, h) rep(j, w) rep(k, h) rep(l, w) rep(turn, 2) dp[i][j][k][l][turn] = 0; } void showdp(int i, int j, int k, int l, int turn, int value) { printf("dp[%d][%d][%d][%d][%d]=%d\n", i, j, k, l, turn, value); } int main() { while (true) { cin >> w >> h; if (w == 0) break; init(); rep(i, h) cin >> s[i]; rep(i, h) rep(j, w) { if (s[i][j] == 'Q') qx = i, qy = j; if (s[i][j] == 'A') ax = i, ay = j; if (s[i][j] == 'E') exits.pb(P(i, j)); } bool update = true; for (P ex : exits) { int x = ex.fs, y = ex.sc; rep(i, h) rep(j, w) dp[x][y][i][j][0] = 1; } rep(i, h) rep(j, w) rep(turn, 2) dp[i][j][i][j][turn] = -1; while (update) { update = false; // turn=0 rep(i, h) rep(j, w) rep(k, h) rep(l, w) { if (dp[i][j][k][l][0] != 0) continue; bool allcant = true, canwin = false; rep(di, 5) { int nx = i + dx[di], ny = j + dy[di]; if (!is(nx, ny)) continue; if (dp[nx][ny][k][l][1] == 1) canwin = true; if (dp[nx][ny][k][l][1] != -1) allcant = false; } if (canwin) { dp[i][j][k][l][0] = 1; // showdp(i,j,k,l,0,1); update = true; continue; } if (allcant) { dp[i][j][k][l][0] = -1; // showdp(i,j,k,l,0,-1); update = true; continue; } } // turn=1 rep(i, h) rep(j, w) rep(k, h) rep(l, w) { if (dp[i][j][k][l][1] != 0) continue; bool allcant = true, canwin = false; rep(di, 5) { int nx = k + dx[di], ny = l + dy[di]; if (!is(nx, ny)) continue; if (dp[i][j][nx][ny][0] == -1) canwin = true; if (dp[i][j][nx][ny][0] != 1) allcant = false; } if (canwin) { dp[i][j][k][l][1] = -1; // showdp(i,j,k,l,1,-1); update = true; continue; } if (allcant) { dp[i][j][k][l][1] = 1; // showdp(i,j,k,l,1,1); update = true; continue; } } } // rep(i,h) rep(j,w) rep(k,h) rep(l,w) rep(turn,2) //showdp(i,j,k,l,turn,dp[i][j][k][l][turn]); switch (dp[qx][qy][ax][ay][0]) { case -1: puts("Army can catch Queen."); break; case 0: puts("Queen can not escape and Army can not catch Queen."); break; case 1: puts("Queen can escape."); break; } } }
replace
18
19
18
19
MLE
p01290
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; int dx[5] = {1, 0, -1, 0, 0}; int dy[5] = {0, 1, 0, -1, 0}; const int MAX_T = 10 * 30; int W, H; string grid[30]; char memo[30][30][30][30][MAX_T]; bool valid(int x, int w) { return 0 <= x && x < w; } int dfs(int x1, int y1, int x2, int y2, int t) { // printf("%d %d %d %d %d %c %c\n", x1, y1, x2, y2, t, grid[y1][x1], // grid[y2][x2]); if (t >= MAX_T) return 2; char &res = memo[y1][x1][y2][x2][t]; if (res != -1) return res; if (y1 == y2 && x1 == x2) return res = 1; int turn = t % 2; if (turn == 0) { if (grid[y1][x1] == 'E') return res = 0; bool draw = false; REP(r, 5) { int nx = x1 + dx[r]; int ny = y1 + dy[r]; if (valid(nx, W) && valid(ny, H) && grid[ny][nx] != '#') { int check = dfs(nx, ny, x2, y2, t + 1); if (check == 0) { return res = 0; } else if (check == 2) { draw = true; } } } return res = (draw ? 2 : 1); } else { bool draw = false; REP(r, 5) { int nx = x2 + dx[r]; int ny = y2 + dy[r]; if (valid(nx, W) && valid(ny, H) && grid[ny][nx] != '#') { int check = dfs(x1, y1, nx, ny, t + 1); if (check == 1) { return res = 1; } else if (check == 2) { draw = true; } } } return res = (draw ? 2 : 0); } } int main() { while (cin >> W >> H && W > 0) { REP(y, H) cin >> grid[y]; int x1 = -1, y1 = -1, x2 = -1, y2 = -1; REP(y, H) REP(x, W) if (grid[y][x] == 'Q') x1 = x, y1 = y; REP(y, H) REP(x, W) if (grid[y][x] == 'A') x2 = x, y2 = y; memset(memo, -1, sizeof(memo)); int ans = dfs(x1, y1, x2, y2, 0); if (ans == 0) { cout << "Queen can escape." << endl; } else if (ans == 1) { cout << "Army can catch Queen." << endl; } else { cout << "Queen can not escape and Army can not catch Queen." << endl; } } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; typedef long long LL; int dx[5] = {1, 0, -1, 0, 0}; int dy[5] = {0, 1, 0, -1, 0}; const int MAX_T = 10 * 30 / 4; int W, H; string grid[30]; char memo[30][30][30][30][MAX_T]; bool valid(int x, int w) { return 0 <= x && x < w; } int dfs(int x1, int y1, int x2, int y2, int t) { // printf("%d %d %d %d %d %c %c\n", x1, y1, x2, y2, t, grid[y1][x1], // grid[y2][x2]); if (t >= MAX_T) return 2; char &res = memo[y1][x1][y2][x2][t]; if (res != -1) return res; if (y1 == y2 && x1 == x2) return res = 1; int turn = t % 2; if (turn == 0) { if (grid[y1][x1] == 'E') return res = 0; bool draw = false; REP(r, 5) { int nx = x1 + dx[r]; int ny = y1 + dy[r]; if (valid(nx, W) && valid(ny, H) && grid[ny][nx] != '#') { int check = dfs(nx, ny, x2, y2, t + 1); if (check == 0) { return res = 0; } else if (check == 2) { draw = true; } } } return res = (draw ? 2 : 1); } else { bool draw = false; REP(r, 5) { int nx = x2 + dx[r]; int ny = y2 + dy[r]; if (valid(nx, W) && valid(ny, H) && grid[ny][nx] != '#') { int check = dfs(x1, y1, nx, ny, t + 1); if (check == 1) { return res = 1; } else if (check == 2) { draw = true; } } } return res = (draw ? 2 : 0); } } int main() { while (cin >> W >> H && W > 0) { REP(y, H) cin >> grid[y]; int x1 = -1, y1 = -1, x2 = -1, y2 = -1; REP(y, H) REP(x, W) if (grid[y][x] == 'Q') x1 = x, y1 = y; REP(y, H) REP(x, W) if (grid[y][x] == 'A') x2 = x, y2 = y; memset(memo, -1, sizeof(memo)); int ans = dfs(x1, y1, x2, y2, 0); if (ans == 0) { cout << "Queen can escape." << endl; } else if (ans == 1) { cout << "Army can catch Queen." << endl; } else { cout << "Queen can not escape and Army can not catch Queen." << endl; } } return 0; }
replace
7
8
7
8
MLE
p01291
C++
Time Limit Exceeded
#include <algorithm> #include <complex> #include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef complex<double> Complex; typedef Complex Line[2]; typedef vector<Complex> Polygon; static const double EPS = 1e-8; inline double dot(const Complex &a, const Complex &b) { return real(conj(a) * b); } inline Complex projection(const Line &l, const Complex &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } inline bool intersectSP(const Line &s, const Complex &p) { return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; } inline double distanceSP(const Line &s, const Complex &p) { const Complex r = projection(s, p); if (intersectSP(s, r)) { return abs(r - p); } return min(abs(s[0] - p), abs(s[1] - p)); } double distanceSS(const Line &s, const Line &t) { return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])), min(distanceSP(t, s[0]), distanceSP(t, s[1]))); } double distancePP(const Polygon &p1, const Polygon &p2) { double len = 100000000000.0; Line l1; Line l2; for (int i = 0; i < (int)p1.size(); i++) { l1[0] = p1[i]; l1[1] = p1[0]; if (i != (int)p1.size() - 1) { l1[1] = p1[i + 1]; } for (int j = 0; j < (int)p2.size(); j++) { l2[0] = p2[j]; l2[1] = p2[0]; if (j != (int)p2.size() - 1) { l2[1] = p2[j + 1]; } len = min(len, distanceSS(l1, l2)); } } return len; } struct Node { int where; double cost; Node(int w, double c) : where(w), cost(c) { ; } bool operator<(const Node &rhs) const { return cost > rhs.cost; } }; int width; int n; Polygon pillar[210]; bool check[210]; double dist[210]; double matrix[210][210]; inline int SOURCE() { return n; } inline int DESTINATION() { return n + 1; } inline int SIZE() { return n + 2; } void print_matrix() { for (int i = 0; i < SIZE(); i++) { for (int j = 0; j < SIZE(); j++) { cout << matrix[i][j] << " "; } cout << endl; } cout << endl; } int main() { while (scanf("%d %d", &width, &n), width) { for (int i = 0; i < n; i++) { int size; scanf("%d", &size); for (int j = 0; j < size; j++) { int a, b; scanf("%d %d", &a, &b); pillar[i].push_back(Complex(a, b)); } } pillar[SOURCE()].push_back(Complex(0, 0)); pillar[SOURCE()].push_back(Complex(0, 100000)); pillar[DESTINATION()].push_back(Complex(width, 0)); pillar[DESTINATION()].push_back(Complex(width, 100000)); for (int i = 0; i < SIZE(); i++) { check[i] = false; dist[i] = 100000000000000.0; for (int j = i; j < SIZE(); j++) { if (i == j) { matrix[i][j] = 0.0; continue; } matrix[i][j] = distancePP(pillar[i], pillar[j]); matrix[j][i] = matrix[i][j]; } } priority_queue<Node> que; que.push(Node(SOURCE(), 0.0)); while (!que.empty()) { Node node = que.top(); que.pop(); if (check[node.where]) { continue; } // cout << node.where << " " << node.cost << endl; check[node.where] = true; dist[node.where] = node.cost; if (node.where == DESTINATION()) { break; } for (int i = 0; i < SIZE(); i++) { double ncost = node.cost + matrix[node.where][i]; if (check[i] || dist[i] < ncost) { continue; } dist[i] = ncost; que.push(Node(i, ncost)); } } printf("%.6lf\n", dist[DESTINATION()]); } }
#include <algorithm> #include <complex> #include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef complex<double> Complex; typedef Complex Line[2]; typedef vector<Complex> Polygon; static const double EPS = 1e-8; inline double dot(const Complex &a, const Complex &b) { return real(conj(a) * b); } inline Complex projection(const Line &l, const Complex &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } inline bool intersectSP(const Line &s, const Complex &p) { return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; } inline double distanceSP(const Line &s, const Complex &p) { const Complex r = projection(s, p); if (intersectSP(s, r)) { return abs(r - p); } return min(abs(s[0] - p), abs(s[1] - p)); } double distanceSS(const Line &s, const Line &t) { return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])), min(distanceSP(t, s[0]), distanceSP(t, s[1]))); } double distancePP(const Polygon &p1, const Polygon &p2) { double len = 100000000000.0; Line l1; Line l2; for (int i = 0; i < (int)p1.size(); i++) { l1[0] = p1[i]; l1[1] = p1[0]; if (i != (int)p1.size() - 1) { l1[1] = p1[i + 1]; } for (int j = 0; j < (int)p2.size(); j++) { l2[0] = p2[j]; l2[1] = p2[0]; if (j != (int)p2.size() - 1) { l2[1] = p2[j + 1]; } len = min(len, distanceSS(l1, l2)); } } return len; } struct Node { int where; double cost; Node(int w, double c) : where(w), cost(c) { ; } bool operator<(const Node &rhs) const { return cost > rhs.cost; } }; int width; int n; Polygon pillar[210]; bool check[210]; double dist[210]; double matrix[210][210]; inline int SOURCE() { return n; } inline int DESTINATION() { return n + 1; } inline int SIZE() { return n + 2; } void print_matrix() { for (int i = 0; i < SIZE(); i++) { for (int j = 0; j < SIZE(); j++) { cout << matrix[i][j] << " "; } cout << endl; } cout << endl; } int main() { while (scanf("%d %d", &width, &n), width) { for (int i = 0; i < 210; i++) { pillar[i].clear(); } for (int i = 0; i < n; i++) { int size; scanf("%d", &size); for (int j = 0; j < size; j++) { int a, b; scanf("%d %d", &a, &b); pillar[i].push_back(Complex(a, b)); } } pillar[SOURCE()].push_back(Complex(0, 0)); pillar[SOURCE()].push_back(Complex(0, 100000)); pillar[DESTINATION()].push_back(Complex(width, 0)); pillar[DESTINATION()].push_back(Complex(width, 100000)); for (int i = 0; i < SIZE(); i++) { check[i] = false; dist[i] = 100000000000000.0; for (int j = i; j < SIZE(); j++) { if (i == j) { matrix[i][j] = 0.0; continue; } matrix[i][j] = distancePP(pillar[i], pillar[j]); matrix[j][i] = matrix[i][j]; } } priority_queue<Node> que; que.push(Node(SOURCE(), 0.0)); while (!que.empty()) { Node node = que.top(); que.pop(); if (check[node.where]) { continue; } // cout << node.where << " " << node.cost << endl; check[node.where] = true; dist[node.where] = node.cost; if (node.where == DESTINATION()) { break; } for (int i = 0; i < SIZE(); i++) { double ncost = node.cost + matrix[node.where][i]; if (check[i] || dist[i] < ncost) { continue; } dist[i] = ncost; que.push(Node(i, ncost)); } } printf("%.6lf\n", dist[DESTINATION()]); } }
insert
94
94
94
97
TLE
p01292
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define EQ(a, b) (abs((a) - (b)) < EPS) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fs first #define sc second #define pb push_back #define sz size() #define all(a) (a).begin(), (a).end() using namespace std; typedef long double D; typedef complex<D> P; typedef pair<P, P> Line; typedef vector<P> Poly; const D EPS = 1e-8; const D PI = acos(-1); const D sq2 = 1.0 / sqrt(2.0); inline D dot(P x, P y) { return real(conj(x) * y); } inline D cross(P x, P y) { return imag(conj(x) * y); } int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > EPS) return 1; // counter clockwise if (cross(b, c) < -EPS) return -1; // clockwise if (dot(b, c) < -EPS) return 2; // c--a--b on line if (abs(b) + EPS < abs(c)) return -2; // a--b--c on line return 0; // on segment } inline bool is_cp(Line a, Line b) { if (ccw(a.fs, a.sc, b.fs) * ccw(a.fs, a.sc, b.sc) <= 0) if (ccw(b.fs, b.sc, a.fs) * ccw(b.fs, b.sc, a.sc) <= 0) return true; return false; } int main() { D Time, R; int L, M, N; P pm[55], vm[55], pg[55], vg[55]; D tm[55], tg[55]; Poly obj[55]; cin.tie(0); std::ios::sync_with_stdio(false); while (cin >> Time >> R) { if (EQ(Time, 0) && EQ(R, 0)) break; cin >> L; rep(i, L) { D x, y; cin >> x >> y >> tm[i]; pm[i] = P(x, y); } rep(i, L - 1) vm[i] = (pm[i + 1] - pm[i]) / (tm[i + 1] - tm[i]); cin >> M; rep(i, M) { D x, y; cin >> x >> y >> tg[i]; pg[i] = P(x, y); } rep(i, M - 1) vg[i] = (pg[i + 1] - pg[i]) / (tg[i + 1] - tg[i]); cin >> N; rep(i, N) { D xl, yb, xr, yt; cin >> xl >> yb >> xr >> yt; if (xl > xr) swap(xl, xr); if (yb > yt) swap(yb, yt); obj[i].clear(); obj[i].pb(P(xl, yb)); obj[i].pb(P(xr, yb)); obj[i].pb(P(xr, yt)); obj[i].pb(P(xl, yt)); } vector<D> event; int sm = 0, sg = 0; D hoge = 0; while (hoge <= Time + EPS) { event.pb(hoge); hoge += 0.00005; } D ans = 0; sm = 0; sg = 0; rep(t, event.sz - 1) { D T = (event[t] + event[t + 1]) / 2; if (sm < L && EQ(tm[sm + 1], event[t])) sm++; if (sg < M && EQ(tg[sg + 1], event[t])) sg++; // check bool f = true; P curM = P(vm[sm].real() * (T - tm[sm]) + pm[sm].real(), vm[sm].imag() * (T - tm[sm]) + pm[sm].imag()); P curG = P(vg[sg].real() * (T - tg[sg]) + pg[sg].real(), vg[sg].imag() * (T - tg[sg]) + pg[sg].imag()); if (abs(curM - curG) > R + EPS) f = false; else { P A = pg[sg + 1] - curG, B = curM - curG; if (dot(A, B) / abs(A) / abs(B) + EPS < sq2) f = false; else { Line eye = Line(curM, curG); rep(i, N) { rep(j, obj[i].sz) { Line wall = Line(obj[i][j], obj[i][(j + 1) % obj[i].sz]); if (is_cp(eye, wall)) { f = false; break; } if (!f) break; } } } } if (f) ans += event[t + 1] - event[t]; } cout << fixed << setprecision(12) << ans << endl; } }
#include <bits/stdc++.h> #define EQ(a, b) (abs((a) - (b)) < EPS) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fs first #define sc second #define pb push_back #define sz size() #define all(a) (a).begin(), (a).end() using namespace std; typedef double D; typedef complex<D> P; typedef pair<P, P> Line; typedef vector<P> Poly; const D EPS = 1e-8; const D PI = acos(-1); const D sq2 = 1.0 / sqrt(2.0); inline D dot(P x, P y) { return real(conj(x) * y); } inline D cross(P x, P y) { return imag(conj(x) * y); } int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > EPS) return 1; // counter clockwise if (cross(b, c) < -EPS) return -1; // clockwise if (dot(b, c) < -EPS) return 2; // c--a--b on line if (abs(b) + EPS < abs(c)) return -2; // a--b--c on line return 0; // on segment } inline bool is_cp(Line a, Line b) { if (ccw(a.fs, a.sc, b.fs) * ccw(a.fs, a.sc, b.sc) <= 0) if (ccw(b.fs, b.sc, a.fs) * ccw(b.fs, b.sc, a.sc) <= 0) return true; return false; } int main() { D Time, R; int L, M, N; P pm[55], vm[55], pg[55], vg[55]; D tm[55], tg[55]; Poly obj[55]; cin.tie(0); std::ios::sync_with_stdio(false); while (cin >> Time >> R) { if (EQ(Time, 0) && EQ(R, 0)) break; cin >> L; rep(i, L) { D x, y; cin >> x >> y >> tm[i]; pm[i] = P(x, y); } rep(i, L - 1) vm[i] = (pm[i + 1] - pm[i]) / (tm[i + 1] - tm[i]); cin >> M; rep(i, M) { D x, y; cin >> x >> y >> tg[i]; pg[i] = P(x, y); } rep(i, M - 1) vg[i] = (pg[i + 1] - pg[i]) / (tg[i + 1] - tg[i]); cin >> N; rep(i, N) { D xl, yb, xr, yt; cin >> xl >> yb >> xr >> yt; if (xl > xr) swap(xl, xr); if (yb > yt) swap(yb, yt); obj[i].clear(); obj[i].pb(P(xl, yb)); obj[i].pb(P(xr, yb)); obj[i].pb(P(xr, yt)); obj[i].pb(P(xl, yt)); } vector<D> event; int sm = 0, sg = 0; D hoge = 0; while (hoge <= Time + EPS) { event.pb(hoge); hoge += 0.00005; } D ans = 0; sm = 0; sg = 0; rep(t, event.sz - 1) { D T = (event[t] + event[t + 1]) / 2; if (sm < L && EQ(tm[sm + 1], event[t])) sm++; if (sg < M && EQ(tg[sg + 1], event[t])) sg++; // check bool f = true; P curM = P(vm[sm].real() * (T - tm[sm]) + pm[sm].real(), vm[sm].imag() * (T - tm[sm]) + pm[sm].imag()); P curG = P(vg[sg].real() * (T - tg[sg]) + pg[sg].real(), vg[sg].imag() * (T - tg[sg]) + pg[sg].imag()); if (abs(curM - curG) > R + EPS) f = false; else { P A = pg[sg + 1] - curG, B = curM - curG; if (dot(A, B) / abs(A) / abs(B) + EPS < sq2) f = false; else { Line eye = Line(curM, curG); rep(i, N) { rep(j, obj[i].sz) { Line wall = Line(obj[i][j], obj[i][(j + 1) % obj[i].sz]); if (is_cp(eye, wall)) { f = false; break; } if (!f) break; } } } } if (f) ans += event[t + 1] - event[t]; } cout << fixed << setprecision(12) << ans << endl; } }
replace
11
12
11
12
TLE
p01292
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <valarray> #include <vector> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } namespace geom { #define X real() #define Y imag() #define at(i) ((*this)[i]) #define SELF (*this) enum { TRUE = 1, FALSE = 0, BORDER = -1 }; typedef int BOOL; typedef double R; const R INF = 1e8; R EPS = 1e-6; const R PI = 3.1415926535897932384626; inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; } typedef complex<R> P; inline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; } inline R inp(const P &a, const P &b) { return (conj(a) * b).X; } inline R outp(const P &a, const P &b) { return (conj(a) * b).Y; } inline P unit(const P &p) { return p / abs(p); } inline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); } inline int ccw(const P &s, const P &t, const P &p, int adv = 0) { int res = sig(outp(t - s, p - s)); if (res || !adv) return res; if (sig(inp(t - s, p - s)) < 0) return -2; // p-s-t if (sig(inp(s - t, p - t)) < 0) return 2; // s-t-p return 0; // s-p-t } struct L : public vector<P> { // line L(const P &p1, const P &p2) { this->push_back(p1); this->push_back(p2); } L() {} P dir() const { return at(1) - at(0); } BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); } }; struct S : public L { // segment S(const P &p1, const P &p2) : L(p1, p2) {} S() {} BOOL online(const P &p) const { if (!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER; // 座標の二乗とEPSの差が大きすぎないように注意 return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > EPS && inp(p - at(1), -dir()) > -EPS; // return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1))); } }; BOOL intersect(const S &s, const S &t) { const int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1); const int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1); return (p > 0 || q > 0) ? FALSE : (!p || !q) ? BORDER : TRUE; } #undef SELF #undef at } // namespace geom using namespace geom; namespace std { bool operator<(const P &a, const P &b) { return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y; } bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; } istream &operator>>(istream &is, P &p) { R x, y; is >> x >> y; p = P(x, y); return is; } const R B = 500; const R Z = .05; ostream &operator<<(ostream &os, const P &c) { return os << "circle(" << B + Z * (c.X) << ", " << 1000 - B - Z * (c.Y) << ", 2)"; } ostream &operator<<(ostream &os, const S &s) { return os << "line(" << B + Z * (s[0].X) << ", " << 1000 - B - Z * (s[0].Y) << ", " << B + Z * (s[1].X) << ", " << 1000 - B - Z * (s[1].Y) << ")"; } } // namespace std R T, range; int n; const R inc = 7e-5; int main(int argc, char *argv[]) { ios::sync_with_stdio(false); while (cin >> T >> range, range > EPS) { vector<pair<P, R>> M, G; int L; cin >> L; REP(i, L) { P p; R t; cin >> p >> t; M.emplace_back(p, t); } cin >> L; REP(i, L) { P p; R t; cin >> p >> t; G.emplace_back(p, t); } vector<S> obs; cin >> L; REP(i, L) { R sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty; obs.emplace_back(P(sx, sy), P(sx, ty)); obs.emplace_back(P(sx, sy), P(tx, sy)); obs.emplace_back(P(tx, sy), P(tx, ty)); obs.emplace_back(P(sx, ty), P(tx, ty)); } int i = -1, j = -1; R ans = 0; P m = M[0].first; P g = G[0].first; P md; P gd; for (R t = 0; t < T; t += inc) { if (M[i + 1].second <= t) { i++; m = M[i].first; md = (M[i + 1].first - M[i].first); } if (G[j + 1].second <= t) { j++; g = G[j].first; gd = (G[j + 1].first - G[j].first); } const P mm = m + md * ((t - M[i].second) / (M[i + 1].second - M[i].second)); const P gg = g + gd * ((t - G[j].second) / (G[j + 1].second - G[j].second)); const S s(gg, mm); if (abs(arg((G[j + 1].first - G[j].first) / s.dir())) > PI * (R).25 || norm(s.dir()) > range * range || [&]() { FOR(o, obs) if (intersect(s, *o)) return 1; return 0; }()) continue; ans += inc; } printf("%.5f\n", ans); } return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <valarray> #include <vector> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define RFOR(i, c) \ for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a, b) make_pair(a, b) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; FOR(it, t) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } namespace geom { #define X real() #define Y imag() #define at(i) ((*this)[i]) #define SELF (*this) enum { TRUE = 1, FALSE = 0, BORDER = -1 }; typedef int BOOL; typedef double R; const R INF = 1e8; R EPS = 1e-6; const R PI = 3.1415926535897932384626; inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline BOOL less(const R &x, const R &y) { return sig(x - y) ? x < y : BORDER; } typedef complex<R> P; inline R norm(const P &p) { return p.X * p.X + p.Y * p.Y; } inline R inp(const P &a, const P &b) { return (conj(a) * b).X; } inline R outp(const P &a, const P &b) { return (conj(a) * b).Y; } inline P unit(const P &p) { return p / abs(p); } inline P proj(const P &s, const P &t) { return t * inp(s, t) / norm(t); } inline int ccw(const P &s, const P &t, const P &p, int adv = 0) { int res = sig(outp(t - s, p - s)); if (res || !adv) return res; if (sig(inp(t - s, p - s)) < 0) return -2; // p-s-t if (sig(inp(s - t, p - t)) < 0) return 2; // s-t-p return 0; // s-p-t } struct L : public vector<P> { // line L(const P &p1, const P &p2) { this->push_back(p1); this->push_back(p2); } L() {} P dir() const { return at(1) - at(0); } BOOL online(const P &p) const { return !sig(outp(p - at(0), dir())); } }; struct S : public L { // segment S(const P &p1, const P &p2) : L(p1, p2) {} S() {} BOOL online(const P &p) const { if (!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER; // 座標の二乗とEPSの差が大きすぎないように注意 return !sig(outp(p - at(0), dir())) && inp(p - at(0), dir()) > EPS && inp(p - at(1), -dir()) > -EPS; // return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1))); } }; BOOL intersect(const S &s, const S &t) { const int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1); const int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1); return (p > 0 || q > 0) ? FALSE : (!p || !q) ? BORDER : TRUE; } #undef SELF #undef at } // namespace geom using namespace geom; namespace std { bool operator<(const P &a, const P &b) { return sig(a.X - b.X) ? a.X < b.X : a.Y + EPS < b.Y; } bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; } istream &operator>>(istream &is, P &p) { R x, y; is >> x >> y; p = P(x, y); return is; } const R B = 500; const R Z = .05; ostream &operator<<(ostream &os, const P &c) { return os << "circle(" << B + Z * (c.X) << ", " << 1000 - B - Z * (c.Y) << ", 2)"; } ostream &operator<<(ostream &os, const S &s) { return os << "line(" << B + Z * (s[0].X) << ", " << 1000 - B - Z * (s[0].Y) << ", " << B + Z * (s[1].X) << ", " << 1000 - B - Z * (s[1].Y) << ")"; } } // namespace std R T, range; int n; const R inc = 7e-5; int main(int argc, char *argv[]) { ios::sync_with_stdio(false); while (cin >> T >> range, range > EPS) { vector<pair<P, R>> M, G; int L; cin >> L; REP(i, L) { P p; R t; cin >> p >> t; M.emplace_back(p, t); } cin >> L; REP(i, L) { P p; R t; cin >> p >> t; G.emplace_back(p, t); } vector<S> obs; cin >> L; REP(i, L) { R sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty; obs.emplace_back(P(sx, sy), P(sx, ty)); obs.emplace_back(P(sx, sy), P(tx, sy)); obs.emplace_back(P(tx, sy), P(tx, ty)); obs.emplace_back(P(sx, ty), P(tx, ty)); } int i = -1, j = -1; R ans = 0; P m = M[0].first; P g = G[0].first; P md; P gd; for (R t = 0; t < T; t += inc) { if (M[i + 1].second <= t) { i++; m = M[i].first; md = (M[i + 1].first - M[i].first); } if (G[j + 1].second <= t) { j++; g = G[j].first; gd = (G[j + 1].first - G[j].first); } const P mm = m + md * ((t - M[i].second) / (M[i + 1].second - M[i].second)); const P gg = g + gd * ((t - G[j].second) / (G[j + 1].second - G[j].second)); const S s(gg, mm); if (norm(s.dir()) > range * range || abs(arg(gd / s.dir())) > PI * (R).25 || [&]() { FOR(o, obs) if (intersect(s, *o)) return 1; return 0; }()) continue; ans += inc; } printf("%.5f\n", ans); } return 0; }
replace
238
240
238
240
TLE
p01293
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}; int toi(char c) { if (isdigit(c)) return c - '2'; if (c == 'T') return 8; if (c == 'J') return 9; if (c == 'Q') return 10; if (c == 'K') return 11; if (c == 'A') return 12; } int main() { char c; string deb[4] = {"norst", "east", "south", "west"}; while (cin >> c && c != '#') { int point[2] = {}; string card[4][13]; REP(i, 4) REP(j, 13) cin >> card[i][j]; char d = card[0][0][1]; REP(i, 13) { int high = -24; int win = -1; REP(j, 4) { string s = card[j][i]; int n = toi(s[0]); if (s[1] == c) n += 13; else if (s[1] != d) n -= 13; assert(high != n); if (high < n) { high = n; win = j; } } if (i != 12) { d = card[win][i + 1][1]; } point[win % 2] += 1; } if (point[0] > point[1]) { cout << "NS" << " " << point[0] - 6 << endl; } else { cout << "EW" << " " << point[1] - 6 << 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}; int toi(char c) { if (isdigit(c)) return c - '2'; if (c == 'T') return 8; if (c == 'J') return 9; if (c == 'Q') return 10; if (c == 'K') return 11; if (c == 'A') return 12; } int main() { char c; string deb[4] = {"norst", "east", "south", "west"}; while (cin >> c && c != '#') { int point[2] = {}; string card[4][13]; REP(i, 4) REP(j, 13) cin >> card[i][j]; char d = card[0][0][1]; REP(i, 13) { int high = -24; int win = -1; REP(j, 4) { string s = card[j][i]; int n = toi(s[0]); if (s[1] == c) n += 13; else if (s[1] != d) n -= 13; // assert(high != n); if (high < n) { high = n; win = j; } } if (i != 12) { d = card[win][i + 1][1]; } point[win % 2] += 1; } if (point[0] > point[1]) { cout << "NS" << " " << point[0] - 6 << endl; } else { cout << "EW" << " " << point[1] - 6 << endl; } } return 0; }
replace
70
71
70
71
0
p01293
C++
Runtime Error
// Header {{{ // includes {{{ #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <unistd.h> #include <vector> // }}} using namespace std; // consts {{{ static const int INF = 1e9; static const double PI = acos(-1.0); static const double EPS = 1e-10; // }}} // typedefs {{{ typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<ULL> VULL; typedef vector<VULL> VVULL; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef vector<VS> VVS; typedef pair<int, int> PII; typedef complex<int> P; // }}} // macros & inline functions {{{ // syntax sugars {{{ #define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i) #define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i) #define REP(i, n) FOR(i, 0, n) #define REPI(i, n) FORI(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const // }}} // conversion {{{ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // }}} // array and STL {{{ #define ARRSIZE(a) (sizeof(a) / sizeof(a[0])) #define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a))) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SIZE(a) ((LL)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(c, it) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define REACH(c, it) \ for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(s, e) ((s).find(e) != (s).end()) // }}} // bit manipulation {{{ // singed integers are not for bitwise operations, specifically arithmetic // shifts ('>>', and maybe not good for '<<' too) #define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed) #define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n))) #define BITOF(n, m) \ (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1)) inline int BITS_COUNT(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int MSB(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c - 1; } inline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); } // }}} // for readable code {{{ #define EVEN(n) (n % 2 == 0) #define ODD(n) (!EVEN(n)) // }}} // debug {{{ #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define dprt(fmt, ...) \ if (opt_debug) { \ fprintf(stderr, fmt, ##__VA_ARGS__); \ } #define darr(a) \ if (opt_debug) { \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr_range(a, f, t) \ if (opt_debug) { \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define dvec(v) \ if (opt_debug) { \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr2(a) \ if (opt_debug) { \ FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \ } #define WAIT() \ if (opt_debug) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \ } #define dumpl(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \ } #define dumpf() \ if (opt_debug) { \ cerr << __PRETTY_FUNCTION__ << endl; \ } #define where() \ if (opt_debug) { \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; \ } #define show_bits(b, s) \ if (opt_debug) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } // ostreams {{{ // complex template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) { return s << "(" << d.real() << ", " << d.imag() << ")"; } // pair template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) { int len = d.size(); REP(i, len) { s << d[i]; if (i < len - 1) s << "\t"; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &d) { int len = d.size(); REP(i, len) { s << d[i] << endl; } return s; } // map template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } // }}} // }}} // }}} // time {{{ inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } // }}} // string manipulation {{{ inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } inline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; } // }}} // geometry {{{ #define Y real() #define X imag() // }}} // 2 dimentional array {{{ P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)}; P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)}; int g_height, g_width; bool in_field(P p) { return (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width); } // }}} // input and output {{{ inline void input(string filename) { freopen(filename.c_str(), "r", stdin); } inline void output(string filename) { freopen(filename.c_str(), "w", stdout); } // }}} // }}} bool opt_debug = false; class Card { public: char suit; int rank; Card(char _suit, int _rank); }; Card::Card(char _suit, int _rank) { suit = _suit; rank = _rank; } int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); // set options {{{ int __c; while ((__c = getopt(argc, argv, "d")) != -1) { switch (__c) { case 'd': opt_debug = true; break; default: abort(); } } // }}} input("./inputs/0.txt"); // output("./outputs/0.txt"); enum { N, E, S, W }; char trump; while (cin >> trump, trump != '#') { vector<vector<Card>> cards(4, vector<Card>(13, Card(' ', ' '))); REP(i, 4) { REP(j, 13) { string s; cin >> s; int r; switch (s[0]) { case 'T': r = 10; break; case 'J': r = 11; break; case 'Q': r = 12; break; case 'K': r = 13; break; case 'A': r = 14; break; default: r = s[0] - '0'; break; } Card c(s[1], r); cards[i][j] = c; } } int ns = 0, ew = 0; int lead = N; REP(round, 13) { dump(round + 1); int winner = lead; Card strongest = cards[lead][round]; dump(lead); dump(strongest.suit); dump(strongest.rank); char suit = cards[lead][round].suit; FOR(turn, 1, 4) { int player = (lead + turn) % 4; Card &c = cards[player][round]; dump(c.suit); dump(c.rank); if (c.suit == strongest.suit && c.rank > strongest.rank) { strongest = c; winner = player; } else if (c.suit == trump && (strongest.suit != trump || c.rank > strongest.rank)) { strongest = c; winner = player; } dump(strongest.suit); dump(strongest.rank); } switch (winner) { case N: case S: ns++; break; default: ew++; break; } lead = winner; } if (ns > ew) { cout << "NS " << ns - 6 << endl; } else { cout << "EW " << ew - 6 << endl; } } return 0; } // vim: foldmethod=marker
// Header {{{ // includes {{{ #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/time.h> #include <unistd.h> #include <vector> // }}} using namespace std; // consts {{{ static const int INF = 1e9; static const double PI = acos(-1.0); static const double EPS = 1e-10; // }}} // typedefs {{{ typedef long long int LL; typedef unsigned long long int ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<ULL> VULL; typedef vector<VULL> VVULL; typedef vector<double> VD; typedef vector<VD> VVD; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<char> VC; typedef vector<VC> VVC; typedef vector<string> VS; typedef vector<VS> VVS; typedef pair<int, int> PII; typedef complex<int> P; // }}} // macros & inline functions {{{ // syntax sugars {{{ #define FOR(i, b, e) for (typeof(e) i = (b); i < (e); ++i) #define FORI(i, b, e) for (typeof(e) i = (b); i <= (e); ++i) #define REP(i, n) FOR(i, 0, n) #define REPI(i, n) FORI(i, 0, n) #define OPOVER(_op, _type) inline bool operator _op(const _type &t) const // }}} // conversion {{{ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // }}} // array and STL {{{ #define ARRSIZE(a) (sizeof(a) / sizeof(a[0])) #define ZERO(a, v) (assert(v == 0 || v == -1), memset(a, v, sizeof(a))) #define F first #define S second #define MP(a, b) make_pair(a, b) #define SIZE(a) ((LL)a.size()) #define PB(e) push_back(e) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EACH(c, it) \ for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define REACH(c, it) \ for (typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(s, e) ((s).find(e) != (s).end()) // }}} // bit manipulation {{{ // singed integers are not for bitwise operations, specifically arithmetic // shifts ('>>', and maybe not good for '<<' too) #define IS_UNSIGNED(n) (!numeric_limits<typeof(n)>::is_signed) #define BIT(n) (assert(IS_UNSIGNED(n)), assert(n < 64), (1ULL << (n))) #define BITOF(n, m) \ (assert(IS_UNSIGNED(n)), assert(m < 64), ((ULL)(n) >> (m)&1)) inline int BITS_COUNT(ULL b) { int c = 0; while (b != 0) { c += (b & 1); b >>= 1; } return c; } inline int MSB(ULL b) { int c = 0; while (b != 0) { ++c; b >>= 1; } return c - 1; } inline int MAKE_MASK(ULL upper, ULL lower) { assert(lower < 64 && upper < 64 && lower <= upper); return (BIT(upper) - 1) ^ (BIT(lower) - 1); } // }}} // for readable code {{{ #define EVEN(n) (n % 2 == 0) #define ODD(n) (!EVEN(n)) // }}} // debug {{{ #define arrsz(a) (sizeof(a) / sizeof(a[0])) #define dprt(fmt, ...) \ if (opt_debug) { \ fprintf(stderr, fmt, ##__VA_ARGS__); \ } #define darr(a) \ if (opt_debug) { \ copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr_range(a, f, t) \ if (opt_debug) { \ copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define dvec(v) \ if (opt_debug) { \ copy(ALL(v), ostream_iterator<int>(cerr, " ")); \ cerr << endl; \ } #define darr2(a) \ if (opt_debug) { \ FOR(__i, 0, (arrsz(a))) { darr((a)[__i]); } \ } #define WAIT() \ if (opt_debug) { \ string _wait_; \ cerr << "(hit return to continue)" << endl; \ getline(cin, _wait_); \ } #define dump(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl; \ } #define dumpl(x) \ if (opt_debug) { \ cerr << " [L" << __LINE__ << "] " << #x << endl << (x) << endl; \ } #define dumpf() \ if (opt_debug) { \ cerr << __PRETTY_FUNCTION__ << endl; \ } #define where() \ if (opt_debug) { \ cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \ << "]" << endl; \ } #define show_bits(b, s) \ if (opt_debug) { \ REP(i, s) { \ cerr << BITOF(b, s - 1 - i); \ if (i % 4 == 3) \ cerr << ' '; \ } \ cerr << endl; \ } // ostreams {{{ // complex template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) { return s << "(" << d.real() << ", " << d.imag() << ")"; } // pair template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &d) { return s << "(" << d.first << ", " << d.second << ")"; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) { int len = d.size(); REP(i, len) { s << d[i]; if (i < len - 1) s << "\t"; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &d) { int len = d.size(); REP(i, len) { s << d[i] << endl; } return s; } // map template <typename T1, typename T2> ostream &operator<<(ostream &s, const map<T1, T2> &m) { s << "{" << endl; for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; } // }}} // }}} // }}} // time {{{ inline double now() { struct timeval tv; gettimeofday(&tv, NULL); return (static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6); } // }}} // string manipulation {{{ inline VS split(string s, char delimiter) { VS v; string t; REP(i, s.length()) { if (s[i] == delimiter) v.PB(t), t = ""; else t += s[i]; } v.PB(t); return v; } inline string join(VS s, string j) { string t; REP(i, s.size()) { t += s[i] + j; } return t; } // }}} // geometry {{{ #define Y real() #define X imag() // }}} // 2 dimentional array {{{ P dydx4[4] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)}; P dydx8[8] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)}; int g_height, g_width; bool in_field(P p) { return (0 <= p.Y && p.Y < g_height) && (0 <= p.X && p.X < g_width); } // }}} // input and output {{{ inline void input(string filename) { freopen(filename.c_str(), "r", stdin); } inline void output(string filename) { freopen(filename.c_str(), "w", stdout); } // }}} // }}} bool opt_debug = false; class Card { public: char suit; int rank; Card(char _suit, int _rank); }; Card::Card(char _suit, int _rank) { suit = _suit; rank = _rank; } int main(int argc, char **argv) { std::ios_base::sync_with_stdio(false); // set options {{{ int __c; while ((__c = getopt(argc, argv, "d")) != -1) { switch (__c) { case 'd': opt_debug = true; break; default: abort(); } } // }}} // input("./inputs/0.txt"); // output("./outputs/0.txt"); enum { N, E, S, W }; char trump; while (cin >> trump, trump != '#') { vector<vector<Card>> cards(4, vector<Card>(13, Card(' ', ' '))); REP(i, 4) { REP(j, 13) { string s; cin >> s; int r; switch (s[0]) { case 'T': r = 10; break; case 'J': r = 11; break; case 'Q': r = 12; break; case 'K': r = 13; break; case 'A': r = 14; break; default: r = s[0] - '0'; break; } Card c(s[1], r); cards[i][j] = c; } } int ns = 0, ew = 0; int lead = N; REP(round, 13) { dump(round + 1); int winner = lead; Card strongest = cards[lead][round]; dump(lead); dump(strongest.suit); dump(strongest.rank); char suit = cards[lead][round].suit; FOR(turn, 1, 4) { int player = (lead + turn) % 4; Card &c = cards[player][round]; dump(c.suit); dump(c.rank); if (c.suit == strongest.suit && c.rank > strongest.rank) { strongest = c; winner = player; } else if (c.suit == trump && (strongest.suit != trump || c.rank > strongest.rank)) { strongest = c; winner = player; } dump(strongest.suit); dump(strongest.rank); } switch (winner) { case N: case S: ns++; break; default: ew++; break; } lead = winner; } if (ns > ew) { cout << "NS " << ns - 6 << endl; } else { cout << "EW " << ew - 6 << endl; } } return 0; } // vim: foldmethod=marker
replace
299
300
299
300
TLE
p01294
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define dump(a) (cerr << (#a) << " = " << (a) << endl) template <class T> inline void chmax(T &a, const T &b) { if (b > a) a = b; } template <class T> inline void chmin(T &a, const T &b) { if (b < a) a = b; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class Tuple, unsigned Index> void print_tuple(ostream &os, const Tuple &t) {} template <class Tuple, unsigned Index, class Type, class... Types> void print_tuple(ostream &os, const Tuple &t) { if (Index > 0) os << ", "; os << get<Index>(t); print_tuple<Tuple, Index + 1, Types...>(os, t); } template <class... Types> ostream &operator<<(ostream &os, const tuple<Types...> &t) { os << '('; print_tuple<tuple<Types...>, 0, Types...>(os, t); return os << ')'; } template <class Iterator> ostream &dump_range(ostream &, Iterator, const Iterator &); template <typename T> ostream &operator<<(ostream &os, vector<T> c) { return dump_range(os, c.cbegin(), c.cend()); } template <class Iterator> ostream &dump_range(ostream &os, Iterator first, const Iterator &last) { os << '['; for (int i = 0; first != last; ++i, ++first) { if (i) os << ", "; os << *first; } return os << ']'; } int main() { cin.tie(0); ios::sync_with_stdio(false); for (int n, d; cin >> n >> d && n;) { int rest = 0; vector<int> power(n, 0); vector<vector<int>> missiles(n); for (int i = 0; i < n; ++i) { int m; cin >> m; rest += m; missiles[i].resize(m); for (int j = 0; j < m; ++j) { cin >> missiles[i][j]; power[i] += missiles[i][j]; } } while (rest) { bool update = false; for (int i = 0; i < n;) { if (missiles.size() == 0) { power.erase(power.begin() + i); missiles.erase(missiles.begin() + i); --n; continue; } const int next = power[i] - missiles[i].back(); for (int j = 0; j < n; ++j) { if (i == j) continue; if (next + d < power[j]) goto ng; } --rest; missiles[i].pop_back(); power[i] = next; update = true; ng:; ++i; } if (!update) break; } cout << (rest ? "No" : "Yes") << endl; } return EXIT_SUCCESS; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define dump(a) (cerr << (#a) << " = " << (a) << endl) template <class T> inline void chmax(T &a, const T &b) { if (b > a) a = b; } template <class T> inline void chmin(T &a, const T &b) { if (b < a) a = b; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class Tuple, unsigned Index> void print_tuple(ostream &os, const Tuple &t) {} template <class Tuple, unsigned Index, class Type, class... Types> void print_tuple(ostream &os, const Tuple &t) { if (Index > 0) os << ", "; os << get<Index>(t); print_tuple<Tuple, Index + 1, Types...>(os, t); } template <class... Types> ostream &operator<<(ostream &os, const tuple<Types...> &t) { os << '('; print_tuple<tuple<Types...>, 0, Types...>(os, t); return os << ')'; } template <class Iterator> ostream &dump_range(ostream &, Iterator, const Iterator &); template <typename T> ostream &operator<<(ostream &os, vector<T> c) { return dump_range(os, c.cbegin(), c.cend()); } template <class Iterator> ostream &dump_range(ostream &os, Iterator first, const Iterator &last) { os << '['; for (int i = 0; first != last; ++i, ++first) { if (i) os << ", "; os << *first; } return os << ']'; } int main() { cin.tie(0); ios::sync_with_stdio(false); for (int n, d; cin >> n >> d && n;) { int rest = 0; vector<int> power(n, 0); vector<vector<int>> missiles(n); for (int i = 0; i < n; ++i) { int m; cin >> m; rest += m; missiles[i].resize(m); for (int j = 0; j < m; ++j) { cin >> missiles[i][j]; power[i] += missiles[i][j]; } } while (rest) { bool update = false; for (int i = 0; i < n;) { if (missiles[i].size() == 0) { power.erase(power.begin() + i); missiles.erase(missiles.begin() + i); --n; continue; } const int next = power[i] - missiles[i].back(); for (int j = 0; j < n; ++j) { if (i == j) continue; if (next + d < power[j]) goto ng; } --rest; missiles[i].pop_back(); power[i] = next; update = true; ng:; ++i; } if (!update) break; } cout << (rest ? "No" : "Yes") << endl; } return EXIT_SUCCESS; }
replace
107
108
107
108
0
p01294
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; bool solve(int n, int d) { vector<vector<int>> c(n); for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { int buf; cin >> buf; c[i].push_back(buf); } } if (n == 1) return true; while (true) { vector<int> sum(n); for (int i = 0; i < n; i++) sum[i] = accumulate(c[i].begin(), c[i].end(), 0); int midx = max_element(sum.begin(), sum.end()) - sum.begin(); int smidx = midx ? 0 : 1; for (int i = 0; i < n; i++) { if (i != midx && sum[smidx] < sum[i]) smidx = i; } bool isok = false; for (int i = 0; i < n && !isok; i++) { if (i == midx) continue; if (c[i].size() > 0 && sum[midx] - (sum[i] - c[i].back()) <= d) { c[i].pop_back(); isok = true; } } if (isok) continue; if (c[midx].size() == 0 || sum[smidx] - (sum[midx] - c[midx].back()) > d) { break; } c[midx].pop_back(); } for (int i = 0; i < n; i++) { cerr << c[i].size() << endl; } return all_of(c.begin(), c.end(), [](const vector<int> &vec) { return vec.empty(); }); } int main() { int n, d; while (cin >> n >> d, n) { cout << (solve(n, d) ? "Yes" : "No") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool solve(int n, int d) { vector<vector<int>> c(n); for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { int buf; cin >> buf; c[i].push_back(buf); } } if (n == 1) return true; while (true) { vector<int> sum(n); for (int i = 0; i < n; i++) sum[i] = accumulate(c[i].begin(), c[i].end(), 0); int midx = max_element(sum.begin(), sum.end()) - sum.begin(); int smidx = midx ? 0 : 1; for (int i = 0; i < n; i++) { if (i != midx && sum[smidx] < sum[i]) smidx = i; } bool isok = false; for (int i = 0; i < n && !isok; i++) { if (i == midx) continue; if (c[i].size() > 0 && sum[midx] - (sum[i] - c[i].back()) <= d) { c[i].pop_back(); isok = true; } } if (isok) continue; if (c[midx].size() == 0 || sum[smidx] - (sum[midx] - c[midx].back()) > d) { break; } c[midx].pop_back(); } return all_of(c.begin(), c.end(), [](const vector<int> &vec) { return vec.empty(); }); } int main() { int n, d; while (cin >> n >> d, n) { cout << (solve(n, d) ? "Yes" : "No") << endl; } return 0; }
delete
44
47
44
44
0
0 0 0 2 2 1
p01294
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { for (int n, d; cin >> n >> d, n;) { vector<int> c[100]; int s[100] = {}; int nm = 0; for (int i = 0; i < n; i++) { int m; cin >> m; c[i].resize(m); for (int j = 0; j < m; j++) { cin >> c[i][j]; nm++; s[i] += c[i][j]; } } if (n == 1) { nm = 0; } for (; nm; nm--) { vector<int> v(begin(s), begin(s) + n); sort(v.rbegin(), v.rend()); for (int i = 0; i < n; i++) { if (!v.empty() && ((s[i] == v[0]) ? v[1] : v[0]) - (s[i] - c[i].back()) <= d) { s[i] -= c[i].back(); c[i].pop_back(); goto next; } } break; next:; } cout << (nm ? "No" : "Yes") << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { for (int n, d; cin >> n >> d, n;) { vector<int> c[100]; int s[100] = {}; int nm = 0; for (int i = 0; i < n; i++) { int m; cin >> m; c[i].resize(m); for (int j = 0; j < m; j++) { cin >> c[i][j]; nm++; s[i] += c[i][j]; } } if (n == 1) { nm = 0; } for (; nm; nm--) { vector<int> v(begin(s), begin(s) + n); sort(v.rbegin(), v.rend()); for (int i = 0; i < n; i++) { if (!c[i].empty() && ((s[i] == v[0]) ? v[1] : v[0]) - (s[i] - c[i].back()) <= d) { s[i] -= c[i].back(); c[i].pop_back(); goto next; } } break; next:; } cout << (nm ? "No" : "Yes") << endl; } }
replace
28
29
28
29
0
p01294
C++
Runtime Error
#include <algorithm> #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, d; while (cin >> n >> d && (n || d)) { vector<int> si[100]; int max_potential = -1; for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { int cap; cin >> cap; if (si[i].empty()) si[i].push_back(cap); else si[i].push_back(cap + si[i][si[i].size() - 1]); } if (max_potential < si[i][si[i].size() - 1]) max_potential = si[i][si[i].size() - 1]; } bool update = true; while (update) { update = false; for (int i = 0; i < n; i++) { max_potential = 0; if (si[i].empty()) continue; for (int j = 0; j < n; j++) { if (j != i && !si[j].empty()) max_potential = max(max_potential, si[j][si[j].size() - 1]); } while (!si[i].empty() && ((si[i].size() == 1 && d >= max_potential) || (si[i].size() > 1 && si[i][si[i].size() - 2] + d >= max_potential))) { int c = si[i][si[i].size() - 1]; si[i].pop_back(); update = true; } } } bool f = true; for (int i = 0; i < n; i++) { if (!si[i].empty()) f = false; } if (f) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
#include <algorithm> #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, d; while (cin >> n >> d && (n || d)) { vector<int> si[100]; int max_potential = -1; for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { int cap; cin >> cap; if (si[i].empty()) si[i].push_back(cap); else si[i].push_back(cap + si[i][si[i].size() - 1]); } if (!si[i].empty() && max_potential < si[i][si[i].size() - 1]) max_potential = si[i][si[i].size() - 1]; } bool update = true; while (update) { update = false; for (int i = 0; i < n; i++) { max_potential = 0; if (si[i].empty()) continue; for (int j = 0; j < n; j++) { if (j != i && !si[j].empty()) max_potential = max(max_potential, si[j][si[j].size() - 1]); } while (!si[i].empty() && ((si[i].size() == 1 && d >= max_potential) || (si[i].size() > 1 && si[i][si[i].size() - 2] + d >= max_potential))) { int c = si[i][si[i].size() - 1]; si[i].pop_back(); update = true; } } } bool f = true; for (int i = 0; i < n; i++) { if (!si[i].empty()) f = false; } if (f) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
replace
23
24
23
24
0
p01294
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; int main() { for (int n, d; cin >> n >> d && (n | d);) { vector<vector<int>> c(n); for (int i = 0; i < n; ++i) { int m; cin >> m; c[i].resize(m); for (int j = 0; j < m; ++j) cin >> c[i][j]; } vector<int> sum(n); for (int i = 0; i < n; ++i) { sum[i] = accumulate(c[i].begin(), c[i].end(), 0); } while (1) { bool update = false; for (int i = 0; i < n; ++i) { sum[i] -= c[i].back(); if (*max_element(sum.begin(), sum.end()) - *min_element(sum.begin(), sum.end()) <= d) { c[i].pop_back(); update = true; } else { sum[i] += c[i].back(); } } if (!update) break; } if (accumulate(sum.begin(), sum.end(), 0)) { cout << "No" << endl; } else { cout << "Yes" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; int main() { for (int n, d; cin >> n >> d && (n | d);) { vector<vector<int>> c(n); for (int i = 0; i < n; ++i) { int m; cin >> m; c[i].resize(m); for (int j = 0; j < m; ++j) cin >> c[i][j]; } vector<int> sum(n); for (int i = 0; i < n; ++i) { sum[i] = accumulate(c[i].begin(), c[i].end(), 0); } while (1) { bool update = false; for (int i = 0; i < n; ++i) { if (c[i].empty()) continue; sum[i] -= c[i].back(); if (*max_element(sum.begin(), sum.end()) - *min_element(sum.begin(), sum.end()) <= d) { c[i].pop_back(); update = true; } else { sum[i] += c[i].back(); } } if (!update) break; } if (accumulate(sum.begin(), sum.end(), 0)) { cout << "No" << endl; } else { cout << "Yes" << endl; } } return 0; }
insert
22
22
22
24
0
p01294
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-9 #define INF MOD #define MOD 1000000007LL #define fir first #define iss istringstream #define sst stringstream #define ite iterator #define ll long long #define mp make_pair #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < n; i++) #define pi pair<int, int> #define pb push_back #define sec second #define sh(i) (1LL << i) #define sz size() #define vi vector<int> #define vc vector #define vl vector<ll> #define vs vector<string> int n, d, m[110], s[110], x; int main() { while (cin >> n >> d && n) { vi c[110]; fill(s, s + n, 0); rep(i, n) { cin >> m[i]; rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x; } int f; while (1) { f = 0; rep(i, n) { s[i] -= c[i][m[i] - 1]; if (*max_element(s, s + n) - *min_element(s, s + n) <= d) { c[i].erase(c[i].end() - 1), m[i]--, f = 1; } else s[i] += c[i][m[i] - 1]; } if (!f || !accumulate(m, m + n, 0)) break; } cout << (f ? "Yes" : "No") << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define EPS 1e-9 #define INF MOD #define MOD 1000000007LL #define fir first #define iss istringstream #define sst stringstream #define ite iterator #define ll long long #define mp make_pair #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < n; i++) #define pi pair<int, int> #define pb push_back #define sec second #define sh(i) (1LL << i) #define sz size() #define vi vector<int> #define vc vector #define vl vector<ll> #define vs vector<string> int n, d, m[110], s[110], x; int main() { while (cin >> n >> d && n) { vi c[110]; fill(s, s + n, 0); rep(i, n) { cin >> m[i]; rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x; } int f; while (1) { f = 0; rep(i, n) if (m[i]) { s[i] -= c[i][m[i] - 1]; if (*max_element(s, s + n) - *min_element(s, s + n) <= d) { c[i].erase(c[i].end() - 1), m[i]--, f = 1; } else s[i] += c[i][m[i] - 1]; } if (!f || !accumulate(m, m + n, 0)) break; } cout << (f ? "Yes" : "No") << endl; } }
replace
57
58
57
58
0
p01295
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-10; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ ll solve(ll n) { ll t = 1, d = 1, cnt = 0; while (t * 10 < n) { cnt += (t * 10 - t) * d; t *= 10; d++; } cnt += d * (n - t); return cnt; } int main() { int n, k; while (cin >> n >> k, n) { ll lb = 0, ub = INT_MAX; ll cnt; while (ub - lb > 1) { ll mid = (lb + ub) / 2; cnt = solve(mid); if (cnt >= n) ub = mid; else lb = mid; } string s; REP(i, 100) s += to_string(lb + i); cout << s.substr(n - cnt - 1, k) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; typedef long double ld; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-10; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ ll solve(ll n) { ll t = 1, d = 1, cnt = 0; while (t * 10 < n) { cnt += (t * 10 - t) * d; t *= 10; d++; } cnt += d * (n - t); return cnt; } int main() { int n, k; while (cin >> n >> k, n) { ll lb = 0, ub = INT_MAX; ll cnt; while (ub - lb > 1) { ll mid = (lb + ub) / 2; cnt = solve(mid); if (cnt >= n) ub = mid; else lb = mid; } string s; cnt = solve(lb); REP(i, 100) s += to_string(lb + i); cout << s.substr(n - cnt - 1, k) << endl; } return 0; }
insert
44
44
44
45
0
p01295
C++
Runtime Error
#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 <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #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) const double EPS = 1e-9; const double PI = acos(-1.0); int numdigits(ll n) { int ret = 0; while (n) { n /= 10; ret++; } return ret; } int main() { ll n, k; while (cin >> n >> k, n | k) { int a = -1; ll pos = 0; REP(i, 9) { if (pos + pow(10LL, i) * 9 * (i + 1) > n) { a = i - 1; break; } pos += pow(10LL, i) * 9 * (i + 1); } ll num = pow(10LL, a + 1) - 1; int b = a + 1; while (pos < n && b >= 0) { if (pos + pow(10LL, b) * (a + 2) <= n) { pos += pow(10LL, b) * (a + 2); num += pow(10LL, b); } else { b--; } } if (pos < n) { num++; pos += numdigits(num); } cerr << numdigits(num) - 1 - (pos - n) << "th digit of " << num << " " << endl; ostringstream oss; REP(i, 100) { oss << num + i; } string s = oss.str(); REP(i, k) { cout << s[numdigits(num) - 1 - (pos - n) + i]; } cout << 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 <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #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) const double EPS = 1e-9; const double PI = acos(-1.0); int numdigits(ll n) { int ret = 0; while (n) { n /= 10; ret++; } return ret; } int main() { ll n, k; while (cin >> n >> k, n | k) { int a = -1; ll pos = 0; REP(i, 9) { if (pos + pow(10LL, i) * 9 * (i + 1) > n) { a = i - 1; break; } pos += pow(10LL, i) * 9 * (i + 1); } ll num = pow(10LL, a + 1) - 1; int b = a + 1; while (pos < n && b >= 0) { if (pos + pow(10LL, b) * (a + 2) <= n) { pos += pow(10LL, b) * (a + 2); num += pow(10LL, b); } else { b--; } } if (pos < n) { num++; pos += numdigits(num); } // cerr<<numdigits(num)-1-(pos-n)<<"th digit of " <<num<<" "<<endl; ostringstream oss; REP(i, 100) { oss << num + i; } string s = oss.str(); REP(i, k) { cout << s[numdigits(num) - 1 - (pos - n) + i]; } cout << endl; } }
replace
85
87
85
86
0
0th digit of 4 0th digit of 6
p01295
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll len(ll x) { ll a = 1, k = 0, j = 1; while (a * 10 <= x) k += 9 * a * j, a *= 10, j++; k += (x - a + 1) * j; return k; } int main() { ll n, k; while (cin >> n >> k, n || k) { ll l = 0, r = 100000000LL, m, i; string s; while (l + 1 < r) (len(m = (l + r) / 2) < n) ? l = m : r = m; for (i = 1; i < 10000; i++) s += to_string(l + i); for (i = 0; i < k; i++) cout << s[n - len(l) - 1 + i]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll len(ll x) { ll a = 1, k = 0, j = 1; while (a * 10 <= x) k += 9 * a * j, a *= 10, j++; k += (x - a + 1) * j; return k; } int main() { ll n, k; while (cin >> n >> k, n || k) { ll l = 0, r = 1000000000LL, m, i; string s; while (l + 1 < r) (len(m = (l + r) / 2) < n) ? l = m : r = m; for (i = 1; i < 10000; i++) s += to_string(l + i); for (i = 0; i < k; i++) cout << s[n - len(l) - 1 + i]; cout << endl; } return 0; }
replace
13
14
13
14
0
p01295
C++
Time Limit Exceeded
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { long long int N, K; while (1) { scanf("%lld%lld", &N, &K); long long int base = 9; for (int d = 1; d < 10; d++) { long long int sum = d * base; if (N > sum) { N -= sum; base *= 10; } else { long long int div = (N - 1) / d + base / 9, mo = (N - 1 + d) % d; string s = ""; for (int i = 0; s.length() < K + 10; i++) { s += to_string(div + i); } cout << s.substr(mo, K) << endl; break; } } } }
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { long long int N, K; while (1) { scanf("%lld%lld", &N, &K); if (!N) break; long long int base = 9; for (int d = 1; d < 10; d++) { long long int sum = d * base; if (N > sum) { N -= sum; base *= 10; } else { long long int div = (N - 1) / d + base / 9, mo = (N - 1 + d) % d; string s = ""; for (int i = 0; s.length() < K + 10; i++) { s += to_string(div + i); } cout << s.substr(mo, K) << endl; break; } } } }
insert
9
9
9
11
TLE
p01295
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } int N, K; void solve() { N--; int d = 1, t = 1; while (true) { if (N < t * 9) break; N -= t * 9 * d; d++; t *= 10; } int tmp = N / d; N %= d; t += tmp; stringstream ss; while (ss.str().size() < 120) { ss << t; t++; } string s = ss.str(); cout << s.substr(N, K) << endl; } signed main() { while (cin >> N >> K, N || K) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; #define rep(i, n) for (int i = 0; i < (n); i++) #define reps(i, f, n) for (int i = (f); i < (n); i++) #define all(v) (v).begin(), (v).end() #define each(it, v) \ for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) #define pb push_back #define fi first #define se second template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } int N, K; void solve() { N--; int d = 1, t = 1; while (true) { if (N < t * 9 * d) break; N -= t * 9 * d; d++; t *= 10; } int tmp = N / d; N %= d; t += tmp; stringstream ss; while (ss.str().size() < 120) { ss << t; t++; } string s = ss.str(); cout << s.substr(N, K) << endl; } signed main() { while (cin >> N >> K, N || K) solve(); return 0; }
replace
30
31
30
31
0
p01295
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstring> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { ll order[9]; rep(i, 9) order[i] = (i + 1) * 9 * (ll)pow(10., i); for (int n, k; scanf("%d%d", &n, &k), n;) { int p; for (p = 0; p < 9; p++) { if (n <= order[p]) break; n -= order[p]; } int startnum = (int)pow(10., p) + (n - 1) / (p + 1); int startdgt = (n - 1) % (p + 1); char ans[128] = "", tmp[128]; sprintf(tmp, "%d", startnum); strcat(ans, tmp + startdgt); for (int i = 0, j = startnum + 1; i < k; i += p + 1, j++) { sprintf(tmp, "%d", j); strcat(ans, tmp); } ans[k] = '\0'; puts(ans); } return 0; }
#include <cmath> #include <cstdio> #include <cstring> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { ll order[9]; rep(i, 9) order[i] = (i + 1) * 9 * (ll)pow(10., i); for (int n, k; scanf("%d%d", &n, &k), n;) { int p; for (p = 0; p < 9; p++) { if (n <= order[p]) break; n -= order[p]; } int startnum = (int)pow(10., p) + (n - 1) / (p + 1); int startdgt = (n - 1) % (p + 1); char ans[1024] = "", tmp[1024]; sprintf(tmp, "%d", startnum); strcat(ans, tmp + startdgt); for (int i = 0, j = startnum + 1; i < k; i += p + 1, j++) { sprintf(tmp, "%d", j); strcat(ans, tmp); } ans[k] = '\0'; puts(ans); } return 0; }
replace
24
25
24
25
0
p01296
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; char c; int x[20001][2], y[20001][2]; vector<int> v[40001]; int b[40001], f, n; void dfs(int p, int col) { b[p] = col; r(i, v[p].size()) { if (!(p == v[p][i] + n || p + n == v[p][i]) && b[v[p][i]] && b[v[p][i]] != col) f++; else if (!b[v[p][i]]) { if (p == v[p][i] + n || p + n == v[p][i]) { if (col == 1) dfs(v[p][i], 2); else dfs(v[p][i], 1); } else dfs(v[p][i], col); } } } int main() { while (cin >> n, n) { memset(b, 0, sizeof(b)); r(i, 40001) v[i].clear(); r(i, n) { cin >> x[i][0] >> y[i][0] >> c; if (c == 'x') x[i][1] = x[i][0] + 1, y[i][1] = y[i][0]; else x[i][1] = x[i][0], y[i][1] = y[i][0] + 1; } r(i, n) v[i].push_back(i + n), v[i + n].push_back(i); r(i, n) r(j, n) if (i != j && i < j) r(l, 2) r(k, 2) { if (abs(x[i][l] - x[j][k]) + abs(y[i][l] - y[j][k]) == 1) { int p1 = i, p2 = j; if (l) p1 += n; if (k) p2 += n; v[p1].push_back(p2); v[p2].push_back(p1); } else if (abs(x[i][l] - x[j][k]) == 3 && abs(y[i][l] - y[j][k]) == 0) { int p1 = i, p2 = j; if (l) p1 += n; if (k) p2 += n; v[p1].push_back(p2); v[p2].push_back(p1); } if (abs(x[i][l] - x[j][k]) == 0 && abs(y[i][l] - y[j][k]) == 3) { int p1 = i, p2 = j; if (l) p1 += n; if (k) p2 += n; v[p1].push_back(p2); v[p2].push_back(p1); } } f = 0; r(i, n * 2) if (!b[i]) dfs(i, 1); cout << (f ? "No" : "Yes") << endl; } }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; char c; int x[20001][2], y[20001][2]; vector<int> v[40001]; int b[40001], f, n; void dfs(int p, int col) { b[p] = col; r(i, v[p].size()) { if (!(p == v[p][i] + n || p + n == v[p][i]) && b[v[p][i]] && b[v[p][i]] != col) f++; else if (!b[v[p][i]]) { if (p == v[p][i] + n || p + n == v[p][i]) { if (col == 1) dfs(v[p][i], 2); else dfs(v[p][i], 1); } else dfs(v[p][i], col); } } } int main() { while (cin >> n, n) { memset(b, 0, sizeof(b)); r(i, 40001) v[i].clear(); r(i, n) { cin >> x[i][0] >> y[i][0] >> c; if (c == 'x') x[i][1] = x[i][0] + 1, y[i][1] = y[i][0]; else x[i][1] = x[i][0], y[i][1] = y[i][0] + 1; } r(i, n) v[i].push_back(i + n), v[i + n].push_back(i); r(i, n) r(j, n) if (i != j && i < j) r(l, 2) r(k, 2) { if (abs(x[i][l] - x[j][k]) + abs(y[i][l] - y[j][k]) == 1) { int p1 = i, p2 = j; if (l) p1 += n; if (k) p2 += n; v[p1].push_back(p2); v[p2].push_back(p1); } } f = 0; r(i, n * 2) if (!b[i]) dfs(i, 1); cout << (f ? "No" : "Yes") << endl; } }
delete
45
62
45
45
TLE
p01296
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> P; #define X first #define Y second signed main() { int n; while (cin >> n, n) { P p[n]; char d[n]; for (int i = 0; i < n; i++) cin >> p[i].X >> p[i].Y >> d[i]; map<P, int> m; for (int i = 0; i < n; i++) m[p[i]] = i; int used[n], visit[n]; memset(used, 0, sizeof(used)); bool f = 1; int ax1[] = {-1, 0, 1, 2, -2, -1, 0, 1}; int ay1[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int ad1[] = {1, 0, 1, 1, 1, 1, 0, 1}; int ax2[] = {-1, 0, 1, 2, 2, 1, 0, -1}; int ay2[] = {0, 1, 1, 0, -1, -2, -2, -1}; int ad2[] = {0, 0, 1, 1, 0, 0, 1, 1}; int ax3[] = {0, 1, 1, 1, 0, -1, -1, -1}; int ay3[] = {2, 1, 0, -1, -2, -1, 0, 1}; int ad3[] = {1, 1, 0, 1, 1, 1, 0, 1}; int ax4[] = {-1, 0, 1, 1, 0, -1, -2, -2}; int ay4[] = {-1, -1, 0, 1, 2, 2, 1, 0}; int ad4[] = {1, 0, 0, 1, 1, 0, 0, 1}; for (int i = 0; i < n; i++) { if (used[i]) continue; bool ff = 0; for (int k = 0; k < 2; k++) { bool fff = 1; memset(visit, -1, sizeof(visit)); queue<int> q; q.push(i); visit[i] = k; while (!q.empty()) { int t = q.front(); q.pop(); // cout<<t<<" "<<visit[t]<<endl; if (d[t] == 'x') { for (int j = 0; j < 8; j++) { int nx = p[t].X + ax1[j], ny = p[t].Y + ay1[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'y') continue; // cout<<"a1:"<<u<<endl; if (ad1[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } for (int j = 0; j < 8; j++) { int nx = p[t].X + ax2[j], ny = p[t].Y + ay2[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'x') continue; // cout<<"a2:"<<u<<endl; if (ad2[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } } if (d[t] == 'y') { for (int j = 0; j < 8; j++) { int nx = p[t].X + ax3[j], ny = p[t].Y + ay3[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'x') continue; // cout<<"a3:"<<u<<endl; if (ad3[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } for (int j = 0; j < 8; j++) { int nx = p[t].X + ax4[j], ny = p[t].Y + ay4[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'y') continue; // cout<<"a4:"<<u<<endl; if (ad4[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } } } END: // cout<<"fff:"<<fff<<endl; if (fff) { for (int j = 0; j < n; j++) if (~visit[j]) used[i] = 1; ff = 1; break; } } if (!ff) { f = 0; break; } } cout << (f ? "Yes" : "No") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> P; #define X first #define Y second signed main() { int n; while (cin >> n, n) { P p[n]; char d[n]; for (int i = 0; i < n; i++) cin >> p[i].X >> p[i].Y >> d[i]; map<P, int> m; for (int i = 0; i < n; i++) m[p[i]] = i; int used[n], visit[n]; memset(used, 0, sizeof(used)); bool f = 1; int ax1[] = {-1, 0, 1, 2, -2, -1, 0, 1}; int ay1[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int ad1[] = {1, 0, 1, 1, 1, 1, 0, 1}; int ax2[] = {-1, 0, 1, 2, 2, 1, 0, -1}; int ay2[] = {0, 1, 1, 0, -1, -2, -2, -1}; int ad2[] = {0, 0, 1, 1, 0, 0, 1, 1}; int ax3[] = {0, 1, 1, 1, 0, -1, -1, -1}; int ay3[] = {2, 1, 0, -1, -2, -1, 0, 1}; int ad3[] = {1, 1, 0, 1, 1, 1, 0, 1}; int ax4[] = {-1, 0, 1, 1, 0, -1, -2, -2}; int ay4[] = {-1, -1, 0, 1, 2, 2, 1, 0}; int ad4[] = {1, 0, 0, 1, 1, 0, 0, 1}; for (int i = 0; i < n; i++) { if (used[i]) continue; bool ff = 0; for (int k = 0; k < 2; k++) { bool fff = 1; memset(visit, -1, sizeof(visit)); queue<int> q; q.push(i); visit[i] = k; while (!q.empty()) { int t = q.front(); q.pop(); // cout<<t<<" "<<visit[t]<<endl; if (d[t] == 'x') { for (int j = 0; j < 8; j++) { int nx = p[t].X + ax1[j], ny = p[t].Y + ay1[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'y') continue; // cout<<"a1:"<<u<<endl; if (ad1[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } for (int j = 0; j < 8; j++) { int nx = p[t].X + ax2[j], ny = p[t].Y + ay2[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'x') continue; // cout<<"a2:"<<u<<endl; if (ad2[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } } if (d[t] == 'y') { for (int j = 0; j < 8; j++) { int nx = p[t].X + ax3[j], ny = p[t].Y + ay3[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'x') continue; // cout<<"a3:"<<u<<endl; if (ad3[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } for (int j = 0; j < 8; j++) { int nx = p[t].X + ax4[j], ny = p[t].Y + ay4[j], nv = visit[t]; if (!m.count(P(nx, ny))) continue; int u = m[P(nx, ny)]; if (d[u] == 'y') continue; // cout<<"a4:"<<u<<endl; if (ad4[j]) nv = !nv; if (~visit[u]) { if (visit[u] != nv) { fff = 0; goto END; } } else { visit[u] = nv; q.push(u); } } } } END: // cout<<"fff:"<<fff<<endl; if (fff) { for (int j = 0; j < n; j++) if (~visit[j]) used[j] = 1; ff = 1; break; } } if (!ff) { f = 0; break; } } cout << (f ? "Yes" : "No") << endl; } return 0; }
replace
146
147
146
147
TLE
p01296
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <set> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; 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)) int n; map<pair<int, int>, int> futon; map<pair<int, int>, int> color; int futonX[30000]; int futonY[30000]; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; bool dfs(int x, int y, int c) { if (color[make_pair(x, y)] != -1) { if (color[make_pair(x, y)] == c) { return true; } else { return false; } } color[make_pair(x, y)] = c; REP(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (!futon.count(make_pair(nx, ny))) { continue; } int nc = c; if (futon[make_pair(nx, ny)] == futon[make_pair(x, y)]) { nc ^= 1; } assert(nx != 1 || ny != 0 || nc != 0); if (!dfs(nx, ny, nc)) { return false; } } return true; } int main() { while (scanf("%d", &n), n) { futon.clear(); color.clear(); REP(i, n) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); futonX[i] = x; futonY[i] = y; int dir = c == 'x' ? 0 : 1; REP(j, 2) { int nx = x + dx[dir] * j; int ny = y + dy[dir] * j; futon[make_pair(nx, ny)] = i; color[make_pair(nx, ny)] = -1; } } REP(i, n) { int x = futonX[i]; int y = futonY[i]; if (color[make_pair(x, y)] != -1) { continue; } if (!dfs(x, y, 0)) { puts("No"); goto next; } } puts("Yes"); next:; } }
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <set> #include <stdio.h> #include <string.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; 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)) int n; map<pair<int, int>, int> futon; map<pair<int, int>, int> color; int futonX[30000]; int futonY[30000]; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; bool dfs(int x, int y, int c) { if (color[make_pair(x, y)] != -1) { if (color[make_pair(x, y)] == c) { return true; } else { return false; } } color[make_pair(x, y)] = c; REP(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (!futon.count(make_pair(nx, ny))) { continue; } int nc = c; if (futon[make_pair(nx, ny)] == futon[make_pair(x, y)]) { nc ^= 1; } if (!dfs(nx, ny, nc)) { return false; } } return true; } int main() { while (scanf("%d", &n), n) { futon.clear(); color.clear(); REP(i, n) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); futonX[i] = x; futonY[i] = y; int dir = c == 'x' ? 0 : 1; REP(j, 2) { int nx = x + dx[dir] * j; int ny = y + dy[dir] * j; futon[make_pair(nx, ny)] = i; color[make_pair(nx, ny)] = -1; } } REP(i, n) { int x = futonX[i]; int y = futonY[i]; if (color[make_pair(x, y)] != -1) { continue; } if (!dfs(x, y, 0)) { puts("No"); goto next; } } puts("Yes"); next:; } }
delete
51
52
51
51
0
p01296
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; struct union_find { vector<int> data; union_find(int n) : data(n, -1) {} int find(int a) { return data[a] < 0 ? a : data[a] = find(data[a]); } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (data[a] > data[b]) swap(a, b); data[a] += data[b]; data[b] = a; } }; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { ios::sync_with_stdio(false), cin.tie(0); int n; while (cin >> n, n) { vector<int> x(n), y(n); vector<char> d(n); map<pii, pii> pos; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> d[i]; pos[make_pair(x[i], y[i])] = make_pair(i, 0); (d[i] == 'x' ? pos[make_pair(x[i] + 1, y[i])] : pos[make_pair(x[i], y[i] + 1)]) = make_pair(i, 1); } bool ok = true; union_find uf(n * 2); for (int i = 0; i < n; i++) { int nx = x[i], ny = y[i]; for (int a = 0; a < 2; a++) { for (int j = 0; j < 4; j++) { int tx = nx + dx[i], ty = ny + dy[i]; pii tp(tx, ty); if (pos.count(tp) && pos[tp].first != i) { if (a == pos[tp].second) { int u = i, v = pos[tp].first; uf.unite(u, v); uf.unite(n + u, n + v); if (uf.same(u, n + u) || uf.same(v, n + v)) { ok = false; } } else { int u = i, v = pos[tp].first; uf.unite(u, n + v); uf.unite(n + u, v); if (uf.same(u, n + u) || uf.same(v, n + v)) { ok = false; } } } } (d[i] == 'x' ? nx : ny) += 1; } } puts(ok ? "Yes" : "No"); } return 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; struct union_find { vector<int> data; union_find(int n) : data(n, -1) {} int find(int a) { return data[a] < 0 ? a : data[a] = find(data[a]); } bool same(int a, int b) { return find(a) == find(b); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (data[a] > data[b]) swap(a, b); data[a] += data[b]; data[b] = a; } }; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { ios::sync_with_stdio(false), cin.tie(0); int n; while (cin >> n, n) { vector<int> x(n), y(n); vector<char> d(n); map<pii, pii> pos; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> d[i]; pos[make_pair(x[i], y[i])] = make_pair(i, 0); (d[i] == 'x' ? pos[make_pair(x[i] + 1, y[i])] : pos[make_pair(x[i], y[i] + 1)]) = make_pair(i, 1); } bool ok = true; union_find uf(n * 2); for (int i = 0; i < n; i++) { int nx = x[i], ny = y[i]; for (int a = 0; a < 2; a++) { for (int j = 0; j < 4; j++) { int tx = nx + dx[j], ty = ny + dy[j]; pii tp(tx, ty); if (pos.count(tp) && pos[tp].first != i) { if (a == pos[tp].second) { int u = i, v = pos[tp].first; uf.unite(u, v); uf.unite(n + u, n + v); if (uf.same(u, n + u) || uf.same(v, n + v)) { ok = false; } } else { int u = i, v = pos[tp].first; uf.unite(u, n + v); uf.unite(n + u, v); if (uf.same(u, n + u) || uf.same(v, n + v)) { ok = false; } } } } (d[i] == 'x' ? nx : ny) += 1; } } puts(ok ? "Yes" : "No"); } return 0; }
replace
43
44
43
44
0
p01297
C++
Time Limit Exceeded
//////////////////////////////////////// /// tu3 pro-con template /// //////////////////////////////////////// #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; //// MACRO //// #define countof(a) (sizeof(a) / sizeof(a[0])) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define FOR(i, s, n) for (int i = (s); i < (n); i++) #define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--) #define pos(c, i) c.being() + (i) #define allof(c) c.begin(), c.end() #define aallof(a) a, countof(a) #define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n) #define apartof(a, i, n) a + (i), a + (i) + (n) #define long long long #define EPS 1e-9 #define INF (1L << 30) #define LINF (1LL << 60) #define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; } #define COMPARISON_T(t) bool (*)(const t &, const t &) #define COMPARISON(t, a, b, exp) \ [&](const t &a, const t &b) -> bool { return exp; } #define CONVERTER(TSrc, t, TDest, exp) \ [&](const TSrc &t) -> TDest { return exp; } inline int sign(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } //// i/o helper //// struct _Reader { template <class T> _Reader operator,(T &rhs) { cin >> rhs; return *this; } }; struct _Writer { bool f; _Writer() : f(false) {} template <class T> _Writer operator,(const T &rhs) { cout << (f ? " " : "") << rhs; f = true; return *this; } }; #define READ(t, ...) \ t __VA_ARGS__; \ _Reader(), __VA_ARGS__ #define WRITE(...) \ _Writer(), __VA_ARGS__; \ cout << endl template <class T> struct vevector : public vector<vector<T>> { vevector(int n = 0, int m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) {} }; template <class T> struct vevevector : public vector<vevector<T>> { vevevector(int n = 0, int m = 0, int l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {} }; template <class T> struct vevevevector : public vector<vevevector<T>> { vevevevector(int n = 0, int m = 0, int l = 0, int k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {} }; template <class T> T read() { T t; cin >> t; return t; } template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; } template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; } template <class T> vevector<T> readjag() { return read<T>(read<int>()); } template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; } template <class T1, class T2> inline istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T1, class T2> inline ostream &operator<<(ostream &out, pair<T1, T2> &p) { out << p.first << p.second; return out; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &v) { ostringstream ss; for (auto x : v) ss << x << ' '; auto s = ss.str(); out << s.substr(0, s.length() - 1) << endl; return out; } /// 2次元 struct P2 { double x, y; P2(double x = 0, double y = 0) : x(x), y(y) {} P2(complex<double> c) : x(c.real()), y(c.imag()) {} P2 operator+() const { return *this; } P2 operator+(const P2 &_) const { return P2(x + _.x, y + _.y); } P2 operator-() const { return P2(-x, -y); } P2 operator-(const P2 &_) const { return *this + -_; } P2 operator*(double _) const { return P2(x * _, y * _); } P2 operator/(double _) const { return P2(x / _, y / _); } double dot(const P2 &_) const { return x * _.x + y * _.y; } // 内積 double cross(const P2 &_) const { return x * _.y - y * _.x; } // 外積 double sqlength() const { return x * x + y * y; } // 二乗長さ double length() const { return sqrt(sqlength()); } // 長さ P2 orthogonal() const { return P2(y, -x); } P2 direction() const { return *this / length(); } // 方向ベクトル }; inline istream &operator>>(istream &in, P2 &p) { in >> p.x >> p.y; return in; } inline ostream &operator<<(ostream &out, const P2 &p) { out << p.x << ' ' << p.y; return out; } inline double abs(P2 p2) { return p2.length(); } inline P2 orthogonal(P2 p) { return p.orthogonal(); } inline complex<double> orthogonal(complex<double> c) { return c * complex<double>(0, 1); } // a,b から ちょうど d だけ離れた点。aとbを円周に持つ円の半径。 inline pair<P2, P2> get_same_distance_points(P2 a, P2 b, double d) { assert(abs(a - b) <= 2 * d + EPS); auto v = (a + b) / 2.0 - a; // a から aとbの中点 auto vl = abs(v); auto wl = sqrt(d * d - vl * vl); // 直行Vの大きさ auto w = orthogonal(v) * (wl / vl); // 直行V return make_pair(a + v + w, a + v - w); } // a から b に向かって、cが右手か左手か。 inline int clockwise(P2 a, P2 b, P2 c) { const P2 u = b - a, v = c - a; if (u.cross(v) > EPS) { return 1; } if (u.cross(v) < -EPS) { return -1; } if (u.dot(v) < -EPS) { return -1; } if (u.sqlength() < v.sqlength() - EPS) { return 1; } return 0; } /// 直線 struct Line { P2 p, d; explicit Line(P2 pos = P2(), P2 dir = P2()) : p(pos), d(dir) {} static Line FromPD(P2 pos, P2 dir) { return Line(pos, dir); } static Line From2Point(P2 a, P2 b) { return Line(a, b - a); } }; /// 線分 struct LineSeg { P2 p, d; explicit LineSeg(P2 pos = P2(), P2 dir = P2()) : p(pos), d(dir) {} static LineSeg FromPD(P2 pos, P2 dir) { return LineSeg(pos, dir); } static LineSeg From2Point(P2 a, P2 b) { return LineSeg(a, b - a); } operator Line() { return Line(p, d); } }; inline P2 projective(P2 a, P2 b) { return a * (a.dot(b) / a.length()); } inline P2 perpendicular_foot(P2 a, Line b) { Line l = Line(b.p - a, b.d); return a + l.p - projective(l.p, l.d); } inline LineSeg projective(LineSeg a, Line b) { return LineSeg(perpendicular_foot(a.p, b), projective(a.d, b.d)); } // 交点 inline P2 crossPoint(Line a, Line b) { return a.p + a.d * (b.d.cross(b.p - a.p) / b.d.cross(a.d)); } // 包括判定 inline bool contains(LineSeg a, P2 p) { return abs(a.p - p) + abs(a.p + a.d - p) - abs(a.d) < EPS; } // 交差判定 inline bool isCross(Line a, Line b) { return abs(a.d.cross(b.d)) > EPS; } inline bool isCross(Line a, LineSeg b) { return sign(a.d.cross(b.p - a.p)) * sign(a.d.cross(b.p + b.d - a.d)) <= 0; } inline bool isCross(LineSeg a, Line b) { return isCross(b, a); } inline bool isCross(LineSeg a, LineSeg b) { P2 ae = a.p + a.d, be = b.p + b.d; return clockwise(a.p, ae, b.p) * clockwise(a.p, ae, be) <= 0 && clockwise(b.p, be, a.p) * clockwise(b.p, be, ae) <= 0; } // 重なり判定 inline bool isOverlap(Line a, Line b) { return abs(a.d.cross(b.p - a.p)) < EPS; } inline bool isOverlap(Line a, LineSeg b) { return isOverlap(a, Line(b.p, b.d)); } inline bool isOverlap(LineSeg a, Line b) { return isOverlap(b, a); } inline bool isOverlap(LineSeg a, LineSeg b) { return contains(a, b.p) || contains(a, b.p + b.d) || contains(b, a.p) || contains(b, a.p + a.d); } // 距離 inline double getDistance(P2 a, P2 b) { return (a - b).length(); } inline double getDistance(P2 a, Line b) { return abs(b.d.cross(a - b.p) / b.d.length()); } inline double getDistance(P2 a, LineSeg b) { return min(getDistance(perpendicular_foot(a, (Line)b), a), min(getDistance(b.p, a), getDistance(b.p + b.d, a))); } inline double getDistance(Line a, P2 b) { return getDistance(b, a); } inline double getDistance(Line a, Line b) { return isCross(a, b) ? 0 : getDistance(a, b.p); } inline double getDistance(Line a, LineSeg b) { return isCross(a, b) ? 0 : min(getDistance(a, b.p), getDistance(a, b.p + b.d)); } inline double getDistance(LineSeg a, P2 b) { return getDistance(b, a); } inline double getDistance(LineSeg a, Line b) { return getDistance(b, a); } inline double getDistance(LineSeg a, LineSeg b) { return isCross(a, b) ? 0 : min(min(getDistance(a.p, b.p), getDistance(a.p, b.p + b.d)), min(getDistance(a.p + a.d, b.p), getDistance(a.p + a.d, b.p + b.d))); } // a から ta, bから tb だけ離れた点。ta=tb=r なら aとbに内接する円 inline pair<pair<P2, P2>, pair<P2, P2>> get_distance_points(Line a, double ta, Line b, double tb) { assert(isCross(a, b)); P2 va = a.d.orthogonal().direction() * ta; P2 vb = b.d.orthogonal().direction() * tb; return make_pair( make_pair(crossPoint(Line(a.p + va, a.d), Line(b.p + vb, b.d)), crossPoint(Line(a.p + va, a.d), Line(b.p - vb, b.d))), make_pair(crossPoint(Line(a.p - va, a.d), Line(b.p + vb, b.d)), crossPoint(Line(a.p - va, a.d), Line(b.p - vb, b.d)))); } /// 円 struct Circle { P2 c; double r; Circle() : c(), r() {} Circle(double x, double y, double r) : c(x, y), r(r) {} Circle(P2 c, double r) : c(c), r(r) {} bool IntersectWith(const Circle &rhs) const { return abs(c - rhs.c) - (r + rhs.r) < EPS; } // 接してても真。 bool Contains(const P2 &p) const { return abs(p - c) - r < EPS; } // 接してても真。 }; inline istream &operator>>(istream &in, Circle &c) { in >> c.c >> c.r; return in; } // 交差している2円の交点を求める。角度の小さい方から出る(右下方向が正なら時計回り) inline pair<P2, P2> crossPoint(Circle A, Circle B) { P2 v = B.c - A.c; P2 dir = v.direction(); // 他方の中心への方向 double d = v.length(); // 他方の中心への距離 double lh = (A.r * A.r + d * d - B.r * B.r) / (2 * d); // 垂線の足までの距離 P2 h = A.c + dir * lh; // 垂線の足 double lp = sqrt(A.r * A.r - lh * lh); // 垂線の足から交点までの距離 P2 p = dir.orthogonal() * lp; // 垂線の足から交点へのベクトル return make_pair(h + p, h - p); // 交点の組。 } /// 長方形 struct Rect { P2 l, s; Rect() : l(), s() {} Rect(double x, double y, double w, double h) : l(x, y), s(w, h) {} Rect(P2 location, P2 size) : l(location), s(size) {} bool Contains(const P2 &p) const { return p.x - l.x > -EPS && p.y - l.y > -EPS && p.x - (l.x + s.x) < EPS && p.y - (l.y + s.y) < EPS; } // 接してても真。 }; //// start up //// void solve(); int main() { freopen("E:/My Documents/Downloads/regional-2009-data/E.txt", "r", stdin); freopen("E:/My Documents/Downloads/regional-2009-data/E.out.txt", "w", stdout); solve(); return 0; } //////////////////// /// template end /// //////////////////// void solve() { int cases = INF; REP(_, cases) { READ(int, W, H, N, R); if (!W) { break; } struct Laser { Line line; double thick; }; vector<Laser> beams; REP(i, N) { READ(double, x, y, z, w, t); beams.push_back({Line::From2Point(P2(x, y), P2(z, w)), t}); } // 画面端も死ぬ beams.push_back({Line::From2Point(P2(0, 0), P2(W, 0)), 0}); beams.push_back({Line::From2Point(P2(W, 0), P2(W, H)), 0}); beams.push_back({Line::From2Point(P2(W, H), P2(0, H)), 0}); beams.push_back({Line::From2Point(P2(0, H), P2(0, 0)), 0}); // 移動可能範囲 Rect screen(R, R, W - R, H - R); bool ok = false; for (auto i : beams) for (auto j : beams) { // WRITE("i:", i.line.p, i.thick, "j:", j.line.p, j.thick); if (isCross(i.line, j.line)) { // 点の候補 auto cand = get_distance_points(i.line, i.thick + R, j.line, j.thick + R); P2 ps[] = { cand.first.first, cand.first.second, cand.second.first, cand.second.second, }; for (auto p : ps) { // その位置は、移動可能でかつすべてのレーザーに当たらない? bool test = true; test &= screen.Contains(p); if (!test) continue; // WRITE(p); for (auto k : beams) { // WRITE("k:", k.line.p, k.thick, // "distance:", getDistance(k.line, p), // "need:", (k.thick + R), // "is ok? ", (getDistance(k.line, p) - (k.thick + R) > //-EPS)); test &= (getDistance(k.line, p) - (k.thick + R) > -EPS); } ok |= test; // WRITE(p, "ok? ", ok); if (ok) break; } } else { // 平行……。今回は画面端があるので、平行は考えなくてよい。 } } WRITE(ok ? "Yes" : "No"); } }
//////////////////////////////////////// /// tu3 pro-con template /// //////////////////////////////////////// #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; //// MACRO //// #define countof(a) (sizeof(a) / sizeof(a[0])) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define FOR(i, s, n) for (int i = (s); i < (n); i++) #define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--) #define pos(c, i) c.being() + (i) #define allof(c) c.begin(), c.end() #define aallof(a) a, countof(a) #define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n) #define apartof(a, i, n) a + (i), a + (i) + (n) #define long long long #define EPS 1e-9 #define INF (1L << 30) #define LINF (1LL << 60) #define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; } #define COMPARISON_T(t) bool (*)(const t &, const t &) #define COMPARISON(t, a, b, exp) \ [&](const t &a, const t &b) -> bool { return exp; } #define CONVERTER(TSrc, t, TDest, exp) \ [&](const TSrc &t) -> TDest { return exp; } inline int sign(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } //// i/o helper //// struct _Reader { template <class T> _Reader operator,(T &rhs) { cin >> rhs; return *this; } }; struct _Writer { bool f; _Writer() : f(false) {} template <class T> _Writer operator,(const T &rhs) { cout << (f ? " " : "") << rhs; f = true; return *this; } }; #define READ(t, ...) \ t __VA_ARGS__; \ _Reader(), __VA_ARGS__ #define WRITE(...) \ _Writer(), __VA_ARGS__; \ cout << endl template <class T> struct vevector : public vector<vector<T>> { vevector(int n = 0, int m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) {} }; template <class T> struct vevevector : public vector<vevector<T>> { vevevector(int n = 0, int m = 0, int l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {} }; template <class T> struct vevevevector : public vector<vevevector<T>> { vevevevector(int n = 0, int m = 0, int l = 0, int k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {} }; template <class T> T read() { T t; cin >> t; return t; } template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; } template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; } template <class T> vevector<T> readjag() { return read<T>(read<int>()); } template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; } template <class T1, class T2> inline istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T1, class T2> inline ostream &operator<<(ostream &out, pair<T1, T2> &p) { out << p.first << p.second; return out; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &v) { ostringstream ss; for (auto x : v) ss << x << ' '; auto s = ss.str(); out << s.substr(0, s.length() - 1) << endl; return out; } /// 2次元 struct P2 { double x, y; P2(double x = 0, double y = 0) : x(x), y(y) {} P2(complex<double> c) : x(c.real()), y(c.imag()) {} P2 operator+() const { return *this; } P2 operator+(const P2 &_) const { return P2(x + _.x, y + _.y); } P2 operator-() const { return P2(-x, -y); } P2 operator-(const P2 &_) const { return *this + -_; } P2 operator*(double _) const { return P2(x * _, y * _); } P2 operator/(double _) const { return P2(x / _, y / _); } double dot(const P2 &_) const { return x * _.x + y * _.y; } // 内積 double cross(const P2 &_) const { return x * _.y - y * _.x; } // 外積 double sqlength() const { return x * x + y * y; } // 二乗長さ double length() const { return sqrt(sqlength()); } // 長さ P2 orthogonal() const { return P2(y, -x); } P2 direction() const { return *this / length(); } // 方向ベクトル }; inline istream &operator>>(istream &in, P2 &p) { in >> p.x >> p.y; return in; } inline ostream &operator<<(ostream &out, const P2 &p) { out << p.x << ' ' << p.y; return out; } inline double abs(P2 p2) { return p2.length(); } inline P2 orthogonal(P2 p) { return p.orthogonal(); } inline complex<double> orthogonal(complex<double> c) { return c * complex<double>(0, 1); } // a,b から ちょうど d だけ離れた点。aとbを円周に持つ円の半径。 inline pair<P2, P2> get_same_distance_points(P2 a, P2 b, double d) { assert(abs(a - b) <= 2 * d + EPS); auto v = (a + b) / 2.0 - a; // a から aとbの中点 auto vl = abs(v); auto wl = sqrt(d * d - vl * vl); // 直行Vの大きさ auto w = orthogonal(v) * (wl / vl); // 直行V return make_pair(a + v + w, a + v - w); } // a から b に向かって、cが右手か左手か。 inline int clockwise(P2 a, P2 b, P2 c) { const P2 u = b - a, v = c - a; if (u.cross(v) > EPS) { return 1; } if (u.cross(v) < -EPS) { return -1; } if (u.dot(v) < -EPS) { return -1; } if (u.sqlength() < v.sqlength() - EPS) { return 1; } return 0; } /// 直線 struct Line { P2 p, d; explicit Line(P2 pos = P2(), P2 dir = P2()) : p(pos), d(dir) {} static Line FromPD(P2 pos, P2 dir) { return Line(pos, dir); } static Line From2Point(P2 a, P2 b) { return Line(a, b - a); } }; /// 線分 struct LineSeg { P2 p, d; explicit LineSeg(P2 pos = P2(), P2 dir = P2()) : p(pos), d(dir) {} static LineSeg FromPD(P2 pos, P2 dir) { return LineSeg(pos, dir); } static LineSeg From2Point(P2 a, P2 b) { return LineSeg(a, b - a); } operator Line() { return Line(p, d); } }; inline P2 projective(P2 a, P2 b) { return a * (a.dot(b) / a.length()); } inline P2 perpendicular_foot(P2 a, Line b) { Line l = Line(b.p - a, b.d); return a + l.p - projective(l.p, l.d); } inline LineSeg projective(LineSeg a, Line b) { return LineSeg(perpendicular_foot(a.p, b), projective(a.d, b.d)); } // 交点 inline P2 crossPoint(Line a, Line b) { return a.p + a.d * (b.d.cross(b.p - a.p) / b.d.cross(a.d)); } // 包括判定 inline bool contains(LineSeg a, P2 p) { return abs(a.p - p) + abs(a.p + a.d - p) - abs(a.d) < EPS; } // 交差判定 inline bool isCross(Line a, Line b) { return abs(a.d.cross(b.d)) > EPS; } inline bool isCross(Line a, LineSeg b) { return sign(a.d.cross(b.p - a.p)) * sign(a.d.cross(b.p + b.d - a.d)) <= 0; } inline bool isCross(LineSeg a, Line b) { return isCross(b, a); } inline bool isCross(LineSeg a, LineSeg b) { P2 ae = a.p + a.d, be = b.p + b.d; return clockwise(a.p, ae, b.p) * clockwise(a.p, ae, be) <= 0 && clockwise(b.p, be, a.p) * clockwise(b.p, be, ae) <= 0; } // 重なり判定 inline bool isOverlap(Line a, Line b) { return abs(a.d.cross(b.p - a.p)) < EPS; } inline bool isOverlap(Line a, LineSeg b) { return isOverlap(a, Line(b.p, b.d)); } inline bool isOverlap(LineSeg a, Line b) { return isOverlap(b, a); } inline bool isOverlap(LineSeg a, LineSeg b) { return contains(a, b.p) || contains(a, b.p + b.d) || contains(b, a.p) || contains(b, a.p + a.d); } // 距離 inline double getDistance(P2 a, P2 b) { return (a - b).length(); } inline double getDistance(P2 a, Line b) { return abs(b.d.cross(a - b.p) / b.d.length()); } inline double getDistance(P2 a, LineSeg b) { return min(getDistance(perpendicular_foot(a, (Line)b), a), min(getDistance(b.p, a), getDistance(b.p + b.d, a))); } inline double getDistance(Line a, P2 b) { return getDistance(b, a); } inline double getDistance(Line a, Line b) { return isCross(a, b) ? 0 : getDistance(a, b.p); } inline double getDistance(Line a, LineSeg b) { return isCross(a, b) ? 0 : min(getDistance(a, b.p), getDistance(a, b.p + b.d)); } inline double getDistance(LineSeg a, P2 b) { return getDistance(b, a); } inline double getDistance(LineSeg a, Line b) { return getDistance(b, a); } inline double getDistance(LineSeg a, LineSeg b) { return isCross(a, b) ? 0 : min(min(getDistance(a.p, b.p), getDistance(a.p, b.p + b.d)), min(getDistance(a.p + a.d, b.p), getDistance(a.p + a.d, b.p + b.d))); } // a から ta, bから tb だけ離れた点。ta=tb=r なら aとbに内接する円 inline pair<pair<P2, P2>, pair<P2, P2>> get_distance_points(Line a, double ta, Line b, double tb) { assert(isCross(a, b)); P2 va = a.d.orthogonal().direction() * ta; P2 vb = b.d.orthogonal().direction() * tb; return make_pair( make_pair(crossPoint(Line(a.p + va, a.d), Line(b.p + vb, b.d)), crossPoint(Line(a.p + va, a.d), Line(b.p - vb, b.d))), make_pair(crossPoint(Line(a.p - va, a.d), Line(b.p + vb, b.d)), crossPoint(Line(a.p - va, a.d), Line(b.p - vb, b.d)))); } /// 円 struct Circle { P2 c; double r; Circle() : c(), r() {} Circle(double x, double y, double r) : c(x, y), r(r) {} Circle(P2 c, double r) : c(c), r(r) {} bool IntersectWith(const Circle &rhs) const { return abs(c - rhs.c) - (r + rhs.r) < EPS; } // 接してても真。 bool Contains(const P2 &p) const { return abs(p - c) - r < EPS; } // 接してても真。 }; inline istream &operator>>(istream &in, Circle &c) { in >> c.c >> c.r; return in; } // 交差している2円の交点を求める。角度の小さい方から出る(右下方向が正なら時計回り) inline pair<P2, P2> crossPoint(Circle A, Circle B) { P2 v = B.c - A.c; P2 dir = v.direction(); // 他方の中心への方向 double d = v.length(); // 他方の中心への距離 double lh = (A.r * A.r + d * d - B.r * B.r) / (2 * d); // 垂線の足までの距離 P2 h = A.c + dir * lh; // 垂線の足 double lp = sqrt(A.r * A.r - lh * lh); // 垂線の足から交点までの距離 P2 p = dir.orthogonal() * lp; // 垂線の足から交点へのベクトル return make_pair(h + p, h - p); // 交点の組。 } /// 長方形 struct Rect { P2 l, s; Rect() : l(), s() {} Rect(double x, double y, double w, double h) : l(x, y), s(w, h) {} Rect(P2 location, P2 size) : l(location), s(size) {} bool Contains(const P2 &p) const { return p.x - l.x > -EPS && p.y - l.y > -EPS && p.x - (l.x + s.x) < EPS && p.y - (l.y + s.y) < EPS; } // 接してても真。 }; //// start up //// void solve(); int main() { solve(); return 0; } //////////////////// /// template end /// //////////////////// void solve() { int cases = INF; REP(_, cases) { READ(int, W, H, N, R); if (!W) { break; } struct Laser { Line line; double thick; }; vector<Laser> beams; REP(i, N) { READ(double, x, y, z, w, t); beams.push_back({Line::From2Point(P2(x, y), P2(z, w)), t}); } // 画面端も死ぬ beams.push_back({Line::From2Point(P2(0, 0), P2(W, 0)), 0}); beams.push_back({Line::From2Point(P2(W, 0), P2(W, H)), 0}); beams.push_back({Line::From2Point(P2(W, H), P2(0, H)), 0}); beams.push_back({Line::From2Point(P2(0, H), P2(0, 0)), 0}); // 移動可能範囲 Rect screen(R, R, W - R, H - R); bool ok = false; for (auto i : beams) for (auto j : beams) { // WRITE("i:", i.line.p, i.thick, "j:", j.line.p, j.thick); if (isCross(i.line, j.line)) { // 点の候補 auto cand = get_distance_points(i.line, i.thick + R, j.line, j.thick + R); P2 ps[] = { cand.first.first, cand.first.second, cand.second.first, cand.second.second, }; for (auto p : ps) { // その位置は、移動可能でかつすべてのレーザーに当たらない? bool test = true; test &= screen.Contains(p); if (!test) continue; // WRITE(p); for (auto k : beams) { // WRITE("k:", k.line.p, k.thick, // "distance:", getDistance(k.line, p), // "need:", (k.thick + R), // "is ok? ", (getDistance(k.line, p) - (k.thick + R) > //-EPS)); test &= (getDistance(k.line, p) - (k.thick + R) > -EPS); } ok |= test; // WRITE(p, "ok? ", ok); if (ok) break; } } else { // 平行……。今回は画面端があるので、平行は考えなくてよい。 } } WRITE(ok ? "Yes" : "No"); } }
delete
339
343
339
339
TLE
p01298
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ const ld day = 86400; int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); int n, l; while (cin >> n >> l, n) { vector<pair<pii, int>> v; vi use(day); REP(i, n) { int s, t, u; cin >> s >> t >> u; FOR(j, s, t) use[j] = u; } ld lb = 0, ub = 1000000; REP(bs, 200) { ld mid = (lb + ub) / 2.0; ld vol = l; ld tmp; REP(i, day) { vol -= use[i]; vol += mid; if (vol > l) vol = l; if (vol < 0) { lb = mid; goto end; } } tmp = vol; REP(i, day) { vol -= use[i]; vol += mid; if (vol > l) vol = l; if (vol < 0) { lb = mid; goto end; } } if (tmp > vol) lb = mid; else ub = mid; end:; } cout << lb << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ const ld day = 86400; int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); int n, l; while (cin >> n >> l, n) { vector<pair<pii, int>> v; vi use(day); REP(i, n) { int s, t, u; cin >> s >> t >> u; FOR(j, s, t) use[j] = u; } ld lb = 0, ub = 1000000; REP(bs, 150) { ld mid = (lb + ub) / 2.0; ld vol = l; ld tmp; REP(i, day) { vol -= use[i]; vol += mid; if (vol > l) vol = l; if (vol < 0) { lb = mid; goto end; } } tmp = vol; REP(i, day) { vol -= use[i]; vol += mid; if (vol > l) vol = l; if (vol < 0) { lb = mid; goto end; } } if (tmp > vol) lb = mid; else ub = mid; end:; } cout << lb << endl; } return 0; }
replace
34
35
34
35
TLE
p01298
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, a) repi(i, 0, a) #define repd(i, a, b) for (int i = (a); i >= (b); i--) #define repit(i, a) \ for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define UNIQUE(u) (u).erase(unique(all(u)), (u).end()) #define pb push_back #define mp make_pair #define INF 1e9 #define EPS 1e-12 #define PI acos(-1.0) using namespace std; typedef long long ll; typedef vector<int> vi; int n, l; vector<double> s, t, u; bool C3(double v, double start) { double volume = start; double before = 0; rep(i, n) { volume += (s[i] - before) * v; if (volume > l) volume = l; double d = v - u[i]; volume += d * (t[i] - s[i]); if (volume > l) volume = l; else if (volume < -EPS) return false; before = t[i]; } return true; } bool C2(double v, double start) { double volume = start; double before = 0; rep(i, n) { volume += (s[i] - before) * v; if (volume > l) volume = l; double d = v - u[i]; volume += d * (t[i] - s[i]); if (volume > l) volume = l; // else if(volume < -EPS) return false; before = t[i]; } volume += (86400 - before) * v; return volume > start - EPS; } bool C(double v) { double lower = 0; double upper = l; double mid; double tl; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C3(v, mid)) upper = mid; else lower = mid; // cout << mid << endl; } // cout << v << " " << lower << " " << upper << endl; if (l == upper) return false; tl = lower; upper = l; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C2(v, mid)) lower = mid; else upper = mid; } return upper > tl + EPS * 10.0; } int main() { while (cin >> n >> l, n || l) { s.resize(n); t.resize(n); u.resize(n); rep(i, n) cin >> s[i] >> t[i] >> u[i]; double lower = 0; double upper = INF; double mid; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C(mid)) upper = mid; else lower = mid; } printf("%.6f\n", upper); } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, a) repi(i, 0, a) #define repd(i, a, b) for (int i = (a); i >= (b); i--) #define repit(i, a) \ for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define UNIQUE(u) (u).erase(unique(all(u)), (u).end()) #define pb push_back #define mp make_pair #define INF 1e9 #define EPS 1e-8 #define PI acos(-1.0) using namespace std; typedef long long ll; typedef vector<int> vi; int n, l; vector<double> s, t, u; bool C3(double v, double start) { double volume = start; double before = 0; rep(i, n) { volume += (s[i] - before) * v; if (volume > l) volume = l; double d = v - u[i]; volume += d * (t[i] - s[i]); if (volume > l) volume = l; else if (volume < -EPS) return false; before = t[i]; } return true; } bool C2(double v, double start) { double volume = start; double before = 0; rep(i, n) { volume += (s[i] - before) * v; if (volume > l) volume = l; double d = v - u[i]; volume += d * (t[i] - s[i]); if (volume > l) volume = l; // else if(volume < -EPS) return false; before = t[i]; } volume += (86400 - before) * v; return volume > start - EPS; } bool C(double v) { double lower = 0; double upper = l; double mid; double tl; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C3(v, mid)) upper = mid; else lower = mid; // cout << mid << endl; } // cout << v << " " << lower << " " << upper << endl; if (l == upper) return false; tl = lower; upper = l; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C2(v, mid)) lower = mid; else upper = mid; } return upper > tl + EPS * 10.0; } int main() { while (cin >> n >> l, n || l) { s.resize(n); t.resize(n); u.resize(n); rep(i, n) cin >> s[i] >> t[i] >> u[i]; double lower = 0; double upper = INF; double mid; while (upper - lower > EPS) { mid = (lower + upper) / 2.0; if (C(mid)) upper = mid; else lower = mid; } printf("%.6f\n", upper); } return 0; }
replace
27
28
27
28
TLE
p01298
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> P; const double EPS = 1e-12; const int INF = numeric_limits<int>::max() / 2; const int MOD = 1e9 + 7; vector<double> cap(200000, 0); int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n, n) { double l; cin >> l; double low = 0, high = 2000000; vector<double> v(200000, 0); for (int i = 0; i < n; i++) { int st, gt; cin >> st >> gt; double u; cin >> u; for (int j = st + 1; j <= gt; j++) { v[j] = v[j + 86400] = u; } for (int k = 0; k < 50; k++) { cap[0] = l; double mid = (low + high) / 2; bool f = true; for (int i = 1; i <= 86400 * 2; i++) { cap[i] = min(l, cap[i - 1] + (mid - v[i])); if (cap[i] < 0) f = false; } if (cap[86400] <= cap[86400 * 2] && f) { high = mid; } else { low = mid; } } } cout << fixed << setprecision(10) << low << endl; } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> P; const double EPS = 1e-12; const int INF = numeric_limits<int>::max() / 2; const int MOD = 1e9 + 7; vector<double> cap(200000, 0); int main() { cin.tie(0); ios::sync_with_stdio(false); int n; while (cin >> n, n) { double l; cin >> l; double low = 0, high = 2000000; vector<double> v(200000, 0); for (int i = 0; i < n; i++) { int st, gt; cin >> st >> gt; double u; cin >> u; for (int j = st + 1; j <= gt; j++) { v[j] = v[j + 86400] = u; } } for (int k = 0; k < 100; k++) { cap[0] = l; double mid = (low + high) / 2; bool f = true; for (int i = 1; i <= 86400 * 2; i++) { cap[i] = min(l, cap[i - 1] + (mid - v[i])); if (cap[i] < 0) f = false; } if (cap[86400] <= cap[86400 * 2] && f) { high = mid; } else { low = mid; } } cout << fixed << setprecision(10) << low << endl; } }
replace
30
44
30
44
TLE
p01298
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) ((abs((a) - (b))) < EPS) int a[100001]; int n, l; bool check(double f) { double lf = l; double rec = 0; bool flag = false; for (int j = 0; j < 3; j++) { for (int i = 1; i <= 86400; i++) { if (j == 1 && a[i] != 0 && !flag) { rec = lf; flag = true; } else if (j == 2 && a[i] != 0) { if (EQ(rec, lf)) return true; return false; } double b = -a[i] + f; lf += b; if (EQ(lf, 0) || lf < 0) return false; lf = min(lf, 1.0 * l); } } return true; } int main() { while (cin >> n >> l && !(n == 0 && l == 0)) { memset(a, 0, sizeof(a)); int s, t, u; for (int j = 0; j < n; j++) { cin >> s >> t >> u; for (int i = s + 1; i <= t; i++) a[i] = u; } double ub = 10000000; double lb = 0; int cnt = 100; while (cnt--) { double mid = (ub + lb) / 2; if (check(mid)) ub = mid; else lb = mid; } printf("%.10f\n", ub); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; #define EPS (1e-10) #define EQ(a, b) ((abs((a) - (b))) < EPS) int a[100001]; int n, l; bool check(double f) { double lf = l; double rec = 0; bool flag = false; for (int j = 0; j < 3; j++) { for (int i = 1; i <= 86400; i++) { if (j == 1 && a[i] != 0 && !flag) { rec = lf; flag = true; } else if (j == 2 && a[i] != 0) { if (EQ(rec, lf)) return true; return false; } double b = -a[i] + f; lf += b; if (EQ(lf, 0) || lf < 0) return false; lf = min(lf, 1.0 * l); } } return true; } int main() { while (cin >> n >> l && !(n == 0 && l == 0)) { memset(a, 0, sizeof(a)); int s, t, u; for (int j = 0; j < n; j++) { cin >> s >> t >> u; for (int i = s + 1; i <= t; i++) a[i] = u; } double ub = 10000000; double lb = 0; int cnt = 80; while (cnt--) { double mid = (ub + lb) / 2; if (check(mid)) ub = mid; else lb = mid; } printf("%.10f\n", ub); } return 0; }
replace
51
52
51
52
TLE
p01300
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; string S; int dp[80000][11]; int rec(int idx, int mod) { if (idx == S.size()) return (mod == 0); if (~dp[idx][mod]) return (dp[idx][mod]); return (rec(idx + 1, (mod * 10 + S[idx] - '0') % 11) + (mod == 0)); } int main() { while (cin >> S, S != "0") { memset(dp, -1, sizeof(dp)); int ret = 0; for (int i = 0; i < S.size(); i++) { if (S[i] != '0') ret += rec(i + 1, S[i] - '0'); } cout << ret << endl; } }
#include <bits/stdc++.h> using namespace std; string S; int dp[80000][11]; int rec(int idx, int mod) { if (idx == S.size()) return (mod == 0); if (~dp[idx][mod]) return (dp[idx][mod]); return (dp[idx][mod] = rec(idx + 1, (mod * 10 + S[idx] - '0') % 11) + (mod == 0)); } int main() { while (cin >> S, S != "0") { memset(dp, -1, sizeof(dp)); int ret = 0; for (int i = 0; i < S.size(); i++) { if (S[i] != '0') ret += rec(i + 1, S[i] - '0'); } cout << ret << endl; } }
replace
11
12
11
13
TLE
p01300
C++
Time Limit Exceeded
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); int c2d(char c) { return c - '0'; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); string s; while (cin >> s, s != "0") { int N = SZ(s); VVI dp(2, VI(N + 1, 0)); int crt = 0, nxt = 1; int ans = 0; for (int l = 1; l <= N; ++l) { for (int i = 0; i + l <= N; ++i) { dp[nxt][i] = c2d(s[i]) - dp[crt][i + 1]; if (dp[nxt][i] % 11 == 0 && c2d(s[i]) != 0) { ++ans; // cout << l << "," << i << endl; } } swap(crt, nxt); } cout << ans << endl; } return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); int c2d(char c) { return c - '0'; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); string s; while (cin >> s, s != "0") { int N = SZ(s); VI dp(11, 0); int ans = 0, sum = 0; dp[0] = 1; for (int i = 0; i < N; ++i) { sum = (sum + (i % 2 ? -1 : 1) * c2d(s[i]) + 11) % 11; ans += dp[sum]; if (i + 1 < N && c2d(s[i + 1]) != 0) dp[sum]++; } cout << ans << endl; } return 0; }
replace
67
79
67
75
TLE
p01300
C++
Runtime Error
#include <iostream> #include <stdlib.h> int main() { while (1) { // 入力 std::string number; std::cin >> number; // 演算 if (number == "0") return 0; int N = number.length(); int dp[801][11]; int answer = 0; for (int i = 0; i < 11; i++) { dp[0][i] = 0; } for (int i = 0; i < N; i++) { int digit = number.at(i) - '0'; for (int j = 0; j < 11; j++) { dp[i + 1][(j * 10 + digit) % 11] = dp[i][j]; } if (digit != 0) { dp[i + 1][digit]++; } /* for (int j = 0; j < 11; j++) { std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j] << std::endl; } */ answer += dp[i + 1][0]; } // 出力 /* for (int i = 0; i < 11; i++) { std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] << std::endl; } */ std::cout << answer << std::endl; } }
#include <iostream> #include <stdlib.h> int main() { while (1) { // 入力 std::string number; std::cin >> number; // 演算 if (number == "0") return 0; int N = number.length(); int dp[80001][11]; int answer = 0; for (int i = 0; i < 11; i++) { dp[0][i] = 0; } for (int i = 0; i < N; i++) { int digit = number.at(i) - '0'; for (int j = 0; j < 11; j++) { dp[i + 1][(j * 10 + digit) % 11] = dp[i][j]; } if (digit != 0) { dp[i + 1][digit]++; } /* for (int j = 0; j < 11; j++) { std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j] << std::endl; } */ answer += dp[i + 1][0]; } // 出力 /* for (int i = 0; i < 11; i++) { std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] << std::endl; } */ std::cout << answer << std::endl; } }
replace
12
13
12
13
0
p01301
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; template <class T> inline void chmin(T &a, T b) { a = min(a, b); } template <class T> inline void chmax(T &a, T &b) { a = max(a, b); } string getBin(int n) { string res = ""; /* for(int i=26;i>=0;i--){ if((n>>i)&1) res+='1'; else res+='0'; if(i>0&&i%9==0) res+=' '; }*/ for (int i = 0; i < 27; i++) { if ((n >> i) & 1) res += '1'; else res += '0'; if (i == 8 || i == 17) res += ' '; } return res; } int id = 0; bool cur_block[24][3][3][3]; bool cur[3][3][3]; bool tmp[3][3][3]; int encode_() { int c = 0; int res = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur[i][j][k]) res |= (1 << c); c++; } return res; } void put() { int enc = 0; int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur_block[id][i][j][k] = cur[i][j][k]; if (cur[i][j][k]) enc |= (1 << c); c++; } id++; // cerr<<"id="<<id<<"\n"; // cerr<<getBin(enc)<<"\n"; } void cp() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur[i][j][k] = tmp[i][j][k]; } } void rot3() { for (int x = 0; x < 2; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[2 - j][2 - i][2 - k] = cur[i][j][k]; } cp(); put(); } } void rot2() { for (int x = 0; x < 3; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[j][k][i] = cur[i][j][k]; } cp(); rot3(); } } void rot1() { for (int x = 0; x < 4; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[2 - j][i][k] = cur[i][j][k]; } cp(); rot2(); } } int csum; vector<int> cur_block_encode; int goal; void encode(int id) { int c = 0; int res = 0; int mini = 3, minj = 3, mink = 3; int maxi = -1, maxj = -1, maxk = -1; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur_block[id][i][j][k]) { chmin(mini, i); chmin(minj, j); chmin(mink, k); chmax(maxi, i); chmax(maxj, j); chmax(maxk, k); } } memset(cur, false, sizeof(cur)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur[i][j][k] = (i + mini < 3 && j + minj < 3 && k + mink < 3) ? cur_block[id][i + mini][j + minj][k + mink] : false; } for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur[i][j][k]) res |= (1 << c); c++; } // cur_block_encode.push_back(res); csum = maxi + maxj + maxk - mini - minj - mink + 3; int I = 3 - (maxi - mini + 1), J = 3 - (maxj - minj + 1), K = 3 - (maxk - mink + 1); for (int mi = 0; mi <= I; mi++) for (int mj = 0; mj <= J; mj++) for (int mk = 0; mk <= K; mk++) { // int x=9*mk+3*mi+mj; int x = mk + 3 * mj + 9 * mi; if (((res << x) & goal) != (res << x)) continue; cur_block_encode.push_back(res << x); } // return res; } void encode() { cur_block_encode.clear(); for (int i = 0; i < 24; i++) { // int cur_encode=encode(i); // if((goal&cur_encode)==cur_encode) //cur_block_encode.push_back(cur_encode); encode(i); } sort(cur_block_encode.begin(), cur_block_encode.end()); cur_block_encode.erase( unique(cur_block_encode.begin(), cur_block_encode.end()), cur_block_encode.end()); } struct Block { int weight; int sum; // W+H+D vector<int> nums; }; bool operator<(const Block &b1, const Block &b2) { if (b1.weight != b2.weight) return b1.weight < b2.weight; return b1.sum < b2.sum; } int W, H, D; int N; void inputGoal() { scanf("%d%d%d", &W, &D, &H); if (W == 0 && D == 0 && H == 0) exit(0); goal = 0; int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (i < H && j < W && k < D) goal |= (1 << c); c++; } // cerr<<getBin(goal)<<"\n"; } vector<Block> blocks; void inputBlock() { memset(cur_block, false, sizeof(cur_block)); memset(cur, false, sizeof(cur)); memset(tmp, false, sizeof(tmp)); int w, d, h; int weight = 0; scanf("%d%d%d", &w, &d, &h); char ch[10]; for (int i = 0; i < h; i++) for (int j = 0; j < d; j++) { scanf("%s", ch); for (int k = 0; k < w; k++) { cur[h - 1 - i][k][j] = ch[k] == '*'; if (ch[k] == '*') weight++; } } int a = encode_(); // cerr<<"init="<<getBin(a)<<"\n"; if (weight == 1) return; id = 0; rot1(); encode(); Block b; b.nums = cur_block_encode; b.sum = csum; b.weight = weight; blocks.push_back(b); // cerr<<"enc = "<<getBin(b.nums[0])<<"\n"; // cerr<<weight<<"\n"; } void inputBlocks() { blocks.clear(); scanf("%d", &N); for (int i = 0; i < N; i++) inputBlock(); } bitset<(1 << 27)> calculated, memo; bool dfs(int id, int curVal) { if (calculated[curVal]) return memo[curVal]; // cerr<<"id="<<id<<"val="<<getBin(curVal)<<"\n"; if (id == blocks.size()) { calculated[curVal] = true; memo[curVal] = true; return true; } vector<int> &nums = blocks[id].nums; for (int i = 0; i < nums.size(); i++) { if (curVal & nums[i]) continue; bool flg = dfs(id + 1, curVal | nums[i]); if (flg) { calculated[curVal] = true; memo[curVal] = true; return true; } } calculated[curVal] = true; memo[curVal] = false; return false; } bool solve() { // sort(blocks.begin(),blocks.end()); // reverse(blocks.begin(),blocks.end()); calculated.reset(); memo.reset(); /*for(int i=0;i<blocks.size();i++){ cerr<<"i="<<i<<"\n"; for(int j=0;j<blocks[i].nums.size();j++){ int n=blocks[i].nums[j]; cerr<<getBin(n)<<"\n"; } cerr<<"\n"; }*/ bool res = dfs(0, 0); return res; } int main() { while (true) { inputGoal(); inputBlocks(); bool ans = solve(); if (ans) printf("Yes\n"); else printf("No\n"); } return 0; }
#include <algorithm> #include <bitset> #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; template <class T> inline void chmin(T &a, T b) { a = min(a, b); } template <class T> inline void chmax(T &a, T &b) { a = max(a, b); } string getBin(int n) { string res = ""; /* for(int i=26;i>=0;i--){ if((n>>i)&1) res+='1'; else res+='0'; if(i>0&&i%9==0) res+=' '; }*/ for (int i = 0; i < 27; i++) { if ((n >> i) & 1) res += '1'; else res += '0'; if (i == 8 || i == 17) res += ' '; } return res; } int id = 0; bool cur_block[24][3][3][3]; bool cur[3][3][3]; bool tmp[3][3][3]; int encode_() { int c = 0; int res = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur[i][j][k]) res |= (1 << c); c++; } return res; } void put() { int enc = 0; int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur_block[id][i][j][k] = cur[i][j][k]; if (cur[i][j][k]) enc |= (1 << c); c++; } id++; // cerr<<"id="<<id<<"\n"; // cerr<<getBin(enc)<<"\n"; } void cp() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur[i][j][k] = tmp[i][j][k]; } } void rot3() { for (int x = 0; x < 2; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[2 - j][2 - i][2 - k] = cur[i][j][k]; } cp(); put(); } } void rot2() { for (int x = 0; x < 3; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[j][k][i] = cur[i][j][k]; } cp(); rot3(); } } void rot1() { for (int x = 0; x < 4; x++) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { tmp[2 - j][i][k] = cur[i][j][k]; } cp(); rot2(); } } int csum; vector<int> cur_block_encode; int goal; void encode(int id) { int c = 0; int res = 0; int mini = 3, minj = 3, mink = 3; int maxi = -1, maxj = -1, maxk = -1; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur_block[id][i][j][k]) { chmin(mini, i); chmin(minj, j); chmin(mink, k); chmax(maxi, i); chmax(maxj, j); chmax(maxk, k); } } memset(cur, false, sizeof(cur)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { cur[i][j][k] = (i + mini < 3 && j + minj < 3 && k + mink < 3) ? cur_block[id][i + mini][j + minj][k + mink] : false; } for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (cur[i][j][k]) res |= (1 << c); c++; } // cur_block_encode.push_back(res); csum = maxi + maxj + maxk - mini - minj - mink + 3; int I = 3 - (maxi - mini + 1), J = 3 - (maxj - minj + 1), K = 3 - (maxk - mink + 1); for (int mi = 0; mi <= I; mi++) for (int mj = 0; mj <= J; mj++) for (int mk = 0; mk <= K; mk++) { // int x=9*mk+3*mi+mj; int x = mk + 3 * mj + 9 * mi; if (((res << x) & goal) != (res << x)) continue; cur_block_encode.push_back(res << x); } // return res; } void encode() { cur_block_encode.clear(); for (int i = 0; i < 24; i++) { // int cur_encode=encode(i); // if((goal&cur_encode)==cur_encode) //cur_block_encode.push_back(cur_encode); encode(i); } sort(cur_block_encode.begin(), cur_block_encode.end()); cur_block_encode.erase( unique(cur_block_encode.begin(), cur_block_encode.end()), cur_block_encode.end()); } struct Block { int weight; int sum; // W+H+D vector<int> nums; }; bool operator<(const Block &b1, const Block &b2) { if (b1.weight != b2.weight) return b1.weight < b2.weight; return b1.sum < b2.sum; } int W, H, D; int N; void inputGoal() { scanf("%d%d%d", &W, &D, &H); if (W == 0 && D == 0 && H == 0) exit(0); goal = 0; int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { if (i < H && j < W && k < D) goal |= (1 << c); c++; } // cerr<<getBin(goal)<<"\n"; } vector<Block> blocks; void inputBlock() { memset(cur_block, false, sizeof(cur_block)); memset(cur, false, sizeof(cur)); memset(tmp, false, sizeof(tmp)); int w, d, h; int weight = 0; scanf("%d%d%d", &w, &d, &h); char ch[10]; for (int i = 0; i < h; i++) for (int j = 0; j < d; j++) { scanf("%s", ch); for (int k = 0; k < w; k++) { cur[h - 1 - i][k][j] = ch[k] == '*'; if (ch[k] == '*') weight++; } } int a = encode_(); // cerr<<"init="<<getBin(a)<<"\n"; if (weight == 1) return; id = 0; rot1(); encode(); Block b; b.nums = cur_block_encode; b.sum = csum; b.weight = weight; blocks.push_back(b); // cerr<<"enc = "<<getBin(b.nums[0])<<"\n"; // cerr<<weight<<"\n"; } void inputBlocks() { blocks.clear(); scanf("%d", &N); for (int i = 0; i < N; i++) inputBlock(); } bitset<(1 << 27)> calculated, memo; bool dfs(int id, int curVal) { if (calculated[curVal]) return memo[curVal]; // cerr<<"id="<<id<<"val="<<getBin(curVal)<<"\n"; if (id == blocks.size()) { calculated[curVal] = true; memo[curVal] = true; return true; } vector<int> &nums = blocks[id].nums; for (int i = 0; i < nums.size(); i++) { if (curVal & nums[i]) continue; bool flg = dfs(id + 1, curVal | nums[i]); if (flg) { calculated[curVal] = true; memo[curVal] = true; return true; } } calculated[curVal] = true; memo[curVal] = false; return false; } bool solve() { sort(blocks.begin(), blocks.end()); reverse(blocks.begin(), blocks.end()); calculated.reset(); memo.reset(); /*for(int i=0;i<blocks.size();i++){ cerr<<"i="<<i<<"\n"; for(int j=0;j<blocks[i].nums.size();j++){ int n=blocks[i].nums[j]; cerr<<getBin(n)<<"\n"; } cerr<<"\n"; }*/ bool res = dfs(0, 0); return res; } int main() { while (true) { inputGoal(); inputBlocks(); bool ans = solve(); if (ans) printf("Yes\n"); else printf("No\n"); } return 0; }
replace
277
279
277
279
TLE
p01301
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using B = ull; bool getb(B b, int x, int y, int z) { // assert(0 <= x && x < 4); // assert(0 <= y && y < 4); // assert(0 <= z && z < 4); ull mp = (1ULL << (z * 16 + y * 4 + x)); return (b & mp) != 0; } B setb(B b, int x, int y, int z, bool d) { // assert(0 <= x && x < 4); // assert(0 <= y && y < 4); // assert(0 <= z && z < 4); ull mp = (1ULL << (z * 16 + y * 4 + x)); if (d) b |= mp; else b &= ~mp; return b; } void prib(B b) { for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(b, x, y, z)) { printf("*"); } else { printf("."); } } printf("\n"); } printf("\n"); } } using BS = array<B, 24>; int W, D, H; vector<BS> Piese; int Pisz[30]; int PS; using Z = pair<B, int>; set<Z> s[30]; bool dfs(B b, int ump) { // prib(b); if (ump == (1 << PS) - 1) return true; int bc = __builtin_popcount(ump); if (5 <= bc) { if (s[bc].count(Z(b, ump))) return false; s[bc].insert(Z(b, ump)); } int nx, ny, nz; for (int z = H - 1; z >= 0; z--) { for (int y = D - 1; y >= 0; y--) { for (int x = W - 1; x >= 0; x--) { if (!getb(b, x, y, z)) { nx = x; ny = y; nz = z; } } } } // if (nx == -1) return true; for (int i = PS - 1; i >= 0; i--) { if (ump & (1 << i)) continue; for (int j = Pisz[i] - 1; j >= 0; j--) { B now = Piese[i][j]; int fx = -1, fy; { bool f = false; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(now, x, y, 0)) { fx = x; fy = y; f = true; break; } } if (f) break; } } B nb = b; bool f = true; int offx = nx - fx, offy = ny - fy; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (!getb(now, x, y, z)) continue; int sx = offx + x; int sy = offy + y; int sz = nz + z; if (sx < 0 or W <= sx) f = false; else if (sy < 0 or D <= sy) f = false; else if (sz < 0 or H <= sz) f = false; else if (getb(b, sx, sy, sz)) f = false; else nb = setb(nb, sx, sy, sz, true); if (!f) goto end_loop; } } } end_loop: if (!f) continue; if (dfs(nb, ump | (1 << i))) return true; } } return false; } bool calc() { // s.clear(); for (int i = 0; i < 30; i++) { s[i].clear(); } return dfs(0, 0); } B shrink(B b) { B nb; while (true) { bool f = true; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { if (getb(b, 0, y, z)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 2; x++) { nb = setb(nb, x, y, z, getb(b, x + 1, y, z)); } } } b = nb; } while (true) { bool f = true; for (int x = 0; x < 3; x++) { for (int z = 0; z < 3; z++) { if (getb(b, x, 0, z)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 3; z++) { for (int y = 0; y < 2; y++) { for (int x = 0; x < 3; x++) { nb = setb(nb, x, y, z, getb(b, x, y + 1, z)); } } } b = nb; } while (true) { bool f = true; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(b, x, y, 0)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 2; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { nb = setb(nb, x, y, z, getb(b, x, y, z + 1)); } } } b = nb; } return b; } bool solve() { int N; cin >> W >> D >> H >> N; if (!W) return false; vector<B> v; for (int i = 0; i < N; i++) { B b = 0; int w, d, h; cin >> w >> d >> h; for (int z = 0; z < h; z++) { for (int y = 0; y < d; y++) { for (int x = 0; x < w; x++) { char c; cin >> c; bool f = c == '*'; b = setb(b, x, y, z, f); } } } v.push_back(b); } vector<BS> nv; for (B b : v) { // printf("ro b %016llx\n", b); BS nb = {}; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { nb[0] = setb(nb[0], x, y, z, getb(b, x, y, z)); nb[1] = setb(nb[1], x, y, z, getb(b, 2 - x, 2 - y, z)); nb[2] = setb(nb[2], x, y, z, getb(b, 2 - x, y, 2 - z)); nb[3] = setb(nb[3], x, y, z, getb(b, x, 2 - y, 2 - z)); nb[4] = setb(nb[4], x, y, z, getb(b, 2 - x, z, y)); nb[5] = setb(nb[5], x, y, z, getb(b, x, 2 - z, y)); nb[6] = setb(nb[6], x, y, z, getb(b, x, z, 2 - y)); nb[7] = setb(nb[7], x, y, z, getb(b, 2 - x, 2 - z, 2 - y)); nb[8] = setb(nb[8], x, y, z, getb(b, 2 - y, x, z)); nb[9] = setb(nb[9], x, y, z, getb(b, y, 2 - x, z)); nb[10] = setb(nb[10], x, y, z, getb(b, y, x, 2 - z)); nb[11] = setb(nb[11], x, y, z, getb(b, 2 - y, 2 - x, 2 - z)); nb[12] = setb(nb[12], x, y, z, getb(b, y, z, x)); nb[13] = setb(nb[13], x, y, z, getb(b, 2 - y, 2 - z, x)); nb[14] = setb(nb[14], x, y, z, getb(b, 2 - y, z, 2 - x)); nb[15] = setb(nb[15], x, y, z, getb(b, y, 2 - z, 2 - x)); nb[16] = setb(nb[16], x, y, z, getb(b, z, x, y)); nb[17] = setb(nb[17], x, y, z, getb(b, 2 - z, 2 - x, y)); nb[18] = setb(nb[18], x, y, z, getb(b, 2 - z, x, 2 - y)); nb[19] = setb(nb[19], x, y, z, getb(b, z, 2 - x, 2 - y)); nb[20] = setb(nb[20], x, y, z, getb(b, 2 - z, y, x)); nb[21] = setb(nb[21], x, y, z, getb(b, z, 2 - y, x)); nb[22] = setb(nb[22], x, y, z, getb(b, z, y, 2 - x)); nb[23] = setb(nb[23], x, y, z, getb(b, 2 - z, 2 - y, 2 - x)); } } } for (int i = 0; i < 24; i++) { nb[i] = shrink(nb[i]); } sort(nb.begin(), nb.end()); Pisz[nv.size()] = unique(nb.begin(), nb.end()) - nb.begin(); nv.push_back(nb); } Piese = nv; PS = (int)nv.size(); if (calc()) { cout << "Yes" << endl; } else { cout << "No" << endl; } return true; } int main() { while (solve()) { } return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using B = ull; bool getb(B b, int x, int y, int z) { // assert(0 <= x && x < 4); // assert(0 <= y && y < 4); // assert(0 <= z && z < 4); ull mp = (1ULL << (z * 16 + y * 4 + x)); return (b & mp) != 0; } B setb(B b, int x, int y, int z, bool d) { // assert(0 <= x && x < 4); // assert(0 <= y && y < 4); // assert(0 <= z && z < 4); ull mp = (1ULL << (z * 16 + y * 4 + x)); if (d) b |= mp; else b &= ~mp; return b; } void prib(B b) { for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(b, x, y, z)) { printf("*"); } else { printf("."); } } printf("\n"); } printf("\n"); } } using BS = array<B, 24>; int W, D, H; vector<BS> Piese; int Pisz[30]; int PS; using Z = pair<B, int>; set<Z> s[30]; bool dfs(B b, int ump) { // prib(b); if (ump == (1 << PS) - 1) return true; int bc = __builtin_popcount(ump); if (4 <= bc) { if (s[bc].count(Z(b, ump))) return false; s[bc].insert(Z(b, ump)); } int nx, ny, nz; for (int z = H - 1; z >= 0; z--) { for (int y = D - 1; y >= 0; y--) { for (int x = W - 1; x >= 0; x--) { if (!getb(b, x, y, z)) { nx = x; ny = y; nz = z; } } } } // if (nx == -1) return true; for (int i = PS - 1; i >= 0; i--) { if (ump & (1 << i)) continue; for (int j = Pisz[i] - 1; j >= 0; j--) { B now = Piese[i][j]; int fx = -1, fy; { bool f = false; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(now, x, y, 0)) { fx = x; fy = y; f = true; break; } } if (f) break; } } B nb = b; bool f = true; int offx = nx - fx, offy = ny - fy; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (!getb(now, x, y, z)) continue; int sx = offx + x; int sy = offy + y; int sz = nz + z; if (sx < 0 or W <= sx) f = false; else if (sy < 0 or D <= sy) f = false; else if (sz < 0 or H <= sz) f = false; else if (getb(b, sx, sy, sz)) f = false; else nb = setb(nb, sx, sy, sz, true); if (!f) goto end_loop; } } } end_loop: if (!f) continue; if (dfs(nb, ump | (1 << i))) return true; } } return false; } bool calc() { // s.clear(); for (int i = 0; i < 30; i++) { s[i].clear(); } return dfs(0, 0); } B shrink(B b) { B nb; while (true) { bool f = true; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { if (getb(b, 0, y, z)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 2; x++) { nb = setb(nb, x, y, z, getb(b, x + 1, y, z)); } } } b = nb; } while (true) { bool f = true; for (int x = 0; x < 3; x++) { for (int z = 0; z < 3; z++) { if (getb(b, x, 0, z)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 3; z++) { for (int y = 0; y < 2; y++) { for (int x = 0; x < 3; x++) { nb = setb(nb, x, y, z, getb(b, x, y + 1, z)); } } } b = nb; } while (true) { bool f = true; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { if (getb(b, x, y, 0)) { f = false; break; } } } if (!f) break; nb = 0; for (int z = 0; z < 2; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { nb = setb(nb, x, y, z, getb(b, x, y, z + 1)); } } } b = nb; } return b; } bool solve() { int N; cin >> W >> D >> H >> N; if (!W) return false; vector<B> v; for (int i = 0; i < N; i++) { B b = 0; int w, d, h; cin >> w >> d >> h; for (int z = 0; z < h; z++) { for (int y = 0; y < d; y++) { for (int x = 0; x < w; x++) { char c; cin >> c; bool f = c == '*'; b = setb(b, x, y, z, f); } } } v.push_back(b); } vector<BS> nv; for (B b : v) { // printf("ro b %016llx\n", b); BS nb = {}; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { nb[0] = setb(nb[0], x, y, z, getb(b, x, y, z)); nb[1] = setb(nb[1], x, y, z, getb(b, 2 - x, 2 - y, z)); nb[2] = setb(nb[2], x, y, z, getb(b, 2 - x, y, 2 - z)); nb[3] = setb(nb[3], x, y, z, getb(b, x, 2 - y, 2 - z)); nb[4] = setb(nb[4], x, y, z, getb(b, 2 - x, z, y)); nb[5] = setb(nb[5], x, y, z, getb(b, x, 2 - z, y)); nb[6] = setb(nb[6], x, y, z, getb(b, x, z, 2 - y)); nb[7] = setb(nb[7], x, y, z, getb(b, 2 - x, 2 - z, 2 - y)); nb[8] = setb(nb[8], x, y, z, getb(b, 2 - y, x, z)); nb[9] = setb(nb[9], x, y, z, getb(b, y, 2 - x, z)); nb[10] = setb(nb[10], x, y, z, getb(b, y, x, 2 - z)); nb[11] = setb(nb[11], x, y, z, getb(b, 2 - y, 2 - x, 2 - z)); nb[12] = setb(nb[12], x, y, z, getb(b, y, z, x)); nb[13] = setb(nb[13], x, y, z, getb(b, 2 - y, 2 - z, x)); nb[14] = setb(nb[14], x, y, z, getb(b, 2 - y, z, 2 - x)); nb[15] = setb(nb[15], x, y, z, getb(b, y, 2 - z, 2 - x)); nb[16] = setb(nb[16], x, y, z, getb(b, z, x, y)); nb[17] = setb(nb[17], x, y, z, getb(b, 2 - z, 2 - x, y)); nb[18] = setb(nb[18], x, y, z, getb(b, 2 - z, x, 2 - y)); nb[19] = setb(nb[19], x, y, z, getb(b, z, 2 - x, 2 - y)); nb[20] = setb(nb[20], x, y, z, getb(b, 2 - z, y, x)); nb[21] = setb(nb[21], x, y, z, getb(b, z, 2 - y, x)); nb[22] = setb(nb[22], x, y, z, getb(b, z, y, 2 - x)); nb[23] = setb(nb[23], x, y, z, getb(b, 2 - z, 2 - y, 2 - x)); } } } for (int i = 0; i < 24; i++) { nb[i] = shrink(nb[i]); } sort(nb.begin(), nb.end()); Pisz[nv.size()] = unique(nb.begin(), nb.end()) - nb.begin(); nv.push_back(nb); } Piese = nv; PS = (int)nv.size(); if (calc()) { cout << "Yes" << endl; } else { cout << "No" << endl; } return true; } int main() { while (solve()) { } return 0; }
replace
72
73
72
73
TLE
p01304
C++
Runtime Error
#include <iostream> using namespace std; int mt[16][16][16][16]; int dp[16][16]; int i, j, k, l, m, n, o, a, b, c, d, cnt, X, Y; void init() { for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 0; for (int k = 0; k < 16; k++) { for (int l = 0; l < 16; l++) { mt[i][j][k][l] = 0; } } } } } int main() { int n; cin >> n; for (int data = 0; data < n; data++) { init(); cin >> X >> Y >> m; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> d; mt[a][b][c][d] = 1; mt[c][d][a][b] = 1; } dp[0][0] = 1; for (int i = 1; i <= X; i++) { if (mt[i][0][i - 1][0] == 1) { dp[i][0] = 0; } else { dp[i][0] = dp[i - 1][0]; } } for (int i = 1; i <= Y; i++) { if (mt[0][i][0][i - 1]) { dp[0][i] = 0; } else { dp[0][i] = dp[0][i - 1]; } for (int j = 1; j <= X; j++) { if (mt[j][i][j - 1][i] == 0) { dp[j][i] += dp[j - 1][i]; } if (mt[j][i][j][i - 1] == 0) { dp[j][i] += dp[j][i - 1]; } } } if (dp[X][Y] == 0) { cout << "Miserable Hokusai!" << endl; } else { cout << dp[X][Y] << endl; } } return n; }
#include <iostream> using namespace std; int mt[16][16][16][16]; int dp[16][16]; int i, j, k, l, m, n, o, a, b, c, d, cnt, X, Y; void init() { for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 0; for (int k = 0; k < 16; k++) { for (int l = 0; l < 16; l++) { mt[i][j][k][l] = 0; } } } } } int main() { int n; cin >> n; for (int data = 0; data < n; data++) { init(); cin >> X >> Y >> m; for (int i = 0; i < m; i++) { cin >> a >> b >> c >> d; mt[a][b][c][d] = 1; mt[c][d][a][b] = 1; } dp[0][0] = 1; for (int i = 1; i <= X; i++) { if (mt[i][0][i - 1][0] == 1) { dp[i][0] = 0; } else { dp[i][0] = dp[i - 1][0]; } } for (int i = 1; i <= Y; i++) { if (mt[0][i][0][i - 1]) { dp[0][i] = 0; } else { dp[0][i] = dp[0][i - 1]; } for (int j = 1; j <= X; j++) { if (mt[j][i][j - 1][i] == 0) { dp[j][i] += dp[j - 1][i]; } if (mt[j][i][j][i - 1] == 0) { dp[j][i] += dp[j][i - 1]; } } } if (dp[X][Y] == 0) { cout << "Miserable Hokusai!" << endl; } else { cout << dp[X][Y] << endl; } } return 0; }
replace
60
61
60
61
4
p01306
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <string> using namespace std; int Tanni(string); int main() { int i, j, n; string st, str, str2; cin >> n; for (int t = 0; t < n; t++) { int keta, a[1003], aa, dot; cin >> st >> str; dot = -1; bool f = false; for (i = 0; i < st.size(); i++) { if (st[i] == '.') { a[i] = -1; dot = i; f = true; } else a[i] = st[i] - '0'; } aa = i; if (dot == 0) { for (i = aa - 1; i >= 0; i--) a[i + 1] = a[i]; a[0] = 0; dot = 1; aa++; } else if (dot == aa - 1) a[aa++] = 0; if (!f) { a[aa] = -1; dot = aa; a[aa + 1] = 0; aa += 2; } keta = Tanni(str); if (keta == 0) str2 = str; else cin >> str2; while (a[0] == 0 || dot != 1) { if (a[0] != 0 && a[1] == -1) break; // for(dot=0;dot<aa;dot++) if(a[dot] == -1) break; if (a[0] == 0) { keta--; int c; c = a[dot]; a[dot] = a[dot + 1]; a[dot + 1] = c; for (j = 0; j < aa - 1; j++) a[j] = a[j + 1]; a[aa - 1] = 0; aa--; } else if (a[1] != 1) { keta++; int c; c = a[dot]; a[dot] = a[dot - 1]; a[dot - 1] = c; dot--; } } if (!f) aa--; if (aa - 1 == dot) aa--; for (i = 0; i < aa; i++) { if (i == dot) cout << "."; else cout << a[i]; } cout << " * 10^" << keta << " " << str2 << endl; } return 0; } int Tanni(string str) { if (str == "yotta") return 24; else if (str == "zetta") return 21; else if (str == "exa") return 18; else if (str == "peta") return 15; else if (str == "tera") return 12; else if (str == "giga") return 9; else if (str == "mega") return 6; else if (str == "kilo") return 3; else if (str == "hecto") return 2; else if (str == "deca") return 1; else if (str == "deci") return -1; else if (str == "centi") return -2; else if (str == "milli") return -3; else if (str == "micro") return -6; else if (str == "nano") return -9; else if (str == "pico") return -12; else if (str == "femto") return -15; else if (str == "ato") return -18; else if (str == "zepto") return -21; else if (str == "yocto") return -24; else return 0; };
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <string> using namespace std; int Tanni(string); int main() { int i, j, n; string st, str, str2; cin >> n; for (int t = 0; t < n; t++) { int keta, a[1003], aa, dot; cin >> st >> str; dot = -1; bool f = false; for (i = 0; i < st.size(); i++) { if (st[i] == '.') { a[i] = -1; dot = i; f = true; } else a[i] = st[i] - '0'; } aa = i; if (dot == 0) { for (i = aa - 1; i >= 0; i--) a[i + 1] = a[i]; a[0] = 0; dot = 1; aa++; } else if (dot == aa - 1) a[aa++] = 0; if (!f) { a[aa] = -1; dot = aa; a[aa + 1] = 0; aa += 2; } keta = Tanni(str); if (keta == 0) str2 = str; else cin >> str2; while (a[0] == 0 || dot != 1) { if (a[0] != 0 && a[1] == -1) break; // for(dot=0;dot<aa;dot++) if(a[dot] == -1) break; if (a[0] == 0) { keta--; int c; c = a[dot]; a[dot] = a[dot + 1]; a[dot + 1] = c; for (j = 0; j < aa - 1; j++) a[j] = a[j + 1]; a[aa - 1] = 0; aa--; } else if (dot != 1) { keta++; int c; c = a[dot]; a[dot] = a[dot - 1]; a[dot - 1] = c; dot--; } } if (!f) aa--; if (aa - 1 == dot) aa--; for (i = 0; i < aa; i++) { if (i == dot) cout << "."; else cout << a[i]; } cout << " * 10^" << keta << " " << str2 << endl; } return 0; } int Tanni(string str) { if (str == "yotta") return 24; else if (str == "zetta") return 21; else if (str == "exa") return 18; else if (str == "peta") return 15; else if (str == "tera") return 12; else if (str == "giga") return 9; else if (str == "mega") return 6; else if (str == "kilo") return 3; else if (str == "hecto") return 2; else if (str == "deca") return 1; else if (str == "deci") return -1; else if (str == "centi") return -2; else if (str == "milli") return -3; else if (str == "micro") return -6; else if (str == "nano") return -9; else if (str == "pico") return -12; else if (str == "femto") return -15; else if (str == "ato") return -18; else if (str == "zepto") return -21; else if (str == "yocto") return -24; else return 0; };
replace
70
71
70
71
TLE
p01308
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #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 rp(i, c) rep(i, (c).size()) #define fr(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define mp make_pair #define pb push_back #define all(c) (c).begin(), (c).end() #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 = 1e10, EPS = 1e-9; int n, m, t[50000], s[50000]; int so[256]; int tonum(char *buf) { return so[buf[0]] + (buf[1] == '#'); } bool bfs() { queue<pi> Q; Q.push(mp(0, 0)); while (!Q.empty()) { int cur = Q.front().first, step = Q.front().second; Q.pop(); if (step == m) { if (cur + 1 == n + 1 || cur + 2 == n + 1) return 1; continue; } if (cur + 1 <= n && t[cur] == s[step]) Q.push(mp(cur + 1, step + 1)); if (cur + 2 <= n && (t[cur + 1] + 1) % 12 == s[step]) Q.push(mp(cur + 2, step + 1)); if (cur - 1 > 0 && (t[cur - 2] + 11) % 12 == s[step]) Q.push(mp(cur - 1, step + 1)); } return 0; } int main() { so['C'] = 0; so['D'] = 2; so['E'] = 4; so['F'] = 5; so['G'] = 7; so['A'] = 9; so['B'] = 11; int cs; scanf("%d", &cs); while (cs--) { scanf("%d%d", &n, &m); char buf[3]; rep(i, n) scanf("%s", buf), t[i] = tonum(buf); rep(i, m) scanf("%s", buf), s[i] = tonum(buf); cout << (bfs() ? "Yes" : "No") << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <complex> #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 rp(i, c) rep(i, (c).size()) #define fr(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define mp make_pair #define pb push_back #define all(c) (c).begin(), (c).end() #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 = 1e10, EPS = 1e-9; int n, m, t[50000], s[50000]; int so[256]; int tonum(char *buf) { return so[buf[0]] + (buf[1] == '#'); } bool bfs() { queue<pi> Q; Q.push(mp(0, 0)); while (!Q.empty()) { int cur = Q.front().first, step = Q.front().second; Q.pop(); if (n + 1 - cur > 2 * (m - step + 1)) continue; if (step == m) { if (cur + 1 == n + 1 || cur + 2 == n + 1) return 1; continue; } if (cur + 1 <= n && t[cur] == s[step]) Q.push(mp(cur + 1, step + 1)); if (cur + 2 <= n && (t[cur + 1] + 1) % 12 == s[step]) Q.push(mp(cur + 2, step + 1)); if (cur - 1 > 0 && (t[cur - 2] + 11) % 12 == s[step]) Q.push(mp(cur - 1, step + 1)); } return 0; } int main() { so['C'] = 0; so['D'] = 2; so['E'] = 4; so['F'] = 5; so['G'] = 7; so['A'] = 9; so['B'] = 11; int cs; scanf("%d", &cs); while (cs--) { scanf("%d%d", &n, &m); char buf[3]; rep(i, n) scanf("%s", buf), t[i] = tonum(buf); rep(i, m) scanf("%s", buf), s[i] = tonum(buf); cout << (bfs() ? "Yes" : "No") << endl; } return 0; }
insert
44
44
44
47
TLE
p01314
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; int main() { int n; while (cin >> n) { if (n == 0) break; int l = 1; int r = 1; int sum = 0; int cnt = 0; while (true) { if (r >= n) break; while (sum > n) { sum -= l; l++; if (sum == n) cnt++; } sum += r; r++; if (sum == n) cnt++; cerr << sum << " " << l << " " << r << endl; } cout << cnt << endl; } return 0; } // EOF
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; int main() { int n; while (cin >> n) { if (n == 0) break; int l = 1; int r = 1; int sum = 0; int cnt = 0; while (true) { if (r >= n) break; while (sum > n) { sum -= l; l++; if (sum == n) cnt++; } sum += r; r++; if (sum == n) cnt++; // cerr << sum << " " << l << " " << r << endl; } cout << cnt << endl; } return 0; } // EOF
replace
39
40
39
40
0
1 1 2 3 1 3 6 1 4 10 1 5 14 2 6 15 4 7 13 6 8 15 7 9 1 1 2 3 1 3 6 1 4 10 1 5 15 1 6 21 1 7 28 1 8 36 1 9 45 1 10 55 1 11 66 1 12 78 1 13 91 1 14 105 1 15 120 1 16 136 1 17 153 1 18 171 1 19 190 1 20 210 1 21 231 1 22 253 1 23 276 1 24 300 1 25 325 1 26 351 1 27 378 1 28 406 1 29 435 1 30 465 1 31 496 1 32 528 1 33 533 8 34 529 12 35 525 15 36 530 17 37 532 19 38 531 21 39 527 23 40 520 25 41 536 26 42 525 28 43 540 29 44 525 31 45 539 32 46 520 34 47 533 35 48 546 36 49 522 38 50 534 39 51 546 40 52 517 42 53 528 43 54 539 44 55 550 45 56 515 47 57 525 48 58 535 49 59 545 50 60 555 51 61 513 53 62 522 54 63 531 55 64 540 56 65 549 57 66 558 58 67 567 59 68 516 61 69 524 62 70 532 63 71 540 64 72 548 65 73 556 66 74 564 67 75 572 68 76 511 70 77 518 71 78 525 72 79 532 73 80 539 74 81 546 75 82 553 76 83 560 77 84 567 78 85 574 79 86 581 80 87 507 82 88 513 83 89 519 84 90 525 85 91 531 86 92 537 87 93 543 88 94 549 89 95 555 90 96 561 91 97 567 92 98 573 93 99 579 94 100 585 95 101 591 96 102 597 97 103 603 98 104 510 100 105 515 101 106 520 102 107 525 103 108 530 104 109 535 105 110 540 106 111 545 107 112 550 108 113 555 109 114 560 110 115 565 111 116 570 112 117 575 113 118 580 114 119 585 115 120 590 116 121 595 117 122 600 118 123 605 119 124 610 120 125 615 121 126 620 122 127 625 123 128 506 125 129 510 126 130 514 127 131 518 128 132 522 129 133 526 130 134 530 131 135 534 132 136 538 133 137 542 134 138 546 135 139 550 136 140 554 137 141 558 138 142 562 139 143 566 140 144 570 141 145 574 142 146 578 143 147 582 144 148 586 145 149 590 146 150 594 147 151 598 148 152 602 149 153 606 150 154 610 151 155 614 152 156 618 153 157 622 154 158 626 155 159 630 156 160 634 157 161 638 158 162 642 159 163 646 160 164 650 161 165 654 162 166 658 163 167 662 164 168 666 165 169 504 167 170 507 168 171 510 169 172 513 170 173 516 171 174 519 172 175 522 173 176 525 174 177 528 175 178 531 176 179 534 177 180 537 178 181 540 179 182 543 180 183 546 181 184 549 182 185 552 183 186 555 184 187 558 185 188 561 186 189 564 187 190 567 188 191 570 189 192 573 190 193 576 191 194 579 192 195 582 193 196 585 194 197 588 195 198 591 196 199 594 197 200 597 198 201 600 199 202 603 200 203 606 201 204 609 202 205 612 203 206 615 204 207 618 205 208 621 206 209 624 207 210 627 208 211 630 209 212 633 210 213 636 211 214 639 212 215 642 213 216 645 214 217 648 215 218 651 216 219 654 217 220 657 218 221 660 219 222 663 220 223 666 221 224 669 222 225 672 223 226 675 224 227 678 225 228 681 226 229 684 227 230 687 228 231 690 229 232 693 230 233 696 231 234 699 232 235 702 233 236 705 234 237 708 235 238 711 236 239 714 237 240 717 238 241 720 239 242 723 240 243 726 241 244 729 242 245 732 243 246 735 244 247 738 245 248 741 246 249 744 247 250 747 248 251 750 249 252 503 251 253 505 252 254 507 253 255 509 254 256 511 255 257 513 256 258 515 257 259 517 258 260 519 259 261 521 260 262 523 261 263 525 262 264 527 263 265 529 264 266 531 265 267 533 266 268 535 267 269 537 268 270 539 269 271 541 270 272 543 271 273 545 272 274 547 273 275 549 274 276 551 275 277 553 276 278 555 277 279 557 278 280 559 279 281 561 280 282 563 281 283 565 282 284 567 283 285 569 284 286 571 285 287 573 286 288 575 287 289 577 288 290 579 289 291 581 290 292 583 291 293 585 292 294 587 293 295 589 294 296 591 295 297 593 296 298 595 297 299 597 298 300 599 299 301 601 300 302 603 301 303 605 302 304 607 303 305 609 304 306 611 305 307 613 306 308 615 307 309 617 308 310 619 309 311 621 310 312 623 311 313 625 312 314 627 313 315 629 314 316 631 315 317 633 316 318 635 317 319 637 318 320 639 319 321 641 320 322 643 321 323 645 322 324 647 323 325 649 324 326 651 325 327 653 326 328 655 327 329 657 328 330 659 329 331 661 330 332 663 331 333 665 332 334 667 333 335 669 334 336 671 335 337 673 336 338 675 337 339 677 338 340 679 339 341 681 340 342 683 341 343 685 342 344 687 343 345 689 344 346 691 345 347 693 346 348 695 347 349 697 348 350 699 349 351 701 350 352 703 351 353 705 352 354 707 353 355 709 354 356 711 355 357 713 356 358 715 357 359 717 358 360 719 359 361 721 360 362 723 361 363 725 362 364 727 363 365 729 364 366 731 365 367 733 366 368 735 367 369 737 368 370 739 369 371 741 370 372 743 371 373 745 372 374 747 373 375 749 374 376 751 375 377 753 376 378 755 377 379 757 378 380 759 379 381 761 380 382 763 381 383 765 382 384 767 383 385 769 384 386 771 385 387 773 386 388 775 387 389 777 388 390 779 389 391 781 390 392 783 391 393 785 392 394 787 393 395 789 394 396 791 395 397 793 396 398 795 397 399 797 398 400 799 399 401 801 400 402 803 401 403 805 402 404 807 403 405 809 404 406 811 405 407 813 406 408 815 407 409 817 408 410 819 409 411 821 410 412 823 411 413 825 412 414 827 413 415 829 414 416 831 415 417 833 416 418 835 417 419 837 418 420 839 419 421 841 420 422 843 421 423 845 422 424 847 423 425 849 424 426 851 425 427 853 426 428 855 427 429 857 428 430 859 429 431 861 430 432 863 431 433 865 432 434 867 433 435 869 434 436 871 435 437 873 436 438 875 437 439 877 438 440 879 439 441 881 440 442 883 441 443 885 442 444 887 443 445 889 444 446 891 445 447 893 446 448 895 447 449 897 448 450 899 449 451 901 450 452 903 451 453 905 452 454 907 453 455 909 454 456 911 455 457 913 456 458 915 457 459 917 458 460 919 459 461 921 460 462 923 461 463 925 462 464 927 463 465 929 464 466 931 465 467 933 466 468 935 467 469 937 468 470 939 469 471 941 470 472 943 471 473 945 472 474 947 473 475 949 474 476 951 475 477 953 476 478 955 477 479 957 478 480 959 479 481 961 480 482 963 481 483 965 482 484 967 483 485 969 484 486 971 485 487 973 486 488 975 487 489 977 488 490 979 489 491 981 490 492 983 491 493 985 492 494 987 493 495 989 494 496 991 495 497 993 496 498 995 497 499 997 498 500
p01314
C++
Time Limit Exceeded
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { int n, x; while (true) { scanf("%d", &n); x = 0; if (n == 0) break; for (int i = 2; i <= n; i++) { for (int j = 1; j < n; j++) { int count = 0; for (int k = 0; k < i; k++) count += j + k; if (count == n) x++; } } printf("%d\n", x); } return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { int n, x; while (true) { scanf("%d", &n); x = 0; if (n == 0) break; for (int i = 2; i <= n; i++) { for (int j = 1; j < n; j++) { int count = 0; for (int k = j; k < j + i; k++) count += k; if (count == n) x++; } } printf("%d\n", x); } return 0; }
replace
14
16
14
16
TLE
p01314
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(a) (a).begin(), (a).end() #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define ll long long #define ull unsigned long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; int main() { int n, sum, ans, left, right; while (cin >> n) { if (!n) break; left = 1; right = 2; ans = 0; sum = left + right; while (n - 1 != left) { if (n == sum) ans++; if (sum <= n && right < n) { right++; sum += right; } else if (right - left < 2 && right < n) { right++; sum += right; } else if (sum > n && right - left >= 2) { sum -= left; left++; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define ALL(a) (a).begin(), (a).end() #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) #define ll long long #define ull unsigned long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; int main() { int n, sum, ans, left, right; while (cin >> n) { if (!n) break; left = 1; right = 2; ans = 0; sum = left + right; if (n < 3) { cout << 0 << endl; continue; } while (n - 1 > left) { if (n == sum) ans++; if (sum <= n && right < n) { right++; sum += right; } else if (right - left < 2 && right < n) { right++; sum += right; } else if (sum > n && right - left >= 2) { sum -= left; left++; } } cout << ans << endl; } return 0; }
replace
22
23
22
27
TLE
p01314
C++
Time Limit Exceeded
// 52 #include <iostream> using namespace std; int main() { for (int n; cin >> n, n;) { int ans = 0; for (int i = 2; i <= 1000; i++) { for (int j = 1; j <= 1000; j++) { int s = 0; for (int k = 0; k < i; k++) { s += j + k; } ans += s == n; } } cout << ans << endl; } return 0; }
// 52 #include <iostream> using namespace std; int main() { for (int n; cin >> n, n;) { int ans = 0; for (int i = 1; i <= 1000; i++) { for (int j = 2; j <= 1000; j++) { ans += n == i * j + j * (j - 1) / 2; } } cout << ans << endl; } return 0; }
replace
8
15
8
11
TLE
p01315
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; int main() { int n; string l[50]; double ans[50]; int a[9]; while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> l[i]; for (int j = 0; j < 9; j++) cin >> a[j]; ans[i] = a[6] * a[7] * a[8] - a[0]; ans[i] /= a[1] + a[2] + a[3] + (a[4] + a[5]) * a[8]; } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { if (ans[j] > ans[j - 1]) { double tmp = ans[j]; ans[j] = ans[j - 1]; ans[j - 1] = tmp; string smp = l[j]; l[j] = l[j - 1]; l[j - 1] = smp; } if (ans[j] == ans[j - 1]) { bool t = true; int i = 0; while (true) { if (i == l[j].size() && i != l[j - 1].size()) { t = false; break; } if (i == l[j - 1].size() && i != l[j].size()) break; if (l[j][i] < l[j - 1][i]) { t = false; break; } if (l[j][i] > l[j - 1][i]) break; } if (t == false) { double tmp = ans[j]; ans[j] = ans[j - 1]; ans[j - 1] = tmp; string smp = l[j]; l[j] = l[j - 1]; l[j - 1] = smp; } } } } for (int i = 0; i < n; i++) cout << l[i] << endl; cout << "#" << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { int n; string l[50]; double ans[50]; int a[9]; while (true) { cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { cin >> l[i]; for (int j = 0; j < 9; j++) cin >> a[j]; ans[i] = a[6] * a[7] * a[8] - a[0]; ans[i] /= a[1] + a[2] + a[3] + (a[4] + a[5]) * a[8]; } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { if (ans[j] > ans[j - 1]) { double tmp = ans[j]; ans[j] = ans[j - 1]; ans[j - 1] = tmp; string smp = l[j]; l[j] = l[j - 1]; l[j - 1] = smp; } if (ans[j] == ans[j - 1]) { bool t = true; int i = 0; while (true) { if (i == l[j].size() && i != l[j - 1].size()) { t = false; break; } if (i == l[j - 1].size() && i != l[j].size()) break; if (l[j][i] < l[j - 1][i]) { t = false; break; } if (l[j][i] > l[j - 1][i]) break; i++; } if (t == false) { double tmp = ans[j]; ans[j] = ans[j - 1]; ans[j - 1] = tmp; string smp = l[j]; l[j] = l[j - 1]; l[j - 1] = smp; } } } } for (int i = 0; i < n; i++) cout << l[i] << endl; cout << "#" << endl; } return 0; }
insert
45
45
45
46
TLE
p01315
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; typedef pair<double, string> P; bool comp(P a, P b) { if ((a.first - b.first) <= 0.000001) { return a.second <= b.second; } return a.first >= b.first; } int main() { int n; while (cin >> n, n) { vector<P> v(n); FOR(i, 0, n) { string L; cin >> L; ll p, a, b, c, d, e, f, s, m; cin >> p >> a >> b >> c >> d >> e >> f >> s >> m; ll t = a + b + c + (d + e) * m; double buy = (double)(m * f * s - p) / t; v[i] = P(buy, L); } sort(v.begin(), v.end(), comp); FOR(i, 0, n) cout << v[i].second << endl; puts("#"); } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define FOR(I, A, B) for (int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; typedef pair<double, string> P; bool comp(P a, P b) { if (fabs(a.first - b.first) <= 0.00000001) { return a.second <= b.second; } return a.first >= b.first; } int main() { int n; while (cin >> n, n) { vector<P> v(n); FOR(i, 0, n) { string L; cin >> L; ll p, a, b, c, d, e, f, s, m; cin >> p >> a >> b >> c >> d >> e >> f >> s >> m; ll t = a + b + c + (d + e) * m; double buy = (double)(m * f * s - p) / t; v[i] = P(buy, L); } sort(v.begin(), v.end(), comp); FOR(i, 0, n) cout << v[i].second << endl; puts("#"); } return 0; }
replace
21
22
21
22
0
p01315
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct cmp { bool operator()(const pair<double, string> &lhs, const pair<double, string> &rhs) const { return greater<double>()(lhs.first, rhs.first) ? true : less<string>()(lhs.second, rhs.second); } }; int main() { int N; while (cin >> N && N != 0) { vector<pair<double, string>> v(N); for (int i = 0; i < N; i++) { string L; int P, A, B, C, D, E, F, S, M; cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M; int t = A + B + C + (D + E) * M; int c = F * S * M; double e = double(c - P) / t; v[i] = make_pair(e, L); } sort(v.begin(), v.end(), cmp()); for (int i = 0; i < N; i++) { cout << v[i].second << endl; } cout << '#' << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct cmp { bool operator()(const pair<double, string> &lhs, const pair<double, string> &rhs) const { return lhs.first == rhs.first ? lhs.second < rhs.second : lhs.first > rhs.first; } }; int main() { int N; while (cin >> N && N != 0) { vector<pair<double, string>> v(N); for (int i = 0; i < N; i++) { string L; int P, A, B, C, D, E, F, S, M; cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M; int t = A + B + C + (D + E) * M; int c = F * S * M; double e = double(c - P) / t; v[i] = make_pair(e, L); } sort(v.begin(), v.end(), cmp()); for (int i = 0; i < N; i++) { cout << v[i].second << endl; } cout << '#' << endl; } return 0; }
replace
8
11
8
10
0
p01316
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int C[16]; int x[2001]; int dp[2001][256]; const int INF = 1 << 30; int main() { int N, M; while (cin >> N >> M, N) { for (int i = 0; i < M; i++) { cin >> C[i]; } for (int i = 0; i < N; i++) { cin >> x[i]; } for (int i = 0; i <= N; i++) { for (int j = 0; j < 256; j++) dp[i][j] = INF; } dp[0][128] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < 256; j++) { if (dp[i][j] == INF) continue; for (int k = 0; k < M; k++) { int nj = j + C[k]; if (0 > nj) nj = 0; if (nj > 255) nj = 255; int tmp = (nj - x[i]) * (nj - x[i]); if (dp[i][j] + tmp < dp[i + 1][nj]) { dp[i + 1][nj] = dp[i][j] + tmp; } } } } int ans = INF; for (int i = 0; i < 256; i++) { ans = min(ans, dp[N][i]); } cout << ans << endl; } }
#include "bits/stdc++.h" using namespace std; int C[16]; int x[20001]; int dp[20001][256]; const int INF = 1 << 30; int main() { int N, M; while (cin >> N >> M, N) { for (int i = 0; i < M; i++) { cin >> C[i]; } for (int i = 0; i < N; i++) { cin >> x[i]; } for (int i = 0; i <= N; i++) { for (int j = 0; j < 256; j++) dp[i][j] = INF; } dp[0][128] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < 256; j++) { if (dp[i][j] == INF) continue; for (int k = 0; k < M; k++) { int nj = j + C[k]; if (0 > nj) nj = 0; if (nj > 255) nj = 255; int tmp = (nj - x[i]) * (nj - x[i]); if (dp[i][j] + tmp < dp[i + 1][nj]) { dp[i + 1][nj] = dp[i][j] + tmp; } } } } int ans = INF; for (int i = 0; i < 256; i++) { ans = min(ans, dp[N][i]); } cout << ans << endl; } }
replace
3
5
3
5
0
p01316
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define MOD 1000000007 #define ALL(a) (a).begin(), (a).end() #define ms(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef long long ll; struct edge { int from, to, cost; }; int n, m; int c[20]; int x[20005]; int memo[20005][300]; int dp(int y, int i) { if (i == n) return 0; if (memo[i][y] != INF) return memo[i][y]; int res = INF; REP(j, m) { int tmp = y; tmp += c[j]; if (tmp > 255) tmp = 255; if (tmp < 0) tmp = 0; res = min(res, dp(tmp, i + 1) + (tmp - x[i]) * (tmp - x[i])); } return memo[y][i] = res; } int main() { while (cin >> n >> m, n) { REP(i, 20005) REP(j, 300) memo[i][j] = INF; REP(i, m) cin >> c[i]; REP(i, n) cin >> x[i]; cout << dp(128, 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define MOD 1000000007 #define ALL(a) (a).begin(), (a).end() #define ms(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef long long ll; struct edge { int from, to, cost; }; int n, m; int c[20]; int x[20005]; int memo[20005][300]; int dp(int y, int i) { if (i == n) return 0; if (memo[i][y] != INF) return memo[i][y]; int res = INF; REP(j, m) { int tmp = y; tmp += c[j]; if (tmp > 255) tmp = 255; if (tmp < 0) tmp = 0; res = min(res, dp(tmp, i + 1) + (tmp - x[i]) * (tmp - x[i])); } return memo[i][y] = res; } int main() { while (cin >> n >> m, n) { REP(i, 20005) REP(j, 300) memo[i][j] = INF; REP(i, m) cin >> c[i]; REP(i, n) cin >> x[i]; cout << dp(128, 0) << endl; } return 0; }
replace
37
38
37
38
TLE
p01316
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define inf 1e9 #define ll long long #define ull unsigned long long #define M 1000000007 #define P pair<int, int> #define PLL pair<ll, ll> #define FOR(i, m, n) for (int i = m; i < n; i++) #define RFOR(i, m, n) for (int i = m; i >= n; i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, n, 0) #define all(a) a.begin(), a.end() const int vx[4] = {0, 1, 0, -1}; const int vy[4] = {1, 0, -1, 0}; #define PI 3.14159265 void f(int n, int m) { int c[1000000]; rep(i, m) { cin >> c[i]; } int dp[20][300] = {}; rep(i, 20) { rep(j, 300) { dp[i][j] = inf; } } dp[0][128] = 0; FOR(i, 1, n + 1) { int x; cin >> x; rep(j, 256) { rep(k, m) { int a = j + c[k]; a = min(a, 255); a = max(a, 0); dp[i][a] = min(dp[i][a], dp[i - 1][j] + (x - a) * (x - a)); } } } int ans = inf; rep(i, 256) { ans = min(ans, dp[n][i]); } cout << ans << endl; } int main() { int n, m; while (1) { cin >> n >> m; if (n == 0) break; f(n, m); } return 0; }
#include <bits/stdc++.h> using namespace std; #define inf 1e9 #define ll long long #define ull unsigned long long #define M 1000000007 #define P pair<int, int> #define PLL pair<ll, ll> #define FOR(i, m, n) for (int i = m; i < n; i++) #define RFOR(i, m, n) for (int i = m; i >= n; i--) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) RFOR(i, n, 0) #define all(a) a.begin(), a.end() const int vx[4] = {0, 1, 0, -1}; const int vy[4] = {1, 0, -1, 0}; #define PI 3.14159265 void f(int n, int m) { int c[1000000]; rep(i, m) { cin >> c[i]; } int dp[21000][300] = {}; rep(i, 21000) { rep(j, 300) { dp[i][j] = inf; } } dp[0][128] = 0; FOR(i, 1, n + 1) { int x; cin >> x; rep(j, 256) { rep(k, m) { int a = j + c[k]; a = min(a, 255); a = max(a, 0); dp[i][a] = min(dp[i][a], dp[i - 1][j] + (x - a) * (x - a)); } } } int ans = inf; rep(i, 256) { ans = min(ans, dp[n][i]); } cout << ans << endl; } int main() { int n, m; while (1) { cin >> n >> m; if (n == 0) break; f(n, m); } return 0; }
replace
22
24
22
24
0