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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00222 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#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 MAX 10000100
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
bool sosu[MAX];
int main() {
bool sosu[MAX];
memset(sosu, true, MAX);
for (int i = 4; i < MAX; i += 2)
sosu[i] = false;
for (int i = 3; i < MAX; i += 2)
for (int j = 3 * i; j < MAX; j += 2 * i)
sosu[j] = false;
int n;
while (cin >> n, n) {
for (int i = n; i >= 0; i--) {
if (sosu[i] && sosu[i - 2] && sosu[i - 6] & sosu[i - 8]) {
cout << i << endl;
break;
}
}
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#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 MAX 10000100
const double PI = acos(-1);
const double EPS = 1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
bool sosu[MAX];
int main() {
memset(sosu, true, MAX);
for (int i = 4; i < MAX; i += 2)
sosu[i] = false;
for (int i = 3; i < MAX; i += 2)
for (int j = 3 * i; j < MAX; j += 2 * i)
sosu[j] = false;
int n;
while (cin >> n, n) {
for (int i = n; i >= 0; i--) {
if (sosu[i] && sosu[i - 2] && sosu[i - 6] & sosu[i - 8]) {
cout << i << endl;
break;
}
}
}
} | delete | 24 | 25 | 24 | 24 | -11 | |
p00223 | C++ | Runtime Error | #include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct AO {
int tx;
int ty;
int kx;
int ky;
int cnt;
};
queue<AO> Q;
AO TK, f;
int d[51][51];
int sa[51][51][51][51];
int main() {
int X, Y, F; // tx ty kx ky
int mv[4][4] = {{0, -1, 0, 1}, //???
{0, 1, 0, -1}, //???
{-1, 0, 1, 0}, //???
{1, 0, -1, 0}}; //???
while (1) {
F = 1;
for (int i = 0; i < 52; i++) {
for (int j = 0; j < 52; j++) {
d[i][j] = 1;
}
}
memset(sa, 0, sizeof(sa));
// printf("%d\n",d[0][0]);
scanf("%d %d", &X, &Y);
if (X == 0 && Y == 0)
break;
scanf("%d %d %d %d", &TK.tx, &TK.ty, &TK.kx, &TK.ky);
sa[TK.ty][TK.tx][TK.ky][TK.kx] = 1;
TK.cnt = 0;
Q.push(TK);
for (int i = 1; i <= Y; i++) {
for (int j = 1; j <= X; j++) {
scanf("%d", &d[i][j]);
}
}
while (!Q.empty()) { //! Q.empty()
TK = Q.front();
Q.pop();
// printf("?????? ??????:%d %d ??????:%d %d\n",TK.tx,TK.ty,TK.kx,TK.ky);
if (TK.cnt >= 100) {
break;
}
if (TK.tx == TK.kx && TK.ty == TK.ky) {
F = 0;
break;
}
if (TK.cnt >= 100) {
break;
}
for (int i = 0; i < 4; i++) {
f.tx = TK.tx + mv[i][0];
f.ty = TK.ty + mv[i][1];
f.kx = TK.kx + mv[i][2];
f.ky = TK.ky + mv[i][3];
f.cnt = TK.cnt + 1;
// printf("A");
if (d[f.ty][f.tx] == 0 || d[f.ky][f.kx] == 0) {
if (d[f.ty][f.tx] == 1) {
f.tx = TK.tx;
f.ty = TK.ty;
}
if (d[f.ky][f.kx] == 1) {
f.kx = TK.kx;
f.ky = TK.ky;
}
} else if (d[f.ty][f.tx] == 1 && d[f.ky][f.kx] == 1) {
continue;
}
if (sa[f.ty][f.tx][f.ky][f.kx] == 1 || f.tx < 0 || f.kx < 0 ||
f.ty < 0 || f.ky < 0 || f.tx > X || f.kx > X || f.ty > Y ||
f.ky > Y) {
continue;
}
sa[f.ty][f.tx][f.ky][f.kx] = 1;
Q.push(f);
// printf("??????%d %d\n?????? %d %d\n",f.tx,f.ty,f.kx,f.ky);
}
// printf("------------\n");
}
if (F == 0)
printf("%d\n", TK.cnt);
// printf("%d %d %d %d\n",TK.tx,TK.ty,TK.kx,TK.ky);
else
printf("NA\n");
while (!Q.empty())
Q.pop();
}
return 0;
} | #include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct AO {
int tx;
int ty;
int kx;
int ky;
int cnt;
};
queue<AO> Q;
AO TK, f;
int d[52][52];
int sa[51][51][51][51];
int main() {
int X, Y, F; // tx ty kx ky
int mv[4][4] = {{0, -1, 0, 1}, //???
{0, 1, 0, -1}, //???
{-1, 0, 1, 0}, //???
{1, 0, -1, 0}}; //???
while (1) {
F = 1;
for (int i = 0; i < 52; i++) {
for (int j = 0; j < 52; j++) {
d[i][j] = 1;
}
}
memset(sa, 0, sizeof(sa));
// printf("%d\n",d[0][0]);
scanf("%d %d", &X, &Y);
if (X == 0 && Y == 0)
break;
scanf("%d %d %d %d", &TK.tx, &TK.ty, &TK.kx, &TK.ky);
sa[TK.ty][TK.tx][TK.ky][TK.kx] = 1;
TK.cnt = 0;
Q.push(TK);
for (int i = 1; i <= Y; i++) {
for (int j = 1; j <= X; j++) {
scanf("%d", &d[i][j]);
}
}
while (!Q.empty()) { //! Q.empty()
TK = Q.front();
Q.pop();
// printf("?????? ??????:%d %d ??????:%d %d\n",TK.tx,TK.ty,TK.kx,TK.ky);
if (TK.cnt >= 100) {
break;
}
if (TK.tx == TK.kx && TK.ty == TK.ky) {
F = 0;
break;
}
if (TK.cnt >= 100) {
break;
}
for (int i = 0; i < 4; i++) {
f.tx = TK.tx + mv[i][0];
f.ty = TK.ty + mv[i][1];
f.kx = TK.kx + mv[i][2];
f.ky = TK.ky + mv[i][3];
f.cnt = TK.cnt + 1;
// printf("A");
if (d[f.ty][f.tx] == 0 || d[f.ky][f.kx] == 0) {
if (d[f.ty][f.tx] == 1) {
f.tx = TK.tx;
f.ty = TK.ty;
}
if (d[f.ky][f.kx] == 1) {
f.kx = TK.kx;
f.ky = TK.ky;
}
} else if (d[f.ty][f.tx] == 1 && d[f.ky][f.kx] == 1) {
continue;
}
if (sa[f.ty][f.tx][f.ky][f.kx] == 1 || f.tx < 0 || f.kx < 0 ||
f.ty < 0 || f.ky < 0 || f.tx > X || f.kx > X || f.ty > Y ||
f.ky > Y) {
continue;
}
sa[f.ty][f.tx][f.ky][f.kx] = 1;
Q.push(f);
// printf("??????%d %d\n?????? %d %d\n",f.tx,f.ty,f.kx,f.ky);
}
// printf("------------\n");
}
if (F == 0)
printf("%d\n", TK.cnt);
// printf("%d %d %d %d\n",TK.tx,TK.ty,TK.kx,TK.ky);
else
printf("NA\n");
while (!Q.empty())
Q.pop();
}
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p00223 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;
const int needleX[4] = {-1, 0, 1, 0};
const int needleY[4] = {0, -1, 0, 1};
pair<int, int> newPoint(pair<int, int> &nowPoint, int i,
vector<vector<bool>> &depart, bool takayuki) {
pair<int, int> result;
result.first = nowPoint.first + (needleX[i] * (takayuki ? 1 : -1));
result.second = nowPoint.second + (needleY[i] * (takayuki ? 1 : -1));
if (!depart[result.first][result.second]) {
result = nowPoint;
}
return result;
}
int main(void) {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
pair<int, int> takayuki, kazuyuki;
cin >> takayuki.first >> takayuki.second;
cin >> kazuyuki.first >> kazuyuki.second;
int input;
vector<vector<bool>> depart(h + 2, vector<bool>(w + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> input;
if (input == 0) {
depart[i][j] = true;
}
}
}
int ans = -1;
set<pair<pair<int, int>, pair<int, int>>> used;
queue<pair<int, pair<pair<int, int>, pair<int, int>>>> wait;
used.insert(make_pair(takayuki, kazuyuki));
wait.push(make_pair(0, make_pair(takayuki, kazuyuki)));
while (!wait.empty()) {
int cost = wait.front().first;
pair<int, int> t = wait.front().second.first;
pair<int, int> k = wait.front().second.second;
wait.pop();
if (t == k) {
ans = cost;
break;
}
for (int i = 0; i < 4; i++) {
pair<int, int> newt = newPoint(t, i, depart, true);
pair<int, int> newk = newPoint(k, i, depart, false);
if (used.count(make_pair(newt, newk)) == 0) {
used.insert(make_pair(newt, newk));
wait.push(make_pair(cost + 1, make_pair(newt, newk)));
}
}
}
if (ans == -1) {
cout << "NA" << endl;
} else {
cout << ans << endl;
}
}
return 0;
} | #include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
using namespace std;
const int needleX[4] = {-1, 0, 1, 0};
const int needleY[4] = {0, -1, 0, 1};
pair<int, int> newPoint(pair<int, int> &nowPoint, int i,
vector<vector<bool>> &depart, bool takayuki) {
pair<int, int> result;
result.first = nowPoint.first + (needleX[i] * (takayuki ? 1 : -1));
result.second = nowPoint.second + (needleY[i] * (takayuki ? 1 : -1));
if (!depart[result.first][result.second]) {
result = nowPoint;
}
return result;
}
int main(void) {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
pair<int, int> takayuki, kazuyuki;
cin >> takayuki.second >> takayuki.first;
cin >> kazuyuki.second >> kazuyuki.first;
int input;
vector<vector<bool>> depart(h + 2, vector<bool>(w + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> input;
if (input == 0) {
depart[i][j] = true;
}
}
}
int ans = -1;
set<pair<pair<int, int>, pair<int, int>>> used;
queue<pair<int, pair<pair<int, int>, pair<int, int>>>> wait;
used.insert(make_pair(takayuki, kazuyuki));
wait.push(make_pair(0, make_pair(takayuki, kazuyuki)));
while (!wait.empty()) {
int cost = wait.front().first;
pair<int, int> t = wait.front().second.first;
pair<int, int> k = wait.front().second.second;
wait.pop();
if (t == k) {
ans = cost;
break;
}
for (int i = 0; i < 4; i++) {
pair<int, int> newt = newPoint(t, i, depart, true);
pair<int, int> newk = newPoint(k, i, depart, false);
if (used.count(make_pair(newt, newk)) == 0) {
used.insert(make_pair(newt, newk));
wait.push(make_pair(cost + 1, make_pair(newt, newk)));
}
}
}
if (ans == -1) {
cout << "NA" << endl;
} else {
cout << ans << endl;
}
}
return 0;
} | replace | 29 | 31 | 29 | 31 | 0 | |
p00223 | C++ | Memory Limit Exceeded | #include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int H, W;
bool F[52][52]; // 障害物
bool D[52][52][52][52]; // 探索済みかの判定用
int inc_tx[4] = {1, -1, 0, 0};
int inc_ty[4] = {0, 0, 1, -1};
int inc_kx[4] = {-1, 1, 0, 0};
int inc_ky[4] = {0, 0, -1, 1};
class Point {
public:
int tx; // タカユキの座標
int ty;
int kx; // カズユキの座標
int ky;
int d; // ターン回数
void set(int a, int b, int c, int d) {
tx = a;
ty = b;
kx = c;
ky = d;
}
};
bool is_out_of_area(int y, int x) // 座標が範囲内か確認する関数
{
return x < 1 || x > W || y < 1 || y > H || F[y][x];
}
int main() {
while (1) {
Point p, q;
int tx, ty, kx, ky;
queue<Point> qa;
cin >> W >> H;
if (W == 0 && H == 0)
break;
cin >> tx >> ty >> kx >> ky;
memset(F, 1, sizeof(F));
for (int y = 1; y <= H; y++) {
for (int x = 1; x <= W; x++) {
cin >> F[y][x];
}
}
memset(D, 1, sizeof(D));
p.set(tx, ty, kx, ky);
D[ty][tx][ky][kx] = 0;
p.d = 0;
qa.push(p);
while (1) {
if (qa.empty()) {
cout << "NA" << endl;
break;
}
p = qa.front();
qa.pop();
if (p.d >= 100) {
cout << "NA" << endl;
break;
}
if (p.tx == p.kx && p.ty == p.ky) {
cout << p.d << endl;
break;
}
for (int i = 0; i < 4; i++) {
q.tx = p.tx + inc_tx[i];
q.ty = p.ty + inc_ty[i];
q.kx = p.kx + inc_kx[i];
q.ky = p.ky + inc_ky[i];
if (D[q.ty][q.tx][q.ky][q.kx]) {
if (is_out_of_area(q.ty, q.tx) == 0 ||
is_out_of_area(q.ky, q.kx) == 0) {
if (is_out_of_area(q.ty, q.tx)) {
q.ty = p.ty;
q.tx = p.tx;
}
if (is_out_of_area(q.ky, q.kx)) {
q.ky = p.ky;
q.kx = p.kx;
}
q.d = p.d + 1;
qa.push(q);
D[q.ty][q.tx][q.ky][q.kx] = 0;
}
}
}
}
}
return 0;
}
| #include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int H, W;
bool F[52][52]; // 障害物
bool D[52][52][52][52]; // 探索済みかの判定用
int inc_tx[4] = {1, -1, 0, 0};
int inc_ty[4] = {0, 0, 1, -1};
int inc_kx[4] = {-1, 1, 0, 0};
int inc_ky[4] = {0, 0, -1, 1};
class Point {
public:
int tx; // タカユキの座標
int ty;
int kx; // カズユキの座標
int ky;
int d; // ターン回数
void set(int a, int b, int c, int d) {
tx = a;
ty = b;
kx = c;
ky = d;
}
};
bool is_out_of_area(int y, int x) // 座標が範囲内か確認する関数
{
return x < 1 || x > W || y < 1 || y > H || F[y][x];
}
int main() {
while (1) {
Point p, q;
int tx, ty, kx, ky;
queue<Point> qa;
cin >> W >> H;
if (W == 0 && H == 0)
break;
cin >> tx >> ty >> kx >> ky;
memset(F, 1, sizeof(F));
for (int y = 1; y <= H; y++) {
for (int x = 1; x <= W; x++) {
cin >> F[y][x];
}
}
memset(D, 1, sizeof(D));
p.set(tx, ty, kx, ky);
D[ty][tx][ky][kx] = 0;
p.d = 0;
qa.push(p);
while (1) {
if (qa.empty()) {
cout << "NA" << endl;
break;
}
p = qa.front();
qa.pop();
if (p.d >= 100) {
cout << "NA" << endl;
break;
}
if (p.tx == p.kx && p.ty == p.ky) {
cout << p.d << endl;
break;
}
for (int i = 0; i < 4; i++) {
q.tx = p.tx + inc_tx[i];
q.ty = p.ty + inc_ty[i];
q.kx = p.kx + inc_kx[i];
q.ky = p.ky + inc_ky[i];
if (is_out_of_area(q.ty, q.tx) == 0 ||
is_out_of_area(q.ky, q.kx) == 0) {
if (is_out_of_area(q.ty, q.tx)) {
q.ty = p.ty;
q.tx = p.tx;
}
if (is_out_of_area(q.ky, q.kx)) {
q.ky = p.ky;
q.kx = p.kx;
}
if (D[q.ty][q.tx][q.ky][q.kx]) {
q.d = p.d + 1;
qa.push(q);
D[q.ty][q.tx][q.ky][q.kx] = 0;
}
}
}
}
}
return 0;
}
| replace | 75 | 86 | 75 | 87 | MLE | |
p00223 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
template <typename T> void remove(vector<T> &vector, unsigned int index) {
vector.erase(vector.begin() + index);
}
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int H, W;
// maps[y][x];
int state[55][55];
int maps[55][55][55][55];
struct Data {
P t, k;
int cost;
Data(P _t, P _k, int c) {
t = _t;
k = _k;
cost = c;
}
Data() {}
};
int bfs(Data d) {
queue<Data> que;
que.push(d);
int ans = -1;
while (que.size()) {
Data d = que.front();
que.pop();
P taka = d.t;
P kazu = d.k;
int cost = d.cost;
if (cost >= 100)
break;
if (maps[taka.first][taka.second][kazu.first][kazu.second] < cost)
continue;
maps[taka.first][taka.second][kazu.first][kazu.second] = cost;
if (taka.first == kazu.first && taka.second == kazu.second) {
ans = cost;
break;
}
for (int i = 0; i < 4; i++) {
int ntx, nty, nkx, nky;
ntx = taka.first + dx[i];
nty = taka.second + dy[i];
nkx = kazu.first - dx[i];
nky = kazu.second - dy[i];
if (ntx <= -1 || nty <= -1) {
ntx = taka.first;
nty = taka.second;
}
if (nkx <= -1 || nky <= -1) {
nkx = kazu.first;
nky = kazu.second;
}
if (state[nky][nkx] != 0) {
nkx = kazu.first;
nky = kazu.second;
}
if (state[nty][ntx] != 0) {
ntx = taka.first;
nty = taka.second;
}
/// cout << ntx << "," << nty << " " << nkx << "," << nky << endl;
que.push(Data(P(ntx, nty), P(nkx, nky), cost + 1));
}
}
return ans;
}
int main(void) {
while (cin >> W >> H, W | H) {
int x, y;
fill_n(*state, 55 * 55, -1);
fill_n(***maps, 55 * 55 * 55 * 55, INF);
cin >> x >> y;
x--;
y--;
P taka = P(x, y);
cin >> x >> y;
x--;
y--;
P kazu = P(x, y);
rep(i, H) rep(j, W) { cin >> state[i][j]; }
int ans = bfs(Data(taka, kazu, 0));
if (ans < 0 || ans >= 100) {
cout << "NA" << endl;
continue;
}
cout << ans << endl;
}
return 0;
}
int StrToInt(string s) {
stringstream ss;
ss << s;
int val;
ss >> val;
return val;
}
string IntToStr(int i) {
stringstream ss;
ss << i;
return ss.str();
} | #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
template <typename T> void remove(vector<T> &vector, unsigned int index) {
vector.erase(vector.begin() + index);
}
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int H, W;
// maps[y][x];
int state[55][55];
int maps[55][55][55][55];
struct Data {
P t, k;
int cost;
Data(P _t, P _k, int c) {
t = _t;
k = _k;
cost = c;
}
Data() {}
};
int bfs(Data d) {
queue<Data> que;
que.push(d);
int ans = -1;
while (que.size()) {
Data d = que.front();
que.pop();
P taka = d.t;
P kazu = d.k;
int cost = d.cost;
if (cost >= 100)
break;
if (maps[taka.first][taka.second][kazu.first][kazu.second] <= cost)
continue;
maps[taka.first][taka.second][kazu.first][kazu.second] = cost;
if (taka.first == kazu.first && taka.second == kazu.second) {
ans = cost;
break;
}
for (int i = 0; i < 4; i++) {
int ntx, nty, nkx, nky;
ntx = taka.first + dx[i];
nty = taka.second + dy[i];
nkx = kazu.first - dx[i];
nky = kazu.second - dy[i];
if (ntx <= -1 || nty <= -1) {
ntx = taka.first;
nty = taka.second;
}
if (nkx <= -1 || nky <= -1) {
nkx = kazu.first;
nky = kazu.second;
}
if (state[nky][nkx] != 0) {
nkx = kazu.first;
nky = kazu.second;
}
if (state[nty][ntx] != 0) {
ntx = taka.first;
nty = taka.second;
}
/// cout << ntx << "," << nty << " " << nkx << "," << nky << endl;
que.push(Data(P(ntx, nty), P(nkx, nky), cost + 1));
}
}
return ans;
}
int main(void) {
while (cin >> W >> H, W | H) {
int x, y;
fill_n(*state, 55 * 55, -1);
fill_n(***maps, 55 * 55 * 55 * 55, INF);
cin >> x >> y;
x--;
y--;
P taka = P(x, y);
cin >> x >> y;
x--;
y--;
P kazu = P(x, y);
rep(i, H) rep(j, W) { cin >> state[i][j]; }
int ans = bfs(Data(taka, kazu, 0));
if (ans < 0 || ans >= 100) {
cout << "NA" << endl;
continue;
}
cout << ans << endl;
}
return 0;
}
int StrToInt(string s) {
stringstream ss;
ss << s;
int val;
ss >> val;
return val;
}
string IntToStr(int i) {
stringstream ss;
ss << i;
return ss.str();
} | replace | 62 | 63 | 62 | 63 | 0 | |
p00223 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int h, w;
int map[100][100];
int qtx[600000], qty[600000], qkx[600000], qky[600000];
int ql[600000];
int tail, head;
int ans;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool vis[60][60][60][60];
bool bend;
void BFS(int tx, int ty, int kx, int ky, int len) {
if (len == 100)
return;
if (tx == kx && ty == ky) {
ans = len;
bend = true;
return;
}
for (int i = 0; i < 4; i++) {
bool mt = false, mk = false;
if (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
ty + dy[i] < h) {
if (map[ty + dy[i]][tx + dx[i]] == 0) {
mt = true;
}
}
if (kx - dx[i] >= 0 && kx - dx[i] < w && ky - dy[i] >= 0 &&
ky - dy[i] < h) {
if (map[ky - dy[i]][kx - dx[i]] == 0) {
mk = true;
}
}
qtx[tail] = tx + mt * dx[i], qty[tail] = ty + mt * dy[i];
qkx[tail] = kx - mk * dx[i], qky[tail] = ky - mk * dy[i];
ql[tail] = len + 1;
tail++;
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < 60; i++) {
for (int j = 0; j < 60; j++) {
for (int k = 0; k < 60; k++) {
for (int r = 0; r < 60; r++) {
vis[i][j][k][r] = false;
}
}
}
}
int tx, ty, kx, ky;
cin >> tx >> ty >> kx >> ky;
tx--, ty--, kx--, ky--;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
}
}
ans = 869121;
qtx[0] = tx, qty[0] = ty, qkx[0] = kx, qky[0] = ky;
ql[0] = 0;
tail = 1, head = 0;
bend = false;
while (true) {
if (tail == head)
break;
if (!vis[qtx[head]][qty[head]][qkx[head]][qky[head]]) {
vis[qtx[head]][qty[head]][qkx[head]][qky[head]] = true;
BFS(qtx[head], qty[head], qkx[head], qky[head], ql[head]);
}
if (bend)
break;
head++;
}
if (ans == 869121)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int h, w;
int map[100][100];
int qtx[800000], qty[800000], qkx[800000], qky[800000];
int ql[800000];
int tail, head;
int ans;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool vis[60][60][60][60];
bool bend;
void BFS(int tx, int ty, int kx, int ky, int len) {
if (len == 100)
return;
if (tx == kx && ty == ky) {
ans = len;
bend = true;
return;
}
for (int i = 0; i < 4; i++) {
bool mt = false, mk = false;
if (tx + dx[i] >= 0 && tx + dx[i] < w && ty + dy[i] >= 0 &&
ty + dy[i] < h) {
if (map[ty + dy[i]][tx + dx[i]] == 0) {
mt = true;
}
}
if (kx - dx[i] >= 0 && kx - dx[i] < w && ky - dy[i] >= 0 &&
ky - dy[i] < h) {
if (map[ky - dy[i]][kx - dx[i]] == 0) {
mk = true;
}
}
qtx[tail] = tx + mt * dx[i], qty[tail] = ty + mt * dy[i];
qkx[tail] = kx - mk * dx[i], qky[tail] = ky - mk * dy[i];
ql[tail] = len + 1;
tail++;
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < 60; i++) {
for (int j = 0; j < 60; j++) {
for (int k = 0; k < 60; k++) {
for (int r = 0; r < 60; r++) {
vis[i][j][k][r] = false;
}
}
}
}
int tx, ty, kx, ky;
cin >> tx >> ty >> kx >> ky;
tx--, ty--, kx--, ky--;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> map[i][j];
}
}
ans = 869121;
qtx[0] = tx, qty[0] = ty, qkx[0] = kx, qky[0] = ky;
ql[0] = 0;
tail = 1, head = 0;
bend = false;
while (true) {
if (tail == head)
break;
if (!vis[qtx[head]][qty[head]][qkx[head]][qky[head]]) {
vis[qtx[head]][qty[head]][qkx[head]][qky[head]] = true;
BFS(qtx[head], qty[head], qkx[head], qky[head], ql[head]);
}
if (bend)
break;
head++;
}
if (ans == 869121)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | replace | 5 | 7 | 5 | 7 | 0 | |
p00223 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;
int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
while (true) {
cin >> x >> y;
if (!x && !y) {
break;
}
int tx, ty, kx, ky;
memset(visited, false, sizeof visited);
memset(flr, false, sizeof flr);
cin >> tx >> ty >> kx >> ky;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
int in;
cin >> in;
flr[i][j] = (bool)in;
}
}
for (int i = 1; i <= y; i++) {
flr[i][0] = flr[i][x + 1] = true;
}
for (int i = 1; i <= x; i++) {
flr[0][i] = flr[y + 1][i] = true;
}
queue<PPP> q;
q.push(MP(MP(ty, tx), MP(ky, kx)));
for (int i = 0; i <= 100; i++) {
int sz = q.size();
for (int j = 0; j < sz; j++) {
PPP now = q.front();
q.pop();
if (!visited[now.F.F][now.F.S][now.S.F][now.S.S]) {
visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;
if (now.F.F == now.S.F && now.F.S == now.S.S) {
cout << i << endl;
goto end;
}
for (int k = 0; k < 4; k++) {
int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
PPP next =
MP(MP(now.F.F + ntdy * !flr[now.F.F + ntdy][now.F.S + ntdx],
now.F.S + ntdx * !flr[now.F.F + ntdy][now.F.S + ntdx]),
MP(now.S.F + nkdy * !flr[now.S.F + nkdy][now.S.S + nkdx],
now.S.S + nkdx * !flr[now.S.F + nkdy][now.S.S + nkdx]));
cout << now.F.F + ntdx * !flr[now.F.F + ntdx][now.F.S + ntdy] << " "
<< now.F.S + ntdy * !flr[now.F.F + ntdx][now.F.S + ntdy] << " "
<< now.S.F + nkdx * !flr[now.S.F + nkdx][now.S.S + nkdy] << " "
<< now.S.S + nkdy * !flr[now.S.F + nkdx][now.S.S + nkdy]
<< endl;
q.push(next);
}
}
}
}
cout << "NA" << endl;
end:;
}
return 0;
} | #include <bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;
int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
while (true) {
cin >> x >> y;
if (!x && !y) {
break;
}
int tx, ty, kx, ky;
memset(visited, false, sizeof visited);
memset(flr, false, sizeof flr);
cin >> tx >> ty >> kx >> ky;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
int in;
cin >> in;
flr[i][j] = (bool)in;
}
}
for (int i = 1; i <= y; i++) {
flr[i][0] = flr[i][x + 1] = true;
}
for (int i = 1; i <= x; i++) {
flr[0][i] = flr[y + 1][i] = true;
}
queue<PPP> q;
q.push(MP(MP(ty, tx), MP(ky, kx)));
for (int i = 0; i <= 100; i++) {
int sz = q.size();
for (int j = 0; j < sz; j++) {
PPP now = q.front();
q.pop();
if (!visited[now.F.F][now.F.S][now.S.F][now.S.S]) {
visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;
if (now.F.F == now.S.F && now.F.S == now.S.S) {
cout << i << endl;
goto end;
}
for (int k = 0; k < 4; k++) {
int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
PPP next =
MP(MP(now.F.F + ntdy * !flr[now.F.F + ntdy][now.F.S + ntdx],
now.F.S + ntdx * !flr[now.F.F + ntdy][now.F.S + ntdx]),
MP(now.S.F + nkdy * !flr[now.S.F + nkdy][now.S.S + nkdx],
now.S.S + nkdx * !flr[now.S.F + nkdy][now.S.S + nkdx]));
q.push(next);
}
}
}
}
cout << "NA" << endl;
end:;
}
return 0;
} | delete | 62 | 67 | 62 | 62 | TLE | |
p00223 | C++ | Time Limit Exceeded | #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int x, y;
int tm[51][51][51][51];
char field[51][51];
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
struct twins {
int x1;
int y1;
int x2;
int y2;
};
bool can_move(int x1, int y1) {
if (x1 > 0 && x1 <= x && y1 > 0 && y1 <= y) {
if (field[y1][x1] == '0')
return true;
else
return false;
}
return false;
}
void get_time(int x1, int y1, int x2, int y2) {
twins t0;
t0.x1 = x1, t0.x2 = x2, t0.y1 = y1, t0.y2 = y2;
queue<twins> qt;
qt.push(t0);
while (!qt.empty()) {
twins t = qt.front();
qt.pop();
for (int i = 0; i < 4; i++) {
int ay = t.y1 + dy[i];
int ax = t.x1 + dx[i];
int by = t.y2 - dy[i];
int bx = t.x2 - dx[i];
if (!can_move(ax, ay)) {
ay = t.y1;
ax = t.x1;
}
if (!can_move(bx, by)) {
by = t.y2;
bx = t.x2;
}
if (tm[ay][ax][by][bx] < 0 ||
tm[ay][ax][by][bx] > tm[t.y1][t.x1][t.y2][t.x2] + 1) {
tm[ay][ax][by][bx] = tm[t.y1][t.x1][t.y2][t.x2] + 1;
t0.x1 = ax, t0.x2 = bx, t0.y1 = ay, t0.y2 = by;
qt.push(t0);
}
}
}
}
int main() {
int ax, ay, bx, by;
while (cin >> x >> y && (x || y)) {
int count = 101;
memset(tm, -1, sizeof(tm));
cin >> ax >> ay >> bx >> by;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
cin >> field[i][j];
}
}
tm[ay][ax][by][bx] = 0;
get_time(ax, ay, bx, by);
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
if (tm[i][j][i][j] >= 0)
count = min(count, tm[i][j][i][j]);
}
}
if (count == 101)
cout << "NA" << endl;
else
cout << count << endl;
}
return 0;
} | #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int x, y;
int tm[51][51][51][51];
char field[51][51];
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
struct twins {
int x1;
int y1;
int x2;
int y2;
};
bool can_move(int x1, int y1) {
if (x1 > 0 && x1 <= x && y1 > 0 && y1 <= y) {
if (field[y1][x1] == '0')
return true;
else
return false;
}
return false;
}
void get_time(int x1, int y1, int x2, int y2) {
twins t0;
t0.x1 = x1, t0.x2 = x2, t0.y1 = y1, t0.y2 = y2;
queue<twins> qt;
qt.push(t0);
while (!qt.empty()) {
twins t = qt.front();
qt.pop();
for (int i = 0; i < 4; i++) {
int ay = t.y1 + dy[i];
int ax = t.x1 + dx[i];
int by = t.y2 - dy[i];
int bx = t.x2 - dx[i];
if (!can_move(ax, ay)) {
ay = t.y1;
ax = t.x1;
}
if (!can_move(bx, by)) {
by = t.y2;
bx = t.x2;
}
if (tm[ay][ax][by][bx] < 0) {
tm[ay][ax][by][bx] = tm[t.y1][t.x1][t.y2][t.x2] + 1;
t0.x1 = ax, t0.x2 = bx, t0.y1 = ay, t0.y2 = by;
qt.push(t0);
}
}
}
}
int main() {
int ax, ay, bx, by;
while (cin >> x >> y && (x || y)) {
int count = 101;
memset(tm, -1, sizeof(tm));
cin >> ax >> ay >> bx >> by;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
cin >> field[i][j];
}
}
tm[ay][ax][by][bx] = 0;
get_time(ax, ay, bx, by);
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
if (tm[i][j][i][j] >= 0)
count = min(count, tm[i][j][i][j]);
}
}
if (count == 101)
cout << "NA" << endl;
else
cout << count << endl;
}
return 0;
} | replace | 59 | 61 | 59 | 60 | TLE | |
p00223 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
typedef long long int ll;
using namespace std;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int xs, ys, tx, ty, kx, ky;
int main() {
while (true) {
cin >> xs >> ys;
if (xs == 0 && ys == 0)
break;
cin >> tx >> ty;
cin >> kx >> ky;
tx--;
ty--;
kx--;
ky--;
if (tx == kx && ty == ky) {
cout << 0 << endl;
continue;
}
vector<vector<int>> map(ys, vector<int>(xs));
REP(i, ys) REP(j, xs) cin >> map[i][j];
queue<int> qtx;
queue<int> qty;
queue<int> qkx;
queue<int> qky;
queue<int> qn;
set<pair<pair<int, int>, pair<int, int>>> used;
int ans = 100;
qtx.push(tx);
qty.push(ty);
qkx.push(kx);
qky.push(ky);
qn.push(1);
while (!qtx.empty()) {
int x1 = qtx.front();
int y1 = qty.front();
int x2 = qkx.front();
int y2 = qky.front();
int n = qn.front();
if (n >= 100) {
ans = min(ans, 100);
break;
}
qtx.pop();
qty.pop();
qkx.pop();
qky.pop();
qn.pop();
REP(i, 4) {
int ntx = x1 + dx[i];
int nty = y1 + dy[i];
int nkx = x2 - dx[i];
int nky = y2 - dy[i];
if (ntx < 0 || xs <= ntx)
ntx = x1;
if (nty < 0 || ys <= nty)
nty = y1;
if (nkx < 0 || xs <= nkx)
nkx = x2;
if (nky < 0 || ys <= nky)
nky = y2;
if (map[nty][ntx] == 1) {
ntx = x1;
nty = y1;
}
if (map[nky][nkx] == 1) {
nkx = x2;
nky = y2;
}
pair<pair<int, int>, pair<int, int>> pp(pair<int, int>(ntx, nty),
pair<int, int>(nkx, nky));
if (used.find(pp) != used.end())
continue;
used.insert(pp);
if (ntx == nkx && nty == nky) {
ans = min(ans, n);
break;
}
qtx.push(ntx);
qty.push(nty);
qkx.push(nkx);
qky.push(nky);
qn.push(n + 1);
}
}
if (ans >= 100)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
typedef long long int ll;
using namespace std;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int xs, ys, tx, ty, kx, ky;
int main() {
while (true) {
cin >> xs >> ys;
if (xs == 0 && ys == 0)
break;
cin >> tx >> ty;
cin >> kx >> ky;
tx--;
ty--;
kx--;
ky--;
if (tx == kx && ty == ky) {
cout << 0 << endl;
continue;
}
vector<vector<int>> map(ys, vector<int>(xs));
REP(i, ys) REP(j, xs) cin >> map[i][j];
queue<int> qtx;
queue<int> qty;
queue<int> qkx;
queue<int> qky;
queue<int> qn;
set<pair<pair<int, int>, pair<int, int>>> used;
int ans = 100;
qtx.push(tx);
qty.push(ty);
qkx.push(kx);
qky.push(ky);
qn.push(1);
while (!qtx.empty()) {
int x1 = qtx.front();
int y1 = qty.front();
int x2 = qkx.front();
int y2 = qky.front();
int n = qn.front();
if (n >= 100) {
ans = min(ans, 100);
break;
}
qtx.pop();
qty.pop();
qkx.pop();
qky.pop();
qn.pop();
REP(i, 4) {
int ntx = x1 + dx[i];
int nty = y1 + dy[i];
int nkx = x2 - dx[i];
int nky = y2 - dy[i];
if (ntx < 0 || xs <= ntx)
ntx = x1;
if (nty < 0 || ys <= nty)
nty = y1;
if (nkx < 0 || xs <= nkx)
nkx = x2;
if (nky < 0 || ys <= nky)
nky = y2;
if (map[nty][ntx] == 1) {
ntx = x1;
nty = y1;
}
if (map[nky][nkx] == 1) {
nkx = x2;
nky = y2;
}
pair<pair<int, int>, pair<int, int>> pp(pair<int, int>(ntx, nty),
pair<int, int>(nkx, nky));
if (used.find(pp) != used.end())
continue;
used.insert(pp);
if (ntx == nkx && nty == nky) {
ans = min(ans, n);
break;
}
qtx.push(ntx);
qty.push(nty);
qkx.push(nkx);
qky.push(nky);
qn.push(n + 1);
}
if (ans < 100)
break;
}
if (ans >= 100)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | insert | 101 | 101 | 101 | 103 | MLE | |
p00223 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct State {
pair<int, int> t, k;
int turn;
};
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int w, h;
int field[52][52];
bool visited[52][52][52][52];
int main() {
while (cin >> w >> h, w | h) {
memset(visited, false, sizeof(visited));
queue<State> q;
State a, b;
a.turn = 0;
cin >> a.t.second >> a.t.first;
cin >> a.k.second >> a.k.first;
for (int i = 0; i < 52; i++) {
for (int j = 0; j < 52; j++)
field[i][j] = 1;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
cin >> field[i][j];
}
int ans = -1;
q.push(a);
visited[a.t.first][a.t.second][a.k.first][a.k.second] = true;
while (!q.empty()) {
a = q.front();
q.pop();
if (a.t.first == a.k.first && a.t.second == a.k.second) {
ans = a.turn;
break;
}
if (100 <= a.turn + abs(a.t.first - a.k.first) / 2 +
abs(a.t.second - a.k.second) / 2) {
break;
}
for (int i = 0; i < 4; i++) {
b = a;
b.t.first += dy[i];
b.t.second += dx[i];
b.k.first -= dy[i];
b.k.second -= dx[i];
if (field[b.t.first][b.t.second])
b.t = a.t;
if (field[b.k.first][b.k.second])
b.k = a.k;
b.turn = a.turn + 1;
if (!visited[b.t.first][b.t.second][b.k.first][b.k.second]) {
visited[b.t.first][b.t.second][b.k.first][b.k.second] = true;
q.push(b);
}
}
}
while (!q.empty())
q.pop();
if (ans == -1)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct State {
pair<int, int> t, k;
int turn;
};
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int w, h;
int field[52][52];
bool visited[52][52][52][52];
int main() {
while (cin >> w >> h, w | h) {
memset(visited, false, sizeof(visited));
queue<State> q;
State a, b;
a.turn = 0;
cin >> a.t.second >> a.t.first;
cin >> a.k.second >> a.k.first;
for (int i = 0; i < 52; i++) {
for (int j = 0; j < 52; j++)
field[i][j] = 1;
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++)
cin >> field[i][j];
}
int ans = -1;
q.push(a);
visited[a.t.first][a.t.second][a.k.first][a.k.second] = true;
while (!q.empty()) {
a = q.front();
q.pop();
if (a.t.first == a.k.first && a.t.second == a.k.second) {
ans = a.turn;
break;
}
if (100 <= a.turn + abs(a.t.first - a.k.first) / 2 +
abs(a.t.second - a.k.second) / 2) {
break;
}
for (int i = 0; i < 4; i++) {
b = a;
b.t.first += dy[i];
b.t.second += dx[i];
b.k.first -= dy[i];
b.k.second -= dx[i];
if (field[b.t.first][b.t.second])
b.t = a.t;
if (field[b.k.first][b.k.second])
b.k = a.k;
b.turn = a.turn + 1;
if (!visited[b.t.first][b.t.second][b.k.first][b.k.second]) {
visited[b.t.first][b.t.second][b.k.first][b.k.second] = true;
q.push(b);
}
}
}
while (!q.empty())
q.pop();
if (ans == -1)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | replace | 29 | 31 | 29 | 31 | -11 | |
p00224 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;
struct E {
E(int dis, string to) : dis(dis), to(to) {}
int dis;
string to;
};
struct P {
P(int calo, int visit, string pos) : calo(calo), visit(visit), pos(pos) {}
int calo, visit;
string pos;
};
bool operator>(const P &a, const P &b) { return a.calo < b.calo; }
int main() {
int m, n, k, d;
while (cin >> m >> n >> k >> d, m | n | k | d) {
map<string, int> cake;
for (int i = 1; i <= m; ++i)
cin >> cake[string("C") + (char)('0' + i)];
map<string, vector<E>> edge;
for (int i = 0; i < d; ++i) {
string a, b;
int t;
cin >> a >> b >> t;
edge[a].push_back(E(t, b));
edge[b].push_back(E(t, a));
}
map<string, map<int, int>> calo;
queue<P> qq;
priority_queue<P, vector<P>, greater<P>> q;
calo["H"][0] = 0;
q.push(P(0, 0, "H"));
while (!q.empty()) {
P p = q.top();
q.pop();
if (calo.count(p.pos) && calo[p.pos].count(p.calo) &&
p.calo > calo[p.pos][p.visit])
continue;
foreach (it, edge[p.pos]) {
int next_calo = p.calo + it->dis * k;
int next_visit = p.visit;
if (it->to[0] == 'C') {
if (p.visit & 1 << (it->to[1] - '1'))
continue;
else {
next_calo -= cake[it->to];
next_visit |= 1 << (it->to[1] - '1');
}
}
if (!calo.count(it->to) || !calo[it->to].count(next_visit) ||
next_calo < calo[it->to][next_visit]) {
calo[it->to][next_visit] = next_calo;
q.push(P(next_calo, next_visit, it->to));
}
}
}
int res = 100000000;
foreach (it, calo["D"])
res = min(res, it->second);
printf("%d\n", res);
}
return 0;
} | #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;
struct E {
E(int dis, string to) : dis(dis), to(to) {}
int dis;
string to;
};
struct P {
P(int calo, int visit, string pos) : calo(calo), visit(visit), pos(pos) {}
int calo, visit;
string pos;
};
bool operator>(const P &a, const P &b) { return a.calo > b.calo; }
int main() {
int m, n, k, d;
while (cin >> m >> n >> k >> d, m | n | k | d) {
map<string, int> cake;
for (int i = 1; i <= m; ++i)
cin >> cake[string("C") + (char)('0' + i)];
map<string, vector<E>> edge;
for (int i = 0; i < d; ++i) {
string a, b;
int t;
cin >> a >> b >> t;
edge[a].push_back(E(t, b));
edge[b].push_back(E(t, a));
}
map<string, map<int, int>> calo;
queue<P> qq;
priority_queue<P, vector<P>, greater<P>> q;
calo["H"][0] = 0;
q.push(P(0, 0, "H"));
while (!q.empty()) {
P p = q.top();
q.pop();
if (calo.count(p.pos) && calo[p.pos].count(p.calo) &&
p.calo > calo[p.pos][p.visit])
continue;
foreach (it, edge[p.pos]) {
int next_calo = p.calo + it->dis * k;
int next_visit = p.visit;
if (it->to[0] == 'C') {
if (p.visit & 1 << (it->to[1] - '1'))
continue;
else {
next_calo -= cake[it->to];
next_visit |= 1 << (it->to[1] - '1');
}
}
if (!calo.count(it->to) || !calo[it->to].count(next_visit) ||
next_calo < calo[it->to][next_visit]) {
calo[it->to][next_visit] = next_calo;
q.push(P(next_calo, next_visit, it->to));
}
}
}
int res = 100000000;
foreach (it, calo["D"])
res = min(res, it->second);
printf("%d\n", res);
}
return 0;
} | replace | 52 | 55 | 52 | 53 | TLE | |
p00225 | C++ | Runtime Error | // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
// ユニオンファインド木
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// int n;//単語の数
int par[26];
int r[26]; // rank
void init(int n) {
for (int i = 0; i < n; i++) {
r[i] = 0;
par[i] = i;
}
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (r[x] < r[y]) {
par[x] = y;
} else if (r[x] > r[y]) {
par[y] = x;
} else if (r[x] == r[y]) {
par[x] = y;
r[y]++;
}
}
int main() {
int n;
while (cin >> n, n) {
init(n);
vector<string> word(n);
for (auto &s : word) {
cin >> s;
}
vector<int> in(26), out(26);
for (auto &s : word) {
const auto
first = s[0] - 'a',
last =
s.back() -
'a'; // 最初と最後の文字を取り出す -'a'はa-zを0-25に対応させている
in[first]++; // 入次数
out[last]++; // 出次数
unite(first, last); // 頂点(アルファべット)first,lastが同じ集合に属する
}
bool ok = true;
int comp_num = 0; // 集合の数
for (int i = 0; i < 26; i++) {
comp_num += (in[i] && root(i) == i);
ok &= in[i] == out[i];
}
ok &= (comp_num == 1);
if (ok) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
return 0;
}
| // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
// ユニオンファインド木
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// int n;//単語の数
int par[26];
int r[26]; // rank
void init(int n) {
for (int i = 0; i < n; i++) {
r[i] = 0;
par[i] = i;
}
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = root(par[x]);
}
}
bool same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (r[x] < r[y]) {
par[x] = y;
} else if (r[x] > r[y]) {
par[y] = x;
} else if (r[x] == r[y]) {
par[x] = y;
r[y]++;
}
}
int main() {
int n;
while (cin >> n, n) {
init(26);
vector<string> word(n);
for (auto &s : word) {
cin >> s;
}
vector<int> in(26), out(26);
for (auto &s : word) {
const auto
first = s[0] - 'a',
last =
s.back() -
'a'; // 最初と最後の文字を取り出す -'a'はa-zを0-25に対応させている
in[first]++; // 入次数
out[last]++; // 出次数
unite(first, last); // 頂点(アルファべット)first,lastが同じ集合に属する
}
bool ok = true;
int comp_num = 0; // 集合の数
for (int i = 0; i < 26; i++) {
comp_num += (in[i] && root(i) == i);
ok &= in[i] == out[i];
}
ok &= (comp_num == 1);
if (ok) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
return 0;
}
| replace | 49 | 50 | 49 | 50 | 0 | |
p00225 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct UnionFind {
int n;
vector<int> parent;
vector<int> rank;
vector<int> num;
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
UnionFind(int n_) {
n = n_;
parent.resize(n);
for (int i = 0; i < n; i++)
parent[i] = i;
rank.assign(n, 0);
num.assign(n, 1);
}
void unite(int x, int y) {
if ((x = find(x)) != (y = find(y))) {
if (rank[x] < rank[y]) {
parent[x] = y;
num[y] += num[x];
} else {
parent[y] = x;
if (rank[x] == rank[y])
rank[x]++;
num[x] += num[y];
}
n--;
}
}
bool same(int x, int y) { return find(x) == find(y); }
int get() { return n; }
int get(int x) { return num[find(x)]; }
};
int main() {
int n;
while (scanf("%d", &n), n) {
UnionFind uf(26);
vector<int> in_deg(26), out_deg(26);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int a = s[0] - 'a';
int b = s.back() - 'a';
in_deg[a]++, out_deg[b]++;
uf.unite(a, b);
}
bool ok = true;
int cnt = 0;
for (int i = 0; i < 26; i++)
if (in_deg[i] > 0 || out_deg[i] > 0)
cnt++;
cerr << cnt << endl;
for (int i = 0; i < 26; i++) {
if (in_deg[i] > 0 || out_deg[i] > 0) {
if (uf.get(i) != cnt) {
ok = false;
break;
}
}
}
for (int i = 0; i < 26; i++)
ok &= in_deg[i] == out_deg[i];
puts(ok ? "OK" : "NG");
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct UnionFind {
int n;
vector<int> parent;
vector<int> rank;
vector<int> num;
int find(int x) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
UnionFind(int n_) {
n = n_;
parent.resize(n);
for (int i = 0; i < n; i++)
parent[i] = i;
rank.assign(n, 0);
num.assign(n, 1);
}
void unite(int x, int y) {
if ((x = find(x)) != (y = find(y))) {
if (rank[x] < rank[y]) {
parent[x] = y;
num[y] += num[x];
} else {
parent[y] = x;
if (rank[x] == rank[y])
rank[x]++;
num[x] += num[y];
}
n--;
}
}
bool same(int x, int y) { return find(x) == find(y); }
int get() { return n; }
int get(int x) { return num[find(x)]; }
};
int main() {
int n;
while (scanf("%d", &n), n) {
UnionFind uf(26);
vector<int> in_deg(26), out_deg(26);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int a = s[0] - 'a';
int b = s.back() - 'a';
in_deg[a]++, out_deg[b]++;
uf.unite(a, b);
}
bool ok = true;
int cnt = 0;
for (int i = 0; i < 26; i++)
if (in_deg[i] > 0 || out_deg[i] > 0)
cnt++;
for (int i = 0; i < 26; i++) {
if (in_deg[i] > 0 || out_deg[i] > 0) {
if (uf.get(i) != cnt) {
ok = false;
break;
}
}
}
for (int i = 0; i < 26; i++)
ok &= in_deg[i] == out_deg[i];
puts(ok ? "OK" : "NG");
}
return 0;
} | delete | 62 | 63 | 62 | 62 | 0 | 7
7
|
p00227 | C++ | Runtime Error | #include <stdio.h>
int main() {
int N, M;
int n[1001] = {};
while (1) {
int s = 0;
scanf("%d %d", &N, M);
if (N == 0 && M == 0)
break;
for (int i = 1; i <= N; i++)
scanf("%d", &n[i]);
for (int i = 1; i <= N; i++)
for (int j = N; j > i; j--)
if (n[j] > n[j - 1]) {
int t = n[j - 1];
n[j - 1] = n[j];
n[j] = t;
}
for (int i = 1; i <= N; i++) {
if (i % M != 0)
s += n[i];
}
printf("%d\n", s);
}
return 0;
} | #include <stdio.h>
int main() {
int N, M;
int n[1001] = {};
while (1) {
int s = 0;
scanf("%d %d", &N, &M);
if (N == 0 && M == 0)
break;
for (int i = 1; i <= N; i++)
scanf("%d", &n[i]);
for (int i = 1; i <= N; i++)
for (int j = N; j > i; j--)
if (n[j] > n[j - 1]) {
int t = n[j - 1];
n[j - 1] = n[j];
n[j] = t;
}
for (int i = 1; i <= N; i++) {
if (i % M != 0)
s += n[i];
}
printf("%d\n", s);
}
return 0;
} | replace | 8 | 9 | 8 | 9 | -11 | |
p00227 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
int n, m, fig[1001];
while (cin >> n >> m) {
int ans = 0;
for (int i = 0; i < n; i++)
cin >> fig[i];
sort(fig, fig + n, greater<int>());
for (int i = 0; i < (n / m) * m; i++) {
if ((i + 1) % m)
ans += fig[i];
}
for (int i = n - 1, j = 0; j < (n % m); i--, j++)
ans += fig[i];
fill(fig, fig + 1001, 0);
cout << ans << endl;
}
} | #include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
int n, m, fig[1001];
while (cin >> n >> m, n || m) {
int ans = 0;
for (int i = 0; i < n; i++)
cin >> fig[i];
sort(fig, fig + n, greater<int>());
for (int i = 0; i < (n / m) * m; i++) {
if ((i + 1) % m)
ans += fig[i];
}
for (int i = n - 1, j = 0; j < (n % m); i--, j++)
ans += fig[i];
fill(fig, fig + 1001, 0);
cout << ans << endl;
}
} | replace | 7 | 8 | 7 | 8 | -8 | |
p00227 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int a, b, i, sum = 0;
int c[a];
while (1) {
cin >> a >> b;
if (a == 0 && b == 0)
break;
for (i = 0; i < a; i++) {
cin >> c[i];
sum += c[i];
}
sort(c, c + a);
reverse(c, c + a);
for (i = b - 1; i < a; i += b) {
sum -= c[i];
}
cout << sum << endl;
sum = 0;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int a, b, i, sum = 0;
int c[10000];
while (1) {
cin >> a >> b;
if (a == 0 && b == 0)
break;
for (i = 0; i < a; i++) {
cin >> c[i];
sum += c[i];
}
sort(c, c + a);
reverse(c, c + a);
for (i = b - 1; i < a; i += b) {
sum -= c[i];
}
cout << sum << endl;
sum = 0;
}
return 0;
} | replace | 6 | 7 | 6 | 7 | -11 | |
p00227 | 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) \
{ cerr << #o << " " << o << endl; }
#define dumpc(o) \
{ \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
signed main() {
int p[1010] = {};
for (int n, m; cin >> n >> m && n;) {
memset(p, 0, sizeof(p));
int ans = 0;
rep(i, 0, n) {
cin >> p[i];
ans += p[i];
}
sort(p, p + n);
dumpc(p);
for (int i = n % m; i < n; i += m) {
ans -= p[i];
}
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) \
{ cerr << #o << " " << o << endl; }
#define dumpc(o) \
{ \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
signed main() {
int p[1010] = {};
for (int n, m; cin >> n >> m && n;) {
memset(p, 0, sizeof(p));
int ans = 0;
rep(i, 0, n) {
cin >> p[i];
ans += p[i];
}
sort(p, p + n);
for (int i = n % m; i < n; i += m) {
ans -= p[i];
}
cout << ans << endl;
}
return 0;
} | delete | 55 | 56 | 55 | 55 | 0 | p 40 50 80 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
p 100 200 300 400 500 600 700 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
p00227 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
int n, m; // 問題文のn,m
while (std::cin >> n >> m) {
if (n == 0 && m == 0)
break;
std::vector<int> p(n); // 野菜価格一覧
for (int i = 0; i < n; ++i) {
std::cin >> p[i];
}
sort(p.begin(), p.end());
reverse(p.begin(), p.end()); // 降順ソート
for (int i = m - 1; i < n; i += m) {
p.erase(p.begin() + i);
}
std::cout << accumulate(p.begin(), p.end(), 0) << std::endl;
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
int n, m; // 問題文のn,m
while (std::cin >> n >> m) {
if (n == 0 && m == 0)
break;
std::vector<int> p(n); // 野菜価格一覧
for (int i = 0; i < n; ++i) {
std::cin >> p[i];
}
sort(p.begin(), p.end());
reverse(p.begin(), p.end()); // 降順ソート
for (int i = m - 1; i <= n - 1; i += m) {
p[i] = 0;
}
std::cout << accumulate(p.begin(), p.end(), 0) << std::endl;
}
return 0;
}
| replace | 20 | 22 | 20 | 22 | -6 | Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p00227 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int n, m;
vector<int> vege;
const int INF = 1 << 29;
int main(void) {
while (cin >> n >> m) {
vege.resize(n);
for (int i = 0; i < n; i++) {
cin >> vege[i];
}
sort(vege.begin(), vege.end(), greater<int>());
int sum = accumulate(vege.begin(), vege.end(), 0);
for (int i = 1; i - 1 < n / m; i++) {
sum -= vege[i * m - 1];
}
cout << sum << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int n, m;
vector<int> vege;
const int INF = 1 << 29;
int main(void) {
while (cin >> n >> m && n && m) {
vege.resize(n);
for (int i = 0; i < n; i++) {
cin >> vege[i];
}
sort(vege.begin(), vege.end(), greater<int>());
int sum = accumulate(vege.begin(), vege.end(), 0);
for (int i = 1; i - 1 < n / m; i++) {
sum -= vege[i * m - 1];
}
cout << sum << endl;
}
return 0;
} | replace | 19 | 20 | 19 | 20 | -8 | |
p00228 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
string table[] = {"0111111", "0000110", "1011011", "1001111", "1100110",
"1101101", "1111101", "1111111", "1101111"};
int n;
while (cin >> n, ~n) {
string cur = "0000000";
rep(i, n) {
int number;
cin >> number;
rep(i, 7) cout << (cur[i] != table[number][i]);
cout << endl;
cur = table[number];
}
}
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
string table[] = {"0111111", "0000110", "1011011", "1001111", "1100110",
"1101101", "1111101", "0100111", "1111111", "1101111"};
int n;
while (cin >> n, ~n) {
string cur = "0000000";
rep(i, n) {
int number;
cin >> number;
rep(i, 7) cout << (cur[i] != table[number][i]);
cout << endl;
cur = table[number];
}
}
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00230 | 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 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, b[2][128];
int rec1(int x, int y) {
if (b[x][y + 1] == 1)
return rec1(x, y + 1);
return y;
}
int rec2(int x, int y) {
if (b[x][y] == 2)
return rec2(x, y - 1);
return y;
}
int main() {
while (scanf("%d", &n) && n) {
rep(i, 2) rep(j, n) scanf("%d", &b[i][j]);
queue<pii> q;
q.push(MP(0, MP(0, 0)));
q.push(MP(1, MP(0, 0)));
map<pi, int> m;
m[MP(0, 0)] = 1;
m[MP(1, 0)] = 0;
int res = -1;
while (!q.empty()) {
pii now = q.front();
q.pop();
// printf("%d %d %d\n", now.F, now.S.F, now.S.S);
if (b[now.F][now.S.F] == 1)
now.S.F = rec1(now.F, now.S.F);
else if (b[now.F][now.S.F] == 2)
now.S.F = rec2(now.F, now.S.F);
if (now.S.F == n - 1) {
res = now.S.S;
break;
}
rep(i, 3) {
pii next = now;
next.F = (next.F + 1) % 2;
next.S.F += i;
next.S.S++;
if (m.count(MP(next.F, next.S.F)))
continue;
m[MP(next.F, next.S.F)] = next.S.S;
q.push(next);
}
}
if (res < 0)
puts("NA");
else
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 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, b[2][128];
int rec1(int x, int y) {
if (b[x][y + 1] == 1)
return rec1(x, y + 1);
return y;
}
int rec2(int x, int y) {
if (b[x][y] == 2)
return rec2(x, y - 1);
return y;
}
int main() {
while (scanf("%d", &n) && n) {
rep(i, 2) rep(j, n) scanf("%d", &b[i][j]);
queue<pii> q;
q.push(MP(0, MP(0, 0)));
q.push(MP(1, MP(0, 0)));
map<pi, int> m;
m[MP(0, 0)] = 1;
m[MP(1, 0)] = 0;
int res = -1;
while (!q.empty()) {
pii now = q.front();
q.pop();
// printf("%d %d %d\n", now.F, now.S.F, now.S.S);
if (b[now.F][now.S.F] == 1)
now.S.F = rec1(now.F, now.S.F);
else if (b[now.F][now.S.F] == 2)
now.S.F = rec2(now.F, now.S.F);
if (now.S.F >= n - 1) {
res = now.S.S;
break;
}
rep(i, 3) {
pii next = now;
next.F = (next.F + 1) % 2;
next.S.F += i;
next.S.S++;
if (m.count(MP(next.F, next.S.F)))
continue;
m[MP(next.F, next.S.F)] = next.S.S;
q.push(next);
}
}
if (res < 0)
puts("NA");
else
printf("%d\n", res);
}
return 0;
} | replace | 66 | 67 | 66 | 67 | 0 | |
p00231 | C++ | Time Limit Exceeded | #include <iostream>
#include <map>
using namespace std;
#define START 0
#define END 1
int main() {
while (true) {
int n;
scanf("%d", &n);
if (n == 0)
break;
multimap<int, pair<int, int>> M;
for (int i = 0; i < n; ++i) {
int m, a, b;
scanf("%d%d%d", &m, &a, &b);
M.insert(make_pair(a, make_pair(START, m)));
M.insert(make_pair(b, make_pair(END, m)));
}
int total = 0;
bool ng = false;
for (map<int, pair<int, int>>::iterator itm = M.begin(); itm != M.end();
++itm) {
int t = itm->first;
while (t == itm->first) {
if (itm->second.first == START) {
total += itm->second.second;
} else {
total -= itm->second.second;
}
++itm;
}
if (total > 150) {
ng = true;
}
--itm;
}
if (ng) {
puts("NG");
} else {
puts("OK");
}
}
return 0;
} | #include <iostream>
#include <map>
using namespace std;
#define START 0
#define END 1
int main() {
while (true) {
int n;
scanf("%d", &n);
if (n == 0)
break;
multimap<int, pair<int, int>> M;
for (int i = 0; i < n; ++i) {
int m, a, b;
scanf("%d%d%d", &m, &a, &b);
M.insert(make_pair(a, make_pair(START, m)));
M.insert(make_pair(b, make_pair(END, m)));
}
int total = 0;
bool ng = false;
for (map<int, pair<int, int>>::iterator itm = M.begin(); itm != M.end();
++itm) {
int t = itm->first;
while (t == itm->first) {
if (itm->second.first == START) {
total += itm->second.second;
} else {
total -= itm->second.second;
}
++itm;
if (itm == M.end())
break;
}
if (total > 150) {
ng = true;
}
--itm;
}
if (ng) {
puts("NG");
} else {
puts("OK");
}
}
return 0;
} | insert | 32 | 32 | 32 | 34 | TLE | |
p00231 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P; // time,which,weight
int main() {
P t[101];
int n, a, b, c;
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
t[i].second.second = a;
t[i].first = b;
t[i].second.first = 1;
t[i + n].second.second = a;
t[i + n].first = c;
t[i + n].second.first = 0;
}
int now = 0;
sort(t, t + 2 * n);
for (int i = 0; i < 2 * n; i++) {
if (t[i].second.first)
now += t[i].second.second;
else
now -= t[i].second.second;
if (now > 150) {
cout << "NG" << endl;
goto end;
}
}
cout << "OK" << endl;
end:;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P; // time,which,weight
int main() {
P t[300];
int n, a, b, c;
while (cin >> n, n) {
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
t[i].second.second = a;
t[i].first = b;
t[i].second.first = 1;
t[i + n].second.second = a;
t[i + n].first = c;
t[i + n].second.first = 0;
}
int now = 0;
sort(t, t + 2 * n);
for (int i = 0; i < 2 * n; i++) {
if (t[i].second.first)
now += t[i].second.second;
else
now -= t[i].second.second;
if (now > 150) {
cout << "NG" << endl;
goto end;
}
}
cout << "OK" << endl;
end:;
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p00231 | 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;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
int main() {
while (1) {
int N;
cin >> N;
vector<pair<int, int>> qs;
for (int i = 0; i < N; ++i) {
int m, a, b;
cin >> m >> a >> b;
qs.push_back(make_pair(a, m));
qs.push_back(make_pair(b, -m));
}
sort(qs.begin(), qs.end());
int nw = 0;
bool ok = true;
for (int i = 0; i < qs.size(); ++i) {
nw += qs[i].second;
if (nw > 150)
ok = false;
}
if (ok) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
vector<pair<int, int>> qs;
for (int i = 0; i < N; ++i) {
int m, a, b;
cin >> m >> a >> b;
qs.push_back(make_pair(a, m));
qs.push_back(make_pair(b, -m));
}
sort(qs.begin(), qs.end());
int nw = 0;
bool ok = true;
for (int i = 0; i < qs.size(); ++i) {
nw += qs[i].second;
if (nw > 150)
ok = false;
}
if (ok) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
return 0;
} | insert | 16 | 16 | 16 | 18 | TLE | |
p00231 | C++ | Runtime Error | #include <stdio.h>
#define W_LIM 150
int main(void) {
int n;
int curW;
int ev[127];
int evTime[127];
int evCnt;
int broken;
while (scanf("%d", &n), n) {
evCnt = 0;
// read events
for (int i = 0; i < n; i++) {
int m, a, b;
scanf("%d %d %d", &m, &a, &b);
ev[evCnt] = m;
evTime[evCnt] = a;
evCnt++;
ev[evCnt] = -m;
evTime[evCnt] = b;
evCnt++;
}
// sort eventTable
for (int i = 0; i < evCnt; i++) {
int min = i, minT = evTime[i], tmp;
for (int j = i + 1; j < evCnt; j++) {
if (evTime[j] < minT)
minT = evTime[min = j];
}
evTime[min] = evTime[i];
evTime[i] = minT;
tmp = ev[min];
ev[min] = ev[i];
ev[i] = tmp;
}
// simulate
curW = 0;
broken = 0;
for (int i = 0; i < evCnt; i++) {
curW += ev[i];
// printf("t:%d ev:%d curW:%d\n", evTime[i], ev[i],
//curW);
// ????????????????????????????????????????????????????????§???????????????
if (evTime[i + 1] == evTime[i])
continue;
if (curW > W_LIM)
broken = 1;
}
puts(broken ? "NG" : "OK");
// for (int i=0; i<evCnt; i++){
// printf("t:%d ev:%d\n", evTime[i], ev[i]);
// }
}
return 0;
} | #include <stdio.h>
#define W_LIM 150
int main(void) {
int n;
int curW;
int ev[256];
int evTime[256];
int evCnt;
int broken;
while (scanf("%d", &n), n) {
evCnt = 0;
// read events
for (int i = 0; i < n; i++) {
int m, a, b;
scanf("%d %d %d", &m, &a, &b);
ev[evCnt] = m;
evTime[evCnt] = a;
evCnt++;
ev[evCnt] = -m;
evTime[evCnt] = b;
evCnt++;
}
// sort eventTable
for (int i = 0; i < evCnt; i++) {
int min = i, minT = evTime[i], tmp;
for (int j = i + 1; j < evCnt; j++) {
if (evTime[j] < minT)
minT = evTime[min = j];
}
evTime[min] = evTime[i];
evTime[i] = minT;
tmp = ev[min];
ev[min] = ev[i];
ev[i] = tmp;
}
// simulate
curW = 0;
broken = 0;
for (int i = 0; i < evCnt; i++) {
curW += ev[i];
// printf("t:%d ev:%d curW:%d\n", evTime[i], ev[i],
//curW);
// ????????????????????????????????????????????????????????§???????????????
if (evTime[i + 1] == evTime[i])
continue;
if (curW > W_LIM)
broken = 1;
}
puts(broken ? "NG" : "OK");
// for (int i=0; i<evCnt; i++){
// printf("t:%d ev:%d\n", evTime[i], ev[i]);
// }
}
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p00232 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
#define INF 2147483600
int solve(int x, int y, int z) {
vector<int> v(x);
rep(i, x) cin >> v[i];
vector<int> e(y + 1, 0), a(y + 1);
rep(i, z) {
int p, q, r;
cin >> p >> q >> r;
e[p] = q;
a[p] = r;
}
vector<vector<double>> dp(y + 1, vector<double>(5005, 0));
dp[0][0] = 1;
rep(i, y) rep(j, 5005) if (dp[i][j] > 0) {
rep(k, x) {
int ni = min(i + v[k], y);
int nj = j;
if (e[ni] == 1)
ni += a[ni];
else if (e[ni] == 2)
nj += a[ni];
else if (e[ni] == 3)
nj = max(nj - a[ni], 0);
dp[ni][nj] += dp[i][j] / x;
}
}
double ret = 0;
rep(i, 5005) ret += dp[y][i] * i;
return (int)ret;
}
int main() {
int x, y, z;
while (cin >> x >> y >> z, x | y | z) {
cout << solve(x, y, z) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
#define INF 2147483600
int solve(int x, int y, int z) {
vector<int> v(x);
rep(i, x) cin >> v[i];
vector<int> e(y + 1, 0), a(y + 1);
rep(i, z) {
int p, q, r;
cin >> p >> q >> r;
e[p] = q;
a[p] = r;
}
vector<vector<double>> dp(y + 1, vector<double>(5005, 0));
dp[0][0] = 1;
rep(i, y) rep(j, 5005) if (dp[i][j] > 0) {
rep(k, x) {
int ni = min(i + v[k], y);
int nj = j;
if (e[ni] == 1)
ni = min(ni + a[ni], y);
else if (e[ni] == 2)
nj += a[ni];
else if (e[ni] == 3)
nj = max(nj - a[ni], 0);
dp[ni][nj] += dp[i][j] / x;
}
}
double ret = 0;
rep(i, 5005) ret += dp[y][i] * i;
return (int)ret;
}
int main() {
int x, y, z;
while (cin >> x >> y >> z, x | y | z) {
cout << solve(x, y, z) << endl;
}
return 0;
} | replace | 45 | 46 | 45 | 46 | 0 | |
p00232 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FOREACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
int main() {
ios::sync_with_stdio(false);
int x, y, z, v[4], n, e, a;
int dx[51], mp[51];
while (cin >> x >> y >> z && (x > 0 || y > 0 || z > 0)) {
REP(i, x) cin >> v[i];
REP(i, 51) {
dx[i] = 0;
mp[i] = 0;
}
REP(i, z) {
cin >> n >> e >> a;
if (e == 1)
dx[n] = a;
else if (e == 2)
mp[n] = a;
else if (e == 3)
mp[n] = -a;
}
double dp[51][5001];
REP(i, y + 1) REP(j, 100 * y + 1) dp[i][j] = 0.0;
dp[0][0] = 1.0;
REP(i, y) {
REP(j, 100 * y + 1) {
if (dp[i][j] == 0)
continue;
REP(k, x) {
if (i + v[k] >= y) {
dp[y][0] += dp[i][j] / x;
}
int money = j + mp[i + v[k]];
if (money < 0)
money = 0;
int pos = i + v[k] + dx[i + v[k]];
if (pos > y)
pos = y;
dp[pos][money] += dp[i][j] / x;
}
}
}
double ans = 0.0;
REP(i, 100 * y + 1) { ans += dp[y][i] * i; }
cout << (int)ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define FOREACH(i, n) \
for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
int main() {
ios::sync_with_stdio(false);
int x, y, z, v[4], n, e, a;
int dx[51], mp[51];
while (cin >> x >> y >> z && (x > 0 || y > 0 || z > 0)) {
REP(i, x) cin >> v[i];
REP(i, 51) {
dx[i] = 0;
mp[i] = 0;
}
REP(i, z) {
cin >> n >> e >> a;
if (e == 1)
dx[n] = a;
else if (e == 2)
mp[n] = a;
else if (e == 3)
mp[n] = -a;
}
double dp[51][5001];
REP(i, y + 1) REP(j, 100 * y + 1) dp[i][j] = 0.0;
dp[0][0] = 1.0;
REP(i, y) {
REP(j, 100 * y + 1) {
if (dp[i][j] == 0)
continue;
REP(k, x) {
if (i + v[k] >= y) {
dp[y][j] += dp[i][j] / x;
continue;
}
int money = j + mp[i + v[k]];
if (money < 0)
money = 0;
int pos = i + v[k] + dx[i + v[k]];
if (pos > y)
pos = y;
dp[pos][money] += dp[i][j] / x;
}
}
}
double ans = 0.0;
REP(i, 100 * y + 1) { ans += dp[y][i] * i; }
cout << (int)ans << endl;
}
return 0;
} | replace | 59 | 60 | 59 | 61 | 0 | |
p00232 | C++ | Runtime Error | // AOJ 0232
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility>
#define rep(i, a) for (int i = 0; i < (a); ++i)
typedef std::pair<int, int> P;
int X, Y, Z;
int V[4];
std::map<int, P> evnts;
double dp[50][5001][2];
double rec(int pos, int sum, bool eof) {
double &ret = dp[pos][sum][eof];
if (ret >= 0)
return ret;
ret = 0;
if (pos == Y)
return ret = sum;
if (evnts.count(pos) && !eof) {
int a = evnts[pos].second;
switch (evnts[pos].first) {
case 1:
return ret = rec(std::min(Y, pos + a), sum, true);
case 2:
return ret = rec(pos, sum + a, true);
case 3:
return ret = rec(pos, std::max(0, sum - a), true);
}
}
rep(i, X) ret += rec(std::min(Y, pos + V[i]), sum, false) / X;
return ret;
}
int main() {
while (scanf("%d%d%d", &X, &Y, &Z), X | Y | Z) {
evnts.clear();
rep(i, X) scanf("%d", V + i);
int sum = 0;
rep(i, Z) {
int N, E, A;
scanf("%d%d%d", &N, &E, &A);
evnts[N] = P(E, A);
if (E == 2)
sum += A;
}
rep(i, Y + 1) rep(j, sum + 1) rep(l, 2) dp[i][j][l] = -1.0;
printf("%d\n", (int)rec(0, 0, false));
}
return 0;
} | // AOJ 0232
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <utility>
#define rep(i, a) for (int i = 0; i < (a); ++i)
typedef std::pair<int, int> P;
int X, Y, Z;
int V[4];
std::map<int, P> evnts;
double dp[51][5001][2];
double rec(int pos, int sum, bool eof) {
double &ret = dp[pos][sum][eof];
if (ret >= 0)
return ret;
ret = 0;
if (pos == Y)
return ret = sum;
if (evnts.count(pos) && !eof) {
int a = evnts[pos].second;
switch (evnts[pos].first) {
case 1:
return ret = rec(std::min(Y, pos + a), sum, true);
case 2:
return ret = rec(pos, sum + a, true);
case 3:
return ret = rec(pos, std::max(0, sum - a), true);
}
}
rep(i, X) ret += rec(std::min(Y, pos + V[i]), sum, false) / X;
return ret;
}
int main() {
while (scanf("%d%d%d", &X, &Y, &Z), X | Y | Z) {
evnts.clear();
rep(i, X) scanf("%d", V + i);
int sum = 0;
rep(i, Z) {
int N, E, A;
scanf("%d%d%d", &N, &E, &A);
evnts[N] = P(E, A);
if (E == 2)
sum += A;
}
rep(i, Y + 1) rep(j, sum + 1) rep(l, 2) dp[i][j][l] = -1.0;
printf("%d\n", (int)rec(0, 0, false));
}
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p00232 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
struct Cell {
int e;
int a;
};
double solve(vector<Cell> &cel, int pos, vector<int> &vs, int money,
vector<map<int, double>> &mem) {
double retval = 0;
int vl = vs.size();
int cl = cel.size();
if (!mem[pos].empty() && (mem[pos].find(money) != mem[pos].end())) {
return mem[pos][money];
}
if (pos >= cl)
return money;
for (int i = 0; i < vl; ++i) {
int v = vs[i];
if (pos + v >= cl) {
retval += money;
continue;
}
Cell &cur = cel[pos + v];
if (cur.e == 0)
retval += solve(cel, pos + v, vs, money, mem);
else if (cur.e == 1)
retval += solve(cel, pos + v + cur.a, vs, money, mem);
else if (cur.e == 2)
retval += solve(cel, pos + v, vs, money + cur.a, mem);
else if (cur.e == 3)
retval +=
solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, mem);
}
mem[pos][money] = retval / vl;
return mem[pos][money];
}
int main() {
while (1) {
int x, y, z;
int i;
int vl;
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
break;
vector<int> vs(x);
vector<Cell> cells(y);
vector<map<int, double>> mem(y);
for (i = 0; i < x; ++i) {
cin >> vs[i];
}
for (i = 0; i < z; ++i) {
int n;
cin >> n;
cin >> cells[n].e;
cin >> cells[n].a;
}
cout << (int)solve(cells, 0, vs, 0, mem) << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
struct Cell {
int e;
int a;
};
double solve(vector<Cell> &cel, int pos, vector<int> &vs, int money,
vector<map<int, double>> &mem) {
double retval = 0;
int vl = vs.size();
int cl = cel.size();
if (!mem[pos].empty() && (mem[pos].find(money) != mem[pos].end())) {
return mem[pos][money];
}
if (pos >= cl)
return money;
for (int i = 0; i < vl; ++i) {
int v = vs[i];
if (pos + v >= cl) {
retval += money;
continue;
}
Cell &cur = cel[pos + v];
if (cur.e == 0)
retval += solve(cel, pos + v, vs, money, mem);
else if (cur.e == 1)
retval += solve(cel, pos + v + cur.a, vs, money, mem);
else if (cur.e == 2)
retval += solve(cel, pos + v, vs, money + cur.a, mem);
else if (cur.e == 3)
retval +=
solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, mem);
}
mem[pos][money] = retval / vl;
return mem[pos][money];
}
int main() {
while (1) {
int x, y, z;
int i;
int vl;
cin >> x >> y >> z;
if (x == 0 && y == 0 && z == 0)
break;
vector<int> vs(x);
vector<Cell> cells(y);
vector<map<int, double>> mem(y + 100);
for (i = 0; i < x; ++i) {
cin >> vs[i];
}
for (i = 0; i < z; ++i) {
int n;
cin >> n;
cin >> cells[n].e;
cin >> cells[n].a;
}
cout << (int)solve(cells, 0, vs, 0, mem) << endl;
}
} | replace | 54 | 55 | 54 | 55 | 0 | |
p00232 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;
double calc(int v, int m) {
if (v == Y)
return (double)m;
if (dp[v][m] > -1.)
return dp[v][m];
double sum = 0;
for (int i = 0; i < V.size(); i++) {
int tv = min(Y, v + V[i]);
int tm = max(0, m + gt[v]);
tv += pl[tv];
sum += calc(tv, tm);
}
return dp[v][m] = sum / V.size();
}
int main() {
while (scanf("%d %d %d", &X, &Y, &Z), X) {
V.resize(X);
pl = vector<int>(Y + 1, 0);
gt = vector<int>(Y + 1, 0);
dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));
for (int i = 0; i < X; i++) {
scanf("%d", &V[i]);
}
for (int i = 0; i < Z; i++) {
int N, E, A;
scanf("%d %d %d", &N, &E, &A);
if (E == 1)
pl[N] = A;
if (E == 2)
gt[N] = A;
if (E == 3)
gt[N] = -A;
}
printf("%d\n", (int)calc(0, 0));
}
} | #include <bits/stdc++.h>
using namespace std;
int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;
double calc(int v, int m) {
if (v == Y)
return (double)m;
if (dp[v][m] > -1.)
return dp[v][m];
double sum = 0;
for (int i = 0; i < V.size(); i++) {
int tv = min(Y, v + V[i]);
int tm = max(0, m + gt[tv]);
tv = min(Y, tv + pl[tv]);
sum += calc(tv, tm);
}
return dp[v][m] = sum / V.size();
}
int main() {
while (scanf("%d %d %d", &X, &Y, &Z), X) {
V.resize(X);
pl = vector<int>(Y + 1, 0);
gt = vector<int>(Y + 1, 0);
dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));
for (int i = 0; i < X; i++) {
scanf("%d", &V[i]);
}
for (int i = 0; i < Z; i++) {
int N, E, A;
scanf("%d %d %d", &N, &E, &A);
if (E == 1)
pl[N] = A;
if (E == 2)
gt[N] = A;
if (E == 3)
gt[N] = -A;
}
printf("%d\n", (int)calc(0, 0));
}
} | replace | 17 | 19 | 17 | 19 | 0 | |
p00232 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};
static const double EPS = 1e-8;
int roulette[4];
int X, Y, Z;
double dp[51][5001];
struct Data {
int ev;
int val;
};
int main() {
while (~scanf("%d %d %d", &X, &Y, &Z)) {
if (X == Y && Y == Z && Z == 0)
break;
for (int i = 0; i < X; i++) {
scanf("%d", roulette + i);
}
map<int, Data> stage;
for (int i = 0; i < Z; i++) {
int N, E, A;
scanf("%d %d %d", &N, &E, &A);
Data d = {E, A};
stage[N] = d;
}
fill((double *)dp, (double *)dp + 51 * 5001, 0.0);
dp[0][0] = 1.0;
for (int pos = 0; pos < Y; pos++) {
for (int coin = 0; coin <= Y * 100; coin++) {
for (int i = 0; i < X; i++) {
int next_pos = min(pos + roulette[i], Y);
int next_coin = coin;
if (next_pos < Y) {
if (stage[next_pos].ev == 1) {
next_pos = min(next_pos + stage[next_pos].val, Y);
} else if (stage[next_pos].ev == 2) {
next_coin += stage[next_pos].val;
} else if (stage[next_pos].ev == 3) {
next_coin -= stage[next_pos].val;
if (next_coin <= 0)
next_coin = 0;
}
}
next_pos = min(next_pos, Y);
dp[next_pos][next_coin] += dp[pos][coin] / (double)X;
}
}
}
double exp = 0.0;
for (int coin = 0; coin <= 5000; coin++) {
exp += coin * dp[Y][coin];
}
printf("%d\n", (int)(exp));
}
} | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};
static const double EPS = 1e-8;
int roulette[4];
int X, Y, Z;
double dp[51][5001];
struct Data {
int ev;
int val;
};
int main() {
while (~scanf("%d %d %d", &X, &Y, &Z)) {
if (X == Y && Y == Z && Z == 0)
break;
for (int i = 0; i < X; i++) {
scanf("%d", roulette + i);
}
map<int, Data> stage;
for (int i = 0; i < Z; i++) {
int N, E, A;
scanf("%d %d %d", &N, &E, &A);
Data d = {E, A};
stage[N] = d;
}
fill((double *)dp, (double *)dp + 51 * 5001, 0.0);
dp[0][0] = 1.0;
for (int pos = 0; pos < Y; pos++) {
for (int coin = 0; coin <= Y * 100; coin++) {
if (dp[pos][coin] == 0)
continue;
for (int i = 0; i < X; i++) {
int next_pos = min(pos + roulette[i], Y);
int next_coin = coin;
if (next_pos < Y) {
if (stage[next_pos].ev == 1) {
next_pos = min(next_pos + stage[next_pos].val, Y);
} else if (stage[next_pos].ev == 2) {
next_coin += stage[next_pos].val;
} else if (stage[next_pos].ev == 3) {
next_coin -= stage[next_pos].val;
if (next_coin <= 0)
next_coin = 0;
}
}
next_pos = min(next_pos, Y);
dp[next_pos][next_coin] += dp[pos][coin] / (double)X;
}
}
}
double exp = 0.0;
for (int coin = 0; coin <= 5000; coin++) {
exp += coin * dp[Y][coin];
}
printf("%d\n", (int)(exp));
}
} | insert | 62 | 62 | 62 | 64 | TLE | |
p00232 | C++ | Memory Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
vector<int> dices;
struct masu {
int num;
int type;
int a;
};
map<int, masu> mp;
int X, Y, Z;
vector<vector<ld>> memo;
ld getans(const int nowplace, const int nowmoney) {
if (memo[nowplace][nowmoney] < -0.5) {
ld ans = 0;
for (auto di : dices) {
int nextmoney(nowmoney);
int next = nowplace + di;
if (next < Y) {
if (mp.find(next) != mp.end()) {
auto it = mp.find(next);
masu action(it->second);
if (action.type == 1) {
next += action.a;
} else if (action.type == 2) {
nextmoney += action.a;
} else {
nextmoney -= action.a;
nextmoney = max(nextmoney, 0);
}
}
}
if (next < Y) {
ans += getans(next, nextmoney);
} else {
ans += nextmoney;
}
}
memo[nowplace][nowmoney] = ans / dices.size();
}
return memo[nowplace][nowmoney];
}
int main() {
while (1) {
cin >> X >> Y >> Z;
if (!X)
break;
memo = vector<vector<ld>>(Y, vector<ld>(100000, -1));
dices.clear();
for (int i = 0; i < X; ++i) {
int v;
cin >> v;
dices.emplace_back(v);
}
mp.clear();
for (int i = 0; i < Z; ++i) {
int n, e, a;
cin >> n >> e >> a;
mp[n] = masu{n, e, a};
}
ld ans = getans(0, 0);
cout << int(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;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
vector<int> dices;
struct masu {
int num;
int type;
int a;
};
map<int, masu> mp;
int X, Y, Z;
vector<vector<ld>> memo;
ld getans(const int nowplace, const int nowmoney) {
if (memo[nowplace][nowmoney] < -0.5) {
ld ans = 0;
for (auto di : dices) {
int nextmoney(nowmoney);
int next = nowplace + di;
if (next < Y) {
if (mp.find(next) != mp.end()) {
auto it = mp.find(next);
masu action(it->second);
if (action.type == 1) {
next += action.a;
} else if (action.type == 2) {
nextmoney += action.a;
} else {
nextmoney -= action.a;
nextmoney = max(nextmoney, 0);
}
}
}
if (next < Y) {
ans += getans(next, nextmoney);
} else {
ans += nextmoney;
}
}
memo[nowplace][nowmoney] = ans / dices.size();
}
return memo[nowplace][nowmoney];
}
int main() {
while (1) {
cin >> X >> Y >> Z;
if (!X)
break;
memo = vector<vector<ld>>(Y, vector<ld>(10000, -1));
dices.clear();
for (int i = 0; i < X; ++i) {
int v;
cin >> v;
dices.emplace_back(v);
}
mp.clear();
for (int i = 0; i < Z; ++i) {
int n, e, a;
cin >> n >> e >> a;
mp[n] = masu{n, e, a};
}
ld ans = getans(0, 0);
cout << int(ans) << endl;
}
return 0;
} | replace | 58 | 59 | 58 | 59 | MLE | |
p00232 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
int X;
int V[4];
int Y, Z;
int T[55], E[55];
double dp[55][5010];
signed main() {
while (cin >> X >> Y >> Z, X || Y || Z) {
memset(T, 0, sizeof(T));
memset(dp, 0, sizeof(dp));
rep(i, X) cin >> V[i];
rep(i, Z) {
int a;
cin >> a;
cin >> T[a] >> E[a];
}
double ans = 0;
dp[0][0] = 1;
rep(i, Y) {
rep(j, i * 100 + 1) dp[Y][j] = 0;
for (int j = Y - 1; j >= 0; j--) {
rep(k, i * 100 + 1) {
rep(l, X) {
int t = j + V[l];
int c = k;
chmin(t, Y);
if (T[t] == 1) {
t += E[t];
chmin(t, Y);
} else if (T[t] == 2) {
c += E[t];
} else if (T[t] == 3) {
c -= E[t];
if (c < 0)
c = 0;
}
dp[t][c] += dp[j][k] / X;
}
dp[j][k] = 0;
}
}
rep(j, i * 100 + 101) ans += dp[Y][j] * j;
}
printf("%lld\n", (int)ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
if (a > b)
a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
if (a < b)
a = b;
}
int X;
int V[4];
int Y, Z;
int T[55], E[55];
double dp[55][5010];
signed main() {
while (cin >> X >> Y >> Z, X || Y || Z) {
memset(T, 0, sizeof(T));
memset(dp, 0, sizeof(dp));
rep(i, X) cin >> V[i];
rep(i, Z) {
int a;
cin >> a;
cin >> T[a] >> E[a];
}
double ans = 0;
dp[0][0] = 1;
rep(i, Y) {
rep(j, i * 100 + 1) dp[Y][j] = 0;
for (int j = Y - 1; j >= i; j--) {
rep(k, i * 100 + 1) {
rep(l, X) {
int t = j + V[l];
int c = k;
chmin(t, Y);
if (T[t] == 1) {
t += E[t];
chmin(t, Y);
} else if (T[t] == 2) {
c += E[t];
} else if (T[t] == 3) {
c -= E[t];
if (c < 0)
c = 0;
}
dp[t][c] += dp[j][k] / X;
}
dp[j][k] = 0;
}
}
rep(j, i * 100 + 101) ans += dp[Y][j] * j;
}
printf("%lld\n", (int)ans);
}
return 0;
} | replace | 48 | 49 | 48 | 49 | TLE | |
p00232 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
double ans, dp[51][3000];
int X, Y, Z;
int dice[4], t[51], u[51], N;
void init();
double dfs(int, int);
int main() {
while (cin >> X >> Y >> Z && X) {
init();
for (int i = 0; i < X; i++)
cin >> dice[i];
for (int i = 0; i < Z; i++) {
cin >> N;
cin >> t[N] >> u[N];
}
cout << (int)dfs(0, 0) << endl;
}
return 0;
}
void init() {
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 3000; j++)
dp[i][j] = -1.0;
t[i] = u[i] = 0;
}
}
double dfs(int pos, int cnt) {
if (pos >= Y)
return (double)cnt;
if (dp[pos][cnt] != -1.0)
return dp[pos][cnt];
double res = 0;
for (int i = 0; i < X; i++) {
int npos = pos + dice[i];
if (t[npos] == 1)
res += dfs(npos + u[npos], cnt);
else if (t[npos] == 2)
res += dfs(npos, cnt + u[npos]);
else
res += dfs(npos, max(0, cnt - u[npos]));
}
return res / (double)X;
} | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
double ans, dp[51][3000];
int X, Y, Z;
int dice[4], t[51], u[51], N;
void init();
double dfs(int, int);
int main() {
while (cin >> X >> Y >> Z && X) {
init();
for (int i = 0; i < X; i++)
cin >> dice[i];
for (int i = 0; i < Z; i++) {
cin >> N;
cin >> t[N] >> u[N];
}
cout << (int)dfs(0, 0) << endl;
}
return 0;
}
void init() {
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 3000; j++)
dp[i][j] = -1.0;
t[i] = u[i] = 0;
}
}
double dfs(int pos, int cnt) {
if (pos >= Y)
return (double)cnt;
if (dp[pos][cnt] != -1.0)
return dp[pos][cnt];
double res = 0;
for (int i = 0; i < X; i++) {
int npos = pos + dice[i];
if (t[npos] == 1)
res += dfs(npos + u[npos], cnt);
else if (t[npos] == 2)
res += dfs(npos, cnt + u[npos]);
else
res += dfs(npos, max(0, cnt - u[npos]));
}
return dp[pos][cnt] = res / (double)X;
} | replace | 48 | 49 | 48 | 49 | TLE | |
p00232 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define MONEY_MAX 4901
typedef pair<int, int> pii;
int main() {
while (1) {
int X, Y, Z;
cin >> X >> Y >> Z;
if (X == 0 && Y == 0 && Z == 0) {
break;
} else {
int V[X];
for (int i = 0; i < X; i++)
cin >> V[i];
pii event[Y + 1];
fill(&event[0].first, &event[Y + 1].first, -1);
for (int i = 0; i < Z; i++) {
int N;
cin >> N;
cin >> event[N].first >> event[N].second;
}
double dp[Y + 1][MONEY_MAX];
fill(&dp[0][0], &dp[Y + 1][0], 0.0);
dp[0][0] = 1.0;
double ans = 0;
for (int i = 0; i < Y; i++) {
for (int j = 0; j < MONEY_MAX; j++) {
if (dp[i][j] == 0)
continue;
for (int k = 0; k < X; k++) {
cout << ans << endl;
if (i + V[k] >= Y) {
ans += dp[i][j] * j / (double)X;
} else if (event[i + V[k]].first == 1) {
if (i + V[k] + event[i + V[k]].second >= Y)
ans += dp[i][j] * j / (double)X;
else
dp[i + V[k] + event[i + V[k]].second][j] +=
dp[i][j] / (double)X;
} else if (event[i + V[k]].first == 2) {
dp[i + V[k]][j + event[i + V[k]].second] += dp[i][j] / (double)X;
} else if (event[i + V[k]].first == 3) {
if (j <= event[i + V[k]].second)
dp[i + V[k]][0] += dp[i][j] / (double)X;
else
dp[i + V[k]][j - event[i + V[k]].second] +=
dp[i][j] / (double)X;
} else {
dp[i + V[k]][j] += dp[i][j] / (double)X;
}
}
}
}
cout << (int)ans << endl;
}
}
} | #include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define MONEY_MAX 4901
typedef pair<int, int> pii;
int main() {
while (1) {
int X, Y, Z;
cin >> X >> Y >> Z;
if (X == 0 && Y == 0 && Z == 0) {
break;
} else {
int V[X];
for (int i = 0; i < X; i++)
cin >> V[i];
pii event[Y + 1];
fill(&event[0].first, &event[Y + 1].first, -1);
for (int i = 0; i < Z; i++) {
int N;
cin >> N;
cin >> event[N].first >> event[N].second;
}
double dp[Y + 1][MONEY_MAX];
fill(&dp[0][0], &dp[Y + 1][0], 0.0);
dp[0][0] = 1.0;
double ans = 0;
for (int i = 0; i < Y; i++) {
for (int j = 0; j < MONEY_MAX; j++) {
if (dp[i][j] == 0)
continue;
for (int k = 0; k < X; k++) {
if (i + V[k] >= Y) {
ans += dp[i][j] * j / (double)X;
} else if (event[i + V[k]].first == 1) {
if (i + V[k] + event[i + V[k]].second >= Y)
ans += dp[i][j] * j / (double)X;
else
dp[i + V[k] + event[i + V[k]].second][j] +=
dp[i][j] / (double)X;
} else if (event[i + V[k]].first == 2) {
dp[i + V[k]][j + event[i + V[k]].second] += dp[i][j] / (double)X;
} else if (event[i + V[k]].first == 3) {
if (j <= event[i + V[k]].second)
dp[i + V[k]][0] += dp[i][j] / (double)X;
else
dp[i + V[k]][j - event[i + V[k]].second] +=
dp[i][j] / (double)X;
} else {
dp[i + V[k]][j] += dp[i][j] / (double)X;
}
}
}
}
cout << (int)ans << endl;
}
}
} | delete | 35 | 36 | 35 | 35 | TLE | |
p00234 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct S {
int x, y, o, s, t;
S(int x, int y, int o, int s, int t) : x(x), y(y), o(o), s(s), t(t) {}
bool operator<(const S &st) const {
if (t != st.t)
return t > st.t; // reverse
if (x != st.x)
return x < st.x;
if (y != st.y)
return y < st.y;
if (o != st.o)
return o < st.o;
return s < st.s;
}
};
int W, H;
int F, M, O;
int A[10][10];
bool update(S s, int dist[10][10][51][1 << 10]) {
assert(valid(s.x, s.y, W, H));
assert(s.o >= 0 && s.o <= 50);
assert(s.s >= 0 && s.s < (1 << W));
int &d = dist[s.y][s.x][s.o][s.s];
if (d > s.t) {
d = s.t;
return true;
} else {
return false;
}
}
void move(S &s, int dx) {
s.x += dx;
s.o--;
if (dx == 0) {
s.s = 0;
s.y++;
}
if (!(s.s >> s.x & 1)) {
s.s |= 1 << s.x;
assert(valid(s.x, s.y, W, H));
if (A[s.y][s.x] < 0) {
s.t += abs(A[s.y][s.x]);
} else {
s.o = min(s.o + abs(A[s.y][s.x]), M);
}
}
}
void print(S s) {
printf("x = %d y = %d o = %d s = %04X t = %d\n", s.x, s.y, s.o, s.s, s.t);
}
int main() {
while (cin >> W >> H && W) {
cin >> F >> M >> O;
REP(y, H) REP(x, W) cin >> A[y][x];
static int dist[10][10][51][1 << 10];
REP(y, H) REP(x, W) REP(o, M + 1) REP(s, 1 << W) dist[y][x][o][s] = INF;
priority_queue<S> que;
REP(sx, W) {
S start(sx, -1, O, 0, 0);
move(start, 0);
update(start, dist);
que.push(start);
}
int ans = -1;
while (!que.empty()) {
S s = que.top();
que.pop();
// print(s);
if (s.y == H - 1) {
ans = s.t;
break;
}
if (dist[s.y][s.x][s.o][s.s] < s.t)
continue;
if (s.o <= 1)
continue;
for (int dx = -1; dx <= 1; dx++) {
if (!(s.x + dx >= 0 && s.x + dx < W))
continue;
S next = s;
move(next, dx);
if (update(next, dist)) {
que.push(next);
}
}
}
if (ans == -1 || ans > F)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct S {
int x, y, o, s, t;
S(int x, int y, int o, int s, int t) : x(x), y(y), o(o), s(s), t(t) {}
bool operator<(const S &st) const {
if (t != st.t)
return t > st.t; // reverse
if (x != st.x)
return x < st.x;
if (y != st.y)
return y < st.y;
if (o != st.o)
return o < st.o;
return s < st.s;
}
};
int W, H;
int F, M, O;
int A[10][10];
bool update(S s, int dist[10][10][51][1 << 10]) {
assert(valid(s.x, s.y, W, H));
assert(s.o >= 0 && s.o <= 50);
assert(s.s >= 0 && s.s < (1 << W));
int &d = dist[s.y][s.x][s.o][s.s];
if (d > s.t) {
d = s.t;
return true;
} else {
return false;
}
}
void move(S &s, int dx) {
s.x += dx;
s.o--;
if (dx == 0) {
s.s = 0;
s.y++;
}
if (!(s.s >> s.x & 1)) {
s.s |= 1 << s.x;
assert(valid(s.x, s.y, W, H));
if (A[s.y][s.x] < 0) {
s.t += abs(A[s.y][s.x]);
} else {
s.o = min(s.o + abs(A[s.y][s.x]), M);
}
}
}
void print(S s) {
printf("x = %d y = %d o = %d s = %04X t = %d\n", s.x, s.y, s.o, s.s, s.t);
}
int main() {
while (cin >> W >> H && W) {
cin >> F >> M >> O;
REP(y, H) REP(x, W) cin >> A[y][x];
static int dist[10][10][51][1 << 10];
REP(y, H) REP(x, W) REP(o, M + 1) REP(s, 1 << W) dist[y][x][o][s] = INF;
if (O < 2) {
cout << "NA" << endl;
continue;
}
priority_queue<S> que;
REP(sx, W) {
S start(sx, -1, O, 0, 0);
move(start, 0);
update(start, dist);
que.push(start);
}
int ans = -1;
while (!que.empty()) {
S s = que.top();
que.pop();
// print(s);
if (s.y == H - 1) {
ans = s.t;
break;
}
if (dist[s.y][s.x][s.o][s.s] < s.t)
continue;
if (s.o <= 1)
continue;
for (int dx = -1; dx <= 1; dx++) {
if (!(s.x + dx >= 0 && s.x + dx < W))
continue;
S next = s;
move(next, dx);
if (update(next, dist)) {
que.push(next);
}
}
}
if (ans == -1 || ans > F)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | insert | 96 | 96 | 96 | 100 | 0 | |
p00234 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int g[12][12][60][12][12]; // y, x, air, l, r
int event[12][12];
int w, h, f, m, o;
int dx[] = {1, 0, -1}, dy[] = {0, 1, 0};
typedef struct state {
int cost, air, x, y, l, r;
bool operator<(const state &a) const { return cost < a.cost; }
bool operator>(const state &a) const { return cost > a.cost; }
} state;
bool range(int y, int x) { return (0 <= x and x < w and 0 <= y and y < h); }
void solve() {
priority_queue<state, vector<state>, greater<state>> que;
for (int i = 0; i < w; i++) {
if (event[0][i] < 0 and m >= 1) {
g[0][i][m - 1][i][i] = -event[0][i];
state tmp = (state){-event[0][i], m - 1, i, 0, i, i};
que.push(tmp);
} else {
int air = min(o, m + event[0][i] - 1);
g[0][i][air][i][i] = 0;
state tmp = (state){0, air, i, 0, i, i};
que.push(tmp);
}
}
while (not que.empty()) {
state tmp = que.top();
que.pop();
int l = tmp.l, r = tmp.r;
int cost = tmp.cost;
int air = tmp.air;
int x = tmp.x, y = tmp.y;
if (air <= 1)
continue;
for (int i = 0; i < 3; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (not range(ny, nx))
continue;
int nl = min(l, nx), nr = max(r, nx);
if (y != ny) {
nl = nx;
nr = nx;
}
if (event[ny][nx] < 0) {
int add = -event[ny][nx];
if (l <= nx and nx <= r and y == ny)
add = 0;
if (g[ny][nx][air - 1][nl][nr] > cost + add) {
g[ny][nx][air - 1][nl][nr] = cost + add;
int c = cost + add;
tmp = (state){c, air - 1, nx, ny, nl, nr};
que.push(tmp);
}
} else {
int nair = min(air - 1 + event[ny][nx], o);
if (l <= nx and nx <= r and y == ny)
nair = air - 1;
if (g[ny][nx][nair][nl][nr] > cost) {
g[ny][nx][nair][nl][nr] = cost;
tmp = (state){cost, nair, nx, ny, nl, nr};
que.push(tmp);
}
}
}
}
}
void init() {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
for (int k = 0; k <= o; k++)
for (int l = 0; l < w; l++)
for (int m = 0; m < w; m++)
g[i][j][k][l][m] = 1e9;
}
int main() {
while (std::cin >> w >> h, w) {
std::cin >> f >> o >> m;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
std::cin >> event[i][j];
}
}
init();
solve();
int ans = 1e9;
for (int i = 0; i < w; i++) {
for (int j = 1; j <= o; j++) {
for (int k = 0; k < w; k++) {
for (int l = 0; l < w; l++) {
ans = min(ans, g[h - 1][i][j][k][l]);
}
}
}
}
if (ans > f)
std::cout << "NA" << std::endl;
else
std::cout << ans << std::endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int g[12][12][60][12][12]; // y, x, air, l, r
int event[12][12];
int w, h, f, m, o;
int dx[] = {1, 0, -1}, dy[] = {0, 1, 0};
typedef struct state {
int cost, air, x, y, l, r;
bool operator<(const state &a) const { return cost < a.cost; }
bool operator>(const state &a) const { return cost > a.cost; }
} state;
bool range(int y, int x) { return (0 <= x and x < w and 0 <= y and y < h); }
void solve() {
priority_queue<state, vector<state>, greater<state>> que;
for (int i = 0; i < w; i++) {
if (m <= 1)
continue;
if (event[0][i] < 0) {
g[0][i][m - 1][i][i] = -event[0][i];
state tmp = (state){-event[0][i], m - 1, i, 0, i, i};
que.push(tmp);
} else {
int air = min(o, m + event[0][i] - 1);
g[0][i][air][i][i] = 0;
state tmp = (state){0, air, i, 0, i, i};
que.push(tmp);
}
}
while (not que.empty()) {
state tmp = que.top();
que.pop();
int l = tmp.l, r = tmp.r;
int cost = tmp.cost;
int air = tmp.air;
int x = tmp.x, y = tmp.y;
if (air <= 1)
continue;
for (int i = 0; i < 3; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (not range(ny, nx))
continue;
int nl = min(l, nx), nr = max(r, nx);
if (y != ny) {
nl = nx;
nr = nx;
}
if (event[ny][nx] < 0) {
int add = -event[ny][nx];
if (l <= nx and nx <= r and y == ny)
add = 0;
if (g[ny][nx][air - 1][nl][nr] > cost + add) {
g[ny][nx][air - 1][nl][nr] = cost + add;
int c = cost + add;
tmp = (state){c, air - 1, nx, ny, nl, nr};
que.push(tmp);
}
} else {
int nair = min(air - 1 + event[ny][nx], o);
if (l <= nx and nx <= r and y == ny)
nair = air - 1;
if (g[ny][nx][nair][nl][nr] > cost) {
g[ny][nx][nair][nl][nr] = cost;
tmp = (state){cost, nair, nx, ny, nl, nr};
que.push(tmp);
}
}
}
}
}
void init() {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
for (int k = 0; k <= o; k++)
for (int l = 0; l < w; l++)
for (int m = 0; m < w; m++)
g[i][j][k][l][m] = 1e9;
}
int main() {
while (std::cin >> w >> h, w) {
std::cin >> f >> o >> m;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
std::cin >> event[i][j];
}
}
init();
solve();
int ans = 1e9;
for (int i = 0; i < w; i++) {
for (int j = 1; j <= o; j++) {
for (int k = 0; k < w; k++) {
for (int l = 0; l < w; l++) {
ans = min(ans, g[h - 1][i][j][k][l]);
}
}
}
}
if (ans > f)
std::cout << "NA" << std::endl;
else
std::cout << ans << std::endl;
}
return 0;
} | replace | 18 | 19 | 18 | 21 | 0 | |
p00234 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 11][11][51];
void dfs(int x, int bit, int y, int air, int c) {
if (air <= 0)
return;
if (!((bit >> x) & 1)) {
if (fie[x][y] > 0)
air = min(air + fie[x][y], m);
if (fie[x][y] < 0)
c += abs(fie[x][y]);
bit |= 1 << x;
}
if (c > f)
return;
if (cost[x][bit][y][air] <= c)
return;
cost[x][bit][y][air] = c;
if (y == h - 1)
ans = min(ans, c);
else {
dfs(x, 0, y + 1, air - 1, c);
if (x - 1 >= 0)
dfs(x - 1, bit, y, air - 1, c);
if (x + 1 < w)
dfs(x + 1, bit, y, air - 1, c);
}
}
int main(void) {
while (1) {
ans = INF;
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
for (int k = 0; k <= 50; k++) {
cost[i][bit][j][k] = INF;
}
}
}
}
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
scanf("%d%d%d", &f, &m, &o);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &fie[j][i]);
}
}
for (int i = 0; i < w; i++) {
dfs(i, 0, 0, o - 1, 0);
}
if (ans == INF)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 10][11][51];
void dfs(int x, int bit, int y, int air, int c) {
if (air <= 0)
return;
if (!((bit >> x) & 1)) {
if (fie[x][y] > 0)
air = min(air + fie[x][y], m);
if (fie[x][y] < 0)
c += abs(fie[x][y]);
bit |= 1 << x;
}
if (c > f)
return;
if (cost[x][bit][y][air] <= c)
return;
cost[x][bit][y][air] = c;
if (y == h - 1)
ans = min(ans, c);
else {
dfs(x, 0, y + 1, air - 1, c);
if (x - 1 >= 0)
dfs(x - 1, bit, y, air - 1, c);
if (x + 1 < w)
dfs(x + 1, bit, y, air - 1, c);
}
}
int main(void) {
while (1) {
ans = INF;
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
for (int k = 0; k <= 50; k++) {
cost[i][bit][j][k] = INF;
}
}
}
}
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
scanf("%d%d%d", &f, &m, &o);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &fie[j][i]);
}
}
for (int i = 0; i < w; i++) {
dfs(i, 0, 0, o - 1, 0);
}
if (ans == INF)
cout << "NA" << endl;
else
cout << ans << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 18 | MLE | |
p00235 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph(20);
int bridges[20][20];
bool used[20][20];
int calc_sum(int from) {
int sum = 0;
for (int i = 0; i < graph[from].size(); ++i) {
int to = graph[from][i];
if (graph[to].size() == 1)
continue;
if (used[from][to])
continue;
used[to][from] = true;
sum += bridges[from][to] * 2 + calc_sum(to);
}
return sum;
}
int calc_max(int from) {
int ma = 0;
for (int i = 0; i < graph[from].size(); ++i) {
int to = graph[from][i];
if (graph[to].size() == 1)
continue;
if (used[from][to])
continue;
used[to][from] = true;
ma = max(ma, bridges[from][to] + calc_max(to));
}
return ma;
}
int main() {
int n;
while (cin >> n, n) {
memset(bridges, 0, sizeof(bridges));
graph.clear();
for (int i = 0; i < n - 1; ++i) {
int a, b, t;
cin >> a >> b >> t;
a--;
b--;
bridges[a][b] = bridges[b][a] = t;
graph[a].push_back(b);
graph[b].push_back(a);
}
memset(used, 0, sizeof(used));
int sum = calc_sum(0);
memset(used, 0, sizeof(used));
int ma = calc_max(0);
cout << sum - ma << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph(20);
int bridges[20][20];
bool used[20][20];
int calc_sum(int from) {
int sum = 0;
for (int i = 0; i < graph[from].size(); ++i) {
int to = graph[from][i];
if (graph[to].size() == 1)
continue;
if (used[from][to])
continue;
used[to][from] = true;
sum += bridges[from][to] * 2 + calc_sum(to);
}
return sum;
}
int calc_max(int from) {
int ma = 0;
for (int i = 0; i < graph[from].size(); ++i) {
int to = graph[from][i];
if (graph[to].size() == 1)
continue;
if (used[from][to])
continue;
used[to][from] = true;
ma = max(ma, bridges[from][to] + calc_max(to));
}
return ma;
}
int main() {
int n;
while (cin >> n, n) {
memset(bridges, 0, sizeof(bridges));
for (int i = 0; i < 20; ++i)
graph[i].clear();
for (int i = 0; i < n - 1; ++i) {
int a, b, t;
cin >> a >> b >> t;
a--;
b--;
bridges[a][b] = bridges[b][a] = t;
graph[a].push_back(b);
graph[b].push_back(a);
}
memset(used, 0, sizeof(used));
int sum = calc_sum(0);
memset(used, 0, sizeof(used));
int ma = calc_max(0);
cout << sum - ma << endl;
}
} | replace | 44 | 45 | 44 | 46 | 0 | |
p00235 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1001001001;
struct Edge {
Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
int src, dst, cost;
};
vector<Edge> G[20];
int cost[20];
int f(int now, int pre, int dist) {
cost[now] = dist;
int sum = 0;
rep(i, G[now].size()) {
Edge next = G[now][i];
if (next.dst != pre && G[next.dst].size() > 1)
sum += f(next.dst, now, dist + next.cost) + next.cost;
}
return sum;
}
int main() {
int n;
while (scanf("%d", &n), n) {
fill(cost, cost + n, 0);
rep(i, n - 1) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
--a;
--b;
G[a].push_back(Edge(a, b, c));
G[b].push_back(Edge(b, a, c));
}
int sum = f(0, -1, 0);
printf("%d\n", sum * 2 - *max_element(cost, cost + n));
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1001001001;
struct Edge {
Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
int src, dst, cost;
};
vector<Edge> G[20];
int cost[20];
int f(int now, int pre, int dist) {
cost[now] = dist;
int sum = 0;
rep(i, G[now].size()) {
Edge next = G[now][i];
if (next.dst != pre && G[next.dst].size() > 1)
sum += f(next.dst, now, dist + next.cost) + next.cost;
}
return sum;
}
int main() {
int n;
while (scanf("%d", &n), n) {
fill(cost, cost + n, 0);
rep(i, n) G[i].clear();
rep(i, n - 1) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
--a;
--b;
G[a].push_back(Edge(a, b, c));
G[b].push_back(Edge(b, a, c));
}
int sum = f(0, -1, 0);
printf("%d\n", sum * 2 - *max_element(cost, cost + n));
}
return 0;
} | insert | 51 | 51 | 51 | 52 | 0 | |
p00235 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;
bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
MAX = max(MAX, b);
S[a] = true;
for (P i : rinsetu[a]) {
if (S[i.second] == false && X[i.second]) {
saiki(i.second, b + i.first);
}
}
S[b] = false;
}
signed main() {
int a;
while (cin >> a, a) {
memset(X, true, sizeof(X));
int sum = 0;
for (int b = 0; b < a; b++) {
rinsetu[b].clear();
}
for (int b = 1; b < a; b++) {
int c, d, e;
cin >> c >> d >> e;
c--;
d--;
rinsetu[c].push_back(P(e, d));
rinsetu[d].push_back(P(e, c));
sum += e * 2;
}
for (int i = 1; i < a; i++) {
if (rinsetu[i].size() == 1) {
sum -= rinsetu[i][0].first * 2;
X[i] = false;
}
}
memset(S, false, sizeof(S));
MAX = 0;
saiki(0, 0);
cout << sum - MAX << endl;
}
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;
bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
MAX = max(MAX, b);
S[a] = true;
for (P i : rinsetu[a]) {
if (S[i.second] == false && X[i.second]) {
saiki(i.second, b + i.first);
}
}
S[a] = false;
}
signed main() {
int a;
while (cin >> a, a) {
memset(X, true, sizeof(X));
int sum = 0;
for (int b = 0; b < a; b++) {
rinsetu[b].clear();
}
for (int b = 1; b < a; b++) {
int c, d, e;
cin >> c >> d >> e;
c--;
d--;
rinsetu[c].push_back(P(e, d));
rinsetu[d].push_back(P(e, c));
sum += e * 2;
}
for (int i = 1; i < a; i++) {
if (rinsetu[i].size() == 1) {
sum -= rinsetu[i][0].first * 2;
X[i] = false;
}
}
memset(S, false, sizeof(S));
MAX = 0;
saiki(0, 0);
cout << sum - MAX << endl;
}
} | replace | 26 | 27 | 26 | 27 | 0 | |
p00235 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 30;
vector<P> G[N];
int dp[N][2];
int dfs(int v, int p, int back) {
int &res = dp[v][back];
if (res >= 0)
return res;
if (G[v].size() == 1 && G[v][0].first == p)
return -1;
if (back) {
res = 0;
for (auto &e : G[v]) {
if (e.first == p)
continue;
int ret = dfs(e.first, v, 1);
if (ret < 0)
continue;
res += ret + e.second * 2;
}
return res;
}
int sum = dfs(v, p, true);
res = sum;
for (auto &e : G[v]) {
if (e.first == p)
continue;
int ret = dfs(e.first, v, 1);
if (ret < 0)
continue;
int ret2 = dfs(e.first, v, 0);
res = min(res, sum - e.second - ret + ret2);
}
return res;
}
int main() {
int n;
while (cin >> n, n) {
MEMSET(dp, -1);
rep(i, n - 1) {
int a, b, t;
cin >> a >> b >> t;
--a, --b;
G[a].push_back(make_pair(b, t));
G[b].push_back(make_pair(a, t));
}
cout << dfs(0, -1, 0) << endl;
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 30;
vector<P> G[N];
int dp[N][2];
int dfs(int v, int p, int back) {
int &res = dp[v][back];
if (res >= 0)
return res;
if (G[v].size() == 1 && G[v][0].first == p)
return -1;
if (back) {
res = 0;
for (auto &e : G[v]) {
if (e.first == p)
continue;
int ret = dfs(e.first, v, 1);
if (ret < 0)
continue;
res += ret + e.second * 2;
}
return res;
}
int sum = dfs(v, p, true);
res = sum;
for (auto &e : G[v]) {
if (e.first == p)
continue;
int ret = dfs(e.first, v, 1);
if (ret < 0)
continue;
int ret2 = dfs(e.first, v, 0);
res = min(res, sum - e.second - ret + ret2);
}
return res;
}
int main() {
int n;
while (cin >> n, n) {
rep(i, n) G[i].clear();
MEMSET(dp, -1);
rep(i, n - 1) {
int a, b, t;
cin >> a >> b >> t;
--a, --b;
G[a].push_back(make_pair(b, t));
G[b].push_back(make_pair(a, t));
}
cout << dfs(0, -1, 0) << endl;
}
return 0;
} | insert | 76 | 76 | 76 | 77 | 0 | |
p00236 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int N = 49;
int w, h;
vector<vector<int>> field;
int size;
vector<vector<int>> vertex;
vector<vector<int>> edge;
bitset<N> visited;
set<unsigned long long> memo;
void make_graph() {
enum { Y, X };
const int NEXT[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
// const int NEXT[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int NIL = -1;
size = 0;
vertex = vector<vector<int>>(h, vector<int>(w, NIL));
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
if (!field[y][x])
vertex[y][x] = size++;
edge = vector<vector<int>>(h * w, vector<int>());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
if (vertex[y][x] == NIL)
continue;
for (const auto &d : NEXT) {
int ny = y + d[Y];
int nx = x + d[X];
if (ny < 0 || h <= ny || nx < 0 || w <= nx)
continue;
if (vertex[ny][nx] == NIL)
continue;
edge[vertex[y][x]].push_back(vertex[ny][nx]);
}
}
};
void reachability(const int &from, bitset<N> &visited) {
visited.set(from);
for (const auto &to : edge[from])
if (!visited[to])
reachability(to, visited);
}
bool cut_vertex(const int &from) {
auto dup = visited;
dup.set(from);
for (const auto &to : edge[from]) {
if (dup[to])
continue;
reachability(to, dup);
return dup.count() != size;
}
return dup.count() != size;
}
bool dfs(const int &start, int from) {
// cout<<visited<<" "<<from<<endl;
unsigned long long m = visited.to_ullong() * N + from;
if (memo.count(m))
return false;
memo.insert(m);
// startの四方が囲まれていたらダメ
bool deadend = true;
for (const auto &v : edge[start])
deadend &= visited[v];
if (deadend)
return false;
// 現在地以外で次数1の頂点があるとダメ
for (int u = 0; u < size; ++u) {
if (u == from)
continue;
if (visited[u])
continue;
int degree = 0;
for (const auto &v : edge[u])
if (v == start || !visited[v])
++degree;
if (degree < 2)
return false;
}
visited.set(from);
for (const auto &to : edge[from]) {
if (to == start && visited.count() == size)
return true;
if (visited[to])
continue;
if (dfs(start, to))
return true;
}
visited.reset(from);
return false;
}
bool solve() {
visited = bitset<N>(0);
make_graph();
memo.clear();
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
if (!field[y][x])
return dfs(vertex[y][x], vertex[y][x]);
// for(int y = 3; y < h; ++y) for(int x = 0; x < w; ++x) if(!field[y][x])
// return dfs(vertex[y][x], vertex[y][x]); for(int y = 0; y < 3; ++y)
// for(int x = 0; x < w; ++x) if(!field[y][x]) return dfs(vertex[y][x],
// vertex[y][x]);
return false;
}
int main() {
while (cin >> w >> h, w | h) {
field = vector<vector<int>>(h, vector<int>(w));
for (auto &line : field)
for (auto &v : line)
cin >> v;
cout << (solve() ? "Yes" : "No") << endl;
}
} | #include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int N = 49;
int w, h;
vector<vector<int>> field;
int size;
vector<vector<int>> vertex;
vector<vector<int>> edge;
bitset<N> visited;
set<unsigned long long> memo;
void make_graph() {
enum { Y, X };
const int NEXT[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
// const int NEXT[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int NIL = -1;
size = 0;
vertex = vector<vector<int>>(h, vector<int>(w, NIL));
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
if (!field[y][x])
vertex[y][x] = size++;
edge = vector<vector<int>>(h * w, vector<int>());
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
if (vertex[y][x] == NIL)
continue;
for (const auto &d : NEXT) {
int ny = y + d[Y];
int nx = x + d[X];
if (ny < 0 || h <= ny || nx < 0 || w <= nx)
continue;
if (vertex[ny][nx] == NIL)
continue;
edge[vertex[y][x]].push_back(vertex[ny][nx]);
}
}
};
void reachability(const int &from, bitset<N> &visited) {
visited.set(from);
for (const auto &to : edge[from])
if (!visited[to])
reachability(to, visited);
}
bool cut_vertex(const int &from) {
auto dup = visited;
dup.set(from);
for (const auto &to : edge[from]) {
if (dup[to])
continue;
reachability(to, dup);
return dup.count() != size;
}
return dup.count() != size;
}
bool dfs(const int &start, int from) {
// cout<<visited<<" "<<from<<endl;
unsigned long long m = visited.to_ullong() * N + from;
if (memo.count(m))
return false;
memo.insert(m);
// cut vertexはダメ
if (cut_vertex(from))
return false;
// 現在地以外で次数1の頂点があるとダメ
for (int u = 0; u < size; ++u) {
if (u == from)
continue;
if (visited[u])
continue;
int degree = 0;
for (const auto &v : edge[u])
if (v == start || !visited[v])
++degree;
if (degree < 2)
return false;
}
visited.set(from);
for (const auto &to : edge[from]) {
if (to == start && visited.count() == size)
return true;
if (visited[to])
continue;
if (dfs(start, to))
return true;
}
visited.reset(from);
return false;
}
bool solve() {
visited = bitset<N>(0);
make_graph();
memo.clear();
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
if (!field[y][x])
return dfs(vertex[y][x], vertex[y][x]);
// for(int y = 3; y < h; ++y) for(int x = 0; x < w; ++x) if(!field[y][x])
// return dfs(vertex[y][x], vertex[y][x]); for(int y = 0; y < 3; ++y)
// for(int x = 0; x < w; ++x) if(!field[y][x]) return dfs(vertex[y][x],
// vertex[y][x]);
return false;
}
int main() {
while (cin >> w >> h, w | h) {
field = vector<vector<int>>(h, vector<int>(w));
for (auto &line : field)
for (auto &v : line)
cin >> v;
cout << (solve() ? "Yes" : "No") << endl;
}
} | replace | 70 | 75 | 70 | 72 | MLE | |
p00236 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int T_MAX = 11;
int tb[][3][3] = {
{{9, 1, 9}, {1, 0, 1}, {9, 9, 9}},
{{9, 1, 9}, {9, 0, 1}, {9, 1, 9}},
{{9, 0, 9}, {1, 0, 1}, {9, 1, 9}},
{{9, 1, 9}, {1, 0, 9}, {9, 1, 9}},
// +
{{9, 1, 9}, {1, 0, 1}, {9, 1, 9}},
};
int cur[9][9], W, H, sx, sy;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
bool done[9][9];
int dfs(int x, int y) {
if (done[y][x])
return 0;
else
done[y][x] = true;
if (y == sy && x == sx)
return 1;
if (cur[y][x] != 0)
return 0;
int ans = 1;
rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
return ans;
}
void output() {
rep(i, H + 2) {
rep(j, W + 2) cout << cur[i][j];
cout << endl;
}
}
bool isTaboo(int c, int x, int y) {
int ans = 0;
rep(i, 9) rep(j, 9) done[i][j] = 0;
rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
return c != ans;
}
bool badFlag;
int nest[50] = {0};
int bt(int c, int x, int y) {
if (nest[c]++ > 30000) {
badFlag = true;
return true;
}
if (c == 0)
return (x == sx && y == sy);
if (cur[y][x] != 0)
return false;
cur[y][x] = 2;
if (isTaboo(c, x, y)) {
cur[y][x] = 0;
return false;
}
rep(i, 4) if (bt(c - 1, x + dx[i], y + dy[i])) return true;
cur[y][x] = 0;
return false;
}
int main() {
while (cin >> W >> H && W) {
int eCount = 0;
rep(i, 9) rep(j, 9) cur[i][j] = 1;
rep(i, H) rep(j, W) cin >> cur[i + 1][j + 1], eCount += !cur[i + 1][j + 1];
rep(i, H + 2) rep(j, W + 2) if (cur[i][j] == 0) sx = j, sy = i;
rep(i, 50) nest[i] = 0;
badFlag = 0;
cout << (eCount > 3 && bt(eCount, sx, sy) && !badFlag ? "Yes" : "No")
<< endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int T_MAX = 11;
int tb[][3][3] = {
{{9, 1, 9}, {1, 0, 1}, {9, 9, 9}},
{{9, 1, 9}, {9, 0, 1}, {9, 1, 9}},
{{9, 0, 9}, {1, 0, 1}, {9, 1, 9}},
{{9, 1, 9}, {1, 0, 9}, {9, 1, 9}},
// +
{{9, 1, 9}, {1, 0, 1}, {9, 1, 9}},
};
int cur[9][9], W, H, sx, sy;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
bool done[9][9];
int dfs(int x, int y) {
if (done[y][x])
return 0;
else
done[y][x] = true;
if (y == sy && x == sx)
return 1;
if (cur[y][x] != 0)
return 0;
int ans = 1;
rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
return ans;
}
void output() {
rep(i, H + 2) {
rep(j, W + 2) cout << cur[i][j];
cout << endl;
}
}
bool isTaboo(int c, int x, int y) {
int ans = 0;
rep(i, 9) rep(j, 9) done[i][j] = 0;
rep(i, 4) ans += dfs(x + dx[i], y + dy[i]);
return c != ans;
}
bool badFlag;
int nest[50] = {0};
int bt(int c, int x, int y) {
if (nest[c]++ > 25000) {
badFlag = true;
return true;
}
if (c == 0)
return (x == sx && y == sy);
if (cur[y][x] != 0)
return false;
cur[y][x] = 2;
if (isTaboo(c, x, y)) {
cur[y][x] = 0;
return false;
}
rep(i, 4) if (bt(c - 1, x + dx[i], y + dy[i])) return true;
cur[y][x] = 0;
return false;
}
int main() {
while (cin >> W >> H && W) {
int eCount = 0;
rep(i, 9) rep(j, 9) cur[i][j] = 1;
rep(i, H) rep(j, W) cin >> cur[i + 1][j + 1], eCount += !cur[i + 1][j + 1];
rep(i, H + 2) rep(j, W + 2) if (cur[i][j] == 0) sx = j, sy = i;
rep(i, 50) nest[i] = 0;
badFlag = 0;
cout << (eCount > 3 && bt(eCount, sx, sy) && !badFlag ? "Yes" : "No")
<< endl;
}
} | replace | 51 | 52 | 51 | 52 | TLE | |
p00236 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;
typedef long long ll;
typedef pair<int, int> pii;
int w, h;
inline bool get(ll S, int y, int x) { return S >> (y * w + x) & 1; }
inline ll bit(int y, int x) { return 1LL << (y * w + x); }
inline bool ok(ll S, int y, int x) {
if (y < 0 || y >= h || x < 0 || x >= w)
return 0;
if (get(S, y, x))
return 0;
return 1;
}
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
bool visited[7][7];
int sy, sx;
map<ll, int> mp[7][7];
bool dfs(ll S, int y, int x, int num) {
// if (num < 30) {
if (mp[y][x].count(S))
return 0;
mp[y][x][S] = 1;
// }
// cout << y << "," << x << "," << num << endl;
// REP(i,h) {
// REP(j,w) cout << get(S,i,j);
// cout << endl;
// }
if (num == 0) {
return abs(y - sy) + abs(x - sx) == 1;
}
// BFSして領域が2つあったらだめ
{
REP(i, h) REP(j, w) visited[i][j] = 0;
REP(i, h) REP(j, w) {
if (get(S, i, j))
continue;
queue<pii> Q;
Q.push(pii(i, j));
visited[i][j] = 1;
int cnt = 0;
while (!Q.empty()) {
pii p = Q.front();
Q.pop();
++cnt;
REP(k, 4) {
int ii = p.first + dy[k];
int jj = p.second + dx[k];
if (!ok(S, ii, jj))
continue;
if (visited[ii][jj])
continue;
visited[ii][jj] = 1;
Q.push(pii(ii, jj));
}
}
if (cnt != num)
return 0;
goto next;
}
next:;
}
{
// 今の場所と最初の場所以外で凸の部分があったらだめ
int cnt = 0;
REP(i, h) REP(j, w) {
if (get(S, i, j))
continue;
int c = 0;
REP(k, 4) {
int ii = i + dy[k];
int jj = j + dx[k];
if (!ok(S, ii, jj)) {
if (ii != y || jj != x) {
if (ii != sy || jj != sx) {
++c;
}
}
}
}
if (c >= 3) {
++cnt;
}
}
if (cnt >= 1)
return 0;
}
REP(k, 4) {
int yy = y + dy[k];
int xx = x + dx[k];
if (!ok(S, yy, xx))
continue;
if (dfs(S | bit(yy, xx), yy, xx, num - 1))
return 1;
}
return 0;
}
int main() {
while (cin >> w >> h, w || h) {
ll S = 0;
int num = 0;
REP(i, h) REP(j, w) {
int a;
cin >> a;
if (a)
S |= bit(i, j);
else {
sy = i, sx = j;
++num;
}
}
if (num <= 3) { // 空きマスが3以下
puts("No");
continue;
}
S |= bit(sy, sx);
--num;
REP(i, h) REP(j, w) mp[i][j].clear();
cout << (dfs(S, sy, sx, num) ? "Yes" : "No") << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;
typedef long long ll;
typedef pair<int, int> pii;
int w, h;
inline bool get(ll S, int y, int x) { return S >> (y * w + x) & 1; }
inline ll bit(int y, int x) { return 1LL << (y * w + x); }
inline bool ok(ll S, int y, int x) {
if (y < 0 || y >= h || x < 0 || x >= w)
return 0;
if (get(S, y, x))
return 0;
return 1;
}
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
bool visited[7][7];
int sy, sx;
map<ll, int> mp[7][7];
bool dfs(ll S, int y, int x, int num) {
// if (num < 30) {
if (y) {
if (mp[y][x].count(S))
return 0;
mp[y][x][S] = 1;
}
// }
// cout << y << "," << x << "," << num << endl;
// REP(i,h) {
// REP(j,w) cout << get(S,i,j);
// cout << endl;
// }
if (num == 0) {
return abs(y - sy) + abs(x - sx) == 1;
}
// BFSして領域が2つあったらだめ
{
REP(i, h) REP(j, w) visited[i][j] = 0;
REP(i, h) REP(j, w) {
if (get(S, i, j))
continue;
queue<pii> Q;
Q.push(pii(i, j));
visited[i][j] = 1;
int cnt = 0;
while (!Q.empty()) {
pii p = Q.front();
Q.pop();
++cnt;
REP(k, 4) {
int ii = p.first + dy[k];
int jj = p.second + dx[k];
if (!ok(S, ii, jj))
continue;
if (visited[ii][jj])
continue;
visited[ii][jj] = 1;
Q.push(pii(ii, jj));
}
}
if (cnt != num)
return 0;
goto next;
}
next:;
}
{
// 今の場所と最初の場所以外で凸の部分があったらだめ
int cnt = 0;
REP(i, h) REP(j, w) {
if (get(S, i, j))
continue;
int c = 0;
REP(k, 4) {
int ii = i + dy[k];
int jj = j + dx[k];
if (!ok(S, ii, jj)) {
if (ii != y || jj != x) {
if (ii != sy || jj != sx) {
++c;
}
}
}
}
if (c >= 3) {
++cnt;
}
}
if (cnt >= 1)
return 0;
}
REP(k, 4) {
int yy = y + dy[k];
int xx = x + dx[k];
if (!ok(S, yy, xx))
continue;
if (dfs(S | bit(yy, xx), yy, xx, num - 1))
return 1;
}
return 0;
}
int main() {
while (cin >> w >> h, w || h) {
ll S = 0;
int num = 0;
REP(i, h) REP(j, w) {
int a;
cin >> a;
if (a)
S |= bit(i, j);
else {
sy = i, sx = j;
++num;
}
}
if (num <= 3) { // 空きマスが3以下
puts("No");
continue;
}
S |= bit(sy, sx);
--num;
REP(i, h) REP(j, w) mp[i][j].clear();
cout << (dfs(S, sy, sx, num) ? "Yes" : "No") << endl;
}
} | replace | 46 | 49 | 46 | 51 | MLE | |
p00236 | 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 rep(i, s, n) for (int i = (s); 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;
}
bool f[7][7];
bool tmp[7][7];
int sum;
int xs, ys;
int w, h;
void check(int y, int x) {
REP(i, 4) {
int yy = y + _dy[i];
int xx = x + _dx[i];
if (ISIN(xx, yy, w, h) && tmp[yy][xx]) {
tmp[yy][xx] = false;
check(yy, xx);
}
}
}
bool solve(int y, int x, int cnt) {
if (cnt != 2 && cnt != sum) {
if (abs(y - ys) + abs(x - xs) == 1)
return false;
}
bool ret = false;
f[y][x] = false;
if (sum == cnt) {
if (abs(y - ys) + abs(x - xs) == 1)
ret = true;
} else {
/* check */
REP(i, h) REP(j, w) tmp[i][j] = f[i][j];
check(y, x);
bool ok = true;
REP(i, h) REP(j, w) if (tmp[i][j]) ok = false;
if (ok) {
REP(i, 4) {
int yy = y + _dy[i];
int xx = x + _dx[i];
if (ISIN(xx, yy, w, h) && f[yy][xx]) {
if (solve(yy, xx, cnt + 1)) {
ret = true;
break;
}
}
}
}
}
f[y][x] = true;
return ret;
}
int main() {
while (true) {
w = getInt();
h = getInt();
bool ans = false;
if (w + h == 0)
break;
xs = ys = -1;
sum = 0;
REP(i, h) REP(j, w) {
f[i][j] = getInt() == 0 ? true : false;
if (f[i][j])
sum++;
if (ys == -1 && f[i][j]) {
ys = i;
xs = j;
}
}
if (h == 7 && w == 7 && sum == 7 * 7)
ans = false;
else if (h == 7 && w == 7 && sum == 7 * 7 - 1) {
REP(i, h) REP(j, w) if (!f[i][j]) {
if ((i + j) % 2 == 0)
ans = true;
else
ans = false;
}
} else if (ys != -1)
ans = solve(ys, xs, 1);
puts(ans ? "Yes" : "No");
}
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 rep(i, s, n) for (int i = (s); 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;
}
bool f[7][7];
bool tmp[7][7];
int sum;
int xs, ys;
int w, h;
void check(int y, int x) {
REP(i, 4) {
int yy = y + _dy[i];
int xx = x + _dx[i];
if (ISIN(xx, yy, w, h) && tmp[yy][xx]) {
tmp[yy][xx] = false;
check(yy, xx);
}
}
}
bool solve(int y, int x, int cnt) {
if (cnt != 2 && cnt != sum) {
if (abs(y - ys) + abs(x - xs) == 1)
return false;
}
bool ret = false;
f[y][x] = false;
if (sum == cnt) {
if (abs(y - ys) + abs(x - xs) == 1)
ret = true;
} else {
/* check */
memcpy(tmp, f, sizeof(tmp));
// REP(i, h) REP(j, w) tmp[i][j] = f[i][j];
check(y, x);
bool ok = true;
REP(i, h) REP(j, w) if (tmp[i][j]) ok = false;
if (ok) {
REP(i, 4) {
int yy = y + _dy[i];
int xx = x + _dx[i];
if (ISIN(xx, yy, w, h) && f[yy][xx]) {
if (solve(yy, xx, cnt + 1)) {
ret = true;
break;
}
}
}
}
}
f[y][x] = true;
return ret;
}
int main() {
while (true) {
w = getInt();
h = getInt();
bool ans = false;
if (w + h == 0)
break;
xs = ys = -1;
sum = 0;
REP(i, h) REP(j, w) {
f[i][j] = getInt() == 0 ? true : false;
if (f[i][j])
sum++;
if (ys == -1 && f[i][j]) {
ys = i;
xs = j;
}
}
if (h == 7 && w == 7 && sum == 7 * 7)
ans = false;
else if (h == 7 && w == 7 && sum == 7 * 7 - 1) {
REP(i, h) REP(j, w) if (!f[i][j]) {
if ((i + j) % 2 == 0)
ans = true;
else
ans = false;
}
} else if (ys != -1)
ans = solve(ys, xs, 1);
puts(ans ? "Yes" : "No");
}
return 0;
} | replace | 81 | 82 | 81 | 83 | TLE | |
p00236 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int W, H;
int board[10][10][3];
int R_end, C_end;
bool solve(int row, int column) {
// cout << "@" << row << "," << column << endl;
int nex_row, nex_column;
nex_row = row + 1;
nex_column = column + nex_row / H;
nex_row = nex_row % H;
if (row >= R_end && column >= C_end && !(row == R_end && column == C_end)) {
// cout << "err" << endl;
return false;
}
if (board[row + 1][column + 1][0] == 1) {
if ((board[row][column + 1][1] & 2) || (board[row + 1][column][1] & 1))
return false;
board[row + 1][column + 1][1] = 0;
board[row + 1][column + 1][2] = 0;
return solve(nex_row, nex_column);
} else {
if ((board[row][column + 1][1] & 2) && (board[row + 1][column][1] & 1)) {
// cout << "@" << row << "," << column << ":";
// cout << board[row+1][column][2] << " " <<
//board[row][column+1][2] << endl;
if (row == R_end && column == C_end &&
board[row + 1][column][2] == board[row][column + 1][2])
return true;
if ((row != R_end || column != C_end) &&
board[row + 1][column][2] == board[row][column + 1][2])
return false;
board[row + 1][column + 1][1] = 0;
int cp[10];
for (int i = 0; i < H; i++)
cp[i] = board[(row + i) % H + 1][column + (row + i) / H][2];
int tmp = board[row + 1][column][2];
for (int i = 0; i < H; i++) {
if (board[(row + i) % H + 1][column + (row + i) / H][2] == tmp)
board[(row + i) % H + 1][column + (row + i) / H][2] =
board[row][column + 1][2];
}
board[row + 1][column + 1][2] = board[row][column + 1][2];
if (solve(nex_row, nex_column))
return true;
for (int i = 0; i < H; i++)
board[(row + i) % H + 1][column + (row + i) / H][2] = cp[i];
} else if (board[row][column + 1][1] & 2) {
board[row + 1][column + 1][2] = board[row][column + 1][2];
for (int i = 0; i < 2; i++) {
if (board[row + 1 + i][column + 1 + !i][0] == 0) {
board[row + 1][column + 1][1] = i + 1;
if (solve(nex_row, nex_column))
return true;
}
}
} else if (board[row + 1][column][1] & 1) {
board[row + 1][column + 1][2] = board[row + 1][column][2];
for (int i = 0; i < 2; i++) {
if (board[row + 1 + i][column + 1 + !i][0] == 0) {
board[row + 1][column + 1][1] = i + 1;
if (solve(nex_row, nex_column))
return true;
}
}
} else {
if (board[row + 2][column + 1][0] || board[row + 1][column + 2][0])
return false;
board[row + 1][column + 1][1] = 3;
int mx = 0;
for (int i = 0; i < H; i++)
mx = max(mx, board[(row + i) % H + 1][column + (row + i) / H][2]);
board[row + 1][column + 1][2] = mx + 1;
return solve(nex_row, nex_column);
}
}
return false;
}
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
board[i][j][0] = 1;
board[i][j][1] = 0;
board[i][j][2] = 0;
}
}
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> board[i + 1][j + 1][0];
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
if (board[j + 1][i + 1][0] == 0) {
R_end = j;
C_end = i;
}
}
}
// cout << "*" << R_end << " " << C_end << endl;
if (solve(0, 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
} | #include <algorithm>
#include <iostream>
using namespace std;
int W, H;
int board[10][10][3];
int R_end, C_end;
bool solve(int row, int column) {
// cout << "@" << row << "," << column << endl;
int nex_row, nex_column;
nex_row = row + 1;
nex_column = column + nex_row / H;
nex_row = nex_row % H;
if (row >= R_end && column >= C_end && !(row == R_end && column == C_end)) {
// cout << "err" << endl;
return false;
}
if (board[row + 1][column + 1][0] == 1) {
if ((board[row][column + 1][1] & 2) || (board[row + 1][column][1] & 1))
return false;
board[row + 1][column + 1][1] = 0;
board[row + 1][column + 1][2] = 0;
return solve(nex_row, nex_column);
} else {
if ((board[row][column + 1][1] & 2) && (board[row + 1][column][1] & 1)) {
// cout << "@" << row << "," << column << ":";
// cout << board[row+1][column][2] << " " <<
//board[row][column+1][2] << endl;
if (row == R_end && column == C_end &&
board[row + 1][column][2] == board[row][column + 1][2])
return true;
if ((row != R_end || column != C_end) &&
board[row + 1][column][2] == board[row][column + 1][2])
return false;
board[row + 1][column + 1][1] = 0;
int cp[10];
for (int i = 0; i < H; i++)
cp[i] = board[(row + i) % H + 1][column + (row + i) / H][2];
int tmp = board[row + 1][column][2];
for (int i = 0; i < H; i++) {
if (board[(row + i) % H + 1][column + (row + i) / H][2] == tmp)
board[(row + i) % H + 1][column + (row + i) / H][2] =
board[row][column + 1][2];
}
board[row + 1][column + 1][2] = board[row][column + 1][2];
if (solve(nex_row, nex_column))
return true;
for (int i = 0; i < H; i++)
board[(row + i) % H + 1][column + (row + i) / H][2] = cp[i];
} else if (board[row][column + 1][1] & 2) {
board[row + 1][column + 1][2] = board[row][column + 1][2];
for (int i = 0; i < 2; i++) {
if (board[row + 1 + i][column + 1 + !i][0] == 0) {
board[row + 1][column + 1][1] = i + 1;
if (solve(nex_row, nex_column))
return true;
}
}
} else if (board[row + 1][column][1] & 1) {
board[row + 1][column + 1][2] = board[row + 1][column][2];
for (int i = 0; i < 2; i++) {
if (board[row + 1 + i][column + 1 + !i][0] == 0) {
board[row + 1][column + 1][1] = i + 1;
if (solve(nex_row, nex_column))
return true;
}
}
} else {
if (board[row + 2][column + 1][0] || board[row + 1][column + 2][0])
return false;
board[row + 1][column + 1][1] = 3;
int mx = 0;
for (int i = 0; i < H; i++)
mx = max(mx, board[(row + i) % H + 1][column + (row + i) / H][2]);
board[row + 1][column + 1][2] = mx + 1;
return solve(nex_row, nex_column);
}
}
return false;
}
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
board[i][j][0] = 1;
board[i][j][1] = 0;
board[i][j][2] = 0;
}
}
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> board[i + 1][j + 1][0];
R_end = C_end = 0;
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
if (board[j + 1][i + 1][0] == 0) {
R_end = j;
C_end = i;
}
}
}
// cout << "*" << R_end << " " << C_end << endl;
if (solve(0, 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
} | insert | 103 | 103 | 103 | 104 | 0 | |
p00238 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, c, a, b;
while (cin >> n, n) {
cin >> c;
int y = 0;
for (i = 0; i < c; c++) {
cin >> a >> b;
y += b - a;
}
if (y >= n)
cout << "OK" << endl;
else
cout << n - y << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, c, a, b;
while (cin >> n, n) {
cin >> c;
int y = 0;
for (i = 0; i < c; i++) {
cin >> a >> b;
y += b - a;
}
if (y >= n)
cout << "OK" << endl;
else
cout << n - y << endl;
}
} | replace | 7 | 8 | 7 | 8 | TLE | |
p00239 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int n, k, j, P, Q, R, C, flg;
int i[1001], p1[101], q1[101], r1[101], c[1701], p[101], q[101], r[101];
flg = 0;
scanf("%d", &n);
while (n != 0) {
for (j = 1; j <= n; j++) {
scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
p1[j] = p[j] * 4;
q1[j] = q[j] * 9;
r1[j] = r[j] * 4;
c[j] = p1[j] + q1[j] + r1[j];
}
scanf("%d %d %d %d", &P, &Q, &R, &C);
for (j = 1; j <= n; j++) {
if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
printf("%d\n", i[j]);
flg = 1;
}
}
if (flg == 0) {
printf("NA\n");
}
flg = 0;
scanf("%d", &n);
}
return 0;
} | #include <stdio.h>
int main(void) {
int n, k, j, P, Q, R, C, flg;
int i[1001], p1[101], q1[101], r1[101], c[1701], p[1001], q[1001], r[1001];
flg = 0;
scanf("%d", &n);
while (n != 0) {
for (j = 1; j <= n; j++) {
scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
p1[j] = p[j] * 4;
q1[j] = q[j] * 9;
r1[j] = r[j] * 4;
c[j] = p1[j] + q1[j] + r1[j];
}
scanf("%d %d %d %d", &P, &Q, &R, &C);
for (j = 1; j <= n; j++) {
if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
printf("%d\n", i[j]);
flg = 1;
}
}
if (flg == 0) {
printf("NA\n");
}
flg = 0;
scanf("%d", &n);
}
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00239 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int N;
int s[100], h[100], L[100], C[100];
while (cin >> N) {
if (N == 0)
break;
for (int i = 0; i < N; i++) {
cin >> s[i] >> h[i] >> L[i] >> C[i];
}
int P, Q, R, c;
int a = 0;
cin >> P >> Q >> R >> c;
for (int i = 0; i < N; i++) {
if ((h[i] <= P) && (L[i] <= Q) && (C[i] <= R) &&
((h[i] * 4 + L[i] * 9 + C[i] * 4) <= c)) {
cout << s[i] << endl;
a = 1;
}
}
if (a == 0)
cout << "NA" << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int N;
int s[1000], h[1000], L[1000], C[1000];
while (cin >> N) {
if (N == 0)
break;
for (int i = 0; i < N; i++) {
cin >> s[i] >> h[i] >> L[i] >> C[i];
}
int P, Q, R, c;
int a = 0;
cin >> P >> Q >> R >> c;
for (int i = 0; i < N; i++) {
if ((h[i] <= P) && (L[i] <= Q) && (C[i] <= R) &&
((h[i] * 4 + L[i] * 9 + C[i] * 4) <= c)) {
cout << s[i] << endl;
a = 1;
}
}
if (a == 0)
cout << "NA" << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00240 | C++ | Runtime Error | #include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;
int main() {
int n;
while (cin >> n, n) {
double y;
cin >> y;
pair<int, double> p;
auto f1 = [](double y, double r) { return 1 + y * r / 100; };
auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
rep(i, n) {
int b, t;
double r;
cin >> b >> r >> t;
show(t == 1 ? f1(y, r) : f2(y, r));
if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
p.first = b;
p.second = (t == 1 ? f1(y, r) : f2(y, r));
}
}
cout << p.first << endl;
}
} | #include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;
int main() {
int n;
while (cin >> n, n) {
double y;
cin >> y;
pair<int, double> p;
auto f1 = [](double y, double r) { return 1 + y * r / 100; };
auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
rep(i, n) {
int b, t;
double r;
cin >> b >> r >> t;
if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
p.first = b;
p.second = (t == 1 ? f1(y, r) : f2(y, r));
}
}
cout << p.first << endl;
}
} | delete | 20 | 21 | 20 | 20 | 0 | t == 1 ? f1(y,r) : f2(y,r) = 1.47746
t == 1 ? f1(y,r) : f2(y,r) = 1.48
t == 1 ? f1(y,r) : f2(y,r) = 1.55133
t == 1 ? f1(y,r) : f2(y,r) = 1.54
|
p00240 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class Bank {
public:
int id, r, t;
Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};
int main() {
while (true) {
int n;
cin >> n;
int y;
cin >> y;
vector<Bank> vb;
for (int i = 0; i < n; ++i) {
int b, r, t;
cin >> b >> r >> t;
vb.push_back(Bank(b, r, t));
}
// ganpon * (1 + y * r/100)
// ganpon * (1 + r/100)^y
double maxi = 0;
int ans = -1;
for (int i = 0; i < vb.size(); ++i) {
double g = 100000;
double r1 = g * (1. + y * vb[i].r / 100.);
double r2 = g * pow(1 + vb[i].r / 100., y);
if (vb[i].t == 1) {
if (maxi < r1) {
maxi = r1;
ans = vb[i].id;
}
} else {
if (maxi < r2) {
maxi = r2;
ans = vb[i].id;
}
}
}
cout << ans << endl;
}
}
// 1 | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class Bank {
public:
int id, r, t;
Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
int y;
cin >> y;
vector<Bank> vb;
for (int i = 0; i < n; ++i) {
int b, r, t;
cin >> b >> r >> t;
vb.push_back(Bank(b, r, t));
}
// ganpon * (1 + y * r/100)
// ganpon * (1 + r/100)^y
double maxi = 0;
int ans = -1;
for (int i = 0; i < vb.size(); ++i) {
double g = 100000;
double r1 = g * (1. + y * vb[i].r / 100.);
double r2 = g * pow(1 + vb[i].r / 100., y);
if (vb[i].t == 1) {
if (maxi < r1) {
maxi = r1;
ans = vb[i].id;
}
} else {
if (maxi < r2) {
maxi = r2;
ans = vb[i].id;
}
}
}
cout << ans << endl;
}
}
// 1 | insert | 16 | 16 | 16 | 20 | TLE | |
p00241 | C++ | Runtime Error | #ifdef __GNU_C__
#include <bits/stdc++.h>
#else
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <memory.h>
#include <sstream>
#include <string>
#include <vector>
#endif // __GNU_C__
using namespace std;
#define REP(i, max) for (int i = 0, iMax = max; i < iMax; i++)
#define RREP(i, min, max) for (int i = min, iMax = max; i < iMax; i++)
#define EREP(i, min, max) for (int i = min, iMax = max; i <= iMax; i++)
#define REV(i, max) for (int i = max - 1; i >= 0; i--)
#define RREV(i, min, max) for (int i = max, iMin = min; i > iMin; i--)
#define EREV(i, min, max) for (int i = max - 1, iMin = min; i >= iMin; i--)
typedef unsigned long long u64;
struct Quaternion {
int q[4];
void Print() { ::scanf("%d %d %d %d\n", q[0], q[1], q[2], q[3]); }
Quaternion operator*(Quaternion &o) {
/* static int eddingtonEpsilon[][][] =
{
{{0, 0, 0}, {0, 0, 1}, {-1, 0, 0} },
{{0, 0, -1}, {0, 0, 0}, {1, 0, 0} },
{{0, 1, 0}, {-1, 0, 0}, {0, 0, 0} },
};
*/
return Quaternion(
q[0] * o.q[0] - q[1] * o.q[1] - q[2] * o.q[2] - q[3] * o.q[3],
q[2] * o.q[3] - q[3] * o.q[2] + q[0] * o.q[1] + q[1] * o.q[0],
q[3] * o.q[1] - q[1] * o.q[3] + q[0] * o.q[2] + q[2] * o.q[0],
q[1] * o.q[2] - q[2] * o.q[1] + q[0] * o.q[3] + q[3] * o.q[0]);
}
Quaternion() {}
Quaternion(int ct, int x, int y, int z) {
q[0] = ct, q[1] = x, q[2] = y, q[3] = z;
}
};
int main(int args, char *psArgs[]) {
int numQuaternions;
vector<Quaternion> quaternions;
while (::scanf("%d", &numQuaternions), numQuaternions) {
while (numQuaternions--) {
Quaternion a, b;
::scanf("%d %d %d %d", &a.q[0], &a.q[1], &a.q[2], &a.q[3]);
::scanf("%d %d %d %d", &b.q[0], &b.q[1], &b.q[2], &b.q[3]);
(a * b).Print();
}
}
} | #ifdef __GNU_C__
#include <bits/stdc++.h>
#else
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <memory.h>
#include <sstream>
#include <string>
#include <vector>
#endif // __GNU_C__
using namespace std;
#define REP(i, max) for (int i = 0, iMax = max; i < iMax; i++)
#define RREP(i, min, max) for (int i = min, iMax = max; i < iMax; i++)
#define EREP(i, min, max) for (int i = min, iMax = max; i <= iMax; i++)
#define REV(i, max) for (int i = max - 1; i >= 0; i--)
#define RREV(i, min, max) for (int i = max, iMin = min; i > iMin; i--)
#define EREV(i, min, max) for (int i = max - 1, iMin = min; i >= iMin; i--)
typedef unsigned long long u64;
struct Quaternion {
int q[4];
void Print() { ::printf("%d %d %d %d\n", q[0], q[1], q[2], q[3]); }
Quaternion operator*(Quaternion &o) {
/* static int eddingtonEpsilon[][][] =
{
{{0, 0, 0}, {0, 0, 1}, {-1, 0, 0} },
{{0, 0, -1}, {0, 0, 0}, {1, 0, 0} },
{{0, 1, 0}, {-1, 0, 0}, {0, 0, 0} },
};
*/
return Quaternion(
q[0] * o.q[0] - q[1] * o.q[1] - q[2] * o.q[2] - q[3] * o.q[3],
q[2] * o.q[3] - q[3] * o.q[2] + q[0] * o.q[1] + q[1] * o.q[0],
q[3] * o.q[1] - q[1] * o.q[3] + q[0] * o.q[2] + q[2] * o.q[0],
q[1] * o.q[2] - q[2] * o.q[1] + q[0] * o.q[3] + q[3] * o.q[0]);
}
Quaternion() {}
Quaternion(int ct, int x, int y, int z) {
q[0] = ct, q[1] = x, q[2] = y, q[3] = z;
}
};
int main(int args, char *psArgs[]) {
int numQuaternions;
vector<Quaternion> quaternions;
while (::scanf("%d", &numQuaternions), numQuaternions) {
while (numQuaternions--) {
Quaternion a, b;
::scanf("%d %d %d %d", &a.q[0], &a.q[1], &a.q[2], &a.q[3]);
::scanf("%d %d %d %d", &b.q[0], &b.q[1], &b.q[2], &b.q[3]);
(a * b).Print();
}
}
} | replace | 27 | 28 | 27 | 28 | -11 | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int h, w, n, ans, colc;
char cmap[110][110];
bool used[110][110];
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};
void display() {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
cout << cmap[i][j] << " ";
cout << endl;
}
cout << endl;
}
void draw(char c, char t, int x, int y) {
cmap[y][x] = t;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < w && 0 <= ny && ny < h && c == cmap[ny][nx]) {
draw(c, t, nx, ny);
}
}
}
bool fin() {
bool finish = true;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (cmap[0][0] != cmap[i][j])
return false;
}
}
return true;
}
int search(int x, int y) {
used[y][x] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < w && 0 <= ny && ny < h && cmap[0][0] == cmap[ny][nx] &&
!used[ny][nx]) {
colc++;
search(nx, ny);
}
}
}
void rec(int cnt) {
if (fin()) {
ans = min(ans, cnt);
return;
}
// display();
char A, B, erase[110][110];
if (cmap[0][0] == 'R') {
A = 'B';
B = 'G';
} else if (cmap[0][0] == 'B') {
A = 'R';
B = 'G';
} else if (cmap[0][0] == 'G') {
A = 'B';
B = 'R';
}
colc = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
search(0, 0);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
erase[i][j] = cmap[i][j];
draw(cmap[0][0], A, 0, 0);
int precolc = colc;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
if (colc != precolc)
rec(cnt + 1);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cmap[i][j] = erase[i][j];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
draw(cmap[0][0], B, 0, 0);
precolc = colc;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
if (colc != precolc)
rec(cnt + 1);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cmap[i][j] = erase[i][j];
}
int main() {
while (true) {
cin >> w >> h;
if (w + h == 0)
break;
ans = (1 << 29);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> cmap[i][j];
}
}
rec(0);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int h, w, n, ans, colc;
char cmap[110][110];
bool used[110][110];
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};
void display() {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
cout << cmap[i][j] << " ";
cout << endl;
}
cout << endl;
}
void draw(char c, char t, int x, int y) {
cmap[y][x] = t;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < w && 0 <= ny && ny < h && c == cmap[ny][nx]) {
draw(c, t, nx, ny);
}
}
}
bool fin() {
bool finish = true;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (cmap[0][0] != cmap[i][j])
return false;
}
}
return true;
}
int search(int x, int y) {
used[y][x] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < w && 0 <= ny && ny < h && cmap[0][0] == cmap[ny][nx] &&
!used[ny][nx]) {
colc++;
search(nx, ny);
}
}
}
void rec(int cnt) {
if (cnt >= ans)
return;
if (fin()) {
ans = min(ans, cnt);
return;
}
// display();
char A, B, erase[110][110];
if (cmap[0][0] == 'R') {
A = 'B';
B = 'G';
} else if (cmap[0][0] == 'B') {
A = 'R';
B = 'G';
} else if (cmap[0][0] == 'G') {
A = 'B';
B = 'R';
}
colc = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
search(0, 0);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
erase[i][j] = cmap[i][j];
draw(cmap[0][0], A, 0, 0);
int precolc = colc;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
if (colc != precolc)
rec(cnt + 1);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cmap[i][j] = erase[i][j];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
draw(cmap[0][0], B, 0, 0);
precolc = colc;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
used[i][j] = false;
colc = 0;
search(0, 0);
if (colc != precolc)
rec(cnt + 1);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cmap[i][j] = erase[i][j];
}
int main() {
while (true) {
cin >> w >> h;
if (w + h == 0)
break;
ans = (1 << 29);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> cmap[i][j];
}
}
rec(0);
cout << ans << endl;
}
return 0;
} | insert | 57 | 57 | 57 | 59 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
fld[y][x] = c;
int cnt = 1;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d], nx = x + dx[d];
if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
continue;
cnt += change(ny, nx, c, f);
}
return cnt;
}
bool dfs(int t = 0, int sum = 1) {
if (lim == t) {
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
if (fld[0][0] != fld[i][j])
ok = false;
}
return ok;
}
int _fld[10][10];
memcpy(_fld, fld, sizeof(_fld));
bool ret = false;
for (int k = 0; k < 3; k++)
if (k != fld[0][0]) {
int la = change(0, 0, k, fld[0][0]);
if (la > sum)
ret |= dfs(t + 1, la);
memcpy(fld, _fld, sizeof(fld));
}
return ret;
}
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c;
cin >> c;
switch (c) {
case 'R':
fld[i][j] = 0;
break;
case 'G':
fld[i][j] = 1;
break;
case 'B':
fld[i][j] = 2;
break;
}
}
}
for (lim = 0;; lim++)
if (dfs())
break;
cout << lim << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
fld[y][x] = c;
int cnt = 1;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d], nx = x + dx[d];
if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
continue;
cnt += change(ny, nx, c, f);
}
return cnt;
}
bool dfs(int t = 0, int sum = 1) {
if (lim == t) {
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
if (fld[0][0] != fld[i][j])
ok = false;
}
return ok;
}
int _fld[10][10];
memcpy(_fld, fld, sizeof(_fld));
bool ret = false;
for (int k = 0; k < 3; k++)
if (k != fld[0][0]) {
int la = change(0, 0, k, fld[0][0]);
if (la >= sum)
ret |= dfs(t + 1, la);
memcpy(fld, _fld, sizeof(fld));
}
return ret;
}
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c;
cin >> c;
switch (c) {
case 'R':
fld[i][j] = 0;
break;
case 'G':
fld[i][j] = 1;
break;
case 'B':
fld[i][j] = 2;
break;
}
}
}
for (lim = 0;; lim++)
if (dfs())
break;
cout << lim << endl;
}
return 0;
} | replace | 39 | 40 | 39 | 40 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1001001001;
const double EPS = 1e-10;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};
int h, w;
int board[10][10];
int label[10][10];
vi color;
vvi G;
void labeling(int y, int x, int id) {
label[y][x] = id;
rep(i, 4) {
int py = y + dy[i];
int px = x + dx[i];
if (py < 0 || h <= py || px < 0 || w <= px)
continue;
if (board[py][px] == board[y][x] && label[py][px] == -1)
labeling(py, px, id);
}
}
bool dfs(vi cand, int now, int depth) {
if (depth == 0) {
rep(i, cand.size()) if (cand[i] != 2) return false;
return true;
}
vi tmp = cand;
rep(c, 3) {
if (c == now)
continue;
bool found = false;
rep(i, cand.size()) {
if (cand[i] == 1 && color[i] == c) {
rep(j, G[i].size()) {
if (cand[G[i][j]] == 0)
cand[G[i][j]] = 1;
}
cand[i] = 2;
found = true;
}
}
if (found) {
if (dfs(cand, c, depth - 1))
return true;
cand = tmp;
}
}
return false;
}
int main() {
while (scanf("%d%d", &w, &h), w) {
rep(i, h) rep(j, w) {
char c[5];
scanf("%s", c);
if (c[0] == 'R')
board[i][j] = 0;
else if (c[0] == 'G')
board[i][j] = 1;
else
board[i][j] = 2;
}
rep(i, h) fill(label[i], label[i] + w, -1);
color.clear();
rep(i, h) rep(j, w) {
if (label[i][j] == -1) {
labeling(i, j, color.size());
color.push_back(board[i][j]);
}
}
G.assign(color.size(), vi());
rep(i, h) rep(j, w) {
rep(k, 4) {
int py = i + dy[k];
int px = j + dx[k];
if (py < 0 || h <= py || px < 0 || w <= px)
continue;
if (label[i][j] != label[py][px]) {
G[label[i][j]].push_back(label[py][px]);
G[label[py][px]].push_back(label[i][j]);
}
}
}
rep(i, G.size()) {
sort(G[i].begin(), G[i].end());
G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
}
for (int depth = 1;; ++depth) {
vi cand(G.size(), 0);
rep(i, G[0].size()) cand[G[0][i]] = 1;
cand[0] = 2;
if (dfs(cand, color[0], depth)) {
printf("%d\n", depth);
break;
}
}
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); ++i)
#define rep(i, n) reps(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1001001001;
const double EPS = 1e-10;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};
int h, w;
int board[10][10];
int label[10][10];
vi color;
vvi G;
void labeling(int y, int x, int id) {
label[y][x] = id;
rep(i, 4) {
int py = y + dy[i];
int px = x + dx[i];
if (py < 0 || h <= py || px < 0 || w <= px)
continue;
if (board[py][px] == board[y][x] && label[py][px] == -1)
labeling(py, px, id);
}
}
bool dfs(vi cand, int now, int depth) {
if (depth == 0) {
rep(i, cand.size()) if (cand[i] != 2) return false;
return true;
}
vi tmp = cand;
rep(c, 3) {
if (c == now)
continue;
bool found = false;
rep(i, cand.size()) {
if (cand[i] == 1 && color[i] == c) {
rep(j, G[i].size()) {
if (cand[G[i][j]] == 0)
cand[G[i][j]] = 1;
}
cand[i] = 2;
found = true;
}
}
if (found) {
if (dfs(cand, c, depth - 1))
return true;
cand = tmp;
}
}
return false;
}
int main() {
while (scanf("%d%d", &w, &h), w) {
rep(i, h) rep(j, w) {
char c[5];
scanf("%s", c);
if (c[0] == 'R')
board[i][j] = 0;
else if (c[0] == 'G')
board[i][j] = 1;
else
board[i][j] = 2;
}
rep(i, h) fill(label[i], label[i] + w, -1);
color.clear();
rep(i, h) rep(j, w) {
if (label[i][j] == -1) {
labeling(i, j, color.size());
color.push_back(board[i][j]);
}
}
G.assign(color.size(), vi());
rep(i, h) rep(j, w) {
rep(k, 4) {
int py = i + dy[k];
int px = j + dx[k];
if (py < 0 || h <= py || px < 0 || w <= px)
continue;
if (label[i][j] != label[py][px]) {
G[label[i][j]].push_back(label[py][px]);
G[label[py][px]].push_back(label[i][j]);
}
}
}
rep(i, G.size()) {
sort(G[i].begin(), G[i].end());
G[i].erase(unique(G[i].begin(), G[i].end()), G[i].end());
}
for (int depth = 0;; ++depth) {
vi cand(G.size(), 0);
rep(i, G[0].size()) cand[G[0][i]] = 1;
cand[0] = 2;
if (dfs(cand, color[0], depth)) {
printf("%d\n", depth);
break;
}
}
}
return 0;
} | replace | 125 | 126 | 125 | 126 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int W, H;
int field[20][20];
int field2[21][21];
int res;
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
bool used[101][101];
int lim;
void fillField(int cy, int cx, int oldc, int newc, bool &ok, bool flag) {
if (!flag)
field[cy][cx] = newc;
else
field2[cy][cx] = newc;
used[cy][cx] = true;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && !used[ny][nx]) {
if (!flag) {
if (field[ny][nx] == oldc)
fillField(ny, nx, oldc, newc, ok, flag);
else if (field[ny][nx] == newc)
ok = true;
} else {
if (field2[ny][nx] == oldc)
fillField(ny, nx, oldc, newc, ok, flag);
else if (field2[ny][nx] == newc)
ok = true;
}
}
}
}
void dfs(int cnt) {
if (lim <= cnt)
return;
{
int goal = field[0][0];
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (field[i][j] != goal)
ok = false;
if (ok) {
res = min(res, cnt);
return;
}
}
for (int i = 0; i < 3; i++) {
if (field[0][0] == i)
continue;
int backUp[20][20];
for (int j = 0; j < H; j++)
for (int k = 0; k < W; k++)
backUp[j][k] = field[j][k];
bool ok = false;
// 塗りつぶし
memset(used, 0, sizeof(used));
fillField(0, 0, field[0][0], i, ok, false);
// もし一つ以上属する色が増えるならdfs
if (ok)
dfs(cnt + 1);
for (int j = 0; j < H; j++)
for (int k = 0; k < W; k++)
field[j][k] = backUp[j][k];
}
}
int greedy() {
{
int goal = field2[0][0];
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (field2[i][j] != goal)
ok = false;
if (ok)
return 0;
}
for (int i = 0; i < 3; i++) {
if (field2[0][0] == i)
continue;
bool ok = false;
// 塗りつぶし
fillField(0, 0, field2[0][0], i, ok, true);
// もし一つ以上属する色が増えるならdfs
if (ok)
return greedy() + 1;
}
}
int main() {
while (cin >> W >> H && (W | H)) {
res = H * W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char ch;
cin >> ch;
if (ch == 'R')
field[i][j] = 0;
else if (ch == 'G')
field[i][j] = 1;
else if (ch == 'B')
field[i][j] = 2;
field2[i][j] = field[i][j];
}
}
lim = greedy();
res = lim;
dfs(0);
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int W, H;
int field[20][20];
int field2[21][21];
int res;
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
bool used[101][101];
int lim;
void fillField(int cy, int cx, int oldc, int newc, bool &ok, bool flag) {
if (!flag)
field[cy][cx] = newc;
else
field2[cy][cx] = newc;
used[cy][cx] = true;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && !used[ny][nx]) {
if (!flag) {
if (field[ny][nx] == oldc)
fillField(ny, nx, oldc, newc, ok, flag);
else if (field[ny][nx] == newc)
ok = true;
} else {
if (field2[ny][nx] == oldc)
fillField(ny, nx, oldc, newc, ok, flag);
else if (field2[ny][nx] == newc)
ok = true;
}
}
}
}
void dfs(int cnt) {
if (res <= cnt)
return;
{
int goal = field[0][0];
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (field[i][j] != goal)
ok = false;
if (ok) {
res = min(res, cnt);
return;
}
}
for (int i = 0; i < 3; i++) {
if (field[0][0] == i)
continue;
int backUp[20][20];
for (int j = 0; j < H; j++)
for (int k = 0; k < W; k++)
backUp[j][k] = field[j][k];
bool ok = false;
// 塗りつぶし
memset(used, 0, sizeof(used));
fillField(0, 0, field[0][0], i, ok, false);
// もし一つ以上属する色が増えるならdfs
if (ok)
dfs(cnt + 1);
for (int j = 0; j < H; j++)
for (int k = 0; k < W; k++)
field[j][k] = backUp[j][k];
}
}
int greedy() {
{
int goal = field2[0][0];
bool ok = true;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (field2[i][j] != goal)
ok = false;
if (ok)
return 0;
}
for (int i = 0; i < 3; i++) {
if (field2[0][0] == i)
continue;
bool ok = false;
// 塗りつぶし
fillField(0, 0, field2[0][0], i, ok, true);
// もし一つ以上属する色が増えるならdfs
if (ok)
return greedy() + 1;
}
}
int main() {
while (cin >> W >> H && (W | H)) {
res = H * W;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char ch;
cin >> ch;
if (ch == 'R')
field[i][j] = 0;
else if (ch == 'G')
field[i][j] = 1;
else if (ch == 'B')
field[i][j] = 2;
field2[i][j] = field[i][j];
}
}
lim = greedy();
res = lim;
dfs(0);
cout << res << endl;
}
return 0;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 1 << 25;
int ans;
int b[10][10];
int W, H;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool in(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
void copy_board(int dst[10][10], int src[10][10]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
dst[i][j] = src[i][j];
}
}
}
typedef pair<int, int> P;
int vis[10][10];
void color(int b[10][10], int c) {
memset(vis, 0, sizeof vis);
int cc = b[0][0];
queue<P> q;
q.push({0, 0});
b[0][0] = c;
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (in(nx, ny) && b[ny][nx] == cc && !vis[ny][nx]) {
vis[ny][nx] = 1;
b[ny][nx] = c;
q.push({nx, ny});
}
}
}
}
void dfs(int n) {
if (n == 20)
return;
for (int c = 0; c < 3; c++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
cnt += count(b[i], b[i] + W, c);
}
if (cnt == H * W) {
ans = min(ans, n);
return;
}
}
int tmp[10][10];
copy_board(tmp, b);
for (int i = 0; i < 3; i++) {
copy_board(b, tmp);
if (b[0][0] != i) {
color(b, i);
dfs(n + 1);
}
}
copy_board(b, tmp);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c;
cin >> c;
if (c == 'R')
b[i][j] = 0;
if (c == 'G')
b[i][j] = 1;
if (c == 'B')
b[i][j] = 2;
}
}
ans = INF;
dfs(0);
cout << ans << endl;
}
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 1 << 25;
int ans;
int b[10][10];
int W, H;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool in(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
void copy_board(int dst[10][10], int src[10][10]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
dst[i][j] = src[i][j];
}
}
}
typedef pair<int, int> P;
int vis[10][10];
void color(int b[10][10], int c) {
memset(vis, 0, sizeof vis);
int cc = b[0][0];
queue<P> q;
q.push({0, 0});
b[0][0] = c;
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (in(nx, ny) && b[ny][nx] == cc && !vis[ny][nx]) {
vis[ny][nx] = 1;
b[ny][nx] = c;
q.push({nx, ny});
}
}
}
}
void dfs(int n) {
if (n == 20 || ans < n)
return;
for (int c = 0; c < 3; c++) {
int cnt = 0;
for (int i = 0; i < H; i++) {
cnt += count(b[i], b[i] + W, c);
}
if (cnt == H * W) {
ans = min(ans, n);
return;
}
}
int tmp[10][10];
copy_board(tmp, b);
for (int i = 0; i < 3; i++) {
copy_board(b, tmp);
if (b[0][0] != i) {
color(b, i);
dfs(n + 1);
}
}
copy_board(b, tmp);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c;
cin >> c;
if (c == 'R')
b[i][j] = 0;
if (c == 'G')
b[i][j] = 1;
if (c == 'B')
b[i][j] = 2;
}
}
ans = INF;
dfs(0);
cout << ans << endl;
}
} | replace | 46 | 47 | 46 | 47 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
typedef unsigned long long UInt64;
typedef long long Int64;
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
int ans = INF;
int x, y;
inline int change(string ss) {
if (ss == "R")
return 0;
if (ss == "G")
return 1;
if (ss == "B")
return 2;
}
inline bool check(vector<vector<int>> tile) {
rep(i, tile.size()) {
rep(j, tile[i].size()) {
if (tile[0][0] != tile[i][j])
return false;
}
}
return true;
}
inline void f(vector<vector<int>> &tile, int color) {
// x,y
queue<P> que;
int oldcolor = tile[0][0];
tile[0][0] = color;
que.push(P(0, 0));
while (que.size()) {
P p = que.front();
que.pop();
rep(i, 4) {
int nx = p.fr + dx[i];
int ny = p.sc + dy[i];
if (0 <= nx && nx < x && 0 <= ny && ny < y && tile[nx][ny] == oldcolor) {
tile[nx][ny] = color;
que.push(P(nx, ny));
}
}
}
}
inline void dfs(vector<vector<int>> tile, int depth) {
if (check(tile)) {
ans = min(ans, depth);
return;
}
// 最大でも18手を越えないらしい
if (depth == 18)
return;
vector<vector<int>> tile1 = tile;
vector<vector<int>> tile2 = tile;
if (tile[0][0] == 0) {
f(tile1, 1);
f(tile2, 2);
}
if (tile[0][0] == 1) {
f(tile1, 0);
f(tile2, 2);
}
if (tile[0][0] == 2) {
f(tile1, 1);
f(tile2, 0);
}
dfs(tile1, depth + 1);
dfs(tile2, depth + 1);
}
inline void solve() {
vector<vector<int>> tile;
tile.reserve(10);
rep(i, x) {
vector<int> vec;
vec.reserve(10);
rep(j, y) {
string c;
cin >> c;
vec.pb(change(c));
}
tile.pb(vec);
}
dfs(tile, 0);
}
int main() {
while (cin >> x >> y) {
swap(x, y);
if (x == 0 && y == 0)
return 0;
solve();
cout << ans << endl;
ans = INF;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second
typedef unsigned long long UInt64;
typedef long long Int64;
const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
int ans = INF;
int x, y;
inline int change(string ss) {
if (ss == "R")
return 0;
if (ss == "G")
return 1;
if (ss == "B")
return 2;
}
inline bool check(vector<vector<int>> tile) {
rep(i, tile.size()) {
rep(j, tile[i].size()) {
if (tile[0][0] != tile[i][j])
return false;
}
}
return true;
}
inline void f(vector<vector<int>> &tile, int color) {
// x,y
queue<P> que;
int oldcolor = tile[0][0];
tile[0][0] = color;
que.push(P(0, 0));
while (que.size()) {
P p = que.front();
que.pop();
rep(i, 4) {
int nx = p.fr + dx[i];
int ny = p.sc + dy[i];
if (0 <= nx && nx < x && 0 <= ny && ny < y && tile[nx][ny] == oldcolor) {
tile[nx][ny] = color;
que.push(P(nx, ny));
}
}
}
}
inline void dfs(vector<vector<int>> tile, int depth) {
if (check(tile)) {
ans = min(ans, depth);
return;
}
// 最大でも18手を越えないらしい
if (depth == 17)
return;
vector<vector<int>> tile1 = tile;
vector<vector<int>> tile2 = tile;
if (tile[0][0] == 0) {
f(tile1, 1);
f(tile2, 2);
}
if (tile[0][0] == 1) {
f(tile1, 0);
f(tile2, 2);
}
if (tile[0][0] == 2) {
f(tile1, 1);
f(tile2, 0);
}
dfs(tile1, depth + 1);
dfs(tile2, depth + 1);
}
inline void solve() {
vector<vector<int>> tile;
tile.reserve(10);
rep(i, x) {
vector<int> vec;
vec.reserve(10);
rep(j, y) {
string c;
cin >> c;
vec.pb(change(c));
}
tile.pb(vec);
}
dfs(tile, 0);
}
int main() {
while (cin >> x >> y) {
swap(x, y);
if (x == 0 && y == 0)
return 0;
solve();
cout << ans << endl;
ans = INF;
}
return 0;
} | replace | 90 | 91 | 90 | 91 | TLE | |
p00243 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MAX_W = 10;
int w, h, ans;
bool memo[MAX_W][MAX_W];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char C[3] = {'R', 'G', 'B'};
int c_to_n[256] = {0};
void debug(vector<string> &v, vector<string> &v_, int cnt = 0) {
cout << "[debug] cnt:" << cnt - 1 << " -> " << cnt << endl;
cout << "before:" << endl;
for (int y = 0; y < h; y++) {
cout << v[y] << endl;
}
cout << "after:" << endl;
for (int y = 0; y < h; y++) {
cout << v_[y] << endl;
}
cout << endl;
}
bool check(vector<string> &v, char c) {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (v[y][x] != c) {
return false;
}
}
}
return true;
}
void dfs(int x, int y, char c, vector<string> v, vector<P> &vp) {
memo[y][x] = true;
if (v[y][x] == c) {
vp.push_back(P(x, y));
}
for (int i = 0; i < 4; i++) {
int mx = x + dx[i];
int my = y + dy[i];
if (mx < 0 || my < 0 || w <= mx || h <= my)
continue;
if (!memo[my][mx] && v[my][mx] == c) {
dfs(mx, my, c, v, vp);
}
}
}
void solve(vector<string> &v, int cnt) {
if (ans < cnt)
return;
if (check(v, v[0][0])) {
ans = min(ans, cnt);
return;
}
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
memo[y][x] = false;
}
}
vector<P> vp;
dfs(0, 0, v[0][0], v, vp);
bool color[3] = {false};
for (int i = 0; i < vp.size(); i++) {
for (int j = 0; j < 4; j++) {
int mx = vp[i].first + dx[j];
int my = vp[i].second + dy[j];
if (mx < 0 || my < 0 || w <= mx || h <= my)
continue;
if (v[0][0] == v[my][mx])
continue;
color[c_to_n[v[my][mx]]] = true;
if ((color[0] && color[1]) || (color[0] && color[2]) ||
(color[1] && color[2])) {
break;
}
}
if ((color[0] && color[1]) || (color[0] && color[2]) ||
(color[1] && color[2])) {
break;
}
}
for (int i = 0; i < 3; i++) {
if (!color[i])
continue;
// if( v[0][0] == C[i] ) continue;
vector<string> next = v;
for (int j = 0; j < vp.size(); j++) {
next[vp[j].second][vp[j].first] = C[i];
}
if (cnt + 1 <= ans) {
// debug(v,next,cnt+1);
solve(next, cnt + 1);
}
}
}
int main() {
c_to_n['R'] = 0;
c_to_n['G'] = 1;
c_to_n['B'] = 2;
while (cin >> w >> h, w || h) {
vector<string> v(h);
for (int y = 0; y < h; y++) {
v[y] = string(w, ' ');
for (int x = 0; x < w; x++) {
cin >> v[y][x];
}
}
ans = min(w * h - 1, 17);
solve(v, 0);
cout << ans << endl;
}
} | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MAX_W = 10;
int w, h, ans;
bool memo[MAX_W][MAX_W];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char C[3] = {'R', 'G', 'B'};
int c_to_n[256] = {0};
void debug(vector<string> &v, vector<string> &v_, int cnt = 0) {
cout << "[debug] cnt:" << cnt - 1 << " -> " << cnt << endl;
cout << "before:" << endl;
for (int y = 0; y < h; y++) {
cout << v[y] << endl;
}
cout << "after:" << endl;
for (int y = 0; y < h; y++) {
cout << v_[y] << endl;
}
cout << endl;
}
bool check(vector<string> &v, char c) {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (v[y][x] != c) {
return false;
}
}
}
return true;
}
void dfs(int x, int y, char c, vector<string> &v, vector<P> &vp) {
memo[y][x] = true;
if (v[y][x] == c) {
vp.push_back(P(x, y));
}
for (int i = 0; i < 4; i++) {
int mx = x + dx[i];
int my = y + dy[i];
if (mx < 0 || my < 0 || w <= mx || h <= my)
continue;
if (!memo[my][mx] && v[my][mx] == c) {
dfs(mx, my, c, v, vp);
}
}
}
void solve(vector<string> &v, int cnt) {
if (ans < cnt)
return;
if (check(v, v[0][0])) {
ans = min(ans, cnt);
return;
}
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
memo[y][x] = false;
}
}
vector<P> vp;
dfs(0, 0, v[0][0], v, vp);
bool color[3] = {false};
for (int i = 0; i < vp.size(); i++) {
for (int j = 0; j < 4; j++) {
int mx = vp[i].first + dx[j];
int my = vp[i].second + dy[j];
if (mx < 0 || my < 0 || w <= mx || h <= my)
continue;
if (v[0][0] == v[my][mx])
continue;
color[c_to_n[v[my][mx]]] = true;
if ((color[0] && color[1]) || (color[0] && color[2]) ||
(color[1] && color[2])) {
break;
}
}
if ((color[0] && color[1]) || (color[0] && color[2]) ||
(color[1] && color[2])) {
break;
}
}
for (int i = 0; i < 3; i++) {
if (!color[i])
continue;
// if( v[0][0] == C[i] ) continue;
vector<string> next = v;
for (int j = 0; j < vp.size(); j++) {
next[vp[j].second][vp[j].first] = C[i];
}
if (cnt + 1 <= ans) {
// debug(v,next,cnt+1);
solve(next, cnt + 1);
}
}
}
int main() {
c_to_n['R'] = 0;
c_to_n['G'] = 1;
c_to_n['B'] = 2;
while (cin >> w >> h, w || h) {
vector<string> v(h);
for (int y = 0; y < h; y++) {
v[y] = string(w, ' ');
for (int x = 0; x < w; x++) {
cin >> v[y][x];
}
}
ans = min(w * h - 1, 17);
solve(v, 0);
cout << ans << endl;
}
} | replace | 41 | 42 | 41 | 42 | TLE | |
p00244 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
class State {
public:
int id, cost;
bool used;
State(int _id, int _cost, bool _used) {
id = _id;
cost = _cost;
used = _used;
}
bool operator<(const State &st) const { return cost > st.cost; }
};
int n, m;
bool closed[102][2];
vector<P> t[102];
void solve() {
priority_queue<State> open;
memset(closed, 0, sizeof(closed));
open.push(State(0, 0, 0));
while (!open.empty()) {
State st = open.top();
open.pop();
if (closed[st.id][st.used])
continue;
closed[st.id][st.used] = true;
if (st.id == n - 1) {
cout << st.cost << endl;
return;
}
for (int i = 0; i < t[st.id].size(); i++) {
int to = t[st.id][i].first;
int cost = t[st.id][i].second;
open.push(State(to, st.cost + cost, st.used));
}
if (st.used)
continue;
for (int i = 0; i < t[st.id].size(); i++) {
int a = t[st.id][i].first;
for (int j = 0; j < t[a].size(); j++) {
int b = t[a][j].first;
open.push(State(b, st.cost, true));
}
}
}
while (true) {
}
}
int main() {
while (cin >> n >> m, n || m) {
for (int i = 0; i < n; i++) {
t[i].clear();
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
t[a].push_back(P(b, c));
}
solve();
}
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
class State {
public:
int id, cost;
bool used;
State(int _id, int _cost, bool _used) {
id = _id;
cost = _cost;
used = _used;
}
bool operator<(const State &st) const { return cost > st.cost; }
};
int n, m;
bool closed[102][2];
vector<P> t[102];
void solve() {
priority_queue<State> open;
memset(closed, 0, sizeof(closed));
open.push(State(0, 0, 0));
while (!open.empty()) {
State st = open.top();
open.pop();
if (closed[st.id][st.used])
continue;
closed[st.id][st.used] = true;
if (st.id == n - 1) {
cout << st.cost << endl;
return;
}
for (int i = 0; i < t[st.id].size(); i++) {
int to = t[st.id][i].first;
int cost = t[st.id][i].second;
open.push(State(to, st.cost + cost, st.used));
}
if (st.used)
continue;
for (int i = 0; i < t[st.id].size(); i++) {
int a = t[st.id][i].first;
for (int j = 0; j < t[a].size(); j++) {
int b = t[a][j].first;
open.push(State(b, st.cost, true));
}
}
}
while (true) {
}
}
int main() {
while (cin >> n >> m, n || m) {
for (int i = 0; i < n; i++) {
t[i].clear();
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
t[a].push_back(P(b, c));
t[b].push_back(P(a, c));
}
solve();
}
} | insert | 92 | 92 | 92 | 93 | TLE | |
p00246 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;
int list(int n, int k, int g) {
int i;
if (k == 10 && n == h) {
for (i = 0; i < h; i++)
da[v][a[i]]++;
dn[v] = h;
v++;
}
if (n == h || k >= 10)
return 1;
for (i = g; i < 10; i++) {
a[n] = i;
list(n + 1, k + i, i);
}
return 0;
}
int dfs(int g, int u, int t) {
int i, j, k;
if (an < g)
an = g;
if (g + t / dn[u] <= an)
return 0;
for (i = u; i < 41; i++) {
f = 0;
for (j = 1; j < 10; j++) {
a[j] -= da[i][j];
if (a[j] < 0) {
f = 1;
for (k = j; k > 0; k--)
a[k] += da[i][k];
break;
}
}
if (f == 0) {
dfs(g + 1, i, t - dn[i]);
for (j = 1; j < 10; j++)
a[j] += da[i][j];
}
}
return 0;
}
int main() {
int i, j;
v = 0;
for (i = 0; i < 40; i++)
for (j = 0; j < 11; j++)
da[i][j] = 0;
for (i = 2; i < 11; i++) {
h = i;
list(0, 0, 1);
}
while (cin >> n && n > 0) {
for (i = 0; i < 11; i++)
a[i] = 0;
for (i = 0; i < n; i++) {
cin >> j;
a[j]++;
}
v = 0;
an = 0;
dfs(0, 0, n);
cout << an << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int a[11], da[43][11], dn[43], h, v, n, f, an, z;
int list(int n, int k, int g) {
int i;
if (k == 10 && n == h) {
for (i = 0; i < h; i++)
da[v][a[i]]++;
dn[v] = h;
v++;
}
if (n == h || k >= 10)
return 1;
for (i = g; i < 10; i++) {
a[n] = i;
list(n + 1, k + i, i);
}
return 0;
}
int dfs(int g, int u, int t) {
int i, j, k;
if (an < g)
an = g;
if (g + t / dn[u] <= an)
return 0;
for (i = u; i < 41; i++) {
f = 0;
for (j = 1; j < 10; j++) {
a[j] -= da[i][j];
if (a[j] < 0) {
f = 1;
for (k = j; k > 0; k--)
a[k] += da[i][k];
break;
}
}
if (f == 0) {
dfs(g + 1, i, t - dn[i]);
for (j = 1; j < 10; j++)
a[j] += da[i][j];
}
}
return 0;
}
int main() {
int i, j;
v = 0;
for (i = 0; i < 40; i++)
for (j = 0; j < 11; j++)
da[i][j] = 0;
for (i = 2; i < 11; i++) {
h = i;
list(0, 0, 1);
}
while (cin >> n && n > 0) {
for (i = 0; i < 11; i++)
a[i] = 0;
for (i = 0; i < n; i++) {
cin >> j;
a[j]++;
}
v = 0;
an = 0;
while (v < 5) {
while (true) {
f = 0;
for (i = 1; i < 10; i++)
if (a[i] < da[v][i]) {
f = 1;
break;
}
if (f == 1)
break;
for (i = 1; i < 10; i++)
a[i] -= da[v][i];
an++;
n -= 2;
}
v++;
}
dfs(an, 5, n);
cout << an << endl;
}
return 0;
} | replace | 66 | 67 | 66 | 84 | TLE | |
p00246 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) \
for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
template <class T> string to_s(const T &a) {
ostringstream os;
os << a;
return os.str();
}
template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
cout << "{ ";
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n)
cout << deli;
}
cout << " }";
while (br--)
cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1)
cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--)
cout << endl;
}
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i] >> b[i];
}
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
typedef pair<ll, ll> P;
ll enc(int *s) {
ll res = 0;
for (int i = 0; i < 5; ++i)
res = (res << 8) | s[i];
return res;
}
P encode(int *s) { return P(enc(s + 1), enc(s + 5)); }
int n, a[128];
int res, possi_max;
int sack[11];
set<P> memo[101];
void dfs(int d) {
if (res == possi_max || sack[10] + (n - d) <= res ||
memo[d].count(encode(sack)))
return;
else if (d == n) {
res = sack[10];
return;
}
memo[d].insert(encode(sack));
for (int i = 10 - a[d]; i >= 0; --i) {
if (sack[i] > 0) {
int j = a[d] + i;
--sack[i];
++sack[j];
dfs(d + 1);
++sack[i];
--sack[j];
}
}
}
int solve() {
sort(a, a + n, greater<int>());
for (int i = 0; i < n; ++i)
memo[i].clear();
possi_max = accumulate(a, a + n, 0) / 10;
res = 0;
dfs(0);
return res;
}
int main() {
ios::sync_with_stdio(false);
sack[0] = 114514;
while (cin >> n, n) {
input(a, n);
cout << solve() << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#if __GNUC__
#include <tr1/unordered_map>
#include <tr1/unordered_set>
using namespace tr1;
#else
#include <unordered_map>
#include <unordered_set>
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return __builtin_popcount(n); }
template <> int popcount(int n) { return __builtin_popcount(n); }
template <> int popcount(unsigned long long n) {
return __builtin_popcountll(n);
}
template <> int popcount(long long n) { return __builtin_popcountll(n); }
#else
#define __typeof__ decltype
template <class T> int popcount(T n) {
return n ? 1 + popcount(n & (n - 1)) : 0;
}
#endif
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) \
for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
template <class T> string to_s(const T &a) {
ostringstream os;
os << a;
return os.str();
}
template <class T>
void print(T a, int n, int br = 1, const string &deli = ", ") {
cout << "{ ";
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n)
cout << deli;
}
cout << " }";
while (br--)
cout << endl;
}
template <class T>
void print(const vector<T> &v, int br = 1, const string &deli = ", ") {
print(v, v.size(), br, deli);
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1)
cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--)
cout << endl;
}
template <class T> void input(T &a, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i];
}
template <class T, class U> void input(T &a, U &b, int n) {
for (int i = 0; i < n; ++i)
cin >> a[i] >> b[i];
}
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
typedef pair<ll, ll> P;
ll enc(int *s) {
ll res = 0;
for (int i = 0; i < 5; ++i)
res = (res << 8) | s[i];
return res;
}
P encode(int *s) { return P(enc(s + 1), enc(s + 5)); }
int n, a[128];
int res, possi_max;
int sack[11];
set<P> memo[101];
void dfs(int d) {
if (res == possi_max || sack[10] + (n - d) <= res ||
memo[d].count(encode(sack)))
return;
else if (d == n) {
res = sack[10];
return;
}
memo[d].insert(encode(sack));
for (int i = 10 - a[d]; i >= 0; --i) {
if (sack[i] > 0) {
int j = a[d] + i;
--sack[i];
++sack[j];
dfs(d + 1);
++sack[i];
--sack[j];
}
}
}
int solve() {
sort(a, a + n, greater<int>());
for (int i = 0; i < 100; ++i)
memo[i].clear();
possi_max = accumulate(a, a + n, 0) / 10;
res = 0;
dfs(0);
return res;
}
int main() {
ios::sync_with_stdio(false);
sack[0] = 114514;
while (cin >> n, n) {
input(a, n);
cout << solve() << endl;
}
} | replace | 162 | 163 | 162 | 163 | MLE | |
p00246 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
// int N;
// int MS[101];
// vector<map<char,char> > howToGet;
// map<map<char,char>,char> dp[43];
// void dfs(const int kind,int wgt,map<char,char> v){
// if(kind==0){
// map<char,char> nv=v;
// dfs(kind+1,wgt,nv);
// }
// else if(wgt==0)howToGet.push_back(v);
// else if(kind==10)return;
// else{
// for(int i=0;i*kind<=10;i++){
// if(wgt-i*kind>=0){
// map<char,char> nv=v;
// if(i>=1)nv[kind]=i;
// dfs(kind+1,wgt-i*kind,nv);
// }
// }
// }
// }
// void dfs2(const int pos,const map<char,char> v,int cnt,int &maxVal,int
// preLast){
// if(pos==(int)howToGet.size())return;
// if(preLast/10+cnt<=maxVal)return;
// // 1セット分取れるか
// map<char,char> nv=v;
// bool ok=true;
// int sz=0;
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++){
// if(nv.count(it->first)==0
// ||nv[it->first]-(it->second)<0){
// ok=false;
// break;
// }
// else{
// nv[it->first]-=it->second;
// sz+=it->second;
// }
// }
// // 1セット取れるならとる
// if(ok){
// maxVal=max(maxVal,cnt+1);
// dfs2(pos,nv,cnt+1,maxVal,preLast-sz);
// }
// // 取らずに次のセットへ
// dfs2(pos+1,v,cnt,maxVal,preLast);
// }
// void greedy(int &maxVal,map<char,char> v){
// for(int pos=0;pos<(int)howToGet.size();pos++){
// while(1){
// bool ok=true;
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++){
// if(v.count(it->first)==0||v[it->first]-(it->second)<0){
// ok=false;
// break;
// }
// }
// if(ok){
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++)
// v[it->first]-=it->second;
// maxVal++;
// }
// else
// break;
// }
// }
// }
// int main(){
// dfs(0,10,map<char,char>());
// while(cin>>N&&N){
// map<char,char> cnts;
// int preLast=0;
// for(int i=0;i<N;i++)cin>>MS[i];
// for(int i=0;i<N;i++){
// cnts[MS[i]]++;
// preLast+=MS[i];
// }
// for(int i=0;i<43;i++)dp[i].clear();
// int maxVal=0;
// greedy(maxVal,cnts);
// // cout<<maxVal<<endl;
// // dfs2(0,cnts,0,maxVal,preLast);
// cout<<maxVal<<endl;
// }
// return 0;
// }
int N;
int MS[101];
vector<vector<char>> howToGet;
map<vector<char>, char> dp;
void dfs(int kind, int wgt, const vector<char> &v) {
if (kind == 0) {
vector<char> nv = v;
nv.push_back(0);
dfs(kind + 1, wgt, nv);
} else if (wgt == 0)
howToGet.push_back(v);
else if (kind == 10)
return;
else {
for (int i = 0; i * kind <= 10; i++) {
if (wgt - i * kind >= 0) {
vector<char> nv = v;
nv.push_back(i);
dfs(kind + 1, wgt - i * kind, nv);
}
}
}
}
int dfs2(const vector<char> &v) {
if (dp.count(v) != 0)
return dp[v];
int res = 0;
for (int pos = 0; pos < (int)howToGet.size(); pos++) {
// 1セット分取れるか
vector<char> nv = v;
bool ok = true;
for (int j = 1; j < (int)howToGet[pos].size(); j++) {
nv[j] -= howToGet[pos][j];
if (nv[j] < 0) {
ok = false;
break;
}
}
// iセット取れるならとる
if (ok)
res = max(res, dfs2(nv) + 1);
}
return dp[v] = res;
}
int main() {
dfs(0, 10, vector<char>());
while (cin >> N && N) {
vector<char> cnts;
for (int i = 0; i < N; i++)
cin >> MS[i];
for (int i = 0; i <= 10; i++)
cnts.push_back(0);
for (int i = 0; i < N; i++)
cnts[MS[i]]++;
int a = min(cnts[9], cnts[1]);
cnts[9] -= a;
cnts[1] -= a;
int b = min(cnts[8], cnts[2]);
cnts[8] -= b;
cnts[2] -= b;
int c = min((int)cnts[8], (int)cnts[1] / 2);
cnts[8] -= c;
cnts[2] -= c;
int res = dfs2(cnts) + a + b + c;
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
// int N;
// int MS[101];
// vector<map<char,char> > howToGet;
// map<map<char,char>,char> dp[43];
// void dfs(const int kind,int wgt,map<char,char> v){
// if(kind==0){
// map<char,char> nv=v;
// dfs(kind+1,wgt,nv);
// }
// else if(wgt==0)howToGet.push_back(v);
// else if(kind==10)return;
// else{
// for(int i=0;i*kind<=10;i++){
// if(wgt-i*kind>=0){
// map<char,char> nv=v;
// if(i>=1)nv[kind]=i;
// dfs(kind+1,wgt-i*kind,nv);
// }
// }
// }
// }
// void dfs2(const int pos,const map<char,char> v,int cnt,int &maxVal,int
// preLast){
// if(pos==(int)howToGet.size())return;
// if(preLast/10+cnt<=maxVal)return;
// // 1セット分取れるか
// map<char,char> nv=v;
// bool ok=true;
// int sz=0;
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++){
// if(nv.count(it->first)==0
// ||nv[it->first]-(it->second)<0){
// ok=false;
// break;
// }
// else{
// nv[it->first]-=it->second;
// sz+=it->second;
// }
// }
// // 1セット取れるならとる
// if(ok){
// maxVal=max(maxVal,cnt+1);
// dfs2(pos,nv,cnt+1,maxVal,preLast-sz);
// }
// // 取らずに次のセットへ
// dfs2(pos+1,v,cnt,maxVal,preLast);
// }
// void greedy(int &maxVal,map<char,char> v){
// for(int pos=0;pos<(int)howToGet.size();pos++){
// while(1){
// bool ok=true;
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++){
// if(v.count(it->first)==0||v[it->first]-(it->second)<0){
// ok=false;
// break;
// }
// }
// if(ok){
// for(map<char,char>::iterator
// it=howToGet[pos].begin();it!=howToGet[pos].end();it++)
// v[it->first]-=it->second;
// maxVal++;
// }
// else
// break;
// }
// }
// }
// int main(){
// dfs(0,10,map<char,char>());
// while(cin>>N&&N){
// map<char,char> cnts;
// int preLast=0;
// for(int i=0;i<N;i++)cin>>MS[i];
// for(int i=0;i<N;i++){
// cnts[MS[i]]++;
// preLast+=MS[i];
// }
// for(int i=0;i<43;i++)dp[i].clear();
// int maxVal=0;
// greedy(maxVal,cnts);
// // cout<<maxVal<<endl;
// // dfs2(0,cnts,0,maxVal,preLast);
// cout<<maxVal<<endl;
// }
// return 0;
// }
int N;
int MS[101];
vector<vector<char>> howToGet;
map<vector<char>, char> dp;
void dfs(int kind, int wgt, const vector<char> &v) {
if (kind == 0) {
vector<char> nv = v;
nv.push_back(0);
dfs(kind + 1, wgt, nv);
} else if (wgt == 0)
howToGet.push_back(v);
else if (kind == 10)
return;
else {
for (int i = 0; i * kind <= 10; i++) {
if (wgt - i * kind >= 0) {
vector<char> nv = v;
nv.push_back(i);
dfs(kind + 1, wgt - i * kind, nv);
}
}
}
}
int dfs2(const vector<char> &v) {
if (dp.count(v) != 0)
return dp[v];
int res = 0;
for (int pos = 0; pos < (int)howToGet.size(); pos++) {
// 1セット分取れるか
vector<char> nv = v;
bool ok = true;
for (int j = 1; j < (int)howToGet[pos].size(); j++) {
nv[j] -= howToGet[pos][j];
if (nv[j] < 0) {
ok = false;
break;
}
}
// iセット取れるならとる
if (ok)
res = max(res, dfs2(nv) + 1);
}
return dp[v] = res;
}
int main() {
dfs(0, 10, vector<char>());
while (cin >> N && N) {
vector<char> cnts;
for (int i = 0; i < N; i++)
cin >> MS[i];
for (int i = 0; i <= 10; i++)
cnts.push_back(0);
for (int i = 0; i < N; i++)
cnts[MS[i]]++;
int sum = 0;
// for(int i=1;i<=9;i++)
// cout<<(int)cnts[i]<<" ";
// cout<<endl;
for (int i = 1; i <= 4; i++) {
int a = min(cnts[i], cnts[10 - i]);
cnts[i] -= a;
cnts[10 - i] -= a;
sum += a;
}
int a = cnts[5] / 2;
cnts[5] -= 2 * a;
sum += a;
// cout<<sum<<endl;
int res = dfs2(cnts) + sum;
cout << res << endl;
}
return 0;
} | replace | 159 | 169 | 159 | 174 | TLE | |
p00247 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAXH = 13;
const int MAXW = 13;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};
const int INF = 1 << 28;
int W, H;
char G[MAXH][MAXW];
int si, sj, ti, tj;
int N;
int ice[MAXH][MAXW];
int ans;
bool vis[MAXH][MAXW];
int cnt[1000];
int fromG[MAXH][MAXW];
int dfs(int pi, int pj, int n) {
int res = 1;
ice[pi][pj] = n;
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (G[ni][nj] != 'X')
continue;
if (ice[ni][nj] != -1)
continue;
res += dfs(ni, nj, n);
}
return res;
}
void bfs(int si, int sj) {
queue<pair<int, int>> que;
fill(fromG[0], fromG[MAXH], INF);
que.push(make_pair(si, sj));
fromG[si][sj] = 0;
while (que.size()) {
int pi = que.front().first;
int pj = que.front().second;
que.pop();
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (G[ni][nj] == '#')
continue;
if (fromG[ni][nj] != INF)
continue;
que.push(make_pair(ni, nj));
fromG[ni][nj] = fromG[pi][pj] + 1;
}
}
}
void dfs2(int pi, int pj, int previ, int prevj, int cost) {
if (cost + fromG[pi][pj] >= ans)
return;
if (pi == ti && pj == tj) {
ans = min(ans, cost);
return;
}
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (ni == previ && nj == prevj)
continue;
if (vis[ni][nj])
return;
}
vis[pi][pj] = true;
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (vis[ni][nj])
continue;
if (G[ni][nj] == '#')
continue;
if (G[ni][nj] == 'X' && cnt[ice[ni][nj]] == 0)
continue;
if (G[ni][nj] == 'X')
cnt[ice[ni][nj]] -= 1;
dfs2(ni, nj, pi, pj, cost + 1);
if (G[ni][nj] == 'X')
cnt[ice[ni][nj]] += 1;
}
vis[pi][pj] = false;
}
int main() {
while (cin >> W >> H && (W | H)) {
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> G[i][j];
if (G[i][j] == 'S') {
si = i;
sj = j;
G[i][j] = '.';
}
if (G[i][j] == 'G') {
ti = i;
tj = j;
G[i][j] = '.';
}
}
}
fill(ice[0], ice[MAXH], -1);
N = 0;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (G[i][j] == 'X' && ice[i][j] == -1) {
cnt[N] = dfs(i, j, N) / 2;
if (cnt[N] == 0) {
ice[i][j] = -1;
G[i][j] = '#';
} else {
++N;
}
}
}
}
bfs(ti, tj);
ans = INF;
fill(vis[0], vis[MAXH], 0);
dfs2(si, sj, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int MAXH = 13;
const int MAXW = 13;
const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};
const int INF = 1 << 28;
int W, H;
char G[MAXH][MAXW];
int si, sj, ti, tj;
int N;
int ice[MAXH][MAXW];
int ans;
bool vis[MAXH][MAXW];
int cnt[1000];
int fromG[MAXH][MAXW];
int dfs(int pi, int pj, int n) {
int res = 1;
ice[pi][pj] = n;
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (G[ni][nj] != 'X')
continue;
if (ice[ni][nj] != -1)
continue;
res += dfs(ni, nj, n);
}
return res;
}
void bfs(int si, int sj) {
queue<pair<int, int>> que;
fill(fromG[0], fromG[MAXH], INF);
que.push(make_pair(si, sj));
fromG[si][sj] = 0;
while (que.size()) {
int pi = que.front().first;
int pj = que.front().second;
que.pop();
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (G[ni][nj] == '#')
continue;
if (fromG[ni][nj] != INF)
continue;
que.push(make_pair(ni, nj));
fromG[ni][nj] = fromG[pi][pj] + 1;
}
}
}
void dfs2(int pi, int pj, int previ, int prevj, int cost) {
if (cost + fromG[pi][pj] >= ans)
return;
if (pi == ti && pj == tj) {
ans = min(ans, cost);
return;
}
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (ni == previ && nj == prevj)
continue;
if (vis[ni][nj])
return;
}
vis[pi][pj] = true;
for (int k = 0; k < 4; ++k) {
int ni = pi + di[k];
int nj = pj + dj[k];
if (ni < 0 || ni >= H)
continue;
if (nj < 0 || nj >= W)
continue;
if (vis[ni][nj])
continue;
if (G[ni][nj] == '#')
continue;
if (G[ni][nj] == 'X' && cnt[ice[ni][nj]] == 0)
continue;
if (G[ni][nj] == 'X')
cnt[ice[ni][nj]] -= 1;
dfs2(ni, nj, pi, pj, cost + 1);
if (G[ni][nj] == 'X')
cnt[ice[ni][nj]] += 1;
}
vis[pi][pj] = false;
}
int main() {
while (cin >> W >> H && (W | H)) {
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> G[i][j];
if (G[i][j] == 'S') {
si = i;
sj = j;
G[i][j] = '.';
}
if (G[i][j] == 'G') {
ti = i;
tj = j;
G[i][j] = '.';
}
}
}
fill(ice[0], ice[MAXH], -1);
N = 0;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (G[i][j] == 'X' && ice[i][j] == -1) {
cnt[N] = dfs(i, j, N) / 2;
if (cnt[N] == 0) {
ice[i][j] = -1;
G[i][j] = '#';
} else {
++N;
}
}
}
}
bfs(ti, tj);
ans = INF;
fill(vis[0], vis[MAXH], 0);
dfs2(si, sj, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | replace | 8 | 10 | 8 | 10 | TLE | |
p00247 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define LOG(...) fprintf(stderr, __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(ALL(c))
#define RSORT(c) sort(RALL(c))
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;
#define INF 1e9
int W, H;
int start_x, start_y, goal_x, goal_y;
int *ices[12][12];
char maze[12][12];
bool G[12][12];
int depth;
int next_depth;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void dfs_count(int *ice, int x, int y) {
(*ice)++;
ices[y][x] = ice;
maze[y][x] = '?'; // used
REP(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (0 <= sx && sx < W && 0 <= sy && sy < H) {
if (maze[sy][sx] == 'X') {
dfs_count(ice, sx, sy);
}
}
}
}
bool iddfs(int x, int y, int step) {
if (x == goal_x && y == goal_y)
return true;
if (step == depth) {
next_depth = min(next_depth, depth + abs(x - goal_x) + abs(y - goal_y));
return false;
}
REP(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (0 <= sx && sx < W && 0 <= sy && sy < H) {
if (*ices[sy][sx] > 0 && !G[sy][sx]) {
(*ices[sy][sx])--;
G[sy][sx] = true;
if (iddfs(sx, sy, step + 1))
return true;
G[sy][sx] = false;
(*ices[sy][sx])++;
}
}
}
return false;
}
int main() {
while (cin >> W >> H, W | H) {
REP(y, H) REP(x, W) cin >> maze[y][x];
int empty = 100000;
int wall = 0;
int ice[12][12] = {};
REP(y, H) REP(x, W) {
switch (maze[y][x]) {
case '.':
ices[y][x] = ∅
break;
case '#':
ices[y][x] = &wall;
break;
case 'S':
start_x = x;
start_y = y;
ices[y][x] = ∅
break;
case 'G':
goal_x = x;
goal_y = y;
ices[y][x] = ∅
break;
case 'X': {
dfs_count(&ice[y][x], x, y);
ice[y][x] /= 2;
ices[y][x] = &ice[y][x];
break;
}
}
maze[y][x] = '?'; // used
}
fill_n((bool *)G, 12 * 12, false);
depth = abs(start_x - goal_x) + abs(start_y - goal_y);
while (!iddfs(start_x, start_y, 0)) {
depth = next_depth;
next_depth = INF;
}
cout << depth << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define LOG(...) fprintf(stderr, __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(ALL(c))
#define RSORT(c) sort(RALL(c))
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;
#define INF 1e9
int W, H;
int start_x, start_y, goal_x, goal_y;
int *ices[12][12];
char maze[12][12];
bool G[12][12];
int depth;
int next_depth;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void dfs_count(int *ice, int x, int y) {
(*ice)++;
ices[y][x] = ice;
maze[y][x] = '?'; // used
REP(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (0 <= sx && sx < W && 0 <= sy && sy < H) {
if (maze[sy][sx] == 'X') {
dfs_count(ice, sx, sy);
}
}
}
}
bool iddfs(int x, int y, int step) {
if (x == goal_x && y == goal_y)
return true;
int dist = abs(x - goal_x) + abs(y - goal_y);
if (step + dist > depth) {
next_depth = min(next_depth, step + dist);
return false;
}
REP(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (0 <= sx && sx < W && 0 <= sy && sy < H) {
if (*ices[sy][sx] > 0 && !G[sy][sx]) {
(*ices[sy][sx])--;
G[sy][sx] = true;
if (iddfs(sx, sy, step + 1))
return true;
G[sy][sx] = false;
(*ices[sy][sx])++;
}
}
}
return false;
}
int main() {
while (cin >> W >> H, W | H) {
REP(y, H) REP(x, W) cin >> maze[y][x];
int empty = 100000;
int wall = 0;
int ice[12][12] = {};
REP(y, H) REP(x, W) {
switch (maze[y][x]) {
case '.':
ices[y][x] = ∅
break;
case '#':
ices[y][x] = &wall;
break;
case 'S':
start_x = x;
start_y = y;
ices[y][x] = ∅
break;
case 'G':
goal_x = x;
goal_y = y;
ices[y][x] = ∅
break;
case 'X': {
dfs_count(&ice[y][x], x, y);
ice[y][x] /= 2;
ices[y][x] = &ice[y][x];
break;
}
}
maze[y][x] = '?'; // used
}
fill_n((bool *)G, 12 * 12, false);
depth = abs(start_x - goal_x) + abs(start_y - goal_y);
while (!iddfs(start_x, start_y, 0)) {
depth = next_depth;
next_depth = INF;
}
cout << depth << endl;
}
} | replace | 55 | 57 | 55 | 58 | TLE | |
p00247 | C++ | Time Limit Exceeded | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char field[12][12];
int ifield[12][12];
int W, H;
struct State {
int d, y, x;
vec ice;
};
string state2str(State &s) {
string ret = "";
ret.push_back(s.d + '0');
ret.push_back(s.y + '0');
ret.push_back(s.x + '0');
for (int i = 0; i < s.ice.size(); i++) {
ret.push_back(s.ice[i] + '0');
}
return ret;
}
int lump_ice_num;
int ice_num[12 * 12];
int dfs(int y, int x) {
int ret = 1;
ifield[y][x] = lump_ice_num;
field[y][x] = '.';
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (field[ny][nx] == 'X') {
ret += dfs(ny, nx);
}
}
return ret;
}
bool visited[12][12];
int visited_ice[12 * 12];
int ans;
int sy, sx, gy, gx;
void dfs2(int y, int x, int py, int px, int d) {
// printf("%d %d\n",y,x);
if (d >= ans)
return;
if (y == gy && x == gx) {
ans = min(d, ans);
return;
}
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (ny == py && nx == px)
continue;
if (visited[ny][nx])
return;
}
visited[y][x] = true;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
// printf("y=%d x=%d ny=%d nx=%d\n", y, x, ny, nx);
if (visited[ny][nx])
continue;
if (ifield[ny][nx] == -2)
continue;
if (ifield[ny][nx] >= 0) {
int ice_i = ifield[ny][nx];
if (ice_num[ice_i] / 2 < visited_ice[ice_i] + 1)
continue;
visited_ice[ice_i]++;
dfs2(ny, nx, y, x, d + 1);
visited_ice[ice_i]--;
} else {
dfs2(ny, nx, y, x, d + 1);
}
}
visited[y][x] = false;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == '.') {
ifield[i][j] = -1;
} else if (field[i][j] == '#') {
ifield[i][j] = -2;
} else if (field[i][j] == 'S') {
ifield[i][j] = -1;
sy = i;
sx = j;
} else if (field[i][j] == 'G') {
ifield[i][j] = -1;
gy = i;
gx = j;
}
}
}
lump_ice_num = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'X') {
ice_num[lump_ice_num] = dfs(i, j);
lump_ice_num++;
}
}
}
ans = INF;
memset(visited, 0, sizeof(visited));
memset(visited_ice, 0, sizeof(visited_ice));
dfs2(sy, sx, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char field[12][12];
int ifield[12][12];
int W, H;
struct State {
int d, y, x;
vec ice;
};
string state2str(State &s) {
string ret = "";
ret.push_back(s.d + '0');
ret.push_back(s.y + '0');
ret.push_back(s.x + '0');
for (int i = 0; i < s.ice.size(); i++) {
ret.push_back(s.ice[i] + '0');
}
return ret;
}
int lump_ice_num;
int ice_num[12 * 12];
int dfs(int y, int x) {
int ret = 1;
ifield[y][x] = lump_ice_num;
field[y][x] = '.';
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (field[ny][nx] == 'X') {
ret += dfs(ny, nx);
}
}
return ret;
}
bool visited[12][12];
int visited_ice[12 * 12];
int ans;
int sy, sx, gy, gx;
void dfs2(int y, int x, int py, int px, int d) {
// printf("%d %d\n",y,x);
if (d >= ans)
return;
if (y == gy && x == gx) {
ans = min(d, ans);
return;
}
if (d + abs(gy - y) + abs(gx - x) >= ans)
return;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (ny == py && nx == px)
continue;
if (visited[ny][nx])
return;
}
visited[y][x] = true;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
// printf("y=%d x=%d ny=%d nx=%d\n", y, x, ny, nx);
if (visited[ny][nx])
continue;
if (ifield[ny][nx] == -2)
continue;
if (ifield[ny][nx] >= 0) {
int ice_i = ifield[ny][nx];
if (ice_num[ice_i] / 2 < visited_ice[ice_i] + 1)
continue;
visited_ice[ice_i]++;
dfs2(ny, nx, y, x, d + 1);
visited_ice[ice_i]--;
} else {
dfs2(ny, nx, y, x, d + 1);
}
}
visited[y][x] = false;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == '.') {
ifield[i][j] = -1;
} else if (field[i][j] == '#') {
ifield[i][j] = -2;
} else if (field[i][j] == 'S') {
ifield[i][j] = -1;
sy = i;
sx = j;
} else if (field[i][j] == 'G') {
ifield[i][j] = -1;
gy = i;
gx = j;
}
}
}
lump_ice_num = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'X') {
ice_num[lump_ice_num] = dfs(i, j);
lump_ice_num++;
}
}
}
ans = INF;
memset(visited, 0, sizeof(visited));
memset(visited_ice, 0, sizeof(visited_ice));
dfs2(sy, sx, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | insert | 76 | 76 | 76 | 78 | TLE | |
p00247 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int H, W;
int sy, sx, gy, gx;
char field[101][101];
int ice;
int iceNumber[101][101];
// iceごとのタイル数
int tileNum[101];
bool used[101][101];
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
int predic[101][101];
// 状態に必要なもの
// ここまでのコスト
// どのiceを何度踏んだか(vectorでもつ)
typedef pair<pii, vector<int>> Info;
typedef pair<int, pair<pii, vector<int>>> Sit;
// 氷を踏む回数を無視したときのgoalからの最短距離
void bfs() {
queue<pii> q;
memset(predic, -1, sizeof(predic));
q.push(pii(gy, gx));
predic[gy][gx] = 0;
while (q.size()) {
pii p = q.front();
q.pop();
int cy = p.first;
int cx = p.second;
int ccost = predic[cy][cx];
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && predic[ny][nx] == -1 &&
field[ny][nx] != '#') {
int ncost = ccost + 1;
predic[ny][nx] = ncost;
q.push(pii(ny, nx));
}
}
}
}
int astar() {
priority_queue<Sit, vector<Sit>, greater<Sit>> pq;
map<Info, int> d;
vector<int> sv(ice, 0);
d[(make_pair(pii(sy, sx), sv))] = predic[sy][sx];
pq.push(Sit(predic[sy][sx], make_pair(pii(sy, sx), sv)));
int res = 10000000;
while (pq.size()) {
Sit p = pq.top();
pq.pop();
int cy = p.second.first.first;
int cx = p.second.first.second;
int ccost = p.first;
vector<int> ctile = p.second.second;
if (d.count(p.second) > 0 && ccost > d[p.second])
continue;
int realCost = ccost - predic[cy][cx];
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && field[ny][nx] != '#') {
vector<int> ntile = ctile;
int ncost = (realCost + 1) + predic[ny][nx];
Info ninfo = p.second;
ninfo.first.first = ny;
ninfo.first.second = nx;
if (field[ny][nx] == 'X') {
int a = iceNumber[ny][nx];
ninfo.second[a]++;
if (ninfo.second[a] > tileNum[a] / 2)
continue;
}
if (d.count(ninfo) == 0 || d[ninfo] > ncost) {
d[ninfo] = ncost;
pq.push(make_pair(ncost, ninfo));
if (gy == ny && gx == nx)
res = min(res, ncost);
}
}
}
}
return res;
}
int labelling(int cy, int cx, int k) {
int cnt = 1;
used[cy][cx] = true;
iceNumber[cy][cx] = k;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && !used[ny][nx] &&
field[ny][nx] == 'X')
cnt += labelling(ny, nx, k);
}
return cnt;
}
int main() {
while (cin >> W >> H && (H | W)) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == 'S') {
sy = i;
sx = j;
field[i][j] = '.';
} else if (field[i][j] == 'G') {
gy = i;
gx = j;
field[i][j] = '.';
}
}
}
ice = 0;
memset(used, 0, sizeof(used));
memset(iceNumber, -1, sizeof(iceNumber));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (!used[i][j] && field[i][j] == 'X') {
tileNum[ice] = labelling(i, j, ice);
ice++;
}
bfs();
int res = astar();
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int H, W;
int sy, sx, gy, gx;
char field[101][101];
int ice;
int iceNumber[101][101];
// iceごとのタイル数
int tileNum[101];
bool used[101][101];
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
int predic[101][101];
// 状態に必要なもの
// ここまでのコスト
// どのiceを何度踏んだか(vectorでもつ)
typedef pair<pii, vector<int>> Info;
typedef pair<int, pair<pii, vector<int>>> Sit;
// 氷を踏む回数を無視したときのgoalからの最短距離
void bfs() {
queue<pii> q;
memset(predic, -1, sizeof(predic));
q.push(pii(gy, gx));
predic[gy][gx] = 0;
while (q.size()) {
pii p = q.front();
q.pop();
int cy = p.first;
int cx = p.second;
int ccost = predic[cy][cx];
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && predic[ny][nx] == -1 &&
field[ny][nx] != '#') {
int ncost = ccost + 1;
predic[ny][nx] = ncost;
q.push(pii(ny, nx));
}
}
}
}
int astar() {
priority_queue<Sit, vector<Sit>, greater<Sit>> pq;
map<Info, int> d;
vector<int> sv(ice, 0);
d[(make_pair(pii(sy, sx), sv))] = predic[sy][sx];
pq.push(Sit(predic[sy][sx], make_pair(pii(sy, sx), sv)));
int res = 10000000;
while (pq.size()) {
Sit p = pq.top();
pq.pop();
int cy = p.second.first.first;
int cx = p.second.first.second;
int ccost = p.first;
vector<int> ctile = p.second.second;
if (d.count(p.second) > 0 && ccost > d[p.second])
continue;
int realCost = ccost - predic[cy][cx];
if (cy == gy && cx == gx)
return realCost;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && field[ny][nx] != '#') {
vector<int> ntile = ctile;
int ncost = (realCost + 1) + predic[ny][nx];
Info ninfo = p.second;
ninfo.first.first = ny;
ninfo.first.second = nx;
if (field[ny][nx] == 'X') {
int a = iceNumber[ny][nx];
ninfo.second[a]++;
if (ninfo.second[a] > tileNum[a] / 2)
continue;
}
if (d.count(ninfo) == 0 || d[ninfo] > ncost) {
d[ninfo] = ncost;
pq.push(make_pair(ncost, ninfo));
if (gy == ny && gx == nx)
res = min(res, ncost);
}
}
}
}
return res;
}
int labelling(int cy, int cx, int k) {
int cnt = 1;
used[cy][cx] = true;
iceNumber[cy][cx] = k;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i];
int nx = cx + dx[i];
if (ny >= 0 && nx >= 0 && ny < H && nx < W && !used[ny][nx] &&
field[ny][nx] == 'X')
cnt += labelling(ny, nx, k);
}
return cnt;
}
int main() {
while (cin >> W >> H && (H | W)) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == 'S') {
sy = i;
sx = j;
field[i][j] = '.';
} else if (field[i][j] == 'G') {
gy = i;
gx = j;
field[i][j] = '.';
}
}
}
ice = 0;
memset(used, 0, sizeof(used));
memset(iceNumber, -1, sizeof(iceNumber));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
if (!used[i][j] && field[i][j] == 'X') {
tileNum[ice] = labelling(i, j, ice);
ice++;
}
bfs();
int res = astar();
cout << res << endl;
}
return 0;
} | insert | 69 | 69 | 69 | 71 | MLE | |
p00247 | C++ | Time Limit Exceeded | #include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int W, H, SX, SY, GX, GY, ice_N;
int field[14][14], ice[14][14], icelife[1000];
bool flag[14][14];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y);
bool iddfs(int x, int y, int c);
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 14; j++) {
field[i][j] = 1;
flag[i][j] = true;
}
}
memset(ice, 0, sizeof(ice));
memset(icelife, 0, sizeof(icelife));
ice_N = 1;
string s;
int ans;
for (int i = 1; i <= H; i++) {
cin >> s;
for (int j = 1; j <= W; j++) {
if (s[j - 1] == '.') {
field[j][i] = 0;
}
if (s[j - 1] == '#') {
field[j][i] = 1;
}
if (s[j - 1] == 'X') {
field[j][i] = 2;
}
if (s[j - 1] == 'S') {
field[j][i] = 0;
SX = j;
SY = i;
}
if (s[j - 1] == 'G') {
field[j][i] = 0;
GX = j;
GY = i;
}
}
}
for (int i = 1; i <= W; i++) {
for (int j = 1; j <= H; j++) {
if (field[i][j] == 2 && ice[i][j] == 0) {
dfs(i, j);
icelife[ice_N] /= 2;
ice_N++;
}
}
}
for (ans = abs(SX - GX) + abs(SY - GY);; ans++) {
if (iddfs(SX, SY, ans)) {
cout << ans << endl;
break;
}
}
}
return 0;
}
void dfs(int x, int y) {
ice[x][y] = ice_N;
icelife[ice_N]++;
for (int i = 0; i < 4; i++) {
int p = x + dx[i], q = y + dy[i];
if (field[p][q] == 2 && ice[p][q] == 0) {
dfs(p, q);
}
}
}
bool iddfs(int x, int y, int c) {
bool f = false;
if (x == GX && y == GY) {
return true;
}
if (c < abs(x - GX) + abs(y - GY)) {
return false;
}
for (int i = 0; i < 4; i++) {
int p = x + dx[i], q = y + dy[i];
if (flag[p][q]) {
if (field[p][q] == 0) {
flag[p][q] = false;
if (iddfs(p, q, c - 1)) {
f = true;
}
flag[p][q] = true;
}
if (field[p][q] == 2) {
if (icelife[ice[p][q]] > 0) {
flag[p][q] = false;
icelife[ice[p][q]]--;
if (iddfs(p, q, c - 1)) {
f = true;
}
icelife[ice[p][q]]++;
flag[p][q] = true;
}
}
}
}
return f;
} | #include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int W, H, SX, SY, GX, GY, ice_N;
int field[14][14], ice[14][14], icelife[1000];
bool flag[14][14];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y);
bool iddfs(int x, int y, int c);
int main() {
while (cin >> W >> H, W || H) {
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 14; j++) {
field[i][j] = 1;
flag[i][j] = true;
}
}
memset(ice, 0, sizeof(ice));
memset(icelife, 0, sizeof(icelife));
ice_N = 1;
string s;
int ans;
for (int i = 1; i <= H; i++) {
cin >> s;
for (int j = 1; j <= W; j++) {
if (s[j - 1] == '.') {
field[j][i] = 0;
}
if (s[j - 1] == '#') {
field[j][i] = 1;
}
if (s[j - 1] == 'X') {
field[j][i] = 2;
}
if (s[j - 1] == 'S') {
field[j][i] = 0;
SX = j;
SY = i;
}
if (s[j - 1] == 'G') {
field[j][i] = 0;
GX = j;
GY = i;
}
}
}
for (int i = 1; i <= W; i++) {
for (int j = 1; j <= H; j++) {
if (field[i][j] == 2 && ice[i][j] == 0) {
dfs(i, j);
icelife[ice_N] /= 2;
ice_N++;
}
}
}
for (ans = abs(SX - GX) + abs(SY - GY);; ans++) {
if (iddfs(SX, SY, ans)) {
cout << ans << endl;
break;
}
}
}
return 0;
}
void dfs(int x, int y) {
ice[x][y] = ice_N;
icelife[ice_N]++;
for (int i = 0; i < 4; i++) {
int p = x + dx[i], q = y + dy[i];
if (field[p][q] == 2 && ice[p][q] == 0) {
dfs(p, q);
}
}
}
bool iddfs(int x, int y, int c) {
bool f = false;
if (x == GX && y == GY) {
return true;
}
if (c < abs(x - GX) + abs(y - GY)) {
return false;
}
for (int i = 0; i < 4; i++) {
int p = x + dx[i], q = y + dy[i];
bool next = false;
for (int j = 0; j < 4; j++) {
int pp = p + dx[j], qq = q + dy[j];
if (pp != x || qq != y) {
if (!flag[pp][qq]) {
next = true;
}
}
}
if (next) {
continue;
}
if (flag[p][q]) {
if (field[p][q] == 0) {
flag[p][q] = false;
if (iddfs(p, q, c - 1)) {
f = true;
}
flag[p][q] = true;
}
if (field[p][q] == 2) {
if (icelife[ice[p][q]] > 0) {
flag[p][q] = false;
icelife[ice[p][q]]--;
if (iddfs(p, q, c - 1)) {
f = true;
}
icelife[ice[p][q]]++;
flag[p][q] = true;
}
}
}
}
return f;
} | insert | 87 | 87 | 87 | 99 | TLE | |
p00247 | C++ | Time Limit Exceeded | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char field[12][12];
int ifield[12][12];
int W, H;
struct State {
int d, y, x;
vec ice;
};
string state2str(State &s) {
string ret = "";
ret.push_back(s.d + '0');
ret.push_back(s.y + '0');
ret.push_back(s.x + '0');
for (int i = 0; i < s.ice.size(); i++) {
ret.push_back(s.ice[i] + '0');
}
return ret;
}
int lump_ice_num;
int ice_num[12 * 12];
int dfs(int y, int x) {
int ret = 1;
ifield[y][x] = lump_ice_num;
field[y][x] = '.';
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (field[ny][nx] == 'X') {
ret += dfs(ny, nx);
}
}
return ret;
}
bool visited[12][12];
int visited_ice[12 * 12];
int ans;
int sy, sx, gy, gx;
void dfs2(int y, int x, int py, int px, int d) {
// printf("%d %d\n",y,x);
if (y == gy && x == gx) {
ans = min(d, ans);
return;
}
if (d + abs(gy - y) + abs(gx - x) >= ans)
return;
/*for(int i=0;i<4;i++){
int ny = y + dy[i], nx = x + dx[i];
if(ny<0||H<=ny||nx<0||W<=nx)continue;
if(ny==py&&nx==px)continue;
if(visited[ny][nx]) return;
}*/
visited[y][x] = true;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
// printf("y=%d x=%d ny=%d nx=%d\n", y, x, ny, nx);
if (visited[ny][nx])
continue;
if (ifield[ny][nx] == -2)
continue;
if (ifield[ny][nx] >= 0) {
int ice_i = ifield[ny][nx];
if (ice_num[ice_i] / 2 < visited_ice[ice_i] + 1)
continue;
visited_ice[ice_i]++;
dfs2(ny, nx, y, x, d + 1);
visited_ice[ice_i]--;
} else {
dfs2(ny, nx, y, x, d + 1);
}
}
visited[y][x] = false;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == '.') {
ifield[i][j] = -1;
} else if (field[i][j] == '#') {
ifield[i][j] = -2;
} else if (field[i][j] == 'S') {
ifield[i][j] = -1;
sy = i;
sx = j;
} else if (field[i][j] == 'G') {
ifield[i][j] = -1;
gy = i;
gx = j;
}
}
}
lump_ice_num = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'X') {
ice_num[lump_ice_num] = dfs(i, j);
lump_ice_num++;
}
}
}
ans = INF;
memset(visited, 0, sizeof(visited));
memset(visited_ice, 0, sizeof(visited_ice));
dfs2(sy, sx, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char field[12][12];
int ifield[12][12];
int W, H;
struct State {
int d, y, x;
vec ice;
};
string state2str(State &s) {
string ret = "";
ret.push_back(s.d + '0');
ret.push_back(s.y + '0');
ret.push_back(s.x + '0');
for (int i = 0; i < s.ice.size(); i++) {
ret.push_back(s.ice[i] + '0');
}
return ret;
}
int lump_ice_num;
int ice_num[12 * 12];
int dfs(int y, int x) {
int ret = 1;
ifield[y][x] = lump_ice_num;
field[y][x] = '.';
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (field[ny][nx] == 'X') {
ret += dfs(ny, nx);
}
}
return ret;
}
bool visited[12][12];
int visited_ice[12 * 12];
int ans;
int sy, sx, gy, gx;
void dfs2(int y, int x, int py, int px, int d) {
// printf("%d %d\n",y,x);
if (y == gy && x == gx) {
ans = min(d, ans);
return;
}
if (d + abs(gy - y) + abs(gx - x) >= ans)
return;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
if (ny == py && nx == px)
continue;
if (visited[ny][nx])
return;
}
visited[y][x] = true;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || H <= ny || nx < 0 || W <= nx)
continue;
// printf("y=%d x=%d ny=%d nx=%d\n", y, x, ny, nx);
if (visited[ny][nx])
continue;
if (ifield[ny][nx] == -2)
continue;
if (ifield[ny][nx] >= 0) {
int ice_i = ifield[ny][nx];
if (ice_num[ice_i] / 2 < visited_ice[ice_i] + 1)
continue;
visited_ice[ice_i]++;
dfs2(ny, nx, y, x, d + 1);
visited_ice[ice_i]--;
} else {
dfs2(ny, nx, y, x, d + 1);
}
}
visited[y][x] = false;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
if (field[i][j] == '.') {
ifield[i][j] = -1;
} else if (field[i][j] == '#') {
ifield[i][j] = -2;
} else if (field[i][j] == 'S') {
ifield[i][j] = -1;
sy = i;
sx = j;
} else if (field[i][j] == 'G') {
ifield[i][j] = -1;
gy = i;
gx = j;
}
}
}
lump_ice_num = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (field[i][j] == 'X') {
ice_num[lump_ice_num] = dfs(i, j);
lump_ice_num++;
}
}
}
ans = INF;
memset(visited, 0, sizeof(visited));
memset(visited_ice, 0, sizeof(visited_ice));
dfs2(sy, sx, -1, -1, 0);
cout << ans << endl;
}
return 0;
} | replace | 76 | 82 | 76 | 85 | TLE | |
p00248 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> G[10000];
bool used[100000];
bool dfs(int v, int p) {
used[v] = true;
bool ret = true;
for (int i = 0; i < G[v].size(); i++) {
int to = G[v][i];
if (to == p)
continue;
if (used[to])
return false;
ret &= dfs(to, v);
}
return ret;
}
int main() {
while (cin >> N >> M, N || M) {
fill_n(G, N, vector<int>());
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
if (M >= N) {
cout << "no" << endl;
continue;
}
fill_n(used, N, false);
bool ret = true;
for (int i = 0; i < N; i++) {
if (G[i].size() > 2)
ret = false;
if (G[i].size() >= 2)
continue;
if (used[i])
continue;
ret &= dfs(i, -1);
}
for (int i = 0; i < N; i++)
if (!used[i])
ret = false;
if (ret)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> G[100000];
bool used[100000];
bool dfs(int v, int p) {
used[v] = true;
bool ret = true;
for (int i = 0; i < G[v].size(); i++) {
int to = G[v][i];
if (to == p)
continue;
if (used[to])
return false;
ret &= dfs(to, v);
}
return ret;
}
int main() {
while (cin >> N >> M, N || M) {
fill_n(G, N, vector<int>());
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
if (M >= N) {
cout << "no" << endl;
continue;
}
fill_n(used, N, false);
bool ret = true;
for (int i = 0; i < N; i++) {
if (G[i].size() > 2)
ret = false;
if (G[i].size() >= 2)
continue;
if (used[i])
continue;
ret &= dfs(i, -1);
}
for (int i = 0; i < N; i++)
if (!used[i])
ret = false;
if (ret)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00249 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); }
Point operator*(Point a) {
return Point(x * a.x - y * a.y, x * a.y + y * a.x);
}
bool operator<(const Point &p) const {
return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y);
}
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
bool operator<(const Segment &s) const {
return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2;
}
bool operator==(const Segment &s) const {
return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1);
}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
double abs(Point a) { return sqrt(norm(a)); }
Point rotate(Point a, double rad) {
return Point(cos(rad) * a.x - sin(rad) * a.y,
sin(rad) * a.x + cos(rad) * a.y);
}
double toRad(double agl) { return agl * M_PI / 180.0; }
double getArg(Point a, Point b, Point c) {
double arg1 = atan2(b.y - a.y, b.x - a.x);
double arg2 = atan2(c.y - b.y, c.x - b.x);
double arg = fabs(arg1 - arg2);
while (arg > M_PI)
arg -= 2.0 * M_PI;
return fabs(arg);
}
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0;
Point b = p2 - p0;
if (cross(a, b) > EPS)
return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS)
return CLOCKWISE;
if (dot(a, b) < -EPS)
return ONLINE_BACK;
if (norm(a) < norm(b))
return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersectLL(Line l, Line m) {
return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel
abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line
}
bool intersectLS(Line l, Line s) {
return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l
cross(l.p2 - l.p1, s.p2 - l.p1) <
EPS; // s[1] is right of l
}
bool intersectLP(Line l, Point p) {
return abs(cross(l.p2 - p, l.p1 - p)) < EPS;
}
bool intersectSS(Line s, Line t) {
return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 &&
ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0;
}
bool intersectSP(Line s, Point p) {
return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) <
EPS; // triangle inequality
}
Point projection(Line l, Point p) {
double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2);
return l.p1 + (l.p1 - l.p2) * t;
}
Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; }
double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); }
double distanceLL(Line l, Line m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);
}
double distanceLS(Line l, Line s) {
if (intersectLS(l, s))
return 0;
return min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
double distanceSP(Line s, Point p) {
Point r = projection(s, p);
if (intersectSP(s, r))
return abs(r - p);
return min(abs(s.p1 - p), abs(s.p2 - p));
}
double distanceSS(Line s, Line t) {
if (intersectSS(s, t))
return 0;
return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),
min(distanceSP(t, s.p1), distanceSP(t, s.p2)));
}
Point crosspoint(Line l, Line m) {
double A = cross(l.p2 - l.p1, m.p2 - m.p1);
double B = cross(l.p2 - l.p1, l.p2 - m.p1);
if (abs(A) < EPS && abs(B) < EPS) {
vector<Point> vec;
vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1),
vec.push_back(m.p2);
sort(vec.begin(), vec.end());
assert(vec[1] == vec[2]); //???????????°??????????????????
return vec[1];
// return m.p1;
}
if (abs(A) < EPS)
assert(false);
return m.p1 + (m.p2 - m.p1) * (B / A);
}
double getArea(vector<Point> &vec) {
double sum = 0;
for (int i = 0; i < vec.size(); i++)
sum += cross(vec[i], vec[(i + 1) % vec.size()]);
return fabs(sum) / 2.0;
}
double cross3p(Point p, Point q, Point r) {
return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x);
}
bool collinear(Point p, Point q, Point r) {
return fabs(cross3p(p, q, r)) < EPS;
}
bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; }
bool onSegment(Point p, Point q, Point r) {
return collinear(p, q, r) &&
equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) +
sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)),
sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2)));
}
Point getCentroidOfConvex(Polygon &poly) {
double area = getArea(poly);
int V = poly.size();
assert(!equals(area, 0.0));
double x = 0, y = 0;
rep(i, (int)poly.size()) {
x += (poly[i].x + poly[(i + 1) % V].x) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
y += (poly[i].y + poly[(i + 1) % V].y) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
}
return Point(x / (6.0 * area), y / (6.0 * area));
}
bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }
int V;
double d, vol;
Polygon poly;
Point ps[110];
double lets_compute_arg(int L, int R) {
assert(L != R);
Point lp = poly[L], rp = poly[R];
rp = rp - lp;
lp = Point(0, 0);
Vector e = rp / abs(rp);
e = e * 100000;
e = rotate(e, toRad(90.0));
Line line = Line(e, e * -1);
Point cp = crosspoint(line, Line(poly[L], poly[R]));
double ret = getArg(Point(0, -1000), Point(0, 0), cp);
return ret + toRad(180.0);
}
double getNewVolume(double h) {
Segment cut_line = Segment(Point(-20000, h), Point(20000, h));
int nV = 0;
int index = 0;
int cross_ps[2];
rep(i, V) {
Segment seg = Segment(poly[i], poly[(i + 1) % V]);
if (intersectSS(cut_line, seg)) { // ???????????´????????????
Point cp = crosspoint(cut_line, seg);
if (!(cp == seg.p1 || cp == seg.p2)) {
ps[nV++] = cp;
}
}
ps[nV++] = poly[(i + 1) % V];
}
rep(i, nV) if (equals(cut_line.p2.y, ps[i].y)) { cross_ps[index++] = i; }
int sp = -1;
double maxi_h = -1e30;
rep(i, nV) maxi_h = max(maxi_h, ps[i].y);
rep(i, nV) if (equals(maxi_h, ps[i].y)) {
sp = i;
break;
}
bool found = false;
int cur = sp;
vector<Point> npoly;
rep(i, nV) {
bool same_point = (cross_ps[0] == cur || cross_ps[1] == cur);
if (same_point && found) {
npoly.push_back(ps[cur]);
break;
}
if (same_point)
found = true;
if (found)
npoly.push_back(ps[cur]);
cur = (cur + 1) % nV;
}
double area = getArea(npoly);
return area * d;
}
void compute() {
bool full = false;
{
double area = getArea(poly) * d;
if (equals(area, vol))
full = true;
}
double maxi = 0;
Point centroid = getCentroidOfConvex(poly);
rep(i, V) poly[i] = poly[i] - centroid;
centroid = Point(0, 0);
rep(_, V) {
double maxi_y = -1e30, mini_y = 1e30;
rep(i, V) maxi_y = max(maxi_y, poly[i].y), mini_y = min(mini_y, poly[i].y);
double low_y = mini_y, high_y = maxi_y;
if (!full) {
rep(__, 60) {
double mid_y = (low_y + high_y) / 2.0;
double new_volume = getNewVolume(mid_y);
if (equals(new_volume, vol)) {
maxi = max(maxi, fabs(mid_y - mini_y));
break;
}
if (LT(new_volume, vol))
low_y = mid_y;
else
high_y = mid_y;
}
} else {
rep(j, V) maxi = max(maxi, abs(mini_y - poly[j].y));
}
int left_p = -1, right_p = -1;
rep(i, V) if (equals(mini_y, poly[i].y)) {
if (left_p == -1)
left_p = i;
if (right_p == -1)
right_p = i;
if (LT(poly[i].x, poly[left_p].x))
left_p = i;
if (LT(poly[right_p].x, poly[i].x))
right_p = i;
}
(left_p += 1) %= V;
(right_p += 1) %= V;
double rad = lets_compute_arg(left_p, right_p);
rep(i, V) poly[i] = rotate(poly[i], rad);
}
printf("%.10f\n", maxi);
}
void fix() {
bool update = true;
while (update) {
update = false;
rep(i, poly.size()) {
int j = (i + 1) % poly.size();
int k = (i + 2) % poly.size();
if (equals(cross(poly[j] - poly[i], poly[k] - poly[i]), 0.0)) {
update = true;
poly.erase(poly.begin() + j);
goto Skip;
}
}
Skip:;
}
V = poly.size();
}
int main() {
// ????????????????????±?????????????????????????????????????????¨ <=
// ??????????????????????????? ????????????fix????????????
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c), a | b | c) {
V = a, d = b, vol = c;
poly.clear();
poly.resize(V);
rep(i, V) scanf("%lf %lf", &poly[i].x, &poly[i].y);
// fix();
compute();
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); }
Point operator*(Point a) {
return Point(x * a.x - y * a.y, x * a.y + y * a.x);
}
bool operator<(const Point &p) const {
return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y);
}
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
bool operator<(const Segment &s) const {
return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2;
}
bool operator==(const Segment &s) const {
return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1);
}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
double abs(Point a) { return sqrt(norm(a)); }
Point rotate(Point a, double rad) {
return Point(cos(rad) * a.x - sin(rad) * a.y,
sin(rad) * a.x + cos(rad) * a.y);
}
double toRad(double agl) { return agl * M_PI / 180.0; }
double getArg(Point a, Point b, Point c) {
double arg1 = atan2(b.y - a.y, b.x - a.x);
double arg2 = atan2(c.y - b.y, c.x - b.x);
double arg = fabs(arg1 - arg2);
while (arg > M_PI)
arg -= 2.0 * M_PI;
return fabs(arg);
}
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0;
Point b = p2 - p0;
if (cross(a, b) > EPS)
return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS)
return CLOCKWISE;
if (dot(a, b) < -EPS)
return ONLINE_BACK;
if (norm(a) < norm(b))
return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersectLL(Line l, Line m) {
return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel
abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line
}
bool intersectLS(Line l, Line s) {
return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l
cross(l.p2 - l.p1, s.p2 - l.p1) <
EPS; // s[1] is right of l
}
bool intersectLP(Line l, Point p) {
return abs(cross(l.p2 - p, l.p1 - p)) < EPS;
}
bool intersectSS(Line s, Line t) {
return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 &&
ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0;
}
bool intersectSP(Line s, Point p) {
return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) <
EPS; // triangle inequality
}
Point projection(Line l, Point p) {
double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2);
return l.p1 + (l.p1 - l.p2) * t;
}
Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; }
double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); }
double distanceLL(Line l, Line m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);
}
double distanceLS(Line l, Line s) {
if (intersectLS(l, s))
return 0;
return min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
double distanceSP(Line s, Point p) {
Point r = projection(s, p);
if (intersectSP(s, r))
return abs(r - p);
return min(abs(s.p1 - p), abs(s.p2 - p));
}
double distanceSS(Line s, Line t) {
if (intersectSS(s, t))
return 0;
return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),
min(distanceSP(t, s.p1), distanceSP(t, s.p2)));
}
Point crosspoint(Line l, Line m) {
double A = cross(l.p2 - l.p1, m.p2 - m.p1);
double B = cross(l.p2 - l.p1, l.p2 - m.p1);
if (abs(A) < EPS && abs(B) < EPS) {
vector<Point> vec;
vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1),
vec.push_back(m.p2);
sort(vec.begin(), vec.end());
assert(vec[1] == vec[2]); //???????????°??????????????????
return vec[1];
// return m.p1;
}
if (abs(A) < EPS)
assert(false);
return m.p1 + (m.p2 - m.p1) * (B / A);
}
double getArea(vector<Point> &vec) {
double sum = 0;
for (int i = 0; i < vec.size(); i++)
sum += cross(vec[i], vec[(i + 1) % vec.size()]);
return fabs(sum) / 2.0;
}
double cross3p(Point p, Point q, Point r) {
return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x);
}
bool collinear(Point p, Point q, Point r) {
return fabs(cross3p(p, q, r)) < EPS;
}
bool ccwtest(Point p, Point q, Point r) { return cross3p(p, q, r) > 0; }
bool onSegment(Point p, Point q, Point r) {
return collinear(p, q, r) &&
equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) +
sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)),
sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2)));
}
Point getCentroidOfConvex(Polygon &poly) {
double area = getArea(poly);
int V = poly.size();
assert(!equals(area, 0.0));
double x = 0, y = 0;
rep(i, (int)poly.size()) {
x += (poly[i].x + poly[(i + 1) % V].x) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
y += (poly[i].y + poly[(i + 1) % V].y) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
}
return Point(x / (6.0 * area), y / (6.0 * area));
}
bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }
int V;
double d, vol;
Polygon poly;
Point ps[110];
double lets_compute_arg(int L, int R) {
assert(L != R);
Point lp = poly[L], rp = poly[R];
rp = rp - lp;
lp = Point(0, 0);
Vector e = rp / abs(rp);
e = e * 100000;
e = rotate(e, toRad(90.0));
Line line = Line(e, e * -1);
Point cp = crosspoint(line, Line(poly[L], poly[R]));
double ret = getArg(Point(0, -1000), Point(0, 0), cp);
return ret + toRad(180.0);
}
double getNewVolume(double h) {
Segment cut_line = Segment(Point(-20000, h), Point(20000, h));
int nV = 0;
int index = 0;
int cross_ps[2];
rep(i, V) {
Segment seg = Segment(poly[i], poly[(i + 1) % V]);
if (intersectSS(cut_line, seg)) { // ???????????´????????????
Point cp = crosspoint(cut_line, seg);
if (!(cp == seg.p1 || cp == seg.p2)) {
ps[nV++] = cp;
}
}
ps[nV++] = poly[(i + 1) % V];
}
rep(i, nV) if (equals(cut_line.p2.y, ps[i].y)) { cross_ps[index++] = i; }
int sp = -1;
double maxi_h = -1e30;
rep(i, nV) maxi_h = max(maxi_h, ps[i].y);
rep(i, nV) if (equals(maxi_h, ps[i].y)) {
sp = i;
break;
}
bool found = false;
int cur = sp;
vector<Point> npoly;
rep(i, nV) {
bool same_point = (cross_ps[0] == cur || cross_ps[1] == cur);
if (same_point && found) {
npoly.push_back(ps[cur]);
break;
}
if (same_point)
found = true;
if (found)
npoly.push_back(ps[cur]);
cur = (cur + 1) % nV;
}
double area = getArea(npoly);
return area * d;
}
void compute() {
bool full = false;
{
double area = getArea(poly) * d;
if (equals(area, vol))
full = true;
}
double maxi = 0;
Point centroid = getCentroidOfConvex(poly);
rep(i, V) poly[i] = poly[i] - centroid;
centroid = Point(0, 0);
rep(_, V) {
double maxi_y = -1e30, mini_y = 1e30;
rep(i, V) maxi_y = max(maxi_y, poly[i].y), mini_y = min(mini_y, poly[i].y);
double low_y = mini_y, high_y = maxi_y;
if (!full) {
rep(__, 60) {
double mid_y = (low_y + high_y) / 2.0;
double new_volume = getNewVolume(mid_y);
if (equals(new_volume, vol)) {
maxi = max(maxi, fabs(mid_y - mini_y));
break;
}
if (LT(new_volume, vol))
low_y = mid_y;
else
high_y = mid_y;
}
} else {
rep(j, V) maxi = max(maxi, abs(mini_y - poly[j].y));
}
int left_p = -1, right_p = -1;
rep(i, V) if (equals(mini_y, poly[i].y)) {
if (left_p == -1)
left_p = i;
if (right_p == -1)
right_p = i;
if (LT(poly[i].x, poly[left_p].x))
left_p = i;
if (LT(poly[right_p].x, poly[i].x))
right_p = i;
}
(left_p += 1) %= V;
(right_p += 1) %= V;
double rad = lets_compute_arg(left_p, right_p);
rep(i, V) poly[i] = rotate(poly[i], rad);
}
printf("%.10f\n", maxi);
}
void fix() {
bool update = true;
while (update) {
update = false;
rep(i, poly.size()) {
int j = (i + 1) % poly.size();
int k = (i + 2) % poly.size();
if (equals(cross(poly[j] - poly[i], poly[k] - poly[i]), 0.0)) {
update = true;
poly.erase(poly.begin() + j);
goto Skip;
}
}
Skip:;
}
V = poly.size();
}
int main() {
// ????????????????????±?????????????????????????????????????????¨ <=
// ??????????????????????????? ????????????fix????????????
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c), a | b | c) {
V = a, d = b, vol = c;
poly.clear();
poly.resize(V);
rep(i, V) scanf("%lf %lf", &poly[i].x, &poly[i].y);
fix();
compute();
}
return 0;
} | replace | 354 | 355 | 354 | 355 | 0 | |
p00249 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef double D;
typedef complex<D> P;
const D EPS = 1e-8;
D calc(const vector<P> &plg, D v) {
int n = plg.size();
int lt = n - 1, rt = 2;
D lx = 0, rx = real(plg[1]);
D y = 0;
while (1) {
D ny = min(imag(plg[lt]), imag(plg[rt]));
D nlx = lx + (ny - y) / (imag(plg[lt]) - y) * (real(plg[lt]) - lx);
D nrx = rx + (ny - y) / (imag(plg[rt]) - y) * (real(plg[rt]) - rx);
D w1 = rx - lx, w2 = nrx - nlx;
D h = ny - y;
D s = (w1 + w2) * h * 0.5;
if (s < v) {
v -= s;
y = ny;
lx = nlx;
rx = nrx;
if (imag(plg[lt]) == ny) {
--lt;
}
if (imag(plg[rt]) == ny) {
++rt;
}
assert(lt > rt);
} else {
D ret = y;
D a = (w2 - w1) / h * 0.5;
D b = w1;
D c = -v;
if (abs(a) > EPS) {
D d = b * b - 4 * a * c;
assert(d >= 0);
d = sqrt(d);
D t1 = (-b + d) / a * 0.5;
D t2 = (-b - d) / a * 0.5;
if (t1 > t2) {
swap(t1, t2);
}
assert(t2 >= 0);
if (t1 >= 0) {
ret += t1;
} else {
ret += t2;
}
} else {
D t = -c / b;
assert(t >= 0);
ret += t;
}
return ret;
}
}
}
int main() {
cout << fixed << setprecision(9);
ios::sync_with_stdio(false);
int n;
D d, v;
while (cin >> n >> d >> v, n) {
v /= d;
vector<P> pt(n), plg(n);
for (int i = 0; i < n; ++i) {
D x, y;
cin >> x >> y;
pt[i] = P(x, y);
}
D ans = -1;
for (int i = 0; i < n; ++i) {
P rot = polar(D(1), -arg(pt[(i + 1) % n] - pt[i]));
for (int j = 0; j < n; ++j) {
plg[j] = (pt[(i + j) % n] - pt[i]) * rot;
}
D t = calc(plg, v);
ans = max(ans, t);
}
cout << ans << '\n';
}
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef double D;
typedef complex<D> P;
const D EPS = 1e-8;
D calc(const vector<P> &plg, D v) {
int n = plg.size();
int lt = n - 1, rt = 2;
D lx = 0, rx = real(plg[1]);
D y = 0;
while (1) {
D ny = min(imag(plg[lt]), imag(plg[rt]));
D nlx = lx + (ny - y) / (imag(plg[lt]) - y) * (real(plg[lt]) - lx);
D nrx = rx + (ny - y) / (imag(plg[rt]) - y) * (real(plg[rt]) - rx);
D w1 = rx - lx, w2 = nrx - nlx;
D h = ny - y;
D s = (w1 + w2) * h * 0.5;
if (s < v) {
v -= s;
y = ny;
lx = nlx;
rx = nrx;
if (imag(plg[lt]) == ny) {
--lt;
}
if (imag(plg[rt]) == ny) {
++rt;
}
assert(lt >= rt);
} else {
D ret = y;
D a = (w2 - w1) / h * 0.5;
D b = w1;
D c = -v;
if (abs(a) > EPS) {
D d = b * b - 4 * a * c;
assert(d >= 0);
d = sqrt(d);
D t1 = (-b + d) / a * 0.5;
D t2 = (-b - d) / a * 0.5;
if (t1 > t2) {
swap(t1, t2);
}
assert(t2 >= 0);
if (t1 >= 0) {
ret += t1;
} else {
ret += t2;
}
} else {
D t = -c / b;
assert(t >= 0);
ret += t;
}
return ret;
}
}
}
int main() {
cout << fixed << setprecision(9);
ios::sync_with_stdio(false);
int n;
D d, v;
while (cin >> n >> d >> v, n) {
v /= d;
vector<P> pt(n), plg(n);
for (int i = 0; i < n; ++i) {
D x, y;
cin >> x >> y;
pt[i] = P(x, y);
}
D ans = -1;
for (int i = 0; i < n; ++i) {
P rot = polar(D(1), -arg(pt[(i + 1) % n] - pt[i]));
for (int j = 0; j < n; ++j) {
plg[j] = (pt[(i + j) % n] - pt[i]) * rot;
}
D t = calc(plg, v);
ans = max(ans, t);
}
cout << ans << '\n';
}
} | replace | 39 | 40 | 39 | 40 | 0 | |
p00249 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); }
Point operator*(Point a) {
return Point(x * a.x - y * a.y, x * a.y + y * a.x);
}
bool operator<(const Point &p) const {
return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y);
}
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
bool operator<(const Segment &s) const {
return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2;
}
bool operator==(const Segment &s) const {
return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1);
}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
ostream &operator<<(ostream &os, const Point &a) {
os << "(" << a.x << "," << a.y << ")";
}
ostream &operator<<(ostream &os, const Segment &a) {
os << "( " << a.p1 << " , " << a.p2 << " )";
}
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
double abs(Point a) { return sqrt(norm(a)); }
// rad ????§???????????????¢?????§?????????????????¨
Point rotate(Point a, double rad) {
return Point(cos(rad) * a.x - sin(rad) * a.y,
sin(rad) * a.x + cos(rad) * a.y);
}
// ??????????????¢????????????
double toRad(double agl) { return agl * M_PI / 180.0; }
// a => prev, b => cur, c=> next
// prev ?????? cur ????????£??? next ????????????????§????????±???????
double getArg(Point a, Point b, Point c) {
double arg1 = atan2(b.y - a.y, b.x - a.x);
double arg2 = atan2(c.y - b.y, c.x - b.x);
double arg = fabs(arg1 - arg2);
while (arg > M_PI)
arg -= 2.0 * M_PI;
return fabs(arg);
}
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0;
Point b = p2 - p0;
if (cross(a, b) > EPS)
return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS)
return CLOCKWISE;
if (dot(a, b) < -EPS)
return ONLINE_BACK;
if (norm(a) < norm(b))
return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersectLL(Line l, Line m) {
return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel
abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line
}
bool intersectLS(Line l, Line s) {
return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l
cross(l.p2 - l.p1, s.p2 - l.p1) <
EPS; // s[1] is right of l
}
bool intersectLP(Line l, Point p) {
return abs(cross(l.p2 - p, l.p1 - p)) < EPS;
}
bool intersectSS(Line s, Line t) {
return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 &&
ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0;
}
bool intersectSP(Line s, Point p) {
return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) <
EPS; // triangle inequality
}
Point projection(Line l, Point p) {
double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2);
return l.p1 + (l.p1 - l.p2) * t;
}
Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; }
double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); }
double distanceLL(Line l, Line m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);
}
double distanceLS(Line l, Line s) {
if (intersectLS(l, s))
return 0;
return min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
double distanceSP(Line s, Point p) {
Point r = projection(s, p);
if (intersectSP(s, r))
return abs(r - p);
return min(abs(s.p1 - p), abs(s.p2 - p));
}
double distanceSS(Line s, Line t) {
if (intersectSS(s, t))
return 0;
return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),
min(distanceSP(t, s.p1), distanceSP(t, s.p2)));
}
Point crosspoint(Line l, Line m) {
double A = cross(l.p2 - l.p1, m.p2 - m.p1);
double B = cross(l.p2 - l.p1, l.p2 - m.p1);
if (abs(A) < EPS && abs(B) < EPS) {
vector<Point> vec;
vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1),
vec.push_back(m.p2);
sort(vec.begin(), vec.end());
assert(vec[1] == vec[2]); //???????????°??????????????????
return vec[1];
// return m.p1;
}
if (abs(A) < EPS)
assert(false);
return m.p1 + (m.p2 - m.p1) * (B / A);
}
double getArea(vector<Point> &vec) {
double sum = 0;
for (int i = 0; i < vec.size(); i++)
sum += cross(vec[i], vec[(i + 1) % vec.size()]);
return fabs(sum) / 2.0;
}
// cross product of pq and pr
double cross3p(Point p, Point q, Point r) {
return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x);
}
// returns true if point r is on the same line as the line pq
bool collinear(Point p, Point q, Point r) {
return fabs(cross3p(p, q, r)) < EPS;
}
// returns true if point t is on the left side of line pq
bool ccwtest(Point p, Point q, Point r) {
return cross3p(p, q, r) > 0; // can be modified to accept collinear points
}
bool onSegment(Point p, Point q, Point r) {
return collinear(p, q, r) &&
equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) +
sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)),
sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2)));
}
Point getCentroidOfConvex(Polygon &poly) {
double area = getArea(poly);
int V = poly.size();
assert(!equals(area, 0.0));
double x = 0, y = 0;
rep(i, (int)poly.size()) {
x += (poly[i].x + poly[(i + 1) % V].x) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
y += (poly[i].y + poly[(i + 1) % V].y) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
}
return Point(x / (6.0 * area), y / (6.0 * area));
}
// --------------------------
/*
360???????????£??¨?????? <= ?????????
or
???????????§?±±???????????????,?????????????????§??????
360????????????????????£????????£?????????
???????????§?????? ????????????(?????<)
?????´????????§????????¨?????????????????????
*/
bool DEBUG = 0;
bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }
int V;
double d, vol;
Polygon poly;
Point ps[110];
double lets_compute_arg(int L, int R) {
assert(L != R);
Point lp = poly[L], rp = poly[R];
// cout << lp << " " << rp << " ---" << endl;
rp = rp - lp;
lp = Point(0, 0);
// cout << lp << " " << rp << " ---" << endl;
Vector e = rp / abs(rp);
e = e * 100000;
e = rotate(e, toRad(90.0));
Line line = Line(e, e * -1);
// cout << "heikou? " << line.p1-line.p2 << " " << poly[L]-poly[R] << " :: "
// << cross(line.p1-line.p2,poly[L]-poly[R]) << endl;
assert(intersectLL(line, Line(poly[L], poly[R])));
Point cp = crosspoint(line, Line(poly[L], poly[R]));
// cout << "cp = " << cp << endl;
double ret = getArg(Point(0, -1000), Point(0, 0), cp);
if (LT(ret, toRad(90.0)))
ret = toRad(180.0) - ret;
/*
cout << "ret arg = " << ret * 180.0 / M_PI << endl;
cout << rotate(cp,toRad(180.0)+ret) << endl;
cout << rotate(poly[L],toRad(180.0)+ret) << endl;
cout << rotate(poly[R],toRad(180.0)+ret) << endl;
*/
return ret + toRad(180.0);
}
double getNewVolume(double h) {
Segment cut_line = Segment(Point(-20000, h), Point(20000, h));
int nV = 0;
// ps[0] = poly[0];
int index = 0;
int cross_ps[2];
// cout << "cut_line = " << cut_line << endl;
rep(i, V) {
Segment seg = Segment(poly[i], poly[(i + 1) % V]);
if (intersectSS(cut_line, seg)) { // ???????????´????????????
Point cp = crosspoint(cut_line, seg);
if (!(cp == seg.p1 || cp == seg.p2)) {
ps[nV++] = cp;
}
}
ps[nV++] = poly[(i + 1) % V];
}
rep(i, nV) if (equals(cut_line.p2.y, ps[i].y)) { cross_ps[index++] = i; }
/*
cout << "H = " << h << endl;
cout << "cutted poly = " << endl;
rep(i,nV) {
cout << ps[i] << endl;
} puts("");
cout << "index = " << index << endl;
*/
assert(index == 2);
int sp = -1;
double maxi_h = -1e30;
rep(i, nV) maxi_h = max(maxi_h, ps[i].y);
rep(i, nV) if (equals(maxi_h, ps[i].y)) {
sp = i;
break;
}
assert(sp != -1);
// cout << sp << " = " << cross_ps[0] << " or " << cross_ps[1] << endl;
// cout << ps[sp] << endl;
assert((cross_ps[0] != sp && cross_ps[1] != sp));
bool found = false;
int cur = sp;
vector<Point> npoly;
rep(i, nV) {
bool same_point = (cross_ps[0] == cur || cross_ps[1] == cur);
if (same_point && found) {
npoly.push_back(ps[cur]);
break;
}
if (same_point)
found = true;
if (found)
npoly.push_back(ps[cur]);
cur = (cur + 1) % nV;
}
if (DEBUG) {
cout << "added polygon" << endl;
rep(i, nV) { cout << ps[i] << endl; }
cout << endl;
cout << "cutted polygon" << endl;
rep(i, npoly.size()) { cout << npoly[i] << endl; }
}
double area = getArea(npoly);
return area * d;
}
void compute() {
bool full = false;
{
double area = getArea(poly) * d;
if (equals(area, vol))
full = true;
}
double maxi = 0;
Point centroid = getCentroidOfConvex(poly);
// cout << "centroid = " << centroid << endl;
rep(i, V) poly[i] = poly[i] - centroid;
centroid = Point(0, 0);
if (DEBUG) {
cout << "moved polygon" << endl;
rep(i, V) { cout << poly[i] << endl; }
puts("");
}
rep(_, V) {
if (DEBUG) {
cout << "let's try" << endl;
rep(i, V) { cout << poly[i] << endl; }
puts("");
}
double maxi_y = -1e30, mini_y = 1e30;
rep(i, V) maxi_y = max(maxi_y, poly[i].y), mini_y = min(mini_y, poly[i].y);
double low_y = mini_y, high_y = maxi_y;
if (!full) {
rep(__, 60) {
double mid_y = (low_y + high_y) / 2.0;
double new_volume = getNewVolume(mid_y);
if (equals(new_volume, vol)) {
maxi = max(maxi, fabs(mid_y - mini_y));
break;
}
if (LT(new_volume, vol))
low_y = mid_y;
else
high_y = mid_y;
}
} else {
rep(j, V) maxi = max(maxi, abs(mini_y - poly[j].y));
}
int left_p = -1, right_p = -1;
rep(i, V) if (equals(mini_y, poly[i].y)) {
if (left_p == -1)
left_p = i;
if (right_p == -1)
right_p = i;
if (LT(poly[i].x, poly[left_p].x))
left_p = i;
if (LT(poly[right_p].x, poly[i].x))
right_p = i;
}
/*
rep(i,V){
cout << poly[i] << "!" << endl;
}
*/
assert(left_p != right_p);
assert(left_p != -1 && right_p != -1);
// cout << poly[left_p] << " => " << centroid << " => " << poly[right_p] <<
// endl;
(left_p += 1) %= V;
(right_p += 1) %= V;
double rad = lets_compute_arg(left_p, right_p);
// cout << "rad = " << rad * 180.0 / M_PI << endl;
// assert( LT(rad * 180.0 / M_PI,180.0) );
// rad = toRad(360.0) - rad;
// cout << "rad = " << rad * 180.0 / M_PI << endl;
rep(i, V) poly[i] = rotate(poly[i], rad);
}
printf("%.10f\n", maxi);
}
void fix() {
bool update = true;
while (update) {
update = false;
rep(i, poly.size()) {
int j = (i + 1) % poly.size();
int k = (i + 2) % poly.size();
if (equals(cross(poly[j] - poly[i], poly[k] - poly[i]), 0.0)) {
update = true;
poly.erase(poly.begin() + j);
goto Skip;
}
}
Skip:;
}
V = poly.size();
}
int main() {
// ????????????????????±?????????????????????????????????????????¨
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c), a | b | c) {
V = a, d = b, vol = c;
poly.clear();
poly.resize(V);
rep(i, V) scanf("%lf %lf", &poly[i].x, &poly[i].y);
fix();
compute();
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-8)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); }
Point operator*(Point a) {
return Point(x * a.x - y * a.y, x * a.y + y * a.x);
}
bool operator<(const Point &p) const {
return !equals(x, p.x) ? x < p.x : (!equals(y, p.y) && y < p.y);
}
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
bool operator<(const Segment &s) const {
return (p2 == s.p2) ? p1 < s.p1 : p2 < s.p2;
}
bool operator==(const Segment &s) const {
return (s.p1 == p1 && s.p2 == p2) || (s.p1 == p2 && s.p2 == p1);
}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
ostream &operator<<(ostream &os, const Point &a) {
os << "(" << a.x << "," << a.y << ")";
}
ostream &operator<<(ostream &os, const Segment &a) {
os << "( " << a.p1 << " , " << a.p2 << " )";
}
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
double abs(Point a) { return sqrt(norm(a)); }
// rad ????§???????????????¢?????§?????????????????¨
Point rotate(Point a, double rad) {
return Point(cos(rad) * a.x - sin(rad) * a.y,
sin(rad) * a.x + cos(rad) * a.y);
}
// ??????????????¢????????????
double toRad(double agl) { return agl * M_PI / 180.0; }
// a => prev, b => cur, c=> next
// prev ?????? cur ????????£??? next ????????????????§????????±???????
double getArg(Point a, Point b, Point c) {
double arg1 = atan2(b.y - a.y, b.x - a.x);
double arg2 = atan2(c.y - b.y, c.x - b.x);
double arg = fabs(arg1 - arg2);
while (arg > M_PI)
arg -= 2.0 * M_PI;
return fabs(arg);
}
int ccw(Point p0, Point p1, Point p2) {
Point a = p1 - p0;
Point b = p2 - p0;
if (cross(a, b) > EPS)
return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS)
return CLOCKWISE;
if (dot(a, b) < -EPS)
return ONLINE_BACK;
if (norm(a) < norm(b))
return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersectLL(Line l, Line m) {
return abs(cross(l.p2 - l.p1, m.p2 - m.p1)) > EPS || // non-parallel
abs(cross(l.p2 - l.p1, m.p1 - l.p1)) < EPS; // same line
}
bool intersectLS(Line l, Line s) {
return cross(l.p2 - l.p1, s.p1 - l.p1) * // s[0] is left of l
cross(l.p2 - l.p1, s.p2 - l.p1) <
EPS; // s[1] is right of l
}
bool intersectLP(Line l, Point p) {
return abs(cross(l.p2 - p, l.p1 - p)) < EPS;
}
bool intersectSS(Line s, Line t) {
return ccw(s.p1, s.p2, t.p1) * ccw(s.p1, s.p2, t.p2) <= 0 &&
ccw(t.p1, t.p2, s.p1) * ccw(t.p1, t.p2, s.p2) <= 0;
}
bool intersectSP(Line s, Point p) {
return abs(s.p1 - p) + abs(s.p2 - p) - abs(s.p2 - s.p1) <
EPS; // triangle inequality
}
Point projection(Line l, Point p) {
double t = dot(p - l.p1, l.p1 - l.p2) / norm(l.p1 - l.p2);
return l.p1 + (l.p1 - l.p2) * t;
}
Point reflection(Line l, Point p) { return p + (projection(l, p) - p) * 2; }
double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); }
double distanceLL(Line l, Line m) {
return intersectLL(l, m) ? 0 : distanceLP(l, m.p1);
}
double distanceLS(Line l, Line s) {
if (intersectLS(l, s))
return 0;
return min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
double distanceSP(Line s, Point p) {
Point r = projection(s, p);
if (intersectSP(s, r))
return abs(r - p);
return min(abs(s.p1 - p), abs(s.p2 - p));
}
double distanceSS(Line s, Line t) {
if (intersectSS(s, t))
return 0;
return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)),
min(distanceSP(t, s.p1), distanceSP(t, s.p2)));
}
Point crosspoint(Line l, Line m) {
double A = cross(l.p2 - l.p1, m.p2 - m.p1);
double B = cross(l.p2 - l.p1, l.p2 - m.p1);
if (abs(A) < EPS && abs(B) < EPS) {
vector<Point> vec;
vec.push_back(l.p1), vec.push_back(l.p2), vec.push_back(m.p1),
vec.push_back(m.p2);
sort(vec.begin(), vec.end());
assert(vec[1] == vec[2]); //???????????°??????????????????
return vec[1];
// return m.p1;
}
if (abs(A) < EPS)
assert(false);
return m.p1 + (m.p2 - m.p1) * (B / A);
}
double getArea(vector<Point> &vec) {
double sum = 0;
for (int i = 0; i < vec.size(); i++)
sum += cross(vec[i], vec[(i + 1) % vec.size()]);
return fabs(sum) / 2.0;
}
// cross product of pq and pr
double cross3p(Point p, Point q, Point r) {
return (r.x - q.x) * (p.y - q.y) - (r.y - q.y) * (p.x - q.x);
}
// returns true if point r is on the same line as the line pq
bool collinear(Point p, Point q, Point r) {
return fabs(cross3p(p, q, r)) < EPS;
}
// returns true if point t is on the left side of line pq
bool ccwtest(Point p, Point q, Point r) {
return cross3p(p, q, r) > 0; // can be modified to accept collinear points
}
bool onSegment(Point p, Point q, Point r) {
return collinear(p, q, r) &&
equals(sqrt(pow(p.x - r.x, 2) + pow(p.y - r.y, 2)) +
sqrt(pow(r.x - q.x, 2) + pow(r.y - q.y, 2)),
sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2)));
}
Point getCentroidOfConvex(Polygon &poly) {
double area = getArea(poly);
int V = poly.size();
assert(!equals(area, 0.0));
double x = 0, y = 0;
rep(i, (int)poly.size()) {
x += (poly[i].x + poly[(i + 1) % V].x) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
y += (poly[i].y + poly[(i + 1) % V].y) *
(poly[i].x * poly[(i + 1) % V].y - poly[(i + 1) % V].x * poly[i].y);
}
return Point(x / (6.0 * area), y / (6.0 * area));
}
// --------------------------
/*
360???????????£??¨?????? <= ?????????
or
???????????§?±±???????????????,?????????????????§??????
360????????????????????£????????£?????????
???????????§?????? ????????????(?????<)
?????´????????§????????¨?????????????????????
*/
bool DEBUG = 0;
bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }
int V;
double d, vol;
Polygon poly;
Point ps[110];
double lets_compute_arg(int L, int R) {
assert(L != R);
Point lp = poly[L], rp = poly[R];
// cout << lp << " " << rp << " ---" << endl;
rp = rp - lp;
lp = Point(0, 0);
// cout << lp << " " << rp << " ---" << endl;
Vector e = rp / abs(rp);
e = e * 100000;
e = rotate(e, toRad(90.0));
Line line = Line(e, e * -1);
// cout << "heikou? " << line.p1-line.p2 << " " << poly[L]-poly[R] << " :: "
// << cross(line.p1-line.p2,poly[L]-poly[R]) << endl;
assert(intersectLL(line, Line(poly[L], poly[R])));
Point cp = crosspoint(line, Line(poly[L], poly[R]));
// cout << "cp = " << cp << endl;
double ret = getArg(Point(0, -1000), Point(0, 0), cp);
// if( LT(ret,toRad(90.0)) ) ret = toRad(180.0) - ret;
/*
cout << "ret arg = " << ret * 180.0 / M_PI << endl;
cout << rotate(cp,toRad(180.0)+ret) << endl;
cout << rotate(poly[L],toRad(180.0)+ret) << endl;
cout << rotate(poly[R],toRad(180.0)+ret) << endl;
*/
return ret + toRad(180.0);
}
double getNewVolume(double h) {
Segment cut_line = Segment(Point(-20000, h), Point(20000, h));
int nV = 0;
// ps[0] = poly[0];
int index = 0;
int cross_ps[2];
// cout << "cut_line = " << cut_line << endl;
rep(i, V) {
Segment seg = Segment(poly[i], poly[(i + 1) % V]);
if (intersectSS(cut_line, seg)) { // ???????????´????????????
Point cp = crosspoint(cut_line, seg);
if (!(cp == seg.p1 || cp == seg.p2)) {
ps[nV++] = cp;
}
}
ps[nV++] = poly[(i + 1) % V];
}
rep(i, nV) if (equals(cut_line.p2.y, ps[i].y)) { cross_ps[index++] = i; }
/*
cout << "H = " << h << endl;
cout << "cutted poly = " << endl;
rep(i,nV) {
cout << ps[i] << endl;
} puts("");
cout << "index = " << index << endl;
*/
assert(index == 2);
int sp = -1;
double maxi_h = -1e30;
rep(i, nV) maxi_h = max(maxi_h, ps[i].y);
rep(i, nV) if (equals(maxi_h, ps[i].y)) {
sp = i;
break;
}
assert(sp != -1);
// cout << sp << " = " << cross_ps[0] << " or " << cross_ps[1] << endl;
// cout << ps[sp] << endl;
assert((cross_ps[0] != sp && cross_ps[1] != sp));
bool found = false;
int cur = sp;
vector<Point> npoly;
rep(i, nV) {
bool same_point = (cross_ps[0] == cur || cross_ps[1] == cur);
if (same_point && found) {
npoly.push_back(ps[cur]);
break;
}
if (same_point)
found = true;
if (found)
npoly.push_back(ps[cur]);
cur = (cur + 1) % nV;
}
if (DEBUG) {
cout << "added polygon" << endl;
rep(i, nV) { cout << ps[i] << endl; }
cout << endl;
cout << "cutted polygon" << endl;
rep(i, npoly.size()) { cout << npoly[i] << endl; }
}
double area = getArea(npoly);
return area * d;
}
void compute() {
bool full = false;
{
double area = getArea(poly) * d;
if (equals(area, vol))
full = true;
}
double maxi = 0;
Point centroid = getCentroidOfConvex(poly);
// cout << "centroid = " << centroid << endl;
rep(i, V) poly[i] = poly[i] - centroid;
centroid = Point(0, 0);
if (DEBUG) {
cout << "moved polygon" << endl;
rep(i, V) { cout << poly[i] << endl; }
puts("");
}
rep(_, V) {
if (DEBUG) {
cout << "let's try" << endl;
rep(i, V) { cout << poly[i] << endl; }
puts("");
}
double maxi_y = -1e30, mini_y = 1e30;
rep(i, V) maxi_y = max(maxi_y, poly[i].y), mini_y = min(mini_y, poly[i].y);
double low_y = mini_y, high_y = maxi_y;
if (!full) {
rep(__, 60) {
double mid_y = (low_y + high_y) / 2.0;
double new_volume = getNewVolume(mid_y);
if (equals(new_volume, vol)) {
maxi = max(maxi, fabs(mid_y - mini_y));
break;
}
if (LT(new_volume, vol))
low_y = mid_y;
else
high_y = mid_y;
}
} else {
rep(j, V) maxi = max(maxi, abs(mini_y - poly[j].y));
}
int left_p = -1, right_p = -1;
rep(i, V) if (equals(mini_y, poly[i].y)) {
if (left_p == -1)
left_p = i;
if (right_p == -1)
right_p = i;
if (LT(poly[i].x, poly[left_p].x))
left_p = i;
if (LT(poly[right_p].x, poly[i].x))
right_p = i;
}
/*
rep(i,V){
cout << poly[i] << "!" << endl;
}
*/
assert(left_p != right_p);
assert(left_p != -1 && right_p != -1);
// cout << poly[left_p] << " => " << centroid << " => " << poly[right_p] <<
// endl;
(left_p += 1) %= V;
(right_p += 1) %= V;
double rad = lets_compute_arg(left_p, right_p);
// cout << "rad = " << rad * 180.0 / M_PI << endl;
// assert( LT(rad * 180.0 / M_PI,180.0) );
// rad = toRad(360.0) - rad;
// cout << "rad = " << rad * 180.0 / M_PI << endl;
rep(i, V) poly[i] = rotate(poly[i], rad);
}
printf("%.10f\n", maxi);
}
void fix() {
bool update = true;
while (update) {
update = false;
rep(i, poly.size()) {
int j = (i + 1) % poly.size();
int k = (i + 2) % poly.size();
if (equals(cross(poly[j] - poly[i], poly[k] - poly[i]), 0.0)) {
update = true;
poly.erase(poly.begin() + j);
goto Skip;
}
}
Skip:;
}
V = poly.size();
}
int main() {
// ????????????????????±?????????????????????????????????????????¨
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c), a | b | c) {
V = a, d = b, vol = c;
poly.clear();
poly.resize(V);
rep(i, V) scanf("%lf %lf", &poly[i].x, &poly[i].y);
fix();
compute();
}
return 0;
} | replace | 254 | 256 | 254 | 255 | 0 | |
p00250 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n | m) {
vector<int> scone(n);
for (auto &i : scone)
cin >> i;
vector<int> sum = {0};
for (auto i : scone)
sum.push_back((sum.back() + (i % m)) % m);
int answer = 0;
set<int> used = {m};
for (int j = 1; j < sum.size(); ++j) {
answer = max(
answer,
(sum[j] - *lower_bound(begin(used), end(used), sum[j]) + m + m) % m);
used.insert(sum[j]);
used.insert(sum[j] + m);
}
cout << answer << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n | m) {
vector<int> scone(n);
for (auto &i : scone)
cin >> i;
vector<int> sum = {0};
for (auto i : scone)
sum.push_back((sum.back() + (i % m)) % m);
int answer = 0;
set<int> used = {m};
for (int j = 1; j < sum.size(); ++j) {
answer = max(answer, (sum[j] - *used.lower_bound(sum[j]) + m + m) % m);
used.insert(sum[j]);
used.insert(sum[j] + m);
}
cout << answer << endl;
}
} | replace | 16 | 19 | 16 | 17 | TLE | |
p00252 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int b1, b2, b3;
scanf("%d %d %d", b1, b2, b3);
if (b1 == 0 && b2 == 0 && b3 == 1)
printf("Open\n");
else
printf("Close\n");
return 0;
} | #include <stdio.h>
int main(void) {
int b1, b2, b3;
scanf("%d %d %d", &b1, &b2, &b3);
if (b1 == 1 && b2 == 1 || b3 == 1)
printf("Open\n");
else
printf("Close\n");
return 0;
} | replace | 3 | 5 | 3 | 5 | -11 | |
p00253 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int x, n, h[101], r[10], s[99], i, j, flg;
while (1) {
flg = 0;
scanf("%d", &n);
if (n == 0)
break;
for (i = 0; i < n + 1; i++) {
scanf("%d", &h[i]);
}
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n; j++) {
if (j >= i) {
r[j] = h[j + 1];
} else {
r[j] = h[j];
}
}
for (j = 0; j < n - 1; j++) {
s[j] = r[j + 1] - r[j];
}
for (j = 0; j < n - 2; j++) {
if (s[j] != s[j + 1]) {
flg = 0;
break;
}
if (s[j] == s[j + 1]) {
flg = 1;
}
}
if (flg == 1) {
x = h[i];
i = 1000;
}
}
printf("%d\n", x);
}
return 0;
} | #include <stdio.h>
int main(void) {
int x, n, h[101], r[100], s[99], i, j, flg;
while (1) {
flg = 0;
scanf("%d", &n);
if (n == 0)
break;
for (i = 0; i < n + 1; i++) {
scanf("%d", &h[i]);
}
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n; j++) {
if (j >= i) {
r[j] = h[j + 1];
} else {
r[j] = h[j];
}
}
for (j = 0; j < n - 1; j++) {
s[j] = r[j + 1] - r[j];
}
for (j = 0; j < n - 2; j++) {
if (s[j] != s[j + 1]) {
flg = 0;
break;
}
if (s[j] == s[j + 1]) {
flg = 1;
}
}
if (flg == 1) {
x = h[i];
i = 1000;
}
}
printf("%d\n", x);
}
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00253 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n, a, c, j;
int b[105];
int s[105];
int i = 0;
while (cin >> n && n) {
n++;
for (j = 0; j < n; j++) {
cin >> b[j];
}
c = b[1] - b[0];
for (j = 1; j < n - 1; j++) {
if (c != b[j + 1] - b[j]) {
if (j == 1) {
c = b[4] - b[3];
a = b[1] - b[0];
if (c != a) {
if (c == b[2] - b[0]) {
j = 0;
} else {
j = -1;
}
}
}
if (j == 2) {
if (b[2] - b[0] == b[4] - b[3]) {
j = 0;
}
}
j++;
break;
}
}
s[i] = b[j];
i++;
}
for (j = 0; j < i; j++) {
cout << s[j] << endl;
}
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int n, a, c, j;
int b[105];
int s[505];
int i = 0;
while (cin >> n && n) {
n++;
for (j = 0; j < n; j++) {
cin >> b[j];
}
c = b[1] - b[0];
for (j = 1; j < n - 1; j++) {
if (c != b[j + 1] - b[j]) {
if (j == 1) {
c = b[4] - b[3];
a = b[1] - b[0];
if (c != a) {
if (c == b[2] - b[0]) {
j = 0;
} else {
j = -1;
}
}
}
if (j == 2) {
if (b[2] - b[0] == b[4] - b[3]) {
j = 0;
}
}
j++;
break;
}
}
s[i] = b[j];
i++;
}
for (j = 0; j < i; j++) {
cout << s[j] << endl;
}
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p00253 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))
int main() {
int n, t, f;
int h[101];
for (;;) {
scanf("%d", &n);
n++;
rep(i, n) scanf("%d", i + h);
rep(i, n) {
f = 1;
t = h[1 + (i < 2)] - h[i == 0];
rep(j, n - 2) {
if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
f = 0;
break;
}
}
if (f) {
printf("%d\n", h[i]);
break;
}
}
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))
int main() {
int n, t, f;
int h[101];
for (;;) {
scanf("%d", &n);
if (!n)
break;
n++;
rep(i, n) scanf("%d", i + h);
rep(i, n) {
f = 1;
t = h[1 + (i < 2)] - h[i == 0];
rep(j, n - 2) {
if (t != h[j + 1 + (j + 1 >= i)] - h[j + (j >= i)]) {
f = 0;
break;
}
}
if (f) {
printf("%d\n", h[i]);
break;
}
}
}
return 0;
} | insert | 25 | 25 | 25 | 27 | TLE | |
p00253 | C++ | Runtime Error | #include <iostream>
using namespace std;
int n, h[100];
int ans[500], cor = 0;
int main() {
int i, j;
while (cin >> n) {
if (n == 0)
break;
n++;
for (i = 0; i < n; i++)
cin >> h[i];
// 雑草を抜いたら等差数列になるかも…
int before, now, d;
bool flag;
for (i = 0; i < n; i++) {
flag = false;
for (j = 0; j < n; j++) {
if (i == j)
continue;
if (flag == false) {
flag = true;
before = h[j];
if (i != j + 1)
d = h[j + 1] - h[j];
else
d = h[j + 2] - h[j];
} else {
now = h[j];
if (now - before != d)
break;
before = now;
}
}
if (j == n) {
ans[cor] = h[i];
break;
}
}
cor++;
}
for (i = 0; i < cor; i++)
cout << ans[i] << endl;
return 0;
} | #include <iostream>
using namespace std;
int n, h[101];
int ans[500], cor = 0;
int main() {
int i, j;
while (cin >> n) {
if (n == 0)
break;
n++;
for (i = 0; i < n; i++)
cin >> h[i];
// 雑草を抜いたら等差数列になるかも…
int before, now, d;
bool flag;
for (i = 0; i < n; i++) {
flag = false;
for (j = 0; j < n; j++) {
if (i == j)
continue;
if (flag == false) {
flag = true;
before = h[j];
if (i != j + 1)
d = h[j + 1] - h[j];
else
d = h[j + 2] - h[j];
} else {
now = h[j];
if (now - before != d)
break;
before = now;
}
}
if (j == n) {
ans[cor] = h[i];
break;
}
}
cor++;
}
for (i = 0; i < cor; i++)
cout << ans[i] << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00253 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
int N;
int H[100];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> N) {
if (N == 0)
break;
N++;
rep(i, N) cin >> H[i];
int w = -1;
rep(i, N) {
vector<int> xs;
rep(j, i) xs.pb(H[j]);
for (int j = i + 1; j < N; j++)
xs.pb(H[j]);
int diff = xs[1] - xs[0];
bool f = true;
rep(i, (int)xs.size() - 1) {
if (diff != xs[i + 1] - xs[i])
f = false;
}
if (f) {
w = H[i];
break;
}
}
cout << w << "\n";
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
int N;
int H[101];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> N) {
if (N == 0)
break;
N++;
rep(i, N) cin >> H[i];
int w = -1;
rep(i, N) {
vector<int> xs;
rep(j, i) xs.pb(H[j]);
for (int j = i + 1; j < N; j++)
xs.pb(H[j]);
int diff = xs[1] - xs[0];
bool f = true;
rep(i, (int)xs.size() - 1) {
if (diff != xs[i + 1] - xs[i])
f = false;
}
if (f) {
w = H[i];
break;
}
}
cout << w << "\n";
}
return 0;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p00253 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
for (int n; cin >> n && n;) {
vector<int> l(n);
for (int i = 0; i <= n; i++)
cin >> l[i];
for (int s = 0; s <= n; s++) {
int diff = INT_MAX;
for (int i = s ? 1 : 2; i <= n; i++) {
if (i == s)
continue;
int be = i == s + 1 ? 2 : 1;
if (diff == INT_MAX) {
diff = l[i] - l[i - be];
} else if (diff != l[i] - l[i - be]) {
goto BREAK;
}
}
cout << l[s] << endl;
break;
BREAK:;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
for (int n; cin >> n && n;) {
vector<int> l(n + 1);
for (int i = 0; i <= n; i++)
cin >> l[i];
for (int s = 0; s <= n; s++) {
int diff = INT_MAX;
for (int i = s ? 1 : 2; i <= n; i++) {
if (i == s)
continue;
int be = i == s + 1 ? 2 : 1;
if (diff == INT_MAX) {
diff = l[i] - l[i - be];
} else if (diff != l[i] - l[i - be]) {
goto BREAK;
}
}
cout << l[s] << endl;
break;
BREAK:;
}
}
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00253 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
int N;
while (cin >> N && N) {
vector<int> a(N + 1);
vector<int> ans;
REP(i, N + 1) cin >> a[i];
REP(i, N + 1) {
vector<int> b;
REP(j, N + 1) if (i != j) b.push_back(a[j]);
set<int> s;
REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
if (s.size() == 1) {
ans.push_back(a[i]);
}
}
assert(ans.size() == 1);
cout << ans[0] << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
int N;
while (cin >> N && N) {
vector<int> a(N + 1);
vector<int> ans;
REP(i, N + 1) cin >> a[i];
REP(i, N + 1) {
vector<int> b;
REP(j, N + 1) if (i != j) b.push_back(a[j]);
set<int> s;
REP(j, b.size() - 1) s.insert(b[j + 1] - b[j]);
if (s.size() == 1) {
ans.push_back(a[i]);
}
}
// assert(ans.size() == 1);
cout << ans[0] << endl;
}
return 0;
} | replace | 53 | 54 | 53 | 54 | 0 | |
p00254 | 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"
string to_string_keta(const long long int l, const int keta) {
ostringstream oss;
oss << setprecision(keta) << l;
return oss.str();
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
if (N % 1111 == 0) {
cout << "NA" << endl;
continue;
}
int num = 0;
while (1) {
if (N == 6174)
break;
string st(to_string_keta(N, 4));
sort(st.begin(), st.end());
int amin(stoi(st));
reverse(st.begin(), st.end());
int amax(stoi(st));
N = amax - amin;
num++;
}
cout << num << 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"
string to_string_keta(const long long int l, const int keta) {
ostringstream oss;
oss << std::setw(keta) << std::setfill('0') << l;
return oss.str();
}
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
if (N % 1111 == 0) {
cout << "NA" << endl;
continue;
}
int num = 0;
while (1) {
if (N == 6174)
break;
string st(to_string_keta(N, 4));
sort(st.begin(), st.end());
int amin(stoi(st));
reverse(st.begin(), st.end());
int amax(stoi(st));
N = amax - amin;
num++;
}
cout << num << endl;
}
return 0;
} | replace | 15 | 16 | 15 | 16 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int func2(int *temp) {
int n = 0;
n += (temp[0] * 1000);
n += (temp[1] * 100);
n += (temp[2] * 10);
n += temp[3];
return n;
}
int main() {
while (true) {
int N = 0, Nm[4] = {0};
unsigned int count = 0;
int judge = 0;
int Lm[4] = {0}, Sm[4] = {0};
int S = 0, L = 0;
cin >> N;
Nm[0] = N / 1000;
N %= 1000;
Nm[1] = N / 100;
N %= 100;
Nm[2] = N / 10;
N %= 10;
Nm[3] = N;
if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
break;
for (int i = 1; i < 4; i++) {
if (Nm[i] == Nm[0])
judge++;
}
if (judge != 4) {
while (true) {
if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
break;
// Sを作成
sort(Nm, Nm + 4);
for (int i = 0; i < 4; i++) {
Sm[i] = Nm[i];
// cout << Sm[i] << endl;
}
S = func2(Sm);
// Lを作成
sort(Nm, Nm + 4, greater<int>());
for (int i = 0; i < 4; i++) {
Lm[i] = Nm[i];
}
L = func2(Lm);
N = L - S;
count++;
Nm[0] = N / 1000;
N %= 1000;
Nm[1] = N / 100;
N %= 100;
Nm[2] = N / 10;
N %= 10;
Nm[3] = N;
}
cout << count << endl;
}
if (judge == 4) {
cout << "NA" << endl;
}
}
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int func2(int *temp) {
int n = 0;
n += (temp[0] * 1000);
n += (temp[1] * 100);
n += (temp[2] * 10);
n += temp[3];
return n;
}
int main() {
while (true) {
int N = 0, Nm[4] = {0};
unsigned int count = 0;
int judge = 0;
int Lm[4] = {0}, Sm[4] = {0};
int S = 0, L = 0;
cin >> N;
Nm[0] = N / 1000;
N %= 1000;
Nm[1] = N / 100;
N %= 100;
Nm[2] = N / 10;
N %= 10;
Nm[3] = N;
if ((Nm[0] == 0 && Nm[1] == 0) && (Nm[2] == 0 && Nm[3] == 0))
break;
for (int i = 0; i < 4; i++) {
if (Nm[i] == Nm[0])
judge++;
}
if (judge != 4) {
while (true) {
if ((Nm[0] == 6 && Nm[1] == 1) && (Nm[2] == 7 && Nm[3] == 4))
break;
// Sを作成
sort(Nm, Nm + 4);
for (int i = 0; i < 4; i++) {
Sm[i] = Nm[i];
// cout << Sm[i] << endl;
}
S = func2(Sm);
// Lを作成
sort(Nm, Nm + 4, greater<int>());
for (int i = 0; i < 4; i++) {
Lm[i] = Nm[i];
}
L = func2(Lm);
N = L - S;
count++;
Nm[0] = N / 1000;
N %= 1000;
Nm[1] = N / 100;
N %= 100;
Nm[2] = N / 10;
N %= 10;
Nm[3] = N;
}
cout << count << endl;
}
if (judge == 4) {
cout << "NA" << endl;
}
}
return 0;
} | replace | 39 | 40 | 39 | 40 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;
template <class F, class T> void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
int main() {
int x, a, b;
string s;
while (scanf("%d", &x), x) {
if (x % 1111 == 0) {
puts("NA");
} else {
int ans;
for (ans = 0; x != 6174; ++ans) {
convert(x, s);
sort(s.begin(), s.end());
convert(s, a);
reverse(s.begin(), s.end());
convert(s, b);
x = b - a;
}
printf("%d\n", ans);
}
}
} | #include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;
template <class F, class T> void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
int main() {
int x, a, b;
string s;
while (scanf("%d", &x), x) {
if (x % 1111 == 0) {
puts("NA");
} else {
int ans;
for (ans = 0; x != 6174; ++ans) {
convert(x, s);
s.resize(4, '0');
sort(s.begin(), s.end());
convert(s, a);
reverse(s.begin(), s.end());
convert(s, b);
x = b - a;
}
printf("%d\n", ans);
}
}
} | insert | 22 | 22 | 22 | 23 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
vector<int> solve(vector<int> a, vector<int> b) {
string x, y;
rep(i, a.size()) { x += '0' + a[i]; }
rep(i, b.size()) { y += '0' + b[i]; }
int r = stoi(x) - stoi(y);
vector<int> ret;
stringstream ss;
ss << r;
string z = ss.str();
rep(i, z.size()) ret.pb(z[i] - '0');
return ret;
}
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
int x = 0;
vector<int> c;
rep(i, s.size()) c.pb(s[i] - '0');
while (1) {
if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
cout << x << endl;
break;
}
if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
cout << "NA" << endl;
break;
}
vector<int> l = c, s = c;
sort(all(s));
sort(all(l), greater<int>());
c = solve(l, s);
x++;
}
}
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
vector<int> solve(vector<int> a, vector<int> b) {
string x, y;
rep(i, a.size()) { x += '0' + a[i]; }
rep(i, b.size()) { y += '0' + b[i]; }
int r = stoi(x) - stoi(y);
vector<int> ret;
stringstream ss;
ss << r;
string z = ss.str();
string t = "";
rep(i, 4 - z.size()) t += "0";
z = t + z;
rep(i, z.size()) ret.pb(z[i] - '0');
return ret;
}
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
int x = 0;
vector<int> c;
rep(i, s.size()) c.pb(s[i] - '0');
while (1) {
if (c[0] == 6 && c[1] == 1 && c[2] == 7 && c[3] == 4) {
cout << x << endl;
break;
}
if (c[0] == c[1] && c[1] == c[2] && c[2] == c[3]) {
cout << "NA" << endl;
break;
}
vector<int> l = c, s = c;
sort(all(s));
sort(all(l), greater<int>());
c = solve(l, s);
x++;
}
}
} | insert | 16 | 16 | 16 | 19 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
int cnt, a, b;
char num[5];
while (1) {
cin >> num;
cnt = 0;
if (strcmp(num, "0000") == 0)
break;
if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
cout << "NA" << endl;
continue;
}
while (1) {
if (strcmp(num, "6174") == 0)
break;
cnt++;
sort(num, num + 4);
a = atoi(num);
sort(num, num + 4, greater<char>());
b = atoi(num);
b -= a;
sprintf(num, "%d", b);
}
cout << cnt << endl;
}
} | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
int cnt, a, b;
char num[5];
while (1) {
cin >> num;
cnt = 0;
if (strcmp(num, "0000") == 0)
break;
if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
cout << "NA" << endl;
continue;
}
while (1) {
if (strcmp(num, "6174") == 0)
break;
cnt++;
sort(num, num + 4);
a = atoi(num);
sort(num, num + 4, greater<char>());
b = atoi(num);
b -= a;
sprintf(num, "%04d", b);
}
cout << cnt << endl;
}
} | replace | 28 | 29 | 28 | 29 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
void solve() {
string str;
while (cin >> str) {
if (str == "0000") {
break;
} else if (atoi(str.c_str()) % 1111 == 0) {
cout << "NA" << endl;
continue;
}
int count = 0;
while (str != "6174") {
sort(str.begin(), str.end());
string small = str;
string large;
for (int i = str.size() - 1; i >= 0; --i) {
large += str[i];
}
int num = atoi(large.c_str()) - atoi(small.c_str());
stringstream ss;
ss << num;
str = ss.str();
++count;
}
cout << count << endl;
}
}
int main() {
solve();
return (0);
} | #include <algorithm>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
void solve() {
string str;
while (cin >> str) {
if (str == "0000") {
break;
} else if (atoi(str.c_str()) % 1111 == 0) {
cout << "NA" << endl;
continue;
}
int count = 0;
while (str != "6174") {
sort(str.begin(), str.end());
string small = str;
string large;
for (int i = str.size() - 1; i >= 0; --i) {
large += str[i];
}
int num = atoi(large.c_str()) - atoi(small.c_str());
stringstream ss;
ss << num;
str = ss.str();
if (str.size() < 4) {
string buffer = str;
str.clear();
for (int i = 0; i < 4 - buffer.size(); ++i) {
str += "0";
}
str += buffer;
}
++count;
}
cout << count << endl;
}
}
int main() {
solve();
return (0);
} | insert | 29 | 29 | 29 | 37 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
// C++
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct edge {
int to, cost;
};
// typedef pair<int,int> P;
bool isupper(char c) {
if ('A' <= c && c <= 'Z')
return 1;
return 0;
}
bool islower(char c) {
if ('a' <= c && c <= 'z')
return 1;
return 0;
}
bool isPrime(int x) {
if (x == 1)
return 0;
if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (int i = 3; i * i <= x; i++)
if (x % i == 0)
return 0;
return 1;
}
bool iskaibun(string s) {
for (int i = 0; i < s.size() / 2; i++)
if (s[i] != s[s.size() - i - 1])
return 0;
return 1;
}
bool isnumber(char c) { return ('0' <= c && c <= '9'); }
bool isalpha(char c) { return (isupper(c) && islower(c)); }
void printvi(vector<int> v) {
rep(i, 0, v.size()) {
if (i)
cout << " ";
cout << v[i];
}
cout << endl;
}
void printvil(vector<int> v) {
rep(i, 0, v.size()) { cout << v[i] << endl; }
}
void printvvi(vector<vector<int>> v) {
rep(i, 0, v.size()) {
rep(j, 0, v[i].size()) {
if (j)
cout << " ";
cout << v[i][j];
}
cout << endl;
}
}
void printvstr(vector<string> v) {
rep(i, 0, v.size()) { cout << v[i] << endl; }
}
signed main() {
string s;
while (1) {
cin >> s;
if (s == "0000")
break;
auto cmp = [&](char a, char b) { return a > b; };
if (s[0] == s[1] and s[1] == s[2] and s[2] == s[3]) {
cout << "NA" << endl;
continue;
}
int i = 0;
for (; s != "6174"; i++) {
sort(all(s));
int a1 = stol(s);
sort(all(s), cmp);
int a2 = stol(s);
int a3 = a2 - a1;
s = to_string(a3);
}
cout << i << endl;
}
}
/*
*/
| #include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
// C++
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct edge {
int to, cost;
};
// typedef pair<int,int> P;
bool isupper(char c) {
if ('A' <= c && c <= 'Z')
return 1;
return 0;
}
bool islower(char c) {
if ('a' <= c && c <= 'z')
return 1;
return 0;
}
bool isPrime(int x) {
if (x == 1)
return 0;
if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (int i = 3; i * i <= x; i++)
if (x % i == 0)
return 0;
return 1;
}
bool iskaibun(string s) {
for (int i = 0; i < s.size() / 2; i++)
if (s[i] != s[s.size() - i - 1])
return 0;
return 1;
}
bool isnumber(char c) { return ('0' <= c && c <= '9'); }
bool isalpha(char c) { return (isupper(c) && islower(c)); }
void printvi(vector<int> v) {
rep(i, 0, v.size()) {
if (i)
cout << " ";
cout << v[i];
}
cout << endl;
}
void printvil(vector<int> v) {
rep(i, 0, v.size()) { cout << v[i] << endl; }
}
void printvvi(vector<vector<int>> v) {
rep(i, 0, v.size()) {
rep(j, 0, v[i].size()) {
if (j)
cout << " ";
cout << v[i][j];
}
cout << endl;
}
}
void printvstr(vector<string> v) {
rep(i, 0, v.size()) { cout << v[i] << endl; }
}
signed main() {
string s;
while (1) {
cin >> s;
if (s == "0000")
break;
auto cmp = [&](char a, char b) { return a > b; };
if (s[0] == s[1] and s[1] == s[2] and s[2] == s[3]) {
cout << "NA" << endl;
continue;
}
int i = 0;
for (; s != "6174"; i++) {
sort(all(s));
int a1 = stol(s);
sort(all(s), cmp);
int a2 = stol(s);
int a3 = a2 - a1;
s = to_string(a3);
while (s.size() < 4) {
s = "0" + s;
}
// cout<<s<<endl;
}
cout << i << endl;
}
}
/*
*/
| insert | 177 | 177 | 177 | 181 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
const std::string answer = "6174";
int main() {
std::string in_str;
std::cin >> in_str;
int L, S;
int i;
while (in_str[0] != '0' || in_str[1] != '0' || in_str[2] != '0' ||
in_str[3] != '0') {
if (in_str[0] != in_str[1] || in_str[1] != in_str[2] ||
in_str[2] != in_str[3]) {
for (i = 0; in_str != answer; i++) {
{
std::stringstream sss;
std::sort(in_str.begin(), in_str.end());
sss << in_str;
sss >> S;
}
{
std::stringstream sss;
std::sort(in_str.begin(), in_str.end(), std::greater<char>());
sss << in_str;
sss >> L;
}
L -= S;
{
std::stringstream sss;
sss << L;
in_str = sss.str();
}
}
std::cout << i << std::endl;
} else {
std::cout << "NA" << std::endl;
}
std::cin >> in_str;
}
}
| #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
const std::string answer = "6174";
int main() {
std::string in_str;
std::cin >> in_str;
int L, S;
int i;
while (in_str[0] != '0' || in_str[1] != '0' || in_str[2] != '0' ||
in_str[3] != '0') {
if (in_str[0] != in_str[1] || in_str[1] != in_str[2] ||
in_str[2] != in_str[3]) {
for (i = 0; in_str != answer; i++) {
{
std::stringstream sss;
std::sort(in_str.begin(), in_str.end());
sss << in_str;
sss >> S;
}
{
std::stringstream sss;
std::sort(in_str.begin(), in_str.end(), std::greater<char>());
sss << in_str;
sss >> L;
}
L -= S;
{
std::stringstream sss;
sss << L;
in_str = sss.str();
while (in_str.length() != 4) {
in_str = '0' + in_str;
}
}
}
std::cout << i << std::endl;
} else {
std::cout << "NA" << std::endl;
}
std::cin >> in_str;
}
} | insert | 34 | 34 | 34 | 37 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int toInt(string s) {
stringstream ss(s);
int ret;
ss >> ret;
return ret;
}
string toStr(int n) {
stringstream ss;
ss << n;
return ss.str();
}
string next(string N) {
sort(N.begin(), N.end());
int S = toInt(N);
reverse(N.begin(), N.end());
int L = toInt(N);
return toStr(L - S);
}
int main() {
string N;
while (cin >> N, N != "0000") {
if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
cout << "NA" << endl;
continue;
}
int cnt = 0;
while (N != "6174") {
N = next(N);
++cnt;
}
cout << cnt << endl;
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int toInt(string s) {
stringstream ss(s);
int ret;
ss >> ret;
return ret;
}
string toStr(int n) {
stringstream ss;
ss << n;
return ss.str();
}
string next(string N) {
sort(N.begin(), N.end());
int S = toInt(N);
reverse(N.begin(), N.end());
int L = toInt(N);
string ret = "0000" + toStr(L - S);
return ret.substr((int)ret.size() - 4);
}
int main() {
string N;
while (cin >> N, N != "0000") {
if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
cout << "NA" << endl;
continue;
}
int cnt = 0;
while (N != "6174") {
N = next(N);
++cnt;
}
cout << cnt << endl;
}
return 0;
} | replace | 28 | 29 | 28 | 30 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str) \
for (int i = 0; i < (vec).size(); ++i) \
cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec) \
for (int i = 0; i < (vec).size(); ++i) \
cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;
int main() {
int n, cnt;
int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));
while (cin >> n, n) {
if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
cout << "NA" << endl;
continue;
}
cnt = 0;
while (n != 6174) {
stringstream ss;
ss << n;
string str = ss.str();
sort(str.begin(), str.end());
int s = atoi(str.c_str());
reverse(str.begin(), str.end());
int l = atoi(str.c_str());
n = l - s;
++cnt;
}
cout << cnt << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define FOR(v, a, b) for (int v = (a); v < (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define UNWORDS(vec, str) \
for (int i = 0; i < (vec).size(); ++i) \
cout << (i ? str : "") << (vec)[i]
#define UNLINES(vec) \
for (int i = 0; i < (vec).size(); ++i) \
cout << (vec)[i] << endl
#define RS resize
#define CLR clear
#define PB push_back
using namespace std;
int main() {
int n, cnt;
int rep[] = {1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999};
vector<int> repdig(rep, rep + (sizeof(rep) / sizeof(int)));
while (cin >> n, n) {
if (find(repdig.begin(), repdig.end(), n) != repdig.end()) {
cout << "NA" << endl;
continue;
}
cnt = 0;
while (n != 6174) {
stringstream ss;
ss << setfill('0') << setw(4) << n;
string str = ss.str();
sort(str.begin(), str.end());
int s = atoi(str.c_str());
reverse(str.begin(), str.end());
int l = atoi(str.c_str());
n = l - s;
++cnt;
}
cout << cnt << endl;
}
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (cin >> str) {
bool f = false;
for (auto i : str)
if (i != '0')
f = true;
if (!f)
break;
f = false;
for (auto i : str)
if (i != str[0])
f = true;
if (!f) {
cout << "NA" << endl;
continue;
}
int count = 0;
int now;
stringstream org;
org << str;
org >> now;
while (1) {
if (now == 6174) {
cout << count << endl;
break;
}
count++;
sort(str.begin(), str.end());
auto rstr = str;
reverse(rstr.begin(), rstr.end());
stringstream ss;
ss << rstr << " " << str;
int A, B;
ss >> A >> B;
now = A - B;
stringstream next;
next << now;
next >> str;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (cin >> str) {
bool f = false;
for (auto i : str)
if (i != '0')
f = true;
if (!f)
break;
f = false;
for (auto i : str)
if (i != str[0])
f = true;
if (!f) {
cout << "NA" << endl;
continue;
}
int count = 0;
int now;
stringstream org;
org << str;
org >> now;
while (1) {
if (now == 6174) {
cout << count << endl;
break;
}
count++;
sort(str.begin(), str.end());
auto rstr = str;
reverse(rstr.begin(), rstr.end());
stringstream ss;
ss << rstr << " " << str;
int A, B;
ss >> A >> B;
now = A - B;
stringstream next;
next << now;
next >> str;
while (str.size() < 4)
str = "0" + str;
}
}
return 0;
} | insert | 47 | 47 | 47 | 49 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
bool flag = true;
rep(i, s.size()) if (s[0] != s[i]) flag = false;
if (flag) {
cout << "NA" << endl;
} else {
int cnt = 0;
while (true) {
if (s == "6174") {
cout << cnt << endl;
break;
}
string temp = s, temp2 = s;
sort(temp.begin(), temp.end());
sort(temp2.begin(), temp2.end(), greater<char>());
int t = atoi(temp.c_str());
int t2 = atoi(temp2.c_str());
stringstream ss;
ss << t2 - t;
s = ss.str();
cnt++;
}
}
}
} | #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
bool flag = true;
rep(i, s.size()) if (s[0] != s[i]) flag = false;
if (flag) {
cout << "NA" << endl;
} else {
int cnt = 0;
while (true) {
if (s == "6174") {
cout << cnt << endl;
break;
}
string temp = s, temp2 = s;
sort(temp.begin(), temp.end());
sort(temp2.begin(), temp2.end(), greater<char>());
int t = atoi(temp.c_str());
int t2 = atoi(temp2.c_str());
stringstream ss;
ss << t2 - t;
s = "";
REP(i, ss.str().size(), 4) s += "0";
s += ss.str();
cnt++;
}
}
}
} | replace | 41 | 42 | 41 | 45 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair
const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
typedef unsigned long long ull;
typedef std::pair<int, int> P;
int main() {
std::string s;
while (std::cin >> s, s != "0000") {
if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
std::cout << "NA" << std::endl;
} else {
int t = 0;
while (s != "6174") {
int L, S;
std::sort(s.begin(), s.end(), std::greater<char>());
L = std::stoi(s);
std::sort(s.begin(), s.end());
S = std::stoi(s);
s = std::to_string(L - S);
t++;
}
std::cout << t << std::endl;
}
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair
const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
typedef unsigned long long ull;
typedef std::pair<int, int> P;
int main() {
std::string s;
while (std::cin >> s, s != "0000") {
if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
std::cout << "NA" << std::endl;
} else {
int t = 0;
while (s != "6174") {
int L, S;
std::sort(s.begin(), s.end(), std::greater<char>());
L = std::stoi(s);
std::sort(s.begin(), s.end());
S = std::stoi(s);
s = std::to_string(L - S);
while (s.length() < 4) {
s += "0";
}
t++;
}
std::cout << t << std::endl;
}
}
} | insert | 37 | 37 | 37 | 40 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
int main() {
string S;
while (true) {
cin >> S;
if (S == "0000") {
break;
}
if (S[0] == S[1] == S[2] == S[3]) {
cout << "NA" << endl;
} else {
int count_ = 0;
while (S != "6174") {
string S1 = S, S2 = S;
sort(S1.begin(), S1.end(), greater<char>());
sort(S2.begin(), S2.end());
S = to_string(stoi(S1) - stoi(S2));
while (S.size() < 4) {
S = '0' + S;
}
count_++;
}
cout << count_ << endl;
}
}
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
int main() {
string S;
while (true) {
cin >> S;
if (S == "0000") {
break;
}
if (S[0] == S[1] && S[1] == S[2] && S[2] == S[3]) {
cout << "NA" << endl;
} else {
int count_ = 0;
while (S != "6174") {
string S1 = S, S2 = S;
sort(S1.begin(), S1.end(), greater<char>());
sort(S2.begin(), S2.end());
S = to_string(stoi(S1) - stoi(S2));
while (S.size() < 4) {
S = '0' + S;
}
count_++;
}
cout << count_ << endl;
}
}
return 0;
} | replace | 17 | 18 | 17 | 18 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
while (true) {
cin >> S;
if (S == "0000") {
break;
}
if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
cout << "NA" << endl;
} else {
b = stoi(S);
cnt = 0;
while (b != 6174) {
cnt++;
for (int i = 0; i < 4; i++) {
a[i] = (b / (int)pow(10, i)) % 10;
}
sort(a, a + 4);
b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
(a[0] + a[3]);
}
cout << cnt << endl;
}
}
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;
int a[4], b, cnt;
int main() {
while (true) {
cin >> S;
if (S == "0000") {
break;
}
if (S[0] == S[1] && S[2] == S[3] && S[1] == S[2]) {
cout << "NA" << endl;
} else {
b = stoi(S);
cnt = 0;
while (b != 6174) {
cnt++;
for (int i = 0; i < 4; i++) {
a[i] = (b / (int)pow(10, i)) % 10;
}
sort(a, a + 4);
b = (a[3] - a[0]) * 1000 + (a[2] - a[1]) * 100 + (a[1] - a[2]) * 10 +
(a[0] - a[3]);
}
cout << cnt << endl;
}
}
} | replace | 25 | 26 | 25 | 26 | TLE | |
p00254 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define DBG 1
#define dump(o) \
if (DBG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DBG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" ||
s == "6666" || s == "7777" || s == "8888" || s == "9999")
return true;
else
return false;
}
signed main() {
string s;
while (cin >> s, s != "0000") {
int ans(0);
while (s != "6174") {
if (NA(s)) {
cout << "NA" << endl;
goto here;
}
string big(s), small(s);
sort(all(small));
sort(all(big), greater<char>());
s = to_string(stoi(big) - stoi(small));
ans++;
}
cout << ans << endl;
here:;
}
return 0;
} | #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define DBG 1
#define dump(o) \
if (DBG) { \
cerr << #o << " " << o << endl; \
}
#define dumpc(o) \
if (DBG) { \
cerr << #o; \
for (auto &e : (o)) \
cerr << " " << e; \
cerr << endl; \
}
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)
#define all(c) c.begin(), c.end()
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" ||
s == "6666" || s == "7777" || s == "8888" || s == "9999")
return true;
else
return false;
}
signed main() {
string s;
while (cin >> s, s != "0000") {
int ans(0);
while (s != "6174") {
if (NA(s)) {
cout << "NA" << endl;
goto here;
}
string big(s), small(s);
sort(all(small));
sort(all(big), greater<char>());
s = to_string(stoi(big) - stoi(small));
while (s.size() < 4)
s = "0" + s;
ans++;
}
cout << ans << endl;
here:;
}
return 0;
} | insert | 44 | 44 | 44 | 46 | TLE | |
p00254 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
string toString(int n) {
string ret = "";
while (n) {
ret += (n % 10) + '0';
n /= 10;
}
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
cout << "NA" << endl;
continue;
}
int cnt = 0;
while (s != "6174") {
string L = s, S = s;
sort(L.rbegin(), L.rend());
sort(S.begin(), S.end());
int nex = atoi(L.c_str()) - atoi(S.c_str());
s = toString(nex);
cnt++;
}
cout << cnt << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
string toString(int n) {
string ret = "";
while (n) {
ret += (n % 10) + '0';
n /= 10;
}
while (ret.size() < 4)
ret += "0";
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
string s;
while (cin >> s) {
if (s == "0000")
break;
if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
cout << "NA" << endl;
continue;
}
int cnt = 0;
while (s != "6174") {
string L = s, S = s;
sort(L.rbegin(), L.rend());
sort(S.begin(), S.end());
int nex = atoi(L.c_str()) - atoi(S.c_str());
s = toString(nex);
cnt++;
}
cout << cnt << endl;
}
return 0;
} | insert | 9 | 9 | 9 | 11 | TLE |
Subsets and Splits