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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00474 | C++ | Runtime Error | // 21
#include <algorithm>
#include <iostream>
using namespace std;
int n, l;
int c[10000];
int t[10000];
int a = 0;
int gr(int i) {
if (t[i])
return t[i];
int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
t[i] = max(li, ri) + l - c[i];
a = max(a, t[i]);
return t[i];
}
int main() {
cin >> n >> l;
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
gr(i);
}
cout << a << endl;
} | // 21
#include <algorithm>
#include <iostream>
using namespace std;
int n, l;
int c[100000];
int t[100000];
int a = 0;
int gr(int i) {
if (t[i])
return t[i];
int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
t[i] = max(li, ri) + l - c[i];
a = max(a, t[i]);
return t[i];
}
int main() {
cin >> n >> l;
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
gr(i);
}
cout << a << endl;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p00474 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX 100000
typedef pair<int, int> P;
int main() {
int N, L, in, dp[MAX] = {0};
priority_queue<P> Q;
cin >> N >> L;
for (int i = 1; i <= N; i++) {
cin >> in;
Q.push(P(in, i));
}
while (!Q.empty()) {
P p = Q.top();
Q.pop();
dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
}
int ans = 0;
for (int i = 1; i <= N; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX 100002
typedef pair<int, int> P;
int main() {
int N, L, in, dp[MAX] = {0};
priority_queue<P> Q;
cin >> N >> L;
for (int i = 1; i <= N; i++) {
cin >> in;
Q.push(P(in, i));
}
while (!Q.empty()) {
P p = Q.top();
Q.pop();
dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
}
int ans = 0;
for (int i = 1; i <= N; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00474 | C++ | Runtime Error | #include <iostream>
using namespace std;
int ic[50002], icL[50002];
int maxer = 0;
int N, L;
void pick(int i) {
if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
maxer = max(ic[i], maxer);
icL[i] = 0;
pick(i + 1);
pick(i - 1);
}
}
int main() {
cin >> N >> L;
icL[0] = 0;
icL[N + 1] = 0;
for (int i = 1; i <= N; i++)
cin >> icL[i];
for (int i = 1; i <= N; i++) {
pick(i);
}
cout << maxer << endl;
return 0;
} | #include <iostream>
using namespace std;
int ic[100002], icL[100002];
int maxer = 0;
int N, L;
void pick(int i) {
if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
maxer = max(ic[i], maxer);
icL[i] = 0;
pick(i + 1);
pick(i - 1);
}
}
int main() {
cin >> N >> L;
icL[0] = 0;
icL[N + 1] = 0;
for (int i = 1; i <= N; i++)
cin >> icL[i];
for (int i = 1; i <= N; i++) {
pick(i);
}
cout << maxer << endl;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00478 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
string A, S, T;
int P = 0;
int n;
cin >> A;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> S;
T = S + S;
for (int j = 0; j < n; j++) {
if (T.substr(j, A.length()) == A) {
P++;
break;
}
}
}
cout << P << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
string A, S, T;
int P = 0;
int n;
cin >> A;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> S;
T = S + S;
for (int j = 0; j < 10; j++) {
if (T.substr(j, A.length()) == A) {
P++;
break;
}
}
}
cout << P << endl;
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p00479 | C++ | Time Limit Exceeded | #include <iostream>
#define lli long long int
using namespace std;
int main() {
lli n, m, s, t, u, v;
while (cin >> n && n) {
cin >> m;
if (n % 2 == 0) {
v = n / 2;
} else {
v = (n + 1) / 2;
}
while (m--) {
cin >> s >> t;
if (s > v) {
s = n - s + 1;
}
if (t > v) {
t = n - t + 1;
}
if (s < t) {
u = s;
} else {
u = t;
}
for (lli i = 1; i <= v; i += 3) {
if (i == u) {
cout << 1 << endl;
break;
} else if (i + 1 == u) {
cout << 2 << endl;
break;
} else if (i + 2 == u) {
cout << 3 << endl;
break;
}
}
}
}
return 0;
} | #include <iostream>
#define lli long long int
using namespace std;
int main() {
lli n, m, s, t, u, v;
while (cin >> n && n) {
cin >> m;
if (n % 2 == 0) {
v = n / 2;
} else {
v = (n + 1) / 2;
}
while (m--) {
cin >> s >> t;
if (s > v) {
s = n - s + 1;
}
if (t > v) {
t = n - t + 1;
}
if (s < t) {
u = s;
} else {
u = t;
}
u = (u - 1) % 3;
cout << (u + 1) << endl;
}
}
return 0;
} | replace | 30 | 43 | 30 | 32 | TLE | |
p00480 | C++ | Runtime Error | #include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w) \
rep(i, (h)) { \
cout << "["; \
rep(j, (w)) { cout << (arr)[i][j] << ", "; } \
cout << "]" << endl; \
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);
#define print_a(x) \
rep(jejeje, x.size()) { cout << x[jejeje] << " "; }
typedef pair<int, int> P;
int main() {
int n, tmp;
vector<int> x; //[0,n-1]
cin >> n; // 変数の個数
rep(i, n) {
cin >> tmp;
x.push_back(tmp);
}
unsigned long long int dp[99][21] = {0}; //[0,n-2] [0,20]
// 総数 : [今いる変数id:0-ori][今までの総和]
// dp[i][j]:=i番目の数まで使って(足すか引くかして)jを作ることが出来る通りの数
rep(i, n) rep(j, 21) dp[i][j] = 0;
dp[0][x[0]] = 1;
rep(i, n - 1) {
if (i == 0) {
continue;
}
rep(j, 21) {
if (j + x[i] < 21) {
dp[i][j + x[i]] += dp[i - 1][j];
}
if (j - x[i] > -1) {
dp[i][j - x[i]] += dp[i - 1][j];
}
}
}
cout << dp[n - 2][x[n - 1]] << endl;
} | #include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w) \
rep(i, (h)) { \
cout << "["; \
rep(j, (w)) { cout << (arr)[i][j] << ", "; } \
cout << "]" << endl; \
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);
#define print_a(x) \
rep(jejeje, x.size()) { cout << x[jejeje] << " "; }
typedef pair<int, int> P;
int main() {
int n, tmp;
vector<int> x; //[0,n-1]
cin >> n; // 変数の個数
rep(i, n) {
cin >> tmp;
x.push_back(tmp);
}
unsigned long long int dp[100][21] = {0}; //[0,n-2] [0,20]
// 総数 : [今いる変数id:0-ori][今までの総和]
// dp[i][j]:=i番目の数まで使って(足すか引くかして)jを作ることが出来る通りの数
rep(i, n) rep(j, 21) dp[i][j] = 0;
dp[0][x[0]] = 1;
rep(i, n - 1) {
if (i == 0) {
continue;
}
rep(j, 21) {
if (j + x[i] < 21) {
dp[i][j + x[i]] += dp[i - 1][j];
}
if (j - x[i] > -1) {
dp[i][j - x[i]] += dp[i - 1][j];
}
}
}
cout << dp[n - 2][x[n - 1]] << endl;
} | replace | 38 | 39 | 38 | 39 | 0 | |
p00480 | C++ | Runtime Error | #include <cmath>
#include <iostream>
#include <vector>
#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
int num;
cin >> num;
dp[0][num] = 1;
rep(i, 0, N - 1) {
cin >> num;
repe(j, 0, 20) {
if (j + num <= 20)
dp[i][j + num] += dp[i - 1][j];
if (j - num >= 0)
dp[i][j - num] += dp[i - 1][j];
}
}
cin >> num;
cout << dp[N - 2][num] << endl;
return 0;
} | #include <cmath>
#include <iostream>
#include <vector>
#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
using namespace std;
int main() {
int N;
cin >> N;
vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
int num;
cin >> num;
dp[0][num] = 1;
rep(i, 1, N) {
cin >> num;
repe(j, 0, 20) {
if (j + num <= 20)
dp[i][j + num] += dp[i - 1][j];
if (j - num >= 0)
dp[i][j - num] += dp[i - 1][j];
}
}
cin >> num;
cout << dp[N - 2][num] << endl;
return 0;
} | replace | 17 | 18 | 17 | 18 | -11 | |
p00480 | C++ | Runtime Error | #include <iostream>
int main() {
int a;
int dp[21][101] = {0};
int x[101];
std::cin >> a;
for (int i = 1; i <= a; i++)
std::cin >> x[i];
dp[1][x[1]] = 1;
for (int j = 2; j < a; j++) {
for (int s = 0; s <= 20; s++) {
if (s + x[j] <= 20 && s - x[j] >= 0) {
dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
} else if (s + x[j] <= 20 && s - x[j] < 0) {
dp[j][s] = dp[j - 1][s + x[j]];
} else if (s + x[j] > 20 && s - x[j] >= 0) {
dp[j][s] = dp[j - 1][s - x[j]];
}
}
}
std::cout << dp[a - 1][x[a]] << std::endl;
return 0;
} | #include <iostream>
int main() {
int a;
long long int dp[101][101] = {0};
int x[101];
std::cin >> a;
for (int i = 1; i <= a; i++)
std::cin >> x[i];
dp[1][x[1]] = 1;
for (int j = 2; j < a; j++) {
for (int s = 0; s <= 20; s++) {
if (s + x[j] <= 20 && s - x[j] >= 0) {
dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
} else if (s + x[j] <= 20 && s - x[j] < 0) {
dp[j][s] = dp[j - 1][s + x[j]];
} else if (s + x[j] > 20 && s - x[j] >= 0) {
dp[j][s] = dp[j - 1][s - x[j]];
}
}
}
std::cout << dp[a - 1][x[a]] << std::endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00480 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];
int main() {
while (cin >> n) {
rep(i, n) { cin >> d[i]; }
int p = 0;
unsigned long long int dp[100][22] = {0}; // dp[i][j]
dp[0][d[0]] = 1;
for (int i = 1; i < n - 1; i++) {
for (int j = 0; j <= 20; j++) {
if (dp[i - 1][j] > 0) {
if (j + d[i] <= 20) {
dp[i][j + d[i]] += dp[i - 1][j];
}
if (j - d[i] >= 0)
dp[i][j - d[i]] += dp[i - 1][j];
}
}
}
int sum = 0;
for (int i = 0; i <= 100000000000; i++) {
sum += 1;
}
cout << dp[n - 2][d[n - 1]] << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];
int main() {
while (cin >> n) {
rep(i, n) { cin >> d[i]; }
int p = 0;
unsigned long long int dp[100][22] = {0}; // dp[i][j]
dp[0][d[0]] = 1;
for (int i = 1; i < n - 1; i++) {
for (int j = 0; j <= 20; j++) {
if (dp[i - 1][j] > 0) {
if (j + d[i] <= 20) {
dp[i][j + d[i]] += dp[i - 1][j];
}
if (j - d[i] >= 0)
dp[i][j - d[i]] += dp[i - 1][j];
}
}
}
int sum = 0;
for (int i = 0; i <= 1000000000; i++) {
sum += 1;
}
cout << dp[n - 2][d[n - 1]] << endl;
}
return 0;
} | replace | 58 | 59 | 58 | 59 | TLE | |
p00481 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
int gy = y[num + 1], gx = x[num + 1];
int d[1001][1001];
for (int i = 0; i <= 1001; i++) {
for (int j = 0; j <= 1001; j++) {
d[i][j] = INF;
}
}
queue<int> qx;
queue<int> qy;
qx.push(x[num]);
qy.push(y[num]);
d[y[num]][x[num]] = 0;
while (qx.size() + qy.size() != 0) {
int ny, nx;
ny = qy.front();
qy.pop();
nx = qx.front();
qx.pop();
if (ny == gy && nx == gx) {
break;
}
for (int i = 0; i < 4; i++) {
int mx = nx + d1[i], my = ny + d2[i];
if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
d[my][mx] == INF) {
d[my][mx] = d[ny][nx] + 1;
qx.push(mx);
qy.push(my);
}
}
}
return d[gy][gx];
}
int main() {
int ans = 0;
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') {
y[0] = i;
x[0] = j;
} else if ('0' < map[i][j] && map[i][j] <= '9') {
y[(int)(map[i][j] - '0')] = i;
x[(int)(map[i][j] - '0')] = j;
}
}
}
for (int i = 0; i < n; i++) {
ans += bfs(i);
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define INF 100000
char map[1001][1001];
int h, w, n;
int d1[4] = {0, 1, 0, -1};
int d2[4] = {1, 0, -1, 0};
int i, j, x[10], y[10];
int bfs(int num) {
int gy = y[num + 1], gx = x[num + 1];
int d[1001][1001];
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
d[i][j] = INF;
}
}
queue<int> qx;
queue<int> qy;
qx.push(x[num]);
qy.push(y[num]);
d[y[num]][x[num]] = 0;
while (qx.size() + qy.size() != 0) {
int ny, nx;
ny = qy.front();
qy.pop();
nx = qx.front();
qx.pop();
if (ny == gy && nx == gx) {
break;
}
for (int i = 0; i < 4; i++) {
int mx = nx + d1[i], my = ny + d2[i];
if (0 <= mx && mx < w && 0 <= my && my < h && map[my][mx] != 'X' &&
d[my][mx] == INF) {
d[my][mx] = d[ny][nx] + 1;
qx.push(mx);
qy.push(my);
}
}
}
return d[gy][gx];
}
int main() {
int ans = 0;
cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') {
y[0] = i;
x[0] = j;
} else if ('0' < map[i][j] && map[i][j] <= '9') {
y[(int)(map[i][j] - '0')] = i;
x[(int)(map[i][j] - '0')] = j;
}
}
}
for (int i = 0; i < n; i++) {
ans += bfs(i);
}
cout << ans << endl;
return 0;
} | replace | 19 | 21 | 19 | 21 | -11 | |
p00481 | C++ | Runtime Error | #include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 65535
using namespace std;
typedef pair<int, int> P;
int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;
void clear();
void show();
P bfs(int cheese, P n);
int main(void) {
cin >> H >> W >> N;
P s, e;
for (int i = 0; i < H; i++)
cin >> Maps[i];
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (Maps[i][j] == 'S')
s.first = i, s.second = j;
for (int i = 1; i <= N; i++) {
clear();
s = bfs(i, s);
}
cout << ans << endl;
return 0;
}
P bfs(int cheese, P n) {
queue<P> que;
que.push(n);
dist[n.first][n.second] = 0;
P top;
while (que.size()) {
top = que.front();
if (Maps[top.first][top.second] - '0' == cheese) {
ans += dist[top.first][top.second];
break;
}
que.pop();
for (int i = 0; i < 4; i++) {
int nv = top.first + ver[i], nh = top.second + hor[i];
if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
Maps[nv][nh] != 'X') {
que.push(P(nv, nh));
dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
}
}
}
return top;
}
void clear() {
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
dist[i][j] = INFI;
} | #include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 100000000
using namespace std;
typedef pair<int, int> P;
int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;
void clear();
void show();
P bfs(int cheese, P n);
int main(void) {
cin >> H >> W >> N;
P s, e;
for (int i = 0; i < H; i++)
cin >> Maps[i];
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (Maps[i][j] == 'S')
s.first = i, s.second = j;
for (int i = 1; i <= N; i++) {
clear();
s = bfs(i, s);
}
cout << ans << endl;
return 0;
}
P bfs(int cheese, P n) {
queue<P> que;
que.push(n);
dist[n.first][n.second] = 0;
P top;
while (que.size()) {
top = que.front();
if (Maps[top.first][top.second] - '0' == cheese) {
ans += dist[top.first][top.second];
break;
}
que.pop();
for (int i = 0; i < 4; i++) {
int nv = top.first + ver[i], nh = top.second + hor[i];
if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
Maps[nv][nh] != 'X') {
que.push(P(nv, nh));
dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
}
}
}
return top;
}
void clear() {
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
dist[i][j] = INFI;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00481 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int main() {
int h, w, n;
int x[10], y[10];
string map[1000];
int dmap[1000][1000];
queue<int> qx, qy;
int dis;
cin >> h >> w >> n;
for (int i = 0; i < h; i++)
cin >> map[i];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] == 'S') {
x[0] = j;
y[0] = i;
break;
}
if (map[i][j] != 'X' && map[i][j] != '.') {
x[map[i][j] - '0'] = j;
y[map[i][j] - '0'] = i;
}
}
}
dis = 0;
for (int i = 0; i < n; i++) {
while (!qx.empty())
qx.pop();
while (!qy.empty())
qy.pop();
for (int k = 0; k < h; k++) {
for (int j = 0; j < w; j++) {
dmap[k][j] = 10000000;
}
}
dmap[y[i]][x[i]] = 0;
qx.push(x[i]);
qy.push(y[i]);
while (!qx.empty()) {
if (qx.front() > 0 && map[qy.front()][qx.front() - 1] != 'X' &&
dmap[qy.front()][qx.front() - 1] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front()][qx.front() - 1] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front() - 1);
qy.push(qy.front());
}
if (qy.front() > 0 && map[qy.front() - 1][qx.front()] != 'X' &&
dmap[qy.front() - 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front() - 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front());
qy.push(qy.front() - 1);
}
if (qx.front() < w - 1 && map[qy.front()][qx.front() + 1] != 'X' &&
dmap[qy.front()][qx.front() + 1] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front()][qx.front() + 1] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front() + 1);
qy.push(qy.front());
}
if (qy.front() < h - 1 && map[qy.front() + 1][qx.front()] != 'X' &&
dmap[qy.front() + 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front() + 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front());
qy.push(qy.front() + 1);
}
qx.pop();
qy.pop();
}
dis += dmap[y[i + 1]][x[i + 1]];
}
cout << dis << endl;
} | #include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int main() {
int h, w, n;
int x[10], y[10];
string map[1000];
int dmap[1000][1000];
queue<int> qx, qy;
int dis;
cin >> h >> w >> n;
for (int i = 0; i < h; i++)
cin >> map[i];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] == 'S') {
x[0] = j;
y[0] = i;
}
if (map[i][j] != 'X' && map[i][j] != '.') {
x[map[i][j] - '0'] = j;
y[map[i][j] - '0'] = i;
}
}
}
dis = 0;
for (int i = 0; i < n; i++) {
while (!qx.empty())
qx.pop();
while (!qy.empty())
qy.pop();
for (int k = 0; k < h; k++) {
for (int j = 0; j < w; j++) {
dmap[k][j] = 10000000;
}
}
dmap[y[i]][x[i]] = 0;
qx.push(x[i]);
qy.push(y[i]);
while (!qx.empty()) {
if (qx.front() > 0 && map[qy.front()][qx.front() - 1] != 'X' &&
dmap[qy.front()][qx.front() - 1] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front()][qx.front() - 1] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front() - 1);
qy.push(qy.front());
}
if (qy.front() > 0 && map[qy.front() - 1][qx.front()] != 'X' &&
dmap[qy.front() - 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front() - 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front());
qy.push(qy.front() - 1);
}
if (qx.front() < w - 1 && map[qy.front()][qx.front() + 1] != 'X' &&
dmap[qy.front()][qx.front() + 1] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front()][qx.front() + 1] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front() + 1);
qy.push(qy.front());
}
if (qy.front() < h - 1 && map[qy.front() + 1][qx.front()] != 'X' &&
dmap[qy.front() + 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
dmap[qy.front() + 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
qx.push(qx.front());
qy.push(qy.front() + 1);
}
qx.pop();
qy.pop();
}
dis += dmap[y[i + 1]][x[i + 1]];
}
cout << dis << endl;
} | delete | 23 | 24 | 23 | 23 | 0 | |
p00481 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second
const int M = 1024;
const int INF = 114514;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);
void init() {
rep(y, M) rep(x, M) f[y][x] = '$';
cheese.clear();
}
bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }
int bfs(P s, P g) {
int gx = g.F, gy = g.S;
queue<P> Q;
rep(y, M) rep(x, M) d[y][x] = INF;
Q.push(s);
d[s.S][s.F] = 0;
while (Q.size()) {
P p = Q.front();
Q.pop();
if (p.F == gx and p.S == gy)
break;
rep(i, 4) {
int nx = p.F + dx[i], ny = p.S + dy[i];
if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
continue;
d[ny][nx] = d[p.S][p.F] + 1;
Q.push(P(nx, ny));
}
}
return d[gy][gx];
}
int main() {
while (cin >> h >> w >> n) {
init();
rep(y, h) rep(x, w) {
cin >> f[y][x];
char c = f[y][x];
if (c != '.' and c != 'S' and c != 'X')
cheese[c - '0'] = make_pair(x, y);
if (c == 'S')
cheese[0] = make_pair(x, y);
}
int ans = 0;
rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second
const int M = 1024;
const int INF = 114514810;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);
void init() {
rep(y, M) rep(x, M) f[y][x] = '$';
cheese.clear();
}
bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }
int bfs(P s, P g) {
int gx = g.F, gy = g.S;
queue<P> Q;
rep(y, M) rep(x, M) d[y][x] = INF;
Q.push(s);
d[s.S][s.F] = 0;
while (Q.size()) {
P p = Q.front();
Q.pop();
if (p.F == gx and p.S == gy)
break;
rep(i, 4) {
int nx = p.F + dx[i], ny = p.S + dy[i];
if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
continue;
d[ny][nx] = d[p.S][p.F] + 1;
Q.push(P(nx, ny));
}
}
return d[gy][gx];
}
int main() {
while (cin >> h >> w >> n) {
init();
rep(y, h) rep(x, w) {
cin >> f[y][x];
char c = f[y][x];
if (c != '.' and c != 'S' and c != 'X')
cheese[c - '0'] = make_pair(x, y);
if (c == 'S')
cheese[0] = make_pair(x, y);
}
int ans = 0;
rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
cout << ans << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00481 | C++ | Runtime Error | #include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;
int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs() {
for (int n = 1; n < N + 1; n++) {
queue<pair<int, int>> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
que.push(start);
d[start.first][start.second] = 0;
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
if (field[p.first][p.second] == '0' + (char)n) {
cheese = p;
break;
}
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(pair<int, int>(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
result += d[cheese.first][cheese.second];
start = cheese;
}
}
int main() {
scanf(" %d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
scanf(" %c", &field[i][j]);
if (field[i][j] == 'S') {
start.first = i;
start.second = j;
}
}
}
bfs();
printf("%d\n", result);
}
| #include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;
int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs() {
for (int n = 1; n < N + 1; n++) {
queue<pair<int, int>> que;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
que.push(start);
d[start.first][start.second] = 0;
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
if (field[p.first][p.second] == '0' + (char)n) {
cheese = p;
break;
}
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(pair<int, int>(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
result += d[cheese.first][cheese.second];
start = cheese;
}
}
int main() {
scanf(" %d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
scanf(" %c", &field[i][j]);
if (field[i][j] == 'S') {
start.first = i;
start.second = j;
}
}
}
bfs();
printf("%d\n", result);
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p00481 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
#define INF 10000000
typedef pair<int, int> P;
queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;
int main() {
scanf("%d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
scanf("%s", map[i]);
char *ptr = strchr(map[i], 'S');
if (ptr != NULL)
sx = ptr - map[i], sy = i;
}
que.push(P(sx, sy));
for (int i = 1; i <= N; i++) {
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
vst[j][k] = INF;
}
}
vst[sy][sx] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
if (map[p.second][p.first] == i + '0') {
sum += vst[p.second][p.first];
while (!que.empty())
que.pop();
que.push(P(p.first, p.second));
sx = p.first, sy = p.second;
break;
}
for (int j = 0; j < 4; j++) {
int nx = p.first + dx[j], ny = p.second + dy[j];
if (map[ny][nx] != 'X' && vst[ny][nx] == INF) {
vst[ny][nx] = vst[p.second][p.first] + 1;
que.push(P(nx, ny));
}
}
}
}
printf("%d\n", sum);
return 0;
} | #include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
#define INF 10000000
typedef pair<int, int> P;
queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;
int main() {
scanf("%d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
scanf("%s", map[i]);
char *ptr = strchr(map[i], 'S');
if (ptr != NULL)
sx = ptr - map[i], sy = i;
}
que.push(P(sx, sy));
for (int i = 1; i <= N; i++) {
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
vst[j][k] = INF;
}
}
vst[sy][sx] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
if (map[p.second][p.first] == i + '0') {
sum += vst[p.second][p.first];
while (!que.empty())
que.pop();
que.push(P(p.first, p.second));
sx = p.first, sy = p.second;
break;
}
for (int j = 0; j < 4; j++) {
int nx = p.first + dx[j], ny = p.second + dy[j];
if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' &&
vst[ny][nx] == INF) {
vst[ny][nx] = vst[p.second][p.first] + 1;
que.push(P(nx, ny));
}
}
}
}
printf("%d\n", sum);
return 0;
} | replace | 45 | 46 | 45 | 47 | 0 | |
p00481 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, int> PI;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<vector<int>> Array2;
typedef vector<LL> LArray;
typedef vector<P> PArray;
typedef vector<string> SArray;
const int INF = 1 << 29;
const LL LINF = 1LL << 60;
inline int getInt() {
int itiv;
return (cin >> itiv, itiv);
}
template <typename T> void readAll(vector<T> &vec) {
for (int i = 0, size = vec.size(); i < size; i++)
cin >> vec[i];
}
template <typename T, typename U> void readAll(vector<pair<T, U>> &vec) {
for (int i = 0, size = vec.size(); i < size; i++)
cin >> vec[i].first >> vec[i].second;
}
template <typename T> inline string toString(const T &a) {
ostringstream oss;
oss << a;
return oss.str();
};
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int dr[] = {0, -1, 0, 1}, dc[] = {-1, 0, 1, 0};
bool visited[1000][1000];
char stage[1000][1000];
int h, w, n;
int main(void) {
ios::sync_with_stdio(false);
while (cin >> h >> w >> n && n) {
PArray goals(n + 1);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> stage[i][j];
if (stage[i][j] == 'S')
goals[0] = P(i, j);
int num = stage[i][j] - '0';
if (inRange(n + 1, num))
goals[num] = P(i, j);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
P start = goals[i], goal = goals[i + 1];
memset(visited, false, sizeof(visited));
queue<PI> que;
que.push(PI(start, 0));
while (!que.empty()) {
PI pi = que.front();
que.pop();
if (goal == pi.first) {
ans += pi.second;
break;
}
int r = pi.first.first, c = pi.first.second;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i], nc = c + dc[i];
if (!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' ||
visited[nr][nc])
continue;
que.push(PI(P(nr, nc), pi.second + 1));
visited[r][c] = true;
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, int> PI;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<vector<int>> Array2;
typedef vector<LL> LArray;
typedef vector<P> PArray;
typedef vector<string> SArray;
const int INF = 1 << 29;
const LL LINF = 1LL << 60;
inline int getInt() {
int itiv;
return (cin >> itiv, itiv);
}
template <typename T> void readAll(vector<T> &vec) {
for (int i = 0, size = vec.size(); i < size; i++)
cin >> vec[i];
}
template <typename T, typename U> void readAll(vector<pair<T, U>> &vec) {
for (int i = 0, size = vec.size(); i < size; i++)
cin >> vec[i].first >> vec[i].second;
}
template <typename T> inline string toString(const T &a) {
ostringstream oss;
oss << a;
return oss.str();
};
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int dr[] = {0, -1, 0, 1}, dc[] = {-1, 0, 1, 0};
bool visited[1000][1000];
char stage[1000][1000];
int h, w, n;
int main(void) {
ios::sync_with_stdio(false);
while (cin >> h >> w >> n && n) {
PArray goals(n + 1);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> stage[i][j];
if (stage[i][j] == 'S')
goals[0] = P(i, j);
int num = stage[i][j] - '0';
if (inRange(n + 1, num))
goals[num] = P(i, j);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
P start = goals[i], goal = goals[i + 1];
memset(visited, false, sizeof(visited));
queue<PI> que;
que.push(PI(start, 0));
while (!que.empty()) {
PI pi = que.front();
que.pop();
if (goal == pi.first) {
ans += pi.second;
break;
}
int r = pi.first.first, c = pi.first.second;
for (int i = 0; i < 4; i++) {
int nr = r + dr[i], nc = c + dc[i];
if (!inRange(h, nr) || !inRange(w, nc) || stage[nr][nc] == 'X' ||
visited[nr][nc])
continue;
que.push(PI(P(nr, nc), pi.second + 1));
visited[nr][nc] = true;
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 96 | 97 | 96 | 97 | MLE | |
p00481 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int xs[10], ys[10];
int H, W, N;
string S[1002];
int main() {
scanf("%d%d%d", &H, &W, &N);
for (int i = 0; i < W + 2; i++) {
S[0] += 'X';
S[H + 1] += 'X';
}
for (int i = 1; i <= H; i++) {
string x;
cin >> x;
S[i] += 'X' + x + 'X';
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (S[i][j] != 'X' && S[i][j] != '.') {
if (S[i][j] == 'S') {
xs[0] = j;
ys[0] = i;
} else {
xs[S[i][j] - '0'] = j;
ys[S[i][j] - '0'] = i;
}
}
}
}
int XX[102][102];
bool FF[102][102];
int ans = 0;
vector<pair<int, int>> vec1;
vector<pair<int, int>> vec2;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
for (int x = 0; x < N; x++) {
vec1.clear();
vec2.clear();
for (int i = 0; i < H + 2; i++)
for (int j = 0; j < W + 2; j++) {
XX[i][j] = -1;
FF[i][j] = false;
}
int cou = 0;
XX[ys[x]][xs[x]] = cou;
FF[ys[x]][xs[x]] = true;
vec1.push_back(make_pair(ys[x], xs[x]));
while (XX[ys[x + 1]][xs[x + 1]] == -1) {
vec2.clear();
for (int i = 0; i < vec1.size(); i++) {
for (int j = 0; j < 4; j++) {
if (S[vec1[i].first + dy[j]][vec1[i].second + dx[j]] == 'X')
continue;
if (XX[vec1[i].first + dy[j]][vec1[i].second + dx[j]] != -1)
continue;
if (FF[vec1[i].first + dy[j]][vec1[i].second + dx[j]])
continue;
vec2.push_back(
make_pair(vec1[i].first + dy[j], vec1[i].second + dx[j]));
FF[vec1[i].first + dy[j]][vec1[i].second + dx[j]] = true;
}
}
vec1.clear();
for (int i = 0; i < vec2.size(); i++)
vec1.push_back(vec2[i]);
cou++;
for (int i = 0; i < vec1.size(); i++) {
XX[vec1[i].first][vec1[i].second] = cou;
}
}
ans += XX[ys[x + 1]][xs[x + 1]];
}
printf("%d\n", ans);
return 0;
} | #include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int xs[10], ys[10];
int H, W, N;
string S[1002];
int main() {
scanf("%d%d%d", &H, &W, &N);
for (int i = 0; i < W + 2; i++) {
S[0] += 'X';
S[H + 1] += 'X';
}
for (int i = 1; i <= H; i++) {
string x;
cin >> x;
S[i] += 'X' + x + 'X';
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (S[i][j] != 'X' && S[i][j] != '.') {
if (S[i][j] == 'S') {
xs[0] = j;
ys[0] = i;
} else {
xs[S[i][j] - '0'] = j;
ys[S[i][j] - '0'] = i;
}
}
}
}
int XX[1002][1002];
bool FF[1002][1002];
int ans = 0;
vector<pair<int, int>> vec1;
vector<pair<int, int>> vec2;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
for (int x = 0; x < N; x++) {
vec1.clear();
vec2.clear();
for (int i = 0; i < H + 2; i++)
for (int j = 0; j < W + 2; j++) {
XX[i][j] = -1;
FF[i][j] = false;
}
int cou = 0;
XX[ys[x]][xs[x]] = cou;
FF[ys[x]][xs[x]] = true;
vec1.push_back(make_pair(ys[x], xs[x]));
while (XX[ys[x + 1]][xs[x + 1]] == -1) {
vec2.clear();
for (int i = 0; i < vec1.size(); i++) {
for (int j = 0; j < 4; j++) {
if (S[vec1[i].first + dy[j]][vec1[i].second + dx[j]] == 'X')
continue;
if (XX[vec1[i].first + dy[j]][vec1[i].second + dx[j]] != -1)
continue;
if (FF[vec1[i].first + dy[j]][vec1[i].second + dx[j]])
continue;
vec2.push_back(
make_pair(vec1[i].first + dy[j], vec1[i].second + dx[j]));
FF[vec1[i].first + dy[j]][vec1[i].second + dx[j]] = true;
}
}
vec1.clear();
for (int i = 0; i < vec2.size(); i++)
vec1.push_back(vec2[i]);
cou++;
for (int i = 0; i < vec1.size(); i++) {
XX[vec1[i].first][vec1[i].second] = cou;
}
}
ans += XX[ys[x + 1]][xs[x + 1]];
}
printf("%d\n", ans);
return 0;
} | replace | 32 | 34 | 32 | 34 | 0 | |
p00481 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> P;
const int INF = 1e+9;
int H, W, N;
int d[1010][1010];
char board[1010][1010];
int sx, sy;
int gx, gy;
int ans;
void bfs(int y, int x, int k) {
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
d[i][j] = INF;
if (board[i][j] == k + '0') {
gx = j;
gy = i;
}
}
}
d[y][x] = 0;
queue<P> que;
que.push(P(y, x));
while (!que.empty()) {
P p = que.front();
que.pop();
if (p.first == gy && p.second == gx)
break;
for (int i = 0; i < 4; i++) {
int nx = p.second + dx[i];
int ny = p.first + dy[i];
if (nx >= 1 && nx <= W && ny >= 1 && ny <= H && board[ny][nx] != 'X' &&
d[ny][nx] > d[p.first][p.second]) {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
ans += d[gy][gx];
if (k != N)
bfs(gy, gx, k + 1);
}
int main() {
cin >> H >> W >> N;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cin >> board[i][j];
}
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (board[i][j] == 'S') {
sx = j;
sy = i;
}
}
}
bfs(sy, sx, 1);
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
typedef pair<int, int> P;
const int INF = 1e+9;
int H, W, N;
int d[1010][1010];
char board[1010][1010];
int sx, sy;
int gx, gy;
int ans;
void bfs(int y, int x, int k) {
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
d[i][j] = INF;
if (board[i][j] == k + '0') {
gx = j;
gy = i;
}
}
}
d[y][x] = 0;
queue<P> que;
que.push(P(y, x));
while (!que.empty()) {
P p = que.front();
que.pop();
if (p.first == gy && p.second == gx)
break;
for (int i = 0; i < 4; i++) {
int nx = p.second + dx[i];
int ny = p.first + dy[i];
if (nx >= 1 && nx <= W && ny >= 1 && ny <= H && board[ny][nx] != 'X' &&
d[ny][nx] == INF) {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
ans += d[gy][gx];
if (k != N)
bfs(gy, gx, k + 1);
}
int main() {
cin >> H >> W >> N;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cin >> board[i][j];
}
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (board[i][j] == 'S') {
sx = j;
sy = i;
}
}
}
bfs(sy, sx, 1);
cout << ans << endl;
return 0;
} | replace | 69 | 70 | 69 | 70 | 0 | |
p00481 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct OZAWA {
int x;
int y;
int c;
};
int Flag[1000][1000];
char MAP[1000][1000];
queue<OZAWA> Q;
OZAWA ba, v;
int main(void) {
int H, W, N, Nc = 0, X[10], Y[10];
int mv[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
char Cheese[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
memset(MAP, 'X', sizeof(MAP));
memset(Flag, 0, sizeof(Flag));
scanf("%d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
// printf("%d %d ",i,j);
cin >> MAP[i][j];
// printf("Q");
if (MAP[i][j] == 'S') {
ba.x = j;
ba.y = i;
ba.c = 0;
Flag[i][j] = 1;
Q.push(ba);
// printf("Sin\n");
}
for (int ii = 0; ii < 9; ii++) {
if (MAP[i][j] == Cheese[ii]) {
X[ii] = j;
Y[ii] = i;
// printf("\n@@@\n");
}
}
}
}
// printf("S %d %d\n",ba.x,ba.y);
// for(int ii=0;ii<2;ii++)
// printf("%d %d\n",X[0],Y[0]);
while (!Q.empty()) {
// printf("QA\n");
ba = Q.front();
Q.pop();
if (ba.x == X[Nc] && ba.y == Y[Nc]) {
// printf("OZA");
Nc++;
memset(Flag, 0, sizeof(Flag));
while (!Q.empty()) {
Q.pop();
}
if (Nc == N)
break;
Q.push(ba);
Flag[ba.y][ba.x] = 1;
}
// printf("aaa\n");
// if(Nc == N) break;
for (int i = 0; i < 4; i++) {
if (MAP[ba.y + mv[i][1]][ba.x + mv[i][0]] != 'X' &&
Flag[ba.y + mv[i][1]][ba.x + mv[i][0]] == 0 &&
(ba.x + mv[i][0]) >= 0 && (ba.x + mv[i][0]) < W &&
(ba.y + mv[i][1]) >= 0 && (ba.y + mv[i][1]) < H) {
v.x = ba.x + mv[i][0];
v.y = ba.y + mv[i][1];
v.c = ba.c + 1;
Q.push(v);
Flag[v.y][v.x] = 1;
// printf("%d %d\n",v.x,v.y);
}
}
}
cout << ba.c << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct OZAWA {
int x;
int y;
int c;
};
int Flag[1001][1001];
char MAP[1001][1001];
queue<OZAWA> Q;
OZAWA ba, v;
int main(void) {
int H, W, N, Nc = 0, X[10], Y[10];
int mv[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
char Cheese[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
memset(MAP, 'X', sizeof(MAP));
memset(Flag, 0, sizeof(Flag));
scanf("%d %d %d", &H, &W, &N);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
// printf("%d %d ",i,j);
cin >> MAP[i][j];
// printf("Q");
if (MAP[i][j] == 'S') {
ba.x = j;
ba.y = i;
ba.c = 0;
Flag[i][j] = 1;
Q.push(ba);
// printf("Sin\n");
}
for (int ii = 0; ii < 9; ii++) {
if (MAP[i][j] == Cheese[ii]) {
X[ii] = j;
Y[ii] = i;
// printf("\n@@@\n");
}
}
}
}
// printf("S %d %d\n",ba.x,ba.y);
// for(int ii=0;ii<2;ii++)
// printf("%d %d\n",X[0],Y[0]);
while (!Q.empty()) {
// printf("QA\n");
ba = Q.front();
Q.pop();
if (ba.x == X[Nc] && ba.y == Y[Nc]) {
// printf("OZA");
Nc++;
memset(Flag, 0, sizeof(Flag));
while (!Q.empty()) {
Q.pop();
}
if (Nc == N)
break;
Q.push(ba);
Flag[ba.y][ba.x] = 1;
}
// printf("aaa\n");
// if(Nc == N) break;
for (int i = 0; i < 4; i++) {
if (MAP[ba.y + mv[i][1]][ba.x + mv[i][0]] != 'X' &&
Flag[ba.y + mv[i][1]][ba.x + mv[i][0]] == 0 &&
(ba.x + mv[i][0]) >= 0 && (ba.x + mv[i][0]) < W &&
(ba.y + mv[i][1]) >= 0 && (ba.y + mv[i][1]) < H) {
v.x = ba.x + mv[i][0];
v.y = ba.y + mv[i][1];
v.c = ba.c + 1;
Q.push(v);
Flag[v.y][v.x] = 1;
// printf("%d %d\n",v.x,v.y);
}
}
}
cout << ba.c << endl;
return 0;
} | replace | 11 | 13 | 11 | 13 | 0 | |
p00481 | C++ | Runtime Error | #include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
LL to, cost;
};
int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;
int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
std::queue<P> que;
memset(d, -1, sizeof(d));
que.push({sx, sy});
d[sx][sy] = 0;
while (que.size() > 0) {
auto pos = que.front();
que.pop();
if (pos.first == gx && pos.second == gy) {
break;
}
rep(i, 4) {
auto toX = pos.first + dx[i], toY = pos.second + dy[i];
if (0 <= toX && toX < H && 0 <= toY && toY < W) {
if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
que.push({toX, toY});
d[toX][toY] = d[pos.first][pos.second] + 1;
}
}
}
}
}
int main() {
in >> H >> W >> N;
rep(i, H) { in >> field[i]; }
rep(i, H) {
rep(j, W) {
if (field[i][j] == 'S') {
Sx = i;
Sy = j;
} else if (field[i][j] != '.') {
x[field[i][j] - '0'] = i;
y[field[i][j] - '0'] = j;
}
}
}
x[0] = Sx;
y[0] = Sy;
rep(i, N) {
bfs(x[i], y[i], x[i + 1], y[i + 1]);
ans += d[x[i + 1]][y[i + 1]];
}
out << ans << std::endl;
return 0;
} | #include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
LL to, cost;
};
int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;
int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
std::queue<P> que;
memset(d, -1, sizeof(d));
que.push({sx, sy});
d[sx][sy] = 0;
while (que.size() > 0) {
auto pos = que.front();
que.pop();
if (pos.first == gx && pos.second == gy) {
break;
}
rep(i, 4) {
auto toX = pos.first + dx[i], toY = pos.second + dy[i];
if (0 <= toX && toX < H && 0 <= toY && toY < W) {
if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
que.push({toX, toY});
d[toX][toY] = d[pos.first][pos.second] + 1;
}
}
}
}
}
int main() {
in >> H >> W >> N;
rep(i, H) { in >> field[i]; }
rep(i, H) {
rep(j, W) {
if (field[i][j] == 'X') {
continue;
}
if (field[i][j] == 'S') {
Sx = i;
Sy = j;
} else if (field[i][j] != '.') {
x[field[i][j] - '0'] = i;
y[field[i][j] - '0'] = j;
}
}
}
x[0] = Sx;
y[0] = Sy;
rep(i, N) {
bfs(x[i], y[i], x[i + 1], y[i + 1]);
ans += d[x[i + 1]][y[i + 1]];
}
out << ans << std::endl;
return 0;
} | insert | 47 | 47 | 47 | 50 | 0 | |
p00481 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#define N 1002
using namespace std;
int bfs(pair<int, int>, pair<int, int>);
int h, w, d[N][N];
string str[N];
int main() {
int n, i, j, sum;
pair<int, int> P[10];
cin >> h >> w >> n;
for (i = 0; i <= w + 1; i++)
str[0] += 'X', str[h + 1] += 'X';
for (i = 1; i <= h; i++)
cin >> str[i], str[i] = 'X' + str[i] + 'X';
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (str[i][j] == 'S')
P[0].first = i, P[0].second = j;
if ('1' <= str[i][j] && str[i][j] <= '9') {
P[str[i][j] - '0'].first = i;
P[str[i][j] - '0'].second = j;
}
}
}
sum = bfs(P[0], P[1]);
for (i = 1; i < n; i++)
sum += bfs(P[i], P[i + 1]);
cout << sum << endl;
}
int bfs(pair<int, int> s, pair<int, int> g) {
int i, j, ret;
pair<int, int> t;
pair<int, int> u;
queue<pair<int, int>> Q;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
d[i][j] = -1;
}
}
Q.push(s);
d[s.first][s.second] = 0;
while (Q.empty() != 1) {
u = Q.front();
if (str[u.first][u.second] == str[g.first][g.second]) {
ret = d[u.first][u.second];
break;
}
Q.pop();
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (u.first == i && u.second == j) {
if (d[i][j - 1] == -1 && str[i][j - 1] != 'X') {
d[i][j - 1] = d[u.first][u.second] + 1;
t.first = i;
t.second = j - 1;
Q.push(t);
}
if (d[i][j + 1] == -1 && str[i][j + 1] != 'X') {
d[i][j + 1] = d[u.first][u.second] + 1;
t.first = i;
t.second = j + 1;
Q.push(t);
}
if (d[i - 1][j] == -1 && str[i - 1][j] != 'X') {
d[i - 1][j] = d[u.first][u.second] + 1;
t.first = i - 1;
t.second = j;
Q.push(t);
}
if (d[i + 1][j] == -1 && str[i + 1][j] != 'X') {
d[i + 1][j] = d[u.first][u.second] + 1;
t.first = i + 1;
t.second = j;
Q.push(t);
}
}
}
}
}
return ret;
} | #include <iostream>
#include <queue>
#define N 1002
using namespace std;
int bfs(pair<int, int>, pair<int, int>);
int h, w, d[N][N];
string str[N];
int main() {
int n, i, j, sum;
pair<int, int> P[10];
cin >> h >> w >> n;
for (i = 0; i <= w + 1; i++)
str[0] += 'X', str[h + 1] += 'X';
for (i = 1; i <= h; i++)
cin >> str[i], str[i] = 'X' + str[i] + 'X';
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (str[i][j] == 'S')
P[0].first = i, P[0].second = j;
if ('1' <= str[i][j] && str[i][j] <= '9') {
P[str[i][j] - '0'].first = i;
P[str[i][j] - '0'].second = j;
}
}
}
sum = bfs(P[0], P[1]);
for (i = 1; i < n; i++)
sum += bfs(P[i], P[i + 1]);
cout << sum << endl;
}
int bfs(pair<int, int> s, pair<int, int> g) {
int i, j, ret;
pair<int, int> t;
pair<int, int> u;
queue<pair<int, int>> Q;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
d[i][j] = -1;
}
}
Q.push(s);
d[s.first][s.second] = 0;
while (Q.empty() != 1) {
u = Q.front();
if (str[u.first][u.second] == str[g.first][g.second]) {
ret = d[u.first][u.second];
break;
}
Q.pop();
if (d[u.first][u.second - 1] == -1 && str[u.first][u.second - 1] != 'X') {
d[u.first][u.second - 1] = d[u.first][u.second] + 1;
t.first = u.first;
t.second = u.second - 1;
Q.push(t);
}
if (d[u.first][u.second + 1] == -1 && str[u.first][u.second + 1] != 'X') {
d[u.first][u.second + 1] = d[u.first][u.second] + 1;
t.first = u.first;
t.second = u.second + 1;
Q.push(t);
}
if (d[u.first - 1][u.second] == -1 && str[u.first - 1][u.second] != 'X') {
d[u.first - 1][u.second] = d[u.first][u.second] + 1;
t.first = u.first - 1;
t.second = u.second;
Q.push(t);
}
if (d[u.first + 1][u.second] == -1 && str[u.first + 1][u.second] != 'X') {
d[u.first + 1][u.second] = d[u.first][u.second] + 1;
t.first = u.first + 1;
t.second = u.second;
Q.push(t);
}
}
return ret;
} | replace | 49 | 78 | 49 | 72 | TLE | |
p00481 | C++ | Memory Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
// #define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()
#define INTMAX 2147483647
#define INF 200000000
#define TINT(str) atoi(str.c_str())
#define TSTR(num) to_string(num)
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int>> VII;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
PII goal[10];
char k[1000][1000];
int fx, fy;
int H, W;
int BFS(int x, int y) {
VII cost;
cost = VII(H, VI(W, INT_MAX));
cost[fx][fy] = 0;
queue<PII> q;
q.push(make_pair(fx, fy));
while (!q.empty()) {
PII now = q.front();
q.pop();
int nx = now.first;
int ny = now.second;
if (nx == x && ny == y) {
return cost[nx][ny];
}
REP(i, 4) {
if (nx + dx[i] >= H || nx + dx[i] < 0)
continue;
if (ny + dy[i] >= W || ny + dy[i] < 0)
continue;
if (k[nx + dx[i]][ny + dy[i]] != 'X') {
if (cost[nx + dx[i]][ny + dy[i]] > cost[nx][ny]) {
cost[nx + dx[i]][ny + dy[i]] = cost[nx][ny] + 1;
q.push(make_pair(nx + dx[i], ny + dy[i]));
}
}
}
}
return -1;
}
signed main() {
int N;
cin >> H >> W >> N;
char str[1001];
REP(h, H) {
scanf("%s", str);
REP(w, W) {
k[h][w] = str[w];
if (k[h][w] == 'S') {
fx = h;
fy = w;
}
if (k[h][w] != '.' && k[h][w] != 'X') {
goal[k[h][w] - '1'] = make_pair(h, w);
}
}
}
int ans = 0;
REP(i, N) {
ans += BFS(goal[i].first, goal[i].second);
fx = goal[i].first;
fy = goal[i].second;
}
cout << ans << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
// #define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()
#define INTMAX 2147483647
#define INF 200000000
#define TINT(str) atoi(str.c_str())
#define TSTR(num) to_string(num)
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int>> VII;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
PII goal[10];
char k[1000][1000];
int fx, fy;
int H, W;
int BFS(int x, int y) {
VII cost;
cost = VII(H, VI(W, INT_MAX));
cost[fx][fy] = 0;
queue<PII> q;
q.push(make_pair(fx, fy));
while (!q.empty()) {
PII now = q.front();
q.pop();
int nx = now.first;
int ny = now.second;
if (nx == x && ny == y) {
return cost[nx][ny];
}
REP(i, 4) {
if (nx + dx[i] >= H || nx + dx[i] < 0)
continue;
if (ny + dy[i] >= W || ny + dy[i] < 0)
continue;
if (k[nx + dx[i]][ny + dy[i]] != 'X') {
if (cost[nx + dx[i]][ny + dy[i]] > cost[nx][ny] + 1) {
cost[nx + dx[i]][ny + dy[i]] = cost[nx][ny] + 1;
q.push(make_pair(nx + dx[i], ny + dy[i]));
}
}
}
}
return -1;
}
signed main() {
int N;
cin >> H >> W >> N;
char str[1001];
REP(h, H) {
scanf("%s", str);
REP(w, W) {
k[h][w] = str[w];
if (k[h][w] == 'S') {
fx = h;
fy = w;
}
if (k[h][w] != '.' && k[h][w] != 'X') {
goal[k[h][w] - '1'] = make_pair(h, w);
}
}
}
int ans = 0;
REP(i, N) {
ans += BFS(goal[i].first, goal[i].second);
fx = goal[i].first;
fy = goal[i].second;
}
cout << ans << endl;
return 0;
}
| replace | 58 | 59 | 58 | 59 | MLE | |
p00481 | C++ | Runtime Error | #include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>
using namespace std;
const int INF = 100000000;
const int MAX_W = 1000;
const int MAX_H = 1000;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
queue<P> que;
P start;
int pow = 1;
while (std::cin >> H >> W >> N, H) {
for (int i = 0; i < H; i++) {
std::cin >> field[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
if (field[i][j] == 'S') {
start = P(i, j);
}
}
}
que.push(start);
d[start.first][start.second] = 0;
int count = 0;
while (que.size()) {
P p = que.front();
que.pop();
if (field[p.first][p.second] == '0' + N and N <= pow) {
count = d[p.first][p.second];
break;
}
for (int i = 0; i < 4; i++) {
int nx = p.second + dx[i];
int ny = p.first + dy[i];
if (nx >= 0 and ny >= 0 and nx < W and ny < H and
field[ny][nx] != 'X' and d[ny][nx] == INF) {
if (field[ny][nx] == '.') {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
} else {
if (field[ny][nx] <= '0' + pow) {
// cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
int s = d[p.first][p.second];
field[ny][nx] = '.';
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
while (que.size()) {
que.pop();
}
que.push(P(ny, nx));
d[ny][nx] = s + 1;
if (pow == N) {
field[ny][nx] = '0' + N;
}
pow++;
break;
} else {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
}
}
std::cout << count << std::endl;
break;
}
return 0;
} | #include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>
using namespace std;
const int INF = 100000000;
const int MAX_W = 1001;
const int MAX_H = 1001;
int H, W, N;
char field[MAX_H][MAX_W] = {'X'};
int d[MAX_H][MAX_W] = {INF};
typedef pair<int, int> P;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
queue<P> que;
P start;
int pow = 1;
while (std::cin >> H >> W >> N, H) {
for (int i = 0; i < H; i++) {
std::cin >> field[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
if (field[i][j] == 'S') {
start = P(i, j);
}
}
}
que.push(start);
d[start.first][start.second] = 0;
int count = 0;
while (que.size()) {
P p = que.front();
que.pop();
if (field[p.first][p.second] == '0' + N and N <= pow) {
count = d[p.first][p.second];
break;
}
for (int i = 0; i < 4; i++) {
int nx = p.second + dx[i];
int ny = p.first + dy[i];
if (nx >= 0 and ny >= 0 and nx < W and ny < H and
field[ny][nx] != 'X' and d[ny][nx] == INF) {
if (field[ny][nx] == '.') {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
} else {
if (field[ny][nx] <= '0' + pow) {
// cout << "pow:" << pow << ", cheese:" << field[ny][nx] << endl;
int s = d[p.first][p.second];
field[ny][nx] = '.';
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
while (que.size()) {
que.pop();
}
que.push(P(ny, nx));
d[ny][nx] = s + 1;
if (pow == N) {
field[ny][nx] = '0' + N;
}
pow++;
break;
} else {
que.push(P(ny, nx));
d[ny][nx] = d[p.first][p.second] + 1;
}
}
}
}
}
std::cout << count << std::endl;
break;
}
return 0;
} | replace | 8 | 10 | 8 | 10 | 0 | |
p00481 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long
using namespace std;
typedef struct info {
int nx;
int ny;
int cost;
int target;
info(int a, int b, int c, int d) {
nx = a;
ny = b;
cost = c;
target = d;
}
} info;
bool isNumChar(char c) {
if ((0 <= c - 48) && (c - 48 <= 9))
return true;
return false;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int W, H;
int N;
int sx[16] = {0};
int sy[16] = {0};
char field[1024][1024] = {""};
int record[1024][1024][12] = {0};
bool flag[16] = {false};
queue<info> que;
int ans;
int recordReset() {
for (int i = 0; i < 1024; i++) {
for (int j = 0; j < 1024; j++) {
for (int k = 0; k < 16; k++) {
record[i][j][k] = INF;
}
}
}
}
int bfs() {
info now(0, 0, 0, 0);
info next(0, 0, 0, 0);
for (int i = 0; i < N; i++) {
next = info(sx[i], sy[i], 0, i + 1);
que.push(next);
}
recordReset();
while (!que.empty()) {
now = que.front();
que.pop();
if (!flag[now.target]) {
// printf("###(%d,%d):::cost<%d>:::target<%d>\n",now.nx,now.ny,now.cost,now.target);
if (record[now.nx][now.ny][now.target] > now.cost) {
record[now.nx][now.ny][now.target] = now.cost;
if ((sx[now.target] == now.nx) && (sy[now.target] == now.ny)) {
flag[now.target] = true;
// printf("###target<%d> completed\n",now.target);
ans += record[now.nx][now.ny][now.target];
} else {
switch (field[now.nx][now.ny]) {
case 'X':
break;
default:
for (int i = 0; i < 4; i++) {
next = info(now.nx + dx[i], now.ny + dy[i], now.cost + 1,
now.target);
que.push(next);
}
break;
}
}
}
}
}
}
int main(void) {
// while(true){
scanf("%d%d", &H, &W);
scanf("%d", &N);
// if((W == 0)&&(H == 0))break;
ans = 0;
// Reset Field
for (int i = 0; i < 1024; i++) {
for (int j = 0; j < 1024; j++) {
field[j][i] = 'X';
}
}
// Reset Flag
for (int i = 0; i < 16; i++) {
flag[i] = false;
}
// Reading Data
for (int i = 1; i <= H; i++) {
scanf(" ");
for (int j = 1; j <= W; j++) {
scanf("%c", &field[j][i]);
if (isNumChar(field[j][i])) {
sx[field[j][i] - 48] = j;
sy[field[j][i] - 48] = i;
// printf("###<%d>(%d,%d)\n",field[j][i] - 48,sx[field[j][i] -
// 48],sy[field[j][i] - 48]);
} else if (field[j][i] == 'S') {
sx[0] = j;
sy[0] = i;
// printf("###<S>(%d,%d)\n",j,i);
}
}
}
// DBG
/*for(int i = 0;i < N;i++){
printf("<%d>(%d,%d)\n",i,sx[i],sy[i]);
}*/
bfs();
printf("%d\n", ans);
//}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long
using namespace std;
typedef struct info {
int nx;
int ny;
int cost;
int target;
info(int a, int b, int c, int d) {
nx = a;
ny = b;
cost = c;
target = d;
}
} info;
bool isNumChar(char c) {
if ((0 <= c - 48) && (c - 48 <= 9))
return true;
return false;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int W, H;
int N;
int sx[16] = {0};
int sy[16] = {0};
char field[1024][1024] = {""};
int record[1024][1024][12] = {0};
bool flag[16] = {false};
queue<info> que;
int ans;
int recordReset() {
for (int i = 0; i < 1024; i++) {
for (int j = 0; j < 1024; j++) {
for (int k = 0; k < 12; k++) {
record[i][j][k] = INF;
}
}
}
}
int bfs() {
info now(0, 0, 0, 0);
info next(0, 0, 0, 0);
for (int i = 0; i < N; i++) {
next = info(sx[i], sy[i], 0, i + 1);
que.push(next);
}
recordReset();
while (!que.empty()) {
now = que.front();
que.pop();
if (!flag[now.target]) {
// printf("###(%d,%d):::cost<%d>:::target<%d>\n",now.nx,now.ny,now.cost,now.target);
if (record[now.nx][now.ny][now.target] > now.cost) {
record[now.nx][now.ny][now.target] = now.cost;
if ((sx[now.target] == now.nx) && (sy[now.target] == now.ny)) {
flag[now.target] = true;
// printf("###target<%d> completed\n",now.target);
ans += record[now.nx][now.ny][now.target];
} else {
switch (field[now.nx][now.ny]) {
case 'X':
break;
default:
for (int i = 0; i < 4; i++) {
next = info(now.nx + dx[i], now.ny + dy[i], now.cost + 1,
now.target);
que.push(next);
}
break;
}
}
}
}
}
}
int main(void) {
// while(true){
scanf("%d%d", &H, &W);
scanf("%d", &N);
// if((W == 0)&&(H == 0))break;
ans = 0;
// Reset Field
for (int i = 0; i < 1024; i++) {
for (int j = 0; j < 1024; j++) {
field[j][i] = 'X';
}
}
// Reset Flag
for (int i = 0; i < 16; i++) {
flag[i] = false;
}
// Reading Data
for (int i = 1; i <= H; i++) {
scanf(" ");
for (int j = 1; j <= W; j++) {
scanf("%c", &field[j][i]);
if (isNumChar(field[j][i])) {
sx[field[j][i] - 48] = j;
sy[field[j][i] - 48] = i;
// printf("###<%d>(%d,%d)\n",field[j][i] - 48,sx[field[j][i] -
// 48],sy[field[j][i] - 48]);
} else if (field[j][i] == 'S') {
sx[0] = j;
sy[0] = i;
// printf("###<S>(%d,%d)\n",j,i);
}
}
}
// DBG
/*for(int i = 0;i < N;i++){
printf("<%d>(%d,%d)\n",i,sx[i],sy[i]);
}*/
bfs();
printf("%d\n", ans);
//}
return 0;
} | replace | 51 | 52 | 51 | 52 | 0 | |
p00481 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1000][1000 + 1];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1000][1000 + 1];
int BFS(int u) {
queue<P> que;
ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
que.push(P(gx[u], gy[u]));
FAR[gx[u]][gy[u]] = 0;
while (que.size()) {
P p = que.front();
que.pop();
if (p.first == gx[u + 1] && p.second == gy[u + 1])
break;
ROP(i, 4) {
int nx = p.first + DX[i], ny = p.second + DY[i];
if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
FAR[nx][ny] == INF) {
que.push(P(nx, ny));
FAR[nx][ny] = FAR[p.first][p.second] + 1;
}
}
}
return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
int res = 0;
ROP(i, G) { res += BFS(i); }
printf("%d\n", res);
}
int main() {
scanf("%d %d %d", &N, &M, &G);
ROPP(i, N) {
ROP(j, M + 1) {
scanf("%c", &MAP[j][i]);
if (MAP[j][i] == 'S')
gx[0] = j, gy[0] = i;
ROPP(k, G) {
char l = '0' + k;
if (MAP[j][i] == l)
gx[k] = j, gy[k] = i;
}
}
}
solve();
return 0;
} | #include <cstdio>
#include <iostream>
#include <queue>
#define ROP(i, n) for (int i = 0; i < n; i++)
#define ROPP(i, n) for (int i = 1; i <= n; i++)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
int N, M, G, gx[10], gy[10];
char MAP[1005][1005];
int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}, FAR[1005][1005];
int BFS(int u) {
queue<P> que;
ROPP(i, N) { ROPP(j, M) FAR[j][i] = INF; }
que.push(P(gx[u], gy[u]));
FAR[gx[u]][gy[u]] = 0;
while (que.size()) {
P p = que.front();
que.pop();
if (p.first == gx[u + 1] && p.second == gy[u + 1])
break;
ROP(i, 4) {
int nx = p.first + DX[i], ny = p.second + DY[i];
if (1 <= nx && nx <= M && 1 <= ny && ny <= N && MAP[nx][ny] != 'X' &&
FAR[nx][ny] == INF) {
que.push(P(nx, ny));
FAR[nx][ny] = FAR[p.first][p.second] + 1;
}
}
}
return FAR[gx[u + 1]][gy[u + 1]];
}
void solve() {
int res = 0;
ROP(i, G) { res += BFS(i); }
printf("%d\n", res);
}
int main() {
scanf("%d %d %d", &N, &M, &G);
ROPP(i, N) {
ROP(j, M + 1) {
scanf("%c", &MAP[j][i]);
if (MAP[j][i] == 'S')
gx[0] = j, gy[0] = i;
ROPP(k, G) {
char l = '0' + k;
if (MAP[j][i] == l)
gx[k] = j, gy[k] = i;
}
}
}
solve();
return 0;
} | replace | 9 | 11 | 9 | 11 | 0 | |
p00481 | C++ | Runtime Error | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW][MAX_HW][2];
int town[MAX_HW][MAX_HW];
char town2[MAX_HW][MAX_HW];
queue<P> que;
P p;
void bfs(int sy, int sx) {
que.push(P(sy, sx));
flag[sy][sx][0] = rat;
while (que.size()) {
RE:
p = que.front();
que.pop();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 || j == 0) {
if (town2[p.first + i][p.second + j] != 'X' &&
flag[p.first + i][p.second + j][0] != rat && p.first + i < h &&
p.first + i >= 0 && p.second + j < w && p.second + j >= 0) {
flag[p.first + i][p.second + j][0] = rat;
flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
if (town[p.first + i][p.second + j] == rat) {
if (rat == n)
return;
rat++;
flag[p.first + i][p.second + j][0] = rat;
while (que.size()) {
que.pop();
}
que.push(P(p.first + i, p.second + j));
goto RE;
} else {
que.push(P(p.first + i, p.second + j));
}
}
}
}
}
}
}
int main() {
cin >> h >> w >> n;
rat = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> town2[i][j];
town[i][j] = town2[i][j] - 48;
if (town2[i][j] == 'S') {
s[0] = i;
s[1] = j;
}
if (town[i][j] == n) {
g[0] = i;
g[1] = j;
}
}
}
bfs(s[0], s[1]);
cout << flag[g[0]][g[1]][1] << endl;
return 0;
} | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW + 1][MAX_HW + 1][2];
int town[MAX_HW + 1][MAX_HW + 1];
char town2[MAX_HW + 1][MAX_HW + 1];
queue<P> que;
P p;
void bfs(int sy, int sx) {
que.push(P(sy, sx));
flag[sy][sx][0] = rat;
while (que.size()) {
RE:
p = que.front();
que.pop();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 || j == 0) {
if (town2[p.first + i][p.second + j] != 'X' &&
flag[p.first + i][p.second + j][0] != rat && p.first + i < h &&
p.first + i >= 0 && p.second + j < w && p.second + j >= 0) {
flag[p.first + i][p.second + j][0] = rat;
flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
if (town[p.first + i][p.second + j] == rat) {
if (rat == n)
return;
rat++;
flag[p.first + i][p.second + j][0] = rat;
while (que.size()) {
que.pop();
}
que.push(P(p.first + i, p.second + j));
goto RE;
} else {
que.push(P(p.first + i, p.second + j));
}
}
}
}
}
}
}
int main() {
cin >> h >> w >> n;
rat = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> town2[i][j];
town[i][j] = town2[i][j] - 48;
if (town2[i][j] == 'S') {
s[0] = i;
s[1] = j;
}
if (town[i][j] == n) {
g[0] = i;
g[1] = j;
}
}
}
bfs(s[0], s[1]);
cout << flag[g[0]][g[1]][1] << endl;
return 0;
} | replace | 5 | 8 | 5 | 8 | 0 | |
p00481 | C++ | Runtime Error | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW][MAX_HW][2];
int town[MAX_HW][MAX_HW];
char town2[MAX_HW][MAX_HW];
queue<P> que;
P p;
void bfs(int sy, int sx) {
que.push(P(sy, sx));
flag[sy][sx][0] = rat;
while (que.size()) {
RE:
p = que.front();
que.pop();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 || j == 0) {
if (town2[p.first + i][p.second + j] != 'X' &&
flag[p.first + i][p.second + j][0] != rat && p.first + i < h &&
p.first + i >= 0 && p.second + j < w && p.second + j >= 0) {
flag[p.first + i][p.second + j][0] = rat;
flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
if (town[p.first + i][p.second + j] == rat) {
if (rat == n)
return;
rat++;
flag[p.first + i][p.second + j][0] = rat;
while (que.size()) {
que.pop();
}
que.push(P(p.first + i, p.second + j));
goto RE;
} else {
que.push(P(p.first + i, p.second + j));
}
}
}
}
}
}
}
int main() {
cin >> h >> w >> n;
rat = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> town2[i][j];
town[i][j] = town2[i][j] - 48;
if (town2[i][j] == 'S') {
s[0] = i;
s[1] = j;
}
if (town[i][j] == n) {
g[0] = i;
g[1] = j;
}
}
}
bfs(s[0], s[1]);
cout << flag[g[0]][g[1]][1] << endl;
return 0;
} | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int MAX_HW = 1000;
int n, h, w, rat, s[2], g[2], flag[MAX_HW + 1][MAX_HW + 1][2];
int town[MAX_HW][MAX_HW];
char town2[MAX_HW][MAX_HW];
queue<P> que;
P p;
void bfs(int sy, int sx) {
que.push(P(sy, sx));
flag[sy][sx][0] = rat;
while (que.size()) {
RE:
p = que.front();
que.pop();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 || j == 0) {
if (town2[p.first + i][p.second + j] != 'X' &&
flag[p.first + i][p.second + j][0] != rat && p.first + i < h &&
p.first + i >= 0 && p.second + j < w && p.second + j >= 0) {
flag[p.first + i][p.second + j][0] = rat;
flag[p.first + i][p.second + j][1] = flag[p.first][p.second][1] + 1;
if (town[p.first + i][p.second + j] == rat) {
if (rat == n)
return;
rat++;
flag[p.first + i][p.second + j][0] = rat;
while (que.size()) {
que.pop();
}
que.push(P(p.first + i, p.second + j));
goto RE;
} else {
que.push(P(p.first + i, p.second + j));
}
}
}
}
}
}
}
int main() {
cin >> h >> w >> n;
rat = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> town2[i][j];
town[i][j] = town2[i][j] - 48;
if (town2[i][j] == 'S') {
s[0] = i;
s[1] = j;
}
if (town[i][j] == n) {
g[0] = i;
g[1] = j;
}
}
}
bfs(s[0], s[1]);
cout << flag[g[0]][g[1]][1] << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00481 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
// #include <functional>
#include <cstdio>
#include <fstream>
#include <queue>
using namespace std;
#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'
struct Point {
int x;
int y;
};
int dx[] = {0, -1, 0, 1}; // ã©çvñè
int dy[] = {1, 0, -1, 0};
string field[1000];
int mfield[1001][1001] = {};
int life = 1;
int csum = 0;
int H, W, N;
int open(Point gool) {
int counter = 0;
queue<Point> tp;
tp.push(gool);
int i, j;
Point inp;
Point p;
while (1) {
counter++;
int size = tp.size();
for (i = 0; i < size; i++) {
p = tp.front();
tp.pop();
for (j = 0; j < 4; j++) {
inp.x = p.x + dx[j];
inp.y = p.y + dy[j];
if (inp.x < 0 || inp.y < 0 || inp.y >= H || inp.x >= W)
continue;
if (field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life) {
// v·éÉÅZ£Ú®Â\][
if (field[inp.y][inp.x] ==
life + '0') // ©ªÉ©Á½`[YÌ©
{
return counter;
} else {
tp.push(inp);
mfield[inp.y][inp.x] = life;
}
}
}
}
/*for(i=0;i<H;i++)
{
for(j=0;j<W;j++)
{
cout << mfield[i][j];
}
cout << endl;
}*/
}
// copy(tp.begin() , tp.end() , tp.begin());
return 0;
}
int main(void) {
int i, j;
queue<Point> tp;
Point top[10];
Point inp;
int count = 0;
int counter = 0;
cin >> H >> W >> N;
for (i = 0; i < H; i++) // üÍ
{
cin >> field[i];
for (j = 0; j < W; j++) {
if (field[i][j] == 'S') {
top[0].y = i;
top[0].x = j;
mfield[j][i] = life;
}
if (field[i][j] >= '0' && field[i][j] <= '9') {
top[field[i][j] - '0'].y = i;
top[field[i][j] - '0'].x = j;
}
}
} // üÍIíè
while (1) {
if ((counter = open(top[life - 1])) != 0) {
life++;
csum += counter;
}
if (life > N)
break;
}
cout << csum << endl;
return 0;
} | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
// #include <functional>
#include <cstdio>
#include <fstream>
#include <queue>
using namespace std;
#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'
struct Point {
int x;
int y;
};
int dx[] = {0, -1, 0, 1}; // ã©çvñè
int dy[] = {1, 0, -1, 0};
string field[1000];
int mfield[1001][1001] = {};
int life = 1;
int csum = 0;
int H, W, N;
int open(Point gool) {
int counter = 0;
queue<Point> tp;
tp.push(gool);
int i, j;
Point inp;
Point p;
while (1) {
counter++;
int size = tp.size();
for (i = 0; i < size; i++) {
p = tp.front();
tp.pop();
for (j = 0; j < 4; j++) {
inp.x = p.x + dx[j];
inp.y = p.y + dy[j];
if (inp.x < 0 || inp.y < 0 || inp.y >= H || inp.x >= W)
continue;
if (field[inp.y][inp.x] != CANTGO && mfield[inp.y][inp.x] != life) {
// v·éÉÅZ£Ú®Â\][
if (field[inp.y][inp.x] ==
life + '0') // ©ªÉ©Á½`[YÌ©
{
return counter;
} else {
tp.push(inp);
mfield[inp.y][inp.x] = life;
}
}
}
}
/*for(i=0;i<H;i++)
{
for(j=0;j<W;j++)
{
cout << mfield[i][j];
}
cout << endl;
}*/
}
// copy(tp.begin() , tp.end() , tp.begin());
return 0;
}
int main(void) {
int i, j;
queue<Point> tp;
Point top[10];
Point inp;
int count = 0;
int counter = 0;
cin >> H >> W >> N;
for (i = 0; i < H; i++) // üÍ
{
cin >> field[i];
for (j = 0; j < W; j++) {
if (field[i][j] == 'S') {
top[0].y = i;
top[0].x = j;
mfield[i][j] = life;
}
if (field[i][j] >= '0' && field[i][j] <= '9') {
top[field[i][j] - '0'].y = i;
top[field[i][j] - '0'].x = j;
}
}
} // üÍIíè
while (1) {
if ((counter = open(top[life - 1])) != 0) {
life++;
csum += counter;
}
if (life > N)
break;
}
cout << csum << endl;
return 0;
} | replace | 88 | 89 | 88 | 89 | TLE | |
p00481 | C++ | Memory Limit Exceeded | #include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
cin >> h >> w >> k;
rep(i, 0, h) rep(j, 0, w) {
t[i][j] = true;
char z;
cin >> z;
if (z == 'X')
t[i][j] = 0;
if (z == 'S')
p[0] = make_pair(i, j);
if (z > '0' && z <= '9')
p[z - '0'] = make_pair(i, j);
}
int a = 0;
rep(r, 0, k) {
Q.push(make_pair(0, p[r]));
rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
vis[p[r].x][p[r].y] = 1;
while (1) {
int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
if (Y == p[r + 1].x && X == p[r + 1].y) {
a += L;
break;
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
rep(i, 0, 4) {
int f = Y + dy[i], g = X + dx[i];
if (f >= 0 && f < h && g >= 0 && g < w) {
if (t[f][g] == 1 && vis[f][g] == 0) {
vis[Y][X] = 1;
Q.push(make_pair(L + 1, make_pair(f, g)));
}
}
}
Q.pop();
}
while (!Q.empty())
Q.pop();
}
cout << a << endl;
return 0;
} | #include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
cin >> h >> w >> k;
rep(i, 0, h) rep(j, 0, w) {
t[i][j] = true;
char z;
cin >> z;
if (z == 'X')
t[i][j] = 0;
if (z == 'S')
p[0] = make_pair(i, j);
if (z > '0' && z <= '9')
p[z - '0'] = make_pair(i, j);
}
int a = 0;
rep(r, 0, k) {
Q.push(make_pair(0, p[r]));
rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
vis[p[r].x][p[r].y] = 1;
while (1) {
int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
if (Y == p[r + 1].x && X == p[r + 1].y) {
a += L;
break;
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
rep(i, 0, 4) {
int f = Y + dy[i], g = X + dx[i];
if (f >= 0 && f < h && g >= 0 && g < w) {
if (t[f][g] == 1 && vis[f][g] == 0) {
vis[f][g] = 1;
Q.push(make_pair(L + 1, make_pair(f, g)));
}
}
}
Q.pop();
}
while (!Q.empty())
Q.pop();
}
cout << a << endl;
return 0;
} | replace | 39 | 40 | 39 | 40 | MLE | |
p00481 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> tup;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
// int tansaku[110];
// map<string,string> d;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
bool visited[1010][1010];
char field[1010][1010];
queue<tup> kuwa;
int H, W, N;
// CLR(visited);
REP(i, W) { REP(j, H) visited[i][j] = false; }
cin >> H >> W >> N;
REP(j, H) {
REP(i, W) { cin >> field[i][j]; }
}
tup start;
cin.ignore();
REP(i, W) {
REP(j, H) {
if (field[i][j] == 'S') {
start = {0, {i, j}};
}
}
}
kuwa.push(start);
int ans = 0;
int cost = 0;
char cheeze = '1';
while (!kuwa.empty()) {
tup r = kuwa.front();
kuwa.pop();
int haba = r.first;
PII p = r.second;
visited[p.first][p.second] = true;
if (field[p.first][p.second] == cheeze) {
cost += haba;
if (cheeze - '0' == N)
break;
cheeze++;
queue<tup> daisuke;
swap(kuwa, daisuke);
kuwa.push(make_pair(0, p));
// CLR(visited);
REP(i, W) { REP(j, H) visited[i][j] = false; }
continue;
}
for (int i = 0; i < 4; i++) {
PII new_xy = {p.first + dx[i], p.second + dy[i]};
if (0 <= new_xy.first && new_xy.first < W && 0 <= new_xy.second &&
new_xy.second < H && !visited[new_xy.first][new_xy.second] &&
field[new_xy.first][new_xy.second] != 'X') {
kuwa.push(make_pair(haba + 1, new_xy));
}
}
}
cout << cost << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> tup;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))
// int tansaku[110];
// map<string,string> d;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
bool visited[1010][1010];
char field[1010][1010];
queue<tup> kuwa;
int H, W, N;
// CLR(visited);
REP(i, W) { REP(j, H) visited[i][j] = false; }
cin >> H >> W >> N;
REP(j, H) {
REP(i, W) { cin >> field[i][j]; }
}
tup start;
cin.ignore();
REP(i, W) {
REP(j, H) {
if (field[i][j] == 'S') {
start = {0, {i, j}};
}
}
}
kuwa.push(start);
int ans = 0;
int cost = 0;
char cheeze = '1';
while (!kuwa.empty()) {
tup r = kuwa.front();
kuwa.pop();
int haba = r.first;
PII p = r.second;
visited[p.first][p.second] = true;
if (field[p.first][p.second] == cheeze) {
cost += haba;
if (cheeze - '0' == N)
break;
cheeze++;
queue<tup> daisuke;
swap(kuwa, daisuke);
kuwa.push(make_pair(0, p));
// CLR(visited);
REP(i, W) { REP(j, H) visited[i][j] = false; }
continue;
}
for (int i = 0; i < 4; i++) {
PII new_xy = {p.first + dx[i], p.second + dy[i]};
if (0 <= new_xy.first && new_xy.first < W && 0 <= new_xy.second &&
new_xy.second < H && !visited[new_xy.first][new_xy.second] &&
field[new_xy.first][new_xy.second] != 'X') {
kuwa.push(make_pair(haba + 1, new_xy));
visited[new_xy.first][new_xy.second] = true;
}
}
}
cout << cost << endl;
return 0;
} | insert | 75 | 75 | 75 | 76 | 0 | |
p00481 | C++ | Memory Limit Exceeded | #include <iostream>
#include <map>
#include <queue>
#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second
using namespace std;
char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;
int main() {
int h, w, n;
cin >> h >> w >> n;
int sx, sy;
rep(i, 0, h) {
rep(j, 0, w) {
cin >> m[j][i];
if (m[j][i] == 'S') {
sx = j;
sy = i;
}
}
}
pair<int, int> p;
p.ft = sx;
p.sd = sy;
rep(i, 0, h) {
rep(j, 0, w) { dis[j][i] = -1; }
}
dis[sx][sy] = 0;
state.push(p);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int map2[1000][1000];
for (int lp = 1; lp <= n; lp++) {
rep(i, 0, h) {
rep(j, 0, w) { map2[j][i] = 0; }
}
map2[sx][sy] = 1;
while (state.size()) {
pair<int, int> now = state.front();
state.pop();
if (m[now.ft][now.sd] - '0' == lp) {
sx = now.ft;
sy = now.sd;
break;
}
rep(lp2, 0, 4) {
int x = now.ft + dx[lp2];
int y = now.sd + dy[lp2];
if (x < 0 || w <= x || y < 0 || h <= y)
continue;
if (map2[x][y] == 0 && m[x][y] != 'X') {
state.push(make_pair(x, y));
dis[x][y] = dis[now.ft][now.sd] + 1;
map2[now.ft][now.sd] = 1;
}
}
}
while (state.size())
state.pop();
state.push(make_pair(sx, sy));
}
cout << dis[sx][sy] << endl;
return 0;
} | #include <iostream>
#include <map>
#include <queue>
#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second
using namespace std;
char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;
int main() {
int h, w, n;
cin >> h >> w >> n;
int sx, sy;
rep(i, 0, h) {
rep(j, 0, w) {
cin >> m[j][i];
if (m[j][i] == 'S') {
sx = j;
sy = i;
}
}
}
pair<int, int> p;
p.ft = sx;
p.sd = sy;
rep(i, 0, h) {
rep(j, 0, w) { dis[j][i] = -1; }
}
dis[sx][sy] = 0;
state.push(p);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int map2[1000][1000];
for (int lp = 1; lp <= n; lp++) {
rep(i, 0, h) {
rep(j, 0, w) { map2[j][i] = 0; }
}
map2[sx][sy] = 1;
while (state.size()) {
pair<int, int> now = state.front();
state.pop();
if (m[now.ft][now.sd] - '0' == lp) {
sx = now.ft;
sy = now.sd;
break;
}
rep(lp2, 0, 4) {
int x = now.ft + dx[lp2];
int y = now.sd + dy[lp2];
if (x < 0 || w <= x || y < 0 || h <= y)
continue;
if (map2[x][y] == 0 && m[x][y] != 'X') {
state.push(make_pair(x, y));
dis[x][y] = dis[now.ft][now.sd] + 1;
map2[x][y] = 1;
}
}
}
while (state.size())
state.pop();
state.push(make_pair(sx, sy));
}
cout << dis[sx][sy] << endl;
return 0;
} | replace | 67 | 68 | 67 | 68 | MLE | |
p00481 | C++ | Runtime Error | #include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int main() {
int h, w, n, px, py, ans;
int field[100][100];
int ch[100][100];
while (cin >> h >> w >> n) {
ans = 0;
string s;
for (int i = 0; i < h; i++) {
cin >> s;
for (int j = 0; j < w; j++) {
if (s[j] == 'S') {
px = j;
py = i;
field[j][i] = 0;
} else if (s[j] >= '1' && s[j] <= '9') {
field[j][i] = s[j] - '0';
} else if (s[j] == 'X') {
field[j][i] = -1;
} else {
field[j][i] = 0;
}
}
}
for (int i = 0; i < n; i++) {
queue<pair<int, int>> play;
play.push(make_pair(px, py));
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
ch[k][j] = 0;
}
}
ch[px][py] = 1;
for (;;) {
int x = play.front().first;
int y = play.front().second;
if (field[x][y] == i + 1) {
ans += ch[x][y] - 1;
px = x;
py = y;
break;
}
if (x > 0) {
if (field[x - 1][y] != -1) {
if (ch[x - 1][y] == 0) {
play.push(make_pair(x - 1, y));
ch[x - 1][y] = ch[x][y] + 1;
}
}
}
if (y > 0) {
if (field[x][y - 1] != -1) {
if (ch[x][y - 1] == 0) {
play.push(make_pair(x, y - 1));
ch[x][y - 1] = ch[x][y] + 1;
}
}
}
if (x < w - 1) {
if (field[x + 1][y] != -1) {
if (ch[x + 1][y] == 0) {
play.push(make_pair(x + 1, y));
ch[x + 1][y] = ch[x][y] + 1;
}
}
}
if (y < h - 1) {
if (field[x][y + 1] != -1) {
if (ch[x][y + 1] == 0) {
play.push(make_pair(x, y + 1));
ch[x][y + 1] = ch[x][y] + 1;
}
}
}
play.pop();
}
}
cout << ans << endl;
}
return 0;
} | #include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int main() {
int h, w, n, px, py, ans;
int field[1000][1000];
int ch[1000][1000];
while (cin >> h >> w >> n) {
ans = 0;
string s;
for (int i = 0; i < h; i++) {
cin >> s;
for (int j = 0; j < w; j++) {
if (s[j] == 'S') {
px = j;
py = i;
field[j][i] = 0;
} else if (s[j] >= '1' && s[j] <= '9') {
field[j][i] = s[j] - '0';
} else if (s[j] == 'X') {
field[j][i] = -1;
} else {
field[j][i] = 0;
}
}
}
for (int i = 0; i < n; i++) {
queue<pair<int, int>> play;
play.push(make_pair(px, py));
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
ch[k][j] = 0;
}
}
ch[px][py] = 1;
for (;;) {
int x = play.front().first;
int y = play.front().second;
if (field[x][y] == i + 1) {
ans += ch[x][y] - 1;
px = x;
py = y;
break;
}
if (x > 0) {
if (field[x - 1][y] != -1) {
if (ch[x - 1][y] == 0) {
play.push(make_pair(x - 1, y));
ch[x - 1][y] = ch[x][y] + 1;
}
}
}
if (y > 0) {
if (field[x][y - 1] != -1) {
if (ch[x][y - 1] == 0) {
play.push(make_pair(x, y - 1));
ch[x][y - 1] = ch[x][y] + 1;
}
}
}
if (x < w - 1) {
if (field[x + 1][y] != -1) {
if (ch[x + 1][y] == 0) {
play.push(make_pair(x + 1, y));
ch[x + 1][y] = ch[x][y] + 1;
}
}
}
if (y < h - 1) {
if (field[x][y + 1] != -1) {
if (ch[x][y + 1] == 0) {
play.push(make_pair(x, y + 1));
ch[x][y + 1] = ch[x][y] + 1;
}
}
}
play.pop();
}
}
cout << ans << endl;
}
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p00481 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#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>
typedef long long ll;
using namespace std;
#define INF 1000000
int H, W, N;
int sx, sy;
int res = 0;
int d[1100][1100];
char maze[1100][1100];
pair<int, int> cheese[10];
void bfs(int HP) {
queue<pair<int, int>> que;
sx = cheese[HP - 1].first, sy = cheese[HP - 1].second;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
d[i][j] = INF;
}
}
que.push(pair<int, int>(sx, sy));
d[sx][sy] = 0;
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
if ((maze[p.first][p.second] - '0') == HP)
break;
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy) {
if (abs(dx - dy) != 1)
continue;
int nx = p.first + dx, ny = p.second + dy;
if (0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(pair<int, int>(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
if (HP == N) {
res += d[cheese[HP].first][cheese[HP].second];
return;
}
res += d[cheese[HP].first][cheese[HP].second];
bfs(HP + 1);
return;
}
int main(void) {
cin >> H >> W >> N;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> maze[i][j];
if (maze[i][j] - '0' >= 1 && maze[i][j] - '0' <= N) {
cheese[maze[i][j] - '0'] = pair<int, int>(i, j);
} else if (maze[i][j] == 'S') {
cheese[0] = pair<int, int>(i, j);
}
}
}
bfs(1);
int r = res;
cout << r << endl;
while (1)
;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#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>
typedef long long ll;
using namespace std;
#define INF 1000000
int H, W, N;
int sx, sy;
int res = 0;
int d[1100][1100];
char maze[1100][1100];
pair<int, int> cheese[10];
void bfs(int HP) {
queue<pair<int, int>> que;
sx = cheese[HP - 1].first, sy = cheese[HP - 1].second;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
d[i][j] = INF;
}
}
que.push(pair<int, int>(sx, sy));
d[sx][sy] = 0;
while (que.size()) {
pair<int, int> p = que.front();
que.pop();
if ((maze[p.first][p.second] - '0') == HP)
break;
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy) {
if (abs(dx - dy) != 1)
continue;
int nx = p.first + dx, ny = p.second + dy;
if (0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'X' &&
d[nx][ny] == INF) {
que.push(pair<int, int>(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
if (HP == N) {
res += d[cheese[HP].first][cheese[HP].second];
return;
}
res += d[cheese[HP].first][cheese[HP].second];
bfs(HP + 1);
return;
}
int main(void) {
cin >> H >> W >> N;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> maze[i][j];
if (maze[i][j] - '0' >= 1 && maze[i][j] - '0' <= N) {
cheese[maze[i][j] - '0'] = pair<int, int>(i, j);
} else if (maze[i][j] == 'S') {
cheese[0] = pair<int, int>(i, j);
}
}
}
bfs(1);
int r = res;
cout << r << endl;
return 0;
} | delete | 94 | 96 | 94 | 94 | TLE | |
p00481 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
typedef std::pair<int, int> T;
const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];
void bfs(T _goal, T _start) {
std::queue<T> q;
q.push(_start);
while (q.size()) {
T tmp = q.front();
q.pop();
if (tmp == _goal) {
ans += dis[_goal.first][_goal.second];
break;
}
int x = tmp.first;
int y = tmp.second;
for (int i = 0; i < 4; i++) {
if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w)
continue;
if (filed[x + vecx[i]][y + vecy[i]] != 'X') {
dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
q.push(std::make_pair(x + vecx[i], y + vecy[i]));
}
}
}
}
int main() {
std::cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
std::cin >> filed[i][j];
if (filed[i][j] == 'S') {
goal[0].first = i;
goal[0].second = j;
}
if (filed[i][j] > '0' && filed[i][j] <= '9') {
goal[filed[i][j] - '0'].first = i;
goal[filed[i][j] - '0'].second = j;
}
}
}
for (int i = 0; i < n; i++) {
bfs(goal[i + 1], goal[i]);
for (int j = 0; j < h; j++)
for (int k = 0; k < w; k++)
dis[j][k] = 0;
}
std::cout << ans << std::endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
typedef std::pair<int, int> T;
const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];
void bfs(T _goal, T _start) {
std::queue<T> q;
q.push(_start);
while (q.size()) {
T tmp = q.front();
q.pop();
if (tmp == _goal) {
ans += dis[_goal.first][_goal.second];
break;
}
int x = tmp.first;
int y = tmp.second;
for (int i = 0; i < 4; i++) {
if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
y + vecy[i] >= w)
continue;
if (filed[x + vecx[i]][y + vecy[i]] != 'X' &&
!dis[x + vecx[i]][y + vecy[i]]) {
dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
q.push(std::make_pair(x + vecx[i], y + vecy[i]));
}
}
}
}
int main() {
std::cin >> h >> w >> n;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
std::cin >> filed[i][j];
if (filed[i][j] == 'S') {
goal[0].first = i;
goal[0].second = j;
}
if (filed[i][j] > '0' && filed[i][j] <= '9') {
goal[filed[i][j] - '0'].first = i;
goal[filed[i][j] - '0'].second = j;
}
}
}
for (int i = 0; i < n; i++) {
bfs(goal[i + 1], goal[i]);
for (int j = 0; j < h; j++)
for (int k = 0; k < w; k++)
dis[j][k] = 0;
}
std::cout << ans << std::endl;
return 0;
} | replace | 32 | 33 | 32 | 34 | 0 | |
p00481 | C++ | Memory Limit Exceeded | #include <iostream>
#include <vector>
std::vector<std::pair<int, int>> next_list(const int &x, const int &y) {
return {std::make_pair(x + 1, y), std::make_pair(x - 1, y),
std::make_pair(x, y + 1), std::make_pair(x, y - 1)};
}
template <typename T> class Queue {
public:
Queue(const int &n) : vector(n), f(0), b(0){};
T front() const { return vector.at(f); }
T back() const { return vector.at(b); }
void pop() { f = (f + 1) % vector.size(); }
void push(T f) {
vector.at(b) = f;
b = (b + 1) % vector.size();
}
void clear() {
f = 0;
b = 0;
}
private:
std::vector<T> vector;
int f, b;
};
struct Route {
Route(const int &ax = 0, const int &ay = 0, const int &an = 0,
const int &atime = 0)
: x(ax), y(ay), n(an), time(atime){};
int x, y, n, time;
bool operator<(const Route &other) const { return time < other.time; }
};
int wfs(const std::vector<std::vector<int>> &vector,
std::vector<std::vector<int>> &memo, Queue<Route> &queue,
const Route &start, const int &goal) {
queue.clear();
queue.push(start);
while (vector.at(queue.front().y).at(queue.front().x) != start.n + 1) {
for (const auto &coord : next_list(queue.front().x, queue.front().y)) {
if (vector.at(coord.second).at(coord.first) >= 0 &&
memo.at(coord.second).at(coord.first) != start.n) {
memo.at(coord.second).at(coord.first) = start.n;
queue.push(Route(coord.first, coord.second, 0, queue.front().time + 1));
}
}
queue.pop();
}
if (start.n + 1 == goal) {
return queue.front().time;
} else {
return wfs(vector, memo, queue,
Route(queue.front().x, queue.front().y, start.n + 1,
queue.front().time),
goal);
}
}
int main() {
int h, w, n;
std::cin >> h >> w >> n;
std::vector<std::vector<int>> vector(h + 2, std::vector<int>(w + 2, -1));
Route start(0, 0, 0, 0);
char *arg = new char[w + 2];
for (auto i = 1; i <= h; ++i) {
std::cin >> arg;
for (auto j = 1; j <= w; ++j) {
switch (arg[j - 1]) {
case '.':
vector.at(i).at(j) = 0;
break;
case 'X':
vector.at(i).at(j) = -1;
break;
case 'S':
vector.at(i).at(j) = 0;
start = Route(j, i, 0, 0);
break;
default:
vector.at(i).at(j) = arg[j - 1] - '0';
}
}
}
delete[] arg;
arg = nullptr;
std::vector<std::vector<int>> memo(h + 2, std::vector<int>(w + 2, -1));
Queue<Route> queue(h * w * 4);
std::cout << wfs(vector, memo, queue, start, n) << std::endl;
return 0;
} | #include <iostream>
#include <vector>
std::vector<std::pair<int, int>> next_list(const int &x, const int &y) {
return {std::make_pair(x + 1, y), std::make_pair(x - 1, y),
std::make_pair(x, y + 1), std::make_pair(x, y - 1)};
}
template <typename T> class Queue {
public:
Queue(const int &n) : vector(n), f(0), b(0){};
T front() const { return vector.at(f); }
T back() const { return vector.at(b); }
void pop() { f = (f + 1) % vector.size(); }
void push(T f) {
vector.at(b) = f;
b = (b + 1) % vector.size();
}
void clear() {
f = 0;
b = 0;
}
private:
std::vector<T> vector;
int f, b;
};
struct Route {
Route(const int &ax = 0, const int &ay = 0, const int &an = 0,
const int &atime = 0)
: x(ax), y(ay), n(an), time(atime){};
int x, y, n, time;
bool operator<(const Route &other) const { return time < other.time; }
};
int wfs(const std::vector<std::vector<int>> &vector,
std::vector<std::vector<int>> &memo, Queue<Route> &queue,
const Route &start, const int &goal) {
queue.clear();
queue.push(start);
while (vector.at(queue.front().y).at(queue.front().x) != start.n + 1) {
for (const auto &coord : next_list(queue.front().x, queue.front().y)) {
if (vector.at(coord.second).at(coord.first) >= 0 &&
memo.at(coord.second).at(coord.first) != start.n) {
memo.at(coord.second).at(coord.first) = start.n;
queue.push(Route(coord.first, coord.second, 0, queue.front().time + 1));
}
}
queue.pop();
}
if (start.n + 1 == goal) {
return queue.front().time;
} else {
return wfs(vector, memo, queue,
Route(queue.front().x, queue.front().y, start.n + 1,
queue.front().time),
goal);
}
}
int main() {
int h, w, n;
std::cin >> h >> w >> n;
std::vector<std::vector<int>> vector(h + 2, std::vector<int>(w + 2, -1));
Route start(0, 0, 0, 0);
char *arg = new char[w + 2];
for (auto i = 1; i <= h; ++i) {
std::cin >> arg;
for (auto j = 1; j <= w; ++j) {
switch (arg[j - 1]) {
case '.':
vector.at(i).at(j) = 0;
break;
case 'X':
vector.at(i).at(j) = -1;
break;
case 'S':
vector.at(i).at(j) = 0;
start = Route(j, i, 0, 0);
break;
default:
vector.at(i).at(j) = arg[j - 1] - '0';
}
}
}
delete[] arg;
arg = nullptr;
std::vector<std::vector<int>> memo(h + 2, std::vector<int>(w + 2, -1));
Queue<Route> queue(h * w);
std::cout << wfs(vector, memo, queue, start, n) << std::endl;
return 0;
} | replace | 84 | 85 | 84 | 85 | MLE | |
p00482 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
static const int MBIT = 695000;
static const int mod = 100000;
short N, M;
short fld[20 * 20];
int dp[2][3][MBIT];
vector<int> BIT;
int ans = 0;
int all = 1;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
for (int i = 0; i <= maxbit; i++) {
bool isbit = false;
for (int j = 0; j < M - 1; ++j) {
int bit1 = 1 << j, bit2 = 1 << (j + 1);
if ((i & bit1) && (i & bit2)) {
isbit = true;
break;
}
}
if (!isbit)
BIT.PB(i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
int tk = BIT[k];
int t1 = tk << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((tk & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 2) {
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
}
dp[p2][j][tk] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
ans += dp[(M * N - 1) & 1][j][BIT[k]];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
static const int MBIT = 700000;
static const int mod = 100000;
short N, M;
short fld[20 * 20];
int dp[2][3][MBIT];
vector<int> BIT;
int ans = 0;
int all = 1;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
for (int i = 0; i <= maxbit; i++) {
bool isbit = false;
for (int j = 0; j < M - 1; ++j) {
int bit1 = 1 << j, bit2 = 1 << (j + 1);
if ((i & bit1) && (i & bit2)) {
isbit = true;
break;
}
}
if (!isbit)
BIT.PB(i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
int tk = BIT[k];
int t1 = tk << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((tk & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 2) {
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
}
dp[p2][j][tk] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
ans += dp[(M * N - 1) & 1][j][BIT[k]];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p00482 | C++ | Time Limit Exceeded | #include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int w, h;
const int MOD = 100000;
int dp[2][1 << 20];
void solve() {
int res = 1;
vector<string> V;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '?')
res *= 3;
res %= MOD;
}
V.push_back(s);
}
if (w == 1 || h == 1) {
cout << res << endl;
return;
}
for (int y = 1; y < h; y++) {
for (int x = 0; x < w - 1; x++) {
if (V[y][x] == 'I' && V[y - 1][x] == 'J' && V[y - 1][x + 1] == 'O') {
cout << res << endl;
return;
}
}
}
if (res == 1) {
cout << 0 << endl;
return;
}
memset(dp, 0, sizeof(dp));
int *before = dp[0];
int *now = dp[1];
// ÅãÌbitÍJÅ é©Ç¤©
// »êÈOÍA©ªªJÅ©ÂEªOÅ é©
memset(before, 0, sizeof(before));
before[0] = 1;
for (int y = 0; y < h; y++) {
memset(now, 0, sizeof(now));
for (int x = 0; x < w; x++) {
for (int bit = 0; bit < 1 << w; bit++) {
int nbit;
nbit = bit & (~1);
nbit = nbit << 1;
nbit = nbit & (~(1 << w));
if (x == 0) {
if (bit & (1 << (w - 1))) {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O;
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O,I
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit];
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
} else {
if (bit & (1 << (w - 1))) {
if (bit & 1) {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit | (1 << 1)] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
} else {
if (bit & 1) {
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit]; // I
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit | (1 << 1)] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O,I
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit];
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
}
}
}
swap(before, now);
for (int i = 0; i < 1 << w; i++) {
now[i] = 0;
before[i] %= MOD;
}
}
}
int ans = 0;
for (int i = 0; i < 1 << w; i++) {
ans += before[i] % MOD;
}
ans %= MOD;
cout << (res - ans) % MOD << endl;
}
int main() {
while (cin >> h >> w) {
solve();
}
return 0;
} | #include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
int w, h;
const int MOD = 100000;
int dp[2][1 << 20];
void solve() {
int res = 1;
vector<string> V;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '?')
res *= 3;
res %= MOD;
}
V.push_back(s);
}
if (w == 1 || h == 1) {
cout << res << endl;
return;
}
for (int y = 1; y < h; y++) {
for (int x = 0; x < w - 1; x++) {
if (V[y][x] == 'I' && V[y - 1][x] == 'J' && V[y - 1][x + 1] == 'O') {
cout << res << endl;
return;
}
}
}
if (res == 1) {
cout << 0 << endl;
return;
}
memset(dp, 0, sizeof(dp));
int *before = dp[0];
int *now = dp[1];
// ÅãÌbitÍJÅ é©Ç¤©
// »êÈOÍA©ªªJÅ©ÂEªOÅ é©
memset(before, 0, sizeof(before));
before[0] = 1;
for (int y = 0; y < h; y++) {
memset(now, 0, sizeof(now));
for (int x = 0; x < w; x++) {
for (int bit = 0; bit < 1 << w; bit++) {
int nbit;
nbit = bit & (~1);
nbit = nbit << 1;
nbit = nbit & (~(1 << w));
if (before[bit] == 0)
continue;
if (x == 0) {
if (bit & (1 << (w - 1))) {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O;
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O,I
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit];
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
} else {
if (bit & (1 << (w - 1))) {
if (bit & 1) {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit | (1 << 1)] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
} else {
if (bit & 1) {
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit]; // I
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit | (1 << 1)] += before[bit]; // O
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
} else {
if (V[y][x] == 'O' || V[y][x] == '?')
now[nbit] += before[bit]; // O,I
if (V[y][x] == 'I' || V[y][x] == '?')
now[nbit] += before[bit];
if (V[y][x] == 'J' || V[y][x] == '?')
now[nbit | 1] += before[bit]; // J
}
}
}
}
swap(before, now);
for (int i = 0; i < 1 << w; i++) {
now[i] = 0;
before[i] %= MOD;
}
}
}
int ans = 0;
for (int i = 0; i < 1 << w; i++) {
ans += before[i] % MOD;
}
ans %= MOD;
cout << (res - ans) % MOD << endl;
}
int main() {
while (cin >> h >> w) {
solve();
}
return 0;
} | insert | 64 | 64 | 64 | 66 | TLE | |
p00482 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
EACH(x, vec) is >> x;
return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
if (i) os << " ";
os << get<0>(t);
}
return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
REP(i, vec.size()) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
REP(i, vec.size()) {
if (i)
os << endl;
os << vec[i];
}
return os;
}
int H, W;
int dp[2][1 << 19][2][2] = {0};
vector<string> m(H);
bool inRange(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
char getM(int x, int y) {
if (!inRange(x, y))
return 0;
switch (m[y][x]) {
case 'J':
return 0;
case 'O':
return 1;
case 'I':
return 2;
}
return -1;
}
bool check(int x, int y, int c) {
if (getM(x, y) < 0)
return true;
return c == getM(x, y);
}
ll mod = 1e5;
int add(ll a, ll b) { return (a + b) % mod; }
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> H >> W;
if (W == 1 || H == 1) {
cout << 0 << endl;
return 0;
}
m.resize(H);
cin >> m;
reverse(ALL(m));
REP(i, m.size()) reverse(ALL(m[i]));
fill(dp[0][0][0], dp[1][(1 << 19) - 1][2], 0);
dp[0][0][0][0] = 1;
REP(i, W * H) {
fill(dp[i + 1 & 1][0][0], dp[i + 1 & 1][(1 << 19) - 1][2], 0);
int x = i % W, y = i / W;
REP(j, (1 << (W - 1))) REP(m, 3) {
if (!check(x, y, m))
continue;
if (x == 0) {
REP(k, 2) REP(l, 2) {
int nj = j;
int nk = m == 1 ? 1 : 0;
dp[i + 1 & 1][nj][nk][l] =
add(dp[i + 1 & 1][nj][nk][l], dp[i & 1][j][k][l]);
}
} else {
REP(k, 2) REP(l, 2) {
int nj = j >> 1;
if (m == 2)
nj |= (1 << (W - 2));
int nk = m == 1 ? 1 : 0;
int nl = l == 1 || (m == 0 && (j & 1) && k == 1) ? 1 : 0;
dp[i + 1 & 1][nj][nk][nl] =
add(dp[i + 1 & 1][nj][nk][nl], dp[i & 1][j][k][l]);
}
}
}
}
ll ans = 0;
REP(i, (1 << (W - 1))) REP(j, 2) { ans = add(ans, dp[(W * H) & 1][i][j][1]); }
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
EACH(x, vec) is >> x;
return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
if (i) os << " ";
os << get<0>(t);
}
return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
REP(i, vec.size()) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
REP(i, vec.size()) {
if (i)
os << endl;
os << vec[i];
}
return os;
}
int H, W;
int dp[2][1 << 19][2][2] = {0};
vector<string> m(H);
bool inRange(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
char getM(int x, int y) {
if (!inRange(x, y))
return 0;
switch (m[y][x]) {
case 'J':
return 0;
case 'O':
return 1;
case 'I':
return 2;
}
return -1;
}
bool check(int x, int y, int c) {
if (getM(x, y) < 0)
return true;
return c == getM(x, y);
}
ll mod = 1e5;
int add(ll a, ll b) { return a + b > mod ? a + b - mod : a + b; }
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> H >> W;
if (W == 1 || H == 1) {
cout << 0 << endl;
return 0;
}
m.resize(H);
cin >> m;
reverse(ALL(m));
REP(i, m.size()) reverse(ALL(m[i]));
fill(dp[0][0][0], dp[1][(1 << 19) - 1][2], 0);
dp[0][0][0][0] = 1;
REP(i, W * H) {
fill(dp[i + 1 & 1][0][0], dp[i + 1 & 1][(1 << 19) - 1][2], 0);
int x = i % W, y = i / W;
REP(j, (1 << (W - 1))) REP(m, 3) {
if (!check(x, y, m))
continue;
if (x == 0) {
REP(k, 2) REP(l, 2) {
int nj = j;
int nk = m == 1 ? 1 : 0;
dp[i + 1 & 1][nj][nk][l] =
add(dp[i + 1 & 1][nj][nk][l], dp[i & 1][j][k][l]);
}
} else {
REP(k, 2) REP(l, 2) {
int nj = j >> 1;
if (m == 2)
nj |= (1 << (W - 2));
int nk = m == 1 ? 1 : 0;
int nl = l == 1 || (m == 0 && (j & 1) && k == 1) ? 1 : 0;
dp[i + 1 & 1][nj][nk][nl] =
add(dp[i + 1 & 1][nj][nk][nl], dp[i & 1][j][k][l]);
}
}
}
}
ll ans = 0;
REP(i, (1 << (W - 1))) REP(j, 2) { ans = add(ans, dp[(W * H) & 1][i][j][1]); }
cout << ans << endl;
} | replace | 77 | 78 | 77 | 78 | TLE | |
p00482 | C++ | Memory Limit Exceeded | #include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 21][2], fill, all = 1;
char c;
int main() {
scanf("%d%d", &m, &n);
fill = (1 << (n - 1)) - 1;
dp[1][0][0] = 1;
for (int i = 0; i < m * n; i++) {
scanf(" %c", &c);
if (c == '?') {
all *= 3;
all %= 100000;
}
memset(dp[i % 2], 0, sizeof(dp[i % 2]));
for (int j = 0; j <= fill; j++) {
int next = (j << 1) & fill;
if (c == 'J' || c == '?') {
dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
}
if (c == 'O' || c == '?') {
dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
}
if (c == 'I' || c == '?') {
if (j >> (n - 2) == 0 || i % n == n - 1) {
dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
}
}
dp[i % 2][next][0] %= 100000;
dp[i % 2][next][1] %= 100000;
dp[i % 2][next + 1][0] %= 100000;
dp[i % 2][next + 1][1] %= 100000;
}
}
for (int i = 0; i <= fill; i++) {
ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
ans %= 100000;
}
printf("%d\n", (100000 + all - ans) % 100000);
return 0;
} | #include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 19][2], fill, all = 1;
char c;
int main() {
scanf("%d%d", &m, &n);
fill = (1 << (n - 1)) - 1;
dp[1][0][0] = 1;
for (int i = 0; i < m * n; i++) {
scanf(" %c", &c);
if (c == '?') {
all *= 3;
all %= 100000;
}
memset(dp[i % 2], 0, sizeof(dp[i % 2]));
for (int j = 0; j <= fill; j++) {
int next = (j << 1) & fill;
if (c == 'J' || c == '?') {
dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
}
if (c == 'O' || c == '?') {
dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
}
if (c == 'I' || c == '?') {
if (j >> (n - 2) == 0 || i % n == n - 1) {
dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
}
}
dp[i % 2][next][0] %= 100000;
dp[i % 2][next][1] %= 100000;
dp[i % 2][next + 1][0] %= 100000;
dp[i % 2][next + 1][1] %= 100000;
}
}
for (int i = 0; i <= fill; i++) {
ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
ans %= 100000;
}
printf("%d\n", (100000 + all - ans) % 100000);
return 0;
} | replace | 3 | 4 | 3 | 4 | MLE | |
p00482 | C++ | Time Limit Exceeded | #include <algorithm>
#include <stdio.h>
using namespace std;
int MOD = 100000;
char str[20][21];
int dp[2][3][1 << 20];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++)
scanf("%s", str[i]);
int q = 0;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
if (str[i][j] == '?')
q++;
if (str[0][0] == 'J')
dp[1][0][0] = 1;
if (str[0][0] == 'O')
dp[1][1][0] = 1;
if (str[0][0] == 'I')
dp[1][2][0] = 1;
if (str[0][0] == '?')
dp[1][0][0] = dp[1][1][0] = dp[1][2][0] = 1;
for (int i = 0; i < a; i++) {
if (i) {
int e = (i * (b - 1) + 1) % 2;
for (int k = 0; k < (1 << (b - 1)); k++) {
if (str[i][0] == 'J') {
dp[e][0][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == 'O') {
dp[e][1][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == 'I') {
if (k % 2 == 0)
dp[e][2][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == '?') {
int v = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
int w = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
if (k % 2 == 0)
dp[e][2][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
dp[e][0][k] = v;
dp[e][1][k] = w;
}
}
// for(int k=0;k<(1<<(b-1));k++)printf("[%d,%d,%d]
// ",dp[e][0][k],dp[e][1][k],dp[e][2][k]); printf("\n");
for (int k = 0; k < (1 << (b - 1)); k++) {
dp[e][0][k] %= MOD;
dp[e][1][k] %= MOD;
dp[e][2][k] %= MOD;
}
}
for (int j = 0; j < b - 1; j++) {
int e = (i * (b - 1) + j) % 2;
for (int k = 0; k < (1 << (b - 1)); k++)
dp[e][0][k] = dp[e][1][k] = dp[e][2][k] = 0;
for (int k = 0; k < (1 << (b - 1)); k++) {
int t = k / 2;
if (str[i][j] == 'J' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k];
} else if (str[i][j] == 'J' && str[i][j + 1] == 'O')
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
else if (str[i][j] == 'J' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k];
} else if (str[i][j] == 'J' && str[i][j + 1] == '?') {
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][0][t] += dp[!e][0][k];
dp[e][2][t] += dp[!e][0][k];
}
else if (str[i][j] == 'O' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][1][k];
dp[e][1][t] += dp[!e][1][k];
dp[e][2][t] += dp[!e][1][k];
} else if (k & 1) {
if (str[i][j] == '?' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k];
} else if (str[i][j] == '?' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][1][t] += dp[!e][1][k];
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k];
}
} else {
if (str[i][j] == '?' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k] + dp[!e][2][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == '?' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][1][t] += dp[!e][1][k] + dp[!e][2][k];
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][2][k];
dp[e][1][t] += dp[!e][2][k];
dp[e][2][t] += dp[!e][2][k];
}
}
}
// for(int k=0;k<(1<<(b-1));k++)printf("[%d,%d,%d]
// ",dp[e][0][k],dp[e][1][k],dp[e][2][k]); printf("\n");
for (int k = 0; k < (1 << (b - 1)); k++) {
dp[e][0][k] %= MOD;
dp[e][1][k] %= MOD;
dp[e][2][k] %= MOD;
}
}
}
int ret = 0;
for (int i = 0; i < (1 << (b - 1)); i++)
ret = (ret + dp[(a * (b - 1) - 1) % 2][0][i] +
dp[(a * (b - 1) - 1) % 2][1][i] + dp[(a * (b - 1) - 1) % 2][2][i]) %
MOD;
int pow = 1;
for (int i = 0; i < q; i++)
pow = pow * 3 % MOD;
ret = (pow - ret) % MOD;
if (ret < 0)
ret += MOD;
printf("%d\n", ret);
} | #include <algorithm>
#include <stdio.h>
using namespace std;
int MOD = 100000;
char str[20][21];
int dp[2][3][1 << 20];
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 0; i < a; i++)
scanf("%s", str[i]);
int q = 0;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
if (str[i][j] == '?')
q++;
if (str[0][0] == 'J')
dp[1][0][0] = 1;
if (str[0][0] == 'O')
dp[1][1][0] = 1;
if (str[0][0] == 'I')
dp[1][2][0] = 1;
if (str[0][0] == '?')
dp[1][0][0] = dp[1][1][0] = dp[1][2][0] = 1;
for (int i = 0; i < a; i++) {
if (i) {
int e = (i * (b - 1) + 1) % 2;
for (int k = 0; k < (1 << (b - 1)); k++) {
if (str[i][0] == 'J') {
dp[e][0][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == 'O') {
dp[e][1][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == 'I') {
if (k % 2 == 0)
dp[e][2][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
}
if (str[i][0] == '?') {
int v = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
int w = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
if (k % 2 == 0)
dp[e][2][k] = dp[e][0][k] + dp[e][1][k] + dp[e][2][k];
dp[e][0][k] = v;
dp[e][1][k] = w;
}
}
// for(int k=0;k<(1<<(b-1));k++)printf("[%d,%d,%d]
// ",dp[e][0][k],dp[e][1][k],dp[e][2][k]); printf("\n");
for (int k = 0; k < (1 << (b - 1)); k++) {
dp[e][0][k] %= MOD;
dp[e][1][k] %= MOD;
dp[e][2][k] %= MOD;
}
}
for (int j = 0; j < b - 1; j++) {
int e = (i * (b - 1) + j) % 2;
for (int k = 0; k < (1 << (b - 1)); k++)
dp[e][0][k] = dp[e][1][k] = dp[e][2][k] = 0;
for (int k = 0; k < (1 << (b - 1)); k++) {
if (dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k] == 0)
continue;
int t = k / 2;
if (str[i][j] == 'J' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k];
} else if (str[i][j] == 'J' && str[i][j + 1] == 'O')
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
else if (str[i][j] == 'J' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k];
} else if (str[i][j] == 'J' && str[i][j + 1] == '?') {
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][0][t] += dp[!e][0][k];
dp[e][2][t] += dp[!e][0][k];
}
else if (str[i][j] == 'O' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][1][k];
} else if (str[i][j] == 'O' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][1][k];
dp[e][1][t] += dp[!e][1][k];
dp[e][2][t] += dp[!e][1][k];
} else if (k & 1) {
if (str[i][j] == '?' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k];
} else if (str[i][j] == '?' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][1][t] += dp[!e][1][k];
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k];
}
} else {
if (str[i][j] == '?' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][1][k] + dp[!e][2][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
} else if (str[i][j] == '?' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == '?' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
dp[e][1][(1 << (b - 2)) + t] += dp[!e][0][k];
dp[e][1][t] += dp[!e][1][k] + dp[!e][2][k];
dp[e][2][t] += dp[!e][0][k] + dp[!e][1][k] + dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'J') {
dp[e][0][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'O') {
dp[e][1][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == 'I') {
dp[e][2][t] += dp[!e][2][k];
} else if (str[i][j] == 'I' && str[i][j + 1] == '?') {
dp[e][0][t] += dp[!e][2][k];
dp[e][1][t] += dp[!e][2][k];
dp[e][2][t] += dp[!e][2][k];
}
}
}
// for(int k=0;k<(1<<(b-1));k++)printf("[%d,%d,%d]
// ",dp[e][0][k],dp[e][1][k],dp[e][2][k]); printf("\n");
for (int k = 0; k < (1 << (b - 1)); k++) {
dp[e][0][k] %= MOD;
dp[e][1][k] %= MOD;
dp[e][2][k] %= MOD;
}
}
}
int ret = 0;
for (int i = 0; i < (1 << (b - 1)); i++)
ret = (ret + dp[(a * (b - 1) - 1) % 2][0][i] +
dp[(a * (b - 1) - 1) % 2][1][i] + dp[(a * (b - 1) - 1) % 2][2][i]) %
MOD;
int pow = 1;
for (int i = 0; i < q; i++)
pow = pow * 3 % MOD;
ret = (pow - ret) % MOD;
if (ret < 0)
ret += MOD;
printf("%d\n", ret);
} | insert | 60 | 60 | 60 | 62 | TLE | |
p00482 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
static const int MBIT = 600000;
static const int mod = 100000;
short N, M;
short fld[20 * 20];
int dp[2][3][MBIT];
vector<int> BIT;
int ans = 0;
int all = 1;
int main() {
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
for (int i = 0; i <= maxbit; i++) {
bool isbit = false;
for (int j = 0; j < M - 1; ++j) {
int bit1 = 1 << j, bit2 = 1 << (j + 1);
if ((i & bit1) && (i & bit2)) {
isbit = true;
break;
}
}
if (!isbit)
BIT.PB(i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
int tk = BIT[k];
int t1 = tk << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((tk & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 2) {
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
}
dp[p2][j][tk] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
ans += dp[(M * N - 1) & 1][j][BIT[k]];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FI first
#define SE second
#define PB push_back
#define MT make_tuple
using namespace std;
static const int MBIT = 700000;
static const int mod = 100000;
short N, M;
short fld[20 * 20];
int dp[2][3][MBIT];
vector<int> BIT;
int ans = 0;
int all = 1;
int main() {
cin >> N >> M;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
char c;
cin >> c;
if (c == '?')
fld[i * M + j] = 3, all *= 3;
if (c == 'J')
fld[i * M + j] = 0;
if (c == 'O')
fld[i * M + j] = 1;
if (c == 'I')
fld[i * M + j] = 2;
all %= mod;
}
}
if (fld[0] != 3)
dp[0][fld[0]][0] = 1;
else {
dp[0][0][0] = 1;
dp[0][1][0] = 1;
dp[0][2][0] = 1;
}
int maxbit = 0;
for (int i = 0; i < M; ++i) {
maxbit += pow(2, i);
}
for (int i = 0; i <= maxbit; i++) {
bool isbit = false;
for (int j = 0; j < M - 1; ++j) {
int bit1 = 1 << j, bit2 = 1 << (j + 1);
if ((i & bit1) && (i & bit2)) {
isbit = true;
break;
}
}
if (!isbit)
BIT.PB(i);
}
int bit = 1 << M;
int bit1 = 1 << (M - 1);
int bit2 = 1 << (M - 2);
for (int i = 0; i < M * N - 1; ++i) {
int p1 = (i + 1) & 1;
int p2 = i & 1;
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
int tk = BIT[k];
int t1 = tk << 1;
if (t1 & bit)
t1 -= bit;
int t2 = t1 + 1;
if ((tk & bit1) && j == 2 && (i + 1) % M != 0)
continue;
if (fld[i + 1] == 3) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 0) {
dp[p1][0][t1] = (dp[p1][0][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 1) {
if (j == 0 && (i + 1) % M != 0)
dp[p1][1][t2] = (dp[p1][1][t2] + dp[p2][j][tk]) % mod;
else
dp[p1][1][t1] = (dp[p1][1][t1] + dp[p2][j][tk]) % mod;
} else if (fld[i + 1] == 2) {
if (!(tk & bit2) || (i + 2) % M == 0)
dp[p1][2][t1] = (dp[p1][2][t1] + dp[p2][j][tk]) % mod;
}
dp[p2][j][tk] = 0;
}
}
}
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < BIT.size(); ++k) {
ans += dp[(M * N - 1) & 1][j][BIT[k]];
ans %= mod;
}
}
cout << (all % mod - ans % mod + mod) % mod << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p00482 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int M, N;
cin >> M >> N;
char g[20][21];
for (int i = 0; i < M; i++) {
cin >> g[i];
}
static int dp[2][2][1 << 20];
dp[0][0][0] = 1;
int s = M * N;
int MOD = 100000;
int all = 1;
for (int i = 0; i < s; i++) {
int y = i / N;
int x = i % N;
bool q = g[y][x] == '?';
all = (all * (q ? 3 : 1)) % MOD;
int bx = i % 2;
int dx = bx ^ 1;
fill(dp[dx][0], dp[dx + 1][0], 0);
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 1 << 20; l++) {
int db = l & ~(1 << x);
if (q || g[y][x] == 'J') {
dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
}
if (q || g[y][x] == 'O') {
dp[dx][0][db | k << x] =
(dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
}
if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
}
}
}
if (x == s % N - 1) {
for (int k = 0; k < 1 << 20; k++) {
dp[dx][0][k] += dp[dx][1][k];
dp[dx][1][k] = 0;
}
}
}
int a = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1 << 20; j++) {
a = (a + dp[s % 2][i][j]) % MOD;
}
}
cout << (all - a + MOD) % MOD << endl;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int M, N;
cin >> M >> N;
char g[20][21];
for (int i = 0; i < M; i++) {
cin >> g[i];
}
static int dp[2][2][1 << 20];
dp[0][0][0] = 1;
int s = M * N;
int MOD = 100000;
int all = 1;
for (int i = 0; i < s; i++) {
int y = i / N;
int x = i % N;
bool q = g[y][x] == '?';
all = (all * (q ? 3 : 1)) % MOD;
int bx = i % 2;
int dx = bx ^ 1;
fill(dp[dx][0], dp[dx + 1][0], 0);
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 1 << 20; l++) {
if (!dp[bx][k][l])
continue;
int db = l & ~(1 << x);
if (q || g[y][x] == 'J') {
dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
}
if (q || g[y][x] == 'O') {
dp[dx][0][db | k << x] =
(dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
}
if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
}
}
}
if (x == s % N - 1) {
for (int k = 0; k < 1 << 20; k++) {
dp[dx][0][k] += dp[dx][1][k];
dp[dx][1][k] = 0;
}
}
}
int a = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1 << 20; j++) {
a = (a + dp[s % 2][i][j]) % MOD;
}
}
cout << (all - a + MOD) % MOD << endl;
} | insert | 27 | 27 | 27 | 29 | TLE | |
p00482 | C++ | Time Limit Exceeded | #include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef int useType;
typedef pair<useType, useType> pr;
typedef vector<useType> VI;
const int INF = 1 << 30;
const int SIZE = 1 << 20;
const int p = 100000;
int DP[2][2][2][SIZE]; // times beforeJ goodFlag true or false
char maps[22][22] = {};
int main() {
int n, m;
string s;
cin >> m >> n;
REP(i, m) {
cin >> s;
REP(j, n)
maps[i][j] = s[j];
}
DP[0][0][0][0] = 1;
int now = 1;
REP(i, m)
REP(j, n) {
if (maps[i][j] == 'J' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE)
DP[now][1][gF][k & (~(1 << j))] += DP[now ^ 1][bJ][gF][k] % p;
if (maps[i][j] == 'O' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE)
if (bJ && j)
DP[now][0][gF][(k & (~(1 << j))) | (1 << (j - 1))] +=
DP[now ^ 1][bJ][gF][k] % p;
else
DP[now][0][gF][k & (~(1 << j))] += DP[now ^ 1][bJ][gF][k] % p;
if (maps[i][j] == 'I' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE) {
// if(DP[now^1][bJ][gF][k])
// cout << bJ << " " << gF << " " << k <<" " <<
//DP[now^1][bJ][gF][k]<< endl;
DP[now][0][((1 << j) & k) || gF][k & (~(1 << j))] +=
DP[now ^ 1][bJ][gF][k] % p;
}
now ^= 1;
REP(j, 2)
REP(k, 2)
REP(l, SIZE)
DP[now][j][k][l] = 0;
}
int sum = 0;
now ^= 1;
REP(bJ, 2)
REP(k, SIZE)
sum = (sum + DP[now][bJ][1][k]) % p;
cout << sum << endl;
return 0;
} | #include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REP(i, a) FOR(i, 0, a)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef int useType;
typedef pair<useType, useType> pr;
typedef vector<useType> VI;
const int INF = 1 << 30;
const int SIZE = 1 << 19;
const int p = 100000;
int DP[2][2][2][SIZE]; // times beforeJ goodFlag true or false
char maps[22][22] = {};
int main() {
int n, m;
string s;
cin >> m >> n;
REP(i, m) {
cin >> s;
REP(j, n)
maps[i][j] = s[j];
}
DP[0][0][0][0] = 1;
int now = 1;
REP(i, m)
REP(j, n) {
if (maps[i][j] == 'J' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE)
DP[now][1][gF][k & (~(1 << j))] += DP[now ^ 1][bJ][gF][k] % p;
if (maps[i][j] == 'O' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE)
if (bJ && j)
DP[now][0][gF][(k & (~(1 << j))) | (1 << (j - 1))] +=
DP[now ^ 1][bJ][gF][k] % p;
else
DP[now][0][gF][k & (~(1 << j))] += DP[now ^ 1][bJ][gF][k] % p;
if (maps[i][j] == 'I' || maps[i][j] == '?')
REP(gF, 2)
REP(bJ, 2)
REP(k, SIZE) {
// if(DP[now^1][bJ][gF][k])
// cout << bJ << " " << gF << " " << k <<" " <<
//DP[now^1][bJ][gF][k]<< endl;
DP[now][0][((1 << j) & k) || gF][k & (~(1 << j))] +=
DP[now ^ 1][bJ][gF][k] % p;
}
now ^= 1;
REP(j, 2)
REP(k, 2)
REP(l, SIZE)
DP[now][j][k][l] = 0;
}
int sum = 0;
now ^= 1;
REP(bJ, 2)
REP(k, SIZE)
sum = (sum + DP[now][bJ][1][k]) % p;
cout << sum << endl;
return 0;
} | replace | 18 | 19 | 18 | 19 | TLE | |
p00482 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";
int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];
int main() {
lli all = 1;
cin >> H >> W;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> G[i][j];
if (G[i][j] == '?') {
all = (all * 3) % mod;
}
}
}
dp[0][0] = 1;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
bool p = (i * W + j) % 2;
dp[!p].clear();
for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
++it) {
int b = it->first;
for (int k = 0; k < 3; ++k) {
if (G[i][j] == '?' || G[i][j] == C[k]) {
int nb = b & ~(1 << j);
if (C[k] == 'J') {
nb &= ~(1 << (j - 1));
if (j + 1 != W)
nb |= (1 << j);
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
} else if (C[k] == 'O') {
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
} else if (C[k] == 'I') {
if (!(b & (1 << j))) {
nb &= ~(1 << (j - 1));
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
}
}
}
}
}
}
}
lli sum = 0;
for (int b = 0; b < (1 << W); ++b) {
sum = (sum + dp[H * W % 2][b]) % mod;
}
cout << (all - sum + mod) % mod << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";
int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];
int main() {
lli all = 1;
cin >> H >> W;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> G[i][j];
if (G[i][j] == '?') {
all = (all * 3) % mod;
}
}
}
dp[0][0] = 1;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
bool p = (i * W + j) % 2;
dp[!p].clear();
for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
++it) {
int b = it->first;
for (int k = 0; k < 3; ++k) {
if (G[i][j] == '?' || G[i][j] == C[k]) {
int nb = b & ~(1 << j);
if (C[k] == 'J') {
nb &= ~(1 << (j - 1));
if (j + 1 != W)
nb |= (1 << j);
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
} else if (C[k] == 'O') {
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
} else if (C[k] == 'I') {
if (!(b & (1 << j))) {
nb &= ~(1 << (j - 1));
dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
}
}
}
}
}
}
}
lli sum = 0;
for (map<int, lli>::iterator it = dp[H * W % 2].begin();
it != dp[H * W % 2].end(); ++it) {
sum = (sum + it->second) % mod;
}
cout << (all - sum + mod) % mod << endl;
return 0;
} | replace | 54 | 56 | 54 | 57 | MLE | |
p00483 | C++ | Runtime Error | #include <cstdio>
#include <string>
#include <vector>
using namespace std;
int m, n, k;
vector<string> v;
char t[1001];
int dp[1000][1000][3];
void solve(int a, int b, int c, int d) {
int j = 0, o = 0, i = 0;
for (int x = a - 1; x < c; x++) {
for (int y = b - 1; y < d; y++) {
if (v[x][y] == 'J')
j++;
else if (v[x][y] == 'O')
o++;
else
i++;
}
}
printf("%d %d %d\n", j, o, i);
}
int main() {
scanf("%d %d %d", &m, &n, &k);
for (int i = 0; i < m; i++) {
scanf("%s", t);
v.push_back(t);
}
/* int a, b, c, d;
for(int i=0; i<k; i++){
scanf("%d %d %d %d", &a, &b, &c, &d);
solve(a, b, c, d);
}
*/
if (v[0][0] == 'J')
dp[0][0][0] = 1;
else if (v[0][0] == 'O')
dp[0][0][1] = 1;
else
dp[0][0][2] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
dp[i][0][j] += dp[i - 1][0][j];
}
if (v[0][i] == 'J')
dp[i][0][0] += 1;
else if (v[0][i] == 'O')
dp[i][0][1] += 1;
else
dp[i][0][2] += 1;
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < 3; j++) {
dp[0][i][j] += dp[0][i - 1][j];
}
if (v[i][0] == 'J')
dp[0][i][0] += 1;
else if (v[i][0] == 'O')
dp[0][i][1] += 1;
else
dp[0][i][2] += 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
for (int l = 0; l < 3; l++) {
dp[i][j][l] = dp[i - 1][j][l] + dp[i][j - 1][l] - dp[i - 1][j - 1][l];
}
if (v[j][i] == 'J')
dp[i][j][0] += 1;
else if (v[j][i] == 'O')
dp[i][j][1] += 1;
else
dp[i][j][2] += 1;
}
}
/*
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("%d\t", dp[j][i][0]);
}
printf("\n");
}
*/
int a, b, c, d;
for (int i = 0; i < k; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans[3];
if (a == 1 && b == 1)
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j];
else
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j] -
dp[b - 2][c - 1][j] + dp[b - 2][a - 2][j];
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
} | #include <cstdio>
#include <string>
#include <vector>
using namespace std;
int m, n, k;
vector<string> v;
char t[1001];
int dp[1000][1000][3];
void solve(int a, int b, int c, int d) {
int j = 0, o = 0, i = 0;
for (int x = a - 1; x < c; x++) {
for (int y = b - 1; y < d; y++) {
if (v[x][y] == 'J')
j++;
else if (v[x][y] == 'O')
o++;
else
i++;
}
}
printf("%d %d %d\n", j, o, i);
}
int main() {
scanf("%d %d %d", &m, &n, &k);
for (int i = 0; i < m; i++) {
scanf("%s", t);
v.push_back(t);
}
/* int a, b, c, d;
for(int i=0; i<k; i++){
scanf("%d %d %d %d", &a, &b, &c, &d);
solve(a, b, c, d);
}
*/
if (v[0][0] == 'J')
dp[0][0][0] = 1;
else if (v[0][0] == 'O')
dp[0][0][1] = 1;
else
dp[0][0][2] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
dp[i][0][j] += dp[i - 1][0][j];
}
if (v[0][i] == 'J')
dp[i][0][0] += 1;
else if (v[0][i] == 'O')
dp[i][0][1] += 1;
else
dp[i][0][2] += 1;
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < 3; j++) {
dp[0][i][j] += dp[0][i - 1][j];
}
if (v[i][0] == 'J')
dp[0][i][0] += 1;
else if (v[i][0] == 'O')
dp[0][i][1] += 1;
else
dp[0][i][2] += 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
for (int l = 0; l < 3; l++) {
dp[i][j][l] = dp[i - 1][j][l] + dp[i][j - 1][l] - dp[i - 1][j - 1][l];
}
if (v[j][i] == 'J')
dp[i][j][0] += 1;
else if (v[j][i] == 'O')
dp[i][j][1] += 1;
else
dp[i][j][2] += 1;
}
}
/*
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("%d\t", dp[j][i][0]);
}
printf("\n");
}
*/
int a, b, c, d;
for (int i = 0; i < k; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans[3];
if (a == 1 && b == 1)
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j];
else if (a == 1 && b != 1)
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j] - dp[b - 2][c - 1][j];
else if (a != 1 && b == 1)
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j];
else
for (int j = 0; j < 3; j++)
ans[j] = dp[d - 1][c - 1][j] - dp[d - 1][a - 2][j] -
dp[b - 2][c - 1][j] + dp[b - 2][a - 2][j];
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
} | insert | 93 | 93 | 93 | 99 | 0 | |
p00483 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int INF = 1e9, N = 1010;
const ll INFL = 1e18;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int n, m, k;
char a[N][N];
int T[N][N];
struct query {
int X1;
int Y1;
int X2;
int Y2;
int J;
int O;
int I;
} q[N * 10];
void upd(int x, int y) {
for (int i = x; i <= n; i += i & -i)
for (int j = y; j <= m; j += j & -j)
T[i][j]++;
}
int get(int x, int y) {
int res = 0;
for (int i = x; i > 0; i -= i & -i)
for (int j = y; j > 0; j -= j & -j)
res += T[i][j];
return res;
}
int get(query qu) {
return get(qu.X2, qu.Y2) - get(qu.X1 - 1, qu.Y2) - get(qu.X2, qu.Y1 - 1) +
get(qu.X1 - 1, qu.Y1 - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> k;
ff(i, 1, n) ff(j, 1, m) cin >> a[i][j];
ff(i, 1, k) cin >> q[i].X1 >> q[i].Y1 >> q[i].X2 >> q[i].Y2;
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'J') upd(i, j);
ff(i, 1, k) q[i].J = get(q[i]);
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'O') upd(i, j);
ff(i, 1, k) q[i].O = get(q[i]);
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'I') upd(i, j);
ff(i, 1, k) q[i].I = get(q[i]);
ff(i, 1, k) printf("%d %d %d\n", q[i].J, q[i].O, q[i].I);
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int INF = 1e9, N = 1010;
const ll INFL = 1e18;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int n, m, k;
char a[N][N];
int T[N][N];
struct query {
int X1;
int Y1;
int X2;
int Y2;
int J;
int O;
int I;
} q[N * 100];
void upd(int x, int y) {
for (int i = x; i <= n; i += i & -i)
for (int j = y; j <= m; j += j & -j)
T[i][j]++;
}
int get(int x, int y) {
int res = 0;
for (int i = x; i > 0; i -= i & -i)
for (int j = y; j > 0; j -= j & -j)
res += T[i][j];
return res;
}
int get(query qu) {
return get(qu.X2, qu.Y2) - get(qu.X1 - 1, qu.Y2) - get(qu.X2, qu.Y1 - 1) +
get(qu.X1 - 1, qu.Y1 - 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> k;
ff(i, 1, n) ff(j, 1, m) cin >> a[i][j];
ff(i, 1, k) cin >> q[i].X1 >> q[i].Y1 >> q[i].X2 >> q[i].Y2;
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'J') upd(i, j);
ff(i, 1, k) q[i].J = get(q[i]);
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'O') upd(i, j);
ff(i, 1, k) q[i].O = get(q[i]);
memset(T, 0, sizeof T);
ff(i, 1, n) ff(j, 1, m) if (a[i][j] == 'I') upd(i, j);
ff(i, 1, k) q[i].I = get(q[i]);
ff(i, 1, k) printf("%d %d %d\n", q[i].J, q[i].O, q[i].I);
} | replace | 31 | 32 | 31 | 32 | 0 | |
p00483 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define LL long long
using namespace std;
static const LL INF = 1LL << 60LL;
typedef pair<int, int> pii;
string Map[1010];
LL J[1010][1010], O[1010][1010], I[1010][1010];
LL m, n, k;
LL a, b, c, d;
LL res_J, res_O, res_I;
int main() {
cin >> m >> n;
cin >> k;
for (int y = 1; y <= m; ++y) {
for (int x = 1; x <= n; ++x) {
cin >> Map[y][x];
if (Map[y][x] == 'J') {
J[y][x]++;
}
if (Map[y][x] == 'O') {
O[y][x]++;
}
if (Map[y][x] == 'I') {
I[y][x]++;
}
J[y][x] += J[y][x - 1];
O[y][x] += O[y][x - 1];
I[y][x] += I[y][x - 1];
}
}
for (int i = 0; i < k; ++i) {
cin >> a >> b >> c >> d;
int Min = min(a, c);
int Max = max(a, c);
for (int y = Min; y <= Max; ++y) {
res_J += J[y][d] - J[y][b - 1];
res_O += O[y][d] - O[y][b - 1];
res_I += I[y][d] - I[y][b - 1];
}
cout << res_J << " " << res_O << " " << res_I << endl;
res_J = 0;
res_O = 0;
res_I = 0;
}
return 0;
} | #include <bits/stdc++.h>
#define LL long long
using namespace std;
static const LL INF = 1LL << 60LL;
typedef pair<int, int> pii;
char Map[1010][1010];
LL J[1010][1010], O[1010][1010], I[1010][1010];
LL m, n, k;
LL a, b, c, d;
LL res_J, res_O, res_I;
int main() {
cin >> m >> n;
cin >> k;
for (int y = 1; y <= m; ++y) {
for (int x = 1; x <= n; ++x) {
cin >> Map[y][x];
if (Map[y][x] == 'J') {
J[y][x]++;
}
if (Map[y][x] == 'O') {
O[y][x]++;
}
if (Map[y][x] == 'I') {
I[y][x]++;
}
J[y][x] += J[y][x - 1];
O[y][x] += O[y][x - 1];
I[y][x] += I[y][x - 1];
}
}
for (int i = 0; i < k; ++i) {
cin >> a >> b >> c >> d;
int Min = min(a, c);
int Max = max(a, c);
for (int y = Min; y <= Max; ++y) {
res_J += J[y][d] - J[y][b - 1];
res_O += O[y][d] - O[y][b - 1];
res_I += I[y][d] - I[y][b - 1];
}
cout << res_J << " " << res_O << " " << res_I << endl;
res_J = 0;
res_O = 0;
res_I = 0;
}
return 0;
} | replace | 6 | 7 | 6 | 7 | TLE | |
p00483 | C++ | Runtime Error | #include <cstdio>
int joi2num(char c) {
if (c == 'J')
return 0;
if (c == 'O')
return 1;
return 2;
}
int main() {
int m, n, k;
char fld[1024][1024];
int sum[1024][1024][3];
scanf("%d%d%d", &m, &n, &k);
for (int x = 0; x < m; ++x)
scanf("%s", fld[x]);
for (int i = 0; i < 3; ++i)
sum[0][0][i] = (i == joi2num(fld[0][0]));
for (int x = 1; x < m; ++x) {
for (int i = 0; i < 3; ++i)
sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
}
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i)
sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
}
for (int x = 1; x < m; ++x) {
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i) {
int temp = 0;
temp += sum[x - 1][y][i];
temp += sum[x][y - 1][i];
temp -= sum[x - 1][y - 1][i];
sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
}
}
}
for (int i = 0; i < k; ++i) {
int ax, ay, bx, by;
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
ax -= 1, ay -= 1, bx -= 1, by -= 1;
int ans[3] = {0};
for (int j = 0; j < 3; ++j) {
ans[j] += sum[bx][by][j];
ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
}
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
} | #include <cstdio>
int joi2num(char c) {
if (c == 'J')
return 0;
if (c == 'O')
return 1;
return 2;
}
int main() {
int m, n, k;
static char fld[1024][1024];
static int sum[1024][1024][3] = {0};
scanf("%d%d%d", &m, &n, &k);
for (int x = 0; x < m; ++x)
scanf("%s", fld[x]);
for (int i = 0; i < 3; ++i)
sum[0][0][i] = (i == joi2num(fld[0][0]));
for (int x = 1; x < m; ++x) {
for (int i = 0; i < 3; ++i)
sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
}
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i)
sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
}
for (int x = 1; x < m; ++x) {
for (int y = 1; y < n; ++y) {
for (int i = 0; i < 3; ++i) {
int temp = 0;
temp += sum[x - 1][y][i];
temp += sum[x][y - 1][i];
temp -= sum[x - 1][y - 1][i];
sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
}
}
}
for (int i = 0; i < k; ++i) {
int ax, ay, bx, by;
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
ax -= 1, ay -= 1, bx -= 1, by -= 1;
int ans[3] = {0};
for (int j = 0; j < 3; ++j) {
ans[j] += sum[bx][by][j];
ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
}
printf("%d %d %d\n", ans[0], ans[1], ans[2]);
}
return 0;
} | replace | 12 | 14 | 12 | 14 | -11 | |
p00484 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[11][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
int N, K;
int c, g;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> c >> g;
book[g - 1][kind[g - 1]++] = c;
}
for (int i = 0; i < 11; i++) {
sort(book[i], book[i] + kind[i]);
reverse(book[i], book[i] + kind[i]);
}
for (int i = 0; i < 11; i++) {
acc[i][0] = 0;
for (int j = 1; j <= kind[i]; j++) {
acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
}
}
for (int i = 0; i <= K; i++) {
if (0 < i && i <= kind[0])
dp[0][i] = acc[0][i];
else
dp[0][i] = 0;
}
for (int i = 1; i <= 11; i++) {
for (int j = 0; j <= K; j++) {
dp[i][j] = 0;
for (int k = 0; k <= min(j, kind[i]); k++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
}
}
}
cout << dp[11][K] << endl;
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[12][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
int N, K;
int c, g;
cin >> N >> K;
for (int i = 0; i < N; i++) {
cin >> c >> g;
book[g - 1][kind[g - 1]++] = c;
}
for (int i = 0; i < 11; i++) {
sort(book[i], book[i] + kind[i]);
reverse(book[i], book[i] + kind[i]);
}
for (int i = 0; i < 11; i++) {
acc[i][0] = 0;
for (int j = 1; j <= kind[i]; j++) {
acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
}
}
for (int i = 0; i <= K; i++) {
if (0 < i && i <= kind[0])
dp[0][i] = acc[0][i];
else
dp[0][i] = 0;
}
for (int i = 1; i <= 11; i++) {
for (int j = 0; j <= K; j++) {
dp[i][j] = 0;
for (int k = 0; k <= min(j, kind[i]); k++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
}
}
}
cout << dp[11][K] << endl;
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p00484 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<int> cost[10];
int dp[10][2001];
int main() {
int n, k;
scanf("%d%d", &n, &k);
rep(i, n) {
int c, g;
scanf("%d%d", &c, &g);
cost[--g].push_back(c);
}
rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
for (int i = 1; i <= 10; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
int sum = 0;
for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
sum += cost[i - 1][t];
dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
}
}
}
printf("%d\n", dp[10][k]);
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<int> cost[10];
int dp[15][2001];
int main() {
int n, k;
scanf("%d%d", &n, &k);
rep(i, n) {
int c, g;
scanf("%d%d", &c, &g);
cost[--g].push_back(c);
}
rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
for (int i = 1; i <= 10; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
int sum = 0;
for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
sum += cost[i - 1][t];
dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
}
}
}
printf("%d\n", dp[10][k]);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00484 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> a[10];
vector<int> b[10];
int dp[10][MAX_N];
int n, K;
int main() {
int x, y;
scanf("%d%d", &n, &K);
rep(i, n) {
scanf("%d%d", &x, &y);
a[y - 1].push_back(x);
}
rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
rep(i, 10) {
int suma = 0;
rep(j, a[i].size()) {
suma += a[i][j] + 2 * j;
b[i].push_back(suma);
}
}
rep(j, K + 1) { dp[0][j] = 0; }
rep(i, 10) {
rep(j, K + 1) {
dp[i + 1][j] = dp[i][j];
for (int k = 1; k <= min((int)b[i].size(), j); k++) {
dp[i + 1][j] =
max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
}
}
}
printf("%d\n", dp[10][K]);
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> a[11];
vector<int> b[11];
int dp[11][MAX_N];
int n, K;
int main() {
int x, y;
scanf("%d%d", &n, &K);
rep(i, n) {
scanf("%d%d", &x, &y);
a[y - 1].push_back(x);
}
rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
rep(i, 10) {
int suma = 0;
rep(j, a[i].size()) {
suma += a[i][j] + 2 * j;
b[i].push_back(suma);
}
}
rep(j, K + 1) { dp[0][j] = 0; }
rep(i, 10) {
rep(j, K + 1) {
dp[i + 1][j] = dp[i][j];
for (int k = 1; k <= min((int)b[i].size(), j); k++) {
dp[i + 1][j] =
max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
}
}
}
printf("%d\n", dp[10][K]);
return 0;
} | replace | 12 | 15 | 12 | 15 | 0 | |
p00484 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#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 INF_ = 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() {
int n, K;
cin >> n >> K;
vector<int> v[10];
rep(i, 0, n) {
int c, g;
cin >> c >> g;
--g;
v[g].push_back(c);
}
rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());
vector<int> s[10];
rep(i, 0, 10) {
s[i].resize(v[i].size() + 1);
rep(j, 1, s[i].size()) {
s[i][j] = s[i][j - 1] + v[i][j - 1] + 2 * (j - 1);
}
}
rep(i, 0, 10) dump(s[i]);
static vector<vector<int>> dp(10, vector<int>(K + 1));
// static int dp[10][3000];
rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
rep(i, 1, 10) {
rep(j, 0, s[i].size()) {
for (int k = 0; k + j <= K; ++k) {
dp[i][j + k] = max(dp[i][j + k], dp[i - 1][k] + s[i][j]);
}
}
}
rep(i, 0, 10) dump(dp[i]);
cout << dp[9][K] << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#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 INF_ = 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() {
int n, K;
cin >> n >> K;
vector<int> v[10];
rep(i, 0, n) {
int c, g;
cin >> c >> g;
--g;
v[g].push_back(c);
}
rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());
vector<int> s[10];
rep(i, 0, 10) {
s[i].resize(v[i].size() + 1);
rep(j, 1, s[i].size()) {
s[i][j] = s[i][j - 1] + v[i][j - 1] + 2 * (j - 1);
}
}
rep(i, 0, 10) dump(s[i]);
// static vector<vector<int>> dp(10,vector<int>(K+1));
static int dp[10][3000];
rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
rep(i, 1, 10) {
rep(j, 0, s[i].size()) {
for (int k = 0; k + j <= K; ++k) {
dp[i][j + k] = max(dp[i][j + k], dp[i - 1][k] + s[i][j]);
}
}
}
rep(i, 0, 10) dump(dp[i]);
cout << dp[9][K] << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | replace | 56 | 58 | 56 | 58 | 0 | |
p00484 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
long long int lis[11][2003], lissize[11] = {0};
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] = -2000000000;
}
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
lis[b][lissize[b] + 1] = a;
lissize[b]++;
}
for (int i = 0; i < 11; i++) {
sort(lis[i] + 1, lis[i] + 2003);
reverse(lis[i] + 1, lis[i] + 2003);
}
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
}
/*
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
}
puts("");
}
*/
long long int fie[12][4003] = {0};
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 2001; j++) {
for (int k = 0; k < 2001; k++) {
if (j + k <= 2000)
fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i + 1][k]);
}
}
}
cout << fie[10][K] << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
long long int lis[11][2003], lissize[11] = {0};
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] = -2000000000;
}
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
lis[b][lissize[b] + 1] = a;
lissize[b]++;
}
for (int i = 0; i < 11; i++) {
sort(lis[i] + 1, lis[i] + 2003);
reverse(lis[i] + 1, lis[i] + 2003);
}
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 2003; j++)
lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
}
/*
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
}
puts("");
}
*/
long long int fie[13][4003] = {0};
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2001; j++) {
for (int k = 0; k < 2001; k++) {
if (j + k <= 2000)
fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i + 1][k]);
}
}
}
cout << fie[10][K] << endl;
return 0;
} | replace | 35 | 37 | 35 | 37 | 0 | |
p00484 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
for (int n, k; cin >> n >> k, n | k;) {
vector<vector<int>> ps(10);
for (int i = 0; i < n; i++) {
int p, g;
cin >> p >> g;
ps[g - 1].push_back(p);
}
for (int i = 0; i < 10; i++)
sort(ps[i].begin(), ps[i].end(), greater<int>());
vector<vector<int>> ss = ps;
for (int i = 0; i < 10; i++)
for (int j = 1; j < ss[i].size(); j++)
ss[i][j] += ss[i][j - 1] + 2 * j;
vector<vector<int>> dp(2, vector<int>(n + 1));
for (int i = 0; i < 10; i++) {
dp[i + 1 & 1] = dp[i & 1];
for (int j = 0; j < ss[i].size(); j++)
for (int k = n; k > j; k--)
dp[i + 1 & 1][k] =
max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
}
cout << dp[0][k] << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
for (int n, k; cin >> n >> k && n | k;) {
vector<vector<int>> ps(10);
for (int i = 0; i < n; i++) {
int p, g;
cin >> p >> g;
ps[g - 1].push_back(p);
}
for (int i = 0; i < 10; i++)
sort(ps[i].begin(), ps[i].end(), greater<int>());
vector<vector<int>> ss = ps;
for (int i = 0; i < 10; i++)
for (int j = 1; j < ss[i].size(); j++)
ss[i][j] += ss[i][j - 1] + 2 * j;
vector<vector<int>> dp(2, vector<int>(n + 1));
for (int i = 0; i < 10; i++) {
dp[i + 1 & 1] = dp[i & 1];
for (int j = 0; j < ss[i].size(); j++)
for (int k = n; k > j; k--)
dp[i + 1 & 1][k] =
max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
}
cout << dp[0][k] << endl;
}
return 0;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p00484 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> pr(10, vector<int>(n + 1, 0));
vector<int> ko(10, 0);
for (int i = 0; i < n; i++) {
int c, g;
cin >> c >> g;
g--;
pr[g][ko[g]] = c;
ko[g]++;
}
for (int i = 0; i < 10; i++) {
sort(pr[i].begin(), pr[i].end());
reverse(pr[i].begin(), pr[i].end());
}
vector<vector<int>> sum(10, vector<int>(n + 1, 0));
for (int i = 0; i < n; i++) {
int su = 0;
for (int j = 1; j <= ko[i]; j++) {
su += pr[i][j - 1];
sum[i][j] = su + j * (j - 1);
}
}
vector<vector<int>> dp(10, vector<int>(n + 1, 0));
dp[0] = sum[0];
for (int i = 1; i < 10; i++) {
for (int j = 0; j <= k; j++) {
for (int kk = j; kk <= k; kk++) {
dp[i][kk] = max(dp[i][kk], dp[i - 1][j] + sum[i][kk - j]);
}
}
}
cout << dp[9][k] << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> pr(10, vector<int>(n + 1, 0));
vector<int> ko(10, 0);
for (int i = 0; i < n; i++) {
int c, g;
cin >> c >> g;
g--;
pr[g][ko[g]] = c;
ko[g]++;
}
for (int i = 0; i < 10; i++) {
sort(pr[i].begin(), pr[i].end());
reverse(pr[i].begin(), pr[i].end());
}
vector<vector<int>> sum(10, vector<int>(n + 1, 0));
for (int i = 0; i < 10; i++) {
int su = 0;
for (int j = 1; j <= ko[i]; j++) {
su += pr[i][j - 1];
sum[i][j] = su + j * (j - 1);
}
}
vector<vector<int>> dp(10, vector<int>(n + 1, 0));
dp[0] = sum[0];
for (int i = 1; i < 10; i++) {
for (int j = 0; j <= k; j++) {
for (int kk = j; kk <= k; kk++) {
dp[i][kk] = max(dp[i][kk], dp[i - 1][j] + sum[i][kk - j]);
}
}
}
cout << dp[9][k] << endl;
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p00484 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#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 INF_ = 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() {
int n, K;
cin >> n >> K;
vector<int> v[10];
rep(i, 0, n) {
int c, g;
cin >> c >> g;
--g;
v[g].push_back(c);
}
rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());
vector<int> s[10];
rep(i, 0, 10) s[i].resize(v[i].size());
rep(i, 0, 10) {
rep(j, 0, s[i].size()) {
if (j == 0)
s[i][j] = v[i][j];
else
s[i][j] = s[i][j - 1] + v[i][j] + 2 * j;
}
}
rep(i, 0, 10) dump(s[i]);
vector<vector<int>> dp(10, vector<int>(K));
rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
rep(i, 1, 10) {
rep(j, 0, K) {
if (s[i].size() > j)
dp[i][j] = s[i][j];
rep(k, 0, j + 1) {
if ((int)s[i].size() <= j - k - 1)
continue;
if (j - k - 1 < 0)
dp[i][j] = max(dp[i][j], dp[i - 1][k]);
else
dp[i][j] = max(dp[i][j], dp[i - 1][k] + s[i][j - k - 1]);
}
}
}
rep(i, 0, 10) dump(dp[i]);
cout << dp[9][K - 1] << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#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 INF_ = 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() {
int n, K;
cin >> n >> K;
vector<int> v[10];
rep(i, 0, n) {
int c, g;
cin >> c >> g;
--g;
v[g].push_back(c);
}
rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());
vector<int> s[10];
rep(i, 0, 10) s[i].resize(v[i].size());
rep(i, 0, 10) {
rep(j, 0, s[i].size()) {
if (j == 0)
s[i][j] = v[i][j];
else
s[i][j] = s[i][j - 1] + v[i][j] + 2 * j;
}
}
rep(i, 0, 10) dump(s[i]);
// static vector<vector<int>> dp(10,vector<int>(K));
static int dp[10][3000];
rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
rep(i, 1, 10) {
rep(j, 0, K) {
if (s[i].size() > j)
dp[i][j] = s[i][j];
rep(k, 0, j + 1) {
if ((int)s[i].size() <= j - k - 1)
continue;
if (j - k - 1 < 0)
dp[i][j] = max(dp[i][j], dp[i - 1][k]);
else
dp[i][j] = max(dp[i][j], dp[i - 1][k] + s[i][j - k - 1]);
}
}
}
rep(i, 0, 10) dump(dp[i]);
cout << dp[9][K - 1] << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | replace | 59 | 60 | 59 | 61 | 0 | |
p00484 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
long long int cost[10][3000];
long long int DP[3000];
int main() {
int N, K;
while (true) {
cin >> N >> K;
if (cin.eof()) {
break;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3000; j++) {
cost[i][j] = 0;
}
}
vector<int> v_cost[10];
for (int i = 0; i < N; i++) {
int c, g;
cin >> c >> g;
v_cost[g].push_back(c);
}
for (int i = 0; i < 10; i++) {
sort(v_cost[i].begin(), v_cost[i].end());
long long int sum = 0;
for (int j = v_cost[i].size() - 1; j >= 0; j--) {
int count = v_cost[i].size() - j - 1;
sum += v_cost[i][j];
cost[i][count] = sum + count * (count + 1);
}
}
for (int i = 0; i < 3000; i++) {
DP[i] = 0;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
for (int k = 0; k < K; k++) {
if (j + k + 1 > K) {
break;
}
DP[j] = max(DP[j], DP[j + k + 1] + cost[i][k]);
}
// cout << DP[j] << " ";
}
// cout << endl;
}
cout << DP[0] << endl;
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
long long int cost[10][3000];
long long int DP[3000];
int main() {
int N, K;
while (true) {
cin >> N >> K;
if (cin.eof()) {
break;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3000; j++) {
cost[i][j] = 0;
}
}
vector<int> v_cost[10];
for (int i = 0; i < N; i++) {
int c, g;
cin >> c >> g;
g--;
v_cost[g].push_back(c);
}
for (int i = 0; i < 10; i++) {
sort(v_cost[i].begin(), v_cost[i].end());
long long int sum = 0;
for (int j = v_cost[i].size() - 1; j >= 0; j--) {
int count = v_cost[i].size() - j - 1;
sum += v_cost[i][j];
cost[i][count] = sum + count * (count + 1);
}
}
for (int i = 0; i < 3000; i++) {
DP[i] = 0;
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
for (int k = 0; k < K; k++) {
if (j + k + 1 > K) {
break;
}
DP[j] = max(DP[j], DP[j + k + 1] + cost[i][k]);
}
// cout << DP[j] << " ";
}
// cout << endl;
}
cout << DP[0] << endl;
}
return 0;
} | insert | 31 | 31 | 31 | 32 | 0 | |
p00484 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, K, latte[2001] = {0};
for (int i = 1; i <= 2000; i++)
latte[i] = (i - 1) * i;
cin >> N >> K;
vector<vector<int>> books(10);
for (int i = 0; i < N; i++) {
int v, j;
cin >> v >> j;
j--;
books[j].push_back(v);
}
for (int i = 0; i < 10; i++) {
sort(books[i].begin(), books[i].end(), greater<int>());
for (int j = 0; j < books[i].size(); j++) {
if (j + 1 == books[i].size()) {
books[i][j] += j * (j + 1);
continue;
}
books[i][j + 1] += books[i][j];
books[i][j] += j * (j + 1);
}
}
int dp[11][2001];
fill(dp[0], dp[0] + 11 * 2001, -1);
for (int i = 0; i <= N; i++)
dp[i][0] = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
if (dp[i][j] == -1)
break;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
for (int k = 0; k < books[i].size(); k++) {
if (j + k + 1 > K)
break;
dp[i + 1][j + k + 1] =
max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
}
}
}
/*
for(int i=0;i<10;i++){
if(!books[i].size())continue;
cout<<i<<":";
for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
cout<<endl;
}
cout<<endl<<endl;
for(int i=0;i<=10;i++){
for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
cout << dp[10][K] << endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, K, latte[2001] = {0};
for (int i = 1; i <= 2000; i++)
latte[i] = (i - 1) * i;
cin >> N >> K;
vector<vector<int>> books(10);
for (int i = 0; i < N; i++) {
int v, j;
cin >> v >> j;
j--;
books[j].push_back(v);
}
for (int i = 0; i < 10; i++) {
sort(books[i].begin(), books[i].end(), greater<int>());
for (int j = 0; j < books[i].size(); j++) {
if (j + 1 == books[i].size()) {
books[i][j] += j * (j + 1);
continue;
}
books[i][j + 1] += books[i][j];
books[i][j] += j * (j + 1);
}
}
int dp[11][2001];
fill(dp[0], dp[0] + 11 * 2001, -1);
for (int i = 0; i <= 10; i++)
dp[i][0] = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
if (dp[i][j] == -1)
break;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
for (int k = 0; k < books[i].size(); k++) {
if (j + k + 1 > K)
break;
dp[i + 1][j + k + 1] =
max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
}
}
}
/*
for(int i=0;i<10;i++){
if(!books[i].size())continue;
cout<<i<<":";
for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
cout<<endl;
}
cout<<endl<<endl;
for(int i=0;i<=10;i++){
for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
cout << dp[10][K] << endl;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p00484 | 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)
cout << *i << " ";
cout << endl;
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
int main() {
int N, K;
while (cin >> N >> K && N) {
int c[2000], g[2000];
vector<int> cash[10];
REP(i, N) {
cin >> c[i] >> g[i];
cash[g[i]].push_back(c[i]);
}
vector<int> total[10];
REP(i, 10) {
sort(cash[i].begin(), cash[i].end());
reverse(cash[i].begin(), cash[i].end());
total[i] = vector<int>(cash[i].size() + 1);
REP(j, cash[i].size()) total[i][j + 1] = total[i][j] + cash[i][j];
}
int dp[2][2000] = {};
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
for (int k = 0; k <= min(j, (int)cash[i].size()); k++) {
dp[(i + 1) % 2][j] = max(
dp[(i + 1) % 2][j], dp[i % 2][j - k] + total[i][k] + k * (k - 1));
}
}
}
cout << dp[10 % 2][K] << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cout << *i << " ";
cout << endl;
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
int main() {
int N, K;
while (cin >> N >> K && N) {
int c[2000], g[2000];
vector<int> cash[10];
REP(i, N) {
cin >> c[i] >> g[i];
g[i]--;
cash[g[i]].push_back(c[i]);
}
vector<int> total[10];
REP(i, 10) {
sort(cash[i].begin(), cash[i].end());
reverse(cash[i].begin(), cash[i].end());
total[i] = vector<int>(cash[i].size() + 1);
REP(j, cash[i].size()) total[i][j + 1] = total[i][j] + cash[i][j];
}
int dp[2][2000] = {};
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= K; j++) {
for (int k = 0; k <= min(j, (int)cash[i].size()); k++) {
dp[(i + 1) % 2][j] = max(
dp[(i + 1) % 2][j], dp[i % 2][j - k] + total[i][k] + k * (k - 1));
}
}
}
cout << dp[10 % 2][K] << endl;
}
return 0;
} | insert | 40 | 40 | 40 | 41 | 0 | |
p00485 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define sec second
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
int n, m, k;
int cost[3000][3000];
int cnt[3000];
vector<P> e[3000];
bool shop[3000];
int ans;
void dijkstra(int s) {
rep(i, n) cost[s][i] = INF;
if (!shop[s])
return;
cost[s][s] = 0;
queue<P> que;
que.push(P(s, 0));
while (true) {
if (que.size() == 0)
break;
P p = que.front();
que.pop();
if (p.sec > cost[s][p.fi])
continue;
rep(i, e[p.fi].size()) {
if (cost[s][e[p.fi][i].fi] > cost[s][p.fi] + e[p.fi][i].sec) {
cost[s][e[p.fi][i].fi] = cost[s][p.fi] + e[p.fi][i].sec;
que.push(P(e[p.fi][i].fi, cost[s][e[p.fi][i].fi]));
}
}
}
}
int Abs(int x) { return x > 0 ? x : -x; }
int main() {
scanf("%d%d%d", &n, &m, &k);
rep(i, m) {
int a, b, l;
scanf("%d%d%d", &a, &b, &l);
a--;
b--;
e[a].push_back(P(b, l));
e[b].push_back(P(a, l));
}
rep(i, k) {
int s;
scanf("%d", &s);
s--;
shop[s] = true;
}
rep(i, n) dijkstra(i);
rep(i, n) cnt[i] = INF;
rep(i, n) {
rep(j, n) { cnt[i] = min(cnt[i], cost[j][i]); }
}
rep(i, n) ans = max(ans, cnt[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < e[i].size(); j++) {
int f = e[i][j].fi, s = e[i][j].sec;
ans = max(ans, max(cnt[i], cnt[f]) + (s - Abs(cnt[i] - cnt[f]) + 1) / 2);
}
}
printf("%d\n", ans);
} | #include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
#define fi first
#define sec second
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
int n, m, k;
int cost[3000][3000];
int cnt[3000];
vector<P> e[3000];
bool shop[3000];
int ans;
void dijkstra(int s) {
rep(i, n) cost[s][i] = INF;
if (!shop[s])
return;
cost[s][s] = 0;
queue<P> que;
que.push(P(s, 0));
while (true) {
if (que.size() == 0)
break;
P p = que.front();
que.pop();
if (p.sec > cost[s][p.fi])
continue;
rep(i, e[p.fi].size()) {
if (shop[e[p.fi][i].fi])
continue;
if (cost[s][e[p.fi][i].fi] > cost[s][p.fi] + e[p.fi][i].sec) {
cost[s][e[p.fi][i].fi] = cost[s][p.fi] + e[p.fi][i].sec;
que.push(P(e[p.fi][i].fi, cost[s][e[p.fi][i].fi]));
}
}
}
}
int Abs(int x) { return x > 0 ? x : -x; }
int main() {
scanf("%d%d%d", &n, &m, &k);
rep(i, m) {
int a, b, l;
scanf("%d%d%d", &a, &b, &l);
a--;
b--;
e[a].push_back(P(b, l));
e[b].push_back(P(a, l));
}
rep(i, k) {
int s;
scanf("%d", &s);
s--;
shop[s] = true;
}
rep(i, n) dijkstra(i);
rep(i, n) cnt[i] = INF;
rep(i, n) {
rep(j, n) { cnt[i] = min(cnt[i], cost[j][i]); }
}
rep(i, n) ans = max(ans, cnt[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < e[i].size(); j++) {
int f = e[i][j].fi, s = e[i][j].sec;
ans = max(ans, max(cnt[i], cnt[f]) + (s - Abs(cnt[i] - cnt[f]) + 1) / 2);
}
}
printf("%d\n", ans);
} | insert | 34 | 34 | 34 | 36 | TLE | |
p00485 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct TOWN {
bool kaku;
int kyo;
vector<pair<int, int>> road;
TOWN() : kaku(false) {}
} town[3001];
int main() {
int n, m, k, a, b, l, ina[3000], inb[3000], inl[3000], ans = 0;
priority_queue<pair<int, int>> pq;
pair<int, int> p;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &l);
town[a].road.push_back(make_pair(b, -l));
town[b].road.push_back(make_pair(a, -l));
ina[i] = a;
inb[i] = b;
inl[i] = l;
}
for (int i = 0; i < k; i++) {
scanf("%d", &a);
pq.push(make_pair(0, a));
}
while (!pq.empty()) {
p = pq.top();
pq.pop();
if (town[p.second].kaku)
continue;
town[p.second].kaku = true;
town[p.second].kyo = -p.first;
for (int i = 0; i < town[p.second].road.size(); i++)
pq.push(make_pair(p.first + town[p.second].road[i].second,
town[p.second].road[i].first));
}
for (int i = 0; i < m; i++) {
int sa = abs(town[ina[i]].kyo - town[inb[i]].kyo);
ans = max(ans, (max(inl[i] - sa, 0) + 1) / 2 +
max(town[ina[i]].kyo, town[inb[i]].kyo));
}
printf("%d\n", ans);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct TOWN {
bool kaku;
int kyo;
vector<pair<int, int>> road;
TOWN() : kaku(false) {}
} town[3001];
int main() {
int n, m, k, a, b, l, ina[100000], inb[100000], inl[100000], ans = 0;
priority_queue<pair<int, int>> pq;
pair<int, int> p;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &l);
town[a].road.push_back(make_pair(b, -l));
town[b].road.push_back(make_pair(a, -l));
ina[i] = a;
inb[i] = b;
inl[i] = l;
}
for (int i = 0; i < k; i++) {
scanf("%d", &a);
pq.push(make_pair(0, a));
}
while (!pq.empty()) {
p = pq.top();
pq.pop();
if (town[p.second].kaku)
continue;
town[p.second].kaku = true;
town[p.second].kyo = -p.first;
for (int i = 0; i < town[p.second].road.size(); i++)
pq.push(make_pair(p.first + town[p.second].road[i].second,
town[p.second].road[i].first));
}
for (int i = 0; i < m; i++) {
int sa = abs(town[ina[i]].kyo - town[inb[i]].kyo);
ans = max(ans, (max(inl[i] - sa, 0) + 1) / 2 +
max(town[ina[i]].kyo, town[inb[i]].kyo));
}
printf("%d\n", ans);
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p00485 | C++ | Runtime Error | // #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
const int INF = (1LL << 30);
const ll LLINF = (1LL << 60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
// #define int ll
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
struct edge {
int to, cost;
};
int n, d[3010], m, k, s[3010];
vector<edge> g[3010];
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> que;
fill(d, d + n, INF);
REP(i, k) {
d[s[i]] = 0;
que.push(PII{0, s[i]});
}
while (que.size()) {
PII p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (edge e : g[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(PII{d[e.to], e.to});
}
}
}
}
int a[3010], b[3010], c[3010];
signed main(void) {
cin >> n >> m >> k;
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
g[a[i]].PB({b[i], c[i]});
g[b[i]].PB({a[i], c[i]});
}
REP(i, k) cin >> s[i], s[i]--;
dijkstra();
// REP(i, n) cout << d[i] << " "; cout << endl;
int ret = 0;
REP(i, m)
chmax(ret,
(d[a[i]] + d[b[i]] + c[i]) / 2 + !!((d[a[i]] + d[b[i]] + c[i]) % 2));
cout << ret << endl;
return 0;
} | // #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
const int INF = (1LL << 30);
const ll LLINF = (1LL << 60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
// #define int ll
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
struct edge {
int to, cost;
};
int n, d[3010], m, k, s[3010];
vector<edge> g[3010];
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> que;
fill(d, d + n, INF);
REP(i, k) {
d[s[i]] = 0;
que.push(PII{0, s[i]});
}
while (que.size()) {
PII p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (edge e : g[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(PII{d[e.to], e.to});
}
}
}
}
int a[100010], b[100010], c[100010];
signed main(void) {
cin >> n >> m >> k;
REP(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
g[a[i]].PB({b[i], c[i]});
g[b[i]].PB({a[i], c[i]});
}
REP(i, k) cin >> s[i], s[i]--;
dijkstra();
// REP(i, n) cout << d[i] << " "; cout << endl;
int ret = 0;
REP(i, m)
chmax(ret,
(d[a[i]] + d[b[i]] + c[i]) / 2 + !!((d[a[i]] + d[b[i]] + c[i]) % 2));
cout << ret << endl;
return 0;
} | replace | 59 | 60 | 59 | 60 | 0 | |
p00485 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, M, K, A, B, L;
vector<vector<pair<int, int>>> G;
vector<int> P, D;
int main() {
scanf("%d", &N);
scanf("%d", &M);
scanf("%d", &K);
G = vector<vector<pair<int, int>>>(N);
for (int i = 0; i < M; i++) {
scanf("%d", &A);
scanf("%d", &B);
scanf("%d", &L);
G[A - 1].push_back(make_pair(B - 1, L));
G[B - 1].push_back(make_pair(A - 1, L));
}
P = vector<int>(K);
for (int i = 0; i < K; i++) {
scanf("%d", &P[i]);
P[i]--; // 0 - indexed
}
D = vector<int>(N, 999999999);
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
for (int i = 0; i < K; i++) {
que.push(make_pair(0, P[i]));
D[P[i]] = 0;
}
while (!que.empty()) {
pair<int, int> state = que.top();
que.pop();
int dist = state.first;
int node = state.second;
for (int j = 0; j < G[node].size(); j++) {
int node2 = G[node][j].first;
int leng2 = G[node][j].second;
if (D[node2] < D[node] + leng2) {
D[node2] = D[node] + leng2;
que.push(make_pair(D[node2], node2));
}
}
}
int ret = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < G[i].size(); j++) {
ret = max(ret, D[i] + D[G[i][j].first] + G[i][j].second);
}
}
printf("%d\n", (ret + 1) / 2);
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, M, K, A, B, L;
vector<vector<pair<int, int>>> G;
vector<int> P, D;
int main() {
scanf("%d", &N);
scanf("%d", &M);
scanf("%d", &K);
G = vector<vector<pair<int, int>>>(N);
for (int i = 0; i < M; i++) {
scanf("%d", &A);
scanf("%d", &B);
scanf("%d", &L);
G[A - 1].push_back(make_pair(B - 1, L));
G[B - 1].push_back(make_pair(A - 1, L));
}
P = vector<int>(K);
for (int i = 0; i < K; i++) {
scanf("%d", &P[i]);
P[i]--; // 0 - indexed
}
D = vector<int>(N, 999999999);
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
for (int i = 0; i < K; i++) {
que.push(make_pair(0, P[i]));
D[P[i]] = 0;
}
while (!que.empty()) {
pair<int, int> state = que.top();
que.pop();
int dist = state.first;
int node = state.second;
for (int j = 0; j < G[node].size(); j++) {
int node2 = G[node][j].first;
int leng2 = G[node][j].second;
if (D[node2] > D[node] + leng2) {
D[node2] = D[node] + leng2;
que.push(make_pair(D[node2], node2));
}
}
}
int ret = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < G[i].size(); j++) {
ret = max(ret, D[i] + D[G[i][j].first] + G[i][j].second);
}
}
printf("%d\n", (ret + 1) / 2);
return 0;
} | replace | 61 | 62 | 61 | 62 | MLE | |
p00486 | C++ | Memory Limit Exceeded | // JOI2010-2011本選問4歩くサンタクロース(Walking Santa)
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
long long sum = 0LL, osum = 0LL;
long long f1, f2, f3, w, h, L, HL, temp, oxy[2][2], *LXY[2], ox, oy, max = 0,
dx, dy;
vector<long long> XY[2];
freopen("in/18-02.in", "r", stdin);
scanf("%lld %lld", &w, &h);
scanf("%lld", &L);
for (f1 = 0; f1 < 2; f1++)
LXY[f1] = new long long[L];
// 100k
for (f1 = 0; f1 < L; f1++) {
scanf("%lld %lld", &LXY[0][f1], &LXY[1][f1]);
XY[0].push_back(LXY[0][f1]);
XY[1].push_back(LXY[1][f1]);
}
// 1m
stable_sort(XY[0].begin(), XY[0].end());
stable_sort(XY[1].begin(), XY[1].end());
HL = (L + 1) / 2;
// x軸での最小値の点、y軸での最小値の点を探す
// 200k
if (L == 1) {
printf("0\n");
printf("%lld %lld\n", LXY[0][0], LXY[1][0]);
} else {
for (f1 = 0; f1 < 2; f1++) {
oxy[0][f1] = XY[f1][HL - 1];
oxy[1][f1] = XY[f1][HL];
}
dx = (oxy[0][0] == oxy[1][0]) ? 1 : 2;
dy = (oxy[0][1] == oxy[1][1]) ? 1 : 2;
// 400k
for (f2 = 0; f2 < dx; f2++) {
for (f3 = 0; f3 < dy; f3++) {
sum = 0LL;
max = 0LL;
for (f1 = 0; f1 < L; f1++) {
temp = (long long)abs(LXY[0][f1] - oxy[f2][0]) +
abs(LXY[1][f1] - oxy[f3][1]);
sum += temp;
if (max < temp)
max = temp;
}
sum *= 2;
sum -= max;
if (osum == 0 || osum > sum) {
osum = sum;
ox = oxy[f2][0];
oy = oxy[f3][1];
}
}
}
printf("%lld\n", osum);
printf("%lld %lld\n", ox, oy);
}
return 0;
} | // JOI2010-2011本選問4歩くサンタクロース(Walking Santa)
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
long long sum = 0LL, osum = 0LL;
long long f1, f2, f3, w, h, L, HL, temp, oxy[2][2], *LXY[2], ox, oy, max = 0,
dx, dy;
vector<long long> XY[2];
scanf("%lld %lld", &w, &h);
scanf("%lld", &L);
for (f1 = 0; f1 < 2; f1++)
LXY[f1] = new long long[L];
// 100k
for (f1 = 0; f1 < L; f1++) {
scanf("%lld %lld", &LXY[0][f1], &LXY[1][f1]);
XY[0].push_back(LXY[0][f1]);
XY[1].push_back(LXY[1][f1]);
}
// 1m
stable_sort(XY[0].begin(), XY[0].end());
stable_sort(XY[1].begin(), XY[1].end());
HL = (L + 1) / 2;
// x軸での最小値の点、y軸での最小値の点を探す
// 200k
if (L == 1) {
printf("0\n");
printf("%lld %lld\n", LXY[0][0], LXY[1][0]);
} else {
for (f1 = 0; f1 < 2; f1++) {
oxy[0][f1] = XY[f1][HL - 1];
oxy[1][f1] = XY[f1][HL];
}
dx = (oxy[0][0] == oxy[1][0]) ? 1 : 2;
dy = (oxy[0][1] == oxy[1][1]) ? 1 : 2;
// 400k
for (f2 = 0; f2 < dx; f2++) {
for (f3 = 0; f3 < dy; f3++) {
sum = 0LL;
max = 0LL;
for (f1 = 0; f1 < L; f1++) {
temp = (long long)abs(LXY[0][f1] - oxy[f2][0]) +
abs(LXY[1][f1] - oxy[f3][1]);
sum += temp;
if (max < temp)
max = temp;
}
sum *= 2;
sum -= max;
if (osum == 0 || osum > sum) {
osum = sum;
ox = oxy[f2][0];
oy = oxy[f3][1];
}
}
}
printf("%lld\n", osum);
printf("%lld %lld\n", ox, oy);
}
return 0;
} | delete | 12 | 13 | 12 | 12 | MLE | |
p00486 | C++ | Runtime Error | #include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
int md = 0, i;
long long ans = 0;
for (i = 0; i < n; i++) {
int d = abs(ox[i] - a) + abs(oy[i] - b);
md = max(md, d);
ans += (long long)d * 2;
}
return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
int w, h, i, j;
scanf("%d %d", &w, &h);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &ox[i], &oy[i]);
x[i] = ox[i];
y[i] = oy[i];
}
sort(x, x + n);
sort(y, y + n);
long long ans = 1000000000000000000LL;
int ax[2];
int ay[2];
ax[0] = x[n / 2];
ay[0] = y[n / 2];
int nx, ny;
if (n % 2 == 0) {
ax[1] = x[n / 2 - 1];
ay[1] = y[n / 2 - 1];
for (i = 1; i >= 0; i++)
for (j = 1; j >= 0; j++) {
if (ans > calc(ax[i], ay[j])) {
ans = calc(ax[i], ay[j]);
nx = ax[i];
ny = ay[j];
}
}
} else {
ans = calc(ax[0], ay[0]);
nx = ax[0];
ny = ay[0];
}
printf("%lld\n", ans);
printf("%d %d\n", nx, ny);
return 0;
} | #include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
int md = 0, i;
long long ans = 0;
for (i = 0; i < n; i++) {
int d = abs(ox[i] - a) + abs(oy[i] - b);
md = max(md, d);
ans += (long long)d * 2;
}
return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
int w, h, i, j;
scanf("%d %d", &w, &h);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &ox[i], &oy[i]);
x[i] = ox[i];
y[i] = oy[i];
}
sort(x, x + n);
sort(y, y + n);
long long ans = 1000000000000000000LL;
int ax[2];
int ay[2];
ax[0] = x[n / 2];
ay[0] = y[n / 2];
int nx, ny;
if (n % 2 == 0) {
ax[1] = x[n / 2 - 1];
ay[1] = y[n / 2 - 1];
for (i = 1; i >= 0; i--)
for (j = 1; j >= 0; j--) {
if (ans > calc(ax[i], ay[j])) {
ans = calc(ax[i], ay[j]);
nx = ax[i];
ny = ay[j];
}
}
} else {
ans = calc(ax[0], ay[0]);
nx = ax[0];
ny = ay[0];
}
printf("%lld\n", ans);
printf("%d %d\n", nx, ny);
return 0;
} | replace | 40 | 42 | 40 | 42 | 0 | |
p00486 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
EACH(x, vec) is >> x;
return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
if (i) os << " ";
os << get<0>(t);
}
return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
REP(i, vec.size()) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
REP(i, vec.size()) {
if (i)
os << endl;
os << vec[i];
}
return os;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int W, H;
cin >> W >> H;
int N;
cin >> N;
vector<int> x(N), y(N);
REP(i, N) { cin >> x[i] >> y[i]; }
int xl = 0, xr = W;
vector<int> X = x, Y = y;
sort(ALL(X));
sort(ALL(Y));
int idx = (N - 1) / 2;
ll ans = linf;
int ax = inf, ay = inf;
vector<int> kx, ky;
for (int d = -1000; d <= 1000; ++d) {
if (idx + d < 0 || idx + d >= N)
continue;
kx.pb(X[idx + d]);
ky.pb(Y[idx + d]);
}
REP(u, kx.size()) REP(w, ky.size()) {
int sx = kx[u], sy = ky[w];
vector<ll> a;
REP(i, N) { a.pb(abs(x[i] - sx) + abs(y[i] - sy)); }
sort(ALL(a));
ll sum = a[N - 1];
REP(i, N - 1) { sum += a[i] * 2; }
if (sum < ans || (sum == ans && P(sx, sy) < P(ax, ay))) {
ans = sum;
tie(ax, ay) = P(sx, sy);
}
}
cout << ans << endl;
cout << ax << " " << ay << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
EACH(x, vec) is >> x;
return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
if (i) os << " ";
os << get<0>(t);
}
return os;
}
*/
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
REP(i, vec.size()) {
if (i)
os << " ";
os << vec[i];
}
return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
REP(i, vec.size()) {
if (i)
os << endl;
os << vec[i];
}
return os;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int W, H;
cin >> W >> H;
int N;
cin >> N;
vector<int> x(N), y(N);
REP(i, N) { cin >> x[i] >> y[i]; }
int xl = 0, xr = W;
vector<int> X = x, Y = y;
sort(ALL(X));
sort(ALL(Y));
int idx = (N - 1) / 2;
ll ans = linf;
int ax = inf, ay = inf;
vector<int> kx, ky;
for (int d = -10; d <= 10; ++d) {
if (idx + d < 0 || idx + d >= N)
continue;
kx.pb(X[idx + d]);
ky.pb(Y[idx + d]);
}
REP(u, kx.size()) REP(w, ky.size()) {
int sx = kx[u], sy = ky[w];
vector<ll> a;
REP(i, N) { a.pb(abs(x[i] - sx) + abs(y[i] - sy)); }
sort(ALL(a));
ll sum = a[N - 1];
REP(i, N - 1) { sum += a[i] * 2; }
if (sum < ans || (sum == ans && P(sx, sy) < P(ax, ay))) {
ans = sum;
tie(ax, ay) = P(sx, sy);
}
}
cout << ans << endl;
cout << ax << " " << ay << endl;
} | replace | 72 | 73 | 72 | 73 | TLE | |
p00486 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;
int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];
ll calc(ll X, ll Y) {
ll ma = 0LL;
ll tot = 0LL;
FOR(i, n) {
ll d = labs(X - x[i]) + labs(Y - y[i]);
ma = max(ma, d);
tot += d;
}
return 2 * tot - ma;
}
void solve() {
cin >> h >> w;
cin >> n;
FOR(i, n) cin >> x[i] >> y[i];
FOR(i, n) cx[i] = x[i];
FOR(i, n) cy[i] = y[i];
sort(x, cx + n);
sort(y, cy + n);
ll mx[2], my[2];
mx[0] = cx[(n - 1) >> 1];
mx[1] = cx[n >> 1];
my[0] = cy[(n - 1) >> 1];
my[1] = cy[n >> 1];
ll mi = LONG_MAX / 4, miX, miY;
FOR(i, 2) {
FOR(j, 2) {
ll ret = calc(mx[i], my[j]);
if (ret < mi) {
mi = ret;
miX = mx[i];
miY = my[j];
}
}
}
cout << mi << endl;
cout << miX << " " << miY << endl;
}
int main() {
solve();
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;
int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];
ll calc(ll X, ll Y) {
ll ma = 0LL;
ll tot = 0LL;
FOR(i, n) {
ll d = labs(X - x[i]) + labs(Y - y[i]);
ma = max(ma, d);
tot += d;
}
return 2 * tot - ma;
}
void solve() {
cin >> h >> w;
cin >> n;
FOR(i, n) cin >> x[i] >> y[i];
FOR(i, n) cx[i] = x[i];
FOR(i, n) cy[i] = y[i];
sort(cx, cx + n);
sort(cy, cy + n);
ll mx[2], my[2];
mx[0] = cx[(n - 1) >> 1];
mx[1] = cx[n >> 1];
my[0] = cy[(n - 1) >> 1];
my[1] = cy[n >> 1];
ll mi = LONG_MAX / 4, miX, miY;
FOR(i, 2) {
FOR(j, 2) {
ll ret = calc(mx[i], my[j]);
if (ret < mi) {
mi = ret;
miX = mx[i];
miY = my[j];
}
}
}
cout << mi << endl;
cout << miX << " " << miY << endl;
}
int main() {
solve();
return 0;
} | replace | 35 | 37 | 35 | 37 | 0 | |
p00486 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)
#define all(c) begin(c), end(c)
template <typename T> bool maxup(T &a, const T &&b) {
if (a < b) {
a = b;
return true;
};
}
template <typename T> bool maxup(T &a, const T &b) {
if (a < b) {
a = b;
return true;
};
}
template <typename T> bool minup(T &a, const T &&b) {
if (a > b) {
a = b;
return true;
};
}
template <typename T> bool minup(T &a, const T &b) {
if (a > b) {
a = b;
return true;
};
}
#define X first
#define Y second
using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;
template <typename T> inline void input(vector<T> &v) {
for (auto &x : v)
cin >> x;
}
signed main() {
int W, H, N;
cin >> W >> H >> N;
V ws(N), hs(N);
vector<P> v(N);
rep(i, N) {
int x, y;
cin >> x >> y;
ws[i] = x;
hs[i] = y;
v[i] = P(x, y);
}
sort(all(ws));
sort(all(hs));
int xi = N / 2;
int yi = N / 2;
VV dists(9, V(N));
rep(i, N) rep(j, 3) rep(k, 3) {
dists[j * 3 + k][i] =
abs(v[i].first - ws[xi + j - 1]) + abs(v[i].second - hs[yi + k - 1]);
}
rep(i, 9) sort(all(dists[i]));
int ans = 1ll << 62;
int xpos = 0, ypos = 0;
for (int i = 0; i < 9; i++) {
int sum = accumulate(all(dists[i]), 0ll) * 2 - dists[i].back();
if (sum < ans) {
xpos = ws[xi + i / 3 - 1];
ypos = hs[yi + i % 3 - 1];
ans = sum;
}
}
cout << ans << endl;
cout << xpos << " " << ypos << endl;
system("pause");
} | #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, x, ...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)
#define all(c) begin(c), end(c)
template <typename T> bool maxup(T &a, const T &&b) {
if (a < b) {
a = b;
return true;
};
}
template <typename T> bool maxup(T &a, const T &b) {
if (a < b) {
a = b;
return true;
};
}
template <typename T> bool minup(T &a, const T &&b) {
if (a > b) {
a = b;
return true;
};
}
template <typename T> bool minup(T &a, const T &b) {
if (a > b) {
a = b;
return true;
};
}
#define X first
#define Y second
using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;
template <typename T> inline void input(vector<T> &v) {
for (auto &x : v)
cin >> x;
}
signed main() {
int W, H, N;
cin >> W >> H >> N;
V ws(N), hs(N);
vector<P> v(N);
rep(i, N) {
int x, y;
cin >> x >> y;
ws[i] = x;
hs[i] = y;
v[i] = P(x, y);
}
sort(all(ws));
sort(all(hs));
int xi = N / 2;
int yi = N / 2;
VV dists(9, V(N));
rep(i, N) rep(j, 3) rep(k, 3) {
dists[j * 3 + k][i] =
abs(v[i].first - ws[xi + j - 1]) + abs(v[i].second - hs[yi + k - 1]);
}
rep(i, 9) sort(all(dists[i]));
int ans = 1ll << 62;
int xpos = 0, ypos = 0;
for (int i = 0; i < 9; i++) {
int sum = accumulate(all(dists[i]), 0ll) * 2 - dists[i].back();
if (sum < ans) {
xpos = ws[xi + i / 3 - 1];
ypos = hs[yi + i % 3 - 1];
ans = sum;
}
}
cout << ans << endl;
cout << xpos << " " << ypos << endl;
// system("pause");
} | replace | 91 | 92 | 91 | 92 | 0 | sh: 1: pause: not found
|
p00487 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int N, n;
pair<int, int> p[300000];
bool ok(int t) {
priority_queue<int> Q;
ll sum = 0;
for (int i = n - 1; i >= n - t + 1; i--) {
Q.push(p[i].second);
sum += p[i].second;
}
if (p[n - t].first * (ll)t >= (ll)sum + p[n - t].second)
return 1;
for (int i = n - t; i > 0; i--) {
if (Q.top() > p[i].second) {
sum -= Q.top() - p[i].second;
Q.pop();
Q.push(p[i].second);
}
if (p[i - 1].first * (ll)t >= (ll)sum + p[i - 1].second)
return 1;
}
return 0;
}
int main() {
scanf("%d", &n);
rep(i, n) {
int a, b;
scanf("%d%d", &a, &b);
p[i] = {b, a};
}
sort(p, p + n);
int a = 0, b = n;
while (a != b) {
int t = (a + b) / 2;
if (b - a == 1)
t = b;
if (ok(t))
a = t;
else
b = t - 1;
}
printf("%d\n", a);
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int N, n;
pair<int, int> p[300000];
bool ok(int t) {
priority_queue<int> Q;
ll sum = 0;
for (int i = n - 1; i >= n - t + 1; i--) {
Q.push(p[i].second);
sum += p[i].second;
}
if (p[n - t].first * (ll)t >= (ll)sum + p[n - t].second)
return 1;
for (int i = n - t; i > 0; i--) {
if (!Q.empty() && Q.top() > p[i].second) {
sum -= Q.top() - p[i].second;
Q.pop();
Q.push(p[i].second);
}
if (p[i - 1].first * (ll)t >= (ll)sum + p[i - 1].second)
return 1;
}
return 0;
}
int main() {
scanf("%d", &n);
rep(i, n) {
int a, b;
scanf("%d%d", &a, &b);
p[i] = {b, a};
}
sort(p, p + n);
int a = 0, b = n;
while (a != b) {
int t = (a + b) / 2;
if (b - a == 1)
t = b;
if (ok(t))
a = t;
else
b = t - 1;
}
printf("%d\n", a);
} | replace | 17 | 18 | 17 | 18 | 0 | |
p00487 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
struct segtree {
vector<long long> dat;
int n;
segtree(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.resize(2 * n, 0);
}
void update(int i, int x) {
i += n - 1;
int diff = x - dat[i];
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] += diff;
}
}
long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r == -1)
r = n;
if (r <= a || l >= b)
return 0;
if (a <= l && r <= b) {
return dat[k];
}
long long lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
long long rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
return lv + rv;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<int> a(N), b(N);
segtree P(N), Q(N);
vector<pair<int, int>> A, B;
for (int i = 0; i < N; ++i) {
cin >> a[i] >> b[i];
A.push_back(make_pair(a[i], b[i]));
}
sort(begin(A), end(A));
for (int i = 0; i < N; ++i) {
B.push_back(make_pair(A[i].second, i));
}
sort(begin(B), end(B));
for (int i = 0; i < N; ++i) {
P.update(i, A[i].first);
Q.update(i, 1);
}
long long ma = 0;
for (pair<int, int> &d : B) {
int l = 0, r = N;
long long PP, QQ;
for (int j = 0; j < 20; ++j) {
int mid = (l + r) / 2;
PP = P.query(0, mid + 1);
QQ = Q.query(0, mid + 1);
if (PP <= d.first * QQ) {
l = mid;
} else {
r = mid;
}
}
if (QQ == 0) {
return 1;
}
if (PP <= d.first * QQ) {
ma = max(ma, QQ);
}
P.update(d.second, 0);
Q.update(d.second, 0);
}
cout << ma << endl;
} | #include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
struct segtree {
vector<long long> dat;
int n;
segtree(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.resize(2 * n, 0);
}
void update(int i, int x) {
i += n - 1;
int diff = x - dat[i];
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] += diff;
}
}
long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r == -1)
r = n;
if (r <= a || l >= b)
return 0;
if (a <= l && r <= b) {
return dat[k];
}
long long lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
long long rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
return lv + rv;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<int> a(N), b(N);
segtree P(N), Q(N);
vector<pair<int, int>> A, B;
for (int i = 0; i < N; ++i) {
cin >> a[i] >> b[i];
A.push_back(make_pair(a[i], b[i]));
}
sort(begin(A), end(A));
for (int i = 0; i < N; ++i) {
B.push_back(make_pair(A[i].second, i));
}
sort(begin(B), end(B));
for (int i = 0; i < N; ++i) {
P.update(i, A[i].first);
Q.update(i, 1);
}
long long ma = 0;
for (pair<int, int> &d : B) {
int l = 0, r = N;
long long PP, QQ;
for (int j = 0; j < 20; ++j) {
int mid = (l + r) / 2;
PP = P.query(0, mid + 1);
QQ = Q.query(0, mid + 1);
if (PP <= d.first * QQ) {
l = mid;
} else {
r = mid;
}
}
if (PP <= d.first * QQ) {
ma = max(ma, QQ);
}
P.update(d.second, 0);
Q.update(d.second, 0);
}
cout << ma << endl;
} | delete | 98 | 102 | 98 | 98 | 1 | |
p00487 | C++ | Runtime Error | #include <bits/stdc++.h>
#define filein(t) freopen(t, "r", stdin)
#define fileout(t) freopen(t, "w", stdout)
#define f first
#define s second
#define cs 137
#define ll long long
#define mp make_pair
#define N 100025
#define ln 1e16
#define oo 111539786
#define Pa pair<int, int>
#define Pi pair<int, Pa>
using namespace std;
Pa a[N];
priority_queue<int> que;
int n;
bool Check(int X) {
while (!que.empty())
que.pop();
ll S = 0;
for (int i = n; i > n - X; i--) {
que.push(a[i].s);
S += a[i].s;
}
if (S <= (ll)X * a[n - X + 1].f)
return true;
for (int i = n - X; i >= 1; i--) {
int Max = que.top();
if (Max > a[i].s) {
S = S - Max + a[i].s;
que.pop();
que.push(a[i].s);
}
if (S <= (ll)X * a[i].f)
return true;
}
return false;
}
int main() {
// filein("MICROORGANISM.inp"); fileout("MICROORGANISM.out");
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].s >> a[i].f;
sort(a + 1, a + n + 1);
int L = 0;
int R = n + 1;
while (R - L > 1) {
int X = (L + R) / 2;
if (Check(X))
L = X;
else
R = X;
}
cout << L << endl;
} | #include <bits/stdc++.h>
#define filein(t) freopen(t, "r", stdin)
#define fileout(t) freopen(t, "w", stdout)
#define f first
#define s second
#define cs 137
#define ll long long
#define mp make_pair
#define N 300025
#define ln 1e16
#define oo 111539786
#define Pa pair<int, int>
#define Pi pair<int, Pa>
using namespace std;
Pa a[N];
priority_queue<int> que;
int n;
bool Check(int X) {
while (!que.empty())
que.pop();
ll S = 0;
for (int i = n; i > n - X; i--) {
que.push(a[i].s);
S += a[i].s;
}
if (S <= (ll)X * a[n - X + 1].f)
return true;
for (int i = n - X; i >= 1; i--) {
int Max = que.top();
if (Max > a[i].s) {
S = S - Max + a[i].s;
que.pop();
que.push(a[i].s);
}
if (S <= (ll)X * a[i].f)
return true;
}
return false;
}
int main() {
// filein("MICROORGANISM.inp"); fileout("MICROORGANISM.out");
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].s >> a[i].f;
sort(a + 1, a + n + 1);
int L = 0;
int R = n + 1;
while (R - L > 1) {
int X = (L + R) / 2;
if (Check(X))
L = X;
else
R = X;
}
cout << L << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00489 | C++ | Runtime Error | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int A[101], B[101], C[101], D[101];
int N, M;
int S[101] = {};
int main() {
cin >> N;
for (int i = 0; i < (N * (N - 1) / 2); i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
if (C[i] > D[i])
S[A[i]] += 3;
if (C[i] < D[i])
S[B[i]] += 3;
if (C[i] == D[i])
S[A[i]] += 1, S[B[i]] += 1;
}
int T = 0;
for (int i = 1; i < N + 1; i++) {
T = 1;
for (int j = 1; j < N + 1; j++) {
if (S[i] < S[j])
T += 1;
}
cout << T << endl;
}
return 0;
} | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int A[5001], B[5001], C[5001], D[5001];
int N, M;
int S[101] = {};
int main() {
cin >> N;
for (int i = 0; i < (N * (N - 1) / 2); i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
if (C[i] > D[i])
S[A[i]] += 3;
if (C[i] < D[i])
S[B[i]] += 3;
if (C[i] == D[i])
S[A[i]] += 1, S[B[i]] += 1;
}
int T = 0;
for (int i = 1; i < N + 1; i++) {
T = 1;
for (int j = 1; j < N + 1; j++) {
if (S[i] < S[j])
T += 1;
}
cout << T << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00489 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
int k, n, a[101], b[101], c[101], d[101], t[101] = {0};
int cnt;
cin >> n;
k = n * (n - 1) / 2;
for (int i = 0; i < k; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
for (int i = 0; i < k; i++) {
if (c[i] > d[i])
t[a[i]] += 3;
else if (c[i] < d[i])
t[b[i]] += 3;
else {
t[a[i]] += 1;
t[b[i]] += 1;
}
}
for (int i = 1; i <= n; i++) {
cnt = 1;
for (int j = 1; j <= n; j++) {
if (t[i] < t[j])
cnt++;
}
cout << cnt << endl;
}
return 0;
}
| #include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
int k, n, a[5001], b[5001], c[5001], d[5001], t[5001] = {0};
int cnt;
cin >> n;
k = n * (n - 1) / 2;
for (int i = 0; i < k; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
for (int i = 0; i < k; i++) {
if (c[i] > d[i])
t[a[i]] += 3;
else if (c[i] < d[i])
t[b[i]] += 3;
else {
t[a[i]] += 1;
t[b[i]] += 1;
}
}
for (int i = 1; i <= n; i++) {
cnt = 1;
for (int j = 1; j <= n; j++) {
if (t[i] < t[j])
cnt++;
}
cout << cnt << endl;
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p00489 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[100], cop[100], rank[100];
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (c == d) {
s[a - 1]++;
s[b - 1]++;
} else {
(c > d ? s[a - 1] : s[b - 1]) += 3;
}
}
for (int i = 0; i < n; i++)
cop[i] = s[i];
sort(cop, cop + n);
reverse(cop, cop + n);
for (int i = n - 1; i >= 0; i--) {
rank[cop[i]] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << rank[s[i]] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, s[101], cop[101], rank[10101];
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (c == d) {
s[a - 1]++;
s[b - 1]++;
} else {
(c > d ? s[a - 1] : s[b - 1]) += 3;
}
}
for (int i = 0; i < n; i++)
cop[i] = s[i];
sort(cop, cop + n);
reverse(cop, cop + n);
for (int i = n - 1; i >= 0; i--) {
rank[cop[i]] = i + 1;
}
for (int i = 0; i < n; i++) {
cout << rank[s[i]] << endl;
}
} | replace | 4 | 5 | 4 | 5 | -11 | |
p00489 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n;
int score[100] = {0}, a, b, c, d;
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
cin >> a >> b >> c >> d;
if (c > d) {
score[a - 1] += 3;
} else if (c < d) {
score[b - 1] += 3;
} else {
score[a - 1] += 1;
score[b - 1] += 1;
}
}
int cnt;
for (int i = 0; i < n; i++) {
cnt = 0;
for (int j = 0; j < n * (n - 1) / 2; j++) {
if (score[j] > score[i])
cnt++;
}
cout << cnt + 1 << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n;
int score[10000] = {0}, a, b, c, d;
cin >> n;
for (int i = 0; i < n * (n - 1) / 2; i++) {
cin >> a >> b >> c >> d;
if (c > d) {
score[a - 1] += 3;
} else if (c < d) {
score[b - 1] += 3;
} else {
score[a - 1] += 1;
score[b - 1] += 1;
}
}
int cnt;
for (int i = 0; i < n; i++) {
cnt = 0;
for (int j = 0; j < n * (n - 1) / 2; j++) {
if (score[j] > score[i])
cnt++;
}
cout << cnt + 1 << endl;
}
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p00489 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
int n;
int a[105], b[105], c[105], d[105];
int t[11000], k[105];
cin >> n;
for (int i = 0; i < (n * (n - 1)) / 2; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
t[i] = 0;
}
for (int i = 0; i < (n * (n - 1)) / 2; i++) {
if (c[i] > d[i])
t[a[i]] += 3;
if (c[i] < d[i])
t[b[i]] += 3;
if (c[i] == d[i]) {
t[a[i]]++;
t[b[i]]++;
}
}
int j = 1;
int h = 0;
for (int g = n * 3; g >= 0; g--) {
for (int i = 1; i < n + 1; i++) {
if (g == t[i]) {
k[i] = j;
h++;
}
}
j += h;
h = 0;
}
for (int i = 1; i < n + 1; i++) {
cout << k[i] << endl;
}
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
int n;
int a[11000], b[11000], c[11000], d[11000];
int t[11000], k[11000];
cin >> n;
for (int i = 0; i < (n * (n - 1)) / 2; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
t[i] = 0;
}
for (int i = 0; i < (n * (n - 1)) / 2; i++) {
if (c[i] > d[i])
t[a[i]] += 3;
if (c[i] < d[i])
t[b[i]] += 3;
if (c[i] == d[i]) {
t[a[i]]++;
t[b[i]]++;
}
}
int j = 1;
int h = 0;
for (int g = n * 3; g >= 0; g--) {
for (int i = 1; i < n + 1; i++) {
if (g == t[i]) {
k[i] = j;
h++;
}
}
j += h;
h = 0;
}
for (int i = 1; i < n + 1; i++) {
cout << k[i] << endl;
}
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p00490 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n;
int a, b;
int c;
int s[100];
int x, y;
cin >> n;
cin >> a >> b;
cin >> c;
for (int i = 1; i < n + 1; i++) {
cin >> s[i];
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (s[i] > s[j]) {
x = s[i];
s[i] = s[j];
s[j] = x;
}
}
}
x = c;
y = a;
for (int i = 1; i < n + 1; i++) {
if ((x / y) > ((x + s[i]) / (y + b)))
break;
else {
x += s[i];
y += b;
}
}
cout << x / y << endl;
return 0;
} | #include <iostream>
using namespace std;
int main() {
int n;
int a, b;
int c;
int s[101];
int x, y;
cin >> n;
cin >> a >> b;
cin >> c;
for (int i = 1; i < n + 1; i++) {
cin >> s[i];
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (s[i] > s[j]) {
x = s[i];
s[i] = s[j];
s[j] = x;
}
}
}
x = c;
y = a;
for (int i = 1; i < n + 1; i++) {
if ((x / y) > ((x + s[i]) / (y + b)))
break;
else {
x += s[i];
y += b;
}
}
cout << x / y << endl;
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p00490 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
int main() {
int n, a, b, base, top[100], cal[100];
std::cin >> n >> a >> b >> base;
for (int i = 0; i < n; i++)
std::cin >> top[i];
std::sort(top + 0, top + n);
std::reverse(top + 0, top + n);
for (int i = 0; i <= n; i++) {
int all_cal = base;
for (int j = 0; j < i; j++)
all_cal += top[j];
cal[i] = all_cal / (a + i * b);
}
std::sort(cal + 0, cal + n + 1);
std::reverse(cal + 0, cal + n + 1);
std::cout << cal[0] << std::endl;
return 0;
} | #include <algorithm>
#include <iostream>
int main() {
int n, a, b, base, top[101], cal[101];
std::cin >> n >> a >> b >> base;
for (int i = 0; i < n; i++)
std::cin >> top[i];
std::sort(top + 0, top + n);
std::reverse(top + 0, top + n);
for (int i = 0; i <= n; i++) {
int all_cal = base;
for (int j = 0; j < i; j++)
all_cal += top[j];
cal[i] = all_cal / (a + i * b);
}
std::sort(cal + 0, cal + n + 1);
std::reverse(cal + 0, cal + n + 1);
std::cout << cal[0] << std::endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00491 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[3][3][MAX_N];
int dfs(int a, int b, int depth) {
if (memo[a][b][depth])
return memo[a][b][depth];
if (depth > n)
return 1;
int res = 0;
if (plan[depth]) {
if (not(a == b && b == plan[depth]))
res += dfs(b, plan[depth], depth + 1);
} else {
for (int c = 1; c <= 3; c++) {
if (a == b && b == c)
continue;
res += dfs(b, c, depth + 1);
}
}
return memo[a][b][depth] = res % 10000;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> k;
memset(memo, 0, sizeof(memo));
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
plan[a] = b;
}
cout << dfs(0, 0, 1) % 10000 << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[4][4][MAX_N];
int dfs(int a, int b, int depth) {
if (memo[a][b][depth])
return memo[a][b][depth];
if (depth > n)
return 1;
int res = 0;
if (plan[depth]) {
if (not(a == b && b == plan[depth]))
res += dfs(b, plan[depth], depth + 1);
} else {
for (int c = 1; c <= 3; c++) {
if (a == b && b == c)
continue;
res += dfs(b, c, depth + 1);
}
}
return memo[a][b][depth] = res % 10000;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> k;
memset(memo, 0, sizeof(memo));
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
plan[a] = b;
}
cout << dfs(0, 0, 1) % 10000 << endl;
return 0;
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p00491 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int N, K, num;
int day[101];
int a[101][4][3];
long long int dp(int d, int k, int n) {
if (n == 3)
return 0;
if (d == N)
return a[d][k][n] = 1;
long long int b = 0;
if (day[d + 1] != -1) {
if (day[d + 1] == k) {
b += dp(d + 1, day[d + 1], n + 1) % 10000;
} else {
b += dp(d + 1, day[d + 1], 1) % 10000;
}
} else {
for (int i = 1; i <= 3; i++) {
if (i == k) {
b += dp(d + 1, i, n + 1) % 10000;
} else {
b += dp(d + 1, i, 1) % 10000;
}
}
}
return a[d][k][n] = b % 10000;
}
int main() {
fill_n(**a, 101 * 4 * 3, -1);
fill_n(day, 101, -1);
cin >> N >> K;
for (int i = 0; i < K; i++) {
cin >> num;
cin >> day[num];
}
cout << dp(0, 0, 0) << endl;
} | #include <bits/stdc++.h>
using namespace std;
int N, K, num;
int day[101];
int a[101][4][3];
long long int dp(int d, int k, int n) {
if (n == 3)
return 0;
if (a[d][k][n] >= 0)
return a[d][k][n];
if (d == N)
return a[d][k][n] = 1;
long long int b = 0;
if (day[d + 1] != -1) {
if (day[d + 1] == k) {
b += dp(d + 1, day[d + 1], n + 1) % 10000;
} else {
b += dp(d + 1, day[d + 1], 1) % 10000;
}
} else {
for (int i = 1; i <= 3; i++) {
if (i == k) {
b += dp(d + 1, i, n + 1) % 10000;
} else {
b += dp(d + 1, i, 1) % 10000;
}
}
}
return a[d][k][n] = b % 10000;
}
int main() {
fill_n(**a, 101 * 4 * 3, -1);
fill_n(day, 101, -1);
cin >> N >> K;
for (int i = 0; i < K; i++) {
cin >> num;
cin >> day[num];
}
cout << dp(0, 0, 0) << endl;
} | insert | 11 | 11 | 11 | 14 | TLE | |
p00492 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair
typedef pair<int, int> PII;
int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};
inline bool check(int x, int y) { return x >= 0 && x < w && y >= 0 && y < h; }
int main() {
cin >> w >> h;
REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
w += 2;
h += 2;
queue<PII> q;
q.push(MP(0, 0));
while (!q.empty()) {
PII now = q.front();
q.pop();
int x = now.first, y = now.second;
maps[y][x] = 2;
REP(i, 6) {
int nx = x + dx[y % 2][i];
int ny = y + dy[i];
if (check(nx, ny) && maps[ny][nx] == 0)
q.push(MP(nx, ny));
}
}
int ans = 0;
FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
REP(k, 6) {
int nx = j + dx[i % 2][k];
int ny = i + dy[k];
if (check(nx, ny) && maps[ny][nx] == 2)
ans++;
}
}
cout << ans << endl;
return 0;
} | #include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair
typedef pair<int, int> PII;
int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};
inline bool check(int x, int y) { return x >= 0 && x < w && y >= 0 && y < h; }
int main() {
cin >> w >> h;
REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
w += 2;
h += 2;
queue<PII> q;
q.push(MP(0, 0));
while (!q.empty()) {
PII now = q.front();
q.pop();
int x = now.first, y = now.second;
if (maps[y][x] == 2)
continue;
maps[y][x] = 2;
REP(i, 6) {
int nx = x + dx[y % 2][i];
int ny = y + dy[i];
if (check(nx, ny) && maps[ny][nx] == 0)
q.push(MP(nx, ny));
}
}
int ans = 0;
FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
REP(k, 6) {
int nx = j + dx[i % 2][k];
int ny = i + dy[k];
if (check(nx, ny) && maps[ny][nx] == 2)
ans++;
}
}
cout << ans << endl;
return 0;
} | insert | 32 | 32 | 32 | 34 | 0 | |
p00493 | C++ | Runtime Error | #include <iostream>
#include <string>
#define int long long
#define MOD 10000
using namespace std;
int m;
int dp[501][500][10][4][2] = {}; // keta,mod,prevnum,UorDorNoorZero,limit
int calc(string s) {
for (int i = 0; i <= s.length(); i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
for (int l = 0; l < 4; l++) {
for (int o = 0; o < 2; o++)
dp[i][j][k][l][o] = 0;
}
}
}
}
dp[0][0][0][3][1] = 1;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
for (int l = 0; l < 10; l++) {
if (k < l) {
(dp[i + 1][(j * 10 + l) % m][l][0][0] +=
dp[i][j][k][1][0] + dp[i][j][k][2][0]) %= MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][0][0] +=
dp[i][j][k][1][1] + dp[i][j][k][2][1]) %= MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][0][1] +=
dp[i][j][k][1][1] + dp[i][j][k][2][1]) %= MOD;
} else if (k > l) {
(dp[i + 1][(j * 10 + l) % m][l][1][0] +=
dp[i][j][k][0][0] + dp[i][j][k][2][0]) %= MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][1][0] +=
dp[i][j][k][0][1] + dp[i][j][k][2][1]) %= MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][1][1] +=
dp[i][j][k][0][1] + dp[i][j][k][2][1]) %= MOD;
}
if (!k) {
if (!l)
(dp[i + 1][j][0][3][0] +=
dp[i][j][k][3][0] + dp[i][j][k][3][1]) %= MOD;
else {
(dp[i + 1][(j * 10 + l) % m][l][2][0] += dp[i][j][k][3][0]) %=
MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][2][0] += dp[i][j][k][3][1]) %=
MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][2][1] += dp[i][j][k][3][1]) %=
MOD;
}
}
}
}
}
}
int ans = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
ans = (ans + dp[s.length()][0][i][j][k]) % MOD;
}
}
}
return ans;
}
signed main() {
string a, b;
cin >> a >> b >> m;
for (int i = a.length() - 1; i >= 0; i--) {
if (a[i] != '0') {
a[i]--;
if (!i && a[i] == '0')
a = a.substr(1, a.length() - 1);
break;
}
a[i] = '9';
}
// cout << calc(b) << endl;
cout << (calc(b) - calc(a) + MOD) % MOD << endl;
return 0;
} | #include <iostream>
#include <string>
#define int long long
#define MOD 10000
using namespace std;
int m;
int dp[510][500][10][4][2] = {}; // keta,mod,prevnum,UorDorNoorZero,limit
int calc(string s) {
for (int i = 0; i <= s.length(); i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
for (int l = 0; l < 4; l++) {
for (int o = 0; o < 2; o++)
dp[i][j][k][l][o] = 0;
}
}
}
}
dp[0][0][0][3][1] = 1;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 10; k++) {
for (int l = 0; l < 10; l++) {
if (k < l) {
(dp[i + 1][(j * 10 + l) % m][l][0][0] +=
dp[i][j][k][1][0] + dp[i][j][k][2][0]) %= MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][0][0] +=
dp[i][j][k][1][1] + dp[i][j][k][2][1]) %= MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][0][1] +=
dp[i][j][k][1][1] + dp[i][j][k][2][1]) %= MOD;
} else if (k > l) {
(dp[i + 1][(j * 10 + l) % m][l][1][0] +=
dp[i][j][k][0][0] + dp[i][j][k][2][0]) %= MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][1][0] +=
dp[i][j][k][0][1] + dp[i][j][k][2][1]) %= MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][1][1] +=
dp[i][j][k][0][1] + dp[i][j][k][2][1]) %= MOD;
}
if (!k) {
if (!l)
(dp[i + 1][j][0][3][0] +=
dp[i][j][k][3][0] + dp[i][j][k][3][1]) %= MOD;
else {
(dp[i + 1][(j * 10 + l) % m][l][2][0] += dp[i][j][k][3][0]) %=
MOD;
if (l < s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][2][0] += dp[i][j][k][3][1]) %=
MOD;
else if (l == s[i] - '0')
(dp[i + 1][(j * 10 + l) % m][l][2][1] += dp[i][j][k][3][1]) %=
MOD;
}
}
}
}
}
}
int ans = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
ans = (ans + dp[s.length()][0][i][j][k]) % MOD;
}
}
}
return ans;
}
signed main() {
string a, b;
cin >> a >> b >> m;
for (int i = a.length() - 1; i >= 0; i--) {
if (a[i] != '0') {
a[i]--;
if (!i && a[i] == '0')
a = a.substr(1, a.length() - 1);
break;
}
a[i] = '9';
}
// cout << calc(b) << endl;
cout << (calc(b) - calc(a) + MOD) % MOD << endl;
return 0;
} | replace | 7 | 8 | 7 | 8 | -11 | |
p00493 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int mod = 10000;
string A, B, S;
int M;
int memo[1000][10][3][2][500];
int calc(int keta = 0, int mae = 0, int hoko = 0, int jiyuu = 0,
int amari = 0) {
if (keta == S.size()) {
return amari == 0;
}
if (~memo[keta][mae][hoko][jiyuu][amari])
return memo[keta][mae][hoko][jiyuu][amari];
int ret = 0;
for (int i = 0; i < 10; i++) {
if (jiyuu == 0 && S[keta] < i + '0')
break;
if ((hoko || mae) && mae == i)
continue;
int njiyuu = jiyuu | (i + '0' != S[keta]);
int namari = (amari * 10 + i) % M;
if (hoko == 0) {
if (mae)
ret = (ret + calc(keta + 1, i, mae < i ? 2 : 1, njiyuu, namari)) % mod;
else
ret = (ret + calc(keta + 1, i, 0, njiyuu, namari)) % mod;
} else if (hoko == 1 && mae < i) {
ret = (ret + calc(keta + 1, i, 2, njiyuu, namari)) % mod;
} else if (hoko == 2 && mae > i) {
ret = (ret + calc(keta + 1, i, 1, njiyuu, namari)) % mod;
}
}
return memo[keta][mae][hoko][jiyuu][amari] = ret;
}
int main() {
cin >> A >> B >> M;
A[A.size() - 1]--;
for (int i = A.size() - 1; i > 0; i--) {
if (A[i] >= '0')
break;
A[i] = '9';
A[i - 1]--;
}
S = B;
memset(memo, -1, sizeof(memo));
int vb = calc();
S = A;
memset(memo, -1, sizeof(memo));
int va = calc();
cout << (vb - va + mod) % mod << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int mod = 10000;
string A, B, S;
int M;
int memo[501][10][3][2][500];
int calc(int keta = 0, int mae = 0, int hoko = 0, int jiyuu = 0,
int amari = 0) {
if (keta == S.size()) {
return amari == 0;
}
if (~memo[keta][mae][hoko][jiyuu][amari])
return memo[keta][mae][hoko][jiyuu][amari];
int ret = 0;
for (int i = 0; i < 10; i++) {
if (jiyuu == 0 && S[keta] < i + '0')
break;
if ((hoko || mae) && mae == i)
continue;
int njiyuu = jiyuu | (i + '0' != S[keta]);
int namari = (amari * 10 + i) % M;
if (hoko == 0) {
if (mae)
ret = (ret + calc(keta + 1, i, mae < i ? 2 : 1, njiyuu, namari)) % mod;
else
ret = (ret + calc(keta + 1, i, 0, njiyuu, namari)) % mod;
} else if (hoko == 1 && mae < i) {
ret = (ret + calc(keta + 1, i, 2, njiyuu, namari)) % mod;
} else if (hoko == 2 && mae > i) {
ret = (ret + calc(keta + 1, i, 1, njiyuu, namari)) % mod;
}
}
return memo[keta][mae][hoko][jiyuu][amari] = ret;
}
int main() {
cin >> A >> B >> M;
A[A.size() - 1]--;
for (int i = A.size() - 1; i > 0; i--) {
if (A[i] >= '0')
break;
A[i] = '9';
A[i - 1]--;
}
S = B;
memset(memo, -1, sizeof(memo));
int vb = calc();
S = A;
memset(memo, -1, sizeof(memo));
int va = calc();
cout << (vb - va + mod) % mod << endl;
return 0;
} | replace | 6 | 7 | 6 | 7 | MLE | |
p00493 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define ll long long
#define fi first
#define se second
using namespace std;
const ll MOD = 10000;
string a, b;
int m;
ll dp[501][2][500][11][3][2];
/*inline ll solve(string s){
int n=(int)s.length();
for(int i=0;i<510;i++)for(int j=0;j<2;j++)for(int k=0;k<500;k++)for(int
l=0;l<11;l++)for(int o=0;o<3;o++)for(int h=0;h<2;h++)dp[i][j][k][l][o][h]=0;
dp[0][0][0][0][0][0]=1;
for(int i=0;i<n;i++)for(int j=0;j<2;j++)for(int k=0;k<m;k++)for(int
l=0;l<11;l++)for(int o=0;o<3;o++)for(int h=0;h<2;h++){ int lim=j?9:s[i]-'0';
for(int d=0;d<lim+1;d++){
if(l==0 && i==0){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][0][h || (l!=1 &&
l!=0)]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<o<<endl; continue;
}
if(o!=1){
if(d>l-1){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][(o==2?1:(h || l!=1)?1:0)][h ||
l!=1]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<1<<endl;
}
}
if(o!=2){
if(d<l-1){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][(o==1?2:(h || l!=1)?2:0)][h ||
l!=1]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<2<<endl;
}
}
}
}
ll res=0;
for(int i=0;i<2;i++)for(int j=0;j<500;j++)for(int k=1;k<11;k++)for(int
l=1;l<3;l++){ if(j==0) res+=dp[n][i][j][k][l][1]; if(dp[n][i][j][k][l][1]>0 &&
j==0)cout<<i<<" "<<j<<" "<<k<<" "<<l<<"="<<dp[n][i][j][k][l][1]<<endl;
}
return res;
}*/
inline ll solve(string s) {
int n = (int)s.length();
for (int i = 0; i < n + 1; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < m; k++)
for (int l = 0; l < 11; l++)
for (int mm = 0; mm < 3; mm++)
for (int o = 0; o < 2; o++)
dp[i][j][k][l][mm][o] = 0;
dp[0][0][0][0][0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < 11; l++) {
for (int mm = 0; mm < 3; mm++) {
for (int o = 0; o < 2; o++) {
int lim = j ? 9 : s[i] - '0';
int pre = l - 1;
for (int d = 0; d < lim + 1; d++) {
if (d == 0 && o == 0) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][0][0][0] +=
dp[i][j][k][l][mm][o]) %= MOD;
continue;
}
if (pre == -1) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][0][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
continue;
}
if (pre < d && mm != 1) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][1][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
}
if (pre > d && mm != 2) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][2][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
}
}
}
}
}
}
}
}
ll ans = 0;
for (int i = 0; i < 2; i++)
for (int j = 1; j < 11; j++)
for (int k = 0; k < 3; k++) {
(ans += dp[n][i][0][j][k][1]) %= MOD;
}
return ans;
}
inline string min1(string s) {
int n = (int)s.length();
for (int i = n - 1; i >= 0; i--) {
int now = s[i] - '0';
if (now - 1 < 0) {
s[i] = '9';
} else {
s[i] = '0' + now - 1;
break;
}
}
for (int i = 0; i < n; i++) {
if (s == "0")
break;
if (s[i] == '0')
s = s.substr(1, s.length() - 1);
else
break;
}
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
ll ansa, ansb;
cin >> a >> b >> m;
// while(cin>>a>>b>>m){
a = min1(a);
ansb = solve(b);
// cout<<b<<" ansb="<<ansb<<endl;
ansa = solve(a);
// cout<<a<<" ansa="<<ansa<<endl;
cout << (ansb + MOD - ansa) % MOD << "\n";
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#define ll long long
#define fi first
#define se second
using namespace std;
const ll MOD = 10000;
string a, b;
int m;
ll dp[502][2][500][11][3][2];
/*inline ll solve(string s){
int n=(int)s.length();
for(int i=0;i<510;i++)for(int j=0;j<2;j++)for(int k=0;k<500;k++)for(int
l=0;l<11;l++)for(int o=0;o<3;o++)for(int h=0;h<2;h++)dp[i][j][k][l][o][h]=0;
dp[0][0][0][0][0][0]=1;
for(int i=0;i<n;i++)for(int j=0;j<2;j++)for(int k=0;k<m;k++)for(int
l=0;l<11;l++)for(int o=0;o<3;o++)for(int h=0;h<2;h++){ int lim=j?9:s[i]-'0';
for(int d=0;d<lim+1;d++){
if(l==0 && i==0){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][0][h || (l!=1 &&
l!=0)]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<o<<endl; continue;
}
if(o!=1){
if(d>l-1){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][(o==2?1:(h || l!=1)?1:0)][h ||
l!=1]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<1<<endl;
}
}
if(o!=2){
if(d<l-1){
dp[i+1][j || d<lim][(10*k+d)%m][d+1][(o==1?2:(h || l!=1)?2:0)][h ||
l!=1]+=dp[i][j][k][l][o][h];
//cout<<i+1<<" "<<(int)(j || d<lim)<<" "<<(10*k+d)%m<<" "<<d+1<<"
"<<2<<endl;
}
}
}
}
ll res=0;
for(int i=0;i<2;i++)for(int j=0;j<500;j++)for(int k=1;k<11;k++)for(int
l=1;l<3;l++){ if(j==0) res+=dp[n][i][j][k][l][1]; if(dp[n][i][j][k][l][1]>0 &&
j==0)cout<<i<<" "<<j<<" "<<k<<" "<<l<<"="<<dp[n][i][j][k][l][1]<<endl;
}
return res;
}*/
inline ll solve(string s) {
int n = (int)s.length();
for (int i = 0; i < n + 1; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < m; k++)
for (int l = 0; l < 11; l++)
for (int mm = 0; mm < 3; mm++)
for (int o = 0; o < 2; o++)
dp[i][j][k][l][mm][o] = 0;
dp[0][0][0][0][0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < 11; l++) {
for (int mm = 0; mm < 3; mm++) {
for (int o = 0; o < 2; o++) {
int lim = j ? 9 : s[i] - '0';
int pre = l - 1;
for (int d = 0; d < lim + 1; d++) {
if (d == 0 && o == 0) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][0][0][0] +=
dp[i][j][k][l][mm][o]) %= MOD;
continue;
}
if (pre == -1) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][0][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
continue;
}
if (pre < d && mm != 1) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][1][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
}
if (pre > d && mm != 2) {
(dp[i + 1][j || d < lim][(10 * k + d) % m][d + 1][2][1] +=
dp[i][j][k][l][mm][o]) %= MOD;
}
}
}
}
}
}
}
}
ll ans = 0;
for (int i = 0; i < 2; i++)
for (int j = 1; j < 11; j++)
for (int k = 0; k < 3; k++) {
(ans += dp[n][i][0][j][k][1]) %= MOD;
}
return ans;
}
inline string min1(string s) {
int n = (int)s.length();
for (int i = n - 1; i >= 0; i--) {
int now = s[i] - '0';
if (now - 1 < 0) {
s[i] = '9';
} else {
s[i] = '0' + now - 1;
break;
}
}
for (int i = 0; i < n; i++) {
if (s == "0")
break;
if (s[i] == '0')
s = s.substr(1, s.length() - 1);
else
break;
}
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
ll ansa, ansb;
cin >> a >> b >> m;
// while(cin>>a>>b>>m){
a = min1(a);
ansb = solve(b);
// cout<<b<<" ansb="<<ansb<<endl;
ansa = solve(a);
// cout<<a<<" ansa="<<ansa<<endl;
cout << (ansb + MOD - ansa) % MOD << "\n";
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | replace | 27 | 28 | 27 | 28 | -11 | |
p00493 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int dp[501][2][10][3][500];
// dp[a][b][c][d][e]:a桁目 b1で自由に使える0で自由に使えない 今の桁はc
// d1で前より上がってる2で前より下がってる0で前と変わらない eは余り
int MOD = 10000;
int M;
string A, B;
// 1~nまでのジグザグ数%10000
int calc(string n) {
for (int i = 0; i <= 500; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 10; k++)
for (int l = 0; l < 3; l++)
for (int m = 0; m < 500; m++)
dp[i][j][k][l][m] = 0;
for (int i = 0; i < n[0] - '0'; i++)
dp[1][1][i][0][i % M] = 1;
dp[1][0][(n[0] - '0')][0][(n[0] - '0') % M] = 1;
for (int a = 1; a < n.size(); a++) {
for (int b = 0; b < 2; b++) {
int limit = b ? 9 : n[a] - '0';
for (int c = 0; c <= limit; c++) {
for (int d = 0; d <= (b ? 9 : n[a - 1] - '0'); d++) {
for (int e = 0; e < 3; e++) {
for (int f = 0; f < M; f++) {
if (e == 0) {
// 前と比べて増加も減少もしてない
if (d == 0) {
(dp[a + 1][b || c < limit][c][0][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
} else {
if (c < d) {
(dp[a + 1][b || c < limit][c][2][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
if (c > d) {
(dp[a + 1][b || c < limit][c][1][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
}
} else if (e == 1) {
// 今増加→次減少
if (c >= d)
continue;
(dp[a + 1][b || c < limit][c][2][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
} else if (e == 2) {
// 今減少→次増加
if (c <= d)
continue;
(dp[a + 1][b || c < limit][c][1][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
}
}
}
}
}
}
int ans = 0;
for (int b = 0; b < 2; b++)
for (int c = 0; c <= 9; c++)
for (int e = 0; e <= 2; e++)
ans += dp[n.size()][b][c][e][0];
ans--;
ans %= MOD;
return ans;
}
bool isZigZag(string n) {
bool f;
if (n.size() == 1)
return true;
if (n[0] == n[1])
return false;
if (n[0] > n[1])
f = true;
if (n[0] < n[1])
f = false;
for (int i = 1; i < n.size() - 1; i++) {
if (f) {
if (n[i + 1] <= n[i])
return false;
f = !f;
} else {
if (n[i + 1] >= n[i])
return false;
f = !f;
}
}
return true;
}
int main() {
cin >> A >> B >> M;
cout << (calc(B) - calc(A) + isZigZag(A) + MOD) % MOD << endl;
return 0;
}
| #include <iostream>
#include <string>
using namespace std;
int dp[501 + 10][2][10][3][500];
// dp[a][b][c][d][e]:a桁目 b1で自由に使える0で自由に使えない 今の桁はc
// d1で前より上がってる2で前より下がってる0で前と変わらない eは余り
int MOD = 10000;
int M;
string A, B;
// 1~nまでのジグザグ数%10000
int calc(string n) {
for (int i = 0; i <= 500; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 10; k++)
for (int l = 0; l < 3; l++)
for (int m = 0; m < 500; m++)
dp[i][j][k][l][m] = 0;
for (int i = 0; i < n[0] - '0'; i++)
dp[1][1][i][0][i % M] = 1;
dp[1][0][(n[0] - '0')][0][(n[0] - '0') % M] = 1;
for (int a = 1; a < n.size(); a++) {
for (int b = 0; b < 2; b++) {
int limit = b ? 9 : n[a] - '0';
for (int c = 0; c <= limit; c++) {
for (int d = 0; d <= (b ? 9 : n[a - 1] - '0'); d++) {
for (int e = 0; e < 3; e++) {
for (int f = 0; f < M; f++) {
if (e == 0) {
// 前と比べて増加も減少もしてない
if (d == 0) {
(dp[a + 1][b || c < limit][c][0][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
} else {
if (c < d) {
(dp[a + 1][b || c < limit][c][2][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
if (c > d) {
(dp[a + 1][b || c < limit][c][1][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
}
} else if (e == 1) {
// 今増加→次減少
if (c >= d)
continue;
(dp[a + 1][b || c < limit][c][2][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
} else if (e == 2) {
// 今減少→次増加
if (c <= d)
continue;
(dp[a + 1][b || c < limit][c][1][(f * 10 + c) % M] +=
dp[a][b][d][e][f]) %= MOD;
}
}
}
}
}
}
}
int ans = 0;
for (int b = 0; b < 2; b++)
for (int c = 0; c <= 9; c++)
for (int e = 0; e <= 2; e++)
ans += dp[n.size()][b][c][e][0];
ans--;
ans %= MOD;
return ans;
}
bool isZigZag(string n) {
bool f;
if (n.size() == 1)
return true;
if (n[0] == n[1])
return false;
if (n[0] > n[1])
f = true;
if (n[0] < n[1])
f = false;
for (int i = 1; i < n.size() - 1; i++) {
if (f) {
if (n[i + 1] <= n[i])
return false;
f = !f;
} else {
if (n[i + 1] >= n[i])
return false;
f = !f;
}
}
return true;
}
int main() {
cin >> A >> B >> M;
cout << (calc(B) - calc(A) + isZigZag(A) + MOD) % MOD << endl;
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p00493 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
const int MOD = 10000;
std::string a, b; // <=1e500
int m; // <=500
int dp[2][2][510][510][15][3];
char Ch(int i) { return '0' + i; }
int func(bool lower, bool upper, int mod, int idx, int prev,
int stat) { // stat: 0: zero, 1: ?¢???????, 2: ??????
if (idx >= a.size()) {
if (lower && upper && mod % m == 0) {
return true;
} else {
return false;
}
}
if (dp[lower][upper][mod][idx][prev][stat] != -1) {
return dp[lower][upper][mod][idx][prev][stat];
}
mod *= 10;
int ans = 0;
for (int i = 0; i <= 9; ++i) {
int nm = (mod + i) % m;
char c = '0' + i;
bool newLower;
bool newUpper;
int newStat;
if (lower) {
newLower = lower;
} else {
if (b[idx] < c) {
continue;
}
if (b[idx] == c && idx != a.size() - 1) {
newLower = false;
} else {
newLower = true;
}
}
if (upper) {
newUpper = true;
} else {
if (a[idx] > c) {
continue;
}
if (a[idx] == c && idx != a.size() - 1) {
newUpper = false;
} else {
newUpper = true;
}
}
if (stat == 0) {
if (prev == 0) {
newStat = 0;
} else if (prev > i) {
newStat = 1;
} else if (prev < i) {
newStat = 2;
} else {
continue;
}
} else if (stat == 1) {
if (i <= prev) {
continue;
}
newStat = 2;
} else {
if (i >= prev) {
continue;
}
newStat = 1;
}
ans += func(newLower, newUpper, nm, idx + 1, i, newStat);
ans %= MOD;
}
return dp[lower][upper][mod][idx][prev][stat] = ans;
}
int main() {
std::cin >> a >> b >> m;
while (a.size() < b.size()) {
a = std::string("0") + a;
}
memset(dp, -1, sizeof(dp));
std::cout << func(false, false, 0, 0, 0, 0) << std::endl;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
const int MOD = 10000;
std::string a, b; // <=1e500
int m; // <=500
int dp[2][2][510][510][15][3];
char Ch(int i) { return '0' + i; }
int func(bool lower, bool upper, int mod, int idx, int prev,
int stat) { // stat: 0: zero, 1: ?¢???????, 2: ??????
if (idx >= a.size()) {
if (lower && upper && mod % m == 0) {
return true;
} else {
return false;
}
}
if (dp[lower][upper][mod][idx][prev][stat] != -1) {
return dp[lower][upper][mod][idx][prev][stat];
}
mod *= 10;
int ans = 0;
for (int i = 0; i <= 9; ++i) {
int nm = (mod + i) % m;
char c = '0' + i;
bool newLower;
bool newUpper;
int newStat;
if (lower) {
newLower = lower;
} else {
if (b[idx] < c) {
continue;
}
if (b[idx] == c && idx != a.size() - 1) {
newLower = false;
} else {
newLower = true;
}
}
if (upper) {
newUpper = true;
} else {
if (a[idx] > c) {
continue;
}
if (a[idx] == c && idx != a.size() - 1) {
newUpper = false;
} else {
newUpper = true;
}
}
if (stat == 0) {
if (prev == 0) {
newStat = 0;
} else if (prev > i) {
newStat = 1;
} else if (prev < i) {
newStat = 2;
} else {
continue;
}
} else if (stat == 1) {
if (i <= prev) {
continue;
}
newStat = 2;
} else {
if (i >= prev) {
continue;
}
newStat = 1;
}
ans += func(newLower, newUpper, nm, idx + 1, i, newStat);
ans %= MOD;
}
return dp[lower][upper][mod / 10][idx][prev][stat] = ans;
}
int main() {
std::cin >> a >> b >> m;
while (a.size() < b.size()) {
a = std::string("0") + a;
}
memset(dp, -1, sizeof(dp));
std::cout << func(false, false, 0, 0, 0, 0) << std::endl;
} | replace | 93 | 94 | 93 | 94 | -11 | |
p00493 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
const int mod = 10000;
int dp[600][600][11][2][2];
string S, T;
int power[600], n;
int zen_shori(int Keta) {
for (int i = 0; i < 360000; i++) {
for (int j = 0; j < 10; j++) {
dp[i / 600][i % 600][j][0][0] = 0;
dp[i / 600][i % 600][j][0][1] = 0;
dp[i / 600][i % 600][j][1][0] = 0;
dp[i / 600][i % 600][j][1][1] = 0;
}
}
int F = 0;
for (int i = 1; i <= 9; i++) {
if (i % n == 0)
F++;
}
if (Keta == 1)
return F;
if (Keta == 0)
return 0;
dp[0][0][0][0][1] = 1;
dp[0][0][10][1][1] = 1;
for (int i = 0; i < Keta; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 11; k++) {
for (int l = 0; l < 2; l++) {
int L = 0, R = 9;
if (l == 0)
L = k + 1;
if (l == 1)
R = k - 1;
for (int m = 0; m <= 9; m++) {
int Plus = (power[i] * m) % n;
int to = (j + Plus) % n;
if (L <= m && m <= R) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][0];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (m >= 1) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (m == 0) {
dp[i + 1][to][m][1 - l][1] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][1] %= mod;
}
}
}
}
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
sum += dp[Keta][0][i][j][0];
}
}
sum -= F;
// cout << sum << endl;
return sum;
}
int solve(string V) {
for (int i = 0; i < 360000; i++) {
for (int j = 0; j < 10; j++) {
dp[i / 600][i % 600][j][0][0] = 0;
dp[i / 600][i % 600][j][0][1] = 0;
dp[i / 600][i % 600][j][1][0] = 0;
dp[i / 600][i % 600][j][1][1] = 0;
}
}
int F = 0;
for (int i = 1; i <= (int)(V[0] - '0'); i++) {
if (i % n == 0)
F++;
}
if (V.size() == 1)
return F;
dp[0][0][0][0][1] = 1;
dp[0][0][10][1][1] = 1;
for (int i = 0; i < (int)V.size(); i++) {
int P = V[i] - '0';
for (int j = 0; j < n; j++) {
for (int k = 0; k < 11; k++) {
for (int l = 0; l < 2; l++) {
if (dp[i][j][k][l][0] == 0 && dp[i][j][k][l][1] == 0)
continue;
int L = 0, R = 9;
if (l == 0)
L = k + 1;
if (l == 1)
R = k - 1;
for (int m = L; m <= R; m++) {
if (i == 0 && m == 0)
continue;
int Plus = (power[i] * m) % n;
int to = (j + Plus) % n;
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][0];
dp[i + 1][to][m][1 - l][0] %= mod;
if (P > m) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (P == m) {
dp[i + 1][to][m][1 - l][1] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][1] %= mod;
}
}
}
}
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
sum += dp[V.size()][0][i][j][0] + dp[V.size()][0][i][j][1];
}
}
// cout << sum << endl;
return sum;
}
int solve2(string V) {
int v = 1;
for (int i = V.size() - 1; i >= 0; i--) {
power[i] = v;
v *= 10;
v %= n;
}
int r1 = solve(V);
int r2 = zen_shori(V.size() - 1);
return (r2 + r1) % mod;
}
int main() {
cin >> S >> T >> n;
int ii = S.size() - 1;
while (S[ii] == '0') {
S[ii] = '9';
ii--;
}
S[ii]--;
while (S[0] == '0')
S = S.substr(1, S.size() - 1);
cout << (solve2(T) - solve2(S) + mod) % mod << endl;
return 0;
} | #include <iostream>
#include <string>
using namespace std;
const int mod = 10000;
int dp[600][600][11][2][2];
string S, T;
int power[600], n;
int zen_shori(int Keta) {
for (int i = 0; i < 360000; i++) {
for (int j = 0; j < 10; j++) {
dp[i / 600][i % 600][j][0][0] = 0;
dp[i / 600][i % 600][j][0][1] = 0;
dp[i / 600][i % 600][j][1][0] = 0;
dp[i / 600][i % 600][j][1][1] = 0;
}
}
int F = 0;
for (int i = 1; i <= 9; i++) {
if (i % n == 0)
F++;
}
if (Keta == 1)
return F;
if (Keta == 0)
return 0;
dp[0][0][0][0][1] = 1;
dp[0][0][10][1][1] = 1;
for (int i = 0; i < Keta; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < 11; k++) {
for (int l = 0; l < 2; l++) {
int L = 0, R = 9;
if (l == 0)
L = k + 1;
if (l == 1)
R = k - 1;
for (int m = 0; m <= 9; m++) {
int Plus = (power[i] * m) % n;
int to = (j + Plus) % n;
if (L <= m && m <= R) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][0];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (m >= 1) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (m == 0) {
dp[i + 1][to][m][1 - l][1] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][1] %= mod;
}
}
}
}
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
sum += dp[Keta][0][i][j][0];
}
}
sum -= F;
// cout << sum << endl;
return sum;
}
int solve(string V) {
for (int i = 0; i < 360000; i++) {
for (int j = 0; j < 10; j++) {
dp[i / 600][i % 600][j][0][0] = 0;
dp[i / 600][i % 600][j][0][1] = 0;
dp[i / 600][i % 600][j][1][0] = 0;
dp[i / 600][i % 600][j][1][1] = 0;
}
}
int F = 0;
for (int i = 1; i <= (int)(V[0] - '0'); i++) {
if (i % n == 0)
F++;
}
if (V.size() == 1)
return F;
dp[0][0][0][0][1] = 1;
dp[0][0][10][1][1] = 1;
for (int i = 0; i < (int)V.size(); i++) {
int P = V[i] - '0';
for (int j = 0; j < n; j++) {
for (int k = 0; k < 11; k++) {
for (int l = 0; l < 2; l++) {
if (dp[i][j][k][l][0] == 0 && dp[i][j][k][l][1] == 0)
continue;
int L = 0, R = 9;
if (l == 0)
L = k + 1;
if (l == 1)
R = k - 1;
for (int m = L; m <= R; m++) {
if (i == 0 && m == 0)
continue;
int Plus = (power[i] * m) % n;
int to = (j + Plus) % n;
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][0];
dp[i + 1][to][m][1 - l][0] %= mod;
if (P > m) {
dp[i + 1][to][m][1 - l][0] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][0] %= mod;
}
if (P == m) {
dp[i + 1][to][m][1 - l][1] += dp[i][j][k][l][1];
dp[i + 1][to][m][1 - l][1] %= mod;
}
}
}
}
}
}
int sum = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 2; j++) {
sum += dp[V.size()][0][i][j][0] + dp[V.size()][0][i][j][1];
}
}
// cout << sum << endl;
return sum;
}
int solve2(string V) {
int v = 1;
for (int i = V.size() - 1; i >= 0; i--) {
power[i] = v;
v *= 10;
v %= n;
}
int r1 = solve(V);
int r2 = zen_shori(V.size() - 1);
return (r2 + r1) % mod;
}
int main() {
cin >> S >> T >> n;
int ii = S.size() - 1;
while (S[ii] == '0') {
S[ii] = '9';
ii--;
}
S[ii]--;
while (S[0] == '0' && S.size() >= 2)
S = S.substr(1, S.size() - 1);
cout << (solve2(T) - solve2(S) + mod) % mod << endl;
return 0;
} | replace | 144 | 145 | 144 | 145 | 0 | |
p00493 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;
const int mod = 10000;
int dp[501][11][500][2][2];
int solve(string s, int D) {
int n = s.size();
for (int i = 0; i <= n; i++)
for (int j = 0; j <= 10; j++)
for (int k = 0; k < D; k++)
for (int l = 0; l < 2; l++)
for (int m = 0; m < 2; m++)
dp[i][j][k][l][m] = 0;
dp[0][10][0][0][0]++;
for (int i = 0; i < n; i++)
for (int j = 0; j <= 10; j++)
for (int k = 0; k < D; k++)
for (int l = 0; l < 2; l++)
for (int m = 0; m < 2; m++) {
if (j != 10) {
int S = m ? 0 : j + 1, T = m ? j - 1 : 9;
if (!l)
T = min(T, s[i] - '0');
for (int d = S; d < T + 1; d++)
(dp[i + 1][d][(10 * k + d) % D][l || s[i] - '0' != d /*d<T*/]
[!m] += dp[i][j][k][l][m]) %= mod;
} else if (!k && !l && !m) {
int lim = (i == 0) ? s[i] - '0' : 9;
for (int d = 1; d < lim + 1; d++)
for (int I = 0; I < 2; I++)
dp[i + 1][d][d % D][i || d < lim][I]++;
dp[i + 1][10][0][0][0]++;
}
}
int ans = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
(ans += dp[n][i][0][j][k]) %= mod;
}
int num = 9 / D;
if (s.size() == 1 && s[0] - '0' < num * D)
num = (s[0] - '0') / D;
return (ans + mod - num) % mod;
}
string Minus(string s) {
if (s.back() > '0')
s[s.size() - 1] -= 1;
else {
for (int i = s.size() - 1; i >= 0; i--)
if (s[i] > '0') {
s[i] -= 1;
for (int j = i + 1; j < s.size(); j++)
s[j] = '9';
break;
}
}
if (s == "0")
return s;
for (int i = 0; i < s.size(); i++)
if (s[i] != '0') {
s.erase(s.begin(), s.begin() + i);
break;
}
return s;
}
string A, B;
int D;
int main() {
cin >> A >> B >> D;
cout << (solve(B, D) - solve(Minus(A), D) + 2 * mod) % mod << endl;
} | #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 25;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const vi emp;
const int mod = 10000;
int dp[502][11][500][2][2];
int solve(string s, int D) {
int n = s.size();
for (int i = 0; i <= n; i++)
for (int j = 0; j <= 10; j++)
for (int k = 0; k < D; k++)
for (int l = 0; l < 2; l++)
for (int m = 0; m < 2; m++)
dp[i][j][k][l][m] = 0;
dp[0][10][0][0][0]++;
for (int i = 0; i < n; i++)
for (int j = 0; j <= 10; j++)
for (int k = 0; k < D; k++)
for (int l = 0; l < 2; l++)
for (int m = 0; m < 2; m++) {
if (j != 10) {
int S = m ? 0 : j + 1, T = m ? j - 1 : 9;
if (!l)
T = min(T, s[i] - '0');
for (int d = S; d < T + 1; d++)
(dp[i + 1][d][(10 * k + d) % D][l || s[i] - '0' != d /*d<T*/]
[!m] += dp[i][j][k][l][m]) %= mod;
} else if (!k && !l && !m) {
int lim = (i == 0) ? s[i] - '0' : 9;
for (int d = 1; d < lim + 1; d++)
for (int I = 0; I < 2; I++)
dp[i + 1][d][d % D][i || d < lim][I]++;
dp[i + 1][10][0][0][0]++;
}
}
int ans = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
(ans += dp[n][i][0][j][k]) %= mod;
}
int num = 9 / D;
if (s.size() == 1 && s[0] - '0' < num * D)
num = (s[0] - '0') / D;
return (ans + mod - num) % mod;
}
string Minus(string s) {
if (s.back() > '0')
s[s.size() - 1] -= 1;
else {
for (int i = s.size() - 1; i >= 0; i--)
if (s[i] > '0') {
s[i] -= 1;
for (int j = i + 1; j < s.size(); j++)
s[j] = '9';
break;
}
}
if (s == "0")
return s;
for (int i = 0; i < s.size(); i++)
if (s[i] != '0') {
s.erase(s.begin(), s.begin() + i);
break;
}
return s;
}
string A, B;
int D;
int main() {
cin >> A >> B >> D;
cout << (solve(B, D) - solve(Minus(A), D) + 2 * mod) % mod << endl;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll ans = 0;
ll n;
string s;
ll cnt = 0;
vector<P> p;
ll sz;
int main() {
cin >> s;
p.reserve(1000001);
n = s.length();
rep(i, n) {
if (i == 0) {
if (s[0] == 'J')
p.pb(P(1, 0));
else if (s[0] == 'O')
p.pb(P(1, 1));
else
p.pb(P(1, 2));
} else {
if (s[i] != s[i - 1]) {
if (s[i] == 'J')
p.pb(P(1, 0));
else if (s[i] == 'O')
p.pb(P(1, 1));
else
p.pb(P(1, 2));
} else {
p[p.size() - 1].fr++;
}
}
}
rep(i, p.size() - 2) {
if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr) {
ans = max(ans, p[i + 1].fr);
}
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll ans = 0;
ll n;
string s;
ll cnt = 0;
vector<P> p;
ll sz;
int main() {
cin >> s;
p.reserve(1000001);
n = s.length();
rep(i, n) {
if (i == 0) {
if (s[0] == 'J')
p.pb(P(1, 0));
else if (s[0] == 'O')
p.pb(P(1, 1));
else
p.pb(P(1, 2));
} else {
if (s[i] != s[i - 1]) {
if (s[i] == 'J')
p.pb(P(1, 0));
else if (s[i] == 'O')
p.pb(P(1, 1));
else
p.pb(P(1, 2));
} else {
p[p.size() - 1].fr++;
}
}
}
for (ll i = 0; i + 2 < p.size(); i++) {
if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr) {
ans = max(ans, p[i + 1].fr);
}
}
cout << ans << endl;
} | replace | 54 | 55 | 54 | 55 | 0 | |
p00494 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
char s[1000001];
int main() {
fgets(s, sizeof(s), stdin);
int res = 0, oc, ic, jc;
int i, j, k, l;
int slen = strlen(s);
jc = oc = ic = 0;
rep(i, slen) {
if (ic == 0 && oc == 0 && s[i] == 'J')
jc++;
else if (jc && ic == 0 && s[i] == 'O')
oc++;
else if (jc && oc && s[i] == 'I')
ic++;
else {
if (jc >= oc && ic >= oc)
res = max(res, oc);
jc = oc = ic = 0;
i--;
}
}
if (jc >= oc && ic >= oc)
res = max(res, oc);
printf("%d\n", res);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
char s[1000001];
int main() {
fgets(s, sizeof(s), stdin);
int res = 0, oc, ic, jc;
int i, j, k, l;
int slen = strlen(s);
jc = oc = ic = 0;
rep(i, slen) {
if (ic == 0 && oc == 0 && s[i] == 'J')
jc++;
else if (jc && ic == 0 && s[i] == 'O')
oc++;
else if (jc && oc && s[i] == 'I')
ic++;
else {
if (jc >= oc && ic >= oc)
res = max(res, oc);
jc = oc = ic = 0;
if (s[i] == 'J')
jc++;
}
}
if (jc >= oc && ic >= oc)
res = max(res, oc);
printf("%d\n", res);
return 0;
} | replace | 54 | 55 | 54 | 56 | TLE | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main() {
char s[1000001];
vector<pii> v;
cin >> s;
int l = strlen(s);
int cnt = 0;
int x;
for (int i = 0; i < l; i++) {
if (i > 0 && s[i] != s[i - 1]) {
v.push_back(pii(x, cnt));
cnt = 0;
}
switch (s[i]) {
case 'J':
x = 0;
break;
case 'O':
x = 1;
break;
case 'I':
x = 2;
break;
}
cnt++;
}
v.push_back(pii(x, cnt));
int ans = 0;
for (int i = 0; i < v.size() - 2; i++) {
if (v[i].first == 0 && v[i + 1].first == 1 && v[i + 2].first == 2 &&
v[i].second >= v[i + 1].second && v[i + 1].second <= v[i + 2].second) {
ans = max(ans, v[i + 1].second);
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main() {
char s[1000001];
vector<pii> v;
cin >> s;
int l = strlen(s);
int cnt = 0;
int x;
for (int i = 0; i < l; i++) {
if (i > 0 && s[i] != s[i - 1]) {
v.push_back(pii(x, cnt));
cnt = 0;
}
switch (s[i]) {
case 'J':
x = 0;
break;
case 'O':
x = 1;
break;
case 'I':
x = 2;
break;
}
cnt++;
}
v.push_back(pii(x, cnt));
int ans = 0;
for (int i = 0; i < (int)v.size() - 2; i++) {
if (i < (int)v.size() - 2 && v[i].first == 0 && v[i + 1].first == 1 &&
v[i + 2].first == 2 && v[i].second >= v[i + 1].second &&
v[i + 1].second <= v[i + 2].second) {
ans = max(ans, v[i + 1].second);
}
}
cout << ans << endl;
return 0;
} | replace | 35 | 38 | 35 | 39 | 0 | |
p00494 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S;
int main() {
cin >> S;
int res = 0;
for (int i = 0; i < S.size(); i++) {
if (S[i] != 'J')
continue;
int jc, oc, ic;
jc = oc = ic = 0;
int j;
for (j = i; j < S.size(); j++) {
if (S[j] != 'J')
break;
jc++;
}
// if(S[j]!='O') continue;
for (; j < S.size(); j++) {
if (S[j] != 'O')
break;
oc++;
}
for (; j < S.size(); j++) {
if (S[j] != 'I')
break;
ic++;
}
// res = max(res, min(jc, min(oc,ic)));
if (oc <= ic && oc <= jc)
res = max(res, oc);
}
cout << res << endl;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S;
int main() {
cin >> S;
int res = 0;
for (int i = 0; i < S.size(); i++) {
if (S[i] != 'J')
continue;
int jc, oc, ic;
jc = oc = ic = 0;
int j;
for (j = i; j < S.size(); j++) {
if (S[j] != 'J')
break;
jc++;
}
// if(S[j]!='O') continue;
for (; j < S.size(); j++) {
if (S[j] != 'O')
break;
oc++;
}
for (; j < S.size(); j++) {
if (S[j] != 'I')
break;
ic++;
}
// res = max(res, min(jc, min(oc,ic)));
if (oc <= ic && oc <= jc)
res = max(res, oc);
i = j - 1;
}
cout << res << endl;
} | insert | 38 | 38 | 38 | 39 | TLE | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1001001001;
int main() {
char str[100010];
scanf("%s", str);
int n = strlen(str);
vector<pair<char, int>> data;
rep(i, n) {
if (!data.empty() && data[data.size() - 1].first == str[i])
data[data.size() - 1].second++;
else
data.push_back(make_pair(str[i], 1));
}
int ans = 0;
rep(i, data.size() - 2) {
if (data[i].first == 'J' && data[i + 1].first == 'O' &&
data[i + 2].first == 'I') {
int a = data[i].second;
int b = data[i + 1].second;
int c = data[i + 2].second;
if (b <= a && b <= c)
ans = max(ans, b);
}
}
printf("%d\n", ans);
return 0;
} | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1001001001;
int main() {
char str[1000100];
scanf("%s", str);
int n = strlen(str);
vector<pair<char, int>> data;
rep(i, n) {
if (!data.empty() && data[data.size() - 1].first == str[i])
data[data.size() - 1].second++;
else
data.push_back(make_pair(str[i], 1));
}
int ans = 0;
rep(i, data.size() - 2) {
if (data[i].first == 'J' && data[i + 1].first == 'O' &&
data[i + 2].first == 'I') {
int a = data[i].second;
int b = data[i + 1].second;
int c = data[i + 2].second;
if (b <= a && b <= c)
ans = max(ans, b);
}
}
printf("%d\n", ans);
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#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 it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
string s;
cin >> s;
vector<char> ch;
vector<int> co;
int c = 0;
rep(i, s.size()) {
c++;
if (i == s.size() - 1 || s[i] != s[i + 1]) {
co.pb(c);
ch.pb(s[i]);
c = 0;
}
}
int out = 0;
rep(i, ch.size() - 2) {
if (ch[i] == 'J' && ch[i + 1] == 'O' && ch[i + 2] == 'I')
out = max(out, min(co[i], min(co[i + 1], co[i + 2])));
}
cout << out << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#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 it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
string s;
cin >> s;
vector<char> ch;
vector<int> co;
int c = 0;
rep(i, s.size()) {
c++;
if (i == s.size() - 1 || s[i] != s[i + 1]) {
co.pb(c);
ch.pb(s[i]);
c = 0;
}
}
int out = 0;
if (ch.size() > 2) {
rep(i, ch.size() - 2) {
if (ch[i] == 'J' && ch[i + 1] == 'O' && ch[i + 2] == 'I')
if (co[i + 1] <= co[i] && co[i + 1] <= co[i + 2])
out = max(out, co[i + 1]);
}
}
cout << out << endl;
} | replace | 33 | 36 | 33 | 39 | 0 | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<char, int> P;
string s;
int main() {
cin >> s;
vector<P> v;
char c = s[0];
int length = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] == c)
length++;
else {
v.push_back(P(c, length));
c = s[i], length = 1;
}
}
v.push_back(P(c, length));
int ans = 0;
for (int i = 0; i < v.size() - 2; i++) {
if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
if (v[i].second >= v[i + 1].second &&
v[i + 1].second <= v[i + 2].second) {
ans = max(ans, v[i + 1].second);
}
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<char, int> P;
string s;
int main() {
cin >> s;
vector<P> v;
char c = s[0];
int length = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] == c)
length++;
else {
v.push_back(P(c, length));
c = s[i], length = 1;
}
}
v.push_back(P(c, length));
int ans = 0;
if (v.size() < 3) {
cout << 0 << endl;
return 0;
}
for (int i = 0; i < v.size() - 2; i++) {
if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
if (v[i].second >= v[i + 1].second &&
v[i + 1].second <= v[i + 2].second) {
ans = max(ans, v[i + 1].second);
}
}
}
cout << ans << endl;
return 0;
} | insert | 28 | 28 | 28 | 33 | 0 | |
p00494 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
string s;
vector<pair<int, int>> a;
int ans = 0;
int main() {
cin >> s;
For(i, 0, s.size()) {
int cnt;
if (s[i] == 'J') {
cnt = 0;
while (s[i] == 'J') {
cnt++;
i++;
}
a.pb(mp(1, cnt));
}
if (s[i] == 'O') {
cnt = 0;
while (s[i] == 'O') {
cnt++;
i++;
}
a.pb(mp(2, cnt));
}
if (s[i] == 'I') {
cnt = 0;
while (s[i] == 'I') {
cnt++;
i++;
}
a.pb(mp(3, cnt));
}
i--;
}
For(i, 0,
a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 && a[i + 2].fi == 3) {
ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
i += 2;
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
string s;
vector<pair<int, int>> a;
int ans = 0;
int main() {
cin >> s;
For(i, 0, s.size()) {
int cnt;
if (s[i] == 'J') {
cnt = 0;
while (s[i] == 'J') {
cnt++;
i++;
}
a.pb(mp(1, cnt));
}
if (s[i] == 'O') {
cnt = 0;
while (s[i] == 'O') {
cnt++;
i++;
}
a.pb(mp(2, cnt));
}
if (s[i] == 'I') {
cnt = 0;
while (s[i] == 'I') {
cnt++;
i++;
}
a.pb(mp(3, cnt));
}
i--;
}
if (a.size() <= 2) {
cout << "0\n";
return 0;
}
For(i, 0, a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 &&
a[i + 2].fi == 3 &&
min(a[i].se, min(a[i + 1].se, a[i + 2].se)) ==
a[i + 1].se) {
ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
i += 2;
}
cout << ans << '\n';
} | replace | 49 | 51 | 49 | 57 | 0 | |
p00494 | C++ | Time Limit Exceeded | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string S;
int main() {
cin >> S;
int D = 0, E = 0, F = 0;
int ans = 0;
int A, B, C;
for (int i = 0; i < S.size(); i++) {
for (A = i; S[A] == 'J'; A++)
;
for (B = A; S[B] == 'O'; B++)
;
for (C = B; S[C] == 'I'; C++)
;
D = A - i;
E = B - A;
F = C - B;
if (D >= E && E <= F)
ans = max(ans, E);
}
cout << ans << endl;
return 0;
}
| //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string S;
int main() {
cin >> S;
int D = 0, E = 0, F = 0;
int ans = 0;
int A, B, C;
for (int i = 0; i < S.size(); i = C) {
for (A = i; S[A] == 'J'; A++)
;
for (B = A; S[B] == 'O'; B++)
;
for (C = B; S[C] == 'I'; C++)
;
D = A - i;
E = B - A;
F = C - B;
if (D >= E && E <= F)
ans = max(ans, E);
}
cout << ans << endl;
return 0;
}
| replace | 24 | 25 | 24 | 25 | TLE | |
p00494 | C++ | Time Limit Exceeded | //============================================================================
// Name : AOJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
string s;
int main() {
cin >> s;
int R = 0;
int a0, a1, a2, a3;
for (a0 = 0; a0 < s.size(); a0 = a3) {
for (a1 = a0; s[a1] == 'J'; a1++)
;
for (a2 = a1; s[a2] == 'O'; a2++)
;
for (a3 = a2; s[a3] == 'C'; a3++)
;
if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
R = max(R, a2 - a1);
}
cout << R << endl;
return 0;
} | //============================================================================
// Name : AOJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
string s;
int main() {
cin >> s;
int R = 0;
int a0, a1, a2, a3;
for (a0 = 0; a0 < s.size(); a0 = a3) {
for (a1 = a0; s[a1] == 'J'; a1++)
;
for (a2 = a1; s[a2] == 'O'; a2++)
;
for (a3 = a2; s[a3] == 'I'; a3++)
;
if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
R = max(R, a2 - a1);
}
cout << R << endl;
return 0;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p00494 | C++ | Time Limit Exceeded | //============================================================================
// Name : AOJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
int a0, a1, a2, a3;
for (a0 = 0; a0 < s.size(); a0 = a3) {
for (a1 = a0; s[a1] == 'J'; a1++)
;
for (a2 = a1; s[a2] == 'O'; a2++)
;
for (a3 = a2; s[a3] == 'J'; a3++)
;
if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
ans = max(ans, a2 - a1);
}
cout << ans << endl;
return 0;
} | //============================================================================
// Name : AOJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
int a0, a1, a2, a3;
for (a0 = 0; a0 < s.size(); a0 = a3) {
for (a1 = a0; s[a1] == 'J'; a1++)
;
for (a2 = a1; s[a2] == 'O'; a2++)
;
for (a3 = a2; s[a3] == 'I'; a3++)
;
if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
ans = max(ans, a2 - a1);
}
cout << ans << endl;
return 0;
} | replace | 21 | 22 | 21 | 22 | TLE | |
p00494 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<char, int> P;
string s;
vector<P> x;
int main() {
cin >> s;
rep(i, s.size()) {
if (x.empty() || x.back().first != s[i]) {
x.push_back(P(s[i], 1));
} else {
++x.back().second;
}
}
int ans = 0;
rep(i, x.size() - 2) {
if (x[i].first == 'J' && x[i + 1].first == 'O' && x[i + 2].first == 'I') {
ans = max(min(x[i].second, min(x[i + 1].second, x[i + 2].second)), ans);
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<char, int> P;
string s;
vector<P> x;
int main() {
cin >> s;
rep(i, s.size()) {
if (x.empty() || x.back().first != s[i]) {
x.push_back(P(s[i], 1));
} else {
++x.back().second;
}
}
int ans = 0;
rep(i, (int)x.size() - 2) {
P &a = x[i];
P &b = x[i + 1];
P &c = x[i + 2];
if (a.first == 'J' && b.first == 'O' && c.first == 'I') {
if (a.second >= b.second && c.second >= b.second) {
ans = max(b.second, ans);
}
}
}
cout << ans << endl;
return 0;
} | replace | 23 | 26 | 23 | 31 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.