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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00654 | C++ | Time Limit Exceeded | // 25
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
for (int n; cin >> n, n;) {
int b[90000];
for (int i = 0; i < n * (n + 1) / 2; i++) {
cin >> b[i];
}
int a = sqrt(double(b[0]) * b[1] / b[n]) + .5;
cout << a << endl;
int c[250];
for (int i = 0; i < n; i++) {
c[i] = b[i] / a;
}
sort(c, c + n);
for (int i = 0; i < n; i++) {
cout << c[i] << ((i == n - 1) ? '\n' : ' ');
}
}
return 0;
} | // 25
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
for (int n; cin >> n, n;) {
long long b[90000];
for (int i = 0; i < n * (n + 1) / 2; i++) {
cin >> b[i];
}
int a = sqrt(double(b[0]) * b[1] / b[n]) + .5;
cout << a << endl;
int c[250];
for (int i = 0; i < n; i++) {
c[i] = b[i] / a;
}
sort(c, c + n);
for (int i = 0; i < n; i++) {
cout << c[i] << ((i == n - 1) ? '\n' : ' ');
}
}
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p00654 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long long gcd(unsigned long long int x, unsigned long long int y) {
return y == 0 ? x : gcd(y, x % y);
}
void solve() {
int n;
while (cin >> n, n) {
int size = (n + 1) * n / 2;
vector<unsigned long long int> Vecodd;
vector<unsigned long long int> Veceven;
for (int i = 0; i < size; ++i) {
int temp;
cin >> temp;
if (temp % 2 == 0) {
Veceven.push_back(temp);
} else {
Vecodd.push_back(temp);
}
}
sort(Veceven.begin(), Veceven.end());
sort(Vecodd.begin(), Vecodd.end());
unsigned long long int minodd = Vecodd[0];
unsigned long long int mineven[2] = {Veceven[0], Veceven[1]};
unsigned long long int g = gcd(mineven[0], minodd);
unsigned long long int Even = (unsigned long long int)sqrt(
(double)mineven[0] / g * (mineven[1] / (minodd / g)));
cout << Even << endl;
size = Veceven.size();
for (int i = 0; i < size - 1; ++i) {
cout << Veceven[i] / Even << " ";
}
cout << Veceven[size - 1] / Even << endl;
}
}
int main() {
solve();
return (0);
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long long gcd(unsigned long long int x, unsigned long long int y) {
return y == 0 ? x : gcd(y, x % y);
}
void solve() {
int n;
while (cin >> n, n) {
int size = (n + 1) * n / 2;
vector<unsigned long long int> Vecodd;
vector<unsigned long long int> Veceven;
for (int i = 0; i < size; ++i) {
unsigned long long int temp;
cin >> temp;
if (temp % 2 == 0) {
Veceven.push_back(temp);
} else {
Vecodd.push_back(temp);
}
}
sort(Veceven.begin(), Veceven.end());
sort(Vecodd.begin(), Vecodd.end());
unsigned long long int minodd = Vecodd[0];
unsigned long long int mineven[2] = {Veceven[0], Veceven[1]};
unsigned long long int g = gcd(mineven[0], minodd);
unsigned long long int Even = (unsigned long long int)sqrt(
(double)mineven[0] / g * (mineven[1] / (minodd / g)));
cout << Even << endl;
size = Veceven.size();
for (int i = 0; i < size - 1; ++i) {
cout << Veceven[i] / Even << " ";
}
cout << Veceven[size - 1] / Even << endl;
}
}
int main() {
solve();
return (0);
} | replace | 18 | 19 | 18 | 19 | 0 | |
p00654 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long fn(long long p, long long q) {
long long s;
while (q) {
p %= q;
s = p;
p = q;
q = s;
}
return p;
}
int main() {
int i, j, k;
int n;
while (cin >> n && n) {
long long a[31400], b[31400];
j = 0;
k = 0;
for (i = 0; i < n * (n + 1) / 2; i++) {
long long p;
cin >> p;
if (p % 2) {
a[j] = p;
j++;
} else {
b[k] = p;
k++;
}
}
sort(a, a + j);
sort(b, b + k);
long long c = b[0];
for (i = 1; i < k; i++)
c = fn(c, b[i]);
for (; a[0] != (b[0] / c) * (b[1] / c); c /= 2)
;
cout << c << endl;
for (i = 0; i < k - 1; i++)
cout << b[i] / c << " ";
cout << b[i] / c << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long fn(long long p, long long q) {
long long s;
while (q) {
p %= q;
s = p;
p = q;
q = s;
}
return p;
}
int main() {
int i, j, k;
int n;
while (cin >> n && n) {
long long a[31400], b[31400];
j = 0;
k = 0;
for (i = 0; i < n * (n + 1) / 2; i++) {
long long p;
cin >> p;
if (p % 2) {
a[j] = p;
j++;
} else {
b[k] = p;
k++;
}
}
sort(a, a + j);
sort(b, b + k);
long long c = fn(a[0], b[0]);
c = (long long)sqrt(b[0] / c * (b[1] / (a[0] / c)));
cout << c << endl;
for (i = 0; i < k - 1; i++)
cout << b[i] / c << " ";
cout << b[i] / c << endl;
}
return 0;
} | replace | 37 | 42 | 37 | 39 | 0 | |
p00654 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
long long a[100000];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int cmp(const long long &a, const long long &b) {
if (a % 2 == 0 && b % 2 == 1)
return 1;
else if (a % 2 == 1 && b % 2 == 0)
return 0;
else
return a < b;
}
int main() {
int N, n;
while (1) {
long long x, y, z, tmp, r1, r2, d2, m, p, q;
scanf("%d", &n);
if (n == 0)
break;
N = (n + 1) * n / 2;
for (int i = 1; i <= N; i++)
scanf("%lld", a + i);
sort(a + 1, a + N + 1, cmp);
x = a[n + 1];
y = a[1];
z = a[2];
tmp = gcd(y, z);
r1 = y / tmp;
r2 = z / tmp;
d2 = x / r1 / r2;
p = 1;
q = (1 << 31);
while (p <= q) {
m = (p + q) >> 1;
if (m * m == d2)
break;
else if (m * m < d2)
p = m + 1;
else
q = m - 1;
}
tmp /= m;
printf("%lld\n", tmp);
for (int i = 1; i <= n; i++) {
if (i < n)
printf("%lld ", a[i] / tmp);
else
printf("%lld\n", a[i] / tmp);
}
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
long long a[100000];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int cmp(const long long &a, const long long &b) {
if (a % 2 == 0 && b % 2 == 1)
return 1;
else if (a % 2 == 1 && b % 2 == 0)
return 0;
else
return a < b;
}
int main() {
int N, n;
while (1) {
long long x, y, z, tmp, r1, r2, d2, m, p, q;
scanf("%d", &n);
if (n == 0)
break;
N = (n + 1) * n / 2;
for (int i = 1; i <= N; i++)
scanf("%lld", a + i);
sort(a + 1, a + N + 1, cmp);
x = a[n + 1];
y = a[1];
z = a[2];
tmp = gcd(y, z);
r1 = y / tmp;
r2 = z / tmp;
d2 = x / r1 / r2;
p = 1;
q = (1 << 31) - 1;
while (p <= q) {
m = (p + q) >> 1;
if (m * m == d2)
break;
else if (m * m < d2)
p = m + 1;
else
q = m - 1;
}
tmp /= m;
printf("%lld\n", tmp);
for (int i = 1; i <= n; i++) {
if (i < n)
printf("%lld ", a[i] / tmp);
else
printf("%lld\n", a[i] / tmp);
}
}
return 0;
} | replace | 37 | 38 | 37 | 38 | -8 | |
p00657 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int r, c;
int main() {
while (true) {
cin >> r >> c;
if (r % 2 == 1 && c % 2 == 1) {
cout << "no" << endl;
} else {
cout << "yes" << endl;
}
}
return 0;
} | #include <iostream>
using namespace std;
int r, c;
int main() {
while (true) {
cin >> r >> c;
if (r == 0 && c == 0) {
break;
}
if (r % 2 == 1 && c % 2 == 1) {
cout << "no" << endl;
} else {
cout << "yes" << endl;
}
}
return 0;
} | insert | 6 | 6 | 6 | 9 | TLE | |
p00660 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
vector<vector<vector<int>>> dices{
{{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
};
int check(const vector<vector<int>> &field) {
for (int i = 0; i < 9; ++i) {
if (field == dices[i])
return i + 1;
}
return 0;
}
vector<vector<int>> rotate_l(const vector<vector<int>> &field) {
vector<vector<int>> nfield(field);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
nfield[i][j] = field[j][4 - i];
}
}
return nfield;
}
vector<vector<int>> rev(const vector<vector<int>> &field, const bool isleft) {
vector<vector<int>> nfield(field);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (isleft)
nfield[i][j] = field[i][4 - j];
else
nfield[i][j] = field[4 - i][j];
}
}
return nfield;
}
int getnum(vector<vector<int>> field, const int num) {
if (num == 0)
field = rev(field, true);
else if (num == 1) {
field = rev(field, true);
field = rotate_l(field);
} else if (num == 2) {
field = rev(field, true);
} else if (num == 3) {
field = rev(field, true);
for (int i = 0; i < 3; ++i)
field = rotate_l(field);
} else if (num == 4) {
field = rev(field, true);
} else if (num == 5) {
field = rev(field, false);
field = rev(field, true);
}
if (check(field))
return check(field);
assert(false);
return -1;
}
bool getinput(vector<vector<int>> &adices) {
vector<string> sts;
string st;
if (cin >> st) {
sts.emplace_back(st);
for (int i = 1; i < 21; ++i) {
string st;
cin >> st;
sts.emplace_back(st);
}
vector<vector<pair<int, int>>> lu(2, vector<pair<int, int>>(6));
lu[0][0] = make_pair(0, 7);
lu[0][1] = make_pair(7, 0);
lu[0][2] = make_pair(7, 7);
lu[0][3] = make_pair(7, 14);
lu[0][4] = make_pair(7, 21);
lu[0][5] = make_pair(14, 7);
for (int i = 0; i < 6; ++i) {
lu[1][i] = make_pair(lu[0][i].first, lu[0][i].second + 29);
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 6; ++j) {
vector<vector<int>> field(5, vector<int>(5));
for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) {
field[y][x] =
sts[lu[i][j].first + 1 + y][lu[i][j].second + 1 + x] != '.';
}
}
adices[i][j] = getnum(field, j);
}
}
} else {
return false;
}
}
int main() {
vector<vector<int>> dices(2, vector<int>(6));
while (getinput(dices)) {
int high = 0;
int low = 0;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (dices[0][i] > dices[1][j])
high++;
else if (dices[0][i] < dices[1][j])
low++;
}
}
if (high >= low)
cout << "HIGH" << endl;
else
cout << "LOW" << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"
vector<vector<vector<int>>> dices{
{{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0}},
{{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 1, 0, 0}},
};
int check(const vector<vector<int>> &field) {
for (int i = 0; i < 9; ++i) {
if (field == dices[i])
return i + 1;
}
return 0;
}
vector<vector<int>> rotate_l(const vector<vector<int>> &field) {
vector<vector<int>> nfield(field);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
nfield[i][j] = field[j][4 - i];
}
}
return nfield;
}
vector<vector<int>> rev(const vector<vector<int>> &field, const bool isleft) {
vector<vector<int>> nfield(field);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (isleft)
nfield[i][j] = field[i][4 - j];
else
nfield[i][j] = field[4 - i][j];
}
}
return nfield;
}
int getnum(vector<vector<int>> field, const int num) {
if (num == 0)
field = rev(field, true);
else if (num == 1) {
field = rev(field, true);
field = rotate_l(field);
} else if (num == 2) {
field = rev(field, true);
} else if (num == 3) {
field = rev(field, true);
for (int i = 0; i < 3; ++i)
field = rotate_l(field);
} else if (num == 4) {
field = rev(field, true);
} else if (num == 5) {
field = rev(field, false);
field = rev(field, true);
}
if (check(field))
return check(field);
assert(false);
return -1;
}
bool getinput(vector<vector<int>> &adices) {
vector<string> sts;
string st;
if (cin >> st) {
if (st == "0")
return false;
sts.emplace_back(st);
for (int i = 1; i < 21; ++i) {
string st;
cin >> st;
sts.emplace_back(st);
}
vector<vector<pair<int, int>>> lu(2, vector<pair<int, int>>(6));
lu[0][0] = make_pair(0, 7);
lu[0][1] = make_pair(7, 0);
lu[0][2] = make_pair(7, 7);
lu[0][3] = make_pair(7, 14);
lu[0][4] = make_pair(7, 21);
lu[0][5] = make_pair(14, 7);
for (int i = 0; i < 6; ++i) {
lu[1][i] = make_pair(lu[0][i].first, lu[0][i].second + 29);
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 6; ++j) {
vector<vector<int>> field(5, vector<int>(5));
for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) {
field[y][x] =
sts[lu[i][j].first + 1 + y][lu[i][j].second + 1 + x] != '.';
}
}
adices[i][j] = getnum(field, j);
}
}
} else {
return false;
}
}
int main() {
vector<vector<int>> dices(2, vector<int>(6));
while (getinput(dices)) {
int high = 0;
int low = 0;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (dices[0][i] > dices[1][j])
high++;
else if (dices[0][i] < dices[1][j])
low++;
}
}
if (high >= low)
cout << "HIGH" << endl;
else
cout << "LOW" << endl;
}
return 0;
} | insert | 119 | 119 | 119 | 121 | -6 | 104b06d8-1ed8-4e5f-9b06-cc097e0c4349.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00660/C++/s127471588.cpp:127: int getnum(std::vector<std::vector<int> >, int): Assertion `false' failed.
|
p00662 | 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 a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
int x = a + d, y = b + e, z = c + f;
if (!x && !y && !z)
break;
int ans = 0;
int amin = min(min(x, y), z);
for (int i = 0; i <= amin; ++i) {
int sum = i;
sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
ans = max(ans, sum);
}
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
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 a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
int x = a + d, y = b + e, z = c + f;
if (!x && !y && !z)
break;
int ans = 0;
int amin = min(min(x, y), z);
for (int i = 0; i <= min(2, amin); ++i) {
int sum = i;
sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
ans = max(ans, sum);
}
cout << ans << endl;
}
return 0;
} | replace | 21 | 22 | 21 | 22 | TLE | |
p00662 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
int a, b, c, d, e, f;
while (cin >> a >> b >> c >> d >> e >> f, a || b || c || d || e || f) {
a += d;
b += e;
c += f;
int Max = 0;
for (int i = 0; i <= min(a, min(b, c)); i++)
Max = max(Max, (a - i) / 3 + (b - i) / 3 + (c - i) / 3 + i);
cout << Max << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
int a, b, c, d, e, f;
while (cin >> a >> b >> c >> d >> e >> f, a || b || c || d || e || f) {
a += d;
b += e;
c += f;
int Max = 0;
for (int i = 0; i <= min(2, min(a, min(b, c))); i++)
Max = max(Max, (a - i) / 3 + (b - i) / 3 + (c - i) / 3 + i);
cout << Max << endl;
}
} | replace | 11 | 12 | 11 | 12 | TLE | |
p00662 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#define pb push_back
#define mp nake_pair
typedef long long ll;
typedef vector<int> vi;
int calc(int m, const vi &d) {
int ret = m;
rep(i, 3) if (m > d[i]) return -1e9;
rep(i, 3) ret += (d[i] - m) / 3;
return ret;
}
int main() {
vi v(6);
while (1) {
rep(i, 6) cin >> v[i];
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 &&
v[5] == 0)
break;
vi d(3);
rep(i, 6) d[i % 3] += v[i];
double l = 0, r = 1e9;
rep(i, 10000) {
double m1 = l + (r - l) / 3.0, m2 = l + (r - l) * 2.0 / 3.0;
int f1 = calc(m1, d), f2 = calc(m2, d);
if (f1 < f2)
l = m1;
else
r = m2;
}
cout << calc(r, d) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#define pb push_back
#define mp nake_pair
typedef long long ll;
typedef vector<int> vi;
int calc(int m, const vi &d) {
int ret = m;
rep(i, 3) if (m > d[i]) return -1e9;
rep(i, 3) ret += (d[i] - m) / 3;
return ret;
}
int main() {
vi v(6);
while (1) {
rep(i, 6) cin >> v[i];
if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 &&
v[5] == 0)
break;
vi d(3);
rep(i, 6) d[i % 3] += v[i];
double l = 0, r = 1e9;
rep(i, 1000) {
double m1 = l + (r - l) / 3.0, m2 = l + (r - l) * 2.0 / 3.0;
int f1 = calc(m1, d), f2 = calc(m2, d);
if (f1 < f2)
l = m1;
else
r = m2;
}
cout << calc(r, d) << endl;
}
} | replace | 34 | 35 | 34 | 35 | TLE | |
p00662 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int A, B, C;
int main() {
int a, b, c, d, e, f;
while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
A = a + d;
B = b + e;
C = c + f;
// cout<< A << " "<< B << " " << C << endl;
int n = min(A, min(B, C));
int res = 0;
res = max(res, A / 3 + B / 3 + C / 3);
for (int i = n; i > min(-1, n - 4); i--) {
res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3);
}
cout << res << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int A, B, C;
int main() {
int a, b, c, d, e, f;
while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
A = a + d;
B = b + e;
C = c + f;
// cout<< A << " "<< B << " " << C << endl;
int n = min(A, min(B, C));
int res = 0;
res = max(res, A / 3 + B / 3 + C / 3);
for (int i = n; i > max(-1, n - 4); i--) {
res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3);
}
cout << res << endl;
}
} | replace | 16 | 17 | 16 | 17 | TLE | |
p00662 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
using ll = long long;
int main() {
while (1) {
int n[6];
scanf("%d%d%d%d%d%d", n, n + 1, n + 2, n + 3, n + 4, n + 5);
if (n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) {
break;
}
int a = n[0] + n[3];
int b = n[1] + n[4];
int c = n[2] + n[5];
int d = min(a, min(b, c));
int ans = 0;
rep(count4, d + 1) {
ans = max(ans, count4 + (a - count4) / 3 + (b - count4) / 3 +
(c - count4) / 3);
}
printf("%d\n", ans);
}
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
using ll = long long;
int main() {
while (1) {
int n[6];
scanf("%d%d%d%d%d%d", n, n + 1, n + 2, n + 3, n + 4, n + 5);
if (n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) {
break;
}
int a = n[0] + n[3];
int b = n[1] + n[4];
int c = n[2] + n[5];
int d = min(a, min(b, c));
int ans = 0;
rep(count4, min(d + 1, 3)) {
ans = max(ans, count4 + (a - count4) / 3 + (b - count4) / 3 +
(c - count4) / 3);
}
printf("%d\n", ans);
}
} | replace | 30 | 31 | 30 | 31 | TLE | |
p00662 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#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>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
static const double EPS = 1e-8;
int main() {
int math, greedy, geometry, DP, graph, other;
while (~scanf("%d %d %d %d %d %d", &math, &greedy, &geometry, &DP, &graph,
&other)) {
if (math + greedy + geometry + DP + graph + other == 0)
break;
int A = math + DP;
int B = greedy + graph;
int C = geometry + other;
int res = 0;
int max_game = min(min(A, B), C);
for (int balance_game = 0; balance_game <= max_game; balance_game++) {
int tmpA = A;
int tmpB = B;
int tmpC = C;
tmpA -= balance_game;
tmpB -= balance_game;
tmpC -= balance_game;
int math_game = tmpA / 3;
int algorithm_game = tmpB / 3;
int implement_game = tmpC / 3;
res =
max(balance_game + math_game + algorithm_game + implement_game, res);
}
printf("%d\n", res);
}
} | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#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>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
static const double EPS = 1e-8;
int main() {
int math, greedy, geometry, DP, graph, other;
while (~scanf("%d %d %d %d %d %d", &math, &greedy, &geometry, &DP, &graph,
&other)) {
if (math + greedy + geometry + DP + graph + other == 0)
break;
int A = math + DP;
int B = greedy + graph;
int C = geometry + other;
int res = 0;
int max_game = min(min(A, B), C);
for (int balance_game = 0; balance_game <= min(2, max_game);
balance_game++) {
int tmpA = A;
int tmpB = B;
int tmpC = C;
tmpA -= balance_game;
tmpB -= balance_game;
tmpC -= balance_game;
int math_game = tmpA / 3;
int algorithm_game = tmpB / 3;
int implement_game = tmpC / 3;
res =
max(balance_game + math_game + algorithm_game + implement_game, res);
}
printf("%d\n", res);
}
} | replace | 40 | 41 | 40 | 42 | TLE | |
p00662 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
int a, b, c, d, e, f, sum1, sum2, sum3, sum, p;
int main() {
while (true) {
sum = 0;
cin >> a >> b >> c >> d >> e >> f;
if (a + b + c + d + e + f == 0) {
break;
}
sum1 = a + d;
sum2 = b + e;
sum3 = c + f;
p = 0;
p = min(sum1, sum2);
p = min(p, sum3);
for (int i = 0; i <= p; i++) {
sum = max(sum, i + (sum1 - i) / 3 + (sum2 - i) / 3 + (sum3 - i) / 3);
}
cout << sum << endl;
}
} | #include <algorithm>
#include <iostream>
using namespace std;
int a, b, c, d, e, f, sum1, sum2, sum3, sum, p;
int main() {
while (true) {
sum = 0;
cin >> a >> b >> c >> d >> e >> f;
if (a + b + c + d + e + f == 0) {
break;
}
sum1 = a + d;
sum2 = b + e;
sum3 = c + f;
p = 0;
p = min(sum1, sum2);
p = min(p, sum3);
for (int i = max(0, p - 10); i <= p; i++) {
sum = max(sum, i + (sum1 - i) / 3 + (sum2 - i) / 3 + (sum3 - i) / 3);
}
cout << sum << endl;
}
} | replace | 17 | 18 | 17 | 18 | TLE | |
p00663 | C++ | Runtime Error | // Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:
/**
* 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
*
* オーダーは O(|S|)。
*/
#include <cassert>
#include <iostream>
#include <map>
#include <string>
using namespace std;
pair<char, bool> literal(const string &str, int &pos) {
pair<char, bool> res('\0', false);
if (str[pos] == '~') {
res.second = true;
++pos;
}
res.first = str[pos++];
return res;
}
bool parse(const string &str, int pos) {
if (pos >= str.size())
return false;
assert(str[pos] == '(');
++pos;
map<char, bool> vars;
bool ok = true;
for (int i = 0; i < 3; ++i) {
pair<char, bool> var = literal(str, pos);
if (vars.count(var.first) && vars[var.first] != var.second) {
ok = false;
}
vars[var.first] = var.second;
if (i != 2) {
assert(str[pos] == '&');
++pos;
}
}
assert(str[pos] == ')');
++pos;
if (ok)
return true;
else
return parse(str, pos);
}
bool solve() {
string str;
if (!(cin >> str))
return false;
if (str == "#")
return false;
cout << (parse(str, 0) ? "yes" : "no") << endl;
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
while (solve())
;
return 0;
} | // Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:
/**
* 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
*
* オーダーは O(|S|)。
*/
#include <cassert>
#include <iostream>
#include <map>
#include <string>
using namespace std;
pair<char, bool> literal(const string &str, int &pos) {
pair<char, bool> res('\0', false);
if (str[pos] == '~') {
res.second = true;
++pos;
}
res.first = str[pos++];
return res;
}
bool parse(const string &str, int pos) {
if (pos >= str.size())
return false;
if (str[pos] == '|')
++pos;
assert(str[pos] == '(');
++pos;
map<char, bool> vars;
bool ok = true;
for (int i = 0; i < 3; ++i) {
pair<char, bool> var = literal(str, pos);
if (vars.count(var.first) && vars[var.first] != var.second) {
ok = false;
}
vars[var.first] = var.second;
if (i != 2) {
assert(str[pos] == '&');
++pos;
}
}
assert(str[pos] == ')');
++pos;
if (ok)
return true;
else
return parse(str, pos);
}
bool solve() {
string str;
if (!(cin >> str))
return false;
if (str == "#")
return false;
cout << (parse(str, 0) ? "yes" : "no") << endl;
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
while (solve())
;
return 0;
} | insert | 30 | 30 | 30 | 32 | 0 | |
p00664 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
int n, m, q;
while (cin >> n >> m >> q, n + m + q) {
vi h(n), w(m);
int coh = 0, cow = 0;
vvi in(q, vi(3));
rep(i, q) rep(j, 3) cin >> in[n - 1 - i][j];
int out = 0;
rep(i, q) {
if (in[i][0] && w[in[i][1]] == 0) {
if (in[i][2])
out += n - coh;
cow++;
w[in[i][1]] = true;
} else if (in[i][0] == 0 && h[in[i][1]] == 0) {
if (in[i][2])
out += m - cow;
coh++;
h[in[i][1]] = true;
}
}
cout << out << endl;
}
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
int n, m, q;
while (cin >> n >> m >> q, n + m + q) {
vi h(n), w(m);
int coh = 0, cow = 0;
vvi in(q, vi(3));
rep(i, q) rep(j, 3) cin >> in[q - 1 - i][j];
int out = 0;
rep(i, q) {
if (in[i][0] && w[in[i][1]] == 0) {
if (in[i][2])
out += n - coh;
cow++;
w[in[i][1]] = true;
} else if (in[i][0] == 0 && h[in[i][1]] == 0) {
if (in[i][2])
out += m - cow;
coh++;
h[in[i][1]] = true;
}
}
cout << out << endl;
}
} | replace | 41 | 42 | 41 | 42 | 0 | |
p00664 | C++ | Runtime Error | #include <bits/stdc++.h>
#define N 50001
using namespace std;
typedef long long ll;
int main() {
while (1) {
ll r, c, q;
cin >> r >> c >> q;
if (!r && !c && !q)
return 0;
ll A[N], B[N], O[N];
for (int i = 0; i < q; i++)
cin >> A[i] >> B[i] >> O[i];
ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
for (int i = q - 1; i >= 0; i--) {
ll f = A[i], RC = B[i];
if (used[f][RC]++)
continue;
cnt[f]++;
assert(rc[f] - cnt[!f] >= 0);
ans += (rc[f] - cnt[!f]) * O[i];
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define N 50001
using namespace std;
typedef long long ll;
int main() {
while (1) {
ll r, c, q;
cin >> r >> c >> q;
if (!r && !c && !q)
return 0;
ll A[N], B[N], O[N];
for (int i = 0; i < q; i++)
cin >> A[i] >> B[i] >> O[i];
ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
for (int i = q - 1; i >= 0; i--) {
ll f = A[i], RC = B[i];
if (used[f][RC]++)
continue;
cnt[f]++;
ans += (rc[!f] - cnt[!f]) * O[i];
}
cout << ans << endl;
}
return 0;
} | replace | 22 | 24 | 22 | 23 | 0 | |
p00666 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const double EPS = 1.0e-10;
const int MOD = 100000007;
void combination(int n, vector<vector<int>> &comb) {
comb.assign(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i <= n; ++i) {
comb[i][0] = 1;
for (int j = 1; j <= i; ++j) {
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
comb[i][j] %= MOD;
}
}
}
vector<double> destroy(1001, 0.0); // 破壊されるまでの時間の期待値
double solve(vector<double> &p, vector<double> &q, int &pattern) {
int n = p.size();
double t = 0.0;
for (int i = 0; i < n; ++i)
t += destroy[q[i] * 1000 + EPS];
double ret = DBL_MAX;
pattern = 0;
for (int i = 0; i < n; ++i) {
double t2 = t - destroy[q[i] * 1000 + EPS] + destroy[p[i] * 1000 + EPS];
if (t2 < ret - EPS) {
ret = t2;
pattern = 1;
} else if (t2 < ret + EPS) {
++pattern;
}
}
return ret;
}
int main() {
for (int i = 1; i <= 1000; ++i) {
double p = i / 1000.0;
double a = 1.0;
for (int j = 1; j <= 50000; ++j) {
destroy[i] += a * p * j;
a *= 1.0 - p;
}
}
vector<vector<int>> comb;
combination(100, comb);
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<double> p(n), q(n);
vector<int> id(n);
for (int i = 0; i < n; ++i)
cin >> p[i] >> id[i] >> q[i];
vector<bool> check(n, false);
double ret = 0.0;
long long pattern = 1;
int tmp = n;
for (int i = 0; i < n; ++i) {
if (check[i])
continue;
int j = i;
vector<double> p2, q2;
while (!check[j]) {
p2.push_back(p[j]);
q2.push_back(q[j]);
check[j] = true;
j = id[j];
}
int pattern2;
ret += solve(p2, q2, pattern2);
pattern *= pattern2;
pattern %= MOD;
pattern *= comb[tmp][p2.size()];
pattern %= MOD;
tmp -= p2.size();
}
printf("%.10f %d\n", ret, (int)pattern);
}
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const double EPS = 1.0e-10;
const int MOD = 100000007;
void combination(int n, vector<vector<int>> &comb) {
comb.assign(n + 1, vector<int>(n + 1, 0));
for (int i = 0; i <= n; ++i) {
comb[i][0] = 1;
for (int j = 1; j <= i; ++j) {
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
comb[i][j] %= MOD;
}
}
}
vector<double> destroy(1001, 0.0); // 破壊されるまでの時間の期待値
double solve(vector<double> &p, vector<double> &q, int &pattern) {
int n = p.size();
double t = 0.0;
for (int i = 0; i < n; ++i)
t += destroy[q[i] * 1000 + EPS];
double ret = DBL_MAX;
pattern = 0;
for (int i = 0; i < n; ++i) {
double t2 = t - destroy[q[i] * 1000 + EPS] + destroy[p[i] * 1000 + EPS];
if (t2 < ret - EPS) {
ret = t2;
pattern = 1;
} else if (t2 < ret + EPS) {
++pattern;
}
}
return ret;
}
int main() {
for (int i = 1; i <= 1000; ++i) {
double p = i / 1000.0;
double a = 1.0;
for (int j = 1;; ++j) {
if (a * p * j < EPS / 1000)
break;
destroy[i] += a * p * j;
a *= 1.0 - p;
}
}
vector<vector<int>> comb;
combination(100, comb);
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<double> p(n), q(n);
vector<int> id(n);
for (int i = 0; i < n; ++i)
cin >> p[i] >> id[i] >> q[i];
vector<bool> check(n, false);
double ret = 0.0;
long long pattern = 1;
int tmp = n;
for (int i = 0; i < n; ++i) {
if (check[i])
continue;
int j = i;
vector<double> p2, q2;
while (!check[j]) {
p2.push_back(p[j]);
q2.push_back(q[j]);
check[j] = true;
j = id[j];
}
int pattern2;
ret += solve(p2, q2, pattern2);
pattern *= pattern2;
pattern %= MOD;
pattern *= comb[tmp][p2.size()];
pattern %= MOD;
tmp -= p2.size();
}
printf("%.10f %d\n", ret, (int)pattern);
}
} | replace | 61 | 62 | 61 | 64 | TLE | |
p00667 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int mod = 100000007;
int dp[100001][6], dp2[100001][4];
int sum[100001], sum2[100001];
string in;
int main() {
dp[0][0] = dp2[0][0] = 1;
rep(i, 100000) rep(j, 6) {
if (j < 5)
(dp[i + 1][j + 1] += dp[i][j]) %= mod;
(dp[i + 1][1] += dp[i][j]) %= mod;
}
rep(i, 100000) rep(j, 4) {
if (j < 3)
(dp2[i + 1][j + 1] += dp2[i][j]) %= mod;
(dp2[i + 1][1] += dp2[i][j]) %= mod;
}
rep(i, 100001) {
rep(j, 6)(sum[i] += dp[i][j]) %= mod;
rep(j, 4)(sum2[i] += dp2[i][j]) %= mod;
}
rep(i, 100001) {
if (i > 5)
(sum[i] += sum[i - 6]) %= mod;
if (i > 3)
(sum2[i] += sum2[i - 4]) %= mod;
}
rep(i, 20) cerr << sum[i] << " ";
cerr << endl;
while (getline(cin, in), in != "#") {
int cnt = 0;
vi v, type;
rep(i, in.size()) {
if (i && in[i] != in[i - 1]) {
v.pb(cnt);
cnt = 1;
type.pb(in[i - 1] == '0' || in[i - 1] == '8');
} else
cnt++;
}
type.pb(in[in.size() - 1] == '0' || in[in.size() - 1] == '8');
v.pb(cnt);
ll ans = 1;
rep(i, v.size()) {
if (!type[i])
(ans *= sum[v[i] - 1]) %= mod;
else
(ans *= sum2[v[i] - 1]) %= mod;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int mod = 100000007;
int dp[100001][6], dp2[100001][4];
int sum[100001], sum2[100001];
string in;
int main() {
dp[0][0] = dp2[0][0] = 1;
rep(i, 100000) rep(j, 6) {
if (j < 5)
(dp[i + 1][j + 1] += dp[i][j]) %= mod;
(dp[i + 1][1] += dp[i][j]) %= mod;
}
rep(i, 100000) rep(j, 4) {
if (j < 3)
(dp2[i + 1][j + 1] += dp2[i][j]) %= mod;
(dp2[i + 1][1] += dp2[i][j]) %= mod;
}
rep(i, 100001) {
rep(j, 6)(sum[i] += dp[i][j]) %= mod;
rep(j, 4)(sum2[i] += dp2[i][j]) %= mod;
}
rep(i, 100001) {
if (i > 5)
(sum[i] += sum[i - 6]) %= mod;
if (i > 3)
(sum2[i] += sum2[i - 4]) %= mod;
}
while (getline(cin, in), in != "#") {
int cnt = 0;
vi v, type;
rep(i, in.size()) {
if (i && in[i] != in[i - 1]) {
v.pb(cnt);
cnt = 1;
type.pb(in[i - 1] == '0' || in[i - 1] == '8');
} else
cnt++;
}
type.pb(in[in.size() - 1] == '0' || in[in.size() - 1] == '8');
v.pb(cnt);
ll ans = 1;
rep(i, v.size()) {
if (!type[i])
(ans *= sum[v[i] - 1]) %= mod;
else
(ans *= sum2[v[i] - 1]) %= mod;
}
cout << ans << endl;
}
return 0;
} | delete | 51 | 54 | 51 | 51 | TLE | |
p00668 | C++ | Runtime Error | #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;
}
const int MAX = 64 * 1024;
int id[MAX];
int idCnt;
int segTree[MAX * 2];
map<int, int> dict;
int q;
int lim;
void init() {
idCnt = 0;
memset(segTree, 0, sizeof(segTree));
dict.clear();
}
void update(int pos, int val) {
pos += MAX;
segTree[pos] = val;
while (pos) {
pos = (pos - 1) / 2;
segTree[pos] = segTree[2 * pos + 1] + segTree[2 * pos + 2];
}
}
int nthId(int n) {
int pos = 0;
n += 1;
while (pos < MAX) {
int left = pos * 2 + 1;
int right = pos * 2 + 2;
// printf("n=%d l=%d r=%d\n", n, segTree[left], segTree[right]);
if (segTree[left] >= n) {
pos = left;
} else {
pos = right;
n -= segTree[left];
}
}
if (n != 1 || segTree[pos] != 1) {
printf("error: n=%d segTree[%d]=%d %d\n", n, pos, segTree[pos], pos - MAX);
}
return pos - MAX;
}
void delId(int i) { update(nthId(i), 0); }
void delVal(int i) { update(dict[i], 0); }
void addId(int i) {
id[idCnt] = i;
dict[i] = idCnt;
update(idCnt, 1);
if (segTree[0] > lim)
delId(0);
idCnt++;
}
int main() {
while (true) {
q = getInt();
lim = getInt();
if (q == 0)
break;
init();
while (q-- > 0) {
int query = getInt();
int x = getInt();
switch (query) {
case 0:
addId(x);
break;
case 1:
delId(x - 1);
break;
case 2:
printf("%d\n", id[nthId(x - 1)]);
break;
case 3:
delVal(x);
break;
}
// printf("query: %d %d\n", query, x);
// printf("alive: %d\n", segTree[0]);
// REP(i, idCnt) printf("%d(%d) ", id[i], segTree[i+MAX]); puts("");
}
puts("end");
}
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;
}
const int MAX = 16 * 64 * 1024;
int id[MAX];
int idCnt;
int segTree[MAX * 2];
map<int, int> dict;
int q;
int lim;
void init() {
idCnt = 0;
memset(segTree, 0, sizeof(segTree));
dict.clear();
}
void update(int pos, int val) {
pos += MAX;
segTree[pos] = val;
while (pos) {
pos = (pos - 1) / 2;
segTree[pos] = segTree[2 * pos + 1] + segTree[2 * pos + 2];
}
}
int nthId(int n) {
int pos = 0;
n += 1;
while (pos < MAX) {
int left = pos * 2 + 1;
int right = pos * 2 + 2;
// printf("n=%d l=%d r=%d\n", n, segTree[left], segTree[right]);
if (segTree[left] >= n) {
pos = left;
} else {
pos = right;
n -= segTree[left];
}
}
if (n != 1 || segTree[pos] != 1) {
printf("error: n=%d segTree[%d]=%d %d\n", n, pos, segTree[pos], pos - MAX);
}
return pos - MAX;
}
void delId(int i) { update(nthId(i), 0); }
void delVal(int i) { update(dict[i], 0); }
void addId(int i) {
id[idCnt] = i;
dict[i] = idCnt;
update(idCnt, 1);
if (segTree[0] > lim)
delId(0);
idCnt++;
}
int main() {
while (true) {
q = getInt();
lim = getInt();
if (q == 0)
break;
init();
while (q-- > 0) {
int query = getInt();
int x = getInt();
switch (query) {
case 0:
addId(x);
break;
case 1:
delId(x - 1);
break;
case 2:
printf("%d\n", id[nthId(x - 1)]);
break;
case 3:
delVal(x);
break;
}
// printf("query: %d %d\n", query, x);
// printf("alive: %d\n", segTree[0]);
// REP(i, idCnt) printf("%d(%d) ", id[i], segTree[i+MAX]); puts("");
}
puts("end");
}
return 0;
} | replace | 50 | 51 | 50 | 51 | 0 | |
p00668 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;
//
// using Graph = vector<vector<int>>;
//
// int dfs(const Graph&g, const int now, vector<int>&ch_cnts) {
// ch_cnts[now]++;
// for (auto&& e : g[now]) {
// ch_cnts[now] += dfs(g, e, ch_cnts);
// }
// return ch_cnts[now];
//}
//
// struct HL_Edge {
// int from;
// int to;
// bool heavy;
//};
//
// void dfs2(const Graph&g, vector<vector<HL_Edge>>&hl_tree, const int now,
// const vector<int>&ch_cnts) {
//
// int max_ch_id = 1;
// {
// int max_ch_num = -1;
// for (auto&&e : g[now]) {
// if (max_ch_num < ch_cnts[e]) {
// max_ch_num = ch_cnts[e];
// max_ch_id = e;
// }
// }
// }
// for (auto e : g[now]) {
// bool heavy = (e == max_ch_id);
//
// hl_tree[now].push_back(HL_Edge{ now,e,heavy });
// }
// return;
//}
//
// void HL(const vector<vector<int>>&ro_tree) {
// vector<int>ch_cnts(ro_tree.size());
//
// dfs(ro_tree, 0, ch_cnts);
//
// vector<vector<HL_Edge>>hl_tree(ro_tree.size());
//
// dfs2(ro_tree, hl_tree, 0, ch_cnts);
//}
//
// namespace FastFourierTransform
//{
// using C = complex< double >;
//
// void DiscreteFourierTransform(vector< C > &F, bool rev)
// {
// const int N = (int)F.size();
// const double PI = (rev ? -1 : 1) * acos(-1);
// for (int i = 0, j = 1; j + 1 < N; j++) {
// for (int k = N >> 1; k > (i ^= k); k >>= 1);
// if (i > j) swap(F[i], F[j]);
// }
// C w, s, t;
// for (int i = 1; i < N; i <<= 1) {
// for (int k = 0; k < i; k++) {
// w = polar(1.0, PI / i * k);
// for (int j = 0; j < N; j += i * 2) {
// s = F[j + k];
// t = C(F[j + k + i].real() * w.real() - F[j + k + i].imag() *
// w.imag(),
// F[j + k + i].real() * w.imag() + F[j + k + i].imag() *
// w.real());
// F[j + k] = s + t, F[j + k + i] = s - t;
// }
// }
// }
// if (rev) for (int i = 0; i < N; i++) F[i] /= N;
// }
//
// vector< int> Multiply(const vector<int > &A, const vector<int > &B)
// {
// int sz = 1;
// while (sz <= A.size() + B.size()) sz <<= 1;
// vector< C > F(sz), G(sz);
// for (int i = 0; i < A.size(); i++) F[i] = A[i];
// for (int i = 0; i < B.size(); i++) G[i] = B[i];
// DiscreteFourierTransform(F, false);
// DiscreteFourierTransform(G, false);
// for (int i = 0; i < sz; i++) F[i] *= G[i];
// DiscreteFourierTransform(F, true);
// vector< int > X(A.size() + B.size() - 1);
// for (int i = 0; i < A.size() + B.size() - 1; i++) X[i] = F[i].real() +
// 0.5; return (X);
// }
//};
#define Seg_Max_N (1 << 19)
using Value = int;
const Value ini = 0;
struct segtree {
int N;
vector<Value> dat;
segtree() {}
segtree(int n) : dat(2 * Seg_Max_N) {
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < 2 * N - 1; i++) {
dat[i] = ini;
}
}
Value connect(const Value &l, const Value &r) { return l + r; }
// update k th element
void update(int k, Value a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
const Value al(dat[k * 2 + 1]);
const Value ar(dat[k * 2 + 2]);
dat[k] = connect(al, ar);
}
}
// min [a, b)
Value query(int a, int b) { return query(a, b, 0, 0, N); }
Value query(int a, int b, int k, int l, int r) {
if (r <= a or b <= l)
return ini;
if (a <= l and r <= b)
return dat[k];
const int m = (l + r) / 2;
const Value al(query(a, b, k * 2 + 1, l, m));
const Value ar(query(a, b, k * 2 + 2, m, r));
return connect(al, ar);
}
};
int main() {
while (true) {
int Q, LIM;
cin >> Q >> LIM;
vector<int> nums;
map<int, int> mp;
segtree tree(4e5);
int nl = 0;
int sum = 0;
while (Q--) {
int a, x;
cin >> a >> x;
if (a == 0) {
nums.push_back(x);
tree.update(nums.size() - 1, 1);
mp[x] = nums.size() - 1;
sum++;
if (sum > LIM) {
while (true) {
if (tree.query(nl, nl + 1) == 1) {
tree.update(nl, 0);
nl++;
break;
} else {
nl++;
}
}
sum--;
}
} else if (a == 1) {
int minr = 0;
int maxr = nums.size();
while (minr + 1 != maxr) {
int midr((minr + maxr + 1) / 2);
if (tree.query(0, midr) >= x) {
maxr = midr;
} else {
minr = midr;
}
}
tree.update(maxr - 1, 0);
sum--;
} else if (a == 3) {
assert(tree.query(mp[x], mp[x] + 1) == 1);
tree.update(mp[x], 0);
sum--;
} else {
int minr = 0;
int maxr = nums.size();
while (minr + 1 != maxr) {
int midr((minr + maxr + 1) / 2);
if (tree.query(0, midr) >= x) {
maxr = midr;
} else {
minr = midr;
}
}
cout << nums[maxr - 1] << endl;
}
}
cout << "end" << endl;
}
return 0;
}
| #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;
//
// using Graph = vector<vector<int>>;
//
// int dfs(const Graph&g, const int now, vector<int>&ch_cnts) {
// ch_cnts[now]++;
// for (auto&& e : g[now]) {
// ch_cnts[now] += dfs(g, e, ch_cnts);
// }
// return ch_cnts[now];
//}
//
// struct HL_Edge {
// int from;
// int to;
// bool heavy;
//};
//
// void dfs2(const Graph&g, vector<vector<HL_Edge>>&hl_tree, const int now,
// const vector<int>&ch_cnts) {
//
// int max_ch_id = 1;
// {
// int max_ch_num = -1;
// for (auto&&e : g[now]) {
// if (max_ch_num < ch_cnts[e]) {
// max_ch_num = ch_cnts[e];
// max_ch_id = e;
// }
// }
// }
// for (auto e : g[now]) {
// bool heavy = (e == max_ch_id);
//
// hl_tree[now].push_back(HL_Edge{ now,e,heavy });
// }
// return;
//}
//
// void HL(const vector<vector<int>>&ro_tree) {
// vector<int>ch_cnts(ro_tree.size());
//
// dfs(ro_tree, 0, ch_cnts);
//
// vector<vector<HL_Edge>>hl_tree(ro_tree.size());
//
// dfs2(ro_tree, hl_tree, 0, ch_cnts);
//}
//
// namespace FastFourierTransform
//{
// using C = complex< double >;
//
// void DiscreteFourierTransform(vector< C > &F, bool rev)
// {
// const int N = (int)F.size();
// const double PI = (rev ? -1 : 1) * acos(-1);
// for (int i = 0, j = 1; j + 1 < N; j++) {
// for (int k = N >> 1; k > (i ^= k); k >>= 1);
// if (i > j) swap(F[i], F[j]);
// }
// C w, s, t;
// for (int i = 1; i < N; i <<= 1) {
// for (int k = 0; k < i; k++) {
// w = polar(1.0, PI / i * k);
// for (int j = 0; j < N; j += i * 2) {
// s = F[j + k];
// t = C(F[j + k + i].real() * w.real() - F[j + k + i].imag() *
// w.imag(),
// F[j + k + i].real() * w.imag() + F[j + k + i].imag() *
// w.real());
// F[j + k] = s + t, F[j + k + i] = s - t;
// }
// }
// }
// if (rev) for (int i = 0; i < N; i++) F[i] /= N;
// }
//
// vector< int> Multiply(const vector<int > &A, const vector<int > &B)
// {
// int sz = 1;
// while (sz <= A.size() + B.size()) sz <<= 1;
// vector< C > F(sz), G(sz);
// for (int i = 0; i < A.size(); i++) F[i] = A[i];
// for (int i = 0; i < B.size(); i++) G[i] = B[i];
// DiscreteFourierTransform(F, false);
// DiscreteFourierTransform(G, false);
// for (int i = 0; i < sz; i++) F[i] *= G[i];
// DiscreteFourierTransform(F, true);
// vector< int > X(A.size() + B.size() - 1);
// for (int i = 0; i < A.size() + B.size() - 1; i++) X[i] = F[i].real() +
// 0.5; return (X);
// }
//};
#define Seg_Max_N (1 << 19)
using Value = int;
const Value ini = 0;
struct segtree {
int N;
vector<Value> dat;
segtree() {}
segtree(int n) : dat(2 * Seg_Max_N) {
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < 2 * N - 1; i++) {
dat[i] = ini;
}
}
Value connect(const Value &l, const Value &r) { return l + r; }
// update k th element
void update(int k, Value a) {
k += N - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
const Value al(dat[k * 2 + 1]);
const Value ar(dat[k * 2 + 2]);
dat[k] = connect(al, ar);
}
}
// min [a, b)
Value query(int a, int b) { return query(a, b, 0, 0, N); }
Value query(int a, int b, int k, int l, int r) {
if (r <= a or b <= l)
return ini;
if (a <= l and r <= b)
return dat[k];
const int m = (l + r) / 2;
const Value al(query(a, b, k * 2 + 1, l, m));
const Value ar(query(a, b, k * 2 + 2, m, r));
return connect(al, ar);
}
};
int main() {
while (true) {
int Q, LIM;
cin >> Q >> LIM;
if (!Q)
break;
vector<int> nums;
map<int, int> mp;
segtree tree(4e5);
int nl = 0;
int sum = 0;
while (Q--) {
int a, x;
cin >> a >> x;
if (a == 0) {
nums.push_back(x);
tree.update(nums.size() - 1, 1);
mp[x] = nums.size() - 1;
sum++;
if (sum > LIM) {
while (true) {
if (tree.query(nl, nl + 1) == 1) {
tree.update(nl, 0);
nl++;
break;
} else {
nl++;
}
}
sum--;
}
} else if (a == 1) {
int minr = 0;
int maxr = nums.size();
while (minr + 1 != maxr) {
int midr((minr + maxr + 1) / 2);
if (tree.query(0, midr) >= x) {
maxr = midr;
} else {
minr = midr;
}
}
tree.update(maxr - 1, 0);
sum--;
} else if (a == 3) {
assert(tree.query(mp[x], mp[x] + 1) == 1);
tree.update(mp[x], 0);
sum--;
} else {
int minr = 0;
int maxr = nums.size();
while (minr + 1 != maxr) {
int midr((minr + maxr + 1) / 2);
if (tree.query(0, midr) >= x) {
maxr = midr;
} else {
minr = midr;
}
}
cout << nums[maxr - 1] << endl;
}
}
cout << "end" << endl;
}
return 0;
}
| insert | 149 | 149 | 149 | 151 | TLE | |
p00668 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
int n, ln;
int bn = 0;
int non = 0;
int bit[400005] = {};
vector<int> dats;
map<int, int> poss;
int bget(int p) {
int res = 0;
while (p) {
res += bit[p];
p -= (p & (-p));
}
return res;
}
void bset(int p, int q) { // 1.indexd
while (p <= bn) {
bit[p] += q;
p += (p & (-p));
}
}
int ordtopos(int p) {
int l = 0, r = dats.size();
r--;
while (l + 1 < r) {
int tyu = (l + r) / 2;
int no = bget(tyu);
if (no >= p)
r = tyu;
else
l = tyu;
}
return r;
}
void enkanbypos(int p) {
bset(p, -1);
non--;
}
int main(void) {
/*
bn = 10000;
bset(1,1);
bset(2,3);
printf("%d\n",bget(2));
printf("%d\n",bget(1));
bset(1,-5);
printf("%d\n",bget(2));
printf("%d\n",bget(1));
*/
while (1) {
scanf("%d%d", &n, &ln);
if (n == 0)
break;
poss.clear();
dats.clear();
memset(bit, 0, sizeof(bit));
bn = 1;
non = 0;
while (bn <= n + 5)
bn *= 2;
dats.push_back(-10000);
rep(i, n) {
int a, b;
scanf("%d%d", &a, &b); // input .. 1.indexed
// printf("%d %d\n",a,b);
switch (a) {
case 0: {
int no = ((int)dats.size());
poss[b] = no;
dats.push_back(b);
bset(no, 1);
non++;
if (non > ln)
enkanbypos(ordtopos(1));
} break;
case 1:
enkanbypos(ordtopos(b));
break;
case 2:
printf("%d\n", dats[ordtopos(b)]);
break;
case 3:
enkanbypos(poss[b]);
break;
}
/*
if(a==0){
printf("ordpos .. %d is %d\n",1,ordtopos(1));
}
else if(a==1 || a==2){
printf("ordpos .. %d is %d\n",b,ordtopos(b));
}
printf("non .. %d\n",non);
rep(k,dats.size()){
printf("(%d %d) ",dats[k],bget(k));
}
printf("\n\n");*/
}
printf("end\n");
}
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
int n, ln;
int bn = 0;
int non = 0;
int bit[800005] = {};
vector<int> dats;
map<int, int> poss;
int bget(int p) {
int res = 0;
while (p) {
res += bit[p];
p -= (p & (-p));
}
return res;
}
void bset(int p, int q) { // 1.indexd
while (p <= bn) {
bit[p] += q;
p += (p & (-p));
}
}
int ordtopos(int p) {
int l = 0, r = dats.size();
r--;
while (l + 1 < r) {
int tyu = (l + r) / 2;
int no = bget(tyu);
if (no >= p)
r = tyu;
else
l = tyu;
}
return r;
}
void enkanbypos(int p) {
bset(p, -1);
non--;
}
int main(void) {
/*
bn = 10000;
bset(1,1);
bset(2,3);
printf("%d\n",bget(2));
printf("%d\n",bget(1));
bset(1,-5);
printf("%d\n",bget(2));
printf("%d\n",bget(1));
*/
while (1) {
scanf("%d%d", &n, &ln);
if (n == 0)
break;
poss.clear();
dats.clear();
memset(bit, 0, sizeof(bit));
bn = 1;
non = 0;
while (bn <= n + 5)
bn *= 2;
dats.push_back(-10000);
rep(i, n) {
int a, b;
scanf("%d%d", &a, &b); // input .. 1.indexed
// printf("%d %d\n",a,b);
switch (a) {
case 0: {
int no = ((int)dats.size());
poss[b] = no;
dats.push_back(b);
bset(no, 1);
non++;
if (non > ln)
enkanbypos(ordtopos(1));
} break;
case 1:
enkanbypos(ordtopos(b));
break;
case 2:
printf("%d\n", dats[ordtopos(b)]);
break;
case 3:
enkanbypos(poss[b]);
break;
}
/*
if(a==0){
printf("ordpos .. %d is %d\n",1,ordtopos(1));
}
else if(a==1 || a==2){
printf("ordpos .. %d is %d\n",b,ordtopos(b));
}
printf("non .. %d\n",non);
rep(k,dats.size()){
printf("(%d %d) ",dats[k],bget(k));
}
printf("\n\n");*/
}
printf("end\n");
}
return 0;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p00668 | C++ | Time Limit Exceeded | #include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int q, lim;
// セグ木
int dat[1 << 20 + 2];
// リーフ(一番下の葉)の最小
int low = 1 << 19 + 1;
// dat配列はどこまで埋まっているか
int size;
// 固体番号
int rnum[500000];
// 固体番号を円環するときに利用
map<int, int> num;
void add(int x) {
// 固体番号(円環)追加
num[x] = size;
// 要素数追加
dat[size] = 0;
// 固体番号追加
rnum[size - low] = x;
// 配列が1つ埋まった
size++;
// 管理できる個体数が1つ減った
lim--;
int pos = size - 1;
// ルートノードまで登りながら, 各ノードをインクリメント
while (0 < pos) {
dat[pos]++;
pos = (pos - 1) / 2;
}
}
// 配列のx番目が, dat配列の何番目か求める
int find(int x) {
int pos = 0;
// 左にもぐるか, 右にもぐるか決めつつ, リーフまでもぐる
while (pos < low) {
int left = pos * 2 + 1;
int right = pos * 2 + 2;
if (x <= dat[left]) {
pos = left;
} else {
x -= dat[left];
pos = right;
}
}
return pos;
}
// dat配列のpos番目を削除する処理
void erase(int pos) {
lim++;
while (0 < pos) {
dat[pos]--;
pos = (pos - 1) / 2;
}
}
int main(void) {
while (1) {
scanf("%d%d", &q, &lim);
memset(dat, 0, sizeof(0));
memset(rnum, 0, sizeof(rnum));
num.clear();
size = low;
for (int i = 0; i < q; i++) {
int query, x;
scanf("%d%d", &query, &x);
if (query == 0) {
add(x);
if (lim < 0) {
int pos = find(0);
erase(pos);
}
} else if (query == 1) {
int pos = find(x);
erase(pos);
} else if (query == 2) {
int pos = find(x);
printf("%d\n", rnum[pos - low]);
} else if (query == 3) {
int pos = num[x];
erase(pos);
}
}
printf("end\n");
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int q, lim;
// セグ木
int dat[1 << 20 + 2];
// リーフ(一番下の葉)の最小
int low = 1 << 19 + 1;
// dat配列はどこまで埋まっているか
int size;
// 固体番号
int rnum[500000];
// 固体番号を円環するときに利用
map<int, int> num;
void add(int x) {
// 固体番号(円環)追加
num[x] = size;
// 要素数追加
dat[size] = 0;
// 固体番号追加
rnum[size - low] = x;
// 配列が1つ埋まった
size++;
// 管理できる個体数が1つ減った
lim--;
int pos = size - 1;
// ルートノードまで登りながら, 各ノードをインクリメント
while (0 < pos) {
dat[pos]++;
pos = (pos - 1) / 2;
}
}
// 配列のx番目が, dat配列の何番目か求める
int find(int x) {
int pos = 0;
// 左にもぐるか, 右にもぐるか決めつつ, リーフまでもぐる
while (pos < low) {
int left = pos * 2 + 1;
int right = pos * 2 + 2;
if (x <= dat[left]) {
pos = left;
} else {
x -= dat[left];
pos = right;
}
}
return pos;
}
// dat配列のpos番目を削除する処理
void erase(int pos) {
lim++;
while (0 < pos) {
dat[pos]--;
pos = (pos - 1) / 2;
}
}
int main(void) {
while (1) {
scanf("%d%d", &q, &lim);
if (q == 0 && lim == 0)
break;
memset(dat, 0, sizeof(dat));
memset(rnum, 0, sizeof(rnum));
num.clear();
size = low;
for (int i = 0; i < q; i++) {
int query, x;
scanf("%d%d", &query, &x);
if (query == 0) {
add(x);
if (lim < 0) {
int pos = find(0);
erase(pos);
}
} else if (query == 1) {
int pos = find(x);
erase(pos);
} else if (query == 2) {
int pos = find(x);
printf("%d\n", rnum[pos - low]);
} else if (query == 3) {
int pos = num[x];
erase(pos);
}
}
printf("end\n");
}
return 0;
} | replace | 68 | 69 | 68 | 71 | TLE | |
p00670 | C++ | Time 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 <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
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;
}
int main() {
ios::sync_with_stdio(false);
int n, s;
while (cin >> n >> s, n) {
int r[128];
for (int i = 0; i < n; ++i)
cin >> r[i];
sort(r, r + n);
int res = 0;
for (int i = 0; i < n; ++i)
res += (r + n) - upper_bound(r + (i + 1), r + n, s - r[i]);
cout << res << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif
#define foreach(it, c) \
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
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;
}
int main() {
ios::sync_with_stdio(false);
int n, s;
while (cin >> n >> s, n) {
int r[21234];
for (int i = 0; i < n; ++i)
cin >> r[i];
sort(r, r + n);
int res = 0;
for (int i = 0; i < n; ++i)
res += (r + n) - upper_bound(r + (i + 1), r + n, s - r[i]);
cout << res << endl;
}
} | replace | 60 | 61 | 60 | 61 | TLE | |
p00670 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int n, s;
int r[20000];
while (cin >> n >> s, n || s) {
for (int i = 0; i < n; i++) {
cin >> r[i];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (r[i] + r[j] > s) {
cnt++;
}
}
}
cout << cnt << endl;
}
} | #include <iostream>
using namespace std;
int main() {
int n, s;
int r[20000];
while (cin >> n >> s, n || s) {
for (int i = 0; i < n; i++) {
cin >> r[i];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (r[i] > s) {
cnt += n - i - 1;
} else {
for (int j = i + 1; j < n; j++) {
if (r[i] + r[j] > s) {
cnt++;
}
}
}
}
cout << cnt << endl;
}
} | replace | 14 | 17 | 14 | 21 | TLE | |
p00670 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
using namespace std;
int main() {
int n, S;
while (cin >> n >> S, n + S) {
vi r;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
r.push_back(m);
}
int ans = 0;
for (int i = 0; i < r.size(); i++) {
for (int j = i + 1; j < r.size(); j++) {
if (S < r[i] + r[j])
ans++;
}
}
cout << ans << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
using namespace std;
int main() {
int n, S;
while (cin >> n >> S, n + S) {
vi r;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
r.push_back(m);
}
int ans = 0;
for (int i = 0; i < r.size(); i++) {
for (int j = 0; j < i; j++) {
if (S < r[i] + r[j])
ans++;
}
}
cout << ans << endl;
}
} | replace | 42 | 43 | 42 | 43 | TLE | |
p00672 | C++ | Time Limit Exceeded | #include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
int n, m, p;
int pos;
char str[10000];
map<string, vector<int>> mapto;
map<vector<int>, string> mapfrom;
map<string, string> convert;
vector<int> error;
vector<int> formula();
vector<int> factor() {
vector<int> lhs;
if (str[pos] == '(') {
pos++;
lhs = formula();
pos++;
} else {
string s;
while (isalpha(str[pos])) {
s += str[pos++];
}
if (!convert.count(s)) {
return error;
}
if (!mapto.count(convert[s])) {
return error;
}
lhs = mapto[convert[s]];
}
return lhs;
}
vector<int> term() {
vector<int> lhs = factor();
if (lhs == error) {
return error;
}
while (str[pos] == '*' || str[pos] == '/') {
int s = str[pos] == '*' ? 1 : -1;
pos++;
vector<int> rhs = factor();
if (rhs == error) {
return error;
}
REP(i, n) { lhs[i] += s * rhs[i]; }
}
return lhs;
}
vector<int> formula() {
vector<int> lhs = term();
if (lhs == error) {
return error;
}
while (str[pos] == '+' || str[pos] == '-') {
pos++;
vector<int> rhs = term();
if (rhs == error) {
return error;
}
REP(i, n) {
if (lhs[i] != rhs[i]) {
return error;
}
}
}
return lhs;
}
int main() {
error = vector<int>(1, -1000000000);
while (scanf("%d %d %d", &n, &m, &p), n | m | p) {
pos = 0;
mapto.clear();
mapfrom.clear();
REP(i, m) {
string s;
cin >> s;
vector<int> vect(n, 0);
REP(i, n) { scanf("%d", &vect[i]); }
mapto[s] = vect;
mapfrom[vect] = s;
}
scanf("%s", str);
REP(i, p) {
string from, to;
cin >> from >> to;
convert[from] = to;
}
vector<int> ans = formula();
int cnt = 0;
REP(i, strlen(str)) {
cnt += str[pos] == '(';
cnt -= str[pos] == ')';
}
while (cnt != 0)
;
if (ans[0] <= -100000) {
puts("error");
} else if (mapfrom.count(ans)) {
printf("%s\n", mapfrom[ans].c_str());
} else {
puts("undefined");
}
}
} | #include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
int n, m, p;
int pos;
char str[10000];
map<string, vector<int>> mapto;
map<vector<int>, string> mapfrom;
map<string, string> convert;
vector<int> error;
vector<int> formula();
vector<int> factor() {
vector<int> lhs;
if (str[pos] == '(') {
pos++;
lhs = formula();
pos++;
} else {
string s;
while (isalpha(str[pos])) {
s += str[pos++];
}
if (!convert.count(s)) {
return error;
}
if (!mapto.count(convert[s])) {
return error;
}
lhs = mapto[convert[s]];
}
return lhs;
}
vector<int> term() {
vector<int> lhs = factor();
if (lhs == error) {
return error;
}
while (str[pos] == '*' || str[pos] == '/') {
int s = str[pos] == '*' ? 1 : -1;
pos++;
vector<int> rhs = factor();
if (rhs == error) {
return error;
}
REP(i, n) { lhs[i] += s * rhs[i]; }
}
return lhs;
}
vector<int> formula() {
vector<int> lhs = term();
if (lhs == error) {
return error;
}
while (str[pos] == '+' || str[pos] == '-') {
pos++;
vector<int> rhs = term();
if (rhs == error) {
return error;
}
REP(i, n) {
if (lhs[i] != rhs[i]) {
return error;
}
}
}
return lhs;
}
int main() {
error = vector<int>(1, -1000000000);
while (scanf("%d %d %d", &n, &m, &p), n | m | p) {
pos = 0;
mapto.clear();
mapfrom.clear();
REP(i, m) {
string s;
cin >> s;
vector<int> vect(n, 0);
REP(i, n) { scanf("%d", &vect[i]); }
mapto[s] = vect;
mapfrom[vect] = s;
}
scanf("%s", str);
REP(i, p) {
string from, to;
cin >> from >> to;
convert[from] = to;
}
vector<int> ans = formula();
int cnt = 0;
REP(i, strlen(str)) {
cnt += str[i] == '(';
cnt -= str[i] == ')';
}
while (cnt != 0)
;
if (ans[0] <= -100000) {
puts("error");
} else if (mapfrom.count(ans)) {
printf("%s\n", mapfrom[ans].c_str());
} else {
puts("undefined");
}
}
} | replace | 117 | 119 | 117 | 119 | TLE | |
p00673 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
struct Primal_Dual {
typedef pair<int, int> Pi;
struct edge {
int to, cap, cost, rev;
};
vector<vector<edge>> graph;
vector<int> potential, min_cost, prevv, preve;
Primal_Dual(int V) : graph(V) {}
void add_edge(int from, int to, int cap, int cost) {
graph[from].push_back((edge){to, cap, cost, (int)graph[to].size()});
graph[to].push_back((edge){from, 0, -cost, (int)graph[from].size() - 1});
}
int min_cost_flow(int s, int t, int f) {
int V = graph.size(), ret = 0;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
potential.assign(V, 0);
prevv.assign(V, -1);
preve.assign(V, -1);
while (f > 0) {
min_cost.assign(V, INF);
que.push(Pi(0, s));
min_cost[s] = 0;
while (!que.empty()) {
Pi p = que.top();
que.pop();
if (min_cost[p.second] < p.first)
continue;
for (int i = 0; i < graph[p.second].size(); i++) {
edge &e = graph[p.second][i];
int nextCost = min_cost[p.second] + e.cost + potential[p.second] -
potential[e.to];
if (e.cap > 0 && min_cost[e.to] > nextCost) {
min_cost[e.to] = nextCost;
prevv[e.to] = p.second, preve[e.to] = i;
que.push(Pi(min_cost[e.to], e.to));
}
}
}
if (min_cost[t] == INF)
return -1;
for (int v = 0; v < V; v++)
potential[v] += min_cost[v];
int addflow = f;
for (int v = t; v != s; v = prevv[v]) {
addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
}
f -= addflow;
ret += addflow * potential[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = graph[prevv[v]][preve[v]];
e.cap -= addflow;
graph[v][e.rev].cap += addflow;
}
}
return (ret);
}
};
struct Data {
int X, Y, C;
bool operator<(const Data &e) const { return (Y < e.Y); }
};
int main() {
int N;
while (scanf("%d", &N), N) {
Primal_Dual MCF(N * 20 * 3);
vector<vector<Data>> train(N);
vector<int> nums;
for (int i = 0; i < N - 1; i++) {
int M;
scanf("%d", &M);
while (M--) {
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
nums.push_back(y);
train[i + 1].push_back((Data){x, y, c});
}
}
int G;
train[0].push_back((Data){0, 0, 0});
scanf("%d", &G);
int ptr = 0;
int prev = 0;
vector<int> arras, now;
now.push_back(0);
for (int i = 1; i < train.size(); i++) {
sort(train[i].begin(), train[i].end());
int buff = ptr;
for (int j = 0; j < train[i].size(); j++) {
if (train[i - 1][0].Y > train[i][j].X)
continue;
int low = 0, high = train[i - 1].size() - 1;
while (high - low > 0) {
int mid = (low + high + 1) >> 1;
if (train[i][j].X >= train[i - 1][mid].Y)
low = mid;
else
high = mid - 1;
}
if (j == 0 || train[i][j - 1].Y != train[i][j].Y) {
MCF.add_edge(now[low], ++ptr, 1, train[i][j].C);
MCF.add_edge(ptr, ptr + 1, 1, 0);
++ptr;
arras.push_back(ptr);
if (j != 0)
MCF.add_edge(ptr - 2, ptr, INF, 0);
} else {
arras.push_back(ptr);
MCF.add_edge(now[low], ptr - 1, 1, train[i][j].C);
}
}
prev = buff;
swap(arras, now);
arras.clear();
}
int ret = 0, buff = 0, tail = 0;
for (tail = 1; tail <= G; tail++) {
if ((buff = MCF.min_cost_flow(0, ptr, 1)) == -1)
break;
ret += buff;
};
scanf("%d %d\n", tail - 1, ret);
}
} | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
struct Primal_Dual {
typedef pair<int, int> Pi;
struct edge {
int to, cap, cost, rev;
};
vector<vector<edge>> graph;
vector<int> potential, min_cost, prevv, preve;
Primal_Dual(int V) : graph(V) {}
void add_edge(int from, int to, int cap, int cost) {
graph[from].push_back((edge){to, cap, cost, (int)graph[to].size()});
graph[to].push_back((edge){from, 0, -cost, (int)graph[from].size() - 1});
}
int min_cost_flow(int s, int t, int f) {
int V = graph.size(), ret = 0;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
potential.assign(V, 0);
prevv.assign(V, -1);
preve.assign(V, -1);
while (f > 0) {
min_cost.assign(V, INF);
que.push(Pi(0, s));
min_cost[s] = 0;
while (!que.empty()) {
Pi p = que.top();
que.pop();
if (min_cost[p.second] < p.first)
continue;
for (int i = 0; i < graph[p.second].size(); i++) {
edge &e = graph[p.second][i];
int nextCost = min_cost[p.second] + e.cost + potential[p.second] -
potential[e.to];
if (e.cap > 0 && min_cost[e.to] > nextCost) {
min_cost[e.to] = nextCost;
prevv[e.to] = p.second, preve[e.to] = i;
que.push(Pi(min_cost[e.to], e.to));
}
}
}
if (min_cost[t] == INF)
return -1;
for (int v = 0; v < V; v++)
potential[v] += min_cost[v];
int addflow = f;
for (int v = t; v != s; v = prevv[v]) {
addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
}
f -= addflow;
ret += addflow * potential[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = graph[prevv[v]][preve[v]];
e.cap -= addflow;
graph[v][e.rev].cap += addflow;
}
}
return (ret);
}
};
struct Data {
int X, Y, C;
bool operator<(const Data &e) const { return (Y < e.Y); }
};
int main() {
int N;
while (scanf("%d", &N), N) {
Primal_Dual MCF(N * 20 * 3);
vector<vector<Data>> train(N);
vector<int> nums;
for (int i = 0; i < N - 1; i++) {
int M;
scanf("%d", &M);
while (M--) {
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
nums.push_back(y);
train[i + 1].push_back((Data){x, y, c});
}
}
int G;
train[0].push_back((Data){0, 0, 0});
scanf("%d", &G);
int ptr = 0;
int prev = 0;
vector<int> arras, now;
now.push_back(0);
for (int i = 1; i < train.size(); i++) {
sort(train[i].begin(), train[i].end());
int buff = ptr;
for (int j = 0; j < train[i].size(); j++) {
if (train[i - 1][0].Y > train[i][j].X)
continue;
int low = 0, high = train[i - 1].size() - 1;
while (high - low > 0) {
int mid = (low + high + 1) >> 1;
if (train[i][j].X >= train[i - 1][mid].Y)
low = mid;
else
high = mid - 1;
}
if (j == 0 || train[i][j - 1].Y != train[i][j].Y) {
MCF.add_edge(now[low], ++ptr, 1, train[i][j].C);
MCF.add_edge(ptr, ptr + 1, 1, 0);
++ptr;
arras.push_back(ptr);
if (j != 0)
MCF.add_edge(ptr - 2, ptr, INF, 0);
} else {
arras.push_back(ptr);
MCF.add_edge(now[low], ptr - 1, 1, train[i][j].C);
}
}
prev = buff;
swap(arras, now);
arras.clear();
}
int ret = 0, buff = 0, tail = 0;
for (tail = 1; tail <= G; tail++) {
if ((buff = MCF.min_cost_flow(0, ptr, 1)) == -1)
break;
ret += buff;
};
printf("%d %d\n", tail - 1, ret);
}
} | replace | 138 | 139 | 138 | 139 | -11 | |
p00674 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<vd> vvd;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)
template <typename F> ld bsearch(ld lower, ld upper, ld k, ll max_loop, F f) {
if (upper < lower)
swap(upper, lower);
ld nu = upper - lower, nl = 0;
if (k < f(0))
swap(nl, nu); // ??????????°??????´???
REP(_, max_loop) {
ld mid = (nl + nu) / 2;
(f(mid + lower) > k ? nu : nl) = mid;
}
return (nl + nu) / 2 + lower;
}
// ?????°
#define EPS (1e-10)
// ?????????
#define X real()
#define Y imag()
#define SZ size()
#define PB(x) push_back(x)
#define PREV(x, i) (x[(i + x.SZ - 1) % x.SZ])
#define NEXT(x, i) (x[(i + 1) % x.SZ])
// ??????????????£??????
using ld = long double;
template <class T> using CR = const T &;
// ?¬??????¢??°
int sgn(ld a, ld b = 0) {
if (a > b + EPS)
return 1;
if (a < b - EPS)
return -1;
return 0;
}
///////////////////////
// ???
using P = complex<ld>;
using G = vector<P>; // ???????¨???????
struct L : public vector<P> {
L(P a, P b) {
PB(a);
PB(b);
}
};
// ????????¢??°
namespace std {
bool operator<(P a, P b) { return sgn(a.X, b.X) ? a.X < b.X : a.Y < b.Y; }
} // namespace std
ld dot(P a, P b) { return (conj(a) * b).X; }
ld cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) { // Verified : AOJCGL1C
b -= a;
c -= a;
if (sgn(cross(b, c)))
return sgn(cross(b, c)); // clockwise(-1) or counter clockwise(1)
if (sgn(dot(b, c)) == -1)
return 2; // c--a--b
if (sgn(norm(b), norm(c)) == -1)
return -2; // a--b--c
return 0; // a--c--b
}
bool iLS(CR<L> l, CR<L> s, bool strict = false) {
int a = sgn(cross(l[1] - l[0], s[0] - l[0])) *
sgn(cross(l[1] - l[0], s[1] - l[0]));
return strict ? a == -1 // strict
: a <= 0; // not strict
}
P pLL(CR<L> l, CR<L> m) {
ld a = cross(l[1] - l[0], m[1] - m[0]);
ld b = cross(l[1] - l[0], l[1] - m[0]);
assert(sgn(a)); // parallel or same line
return m[0] + b / a * (m[1] - m[0]);
}
G convex_cut(CR<G> g, CR<L> l) {
G q;
REP(i, g.size()) {
if (ccw(l[0], l[1], g[i]) != -1)
q.PB(g[i]);
L s = {g[i], NEXT(g, i)};
if (iLS(l, s, true))
q.PB(pLL(l, s));
}
return q;
}
ld area2(CR<G> g) {
ld s = 0;
REP(i, g.size()) s += cross(g[i], NEXT(g, i));
return s;
}
L half_convex(CR<G> g, P p, ld lower, ld upper, ll max_loop) {
ld ans = bsearch(lower, upper, area2(g) / 2, max_loop, [&](ld x) {
return area2(convex_cut(g, {p, p + polar<ld>(1, x)}));
});
return {p, p + polar<ld>(max_loop, ans)};
}
int main() {
#ifdef _WIN32
ifstream cin("sample.in");
ofstream cout("sample.out");
#endif
cin.tie(0); // cin??¨cout?????£????????????
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(50);
while (true) {
int n;
cin >> n;
if (n == 0)
break;
G g(n);
REP(i, n) {
ld x, y;
cin >> x >> y;
g[i].real(x);
g[i].imag(y);
}
vector<ld> angles;
angles.push_back(0);
angles.push_back(acos(-1));
REP(i, n) {
ld angle = arg(g[i]);
if (angle < 0)
angle += acos(-1);
angles.push_back(angle);
}
sort(ALL(angles));
REP(i, angles.size() - 1) {
L l = half_convex(g, 0, angles[i], angles[i + 1], 1000);
G cc = convex_cut(g, l);
if (!sgn(area2(g) / 2, area2(cc))) {
cout << l[1].X << " " << l[1].Y << endl;
break;
}
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<vd> vvd;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)
template <typename F> ld bsearch(ld lower, ld upper, ld k, ll max_loop, F f) {
if (upper < lower)
swap(upper, lower);
ld nu = upper - lower, nl = 0;
if (k < f(0))
swap(nl, nu); // ??????????°??????´???
REP(_, max_loop) {
ld mid = (nl + nu) / 2;
(f(mid + lower) > k ? nu : nl) = mid;
}
return (nl + nu) / 2 + lower;
}
// ?????°
#define EPS (1e-10)
// ?????????
#define X real()
#define Y imag()
#define SZ size()
#define PB(x) push_back(x)
#define PREV(x, i) (x[(i + x.SZ - 1) % x.SZ])
#define NEXT(x, i) (x[(i + 1) % x.SZ])
// ??????????????£??????
using ld = long double;
template <class T> using CR = const T &;
// ?¬??????¢??°
int sgn(ld a, ld b = 0) {
if (a > b + EPS)
return 1;
if (a < b - EPS)
return -1;
return 0;
}
///////////////////////
// ???
using P = complex<ld>;
using G = vector<P>; // ???????¨???????
struct L : public vector<P> {
L(P a, P b) {
PB(a);
PB(b);
}
};
// ????????¢??°
namespace std {
bool operator<(P a, P b) { return sgn(a.X, b.X) ? a.X < b.X : a.Y < b.Y; }
} // namespace std
ld dot(P a, P b) { return (conj(a) * b).X; }
ld cross(P a, P b) { return (conj(a) * b).Y; }
int ccw(P a, P b, P c) { // Verified : AOJCGL1C
b -= a;
c -= a;
if (sgn(cross(b, c)))
return sgn(cross(b, c)); // clockwise(-1) or counter clockwise(1)
if (sgn(dot(b, c)) == -1)
return 2; // c--a--b
if (sgn(norm(b), norm(c)) == -1)
return -2; // a--b--c
return 0; // a--c--b
}
bool iLS(CR<L> l, CR<L> s, bool strict = false) {
int a = sgn(cross(l[1] - l[0], s[0] - l[0])) *
sgn(cross(l[1] - l[0], s[1] - l[0]));
return strict ? a == -1 // strict
: a <= 0; // not strict
}
P pLL(CR<L> l, CR<L> m) {
ld a = cross(l[1] - l[0], m[1] - m[0]);
ld b = cross(l[1] - l[0], l[1] - m[0]);
assert(sgn(a)); // parallel or same line
return m[0] + b / a * (m[1] - m[0]);
}
G convex_cut(CR<G> g, CR<L> l) {
G q;
REP(i, g.size()) {
if (ccw(l[0], l[1], g[i]) != -1)
q.PB(g[i]);
L s = {g[i], NEXT(g, i)};
if (iLS(l, s, true))
q.PB(pLL(l, s));
}
return q;
}
ld area2(CR<G> g) {
ld s = 0;
REP(i, g.size()) s += cross(g[i], NEXT(g, i));
return s;
}
L half_convex(CR<G> g, P p, ld lower, ld upper, ll max_loop) {
ld ans = bsearch(lower, upper, area2(g) / 2, max_loop, [&](ld x) {
return area2(convex_cut(g, {p, p + polar<ld>(1, x)}));
});
return {p, p + polar<ld>(max_loop, ans)};
}
int main() {
#ifdef _WIN32
ifstream cin("sample.in");
ofstream cout("sample.out");
#endif
cin.tie(0); // cin??¨cout?????£????????????
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(50);
while (true) {
int n;
cin >> n;
if (n == 0)
break;
G g(n);
REP(i, n) {
ld x, y;
cin >> x >> y;
g[i].real(x);
g[i].imag(y);
}
vector<ld> angles;
angles.push_back(0);
angles.push_back(acos(-1));
REP(i, n) {
ld angle = arg(g[i]);
if (angle < 0)
angle += acos(-1);
angles.push_back(angle);
}
sort(ALL(angles));
REP(i, angles.size() - 1) {
L l = half_convex(g, 0, angles[i], angles[i + 1], 100);
G cc = convex_cut(g, l);
if (!sgn(area2(g) / 2, area2(cc))) {
cout << l[1].X << " " << l[1].Y << endl;
break;
}
}
}
return 0;
} | replace | 152 | 153 | 152 | 153 | TLE | |
p00675 | C++ | Time Limit Exceeded | #define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define FOREACH(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define TIMES(i, n) for (int i = 0; i < (n); ++i)
typedef long long Cost;
const long long INF = 1000 * 1000 * 10 * 10 * 2 * 10LL;
struct Edge {
int from, to;
Cost cost;
Edge() {}
Edge(int f, int t, Cost c) : from(f), to(t), cost(c) {}
};
struct DistTag {
int size;
deque<Cost> buf, newbie;
DistTag(int size) : size(size), buf(size, INF) {
// newbie.reserve(size);
}
bool insert(Cost val) {
if (buf.back() <= val)
return false;
deque<Cost>::iterator ins_pos =
upper_bound(newbie.begin(), newbie.end(), val);
newbie.insert(ins_pos, val);
if (static_cast<int>(newbie.size()) > size)
newbie.pop_back();
return true;
}
bool has(Cost val) { return buf.back() >= val; }
void flush() {
FOREACH(it, newbie) {
deque<Cost>::iterator ins_pos = upper_bound(buf.begin(), buf.end(), *it);
buf.insert(ins_pos, *it);
}
buf.resize(size);
newbie.clear();
}
};
struct State {
int pos;
Cost cost;
State() {}
State(int p, Cost c) : pos(p), cost(c) {}
bool operator<(const State &other) const {
return cost > other.cost; // Reversed
}
};
typedef vector<Edge> EdgeList;
void check_reachable(int start, const vector<EdgeList> &graph,
vector<unsigned char> &reachable) {
queue<int> q;
q.push(start);
reachable[start] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
FOREACH(it, graph[cur]) {
if (!reachable[it->to]) {
reachable[it->to] = 1;
q.push(it->to);
}
}
}
}
bool relax(const vector<EdgeList> &graph, vector<Cost> &dist) {
bool updated = false;
FOREACH(list_it, graph) {
FOREACH(it, *list_it) {
const Cost cost = dist[it->from] + it->cost;
if (dist[it->to] > cost) {
dist[it->to] = cost;
updated = true;
}
}
}
return updated;
}
pair<int, Cost> search(int start, const vector<EdgeList> &graph, int N, int P,
int K) {
vector<DistTag> dist(graph.size(), DistTag(K));
dist[start].insert(0);
TIMES(_, static_cast<int>(graph.size()) * K) {
TIMES(i, static_cast<int>(graph.size())) {
DistTag &dt = dist[i];
if (dt.newbie.size() == 0)
continue;
vector<int> tmp;
FOREACH(it, graph[i]) {
assert(it->from == i);
// Corner case: loop edge
if (it->to == i) {
FOREACH(nit, dt.newbie) { tmp.push_back(*nit + it->cost); }
} else {
FOREACH(nit, dt.newbie) {
// cout << "insert " << i << ' ' << it->to << endl;
dist[it->to].insert(*nit + it->cost);
}
}
}
dt.flush();
FOREACH(tit, tmp) { dt.insert(*tit); }
}
}
pair<int, Cost> res(0, 0);
vector<Cost> costs;
TIMES(i, P) {
FOREACH(it, dist[(N - 1) * P + i].buf) { costs.push_back(*it); }
}
sort(costs.begin(), costs.end());
TIMES(i, min(K, static_cast<int>(costs.size()))) {
if (costs[i] == INF)
break;
res.first++;
res.second += costs[i];
}
return res;
}
pair<int, Cost> solve(int N) {
vector<int> color_of(N);
TIMES(i, N) {
cin >> color_of[i];
--color_of[i];
}
int M;
cin >> M;
vector<Edge> edges(M);
TIMES(i, M) {
Edge &e = edges[i];
cin >> e.from >> e.to >> e.cost;
--e.from;
--e.to;
}
int K;
cin >> K;
vector<int> prohibited;
{
string str;
cin >> str;
FOREACH(it, str) { prohibited.push_back(*it - '0' - 1); }
}
const int P = prohibited.size();
vector<vector<int>> next_prohibited_pos(P, vector<int>(4, 0));
{
int shadow = 0;
TIMES(i, P) {
next_prohibited_pos[i][prohibited[i]] = i + 1;
if (i > 0) {
TIMES(j, 4) {
if (j != prohibited[i])
next_prohibited_pos[i][j] = next_prohibited_pos[shadow][j];
}
shadow = next_prohibited_pos[shadow][prohibited[i]];
}
}
}
const int start = 0 + next_prohibited_pos[0][color_of[0]];
vector<EdgeList> graph(N * P);
FOREACH(it, edges) {
TIMES(i, P) {
// Cannot reach this state.
if (i > 0 && prohibited[i - 1] != color_of[it->from])
continue;
int np = next_prohibited_pos[i][color_of[it->to]];
if (np < P) {
graph[it->from * P + i].push_back(
Edge(it->from * P + i, it->to * P + np, it->cost));
// cout << it->from << ' ' << i << ' ' << it->to << ' ' << np << endl;
}
}
}
// Remove all unreachable nodes.
// It causes fake negative loop detection.
vector<unsigned char> reachable(N * P, 0);
check_reachable(start, graph, reachable);
TIMES(i, N * P) {
if (!reachable[i])
graph[i].clear();
}
// Check if it has negative loop
vector<Cost> dist(N * P, INF);
dist[start] = 0;
TIMES(_, N * P) { relax(graph, dist); }
vector<Cost> tmp_last(P);
TIMES(i, P) { tmp_last[i] = dist[(N - 1) * P + i]; }
TIMES(_, N * P) {
relax(graph, dist);
TIMES(i, P) {
if (dist[(N - 1) * P + i] != tmp_last[i])
return make_pair(-1, -1);
}
}
// Corner case: cannot start
if (P == 1 && prohibited[0] == color_of[0])
return make_pair(0, 0);
pair<int, Cost> res = search(start, graph, N, P, K);
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
while (cin >> N) {
if (!N)
break;
pair<int, Cost> ans = solve(N);
if (ans.first == -1)
cout << -1 << endl;
else
cout << ans.first << ' ' << ans.second << endl;
}
return 0;
} | // #define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define FOREACH(it, c) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define TIMES(i, n) for (int i = 0; i < (n); ++i)
typedef long long Cost;
const long long INF = 1000 * 1000 * 10 * 10 * 2 * 10LL;
struct Edge {
int from, to;
Cost cost;
Edge() {}
Edge(int f, int t, Cost c) : from(f), to(t), cost(c) {}
};
struct DistTag {
int size;
deque<Cost> buf, newbie;
DistTag(int size) : size(size), buf(size, INF) {
// newbie.reserve(size);
}
bool insert(Cost val) {
if (buf.back() <= val)
return false;
deque<Cost>::iterator ins_pos =
upper_bound(newbie.begin(), newbie.end(), val);
newbie.insert(ins_pos, val);
if (static_cast<int>(newbie.size()) > size)
newbie.pop_back();
return true;
}
bool has(Cost val) { return buf.back() >= val; }
void flush() {
FOREACH(it, newbie) {
deque<Cost>::iterator ins_pos = upper_bound(buf.begin(), buf.end(), *it);
buf.insert(ins_pos, *it);
}
buf.resize(size);
newbie.clear();
}
};
struct State {
int pos;
Cost cost;
State() {}
State(int p, Cost c) : pos(p), cost(c) {}
bool operator<(const State &other) const {
return cost > other.cost; // Reversed
}
};
typedef vector<Edge> EdgeList;
void check_reachable(int start, const vector<EdgeList> &graph,
vector<unsigned char> &reachable) {
queue<int> q;
q.push(start);
reachable[start] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
FOREACH(it, graph[cur]) {
if (!reachable[it->to]) {
reachable[it->to] = 1;
q.push(it->to);
}
}
}
}
bool relax(const vector<EdgeList> &graph, vector<Cost> &dist) {
bool updated = false;
FOREACH(list_it, graph) {
FOREACH(it, *list_it) {
const Cost cost = dist[it->from] + it->cost;
if (dist[it->to] > cost) {
dist[it->to] = cost;
updated = true;
}
}
}
return updated;
}
pair<int, Cost> search(int start, const vector<EdgeList> &graph, int N, int P,
int K) {
vector<DistTag> dist(graph.size(), DistTag(K));
dist[start].insert(0);
TIMES(_, static_cast<int>(graph.size()) * K) {
TIMES(i, static_cast<int>(graph.size())) {
DistTag &dt = dist[i];
if (dt.newbie.size() == 0)
continue;
vector<int> tmp;
FOREACH(it, graph[i]) {
assert(it->from == i);
// Corner case: loop edge
if (it->to == i) {
FOREACH(nit, dt.newbie) { tmp.push_back(*nit + it->cost); }
} else {
FOREACH(nit, dt.newbie) {
// cout << "insert " << i << ' ' << it->to << endl;
dist[it->to].insert(*nit + it->cost);
}
}
}
dt.flush();
FOREACH(tit, tmp) { dt.insert(*tit); }
}
}
pair<int, Cost> res(0, 0);
vector<Cost> costs;
TIMES(i, P) {
FOREACH(it, dist[(N - 1) * P + i].buf) { costs.push_back(*it); }
}
sort(costs.begin(), costs.end());
TIMES(i, min(K, static_cast<int>(costs.size()))) {
if (costs[i] == INF)
break;
res.first++;
res.second += costs[i];
}
return res;
}
pair<int, Cost> solve(int N) {
vector<int> color_of(N);
TIMES(i, N) {
cin >> color_of[i];
--color_of[i];
}
int M;
cin >> M;
vector<Edge> edges(M);
TIMES(i, M) {
Edge &e = edges[i];
cin >> e.from >> e.to >> e.cost;
--e.from;
--e.to;
}
int K;
cin >> K;
vector<int> prohibited;
{
string str;
cin >> str;
FOREACH(it, str) { prohibited.push_back(*it - '0' - 1); }
}
const int P = prohibited.size();
vector<vector<int>> next_prohibited_pos(P, vector<int>(4, 0));
{
int shadow = 0;
TIMES(i, P) {
next_prohibited_pos[i][prohibited[i]] = i + 1;
if (i > 0) {
TIMES(j, 4) {
if (j != prohibited[i])
next_prohibited_pos[i][j] = next_prohibited_pos[shadow][j];
}
shadow = next_prohibited_pos[shadow][prohibited[i]];
}
}
}
const int start = 0 + next_prohibited_pos[0][color_of[0]];
vector<EdgeList> graph(N * P);
FOREACH(it, edges) {
TIMES(i, P) {
// Cannot reach this state.
if (i > 0 && prohibited[i - 1] != color_of[it->from])
continue;
int np = next_prohibited_pos[i][color_of[it->to]];
if (np < P) {
graph[it->from * P + i].push_back(
Edge(it->from * P + i, it->to * P + np, it->cost));
// cout << it->from << ' ' << i << ' ' << it->to << ' ' << np << endl;
}
}
}
// Remove all unreachable nodes.
// It causes fake negative loop detection.
vector<unsigned char> reachable(N * P, 0);
check_reachable(start, graph, reachable);
TIMES(i, N * P) {
if (!reachable[i])
graph[i].clear();
}
// Check if it has negative loop
vector<Cost> dist(N * P, INF);
dist[start] = 0;
TIMES(_, N * P) { relax(graph, dist); }
vector<Cost> tmp_last(P);
TIMES(i, P) { tmp_last[i] = dist[(N - 1) * P + i]; }
TIMES(_, N * P) {
relax(graph, dist);
TIMES(i, P) {
if (dist[(N - 1) * P + i] != tmp_last[i])
return make_pair(-1, -1);
}
}
// Corner case: cannot start
if (P == 1 && prohibited[0] == color_of[0])
return make_pair(0, 0);
pair<int, Cost> res = search(start, graph, N, P, K);
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
while (cin >> N) {
if (!N)
break;
pair<int, Cost> ans = solve(N);
if (ans.first == -1)
cout << -1 << endl;
else
cout << ans.first << ' ' << ans.second << endl;
}
return 0;
} | replace | 0 | 1 | 0 | 1 | TLE | |
p00676 | C++ | Runtime Error | #include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
double calc(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
void func(double a, double l, double x) {
double tmp = (l + x) / 2;
printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2);
}
int main() {
double a, l, x;
while (scanf("%lf %lf %lf", a, l, x) != EOF) {
func(a, l, x);
}
} | #include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
double calc(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
void func(double a, double l, double x) {
double tmp = (l + x) / 2;
printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2);
}
int main() {
double a, l, x;
while (scanf("%lf %lf %lf", &a, &l, &x) != EOF) {
func(a, l, x);
}
} | replace | 32 | 33 | 32 | 33 | -11 | |
p00676 | 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 main() {
double a, l, x;
while (~scanf("%lf %lf %lf", &a, &l, &x)) {
double s = (a + l + l) / 2.0;
double ca = sqrt(s * (s - a) * (s - l) * (s - l));
double res = 0.0;
for (double scale = 0.000001; scale < 1.0; scale += 0.000001) {
double e1 = scale * (l + x);
double e2 = (1.0 - scale) * (l + x);
double s2 = (e1 + e2 + l) / 2.0;
if (s2 * (s2 - e1) * (s2 - e2) * (s2 - l) < 0)
continue;
double ca2 = sqrt(s2 * (s2 - e1) * (s2 - e2) * (s2 - l));
res = max(ca + ca2 + ca2, res);
}
printf("%lf\n", res);
}
} | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};
static const double EPS = 1e-8;
int main() {
double a, l, x;
while (~scanf("%lf %lf %lf", &a, &l, &x)) {
double s = (a + l + l) / 2.0;
double ca = sqrt(s * (s - a) * (s - l) * (s - l));
double res = 0.0;
for (double scale = 0.00001; scale < 1.0; scale += 0.00001) {
double e1 = scale * (l + x);
double e2 = (1.0 - scale) * (l + x);
double s2 = (e1 + e2 + l) / 2.0;
if (s2 * (s2 - e1) * (s2 - e2) * (s2 - l) < 0)
continue;
double ca2 = sqrt(s2 * (s2 - e1) * (s2 - e2) * (s2 - l));
res = max(ca + ca2 + ca2, res);
}
printf("%lf\n", res);
}
} | replace | 41 | 42 | 41 | 42 | TLE | |
p00676 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
double a, l, x;
while (~scanf("%lf%lf%lf", &a, &l, &x), a) {
double s1 = (a + l + l) / 2, s2 = (l + l + x) / 2;
double d = (l + x) / 2;
printf("%.10lf\n", sqrt(s1 * (s1 - a) * (s1 - l) * (s1 - l)) +
sqrt(s2 * (s2 - d) * (s2 - d) * (s2 - l)) * 2);
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
double a, l, x;
while (~scanf("%lf%lf%lf", &a, &l, &x)) {
double s1 = (a + l + l) / 2, s2 = (l + l + x) / 2;
double d = (l + x) / 2;
printf("%.10lf\n", sqrt(s1 * (s1 - a) * (s1 - l) * (s1 - l)) +
sqrt(s2 * (s2 - d) * (s2 - d) * (s2 - l)) * 2);
}
} | replace | 6 | 7 | 6 | 7 | TLE | |
p00676 | C++ | Runtime Error | #include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int a, l, x, i = 0;
double M[1000], c, d, e, f;
while (scanf("%d%d%d", &a, &l, &x) != EOF) {
c = (double)((2 * x * l) + (x * x)) / 4;
e = l * l - (double)(a * a) / 4;
d = sqrt(c);
f = sqrt(e);
M[i] = ((double)a / 2) * f + l * d;
i++;
}
for (int j = 0; j < i; j++) {
printf("%.10f\n", M[j]);
}
return 0;
} | #include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int a, l, x, i = 0;
double M[100000], c, d, e, f;
while (scanf("%d%d%d", &a, &l, &x) != EOF) {
c = (double)((2 * x * l) + (x * x)) / 4;
e = l * l - (double)(a * a) / 4;
d = sqrt(c);
f = sqrt(e);
M[i] = ((double)a / 2) * f + l * d;
i++;
}
for (int j = 0; j < i; j++) {
printf("%.10f\n", M[j]);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p00677 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
void knapsack(int yosan, const vector<pair<int, int>> &a) {
int n = a.size();
int dp[101] = {};
rep(i, n) {
int value = a[i].first, price = a[i].second;
for (int j = yosan; j >= price; j--)
dp[j] = max(dp[j], dp[j - price] + value);
}
int ans = max_element(dp, dp + yosan + 1) - dp;
printf("%d %d\n", dp[ans], ans);
}
int main() {
for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) {
vector<pair<int, int>> kanmi[100];
rep(i, n) {
int m;
scanf("%d", &m);
kanmi[i].resize(m);
rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second);
}
vector<pair<int, int>> item;
rep(t, days) {
int id;
scanf("%d", &id);
item.insert(item.end(), kanmi[id].begin(), kanmi[id].end());
}
knapsack(yosan, item);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
void knapsack(int yosan, const vector<pair<int, int>> &a) {
int n = a.size();
int dp[301] = {};
rep(i, n) {
int value = a[i].first, price = a[i].second;
for (int j = yosan; j >= price; j--)
dp[j] = max(dp[j], dp[j - price] + value);
}
int ans = max_element(dp, dp + yosan + 1) - dp;
printf("%d %d\n", dp[ans], ans);
}
int main() {
for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) {
vector<pair<int, int>> kanmi[100];
rep(i, n) {
int m;
scanf("%d", &m);
kanmi[i].resize(m);
rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second);
}
vector<pair<int, int>> item;
rep(t, days) {
int id;
scanf("%d", &id);
item.insert(item.end(), kanmi[id].begin(), kanmi[id].end());
}
knapsack(yosan, item);
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00678 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
double EPS = 1e-10;
double add(double a, double b) {
if (abs(a + b) < EPS * (abs(a) + abs(b)))
return 0;
return a + b;
}
struct point {
double x, y;
point() {}
point(double x, double y) : x(x), y(y) {}
point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
point operator*(double d) { return point(x * d, y * d); }
point operator/(double d) { return point(x / d, y / d); }
};
double dist(point a, point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
int main(void) {
int n, x, y, v;
vector<point> P;
vector<double> V;
while (cin >> n, n) {
P.clear();
V.clear();
for (int i = 0; i < n; i++) {
cin >> x >> y >> v;
P.push_back(point(x, y));
V.push_back(v);
}
double r = 0.99999;
point now(0, 0);
for (double d = 10; d > EPS; d *= r) {
int mx = 0;
double mxd = 0;
for (int j = 0; j < P.size(); j++) {
if (dist(now, P[j]) / V[j] > mxd)
mx = j, mxd = dist(now, P[j]) / V[j];
}
now = now + ((P[mx] - now) / dist(P[mx], now) * d);
}
double ans = 0;
for (int i = 0; i < P.size(); i++) {
ans = max(ans, dist(now, P[i]) / V[i]);
}
printf("%.8f\n", ans);
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
double EPS = 1e-10;
double add(double a, double b) {
if (abs(a + b) < EPS * (abs(a) + abs(b)))
return 0;
return a + b;
}
struct point {
double x, y;
point() {}
point(double x, double y) : x(x), y(y) {}
point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
point operator*(double d) { return point(x * d, y * d); }
point operator/(double d) { return point(x / d, y / d); }
};
double dist(point a, point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
int main(void) {
int n, x, y, v;
vector<point> P;
vector<double> V;
while (cin >> n, n) {
P.clear();
V.clear();
for (int i = 0; i < n; i++) {
cin >> x >> y >> v;
P.push_back(point(x, y));
V.push_back(v);
}
double r = 0.999;
point now(0, 0);
for (double d = 10; d > EPS; d *= r) {
int mx = 0;
double mxd = 0;
for (int j = 0; j < P.size(); j++) {
if (dist(now, P[j]) / V[j] > mxd)
mx = j, mxd = dist(now, P[j]) / V[j];
}
now = now + ((P[mx] - now) / dist(P[mx], now) * d);
}
double ans = 0;
for (int i = 0; i < P.size(); i++) {
ans = max(ans, dist(now, P[i]) / V[i]);
}
printf("%.8f\n", ans);
}
return 0;
} | replace | 45 | 46 | 45 | 46 | TLE | |
p00680 | C++ | Time Limit Exceeded | #include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 110
#define MAXM 30000
#define wint int
#define cint long double
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 1e-7L;
const cint cINF = 1e10L;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
n = _n;
m = 0;
memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
next[m] = ptr[u];
ptr[u] = m;
zu[m] = v;
capa[m] = w;
cost[m] = +c;
++m;
next[m] = ptr[v];
ptr[v] = m;
zu[m] = u;
capa[m] = 0;
cost[m] = -c;
++m;
}
bool solve(int src, int ink, wint flo = wINF) {
int i, u, v;
wint f;
cint c, cc;
memset(pot, 0, n * sizeof(cint));
//*
for (bool cont = 1; cont;) {
cont = 0;
for (u = 0; u < n; ++u)
for (i = ptr[u]; ~i; i = next[i])
if (capa[i] > wEPS) {
if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
pot[zu[i]] = pot[u] + cost[i];
cont = 1;
}
}
}
//*/
for (toc = 0, tof = 0; tof + wEPS < flo;) {
typedef pair<cint, int> node;
priority_queue<node, vector<node>, greater<node>> q;
for (u = 0; u < n; ++u) {
d[u] = cINF;
vis[u] = 0;
}
for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
c = q.top().first;
u = q.top().second;
q.pop();
if (vis[u]++)
continue;
for (i = ptr[u]; ~i; i = next[i])
if (capa[i] > wEPS) {
cc = c + cost[i] + pot[u] - pot[v = zu[i]];
if (d[v] > cc) {
q.push(make_pair(d[v] = cc, v));
pree[v] = i;
}
}
}
if (!vis[ink])
return 0;
f = flo - tof;
for (v = ink; v != src; v = zu[i ^ 1]) {
i = pree[v];
f = min(f, capa[i]);
}
for (v = ink; v != src; v = zu[i ^ 1]) {
i = pree[v];
capa[i] -= f;
capa[i ^ 1] += f;
}
tof += f;
toc += f * (d[ink] - pot[src] + pot[ink]);
for (u = 0; u < n; ++u)
pot[u] += d[u];
}
return 1;
}
} // namespace MCF
const long double EPS = 1e-10L;
typedef vector<long double> vec;
typedef vector<vec> mat;
long double ABS(long double a) { return max(a, -a); }
vec gauss_jordan(const mat &A, const vec &b) {
int n = A.size();
mat B(n, vec(n + 1));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
B[i][j] = A[i][j];
for (int i = 0; i < n; i++)
B[i][n] = b[i];
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (ABS(B[j][i]) > ABS(B[pivot][i]))
pivot = j;
}
swap(B[i], B[pivot]);
if (ABS(B[i][i]) < EPS)
return vec();
for (int j = i + 1; j <= n; j++)
B[i][j] /= B[i][i];
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++)
B[j][k] -= B[j][i] * B[i][k];
}
}
}
vec x(n);
for (int i = 0; i < n; i++)
x[i] = B[i][n];
return x;
}
int t1[110];
int t2[110];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
mat A(a, vec(a));
vec B(a);
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
scanf("%Lf", &A[i][j]);
}
scanf("%Lf", &B[i]);
}
vec C = gauss_jordan(A, B);
// s for(int i=0;i<a;i++)printf("%Lf ",C[i]);
MCF::init(a);
for (int i = 0; i < a; i++) {
int p;
scanf("%d", &p);
for (int j = 0; j < p; j++) {
scanf("%d", t1 + j);
}
for (int j = 0; j < p; j++)
scanf("%d", t2 + j);
for (int j = 0; j < p; j++)
MCF::ae(i, t1[j], t2[j], ABS(C[i] - C[t1[j]]));
}
int res = MCF::solve(b, c, d);
if (!res)
printf("impossible\n");
else
printf("%.12Lf\n", MCF::toc);
}
} | #include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 110
#define MAXM 30000
#define wint int
#define cint long double
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 1e-7L;
const cint cINF = 1e10L;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
n = _n;
m = 0;
memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
next[m] = ptr[u];
ptr[u] = m;
zu[m] = v;
capa[m] = w;
cost[m] = +c;
++m;
next[m] = ptr[v];
ptr[v] = m;
zu[m] = u;
capa[m] = 0;
cost[m] = -c;
++m;
}
bool solve(int src, int ink, wint flo = wINF) {
int i, u, v;
wint f;
cint c, cc;
memset(pot, 0, n * sizeof(cint));
//*
for (bool cont = 1; cont;) {
cont = 0;
for (u = 0; u < n; ++u)
for (i = ptr[u]; ~i; i = next[i])
if (capa[i] > wEPS) {
if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
pot[zu[i]] = pot[u] + cost[i];
cont = 1;
}
}
}
//*/
for (toc = 0, tof = 0; tof + wEPS < flo;) {
typedef pair<cint, int> node;
priority_queue<node, vector<node>, greater<node>> q;
for (u = 0; u < n; ++u) {
d[u] = cINF;
vis[u] = 0;
}
for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
c = q.top().first;
u = q.top().second;
q.pop();
if (vis[u]++)
continue;
for (i = ptr[u]; ~i; i = next[i])
if (capa[i] > wEPS) {
cc = c + cost[i] + pot[u] - pot[v = zu[i]];
if (d[v] > cc + cEPS) {
q.push(make_pair(d[v] = cc, v));
pree[v] = i;
}
}
}
if (!vis[ink])
return 0;
f = flo - tof;
for (v = ink; v != src; v = zu[i ^ 1]) {
i = pree[v];
f = min(f, capa[i]);
}
for (v = ink; v != src; v = zu[i ^ 1]) {
i = pree[v];
capa[i] -= f;
capa[i ^ 1] += f;
}
tof += f;
toc += f * (d[ink] - pot[src] + pot[ink]);
for (u = 0; u < n; ++u)
pot[u] += d[u];
}
return 1;
}
} // namespace MCF
const long double EPS = 1e-10L;
typedef vector<long double> vec;
typedef vector<vec> mat;
long double ABS(long double a) { return max(a, -a); }
vec gauss_jordan(const mat &A, const vec &b) {
int n = A.size();
mat B(n, vec(n + 1));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
B[i][j] = A[i][j];
for (int i = 0; i < n; i++)
B[i][n] = b[i];
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (ABS(B[j][i]) > ABS(B[pivot][i]))
pivot = j;
}
swap(B[i], B[pivot]);
if (ABS(B[i][i]) < EPS)
return vec();
for (int j = i + 1; j <= n; j++)
B[i][j] /= B[i][i];
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++)
B[j][k] -= B[j][i] * B[i][k];
}
}
}
vec x(n);
for (int i = 0; i < n; i++)
x[i] = B[i][n];
return x;
}
int t1[110];
int t2[110];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
mat A(a, vec(a));
vec B(a);
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
scanf("%Lf", &A[i][j]);
}
scanf("%Lf", &B[i]);
}
vec C = gauss_jordan(A, B);
// s for(int i=0;i<a;i++)printf("%Lf ",C[i]);
MCF::init(a);
for (int i = 0; i < a; i++) {
int p;
scanf("%d", &p);
for (int j = 0; j < p; j++) {
scanf("%d", t1 + j);
}
for (int j = 0; j < p; j++)
scanf("%d", t2 + j);
for (int j = 0; j < p; j++)
MCF::ae(i, t1[j], t2[j], ABS(C[i] - C[t1[j]]));
}
int res = MCF::solve(b, c, d);
if (!res)
printf("impossible\n");
else
printf("%.12Lf\n", MCF::toc);
}
} | replace | 73 | 74 | 73 | 74 | TLE | |
p00680 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define MAX_V 300
#define INF 1e9
const double EPS = 1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;
typedef pair<double, int> P;
struct edge {
int to, cap, rev;
double cost;
edge(int to, int cap, int rev, double cost)
: to(to), cap(cap), rev(rev), cost(cost) {}
};
int V;
vector<edge> G[MAX_V];
double dist[MAX_V], h[MAX_V];
;
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, double cost) {
G[from].push_back(edge(to, cap, G[to].size(), cost));
G[to].push_back(edge(from, 0, G[from].size() - 1, -cost));
}
double min_cost_flow(int s, int t, int f) {
double res = 0;
fill(h, h + V, 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> Q;
fill(dist, dist + V, INF);
dist[s] = 0;
Q.push(P(0, s));
while (!Q.empty()) {
P p = Q.top();
Q.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (int i = 0; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
Q.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
return -1;
}
for (int v = 0; v < V; v++) {
h[v] += dist[v];
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
vec gauss_jordan(const mat &A, const vec &b) {
int n = A.size();
mat B(n, vec(n + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
B[i][j] = A[i][j];
}
}
for (int i = 0; i < n; i++) {
B[i][n] = b[i];
}
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (abs(B[j][i]) > abs(B[pivot][i])) {
pivot = j;
}
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS)
return vec();
for (int j = i + 1; j <= n; j++) {
B[i][j] /= B[i][i];
}
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++) {
B[j][k] -= B[j][i] * B[i][k];
}
}
}
}
vec x(n);
for (int i = 0; i < n; i++) {
x[i] = B[i][n];
}
return x;
}
void init() {
for (int i = 0; i < MAX_V; i++) {
G[i].clear();
}
}
int main() {
int Tc;
cin >> Tc;
while (Tc--) {
int N, s, t, F;
init();
cin >> N >> s >> t >> F;
mat a(N, vec(N));
vec c(N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
}
cin >> c[i];
}
vec res = gauss_jordan(a, c);
for (int i = 0; i < N; i++) {
int M, d[N], f;
cin >> M;
for (int j = 0; j < M; j++) {
cin >> d[j];
}
for (int j = 0; j < M; j++) {
cin >> f;
add_edge(i, d[j], f, abs(res[i] - res[d[j]]));
}
}
V = N;
double ans = min_cost_flow(s, t, F);
if (ans == -1) {
printf("impossible\n");
} else {
printf("%.6f\n", ans);
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX_V 300
#define INF 1e9
const double EPS = 1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;
typedef pair<double, int> P;
struct edge {
int to, cap, rev;
double cost;
edge(int to, int cap, int rev, double cost)
: to(to), cap(cap), rev(rev), cost(cost) {}
};
int V;
vector<edge> G[MAX_V];
double dist[MAX_V], h[MAX_V];
;
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, double cost) {
G[from].push_back(edge(to, cap, G[to].size(), cost));
G[to].push_back(edge(from, 0, G[from].size() - 1, -cost));
}
double min_cost_flow(int s, int t, int f) {
double res = 0;
fill(h, h + V, 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> Q;
fill(dist, dist + V, INF);
dist[s] = 0;
Q.push(P(0, s));
while (!Q.empty()) {
P p = Q.top();
Q.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (int i = 0; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to] + EPS) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
Q.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF) {
return -1;
}
for (int v = 0; v < V; v++) {
h[v] += dist[v];
}
int d = f;
for (int v = t; v != s; v = prevv[v]) {
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
vec gauss_jordan(const mat &A, const vec &b) {
int n = A.size();
mat B(n, vec(n + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
B[i][j] = A[i][j];
}
}
for (int i = 0; i < n; i++) {
B[i][n] = b[i];
}
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (abs(B[j][i]) > abs(B[pivot][i])) {
pivot = j;
}
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS)
return vec();
for (int j = i + 1; j <= n; j++) {
B[i][j] /= B[i][i];
}
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++) {
B[j][k] -= B[j][i] * B[i][k];
}
}
}
}
vec x(n);
for (int i = 0; i < n; i++) {
x[i] = B[i][n];
}
return x;
}
void init() {
for (int i = 0; i < MAX_V; i++) {
G[i].clear();
}
}
int main() {
int Tc;
cin >> Tc;
while (Tc--) {
int N, s, t, F;
init();
cin >> N >> s >> t >> F;
mat a(N, vec(N));
vec c(N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
}
cin >> c[i];
}
vec res = gauss_jordan(a, c);
for (int i = 0; i < N; i++) {
int M, d[N], f;
cin >> M;
for (int j = 0; j < M; j++) {
cin >> d[j];
}
for (int j = 0; j < M; j++) {
cin >> f;
add_edge(i, d[j], f, abs(res[i] - res[d[j]]));
}
}
V = N;
double ans = min_cost_flow(s, t, F);
if (ans == -1) {
printf("impossible\n");
} else {
printf("%.6f\n", ans);
}
}
return 0;
} | replace | 45 | 46 | 45 | 46 | TLE | |
p00686 | C++ | Time Limit Exceeded | #include <iostream>
#include <string>
using namespace std;
int main() {
int w, h;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
string a;
int s = 0;
int x = 1, y = 1, z;
while (true) {
cin >> a;
if (a == "STOP")
break;
if (a == "RIGHT")
s = (s + 1) % 4;
if (a == "LEFT")
s = (s + 3) % 4;
if (a == "FORWARD") {
cin >> z;
x += dx[s] * z, y += dy[s] * z;
if (x < 1)
x = 1;
if (x > w)
x = w;
if (y < 1)
y = 1;
if (y > h)
y = h;
}
if (a == "BACKWARD") {
cin >> z;
x -= dx[s] * z, y -= dy[s] * z;
if (x < 1)
x = 1;
if (x > w)
x = w;
if (y < 1)
y = 1;
if (y > h)
y = h;
}
}
cout << x << " " << y << endl;
}
while (true) {
cin >> w;
}
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
int w, h;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
string a;
int s = 0;
int x = 1, y = 1, z;
while (true) {
cin >> a;
if (a == "STOP")
break;
if (a == "RIGHT")
s = (s + 1) % 4;
if (a == "LEFT")
s = (s + 3) % 4;
if (a == "FORWARD") {
cin >> z;
x += dx[s] * z, y += dy[s] * z;
if (x < 1)
x = 1;
if (x > w)
x = w;
if (y < 1)
y = 1;
if (y > h)
y = h;
}
if (a == "BACKWARD") {
cin >> z;
x -= dx[s] * z, y -= dy[s] * z;
if (x < 1)
x = 1;
if (x > w)
x = w;
if (y < 1)
y = 1;
if (y > h)
y = h;
}
}
cout << x << " " << y << endl;
}
return 0;
} | delete | 48 | 51 | 48 | 48 | TLE | |
p00687 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
#define loop(n, i) for (int i = 0; i < n; i++)
#define loop_from_to(from, to, i) for (int i = from; i <= to; i++)
#define repeat(n) for (int i__ = 0; i__ < n; i__++)
#define multi_cin(n, array) \
loop(n, i) { cin >> array[i]; }
#define assign_if_smaller(var, val) \
if (var > val) \
var = val;
#define assign_if_larger(var, val) \
if (var < val) \
var = val;
#define each(itr, c) \
for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define r_each(itr, c) \
for (__typeof((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); itr++)
using namespace std;
int main(int argc, char const *argv[]) {
while (1) {
int n, a, b;
cin >> n >> a >> b;
if (!n && !a && !b)
break;
bool c[n];
fill(c, c + n, false);
c[a - 1] = true;
c[b - 1] = true;
int count = 0;
loop(n, i) {
if (!c[i])
count++;
else {
if (i + a < n)
c[i + a] = true;
if (i + b < n)
c[i + b] = true;
}
}
cout << count << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
#define loop(n, i) for (int i = 0; i < n; i++)
#define loop_from_to(from, to, i) for (int i = from; i <= to; i++)
#define repeat(n) for (int i__ = 0; i__ < n; i__++)
#define multi_cin(n, array) \
loop(n, i) { cin >> array[i]; }
#define assign_if_smaller(var, val) \
if (var > val) \
var = val;
#define assign_if_larger(var, val) \
if (var < val) \
var = val;
#define each(itr, c) \
for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define r_each(itr, c) \
for (__typeof((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); itr++)
using namespace std;
int main(int argc, char const *argv[]) {
while (1) {
int n, a, b;
cin >> n >> a >> b;
if (!n && !a && !b)
break;
bool c[n];
fill(c, c + n, false);
if (a - 1 < n)
c[a - 1] = true;
if (b - 1 < n)
c[b - 1] = true;
int count = 0;
loop(n, i) {
if (!c[i])
count++;
else {
if (i + a < n)
c[i + a] = true;
if (i + b < n)
c[i + b] = true;
}
}
cout << count << endl;
}
return 0;
} | replace | 31 | 33 | 31 | 35 | 0 | |
p00687 | C++ | Time Limit Exceeded | #include <cstdio>
#include <cstring>
using namespace std;
char c[1000001] = {0};
int main() {
for (;;) {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
if (n == 0 && a == 0 && b == 0)
break;
memset(c, 0, sizeof(c));
for (int i = 0; a * i <= n; ++i) {
for (int j = 0; a * i + b * j <= n; ++j) {
c[a * i + b * j] = 1;
// printf(" %d, %d\n", i, j);
}
}
int t = 0;
for (int i = 1; i <= n; ++i)
if (c[i] == 0)
++t;
printf("%d\n", t);
}
return 0;
} | #include <cstdio>
#include <cstring>
using namespace std;
char c[1000001] = {0};
int main() {
for (;;) {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
if (n == 0 && a == 0 && b == 0)
break;
memset(c, 0, sizeof(c));
c[0] = 1;
for (int i = 0; i <= n; ++i) {
if (c[i] == 1) {
if (i + a <= n)
c[i + a] = 1;
if (i + b <= n)
c[i + b] = 1;
}
}
int t = 0;
for (int i = 1; i <= n; ++i)
if (c[i] == 0)
++t;
printf("%d\n", t);
}
return 0;
} | replace | 16 | 20 | 16 | 23 | TLE | |
p00687 | C++ | Runtime Error | // AOJ 1105
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> Seq;
const int NMAX = 1000100;
int solve(int a, int b, int n) {
bool countable[NMAX];
REP(i, n + 1) { countable[i] = false; }
countable[0] = true;
int unables = 0;
REP(i, n + 1) {
if (countable[i]) {
countable[i + a] = true;
countable[i + b] = true;
} else {
unables++;
}
}
return unables;
}
int main() {
int a, b, n;
while (cin >> n >> a >> b and n + a + b) {
cout << solve(a, b, n) << endl;
}
} | // AOJ 1105
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> Seq;
const int NMAX = 2000100;
int solve(int a, int b, int n) {
bool countable[NMAX];
REP(i, n + 1) { countable[i] = false; }
countable[0] = true;
int unables = 0;
REP(i, n + 1) {
if (countable[i]) {
countable[i + a] = true;
countable[i + b] = true;
} else {
unables++;
}
}
return unables;
}
int main() {
int a, b, n;
while (cin >> n >> a >> b and n + a + b) {
cout << solve(a, b, n) << endl;
}
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00687 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n, a, b;
bool t[1000001];
while (cin >> n >> a >> b && (n + a + b)) {
fill(t, t + 1000001, false);
t[0] = true;
for (int i = 0; i <= n; i++) {
if (t[i]) {
t[i + a] = t[i + b] = true;
}
}
a = 0;
for (int i = 1; i <= n; i++) {
if (!t[i]) {
a++;
}
}
cout << a << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n, a, b;
bool t[1000001];
while (cin >> n >> a >> b && (n + a + b)) {
fill(t, t + 1000001, false);
t[0] = true;
for (int i = 0; i <= n; i++) {
if (t[i]) {
if (i + a <= n) {
t[i + a] = true;
}
if (i + b <= n) {
t[i + b] = true;
}
}
}
a = 0;
for (int i = 1; i <= n; i++) {
if (!t[i]) {
a++;
}
}
cout << a << endl;
}
return 0;
} | replace | 13 | 14 | 13 | 19 | 0 | |
p00687 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 100010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;
int gcm(int a, int b) {
if (a < b) {
swap(a, b);
}
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
int lcm(int a, int b) { return a / gcm(a, b) * b; }
int main() {
int n, a, b;
int LCM, GCM;
bool flag[MAX_N];
int count[MAX_N];
while (1) {
cin >> n >> a >> b;
if (n == 0)
break;
rep(i, MAX_N) { flag[i] = 0; }
LCM = lcm(a, b);
GCM = gcm(a, b);
int ans;
int irange = min(MAX_N - 1, LCM) / a + 1;
rep(i, irange) {
int jrange = (min(MAX_N - 1, LCM) - a * i) / b + 1;
rep(j, jrange) { flag[a * i + b * j] = true; }
}
count[0] = 0;
rep(i, MAX_N - 1) { count[i + 1] = count[i] + flag[i + 1]; }
if (n < LCM) {
ans = count[n];
} else {
ans = count[LCM] + (n - LCM) / GCM;
}
cout << n - ans << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MAX_N 1000010
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 100000005
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;
int gcm(int a, int b) {
if (a < b) {
swap(a, b);
}
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
int lcm(int a, int b) { return a / gcm(a, b) * b; }
int main() {
int n, a, b;
int LCM, GCM;
bool flag[MAX_N];
int count[MAX_N];
while (1) {
cin >> n >> a >> b;
if (n == 0)
break;
rep(i, MAX_N) { flag[i] = 0; }
LCM = lcm(a, b);
GCM = gcm(a, b);
int ans;
int irange = min(MAX_N - 1, LCM) / a + 1;
rep(i, irange) {
int jrange = (min(MAX_N - 1, LCM) - a * i) / b + 1;
rep(j, jrange) { flag[a * i + b * j] = true; }
}
count[0] = 0;
rep(i, MAX_N - 1) { count[i + 1] = count[i] + flag[i + 1]; }
if (n < LCM) {
ans = count[n];
} else {
ans = count[LCM] + (n - LCM) / GCM;
}
cout << n - ans << endl;
}
} | replace | 12 | 13 | 12 | 13 | 0 | |
p00687 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
bool ok(int n, int a, int b) {
for (int i = 0; i <= n; i += a) {
if ((n - i) % b == 0)
return true;
}
return false;
}
int main() {
int n, a, b;
while (cin >> n >> a >> b, n | a | b) {
int ans = 0;
for (int i = 1; i <= n; i++) {
if (ok(i, a, b))
continue;
ans++;
}
cout << ans << endl;
}
} | #include <algorithm>
#include <iostream>
using namespace std;
bool ok(int n, int a, int b) {
if (a < b)
swap(a, b);
for (int i = 0; i <= n; i += a) {
if ((n - i) % b == 0)
return true;
}
return false;
}
int main() {
int n, a, b;
while (cin >> n >> a >> b, n | a | b) {
int ans = 0;
for (int i = 1; i <= n; i++) {
if (ok(i, a, b))
continue;
ans++;
}
cout << ans << endl;
}
} | insert | 6 | 6 | 6 | 8 | TLE | |
p00687 | C++ | Time Limit Exceeded | // 05
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
for (int n, a, b; cin >> n >> a >> b, n | a | b;) {
static bool ap[1000001];
fill(ap, ap + 1000001, false);
for (int i = 0; i * a <= n; i++) {
for (int j = 0; j * b + i * a <= n; j++) {
ap[i * a + j * b] = true;
}
}
cout << count(ap + 1, ap + 1 + n, false) << endl;
}
return 0;
} | // 05
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
for (int n, a, b; cin >> n >> a >> b, n | a | b;) {
static bool ap[1000001];
fill(ap, ap + 1000001, false);
ap[0] = true;
for (int i = a; i <= n; i++) {
ap[i] = ap[i - a];
}
for (int i = b; i <= n; i++) {
ap[i] |= ap[i - b];
}
cout << count(ap + 1, ap + 1 + n, false) << endl;
}
return 0;
} | replace | 10 | 14 | 10 | 16 | TLE | |
p00687 | 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 between(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30
int main() {
int n, a, b;
while (scanf("%d%d%d", &n, &a, &b) && n + a + b) {
bool f[1000001] = {};
f[0] = 1;
rep(i, n + 1) {
if (!f[i])
continue;
f[i + a] = 1;
f[i + b] = 1;
}
// puts("error\n");
int res = 0;
rep(i, n + 1) if (!f[i]) res++;
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 between(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30
int main() {
int n, a, b;
while (scanf("%d%d%d", &n, &a, &b) && n + a + b) {
bool f[2000000] = {};
f[0] = 1;
rep(i, n + 1) {
if (!f[i])
continue;
f[i + a] = 1;
f[i + b] = 1;
}
// puts("error\n");
int res = 0;
rep(i, n + 1) if (!f[i]) res++;
printf("%d\n", res);
}
return 0;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p00688 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
int a, b, c;
void solve() {
FOR(r, 1, 101) {
FOR(p, r, 101) {
FOR(q, -100, 101) {
FOR(s, -100, 101) {
if (r == p && q < s)
break;
if (p * r == a && p * s + r * q == b && q * s == c) {
cout << p << " " << q << " " << r << " " << s << endl;
return;
}
}
}
}
}
cout << "Impossible" << endl;
}
int main() {
while (cin >> a >> b >> c && a)
solve();
return 0;
} | #include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
int a, b, c;
void solve() {
FOR(p, 1, 10001) {
if (a % p)
continue;
int r = a / p;
FOR(q, -10001, 10001) {
int s;
if (q == 0)
s = b / p;
else
s = c / q;
if (p < r || (r == p && q < s))
continue;
if (p * r == a && p * s + r * q == b && q * s == c) {
cout << p << " " << q << " " << r << " " << s << endl;
return;
}
}
}
cout << "Impossible" << endl;
}
int main() {
while (cin >> a >> b >> c && a)
solve();
return 0;
} | replace | 29 | 40 | 29 | 44 | TLE | |
p00689 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define dump(a) (cerr << #a << " = " << (a) << endl)
constexpr double EPS = 1e-9;
struct point {
double x, y;
point(double x_ = 0.0, double y_ = 0.0) : x(x_), y(y_) {}
point(const point &p) : x(p.x), y(p.y) {}
point operator+(const point &p) const { return point(x + p.x, y + p.y); }
point operator-(const point &p) const { return point(x - p.x, y - p.y); }
};
ostream &operator<<(ostream &os, const point &p) {
return os << '(' << p.x << ", " << p.y << ')';
}
double angle(const point &p) { return atan2(p.y, p.x); }
double norm(const point &p) { return p.x * p.x + p.y * p.y; }
double normalize(double theta) {
if (theta < -M_PI)
return theta + 2 * M_PI;
if (theta > M_PI)
return theta - 2 * M_PI;
return theta;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.precision(1);
cout.setf(ios::fixed);
constexpr double INF = 1e9;
for (int n; cin >> n && n;) {
vector<point> flags;
flags.reserve(n);
for (int i = 0; i < n; ++i) {
double x, y;
cin >> x >> y;
flags.emplace_back(x, y);
}
double ans = 0.0;
point pos(0, 0);
double dir = M_PI / 2;
bitset<40> visited;
for (int i = 0; i < n; ++i) {
pair<double, double> best(INF, INF);
int next = -1;
double next_angle;
for (int v = 0; v < n; ++v) {
if (visited[v])
continue;
double dist2 = norm(flags[v] - pos);
double theta = normalize(dir - angle(flags[v] - pos));
pair<double, double> tmp(theta, dist2);
if (tmp < best) {
best = tmp;
next = v;
next_angle = angle(flags[v] - pos);
}
}
assert(next != -1);
ans += sqrt(best.second);
pos = flags[next];
dir = next_angle;
visited.set(next);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define dump(a) (cerr << #a << " = " << (a) << endl)
constexpr double EPS = 1e-9;
struct point {
double x, y;
point(double x_ = 0.0, double y_ = 0.0) : x(x_), y(y_) {}
point(const point &p) : x(p.x), y(p.y) {}
point operator+(const point &p) const { return point(x + p.x, y + p.y); }
point operator-(const point &p) const { return point(x - p.x, y - p.y); }
};
ostream &operator<<(ostream &os, const point &p) {
return os << '(' << p.x << ", " << p.y << ')';
}
double angle(const point &p) { return atan2(p.y, p.x); }
double norm(const point &p) { return p.x * p.x + p.y * p.y; }
double normalize(double theta) {
if (theta < -M_PI)
return theta + 2 * M_PI;
if (theta > M_PI)
return theta - 2 * M_PI;
return theta;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.precision(1);
cout.setf(ios::fixed);
constexpr double INF = 1e9;
for (int n; cin >> n && n;) {
vector<point> flags;
flags.reserve(n);
for (int i = 0; i < n; ++i) {
double x, y;
cin >> x >> y;
flags.emplace_back(x, y);
}
double ans = 0.0;
point pos(0, 0);
double dir = M_PI / 2;
bitset<400> visited;
for (int i = 0; i < n; ++i) {
pair<double, double> best(INF, INF);
int next = -1;
double next_angle;
for (int v = 0; v < n; ++v) {
if (visited[v])
continue;
double dist2 = norm(flags[v] - pos);
double theta = normalize(dir - angle(flags[v] - pos));
pair<double, double> tmp(theta, dist2);
if (tmp < best) {
best = tmp;
next = v;
next_angle = angle(flags[v] - pos);
}
}
assert(next != -1);
ans += sqrt(best.second);
pos = flags[next];
dir = next_angle;
visited.set(next);
}
cout << ans << endl;
}
return 0;
} | replace | 59 | 60 | 59 | 60 | 0 | |
p00691 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int z;
while (cin >> z, z) {
int max_xy = 0;
int z3 = z * z * z;
for (int x = 1; x <= z3; ++x) {
for (int y = x; y <= z3; ++y) {
if (x * x * x + y * y * y <= z3)
max_xy = max(max_xy, x * x * x + y * y * y);
else
break;
}
}
cout << z3 - max_xy << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int z;
while (cin >> z, z) {
int max_xy = 0;
int z3 = z * z * z;
for (int x = 1; x * x * x <= z3; ++x) {
int x3 = x * x * x;
for (int y = 1; y * y * y <= z3 - x3; ++y)
max_xy = max(max_xy, x3 + y * y * y);
}
cout << z3 - max_xy << endl;
}
return 0;
} | replace | 12 | 19 | 12 | 16 | TLE | |
p00691 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long int n;
while (cin >> n, n) {
long long int ans = 100000;
for (long long int i = 1; pow(i, 3) <= pow(n, 3); i++) {
for (long long int j = 1; pow(i, 3) + pow(j, 3) <= pow(n, 3); j++) {
long long int m = pow(n, 3) - pow(i, 3) - pow(j, 3);
ans = min(ans, m);
}
}
cout << ans << endl;
}
} | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long int n;
while (cin >> n, n) {
long long int ans = 100000;
for (long long int i = 1; i * i * i <= n * n * n; i++) {
for (long long int j = 1; i * i * i + j * j * j <= n * n * n; j++) {
ans = min(ans, n * n * n - i * i * i - j * j * j);
}
}
cout << ans << endl;
}
} | replace | 9 | 13 | 9 | 12 | TLE | |
p00692 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
const int H = 5, W = 4;
/*
inline int countZero(const string &str) {
int ret = 0;
for(int i=0; i<H*W; i++) {
ret += str[i] == '0';
}
return ret;
}
void rearrange(string &str) {
int pos = 0;
for(int i=0; i<H*W; i++) {
if(str[i] == '0') continue;
char ch = str[i];
str[i] = '0';
str[pos++] = ch;
}
}
*/
int main() {
int Tc;
cin >> Tc;
while (Tc--) {
string initial;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char ch;
cin >> ch;
initial += ch;
}
}
int ans = 0;
set<string> used;
queue<string> Q;
Q.push(initial);
while (!Q.empty()) {
const string now = Q.front();
Q.pop();
int zerocnt = 0;
for (int i = H * W - 1; i >= 0; i--) {
if (now[i] != '0')
break;
zerocnt++;
}
// ans = max(ans, countZero(now));
ans = max(ans, zerocnt);
for (int pos = 0; pos < H * W; pos++) {
if (now[pos] == '0')
continue;
int x = pos % W, y = pos / W;
for (int k = 0; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
const int ny = y + k, nx = x + l;
if (nx < 0 || W <= nx || ny < 0 || H <= ny)
continue;
const int npos = ny * W + nx;
if (now[pos] != now[npos])
continue;
string next;
for (int i = 0; i < H * W; i++) {
if (i == pos || i == npos)
continue;
next += now[i];
}
next += "00";
// string next = now;
// next[pos] = next[npos] = '0';
// rearrange(next);
if (used.count(next))
continue;
used.insert(now);
Q.push(next);
}
}
}
}
cout << 20 - ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
const int H = 5, W = 4;
/*
inline int countZero(const string &str) {
int ret = 0;
for(int i=0; i<H*W; i++) {
ret += str[i] == '0';
}
return ret;
}
void rearrange(string &str) {
int pos = 0;
for(int i=0; i<H*W; i++) {
if(str[i] == '0') continue;
char ch = str[i];
str[i] = '0';
str[pos++] = ch;
}
}
*/
int main() {
int Tc;
cin >> Tc;
while (Tc--) {
string initial;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char ch;
cin >> ch;
initial += ch;
}
}
int ans = 0;
set<string> used;
queue<string> Q;
Q.push(initial);
while (!Q.empty()) {
const string now = Q.front();
Q.pop();
int zerocnt = 0;
for (int i = H * W - 1; i >= 0; i--) {
if (now[i] != '0')
break;
zerocnt++;
}
// ans = max(ans, countZero(now));
ans = max(ans, zerocnt);
for (int pos = 0; pos < H * W; pos++) {
if (now[pos] == '0')
continue;
int x = pos % W, y = pos / W;
for (int k = 0; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
const int ny = y + k, nx = x + l;
if (nx < 0 || W <= nx || ny < 0 || H <= ny)
continue;
const int npos = ny * W + nx;
if (now[pos] != now[npos])
continue;
string next;
for (int i = 0; i < H * W; i++) {
if (i == pos || i == npos)
continue;
next += now[i];
}
next += "00";
// string next = now;
// next[pos] = next[npos] = '0';
// rearrange(next);
if (used.count(next))
continue;
used.insert(next);
Q.push(next);
}
}
}
}
cout << 20 - ans << endl;
}
return 0;
} | replace | 86 | 87 | 86 | 87 | MLE | |
p00693 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
bool match(string s, string t) {
for (int i = 0; i < 8; i++) {
if (s[i] != '?' && s[i] != t[i])
return false;
}
return true;
}
int n, m;
bool x[1111];
string r1[1111], r2[1111], a, b, c;
int main() {
while (cin >> n >> m, n | m) {
for (int i = 0; i < n; i++) {
cin >> a >> r1[i] >> r2[i];
if (a == "permit")
x[i] = true;
else
x[i] = false;
}
int ret = 0;
vector<string> v1, v2, v3;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
bool p1 = false, p2 = false;
for (int j = n - 1; j >= 0; j++) {
if (match(r1[j], a) && match(r2[j], b)) {
if (x[j])
p1 = true;
else
p2 = true;
break;
}
}
if (p1) {
v1.push_back(a);
v2.push_back(b);
v3.push_back(c);
ret++;
}
}
cout << ret << endl;
for (int i = 0; i < ret; i++) {
cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << endl;
}
}
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
bool match(string s, string t) {
for (int i = 0; i < 8; i++) {
if (s[i] != '?' && s[i] != t[i])
return false;
}
return true;
}
int n, m;
bool x[1111];
string r1[1111], r2[1111], a, b, c;
int main() {
while (cin >> n >> m, n | m) {
for (int i = 0; i < n; i++) {
cin >> a >> r1[i] >> r2[i];
if (a == "permit")
x[i] = true;
else
x[i] = false;
}
int ret = 0;
vector<string> v1, v2, v3;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
bool p1 = false, p2 = false;
for (int j = n - 1; j >= 0; j--) {
if (match(r1[j], a) && match(r2[j], b)) {
if (x[j])
p1 = true;
else
p2 = true;
break;
}
}
if (p1) {
v1.push_back(a);
v2.push_back(b);
v3.push_back(c);
ret++;
}
}
cout << ret << endl;
for (int i = 0; i < ret; i++) {
cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << endl;
}
}
} | replace | 28 | 29 | 28 | 29 | -11 | |
p00693 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, m;
string S0[1000], S1[1000], S2[1000], T0[1000], T1[1000], T2[1000];
bool solve(string p1, string p2) {
for (int i = 0; i < p1.size(); i++) {
if (p1[i] != '?' && p1[i] != p2[i])
return false;
}
return true;
}
int main() {
while (true) {
cin >> n >> m;
vector<int> vec;
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++)
cin >> S0[i] >> S1[i] >> S2[i];
for (int i = 0; i < m; i++) {
cin >> T1[i] >> T2[i] >> T0[i];
for (int j = n - 1; j >= 0; j--) {
bool P = solve(S1[j], T1[i]);
if (P == true && solve(S2[j], T2[i]) == false)
P = false;
if (P == true && S0[j] == "deny")
break;
if (P == true && S0[j] == "permit") {
vec.push_back(i);
break;
}
}
}
cout << vec.size() << endl;
for (int i = 0; i < vec.size(); i++)
cout << T1[vec[i]] << ' ' << T2[vec[i]] << ' ' << T0[vec[i]] << endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, m;
string S0[5000], S1[5000], S2[5000], T0[5000], T1[5000], T2[5000];
bool solve(string p1, string p2) {
for (int i = 0; i < p1.size(); i++) {
if (p1[i] != '?' && p1[i] != p2[i])
return false;
}
return true;
}
int main() {
while (true) {
cin >> n >> m;
vector<int> vec;
if (n == 0 && m == 0)
break;
for (int i = 0; i < n; i++)
cin >> S0[i] >> S1[i] >> S2[i];
for (int i = 0; i < m; i++) {
cin >> T1[i] >> T2[i] >> T0[i];
for (int j = n - 1; j >= 0; j--) {
bool P = solve(S1[j], T1[i]);
if (P == true && solve(S2[j], T2[i]) == false)
P = false;
if (P == true && S0[j] == "deny")
break;
if (P == true && S0[j] == "permit") {
vec.push_back(i);
break;
}
}
}
cout << vec.size() << endl;
for (int i = 0; i < vec.size(); i++)
cout << T1[vec[i]] << ' ' << T2[vec[i]] << ' ' << T0[vec[i]] << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00697 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
int res = 0;
rep(i, 3) rep(j, 3) {
if (t[i][j][0])
continue;
rep(k, 4) {
rep(l, 4) {
int nx = i + dx[l], ny = j + dy[l];
if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
continue;
if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
goto g;
}
if (p == 8)
res++;
else {
rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
res += dfs(p + 1);
t[i][j][0] = 0;
}
g:;
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) {
rep(j, 9) scanf("%s", s[i]);
printf("%d\n", dfs(0));
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
char s[9][5], t[3][3][5];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
int res = 0;
rep(i, 3) rep(j, 3) {
if (t[i][j][0])
continue;
rep(k, 4) {
rep(l, 4) {
int nx = i + dx[l], ny = j + dy[l];
if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || !t[nx][ny][0])
continue;
if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
goto g;
}
if (p == 8)
res++;
else {
rep(l, 4) t[i][j][l] = s[p][(l + k) % 4];
res += dfs(p + 1);
t[i][j][0] = 0;
}
g:;
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) {
rep(j, 9) scanf("%s", s[j]);
printf("%d\n", dfs(0));
}
} | replace | 35 | 36 | 35 | 36 | TLE | |
p00697 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
int res = 0;
rep(i, 3) rep(j, 3) {
if (!t[i][j].empty())
continue;
rep(k, 4) {
rep(l, 4) {
int nx = i + dx[l], ny = j + dy[l];
if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
continue;
char c = t[nx][ny][(l + 2) % 4];
if (isupper(s[p][(l + k) % 4])) {
if (isupper(c) || toupper(c) != s[p][(l + k) % 4])
goto g;
} else if (islower(c) || tolower(c) != s[p][(l + k) % 4])
goto g;
}
if (p == 8)
res++;
else {
rep(l, 4) t[i][j] += s[p][(l + k) % 4];
res += dfs(p + 1);
t[i][j].clear();
}
g:;
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) {
rep(j, 9) cin >> s[j];
printf("%d\n", dfs(0));
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
string s[9], t[3][3];
int dx[]{-1, 0, 1, 0}, dy[]{0, 1, 0, -1};
int dfs(int p) {
int res = 0;
rep(i, 3) rep(j, 3) {
if (!t[i][j].empty())
continue;
rep(k, 4) {
rep(l, 4) {
int nx = i + dx[l], ny = j + dy[l];
if (nx < 0 || 3 <= nx || ny < 0 || 3 <= ny || t[nx][ny].empty())
continue;
if (abs(s[p][(l + k) % 4] - t[nx][ny][(l + 2) % 4]) != 'a' - 'A')
goto g;
}
if (p == 8)
res++;
else {
rep(l, 4) t[i][j] += s[p][(l + k) % 4];
res += dfs(p + 1);
t[i][j].clear();
}
g:;
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) {
rep(j, 9) cin >> s[j];
printf("%d\n", dfs(0));
}
} | replace | 16 | 21 | 16 | 17 | TLE | |
p00698 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <sstream>
#include <vector>
#define MAX 1000
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
using namespace std;
typedef pair<int, int> P;
int h, w;
int G[MAX][MAX];
int A[MAX][2]; // 0->r, 1->c
P B[MAX][2];
int C[MAX][2];
int D[MAX][2];
void printA() {
cout << "printA---" << endl;
rep(i, w) cout << A[i][0] << " ";
cout << endl;
rep(i, h) cout << A[i][1] << " ";
cout << endl;
cout << endl;
}
void printB() {
cout << "printB---" << endl;
rep(i, w) cout << B[i][0].first << "," << B[i][0].second << " ";
cout << endl;
rep(i, h) cout << B[i][1].first << "," << B[1][i].second << " ";
cout << endl;
cout << endl;
}
void printC() {
cout << "printC---" << endl;
rep(i, w) cout << C[i][0] << " ";
cout << endl;
rep(i, h) cout << C[i][1] << " ";
cout << endl;
cout << endl;
}
void printD() {
cout << "printD---" << endl;
rep(i, w) cout << D[i][0] << " ";
cout << endl;
rep(i, h) cout << D[i][1] << " ";
cout << endl;
}
P Bsearch(int a, int b) {
int Size = (b == 0 ? w : h);
for (int i = 0; i < Size; i++) {
if (b == 0) {
if (G[i][a] == -inf)
return P(a, i);
} else {
if (G[a][i] == -inf)
return P(i, a);
}
}
}
int main() {
int blank = false;
while (true) {
cin >> h;
if (!h)
break;
cin >> w;
rep(i, MAX) rep(j, 2) A[i][j] = C[i][j] = 0, B[i][j] = P(-inf, -inf);
vector<P> ans;
int hatena = 0;
rep(i, h) {
rep(j, w) {
string input;
cin >> input;
if (input[0] == '?') {
G[i][j] = -inf;
A[j][0]++;
B[j][0] = P(j, i);
A[i][1]++;
B[i][1] = P(j, i);
ans.push_back(P(j, i));
} else {
G[i][j] = (atoi)(input.c_str());
C[j][0] += G[i][j];
C[i][1] += G[i][j];
}
}
cin >> D[i][1];
}
rep(i, w + 1) cin >> D[i][0];
// printA();
// printB();
// printC();
// printD();
// return 0;
bool Fin = true;
bool found = false;
while (true) {
// cout << "phase !" << endl;
// printA();
// printB();
// printC();
// printD();
Fin = true;
found = false;
rep(i, w) {
if (A[i][0] >= 1)
Fin = false;
if (A[i][0] == 1) {
// cout << "erase! " << B[i][0].first << "," << B[i][0].second <<
// endl;
found = true;
int value = D[i][0] - C[i][0];
P p = Bsearch(i, 0);
C[p.first][0] += value;
C[p.second][1] += value;
A[p.first][0]--;
A[p.second][1]--;
assert(G[p.second][p.first] == -inf);
G[p.second][p.first] = value;
}
}
rep(i, h) {
if (A[i][1] >= 1)
Fin = false;
if (A[i][1] == 1) {
// cout << "erase! " << B[i][1].first << "," << B[i][1].second <<
// endl;
found = true;
int value = D[i][1] - C[i][1];
P p = Bsearch(i, 1);
C[p.first][0] += value;
C[p.second][1] += value;
A[p.first][0]--;
A[p.second][1]--;
assert(G[p.second][p.first] == -inf);
G[p.second][p.first] = value;
}
}
if (!Fin && !found)
break;
if (Fin)
break;
}
if (!blank)
blank = true;
else
cout << endl;
if (!Fin && !found)
cout << "NO" << endl;
else {
rep(i, ans.size()) cout << G[ans[i].second][ans[i].first] << endl;
}
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <sstream>
#include <vector>
#define MAX 1000
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
using namespace std;
typedef pair<int, int> P;
int h, w;
int G[MAX][MAX];
int A[MAX][2]; // 0->r, 1->c
P B[MAX][2];
int C[MAX][2];
int D[MAX][2];
void printA() {
cout << "printA---" << endl;
rep(i, w) cout << A[i][0] << " ";
cout << endl;
rep(i, h) cout << A[i][1] << " ";
cout << endl;
cout << endl;
}
void printB() {
cout << "printB---" << endl;
rep(i, w) cout << B[i][0].first << "," << B[i][0].second << " ";
cout << endl;
rep(i, h) cout << B[i][1].first << "," << B[1][i].second << " ";
cout << endl;
cout << endl;
}
void printC() {
cout << "printC---" << endl;
rep(i, w) cout << C[i][0] << " ";
cout << endl;
rep(i, h) cout << C[i][1] << " ";
cout << endl;
cout << endl;
}
void printD() {
cout << "printD---" << endl;
rep(i, w) cout << D[i][0] << " ";
cout << endl;
rep(i, h) cout << D[i][1] << " ";
cout << endl;
}
P Bsearch(int a, int b) {
int Size = (b == 0 ? h : w);
for (int i = 0; i < Size; i++) {
if (b == 0) {
if (G[i][a] == -inf)
return P(a, i);
} else {
if (G[a][i] == -inf)
return P(i, a);
}
}
}
int main() {
int blank = false;
while (true) {
cin >> h;
if (!h)
break;
cin >> w;
rep(i, MAX) rep(j, 2) A[i][j] = C[i][j] = 0, B[i][j] = P(-inf, -inf);
vector<P> ans;
int hatena = 0;
rep(i, h) {
rep(j, w) {
string input;
cin >> input;
if (input[0] == '?') {
G[i][j] = -inf;
A[j][0]++;
B[j][0] = P(j, i);
A[i][1]++;
B[i][1] = P(j, i);
ans.push_back(P(j, i));
} else {
G[i][j] = (atoi)(input.c_str());
C[j][0] += G[i][j];
C[i][1] += G[i][j];
}
}
cin >> D[i][1];
}
rep(i, w + 1) cin >> D[i][0];
// printA();
// printB();
// printC();
// printD();
// return 0;
bool Fin = true;
bool found = false;
while (true) {
// cout << "phase !" << endl;
// printA();
// printB();
// printC();
// printD();
Fin = true;
found = false;
rep(i, w) {
if (A[i][0] >= 1)
Fin = false;
if (A[i][0] == 1) {
// cout << "erase! " << B[i][0].first << "," << B[i][0].second <<
// endl;
found = true;
int value = D[i][0] - C[i][0];
P p = Bsearch(i, 0);
C[p.first][0] += value;
C[p.second][1] += value;
A[p.first][0]--;
A[p.second][1]--;
assert(G[p.second][p.first] == -inf);
G[p.second][p.first] = value;
}
}
rep(i, h) {
if (A[i][1] >= 1)
Fin = false;
if (A[i][1] == 1) {
// cout << "erase! " << B[i][1].first << "," << B[i][1].second <<
// endl;
found = true;
int value = D[i][1] - C[i][1];
P p = Bsearch(i, 1);
C[p.first][0] += value;
C[p.second][1] += value;
A[p.first][0]--;
A[p.second][1]--;
assert(G[p.second][p.first] == -inf);
G[p.second][p.first] = value;
}
}
if (!Fin && !found)
break;
if (Fin)
break;
}
if (!blank)
blank = true;
else
cout << endl;
if (!Fin && !found)
cout << "NO" << endl;
else {
rep(i, ans.size()) cout << G[ans[i].second][ans[i].first] << endl;
}
}
return 0;
} | replace | 56 | 57 | 56 | 57 | 0 | |
p00699 | 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)
using namespace std;
const int IINF = INT_MAX;
//
vector<string> trim(const vector<string> &vec) {
int n, e, s, w;
w = IINF, e = -IINF, n = IINF, s = -IINF;
for (int y = 0; y < vec.size(); y++) {
for (int x = 0; x < vec[y].size(); x++) {
if (vec[y][x] == '0')
continue;
w = min(w, x);
e = max(e, x);
n = min(n, y);
s = max(s, y);
}
}
vector<string> ret;
for (int y = n; y <= s; y++)
ret.push_back(vec[y].substr(w, e - w + 1));
return ret;
}
vector<string> rotate90(const vector<string> &piece) {
vector<string> ret;
int h = piece[0].size(), w = piece.size();
ret.resize(h);
for (int i = 0; i < h; i++)
ret[i].resize(w);
for (int y = 0; y < ret.size(); y++) {
for (int x = 0; x < ret[0].size(); x++) {
ret[y][x] = piece[ret[0].size() - 1 - x][y];
}
}
return ret;
}
enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
template <class T> class dice {
public:
dice() {
id[TOP] = 0;
id[FRONT] = 1;
id[LEFT] = 2;
id[RIGHT] = 3;
id[BACK] = 4;
id[BOTTOM] = 5;
}
T &operator[](FACE f) { return var[id[f]]; }
const T &operator[](FACE f) const { return var[id[f]]; }
bool operator==(const dice<T> &b) const {
const dice<T> &a = *this;
return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] && a[FRONT] == b[FRONT] &&
a[BACK] == b[BACK] && a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
}
void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
vector<dice> all_rolls() {
vector<dice> ret;
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
ret.push_back(*this);
return ret;
}
bool equivalent_to(const dice &di) {
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
if (*this == di)
return true;
return false;
}
private:
void roll(FACE a, FACE b, FACE c, FACE d) {
T tmp = id[a];
id[a] = id[b];
id[b] = id[c];
id[c] = id[d];
id[d] = tmp;
}
T var[6];
int id[6];
};
FACE face[] = {TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT};
FACE dir[] = {FRONT, LEFT, BACK, RIGHT};
int around[6][4] = {{1, 2, 4, 3}, {0, 3, 5, 2}, {0, 1, 5, 4},
{0, 4, 5, 1}, {0, 2, 5, 3}, {1, 3, 4, 2}};
dice<int> makeDice(int top, int front) {
dice<int> ret;
ret[TOP] = top, ret[BOTTOM] = 5 - top;
ret[FRONT] = front, ret[BACK] = 5 - front;
int idx = IINF;
for (int i = 0; i < 4; i++) {
if (around[top][i] == front) {
idx = i + 1;
break;
}
}
assert(idx != IINF);
idx %= 4;
ret[RIGHT] = around[top][idx], ret[LEFT] = 5 - around[top][idx];
return ret;
}
//
int T, itmp;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
bool used[5][5];
int counter[6];
inline bool isValid(const vector<string> &vec, int x, int y) {
return (0 <= x && x < vec[0].size() && 0 <= y && y < vec.size());
}
void dfs(const vector<string> &vec, int x, int y) {
if (used[y][x])
return;
counter[vec[y][x] - '1']++;
used[y][x] = true;
rep(i, 4) {
int nx = x + dx[i], ny = y + dy[i];
if (isValid(vec, nx, ny)) {
if (!used[ny][nx] && vec[ny][nx] != '0') {
dfs(vec, nx, ny);
}
}
}
}
bool check(const vector<string> &vec) {
rep(i, 5) rep(j, 5) used[i][j] = false;
rep(i, 6) counter[i] = 0;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] != '0') {
dfs(vec, j, i);
goto Skip;
}
Skip:;
rep(i, vec.size())
rep(j, vec[i].size()) if (vec[i][j] != '0' && !used[i][j]) return false;
rep(i, 6) if (counter[i] != 1) return false;
return true;
}
const string YES = "true", NO = "false";
int top, dice_tmp, H, W;
bool error;
const bool DEBUG = false;
inline void roller(dice<int> &die, int type) {
if (type == 0) { // FRONT to TOP
rep(i, 3) die.roll_x();
} else if (type == 1) { // LEFT to TOP
die.roll_y();
} else if (type == 2) { // BACK to TOP
die.roll_x();
} else { // RIGHT to TOP
rep(i, 3) die.roll_y();
}
}
void dfs2(dice<int> &die, const vector<string> &vec, int x, int y) {
if (error)
return;
if (used[y][x])
return;
used[y][x] = true;
if (DEBUG)
cout << "(" << x << "," << y << ") = " << vec[y][x]
<< " top = " << die[TOP] + 1 << " rig = " << die[RIGHT] + 1 << endl;
rep(i, 4) {
int nx = x + dx[i], ny = y + dy[i];
if (!isValid(vec, nx, ny))
continue;
if (vec[ny][nx] == '0')
continue;
if (die[dir[i]] != vec[ny][nx] - '1') {
error = true;
return;
}
roller(die, i);
dfs2(die, vec, nx, ny);
roller(die, (i + 2) % 4);
}
}
bool correct(const vector<string> &vec) {
dice<int> die = makeDice(top, dice_tmp);
int sx = -1, sy = -1;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] - '1' == top) {
sx = j, sy = i;
break;
}
// assert( sx != -1 && sy != -1 );
if (DEBUG)
cout << "TOP = " << top << endl;
if (DEBUG)
rep(i, 6) { cout << die[face[i]] << endl; }
rep(i, 4) {
int nx = sx + dx[i], ny = sy + dy[i];
if (!isValid(vec, nx, ny))
continue;
if (vec[ny][nx] - '1' == dice_tmp) {
int debug = 0;
while (die[dir[i]] != dice_tmp) {
die.roll_z();
debug++;
// assert(debug<=4);
};
break;
}
}
H = vec.size(), W = vec[0].size();
rep(i, H) rep(j, W) used[i][j] = false;
error = false;
if (DEBUG) {
cout << endl;
cout << "is correct???" << endl;
rep(i, H) {
rep(j, W) cout << vec[i][j];
cout << endl;
}
cout << endl;
}
dfs2(die, vec, sx, sy);
if (error)
return false;
rep(i, H) rep(j, W) if (vec[i][j] != '0' && !used[i][j]) return false;
return true;
}
inline void simulate(vector<string> vec) {
rep(i, 2) {
rep(_, 4) {
if (correct(vec)) {
cout << YES << endl;
return;
}
vec = rotate90(vec);
}
rep(j, vec.size()) reverse(vec[j].begin(), vec[j].end());
}
cout << NO << endl;
}
int main() {
cin >> T;
while (T--) {
vector<string> vec(5);
rep(i, 5) vec[i] = "";
rep(i, 5) rep(j, 5) {
cin >> itmp;
vec[i] += string(1, (char)('0' + itmp));
}
// vec = trim(vec);
if (!check(vec)) {
cout << NO << endl;
continue;
}
top = -1, dice_tmp = -1;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] != '0') {
top = vec[i][j] - '1';
rep(k, 4) {
int nx = j + dx[k], ny = i + dy[k];
if (isValid(vec, nx, ny)) {
if (vec[ny][nx] != '0') {
dice_tmp = vec[ny][nx] - '1';
goto Skip2;
}
}
}
}
Skip2:;
assert(top != -1 && dice_tmp != -1);
simulate(vec);
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
const int IINF = INT_MAX;
//
vector<string> trim(const vector<string> &vec) {
int n, e, s, w;
w = IINF, e = -IINF, n = IINF, s = -IINF;
for (int y = 0; y < vec.size(); y++) {
for (int x = 0; x < vec[y].size(); x++) {
if (vec[y][x] == '0')
continue;
w = min(w, x);
e = max(e, x);
n = min(n, y);
s = max(s, y);
}
}
vector<string> ret;
for (int y = n; y <= s; y++)
ret.push_back(vec[y].substr(w, e - w + 1));
return ret;
}
vector<string> rotate90(const vector<string> &piece) {
vector<string> ret;
int h = piece[0].size(), w = piece.size();
ret.resize(h);
for (int i = 0; i < h; i++)
ret[i].resize(w);
for (int y = 0; y < ret.size(); y++) {
for (int x = 0; x < ret[0].size(); x++) {
ret[y][x] = piece[ret[0].size() - 1 - x][y];
}
}
return ret;
}
enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
template <class T> class dice {
public:
dice() {
id[TOP] = 0;
id[FRONT] = 1;
id[LEFT] = 2;
id[RIGHT] = 3;
id[BACK] = 4;
id[BOTTOM] = 5;
}
T &operator[](FACE f) { return var[id[f]]; }
const T &operator[](FACE f) const { return var[id[f]]; }
bool operator==(const dice<T> &b) const {
const dice<T> &a = *this;
return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] && a[FRONT] == b[FRONT] &&
a[BACK] == b[BACK] && a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
}
void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
vector<dice> all_rolls() {
vector<dice> ret;
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
ret.push_back(*this);
return ret;
}
bool equivalent_to(const dice &di) {
for (int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), ++k)
for (int i = 0; i < 4; roll_z(), ++i)
if (*this == di)
return true;
return false;
}
private:
void roll(FACE a, FACE b, FACE c, FACE d) {
T tmp = id[a];
id[a] = id[b];
id[b] = id[c];
id[c] = id[d];
id[d] = tmp;
}
T var[6];
int id[6];
};
FACE face[] = {TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT};
FACE dir[] = {FRONT, LEFT, BACK, RIGHT};
int around[6][4] = {{1, 2, 4, 3}, {0, 3, 5, 2}, {0, 1, 5, 4},
{0, 4, 5, 1}, {0, 2, 5, 3}, {1, 3, 4, 2}};
dice<int> makeDice(int top, int front) {
dice<int> ret;
ret[TOP] = top, ret[BOTTOM] = 5 - top;
ret[FRONT] = front, ret[BACK] = 5 - front;
int idx = IINF;
for (int i = 0; i < 4; i++) {
if (around[top][i] == front) {
idx = i + 1;
break;
}
}
// assert(idx != IINF);
idx %= 4;
ret[RIGHT] = around[top][idx], ret[LEFT] = 5 - around[top][idx];
return ret;
}
//
int T, itmp;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
bool used[5][5];
int counter[6];
inline bool isValid(const vector<string> &vec, int x, int y) {
return (0 <= x && x < vec[0].size() && 0 <= y && y < vec.size());
}
void dfs(const vector<string> &vec, int x, int y) {
if (used[y][x])
return;
counter[vec[y][x] - '1']++;
used[y][x] = true;
rep(i, 4) {
int nx = x + dx[i], ny = y + dy[i];
if (isValid(vec, nx, ny)) {
if (!used[ny][nx] && vec[ny][nx] != '0') {
dfs(vec, nx, ny);
}
}
}
}
bool check(const vector<string> &vec) {
rep(i, 5) rep(j, 5) used[i][j] = false;
rep(i, 6) counter[i] = 0;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] != '0') {
dfs(vec, j, i);
goto Skip;
}
Skip:;
rep(i, vec.size())
rep(j, vec[i].size()) if (vec[i][j] != '0' && !used[i][j]) return false;
rep(i, 6) if (counter[i] != 1) return false;
return true;
}
const string YES = "true", NO = "false";
int top, dice_tmp, H, W;
bool error;
const bool DEBUG = false;
inline void roller(dice<int> &die, int type) {
if (type == 0) { // FRONT to TOP
rep(i, 3) die.roll_x();
} else if (type == 1) { // LEFT to TOP
die.roll_y();
} else if (type == 2) { // BACK to TOP
die.roll_x();
} else { // RIGHT to TOP
rep(i, 3) die.roll_y();
}
}
void dfs2(dice<int> &die, const vector<string> &vec, int x, int y) {
if (error)
return;
if (used[y][x])
return;
used[y][x] = true;
if (DEBUG)
cout << "(" << x << "," << y << ") = " << vec[y][x]
<< " top = " << die[TOP] + 1 << " rig = " << die[RIGHT] + 1 << endl;
rep(i, 4) {
int nx = x + dx[i], ny = y + dy[i];
if (!isValid(vec, nx, ny))
continue;
if (vec[ny][nx] == '0')
continue;
if (die[dir[i]] != vec[ny][nx] - '1') {
error = true;
return;
}
roller(die, i);
dfs2(die, vec, nx, ny);
roller(die, (i + 2) % 4);
}
}
bool correct(const vector<string> &vec) {
dice<int> die = makeDice(top, dice_tmp);
int sx = -1, sy = -1;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] - '1' == top) {
sx = j, sy = i;
break;
}
// assert( sx != -1 && sy != -1 );
if (DEBUG)
cout << "TOP = " << top << endl;
if (DEBUG)
rep(i, 6) { cout << die[face[i]] << endl; }
rep(i, 4) {
int nx = sx + dx[i], ny = sy + dy[i];
if (!isValid(vec, nx, ny))
continue;
if (vec[ny][nx] - '1' == dice_tmp) {
int debug = 0;
while (die[dir[i]] != dice_tmp) {
die.roll_z();
debug++;
// assert(debug<=4);
};
break;
}
}
H = vec.size(), W = vec[0].size();
rep(i, H) rep(j, W) used[i][j] = false;
error = false;
if (DEBUG) {
cout << endl;
cout << "is correct???" << endl;
rep(i, H) {
rep(j, W) cout << vec[i][j];
cout << endl;
}
cout << endl;
}
dfs2(die, vec, sx, sy);
if (error)
return false;
rep(i, H) rep(j, W) if (vec[i][j] != '0' && !used[i][j]) return false;
return true;
}
inline void simulate(vector<string> vec) {
rep(i, 2) {
rep(_, 4) {
if (correct(vec)) {
cout << YES << endl;
return;
}
vec = rotate90(vec);
}
rep(j, vec.size()) reverse(vec[j].begin(), vec[j].end());
}
cout << NO << endl;
}
int main() {
cin >> T;
while (T--) {
vector<string> vec(5);
rep(i, 5) vec[i] = "";
rep(i, 5) rep(j, 5) {
cin >> itmp;
vec[i] += string(1, (char)('0' + itmp));
}
// vec = trim(vec);
if (!check(vec)) {
cout << NO << endl;
continue;
}
top = -1, dice_tmp = -1;
rep(i, vec.size()) rep(j, vec[i].size()) if (vec[i][j] != '0') {
top = vec[i][j] - '1';
rep(k, 4) {
int nx = j + dx[k], ny = i + dy[k];
if (isValid(vec, nx, ny)) {
if (vec[ny][nx] != '0') {
dice_tmp = vec[ny][nx] - '1';
goto Skip2;
}
}
}
}
Skip2:;
assert(top != -1 && dice_tmp != -1);
simulate(vec);
}
return 0;
} | replace | 110 | 111 | 110 | 111 | 0 | |
p00701 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 105;
int N;
int top[MAXN], bottom[MAXN];
int findTop(int x, vector<int> *v = NULL) {
if (v != NULL)
v->push_back(x);
return top[x] == x ? x : findTop(top[x], v);
}
int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }
void dispose(int x) {
vector<int> v;
findTop(x, &v);
int y = bottom[x];
if (y != 0)
top[y] = y;
for (int i = 0; i < v.size(); ++i) {
top[v[i]] = v[i];
bottom[v[i]] = 0;
}
}
void move(int i, int j) {
if (j == 0) {
if (bottom[i] == 0)
return;
dispose(i);
} else {
if (findTop(i) == findTop(j) && getH(i) >= getH(j))
return;
if (top[i] != i)
dispose(top[i]);
j = findTop(j);
bottom[i] = j;
top[j] = i;
}
}
int main() {
while (cin >> N && N) {
for (int i = 0; i <= N; ++i) {
top[i] = i;
bottom[i] = 0;
}
{
int I, J;
while (cin >> I >> J && (I | J)) {
move(I, J);
}
}
vector<int> h;
int sum = 0;
for (int i = 1; i <= N; ++i) {
if (top[i] == i) {
h.push_back(getH(i));
sum += h.back();
}
}
assert(sum == N);
sort(h.begin(), h.end());
for (int i = 0; i < h.size(); ++i) {
cout << h[i] << endl;
}
cout << "end" << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 105;
int N;
int top[MAXN], bottom[MAXN];
int findTop(int x, vector<int> *v = NULL) {
if (v != NULL)
v->push_back(x);
return top[x] == x ? x : findTop(top[x], v);
}
int getH(int x) { return bottom[x] == 0 ? 1 : getH(bottom[x]) + 1; }
void dispose(int x) {
vector<int> v;
findTop(x, &v);
int y = bottom[x];
if (y != 0)
top[y] = y;
for (int i = 0; i < v.size(); ++i) {
top[v[i]] = v[i];
bottom[v[i]] = 0;
}
}
void move(int i, int j) {
if (j == 0) {
if (bottom[i] == 0)
return;
dispose(i);
} else {
if (findTop(i) == findTop(j) && getH(i) >= getH(j))
return;
dispose(i);
j = findTop(j);
bottom[i] = j;
top[j] = i;
}
}
int main() {
while (cin >> N && N) {
for (int i = 0; i <= N; ++i) {
top[i] = i;
bottom[i] = 0;
}
{
int I, J;
while (cin >> I >> J && (I | J)) {
move(I, J);
}
}
vector<int> h;
int sum = 0;
for (int i = 1; i <= N; ++i) {
if (top[i] == i) {
h.push_back(getH(i));
sum += h.back();
}
}
assert(sum == N);
sort(h.begin(), h.end());
for (int i = 0; i < h.size(); ++i) {
cout << h[i] << endl;
}
cout << "end" << endl;
}
return 0;
} | replace | 39 | 41 | 39 | 40 | 0 | |
p00702 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
cin.ignore();
string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
"nt", "nw", "ps", "qu", "cw", "ts"};
map<string, int> index;
for (int i = 0; i < 12; ++i)
index.insert(make_pair(kan[i], 26 + i));
vector<vector<int>> num(38, vector<int>(38, 0));
for (;;) {
string s;
if (!(cin >> s))
break;
int n = s.size();
vector<int> j;
for (int i = 0; i < n; ++i) {
if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
j.push_back(index[s.substr(i, 2)]);
++i;
} else {
j.push_back(s[i] - 'a');
}
}
for (unsigned i = 0; i < j.size() - 1; ++i)
++num[j[i]][j[i + 1]];
}
for (int i = 0; i < 38; ++i) {
int k = 0;
for (int j = 1; j < 38; ++j) {
if (num[i][j] > num[i][k])
k = j;
}
if (i < 26)
cout << (char)('a' + i) << ' ';
else
cout << kan[i - 26] << ' ';
if (k < 26)
cout << (char)('a' + k) << ' ';
else
cout << kan[k - 26] << ' ';
cout << num[i][k] << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
int tmp;
cin >> tmp;
string kan[] = {"ld", "mb", "mp", "nc", "nd", "ng",
"nt", "nw", "ps", "qu", "cw", "ts"};
map<string, int> index;
for (int i = 0; i < 12; ++i)
index.insert(make_pair(kan[i], 26 + i));
vector<vector<int>> num(38, vector<int>(38, 0));
for (;;) {
string s;
if (!(cin >> s))
break;
int n = s.size();
vector<int> j;
for (int i = 0; i < n; ++i) {
if (i < n - 1 && index.find(s.substr(i, 2)) != index.end()) {
j.push_back(index[s.substr(i, 2)]);
++i;
} else {
j.push_back(s[i] - 'a');
}
}
for (unsigned i = 0; i < j.size() - 1; ++i)
++num[j[i]][j[i + 1]];
}
for (int i = 0; i < 38; ++i) {
int k = 0;
for (int j = 1; j < 38; ++j) {
if (num[i][j] > num[i][k])
k = j;
}
if (i < 26)
cout << (char)('a' + i) << ' ';
else
cout << kan[i - 26] << ' ';
if (k < 26)
cout << (char)('a' + k) << ' ';
else
cout << kan[k - 26] << ' ';
cout << num[i][k] << endl;
}
return 0;
} | replace | 19 | 20 | 19 | 21 | 0 | |
p00705 | C++ | Runtime Error | #include <iostream>
// #define N 101
#define N 11
using namespace std;
int main() {
int n, q, m, d;
int data[N];
while (cin >> n >> q && (n || q)) {
// cout << "n="<<n<<" q="<<q << endl;
for (int i = 0; i < N; i++)
data[i] = 0;
for (int i = 0; i < n; i++) {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> d;
data[d]++;
}
}
/*
for( int i=0;i<N;i++ )
cout << " day"<<i <<"="<<data[i]<<" person" << endl;
*/
int max = -1, mm = -1;
for (d = 0; d < N; d++)
if (max < data[d]) {
max = data[d];
mm = d;
}
if (max < q)
cout << "0" << endl;
else
cout << mm << endl;
}
return 0;
} | #include <iostream>
// #define N 101
#define N 101
using namespace std;
int main() {
int n, q, m, d;
int data[N];
while (cin >> n >> q && (n || q)) {
// cout << "n="<<n<<" q="<<q << endl;
for (int i = 0; i < N; i++)
data[i] = 0;
for (int i = 0; i < n; i++) {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> d;
data[d]++;
}
}
/*
for( int i=0;i<N;i++ )
cout << " day"<<i <<"="<<data[i]<<" person" << endl;
*/
int max = -1, mm = -1;
for (d = 0; d < N; d++)
if (max < data[d]) {
max = data[d];
mm = d;
}
if (max < q)
cout << "0" << endl;
else
cout << mm << endl;
}
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00705 | Python | Runtime Error | import collections
while True:
N, Q = map(int, input().split())
if N == 0 and Q == 0:
break
D = []
for _ in range(N):
i = list(map(int, input().split()))
del i[0]
[D.append(ii) for ii in i]
c = collections.Counter(D)
cc = c.most_common()
if cc[0][1] >= Q:
print(cc[0][0])
else:
print(0)
| import collections
while True:
N, Q = map(int, input().split())
if N == 0 and Q == 0:
break
D = []
for _ in range(N):
i = list(map(int, input().split()))
del i[0]
[D.append(ii) for ii in i]
if not len(D) == 0:
c = collections.Counter(D)
cc = c.most_common()
# print(cc)
if cc[0][1] >= Q:
ans = 1000000
tmp = cc[0][1]
for i in range(len(cc)):
if cc[i][1] == tmp:
ans = min(cc[i][0], ans)
print(ans)
else:
print(0)
else:
print(0)
| replace | 11 | 15 | 11 | 24 | 0 | |
p00705 | C++ | Time Limit Exceeded | #include <stdio.h>
#pragma warning(disable : 4996)
int N, Q, M, D, R[100];
int main() {
while (true) {
scanf("%d%d", &N, &Q);
if (N == 0 && Q == 0)
break;
for (int i = 0; i < 100; i++)
R[i] = 0;
for (int i = 0; i < N; i++) {
scanf("%d", &M);
for (int j = 0; j < M; j++) {
scanf("%d", &D);
R[--D]++;
}
}
int ptr = 0;
for (int i = 0; i < 100;) {
if (R[i] >= Q) {
if (ptr == 0) {
ptr = i + 1;
} else if (R[i] > R[ptr - 1]) {
ptr = i + 1;
}
}
}
printf("%d\n", ptr);
}
return 0;
} | #include <stdio.h>
#pragma warning(disable : 4996)
int N, Q, M, D, R[100];
int main() {
while (true) {
scanf("%d%d", &N, &Q);
if (N == 0 && Q == 0)
break;
for (int i = 0; i < 100; i++)
R[i] = 0;
for (int i = 0; i < N; i++) {
scanf("%d", &M);
for (int j = 0; j < M; j++) {
scanf("%d", &D);
R[--D]++;
}
}
int ptr = 0;
for (int i = 0; i < 100; i++) {
if (R[i] >= Q) {
if (ptr == 0) {
ptr = i + 1;
} else if (R[i] > R[ptr - 1]) {
ptr = i + 1;
}
}
}
printf("%d\n", ptr);
}
return 0;
} | replace | 18 | 19 | 18 | 19 | TLE | |
p00705 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 50
using namespace std;
int main() {
int n, q, m;
int d[MAX], date;
while (cin >> n >> q) {
if (n == 0 && q == 0)
break;
memset(d, 0, sizeof(d));
while (n--) {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> date;
d[date]++;
}
}
bool flag = 0;
int ans = 0, tmp = 0;
for (int i = 1; i < MAX; i++) {
if (d[i] >= q) {
if (i >= ans && d[i] > tmp) {
ans = i;
tmp = d[i];
flag = 1;
}
}
}
if (flag == 1)
cout << ans << endl;
else
cout << 0 << endl;
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <iostream>
#define MAX 100
using namespace std;
int main() {
int n, q, m;
int d[MAX], date;
while (cin >> n >> q) {
if (n == 0 && q == 0)
break;
memset(d, 0, sizeof(d));
while (n--) {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> date;
d[date]++;
}
}
bool flag = 0;
int ans = 0, tmp = 0;
for (int i = 1; i < MAX; i++) {
if (d[i] >= q) {
if (i >= ans && d[i] > tmp) {
ans = i;
tmp = d[i];
flag = 1;
}
}
}
if (flag == 1)
cout << ans << endl;
else
cout << 0 << endl;
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00706 | C++ | Runtime Error | // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1125
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int N, W, H, S, T;
bool field[110][110];
void init() { fill(&field[0][0], &field[110][110], false); }
int trees(int o_x, int o_y) {
int count = 0;
for (int i = o_x; i < o_x + S; i++) {
for (int j = o_y; j < o_y + T; j++) {
if (field[i][j]) {
count++;
}
}
}
return count++;
}
int main() {
while (scanf("%d", &N) != EOF && N) {
init();
scanf("%d%d", &W, &H);
int x, y;
for (int i = 0; i < N; i++) {
scanf("%d%d", &x, &y);
x--;
y--; // 0からにする。
field[x][y] = true;
}
scanf("%d%d", &S, &T);
int max = 0;
for (int i = 0; i + S <= W; i++) {
for (int j = 0; j + T <= H; j++) {
int temp = trees(i, j);
if (max < temp) {
max = temp;
}
}
}
printf("%d\n", max);
}
} | // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1125
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int N, W, H, S, T;
bool field[110][110];
void init() { fill(&field[0][0], &field[0][0] + 110 * 110, false); }
int trees(int o_x, int o_y) {
int count = 0;
for (int i = o_x; i < o_x + S; i++) {
for (int j = o_y; j < o_y + T; j++) {
if (field[i][j]) {
count++;
}
}
}
return count++;
}
int main() {
while (scanf("%d", &N) != EOF && N) {
init();
scanf("%d%d", &W, &H);
int x, y;
for (int i = 0; i < N; i++) {
scanf("%d%d", &x, &y);
x--;
y--; // 0からにする。
field[x][y] = true;
}
scanf("%d%d", &S, &T);
int max = 0;
for (int i = 0; i + S <= W; i++) {
for (int j = 0; j + T <= H; j++) {
int temp = trees(i, j);
if (max < temp) {
max = temp;
}
}
}
printf("%d\n", max);
}
} | replace | 10 | 11 | 10 | 11 | -11 | |
p00706 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int i, j, n, w, h, s, t, wx, hy, map[101][101];
while (1) {
cin >> n;
if (n == 0)
break;
int datax[101];
int datay[101];
cin >> w >> h;
for (i = 1; i <= n; i++) {
cin >> wx >> hy;
map[hy][wx] = 1;
datax[i] = wx;
datay[i] = hy;
}
cin >> s >> t;
int max = -1;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
int count = 0;
for (int k = 1; k <= n; k++) {
if (datay[k] >= i && datay[k] < i + t) {
if (datax[k] >= j && datax[k] < j + s) {
count++;
if (max < count)
max = count;
}
}
}
}
}
cout << max << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int i, j, n, w, h, s, t, wx, hy, map[101][101];
while (1) {
cin >> n;
if (n == 0)
break;
int datax[501];
int datay[501];
cin >> w >> h;
for (i = 1; i <= n; i++) {
cin >> wx >> hy;
map[hy][wx] = 1;
datax[i] = wx;
datay[i] = hy;
}
cin >> s >> t;
int max = -1;
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
int count = 0;
for (int k = 1; k <= n; k++) {
if (datay[k] >= i && datay[k] < i + t) {
if (datax[k] >= j && datax[k] < j + s) {
count++;
if (max < count)
max = count;
}
}
}
}
}
cout << max << endl;
}
return 0;
} | replace | 9 | 11 | 9 | 11 | TLE | |
p00706 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000
typedef long long LL;
typedef long double LD;
int main() {
while (1) {
int n, w, h;
cin >> n >> w >> h;
vector<vector<int>> ruiseki(h + 1, vector<int>(w + 1, 0));
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
ruiseki[y][x]++;
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ruiseki[i][j] +=
+ruiseki[i - 1][j] + ruiseki[i][j - 1] - ruiseki[i - 1][j - 1];
}
}
int s, t;
cin >> t >> s;
int ans = 0;
for (int i = s; i <= h; i++) {
for (int j = t; j <= w; j++) {
ans = max(ans, ruiseki[i][j] - ruiseki[i - s][j] - ruiseki[i][j - t] +
ruiseki[i - s][j - t]);
}
}
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) max((a), -(a))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000
typedef long long LL;
typedef long double LD;
int main() {
while (1) {
int n, w, h;
cin >> n >> w >> h;
if (n == 0)
return 0;
vector<vector<int>> ruiseki(h + 1, vector<int>(w + 1, 0));
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
ruiseki[y][x]++;
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ruiseki[i][j] +=
+ruiseki[i - 1][j] + ruiseki[i][j - 1] - ruiseki[i - 1][j - 1];
}
}
int s, t;
cin >> t >> s;
int ans = 0;
for (int i = s; i <= h; i++) {
for (int j = t; j <= w; j++) {
ans = max(ans, ruiseki[i][j] - ruiseki[i - s][j] - ruiseki[i][j - t] +
ruiseki[i - s][j - t]);
}
}
cout << ans << endl;
}
return 0;
}
| insert | 23 | 23 | 23 | 25 | TLE | |
p00706 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v))
// #define scanf scanf_s
typedef pair<int, int> P;
int n;
int w, h;
int tizu[105][105];
int main() {
while (cin >> n, n) {
memset(tizu, 0, sizeof(tizu));
cin >> w >> h;
REP(i, n) {
int x, y;
cin >> x >> y;
tizu[y - 1][x - 1] = 1;
}
int s, t;
cin >> s >> t;
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int cnt = 0;
REP(k, t) REP(l, s) { cnt += tizu[k + i][l + j]; }
ans = max(cnt, ans);
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define SORT(v) sort(ALL(v))
// #define scanf scanf_s
typedef pair<int, int> P;
int n;
int w, h;
int tizu[300][300];
int main() {
while (cin >> n, n) {
memset(tizu, 0, sizeof(tizu));
cin >> w >> h;
REP(i, n) {
int x, y;
cin >> x >> y;
tizu[y - 1][x - 1] = 1;
}
int s, t;
cin >> s >> t;
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int cnt = 0;
REP(k, t) REP(l, s) { cnt += tizu[k + i][l + j]; }
ans = max(cnt, ans);
}
}
cout << ans << endl;
}
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p00706 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main(void) {
int n;
while (cin >> n) {
if (n == 0)
break;
cerr << endl;
int w, h;
cin >> w >> h;
vector<vector<bool>> a(h, vector<bool>(w, false));
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[y - 1][x - 1] = true;
}
int s, t;
cin >> s >> t;
int mx = 0;
for (int i = 0; i <= h - t; i++) {
for (int j = 0; j <= w - s; j++) {
int cnt = 0;
for (int k = 0; k < t; k++) {
for (int l = 0; l < s; l++) {
if (a[i + k][j + l])
cnt++;
}
}
mx = max(mx, cnt);
}
}
cout << mx << endl;
}
return 0;
}
// | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main(void) {
int n;
while (cin >> n) {
if (n == 0)
break;
int w, h;
cin >> w >> h;
vector<vector<bool>> a(h, vector<bool>(w, false));
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[y - 1][x - 1] = true;
}
int s, t;
cin >> s >> t;
int mx = 0;
for (int i = 0; i <= h - t; i++) {
for (int j = 0; j <= w - s; j++) {
int cnt = 0;
for (int k = 0; k < t; k++) {
for (int l = 0; l < s; l++) {
if (a[i + k][j + l])
cnt++;
}
}
mx = max(mx, cnt);
}
}
cout << mx << endl;
}
return 0;
}
// | delete | 18 | 19 | 18 | 18 | 0 | |
p00706 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main(void) {
int n;
while (cin >> n && n) {
int w, h;
cin >> w >> h;
bool field[w][h];
fill(field[0], field[w], 0);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
field[x][y] = 1;
}
int s, t;
cin >> s >> t;
int res = 0;
for (int sy = 0; sy <= h - t; ++sy) {
for (int sx = 0; sx <= w - s; ++sx) {
int count = 0;
for (int dy = 0; dy < t; ++dy) {
for (int dx = 0; dx < s; ++dx) {
count += field[sx + dx][sy + dy];
}
}
res = max(res, count);
}
}
cout << res << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int main(void) {
int n;
while (cin >> n && n) {
int w, h;
cin >> w >> h;
bool field[w][h];
fill(field[0], field[w], 0);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
field[x - 1][y - 1] = 1;
}
int s, t;
cin >> s >> t;
int res = 0;
for (int sy = 0; sy <= h - t; ++sy) {
for (int sx = 0; sx <= w - s; ++sx) {
int count = 0;
for (int dy = 0; dy < t; ++dy) {
for (int dx = 0; dx < s; ++dx) {
count += field[sx + dx][sy + dy];
}
}
res = max(res, count);
}
}
cout << res << endl;
}
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p00706 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main(void) {
while (true) {
int N, W, H;
cin >> N;
if (N == 0)
break;
cin >> W >> H;
vector<vector<bool>> m(H, vector<bool>(W, false));
vector<vector<int>> wa(H + 1, vector<int>(W, 0));
REP(i, N) {
int x, y;
cin >> x >> y;
--x;
--y;
m[y][x] = true;
}
REP(i, H) {
REP(j, W) { wa[i + 1][j + 1] += wa[i + 1][j] + (int)m[i][j]; }
}
REP(i, H) {
REP(j, W) { wa[i + 1][j + 1] += wa[i][j + 1]; }
}
int S, T;
cin >> S >> T;
int ans = 0;
REP(i, H - T + 1) {
REP(j, W - S + 1) {
ans =
max(ans, wa[T + i][S + j] - wa[T + i][j] - wa[i][S + j] + wa[i][j]);
}
}
cout << ans << endl;
}
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main(void) {
while (true) {
int N, W, H;
cin >> N;
if (N == 0)
break;
cin >> W >> H;
vector<vector<bool>> m(H, vector<bool>(W, false));
vector<vector<int>> wa(H + 1, vector<int>(W + 1, 0));
REP(i, N) {
int x, y;
cin >> x >> y;
--x;
--y;
m[y][x] = true;
}
REP(i, H) {
REP(j, W) { wa[i + 1][j + 1] += wa[i + 1][j] + (int)m[i][j]; }
}
REP(i, H) {
REP(j, W) { wa[i + 1][j + 1] += wa[i][j + 1]; }
}
int S, T;
cin >> S >> T;
int ans = 0;
REP(i, H - T + 1) {
REP(j, W - S + 1) {
ans =
max(ans, wa[T + i][S + j] - wa[T + i][j] - wa[i][S + j] + wa[i][j]);
}
}
cout << ans << endl;
}
} | replace | 30 | 31 | 30 | 31 | -6 | munmap_chunk(): invalid pointer
|
p00706 | C++ | Runtime Error | #include <cstdio>
#include <iomanip>
#include <iostream>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cctype>
#include <complex>
#include <sstream>
#include <string>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (unsigned int i = 0; i < (n); i++)
#define tr(it, container) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
typedef long long ll;
typedef complex<double> P;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
while (true) {
int N;
cin >> N;
if (N == 0)
break;
int W, H;
cin >> W >> H;
vector<vector<char>> F(H, vector<char>(W, '.'));
rep(i, N) {
int x, y;
cin >> x >> y;
x--;
y--;
F[y][x] = '*';
}
int S, T;
cin >> S >> T;
int ret = 0;
rep(i, H) {
rep(j, W) cerr << F[i][j];
cerr << endl;
}
for (int i = 0; i < H - T + 1; i++) {
for (int j = 0; j < W - S + 1; j++) {
int c = 0;
for (int k = i; k < i + T; k++) {
for (int l = j; l < j + S; l++) {
if (F[k][l] == '*') {
c++;
}
}
}
ret = max(ret, c);
}
}
cout << ret << endl;
}
return 0;
} | #include <cstdio>
#include <iomanip>
#include <iostream>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cctype>
#include <complex>
#include <sstream>
#include <string>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (unsigned int i = 0; i < (n); i++)
#define tr(it, container) \
for (typeof(container.begin()) it = container.begin(); \
it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))
typedef long long ll;
typedef complex<double> P;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
while (true) {
int N;
cin >> N;
if (N == 0)
break;
int W, H;
cin >> W >> H;
vector<vector<char>> F(H, vector<char>(W, '.'));
rep(i, N) {
int x, y;
cin >> x >> y;
x--;
y--;
F[y][x] = '*';
}
int S, T;
cin >> S >> T;
int ret = 0;
for (int i = 0; i < H - T + 1; i++) {
for (int j = 0; j < W - S + 1; j++) {
int c = 0;
for (int k = i; k < i + T; k++) {
for (int l = j; l < j + S; l++) {
if (F[k][l] == '*') {
c++;
}
}
}
ret = max(ret, c);
}
}
cout << ret << endl;
}
return 0;
} | replace | 59 | 63 | 59 | 60 | 0 | .......*..
.*.*......
..*......*
.....*.*..
.*.*..*...
........*.
.*...*....
..**..*...
.*...*
*..*.*
....*.
.**...
|
p00706 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
int H, W, S, T;
bool fld[100][100];
int Count(int x, int y) {
int res = 0;
for (int i = y; i < y + T; i++) {
for (int j = x; j < x + S; j++) {
res += fld[i][j];
}
}
return res;
}
signed main() {
int N;
while (cin >> N, N) {
fill_n((bool *)fld, 100 * 1000, false);
cin >> W >> H;
for (int i = 0; i < N; i++) {
int X, Y;
cin >> X >> Y;
X--;
Y--;
fld[Y][X] = true;
}
cin >> S >> T;
int ma = 0;
for (int i = 0; i <= H - T; i++) {
for (int j = 0; j <= W - S; j++) {
ma = max(ma, Count(j, i));
}
}
printf("%lld\n", ma);
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
int H, W, S, T;
bool fld[100][100];
int Count(int x, int y) {
int res = 0;
for (int i = y; i < y + T; i++) {
for (int j = x; j < x + S; j++) {
res += fld[i][j];
}
}
return res;
}
signed main() {
int N;
while (cin >> N, N) {
fill_n((bool *)fld, 100 * 100, false);
cin >> W >> H;
for (int i = 0; i < N; i++) {
int X, Y;
cin >> X >> Y;
X--;
Y--;
fld[Y][X] = true;
}
cin >> S >> T;
int ma = 0;
for (int i = 0; i <= H - T; i++) {
for (int j = 0; j <= W - S; j++) {
ma = max(ma, Count(j, i));
}
}
printf("%lld\n", ma);
}
} | replace | 22 | 23 | 22 | 23 | -11 | |
p00707 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int W, H;
char field[100][100];
string dp[100][100];
bool compare(const string &s1, const string &s2) {
if (s1.size() != s2.size())
return !(s1.size() > s2.size());
else
return !(s1 > s2);
}
void output() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cout << dp[i][j] << ' ';
}
cout << endl;
}
}
void init() {
for (int i = 0; i < 71; i++) {
for (int j = 0; j < 71; j++) {
dp[i][j] = "";
}
}
}
string adjust(string s) {
if (s.size() == 1)
return s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0')
continue;
else {
// cout << s.substr(i) << endl;
return s.substr(i);
}
}
}
int main() {
while (cin >> W >> H) {
if (W == 0 && H == 0)
break;
init();
vector<string> vec;
vec.clear();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
}
}
string ans = "";
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (!isdigit(field[i][j]))
continue;
if (i == 0 && j == 0)
dp[i][j] = field[i][j];
else if (i == 0)
dp[i][j] = dp[i][j - 1] + field[i][j];
else if (j == 0)
dp[i][j] = dp[i - 1][j] + field[i][j];
else
dp[i][j] = max(dp[i][j - 1] + field[i][j], dp[i - 1][j] + field[i][j],
compare);
vec.push_back(dp[i][j]);
}
}
for (int i = 0; i < vec.size(); i++) {
vec[i] = adjust(vec[i]);
ans = max(ans, vec[i], compare);
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
int W, H;
char field[100][100];
string dp[100][100];
bool compare(const string &s1, const string &s2) {
if (s1.size() != s2.size())
return !(s1.size() > s2.size());
else
return !(s1 > s2);
}
void output() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cout << dp[i][j] << ' ';
}
cout << endl;
}
}
void init() {
for (int i = 0; i < 71; i++) {
for (int j = 0; j < 71; j++) {
dp[i][j] = "";
}
}
}
string adjust(string s) {
if (s.size() == 1)
return s;
bool f = false;
for (int i = 0; i < s.size(); i++) {
if (s[i] != '0')
f = true;
}
if (!f)
return "0";
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0')
continue;
else {
// cout << s.substr(i) << endl;
return s.substr(i);
}
}
}
int main() {
while (cin >> W >> H) {
if (W == 0 && H == 0)
break;
init();
vector<string> vec;
vec.clear();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> field[i][j];
}
}
string ans = "";
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (!isdigit(field[i][j]))
continue;
if (i == 0 && j == 0)
dp[i][j] = field[i][j];
else if (i == 0)
dp[i][j] = dp[i][j - 1] + field[i][j];
else if (j == 0)
dp[i][j] = dp[i - 1][j] + field[i][j];
else
dp[i][j] = max(dp[i][j - 1] + field[i][j], dp[i - 1][j] + field[i][j],
compare);
vec.push_back(dp[i][j]);
}
}
for (int i = 0; i < vec.size(); i++) {
vec[i] = adjust(vec[i]);
ans = max(ans, vec[i], compare);
}
cout << ans << endl;
}
return 0;
} | insert | 39 | 39 | 39 | 46 | 0 | |
p00707 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int WH = 72;
char board[WH];
string dp[WH];
bool comp(const string &a, const string &b) {
if (a.length() == b.length()) {
return a < b;
}
return a.length() < b.length();
}
int main() {
int w, h, m;
string r;
while (cin >> w >> h, w) {
r = "";
for (int i = 0; i < w * h; ++i) {
cin >> board[i];
}
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
dp[i * w + j] = "";
if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
dp[i * w + j] = dp[(i - 1) * w + j];
}
if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
}
if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
if (dp[i * w + j] != "" || board[i * w + j] != '0') {
dp[i * w + j] += board[i * w + j];
}
} else
dp[i * w + j] = "";
r = max(r, dp[i * w + j], comp);
}
}
cout << r << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int WH = 4900;
char board[WH];
string dp[WH];
bool comp(const string &a, const string &b) {
if (a.length() == b.length()) {
return a < b;
}
return a.length() < b.length();
}
int main() {
int w, h, m;
string r;
while (cin >> w >> h, w) {
r = "";
for (int i = 0; i < w * h; ++i) {
cin >> board[i];
}
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
dp[i * w + j] = "";
if (i - 1 >= 0 && dp[(i - 1) * w + j] != "") {
dp[i * w + j] = dp[(i - 1) * w + j];
}
if (j - 1 >= 0 && dp[i * w + (j - 1)] != "") {
dp[i * w + j] = max(dp[i * w + j], dp[i * w + (j - 1)], comp);
}
if ('0' <= board[i * w + j] && board[i * w + j] <= '9') {
if (dp[i * w + j] != "" || board[i * w + j] != '0') {
dp[i * w + j] += board[i * w + j];
}
} else
dp[i * w + j] = "";
r = max(r, dp[i * w + j], comp);
}
}
cout << r << endl;
}
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
const int IINF = INT_MAX;
int H, W, mincost;
int P[10][10], bin[10], bc[(1 << 10)];
int put[10][10], counter[10][10];
bool can_put(int len, int x, int y) {
REP(i, y, y + len) REP(j, x, x + len) if (!P[i][j]) return false;
return true;
}
void dfs(int cur, int cost, int remain) {
if (cost >= mincost)
return;
// if( cost + ((remain>0)?1:0) >= mincost ) return;
if (cur >= H * W) {
mincost = min(mincost, cost);
return;
}
int x = cur % W, y = cur / W;
if (!P[y][x] || counter[y][x] == 1) {
dfs(cur + 1, cost, remain);
return;
}
if ((bin[y] >> x) & 1)
dfs(cur + 1, cost, remain);
int len = put[y][x];
int bitmask = ((1 << len) - 1) << x;
int buf[len];
int add = 0, nremain = remain;
rep(j, len) {
buf[j] = bin[y + j];
add += len - bc[((1 << len) - 1) & (bin[y + j] >> x)];
bin[y + j] |= bitmask;
}
nremain -= add;
if (add)
dfs(cur + 1, cost + 1, nremain);
rep(j, len) bin[y + j] = buf[j];
}
bool isValid(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
int main() {
rep(i, (1 << 10)) bc[i] = __builtin_popcount(i);
while (scanf("%d%d", &W, &H), W | H) {
rep(i, H) {
bin[i] = 0;
rep(j, W) {
cin >> P[i][j];
put[i][j] = 0;
}
}
rep(y, H) rep(x, W) if (P[y][x]) {
for (int len = min(H, W); len >= 1; len--) {
if (x + len - 1 < W && y + len - 1 < H) {
if (can_put(len, x, y)) {
put[y][x] = len;
break;
}
}
}
}
rep(i, H) rep(j, W) counter[i][j] = 0;
rep(i, H) rep(j, W) if (P[i][j]) {
REP(y, i, i + put[i][j]) REP(x, j, j + put[i][j]) { counter[y][x]++; }
}
rep(i, H) {
rep(j, W) {
if (counter[i][j] == 1) {
REP(y, i, i + put[i][j]) REP(x, j, j + put[i][j]) {
bin[y] |= (1 << x);
}
}
}
}
int add = 0;
int remain = 0;
mincost = 0;
rep(i, H) rep(j, W) remain += ((bin[i] >> j) & 1), mincost += P[i][j];
remain = mincost - remain;
dfs(0, 0, remain);
rep(i, H) rep(j, W) if (counter[i][j] == 1) add++;
cout << mincost + add << endl;
}
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)
using namespace std;
const int IINF = INT_MAX;
int H, W, mincost;
int P[10][10], bin[10], bc[(1 << 10)];
int put[10][10], counter[10][10];
bool can_put(int len, int x, int y) {
REP(i, y, y + len) REP(j, x, x + len) if (!P[i][j]) return false;
return true;
}
void dfs(int cur, int cost, int remain) {
if (cost >= mincost)
return;
if (cost + ((remain > 0) ? 1 : 0) >= mincost)
return;
if (cur >= H * W) {
mincost = min(mincost, cost);
return;
}
int x = cur % W, y = cur / W;
if (!P[y][x] || counter[y][x] == 1) {
dfs(cur + 1, cost, remain);
return;
}
if ((bin[y] >> x) & 1)
dfs(cur + 1, cost, remain);
int len = put[y][x];
int bitmask = ((1 << len) - 1) << x;
int buf[len];
int add = 0, nremain = remain;
rep(j, len) {
buf[j] = bin[y + j];
add += len - bc[((1 << len) - 1) & (bin[y + j] >> x)];
bin[y + j] |= bitmask;
}
nremain -= add;
if (add)
dfs(cur + 1, cost + 1, nremain);
rep(j, len) bin[y + j] = buf[j];
}
bool isValid(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
int main() {
rep(i, (1 << 10)) bc[i] = __builtin_popcount(i);
while (scanf("%d%d", &W, &H), W | H) {
rep(i, H) {
bin[i] = 0;
rep(j, W) {
cin >> P[i][j];
put[i][j] = 0;
}
}
rep(y, H) rep(x, W) if (P[y][x]) {
for (int len = min(H, W); len >= 1; len--) {
if (x + len - 1 < W && y + len - 1 < H) {
if (can_put(len, x, y)) {
put[y][x] = len;
break;
}
}
}
}
rep(i, H) rep(j, W) counter[i][j] = 0;
rep(i, H) rep(j, W) if (P[i][j]) {
REP(y, i, i + put[i][j]) REP(x, j, j + put[i][j]) { counter[y][x]++; }
}
rep(i, H) {
rep(j, W) {
if (counter[i][j] == 1) {
REP(y, i, i + put[i][j]) REP(x, j, j + put[i][j]) {
bin[y] |= (1 << x);
}
}
}
}
int add = 0;
int remain = 0;
mincost = 0;
rep(i, H) rep(j, W) remain += ((bin[i] >> j) & 1), mincost += P[i][j];
remain = mincost - remain;
dfs(0, 0, remain);
rep(i, H) rep(j, W) if (counter[i][j] == 1) add++;
cout << mincost + add << endl;
}
return 0;
} | replace | 21 | 22 | 21 | 23 | TLE | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];
void dfs(int d, bitset<100> s, int sco) {
if (d == n) {
ans = min(ans, sco);
return;
}
cnt++;
if (cnt >= 100000000)
return;
if (ans <= sco)
return;
if (!A[d / w][d % w])
dfs(d + 1, s, sco);
else {
int y = d / w;
int x = d % w;
if ((B[y][x] & s).any()) {
bitset<100> p;
p.set(y * 10 + x); //(y*100+x);
if (!(s & p).any())
dfs(d + 1, s, sco);
dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
} else
dfs(d + 1, s, sco);
}
}
int main() {
while (cin >> w >> h, w) {
memset(A, 0, sizeof(A));
r(i, 11) r(j, 11) B[i][j] &= 0;
n = h * w;
bitset<100> b;
r(i, h) r(j, w) cin >> A[i][j];
r(i, h) r(j, w) if (A[i][j]) {
int sum = 1;
for (;; sum++) {
int f = 0;
for (int y = i; y < i + sum; y++) {
for (int x = j; x < j + sum; x++) {
if (A[y][x] == 0)
f++;
}
}
if (f) {
sum--;
break;
}
}
bitset<100> tmp;
for (int y = i, k = 0; k < sum; k++, y++) {
for (int x = j, l = 0; l < sum; l++, x++) {
tmp.set(y * 10 + x);
}
}
b.set(i * 10 + j);
B[i][j] = tmp;
}
ans = 100;
cnt = 0;
dfs(0, b, 0);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];
void dfs(int d, bitset<100> s, int sco) {
if (d == n) {
ans = min(ans, sco);
return;
}
cnt++;
if (cnt >= 30000000)
return;
if (ans <= sco)
return;
if (!A[d / w][d % w])
dfs(d + 1, s, sco);
else {
int y = d / w;
int x = d % w;
if ((B[y][x] & s).any()) {
bitset<100> p;
p.set(y * 10 + x); //(y*100+x);
if (!(s & p).any())
dfs(d + 1, s, sco);
dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
} else
dfs(d + 1, s, sco);
}
}
int main() {
while (cin >> w >> h, w) {
memset(A, 0, sizeof(A));
r(i, 11) r(j, 11) B[i][j] &= 0;
n = h * w;
bitset<100> b;
r(i, h) r(j, w) cin >> A[i][j];
r(i, h) r(j, w) if (A[i][j]) {
int sum = 1;
for (;; sum++) {
int f = 0;
for (int y = i; y < i + sum; y++) {
for (int x = j; x < j + sum; x++) {
if (A[y][x] == 0)
f++;
}
}
if (f) {
sum--;
break;
}
}
bitset<100> tmp;
for (int y = i, k = 0; k < sum; k++, y++) {
for (int x = j, l = 0; l < sum; l++, x++) {
tmp.set(y * 10 + x);
}
}
b.set(i * 10 + j);
B[i][j] = tmp;
}
ans = 100;
cnt = 0;
dfs(0, b, 0);
cout << ans << endl;
}
}
| replace | 13 | 14 | 13 | 14 | TLE | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int w, h;
int p[11][11];
int cnt[11][11];
int res;
struct square {
int x, y, si;
square() {}
square(int xx, int yy, int ss) {
x = xx;
y = yy;
si = ss;
}
bool operator<(const square &s) const { return si < s.si; }
};
vector<square> sq;
vector<square> usq;
bool check(int s, int y, int x) {
for (int i = y; i < y + s; i++) {
for (int j = x; j < x + s; j++) {
if (i >= h || j >= w)
return false;
if (p[i][j] == 0)
return false;
}
}
return true;
}
bool cover(square a, square b) {
bool flag = false;
bool flag2 = false;
if (a.x <= b.x && b.x + b.si <= a.x + a.si)
flag = true;
if (a.y <= b.y && b.y + b.si <= a.y + a.si)
flag2 = true;
return (flag && flag2);
}
int hstar(int v) {
int n = 0;
int val[101];
int c = 0;
for (; v < usq.size(); v++) {
val[c] = 0;
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
if (p[j][i] == 1)
val[c]++;
}
}
c++;
}
int rest = 0;
sort(val, val + c, greater<int>());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
rest += p[i][j];
}
}
int l = 0;
for (l = 0; rest > 0; l++) {
rest -= val[l];
}
return l;
}
bool empty() {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (p[i][j] == 1)
return false;
}
}
return true;
}
void update(int v, int c) {
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
cnt[j][i] += c;
}
}
}
void dfs(int v, int sum) {
if (empty()) {
res = min(sum, res);
return;
}
if (v == (int)usq.size())
return;
if (sum + hstar(v) >= res)
return;
bool ok1 = false, ok2 = false;
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
if (p[j][i] == 1) {
ok1 = true;
if (cnt[j][i] == 1)
ok2 = true;
}
}
}
update(v, -1);
if (ok1) {
int tmp[11][11];
memcpy(tmp, p, sizeof(p));
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
p[j][i] = 0;
}
}
dfs(v + 1, sum + 1);
memcpy(p, tmp, sizeof(p));
}
if (!ok2) {
dfs(v + 1, sum);
}
update(v, 1);
}
int main(void) {
while (1) {
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
sq.clear();
usq.clear();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &p[i][j]);
}
}
for (int i = 1; i <= min(w, h); i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
if (check(i, j, k)) {
sq.push_back(square(k, j, i));
}
}
}
}
sort(sq.begin(), sq.end());
for (int i = (int)sq.size() - 1; i >= 0; i--) {
bool flag = true;
for (int j = 0; j < usq.size(); j++) {
if (cover(usq[j], sq[i]))
flag = false;
}
if (flag)
usq.push_back(sq[i]);
}
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < usq.size(); i++) {
update(i, 1);
}
res = usq.size();
dfs(0, 0);
printf("%d\n", res);
}
return 0;
} | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int w, h;
int p[11][11];
int cnt[11][11];
int res;
struct square {
int x, y, si;
square() {}
square(int xx, int yy, int ss) {
x = xx;
y = yy;
si = ss;
}
bool operator<(const square &s) const { return si < s.si; }
};
vector<square> sq;
vector<square> usq;
bool check(int s, int y, int x) {
for (int i = y; i < y + s; i++) {
for (int j = x; j < x + s; j++) {
if (i >= h || j >= w)
return false;
if (p[i][j] == 0)
return false;
}
}
return true;
}
bool cover(square a, square b) {
bool flag = false;
bool flag2 = false;
if (a.x <= b.x && b.x + b.si <= a.x + a.si)
flag = true;
if (a.y <= b.y && b.y + b.si <= a.y + a.si)
flag2 = true;
return (flag && flag2);
}
int hstar(int v) {
int n = 0;
int val[101];
int c = 0;
for (; v < usq.size(); v++) {
val[c] = 0;
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
if (p[j][i] == 1)
val[c]++;
}
}
c++;
}
int rest = 0;
sort(val, val + c, greater<int>());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
rest += p[i][j];
}
}
int l = 0;
for (l = 0; rest > 0; l++) {
rest -= val[l];
}
return l;
}
bool empty() {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (p[i][j] == 1)
return false;
}
}
return true;
}
void update(int v, int c) {
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
cnt[j][i] += c;
}
}
}
void dfs(int v, int sum) {
if (empty()) {
res = min(sum, res);
return;
}
if (v == (int)usq.size())
return;
if (sum + hstar(v) >= res)
return;
bool ok1 = false, ok2 = false;
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
if (p[j][i] == 1) {
ok1 = true;
if (cnt[j][i] == 1)
ok2 = true;
}
}
}
update(v, -1);
if (ok1) {
int tmp[11][11];
memcpy(tmp, p, sizeof(p));
for (int i = usq[v].x; i < usq[v].x + usq[v].si; i++) {
for (int j = usq[v].y; j < usq[v].y + usq[v].si; j++) {
p[j][i] = 0;
}
}
dfs(v + 1, sum + 1);
memcpy(p, tmp, sizeof(p));
}
if (!ok2) {
dfs(v + 1, sum);
}
update(v, 1);
}
int main(void) {
while (1) {
scanf("%d%d", &w, &h);
if (w == 0 && h == 0)
break;
sq.clear();
usq.clear();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &p[i][j]);
}
}
for (int i = 1; i <= min(w, h); i++) {
for (int j = 0; j < h; j++) {
for (int k = 0; k < w; k++) {
if (check(i, j, k)) {
sq.push_back(square(k, j, i));
}
}
}
}
sort(sq.begin(), sq.end());
for (int i = (int)sq.size() - 1; i >= 0; i--) {
bool flag = true;
for (int j = 0; j < usq.size(); j++) {
if (cover(usq[j], sq[i]))
flag = false;
}
if (flag)
usq.push_back(sq[i]);
}
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < usq.size(); i++) {
update(i, 1);
}
res = usq.size();
sort(usq.begin(), usq.end());
dfs(0, 0);
printf("%d\n", res);
}
return 0;
} | insert | 167 | 167 | 167 | 168 | TLE | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1 << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int inf = 1 << 28;
int h, w;
int board[10];
int len[15][15];
int min_res = 0;
int dfs(int y, int x, int d, int remain) {
if (d + (remain ? 1 : 0) > min_res)
return inf;
if (y == h) {
if (min_res > d)
min_res = d;
return d;
}
if (x >= w)
return dfs(y + 1, 0, d, remain);
if (len[y][x] == 0)
return dfs(y, x + 1, d, remain);
int ret = inf;
if ((board[y] & bit(x)) == 0) {
int cur = dfs(y, x + 1, d, remain);
if (ret > cur)
ret = cur;
}
int tboard[10], l = len[y][x];
rep(i, y, y + l) tboard[i] = board[i];
int nremain = remain;
int mask = ((1 << l) - 1) << x;
rep(a, y, y + l) {
nremain -= popcount(board[a] & mask);
board[a] &= (1023 ^ mask);
}
if (remain != nremain) {
int cur = dfs(y, x + 1, d + 1, nremain);
if (ret > cur)
ret = cur;
}
rep(i, y, y + l) board[i] = tboard[i];
return ret;
}
int main(void) {
while (1) {
scanf("%d %d", &w, &h);
if (w == 0)
break;
rep(i, h) {
board[i] = 0;
rep(j, w) {
int tmp;
scanf("%d ", &tmp);
board[i] |= (tmp << j);
}
}
rep(i, h) rep(j, w) {
len[i][j] = 0;
if ((board[i] & bit(j)) == 0)
continue;
len[i][j] = min(h - i, w - j);
const int l = len[i][j];
rep(a, i, i + l) rep(b, j, j + l) if ((board[a] & bit(b)) == 0)
len[i][j] = min(len[i][j], max(a - i, b - j));
}
int num[10][10], sy[10][10], sx[10][10];
rep(i, h) rep(j, w) num[i][j] = 0;
rep(i, h) rep(j, w) {
const int l = len[i][j];
rep(a, i, i + l) rep(b, j, j + l) num[a][b]++, sy[a][b] = i, sx[a][b] = j;
}
int add = 0;
rep(i, h) rep(j, w) {
if (num[i][j] != 1)
continue;
const int cy = sy[i][j], cx = sx[i][j], l = len[cy][cx],
mask = ((1 << l) - 1) << cx;
rep(a, cy, cy + l) board[a] &= (1023 ^ mask);
len[cy][cx] = 0, add++;
}
int remain = 0;
rep(i, h) remain += popcount(board[i]);
min_res = min(21, remain);
printf("%d\n", dfs(0, 0, 0, remain) + add);
}
return 0;
} | #include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1 << (n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int inf = 1 << 28;
int h, w;
int board[10];
int len[15][15];
int min_res = 0;
int dfs(int y, int x, int d, int remain) {
if (d + (remain ? 1 : 0) > min_res)
return inf;
if (y == h) {
if (min_res > d)
min_res = d;
return d;
}
if (x >= w)
return dfs(y + 1, 0, d, remain);
if (len[y][x] == 0)
return dfs(y, x + 1, d, remain);
int ret = inf;
if ((board[y] & bit(x)) == 0) {
int cur = dfs(y, x + 1, d, remain);
if (ret > cur)
ret = cur;
}
int tboard[10], l = len[y][x];
rep(i, y, y + l) tboard[i] = board[i];
int nremain = remain;
int mask = ((1 << l) - 1) << x;
rep(a, y, y + l) {
nremain -= popcount(board[a] & mask);
board[a] &= (1023 ^ mask);
}
if (remain != nremain) {
int cur = dfs(y, x + 1, d + 1, nremain);
if (ret > cur)
ret = cur;
}
rep(i, y, y + l) board[i] = tboard[i];
return ret;
}
int main(void) {
while (1) {
scanf("%d %d", &w, &h);
if (w == 0)
break;
rep(i, h) {
board[i] = 0;
rep(j, w) {
int tmp;
scanf("%d ", &tmp);
board[i] |= (tmp << j);
}
}
rep(i, h) rep(j, w) {
len[i][j] = 0;
if ((board[i] & bit(j)) == 0)
continue;
len[i][j] = min(h - i, w - j);
const int l = len[i][j];
rep(a, i, i + l) rep(b, j, j + l) if ((board[a] & bit(b)) == 0)
len[i][j] = min(len[i][j], max(a - i, b - j));
}
int num[10][10], sy[10][10], sx[10][10];
rep(i, h) rep(j, w) num[i][j] = 0;
rep(i, h) rep(j, w) {
const int l = len[i][j];
rep(a, i, i + l) rep(b, j, j + l) num[a][b]++, sy[a][b] = i, sx[a][b] = j;
}
int add = 0;
rep(i, h) rep(j, w) {
if (num[i][j] != 1)
continue;
const int cy = sy[i][j], cx = sx[i][j], l = len[cy][cx],
mask = ((1 << l) - 1) << cx;
rep(a, cy, cy + l) board[a] &= (1023 ^ mask);
len[cy][cx] = 0, add++;
}
int remain = 0;
rep(i, h) remain += popcount(board[i]);
min_res = min(20, remain);
int ans = dfs(0, 0, 0, remain);
if (ans == inf)
ans = 21;
printf("%d\n", ans + add);
}
return 0;
} | replace | 116 | 118 | 116 | 121 | TLE | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define range(x, l, r) (l <= x && x < r)
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> tri;
int w, h;
int g[20][20];
int rem[20][20];
vector<tri> carpet[20][20];
int depth;
void search(int d) {
if (d == depth)
return;
int y = -1, x = -1, mink = h * w;
vector<tri> priority;
rep(i, h) rep(j, w) {
if (rem[i][j] && mink > (int)carpet[i][j].size()) {
y = i;
x = j;
}
}
if (y < 0) {
depth = d;
return;
}
rep(k, carpet[y][x].size()) {
tri t = carpet[y][x][k];
int tmp[20][20];
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
tmp[i][j] = rem[i][j];
}
}
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
rem[i][j] = 0;
}
}
search(d + 1);
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
rem[i][j] = tmp[i][j];
}
}
if (depth == d + 1)
return;
}
}
int main() {
while (cin >> w >> h, w) {
memset(g, 0, sizeof(g));
rep(i, h) rep(j, w) {
cin >> g[i][j];
rem[i][j] = g[i][j];
carpet[i][j].clear();
}
vector<tri> c;
rep(i, h) rep(j, w) {
int k = 0;
while (true) {
if (!range(i + k, 0, h) || !range(j + k, 0, w))
break;
int f = 1;
for (int y = i; y <= i + k; y++)
f &= g[y][j + k];
for (int x = j; x <= j + k; x++)
f &= g[i + k][x];
if (!f)
break;
k++;
}
if (k)
c.push_back(tri(pii(i, j), k));
}
rep(i, c.size()) {
bool f = false;
rep(j, c.size()) {
if (i == j)
continue;
if (range(c[i].fs.fs, c[j].fs.fs, c[j].fs.fs + c[j].sc) &&
range(c[i].fs.fs + c[i].sc, c[j].fs.fs, c[j].fs.fs + c[j].sc + 1) &&
range(c[i].fs.sc, c[j].fs.sc, c[j].fs.sc + c[j].sc) &&
range(c[i].fs.sc + c[i].sc, c[j].fs.sc, c[j].fs.sc + c[j].sc + 1)) {
f = true;
break;
}
}
if (f) {
c.erase(c.begin() + i);
i--;
}
}
rep(i, c.size()) {
for (int y = c[i].fs.fs; y < c[i].fs.fs + c[i].sc; y++) {
for (int x = c[i].fs.sc; x < c[i].fs.sc + c[i].sc; x++) {
carpet[y][x].push_back(c[i]);
}
}
}
int ans = 0;
depth = h * w;
search(0);
cout << ans + depth << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define range(x, l, r) (l <= x && x < r)
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> tri;
int w, h;
int g[20][20];
int rem[20][20];
vector<tri> carpet[20][20];
int depth;
void search(int d) {
if (d == depth)
return;
int y = -1, x = -1, mink = h * w;
vector<tri> priority;
rep(i, h) rep(j, w) {
if (rem[i][j] && mink > (int)carpet[i][j].size()) {
y = i;
x = j;
}
}
if (y < 0) {
depth = d;
return;
}
rep(k, carpet[y][x].size()) {
tri t = carpet[y][x][k];
int tmp[20][20];
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
tmp[i][j] = rem[i][j];
}
}
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
rem[i][j] = 0;
}
}
search(d + 1);
for (int i = t.fs.fs; i < t.fs.fs + t.sc; i++) {
for (int j = t.fs.sc; j < t.fs.sc + t.sc; j++) {
rem[i][j] = tmp[i][j];
}
}
if (depth == d + 1)
return;
}
}
int main() {
while (cin >> w >> h, w) {
memset(g, 0, sizeof(g));
rep(i, h) rep(j, w) {
cin >> g[i][j];
rem[i][j] = g[i][j];
carpet[i][j].clear();
}
vector<tri> c;
rep(i, h) rep(j, w) {
int k = 0;
while (true) {
if (!range(i + k, 0, h) || !range(j + k, 0, w))
break;
int f = 1;
for (int y = i; y <= i + k; y++)
f &= g[y][j + k];
for (int x = j; x <= j + k; x++)
f &= g[i + k][x];
if (!f)
break;
k++;
}
if (k)
c.push_back(tri(pii(i, j), k));
}
rep(i, c.size()) {
bool f = false;
rep(j, c.size()) {
if (i == j)
continue;
if (range(c[i].fs.fs, c[j].fs.fs, c[j].fs.fs + c[j].sc) &&
range(c[i].fs.fs + c[i].sc, c[j].fs.fs, c[j].fs.fs + c[j].sc + 1) &&
range(c[i].fs.sc, c[j].fs.sc, c[j].fs.sc + c[j].sc) &&
range(c[i].fs.sc + c[i].sc, c[j].fs.sc, c[j].fs.sc + c[j].sc + 1)) {
f = true;
break;
}
}
if (f) {
c.erase(c.begin() + i);
i--;
}
}
rep(i, c.size()) {
for (int y = c[i].fs.fs; y < c[i].fs.fs + c[i].sc; y++) {
for (int x = c[i].fs.sc; x < c[i].fs.sc + c[i].sc; x++) {
carpet[y][x].push_back(c[i]);
}
}
}
int ans = 0;
while (true) {
bool f = false;
rep(i, h) rep(j, w) {
if (rem[i][j] && carpet[i][j].size() == 1) {
f = true;
ans++;
tri t = carpet[i][j][0];
for (int y = t.fs.fs; y < t.fs.fs + t.sc; y++) {
for (int x = t.fs.sc; x < t.fs.sc + t.sc; x++) {
rem[y][x] = 0;
}
}
}
}
if (!f)
break;
}
depth = h * w;
search(0);
cout << ans + depth << endl;
}
} | insert | 118 | 118 | 118 | 135 | TLE | |
p00709 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
int H, W;
vec t;
map<vec, int> dp[10][10];
int mem[10][10][10][10];
bool check(int ay, int ax, int by, int bx) {
if (mem[ay][ax][by][bx] != -1)
return mem[ay][ax][by][bx];
mem[ay][ax][by][bx] = false;
for (int y = ay; y <= by; y++)
for (int x = ax; x <= bx; x++)
if ((t[y] >> x & 1) == 0)
return false;
mem[ay][ax][by][bx] = true;
return true;
}
int solve(vec p, int y, int x) {
if (y == H)
return 0;
if (x == W) {
return solve(p, y + 1, 0);
}
if (dp[y][x].count(p))
return dp[y][x][p];
int sum = 0;
for (int i = 0; i < H; i++)
sum += p[i];
if (sum == 0)
return 0;
int res = 1e9;
if ((p[y] >> x & 1) == 0)
res = solve(p, y, x + 1);
for (int i = 0;; i++) {
if (y + i >= H || x + i >= W)
break;
if (!check(y, x, y + i, x + i))
break;
int ma = (1 << (i + 1)) - 1;
vec next = p;
for (int j = 0; j <= i; j++) {
next[y + j] |= (ma << x);
next[y + j] -= (ma << x);
}
if (p == next)
continue;
res = min(res, solve(next, y, x + 1) + 1);
}
return dp[y][x][p] = res;
}
int main() {
while (1) {
cin >> W >> H;
if (H == 0 && W == 0)
break;
memset(mem, -1, sizeof(mem));
vec si;
for (int i = 0; i < H; i++) {
int bit = 0, a;
for (int j = 0; j < W; j++) {
dp[i][j].clear();
cin >> a;
if (a == 1)
bit |= (1 << j);
}
si.push_back(bit);
}
t = si;
cout << solve(si, 0, 0) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
int H, W;
vec t;
map<vec, int> dp[10][10];
int mem[10][10][10][10];
bool check(int ay, int ax, int by, int bx) {
if (mem[ay][ax][by][bx] != -1)
return mem[ay][ax][by][bx];
mem[ay][ax][by][bx] = false;
for (int y = ay; y <= by; y++)
for (int x = ax; x <= bx; x++)
if ((t[y] >> x & 1) == 0)
return false;
mem[ay][ax][by][bx] = true;
return true;
}
int solve(vec p, int y, int x) {
if (y == H)
return 0;
if (x == W) {
return solve(p, y + 1, 0);
}
if (dp[y][x].count(p))
return dp[y][x][p];
int sum = 0;
for (int i = 0; i < H; i++)
sum += p[i];
if (sum == 0)
return 0;
int res = 1e9;
if ((p[y] >> x & 1) == 0)
res = solve(p, y, x + 1);
for (int i = 0;; i++) {
if (y + i >= H || x + i >= W)
break;
if (!check(y, x, y + i, x + i))
break;
if (y + i + 1 < H && x + i + 1 < W && check(y, x, y + i + 1, x + i + 1))
continue;
int ma = (1 << (i + 1)) - 1;
vec next = p;
for (int j = 0; j <= i; j++) {
next[y + j] |= (ma << x);
next[y + j] -= (ma << x);
}
if (p == next)
continue;
res = min(res, solve(next, y, x + 1) + 1);
}
return dp[y][x][p] = res;
}
int main() {
while (1) {
cin >> W >> H;
if (H == 0 && W == 0)
break;
memset(mem, -1, sizeof(mem));
vec si;
for (int i = 0; i < H; i++) {
int bit = 0, a;
for (int j = 0; j < W; j++) {
dp[i][j].clear();
cin >> a;
if (a == 1)
bit |= (1 << j);
}
si.push_back(bit);
}
t = si;
cout << solve(si, 0, 0) << endl;
}
return 0;
} | insert | 49 | 49 | 49 | 51 | TLE | |
p00710 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int main() {
int n, r;
while (cin >> n >> r, n + r) {
int huda[51];
REP(i, n) { huda[i] = n - i; }
while (r--) {
int p, c;
int tmp[c];
cin >> p >> c;
REP(i, c) { tmp[i] = huda[p + i - 1]; }
REPR(i, p - 2) { huda[i + c] = huda[i]; }
REP(i, c) { huda[i] = tmp[i]; }
}
cout << huda[0] << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int main() {
int n, r;
while (cin >> n >> r, n + r) {
int huda[51];
REP(i, n) { huda[i] = n - i; }
while (r--) {
int p, c;
int tmp[51];
cin >> p >> c;
REP(i, c) { tmp[i] = huda[p + i - 1]; }
REPR(i, p - 2) { huda[i + c] = huda[i]; }
REP(i, c) { huda[i] = tmp[i]; }
}
cout << huda[0] << endl;
}
} | replace | 43 | 44 | 43 | 44 | 0 | |
p00710 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> cards;
vector<int>::iterator cardsIterator;
vector<int>::iterator shuffuleIterator;
int n, r, p, c;
while (cin >> n >> r, n && r) {
cards.clear(); // 初期化
// カードの山生成
for (int i = 0; i < n; ++i) {
cards.push_back(i + 1);
}
// シャッフル
while (cin >> p >> c) {
cardsIterator = cards.end();
cardsIterator -= p + (c - 1);
while (c--) {
cards.push_back(*cardsIterator);
cards.erase(cardsIterator);
}
if (r == 0) {
break;
}
}
// 一番上のカード表示
cout << cards.back() << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> cards;
vector<int>::iterator cardsIterator;
vector<int>::iterator shuffuleIterator;
int n, r, p, c;
while (cin >> n >> r, n && r) {
cards.clear(); // 初期化
// カードの山生成
for (int i = 0; i < n; ++i) {
cards.push_back(i + 1);
}
// シャッフル
while (cin >> p >> c, r--) {
cardsIterator = cards.end();
cardsIterator -= p + (c - 1);
while (c--) {
cards.push_back(*cardsIterator);
cards.erase(cardsIterator);
}
if (r == 0) {
break;
}
}
// 一番上のカード表示
cout << cards.back() << endl;
}
} | replace | 18 | 19 | 18 | 19 | TLE | |
p00710 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
int main(void) {
int ans[100], cnt = 0;
while (1) {
int n, r;
cin >> n >> r;
if (n == 0)
break;
int p[50], c[50];
irep(i, r) cin >> p[i] >> c[i]; //?????????????????????
int pos = 0;
rep(i, r) {
if (pos < c[i]) {
pos += p[i] - 1;
} else if (pos < c[i] + p[i] - 1) {
pos -= c[i];
}
}
ans[cnt] = n - pos;
cnt++;
}
rep(i, cnt) cout << ans[i] << endl;
return 0;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
int main(void) {
int ans[1000], cnt = 0;
while (1) {
int n, r;
cin >> n >> r;
if (n == 0)
break;
int p[50], c[50];
irep(i, r) cin >> p[i] >> c[i]; //?????????????????????
int pos = 0;
rep(i, r) {
if (pos < c[i]) {
pos += p[i] - 1;
} else if (pos < c[i] + p[i] - 1) {
pos -= c[i];
}
}
ans[cnt] = n - pos;
cnt++;
}
rep(i, cnt) cout << ans[i] << endl;
return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p00710 | C++ | Runtime Error | #include <iostream>
#define N 10
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp
*/
int array[N];
void makearray(int n) {
for (int i = 0; i < N; i++) {
array[i] = n;
if (n > 0)
n -= 1;
}
}
void shuffle(int p, int c) {
int sarray[N];
// copy unshuffled deck
for (int i = p - 1 + c; i < N; i++) {
sarray[i] = array[i];
}
// copy the c part
int j = 0;
for (int i = p - 1; i < p - 1 + c; i++) {
sarray[j] = array[i];
j++;
}
// copy the p part
j = c;
for (int i = 0; i < p - 1; i++) {
sarray[j] = array[i];
j++;
}
for (int i = 0; i < N; i++) {
array[i] = sarray[i];
}
}
int main() {
int n, r, p, c;
while (1) {
cin >> n >> r;
makearray(n);
if (n == 0 && r == 0)
break;
for (int i = 0; i < r; i++) {
cin >> p >> c;
shuffle(p, c);
}
cout << array[0] << endl;
}
} | #include <iostream>
#define N 50
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp
*/
int array[N];
void makearray(int n) {
for (int i = 0; i < N; i++) {
array[i] = n;
if (n > 0)
n -= 1;
}
}
void shuffle(int p, int c) {
int sarray[N];
// copy unshuffled deck
for (int i = p - 1 + c; i < N; i++) {
sarray[i] = array[i];
}
// copy the c part
int j = 0;
for (int i = p - 1; i < p - 1 + c; i++) {
sarray[j] = array[i];
j++;
}
// copy the p part
j = c;
for (int i = 0; i < p - 1; i++) {
sarray[j] = array[i];
j++;
}
for (int i = 0; i < N; i++) {
array[i] = sarray[i];
}
}
int main() {
int n, r, p, c;
while (1) {
cin >> n >> r;
makearray(n);
if (n == 0 && r == 0)
break;
for (int i = 0; i < r; i++) {
cin >> p >> c;
shuffle(p, c);
}
cout << array[0] << endl;
}
} | replace | 1 | 2 | 1 | 2 | 0 | |
p00711 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];
int dfs(int y, int x) {
board[y][x] = '#';
int ret = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
ret += dfs(y, x);
}
}
return ret;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
cin >> board[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (board[i][j] == '@') {
cout << dfs(i, j) << endl;
}
}
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];
int dfs(int y, int x) {
board[y][x] = '#';
int ret = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
ret += dfs(ny, nx);
}
}
return ret;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
cin >> board[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (board[i][j] == '@') {
cout << dfs(i, j) << endl;
}
}
}
}
return 0;
}
| replace | 14 | 15 | 14 | 15 | -11 | |
p00711 | C++ | Runtime Error | #include <cassert>
#include <iostream>
#define REP(i, l, n) for (int i = l; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define MAX 20
int ans, baseX, baseY;
int w, h;
char tile[MAX][MAX];
void seek(int x, int y);
using namespace std;
int main() {
while (1) {
cin >> w;
cin >> h;
if (w == 0 && h == 0)
return 0;
rep(i, h) {
rep(j, w) {
cin >> tile[i][j];
if (tile[i][j] == '@') {
baseX = i;
baseY = j;
}
}
tile[i][w] = '#';
}
rep(i, w) tile[h][i] = '#';
ans = 1;
seek(baseX, baseY);
cout << ans << endl;
}
return 0;
}
void seek(int x, int y) {
if (tile[x + 1][y] == '.') {
tile[x + 1][y] = '#';
ans++;
seek(x + 1, y);
}
if (tile[x][y + 1] == '.') {
tile[x][y + 1] = '#';
ans++;
seek(x, y + 1);
}
if (tile[x - 1][y] == '.') {
tile[x - 1][y] = '#';
ans++;
seek(x - 1, y);
}
if (tile[x][y - 1] == '.') {
tile[x][y - 1] = '#';
ans++;
seek(x, y - 1);
}
} | #include <cassert>
#include <iostream>
#define REP(i, l, n) for (int i = l; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define MAX 21
int ans, baseX, baseY;
int w, h;
char tile[MAX][MAX];
void seek(int x, int y);
using namespace std;
int main() {
while (1) {
cin >> w;
cin >> h;
if (w == 0 && h == 0)
return 0;
rep(i, h) {
rep(j, w) {
cin >> tile[i][j];
if (tile[i][j] == '@') {
baseX = i;
baseY = j;
}
}
tile[i][w] = '#';
}
rep(i, w) tile[h][i] = '#';
ans = 1;
seek(baseX, baseY);
cout << ans << endl;
}
return 0;
}
void seek(int x, int y) {
if (tile[x + 1][y] == '.') {
tile[x + 1][y] = '#';
ans++;
seek(x + 1, y);
}
if (tile[x][y + 1] == '.') {
tile[x][y + 1] = '#';
ans++;
seek(x, y + 1);
}
if (tile[x - 1][y] == '.') {
tile[x - 1][y] = '#';
ans++;
seek(x - 1, y);
}
if (tile[x][y - 1] == '.') {
tile[x][y - 1] = '#';
ans++;
seek(x, y - 1);
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00711 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
const int NUM = 7;
void grooping(int y, int x, int w, int h, char **room, unsigned long ***group,
unsigned long *next) {
if (room[y][x] != '#') {
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i < 0 || j < 0 || i >= h || j >= w) {
continue;
}
if ((i == y && j != x) || (i != y && j == x)) {
for (int k = 0; k < NUM; k++) {
group[y][x][k] |= group[i][j][k];
}
}
}
}
for (int i = 0; i < NUM; i++) {
if (group[y][x][i] == 0UL && next[i] != 0UL) {
group[y][x][i] |= next[i];
next[i] <<= 1;
break;
}
}
}
}
int main() {
int w, h;
while (true) {
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
unsigned long next[NUM] = {1UL, 1UL, 1UL, 1UL, 1UL, 1UL, 1UL};
char **room = new char *[h];
unsigned long ***group = new unsigned long **[h];
int x, y;
for (int i = 0; i < h; i++) {
room[i] = new char[w];
group[i] = new unsigned long *[w];
for (int j = 0; j < w; j++) {
cin >> room[i][j];
if (room[i][j] == '@') {
x = j;
y = i;
}
group[i][j] = new unsigned long[NUM];
for (int k = 0; k < NUM; k++) {
group[i][j][k] = 0UL;
}
}
}
for (int k = 0; k < 210; k++) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
grooping(i, j, w, h, room, group, next);
}
}
for (int i = h - 1; i >= 0; i--) {
for (int j = w - 1; j >= 0; j--) {
grooping(i, j, w, h, room, group, next);
}
}
for (int j = 0; j < w; j++) {
for (int i = 0; i < h; i++) {
grooping(i, j, w, h, room, group, next);
}
}
for (int j = w - 1; j >= 0; j--) {
for (int i = h - 1; i >= 0; i--) {
grooping(i, j, w, h, room, group, next);
}
}
}
int count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < NUM; k++) {
if ((group[i][j][k] & group[y][x][k]) != 0UL) {
count++;
break;
}
}
}
}
cout << count << '\n';
for (int i = 0; i < h; i++) {
delete[] room[i];
for (int j = 0; j < w; j++) {
delete[] group[i][j];
}
delete[] group[i];
}
delete[] room;
delete[] group;
}
return 0;
} | #include <iostream>
using namespace std;
const int NUM = 7;
void grooping(int y, int x, int w, int h, char **room, unsigned long ***group,
unsigned long *next) {
if (room[y][x] != '#') {
for (int i = y - 1; i <= y + 1; i++) {
for (int j = x - 1; j <= x + 1; j++) {
if (i < 0 || j < 0 || i >= h || j >= w) {
continue;
}
if ((i == y && j != x) || (i != y && j == x)) {
for (int k = 0; k < NUM; k++) {
group[y][x][k] |= group[i][j][k];
}
}
}
}
for (int i = 0; i < NUM; i++) {
if (group[y][x][i] == 0UL && next[i] != 0UL) {
group[y][x][i] |= next[i];
next[i] <<= 1;
break;
}
}
}
}
int main() {
int w, h;
while (true) {
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
unsigned long next[NUM] = {1UL, 1UL, 1UL, 1UL, 1UL, 1UL, 1UL};
char **room = new char *[h];
unsigned long ***group = new unsigned long **[h];
int x, y;
for (int i = 0; i < h; i++) {
room[i] = new char[w];
group[i] = new unsigned long *[w];
for (int j = 0; j < w; j++) {
cin >> room[i][j];
if (room[i][j] == '@') {
x = j;
y = i;
}
group[i][j] = new unsigned long[NUM];
for (int k = 0; k < NUM; k++) {
group[i][j][k] = 0UL;
}
}
}
for (int k = 0; k < 180; k++) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
grooping(i, j, w, h, room, group, next);
}
}
for (int i = h - 1; i >= 0; i--) {
for (int j = w - 1; j >= 0; j--) {
grooping(i, j, w, h, room, group, next);
}
}
for (int j = 0; j < w; j++) {
for (int i = 0; i < h; i++) {
grooping(i, j, w, h, room, group, next);
}
}
for (int j = w - 1; j >= 0; j--) {
for (int i = h - 1; i >= 0; i--) {
grooping(i, j, w, h, room, group, next);
}
}
}
int count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < NUM; k++) {
if ((group[i][j][k] & group[y][x][k]) != 0UL) {
count++;
break;
}
}
}
}
cout << count << '\n';
for (int i = 0; i < h; i++) {
delete[] room[i];
for (int j = 0; j < w; j++) {
delete[] group[i][j];
}
delete[] group[i];
}
delete[] room;
delete[] group;
}
return 0;
} | replace | 58 | 59 | 58 | 59 | TLE | |
p00711 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> f;
void init() { f.clear(); }
int input() {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0)
return false;
string s;
for (int i = 0; i < h; i++) {
cin >> s;
f.push_back(s);
}
return true;
}
int vx[] = {0, 0, 1, -1};
int vy[] = {1, -1, 0, 0};
int rec(int x, int y) {
if (f[x][y] != '.')
return 0;
f[x][y] = '#';
int res = 1;
for (int i = 0; i < 4; i++) {
res += rec(x + vx[i], y + vy[i]);
}
return res;
}
int solve() {
int x, y;
for (int i = 0; i < f.size(); i++) {
for (int j = 0; j < f[i].size(); j++) {
if (f[i][j] == '@') {
x = i;
y = j;
f[i][j] = '.';
}
}
}
return rec(x, y);
}
int main() {
while (init(), input()) {
cout << solve() << endl;
}
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> f;
void init() { f.clear(); }
int input() {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0)
return false;
string s;
for (int i = 0; i < h; i++) {
cin >> s;
f.push_back(s);
}
return true;
}
int vx[] = {0, 0, 1, -1};
int vy[] = {1, -1, 0, 0};
int rec(int x, int y) {
if (x < 0 || f.size() <= x)
return 0;
if (y < 0 || f[x].size() <= y)
return 0;
if (f[x][y] != '.')
return 0;
f[x][y] = '#';
int res = 1;
for (int i = 0; i < 4; i++) {
res += rec(x + vx[i], y + vy[i]);
}
return res;
}
int solve() {
int x, y;
for (int i = 0; i < f.size(); i++) {
for (int j = 0; j < f[i].size(); j++) {
if (f[i][j] == '@') {
x = i;
y = j;
f[i][j] = '.';
}
}
}
return rec(x, y);
}
int main() {
while (init(), input()) {
cout << solve() << endl;
}
} | insert | 25 | 25 | 25 | 29 | -11 | |
p00711 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, N, k) for (int i = k; i < (int)(N); i++)
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
int main() {
ifstream in("b_in.txt");
cin.rdbuf(in.rdbuf());
int w, h;
while (cin >> w >> h, w) {
int sx, sy;
string s[30];
rep(i, h) cin >> s[i];
rep(y, h) rep(x, w) if (s[y][x] == '@') s[y][x] = '.', sx = x, sy = y;
queue<pii> que;
que.push(pii(sx, sy));
bool u[30][30] = {};
u[sy][sx] = true;
int cnt = 1;
while (!que.empty()) {
int x, y;
tie(x, y) = que.front();
que.pop();
rep(i, 4) {
const int dx[] = {0, 1, 0, -1, 0};
int nx = x + dx[i];
int ny = y + dx[i + 1];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
if (s[ny][nx] == '#')
continue;
if (u[ny][nx])
continue;
u[ny][nx] = true;
cnt += 1;
que.push(pii(nx, ny));
}
}
cout << cnt << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, N, k) for (int i = k; i < (int)(N); i++)
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
int main() {
// ifstream in("b_in.txt");
// cin.rdbuf(in.rdbuf());
int w, h;
while (cin >> w >> h, w) {
int sx, sy;
string s[30];
rep(i, h) cin >> s[i];
rep(y, h) rep(x, w) if (s[y][x] == '@') s[y][x] = '.', sx = x, sy = y;
queue<pii> que;
que.push(pii(sx, sy));
bool u[30][30] = {};
u[sy][sx] = true;
int cnt = 1;
while (!que.empty()) {
int x, y;
tie(x, y) = que.front();
que.pop();
rep(i, 4) {
const int dx[] = {0, 1, 0, -1, 0};
int nx = x + dx[i];
int ny = y + dx[i + 1];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
if (s[ny][nx] == '#')
continue;
if (u[ny][nx])
continue;
u[ny][nx] = true;
cnt += 1;
que.push(pii(nx, ny));
}
}
cout << cnt << endl;
}
return 0;
}
| replace | 14 | 16 | 14 | 16 | 0 | |
p00711 | C++ | Runtime Error | #include <iostream>
#include <queue>
using namespace std;
const int MAX_SIZE = 20;
typedef pair<int, int> P;
int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
int n = 0;
while (q.size()) {
n++;
P p = q.front();
q.pop();
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
for (int k = 0; k < 4; ++k) {
int i = p.first + di[k];
int j = p.second + dj[k];
if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
f[i][j] = '#';
q.push(P(i, j));
}
}
}
return n;
}
int main() {
int W, H;
char f[MAX_SIZE][MAX_SIZE];
while (true) {
queue<P> q;
cin >> W >> H;
if (W == 0 && H == 0)
break;
for (int i = 0; i < H; ++i) {
cin >> f[i];
for (int j = 0; j < W; ++j) {
if (f[i][j] == '@') {
q.push(P(i, j));
}
}
}
cout << calc(f, q, W, H) << endl;
}
} | #include <iostream>
#include <queue>
using namespace std;
const int MAX_SIZE = 21;
typedef pair<int, int> P;
int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
int n = 0;
while (q.size()) {
n++;
P p = q.front();
q.pop();
int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};
for (int k = 0; k < 4; ++k) {
int i = p.first + di[k];
int j = p.second + dj[k];
if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
f[i][j] = '#';
q.push(P(i, j));
}
}
}
return n;
}
int main() {
int W, H;
char f[MAX_SIZE][MAX_SIZE];
while (true) {
queue<P> q;
cin >> W >> H;
if (W == 0 && H == 0)
break;
for (int i = 0; i < H; ++i) {
cin >> f[i];
for (int j = 0; j < W; ++j) {
if (f[i][j] == '@') {
q.push(P(i, j));
}
}
}
cout << calc(f, q, W, H) << endl;
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00711 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> x;
vector<int> y;
vector<int> nextx;
vector<int> nexty;
int w, h;
int count;
bool check(int a, int b) {
for (int i = 0; i < nextx.size(); i++) {
if (nextx[i] == a && nexty[i] == b)
return false;
}
return true;
}
int search(int **field) {
x.clear();
y.clear();
for (int i = 0; i < nextx.size(); i++) {
x.push_back(nextx[i]);
y.push_back(nexty[i]);
}
nextx.clear();
nexty.clear();
for (int i = 0; i < x.size(); i++) {
field[x[i]][y[i]] = 0;
count++;
}
for (int i = 0; i < x.size(); i++) {
if (field[x[i] - 1][y[i]] == 1 && x[i] - 1 >= 0 && check(x[i] - 1, y[i])) {
nextx.push_back(x[i] - 1);
nexty.push_back(y[i]);
}
if (field[x[i] + 1][y[i]] == 1 && x[i] + 1 < h && check(x[i] + 1, y[i])) {
nextx.push_back(x[i] + 1);
nexty.push_back(y[i]);
}
if (field[x[i]][y[i] - 1] == 1 && y[i] - 1 >= 0 && check(x[i], y[i] - 1)) {
nextx.push_back(x[i]);
nexty.push_back(y[i] - 1);
}
if (field[x[i]][y[i] + 1] == 1 && y[i] + 1 < w && check(x[i], y[i] + 1)) {
nextx.push_back(x[i]);
nexty.push_back(y[i] + 1);
}
}
if (nextx.empty()) {
return -1;
}
int *f[h];
for (int i = 0; i < h; i++)
f[i] = field[i];
return search(f);
}
int main() {
string temp;
while (cin >> w >> h, w | h != 0) {
count = 0;
int field[h][w];
for (int i = 0; i < h; i++) {
cin >> temp;
for (int j = 0; j < w; j++) {
if (temp[j] == '.')
field[i][j] = 1;
if (temp[j] == '#')
field[i][j] = 0;
if (temp[j] == '@') {
field[i][j] = 1;
nextx.push_back(i);
nexty.push_back(j);
}
}
}
int *f[h];
for (int i = 0; i < h; i++)
f[i] = field[i];
search(f);
cout << count << endl;
}
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> x;
vector<int> y;
vector<int> nextx;
vector<int> nexty;
int w, h;
int count;
bool check(int a, int b) {
for (int i = 0; i < nextx.size(); i++) {
if (nextx[i] == a && nexty[i] == b)
return false;
}
return true;
}
int search(int **field) {
x.clear();
y.clear();
for (int i = 0; i < nextx.size(); i++) {
x.push_back(nextx[i]);
y.push_back(nexty[i]);
}
nextx.clear();
nexty.clear();
for (int i = 0; i < x.size(); i++) {
field[x[i]][y[i]] = 0;
count++;
}
for (int i = 0; i < x.size(); i++) {
if (field[x[i] - 1][y[i]] == 1 && x[i] - 1 >= 0 && check(x[i] - 1, y[i])) {
nextx.push_back(x[i] - 1);
nexty.push_back(y[i]);
}
if (x[i] + 1 < h && field[x[i] + 1][y[i]] == 1 && check(x[i] + 1, y[i])) {
nextx.push_back(x[i] + 1);
nexty.push_back(y[i]);
}
if (field[x[i]][y[i] - 1] == 1 && y[i] - 1 >= 0 && check(x[i], y[i] - 1)) {
nextx.push_back(x[i]);
nexty.push_back(y[i] - 1);
}
if (field[x[i]][y[i] + 1] == 1 && y[i] + 1 < w && check(x[i], y[i] + 1)) {
nextx.push_back(x[i]);
nexty.push_back(y[i] + 1);
}
}
if (nextx.empty()) {
return -1;
}
int *f[h];
for (int i = 0; i < h; i++)
f[i] = field[i];
return search(f);
}
int main() {
string temp;
while (cin >> w >> h, w | h != 0) {
count = 0;
int field[h][w];
for (int i = 0; i < h; i++) {
cin >> temp;
for (int j = 0; j < w; j++) {
if (temp[j] == '.')
field[i][j] = 1;
if (temp[j] == '#')
field[i][j] = 0;
if (temp[j] == '@') {
field[i][j] = 1;
nextx.push_back(i);
nexty.push_back(j);
}
}
}
int *f[h];
for (int i = 0; i < h; i++)
f[i] = field[i];
search(f);
cout << count << endl;
}
} | replace | 35 | 36 | 35 | 36 | -11 | |
p00711 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int paint(vector<string> &field, int x, int y) {
int cnt = 0;
for (int i = 0; i < 4; ++i) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || field[i].size() <= xx || yy < 0 || field.size() <= yy)
continue;
if (field[yy][xx] == '.') {
field[yy][xx] = '#';
cnt += paint(field, xx, yy) + 1;
}
}
return cnt;
}
int main() {
while (true) {
int W, H;
cin >> W >> H;
if (!W && !H)
break;
int sx, sy;
vector<string> field(H);
for (int i = 0; i < H; ++i) {
cin >> field[i];
for (int x = 0; x < W; ++x) {
if (field[i][x] == '@') {
sx = x;
sy = i;
}
}
}
cout << paint(field, sx, sy) + 1 << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int paint(vector<string> &field, int x, int y) {
int cnt = 0;
for (int i = 0; i < 4; ++i) {
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 0 || field[0].size() <= xx || yy < 0 || field.size() <= yy)
continue;
if (field[yy][xx] == '.') {
field[yy][xx] = '#';
cnt += paint(field, xx, yy) + 1;
}
}
return cnt;
}
int main() {
while (true) {
int W, H;
cin >> W >> H;
if (!W && !H)
break;
int sx, sy;
vector<string> field(H);
for (int i = 0; i < H; ++i) {
cin >> field[i];
for (int x = 0; x < W; ++x) {
if (field[i][x] == '@') {
sx = x;
sy = i;
}
}
}
cout << paint(field, sx, sy) + 1 << endl;
}
return 0;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p00711 | C++ | Time Limit Exceeded | #include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w) \
rep(i, (h)) { \
cout << "["; \
rep(j, (w)) { cout << (arr)[i][j] << ", "; } \
cout << "]" << endl; \
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);
#define fs first
#define sc second
typedef pair<int, int> P;
char grid[20][20];
bool arrived[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int w, h;
// i,jから初めて到達可能なタイルの数の最大値(i,j)含む
void rec(int i, int j) {
// printf("i:%d j:%d\n",i,j);
arrived[i][j] = true;
grid[i][j] = '#';
int ret = 0;
rep(k, 4) {
int next_j = j + dx[k];
int next_i = i + dy[k];
if (next_j < w && 0 <= next_j && next_i < h && 0 <= next_i &&
grid[next_i][next_j] == '.') {
rec(next_i, next_j);
}
}
}
int main() {
P start;
while (1) {
cin >> w >> h;
rep(i, 20) rep(j, 20) arrived[i][j] = false;
rep(i, h) {
rep(j, w) {
cin >> grid[i][j];
if (grid[i][j] == '@') {
start.first = i;
start.second = j;
}
}
}
rec(start.first, start.second);
int ret = 0;
rep(i, 20) {
rep(j, 20) {
if (arrived[i][j] == true) {
ret += 1;
}
}
}
cout << ret << endl;
}
} | #include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w) \
rep(i, (h)) { \
cout << "["; \
rep(j, (w)) { cout << (arr)[i][j] << ", "; } \
cout << "]" << endl; \
}
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);
#define fs first
#define sc second
typedef pair<int, int> P;
char grid[20][20];
bool arrived[20][20];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int w, h;
// i,jから初めて到達可能なタイルの数の最大値(i,j)含む
void rec(int i, int j) {
// printf("i:%d j:%d\n",i,j);
arrived[i][j] = true;
grid[i][j] = '#';
int ret = 0;
rep(k, 4) {
int next_j = j + dx[k];
int next_i = i + dy[k];
if (next_j < w && 0 <= next_j && next_i < h && 0 <= next_i &&
grid[next_i][next_j] == '.') {
rec(next_i, next_j);
}
}
}
int main() {
P start;
while (1) {
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
rep(i, 20) rep(j, 20) arrived[i][j] = false;
rep(i, h) {
rep(j, w) {
cin >> grid[i][j];
if (grid[i][j] == '@') {
start.first = i;
start.second = j;
}
}
}
rec(start.first, start.second);
int ret = 0;
rep(i, 20) {
rep(j, 20) {
if (arrived[i][j] == true) {
ret += 1;
}
}
}
cout << ret << endl;
}
} | insert | 56 | 56 | 56 | 59 | TLE | |
p00711 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> PP;
//-----------------------------------------------------------------------------
int sx, sy, h, w;
string fld[20];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int bfs() {
bool used[h][w];
REP(i, h) REP(j, w) used[i][j] = false;
int cnt = 1;
queue<P> q;
used[sy][sx] = true;
q.push(P(sx, sy));
while (!q.empty()) {
P p = q.front();
q.pop();
REP(i, 4) {
int nx = p.fi + dx[i], ny = p.se + dy[i];
if (nx < 0 || nx >= w || ny < 0 || ny >= h || used[ny][nx] ||
fld[ny][nx] == '#')
continue;
used[ny][nx] = true;
cnt++;
q.push(P(nx, ny));
}
}
return cnt;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (true) {
cin >> w >> h;
REP(i, h) {
cin >> fld[i];
REP(j, w) if (fld[i][j] == '@') sx = j, sy = i;
}
cout << bfs() << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> PP;
//-----------------------------------------------------------------------------
int sx, sy, h, w;
string fld[20];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int bfs() {
bool used[h][w];
REP(i, h) REP(j, w) used[i][j] = false;
int cnt = 1;
queue<P> q;
used[sy][sx] = true;
q.push(P(sx, sy));
while (!q.empty()) {
P p = q.front();
q.pop();
REP(i, 4) {
int nx = p.fi + dx[i], ny = p.se + dy[i];
if (nx < 0 || nx >= w || ny < 0 || ny >= h || used[ny][nx] ||
fld[ny][nx] == '#')
continue;
used[ny][nx] = true;
cnt++;
q.push(P(nx, ny));
}
}
return cnt;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (true) {
cin >> w >> h;
if (w == 0)
break;
REP(i, h) {
cin >> fld[i];
REP(j, w) if (fld[i][j] == '@') sx = j, sy = i;
}
cout << bfs() << endl;
}
return 0;
}
| insert | 54 | 54 | 54 | 56 | TLE | |
p00711 | C++ | Time Limit Exceeded | #include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int check(int j) {
if (j == 2 || j == 4 || j == 3)
return j;
else
return 3;
}
int main() {
int board[22][22];
int w, h;
char t;
while (1) {
cin >> w >> h;
for (int i = 0; i <= 21; i++) {
for (int j = 0; j <= 21; j++) {
board[j][i] = 4;
}
} // initialization
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> t;
if (t == '.')
board[j][i] = 1;
else if (t == '#')
board[j][i] = 2;
else
board[j][i] = 3;
}
} // input end
int cmem = 2000;
int c = 0;
while (cmem - c != 0) {
cmem = c;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (board[j][i] == 3) {
board[j - 1][i] = check(board[j - 1][i]);
board[j + 1][i] = check(board[j + 1][i]);
board[j][i - 1] = check(board[j][i - 1]);
board[j][i + 1] = check(board[j][i + 1]);
}
}
}
c = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (board[j][i] == 3)
c += 1;
}
}
}
cout << c << endl;
} // while loop end
return 0;
} | #include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int check(int j) {
if (j == 2 || j == 4 || j == 3)
return j;
else
return 3;
}
int main() {
int board[22][22];
int w, h;
char t;
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i <= 21; i++) {
for (int j = 0; j <= 21; j++) {
board[j][i] = 4;
}
} // initialization
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> t;
if (t == '.')
board[j][i] = 1;
else if (t == '#')
board[j][i] = 2;
else
board[j][i] = 3;
}
} // input end
int cmem = 2000;
int c = 0;
while (cmem - c != 0) {
cmem = c;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (board[j][i] == 3) {
board[j - 1][i] = check(board[j - 1][i]);
board[j + 1][i] = check(board[j + 1][i]);
board[j][i - 1] = check(board[j][i - 1]);
board[j][i + 1] = check(board[j][i + 1]);
}
}
}
c = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (board[j][i] == 3)
c += 1;
}
}
}
cout << c << endl;
} // while loop end
return 0;
} | insert | 16 | 16 | 16 | 18 | TLE | |
p00711 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<vector<int>> vector2d;
int main() {
int H, W, count = 0, kaisuu = 0, kosuu, sum, judge;
char map[20][20];
vector2d nextx, nexty; // nextx[回数][個数]
nextx.reserve(1000);
nexty.reserve(1000);
while (1) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
// マップの入力
for (int cH = 0; cH < H; cH++) {
for (int cW = 0; cW < W; cW++) {
cin >> map[cW][cH];
if (map[cW][cH] == '@') {
nextx[kaisuu].push_back(cW);
nexty[kaisuu].push_back(cH);
}
}
}
sum = 1;
while (1) {
judge = 0;
for (kosuu = 0; kosuu < nextx[kaisuu].size(); kosuu++) {
if (nextx[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] ==
'.') { //@の左側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] - 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] ==
'.') { //@の上側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] - 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] = '@';
sum++;
judge++;
}
}
if (nextx[kaisuu][kosuu] + 1 != W) {
if (map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] ==
'.') { //@の右側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] + 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] + 1 != H) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] ==
'.') { //@の下側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] + 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] = '@';
sum++;
judge++;
}
}
}
if (judge == 0)
break;
kaisuu++;
}
kaisuu++;
cout << sum << endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<vector<int>> vector2d;
int main() {
int H, W, count = 0, kaisuu = 0, kosuu, sum, judge;
char map[20][20];
vector2d nextx, nexty; // nextx[回数][個数]
nextx.reserve(10000);
nexty.reserve(10000);
while (1) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
// マップの入力
for (int cH = 0; cH < H; cH++) {
for (int cW = 0; cW < W; cW++) {
cin >> map[cW][cH];
if (map[cW][cH] == '@') {
nextx[kaisuu].push_back(cW);
nexty[kaisuu].push_back(cH);
}
}
}
sum = 1;
while (1) {
judge = 0;
for (kosuu = 0; kosuu < nextx[kaisuu].size(); kosuu++) {
if (nextx[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] ==
'.') { //@の左側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] - 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] ==
'.') { //@の上側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] - 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] = '@';
sum++;
judge++;
}
}
if (nextx[kaisuu][kosuu] + 1 != W) {
if (map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] ==
'.') { //@の右側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] + 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] + 1 != H) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] ==
'.') { //@の下側をチェック
// もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] + 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] = '@';
sum++;
judge++;
}
}
}
if (judge == 0)
break;
kaisuu++;
}
kaisuu++;
cout << sum << endl;
}
return 0;
} | replace | 12 | 14 | 12 | 14 | 0 | |
p00711 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> ans;
typedef pair<int, int> P;
int main() {
int W, H;
cin >> W >> H;
int tate[4] = {0, 1, 0, -1};
int yoko[4] = {1, 0, -1, 0};
while (W) {
int count = 0;
queue<P> zahyou;
vector<vector<int>> room(H, vector<int>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char a;
cin >> a;
if (a == '.')
room[i][j] = 1;
else if (a == '@') {
zahyou.push(P(i, j));
room[i][j] = 1;
} else
room[i][j] = 0;
}
}
while (zahyou.size()) {
pair<int, int> now = zahyou.front();
zahyou.pop();
if (room[now.first][now.second] == 1) {
count++;
room[now.first][now.second] = 0;
for (int i = 0; i < 4; i++) {
zahyou.push(P(now.first + tate[i], now.second + yoko[i]));
}
}
}
ans.push_back(count);
cin >> W >> H;
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << endl;
}
| #include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> ans;
typedef pair<int, int> P;
int main() {
int W, H;
cin >> W >> H;
int tate[4] = {0, 1, 0, -1};
int yoko[4] = {1, 0, -1, 0};
while (W) {
int count = 0;
queue<P> zahyou;
vector<vector<int>> room(H, vector<int>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char a;
cin >> a;
if (a == '.')
room[i][j] = 1;
else if (a == '@') {
zahyou.push(P(i, j));
room[i][j] = 1;
} else
room[i][j] = 0;
}
}
while (zahyou.size()) {
pair<int, int> now = zahyou.front();
zahyou.pop();
if (room[now.first][now.second] == 1) {
count++;
room[now.first][now.second] = 0;
for (int i = 0; i < 4; i++) {
if (now.first + tate[i] >= 0 && now.first + tate[i] < H &&
now.second + yoko[i] >= 0 && now.second + yoko[i] < W &&
room[now.first + tate[i]][now.second + yoko[i]] == 1)
zahyou.push(P(now.first + tate[i], now.second + yoko[i]));
}
}
}
ans.push_back(count);
cin >> W >> H;
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << endl;
}
| replace | 37 | 38 | 37 | 41 | -11 | |
p00712 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long double ld;
const ld EPS = 1e-9;
ld p, q;
int a, n;
ld t;
int dfs(int m, ld s, int w, int c) {
if (abs(s - t) < EPS) {
return 1;
}
if (m == n)
return 0;
int ret = 0;
for (int i = c; i >= 1; i--) {
if (w * i <= a) {
ld j = s + (ld(1.0) / ld(i));
if (j <= t + EPS) {
ret += dfs(m + 1, j, w * i, i);
}
}
}
return ret;
}
int main() {
while (cin >> p >> q >> a >> n && (p || q || a || n)) {
t = p / q;
cout << dfs(0, 0, 1, a) << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long double ld;
const ld EPS = 1e-9;
ld p, q;
int a, n;
ld t;
int dfs(int m, ld s, int w, int c) {
if (abs(s - t) < EPS) {
return 1;
}
if (m == n)
return 0;
int ret = 0;
for (int i = min(a / w, c); i >= 1; i--) {
if (w * i <= a) {
ld j = s + (ld(1.0) / ld(i));
if (j <= t + EPS) {
ret += dfs(m + 1, j, w * i, i);
}
}
}
return ret;
}
int main() {
while (cin >> p >> q >> a >> n && (p || q || a || n)) {
t = p / q;
cout << dfs(0, 0, 1, a) << endl;
}
return 0;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p00712 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
int s = 0;
if (cn <= n) {
mind = max(mind, q / p);
int maxd = floor((double)(n - cn + 1) * q / p);
for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
i++) {
if (p * i - q == 0) {
s++;
} else if (p * i - q > 0) {
int _p = p * i - q;
int _q = q * i;
for (int j = 2; j <= min(_p, _q); j++) {
while (_p % j + _q % j == 0) {
_p /= j;
_q /= j;
}
}
s += calc(_p, _q, i, ca * i, a, cn + 1, n);
}
}
}
return s;
}
int main() {
int p, q, a, n;
cin >> p >> q >> a >> n;
while (p) {
for (int i = 2; i <= min(p, q); i++) {
while (p % i + q % i == 0) {
p /= i;
q /= i;
}
}
cout << calc(p, q, 1, 1, a, 1, n) << endl;
cin >> p >> q >> a >> n;
}
} | #include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
int s = 0;
if (cn <= n) {
mind = max(mind, q / p);
int maxd = floor((double)(n - cn + 1) * q / p);
for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
i++) {
if (p * i - q == 0) {
s++;
} else if (p * i - q > 0) {
int _p = p * i - q;
int _q = q * i;
s += calc(_p, _q, i, ca * i, a, cn + 1, n);
}
}
}
return s;
}
int main() {
int p, q, a, n;
cin >> p >> q >> a >> n;
while (p) {
for (int i = 2; i <= min(p, q); i++) {
while (p % i + q % i == 0) {
p /= i;
q /= i;
}
}
cout << calc(p, q, 1, 1, a, 1, n) << endl;
cin >> p >> q >> a >> n;
}
} | delete | 18 | 24 | 18 | 18 | TLE | |
p00712 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;
int dfs(int p, int q, int a, int n, int cur = 0, int pro = 1, int start = 1) {
if (p < 0 || cur > n || pro > a)
return 0;
if (p == 0)
return 1;
int res = 0;
for (int i = start; i <= a / pro; ++i) {
int nq = q * i;
int np = p * i - q;
int g = __gcd(np, nq);
np /= g, nq /= g;
if (nq < 0)
nq *= -1, np *= -1;
res += dfs(np, nq, a, n, cur + 1, pro * i, i);
}
return res;
}
int main(void) {
int p, q, a, n;
while (cin >> p >> q >> a >> n) {
if (p == 0)
break;
cout << dfs(p, q, a, n) << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;
int dfs(int p, int q, int a, int n, int cur = 0, int pro = 1, int start = 1) {
if (p < 0 || cur > n || pro > a)
return 0;
if (p == 0)
return 1;
int res = 0;
for (int i = start; i <= a / pro; ++i) {
if (p * i > q * (n - cur))
break;
int nq = q * i;
int np = p * i - q;
int g = __gcd(np, nq);
np /= g, nq /= g;
if (nq < 0)
nq *= -1, np *= -1;
res += dfs(np, nq, a, n, cur + 1, pro * i, i);
}
return res;
}
int main(void) {
int p, q, a, n;
while (cin >> p >> q >> a >> n) {
if (p == 0)
break;
cout << dfs(p, q, a, n) << endl;
}
return 0;
} | insert | 50 | 50 | 50 | 52 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.