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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00449 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()
int N, K;
#define INF 11451419
#define MAX_V 1010
int cost[MAX_V][MAX_V];
int V;
int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
fill(d, d + V, INF);
fill(used, used + V, false);
d[s] = 0;
while (true) {
int v = -1;
for (int u = 0; u < V; u++) {
if (!used[u] && (v == -1 || d[u] < d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
for (int u = 0; u < V; u++) {
d[u] = min(d[u], d[v] + cost[v][u]);
}
}
}
int main() {
while (true) {
cin >> N >> K;
if (N == 0 && K == 0)
break;
V = N * (N - 1) / 2;
rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
rep(i, K) {
int op;
cin >> op;
if (op == 0) {
int a, b;
cin >> a >> b;
dijkstra(a - 1);
int x = d[b - 1];
cout << (x == INF ? -1 : x) << "\n";
} else {
int c, d, e;
cin >> c >> d >> e;
cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
}
}
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()
int N, K;
#define INF 11451419
#define MAX_V 1010
int cost[MAX_V][MAX_V];
int V;
int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
fill(d, d + V, INF);
fill(used, used + V, false);
d[s] = 0;
while (true) {
int v = -1;
for (int u = 0; u < V; u++) {
if (!used[u] && (v == -1 || d[u] < d[v]))
v = u;
}
if (v == -1)
break;
used[v] = true;
for (int u = 0; u < V; u++) {
d[u] = min(d[u], d[v] + cost[v][u]);
}
}
}
int main() {
while (true) {
cin >> N >> K;
if (N == 0 && K == 0)
break;
V = N;
rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
rep(i, K) {
int op;
cin >> op;
if (op == 0) {
int a, b;
cin >> a >> b;
dijkstra(a - 1);
int x = d[b - 1];
cout << (x == INF ? -1 : x) << "\n";
} else {
int c, d, e;
cin >> c >> d >> e;
cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
}
}
}
return 0;
} | replace | 48 | 49 | 48 | 49 | 0 | |
p00449 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
while (1) {
int N, K;
cin >> N >> K;
vector<vector<int>> diss(N, vector<int>(N, 1e9));
for (int i = 0; i < N; ++i)
diss[i][i] = 0;
for (int i = 0; i < K; ++i) {
int a;
cin >> a;
if (a) {
int c, d, e;
cin >> c >> d >> e;
c--;
d--;
diss[c][d] = min(diss[c][d], e);
diss[d][c] = min(diss[d][c], e);
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
}
}
} else {
int a, b;
cin >> a >> b;
a--;
b--;
if (diss[a][b] >= 1e9) {
cout << -1 << endl;
} else {
cout << diss[a][b] << endl;
}
}
}
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
while (1) {
int N, K;
cin >> N >> K;
if (!N)
break;
vector<vector<int>> diss(N, vector<int>(N, 1e9));
for (int i = 0; i < N; ++i)
diss[i][i] = 0;
for (int i = 0; i < K; ++i) {
int a;
cin >> a;
if (a) {
int c, d, e;
cin >> c >> d >> e;
c--;
d--;
diss[c][d] = min(diss[c][d], e);
diss[d][c] = min(diss[d][c], e);
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
}
}
} else {
int a, b;
cin >> a >> b;
a--;
b--;
if (diss[a][b] >= 1e9) {
cout << -1 << endl;
} else {
cout << diss[a][b] << endl;
}
}
}
}
return 0;
} | insert | 15 | 15 | 15 | 17 | TLE | |
p00449 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
int n, k;
while (cin >> n >> k) {
if (n == 0)
break;
int route[100][100];
REP(i, n) REP(j, n) route[i][j] = INF;
int t, a, b, c;
REP(i, k) {
cin >> t;
if (t == 1) {
cin >> a >> b >> c;
a--;
b--;
route[a][b] = min(route[a][b], c);
route[b][a] = min(route[b][a], c);
REP(i, n) REP(j, n) REP(k, n) {
route[i][j] = min(route[i][j], route[i][k] + route[k][j]);
}
} else {
cin >> a >> b;
a--;
b--;
if (route[a][b] == INF)
cout << -1 << endl;
else
cout << route[a][b] << endl;
}
}
}
return 0;
} | #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
int n, k;
while (cin >> n >> k) {
if (n == 0)
break;
int route[100][100];
REP(i, n) REP(j, n) route[i][j] = INF;
int t, a, b, c;
REP(i, k) {
cin >> t;
if (t == 1) {
cin >> a >> b >> c;
a--;
b--;
route[a][b] = min(route[a][b], c);
route[b][a] = min(route[b][a], c);
REP(i, n) REP(j, n) {
route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
}
REP(i, n) REP(j, n) {
route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
}
} else {
cin >> a >> b;
a--;
b--;
if (route[a][b] == INF)
cout << -1 << endl;
else
cout << route[a][b] << endl;
}
}
}
return 0;
} | replace | 44 | 46 | 44 | 51 | TLE | |
p00449 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b, a | b) {
int c[100][100];
for (int x = 0; x < a; x++) {
for (int y = 0; y < a; y++) {
c[x][y] = 1 << 29;
}
}
for (int e = 0; e < b; e++) {
int f;
cin >> f;
if (f == 0) {
int g, h;
cin >> g >> h;
g--;
h--;
if (c[g][h] != 1 << 29)
cout << c[g][h] << endl;
else
cout << -1 << endl;
} else {
int g, h, i;
cin >> g >> h >> i;
g--;
h--;
c[g][h] = c[h][g] = i;
for (int j = 0; j < a; j++) {
for (int k = 0; k < a; k++) {
for (int l = 0; l < a; l++) {
if (k == l)
c[k][l] = 0;
else
c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
}
}
}
}
}
}
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b, a | b) {
int c[100][100];
for (int x = 0; x < a; x++) {
for (int y = 0; y < a; y++) {
c[x][y] = 1 << 29;
}
}
for (int e = 0; e < b; e++) {
int f;
cin >> f;
if (f == 0) {
int g, h;
cin >> g >> h;
g--;
h--;
if (c[g][h] != 1 << 29)
cout << c[g][h] << endl;
else
cout << -1 << endl;
} else {
int g, h, i;
cin >> g >> h >> i;
g--;
h--;
if (c[g][h] > i) {
c[g][h] = c[h][g] = i;
for (int j = 0; j < a; j++) {
for (int k = 0; k < a; k++) {
for (int l = 0; l < a; l++) {
if (k == l)
c[k][l] = 0;
else
c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
}
}
}
}
}
}
}
} | replace | 32 | 40 | 32 | 42 | TLE | |
p00449 | C++ | Time Limit Exceeded | /* {{{ Shinobu kawaii */
/*
______ __ _ __
.' ____ \ [ | (_) [ |
| (___ \_| | |--. __ _ .--. .--. | |.--. __ _
_.____`. | .-. | [ | [ `.-. |/ .'`\ \| '/'`\ \[ | | |
| \____) | | | | | | | | | | || \__. || \__/ | | \_/ |,
\______.'[___]|__][___][___||__]'.__.'[__;.__.' '.__.'_/
*/
/* }}} */
#include <bits/stdc++.h>
using namespace std;
// #define int long long
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* cpp template {{{ */
/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)
/* debug */
#define debug(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
int x;
std::cin >> x;
return x;
}
template <typename T> void print(T x) { std::cout << x << '\n'; }
template <typename T> void print(std::vector<T> &v, std::string s = " ") {
REP(i, v.size()) {
if (i != 0)
std::cout << s;
std::cout << v[i];
}
std::cout << '\n';
}
/* }}} */
void warshallfloyd(vvi &d, int n) {
REP(k, n) REP(i, n) REP(j, n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
}
signed main() {
int n, k;
while (n = in(), k = in(), n || k) {
vvi d(n, vi(n, INF));
REP(i, k) {
bool a = in();
if (a) {
int b = in() - 1, c = in() - 1, cost = in();
d[b][c] = min(d[b][c], cost);
d[c][b] = min(d[c][b], cost);
warshallfloyd(d, n);
} else {
int b = in() - 1, c = in() - 1;
print(d[b][c] == INF ? -1 : d[b][c]);
}
}
}
return 0;
} | /* {{{ Shinobu kawaii */
/*
______ __ _ __
.' ____ \ [ | (_) [ |
| (___ \_| | |--. __ _ .--. .--. | |.--. __ _
_.____`. | .-. | [ | [ `.-. |/ .'`\ \| '/'`\ \[ | | |
| \____) | | | | | | | | | | || \__. || \__/ | | \_/ |,
\______.'[___]|__][___][___||__]'.__.'[__;.__.' '.__.'_/
*/
/* }}} */
#include <bits/stdc++.h>
using namespace std;
// #define int long long
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* cpp template {{{ */
/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)
/* debug */
#define debug(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
int x;
std::cin >> x;
return x;
}
template <typename T> void print(T x) { std::cout << x << '\n'; }
template <typename T> void print(std::vector<T> &v, std::string s = " ") {
REP(i, v.size()) {
if (i != 0)
std::cout << s;
std::cout << v[i];
}
std::cout << '\n';
}
/* }}} */
void warshallfloyd(vvi &d, int n) {
REP(k, n) REP(i, n) REP(j, n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
}
signed main() {
int n, k;
while (n = in(), k = in(), n || k) {
vvi d(n, vi(n, INF));
REP(i, k) {
bool a = in();
if (a) {
int b = in() - 1, c = in() - 1, cost = in();
if (d[b][c] <= cost)
continue;
d[b][c] = min(d[b][c], cost);
d[c][b] = min(d[c][b], cost);
warshallfloyd(d, n);
} else {
int b = in() - 1, c = in() - 1;
print(d[b][c] == INF ? -1 : d[b][c]);
}
}
}
return 0;
} | insert | 97 | 97 | 97 | 99 | TLE | |
p00449 | C++ | Runtime Error | #include <queue>
#include <stdio.h>
using namespace std;
int main() {
const int INF = 10000000;
int n, k, map[100][100], a, b, c;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
q;
while (true) {
scanf("%d%d", &n, &k);
if (n == 0 && k == 0)
break;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
map[i][j] = INF;
}
for (int g = 0; g < k; ++g) {
scanf("%d", &a);
if (a == 0) {
// vZ
int ans = -1;
bool flag[100] = {};
scanf("%d%d", &a, &b);
q.push(pair<int, int>(0, a - 1));
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int &cost = p.first, &num = p.second;
if (flag[num])
continue;
flag[num] = true;
if (num == b - 1) {
ans = cost;
for (int i = q.size(); i > 0; ++i)
q.pop();
break;
}
for (int i = 0; i < n; ++i) {
if (map[num][i] == INF || flag[i])
continue;
q.push(pair<int, int>(cost + map[num][i], i));
}
}
printf("%d\n", ans);
} else {
// ÇÁ
scanf("%d%d%d", &a, &b, &c);
if (map[a - 1][b - 1] > c)
map[a - 1][b - 1] = map[b - 1][a - 1] = c;
}
}
}
return 0;
} | #include <queue>
#include <stdio.h>
using namespace std;
int main() {
const int INF = 10000000;
int n, k, map[100][100], a, b, c;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
q;
while (true) {
scanf("%d%d", &n, &k);
if (n == 0 && k == 0)
break;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
map[i][j] = INF;
}
for (int g = 0; g < k; ++g) {
scanf("%d", &a);
if (a == 0) {
// vZ
int ans = -1;
bool flag[100] = {};
scanf("%d%d", &a, &b);
q.push(pair<int, int>(0, a - 1));
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int &cost = p.first, &num = p.second;
if (flag[num])
continue;
flag[num] = true;
if (num == b - 1) {
ans = cost;
for (int i = q.size(); i > 0; --i)
q.pop();
break;
}
for (int i = 0; i < n; ++i) {
if (map[num][i] == INF || flag[i])
continue;
q.push(pair<int, int>(cost + map[num][i], i));
}
}
printf("%d\n", ans);
} else {
// ÇÁ
scanf("%d%d%d", &a, &b, &c);
if (map[a - 1][b - 1] > c)
map[a - 1][b - 1] = map[b - 1][a - 1] = c;
}
}
}
return 0;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p00450 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;
int n;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (true) {
cin >> n;
if (n == 0)
break;
int cntw = 0;
int cntb = 0;
int pre = -1;
vector<int> wr, br;
rep(i, n) {
int c;
cin >> c;
int wsize = wr.size();
int bsize = br.size();
if (i % 2 == 0) {
if (pre != c) {
if (c == 1)
wr.pb(i - 1);
else
br.pb(i - 1);
pre = c;
}
if (c == 0)
cntw++;
else
cntb++;
} else {
if (c == 0) {
if (pre != c) {
cntw += i - wr[wsize - 1];
cntb -= i - wr[wsize - 1] - 1;
pre = c;
if (wr[wsize - 1] == -1)
br.pb(-1);
wr.pop_back();
} else
cntw++;
} else {
if (pre != c) {
cntb += i - br[bsize - 1];
cntw -= i - br[bsize - 1] - 1;
pre = c;
if (wr[wsize - 1] == -1)
br.pb(-1);
br.pop_back();
} else
cntb++;
}
}
}
cout << cntw << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
const ll mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;
int n;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (true) {
cin >> n;
if (n == 0)
break;
int cntw = 0;
int cntb = 0;
int pre = -1;
vector<int> wr, br;
rep(i, n) {
int c;
cin >> c;
int wsize = wr.size();
int bsize = br.size();
if (i % 2 == 0) {
if (pre != c) {
if (c == 1)
wr.pb(i - 1);
else
br.pb(i - 1);
pre = c;
}
if (c == 0)
cntw++;
else
cntb++;
} else {
if (c == 0) {
if (pre != c) {
cntw += i - wr[wsize - 1];
cntb -= i - wr[wsize - 1] - 1;
pre = c;
if (wr[wsize - 1] == -1)
br.pb(-1);
wr.pop_back();
} else
cntw++;
} else {
if (pre != c) {
cntb += i - br[bsize - 1];
cntw -= i - br[bsize - 1] - 1;
pre = c;
if (br[bsize - 1] == -1)
wr.pb(-1);
br.pop_back();
} else
cntb++;
}
}
}
cout << cntw << endl;
}
} | replace | 77 | 79 | 77 | 79 | 0 | |
p00450 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second
int main() {
while (1) {
int n;
cin >> n;
if (n == 0)
break;
vector<P> mp;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (i == 0)
mp.push_back(P(a, 1));
else if (i % 2 == 0) {
if (mp.back().val == a)
mp[mp.size() - 1].num++;
else
mp.push_back(P(a, 1));
} else if (i % 2 == 1) {
if (mp.back().val == a)
mp[mp.size() - 1].num++;
else if (i == 1)
mp[0].val = a, mp[0].num++;
else
mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
}
}
int ans = 0;
for (int i = 0; i < mp.size(); i++)
if (mp[i].val == 0)
ans += mp[i].num;
cout << ans << endl;
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second
int main() {
while (1) {
int n;
cin >> n;
if (n == 0)
break;
vector<P> mp;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (i == 0)
mp.push_back(P(a, 1));
else if (i % 2 == 0) {
if (mp.back().val == a)
mp[mp.size() - 1].num++;
else
mp.push_back(P(a, 1));
} else if (i % 2 == 1) {
if (mp.back().val == a)
mp[mp.size() - 1].num++;
else if (mp.size() == 1)
mp[0].val = a, mp[0].num++;
else
mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
}
}
int ans = 0;
for (int i = 0; i < mp.size(); i++)
if (mp[i].val == 0)
ans += mp[i].num;
cout << ans << endl;
}
return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p00450 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
vector<int> linear(vector<int> stone) {
int n = stone.size();
int state = 0;
for (int i = n - 2; i >= 0; i--) {
bool eq = (stone[i + 1] == stone[i]);
if (!eq && (state || i % 2 == 0)) {
stone[i] = !(stone[i]);
state = 1;
} else if (eq && (i % 2 != 0)) {
state = 0;
}
}
return stone;
}
vector<int> wrong(vector<int> v) {
REP(i, v.size()) {
if (i % 2 == 1) {
for (int j = i - 1; j >= 0; j--) {
if (v[i] == v[j])
break;
else
v[j] = v[i];
}
}
}
return v;
}
int main(void) {
int n;
while (cin >> n, n) {
if (n == 0)
break;
vector<int> stone(n);
REP(i, n) cin >> stone[i];
/*
if(linear(stone) != wrong(stone)){
REP(i,n){cout<<stone[i]<<" ";} cout<<endl;
cout<<"linear:"; REP(i,n){cout<<linear(stone)[i]<<" ";} cout<<endl;
cout<<"wrong:"; REP(i,n){cout<<wrong(stone)[i]<<" ";} cout<<endl;
}
*/
/*
//REP(i,n){cout<<stone[i]<<" ";} cout<<endl;
vector<int> ans = linear(stone);
int count = 0;
REP(i,n)if(ans[i]==0)count++;
//REP(i,n){cout<<ans[i]<<" ";} cout<<endl;
cout<<count<<endl;
*/
vector<int> comp;
int last = -1;
int ans = 0;
REP(i, n) {
if (i % 2 == 1) {
comp.back() += 1;
if (last != stone[i]) {
if (stone[i] == 0)
ans += comp.back();
else
ans -= comp.back() - 1;
if (comp.size() >= 2) {
int t = comp.back();
comp.pop_back();
comp.back() += t;
}
} else {
ans += (stone[i] == 0);
}
} else {
if (last != stone[i]) {
comp.push_back(1);
} else {
comp.back() += 1;
}
ans += (stone[i] == 0);
}
last = stone[i];
}
vector<int> sim = wrong(stone);
int checkans = 0;
REP(i, n) checkans += (sim[i] == 0);
assert(checkans == ans);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
vector<int> linear(vector<int> stone) {
int n = stone.size();
int state = 0;
for (int i = n - 2; i >= 0; i--) {
bool eq = (stone[i + 1] == stone[i]);
if (!eq && (state || i % 2 == 0)) {
stone[i] = !(stone[i]);
state = 1;
} else if (eq && (i % 2 != 0)) {
state = 0;
}
}
return stone;
}
vector<int> wrong(vector<int> v) {
REP(i, v.size()) {
if (i % 2 == 1) {
for (int j = i - 1; j >= 0; j--) {
if (v[i] == v[j])
break;
else
v[j] = v[i];
}
}
}
return v;
}
int main(void) {
int n;
while (cin >> n, n) {
if (n == 0)
break;
vector<int> stone(n);
REP(i, n) cin >> stone[i];
/*
if(linear(stone) != wrong(stone)){
REP(i,n){cout<<stone[i]<<" ";} cout<<endl;
cout<<"linear:"; REP(i,n){cout<<linear(stone)[i]<<" ";} cout<<endl;
cout<<"wrong:"; REP(i,n){cout<<wrong(stone)[i]<<" ";} cout<<endl;
}
*/
/*
//REP(i,n){cout<<stone[i]<<" ";} cout<<endl;
vector<int> ans = linear(stone);
int count = 0;
REP(i,n)if(ans[i]==0)count++;
//REP(i,n){cout<<ans[i]<<" ";} cout<<endl;
cout<<count<<endl;
*/
vector<int> comp;
int last = -1;
int ans = 0;
REP(i, n) {
if (i % 2 == 1) {
comp.back() += 1;
if (last != stone[i]) {
if (stone[i] == 0)
ans += comp.back();
else
ans -= comp.back() - 1;
if (comp.size() >= 2) {
int t = comp.back();
comp.pop_back();
comp.back() += t;
}
} else {
ans += (stone[i] == 0);
}
} else {
if (last != stone[i]) {
comp.push_back(1);
} else {
comp.back() += 1;
}
ans += (stone[i] == 0);
}
last = stone[i];
}
cout << ans << endl;
}
return 0;
} | delete | 108 | 112 | 108 | 108 | TLE | |
p00450 | C++ | Time Limit Exceeded | #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;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
{
while (1)
cin >> n;
if (!n)
return 0;
v.clear();
for (ll i = 1; i <= n; i++) {
cin >> x;
if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
v.push_back(P(i, x));
} else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
if (v.size() == 1) {
v[0].second = x;
} else {
v.erase(v.end() - 1);
}
}
}
ll ans = 0;
for (ll i = 0; i < v.size(); i++) {
if (v[i].second == 0)
ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
}
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;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
while (1) {
cin >> n;
if (!n)
return 0;
v.clear();
for (ll i = 1; i <= n; i++) {
cin >> x;
if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
v.push_back(P(i, x));
} else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
if (v.size() == 1) {
v[0].second = x;
} else {
v.erase(v.end() - 1);
}
}
}
ll ans = 0;
for (ll i = 0; i < v.size(); i++) {
if (v[i].second == 0)
ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
}
cout << ans << endl;
}
} | replace | 18 | 21 | 18 | 20 | TLE | |
p00450 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
while (true) {
int n;
scanf("%d", &n);
if (n == 0)
break;
priority_queue<int> q[2];
int s[2] = {0};
int prev = -1;
for (int i = 0; i < n; ++i) {
int c, d;
scanf("%d", &c);
d = (c + 1) % 2;
if (i % 2 == 0) {
if (prev != c)
q[c].push(i);
} else {
if (prev != c) {
int l = q[d].top();
q[d].pop();
int re = i - l;
s[c] += re;
s[d] -= re;
}
}
s[c] += 1;
prev = c;
}
printf("%d\n", s[0]);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
while (true) {
int n;
scanf("%d", &n);
if (n == 0)
break;
priority_queue<int> q[2];
int s[2] = {0};
int prev = -1;
for (int i = 0; i < n; ++i) {
int c, d;
scanf("%d", &c);
d = (c + 1) % 2;
if (i % 2 == 0) {
if (prev != c)
q[c].push(i);
} else {
if (prev != c) {
int l = q[d].top();
q[d].pop();
int re = i - l;
s[c] += re;
s[d] -= re;
if (l == 0)
q[c].push(0);
}
}
s[c] += 1;
prev = c;
}
printf("%d\n", s[0]);
}
return 0;
} | insert | 29 | 29 | 29 | 31 | 0 | |
p00450 | C++ | Runtime Error | #include <iostream>
using namespace std;
int st[100002] = {0};
int main() {
int j;
int n;
int cnt = 0;
cin >> n;
while (n != 0) {
for (int i = 0; i < n; i += 2) {
cin >> st[i];
if (n - i != 1) {
cin >> st[i + 1];
if (st[i] != st[i + 1]) {
j = i;
while (st[j] != st[j + 1]) {
st[j] = st[j + 1];
j--;
}
}
}
}
for (int i = 0; i < n; i++) {
if (st[i] == 0) {
cnt++;
}
}
cout << cnt << endl;
cnt = 0;
cin >> n;
}
return 0;
} | #include <iostream>
using namespace std;
int st[100002] = {0};
int main() {
int j;
int n;
int cnt = 0;
cin >> n;
while (n != 0) {
for (int i = 0; i < n; i += 2) {
cin >> st[i];
if (n - i != 1) {
cin >> st[i + 1];
if (st[i] != st[i + 1]) {
j = i;
while (st[j] != st[j + 1]) {
st[j] = st[j + 1];
j--;
if (j < 0) {
break;
}
}
}
}
}
for (int i = 0; i < n; i++) {
if (st[i] == 0) {
cnt++;
}
}
cout << cnt << endl;
cnt = 0;
cin >> n;
}
return 0;
} | insert | 21 | 21 | 21 | 24 | 0 | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#define N 4000
using namespace std;
int n, m, dp[N][N], ans;
int main() {
string A, B;
while (cin >> A >> B) {
n = A.size();
m = B.size();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = 0;
ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A[i] == B[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#define N 4001
using namespace std;
int n, m, dp[N][N], ans;
int main() {
string A, B;
while (cin >> A >> B) {
n = A.size();
m = B.size();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i][j] = 0;
ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A[i] == B[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
}
}
cout << ans << endl;
}
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00451 | C++ | Runtime Error | #include <iostream>
using namespace std;
string s, t;
int l[100][100];
int m;
void list(string s, string t) {
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < t.length(); j++) {
if (s[i] == t[j]) {
if (i == 0 || j == 0) {
l[i][j] = 1;
} else {
l[i][j] = l[i - 1][j - 1] + 1;
}
}
}
}
}
int search() {
m = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < t.length(); j++) {
if (l[i][j] >= m) {
m = l[i][j];
l[i][j] = 0;
} else {
l[i][j] = 0;
}
}
}
return m;
}
int main() {
for (;;) {
m = 0;
if (cin >> s >> t) {
m = 0;
list(s, t);
m = 0;
cout << search() << endl;
} else {
break;
}
m = 0;
}
return 0;
} | #include <iostream>
using namespace std;
string s, t;
int l[4000][4000];
int m;
void list(string s, string t) {
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < t.length(); j++) {
if (s[i] == t[j]) {
if (i == 0 || j == 0) {
l[i][j] = 1;
} else {
l[i][j] = l[i - 1][j - 1] + 1;
}
}
}
}
}
int search() {
m = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < t.length(); j++) {
if (l[i][j] >= m) {
m = l[i][j];
l[i][j] = 0;
} else {
l[i][j] = 0;
}
}
}
return m;
}
int main() {
for (;;) {
m = 0;
if (cin >> s >> t) {
m = 0;
list(s, t);
m = 0;
cout << search() << endl;
} else {
break;
}
m = 0;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00451 | C++ | Runtime Error | // AOJ 0528
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int NMAX = 20000;
int n, k;
int Rank[NMAX + 1];
int tmp[NMAX + 1];
bool compare_sa(int i, int j) {
if (Rank[i] != Rank[j])
return Rank[i] < Rank[j];
else {
int ri = i + k <= n ? Rank[i + k] : -1;
int rj = j + k <= n ? Rank[j + k] : -1;
return ri < rj;
}
}
void construct_sa(string S, int *sa) {
n = S.length();
for (int i = 0; i <= n; i++) {
sa[i] = i;
Rank[i] = i < n ? S[i] : -1;
}
for (k = 1; k <= n; k *= 2) {
sort(sa, sa + n + 1, compare_sa);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++) {
Rank[i] = tmp[i];
}
}
}
void construct_lcp(string S, int *sa, int *lcp) {
int n = S.length();
for (int i = 0; i <= n; i++)
Rank[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; i++) {
int j = sa[Rank[i] - 1];
if (h > 0)
h--;
for (; j + h < n and i + h < n; h++) {
if (S[j + h] != S[i + h])
break;
}
lcp[Rank[i] - 1] = h;
}
}
string S, T;
const int LMAX = 4008;
int sa[LMAX], lcp[LMAX];
int solve() {
int sl = S.length();
S += '_' + T;
construct_sa(S, sa);
construct_lcp(S, sa, lcp);
int ans = 0;
for (int i = 0; i < S.length(); i++) {
if ((sa[i] < sl) != (sa[i + 1] < sl)) {
ans = max(ans, lcp[i]);
}
}
return ans;
}
int main() {
while (cin >> S >> T) {
cout << solve() << endl;
}
} | // AOJ 0528
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int NMAX = 20000;
int n, k;
int Rank[NMAX + 1];
int tmp[NMAX + 1];
bool compare_sa(int i, int j) {
if (Rank[i] != Rank[j])
return Rank[i] < Rank[j];
else {
int ri = i + k <= n ? Rank[i + k] : -1;
int rj = j + k <= n ? Rank[j + k] : -1;
return ri < rj;
}
}
void construct_sa(string S, int *sa) {
n = S.length();
for (int i = 0; i <= n; i++) {
sa[i] = i;
Rank[i] = i < n ? S[i] : -1;
}
for (k = 1; k <= n; k *= 2) {
sort(sa, sa + n + 1, compare_sa);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++) {
Rank[i] = tmp[i];
}
}
}
void construct_lcp(string S, int *sa, int *lcp) {
int n = S.length();
for (int i = 0; i <= n; i++)
Rank[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; i++) {
int j = sa[Rank[i] - 1];
if (h > 0)
h--;
for (; j + h < n and i + h < n; h++) {
if (S[j + h] != S[i + h])
break;
}
lcp[Rank[i] - 1] = h;
}
}
string S, T;
const int LMAX = 20000;
int sa[LMAX], lcp[LMAX];
int solve() {
int sl = S.length();
S += '_' + T;
construct_sa(S, sa);
construct_lcp(S, sa, lcp);
int ans = 0;
for (int i = 0; i < S.length(); i++) {
if ((sa[i] < sl) != (sa[i + 1] < sl)) {
ans = max(ans, lcp[i]);
}
}
return ans;
}
int main() {
while (cin >> S >> T) {
cout << solve() << endl;
}
} | replace | 67 | 68 | 67 | 68 | 0 | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SuffixArray {
void CEB(vector<int> &v, vector<int> &b) {
int vs = v.size(), bs = b.size();
fill(b.begin(), b.end(), 0);
for (int i = 0; i < vs; i++)
b[v[i]]++;
for (int i = 1; i < bs; i++)
b[i] += b[i - 1];
}
void ISort(vector<int> &v, vector<int> &SA, int mv, vector<int> &b,
vector<int> &isL) {
int vs = v.size(), bs = b.size();
fill(b.begin(), b.end(), 0);
for (int i = 0; i < vs; i++)
b[v[i]]++;
int sum = 0;
for (int i = 0; i < bs; i++)
b[i] += sum, swap(sum, b[i]);
for (int i = 0; i < vs; i++) {
if (SA[i] > 0 && isL[SA[i] - 1])
SA[b[v[SA[i] - 1]]++] = SA[i] - 1;
}
}
void IISort(vector<int> &v, vector<int> &SA, int mv, vector<int> &b,
vector<int> &isL) {
CEB(v, b);
for (int i = v.size() - 1; i >= 0; i--) {
if (SA[i] > 0 && !isL[SA[i] - 1])
SA[--b[v[SA[i] - 1]]] = SA[i] - 1;
}
}
vector<int> SA_IS(vector<int> v, int mv) {
int vs = v.size();
if (vs == 1)
return vector<int>(1, 0);
vector<int> isL(vs), b(mv + 1), SA(vs, -1), ord(vs);
auto isLMS = [&](int x) -> bool { return x > 0 && isL[x - 1] && !isL[x]; };
for (int i = vs - 2; i >= 0; i--)
isL[i] = (v[i] > v[i + 1]) || (v[i] == v[i + 1] && isL[i + 1]);
CEB(v, b);
for (int i = 0; i < vs; i++) {
if (isLMS(i))
SA[--b[v[i]]] = i;
}
ISort(v, SA, mv, b, isL);
IISort(v, SA, mv, b, isL);
int cur = 0;
for (int i = 0; i < vs; i++) {
if (isLMS(i))
ord[i] = cur++;
}
vector<int> nxv(cur);
cur = -1;
int prev = -1;
for (int i = 0; i < vs; i++) {
if (!isLMS(SA[i]))
continue;
bool diff = false;
for (int d = 0; d < vs; d++) {
if (prev == -1 || v[SA[i] + d] != v[prev + d] ||
isL[SA[i] + d] != isL[prev + d]) {
diff = true;
break;
} else if (d && isLMS(SA[i] + d))
break;
}
if (diff)
cur++, prev = SA[i];
nxv[ord[SA[i]]] = cur;
}
vector<int> reord(nxv.size());
for (int i = 0; i < vs; i++) {
if (isLMS(i))
reord[ord[i]] = i;
}
vector<int> nxSA = SA_IS(nxv, cur);
CEB(v, b);
for (int i = 0; i < SA.size(); i++)
SA[i] = -1;
for (int i = nxSA.size() - 1; i >= 0; i--) {
SA[--b[v[reord[nxSA[i]]]]] = reord[nxSA[i]];
}
ISort(v, SA, mv, b, isL);
IISort(v, SA, mv, b, isL);
return SA;
}
vector<int> SA_IS(string s) {
vector<int> v(s.size() + 1);
for (int i = 0; i < s.size(); i++)
v[i] = s[i] + 1;
return SA_IS(v, *max_element(v.begin(), v.end()));
}
vector<int> construct_lcp(string &s, vector<int> &sa) {
vector<int> lcp, rank(s.size() + 1);
int n = s.size(), h = 0;
for (int i = 0; i <= n; i++)
rank[sa[i]] = i;
for (int i = 0; i < n; i++) {
int j = sa[rank[i] - 1];
if (h)
h--;
for (; j + h < n && i + h < n; h++) {
if (s[j + h] != s[i + h])
break;
}
lcp[rank[i] - 1] = h;
}
return lcp;
}
public:
string s;
vector<int> sa, lcp;
void init(string &T) {
s = T;
sa = SA_IS(s);
lcp = construct_lcp(s, sa);
}
SuffixArray(string &t) { init(t); }
SuffixArray() {}
bool contain(string &t) {
int a = 0, b = s.size();
while (b - a > 1) {
int c = (a + b) / 2;
if (s.compare(sa[c], t.size(), t) < 0)
a = c;
else
b = c;
}
return s.compare(sa[b], t.size(), t) == 0;
}
};
string s, t;
int main() {
while (cin >> s >> t) {
int sl = s.size();
s += '$' + t;
SuffixArray v(s);
int ret = 0, e = s.size();
for (int i = 0; i < e; i++) {
if ((v.sa[i] < sl) != (v.sa[i + 1] < sl))
ret = max(ret, v.lcp[i]);
}
cout << ret << endl;
}
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SuffixArray {
void CEB(vector<int> &v, vector<int> &b) {
int vs = v.size(), bs = b.size();
fill(b.begin(), b.end(), 0);
for (int i = 0; i < vs; i++)
b[v[i]]++;
for (int i = 1; i < bs; i++)
b[i] += b[i - 1];
}
void ISort(vector<int> &v, vector<int> &SA, int mv, vector<int> &b,
vector<int> &isL) {
int vs = v.size(), bs = b.size();
fill(b.begin(), b.end(), 0);
for (int i = 0; i < vs; i++)
b[v[i]]++;
int sum = 0;
for (int i = 0; i < bs; i++)
b[i] += sum, swap(sum, b[i]);
for (int i = 0; i < vs; i++) {
if (SA[i] > 0 && isL[SA[i] - 1])
SA[b[v[SA[i] - 1]]++] = SA[i] - 1;
}
}
void IISort(vector<int> &v, vector<int> &SA, int mv, vector<int> &b,
vector<int> &isL) {
CEB(v, b);
for (int i = v.size() - 1; i >= 0; i--) {
if (SA[i] > 0 && !isL[SA[i] - 1])
SA[--b[v[SA[i] - 1]]] = SA[i] - 1;
}
}
vector<int> SA_IS(vector<int> v, int mv) {
int vs = v.size();
if (vs == 1)
return vector<int>(1, 0);
vector<int> isL(vs), b(mv + 1), SA(vs, -1), ord(vs);
auto isLMS = [&](int x) -> bool { return x > 0 && isL[x - 1] && !isL[x]; };
for (int i = vs - 2; i >= 0; i--)
isL[i] = (v[i] > v[i + 1]) || (v[i] == v[i + 1] && isL[i + 1]);
CEB(v, b);
for (int i = 0; i < vs; i++) {
if (isLMS(i))
SA[--b[v[i]]] = i;
}
ISort(v, SA, mv, b, isL);
IISort(v, SA, mv, b, isL);
int cur = 0;
for (int i = 0; i < vs; i++) {
if (isLMS(i))
ord[i] = cur++;
}
vector<int> nxv(cur);
cur = -1;
int prev = -1;
for (int i = 0; i < vs; i++) {
if (!isLMS(SA[i]))
continue;
bool diff = false;
for (int d = 0; d < vs; d++) {
if (prev == -1 || v[SA[i] + d] != v[prev + d] ||
isL[SA[i] + d] != isL[prev + d]) {
diff = true;
break;
} else if (d && isLMS(SA[i] + d))
break;
}
if (diff)
cur++, prev = SA[i];
nxv[ord[SA[i]]] = cur;
}
vector<int> reord(nxv.size());
for (int i = 0; i < vs; i++) {
if (isLMS(i))
reord[ord[i]] = i;
}
vector<int> nxSA = SA_IS(nxv, cur);
CEB(v, b);
for (int i = 0; i < SA.size(); i++)
SA[i] = -1;
for (int i = nxSA.size() - 1; i >= 0; i--) {
SA[--b[v[reord[nxSA[i]]]]] = reord[nxSA[i]];
}
ISort(v, SA, mv, b, isL);
IISort(v, SA, mv, b, isL);
return SA;
}
vector<int> SA_IS(string s) {
vector<int> v(s.size() + 1);
for (int i = 0; i < s.size(); i++)
v[i] = s[i] + 1;
return SA_IS(v, *max_element(v.begin(), v.end()));
}
vector<int> construct_lcp(string &s, vector<int> &sa) {
vector<int> lcp(s.size()), rank(s.size() + 1);
int n = s.size(), h = 0;
for (int i = 0; i <= n; i++)
rank[sa[i]] = i;
for (int i = 0; i < n; i++) {
int j = sa[rank[i] - 1];
if (h)
h--;
for (; j + h < n && i + h < n; h++) {
if (s[j + h] != s[i + h])
break;
}
lcp[rank[i] - 1] = h;
}
return lcp;
}
public:
string s;
vector<int> sa, lcp;
void init(string &T) {
s = T;
sa = SA_IS(s);
lcp = construct_lcp(s, sa);
}
SuffixArray(string &t) { init(t); }
SuffixArray() {}
bool contain(string &t) {
int a = 0, b = s.size();
while (b - a > 1) {
int c = (a + b) / 2;
if (s.compare(sa[c], t.size(), t) < 0)
a = c;
else
b = c;
}
return s.compare(sa[b], t.size(), t) == 0;
}
};
string s, t;
int main() {
while (cin >> s >> t) {
int sl = s.size();
s += '$' + t;
SuffixArray v(s);
int ret = 0, e = s.size();
for (int i = 0; i < e; i++) {
if ((v.sa[i] < sl) != (v.sa[i + 1] < sl))
ret = max(ret, v.lcp[i]);
}
cout << ret << endl;
}
} | replace | 98 | 99 | 98 | 99 | -11 | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N = 4000;
int dp[N][N];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
while (1) {
cin >> s;
if (cin.eof() || s == "0") {
break;
}
cin >> t;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t j = 0; j < t.size(); ++j) {
dp[i][j] = 0;
}
}
int ans = 0;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t j = 0; j < t.size(); ++j) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
} else
dp[i + 1][j + 1] = 0;
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N = 4001;
int dp[N][N];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
while (1) {
cin >> s;
if (cin.eof() || s == "0") {
break;
}
cin >> t;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t j = 0; j < t.size(); ++j) {
dp[i][j] = 0;
}
}
int ans = 0;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t j = 0; j < t.size(); ++j) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
} else
dp[i + 1][j + 1] = 0;
}
}
cout << ans << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[4005][4005];
int main() {
string s;
while (cin >> s) {
string sa;
cin >> sa;
for (int i = 0; i < 4003; i++)
for (int j = 0; j < 4003; j++)
dp[i][j] = 0;
s.push_back('@');
int ans = 0;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < sa.size(); j++) {
int a = s.size();
int I = min(a - 1, i + j);
if (s[I] == sa[j]) {
dp[I][j + 1] = dp[I - 1][j] + 1;
ans = max(ans, dp[I][j + 1]);
} else
dp[I][j + 1] = 0;
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[4005][4005];
int main() {
string s;
while (cin >> s) {
string sa;
cin >> sa;
for (int i = 0; i < 4003; i++)
for (int j = 0; j < 4003; j++)
dp[i][j] = 0;
s.push_back('@');
int ans = 0;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < sa.size(); j++) {
int a = s.size();
if (s[i] == sa[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
} else {
dp[i + 1][j + 1] = 0;
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 20 | 26 | 20 | 27 | 0 | |
p00451 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s, t;
while (cin >> s >> t) {
int ans = 0;
vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1, 0));
for (unsigned int i = 0; i < s.size(); ++i) {
for (unsigned int j = 0; j < t.size(); ++j) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s, t;
while (cin >> s >> t) {
short int ans = 0;
vector<vector<short int>> dp(s.size() + 1,
vector<short int>(t.size() + 1, 0));
for (unsigned int i = 0; i < s.size(); ++i) {
for (unsigned int j = 0; j < t.size(); ++j) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
ans = max(ans, dp[i + 1][j + 1]);
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 9 | 11 | 9 | 12 | MLE | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int dp[4001][4001];
int main() {
string s;
string t;
int mx;
while (getline(cin, s)) {
fill(dp[0], dp[4000] + 4000, 0);
getline(cin, t);
int n = s.size(), m = t.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = 0;
}
}
}
mx = 0;
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++)
mx = max(mx, dp[i][j]);
cout << mx << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int dp[4002][4002];
int main() {
string s;
string t;
int mx;
while (getline(cin, s)) {
fill(dp[0], dp[4000] + 4000, 0);
getline(cin, t);
int n = s.size(), m = t.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = 0;
}
}
}
mx = 0;
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= m + 1; j++)
mx = max(mx, dp[i][j]);
cout << mx << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00451 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
short dp[4000][4000];
int main() {
for (;;) {
string s, t;
if (!(cin >> s >> t)) {
break;
}
for (int i = 0; i <= s.size(); i++) {
dp[i][0] = 0;
}
for (int j = 0; j <= t.size(); j++) {
dp[0][j] = 0;
}
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 0;
}
}
}
int ans = 0;
for (int i = 0; i <= s.size(); i++) {
for (int j = 0; j <= t.size(); j++) {
if (dp[i][j] > ans) {
ans = dp[i][j];
}
}
}
cout << ans << endl;
}
return 0;
}
| #include <iostream>
#include <string>
using namespace std;
short dp[4001][4001];
int main() {
for (;;) {
string s, t;
if (!(cin >> s >> t)) {
break;
}
for (int i = 0; i <= s.size(); i++) {
dp[i][0] = 0;
}
for (int j = 0; j <= t.size(); j++) {
dp[0][j] = 0;
}
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 0;
}
}
}
int ans = 0;
for (int i = 0; i <= s.size(); i++) {
for (int j = 0; j <= t.size(); j++) {
if (dp[i][j] > ans) {
ans = dp[i][j];
}
}
}
cout << ans << endl;
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p00451 | 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};
string str1;
string str2;
int main() {
while (cin >> str1 >> str2) {
int ans = 0;
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str1.length(); j++) {
int tmp = 0;
for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
if (str1[i + k] != str2[j + k]) {
break;
}
tmp++;
}
ans = max(ans, tmp);
}
}
cout << ans << 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};
string str1;
string str2;
int main() {
while (cin >> str1 >> str2) {
int ans = 0;
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str1.length(); j++) {
if (i > 0 && j > 0 && str1[i - 1] == str2[j - 1])
continue;
int tmp = 0;
for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
if (str1[i + k] != str2[j + k]) {
break;
}
tmp++;
}
ans = max(ans, tmp);
}
}
cout << ans << endl;
}
return 0;
} | insert | 44 | 44 | 44 | 46 | TLE | |
p00451 | C++ | Memory Limit Exceeded | #include <stdio.h>
#include <string.h>
#define max(i, j) (i < j ? j : i)
int dp[4001][4001];
int main(void) {
char s[4001], t[4001];
// printf("%d\n",4000*4000);
while (scanf("%s", s) != EOF) {
scanf("%s", t);
// dp[0][0]=s[0]==t[0];
for (int i = 0; i < 4001; i++)
dp[i][0] = dp[0][i] = 0;
int sl = strlen(s), tl = strlen(t);
int ans = 0;
for (int i = 1; i <= sl; i++) {
for (int j = 1; j <= tl; j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
ans = max(ans, dp[i][j]);
} else
dp[i][j] = 0;
}
}
printf("%d\n", ans);
}
return 0;
} | #include <stdio.h>
#include <string.h>
#define max(i, j) (i < j ? j : i)
short dp[4001][4001];
int main(void) {
char s[4001], t[4001];
// printf("%d\n",4000*4000);
while (scanf("%s", s) != EOF) {
scanf("%s", t);
// dp[0][0]=s[0]==t[0];
for (int i = 0; i < 4001; i++)
dp[i][0] = dp[0][i] = 0;
int sl = strlen(s), tl = strlen(t);
int ans = 0;
for (int i = 1; i <= sl; i++) {
for (int j = 1; j <= tl; j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
ans = max(ans, dp[i][j]);
} else
dp[i][j] = 0;
}
}
printf("%d\n", ans);
}
return 0;
} | replace | 3 | 4 | 3 | 4 | MLE | |
p00451 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (int)c.size())
#define fr(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(u) (u).rbegin(), (u).rend()
#define dbg(x) cerr << #x << " = " << (x) << endl
#define v_delete(a, b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector<T>>
#define init(a, b) memset((a), (b), sizeof((a)))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double EPS = 1e-9;
const long long INF = 100001000010000100ll;
template <class T> int SIZE(T a) { return a.size(); }
template <class T> string IntToString(T num) {
string res;
stringstream ss;
ss << num;
return ss.str();
}
template <class T> T StringToInt(string str) {
T res = 0;
for (int i = 0; i < SIZE(str); i++)
res = (res * 10 + str[i] - '0');
return res;
}
template <class T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T> void input(T *a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
typedef pair<int, int> pii;
string L, R;
int dp[4000 + 10][4000 + 10];
int main() {
while (cin >> L >> R) {
memset(dp, 0, sizeof(dp));
int ans = 0;
rp(i, R) {
rp(j, L) {
if (R[i] == L[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 0;
}
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (int)c.size())
#define fr(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(u) (u).rbegin(), (u).rend()
#define dbg(x) cerr << #x << " = " << (x) << endl
#define v_delete(a, b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector<T>>
#define init(a, b) memset((a), (b), sizeof((a)))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double EPS = 1e-9;
const long long INF = 100001000010000100ll;
template <class T> int SIZE(T a) { return a.size(); }
template <class T> string IntToString(T num) {
string res;
stringstream ss;
ss << num;
return ss.str();
}
template <class T> T StringToInt(string str) {
T res = 0;
for (int i = 0; i < SIZE(str); i++)
res = (res * 10 + str[i] - '0');
return res;
}
template <class T> T gcd(T a, T b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T> void input(T *a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
typedef pair<int, int> pii;
string L, R;
int dp[4000 + 10][4000 + 10];
int main() {
while (cin >> L >> R) {
memset(dp, 0, sizeof(dp));
int ans = 0;
rp(i, R) {
rp(j, L) {
if (R[i] == L[j]) {
if (i > 0 && j > 0) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 1;
}
} else {
dp[i][j] = 0;
}
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
}
return 0;
} | replace | 88 | 89 | 88 | 93 | 0 | |
p00451 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string a, b;
while (cin >> a >> b) {
int n = 0;
int la = a.size();
int lb = b.size();
for (int i = 0; i < la; i++) {
for (int j = 0; j < lb; j++) {
int k = 0, m = 0;
do {
if (a[i + k] == b[j + k]) {
k++;
m = 1;
if (i + k > la - 1 || j + k > lb - 1) {
m = 0;
}
} else {
m = 0;
}
} while (m == 1);
n = max(n, k);
}
}
cout << n << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string a, b;
while (cin >> a >> b) {
int n = 0;
int la = a.size();
int lb = b.size();
for (int i = 1 - la; i < lb; i++) {
int k = 0;
for (int j = 0; j < la; j++) {
if (j + i < 0 || j + i > lb - 1) {
k = 0;
} else if (a[j] == b[j + i]) {
k++;
n = max(n, k);
} else {
k = 0;
}
}
}
cout << n << endl;
}
return 0;
} | replace | 10 | 25 | 10 | 21 | TLE | |
p00451 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int DP[4096][4096];
int main() {
string s, t;
while (cin >> s >> t) {
memset(DP, 0, sizeof(DP));
int n = s.size(), m = t.size(), ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i] == t[j]) {
DP[i + 1][j + 1] = DP[i][j] + 1;
ans = max(DP[i + 1][j + 1], ans);
} else {
DP[i + 1][j + 1] = 0;
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int DP[4001][4001];
int main() {
string s, t;
while (cin >> s >> t) {
memset(DP, 0, sizeof(DP));
int n = s.size(), m = t.size(), ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i] == t[j]) {
DP[i + 1][j + 1] = DP[i][j] + 1;
ans = max(DP[i + 1][j + 1], ans);
} else {
DP[i + 1][j + 1] = 0;
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 6 | 7 | 6 | 8 | MLE | |
p00451 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define MAX_S 4000
#define MAX_T 4000
template <typename T> int longest_common_substring(T s, T t) {
int lens = s.size(), lent = t.size();
int dp[MAX_S + 1][MAX_T + 1] = {};
int maxi = 0;
rep(i, s.size()) {
rep(j, t.size()) {
if (s[i] == t[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = 0;
maxi = max(maxi, dp[i + 1][j + 1]);
}
}
return maxi;
}
int main() {
string a, b;
while (cin >> a >> b) {
cout << longest_common_substring(a, b) << endl;
}
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define MAX_S 4000
#define MAX_T 4000
template <typename T> int longest_common_substring(T s, T t) {
int lens = s.size(), lent = t.size();
static int dp[MAX_S + 1][MAX_T + 1];
rep(i, MAX_S + 1) rep(j, MAX_T + 1) dp[i][j] = 0;
int maxi = 0;
rep(i, s.size()) {
rep(j, t.size()) {
if (s[i] == t[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = 0;
maxi = max(maxi, dp[i + 1][j + 1]);
}
}
return maxi;
}
int main() {
string a, b;
while (cin >> a >> b) {
cout << longest_common_substring(a, b) << endl;
}
} | replace | 14 | 15 | 14 | 17 | -11 | |
p00451 | C++ | Memory Limit Exceeded | #include <stdio.h>
#include <string.h>
char a[4001];
char b[4001];
int dp[4001][4001];
int max(int a, int b) { return a > b ? a : b; }
int main() {
while (scanf(" %s", a) != EOF) {
scanf(" %s", b);
int n = strlen(a);
int m = strlen(b);
int i, j;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
dp[i][j] = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i] == b[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
}
}
int ans = 0;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
ans = max(ans, dp[i][j]);
printf("%d\n", ans);
}
} | #include <stdio.h>
#include <string.h>
char a[4001];
char b[4001];
short dp[4001][4001];
int max(int a, int b) { return a > b ? a : b; }
int main() {
while (scanf(" %s", a) != EOF) {
scanf(" %s", b);
int n = strlen(a);
int m = strlen(b);
int i, j;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
dp[i][j] = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i] == b[j])
dp[i + 1][j + 1] = dp[i][j] + 1;
}
}
int ans = 0;
for (i = 0; i <= n; i++)
for (j = 0; j <= m; j++)
ans = max(ans, dp[i][j]);
printf("%d\n", ans);
}
} | replace | 4 | 5 | 4 | 5 | MLE | |
p00451 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
int id2 = (id + 1) % 2;
int ret = 0;
int a[40004];
for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
memset(a, 0, sizeof(a));
for (int i = 0; i < (int)s[id].size(); i++) {
if (i + j < 0)
continue;
if (i + j >= (int)s[id2].size())
continue;
if (s[id][i] == s[id2][i + j])
a[i] = 1;
}
int co = 0;
for (int i = 0; i < (int)s[id].size() + 1; i++) {
if (a[i])
co++;
else {
ret = max(ret, co);
co = 0;
}
}
}
return ret;
}
int main() {
while (cin >> s[0]) {
cin >> s[1];
printf("%d\n", max(search(0), search(1)));
}
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
int id2 = (id + 1) % 2;
int ret = 0;
int a[40004];
for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
memset(a, 0, sizeof(a));
for (int i = 0; i < (int)s[id].size(); i++) {
if (i + j < 0)
continue;
if (i + j >= (int)s[id2].size())
continue;
if (s[id][i] == s[id2][i + j])
a[i] = 1;
}
int co = 0;
for (int i = 0; i < (int)s[id].size() + 1; i++) {
if (a[i])
co++;
else {
ret = max(ret, co);
co = 0;
}
}
}
return ret;
}
int main() {
while (cin >> s[0]) {
cin >> s[1];
printf("%d\n", search(0));
}
} | replace | 38 | 39 | 38 | 39 | TLE | |
p00451 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#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);
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
int ret = 0, c;
c = getchar();
while (!isdigit(c))
c = getchar();
while (isdigit(c)) {
ret *= 10;
ret += c - '0';
c = getchar();
}
return ret;
}
char str1[4010];
char str2[4010];
int main() {
while (~scanf("%s %s", str1, str2)) {
int ans = 0;
int len1 = strlen(str1);
int len2 = strlen(str2);
for (int i = 0; i + ans < len1; i++) {
for (int j = 0; j + ans < len2; j++) {
if (str1[i] == str2[j]) {
int tmp = 0;
int ii = i;
int jj = j;
while (str1[ii] && str1[ii++] == str2[jj++])
++tmp;
ans = max(ans, tmp);
}
}
}
print(ans);
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#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);
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
int ret = 0, c;
c = getchar();
while (!isdigit(c))
c = getchar();
while (isdigit(c)) {
ret *= 10;
ret += c - '0';
c = getchar();
}
return ret;
}
char str1[4010];
char str2[4010];
int main() {
while (~scanf("%s %s", str1, str2)) {
int ans = 0;
int len1 = strlen(str1);
int len2 = strlen(str2);
for (int off = -len1; off <= len2; off++) {
int len = 0;
for (int i = max(off, 0); i < min(len1 + off, len2); i++) {
if (str1[i - off] == str2[i])
ans = max(ans, ++len);
else
len = 0;
}
}
print(ans);
}
return 0;
} | replace | 58 | 68 | 58 | 65 | TLE | |
p00451 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;
char A[4002], B[4002];
int DP[4002];
int main() {
while (1) {
scanf("%s", A);
scanf("%s", B);
int ans = 0;
rep(i, strlen(A)) {
for (int j = strlen(B); j > -1; j--) {
if (A[i] == B[j]) {
if (i != 0 && j != 0) {
DP[j] = DP[j - 1];
} else
DP[j] = 0;
DP[j]++;
} else {
DP[j] = 0;
}
ans = max(ans, DP[j]);
}
}
printf("%d\n", ans);
}
} | #include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;
char A[4002], B[4002];
int DP[4002];
int main() {
while (scanf("%s", A) != EOF) {
scanf("%s", B);
int ans = 0;
rep(i, strlen(A)) {
for (int j = strlen(B); j > -1; j--) {
if (A[i] == B[j]) {
if (i != 0 && j != 0) {
DP[j] = DP[j - 1];
} else
DP[j] = 0;
DP[j]++;
} else {
DP[j] = 0;
}
ans = max(ans, DP[j]);
}
}
printf("%d\n", ans);
}
} | replace | 12 | 14 | 12 | 13 | TLE | |
p00452 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
const int MAX_N = 105;
int n, m, p[MAX_N];
int x[MAX_N * MAX_N];
while (true) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
return 0;
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
p[n] = 0;
n++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i * n + j] = p[i] + p[j];
}
}
sort(x, x + n * n);
int ans = 0;
for (int i = 0; i < n * n; i++) {
int mp = m - x[i];
if (mp < 0)
continue;
int l = 0, r = n * n - 1;
int y = 0;
while (r - l >= 1) {
int c = (r + l) / 2;
if (x[c] == mp) {
y = x[c];
break;
} else if (x[c] < mp) {
y = x[c];
l = c + 1;
} else
r = c;
}
ans = max(ans, y + x[i]);
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
const int MAX_N = 1005;
int n, m, p[MAX_N];
int x[MAX_N * MAX_N];
while (true) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
return 0;
for (int i = 0; i < n; i++)
scanf("%d", &p[i]);
p[n] = 0;
n++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i * n + j] = p[i] + p[j];
}
}
sort(x, x + n * n);
int ans = 0;
for (int i = 0; i < n * n; i++) {
int mp = m - x[i];
if (mp < 0)
continue;
int l = 0, r = n * n - 1;
int y = 0;
while (r - l >= 1) {
int c = (r + l) / 2;
if (x[c] == mp) {
y = x[c];
break;
} else if (x[c] < mp) {
y = x[c];
l = c + 1;
} else
r = c;
}
ans = max(ans, y + x[i]);
}
printf("%d\n", ans);
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;
INT P[1002];
INT Q[1002001];
INT solve(int N, INT M) {
INT answer = 0;
for (int i = 0; i < N; ++i) {
int q = Q[i];
int L = 0;
int R = N;
INT m = M - q;
while (L < R) {
int p = (R - L) / 2 + L;
INT t = q + Q[p];
if (t <= M)
answer = max(answer, t);
if (Q[p] > m) {
R = p - 1;
} else {
L = p + 1;
}
}
}
return answer;
}
int main() {
int n, m;
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; ++i) {
INT p;
cin >> p;
P[i] = p;
}
P[n] = 0;
for (int i = 0; i < n + 1; ++i) {
for (int j = 0; j < n + 1; ++j) {
Q[i * (n + 1) + j] = P[i] + P[j];
}
}
int length = (n + 1) * (n + 1);
sort(Q, Q + length);
cout << solve(length, m) << endl;
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;
INT P[1002];
INT Q[1002001];
INT solve(int N, INT M) {
INT answer = 0;
for (int i = 0; i < N; ++i) {
int q = Q[i];
int L = i;
int R = N;
INT m = M - q;
while (L < R) {
int p = (R - L) / 2 + L;
INT t = q + Q[p];
if (t <= M)
answer = max(answer, t);
if (Q[p] > m) {
R = p - 1;
} else {
L = p + 1;
}
}
}
return answer;
}
int main() {
int n, m;
while (cin >> n >> m) {
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; ++i) {
INT p;
cin >> p;
P[i] = p;
}
P[n] = 0;
for (int i = 0; i < n + 1; ++i) {
for (int j = 0; j < n + 1; ++j) {
Q[i * (n + 1) + j] = P[i] + P[j];
}
}
int length = (n + 1) * (n + 1);
sort(Q, Q + length);
cout << solve(length, m) << endl;
}
return 0;
} | replace | 29 | 30 | 29 | 30 | TLE | |
p00452 | C++ | Runtime Error | #include "stdio.h"
#include <algorithm>
#include <iostream>
using namespace std;
long long int gten[1000000];
long long int p[1111];
long long int ans[1000];
int main() {
int ww = 0;
while (1) {
long long int n, m;
scanf("%lld %lld", &n, &m);
if (n == 0 && m == 0) {
break;
}
p[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &(p[i]));
}
int nw = 0;
for (int i = 0; i <= n; i++) {
for (int ii = 0; ii <= n; ii++) {
gten[nw] = p[i] + p[ii];
nw++;
}
}
sort(gten, gten + nw);
long long int max = 0;
int ai = 0;
for (int i = nw - 1; i >= 0; i--) {
while (1) {
if (ai + 1 >= nw) {
break;
}
if (gten[i] + gten[ai + 1] <= m) {
ai++;
if (max < gten[i] + gten[ai]) {
max = gten[i] + gten[ai];
}
} else {
break;
}
}
}
ans[ww] = max;
ww++;
}
for (int i = 0; i < ww; i++) {
printf("%lld\n", ans[i]);
}
return 0;
} | #include "stdio.h"
#include <algorithm>
#include <iostream>
using namespace std;
long long int gten[1111111];
long long int p[1111];
long long int ans[1000];
int main() {
int ww = 0;
while (1) {
long long int n, m;
scanf("%lld %lld", &n, &m);
if (n == 0 && m == 0) {
break;
}
p[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &(p[i]));
}
int nw = 0;
for (int i = 0; i <= n; i++) {
for (int ii = 0; ii <= n; ii++) {
gten[nw] = p[i] + p[ii];
nw++;
}
}
sort(gten, gten + nw);
long long int max = 0;
int ai = 0;
for (int i = nw - 1; i >= 0; i--) {
while (1) {
if (ai + 1 >= nw) {
break;
}
if (gten[i] + gten[ai + 1] <= m) {
ai++;
if (max < gten[i] + gten[ai]) {
max = gten[i] + gten[ai];
}
} else {
break;
}
}
}
ans[ww] = max;
ww++;
}
for (int i = 0; i < ww; i++) {
printf("%lld\n", ans[i]);
}
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
unsigned int N, M;
while (cin >> N >> M, N || M) {
vector<unsigned int> P(N + 1);
P[0] = 0;
for (int i = 1; i <= N; i++)
cin >> P[i];
vector<unsigned int> P2;
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
P2.push_back(P[i] + P[j]);
}
}
sort(P2.begin(), P2.end());
unsigned int ans = 0;
for (int i = 0; i < P2.size(); i++) {
if (P2[i] > M)
continue;
unsigned int left = 0, right = P2.size() - 1;
unsigned int mid;
for (int j = 0; j < 100; j++) {
mid = (left + right) / 2;
if (P2[i] + P2[mid] > M) {
right = mid;
} else {
left = mid;
}
}
ans = max(ans, P2[i] + P2[mid]);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
unsigned int N, M;
while (cin >> N >> M, N || M) {
vector<unsigned int> P(N + 1);
P[0] = 0;
for (int i = 1; i <= N; i++)
cin >> P[i];
vector<unsigned int> P2;
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
P2.push_back(P[i] + P[j]);
}
}
sort(P2.begin(), P2.end());
unsigned int ans = 0;
for (int i = 0; i < P2.size(); i++) {
if (P2[i] > M)
break;
unsigned int left = 0, right = P2.size() - 1;
unsigned int mid;
for (int j = 0; j < 100; j++) {
mid = (left + right) / 2;
if (P2[i] + P2[mid] > M) {
right = mid;
} else {
left = mid;
}
}
ans = max(ans, P2[i] + P2[mid]);
}
cout << ans << endl;
}
return 0;
} | replace | 25 | 26 | 25 | 26 | TLE | |
p00452 | C++ | Runtime Error | #include <stdio.h>
#include <stdlib.h>
int n, m;
int a[1001];
int b[51000];
bool f[1001][1001];
int i, j;
int ans;
int c;
int max(int a, int b) {
if (a >= b)
return a;
else
return b;
}
int comp(const void *a, const void *b) {
int p = *(int *)a;
int q = *(int *)b;
return p - q;
}
int search(int num) {
int r = 0;
int l = c;
int mid;
for (int k = 0; k < 25; k++) {
mid = (int)(r + l) / 2;
if (num - b[mid + 1] >= 0 && num - b[mid] > num - b[mid + 1])
mid = mid + 1;
if (b[mid] > num)
l = mid;
else
r = mid;
}
return mid;
}
int main() {
while (1) {
scanf("%d %d", &n, &m);
if (n == 0 && m == 0)
return 0;
ans = 0;
c = 0;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
a[n] = 0;
for (i = 0; i < n + 1; i++)
for (j = 0; j < n + 1; j++)
f[i][j] = false;
for (i = 0; i < n + 1; i++)
for (j = 0; j < n + 1; j++) {
if (!f[j][i] && a[i] + a[j] <= m) {
b[c++] = a[i] + a[j];
f[i][j] = true;
}
}
qsort(b, c, sizeof(int), comp);
for (i = 0; i < c; i++) {
if (m - b[i] >= 0) {
ans = max(ans, b[search(m - b[i])] + b[i]);
}
}
printf("%d\n", ans);
}
} | #include <stdio.h>
#include <stdlib.h>
int n, m;
int a[1001];
int b[510000];
bool f[1001][1001];
int i, j;
int ans;
int c;
int max(int a, int b) {
if (a >= b)
return a;
else
return b;
}
int comp(const void *a, const void *b) {
int p = *(int *)a;
int q = *(int *)b;
return p - q;
}
int search(int num) {
int r = 0;
int l = c;
int mid;
for (int k = 0; k < 25; k++) {
mid = (int)(r + l) / 2;
if (num - b[mid + 1] >= 0 && num - b[mid] > num - b[mid + 1])
mid = mid + 1;
if (b[mid] > num)
l = mid;
else
r = mid;
}
return mid;
}
int main() {
while (1) {
scanf("%d %d", &n, &m);
if (n == 0 && m == 0)
return 0;
ans = 0;
c = 0;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
a[n] = 0;
for (i = 0; i < n + 1; i++)
for (j = 0; j < n + 1; j++)
f[i][j] = false;
for (i = 0; i < n + 1; i++)
for (j = 0; j < n + 1; j++) {
if (!f[j][i] && a[i] + a[j] <= m) {
b[c++] = a[i] + a[j];
f[i][j] = true;
}
}
qsort(b, c, sizeof(int), comp);
for (i = 0; i < c; i++) {
if (m - b[i] >= 0) {
ans = max(ans, b[search(m - b[i])] + b[i]);
}
}
printf("%d\n", ans);
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00452 | C++ | Runtime Error | #include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.dart";
string NAME;
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
/*
解法3 O(n^2log(n))
3本の矢を投げて、残りの矢を2分探索する
*/
int n, m, k;
int p[1005];
int s[1005];
bool query(int c, int sum) { return (sum + s[c] <= m); }
int solve(int sum) {
int L = 0;
int R = k;
while (R - L > 1) {
int C = (L + R) / 2;
(query(C, sum) ? L : R) = C;
} // end while
return sum + s[L];
}
int main() {
// cut here before submit
#if DEBUG
NAME = FILE_NAME;
int CNT = 1;
NAME += itos(CNT);
while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
while (scanf("%d %d", &n, &m), n, m) {
memset(p, 0, sizeof(p));
memset(s, 0, sizeof(s));
for (int i = 1; i <= n; i++)
scanf("%d", &p[i]);
sort(p, p + n + 1);
k = 0;
for (int a = 0; a <= n; a++) {
for (int b = 0; b <= n; b++) {
if (p[a] + p[b] > m)
continue;
s[k++] = p[a] + p[b];
} // end for
} // end for
sort(s, s + k);
int res = 0;
rep(i, k) {
int curr = solve(s[i]);
res = max(res, curr);
} // end rep
printf("%d\n", res);
} // end while
#if DEBUG
CNT++; // cut here before submit
NAME = FILE_NAME;
NAME += itos(CNT);
} // end loop; cut here before submit
#endif
return 0;
} | #include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.dart";
string NAME;
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
/*
解法3 O(n^2log(n))
3本の矢を投げて、残りの矢を2分探索する
*/
int n, m, k;
int p[1005];
int s[1005 * 1005];
bool query(int c, int sum) { return (sum + s[c] <= m); }
int solve(int sum) {
int L = 0;
int R = k;
while (R - L > 1) {
int C = (L + R) / 2;
(query(C, sum) ? L : R) = C;
} // end while
return sum + s[L];
}
int main() {
// cut here before submit
#if DEBUG
NAME = FILE_NAME;
int CNT = 1;
NAME += itos(CNT);
while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
while (scanf("%d %d", &n, &m), n, m) {
memset(p, 0, sizeof(p));
memset(s, 0, sizeof(s));
for (int i = 1; i <= n; i++)
scanf("%d", &p[i]);
sort(p, p + n + 1);
k = 0;
for (int a = 0; a <= n; a++) {
for (int b = 0; b <= n; b++) {
if (p[a] + p[b] > m)
continue;
s[k++] = p[a] + p[b];
} // end for
} // end for
sort(s, s + k);
int res = 0;
rep(i, k) {
int curr = solve(s[i]);
res = max(res, curr);
} // end rep
printf("%d\n", res);
} // end while
#if DEBUG
CNT++; // cut here before submit
NAME = FILE_NAME;
NAME += itos(CNT);
} // end loop; cut here before submit
#endif
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int tsP[1001 * 1001];
int main() {
int N, M;
int P[1001];
int ans;
int cnt;
P[0] = 0;
// cin >> N >> M;
while (cin >> N >> M, N && M) {
ans = 0;
cnt = 0;
for (int i = 1; i <= N; i++) {
cin >> P[i];
}
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
if (P[i] + P[j] <= M) {
tsP[cnt++] = P[i] + P[j];
}
}
}
sort(tsP, tsP + cnt);
for (int i = 0; i < cnt; i++) {
for (int j = i; j < cnt; j++) {
if (tsP[i] + tsP[j] <= M) {
ans = max(ans, tsP[i] + tsP[j]);
}
}
}
cout << ans << endl;
}
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int tsP[1001 * 1001];
int main() {
int N, M;
int P[1001];
int ans;
int cnt;
P[0] = 0;
// cin >> N >> M;
while (cin >> N >> M, N && M) {
ans = 0;
cnt = 0;
for (int i = 1; i <= N; i++) {
cin >> P[i];
}
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
if (P[i] + P[j] <= M) {
tsP[cnt++] = P[i] + P[j];
}
}
}
sort(tsP, tsP + cnt);
for (int i = 0; i < cnt; i++) {
// for (int j = i; j < cnt; j++) {
// if (tsP[i] + tsP[j] <= M) {
ans = max(ans, /*tsP[i] + tsP[j]*/ tsP[i] +
*(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
//}
//}
}
cout << ans << endl;
}
return 0;
}
| replace | 31 | 36 | 31 | 37 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
while (1) {
int n, m, a, ans = 0;
vector<int> p, pp;
scanf("%d%d", &n, &m);
p.push_back(0);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
p.push_back(a);
}
for (int i = 0; i <= n; i++) {
for (int j = i; j <= n; j++) {
pp.push_back(p[i] + p[j]);
}
}
sort(pp.begin(), pp.end());
int s = pp.size() - 1;
for (int i = 0; i <= s; i++) {
while (pp[i] + pp[s] > m)
s--;
if (ans < pp[i] + pp[s])
ans = pp[i] + pp[s];
}
printf("%d\n", ans);
}
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
while (1) {
int n, m, a, ans = 0;
vector<int> p, pp;
scanf("%d%d", &n, &m);
if (n == 0 && m == 0)
break;
p.push_back(0);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
p.push_back(a);
}
for (int i = 0; i <= n; i++) {
for (int j = i; j <= n; j++) {
pp.push_back(p[i] + p[j]);
}
}
sort(pp.begin(), pp.end());
int s = pp.size() - 1;
for (int i = 0; i <= s; i++) {
while (pp[i] + pp[s] > m)
s--;
if (ans < pp[i] + pp[s])
ans = pp[i] + pp[s];
}
printf("%d\n", ans);
}
} | insert | 11 | 11 | 11 | 13 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair
static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
// v.erase(unique(v.begin(),v.end()),v.end());
signed main() {
int n;
int m;
while (cin >> n >> m, n || m) {
// int n;
// cin>>n;
// int m;
// cin>>m;
vint v(n);
rep(i, n) { cin >> v[i]; }
v.PB(0);
vint tmp;
rep(i, n + 1) {
rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
}
sort(ALL(tmp));
int ans = 0;
tmp.erase(unique(ALL(tmp)), tmp.end());
rep(i, tmp.size()) {
rep(j, tmp.size()) {
if (tmp[i] + tmp[j] <= m) {
ans = max(ans, tmp[i] + tmp[j]);
}
}
}
cout << ans << endl;
}
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair
static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;
template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
// v.erase(unique(v.begin(),v.end()),v.end());
signed main() {
int n;
int m;
while (cin >> n >> m, n || m) {
// int n;
// cin>>n;
// int m;
// cin>>m;
vint v(n);
rep(i, n) { cin >> v[i]; }
v.PB(0);
vint tmp;
rep(i, n + 1) {
rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
}
sort(ALL(tmp));
int ans = 0;
tmp.erase(unique(ALL(tmp)), tmp.end());
rep(i, tmp.size()) {
vint::iterator it = upper_bound(ALL(tmp), m - tmp[i]);
it--;
if (*it + tmp[i] <= m) {
ans = max(ans, *it + tmp[i]);
}
}
cout << ans << endl;
}
} | replace | 60 | 64 | 60 | 64 | TLE | |
p00452 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 100;
int main() {
int N, M;
int point[MAX_N + 1];
int a[(MAX_N + 1) * (MAX_N + 1)];
for (;;) {
scanf("%d %d", &N, &M);
if (N == 0 && M == 0)
break;
point[0] = 0;
for (int i = 1; i <= N; i++)
scanf("%d", &point[i]);
int index = 0;
for (int i = 0; i < N + 1; i++) {
for (int j = 0; j < N + 1; j++) {
a[index] = point[i] + point[j];
index++;
}
}
sort(a, a + (N + 1) * (N + 1));
int num_a = (N + 1) * (N + 1);
int ans = 0;
for (int i = 0; i < num_a; i++) {
if (a[i] > M)
break;
int lb = 0, ub = num_a - 1;
int mid;
while (ub - lb > 1) {
mid = (lb + ub) / 2;
if (a[i] + a[mid] > M)
ub = mid;
else
lb = mid;
}
mid = (lb + ub) / 2;
if (a[i] + a[mid] > ans) {
ans = a[i] + a[mid];
}
}
printf("%d\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 1000;
int main() {
int N, M;
int point[MAX_N + 1];
int a[(MAX_N + 1) * (MAX_N + 1)];
for (;;) {
scanf("%d %d", &N, &M);
if (N == 0 && M == 0)
break;
point[0] = 0;
for (int i = 1; i <= N; i++)
scanf("%d", &point[i]);
int index = 0;
for (int i = 0; i < N + 1; i++) {
for (int j = 0; j < N + 1; j++) {
a[index] = point[i] + point[j];
index++;
}
}
sort(a, a + (N + 1) * (N + 1));
int num_a = (N + 1) * (N + 1);
int ans = 0;
for (int i = 0; i < num_a; i++) {
if (a[i] > M)
break;
int lb = 0, ub = num_a - 1;
int mid;
while (ub - lb > 1) {
mid = (lb + ub) / 2;
if (a[i] + a[mid] > M)
ub = mid;
else
lb = mid;
}
mid = (lb + ub) / 2;
if (a[i] + a[mid] > ans) {
ans = a[i] + a[mid];
}
}
printf("%d\n", ans);
}
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00452 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long int ll;
int N, M;
ll P[1111];
ll PP[1111];
int p;
int main(int argc, char *argv[]) {
while (cin >> N >> M, N) {
int NN = N * N;
rep(i, N) cin >> P[i];
rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
sort(P, P + N);
sort(PP, PP + NN);
ll res = 0;
res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
rep(i, N) res =
max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
res = max(res, PP[p] + PP[i]);
rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
res = max(res, PP[p] + PP[i]);
cout << res << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long int ll;
int N, M;
ll P[1111];
ll PP[11111111];
int p;
int main(int argc, char *argv[]) {
while (cin >> N >> M, N) {
int NN = N * N;
rep(i, N) cin >> P[i];
rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
sort(P, P + N);
sort(PP, PP + NN);
ll res = 0;
res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
rep(i, N) res =
max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
res = max(res, PP[p] + PP[i]);
rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
res = max(res, PP[p] + PP[i]);
cout << res << endl;
}
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00452 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#define INF 200000000
using namespace std;
int point2[500500];
int search(int a[], int n, int key) {
int left = 0;
int right = n;
int mid;
while (left < right) {
mid = (left + right) / 2;
if (a[mid] == key)
return a[mid];
else if (a[mid] < key)
left = mid + 1;
else
right = mid;
}
return a[left - 1];
}
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
int point[n + 1];
for (int i = 0; i < n; i++)
cin >> point[i];
point[n] = 0;
int k = 0;
for (int i = 0; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
point2[k] = point[i] + point[j];
k++;
}
}
int np = k;
sort(point2, point2 + np);
int ans = 0;
for (int i = 0; i < np; i++) {
if (point2[i] > m)
break;
int s = search(point2, np, m - point2[i]);
int t = s + point2[i];
if (t > ans)
ans = t;
}
cout << ans << endl;
}
} | #include <algorithm>
#include <iostream>
#define INF 200000000
using namespace std;
int point2[1000000];
int search(int a[], int n, int key) {
int left = 0;
int right = n;
int mid;
while (left < right) {
mid = (left + right) / 2;
if (a[mid] == key)
return a[mid];
else if (a[mid] < key)
left = mid + 1;
else
right = mid;
}
return a[left - 1];
}
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
return 0;
int point[n + 1];
for (int i = 0; i < n; i++)
cin >> point[i];
point[n] = 0;
int k = 0;
for (int i = 0; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
point2[k] = point[i] + point[j];
k++;
}
}
int np = k;
sort(point2, point2 + np);
int ans = 0;
for (int i = 0; i < np; i++) {
if (point2[i] > m)
break;
int s = search(point2, np, m - point2[i]);
int t = s + point2[i];
if (t > ans)
ans = t;
}
cout << ans << endl;
}
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00452 | C++ | Runtime Error | #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 (int 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;
int N, M, P[1001];
int main() {
while (scanf("%d%d", &N, &M) && N + M) {
rep(i, N) scanf("%d", P + i);
P[N] = 0;
vi p;
rep(i, N + 1) rep(j, N + 1) if (P[i] + P[j] <= M) p.PB(P[i] + P[j]);
sort(p.begin(), p.end());
int res = 0;
rep(i, p.size()) {
int d = M - p[i];
int l = p[0], h = p[p.size() - 1];
int m;
while (h - l > 1) {
m = (l + h) / 2;
if (p[m] < d)
l = m;
else if (p[m] > d)
h = m;
else
break;
}
if (p[m] > d)
m--;
// printf("%d %d\n", p[m], p[i]);
res = max(res, p[m] + p[i]);
}
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 (int 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;
int N, M, P[1001];
int main() {
while (scanf("%d%d", &N, &M) && N + M) {
rep(i, N) scanf("%d", P + i);
P[N] = 0;
vi p;
rep(i, N + 1) rep(j, N + 1) if (P[i] + P[j] <= M) p.PB(P[i] + P[j]);
sort(p.begin(), p.end());
int res = 0;
rep(i, p.size()) {
int d = M - p[i];
int l = 0, h = p.size();
int m;
while (h - l > 1) {
m = (l + h) / 2;
if (p[m] < d)
l = m;
else if (p[m] > d)
h = m;
else
break;
}
if (p[m] > d)
m--;
// printf("%d %d\n", p[m], p[i]);
res = max(res, p[m] + p[i]);
}
printf("%d\n", res);
}
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<int> point(n);
REP(i, n) { cin >> point[i]; }
point.push_back(0);
vector<int> sum = point;
REP(i, n + 1) {
REP(j, n + 1) {
if (count(sum.begin(), sum.end(), sum[i] + sum[j]) == 0)
sum.push_back(sum[i] + sum[j]);
}
}
SORT(sum);
int ma = 0;
REP(i, sum.size()) {
int left = 0;
int right = sum.size() - 1;
while (left < right) {
if (sum[left + (right - left) / 2] + sum[i] <= m) {
ma = max(sum[left + (right - left) / 2] + sum[i], ma);
left = left + (right - left) / 2 + 1;
} else {
right = left + (right - left) / 2 - 1;
}
}
}
cout << ma << endl;
}
} | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int n, m;
while (cin >> n >> m, n) {
vector<int> point(n);
REP(i, n) { cin >> point[i]; }
point.push_back(0);
vector<int> sum = point;
REP(i, n + 1) {
REP(j, n + 1) { sum.push_back(sum[i] + sum[j]); }
}
SORT(sum);
int ma = 0;
REP(i, sum.size()) {
int left = 0;
int right = sum.size() - 1;
while (left < right) {
if (sum[left + (right - left) / 2] + sum[i] <= m) {
ma = max(sum[left + (right - left) / 2] + sum[i], ma);
left = left + (right - left) / 2 + 1;
} else {
right = left + (right - left) / 2 - 1;
}
}
}
cout << ma << endl;
}
} | replace | 54 | 58 | 54 | 55 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define SCAN(x) cin >> x
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<long double> P;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
#ifdef WIN32
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
if (i)
os << " ";
os << v[i];
}
return os;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
while (cin >> n >> m && n && m) {
VI p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
p.push_back(0);
SORT(p);
set<int> two_selected;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n + 1; j++) {
two_selected.insert(p[i] + p[j]);
}
}
int ans = 0;
for (set<int>::iterator it = two_selected.begin(); it != two_selected.end();
++it) {
set<int>::iterator it2 = two_selected.upper_bound(m - *it);
if (it2 != two_selected.begin()) {
--it2;
ans = max(ans, *it + *it2);
}
}
PRINT(ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define SCAN(x) cin >> x
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define PB push_back
#define EACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<long double> P;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef complex<double> Point;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
#ifdef WIN32
#define dump(x) cerr << #x << " = " << (x) << "\n"
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
if (i)
os << " ";
os << v[i];
}
return os;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
while (cin >> n >> m && n && m) {
VI p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
p.push_back(0);
SORT(p);
set<int> two_selected;
for (int i = 0; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
two_selected.insert(p[i] + p[j]);
}
}
int ans = 0;
for (set<int>::iterator it = two_selected.begin(); it != two_selected.end();
++it) {
set<int>::iterator it2 = two_selected.upper_bound(m - *it);
if (it2 != two_selected.begin()) {
--it2;
ans = max(ans, *it + *it2);
}
}
PRINT(ans);
}
return 0;
} | replace | 73 | 74 | 73 | 74 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;
int main() {
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
a.push_back(0);
all.clear();
for (int i = 0; i < a.size(); i++)
for (int j = i; j < a.size(); j++)
all.push_back(a[i] + a[j]);
sort(all.begin(), all.end());
int ans = 0;
int allsize = all.size();
int b = 0;
for (int i = allsize - 1; i >= 0; i--) {
for (int j = b; j < allsize; j++) {
int sum = all[i] + all[j];
if (sum > m)
break;
ans = max(ans, sum);
}
if (ans == m)
break;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;
int main() {
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
a.push_back(0);
all.clear();
for (int i = 0; i < a.size(); i++)
for (int j = i; j < a.size(); j++)
all.push_back(a[i] + a[j]);
sort(all.begin(), all.end());
int ans = 0;
int allsize = all.size();
int b = 0;
for (int i = allsize - 1; i >= 0; i--) {
for (int j = b; j < allsize; j++) {
int sum = all[i] + all[j];
if (sum > m)
break;
b++;
ans = max(ans, sum);
}
if (ans == m)
break;
}
cout << ans << endl;
}
return 0;
} | insert | 35 | 35 | 35 | 36 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n + m) {
vector<int> v;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
int size = v.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
v.push_back(v[i] + v[j]);
}
}
sort(v.begin(), v.end());
int ans = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size(); j++) {
if (v[i] + v[j] <= m)
ans = max(ans, v[i] + v[j]);
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n + m) {
vector<int> v;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back(a);
}
int size = v.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
v.push_back(v[i] + v[j]);
}
}
sort(v.begin(), v.end());
int ans = 0;
for (int i = 0; i < v.size(); i++) {
int want = m - v[i];
int l = 0, r = v.size() - 1;
if (want < 0)
continue;
while (r - l > 1) {
int mid = (l + r) / 2;
if (v[mid] >= want) {
r = mid;
} else {
l = mid;
}
if (v[l] + v[i] <= m) {
ans = max(ans, v[l] + v[i]);
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 28 | 31 | 28 | 44 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n && m) {
vector<int> point(n + 1);
vector<int> sum;
for (int i = 0; i < n; i++) {
cin >> point[i];
}
for (int i = 0; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
sum.push_back(point[i] + point[j]);
}
}
sort(sum.begin(), sum.end());
int ans = 0;
for (int i = 0; i < sum.size(); i++) {
int t = m - sum[i];
cout << "t = " << t << endl;
vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
if (it != sum.begin()) {
ans = max(ans, sum[i] + *(it - 1));
}
}
cout << ans << endl;
}
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n && m) {
vector<int> point(n + 1);
vector<int> sum;
for (int i = 0; i < n; i++) {
cin >> point[i];
}
for (int i = 0; i < n + 1; i++) {
for (int j = i; j < n + 1; j++) {
sum.push_back(point[i] + point[j]);
}
}
sort(sum.begin(), sum.end());
int ans = 0;
for (int i = 0; i < sum.size(); i++) {
int t = m - sum[i];
vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
if (it != sum.begin()) {
ans = max(ans, sum[i] + *(it - 1));
}
}
cout << ans << endl;
}
} | delete | 25 | 26 | 25 | 25 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
int N, M;
int NN;
int *dd;
int *d;
int search(int var) {
int i = 0, j = NN - 1;
int cut;
while (i != j) {
cut = (i + j) / 2;
if (dd[cut] + var > M) {
j = cut - 1;
} else if (dd[cut] + var < M) {
i = cut + 1;
} else
return dd[cut] + var;
}
if (var + dd[cut] <= M)
return dd[cut] + var;
return -1;
}
int main(void) {
while (cin >> N >> M, N && M) {
NN = (N + 1) * (N + 1);
d = new int[N + 1];
dd = new int[NN];
d[0] = 0;
for (int i = 1; i <= N; i++)
cin >> d[i];
int c = 0;
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++, c++)
dd[c] = d[i] + d[j];
sort(dd, dd + NN);
c = 0;
int num = 0;
int MAX = 0;
for (int i = 0; i < NN; i++) {
if (dd[i] > M)
break;
num = search(dd[i]);
if (num > MAX)
MAX = num;
}
cout << MAX << endl;
delete[] d;
delete[] dd;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int N, M;
int NN;
int *dd;
int *d;
int search(int var) {
int i = 0, j = NN - 1;
int cut;
while (i != j) {
cut = (i + j) / 2;
if (dd[cut] + var > M) {
j = cut;
} else if (dd[cut] + var < M) {
i = cut + 1;
} else
return dd[cut] + var;
}
if (var + dd[cut] <= M)
return dd[cut] + var;
return -1;
}
int main(void) {
while (cin >> N >> M, N && M) {
NN = (N + 1) * (N + 1);
d = new int[N + 1];
dd = new int[NN];
d[0] = 0;
for (int i = 1; i <= N; i++)
cin >> d[i];
int c = 0;
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++, c++)
dd[c] = d[i] + d[j];
sort(dd, dd + NN);
c = 0;
int num = 0;
int MAX = 0;
for (int i = 0; i < NN; i++) {
if (dd[i] > M)
break;
num = search(dd[i]);
if (num > MAX)
MAX = num;
}
cout << MAX << endl;
delete[] d;
delete[] dd;
}
return 0;
} | replace | 16 | 17 | 16 | 17 | TLE | |
p00452 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
int point[1000 + 10] = {0};
vector<int> sec;
int sum;
int i, j;
while (cin >> n >> m, n != 0 && m != 0) {
sec.clear();
sum = 0;
for (i = 1; i <= n; i++) {
cin >> point[i];
}
for (i = 0; i <= n; i++) {
for (j = i; j <= n; j++) {
sec.push_back(point[i] + point[j]);
}
}
sort(sec.begin(), sec.end());
for (i = sec.size() - 1; i >= 0; i--) {
for (j = i; j >= 0; j--) {
if (sec[i] + sec[j] <= m) {
sum = max(sum, sec[i] + sec[j]);
break;
}
}
}
cout << sum << endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m;
int point[1000 + 10] = {0};
vector<int> sec;
int sum;
int i, j;
while (cin >> n >> m, n != 0 && m != 0) {
sec.clear();
sum = 0;
for (i = 1; i <= n; i++) {
cin >> point[i];
}
for (i = 0; i <= n; i++) {
for (j = i; j <= n; j++) {
sec.push_back(point[i] + point[j]);
}
}
sort(sec.begin(), sec.end());
j = sec.size() - 1;
for (i = 0; i <= sec.size() - 1; i++) {
// cout <<"A"<< j << " " <<i << endl;
while (sec[i] + sec[j] > m && j > 0)
j--;
if (j == 0 && sec[i] + sec[j] > m)
j--;
// cout << "c " << j << endl;
if (j < 0)
break;
// cout << "B" << sec[i] << " " << sec[j] << endl;
sum = max(sum, sec[i] + sec[j]);
}
cout << sum << endl;
}
return 0;
}
| replace | 29 | 36 | 29 | 44 | TLE | |
p00452 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int sum[1001100];
int main() {
int n, d;
int m[1111];
while (cin >> n >> d, n) {
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
cin >> m[i];
}
m[0] = 0;
n++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i * n + j] = m[i] + m[j];
}
}
sort(sum, sum + n * n);
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int ab = m[i] + m[j];
if (ab <= d) {
int h = d - ab;
int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
if (tmp <= d) {
res = max(res, tmp);
}
}
}
}
cout << res << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int sum[1002100];
int main() {
int n, d;
int m[1111];
while (cin >> n >> d, n) {
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
cin >> m[i];
}
m[0] = 0;
n++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i * n + j] = m[i] + m[j];
}
}
sort(sum, sum + n * n);
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int ab = m[i] + m[j];
if (ab <= d) {
int h = d - ab;
int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
if (tmp <= d) {
res = max(res, tmp);
}
}
}
}
cout << res << endl;
}
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00452 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int P[1000], mem[1000 * 1000], N, M;
int main() {
loop:
cin >> N >> M;
if (!N)
return 0;
N++;
P[0] = 0;
for (int i = 1; i < N; i++)
cin >> P[i];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
mem[N * i + j] = P[i] + P[j];
sort(mem, mem + N * N);
int ans = 0;
for (int i = 0; i < N * N; i++) {
if (mem[i] > M)
continue;
ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
}
cout << ans << endl;
goto loop;
} | #include <algorithm>
#include <iostream>
using namespace std;
int P[1001], mem[1001 * 1001], N, M;
int main() {
loop:
cin >> N >> M;
if (!N)
return 0;
N++;
P[0] = 0;
for (int i = 1; i < N; i++)
cin >> P[i];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
mem[N * i + j] = P[i] + P[j];
sort(mem, mem + N * N);
int ans = 0;
for (int i = 0; i < N * N; i++) {
if (mem[i] > M)
continue;
ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
}
cout << ans << endl;
goto loop;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00452 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o) \
if (DEBUG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DEBUG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;
signed main() {
int P[1010] = {};
static int a[1000010] = {}; // 1000010
for (int N, M; cin >> N >> M && N;) {
memset(a, 0x3f, sizeof(a));
rep(i, 0, N) cin >> P[i];
P[N] = 0;
N++;
rep(i, 0, N) rep(j, 0, N) { a[i * N + j] = P[i] + P[j]; }
dumpc(a);
sort(a, a + N * N);
dumpc(a);
int ans = 0;
rep(i, 0, N * N) {
if (a[i] > M)
break;
int add = a[upper_bound(a, a + N * N, M - a[i]) - a - 1];
ans = max(ans, a[i] + add);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o) \
if (DEBUG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DEBUG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;
signed main() {
int P[1010] = {};
static int a[2000000] = {}; // 1000010
for (int N, M; cin >> N >> M && N;) {
memset(a, 0x3f, sizeof(a));
rep(i, 0, N) cin >> P[i];
P[N] = 0;
N++;
rep(i, 0, N) rep(j, 0, N) { a[i * N + j] = P[i] + P[j]; }
dumpc(a);
sort(a, a + N * N);
dumpc(a);
int ans = 0;
rep(i, 0, N * N) {
if (a[i] > M)
break;
int add = a[upper_bound(a, a + N * N, M - a[i]) - a - 1];
ans = max(ans, a[i] + add);
}
cout << ans << endl;
}
return 0;
} | replace | 50 | 51 | 50 | 51 | 0 | |
p00453 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int fie[152][1002][78];
int main() {
while (1) {
int M, N;
cin >> M >> N;
if (M == 0 && N == 0)
break;
int lis[152][1002];
for (int i = 0; i < 152; i++) {
for (int j = 0; j < 1002; j++) {
if (i != 0 && i != M + 1)
lis[i][j] = -1;
else
lis[i][j] = 0;
}
}
for (int i = 0; i < 152; i++) {
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 78; k++) {
if (i != 0 || k != 0)
fie[i][j][k] = 2000000000;
else
fie[i][j][k] = 0;
}
}
}
for (int i = 0; i < M; i++) {
int a;
cin >> a;
for (int j = 1; j <= a; j++) {
int b, c;
cin >> b >> c;
lis[i + 1][b] = c;
}
}
for (int i = 0; i <= M; i++) {
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 76; k++) {
if (fie[i][j][k] < 2000000000) {
for (int l = 0; l < 1002; l++) {
if (lis[i + 1][l] != -1)
fie[i + 1][l][k] =
min(fie[i + 1][l][k],
fie[i][j][k] +
(lis[i][j] + lis[i + 1][l]) * (int)fabs(j - l));
if (lis[i + 2][l] != -1)
fie[i + 2][l][k + 1] =
min(fie[i + 2][l][k + 1],
fie[i][j][k] +
(lis[i][j] + lis[i + 2][l]) * (int)fabs(j - l));
}
}
}
}
}
int ans = 2000000000;
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 76; k++) {
if (k <= N)
ans = min(ans, fie[M + 1][j][k]);
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int fie[152][1002][78];
int main() {
while (1) {
int M, N;
cin >> M >> N;
if (M == 0 && N == 0)
break;
int lis[154][1002];
for (int i = 0; i < 154; i++) {
for (int j = 0; j < 1002; j++) {
if (i != 0 && i != M + 1)
lis[i][j] = -1;
else
lis[i][j] = 0;
}
}
for (int i = 0; i < 152; i++) {
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 78; k++) {
if (i != 0 || k != 0)
fie[i][j][k] = 2000000000;
else
fie[i][j][k] = 0;
}
}
}
for (int i = 0; i < M; i++) {
int a;
cin >> a;
for (int j = 1; j <= a; j++) {
int b, c;
cin >> b >> c;
lis[i + 1][b] = c;
}
}
for (int i = 0; i <= M; i++) {
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 76; k++) {
if (fie[i][j][k] < 2000000000) {
for (int l = 0; l < 1002; l++) {
if (lis[i + 1][l] != -1)
fie[i + 1][l][k] =
min(fie[i + 1][l][k],
fie[i][j][k] +
(lis[i][j] + lis[i + 1][l]) * (int)fabs(j - l));
if (lis[i + 2][l] != -1)
fie[i + 2][l][k + 1] =
min(fie[i + 2][l][k + 1],
fie[i][j][k] +
(lis[i][j] + lis[i + 2][l]) * (int)fabs(j - l));
}
}
}
}
}
int ans = 2000000000;
for (int j = 0; j < 1002; j++) {
for (int k = 0; k < 76; k++) {
if (k <= N)
ans = min(ans, fie[M + 1][j][k]);
}
}
cout << ans << endl;
}
return 0;
} | replace | 12 | 14 | 12 | 14 | 0 | |
p00453 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
struct stone {
int x;
int y;
int slip;
};
struct Edge {
int to;
int time;
};
struct aa {
int now;
int time;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
while (1) {
int N, M;
cin >> N >> M;
vector<stone> stos;
for (int i = 0; i < N; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int x, s;
cin >> x >> s;
x--;
stos.push_back(stone{x, i + 1, s});
}
}
vector<vector<Edge>> es(stos.size() * (M + 1) + 2);
for (int i = 0; i < stos.size(); ++i) {
for (int j = 0; j < stos.size(); ++j) {
if (i == j)
continue;
for (int m = 0; m <= M; ++m) {
stone si = stos[i];
int iid = i * (M + 1) + m;
stone sj = stos[j];
int jid = j * (M + 1) + m;
if (si.y + 1 == sj.y) {
es[iid].push_back(
Edge{jid, abs(si.x - sj.x) * (si.slip + sj.slip)});
} else if (si.y + 2 == sj.y && m != M) {
es[iid].push_back(
Edge{int(jid + 1), abs(si.x - sj.x) * (si.slip + sj.slip)});
}
}
}
}
const int start = stos.size() * (M + 1);
const int goal = start + 1;
for (int i = 0; i < stos.size(); ++i) {
stone s(stos[i]);
for (int m = 0; m < M; ++m) {
const int nid = i * (M + 1) + m;
es[nid].push_back(Edge{nid + 1, 0});
if (s.y == 2) {
es[start].push_back(Edge{int(nid + 1), 0});
}
if (s.y == N - 1) {
es[nid].push_back(Edge{goal, 0});
}
}
for (int m = 0; m <= M; ++m) {
const int nid = i * (M + 1) + m;
if (s.y == 1) {
es[start].push_back(Edge{nid, 0});
}
if (s.y == N) {
es[nid].push_back(Edge{goal, 0});
}
}
}
priority_queue<aa, vector<aa>, Compare> que;
que.push(aa{start, 0});
vector<long long int> memo(es.size(), 1e18);
memo[start] = 0;
int ans = -1;
while (!que.empty()) {
aa atop(que.top());
que.pop();
if (atop.now == goal) {
ans = atop.time;
break;
}
for (auto e : es[atop.now]) {
const int nexttime = atop.time + e.time;
if (memo[e.to] > nexttime) {
memo[e.to] = nexttime;
que.push(aa{e.to, nexttime});
}
}
}
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
struct stone {
int x;
int y;
int slip;
};
struct Edge {
int to;
int time;
};
struct aa {
int now;
int time;
};
class Compare {
public:
// aa?????????????????¶
bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
while (1) {
int N, M;
cin >> N >> M;
if (!N)
break;
vector<stone> stos;
for (int i = 0; i < N; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int x, s;
cin >> x >> s;
x--;
stos.push_back(stone{x, i + 1, s});
}
}
vector<vector<Edge>> es(stos.size() * (M + 1) + 2);
for (int i = 0; i < stos.size(); ++i) {
for (int j = 0; j < stos.size(); ++j) {
if (i == j)
continue;
for (int m = 0; m <= M; ++m) {
stone si = stos[i];
int iid = i * (M + 1) + m;
stone sj = stos[j];
int jid = j * (M + 1) + m;
if (si.y + 1 == sj.y) {
es[iid].push_back(
Edge{jid, abs(si.x - sj.x) * (si.slip + sj.slip)});
} else if (si.y + 2 == sj.y && m != M) {
es[iid].push_back(
Edge{int(jid + 1), abs(si.x - sj.x) * (si.slip + sj.slip)});
}
}
}
}
const int start = stos.size() * (M + 1);
const int goal = start + 1;
for (int i = 0; i < stos.size(); ++i) {
stone s(stos[i]);
for (int m = 0; m < M; ++m) {
const int nid = i * (M + 1) + m;
es[nid].push_back(Edge{nid + 1, 0});
if (s.y == 2) {
es[start].push_back(Edge{int(nid + 1), 0});
}
if (s.y == N - 1) {
es[nid].push_back(Edge{goal, 0});
}
}
for (int m = 0; m <= M; ++m) {
const int nid = i * (M + 1) + m;
if (s.y == 1) {
es[start].push_back(Edge{nid, 0});
}
if (s.y == N) {
es[nid].push_back(Edge{goal, 0});
}
}
}
priority_queue<aa, vector<aa>, Compare> que;
que.push(aa{start, 0});
vector<long long int> memo(es.size(), 1e18);
memo[start] = 0;
int ans = -1;
while (!que.empty()) {
aa atop(que.top());
que.pop();
if (atop.now == goal) {
ans = atop.time;
break;
}
for (auto e : es[atop.now]) {
const int nexttime = atop.time + e.time;
if (memo[e.to] > nexttime) {
memo[e.to] = nexttime;
que.push(aa{e.to, nexttime});
}
}
}
cout << ans << endl;
}
return 0;
} | insert | 35 | 35 | 35 | 37 | TLE | |
p00453 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;
const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()
struct edge {
LL to, cost;
edge(int t, LL c) : to(t), cost(c) {}
};
int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};
int bitcount(int a) {
int ret = 0;
while (a != 0) {
ret++;
a = (a - 1) & a;
}
return ret;
}
int dp[151][1001][77];
int stage[151][1001];
int main() {
int n, m;
while (cin >> n >> m && n) {
rep(i, n) rep(j, 1001) stage[i][j] = INF;
int width = 0;
rep(i, n) {
int num;
cin >> num;
rep(j, num) {
int idx, slip;
cin >> idx >> slip;
width = max(width, idx);
stage[i][idx - 1] = slip;
}
}
rep(i, n + 1) rep(j, width) rep(k, m + 1) dp[i][j][k] = INF;
rep(i, width) dp[0][i][0] = stage[n][i] = 0;
if (m > 0) {
rep(i, width) dp[1][i][1] = 0;
}
rep(i, n) {
rep(j, width) {
rep(k, width) {
rep(l, m + 1) if (dp[i][j][l] < INF && stage[i][j] < INF) {
dp[i + 1][k][l] =
min(dp[i + 1][k][l],
dp[i][j][l] + (stage[i][j] + stage[i + 1][k]) * abs(j - k));
if (i + 2 <= n)
dp[i + 2][k][l + 1] = min(
dp[i + 2][k][l + 1],
dp[i][j][l] + (stage[i][j] + stage[i + 2][k]) * abs(j - k));
}
}
}
}
int ans = INF;
rep(i, width) rep(j, m + 1) ans = min(ans, dp[n][i][j]);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;
const int INF = 1 << 29;
const LL MOD = 100000;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define all(c) (c).begin(), (c).end()
struct edge {
LL to, cost;
edge(int t, LL c) : to(t), cost(c) {}
};
int dxk[] = {1, 1, 1, 0, -1, 0};
int dxg[] = {0, 1, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, 0, -1};
int bitcount(int a) {
int ret = 0;
while (a != 0) {
ret++;
a = (a - 1) & a;
}
return ret;
}
int dp[151][1001][77];
int stage[151][1001];
int main() {
int n, m;
while (cin >> n >> m && n) {
rep(i, n) rep(j, 1001) stage[i][j] = INF;
int width = 0;
rep(i, n) {
int num;
cin >> num;
rep(j, num) {
int idx, slip;
cin >> idx >> slip;
width = max(width, idx);
stage[i][idx - 1] = slip;
}
}
rep(i, n + 1) rep(j, width) rep(k, m + 1) dp[i][j][k] = INF;
rep(i, width) dp[0][i][0] = stage[n][i] = 0;
if (m > 0) {
rep(i, width) dp[1][i][1] = 0;
}
rep(i, n) {
rep(j, width) if (stage[i][j] < INF) {
rep(l, m + 1) if (dp[i][j][l] < INF) {
rep(k, width) {
dp[i + 1][k][l] =
min(dp[i + 1][k][l],
dp[i][j][l] + (stage[i][j] + stage[i + 1][k]) * abs(j - k));
if (i + 2 <= n)
dp[i + 2][k][l + 1] = min(
dp[i + 2][k][l + 1],
dp[i][j][l] + (stage[i][j] + stage[i + 2][k]) * abs(j - k));
}
}
}
}
int ans = INF;
rep(i, width) rep(j, m + 1) ans = min(ans, dp[n][i][j]);
cout << ans << endl;
}
return 0;
} | replace | 71 | 74 | 71 | 74 | TLE | |
p00453 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int dp[160][1010][100];
int main() {
int n, m;
while (cin >> n >> m, n + m) {
vvi in(n, vi(1010));
rep(i, n) {
int k;
cin >> k;
while (k--) {
int a, b;
cin >> a >> b;
a--;
in[i][a] = b;
}
}
rep(i, 200) rep(j, 1010) rep(k, 100) dp[i][j][k] = inf;
rep(i, 1010) if (in[0][i]) dp[0][i][0] = 0;
rep(i, 1010) if (in[1][i]) dp[1][i][1] = 0;
loop(i, 1, n) rep(j, 1010) rep(k, m + 1) if (in[i][j]) {
rep(l, 1010) if (in[i - 1][l]) dp[i][j][k] =
min(dp[i][j][k],
dp[i - 1][l][k] + (in[i][j] + in[i - 1][l]) * (abs(l - j)));
if (k && i != 1) {
rep(l, 1010) if (in[i - 2][l]) dp[i][j][k] =
min(dp[i][j][k],
dp[i - 2][l][k - 1] + (in[i][j] + in[i - 2][l]) * (abs(j - l)));
}
}
// rep(i,n){rep(j,4)cout<<" "<<dp[i][j][1];cout<<endl;}
int out = inf;
rep(i, 1010) rep(j, m + 1) out = min(out, dp[n - 1][i][j]);
rep(i, 1010) rep(j, m) out = min(out, dp[n - 2][i][j]);
cout << out << endl;
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int dp[160][1010][100];
int main() {
int n, m;
while (cin >> n >> m, n + m) {
vvi in(n, vi(1010));
rep(i, n) {
int k;
cin >> k;
while (k--) {
int a, b;
cin >> a >> b;
a--;
in[i][a] = b;
}
}
rep(i, 160) rep(j, 1010) rep(k, 100) dp[i][j][k] = inf;
rep(i, 1010) if (in[0][i]) dp[0][i][0] = 0;
rep(i, 1010) if (in[1][i]) dp[1][i][1] = 0;
loop(i, 1, n) rep(j, 1010) rep(k, m + 1) if (in[i][j]) {
rep(l, 1010) if (in[i - 1][l]) dp[i][j][k] =
min(dp[i][j][k],
dp[i - 1][l][k] + (in[i][j] + in[i - 1][l]) * (abs(l - j)));
if (k && i != 1) {
rep(l, 1010) if (in[i - 2][l]) dp[i][j][k] =
min(dp[i][j][k],
dp[i - 2][l][k - 1] + (in[i][j] + in[i - 2][l]) * (abs(j - l)));
}
}
// rep(i,n){rep(j,4)cout<<" "<<dp[i][j][1];cout<<endl;}
int out = inf;
rep(i, 1010) rep(j, m + 1) out = min(out, dp[n - 1][i][j]);
rep(i, 1010) rep(j, m) out = min(out, dp[n - 2][i][j]);
cout << out << endl;
}
} | replace | 47 | 48 | 47 | 48 | -11 | |
p00454 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 1001
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int inf = 1e9;
const ll INF = 1e18;
const int size = 6002;
#define B 100
int n, dy[4] = {0, -1, 1, 0}, dx[4] = {-1, 0, 0, 1};
queue<P> q;
int X1[MAX], X2[MAX], Y1[MAX], Y2[MAX];
int compress1(int w) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
int t1 = X1[i] + j, t2 = X2[i] + j;
if (0 <= t1 && t1 <= w)
xs.push_back(t1);
if (0 <= t2 && t2 <= w)
xs.push_back(t2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
X1[i] = find(xs.begin(), xs.end(), X1[i]) - xs.begin() + 1;
X2[i] = find(xs.begin(), xs.end(), X2[i]) - xs.begin() + 1;
}
return xs.size() - 1;
}
int compress2(int h) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
int t1 = Y1[i] + j, t2 = Y2[i] + j;
if (0 <= t1 && t1 <= h)
xs.push_back(t1);
if (0 <= t2 && t2 <= h)
xs.push_back(t2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
Y1[i] = find(xs.begin(), xs.end(), Y1[i]) - xs.begin() + 1;
Y2[i] = find(xs.begin(), xs.end(), Y2[i]) - xs.begin() + 1;
}
return xs.size() - 1;
}
bool a[size][size];
int b[size][size];
void paint(int w, int h) {
fill((int *)b, (int *)(b + size), 0);
for (int i = 0; i < n; i++) {
for (int y = Y1[i]; y < Y2[i]; y++) {
b[y][X1[i]]++;
b[y][X2[i]]--;
}
}
for (int y = 1; y <= h; y++) {
for (int x = 1; x <= w; x++) {
b[y][x] += b[y][x - 1];
// printf("%d ", b[y][x]);
if (b[y][x] > 0)
a[y][x] = 1;
}
// printf("\n");
}
}
int bfs(int w, int h) {
int ans = 0, tx, ty, yy, xx;
for (int y = 1; y <= h; y++) {
for (int x = 1; x <= w; x++) {
if (a[y][x])
continue;
q.push(dmp(y, x));
ans++;
while (!q.empty()) {
P p = q.front();
q.pop();
yy = p.fi;
xx = p.se;
a[yy][xx] = true;
for (int i = 0; i < 4; i++) {
ty = yy + dy[i];
tx = xx + dx[i];
if (!a[ty][tx]) {
a[ty][tx] = true;
q.push(dmp(ty, tx));
}
}
}
}
}
return ans;
}
int main() {
int w, h;
while (1) {
scanf("%d%d", &w, &h);
if (!w)
break;
scanf("%d", &n);
fill((bool *)a, (bool *)(a + size), true);
rep(i, n) { scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); }
w = compress1(w);
h = compress2(h);
rrep(i, h) rrep(j, w) a[i][j] = false;
paint(w, h);
int ans = bfs(w, h);
printf("%d\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define MAX 1001
#define ll long long
#define dmp make_pair
#define dpb push_back
#define P pair<int, int>
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
const int inf = 1e9;
const ll INF = 1e18;
const int size = 3002;
#define B 100
int n, dy[4] = {0, -1, 1, 0}, dx[4] = {-1, 0, 0, 1};
queue<P> q;
int X1[MAX], X2[MAX], Y1[MAX], Y2[MAX];
int compress1(int w) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
int t1 = X1[i] + j, t2 = X2[i] + j;
if (0 <= t1 && t1 <= w)
xs.push_back(t1);
if (0 <= t2 && t2 <= w)
xs.push_back(t2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
X1[i] = find(xs.begin(), xs.end(), X1[i]) - xs.begin() + 1;
X2[i] = find(xs.begin(), xs.end(), X2[i]) - xs.begin() + 1;
}
return xs.size() - 1;
}
int compress2(int h) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
int t1 = Y1[i] + j, t2 = Y2[i] + j;
if (0 <= t1 && t1 <= h)
xs.push_back(t1);
if (0 <= t2 && t2 <= h)
xs.push_back(t2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
Y1[i] = find(xs.begin(), xs.end(), Y1[i]) - xs.begin() + 1;
Y2[i] = find(xs.begin(), xs.end(), Y2[i]) - xs.begin() + 1;
}
return xs.size() - 1;
}
bool a[size][size];
int b[size][size];
void paint(int w, int h) {
fill((int *)b, (int *)(b + size), 0);
for (int i = 0; i < n; i++) {
for (int y = Y1[i]; y < Y2[i]; y++) {
b[y][X1[i]]++;
b[y][X2[i]]--;
}
}
for (int y = 1; y <= h; y++) {
for (int x = 1; x <= w; x++) {
b[y][x] += b[y][x - 1];
// printf("%d ", b[y][x]);
if (b[y][x] > 0)
a[y][x] = 1;
}
// printf("\n");
}
}
int bfs(int w, int h) {
int ans = 0, tx, ty, yy, xx;
for (int y = 1; y <= h; y++) {
for (int x = 1; x <= w; x++) {
if (a[y][x])
continue;
q.push(dmp(y, x));
ans++;
while (!q.empty()) {
P p = q.front();
q.pop();
yy = p.fi;
xx = p.se;
a[yy][xx] = true;
for (int i = 0; i < 4; i++) {
ty = yy + dy[i];
tx = xx + dx[i];
if (!a[ty][tx]) {
a[ty][tx] = true;
q.push(dmp(ty, tx));
}
}
}
}
}
return ans;
}
int main() {
int w, h;
while (1) {
scanf("%d%d", &w, &h);
if (!w)
break;
scanf("%d", &n);
fill((bool *)a, (bool *)(a + size), true);
rep(i, n) { scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); }
w = compress1(w);
h = compress2(h);
rrep(i, h) rrep(j, w) a[i][j] = false;
paint(w, h);
int ans = bfs(w, h);
printf("%d\n", ans);
}
return 0;
} | replace | 15 | 16 | 15 | 16 | MLE | |
p00454 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int W, H, N;
int X1[1000], X2[1000], Y1[1000], Y2[1000];
int f[3030][3030];
int compress(int *x1, int *x2, int w) {
vector<int> xs;
for (int i = 0; i < N; i++) {
if (1 <= x1[i] && x1[i] < w)
xs.PB(x1[i]);
if (1 <= x2[i] && x2[i] < w)
xs.PB(x2[i]);
}
xs.PB(0);
xs.PB(w);
sort(ALL(xs));
xs.erase(unique(ALL(xs)), xs.end());
REP(i, N) {
x1[i] = find(ALL(xs), x1[i]) - xs.begin();
x2[i] = find(ALL(xs), x2[i]) - xs.begin();
}
return xs.size() - 1;
}
int main() {
while (1) {
memset(f, 0, sizeof(f));
cin >> W >> H >> N;
if (W == 0 && H == 0 && N == 0)
break;
REP(i, N) { cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i]; }
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; i++) {
f[Y1[i]][X1[i]]++;
f[Y2[i]][X2[i]]++;
f[Y1[i]][X2[i]]--;
f[Y2[i]][X1[i]]--;
}
REP(i, H) REP(j, W) {
if (i != 0)
f[i][j] += f[i - 1][j];
if (j != 0)
f[i][j] += f[i][j - 1];
if (i != 0 && j != 0)
f[i][j] -= f[i - 1][j - 1];
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (f[i][j])
continue;
ans++;
queue<P> q;
q.push(MP(j, i));
while (!q.empty()) {
int tx = q.front().first, ty = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = tx + dx[k], ny = ty + dy[k];
if (nx < 0 || nx > W || ny < 0 || ny > H || f[ny][nx] > 0)
continue;
q.push(MP(nx, ny));
f[ny][nx] = 1;
}
}
}
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int W, H, N;
int X1[1000], X2[1000], Y1[1000], Y2[1000];
int f[3030][3030];
int compress(int *x1, int *x2, int w) {
vector<int> xs;
for (int i = 0; i < N; i++) {
if (1 <= x1[i] && x1[i] < w)
xs.PB(x1[i]);
if (1 <= x2[i] && x2[i] < w)
xs.PB(x2[i]);
}
xs.PB(0);
xs.PB(w);
sort(ALL(xs));
xs.erase(unique(ALL(xs)), xs.end());
REP(i, N) {
x1[i] = find(ALL(xs), x1[i]) - xs.begin();
x2[i] = find(ALL(xs), x2[i]) - xs.begin();
}
return xs.size() - 1;
}
int main() {
while (1) {
memset(f, 0, sizeof(f));
cin >> W >> H >> N;
if (W == 0 && H == 0)
break;
REP(i, N) { cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i]; }
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; i++) {
f[Y1[i]][X1[i]]++;
f[Y2[i]][X2[i]]++;
f[Y1[i]][X2[i]]--;
f[Y2[i]][X1[i]]--;
}
REP(i, H) REP(j, W) {
if (i != 0)
f[i][j] += f[i - 1][j];
if (j != 0)
f[i][j] += f[i][j - 1];
if (i != 0 && j != 0)
f[i][j] -= f[i - 1][j - 1];
}
int ans = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (f[i][j])
continue;
ans++;
queue<P> q;
q.push(MP(j, i));
while (!q.empty()) {
int tx = q.front().first, ty = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = tx + dx[k], ny = ty + dy[k];
if (nx < 0 || nx > W || ny < 0 || ny > H || f[ny][nx] > 0)
continue;
q.push(MP(nx, ny));
f[ny][nx] = 1;
}
}
}
}
cout << ans << endl;
}
} | replace | 37 | 38 | 37 | 38 | TLE | |
p00454 | C++ | Time Limit Exceeded | #include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int n;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
// vector<int> xl, yl, xr, yr;
// 座標は 2n+2 個ある
void coordinate_compress(vector<int> &xl, vector<int> &xr, int &t) {
vector<int> xc(2 * n + 2);
for (int i = 0; i < n; i++) {
xc[i] = xl[i];
xc[i + n] = xr[i];
}
xc[2 * n] = 0;
xc[2 * n + 1] = t;
sort(xc.begin(), xc.end());
// 重複を削除
xc.erase(unique(xc.begin(), xc.end()), xc.end());
map<int, int> zip;
for (int i = 0; i < xc.size(); i++) {
zip[xc[i]] = i;
}
for (int i = 0; i < n; i++) {
xl[i] = zip[xl[i]];
xr[i] = zip[xr[i]];
}
t = zip.size() - 1;
}
int main() {
ifstream cin("../test.txt");
while (true) {
int w, h;
cin >> w >> h >> n;
if (!(w | h))
break;
vector<int> xl, yl, xr, yr;
xl = vector<int>(n);
xr = vector<int>(n);
yl = vector<int>(n);
yr = vector<int>(n);
for (int i = 0; i < n; i++) {
cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
}
// 座標圧縮
coordinate_compress(xl, xr, w);
coordinate_compress(yl, yr, h);
// 10 10
// 2
// 5 0 6 10
// 0 5 10 8
// ->
// 3 3
// 1 0 2 3
// 1 1 3 2 <- これが実行するたびに違う 0 1 1 0 も。
// 表示 テープの座標
// for (int i = 0; i < n; i++) {
// cout << w << " " << h << endl;
// cout << xl[i] << " " << yl[i] << " " << xr[i] << " " << yr[i] << endl;
// }
// テープを貼る
// 格子は左下の座標を基準にする
vector<vector<int>> m(w, vector<int>(h + 1));
for (int i = 0; i < w; i++) {
for (int j = 0; j <= h; j++) {
m[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int x = xl[i]; x < xr[i]; x++) {
m[x][yl[i]] += 1;
m[x][yr[i]] -= 1;
if (xl[i] >= w || xr[i] > w || yl[i] >= h || yr[i] > h)
cout << "warning" << endl;
}
}
for (int y = 1; y < h; y++) {
for (int x = 0; x < w; x++) {
m[x][y] += m[x][y - 1];
}
}
// 表示
// for (int y = h-1; y >= 0; y--) {
// for (int x = 0; x < w; x++) {
// if (m[x][y] == 0) {
// cout << ".";
// } else {
// cout << "*";
// }
// }
// cout << endl;
// }
// 色を幅優先で塗る
int cnt_clr = 0;
queue<pair<int, int>> qu;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
if (m[x][y] <= 0) { // テープがなければ色塗り開始
cnt_clr++;
qu.push(make_pair(x, y));
while (!qu.empty()) {
pair<int, int> p = qu.front();
qu.pop();
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[nx][ny] <= 0) {
m[nx][ny] = 1;
qu.push(make_pair(nx, ny));
}
}
}
}
}
}
// 出力
cout << cnt_clr << endl;
}
} | #include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int n;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
// vector<int> xl, yl, xr, yr;
// 座標は 2n+2 個ある
void coordinate_compress(vector<int> &xl, vector<int> &xr, int &t) {
vector<int> xc(2 * n + 2);
for (int i = 0; i < n; i++) {
xc[i] = xl[i];
xc[i + n] = xr[i];
}
xc[2 * n] = 0;
xc[2 * n + 1] = t;
sort(xc.begin(), xc.end());
// 重複を削除
xc.erase(unique(xc.begin(), xc.end()), xc.end());
map<int, int> zip;
for (int i = 0; i < xc.size(); i++) {
zip[xc[i]] = i;
}
for (int i = 0; i < n; i++) {
xl[i] = zip[xl[i]];
xr[i] = zip[xr[i]];
}
t = zip.size() - 1;
}
int main() {
// ifstream cin("../test.txt");
while (true) {
int w, h;
cin >> w >> h >> n;
if (!(w | h))
break;
vector<int> xl, yl, xr, yr;
xl = vector<int>(n);
xr = vector<int>(n);
yl = vector<int>(n);
yr = vector<int>(n);
for (int i = 0; i < n; i++) {
cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
}
// 座標圧縮
coordinate_compress(xl, xr, w);
coordinate_compress(yl, yr, h);
// 10 10
// 2
// 5 0 6 10
// 0 5 10 8
// ->
// 3 3
// 1 0 2 3
// 1 1 3 2 <- これが実行するたびに違う 0 1 1 0 も。
// 表示 テープの座標
// for (int i = 0; i < n; i++) {
// cout << w << " " << h << endl;
// cout << xl[i] << " " << yl[i] << " " << xr[i] << " " << yr[i] << endl;
// }
// テープを貼る
// 格子は左下の座標を基準にする
vector<vector<int>> m(w, vector<int>(h + 1));
for (int i = 0; i < w; i++) {
for (int j = 0; j <= h; j++) {
m[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int x = xl[i]; x < xr[i]; x++) {
m[x][yl[i]] += 1;
m[x][yr[i]] -= 1;
if (xl[i] >= w || xr[i] > w || yl[i] >= h || yr[i] > h)
cout << "warning" << endl;
}
}
for (int y = 1; y < h; y++) {
for (int x = 0; x < w; x++) {
m[x][y] += m[x][y - 1];
}
}
// 表示
// for (int y = h-1; y >= 0; y--) {
// for (int x = 0; x < w; x++) {
// if (m[x][y] == 0) {
// cout << ".";
// } else {
// cout << "*";
// }
// }
// cout << endl;
// }
// 色を幅優先で塗る
int cnt_clr = 0;
queue<pair<int, int>> qu;
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
if (m[x][y] <= 0) { // テープがなければ色塗り開始
cnt_clr++;
qu.push(make_pair(x, y));
while (!qu.empty()) {
pair<int, int> p = qu.front();
qu.pop();
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[nx][ny] <= 0) {
m[nx][ny] = 1;
qu.push(make_pair(nx, ny));
}
}
}
}
}
}
// 出力
cout << cnt_clr << endl;
}
} | replace | 40 | 41 | 40 | 41 | TLE | |
p00454 | C++ | Memory Limit Exceeded | /*
AOJ 0531 Paint Color
?¢???????????????£???????????????????????¨????????¢??????????????????????????\??¶??????????????????????????????????¶?????????¨????¢?????´´?\?????????????
?¢??????????????????????????¶??????????????¢????????????????????°±?????\?¶????5?§??¢???????
????????????????¨???????????¶??????°???????????\??°????????????????????????????¢?????????¨??¨???????????¶?????????????????????????°´???????????´??????
?????\????¬¬????????°??????w???1 ??? w ??? 1000000???????¬¬????????°??????h???1
??? h ??? 1000000?????? ?¬¬????????????????????°???n???1 ??? n ???
1000????????\???n????????????????????????????§??????? (x1 , y1
)??????????§??????? (x2 , y2 )???
??¨??????????????? x1 , y1 , x2 , y2 (0 ??? x1< x2 ??? w, 0 ??? y1 < y2 ??? h
????????´??°)
??????????????????????????????????§???? (0, 0) ??????????§????(w, h) ???
???????????????30%???????¶?w ??? 100, h ??? 100, n ??? 100???
?????????????????´??°?????£??¨?¶??????°??????
*/
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <complex>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) c.begin(), c.end()
#define CLR(c, n) memset(c, n, sizeof(c))
#define TR(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define CONTAIN(it, c) (c.find(it) != c.end())
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> void checkmin(T &a, T b) {
if (b < a)
a = b;
}
template <class T> void checkmax(T &a, T b) {
if (b > a)
a = b;
}
typedef long long LL;
const int INF = 0x3F3F3F3F;
const int N = 1024;
int w, h, n, cord_x1[N], cord_y1[N], cord_x2[N], cord_y2[N];
int x[N * 4], y[N * 4];
int cnt[N * 4][N * 4];
void fill(int x0, int y0) {
cnt[x0][y0] = -1;
queue<PII> q;
q.push(PII(x0, y0));
int dx[4] = {-1, 0, 1, 0};
while (!q.empty()) {
int x0 = q.front().first, y0 = q.front().second;
q.pop();
REP(dir, 4) {
int x1 = x0 + dx[dir], y1 = y0 + dx[dir ^ 1];
if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h && cnt[x1][y1] == 0) {
cnt[x1][y1] = -1;
q.push(PII(x1, y1));
}
}
}
}
int main(int argc, char *argv[]) {
while (scanf("%d %d", &w, &h) == 2 && w + h > 0) {
scanf("%d", &n);
REP(i, n) {
scanf("%d %d %d %d", &cord_x1[i], &cord_y1[i], &cord_x2[i], &cord_y2[i]);
}
int nx = 0, ny = 0;
REP(i, n) {
x[nx++] = cord_x1[i];
if (cord_x1[i])
x[nx++] = cord_x1[i] - 1;
x[nx++] = cord_x2[i];
if (cord_x2[i])
x[nx++] = cord_x2[i] - 1;
y[ny++] = cord_y1[i];
if (cord_y1[i])
y[ny++] = cord_y1[i] - 1;
y[ny++] = cord_y2[i];
if (cord_y2[i])
y[ny++] = cord_y2[i] - 1;
}
x[nx++] = w;
y[ny++] = h;
sort(x, x + nx);
sort(y, y + ny);
w = unique(x, x + nx) - x - 1;
h = unique(y, y + ny) - y - 1;
REP(i, n) {
cord_x1[i] = lower_bound(x, x + w, cord_x1[i]) - x;
cord_y1[i] = lower_bound(y, y + h, cord_y1[i]) - y;
cord_x2[i] = lower_bound(x, x + w, cord_x2[i]) - x;
cord_y2[i] = lower_bound(y, y + h, cord_y2[i]) - y;
}
CLR(cnt, 0);
REP(i, n) {
++cnt[cord_x1[i]][cord_y1[i]];
++cnt[cord_x2[i]][cord_y2[i]];
--cnt[cord_x1[i]][cord_y2[i]];
--cnt[cord_x2[i]][cord_y1[i]];
}
FOR(i, 0, w) FOR(j, 1, h) cnt[i][j] += cnt[i][j - 1];
FOR(i, 1, w) FOR(j, 0, h) cnt[i][j] += cnt[i - 1][j];
int ans = 0;
REP(i, w) REP(j, h) if (cnt[i][j] == 0) {
++ans;
fill(i, j);
}
printf("%d\n", ans);
}
} | /*
AOJ 0531 Paint Color
?¢???????????????£???????????????????????¨????????¢??????????????????????????\??¶??????????????????????????????????¶?????????¨????¢?????´´?\?????????????
?¢??????????????????????????¶??????????????¢????????????????????°±?????\?¶????5?§??¢???????
????????????????¨???????????¶??????°???????????\??°????????????????????????????¢?????????¨??¨???????????¶?????????????????????????°´???????????´??????
?????\????¬¬????????°??????w???1 ??? w ??? 1000000???????¬¬????????°??????h???1
??? h ??? 1000000?????? ?¬¬????????????????????°???n???1 ??? n ???
1000????????\???n????????????????????????????§??????? (x1 , y1
)??????????§??????? (x2 , y2 )???
??¨??????????????? x1 , y1 , x2 , y2 (0 ??? x1< x2 ??? w, 0 ??? y1 < y2 ??? h
????????´??°)
??????????????????????????????????§???? (0, 0) ??????????§????(w, h) ???
???????????????30%???????¶?w ??? 100, h ??? 100, n ??? 100???
?????????????????´??°?????£??¨?¶??????°??????
*/
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <complex>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(c) c.begin(), c.end()
#define CLR(c, n) memset(c, n, sizeof(c))
#define TR(it, c) for (typeof(c.begin()) it = c.begin(); it != c.end(); ++it)
#define CONTAIN(it, c) (c.find(it) != c.end())
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> void checkmin(T &a, T b) {
if (b < a)
a = b;
}
template <class T> void checkmax(T &a, T b) {
if (b > a)
a = b;
}
typedef long long LL;
const int INF = 0x3F3F3F3F;
const int N = 1024;
int w, h, n, cord_x1[N], cord_y1[N], cord_x2[N], cord_y2[N];
int x[N * 4], y[N * 4];
short cnt[N * 4][N * 4];
void fill(int x0, int y0) {
cnt[x0][y0] = -1;
queue<PII> q;
q.push(PII(x0, y0));
int dx[4] = {-1, 0, 1, 0};
while (!q.empty()) {
int x0 = q.front().first, y0 = q.front().second;
q.pop();
REP(dir, 4) {
int x1 = x0 + dx[dir], y1 = y0 + dx[dir ^ 1];
if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h && cnt[x1][y1] == 0) {
cnt[x1][y1] = -1;
q.push(PII(x1, y1));
}
}
}
}
int main(int argc, char *argv[]) {
while (scanf("%d %d", &w, &h) == 2 && w + h > 0) {
scanf("%d", &n);
REP(i, n) {
scanf("%d %d %d %d", &cord_x1[i], &cord_y1[i], &cord_x2[i], &cord_y2[i]);
}
int nx = 0, ny = 0;
REP(i, n) {
x[nx++] = cord_x1[i];
if (cord_x1[i])
x[nx++] = cord_x1[i] - 1;
x[nx++] = cord_x2[i];
if (cord_x2[i])
x[nx++] = cord_x2[i] - 1;
y[ny++] = cord_y1[i];
if (cord_y1[i])
y[ny++] = cord_y1[i] - 1;
y[ny++] = cord_y2[i];
if (cord_y2[i])
y[ny++] = cord_y2[i] - 1;
}
x[nx++] = w;
y[ny++] = h;
sort(x, x + nx);
sort(y, y + ny);
w = unique(x, x + nx) - x - 1;
h = unique(y, y + ny) - y - 1;
REP(i, n) {
cord_x1[i] = lower_bound(x, x + w, cord_x1[i]) - x;
cord_y1[i] = lower_bound(y, y + h, cord_y1[i]) - y;
cord_x2[i] = lower_bound(x, x + w, cord_x2[i]) - x;
cord_y2[i] = lower_bound(y, y + h, cord_y2[i]) - y;
}
CLR(cnt, 0);
REP(i, n) {
++cnt[cord_x1[i]][cord_y1[i]];
++cnt[cord_x2[i]][cord_y2[i]];
--cnt[cord_x1[i]][cord_y2[i]];
--cnt[cord_x2[i]][cord_y1[i]];
}
FOR(i, 0, w) FOR(j, 1, h) cnt[i][j] += cnt[i][j - 1];
FOR(i, 1, w) FOR(j, 0, h) cnt[i][j] += cnt[i - 1][j];
int ans = 0;
REP(i, w) REP(j, h) if (cnt[i][j] == 0) {
++ans;
fill(i, j);
}
printf("%d\n", ans);
}
} | replace | 66 | 67 | 66 | 67 | MLE | |
p00454 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 1000 + 16
using namespace std;
int N, H, W;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int fld[6 * MAX_N][6 * MAX_N], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int compress(int *x1, int *x2, int w) {
vector<double> xs;
for (int i = 0; i < N; ++i) {
for (double id = -0.5; id <= 0.5; id += 0.5) {
double tx1 = x1[i] + id, tx2 = x2[i] + id;
if (0 <= tx1 && tx1 <= w)
xs.push_back(tx1);
if (0 <= tx2 && tx2 <= w)
xs.push_back(tx2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
// for (double x : xs) cout << x << "-";cout << endl;
for (int i = 0; i < N; ++i) {
x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
} // cout<<xs.size()<<"**********"<<endl;
return xs.size();
}
int main() {
while (cin >> W >> H, W | H) {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
}
memset(fld, 0, sizeof(fld));
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; ++i) {
// cout << X1[i] << ends << X2[i] << ends << Y1[i] << ends << Y2[i] <<
// endl;
for (int j = X1[i]; j <= X2[i]; ++j) {
for (int k = Y1[i]; k <= Y2[i]; ++k)
fld[j][k] = 1;
}
}
// for (int i = 0; i < W; ++i) {for (int j = 0; j < H; ++j)cout << fld[i][j]
// << ends;cout << endl;}
int ans = 0;
// cout << 123 << endl;
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
if (!fld[i][j]) {
fld[i][j] = 1;
++ans;
queue<int> q;
q.push(i * H + j);
while (!q.empty()) {
int ii = q.front() / H, jj = q.front() % H;
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = ii + dx[k], nj = jj + dy[k];
if (ni >= 0 && ni < W && nj >= 0 && nj < H && !fld[ni][nj]) {
fld[ni][nj] = 1;
q.push(ni * H + nj);
}
}
}
}
}
}
cout << ans << endl;
}
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 1000 + 16
using namespace std;
int N, H, W;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int fld[2 * MAX_N][2 * MAX_N], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int compress(int *x1, int *x2, int w) {
vector<double> xs;
for (int i = 0; i < N; ++i) {
for (double id = -0.5; id <= 0.5; id += 0.5) {
double tx1 = x1[i] + id, tx2 = x2[i] + id;
if (0 <= tx1 && tx1 <= w)
xs.push_back(tx1);
if (0 <= tx2 && tx2 <= w)
xs.push_back(tx2);
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
// for (double x : xs) cout << x << "-";cout << endl;
for (int i = 0; i < N; ++i) {
x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
} // cout<<xs.size()<<"**********"<<endl;
return xs.size();
}
int main() {
while (cin >> W >> H, W | H) {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
}
memset(fld, 0, sizeof(fld));
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; ++i) {
// cout << X1[i] << ends << X2[i] << ends << Y1[i] << ends << Y2[i] <<
// endl;
for (int j = X1[i]; j <= X2[i]; ++j) {
for (int k = Y1[i]; k <= Y2[i]; ++k)
fld[j][k] = 1;
}
}
// for (int i = 0; i < W; ++i) {for (int j = 0; j < H; ++j)cout << fld[i][j]
// << ends;cout << endl;}
int ans = 0;
// cout << 123 << endl;
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
if (!fld[i][j]) {
fld[i][j] = 1;
++ans;
queue<int> q;
q.push(i * H + j);
while (!q.empty()) {
int ii = q.front() / H, jj = q.front() % H;
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = ii + dx[k], nj = jj + dy[k];
if (ni >= 0 && ni < W && nj >= 0 && nj < H && !fld[ni][nj]) {
fld[ni][nj] = 1;
q.push(ni * H + nj);
}
}
}
}
}
}
cout << ans << endl;
}
return 0;
}
| replace | 11 | 12 | 11 | 12 | MLE | |
p00454 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int X1[1005], X2[1005], Y1[1005], Y2[1005];
int f[6030][6030];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int W, H, N;
int ans = 0;
int compress(int *x1, int *x2, int t) {
vector<int> xs;
for (int i = 0; i < N; i++) {
int tx1 = x1[i], tx2 = x2[i];
if (1 <= tx1 && tx1 < t)
xs.pb(tx1);
if (1 <= tx2 && tx2 < t)
xs.pb(tx2);
}
xs.pb(0);
xs.pb(t);
SORT(xs);
xs.erase(unique(all(xs)), xs.end());
for (int i = 0; i < N; i++) {
x1[i] = find(all(xs), x1[i]) - xs.begin();
x2[i] = find(all(xs), x2[i]) - xs.begin();
}
return xs.size() - 1;
}
int main() {
while (1) {
memset(f, 0, sizeof(f));
ans = 0;
cin >> W >> H;
if (W == 0 && H == 0)
return 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
}
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; i++) {
f[Y1[i]][X1[i]]++;
f[Y2[i]][X2[i]]++;
f[Y1[i]][X2[i]]--;
f[Y2[i]][X1[i]]--;
}
for (int i = 0; i < H; i++) {
for (int j = 1; j < W; j++) {
f[i][j] += f[i][j - 1];
}
}
for (int i = 1; i < H; i++) {
for (int j = 0; j < W; j++) {
f[i][j] += f[i - 1][j];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (f[i][j])
continue;
ans++;
queue<P> q;
q.push(mp(j, i));
while (!q.empty()) {
int tx = q.front().fi, ty = q.front().sec;
q.pop();
for (int k = 0; k < 4; k++) {
int ttx = tx + dx[k], tty = ty + dy[k];
if (ttx < 0 || ttx > W || tty < 0 || tty > H || f[tty][ttx] > 0)
continue;
q.push(mp(ttx, tty));
f[tty][ttx] = 1;
}
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int X1[1005], X2[1005], Y1[1005], Y2[1005];
int f[3030][3030];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int W, H, N;
int ans = 0;
int compress(int *x1, int *x2, int t) {
vector<int> xs;
for (int i = 0; i < N; i++) {
int tx1 = x1[i], tx2 = x2[i];
if (1 <= tx1 && tx1 < t)
xs.pb(tx1);
if (1 <= tx2 && tx2 < t)
xs.pb(tx2);
}
xs.pb(0);
xs.pb(t);
SORT(xs);
xs.erase(unique(all(xs)), xs.end());
for (int i = 0; i < N; i++) {
x1[i] = find(all(xs), x1[i]) - xs.begin();
x2[i] = find(all(xs), x2[i]) - xs.begin();
}
return xs.size() - 1;
}
int main() {
while (1) {
memset(f, 0, sizeof(f));
ans = 0;
cin >> W >> H;
if (W == 0 && H == 0)
return 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
}
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < N; i++) {
f[Y1[i]][X1[i]]++;
f[Y2[i]][X2[i]]++;
f[Y1[i]][X2[i]]--;
f[Y2[i]][X1[i]]--;
}
for (int i = 0; i < H; i++) {
for (int j = 1; j < W; j++) {
f[i][j] += f[i][j - 1];
}
}
for (int i = 1; i < H; i++) {
for (int j = 0; j < W; j++) {
f[i][j] += f[i - 1][j];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (f[i][j])
continue;
ans++;
queue<P> q;
q.push(mp(j, i));
while (!q.empty()) {
int tx = q.front().fi, ty = q.front().sec;
q.pop();
for (int k = 0; k < 4; k++) {
int ttx = tx + dx[k], tty = ty + dy[k];
if (ttx < 0 || ttx > W || tty < 0 || tty > H || f[tty][ttx] > 0)
continue;
q.push(mp(ttx, tty));
f[tty][ttx] = 1;
}
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 32 | 33 | 32 | 33 | MLE | |
p00454 | C++ | Memory Limit Exceeded | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
void seach(int x, int y, int X, int Y);
int speace[10000][10000], n;
int main() {
int X, Y, N, x1, y1, x2, y2;
while (1) {
cin >> X >> Y;
if (X == 0 && Y == 0) {
break;
}
cin >> N;
n = 0;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
speace[i][j] = -1;
}
}
for (int i = 0; i < N; i++) {
cin >> x1 >> y1 >> x2 >> y2;
for (int j = x1; j < x2; j++) {
for (int k = y1; k < y2; k++) {
speace[j][k] = n;
}
}
}
n++;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
if (speace[i][j] == -1) {
seach(i, j, X, Y);
n++;
}
}
}
/*cout << "\n";
for(int i=0;i<X;i++){
for(int j=0;j<Y;j++){
cout << speace[i][j] << " ";
}
cout << "\n";
}*/
cout << n - 1 << "\n";
}
return 0;
}
void seach(int x, int y, int X, int Y) {
int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, 1, -1, 0};
queue<P> que;
que.push(P(x, y));
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
if (0 <= p.first + dx[i] && p.first + dx[i] < X &&
0 <= p.second + dy[i] && p.second + dy[i] < Y) {
if (speace[p.first + dx[i]][p.second + dy[i]] == -1) {
speace[p.first + dx[i]][p.second + dy[i]] = n;
que.push(P(p.first + dx[i], p.second + dy[i]));
}
}
}
}
}
| #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
void seach(int x, int y, int X, int Y);
int speace[5000][5000], n;
int main() {
int X, Y, N, x1, y1, x2, y2;
while (1) {
cin >> X >> Y;
if (X == 0 && Y == 0) {
break;
}
cin >> N;
n = 0;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
speace[i][j] = -1;
}
}
for (int i = 0; i < N; i++) {
cin >> x1 >> y1 >> x2 >> y2;
for (int j = x1; j < x2; j++) {
for (int k = y1; k < y2; k++) {
speace[j][k] = n;
}
}
}
n++;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
if (speace[i][j] == -1) {
seach(i, j, X, Y);
n++;
}
}
}
/*cout << "\n";
for(int i=0;i<X;i++){
for(int j=0;j<Y;j++){
cout << speace[i][j] << " ";
}
cout << "\n";
}*/
cout << n - 1 << "\n";
}
return 0;
}
void seach(int x, int y, int X, int Y) {
int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, 1, -1, 0};
queue<P> que;
que.push(P(x, y));
while (que.size()) {
P p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
if (0 <= p.first + dx[i] && p.first + dx[i] < X &&
0 <= p.second + dy[i] && p.second + dy[i] < Y) {
if (speace[p.first + dx[i]][p.second + dy[i]] == -1) {
speace[p.first + dx[i]][p.second + dy[i]] = n;
que.push(P(p.first + dx[i], p.second + dy[i]));
}
}
}
}
}
| replace | 8 | 9 | 8 | 9 | MLE | |
p00454 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define GRGR(dx, dy) \
for (int i = 0, dir[6] = {0, -1, 0, 1, 0, 0}, dx, dy; \
dx = dir[i], dy = dir[i + 1], i < 4; i++)
typedef pair<int, int> P;
struct T {
int x1, y1, x2, y2;
};
int w, h, n;
int vw, vh;
set<int> vx;
set<int> vy;
T t[1002];
bool bd[3000][3000];
void debug() {
REP(j, vh) {
REP(i, vw) cout << bd[i][j] << " ";
cout << endl;
}
}
int r2v(set<int> vs, int r) { return distance(vs.begin(), vs.find(r)); }
void paint(int i, int j) {
queue<P> que;
que.push(P(i, j));
bd[i][j] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int px = p.first, py = p.second;
GRGR(dx, dy) {
if (px < 0 || vw <= px || py < 0 || vh <= py) {
continue;
}
if (bd[px + dx][py + dy]) {
que.push(P(px + dx, py + dy));
bd[px + dx][py + dy] = 0;
}
}
}
}
int main() {
while (cin >> w >> h, w) {
cin >> n;
vx.clear();
vy.clear();
vx.insert(0);
vx.insert(w);
vy.insert(0);
vy.insert(h);
int x1, y1, x2, y2;
REP(i, n) {
cin >> x1 >> y1 >> x2 >> y2;
vx.insert(x1);
vy.insert(y1);
vx.insert(x2);
vy.insert(y2);
t[i] = {x1, y1, x2, y2};
}
vw = vx.size() - 1;
vh = vy.size() - 1;
REP(i, vw) REP(j, vh) bd[i][j] = 1;
REP(i, n) {
x1 = r2v(vx, t[i].x1);
y1 = r2v(vy, t[i].y1);
x2 = r2v(vx, t[i].x2);
y2 = r2v(vy, t[i].y2);
FOR(i, x1, x2) FOR(j, y1, y2) bd[i][j] = 0;
}
// debug();
int ans = 0;
REP(i, vw) REP(j, vh) {
if (bd[i][j]) {
paint(i, j);
ans++;
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define GRGR(dx, dy) \
for (int i = 0, dir[6] = {0, -1, 0, 1, 0, 0}, dx, dy; \
dx = dir[i], dy = dir[i + 1], i < 4; i++)
typedef pair<int, int> P;
struct T {
int x1, y1, x2, y2;
};
int w, h, n;
int vw, vh;
set<int> vx;
set<int> vy;
T t[1002];
bool bd[3000][3000];
void debug() {
REP(j, vh) {
REP(i, vw) cout << bd[i][j] << " ";
cout << endl;
}
}
int r2v(set<int> vs, int r) { return distance(vs.begin(), vs.find(r)); }
void paint(int i, int j) {
queue<P> que;
que.push(P(i, j));
bd[i][j] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int px = p.first, py = p.second;
GRGR(dx, dy) {
if (px + dx < 0 || vw <= px + dx || py + dy < 0 || vh <= py + dy) {
continue;
}
if (bd[px + dx][py + dy]) {
que.push(P(px + dx, py + dy));
bd[px + dx][py + dy] = 0;
}
}
}
}
int main() {
while (cin >> w >> h, w) {
cin >> n;
vx.clear();
vy.clear();
vx.insert(0);
vx.insert(w);
vy.insert(0);
vy.insert(h);
int x1, y1, x2, y2;
REP(i, n) {
cin >> x1 >> y1 >> x2 >> y2;
vx.insert(x1);
vy.insert(y1);
vx.insert(x2);
vy.insert(y2);
t[i] = {x1, y1, x2, y2};
}
vw = vx.size() - 1;
vh = vy.size() - 1;
REP(i, vw) REP(j, vh) bd[i][j] = 1;
REP(i, n) {
x1 = r2v(vx, t[i].x1);
y1 = r2v(vy, t[i].y1);
x2 = r2v(vx, t[i].x2);
y2 = r2v(vy, t[i].y2);
FOR(i, x1, x2) FOR(j, y1, y2) bd[i][j] = 0;
}
// debug();
int ans = 0;
REP(i, vw) REP(j, vh) {
if (bd[i][j]) {
paint(i, j);
ans++;
}
}
cout << ans << endl;
}
return 0;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p00454 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF INT_MAX / 2
#define EPS 1.0e-7
#define MAX_NUM 1000
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
int W, H;
int N;
int vx[MAX_NUM * 2];
int vy[MAX_NUM * 2];
int flag[6 * MAX_NUM + 1][6 * MAX_NUM + 1];
int compress(int *v, int n, int l) {
vector<int> cv;
for (int i = 0; i < n; ++i)
for (int d = -1; d <= 1; ++d)
if (0 <= v[i] + d && v[i] + d <= l - 1)
cv.push_back(v[i] + d);
sort(cv.begin(), cv.end());
int ret = unique(cv.begin(), cv.end()) - cv.begin();
for (int i = 0; i < n; ++i)
v[i] = lower_bound(cv.begin(), cv.begin() + ret, v[i]) - cv.begin();
return ret;
}
int main() {
cin.sync_with_stdio(false);
while (cin >> W >> H, W) {
cin >> N;
for (int i = 0; i < N; ++i)
cin >> vx[2 * i] >> vy[2 * i] >> vx[2 * i + 1] >> vy[2 * i + 1];
int w = compress(vx, 2 * N, W);
int h = compress(vy, 2 * N, H);
memset(flag, 0, sizeof(flag));
for (int i = 0; i < N; ++i)
for (int ix = vx[2 * i]; ix < vx[2 * i + 1]; ++ix)
for (int iy = vy[2 * i]; iy < vy[2 * i + 1]; ++iy) {
flag[ix][iy] = 1;
}
int ans = 0;
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
if (flag[i][j])
continue;
++ans;
queue<PII> q;
q.push(make_pair(i, j));
flag[i][j] = 1;
while (!q.empty()) {
PII p = q.front();
q.pop();
int x = p.first;
int y = p.second;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int k = 0; k < 4; ++k) {
int nx = x + d[k][0];
int ny = y + d[k][1];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h) || flag[nx][ny])
continue;
flag[nx][ny] = 1;
q.push(make_pair(nx, ny));
}
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF INT_MAX / 2
#define EPS 1.0e-7
#define MAX_NUM 1000
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
int W, H;
int N;
int vx[MAX_NUM * 2];
int vy[MAX_NUM * 2];
char flag[6 * MAX_NUM + 1][6 * MAX_NUM + 1];
int compress(int *v, int n, int l) {
vector<int> cv;
for (int i = 0; i < n; ++i)
for (int d = -1; d <= 1; ++d)
if (0 <= v[i] + d && v[i] + d <= l - 1)
cv.push_back(v[i] + d);
sort(cv.begin(), cv.end());
int ret = unique(cv.begin(), cv.end()) - cv.begin();
for (int i = 0; i < n; ++i)
v[i] = lower_bound(cv.begin(), cv.begin() + ret, v[i]) - cv.begin();
return ret;
}
int main() {
cin.sync_with_stdio(false);
while (cin >> W >> H, W) {
cin >> N;
for (int i = 0; i < N; ++i)
cin >> vx[2 * i] >> vy[2 * i] >> vx[2 * i + 1] >> vy[2 * i + 1];
int w = compress(vx, 2 * N, W);
int h = compress(vy, 2 * N, H);
memset(flag, 0, sizeof(flag));
for (int i = 0; i < N; ++i)
for (int ix = vx[2 * i]; ix < vx[2 * i + 1]; ++ix)
for (int iy = vy[2 * i]; iy < vy[2 * i + 1]; ++iy) {
flag[ix][iy] = 1;
}
int ans = 0;
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
if (flag[i][j])
continue;
++ans;
queue<PII> q;
q.push(make_pair(i, j));
flag[i][j] = 1;
while (!q.empty()) {
PII p = q.front();
q.pop();
int x = p.first;
int y = p.second;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (int k = 0; k < 4; ++k) {
int nx = x + d[k][0];
int ny = y + d[k][1];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h) || flag[nx][ny])
continue;
flag[nx][ny] = 1;
q.push(make_pair(nx, ny));
}
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 33 | 34 | 33 | 34 | MLE | |
p00454 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
template <typename T> bool chmax(T &a, const T &b) {
return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
return b < a ? (a = b, true) : false;
}
typedef long long ll;
int W, H, N;
const int MAX_N = 1000;
vector<int> VP, VQ, A, B, C, D;
int acc[MAX_N * 4][MAX_N * 4];
bool vis[MAX_N * 4][MAX_N * 4];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};
int getIdx(vector<int> &v, int val) {
return int(lower_bound(v.begin(), v.end(), val) - v.begin());
}
void dfs(int x, int y) {
vis[x][y] = true;
FOR(i, 0, 4) {
int nx = x + dx[i], ny = y + dy[i];
if ((nx >= 0 && nx + 1 < VP.size() && ny >= 0 && ny + 1 < VQ.size()) ==
false)
continue;
if (vis[nx][ny] || acc[nx][ny] > 0)
continue;
dfs(nx, ny);
}
}
int main() {
while (scanf("%d%d", &W, &H), W) {
scanf("%d", &N);
VP = {0, W};
VQ = {0, H};
memset(vis, 0, sizeof(vis));
memset(acc, 0, sizeof(acc));
A.assign(N, 0);
B.assign(N, 0);
C.assign(N, 0);
D.assign(N, 0);
FOR(i, 0, N) {
scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
VP.push_back(max(0, A[i] - 1));
VP.push_back(A[i]);
VP.push_back(max(0, C[i] - 1));
VP.push_back(C[i]);
VQ.push_back(max(0, B[i] - 1));
VQ.push_back(B[i]);
VQ.push_back(max(0, D[i] - 1));
VQ.push_back(D[i]);
}
sort(VP.begin(), VP.end());
sort(VQ.begin(), VQ.end());
VP.erase(unique(VP.begin(), VP.end()), VP.end());
VQ.erase(unique(VQ.begin(), VQ.end()), VQ.end());
FOR(i, 0, N) {
acc[getIdx(VP, A[i])][getIdx(VQ, B[i])]++;
acc[getIdx(VP, C[i])][getIdx(VQ, B[i])]--;
acc[getIdx(VP, A[i])][getIdx(VQ, D[i])]--;
acc[getIdx(VP, C[i])][getIdx(VQ, D[i])]++;
}
FOR(i, 0, VP.size()) FOR(j, 0, VQ.size()) {
if (i > 0)
acc[i][j] += acc[i - 1][j];
if (j > 0)
acc[i][j] += acc[i][j - 1];
if (i > 0 && j > 0)
acc[i][j] -= acc[i - 1][j - 1];
}
int ans = 0;
FOR(i, 0, int(VP.size()) - 1)
FOR(j, 0, int(VQ.size()) - 1) if (vis[i][j] == false && acc[i][j] == 0) {
dfs(i, j);
ans++;
}
printf("%d\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
template <typename T> bool chmax(T &a, const T &b) {
return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
return b < a ? (a = b, true) : false;
}
typedef long long ll;
int W, H, N;
const int MAX_N = 1000;
vector<int> VP, VQ, A, B, C, D;
int acc[MAX_N * 2 + 17][MAX_N * 2 + 17];
bool vis[MAX_N * 2 + 17][MAX_N * 2 + 17];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};
int getIdx(vector<int> &v, int val) {
return int(lower_bound(v.begin(), v.end(), val) - v.begin());
}
void dfs(int x, int y) {
vis[x][y] = true;
FOR(i, 0, 4) {
int nx = x + dx[i], ny = y + dy[i];
if ((nx >= 0 && nx + 1 < VP.size() && ny >= 0 && ny + 1 < VQ.size()) ==
false)
continue;
if (vis[nx][ny] || acc[nx][ny] > 0)
continue;
dfs(nx, ny);
}
}
int main() {
while (scanf("%d%d", &W, &H), W) {
scanf("%d", &N);
VP = {0, W};
VQ = {0, H};
memset(vis, 0, sizeof(vis));
memset(acc, 0, sizeof(acc));
A.assign(N, 0);
B.assign(N, 0);
C.assign(N, 0);
D.assign(N, 0);
FOR(i, 0, N) {
scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
VP.push_back(max(0, A[i] - 1));
VP.push_back(A[i]);
VP.push_back(max(0, C[i] - 1));
VP.push_back(C[i]);
VQ.push_back(max(0, B[i] - 1));
VQ.push_back(B[i]);
VQ.push_back(max(0, D[i] - 1));
VQ.push_back(D[i]);
}
sort(VP.begin(), VP.end());
sort(VQ.begin(), VQ.end());
VP.erase(unique(VP.begin(), VP.end()), VP.end());
VQ.erase(unique(VQ.begin(), VQ.end()), VQ.end());
FOR(i, 0, N) {
acc[getIdx(VP, A[i])][getIdx(VQ, B[i])]++;
acc[getIdx(VP, C[i])][getIdx(VQ, B[i])]--;
acc[getIdx(VP, A[i])][getIdx(VQ, D[i])]--;
acc[getIdx(VP, C[i])][getIdx(VQ, D[i])]++;
}
FOR(i, 0, VP.size()) FOR(j, 0, VQ.size()) {
if (i > 0)
acc[i][j] += acc[i - 1][j];
if (j > 0)
acc[i][j] += acc[i][j - 1];
if (i > 0 && j > 0)
acc[i][j] -= acc[i - 1][j - 1];
}
int ans = 0;
FOR(i, 0, int(VP.size()) - 1)
FOR(j, 0, int(VQ.size()) - 1) if (vis[i][j] == false && acc[i][j] == 0) {
dfs(i, j);
ans++;
}
printf("%d\n", ans);
}
return 0;
} | replace | 15 | 17 | 15 | 17 | MLE | |
p00454 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>
const int INF = 1000000000;
const int dre_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dre_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int imos[3010][3010];
int a[3010][3010];
int dfs(int x, int y) {
if (a[x][y])
return 0;
a[x][y] = 1;
rep(i, 4) dfs(x + dre_4[i][0], y + dre_4[i][1]);
return 1;
}
int main() {
while (1) {
int w, h, n;
int x1[1010], y1[1010], x2[1010], y2[1010];
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
scanf("%d", &n);
rep(i, n) scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
vector<int> R, C;
R.pb(0);
R.pb(w);
C.pb(0);
C.pb(h);
rep(i, n) {
R.pb(x1[i]);
R.pb(x2[i]);
C.pb(y1[i]);
C.pb(y2[i]);
}
sor(R);
uniq(R);
sor(C);
uniq(C);
w = lb(R, w) - R.begin();
h = lb(C, h) - C.begin();
rep(i, n) {
x1[i] = lb(R, x1[i]) - R.begin();
y1[i] = lb(C, y1[i]) - C.begin();
x2[i] = lb(R, x2[i]) - R.begin();
y2[i] = lb(C, y2[i]) - C.begin();
}
rep(i, 3010) rep(j, 3010) imos[i][j] = 0;
rep(i, 3010) rep(j, 3010) a[i][j] = 1;
rep(i, n) {
imos[x1[i]][y1[i]]++;
imos[x1[i]][y2[i]]--;
imos[x2[i]][y1[i]]--;
imos[x2[i]][y2[i]]++;
}
rep(i, 3010) rep1(j, 3009) imos[i][j] += imos[i][j - 1];
rep1(i, 3009) rep(j, 3010) imos[i][j] += imos[i - 1][j];
rep1(i, R.size() - 1) rep1(j, C.size() - 1) a[i][j] = imos[i - 1][j - 1];
/*rep(i,20){
rep(j,10){
printf("%d",a[i][j]);
}
puts("");
}*/
int cnt = 0;
rep(i, 3010) rep(j, 3010) cnt += dfs(i, j);
printf("%d\n", cnt);
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>
const int INF = 1000000000;
const int dre_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dre_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
short imos[3010][3010];
short a[3010][3010];
int dfs(int x, int y) {
if (a[x][y])
return 0;
a[x][y] = 1;
rep(i, 4) dfs(x + dre_4[i][0], y + dre_4[i][1]);
return 1;
}
int main() {
while (1) {
int w, h, n;
int x1[1010], y1[1010], x2[1010], y2[1010];
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
scanf("%d", &n);
rep(i, n) scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
vector<int> R, C;
R.pb(0);
R.pb(w);
C.pb(0);
C.pb(h);
rep(i, n) {
R.pb(x1[i]);
R.pb(x2[i]);
C.pb(y1[i]);
C.pb(y2[i]);
}
sor(R);
uniq(R);
sor(C);
uniq(C);
w = lb(R, w) - R.begin();
h = lb(C, h) - C.begin();
rep(i, n) {
x1[i] = lb(R, x1[i]) - R.begin();
y1[i] = lb(C, y1[i]) - C.begin();
x2[i] = lb(R, x2[i]) - R.begin();
y2[i] = lb(C, y2[i]) - C.begin();
}
rep(i, 3010) rep(j, 3010) imos[i][j] = 0;
rep(i, 3010) rep(j, 3010) a[i][j] = 1;
rep(i, n) {
imos[x1[i]][y1[i]]++;
imos[x1[i]][y2[i]]--;
imos[x2[i]][y1[i]]--;
imos[x2[i]][y2[i]]++;
}
rep(i, 3010) rep1(j, 3009) imos[i][j] += imos[i][j - 1];
rep1(i, 3009) rep(j, 3010) imos[i][j] += imos[i - 1][j];
rep1(i, R.size() - 1) rep1(j, C.size() - 1) a[i][j] = imos[i - 1][j - 1];
/*rep(i,20){
rep(j,10){
printf("%d",a[i][j]);
}
puts("");
}*/
int cnt = 0;
rep(i, 3010) rep(j, 3010) cnt += dfs(i, j);
printf("%d\n", cnt);
}
} | replace | 42 | 44 | 42 | 44 | MLE | |
p00455 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int h[3] = {0};
int m[3] = {0};
int s[3] = {0};
int H[3] = {0};
int M[3] = {0};
int S[3] = {0};
int j, f, b;
for (int i = 0; i < 4; i++) {
cin >> h[i] >> m[i] >> s[i] >> H[i] >> M[i] >> S[i];
j = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) /
3600;
f = ((3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
j * 3600) /
60;
b = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
j * 3600 - f * 60;
cout << j << " " << f << " " << b << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int sh, sm, ss, eh, em, es;
int h, m, s;
for (int i = 0; i < 3; i++) {
cin >> sh >> sm >> ss >> eh >> em >> es;
if (es < ss) {
em = em - 1;
es = es + 60;
}
s = es - ss;
if (em < sm) {
eh = eh - 1;
em = em + 60;
}
m = em - sm;
h = eh - sh;
cout << h << " " << m << " " << s << endl;
}
return 0;
} | replace | 3 | 20 | 3 | 19 | 0 | |
p00457 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
int a[10000];
int solve(int p, int n) {
int l = p - 1, r = p + 1, t;
int s1, s2, ans = 0, f = 1;
while (0 <= l) {
t = a[l];
s1 = s2 = 0;
if (f)
s1 = 1, f = 0;
// if(p == 5)printf("%d %d %d\n", l, r, t);
while (0 <= l && a[l] == t) {
s1++;
l--;
}
while (r < n && a[r] == t) {
s2++;
r++;
}
if (s1 + s2 >= 4) {
ans += s1 + s2;
} else
break;
}
return n - ans;
}
int main() {
int n, ans;
while (1) {
scanf("%d", &n);
ans = INF;
rep(i, n) scanf("%d", &a[i]);
for (int i = 1; i < n - 1; i++) {
ans = min(ans, solve(i, n));
}
if (ans == INF)
printf("%d\n", n - 1);
else
printf("%d\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
int a[10000];
int solve(int p, int n) {
int l = p - 1, r = p + 1, t;
int s1, s2, ans = 0, f = 1;
while (0 <= l) {
t = a[l];
s1 = s2 = 0;
if (f)
s1 = 1, f = 0;
// if(p == 5)printf("%d %d %d\n", l, r, t);
while (0 <= l && a[l] == t) {
s1++;
l--;
}
while (r < n && a[r] == t) {
s2++;
r++;
}
if (s1 + s2 >= 4) {
ans += s1 + s2;
} else
break;
}
return n - ans;
}
int main() {
int n, ans;
while (1) {
scanf("%d", &n);
ans = INF;
rep(i, n) scanf("%d", &a[i]);
if (!n)
break;
for (int i = 1; i < n - 1; i++) {
ans = min(ans, solve(i, n));
}
if (ans == INF)
printf("%d\n", n - 1);
else
printf("%d\n", ans);
}
return 0;
} | insert | 46 | 46 | 46 | 48 | TLE | |
p00457 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
char chain[10000];
int ccount(int a, int b, int cnt) {
if (chain[a] != chain[b])
return cnt;
int len = 0;
int i, j;
for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
;
for (j = b; j < n && chain[b] == chain[j]; j++, len++)
;
if (len < 4)
return cnt;
if (i == -1 || j == n)
return cnt + len;
return ccount(i, j, cnt + len);
}
int main() {
while (true) {
scanf("%d", &n);
if (!n)
return 0;
int ans = 0;
for (int i = 0; i < n; i++)
scanf("%d", &chain[i]);
for (int i = 0; i < n; i++) {
char def = chain[i];
for (int j = 1; j <= 3; j++) {
chain[i] = j;
int s, g, cnt = 1;
for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
;
for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
;
if (cnt >= 4) {
if (s == -1 || g == n)
ans = max(cnt, ans);
else
ans = max(ccount(s, g, cnt), ans);
}
}
chain[i] = def;
}
printf("%d\n", n - ans);
}
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int chain[10002];
int ccount(int a, int b, int cnt) {
if (chain[a] != chain[b])
return cnt;
int len = 0;
int i, j;
for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
;
for (j = b; j < n && chain[b] == chain[j]; j++, len++)
;
if (len < 4)
return cnt;
if (i == -1 || j == n)
return cnt + len;
return ccount(i, j, cnt + len);
}
int main() {
while (true) {
scanf("%d", &n);
if (!n)
return 0;
int ans = 0;
for (int i = 0; i < n; i++)
scanf("%d", &chain[i]);
for (int i = 0; i < n; i++) {
char def = chain[i];
for (int j = 1; j <= 3; j++) {
chain[i] = j;
int s, g, cnt = 1;
for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
;
for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
;
if (cnt >= 4) {
if (s == -1 || g == n)
ans = max(cnt, ans);
else
ans = max(ccount(s, g, cnt), ans);
}
}
chain[i] = def;
}
printf("%d\n", n - ans);
}
} | replace | 6 | 7 | 6 | 7 | TLE | |
p00457 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
int n, x[10001][2], list[4], save[10001], l1 = 0, l2 = 0, ans = INF;
vector<int> v1, v2, v3;
int solve(int a) {
l1 = 0;
l2 = 0;
for (int i = 1; i < v2.size(); i++) {
if (v2[a + i] == v2[a]) {
l1++;
} else
break;
}
for (int i = 1; i < v2.size(); i++) {
if (v2[a - i] == v2[a]) {
l2++;
} else
break;
}
if (l1 + l2 >= 3) {
v2.erase(v2.begin() + a - l2, v2.begin() + a + l1 + 1);
solve(a - l2);
} else {
return v2.size();
}
}
int main() {
while (true) {
cin >> n;
if (n == 0) {
break;
}
for (int i = 1; i <= n; i++) {
cin >> x[i][0];
v1.push_back(x[i][0]);
if (i - list[x[i][0]] == 2) {
save[i - 1] = 2;
}
list[x[i][0]] = i;
if (x[i][0] == x[i - 1][0] && x[i][0] == x[i - 2][0]) {
save[i + 1] = 1;
save[i - 3] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (save[i] == 1) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
if (x[i + 1][0] == x[i + 2][0] && x[i + 1][0] == x[i + 3][0]) {
v2.insert(v2.begin() + i - 1, x[i + 1][0]);
} else if (x[i - 1][0] == x[i - 2][0] && x[i - 1][0] == x[i - 3][0]) {
v2.insert(v2.begin() + i - 1, x[i - 1][0]);
}
ans = min(solve(i - 1), ans);
} else if (save[i] == 2 && x[i + 1][0] == x[i + 2][0]) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
v2.insert(v2.begin() + i - 1, x[i + 1][0]);
ans = min(solve(i - 1), ans);
} else if (save[i] == 2 && x[i - 1][0] == x[i - 2][0]) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
v2.insert(v2.begin() + i - 1, x[i - 1][0]);
ans = min(solve(i - 1), ans);
} else
save[i] = 0;
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
x[i][0] = 0;
save[i] = 0;
}
list[0] = 0;
list[1] = 0;
list[2] = 0;
list[3] = 0;
ans = INF;
v1 = v3;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 100000000;
int n, x[10001][2], list[4], save[10001], l1 = 0, l2 = 0, ans = INF;
vector<int> v1, v2, v3;
int solve(int a) {
l1 = 0;
l2 = 0;
for (int i = 1; i < v2.size(); i++) {
if (v2[a + i] == v2[a]) {
l1++;
} else
break;
}
for (int i = 1; i < v2.size(); i++) {
if (v2[a - i] == v2[a]) {
l2++;
} else
break;
}
if (l1 + l2 >= 3) {
v2.erase(v2.begin() + a - l2, v2.begin() + a + l1 + 1);
solve(a - l2 - 1);
} else {
return v2.size();
}
}
int main() {
while (true) {
cin >> n;
if (n == 0) {
break;
}
for (int i = 1; i <= n; i++) {
cin >> x[i][0];
v1.push_back(x[i][0]);
if (i - list[x[i][0]] == 2) {
save[i - 1] = 2;
}
list[x[i][0]] = i;
if (x[i][0] == x[i - 1][0] && x[i][0] == x[i - 2][0]) {
save[i + 1] = 1;
save[i - 3] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (save[i] == 1) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
if (x[i + 1][0] == x[i + 2][0] && x[i + 1][0] == x[i + 3][0]) {
v2.insert(v2.begin() + i - 1, x[i + 1][0]);
} else if (x[i - 1][0] == x[i - 2][0] && x[i - 1][0] == x[i - 3][0]) {
v2.insert(v2.begin() + i - 1, x[i - 1][0]);
}
ans = min(solve(i - 1), ans);
} else if (save[i] == 2 && x[i + 1][0] == x[i + 2][0]) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
v2.insert(v2.begin() + i - 1, x[i + 1][0]);
ans = min(solve(i - 1), ans);
} else if (save[i] == 2 && x[i - 1][0] == x[i - 2][0]) {
v2 = v1;
v2.erase(v2.begin() + i - 1);
v2.insert(v2.begin() + i - 1, x[i - 1][0]);
ans = min(solve(i - 1), ans);
} else
save[i] = 0;
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
x[i][0] = 0;
save[i] = 0;
}
list[0] = 0;
list[1] = 0;
list[2] = 0;
list[3] = 0;
ans = INF;
v1 = v3;
}
return 0;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p00457 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
while (1) {
int n;
scanf("%d", &n);
w = n;
ans = n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 3; j++) {
memcpy(b, a, sizeof(int) * n);
if (b[i] != j) {
b[i] = j;
kotani(0, 1, 1, n);
}
free(b);
}
}
printf("%d\n", ans);
}
}
void kotani(int i, int j, int c, int n) {
if (b[i] == b[j] && j < n) {
kotani(i, j + 1, c + 1, n);
} else {
if (c >= 4) {
int u = 0;
for (int z = j; z < n; z++) {
b[i + u] = b[z];
u++;
}
ans = min(ans, n - c);
kotani(0, 1, 1, n - c);
} else if (i + 1 < n) {
kotani(i + 1, i + 2, 1, n);
}
}
} | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
while (1) {
int n;
scanf("%d", &n);
w = n;
ans = n;
if (n == 0)
break;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 3; j++) {
memcpy(b, a, sizeof(int) * n);
if (b[i] != j) {
b[i] = j;
kotani(0, 1, 1, n);
}
}
}
printf("%d\n", ans);
}
}
void kotani(int i, int j, int c, int n) {
if (b[i] == b[j] && j < n) {
kotani(i, j + 1, c + 1, n);
} else {
if (c >= 4) {
int u = 0;
for (int z = j; z < n; z++) {
b[i + u] = b[z];
u++;
}
ans = min(ans, n - c);
kotani(0, 1, 1, n - c);
} else if (i + 1 < n) {
kotani(i + 1, i + 2, 1, n);
}
}
} | delete | 27 | 28 | 27 | 27 | -6 | free(): invalid pointer
|
p00457 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
void debug(vector<P> &v) {
cout << "[debug]" << endl;
for (int i = 0; i < v.size(); i++) {
cout << "(" << v[i].first << "," << v[i].second << ")" << endl;
}
cout << endl;
}
vector<P> remove(vector<P> a) {
bool flag = false;
vector<P> a_;
for (int i = 0; i < a.size(); i++) {
if (a[i].second <= 0 || a[i].second >= 4) {
flag = true;
} else {
if (!a_.empty() && a_[a_.size() - 1].first == a[i].first) {
flag = true;
a_[a_.size() - 1].second += a[i].second;
} else {
a_.push_back(a[i]);
}
}
}
if (flag)
return remove(a_);
return a_;
}
int sum(vector<P> &a) {
int s = 0;
for (int i = 0; i < a.size(); i++) {
s += a[i].second;
}
return s;
}
int solve(vector<P> &a) {
int ans = sum(a);
for (int i = 0; i < a.size(); i++) {
if (0 <= i - 1) {
// 1つ前の色に変える.
int c = a[i - 1].first;
vector<P> a_ = a;
a_[i].second--;
a_[i - 1].second++;
a_ = remove(a_);
/*if( sum(a_) < ans ){
cout << "change:" << i << " color:" << c << endl;
debug(a_);
}*/
ans = min(ans, sum(a_));
}
if (i + 1 < a.size()) {
// 1つ後ろの色に変える.
int c = a[i + 1].first;
vector<P> a_ = a;
a_[i].second--;
a_[i + 1].second++;
a_ = remove(a_);
/*if( sum(a_) < ans ){
cout << "change:" << i << " color:" << c << endl;
debug(a_);
}*/
ans = min(ans, sum(a_));
}
}
return ans;
}
int main() {
int n;
while (cin >> n, n) {
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<P> a;
for (int i = 0; i < v.size(); i++) {
if (i == 0) {
a.push_back(P(v[i], 1));
} else if (v[i - 1] == v[i]) {
a[a.size() - 1].second++;
} else {
a.push_back(P(v[i], 1));
}
}
cout << solve(a) << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef pair<int, int> P;
void debug(vector<P> &v) {
cout << "[debug]" << endl;
for (int i = 0; i < v.size(); i++) {
cout << "(" << v[i].first << "," << v[i].second << ")" << endl;
}
cout << endl;
}
vector<P> remove(vector<P> &a) {
bool flag = false;
vector<P> a_;
for (int i = 0; i < a.size(); i++) {
if (a[i].second <= 0 || a[i].second >= 4) {
flag = true;
} else {
if (!a_.empty() && a_[a_.size() - 1].first == a[i].first) {
flag = true;
a_[a_.size() - 1].second += a[i].second;
} else {
a_.push_back(a[i]);
}
}
}
if (flag)
return remove(a_);
return a_;
}
int sum(vector<P> &a) {
int s = 0;
for (int i = 0; i < a.size(); i++) {
s += a[i].second;
}
return s;
}
int solve(vector<P> &a) {
int ans = sum(a);
for (int i = 0; i < a.size(); i++) {
if (0 <= i - 1) {
// 1つ前の色に変える.
int c = a[i - 1].first;
vector<P> a_ = a;
a_[i].second--;
a_[i - 1].second++;
a_ = remove(a_);
/*if( sum(a_) < ans ){
cout << "change:" << i << " color:" << c << endl;
debug(a_);
}*/
ans = min(ans, sum(a_));
}
if (i + 1 < a.size()) {
// 1つ後ろの色に変える.
int c = a[i + 1].first;
vector<P> a_ = a;
a_[i].second--;
a_[i + 1].second++;
a_ = remove(a_);
/*if( sum(a_) < ans ){
cout << "change:" << i << " color:" << c << endl;
debug(a_);
}*/
ans = min(ans, sum(a_));
}
}
return ans;
}
int main() {
int n;
while (cin >> n, n) {
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<P> a;
for (int i = 0; i < v.size(); i++) {
if (i == 0) {
a.push_back(P(v[i], 1));
} else if (v[i - 1] == v[i]) {
a[a.size() - 1].second++;
} else {
a.push_back(P(v[i], 1));
}
}
cout << solve(a) << endl;
}
} | replace | 16 | 17 | 16 | 17 | MLE | |
p00457 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int64;
const int inf = (int)1e9;
int calc(int it1, int it2, vector<pair<int, int>> &b, vector<int> &sum, int n) {
while (it1 >= 0 && it2 < b.size()) {
if (b[it1].second <= 0) {
it1 -= 1;
continue;
}
if (b[it2].second <= 0) {
it1 += 1;
continue;
}
if (b[it1].first != b[it2].first || b[it1].second + b[it2].second < 4)
break;
it1 -= 1, it2 += 1;
}
int score = n - (sum[it2 - 1] - sum[it1]);
return score;
}
int main() {
while (true) {
int n;
vector<pair<int, int>> b;
vector<int> sum;
int prev = -1;
scanf("%d", &n);
if (n == 0)
break;
b.reserve(n);
for (int i = 0; i < n; ++i) {
int t;
scanf("%d", &t);
if (prev != t)
b.push_back(make_pair(t, 1));
else
b[b.size() - 1].second += 1;
prev = t;
}
sum.resize(b.size(), 0);
sum[0] = b[0].second;
for (int i = 1; i < sum.size(); ++i)
sum[i] = sum[i - 1] + b[i].second;
int ans = n;
for (int start = 0; start < b.size(); ++start) {
b[start].second -= 1;
if (start > 0 && b[start - 1].second >= 3) {
int it1 = start - 2, it2 = start;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
if (start < b.size() - 1 && b[start + 1].second >= 3) {
int it1 = start, it2 = start + 2;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
if (start > 0 && start < b.size() - 1 && b[start].second == 0 &&
b[start - 1].first == b[start + 1].first &&
b[start - 1].second + b[start + 1].second >= 3) {
int it1 = start - 2, it2 = start + 2;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
b[start].second += 1;
}
printf("%d\n", ans);
}
return 0;
}
/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */ | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int64;
const int inf = (int)1e9;
int calc(int it1, int it2, vector<pair<int, int>> &b, vector<int> &sum, int n) {
while (it1 >= 0 && it2 < b.size()) {
if (b[it1].second <= 0) {
it1 -= 1;
continue;
}
if (b[it2].second <= 0) {
it2 += 1;
continue;
}
if (b[it1].first != b[it2].first || b[it1].second + b[it2].second < 4)
break;
it1 -= 1, it2 += 1;
}
int score = n - (sum[it2 - 1] - sum[it1]);
return score;
}
int main() {
while (true) {
int n;
vector<pair<int, int>> b;
vector<int> sum;
int prev = -1;
scanf("%d", &n);
if (n == 0)
break;
b.reserve(n);
for (int i = 0; i < n; ++i) {
int t;
scanf("%d", &t);
if (prev != t)
b.push_back(make_pair(t, 1));
else
b[b.size() - 1].second += 1;
prev = t;
}
sum.resize(b.size(), 0);
sum[0] = b[0].second;
for (int i = 1; i < sum.size(); ++i)
sum[i] = sum[i - 1] + b[i].second;
int ans = n;
for (int start = 0; start < b.size(); ++start) {
b[start].second -= 1;
if (start > 0 && b[start - 1].second >= 3) {
int it1 = start - 2, it2 = start;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
if (start < b.size() - 1 && b[start + 1].second >= 3) {
int it1 = start, it2 = start + 2;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
if (start > 0 && start < b.size() - 1 && b[start].second == 0 &&
b[start - 1].first == b[start + 1].first &&
b[start - 1].second + b[start + 1].second >= 3) {
int it1 = start - 2, it2 = start + 2;
int score = calc(it1, it2, b, sum, n);
ans = min(score, ans);
}
b[start].second += 1;
}
printf("%d\n", ans);
}
return 0;
}
/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */ | replace | 25 | 26 | 25 | 26 | TLE | |
p00457 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int a[10000];
int b[10001];
int ans = 1e9;
int c(int x, int y) { // change b[x] to y return remaining
b[x] = y;
int e = n;
bool f = true;
while (f) {
f = false;
int d = 1;
int p = b[0];
for (int i = 1; i < n + 1; ++i) {
if (b[i] == 4)
continue;
if (b[i] == p)
++d;
else {
if (d > 3) {
f = true;
int j = 0, l = i - 1;
while (j < d) {
if (b[l] == 4) {
--l;
continue;
}
b[l] = 4;
++j;
--l;
--e;
/* for(int k=0; k<n; ++k) printf("%d ", b[k]);
printf("\n");
*/ }
}
d = 1;
}
p = b[i];
}
}
/* if(x == 5){
for(int i=0; i<n; ++i) printf("%d ", b[i]);
printf("\n");
}
*/
return e;
}
int main() {
while (1) {
memset(a, 0, sizeof(a));
scanf("%d", &n);
if (n == 0)
return 0;
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
ans = 1e9;
for (int i = 0; i < n; ++i)
for (int j = 1; j < 4; ++j) {
for (int k = 0; k < n; ++k)
b[k] = a[k];
ans = min(ans, c(i, j));
// printf("%d %d\n", i, j);
// printf("%d\n", ans);
}
printf("%d\n", ans);
}
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int a[10000];
int b[10001];
int ans = 1e9;
int c(int x, int y) { // change b[x] to y return remaining
b[x] = y;
int e = n;
bool f = true;
while (f) {
f = false;
int d = 1;
int p = b[0];
for (int i = 1; i < n + 1; ++i) {
if (b[i] == 4)
continue;
if (b[i] == p)
++d;
else {
if (d > 3) {
f = true;
int j = 0, l = i - 1;
while (j < d) {
if (b[l] == 4) {
--l;
continue;
}
b[l] = 4;
++j;
--l;
--e;
/* for(int k=0; k<n; ++k) printf("%d ", b[k]);
printf("\n");
*/ }
}
d = 1;
}
p = b[i];
}
}
/* if(x == 5){
for(int i=0; i<n; ++i) printf("%d ", b[i]);
printf("\n");
}
*/
return e;
}
int main() {
while (1) {
memset(a, 0, sizeof(a));
scanf("%d", &n);
if (n == 0)
return 0;
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
ans = 1e9;
for (int i = 0; i < n; ++i)
for (int j = 1; j < 4; ++j) {
for (int k = 0; k < n; ++k)
b[k] = a[k];
if (a[i] != j) {
ans = min(ans, c(i, j));
}
// printf("%d %d\n", i, j);
// printf("%d\n", ans);
}
printf("%d\n", ans);
}
} | replace | 67 | 68 | 67 | 70 | TLE | |
p00458 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
int maxim = 0;
maps[y][x] = false;
REP(i, 4)
if (y + dy[i] && x + dx[i] && maps[y + dy[i]][x + dx[i]])
maxim = max(maxim, search(y + dy[i], x + dx[i]));
maps[y][x] = true;
return maxim + 1;
}
int main() {
// your code goes here
int m, n;
while (cin >> m >> n && n) {
int maxim = 0;
REP(i, n)
REP(j, m)
cin >> maps[i][j];
REP(i, n)
REP(j, m)
if (maps[i][j])
maxim = max(maxim, search(i, j));
cout << maxim << endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
int maxim = 0;
maps[y][x] = false;
REP(i, 4)
if (y + dy[i] >= 0 && x + dx[i] >= 0 && maps[y + dy[i]][x + dx[i]])
maxim = max(maxim, search(y + dy[i], x + dx[i]));
maps[y][x] = true;
return maxim + 1;
}
int main() {
// your code goes here
int m, n;
while (cin >> m >> n && n) {
int maxim = 0;
REP(i, n)
REP(j, m)
cin >> maps[i][j];
REP(i, n)
REP(j, m)
if (maps[i][j])
maxim = max(maxim, search(i, j));
cout << maxim << endl;
}
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p00458 | C++ | Time Limit Exceeded | #include "stdio.h"
int ido(int x, int y, int aw[90][90]) {
int a[90][90];
for (int i = 0; i < 90; i++) {
for (int ii = 0; ii < 90; ii++) {
a[i][ii] = aw[i][ii];
}
}
a[x][y] = 0;
int max = 0;
if (x + 1 < 90) {
if (a[x + 1][y] == 1) {
int w = ido(x + 1, y, a);
if (max < w) {
max = w;
}
}
}
if (0 <= x - 1) {
if (a[x - 1][y] == 1) {
int w = ido(x - 1, y, a);
if (max < w) {
max = w;
}
}
}
if (y + 1 < 90) {
if (a[x][y + 1] == 1) {
int w = ido(x, y + 1, a);
if (max < w) {
max = w;
}
}
}
if (0 <= y - 1) {
if (a[x][y - 1] == 1) {
int w = ido(x, y - 1, a);
if (max < w) {
max = w;
}
}
}
return max + 1;
}
int main() {
while (1) {
int m, n;
scanf("%d", &m);
scanf("%d", &n);
if (n == 0 && m == 0) {
break;
}
int aw[90][90];
for (int i = 0; i < 90; i++) {
for (int ii = 0; ii < 90; ii++) {
aw[i][ii] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int ii = 0; ii < m; ii++) {
scanf("%d", &aw[i][ii]);
}
}
int max = 0;
for (int i = 0; i < n; i++) {
for (int ii = 0; ii < m; ii++) {
if (aw[i][ii] == 1) {
int w = ido(i, ii, aw);
if (max < w) {
max = w;
}
}
}
}
printf("%d\n", max);
}
return 0;
} | #include "stdio.h"
int ido(int x, int y, int aw[90][90]) {
int a[90][90];
for (int i = 0; i < 90; i++) {
for (int ii = 0; ii < 90; ii++) {
a[i][ii] = aw[i][ii];
}
}
a[x][y] = 0;
int max = 0;
if (x + 1 < 90) {
if (a[x + 1][y] == 1) {
int w = ido(x + 1, y, a);
if (max < w) {
max = w;
}
}
}
if (0 <= x - 1) {
if (a[x - 1][y] == 1) {
int w = ido(x - 1, y, a);
if (max < w) {
max = w;
}
}
}
if (y + 1 < 90) {
if (a[x][y + 1] == 1) {
int w = ido(x, y + 1, a);
if (max < w) {
max = w;
}
}
}
if (0 <= y - 1) {
if (a[x][y - 1] == 1) {
int w = ido(x, y - 1, a);
if (max < w) {
max = w;
}
}
}
return max + 1;
}
int main() {
while (1) {
int m, n;
scanf("%d", &m);
scanf("%d", &n);
if (n == 0 && m == 0) {
break;
}
int aw[90][90];
for (int i = 0; i < 90; i++) {
for (int ii = 0; ii < 90; ii++) {
aw[i][ii] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int ii = 0; ii < m; ii++) {
scanf("%d", &aw[i][ii]);
}
}
int max = 0;
for (int i = 0; i < n; i++) {
for (int ii = 0; ii < m; ii++) {
if (aw[i][ii] == 1) {
int h = 0;
if (0 <= i - 1) {
h += aw[i - 1][ii];
}
if (i + 1 < 90) {
h += aw[i + 1][ii];
}
if (0 <= ii - 1) {
h += aw[i][ii - 1];
}
if (ii + 1 < 90) {
h += aw[i][ii + 1];
}
if (h == 1) {
int w = ido(i, ii, aw);
if (max < w) {
max = w;
}
}
}
}
}
printf("%d\n", max);
}
return 0;
} | replace | 69 | 72 | 69 | 87 | TLE | |
p00458 | C++ | Runtime Error | #include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890
int map[60][60];
int x_max, y_max;
int ans;
int dp(int x, int y) {
if (map[x][y] == 0)
return 0;
int answer = 1;
map[x][y] = 0;
if (map[x][y - 1] == 1)
answer = max(answer, dp(x, y - 1) + 1);
if (map[x][y + 1] == 1)
answer = max(answer, dp(x, y + 1) + 1);
if (map[x - 1][y] == 1)
answer = max(answer, dp(x - 1, y) + 1);
if (map[x + 1][y] == 1)
answer = max(answer, dp(x + 1, y) + 1);
map[x][y] = 1;
return answer;
}
int main() {
while (1) {
memset(map, 0, sizeof(map));
ans = 0;
scanf("%d%d", &x_max, &y_max);
if (x_max == 0 && y_max == 0)
break;
for (int a = 1; a <= x_max; a++) {
for (int b = 1; b <= y_max; b++) {
scanf("%d", &map[a][b]);
}
}
for (int a = 1; a <= x_max; a++) {
for (int b = 1; b <= y_max; b++) {
ans = max(ans, dp(a, b));
}
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890
int map[100][100];
int x_max, y_max;
int ans;
int dp(int x, int y) {
if (map[x][y] == 0)
return 0;
int answer = 1;
map[x][y] = 0;
if (map[x][y - 1] == 1)
answer = max(answer, dp(x, y - 1) + 1);
if (map[x][y + 1] == 1)
answer = max(answer, dp(x, y + 1) + 1);
if (map[x - 1][y] == 1)
answer = max(answer, dp(x - 1, y) + 1);
if (map[x + 1][y] == 1)
answer = max(answer, dp(x + 1, y) + 1);
map[x][y] = 1;
return answer;
}
int main() {
while (1) {
memset(map, 0, sizeof(map));
ans = 0;
scanf("%d%d", &x_max, &y_max);
if (x_max == 0 && y_max == 0)
break;
for (int a = 1; a <= x_max; a++) {
for (int b = 1; b <= y_max; b++) {
scanf("%d", &map[a][b]);
}
}
for (int a = 1; a <= x_max; a++) {
for (int b = 1; b <= y_max; b++) {
ans = max(ans, dp(a, b));
}
}
printf("%d\n", ans);
}
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p00459 | C++ | Time Limit Exceeded | #include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct tb {
int f, t;
tb(int f, int t) : f(f), t(t) {}
};
int n, m, pr, q, r;
int chx[5001][3];
int main(void) {
while (1) {
scanf("%d", &n);
if (n == 0)
break;
queue<tb> c;
queue<tb> d[4];
c.push(tb(1, n));
scanf("%d", &m);
scanf("%d%d%d", &pr, &q, &r);
for (int i = 0; i < m; i++) {
scanf("%d%d", &chx[i][1], &chx[i][2]);
}
for (int i = 0; i < m; i++) {
int sh = 1, size = 0;
while (c.size()) {
tb p = c.front();
c.pop();
size += p.t - p.f + 1;
if (sh != 3) {
if (size <= chx[i][sh]) {
d[sh].push(tb(p.f, p.t));
if (size == chx[i][sh])
sh++;
} else if (sh == 2) {
int x = size - p.t + p.f - 1;
int xx = chx[i][2] - x;
d[2].push(tb(p.f, p.f + xx - 1));
d[3].push(tb(p.f + xx, p.t));
sh++;
} else if (sh == 1 && size <= chx[i][2]) {
int x = size - p.t + p.f - 1;
int xx = chx[i][1] - x;
d[1].push(tb(p.f, p.f + xx - 1));
d[2].push(tb(p.f + xx, p.t));
if (size == chx[i][2])
sh++;
sh++;
} else {
int x = size - p.t + p.f - 1;
int xx = chx[i][1] - x;
int x2 = chx[i][2] - chx[i][1];
d[1].push(tb(p.f, p.f + xx - 1));
d[2].push(tb(p.f + xx, p.f + xx + x2 - 1));
d[3].push(tb(p.f + xx + x2, p.t));
sh = 3;
}
} else
d[sh].push(tb(p.f, p.t));
}
for (int j = 3; j >= 1; j--) {
while (d[j].size()) {
tb p = d[j].front();
d[j].pop();
int pf = p.f, pt = p.t;
c.push(tb(pf, pt));
}
}
}
int size = 1;
int ans = 0;
while (c.size()) {
tb p = c.front();
c.pop();
while (p.f != p.t + 1) {
if (size >= pr && size <= q && p.f <= r)
ans++;
p.f++;
size++;
}
}
printf("%d\n", ans);
}
return 0;
} | #include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct tb {
int f, t;
tb(int f, int t) : f(f), t(t) {}
};
int n, m, pr, q, r;
int chx[5001][3];
int main(void) {
while (1) {
scanf("%d", &n);
if (n == 0)
break;
queue<tb> c;
queue<tb> d[4];
c.push(tb(1, n));
scanf("%d", &m);
scanf("%d%d%d", &pr, &q, &r);
for (int i = 0; i < m; i++) {
scanf("%d%d", &chx[i][1], &chx[i][2]);
}
for (int i = 0; i < m; i++) {
int sh = 1, size = 0;
while (c.size()) {
tb p = c.front();
c.pop();
size += p.t - p.f + 1;
if (sh != 3) {
if (size <= chx[i][sh]) {
d[sh].push(tb(p.f, p.t));
if (size == chx[i][sh])
sh++;
} else if (sh == 2) {
int x = size - p.t + p.f - 1;
int xx = chx[i][2] - x;
d[2].push(tb(p.f, p.f + xx - 1));
d[3].push(tb(p.f + xx, p.t));
sh++;
} else if (sh == 1 && size <= chx[i][2]) {
int x = size - p.t + p.f - 1;
int xx = chx[i][1] - x;
d[1].push(tb(p.f, p.f + xx - 1));
d[2].push(tb(p.f + xx, p.t));
if (size == chx[i][2])
sh++;
sh++;
} else {
int x = size - p.t + p.f - 1;
int xx = chx[i][1] - x;
int x2 = chx[i][2] - chx[i][1];
d[1].push(tb(p.f, p.f + xx - 1));
d[2].push(tb(p.f + xx, p.f + xx + x2 - 1));
d[3].push(tb(p.f + xx + x2, p.t));
sh = 3;
}
} else
d[sh].push(tb(p.f, p.t));
}
for (int j = 3; j >= 1; j--) {
while (d[j].size()) {
tb p = d[j].front();
d[j].pop();
int pf = p.f, pt = p.t;
c.push(tb(pf, pt));
}
}
}
int size = 1;
int ans = 0;
while (c.size()) {
tb p = c.front();
c.pop();
int x = p.f, y = p.t;
int s = y - x + 1;
if (size < pr)
x += pr - size;
if (size + s > q)
y -= size + s - q - 1;
if (y > r)
y = r;
if (y >= x)
ans += y - x + 1;
// printf("%d %d %d %d\n",p.f,p.t,x,y);
size += s;
}
printf("%d\n", ans);
}
return 0;
} | replace | 78 | 84 | 78 | 90 | TLE | |
p00459 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)
vector<int> u1, v1, u2, v2;
void f(int x, int y, vector<int> &u1, vector<int> &v1, vector<int> &u2,
vector<int> &v2) {
int c = 0;
int a, b, c1, c2;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= x) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= y) {
b = i;
c2 = c;
break;
}
}
if (c2 != y) {
u2.push_back(v1[b] - (c2 - y) + 1);
v2.push_back(v1[b]);
REP(i, b + 1, u1.size() - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
} else {
REP(i, b + 1, u1.size() - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
}
if (a != b) {
if (c1 != x) {
u2.push_back(v1[a] - (c1 - x) + 1);
v2.push_back(v1[a]);
REP(i, a + 1, b - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[b]);
v2.push_back(v1[b] - (c2 - y));
} else {
REP(i, a + 1, b - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[b]);
v2.push_back(v1[b] - (c2 - y));
}
}
else {
u2.push_back(v1[a] - (c1 - x) + 1);
u2.push_back(v1[b] - (c2 - y));
}
REP(i, 0, a - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[a]);
v2.push_back(v1[a] - (c1 - x));
}
int main() {
int m, n, p, q, r, x, y;
while (1) {
cin >> n;
if (n == 0)
break;
cin >> m >> p >> q >> r;
REP(i, 1, m) {
cin >> x >> y;
if (i == 1) {
u1.push_back(y + 1);
v1.push_back(n);
u1.push_back(x + 1);
v1.push_back(y);
u1.push_back(1);
v1.push_back(x);
} else if (i % 2 == 0) {
u2.clear();
v2.clear();
f(x, y, u1, v1, u2, v2);
} else if (i % 2 == 1) {
u1.clear();
v1.clear();
f(x, y, u2, v2, u1, v1);
}
}
int s = 0;
int c = 0;
int a, b, c1, c2;
if (m % 2 == 0) {
rep(i, u2.size()) {
c += v2[i] - u2[i] + 1;
if (c >= p) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u2.size()) {
c += v2[i] - u2[i] + 1;
if (c >= q) {
b = i;
c2 = c;
break;
}
}
if (a != b) {
REP(i, v2[a] - (c1 - p), v2[a])
if (i <= r)
s++;
REP(i, a + 1, b - 1) {
REP(j, u2[i], v2[i]) {
if (j <= r)
s++;
}
}
REP(i, u2[b], v2[b] - (c2 - q))
if (i <= r)
s++;
}
else {
REP(i, v2[a] - (c1 - p), v2[a] - (c2 - q))
if (i <= r)
s++;
}
cout << s << endl;
}
else {
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= p) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= q) {
b = i;
c2 = c;
break;
}
}
if (a != b) {
REP(i, v1[a] - (c1 - p), v1[a])
if (i <= r)
s++;
REP(i, a + 1, b - 1) {
REP(j, u1[i], v1[i]) {
if (j <= r)
s++;
}
}
REP(i, u1[b], v1[b] - (c2 - q))
if (i <= r)
s++;
}
else {
REP(i, v1[a] - (c1 - p), v1[a] - (c2 - q))
if (i <= r)
s++;
}
cout << s << endl;
}
u1.clear();
v1.clear();
u2.clear();
v2.clear();
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)
vector<int> u1, v1, u2, v2;
void f(int x, int y, vector<int> &u1, vector<int> &v1, vector<int> &u2,
vector<int> &v2) {
int c = 0;
int a, b, c1, c2;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= x) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= y) {
b = i;
c2 = c;
break;
}
}
if (c2 != y) {
u2.push_back(v1[b] - (c2 - y) + 1);
v2.push_back(v1[b]);
REP(i, b + 1, u1.size() - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
} else {
REP(i, b + 1, u1.size() - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
}
if (a != b) {
if (c1 != x) {
u2.push_back(v1[a] - (c1 - x) + 1);
v2.push_back(v1[a]);
REP(i, a + 1, b - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[b]);
v2.push_back(v1[b] - (c2 - y));
} else {
REP(i, a + 1, b - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[b]);
v2.push_back(v1[b] - (c2 - y));
}
}
else {
u2.push_back(v1[a] - (c1 - x) + 1);
v2.push_back(v1[b] - (c2 - y));
}
REP(i, 0, a - 1) {
u2.push_back(u1[i]);
v2.push_back(v1[i]);
}
u2.push_back(u1[a]);
v2.push_back(v1[a] - (c1 - x));
}
int main() {
int m, n, p, q, r, x, y;
while (1) {
cin >> n;
if (n == 0)
break;
cin >> m >> p >> q >> r;
REP(i, 1, m) {
cin >> x >> y;
if (i == 1) {
u1.push_back(y + 1);
v1.push_back(n);
u1.push_back(x + 1);
v1.push_back(y);
u1.push_back(1);
v1.push_back(x);
} else if (i % 2 == 0) {
u2.clear();
v2.clear();
f(x, y, u1, v1, u2, v2);
} else if (i % 2 == 1) {
u1.clear();
v1.clear();
f(x, y, u2, v2, u1, v1);
}
}
int s = 0;
int c = 0;
int a, b, c1, c2;
if (m % 2 == 0) {
rep(i, u2.size()) {
c += v2[i] - u2[i] + 1;
if (c >= p) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u2.size()) {
c += v2[i] - u2[i] + 1;
if (c >= q) {
b = i;
c2 = c;
break;
}
}
if (a != b) {
REP(i, v2[a] - (c1 - p), v2[a])
if (i <= r)
s++;
REP(i, a + 1, b - 1) {
REP(j, u2[i], v2[i]) {
if (j <= r)
s++;
}
}
REP(i, u2[b], v2[b] - (c2 - q))
if (i <= r)
s++;
}
else {
REP(i, v2[a] - (c1 - p), v2[a] - (c2 - q))
if (i <= r)
s++;
}
cout << s << endl;
}
else {
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= p) {
a = i;
c1 = c;
break;
}
}
c = 0;
rep(i, u1.size()) {
c += v1[i] - u1[i] + 1;
if (c >= q) {
b = i;
c2 = c;
break;
}
}
if (a != b) {
REP(i, v1[a] - (c1 - p), v1[a])
if (i <= r)
s++;
REP(i, a + 1, b - 1) {
REP(j, u1[i], v1[i]) {
if (j <= r)
s++;
}
}
REP(i, u1[b], v1[b] - (c2 - q))
if (i <= r)
s++;
}
else {
REP(i, v1[a] - (c1 - p), v1[a] - (c2 - q))
if (i <= r)
s++;
}
cout << s << endl;
}
u1.clear();
v1.clear();
u2.clear();
v2.clear();
}
return 0;
} | replace | 86 | 87 | 86 | 87 | TLE | |
p00459 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n, m, p, q, r;
int main() {
while (scanf("%d", &n), n) {
scanf("%d", &m);
scanf("%d%d%d", &p, &q, &r);
vector<P> *z = new vector<P>;
z->push_back(P(1, n));
for (int i = 0; i < m; i++) {
int x[3] = {0, 0, n};
scanf("%d%d", &x[0], &x[1]);
vector<P> *b = new vector<P>;
vector<P> c[3];
int count = 0, cx = 1;
int j = 0;
while (cx <= n)
if (cx + (z->at(j).second) - (z->at(j).first) <= x[count]) {
c[count].push_back(z->at(j));
cx += (z->at(j).second) - (z->at(j).first) + 1;
j++;
} else {
if (cx != x[count] + 1)
c[count].push_back(
P(z->at(j).first, z->at(j).first + x[count] - cx));
z->at(j).first += x[count] - cx + 1;
cx = x[count] + 1;
count++;
}
for (j = 2; j >= 0; j--)
for (int k = 0; k < c[j].size(); k++)
b->push_back(c[j][k]);
z = b;
}
int res = 0, count = 1;
for (int i = 0; i < z->size(); i++) {
P pp = z->at(i);
int c1 = count + pp.second - pp.first;
if (count >= p && q >= c1)
;
else if (count <= p && p <= c1)
pp.first += p - count;
else if (count <= q && q <= c1)
pp.second -= c1 - q;
else {
count = c1 + 1;
continue;
}
count = c1 + 1;
if (r >= pp.second)
res += pp.second - pp.first + 1;
else if (r >= pp.first)
res += r - pp.first + 1;
}
printf("%d\n", res);
}
} | #include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n, m, p, q, r;
int main() {
while (scanf("%d", &n), n) {
scanf("%d", &m);
scanf("%d%d%d", &p, &q, &r);
vector<P> *z = new vector<P>;
z->push_back(P(1, n));
for (int i = 0; i < m; i++) {
int x[3] = {0, 0, n};
scanf("%d%d", &x[0], &x[1]);
vector<P> *b = new vector<P>;
vector<P> c[3];
int count = 0, cx = 1;
int j = 0;
while (cx <= n)
if (cx + (z->at(j).second) - (z->at(j).first) <= x[count]) {
c[count].push_back(z->at(j));
cx += (z->at(j).second) - (z->at(j).first) + 1;
j++;
} else {
if (cx != x[count] + 1)
c[count].push_back(
P(z->at(j).first, z->at(j).first + x[count] - cx));
z->at(j).first += x[count] - cx + 1;
cx = x[count] + 1;
count++;
}
for (j = 2; j >= 0; j--)
for (int k = 0; k < c[j].size(); k++)
b->push_back(c[j][k]);
delete z;
z = b;
}
int res = 0, count = 1;
for (int i = 0; i < z->size(); i++) {
P pp = z->at(i);
int c1 = count + pp.second - pp.first;
if (count >= p && q >= c1)
;
else if (count <= p && p <= c1)
pp.first += p - count;
else if (count <= q && q <= c1)
pp.second -= c1 - q;
else {
count = c1 + 1;
continue;
}
count = c1 + 1;
if (r >= pp.second)
res += pp.second - pp.first + 1;
else if (r >= pp.first)
res += r - pp.first + 1;
}
printf("%d\n", res);
}
} | insert | 39 | 39 | 39 | 40 | MLE | |
p00460 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
while (1) {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0)
break;
static int dp[2002][3002] = {};
rep(i, 2002) {
rep(j, 3002) {
if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = 0;
}
}
int l = 0, r;
rep1(i, n * n) {
l += i;
r = s;
for (int j = i + 1; j <= n * n; j++) {
r -= j;
}
for (int j = r; j >= l; j--) {
rep1(k, min(m, j)) {
dp[k][j] = dp[k - 1][j - k];
if (dp[k][j] >= 100000)
dp[k][j] %= 100000;
}
for (int k = min(m, j) + 1; k <= m; k++) {
dp[k][j] = 0;
}
dp[0][j] = 0;
}
for (int j = l - i; j < l; j++) {
rep1(k, m) { dp[k][j] = 0; }
dp[0][j] = 0;
}
for (int j = l; j <= r; j++) {
rep1(k, m) {
dp[k][j] += dp[k - 1][j];
if (dp[k][j] >= 100000)
dp[k][j] %= 100000;
}
}
}
printf("%d\n", dp[m][s]);
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
while (1) {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0)
break;
static int dp[2002][3002] = {};
rep(i, 2002) {
rep(j, 3002) {
if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = 0;
}
}
int l = 0, r;
rep1(i, n * n) {
l += i;
r = s;
for (int j = i + 1; j <= n * n; j++) {
r -= j;
}
for (int j = r; j >= l; j--) {
rep1(k, min(m, j)) {
dp[k][j] = dp[k - 1][j - k];
if (dp[k][j] >= 100000)
dp[k][j] %= 100000;
}
for (int k = min(m, j) + 1; k <= m; k++) {
dp[k][j] = 0;
}
dp[0][j] = 0;
}
for (int j = l - i; j < l; j++) {
rep1(k, m) { dp[k][j] = 0; }
dp[0][j] = 0;
}
for (int j = l; j <= r; j++) {
for (int k = i; k <= m && k * (n * n - i) <= s; k++) {
// rep1(k,m){
dp[k][j] += dp[k - 1][j];
if (dp[k][j] >= 100000)
dp[k][j] %= 100000;
}
}
}
printf("%d\n", dp[m][s]);
}
} | replace | 83 | 84 | 83 | 85 | TLE | |
p00460 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
#define reps(i, n) for (i = 1; i <= n; i++)
int dp[50][2][3001];
int main() {
int n, m, s, nn, st, i, j, k;
int t1 = 0, t2 = 1;
while (1) {
scanf("%d%d%d", &n, &m, &s);
if (n == 0)
break;
t1 = 0;
t2 = 1;
n *= n;
rep(i, 50) rep(j, 2) rep(k, s + 1) dp[i][j][k] = 0;
dp[0][0][0] = 1;
reps(j, m) {
dp[0][t2][0] = 1;
reps(i, n) {
nn = i;
st = (nn * (nn + 1)) / 2;
int mm, pp, end;
mm = (j - 1) / 2;
if (j % 2 == 0)
pp = j / 2;
else
pp = 0;
end = mm * (mm + 1) + pp + st;
for (k = st - 1; k <= end; k++) {
dp[i][t2][k] = 0;
if (k - j >= 0) {
dp[i][t2][k] += dp[i - 1][t1][k - j];
}
dp[i][t2][k] += dp[i][t1][k];
if (dp[i][t2][k] < 0)
puts("AAAAAAA");
dp[i][t2][k] %= 100000;
}
/*
if(i<9&&j<12){
printf("%d %d\n",i,j);
for(int k=0;k<50;k++){
printf("%d ",dp[i][t2][k]);
}puts("");
}*/
}
swap(t1, t2);
}
printf("%d\n", dp[n][t1][s]);
}
} | #include <algorithm>
#include <cstdio>
using namespace std;
#define rep(i, n) for (i = 0; i < n; i++)
#define reps(i, n) for (i = 1; i <= n; i++)
int dp[50][2][3001];
int main() {
int n, m, s, nn, st, i, j, k;
int t1 = 0, t2 = 1;
while (1) {
scanf("%d%d%d", &n, &m, &s);
if (n == 0)
break;
t1 = 0;
t2 = 1;
n *= n;
rep(i, 50) rep(j, 2) rep(k, s + 1) dp[i][j][k] = 0;
dp[0][0][0] = 1;
reps(j, m) {
dp[0][t2][0] = 1;
reps(i, n) {
nn = i;
st = (nn * (nn + 1)) / 2;
int mm, pp, end;
mm = (j - 1) / 2;
if (j % 2 == 0)
pp = j / 2;
else
pp = 0;
end = min(mm * (mm + 1) + pp + st, s + 1);
for (k = st - 1; k <= end; k++) {
dp[i][t2][k] = 0;
if (k - j >= 0) {
dp[i][t2][k] += dp[i - 1][t1][k - j];
}
dp[i][t2][k] += dp[i][t1][k];
if (dp[i][t2][k] < 0)
puts("AAAAAAA");
dp[i][t2][k] %= 100000;
}
/*
if(i<9&&j<12){
printf("%d %d\n",i,j);
for(int k=0;k<50;k++){
printf("%d ",dp[i][t2][k]);
}puts("");
}*/
}
swap(t1, t2);
}
printf("%d\n", dp[n][t1][s]);
}
} | replace | 38 | 39 | 38 | 39 | 0 | |
p00460 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mod 100000
typedef pair<int, int> P;
int n, m, s, ans;
int dp[2][3010][4010];
int main() {
while (cin >> n >> m >> s && n) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i = 0; i < n * n; ++i) {
for (int j = 0; j < m; ++j)
for (int k = j; k < s; ++k)
dp[1][j + 1][k + 1] = (dp[1][j][k] + dp[0][j][k - j]) % mod;
for (int j = 0; j <= m; ++j) {
for (int k = 0; k <= s; ++k) {
dp[0][j][k] = dp[1][j][k];
dp[1][j][k] = 0;
}
}
}
ans = 0;
for (int i = 0; i <= m; ++i) {
ans += dp[0][i][s];
ans %= mod;
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mod 100000
typedef pair<int, int> P;
int n, m, s, ans;
int dp[2][2001][3001];
int main() {
while (cin >> n >> m >> s && n) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i = 0; i < n * n; ++i) {
for (int j = 0; j < m; ++j)
for (int k = j; k < s; ++k)
dp[1][j + 1][k + 1] = (dp[1][j][k] + dp[0][j][k - j]) % mod;
for (int j = 0; j <= m; ++j) {
for (int k = 0; k <= s; ++k) {
dp[0][j][k] = dp[1][j][k];
dp[1][j][k] = 0;
}
}
}
ans = 0;
for (int i = 0; i <= m; ++i) {
ans += dp[0][i][s];
ans %= mod;
}
cout << ans << endl;
}
} | replace | 7 | 8 | 7 | 8 | MLE | |
p00460 | C++ | Time Limit Exceeded | #include <cstdio>
#include <string.h>
using namespace std;
int n, m, s, ans;
int dp[2][2001][3001];
int main() {
while (scanf("%d%d%d", &n, &m, &s) && n && m && s) {
for (int i = 1; i <= m; i++)
dp[0][i][i] = 1;
for (int i = 2; i <= n * n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k < j; k++) {
for (int l = 1; l <= s; l++) {
if (j + l > s) {
break;
} else {
dp[1][j][j + l] += dp[0][k][l];
dp[1][j][j + l] %= 100000;
}
}
}
}
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= s; k++) {
dp[0][j][k] = dp[1][j][k];
dp[1][j][k] = 0;
}
}
}
for (int i = 1; i <= m; i++) {
ans += dp[0][i][s];
ans %= 100000;
}
printf("%d\n", ans);
memset(dp, 0, sizeof dp);
ans = 0;
}
return 0;
} | #include <cstdio>
#include <string.h>
using namespace std;
int n, m, s, ans;
int dp[2][2001][3001];
int main() {
while (scanf("%d%d%d", &n, &m, &s) && n && m && s) {
for (int i = 1; i <= m; i++)
dp[0][i][i] = 1;
for (int i = 2; i <= n * n; i++) {
for (int j = 1; j < m; j++) {
for (int k = j; k < s; k++) {
dp[1][j + 1][k + 1] += dp[1][j][k];
dp[1][j + 1][k + 1] += dp[0][j][k - j];
dp[1][j + 1][k + 1] %= 100000;
}
}
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= s; k++) {
dp[0][j][k] = dp[1][j][k];
dp[1][j][k] = 0;
}
}
}
for (int i = 1; i <= m; i++) {
ans += dp[0][i][s];
ans %= 100000;
}
printf("%d\n", ans);
memset(dp, 0, sizeof dp);
ans = 0;
}
return 0;
} | replace | 13 | 23 | 13 | 18 | TLE | |
p00460 | C++ | Runtime Error | #include <cstring>
#include <iostream>
using namespace std;
const int mod = 100000;
int main() {
int n, m, s;
int dp[50][2001];
while (cin >> n >> m >> s, n) {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = n * n - 1; j >= 0; --j) {
for (int k = 0; k <= s - i; ++k) {
dp[j + 1][k + i] += dp[j][k];
dp[j + 1][k + i] %= mod;
}
}
}
cout << dp[n * n][s] << endl;
}
return 0;
} | #include <cstring>
#include <iostream>
using namespace std;
const int mod = 100000;
int main() {
int n, m, s;
int dp[50][3001];
while (cin >> n >> m >> s, n) {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = n * n - 1; j >= 0; --j) {
for (int k = 0; k <= s - i; ++k) {
dp[j + 1][k + i] += dp[j][k];
dp[j + 1][k + i] %= mod;
}
}
}
cout << dp[n * n][s] << endl;
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p00460 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
int dp[50][3001];
int main() {
int n, m, s;
while (scanf("%d %d %d", &n, &m, &s), n) {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= m; i++)
for (int j = n * n; j >= 1; j--)
for (int k = 1; k <= s; k++) {
dp[j][k] += dp[j - 1][k - i];
if (dp[j][k] >= 100000)
dp[j][k] -= 100000;
}
printf("%d\n", dp[n * n][s]);
}
} | #include <cstdio>
#include <cstring>
int dp[50][3001];
int main() {
int n, m, s;
while (scanf("%d %d %d", &n, &m, &s), n) {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= m; i++)
for (int j = n * n; j >= 1; j--)
for (int k = i; k <= s; k++) {
dp[j][k] += dp[j - 1][k - i];
if (dp[j][k] >= 100000)
dp[j][k] -= 100000;
}
printf("%d\n", dp[n * n][s]);
}
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00460 | C++ | Runtime Error |
// y09-6 ビンゴ(2回目)
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define LL long long
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while (true)
int main() {
ROOP() {
int N, M, S;
cin >> N >> M >> S;
if (N == 0)
return 0;
int dp[50][3001];
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 3001; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = N * N; j >= 1; j--) {
for (int k = i; k <= S; k++) {
dp[j][k] += dp[j - 1][k - i];
dp[i][k] %= 100000;
}
}
}
cout << dp[N * N][S] << endl;
}
return 0;
}
|
// y09-6 ビンゴ(2回目)
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define LL long long
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while (true)
int main() {
ROOP() {
int N, M, S;
cin >> N >> M >> S;
if (N == 0)
return 0;
int dp[50][3001];
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 3001; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = N * N; j >= 1; j--) {
for (int k = i; k <= S; k++) {
dp[j][k] += dp[j - 1][k - i];
dp[j][k] %= 100000;
}
}
}
cout << dp[N * N][S] << endl;
}
return 0;
}
| replace | 48 | 49 | 48 | 49 | 0 | |
p00460 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
double ans = 0;
rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
return (int)ans;
}
inline string numstr(int m) {
str s = "";
while (m > 0) {
char c;
int a = m / 10;
if (a > 0)
a = m % (a * 10);
else
a = m;
c = (char)('0' + a);
s += c;
m /= 10;
}
str st = "";
for (int i = s.size() - 1; i >= 0; i--) {
st += s[i];
}
return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int dp[50][3003];
///?£????????£???????????§???????????????????£??????§???????????£???(???????°????????????¨???????¶?...)(?????????????????§???.)
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, s;
int mod = 100000;
while (1) {
cin >> n >> m >> s;
if (n == 0 && m == 0 && s == 0)
break;
rep(i, 0, 50) {
rep(j, 0, 3003) { dp[i][j] = 0; }
}
dp[0][0] = 1;
rep(i, 1, m + 1) {
for (int j = n * n; j >= 0; j--) {
rep(k, i, s + 1) { (dp[j][k] += dp[j - 1][k - i]) %= mod; }
}
}
cout << dp[n * n][s] % mod << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
double ans = 0;
rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
return (int)ans;
}
inline string numstr(int m) {
str s = "";
while (m > 0) {
char c;
int a = m / 10;
if (a > 0)
a = m % (a * 10);
else
a = m;
c = (char)('0' + a);
s += c;
m /= 10;
}
str st = "";
for (int i = s.size() - 1; i >= 0; i--) {
st += s[i];
}
return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int dp[50][3003];
///?£????????£???????????§???????????????????£??????§???????????£???(???????°????????????¨???????¶?...)(?????????????????§???.)
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m, s;
int mod = 100000;
while (1) {
cin >> n >> m >> s;
if (n == 0 && m == 0 && s == 0)
break;
rep(i, 0, 50) {
rep(j, 0, 3003) { dp[i][j] = 0; }
}
dp[0][0] = 1;
rep(i, 1, m + 1) {
for (int j = n * n; j > 0; j--) {
rep(k, i, s + 1) { (dp[j][k] += dp[j - 1][k - i]) %= mod; }
}
}
cout << dp[n * n][s] % mod << endl;
}
return 0;
} | replace | 61 | 62 | 61 | 62 | -11 | |
p00460 | C++ | Runtime Error | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
int dp[50][3001];
const int MOD = 100000;
int main() {
while (true) {
int n, m, s;
cin >> n >> m >> s;
if (n == 0)
break;
n = n * n;
rep(i, 50) rep(j, 3001) dp[i][j] = 0;
dp[0][0] = 1;
rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = j; k <= s;
++k) {
dp[j][k] += dp[j - 1][k - i];
dp[j][k] %= MOD;
}
cout << dp[n][s] << endl;
}
} | #include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
int dp[50][3001];
const int MOD = 100000;
int main() {
while (true) {
int n, m, s;
cin >> n >> m >> s;
if (n == 0)
break;
n = n * n;
rep(i, 50) rep(j, 3001) dp[i][j] = 0;
dp[0][0] = 1;
rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = i; k <= s;
++k) {
dp[j][k] += dp[j - 1][k - i];
dp[j][k] %= MOD;
}
cout << dp[n][s] << endl;
}
} | replace | 18 | 19 | 18 | 19 | 0 | |
p00460 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int mod = 100000;
int n, m, s;
int dp[50][2010][3100];
int dfs(int i = 0, int pre = 0, int sum = s) {
if (sum < 0)
return 0;
if (pre > m)
return 0;
if (i == n * n) {
return sum == 0;
}
int &ret = dp[i][pre][sum];
if (ret != -1)
return ret;
ret = 0;
for (int j = pre + 1; j <= m; j++) {
ret += dfs(i + 1, j, sum - j);
ret %= mod;
}
return ret;
}
signed main() {
while (cin >> n >> m >> s, n) {
memset(dp, -1, sizeof(dp));
cout << dfs() << endl;
}
} | #include "bits/stdc++.h"
main() {
for (int n, m, s, i, j, d[50][3500], M = 1e5;
std::cin >> n >> m >> s, n *= n;) {
std::fill(d[0], d[50], M);
d[0][0] = 1;
for (i = 1; i <= n; ++i)
for (j = 0; j <= s; ++j) {
if (j >= i)
d[i][j] += d[i - 1][j - i] + d[i][j - i];
if (j > m)
d[i][j] -= d[i - 1][j - m - 1];
d[i][j] %= M;
}
printf("%d\n", d[n][s]);
}
} | replace | 0 | 35 | 0 | 15 | MLE | |
p00461 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// common
#define BR "\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;
// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) \
DUMP(x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define CHECK(exp, act) \
if (exp != act) { \
DUMP(exp); \
DEBUG(act); \
}
#define STOP(e) \
CHECK(e, true); \
if (!(e)) \
exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif
template <class T> inline string toString(const vector<T> &x) {
stringstream ss;
REP(i, x.size()) {
if (i != 0)
ss << " ";
ss << x[i];
}
return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
stringstream ss;
REP(i, map.size()) {
if (i != 0)
ss << BR;
ss << toString(map[i]);
}
return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
string res;
stringstream ss;
for (auto &p : x)
ss << p.first << ":" << p.second << " ";
return ss.str();
}
template <typename T, typename V> inline T mod(T v, V MOD) {
return (v % MOD + MOD) % MOD;
}
namespace SegmentTreeSpace {
const int INF = 1 << 28;
// RMQ
struct SegmentTree {
vector<int> dat; // dat[i] .. i,i+1のLCP
int n, size;
SegmentTree(int _n) {
n = pow2Fit(_n);
size = 2 * n - 1;
dat = vector<int>(size);
fill(ALL(dat), INF);
}
SegmentTree() { SegmentTree(0); }
int pow2Fit(int n) {
int v = 1;
while (v < n)
v <<= 1;
return v;
}
void update(int v, int a) {
v += n - 1;
dat[v] = a;
while (v > 0) {
int parent = v = (v - 1) / 2;
int chl = parent * 2 + 1, chr = parent * 2 + 2;
dat[parent] = min(dat[chl], dat[chr]);
}
}
// 0<=a<=b<n
int query(int a, int b) { return query(0, a, b, 0, n); }
private:
//[a,...,b] =min(dat[a],...,dat[b-1])
int query(int v, int a, int b, int l, int r) {
if (r <= a || b <= l)
return INF; // out range
if (a <= l && r <= b)
return dat[v];
int vl = query(v * 2 + 1, a, b, l, (l + r) / 2);
int vr = query(v * 2 + 2, a, b, (l + r) / 2, r);
return min(vl, vr);
}
};
} // namespace SegmentTreeSpace
/*
Suffix array O(n lg^2 n)
LCP table O(n)
*/
namespace SuffixArraySpace {
using namespace SegmentTreeSpace;
class SuffixArray {
public:
// sa 辞書順i番目の接尾辞の開始位置 [s...)
// pos 文字列[i...)の辞書順位置 s
/// LCP …Longest Common Prefix
vector<int> sa, pos;
SegmentTree lcp;
SuffixArray(string S) : S(S) {
lcp = SegmentTree(S.size());
N = S.size();
sa = vector<int>(N);
pos = vector<int>(N);
rank = vector<int>(N);
buildSA();
buildLCP();
};
int N, k;
private:
string S;
vector<int> rank;
// class SufCmp{
// public:
// const SuffixArray& suf;
// SufCmp(const SuffixArray& suf):suf(suf){
// }
// bool operator()(const int i,const int j) const{
// if (suf.pos[i] != suf.pos[j])return suf.pos[i] <
// suf.pos[j]; int i2=i+suf.k,j2=j+suf.k; return (i2 < suf.N && j2< suf.N) ?
// suf.pos[i2] < suf.pos[j2] : i2 > j2;
// }
// };
// O(nlog^2(n))
void buildSA() {
auto sufCmp = [&, this](int i, int j) {
if (pos[i] != pos[j])
return pos[i] < pos[j];
i += k;
j += k;
return (i < N && j < N) ? pos[i] < pos[j] : i > j;
};
REP(i, N) sa[i] = i, pos[i] = S[i];
for (k = 1;; k *= 2) {
sort(ALL(sa), sufCmp);
REP(i, N - 1) rank[i + 1] = rank[i] + sufCmp(sa[i], sa[i + 1]);
REP(i, N) pos[sa[i]] = rank[i];
if (rank[N - 1] == N - 1)
break;
}
}
// O(n)
void buildLCP() {
for (int i = 0, k = 0; i < N; ++i)
if (pos[i] != N - 1) {
for (int j = sa[pos[i] + 1]; S[i + k] == S[j + k];)
++k;
lcp.update(pos[i], k);
if (k)
--k;
}
}
public:
// libs
// LCP[a,...b]を求める. Zを含む
// O(log(n))
int LCP(int a, int b) {
if (a == b)
return N - sa[a];
return lcp.query(a, b);
}
// k-ミスマッチマッチング
// O(k*n)
static int mis_match_contains(const string &S, const string &T, int k) {
string st = S + "#" + T;
SuffixArray suf(st);
int res = 0;
for (int i = 0; i + (int)T.size() <= (int)S.size(); i++) {
DUMP(i);
int si = i, ti = S.size() + 1;
DUMP(si);
DUMP(ti);
int match = 0;
for (int j = 0; j <= k; j++) {
if (!(si < (int)S.size() && ti < (int)st.size()))
break;
int l = suf.pos[si], r = suf.pos[ti];
if (l > r)
swap(l, r);
int lcp = suf.LCP(l, r);
match += lcp;
si += lcp + 1;
ti += lcp + 1;
}
DUMP(match);
if (match == (int)T.size() - k)
res++;
DUMP(si);
DUMP(ti);
DUMP(res);
}
return res;
}
//: TODO 最長回文
};
} // namespace SuffixArraySpace
using namespace SuffixArraySpace;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
int m;
cin >> m;
string s;
cin >> s;
string ioi = "I";
REP(i, n) ioi += "OI";
DUMP(ioi.size());
SuffixArray suf(ioi + "#" + s);
int res = 0;
REP(i, suf.N) {}
for (int i = ioi.size() + 1; i < suf.N; i++) {
if (suf.LCP(suf.pos[0], suf.pos[i]) == ioi.size())
res++;
}
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// common
#define BR "\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;
// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;
// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) \
DUMP(x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl
#define CHECK(exp, act) \
if (exp != act) { \
DUMP(exp); \
DEBUG(act); \
}
#define STOP(e) \
CHECK(e, true); \
if (!(e)) \
exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif
template <class T> inline string toString(const vector<T> &x) {
stringstream ss;
REP(i, x.size()) {
if (i != 0)
ss << " ";
ss << x[i];
}
return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
stringstream ss;
REP(i, map.size()) {
if (i != 0)
ss << BR;
ss << toString(map[i]);
}
return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
string res;
stringstream ss;
for (auto &p : x)
ss << p.first << ":" << p.second << " ";
return ss.str();
}
template <typename T, typename V> inline T mod(T v, V MOD) {
return (v % MOD + MOD) % MOD;
}
namespace SegmentTreeSpace {
const int INF = 1 << 28;
// RMQ
struct SegmentTree {
vector<int> dat; // dat[i] .. i,i+1のLCP
int n, size;
SegmentTree(int _n) {
n = pow2Fit(_n);
size = 2 * n - 1;
dat = vector<int>(size);
fill(ALL(dat), INF);
}
SegmentTree() { SegmentTree(0); }
int pow2Fit(int n) {
int v = 1;
while (v < n)
v <<= 1;
return v;
}
void update(int v, int a) {
v += n - 1;
dat[v] = a;
while (v > 0) {
int parent = v = (v - 1) / 2;
int chl = parent * 2 + 1, chr = parent * 2 + 2;
dat[parent] = min(dat[chl], dat[chr]);
}
}
// 0<=a<=b<n
int query(int a, int b) { return query(0, a, b, 0, n); }
private:
//[a,...,b] =min(dat[a],...,dat[b-1])
int query(int v, int a, int b, int l, int r) {
if (r <= a || b <= l)
return INF; // out range
if (a <= l && r <= b)
return dat[v];
int vl = query(v * 2 + 1, a, b, l, (l + r) / 2);
int vr = query(v * 2 + 2, a, b, (l + r) / 2, r);
return min(vl, vr);
}
};
} // namespace SegmentTreeSpace
/*
Suffix array O(n lg^2 n)
LCP table O(n)
*/
namespace SuffixArraySpace {
using namespace SegmentTreeSpace;
class SuffixArray {
public:
// sa 辞書順i番目の接尾辞の開始位置 [s...)
// pos 文字列[i...)の辞書順位置 s
/// LCP …Longest Common Prefix
vector<int> sa, pos;
SegmentTree lcp;
SuffixArray(string S) : S(S) {
lcp = SegmentTree(S.size());
N = S.size();
sa = vector<int>(N);
pos = vector<int>(N);
rank = vector<int>(N);
buildSA();
buildLCP();
};
int N, k;
private:
string S;
vector<int> rank;
// class SufCmp{
// public:
// const SuffixArray& suf;
// SufCmp(const SuffixArray& suf):suf(suf){
// }
// bool operator()(const int i,const int j) const{
// if (suf.pos[i] != suf.pos[j])return suf.pos[i] <
// suf.pos[j]; int i2=i+suf.k,j2=j+suf.k; return (i2 < suf.N && j2< suf.N) ?
// suf.pos[i2] < suf.pos[j2] : i2 > j2;
// }
// };
// O(nlog^2(n))
void buildSA() {
auto sufCmp = [&, this](int i, int j) {
if (pos[i] != pos[j])
return pos[i] < pos[j];
i += k;
j += k;
return (i < N && j < N) ? pos[i] < pos[j] : i > j;
};
REP(i, N) sa[i] = i, pos[i] = S[i];
for (k = 1;; k *= 2) {
sort(ALL(sa), sufCmp);
REP(i, N - 1) rank[i + 1] = rank[i] + sufCmp(sa[i], sa[i + 1]);
REP(i, N) pos[sa[i]] = rank[i];
if (rank[N - 1] == N - 1)
break;
}
}
// O(n)
void buildLCP() {
for (int i = 0, k = 0; i < N; ++i)
if (pos[i] != N - 1) {
for (int j = sa[pos[i] + 1]; S[i + k] == S[j + k];)
++k;
lcp.update(pos[i], k);
if (k)
--k;
}
}
public:
// libs
// LCP[a,...b]を求める. Zを含む
// O(log(n))
int LCP(int a, int b) {
if (a == b)
return N - sa[a];
return lcp.query(a, b);
}
// k-ミスマッチマッチング
// O(k*n)
static int mis_match_contains(const string &S, const string &T, int k) {
string st = S + "#" + T;
SuffixArray suf(st);
int res = 0;
for (int i = 0; i + (int)T.size() <= (int)S.size(); i++) {
DUMP(i);
int si = i, ti = S.size() + 1;
DUMP(si);
DUMP(ti);
int match = 0;
for (int j = 0; j <= k; j++) {
if (!(si < (int)S.size() && ti < (int)st.size()))
break;
int l = suf.pos[si], r = suf.pos[ti];
if (l > r)
swap(l, r);
int lcp = suf.LCP(l, r);
match += lcp;
si += lcp + 1;
ti += lcp + 1;
}
DUMP(match);
if (match == (int)T.size() - k)
res++;
DUMP(si);
DUMP(ti);
DUMP(res);
}
return res;
}
//: TODO 最長回文
};
} // namespace SuffixArraySpace
using namespace SuffixArraySpace;
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
int m;
cin >> m;
string s;
cin >> s;
string ioi = "I";
REP(i, n) ioi += "OI";
// DUMP(ioi.size());
SuffixArray suf(ioi + "#" + s);
int res = 0;
REP(i, suf.N) {}
for (int i = ioi.size() + 1; i < suf.N; i++) {
if (suf.LCP(suf.pos[0], suf.pos[i]) == ioi.size())
res++;
}
cout << res << endl;
}
return 0;
} | replace | 277 | 278 | 277 | 278 | 0 | ioi.size() = 3
ioi.size() = 5
|
p00461 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int n, m;
string s;
int main() {
while (cin >> n) {
if (!n)
return 0;
cin >> m >> s;
int a = 0;
string t = "I";
for (int i = 0; i < n; i++)
t += "OI";
while (s.find(t) != string::npos) {
s = s.substr(s.find(t) + 1);
a++;
}
cout << a << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int n, m;
string s;
int main() {
while (cin >> n) {
if (!n)
return 0;
cin >> m >> s;
int a = 0, b = 0;
for (int i = 0; i < m; i++) {
if (s[i] == 'I' && b % 2 == 0 || s[i] == 'O' && b % 2 == 1)
b++;
else {
if (s[i] == 'I')
b = 1;
else
b = 0;
}
if (b % 2 == 1 && b >= 2 * n + 1)
a++;
}
cout << a << endl;
}
} | replace | 20 | 27 | 20 | 32 | TLE | |
p00461 | C++ | Time Limit Exceeded | #include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
// cut here before submit
// freopen ("testcase.IOIOI", "r", stdin );
int n;
while (scanf("%d", &n), n) {
int m;
scanf("%d", &m);
string s = "";
cin >> s;
string search = "";
rep(i, n) { search += "IO"; } // end rep
search += 'I';
// cerr << search << endl;
int res = 0;
rep(i, m) {
if (s[i] == 'O')
continue;
if (s[i] == 'I' && i + 1 < m && s[i + 1] != 'O')
continue;
string sub = s.substr(i);
if (sub.find(search) != string::npos) {
res++;
} // end if
} // end rep
printf("%d\n", res);
} // en drep
return 0;
} | #include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
// cut here before submit
// freopen ("testcase.IOIOI", "r", stdin );
int n;
while (scanf("%d", &n), n) {
int m;
scanf("%d", &m);
string s = "";
cin >> s;
string search = "";
rep(i, n) { search += "IO"; } // end rep
search += 'I';
// cerr << search << endl;
int res = 0;
rep(i, m) {
if (s[i] == 'O')
continue;
if (s[i] == 'I' && i + 1 < m && s[i + 1] != 'O')
continue;
string sub = s.substr(i, 2 * n + 1);
if (sub == search) {
res++;
} // end if
} // end rep
printf("%d\n", res);
} // en drep
return 0;
} | replace | 50 | 52 | 50 | 52 | TLE | |
p00461 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n, l;
while (cin >> n >> l, n || l) {
string str;
cin >> str;
int mode = 0;
int chain = 0;
int ans = 0;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == 'I') {
switch (mode) {
case 0:
mode = 1;
break;
case 1:
chain = 0;
break;
case 2:
if (++chain >= n) {
++ans;
--chain;
}
mode = 1;
break;
}
} else {
switch (mode) {
case 0:
break;
case 1:
mode = 2;
break;
case 2:
mode = 0;
chain = 0;
break;
}
}
}
cout << ans << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int n, l;
while (cin >> n, n) {
cin >> l;
string str;
cin >> str;
int mode = 0;
int chain = 0;
int ans = 0;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == 'I') {
switch (mode) {
case 0:
mode = 1;
break;
case 1:
chain = 0;
break;
case 2:
if (++chain >= n) {
++ans;
--chain;
}
mode = 1;
break;
}
} else {
switch (mode) {
case 0:
break;
case 1:
mode = 2;
break;
case 2:
mode = 0;
chain = 0;
break;
}
}
}
cout << ans << endl;
}
} | replace | 7 | 8 | 7 | 9 | TLE | |
p00461 | 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;
ll n, m;
string s;
ll dp[100001];
ll ans = 0;
int main() {
while (1) {
memset(dp, 0, sizeof(dp));
cin >> n;
if (!n)
break;
cin >> m >> s;
ans = 0;
for (ll i = 0; i < m; i++) {
if (i > 0 && s[i - 1] != s[i]) {
dp[i] = dp[i - 1] + 1;
} else if (s[i] == 'I')
dp[i] = 1;
if (s[i] == 'I' && dp[i] > 2 * n) {
ans++;
}
}
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;
ll n, m;
string s;
ll dp[1000001];
ll ans = 0;
int main() {
while (1) {
memset(dp, 0, sizeof(dp));
cin >> n;
if (!n)
break;
cin >> m >> s;
ans = 0;
for (ll i = 0; i < m; i++) {
if (i > 0 && s[i - 1] != s[i]) {
dp[i] = dp[i - 1] + 1;
} else if (s[i] == 'I')
dp[i] = 1;
if (s[i] == 'I' && dp[i] > 2 * n) {
ans++;
}
}
cout << ans << endl;
}
} | replace | 16 | 17 | 16 | 17 | 0 | |
p00461 | C++ | Runtime Error | #include "stdio.h"
char s[1111111];
int w[1111111];
int h[1000000];
int main() {
int noww = 0;
while (1) {
int n;
int m;
{
scanf("%d", &n);
if (n == 0) {
break;
}
scanf("%d", &m);
scanf("%s", s + 1);
}
{
s[0] = s[1];
w[0] = 0;
for (int i = 1; i <= m; i++) {
if (s[i - 1] != s[i]) {
w[i] = w[i - 1] + 1;
} else {
w[i] = 1;
}
}
}
{
h[noww] = 0;
for (int i = 1; i <= m; i++) {
if (w[i] > n * 2 && s[i] == 'I') {
h[noww]++;
}
}
noww++;
}
}
for (int i = 0; noww; i++) {
printf("%d\n", h[i]);
}
return 0;
} | #include "stdio.h"
char s[1111111];
int w[1111111];
int h[1000000];
int main() {
int noww = 0;
while (1) {
int n;
int m;
{
scanf("%d", &n);
if (n == 0) {
break;
}
scanf("%d", &m);
scanf("%s", s + 1);
}
{
s[0] = s[1];
w[0] = 0;
for (int i = 1; i <= m; i++) {
if (s[i - 1] != s[i]) {
w[i] = w[i - 1] + 1;
} else {
w[i] = 1;
}
}
}
{
h[noww] = 0;
for (int i = 1; i <= m; i++) {
if (w[i] > n * 2 && s[i] == 'I') {
h[noww]++;
}
}
noww++;
}
}
for (int i = 0; i < noww; i++) {
printf("%d\n", h[i]);
}
return 0;
} | replace | 44 | 45 | 44 | 45 | -11 | |
p00461 | C++ | Runtime Error | #include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
int m;
string s;
while (cin >> n, n != 0) {
cin >> m >> s;
int p[100] = {0};
int f = 1;
for (int i = 0; i < m; i++) {
if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
for (int j = 1; j <= f; j++)
p[j]++;
f++;
i++;
} else {
f = 1;
}
}
cout << p[n] << endl;
}
} | #include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
int m;
string s;
while (cin >> n, n != 0) {
cin >> m >> s;
int p[1000001] = {0};
int f = 1;
for (int i = 0; i < m; i++) {
if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
for (int j = 1; j <= f; j++)
p[j]++;
f++;
i++;
} else {
f = 1;
}
}
cout << p[n] << endl;
}
} | replace | 17 | 18 | 17 | 18 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.