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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00768 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using P = std::pair<int, int>;
using namespace std;
struct team_data {
int team_num = -1;
int solved_num = 0;
int sum_time = 0; // minute
// vector<int> solved_time;
vector<int> penalty;
};
int main() {
int M, T, P, R;
while (cin >> M >> T >> P >> R, M) {
vector<team_data> v(T);
for (int i = 0; i < v.size(); ++i) {
v[i].team_num = i + 1;
v[i].penalty.resize(P);
}
for (int i = 0; i < R; ++i) {
int m, t, p, j;
cin >> m >> t >> p >> j;
if (j != 0) {
v[t - 1].penalty[p - 1] += 20;
} else {
v[t - 1].sum_time += m;
v[t - 1].solved_num += 1;
v[t - 1].sum_time += v[t - 1].penalty[p - 1];
}
}
vector<vector<team_data>> v2(P);
for (auto &x : v) {
v2[x.solved_num].push_back(x);
}
for (auto &x : v2) {
sort(x.begin(), x.end(), [](team_data const &t1, team_data const &t2) {
return t1.team_num > t2.team_num;
});
stable_sort(x.begin(), x.end(),
[](team_data const &t1, team_data const &t2) {
return t1.sum_time < t2.sum_time;
});
}
vector<team_data> ret;
for (int i = v2.size() - 1; i >= 0; --i) {
for (auto &x2 : v2[i]) {
ret.push_back(x2);
}
}
for (int i = 0; i < ret.size(); ++i) {
if (i != 0) {
if (ret[i - 1].solved_num == ret[i].solved_num) {
if (ret[i - 1].sum_time == ret[i].sum_time)
cout << '=';
else
cout << ',';
} else {
cout << ',';
}
}
cout << ret[i].team_num;
}
cout << endl;
}
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using P = std::pair<int, int>;
using namespace std;
struct team_data {
int team_num = -1;
int solved_num = 0;
int sum_time = 0; // minute
// vector<int> solved_time;
vector<int> penalty;
};
int main() {
int M, T, P, R;
while (cin >> M >> T >> P >> R, M) {
vector<team_data> v(T);
for (int i = 0; i < v.size(); ++i) {
v[i].team_num = i + 1;
v[i].penalty.resize(P);
}
for (int i = 0; i < R; ++i) {
int m, t, p, j;
cin >> m >> t >> p >> j;
if (j != 0) {
v[t - 1].penalty[p - 1] += 20;
} else {
v[t - 1].sum_time += m;
v[t - 1].solved_num += 1;
v[t - 1].sum_time += v[t - 1].penalty[p - 1];
}
}
vector<vector<team_data>> v2(P + 1);
for (auto &x : v) {
v2[x.solved_num].push_back(x);
}
for (auto &x : v2) {
sort(x.begin(), x.end(), [](team_data const &t1, team_data const &t2) {
return t1.team_num > t2.team_num;
});
stable_sort(x.begin(), x.end(),
[](team_data const &t1, team_data const &t2) {
return t1.sum_time < t2.sum_time;
});
}
vector<team_data> ret;
for (int i = v2.size() - 1; i >= 0; --i) {
for (auto &x2 : v2[i]) {
ret.push_back(x2);
}
}
for (int i = 0; i < ret.size(); ++i) {
if (i != 0) {
if (ret[i - 1].solved_num == ret[i].solved_num) {
if (ret[i - 1].sum_time == ret[i].sum_time)
cout << '=';
else
cout << ',';
} else {
cout << ',';
}
}
cout << ret[i].team_num;
}
cout << endl;
}
} | replace | 36 | 37 | 36 | 37 | 0 | |
p00768 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
class Team {
public:
int id;
int solvednum;
int penalty;
vector<bool> solved;
vector<int> WA;
Team(int id, int N) {
this->id = id;
solvednum = 0;
penalty = 0;
solved.resize(N, false);
WA.resize(N, 0);
}
bool operator==(const Team &t) const { return id == t.id; }
bool operator<(const Team &t) const {
if (solvednum == t.solvednum) {
if (penalty == t.penalty) {
return id > t.id;
}
return penalty < t.penalty;
}
return solvednum > t.solvednum;
}
string toString() {
return to_string(id) + " " + to_string(solvednum) + " " +
to_string(penalty);
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int M, T, P, R;
while (cin >> M >> T >> P >> R, M | T | P | R) {
vector<Team> teams;
for (int i = 0; i < T; i++) {
teams.push_back(Team(i + 1, P));
}
for (int asd = 0; asd < R; asd++) {
int m, t, p, j;
cin >> m >> t >> p >> j;
t--;
if (j == 0) {
teams[t].solved[p] = true;
teams[t].solvednum++;
teams[t].penalty += m + 20 * teams[t].WA[p];
} else {
teams[t].WA[p]++;
}
}
sort(teams.begin(), teams.end());
cout << teams[0].id;
for (int i = 1; i < T; i++) {
if (teams[i].solvednum == teams[i - 1].solvednum &&
teams[i].penalty == teams[i - 1].penalty) {
cout << '=';
} else {
cout << ',';
}
cout << teams[i].id;
}
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
class Team {
public:
int id;
int solvednum;
int penalty;
vector<bool> solved;
vector<int> WA;
Team(int id, int N) {
this->id = id;
solvednum = 0;
penalty = 0;
solved.resize(N, false);
WA.resize(N, 0);
}
bool operator==(const Team &t) const { return id == t.id; }
bool operator<(const Team &t) const {
if (solvednum == t.solvednum) {
if (penalty == t.penalty) {
return id > t.id;
}
return penalty < t.penalty;
}
return solvednum > t.solvednum;
}
string toString() {
return to_string(id) + " " + to_string(solvednum) + " " +
to_string(penalty);
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int M, T, P, R;
while (cin >> M >> T >> P >> R, M | T | P | R) {
vector<Team> teams;
for (int i = 0; i < T; i++) {
teams.push_back(Team(i + 1, P + 1));
}
for (int asd = 0; asd < R; asd++) {
int m, t, p, j;
cin >> m >> t >> p >> j;
t--;
if (j == 0) {
teams[t].solved[p] = true;
teams[t].solvednum++;
teams[t].penalty += m + 20 * teams[t].WA[p];
} else {
teams[t].WA[p]++;
}
}
sort(teams.begin(), teams.end());
cout << teams[0].id;
for (int i = 1; i < T; i++) {
if (teams[i].solvednum == teams[i - 1].solvednum &&
teams[i].penalty == teams[i - 1].penalty) {
cout << '=';
} else {
cout << ',';
}
cout << teams[i].id;
}
cout << endl;
}
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p00769 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define sz size()
string pre(string s) { // "][" を空白に置き換え
string b = "";
rep(i, s.sz - 1) {
if (s[i] == ']' && s[i + 1] == '[') {
b += ' ';
i++;
} else {
b += s[i];
}
}
b += s[s.sz - 1];
return b;
}
string solve(int n, bool check, string s) {
n++;
string tmp[100] = {};
int count = 0;
REP(j, n, s.sz) {
if (s[j] != ']') {
if (s[j] != ' ') {
tmp[count] += s[j];
} else {
count++;
}
} else
break;
}
vi a;
rep(i, count + 1) { a.pb(stoi(tmp[i])); }
sort(ALL(a));
int sum = 0;
rep(i, count / 2 + 1) {
if (check == false)
sum += a[i] / 2 + 1;
else
sum += a[i];
}
return to_string((sum));
}
string delSpace(string s) { // "] [" を "][" に置き換え
string ret = "";
rep(i, s.sz - 2) {
if (s[i] == ']' && s[i + 1] == ' ' && s[i + 2] == '[') {
ret += "][";
i += 2;
} else {
ret += s[i];
}
}
ret += s[s.sz - 2];
ret += s[s.sz - 1];
return ret;
}
int main() {
int t;
cin >> t;
rep(times, t) {
string s;
cin >> s;
int check = false;
s = pre(s);
while (1) {
if (s.find("[", 0) == -1)
break;
string n = "";
rep(i, s.sz) {
if (s[i + 1] >= '0' && s[i + 1] <= '9') {
n += solve(i, check, s);
while (s[i] != ']')
i++;
} else {
n += s[i];
}
}
s = n;
check = true;
}
cout << s << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define sz size()
string pre(string s) { // "][" を空白に置き換え
string b = "";
rep(i, s.sz - 1) {
if (s[i] == ']' && s[i + 1] == '[') {
b += ' ';
i++;
} else {
b += s[i];
}
}
b += s[s.sz - 1];
return b;
}
string solve(int n, bool check, string s) {
n++;
string tmp[10000] = {};
int count = 0;
REP(j, n, s.sz) {
if (s[j] != ']') {
if (s[j] != ' ') {
tmp[count] += s[j];
} else {
count++;
}
} else
break;
}
vi a;
rep(i, count + 1) { a.pb(stoi(tmp[i])); }
sort(ALL(a));
int sum = 0;
rep(i, count / 2 + 1) {
if (check == false)
sum += a[i] / 2 + 1;
else
sum += a[i];
}
return to_string((sum));
}
string delSpace(string s) { // "] [" を "][" に置き換え
string ret = "";
rep(i, s.sz - 2) {
if (s[i] == ']' && s[i + 1] == ' ' && s[i + 2] == '[') {
ret += "][";
i += 2;
} else {
ret += s[i];
}
}
ret += s[s.sz - 2];
ret += s[s.sz - 1];
return ret;
}
int main() {
int t;
cin >> t;
rep(times, t) {
string s;
cin >> s;
int check = false;
s = pre(s);
while (1) {
if (s.find("[", 0) == -1)
break;
string n = "";
rep(i, s.sz) {
if (s[i + 1] >= '0' && s[i + 1] <= '9') {
n += solve(i, check, s);
while (s[i] != ']')
i++;
} else {
n += s[i];
}
}
s = n;
check = true;
}
cout << s << endl;
}
return 0;
} | replace | 49 | 50 | 49 | 50 | 0 | |
p00769 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
string s;
int calc(int l, int r) {
int d = 0, c = 0, w = l;
for (int i = l; i < r; i++)
c += (s[i] == '[' || s[i] == ']' ? 1 : 0);
if (c == 0)
return stoi(s.substr(l + 1, r - l - 2));
vector<int> v;
for (int i = l; i < r; i++) {
if (s[i] == '[')
d++;
if (s[i] == ']')
d--;
if (d == 0)
v.push_back(calc(w + 1, i)), w = i + 1;
}
sort(v.begin(), v.end());
int ret = 0;
for (int i = 0; i <= v.size() / 2; i++)
ret += v[i];
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
cout << calc(0, s.size()) << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
string s;
int calc(int l, int r) {
int d = 0, c = 0, w = l;
for (int i = l; i < r; i++)
c += (s[i] == '[' || s[i] == ']' ? 1 : 0);
if (c == 0)
return stoi(s.substr(l, r)) / 2 + 1;
vector<int> v;
for (int i = l; i < r; i++) {
if (s[i] == '[')
d++;
if (s[i] == ']')
d--;
if (d == 0)
v.push_back(calc(w + 1, i)), w = i + 1;
}
sort(v.begin(), v.end());
int ret = 0;
for (int i = 0; i <= v.size() / 2; i++)
ret += v[i];
return ret;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
cout << calc(0, s.size()) << endl;
}
return 0;
} | replace | 12 | 13 | 12 | 13 | -6 | terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
|
p00769 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a[10][100], n, cnt[10], u, sum, chain;
string S, T;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> S;
u = 0;
sum = 0;
for (int i = 0; i < 10; i++) {
cnt[i] = 0;
}
for (int i = 0; i < S.size(); i++) {
if (S[i] == '[') {
u++;
T = "";
chain = 0;
} else if (S[i] == ']') {
if (T != "") {
cnt[u] = 0;
u--;
a[u][cnt[u]] = stoi(T);
cnt[u]++;
T = "";
} else {
chain++;
sum = 0;
sort(a[u], a[u] + cnt[u]);
if (chain == 1) {
for (int i = 0; i < cnt[u] / 2 + 1; i++) {
sum += a[u][i] / 2 + 1;
}
cnt[u] = 0;
u--;
a[u][cnt[u]] = sum;
} else {
sum = 0;
for (int i = 0; i < cnt[u] / 2 + 1; i++) {
sum += a[u][i];
}
cnt[u] = 0;
u--;
a[u][cnt[u]] = sum;
}
cnt[u]++;
}
} else {
T += S[i];
chain = 0;
}
}
u = 0;
cout << sum << endl;
}
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a[100][1000], n, cnt[100], u, sum, chain;
string S, T;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> S;
u = 0;
sum = 0;
for (int i = 0; i < 10; i++) {
cnt[i] = 0;
}
for (int i = 0; i < S.size(); i++) {
if (S[i] == '[') {
u++;
T = "";
chain = 0;
} else if (S[i] == ']') {
if (T != "") {
cnt[u] = 0;
u--;
a[u][cnt[u]] = stoi(T);
cnt[u]++;
T = "";
} else {
chain++;
sum = 0;
sort(a[u], a[u] + cnt[u]);
if (chain == 1) {
for (int i = 0; i < cnt[u] / 2 + 1; i++) {
sum += a[u][i] / 2 + 1;
}
cnt[u] = 0;
u--;
a[u][cnt[u]] = sum;
} else {
sum = 0;
for (int i = 0; i < cnt[u] / 2 + 1; i++) {
sum += a[u][i];
}
cnt[u] = 0;
u--;
a[u][cnt[u]] = sum;
}
cnt[u]++;
}
} else {
T += S[i];
chain = 0;
}
}
u = 0;
cout << sum << endl;
}
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00770 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
namespace std {
template <> class hash<std::pair<int, int>> {
public:
size_t operator()(const std::pair<int, int> &x) const {
return size_t((size_t)x.first * (size_t)x.second);
}
};
} // namespace std
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
void solve() {
vector<bool> is_prime(1e6 + 1, true);
is_prime[0] = is_prime[1] = false;
rep(i, 2, 1e+6 + 1) {
if (!is_prime[i])
continue;
for (int j = i * 2; j < 1e6 + 1; j += i)
is_prime[j] = false;
}
while (true) {
int m, n;
cin >> m >> n;
if (m == 0 and n == 0)
break;
unordered_map<pii, int> encode;
unordered_map<int, pii> decode;
unordered_map<int, vector<int>> xs;
{
int x = 0, y = 0, dir = 3;
rep(i, 1, m + 1) {
encode[make_pair(x, y)] = i;
decode[i] = make_pair(x, y);
xs[y].push_back(x);
int nx = x + dx[(dir + 1) % 4], ny = y + dy[(dir + 1) % 4];
if (encode.find(make_pair(nx, ny)) == encode.end())
dir = (dir + 1) % 4;
else {
nx = x + dx[dir];
ny = y + dy[dir];
}
x = nx;
y = ny;
}
}
unordered_map<pii, int> dp;
int y = decode[n].second + 1, ans, max_num;
if (is_prime[n]) {
dp[decode[n]] = 1;
ans = 1;
max_num = n;
} else {
dp[decode[n]] = 0;
ans = 0;
max_num = 0;
}
while (xs.find(y) != xs.end()) {
if (xs.find(y - 2) != xs.end())
for (int x : xs[y - 2])
dp.erase(make_pair(x, y));
for (int x : xs[y]) {
int tmp = -1;
if (dp.find(make_pair(x - 1, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x - 1, y - 1)]);
if (dp.find(make_pair(x, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x, y - 1)]);
if (dp.find(make_pair(x + 1, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x + 1, y - 1)]);
if (tmp == -1)
continue;
if (is_prime[encode[make_pair(x, y)]]) {
++tmp;
if (tmp > ans) {
ans = tmp;
max_num = encode[make_pair(x, y)];
} else if (tmp == ans)
max_num = max(max_num, encode[make_pair(x, y)]);
}
dp[make_pair(x, y)] = tmp;
}
++y;
}
cout << ans << " " << max_num << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
namespace std {
template <> class hash<std::pair<int, int>> {
public:
size_t operator()(const std::pair<int, int> &x) const {
return size_t((size_t)x.first * (size_t)x.second);
}
};
} // namespace std
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
void solve() {
vector<bool> is_prime(1e6 + 1, true);
is_prime[0] = is_prime[1] = false;
rep(i, 2, 1e+6 + 1) {
if (!is_prime[i])
continue;
for (int j = i * 2; j < 1e6 + 1; j += i)
is_prime[j] = false;
}
while (true) {
int m, n;
cin >> m >> n;
if (m == 0 and n == 0)
break;
unordered_map<pii, int> encode;
unordered_map<int, pii> decode;
unordered_map<int, vector<int>> xs;
{
int x = 0, y = 0, dir = 3;
rep(i, 1, m + 1) {
encode[make_pair(x, y)] = i;
decode[i] = make_pair(x, y);
xs[y].push_back(x);
int nx = x + dx[(dir + 1) % 4], ny = y + dy[(dir + 1) % 4];
if (encode.find(make_pair(nx, ny)) == encode.end())
dir = (dir + 1) % 4;
else {
nx = x + dx[dir];
ny = y + dy[dir];
}
x = nx;
y = ny;
}
}
unordered_map<pii, int> dp;
int y = decode[n].second + 1, ans, max_num;
if (is_prime[n]) {
dp[decode[n]] = 1;
ans = 1;
max_num = n;
} else {
dp[decode[n]] = 0;
ans = 0;
max_num = 0;
}
while (xs.find(y) != xs.end()) {
if (xs.find(y - 2) != xs.end())
for (int x : xs[y - 2])
dp.erase(make_pair(x, y - 2));
for (int x : xs[y]) {
int tmp = -1;
if (dp.find(make_pair(x - 1, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x - 1, y - 1)]);
if (dp.find(make_pair(x, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x, y - 1)]);
if (dp.find(make_pair(x + 1, y - 1)) != dp.end())
tmp = max(tmp, dp[make_pair(x + 1, y - 1)]);
if (tmp == -1)
continue;
if (is_prime[encode[make_pair(x, y)]]) {
++tmp;
if (tmp > ans) {
ans = tmp;
max_num = encode[make_pair(x, y)];
} else if (tmp == ans)
max_num = max(max_num, encode[make_pair(x, y)]);
}
dp[make_pair(x, y)] = tmp;
}
++y;
}
cout << ans << " " << max_num << endl;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
solve();
return 0;
} | replace | 90 | 91 | 90 | 91 | MLE | |
p00770 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MAX = 1000010;
int prime[MAX];
bool is_prime[MAX];
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
const int CENTER = 2000;
int b[CENTER * 2][CENTER * 2];
bool v[CENTER * 2][CENTER * 2];
int pi[MAX], pj[MAX];
P dp[CENTER * 2][CENTER * 2];
bool comp(P p1, P p2) {
if (p1.first != p2.first)
return p1.first < p2.first;
return p1.second < p2.second;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
sieve(1000000);
int nx = CENTER, ny = CENTER, dirx = 0, diry = 1;
for (int i = 1; i <= 1000000; i++) {
b[ny][nx] = i;
int dlx = diry, dly = -dirx;
if (b[ny + dly][nx + dlx] == 0) {
dirx = dlx;
diry = dly;
}
nx += dirx;
ny += diry;
}
int n, m;
while (cin >> m >> n, n | m) {
for (int y = 0; y < CENTER * 2; y++) {
for (int x = 0; x < CENTER * 2; x++) {
dp[y][x] = make_pair(0, 0);
v[y][x] = false;
if (b[y][x] == n)
v[y][x] = true;
}
}
P ans = make_pair(0, 0);
for (int y = 0; y < CENTER * 2; y++) {
for (int x = 0; x < CENTER * 2; x++)
if (v[y][x]) {
if (is_prime[b[y][x]]) {
dp[y][x] = make_pair(dp[y][x].first + 1, b[y][x]);
}
for (int dx = -1; dx <= 1; dx++) {
if (b[y + 1][x + dx] != 0 && b[y + 1][x + dx] <= m) {
v[y + 1][x + dx] = true;
dp[y + 1][x + dx] = max(dp[y + 1][x + dx], dp[y][x]);
}
}
ans = max(ans, dp[y][x]);
}
}
if (ans.first) {
cout << ans.first << " " << ans.second << endl;
} else {
cout << "0 0" << endl;
}
}
} | #include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MAX = 1000010;
int prime[MAX];
bool is_prime[MAX];
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
const int CENTER = 1000;
int b[CENTER * 2][CENTER * 2];
bool v[CENTER * 2][CENTER * 2];
int pi[MAX], pj[MAX];
P dp[CENTER * 2][CENTER * 2];
bool comp(P p1, P p2) {
if (p1.first != p2.first)
return p1.first < p2.first;
return p1.second < p2.second;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
sieve(1000000);
int nx = CENTER, ny = CENTER, dirx = 0, diry = 1;
for (int i = 1; i <= 1000000; i++) {
b[ny][nx] = i;
int dlx = diry, dly = -dirx;
if (b[ny + dly][nx + dlx] == 0) {
dirx = dlx;
diry = dly;
}
nx += dirx;
ny += diry;
}
int n, m;
while (cin >> m >> n, n | m) {
for (int y = 0; y < CENTER * 2; y++) {
for (int x = 0; x < CENTER * 2; x++) {
dp[y][x] = make_pair(0, 0);
v[y][x] = false;
if (b[y][x] == n)
v[y][x] = true;
}
}
P ans = make_pair(0, 0);
for (int y = 0; y < CENTER * 2; y++) {
for (int x = 0; x < CENTER * 2; x++)
if (v[y][x]) {
if (is_prime[b[y][x]]) {
dp[y][x] = make_pair(dp[y][x].first + 1, b[y][x]);
}
for (int dx = -1; dx <= 1; dx++) {
if (b[y + 1][x + dx] != 0 && b[y + 1][x + dx] <= m) {
v[y + 1][x + dx] = true;
dp[y + 1][x + dx] = max(dp[y + 1][x + dx], dp[y][x]);
}
}
ans = max(ans, dp[y][x]);
}
}
if (ans.first) {
cout << ans.first << " " << ans.second << endl;
} else {
cout << "0 0" << endl;
}
}
} | replace | 30 | 31 | 30 | 31 | MLE | |
p00770 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#define MAX_M 1000000
using namespace std;
typedef struct {
int num, prime, last;
} HOLE;
int M, N;
HOLE h[1000 + 2][1000 + 2];
bool prime_judge(int x) {
if (x == 0 || x == 1)
return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
void solve() {
int flag = 0, d = 1; //?????????????????????, ???????????¢
int x = 501, y = 501, ox = 501, oy = 501;
int ansx, ansy;
for (int i = 0; i < 1002; i++) {
for (int j = 0; j < 1002; j++) {
h[i][j].num = 0;
h[i][j].last = 0;
h[i][j].prime = 0;
}
}
for (int i = 1; i <= M; i++) {
h[x][y].num = i;
if (i == N) {
ansx = x;
ansy = y;
}
if (flag == 0) {
x++;
if (x - ox == d) {
ox = x;
flag = 1;
}
} else if (flag == 1) {
y--;
if (oy - y == d) {
oy = y;
d++;
flag = 2;
}
} else if (flag == 2) {
x--;
if (ox - x == d) {
ox = x;
flag = 3;
}
} else if (flag == 3) {
y++;
if (y - oy == d) {
oy = y;
d++;
flag = 0;
}
}
}
for (int i = 1001; i >= 0; i--) {
for (int j = 0; j < 1002; j++) {
if (h[j][i].num != 0) {
h[j][i].prime = max(max(h[j - 1][i + 1].prime, h[j][i + 1].prime),
h[j + 1][i + 1].prime);
if (h[j - 1][i + 1].last == 0 && h[j][i + 1].last == 0 &&
h[j + 1][i + 1].last == 0) {
if (prime_judge(h[j][i].num))
h[j][i].last = h[j][i].num;
} else {
int k;
for (k = -1; k <= 1; k++) {
if (h[j + k][i + 1].prime == h[j][i].prime) {
h[j][i].last = h[j + k][i + 1].last;
break;
}
}
for (int l = -1; l <= 1; l++) {
if (h[j + k][i + 1].prime == h[j + l][i + 1].prime) {
h[j][i].last = max(h[j][i].last, h[j + l][i + 1].last);
}
}
}
if (prime_judge(h[j][i].num))
h[j][i].prime++;
}
}
}
if (h[ansx][ansy].prime == 0)
cout << "0 0" << endl;
else
cout << h[ansx][ansy].prime << " " << h[ansx][ansy].last << endl;
}
int main() {
while (1) {
cin >> M >> N;
if (M == 0 && N == 0)
break;
solve();
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#define MAX_M 1000000
using namespace std;
typedef struct {
int num, prime, last;
} HOLE;
int M, N;
HOLE h[1000 + 2][1000 + 2];
bool prime_judge(int x) {
if (x == 0 || x == 1)
return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
void solve() {
int flag = 0, d = 1; //?????????????????????, ???????????¢
int x = 500, y = 500, ox = 500, oy = 500;
int ansx, ansy;
for (int i = 0; i < 1002; i++) {
for (int j = 0; j < 1002; j++) {
h[i][j].num = 0;
h[i][j].last = 0;
h[i][j].prime = 0;
}
}
for (int i = 1; i <= M; i++) {
h[x][y].num = i;
if (i == N) {
ansx = x;
ansy = y;
}
if (flag == 0) {
x++;
if (x - ox == d) {
ox = x;
flag = 1;
}
} else if (flag == 1) {
y--;
if (oy - y == d) {
oy = y;
d++;
flag = 2;
}
} else if (flag == 2) {
x--;
if (ox - x == d) {
ox = x;
flag = 3;
}
} else if (flag == 3) {
y++;
if (y - oy == d) {
oy = y;
d++;
flag = 0;
}
}
}
for (int i = 1001; i >= 0; i--) {
for (int j = 0; j < 1002; j++) {
if (h[j][i].num != 0) {
h[j][i].prime = max(max(h[j - 1][i + 1].prime, h[j][i + 1].prime),
h[j + 1][i + 1].prime);
if (h[j - 1][i + 1].last == 0 && h[j][i + 1].last == 0 &&
h[j + 1][i + 1].last == 0) {
if (prime_judge(h[j][i].num))
h[j][i].last = h[j][i].num;
} else {
int k;
for (k = -1; k <= 1; k++) {
if (h[j + k][i + 1].prime == h[j][i].prime) {
h[j][i].last = h[j + k][i + 1].last;
break;
}
}
for (int l = -1; l <= 1; l++) {
if (h[j + k][i + 1].prime == h[j + l][i + 1].prime) {
h[j][i].last = max(h[j][i].last, h[j + l][i + 1].last);
}
}
}
if (prime_judge(h[j][i].num))
h[j][i].prime++;
}
}
}
if (h[ansx][ansy].prime == 0)
cout << "0 0" << endl;
else
cout << h[ansx][ansy].prime << " " << h[ansx][ansy].last << endl;
}
int main() {
while (1) {
cin >> M >> N;
if (M == 0 && N == 0)
break;
solve();
}
return 0;
} | replace | 29 | 30 | 29 | 30 | -11 | |
p00770 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, -1, 0, 1, 1, 1, -1, -1};
const int L = 5000;
const int N = 1000000;
int grid[L][L];
void init() {
int l = 1;
int x = L / 2;
int y = L / 2;
int r = 0;
int rest = 1;
for (int y = 0; y < L; y++) {
for (int x = 0; x < L; x++) {
grid[y][x] = INF;
}
}
for (int i = 1; i <= N; i++) {
assert(grid[y][x] == INF);
grid[y][x] = i;
x += dx[r];
y += dy[r];
rest--;
if (rest == 0) {
r = (r + 1) % 4;
if (r % 2 == 0)
l++;
rest = l;
}
}
}
bool prime[N + 1] = {};
void make_prime() {
for (int i = 2; i <= N; i++)
prime[i] = true;
for (int i = 2; i <= N; i++)
if (prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
prime[j] = false;
}
}
}
int main() {
init();
make_prime();
int max_n, st;
while (cin >> max_n >> st && max_n) {
static int dp[L][L];
for (int y = 0; y < L; y++) {
for (int x = 0; x < L; x++) {
if (grid[y][x] == st)
dp[y][x] = prime[grid[y][x]];
else
dp[y][x] = -INF;
}
}
int ans1 = 0;
int ans2 = 0;
for (int y = 1; y < L; y++) {
for (int x = 0; x < L; x++)
if (grid[y][x] <= max_n) {
for (int dx = -1; dx <= 1; dx++)
if (x + dx >= 0 && x + dx < L) {
dp[y][x] = max(dp[y][x], dp[y - 1][x + dx] + prime[grid[y][x]]);
if (dp[y][x] > ans1 && dp[y][x] > 0 && prime[grid[y][x]]) {
ans1 = dp[y][x];
ans2 = grid[y][x];
} else if (dp[y][x] == ans1 && dp[y][x] > 0 &&
prime[grid[y][x]]) {
ans2 = max(ans2, grid[y][x]);
}
}
}
}
printf("%d %d\n", ans1, ans2);
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, -1, 0, 1, 1, 1, -1, -1};
const int L = 3500;
const int N = 1000000;
int grid[L][L];
void init() {
int l = 1;
int x = L / 2;
int y = L / 2;
int r = 0;
int rest = 1;
for (int y = 0; y < L; y++) {
for (int x = 0; x < L; x++) {
grid[y][x] = INF;
}
}
for (int i = 1; i <= N; i++) {
assert(grid[y][x] == INF);
grid[y][x] = i;
x += dx[r];
y += dy[r];
rest--;
if (rest == 0) {
r = (r + 1) % 4;
if (r % 2 == 0)
l++;
rest = l;
}
}
}
bool prime[N + 1] = {};
void make_prime() {
for (int i = 2; i <= N; i++)
prime[i] = true;
for (int i = 2; i <= N; i++)
if (prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
prime[j] = false;
}
}
}
int main() {
init();
make_prime();
int max_n, st;
while (cin >> max_n >> st && max_n) {
static int dp[L][L];
for (int y = 0; y < L; y++) {
for (int x = 0; x < L; x++) {
if (grid[y][x] == st)
dp[y][x] = prime[grid[y][x]];
else
dp[y][x] = -INF;
}
}
int ans1 = 0;
int ans2 = 0;
for (int y = 1; y < L; y++) {
for (int x = 0; x < L; x++)
if (grid[y][x] <= max_n) {
for (int dx = -1; dx <= 1; dx++)
if (x + dx >= 0 && x + dx < L) {
dp[y][x] = max(dp[y][x], dp[y - 1][x + dx] + prime[grid[y][x]]);
if (dp[y][x] > ans1 && dp[y][x] > 0 && prime[grid[y][x]]) {
ans1 = dp[y][x];
ans2 = grid[y][x];
} else if (dp[y][x] == ans1 && dp[y][x] > 0 &&
prime[grid[y][x]]) {
ans2 = max(ans2, grid[y][x]);
}
}
}
}
printf("%d %d\n", ans1, ans2);
}
return 0;
} | replace | 37 | 38 | 37 | 38 | MLE | |
p00771 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
template <class T, int s> using va = vector<array<T, s>>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define rrep(i, a) for (int i = a - 1; i >= 0; i--)
#define rrep1(i, a) for (int i = a; i; i--)
const ll mod = 1000000007;
#ifndef INT_MAX
const int INT_MAX = numeric_limits<signed>().max();
#endif
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>;
template <class T> inline void hash_combine(size_t &seed, const T &v) {
hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std {
template <typename S, typename T> struct hash<pair<S, T>> {
inline size_t operator()(const pair<S, T> &v) const {
size_t seed = 0;
hash_combine(seed, v.first);
hash_combine(seed, v.second);
return seed;
}
};
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t &seed, Tuple const &tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple> struct HashValueImpl<Tuple, 0> {
static void apply(size_t &seed, Tuple const &tuple) {
hash_combine(seed, std::get<0>(tuple));
}
};
template <typename... TT> struct hash<std::tuple<TT...>> {
size_t operator()(std::tuple<TT...> const &tt) const {
size_t seed = 0;
HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
return seed;
}
};
} // namespace std
ll pow(ll base, ll i, ll mod) {
ll a = 1;
while (i) {
if (i & 1) {
a *= base;
a %= mod;
}
base *= base;
base %= mod;
i /= 2;
}
return a;
}
class unionfind {
vector<int> par, rank,
size_; //????????§??????????????¢???????????????????????????rank???????????????size?????????
public:
unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); }
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
size_[x] += size_[y];
if (rank[x] == rank[y])
rank[x]++;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return size_[find(x)]; }
};
ll gcd(ll a, ll b) {
while (b) {
ll c = a % b;
a = b;
b = c;
}
return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int popcnt(unsigned long long a) {
a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555);
a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333);
a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f);
a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff);
a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff);
return (a & 0xffffffff) + (a >> 32);
}
template <class T, class Func = function<const T(const T, const T)>>
class segtree {
vector<T> obj;
int offset;
Func updater;
T e;
int bufsize(int num) {
int i = 1;
for (; num > i; i <<= 1)
;
offset = i - 1;
return (i << 1) - 1;
}
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return e;
if (a <= l && r <= b)
return obj[k];
else
return updater(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
public:
T query(int a, int b) { //[a,b)
return query(a, b, 0, 0, offset + 1);
}
void updateall(int l = 0, int r = -1) {
if (r < 0)
r = offset + 1;
l += offset, r += offset;
do {
l = l - 1 >> 1, r = r - 1 >> 1;
for (int i = l; i < r; i++)
obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]);
} while (l);
}
void update(int k, T &a) {
k += offset;
obj[k] = a;
while (k) {
k = k - 1 >> 1;
obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]);
}
}
segtree(int n, T e, const Func &updater_ = Func())
: obj(bufsize(n), e), e(e), updater(updater_) {}
segtree(vector<T> &vec, T e, const Func &updater = Func())
: obj(bufsize(vec.size()), e), e(e), updater(updater) {
copy(vec.begin(), vec.end(), obj.begin() + offset);
updateall();
}
typename vector<T>::reference operator[](int n) { return obj[n + offset]; }
};
template <class T> class matrix {
vector<vector<T>> obj;
pair<int, int> s;
public:
matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {}
matrix(int n, int m = -1, T e = 0)
: obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {}
static matrix e(int n) {
matrix a = (n);
for (int i = 0; i < n; i++)
a[i][i] = 1;
return a;
}
matrix &operator+=(const matrix &p) {
if (s != p.s)
throw runtime_error("matrix error");
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
obj[i][j] += p.obj[i][j];
return *this;
}
matrix operator+(const matrix &p) {
matrix res(*this);
return res += p;
}
matrix &operator-=(const matrix &p) {
if (s != p.s)
throw runtime_error("matrix error");
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
obj[i][j] -= p.obj[i][j];
return *this;
}
matrix operator-(const matrix &p) {
matrix res(*this);
return res -= p;
}
matrix &operator*=(T p) {
for (auto &a : obj)
for (auto &b : a)
b *= p;
return *this;
}
matrix operator*(T p) {
matrix res(*this);
return res *= p;
}
matrix operator*(const matrix &p) {
if (s.second != p.s.first)
throw runtime_error("matrix error");
matrix ret(s.first, p.s.second);
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
for (int k = 0; k < p.s.second; k++)
ret[i][k] += obj[i][j] * p.obj[j][k];
return ret;
}
matrix &operator*=(const matrix &p) { return *this = *this * p; }
pair<int, int> size() const { return s; }
matrix &mod(T m) {
for (auto &a : obj)
for (auto &b : a)
b %= m;
return *this;
}
typename vector<vector<T>>::reference operator[](int t) { return obj[t]; }
};
template <class T> inline matrix<T> pow(matrix<T> &base, unsigned exp) {
auto base_(base);
if (base_.size().first != base_.size().second)
throw runtime_error("matrix error");
matrix<T> res = matrix<T>::e(base_.size().first);
for (;;) {
if (exp & 1)
res *= base_;
if (!(exp /= 2))
break;
base_ *= base_;
}
return res;
}
template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned exp, T m) {
auto base_(base);
if (base.size().first != base_.size().second)
throw runtime_error("matrix error");
matrix<T> res = matrix<T>::e(base_.size().first);
for (;;) {
if (exp & 1)
(res *= base_).mod(m);
if (!(exp /= 2))
break;
(base_ *= base_).mod(m);
}
return res;
}
template <class T> int id(vector<T> &a, T b) {
return lower_bound(ALL(a), b) - a.begin();
}
class Flow {
int V;
struct edge {
int to, cap, rev, cost;
};
vector<vector<edge>> G;
vector<int> level, iter, h, dist, prevv, preve;
public:
Flow(int size) : G(size + 1), V(size + 1) {}
void add_edge(int from, int to, int cap, int cost = 0) {
G[from].push_back(edge{to, cap, (int)G[to].size(), cost});
G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost});
}
void bfs(int s) {
fill(level.begin(), level.end(), -1);
queue<int> que;
level[s] = 0;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f) {
if (v == t)
return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t) {
level.resize(V);
iter.resize(V);
int flow = 0;
for (;;) {
bfs(s);
if (level[t] < 0)
return flow;
fill(iter.begin(), iter.end(), 0);
int f;
while ((f = dfs(s, t, numeric_limits<int>::max())) > 0) {
flow += f;
}
}
}
typedef pair<int, int> P;
int min_cost_flow(int s, int t, int f) {
int res = 0;
h.resize(V);
dist.resize(V);
prevv.resize(V);
preve.resize(V);
fill(h.begin(), h.end(), 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> que;
fill(dist.begin(), dist.end(), numeric_limits<int>::max());
dist[s] = 0;
que.push({0, s});
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (int i = 0; i < 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;
que.push({dist[e.to], e.to});
}
}
}
if (dist[t] == numeric_limits<int>::max()) {
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;
}
};
const ld eps = 1e-11, pi = acos(-1.0);
typedef complex<ld> P;
typedef vector<P> VP;
ld dot(P a, P b) { return real(conj(a) * b); }
ld cross(P a, P b) { return imag(conj(a) * b); }
namespace std {
bool operator<(const P &a, const P &b) {
return abs(a.real() - b.real()) < eps ? a.imag() < b.imag()
: a.real() < b.real();
}
} // namespace std
struct L {
P a, b;
}; // line->l,segment->s
struct C {
P p;
ld r;
};
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > eps)
return 1; // counter clockwise
if (cross(b, c) < -eps)
return -1; // clockwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(L l, L m) { // is intersect
return abs(cross(l.b - l.a, m.b - m.a)) > eps;
}
bool isis_ls(L l, L s) {
ld a = cross(l.b - l.a, s.a - l.a);
ld b = cross(l.b - l.a, s.b - l.a);
return (a * b < eps);
}
bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; }
bool isis_ss(L s, L t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
P is_ll(L s, L t) { // intersect
P sv = s.b - s.a, tv = t.b - t.a;
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
bool isis_sp(L s, P p) {
return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}
P proj(L l, P p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
ld dist_lp(L l, P p) { return abs(p - proj(l, p)); }
ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }
ld dist_ls(L l, L s) {
if (isis_ls(l, s))
return 0;
return min(dist_lp(l, s.a), dist_lp(l, s.b));
}
ld dist_sp(L s, P p) {
P r = proj(s, p);
if (isis_sp(s, r))
return abs(r - p);
return min(abs(s.a - p), abs(s.b - p));
}
ld dist_ss(L s, L t) {
if (isis_ss(s, t))
return 0;
ld a = min(dist_sp(s, t.a), dist_sp(t, s.a));
ld b = min(dist_sp(s, t.b), dist_sp(t, s.b));
return min(a, b);
}
VP is_cc(C c1, C c2) {
VP res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
P diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * P(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * P(rc, -rs));
return res;
}
bool isis_vc(vector<C> vc) {
VP crs;
int n = vc.size();
rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p);
rep(i, n) crs.push_back(vc[i].p);
for (P p : crs) {
bool valid = true;
rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false;
if (valid)
return true;
}
return false;
}
VP is_lc(C c, L l) {
VP res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
P nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
VP is_sc(C c, L l) {
VP v = is_lc(c, l), res;
for (P p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
vector<L> tangent_cp(C c, P p) { //????????\????
vector<L> ret;
P v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
P v1 = v * P(l / d, c.r / d);
P v2 = v * P(l / d, -c.r / d);
ret.push_back(L{p, p + v1});
if (l < eps)
return ret;
ret.push_back(L{p, p + v2});
return ret;
}
vector<L> tangent_cc(C c1, C c2) {
vector<L> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<L> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
P v = c2.p - c1.p;
v /= abs(v);
P q1 = c1.p + v * P(0, 1) * c1.r;
P q2 = c1.p + v * P(0, -1) * c1.r;
ret.push_back(L{q1, q1 + v});
ret.push_back(L{q2, q2 + v});
}
return ret;
}
ld area(const VP &p) { //??¢?????
ld res = 0;
int n = p.size();
rep(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
bool is_polygon(L l, VP &g) {
int n = g.size();
for (int i = 0; i < n; i++) {
P a = g[i];
P b = g[(i + 1) % n];
if (isis_ss(l, L{a, b}))
return true;
}
return false;
}
int is_in_Polygon(const VP &g, P p) {
bool in = false;
int n = g.size();
for (int i = 0; i < n; i++) {
P a = g[i] - p, b = g[(i + 1) % n] - p;
if (imag(a) > imag(b))
swap(a, b);
if (imag(a) <= 0 && 0 < imag(b))
if (cross(a, b) < 0)
in = !in;
if (abs(cross(a, b)) < eps && dot(a, b) < eps)
return 0; // on
}
if (in)
return 1; // in
return -1; // out
}
VP ConvexHull(VP ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
VP ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
VP ConvexCut(const VP &ps, L l) {
VP Q;
for (int i = 0; i < (int)ps.size(); i++) {
P A = ps[i], B = ps[(i + 1) % ps.size()];
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0)
Q.push_back(is_ll(L{A, B}, l));
}
return Q;
}
double dist2(P a) { return real(a) * real(a) + imag(a) * imag(a); }
// Suffix Array ?????????O(|S|log^2|S|), ????´¢O(|T|log|S|),
// ?????????????§????O(|S|)
class StringSearch {
const int n;
string S;
public:
vector<int> sa, rank;
StringSearch(const string &S_) : n(S_.size()), S(S_), sa(n + 1), rank(n + 1) {
for (int i = 0; i <= n; i++) {
sa[i] = i;
rank[i] = i < n ? S[i] : -1;
}
vector<int> tmp(n + 1);
for (int k = 1; k <= n; k *= 2) {
auto Compare_SA = [=](int i, int j) {
if (this->rank[i] != this->rank[j])
return this->rank[i] < this->rank[j];
int ri = i + k <= n ? this->rank[i + k] : -1;
int rj = j + k <= n ? this->rank[j + k] : -1;
return ri < rj;
};
sort(sa.begin(), sa.end(), Compare_SA);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++) {
this->rank[i] = tmp[i];
}
}
}
bool Contain(const string &T) {
int a = 0, b = n;
while (b - a > 1) {
int c = (a + b) / 2;
if (S.compare(sa[c], T.length(), T) < 0)
a = c;
else
b = c;
}
return S.compare(sa[b], T.length(), T) == 0;
}
vector<int> LCPArray() {
for (int i = 0; i <= n; i++)
rank[sa[i]] = i;
int h = 0;
vector<int> lcp(n + 1);
for (int i = 0; i < n; i++) {
int j = sa[rank[i] - 1];
if (h > 0)
h--;
for (; j + h < n && i + h < n; h++) {
if (S[j + h] != S[i + h])
break;
}
lcp[rank[i] - 1] = h;
}
return lcp;
}
};
int main() {
int n;
while (cin >> n, n) {
va<int, 3> bl(n);
rep(i, n) rep(j, 3) cin >> bl[i][j];
// va<double, 3> h;
double ans = 1e9;
int mina = 0;
rep(i, n) if (ans > bl[i][2]) ans = bl[i][2], mina = i;
ans *= ans;
rep(j, n) {
int i = mina;
if ((bl[i][0] - bl[j][0]) * (bl[i][0] - bl[j][0]) +
(bl[i][1] - bl[j][1]) * (bl[i][1] - bl[j][1]) +
bl[i][2] * bl[i][2] >
bl[j][2] * bl[j][2]) {
ans = 1e9;
break;
}
}
rep(i, n) rep(j, i) {
double l = hypot(bl[i][0] - bl[j][0], bl[i][1] - bl[j][1]);
double a = ((bl[j][2] * bl[j][2] - bl[i][2] * bl[i][2]) / l + l) / 2;
double x = (bl[i][0] * a + bl[j][0] * (l - a)) / l,
y = (bl[i][1] * a + bl[j][1] * (l - a)) / l,
z = bl[j][2] * bl[j][2] - a * a;
if (z < 0 || a <= 0 || a >= l)
continue;
if (z >= ans)
continue;
ans = z;
// h.push_back({ x,y,z });
}
rep(i, n) rep(j, i) rep(k, j) {
double x[2][3] = {
{double(bl[i][0] - bl[j][0]), double(bl[i][1] - bl[j][1]),
double(-bl[i][2] * bl[i][2] + bl[j][2] * bl[j][2] +
bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] -
bl[j][0] * bl[j][0] - bl[j][1] * bl[j][1])},
{double(bl[i][0] - bl[k][0]), double(bl[i][1] - bl[k][1]),
double(-bl[i][2] * bl[i][2] + bl[k][2] * bl[k][2] +
bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] -
bl[k][0] * bl[k][0] - bl[k][1] * bl[k][1])}};
double a, b, c;
if (x[1][1] == 0) {
if (x[0][1] == 0)
continue;
if (x[1][0] - (x[1][1] / x[0][1]) * x[0][0] == 0)
continue;
a = (x[1][2] - x[1][1] / x[0][1] * x[0][2]) /
(x[1][0] - (x[1][1] / x[0][1]) * x[0][0]) * 0.5;
b = (x[0][2] - 2 * a * x[0][0]) * 0.5 / x[0][1];
} else {
if (x[0][0] - (x[0][1] / x[1][1]) * x[1][0] == 0)
continue;
a = (x[0][2] - x[0][1] / x[1][1] * x[1][2]) /
(x[0][0] - (x[0][1] / x[1][1]) * x[1][0]) * 0.5;
b = (x[1][2] - 2 * a * x[1][0]) * 0.5 / x[1][1];
}
c = bl[i][2] * bl[i][2] - (a - bl[i][0]) * (a - bl[i][0]) -
(b - bl[i][1]) * (b - bl[i][1]);
if (c < 0)
continue;
if (c >= ans)
continue;
int cc = ccw(P(bl[i][0], bl[i][1]), P(bl[j][0], bl[j][1]), P(a, b));
if (cc == ccw(P(bl[j][0], bl[j][1]), P(bl[k][0], bl[k][1]), P(a, b)) &&
cc == ccw(P(bl[k][0], bl[k][1]), P(bl[i][0], bl[i][1]), P(a, b)))
ans = c;
// h.push_back({ a,b,c });
/*bool f = 1;
rep(i, n) {
if ((a - bl[i][0])*(a - bl[i][0]) + (b - bl[i][1])*(b - bl[i][1])
+ c > bl[i][2]* bl[i][2] + 1e-7) { f = 0; break;
}
}
if (f) {
cmax(ans, sqrt(c));
}*/
}
volatile int a = 0;
rep(i, 50000000) a++;
// sort(ALL(h), [](const array<double, 6> &a, const array<double, 6> &b)
// {return a[2] != b[2] ? a[2] > b[2]:a > b; });
/*double ans = 0;
for (auto &&x : h) {
bool f = 1;
rep(i, n) {
if (pow(x[0] - bl[i][0], 2) + pow(x[1] - bl[i][1], 2) +
pow(x[2], 2) > pow(bl[i][2], 2)+1e-7) { f = 0; break;
}
}
if (f) {
cmax(ans, x[2]);
}
}*/
cout << fixed << setprecision(7) << sqrt(ans) << endl;
}
} | #include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
template <class T, int s> using va = vector<array<T, s>>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
template <class T, class S> void cmin(T &a, const S &b) {
if (a > b)
a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
if (a < b)
a = b;
}
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define rrep(i, a) for (int i = a - 1; i >= 0; i--)
#define rrep1(i, a) for (int i = a; i; i--)
const ll mod = 1000000007;
#ifndef INT_MAX
const int INT_MAX = numeric_limits<signed>().max();
#endif
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>;
template <class T> inline void hash_combine(size_t &seed, const T &v) {
hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std {
template <typename S, typename T> struct hash<pair<S, T>> {
inline size_t operator()(const pair<S, T> &v) const {
size_t seed = 0;
hash_combine(seed, v.first);
hash_combine(seed, v.second);
return seed;
}
};
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t &seed, Tuple const &tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple> struct HashValueImpl<Tuple, 0> {
static void apply(size_t &seed, Tuple const &tuple) {
hash_combine(seed, std::get<0>(tuple));
}
};
template <typename... TT> struct hash<std::tuple<TT...>> {
size_t operator()(std::tuple<TT...> const &tt) const {
size_t seed = 0;
HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
return seed;
}
};
} // namespace std
ll pow(ll base, ll i, ll mod) {
ll a = 1;
while (i) {
if (i & 1) {
a *= base;
a %= mod;
}
base *= base;
base %= mod;
i /= 2;
}
return a;
}
class unionfind {
vector<int> par, rank,
size_; //????????§??????????????¢???????????????????????????rank???????????????size?????????
public:
unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); }
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return;
if (rank[x] < rank[y])
swap(x, y);
par[y] = x;
size_[x] += size_[y];
if (rank[x] == rank[y])
rank[x]++;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return size_[find(x)]; }
};
ll gcd(ll a, ll b) {
while (b) {
ll c = a % b;
a = b;
b = c;
}
return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int popcnt(unsigned long long a) {
a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555);
a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333);
a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f);
a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff);
a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff);
return (a & 0xffffffff) + (a >> 32);
}
template <class T, class Func = function<const T(const T, const T)>>
class segtree {
vector<T> obj;
int offset;
Func updater;
T e;
int bufsize(int num) {
int i = 1;
for (; num > i; i <<= 1)
;
offset = i - 1;
return (i << 1) - 1;
}
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return e;
if (a <= l && r <= b)
return obj[k];
else
return updater(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
public:
T query(int a, int b) { //[a,b)
return query(a, b, 0, 0, offset + 1);
}
void updateall(int l = 0, int r = -1) {
if (r < 0)
r = offset + 1;
l += offset, r += offset;
do {
l = l - 1 >> 1, r = r - 1 >> 1;
for (int i = l; i < r; i++)
obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]);
} while (l);
}
void update(int k, T &a) {
k += offset;
obj[k] = a;
while (k) {
k = k - 1 >> 1;
obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]);
}
}
segtree(int n, T e, const Func &updater_ = Func())
: obj(bufsize(n), e), e(e), updater(updater_) {}
segtree(vector<T> &vec, T e, const Func &updater = Func())
: obj(bufsize(vec.size()), e), e(e), updater(updater) {
copy(vec.begin(), vec.end(), obj.begin() + offset);
updateall();
}
typename vector<T>::reference operator[](int n) { return obj[n + offset]; }
};
template <class T> class matrix {
vector<vector<T>> obj;
pair<int, int> s;
public:
matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {}
matrix(int n, int m = -1, T e = 0)
: obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {}
static matrix e(int n) {
matrix a = (n);
for (int i = 0; i < n; i++)
a[i][i] = 1;
return a;
}
matrix &operator+=(const matrix &p) {
if (s != p.s)
throw runtime_error("matrix error");
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
obj[i][j] += p.obj[i][j];
return *this;
}
matrix operator+(const matrix &p) {
matrix res(*this);
return res += p;
}
matrix &operator-=(const matrix &p) {
if (s != p.s)
throw runtime_error("matrix error");
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
obj[i][j] -= p.obj[i][j];
return *this;
}
matrix operator-(const matrix &p) {
matrix res(*this);
return res -= p;
}
matrix &operator*=(T p) {
for (auto &a : obj)
for (auto &b : a)
b *= p;
return *this;
}
matrix operator*(T p) {
matrix res(*this);
return res *= p;
}
matrix operator*(const matrix &p) {
if (s.second != p.s.first)
throw runtime_error("matrix error");
matrix ret(s.first, p.s.second);
for (int i = 0; i < s.first; i++)
for (int j = 0; j < s.second; j++)
for (int k = 0; k < p.s.second; k++)
ret[i][k] += obj[i][j] * p.obj[j][k];
return ret;
}
matrix &operator*=(const matrix &p) { return *this = *this * p; }
pair<int, int> size() const { return s; }
matrix &mod(T m) {
for (auto &a : obj)
for (auto &b : a)
b %= m;
return *this;
}
typename vector<vector<T>>::reference operator[](int t) { return obj[t]; }
};
template <class T> inline matrix<T> pow(matrix<T> &base, unsigned exp) {
auto base_(base);
if (base_.size().first != base_.size().second)
throw runtime_error("matrix error");
matrix<T> res = matrix<T>::e(base_.size().first);
for (;;) {
if (exp & 1)
res *= base_;
if (!(exp /= 2))
break;
base_ *= base_;
}
return res;
}
template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned exp, T m) {
auto base_(base);
if (base.size().first != base_.size().second)
throw runtime_error("matrix error");
matrix<T> res = matrix<T>::e(base_.size().first);
for (;;) {
if (exp & 1)
(res *= base_).mod(m);
if (!(exp /= 2))
break;
(base_ *= base_).mod(m);
}
return res;
}
template <class T> int id(vector<T> &a, T b) {
return lower_bound(ALL(a), b) - a.begin();
}
class Flow {
int V;
struct edge {
int to, cap, rev, cost;
};
vector<vector<edge>> G;
vector<int> level, iter, h, dist, prevv, preve;
public:
Flow(int size) : G(size + 1), V(size + 1) {}
void add_edge(int from, int to, int cap, int cost = 0) {
G[from].push_back(edge{to, cap, (int)G[to].size(), cost});
G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost});
}
void bfs(int s) {
fill(level.begin(), level.end(), -1);
queue<int> que;
level[s] = 0;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f) {
if (v == t)
return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t) {
level.resize(V);
iter.resize(V);
int flow = 0;
for (;;) {
bfs(s);
if (level[t] < 0)
return flow;
fill(iter.begin(), iter.end(), 0);
int f;
while ((f = dfs(s, t, numeric_limits<int>::max())) > 0) {
flow += f;
}
}
}
typedef pair<int, int> P;
int min_cost_flow(int s, int t, int f) {
int res = 0;
h.resize(V);
dist.resize(V);
prevv.resize(V);
preve.resize(V);
fill(h.begin(), h.end(), 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> que;
fill(dist.begin(), dist.end(), numeric_limits<int>::max());
dist[s] = 0;
que.push({0, s});
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (dist[v] < p.first)
continue;
for (int i = 0; i < 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;
que.push({dist[e.to], e.to});
}
}
}
if (dist[t] == numeric_limits<int>::max()) {
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;
}
};
const ld eps = 1e-11, pi = acos(-1.0);
typedef complex<ld> P;
typedef vector<P> VP;
ld dot(P a, P b) { return real(conj(a) * b); }
ld cross(P a, P b) { return imag(conj(a) * b); }
namespace std {
bool operator<(const P &a, const P &b) {
return abs(a.real() - b.real()) < eps ? a.imag() < b.imag()
: a.real() < b.real();
}
} // namespace std
struct L {
P a, b;
}; // line->l,segment->s
struct C {
P p;
ld r;
};
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > eps)
return 1; // counter clockwise
if (cross(b, c) < -eps)
return -1; // clockwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(L l, L m) { // is intersect
return abs(cross(l.b - l.a, m.b - m.a)) > eps;
}
bool isis_ls(L l, L s) {
ld a = cross(l.b - l.a, s.a - l.a);
ld b = cross(l.b - l.a, s.b - l.a);
return (a * b < eps);
}
bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; }
bool isis_ss(L s, L t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
P is_ll(L s, L t) { // intersect
P sv = s.b - s.a, tv = t.b - t.a;
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
bool isis_sp(L s, P p) {
return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}
P proj(L l, P p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
return l.a + t * (l.a - l.b);
}
ld dist_lp(L l, P p) { return abs(p - proj(l, p)); }
ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }
ld dist_ls(L l, L s) {
if (isis_ls(l, s))
return 0;
return min(dist_lp(l, s.a), dist_lp(l, s.b));
}
ld dist_sp(L s, P p) {
P r = proj(s, p);
if (isis_sp(s, r))
return abs(r - p);
return min(abs(s.a - p), abs(s.b - p));
}
ld dist_ss(L s, L t) {
if (isis_ss(s, t))
return 0;
ld a = min(dist_sp(s, t.a), dist_sp(t, s.a));
ld b = min(dist_sp(s, t.b), dist_sp(t, s.b));
return min(a, b);
}
VP is_cc(C c1, C c2) {
VP res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps)
dfr = 0.0;
else if (dfr < 0.0)
return res; // no intersection
ld rs = sqrt(dfr);
P diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * P(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * P(rc, -rs));
return res;
}
bool isis_vc(vector<C> vc) {
VP crs;
int n = vc.size();
rep(i, n) rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p);
rep(i, n) crs.push_back(vc[i].p);
for (P p : crs) {
bool valid = true;
rep(i, n) if (abs(p - vc[i].p) > vc[i].r + eps) valid = false;
if (valid)
return true;
}
return false;
}
VP is_lc(C c, L l) {
VP res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety;
P nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
VP is_sc(C c, L l) {
VP v = is_lc(c, l), res;
for (P p : v)
if (isis_sp(l, p))
res.push_back(p);
return res;
}
vector<L> tangent_cp(C c, P p) { //????????\????
vector<L> ret;
P v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return ret;
}
P v1 = v * P(l / d, c.r / d);
P v2 = v * P(l / d, -c.r / d);
ret.push_back(L{p, p + v1});
if (l < eps)
return ret;
ret.push_back(L{p, p + v2});
return ret;
}
vector<L> tangent_cc(C c1, C c2) {
vector<L> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<L> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
} else {
P v = c2.p - c1.p;
v /= abs(v);
P q1 = c1.p + v * P(0, 1) * c1.r;
P q2 = c1.p + v * P(0, -1) * c1.r;
ret.push_back(L{q1, q1 + v});
ret.push_back(L{q2, q2 + v});
}
return ret;
}
ld area(const VP &p) { //??¢?????
ld res = 0;
int n = p.size();
rep(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
bool is_polygon(L l, VP &g) {
int n = g.size();
for (int i = 0; i < n; i++) {
P a = g[i];
P b = g[(i + 1) % n];
if (isis_ss(l, L{a, b}))
return true;
}
return false;
}
int is_in_Polygon(const VP &g, P p) {
bool in = false;
int n = g.size();
for (int i = 0; i < n; i++) {
P a = g[i] - p, b = g[(i + 1) % n] - p;
if (imag(a) > imag(b))
swap(a, b);
if (imag(a) <= 0 && 0 < imag(b))
if (cross(a, b) < 0)
in = !in;
if (abs(cross(a, b)) < eps && dot(a, b) < eps)
return 0; // on
}
if (in)
return 1; // in
return -1; // out
}
VP ConvexHull(VP ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
VP ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
--k;
ch.resize(k - 1);
return ch;
}
VP ConvexCut(const VP &ps, L l) {
VP Q;
for (int i = 0; i < (int)ps.size(); i++) {
P A = ps[i], B = ps[(i + 1) % ps.size()];
if (ccw(l.a, l.b, A) != -1)
Q.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0)
Q.push_back(is_ll(L{A, B}, l));
}
return Q;
}
double dist2(P a) { return real(a) * real(a) + imag(a) * imag(a); }
// Suffix Array ?????????O(|S|log^2|S|), ????´¢O(|T|log|S|),
// ?????????????§????O(|S|)
class StringSearch {
const int n;
string S;
public:
vector<int> sa, rank;
StringSearch(const string &S_) : n(S_.size()), S(S_), sa(n + 1), rank(n + 1) {
for (int i = 0; i <= n; i++) {
sa[i] = i;
rank[i] = i < n ? S[i] : -1;
}
vector<int> tmp(n + 1);
for (int k = 1; k <= n; k *= 2) {
auto Compare_SA = [=](int i, int j) {
if (this->rank[i] != this->rank[j])
return this->rank[i] < this->rank[j];
int ri = i + k <= n ? this->rank[i + k] : -1;
int rj = j + k <= n ? this->rank[j + k] : -1;
return ri < rj;
};
sort(sa.begin(), sa.end(), Compare_SA);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++) {
this->rank[i] = tmp[i];
}
}
}
bool Contain(const string &T) {
int a = 0, b = n;
while (b - a > 1) {
int c = (a + b) / 2;
if (S.compare(sa[c], T.length(), T) < 0)
a = c;
else
b = c;
}
return S.compare(sa[b], T.length(), T) == 0;
}
vector<int> LCPArray() {
for (int i = 0; i <= n; i++)
rank[sa[i]] = i;
int h = 0;
vector<int> lcp(n + 1);
for (int i = 0; i < n; i++) {
int j = sa[rank[i] - 1];
if (h > 0)
h--;
for (; j + h < n && i + h < n; h++) {
if (S[j + h] != S[i + h])
break;
}
lcp[rank[i] - 1] = h;
}
return lcp;
}
};
int main() {
int n;
while (cin >> n, n) {
va<int, 3> bl(n);
rep(i, n) rep(j, 3) cin >> bl[i][j];
// va<double, 3> h;
double ans = 1e9;
int mina = 0;
rep(i, n) if (ans > bl[i][2]) ans = bl[i][2], mina = i;
ans *= ans;
rep(j, n) {
int i = mina;
if ((bl[i][0] - bl[j][0]) * (bl[i][0] - bl[j][0]) +
(bl[i][1] - bl[j][1]) * (bl[i][1] - bl[j][1]) +
bl[i][2] * bl[i][2] >
bl[j][2] * bl[j][2]) {
ans = 1e9;
break;
}
}
rep(i, n) rep(j, i) {
double l = hypot(bl[i][0] - bl[j][0], bl[i][1] - bl[j][1]);
double a = ((bl[j][2] * bl[j][2] - bl[i][2] * bl[i][2]) / l + l) / 2;
double x = (bl[i][0] * a + bl[j][0] * (l - a)) / l,
y = (bl[i][1] * a + bl[j][1] * (l - a)) / l,
z = bl[j][2] * bl[j][2] - a * a;
if (z < 0 || a <= 0 || a >= l)
continue;
if (z >= ans)
continue;
ans = z;
// h.push_back({ x,y,z });
}
rep(i, n) rep(j, i) rep(k, j) {
double x[2][3] = {
{double(bl[i][0] - bl[j][0]), double(bl[i][1] - bl[j][1]),
double(-bl[i][2] * bl[i][2] + bl[j][2] * bl[j][2] +
bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] -
bl[j][0] * bl[j][0] - bl[j][1] * bl[j][1])},
{double(bl[i][0] - bl[k][0]), double(bl[i][1] - bl[k][1]),
double(-bl[i][2] * bl[i][2] + bl[k][2] * bl[k][2] +
bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] -
bl[k][0] * bl[k][0] - bl[k][1] * bl[k][1])}};
double a, b, c;
if (x[1][1] == 0) {
if (x[0][1] == 0)
continue;
if (x[1][0] - (x[1][1] / x[0][1]) * x[0][0] == 0)
continue;
a = (x[1][2] - x[1][1] / x[0][1] * x[0][2]) /
(x[1][0] - (x[1][1] / x[0][1]) * x[0][0]) * 0.5;
b = (x[0][2] - 2 * a * x[0][0]) * 0.5 / x[0][1];
} else {
if (x[0][0] - (x[0][1] / x[1][1]) * x[1][0] == 0)
continue;
a = (x[0][2] - x[0][1] / x[1][1] * x[1][2]) /
(x[0][0] - (x[0][1] / x[1][1]) * x[1][0]) * 0.5;
b = (x[1][2] - 2 * a * x[1][0]) * 0.5 / x[1][1];
}
c = bl[i][2] * bl[i][2] - (a - bl[i][0]) * (a - bl[i][0]) -
(b - bl[i][1]) * (b - bl[i][1]);
if (c < 0)
continue;
if (c >= ans)
continue;
int cc = ccw(P(bl[i][0], bl[i][1]), P(bl[j][0], bl[j][1]), P(a, b));
if (cc == ccw(P(bl[j][0], bl[j][1]), P(bl[k][0], bl[k][1]), P(a, b)) &&
cc == ccw(P(bl[k][0], bl[k][1]), P(bl[i][0], bl[i][1]), P(a, b)))
ans = c;
// h.push_back({ a,b,c });
/*bool f = 1;
rep(i, n) {
if ((a - bl[i][0])*(a - bl[i][0]) + (b - bl[i][1])*(b - bl[i][1])
+ c > bl[i][2]* bl[i][2] + 1e-7) { f = 0; break;
}
}
if (f) {
cmax(ans, sqrt(c));
}*/
}
volatile int a = 0;
rep(i, 500000) a++;
// sort(ALL(h), [](const array<double, 6> &a, const array<double, 6> &b)
// {return a[2] != b[2] ? a[2] > b[2]:a > b; });
/*double ans = 0;
for (auto &&x : h) {
bool f = 1;
rep(i, n) {
if (pow(x[0] - bl[i][0], 2) + pow(x[1] - bl[i][1], 2) +
pow(x[2], 2) > pow(bl[i][2], 2)+1e-7) { f = 0; break;
}
}
if (f) {
cmax(ans, x[2]);
}
}*/
cout << fixed << setprecision(7) << sqrt(ans) << endl;
}
} | replace | 794 | 795 | 794 | 795 | TLE | |
p00771 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
#define LT(x, y) ((x) - (y) <= -EPS)
vector<P> cross_circles(P c1, double r1, P c2, double r2) {
double d = abs(c2 - c1);
if (LT(r1 + r2, d) || LT(d, fabs(r1 - r2)))
return vector<P>();
double l = 0.5 * ((r1 * r1 - r2 * r2) / d + d);
double h = sqrt(r1 * r1 - l * l);
vector<P> ret(2);
ret[0] = P(l, +h) * (c2 - c1) / d + c1;
ret[1] = P(l, -h) * (c2 - c1) / d + c1;
return ret;
}
double distance(double y1, double x1, double y2, double x2) {
double dy = y1 - y2;
double dx = x1 - x2;
return sqrt(dy * dy + dx * dx);
}
bool ok(vi &y, vi &x, vi &l, double h) {
int n = y.size();
vector<P> all_cross;
REP(i, n) {
FOR(j, i + 1, n) {
if (l[i] + EPS < h || l[j] + EPS < h) {
return false;
}
double di = sqrt(l[i] * l[i] - h * h);
double dj = sqrt(l[j] * l[j] - h * h);
double dist2 = distance(y[i], x[i], y[j], x[j]);
bool intersected = di + dj + EPS > dist2;
vector<P> cross = cross_circles(P(y[i], x[i]), di, P(y[j], x[j]), dj);
if (cross.size() == 0) {
if (!intersected) {
return false;
}
} else {
all_cross.push_back(cross[0]);
all_cross.push_back(cross[1]);
}
}
all_cross.push_back(P(y[i], x[i]));
}
REP(ci, all_cross.size()) {
bool ok = true;
REP(k, n) {
double dk = sqrt(l[k] * l[k] - h * h);
if (distance(y[k], x[k], all_cross[ci].real(), all_cross[ci].imag()) >
dk + EPS) {
ok = false;
}
}
if (ok) {
return true;
}
}
return false;
}
int main() {
cout.precision(30);
int n;
while (cin >> n, n) {
vi x(n), y(n), l(n);
int maxl = 0;
REP(i, n) {
cin >> x[i] >> y[i] >> l[i];
maxl = max(maxl, l[i]);
}
double lb = 1, ub = maxl;
REP(time, 10000) {
double mid = (lb + ub) / 2;
if (!ok(y, x, l, mid)) {
ub = mid;
} else {
lb = mid;
}
}
cout << ub << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
#define LT(x, y) ((x) - (y) <= -EPS)
vector<P> cross_circles(P c1, double r1, P c2, double r2) {
double d = abs(c2 - c1);
if (LT(r1 + r2, d) || LT(d, fabs(r1 - r2)))
return vector<P>();
double l = 0.5 * ((r1 * r1 - r2 * r2) / d + d);
double h = sqrt(r1 * r1 - l * l);
vector<P> ret(2);
ret[0] = P(l, +h) * (c2 - c1) / d + c1;
ret[1] = P(l, -h) * (c2 - c1) / d + c1;
return ret;
}
double distance(double y1, double x1, double y2, double x2) {
double dy = y1 - y2;
double dx = x1 - x2;
return sqrt(dy * dy + dx * dx);
}
bool ok(vi &y, vi &x, vi &l, double h) {
int n = y.size();
vector<P> all_cross;
REP(i, n) {
FOR(j, i + 1, n) {
if (l[i] + EPS < h || l[j] + EPS < h) {
return false;
}
double di = sqrt(l[i] * l[i] - h * h);
double dj = sqrt(l[j] * l[j] - h * h);
double dist2 = distance(y[i], x[i], y[j], x[j]);
bool intersected = di + dj + EPS > dist2;
vector<P> cross = cross_circles(P(y[i], x[i]), di, P(y[j], x[j]), dj);
if (cross.size() == 0) {
if (!intersected) {
return false;
}
} else {
all_cross.push_back(cross[0]);
all_cross.push_back(cross[1]);
}
}
all_cross.push_back(P(y[i], x[i]));
}
REP(ci, all_cross.size()) {
bool ok = true;
REP(k, n) {
double dk = sqrt(l[k] * l[k] - h * h);
if (distance(y[k], x[k], all_cross[ci].real(), all_cross[ci].imag()) >
dk + EPS) {
ok = false;
}
}
if (ok) {
return true;
}
}
return false;
}
int main() {
cout.precision(30);
int n;
while (cin >> n, n) {
vi x(n), y(n), l(n);
int maxl = 0;
REP(i, n) {
cin >> x[i] >> y[i] >> l[i];
maxl = max(maxl, l[i]);
}
double lb = 1, ub = maxl;
REP(time, 1000) {
double mid = (lb + ub) / 2;
if (!ok(y, x, l, mid)) {
ub = mid;
} else {
lb = mid;
}
}
cout << ub << endl;
}
} | replace | 123 | 124 | 123 | 124 | TLE | |
p00771 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;
namespace std {
bool operator<(const Point &a, const Point &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
struct Line {
Point a, b;
Line(Point p, Point q) : a(p), b(q){};
Line(ld x1, ld y1, ld x2, ld y2) : a(Point(x1, y1)), b(Point(x2, y2)){};
};
struct Circle {
Point p;
ld r;
Circle(Point a, ld b) : p(a), r(b){};
};
ld dot(Point a, Point b) { return real(conj(a) * b); }
ld cross(Point a, Point b) { return imag(conj(a) * b); }
int ccw(Point a, Point b, Point c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return 1; // counter cloclwise
if (cross(b, c) < -EPS)
return -1; // cloclwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }
bool isis_ls(Line l, Line s) {
return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}
bool isis_ss(Line s, Line t) {
return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}
bool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < EPS); }
bool isis_sp(Line s, Point p) {
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}
Point projection(Line l, Point p) {
Point base = l.b - l.a;
ld r = dot(p - l.a, base) / norm(base);
return l.a + base * r;
}
Point mirror(Line l, Point p) { return Point(2, 0) * projection(l, p) - p; }
ld dist_lp(Line l, Point p) { return abs(p - projection(l, p)); }
ld dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }
ld dist_ls(Line l, Line s) {
if (isis_ls(l, s))
return 0;
return min(dist_lp(l, s.a), dist_lp(l, s.b));
}
ld dist_sp(Line s, Point p) {
Point r = projection(s, p);
if (isis_sp(s, r))
return abs(r - p);
return min(abs(s.a - p), abs(s.b - p));
}
ld dist_ss(Line s, Line t) {
if (isis_ss(s, t))
return 0;
return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
min(dist_sp(t, s.a), dist_sp(t, s.b)));
}
Point is_ll(Line s, Line t) {
ld a = cross(s.b - s.a, t.b - t.a);
ld b = cross(s.b - s.a, s.b - t.a);
return t.a + b / a * (t.b - t.a);
}
vector<Point> is_cc(Circle c1, Circle c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
ld dfr = pow(c1.r, 2) - rc * rc;
if (abs(dfr) < EPS)
dfr = 0;
if (dfr < 0.0)
return res;
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
int isis_cc(Circle c1, Circle c2) {
ld d = abs(c1.p - c2.p);
if (d - c1.r - c2.r < -EPS)
return -2; // separate
if (abs(d - c1.r - c2.r) < EPS)
return -1; // circumscribe
if (c1.r < c2.r)
swap(c1, c2);
if (abs(d - c1.r + c2.r) < EPS)
return 1; // inscribe
if (c1.r - d - c2.r > EPS)
return 2; // involve
else
return 0; // intersect
}
bool isis_cl(Circle c, Line l) {
ld d = dist_lp(l, c.p);
return d - c.r < -EPS;
}
vector<Point> is_cl(Circle c, Line l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d > c.r + EPS)
return res;
ld len = (d > c.r) ? 0 : sqrt(c.r * c.r - d * d);
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(projection(l, c.p) + len * nor);
if (abs(len) > EPS)
res.push_back(projection(l, c.p - len * nor));
return res;
}
vector<Point> is_cs(Circle c, Line s) {
vector<Point> v = is_cl(c, s), res;
REP(i, v.size()) {
if (ccw(s.a, v[i], s.b) == -2)
res.push_back(v[i]);
}
return res;
}
vector<Line> tangent_cp(Circle c, Point p) {
vector<Line> res;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return res;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
res.push_back(Line(p, p + v1));
if (l < EPS)
return res;
res.push_back(Line(p, p + v2));
return res;
}
vector<Line> tangent_cc(Circle c1, Circle c2) {
vector<Line> res;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -EPS) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
res = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > EPS) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nres = tangent_cp(c1, out);
res.insert(res.end(), nres.begin(), nres.end());
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
res.push_back(Line(q1, q1 + v));
res.push_back(Line(q2, q2 + v));
}
return res;
}
bool is_in(Circle c, Point p) {
ld dist = abs(c.p - p);
return c.r - dist > -EPS;
}
typedef pair<Point, ld> Data;
int main() {
cin.sync_with_stdio(false);
int n;
while (cin >> n, n) {
vector<Data> v;
REP(i, n) {
ld x, y, l;
cin >> x >> y >> l;
v.emplace_back(Point(x, y), l);
}
ld lb = 0, ub = INF;
REP(loop, 1000) {
ld height = (lb + ub) / 2.0;
vector<Circle> vc;
REP(i, n) {
ld r = sqrt((v[i].second * v[i].second) - height * height);
vc.emplace_back(v[i].first, r);
}
vector<Point> ps;
REP(i, n) ps.emplace_back(vc[i].p);
REP(i, n) REP(j, i) {
if (isis_cc(vc[i], vc[j])) {
vector<Point> tmp = is_cc(vc[i], vc[j]);
for (auto k : tmp)
ps.push_back(k);
}
}
bool f = false;
REP(i, ps.size()) {
bool ff = true;
REP(j, vc.size()) {
if (!is_in(vc[j], ps[i]))
ff = false;
}
if (ff)
f = true;
}
if (f)
lb = height;
else
ub = height;
}
cout << D10 << lb << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
typedef long double ld;
const ld PI = acos(-1.0);
bool eq(ld a, ld b) { return abs(a - b) < EPS; }
typedef complex<ld> Point;
typedef vector<Point> Polygon;
namespace std {
bool operator<(const Point &a, const Point &b) {
return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}
} // namespace std
struct Line {
Point a, b;
Line(Point p, Point q) : a(p), b(q){};
Line(ld x1, ld y1, ld x2, ld y2) : a(Point(x1, y1)), b(Point(x2, y2)){};
};
struct Circle {
Point p;
ld r;
Circle(Point a, ld b) : p(a), r(b){};
};
ld dot(Point a, Point b) { return real(conj(a) * b); }
ld cross(Point a, Point b) { return imag(conj(a) * b); }
int ccw(Point a, Point b, Point c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return 1; // counter cloclwise
if (cross(b, c) < -EPS)
return -1; // cloclwise
if (dot(b, c) < 0)
return 2; // c--a--b on line
if (norm(b) < norm(c))
return -2; // a--b--c on line
return 0; // a--c--b on line
}
bool isis_ll(Line l, Line m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS; }
bool isis_ls(Line l, Line s) {
return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}
bool isis_ss(Line s, Line t) {
return (ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}
bool isis_lp(Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < EPS); }
bool isis_sp(Line s, Point p) {
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a)) < EPS;
}
Point projection(Line l, Point p) {
Point base = l.b - l.a;
ld r = dot(p - l.a, base) / norm(base);
return l.a + base * r;
}
Point mirror(Line l, Point p) { return Point(2, 0) * projection(l, p) - p; }
ld dist_lp(Line l, Point p) { return abs(p - projection(l, p)); }
ld dist_ll(Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); }
ld dist_ls(Line l, Line s) {
if (isis_ls(l, s))
return 0;
return min(dist_lp(l, s.a), dist_lp(l, s.b));
}
ld dist_sp(Line s, Point p) {
Point r = projection(s, p);
if (isis_sp(s, r))
return abs(r - p);
return min(abs(s.a - p), abs(s.b - p));
}
ld dist_ss(Line s, Line t) {
if (isis_ss(s, t))
return 0;
return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
min(dist_sp(t, s.a), dist_sp(t, s.b)));
}
Point is_ll(Line s, Line t) {
ld a = cross(s.b - s.a, t.b - t.a);
ld b = cross(s.b - s.a, s.b - t.a);
return t.a + b / a * (t.b - t.a);
}
vector<Point> is_cc(Circle c1, Circle c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + pow(c1.r, 2) - pow(c2.r, 2)) / (2 * d);
ld dfr = pow(c1.r, 2) - rc * rc;
if (abs(dfr) < EPS)
dfr = 0;
if (dfr < 0.0)
return res;
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0)
res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
int isis_cc(Circle c1, Circle c2) {
ld d = abs(c1.p - c2.p);
if (d - c1.r - c2.r < -EPS)
return -2; // separate
if (abs(d - c1.r - c2.r) < EPS)
return -1; // circumscribe
if (c1.r < c2.r)
swap(c1, c2);
if (abs(d - c1.r + c2.r) < EPS)
return 1; // inscribe
if (c1.r - d - c2.r > EPS)
return 2; // involve
else
return 0; // intersect
}
bool isis_cl(Circle c, Line l) {
ld d = dist_lp(l, c.p);
return d - c.r < -EPS;
}
vector<Point> is_cl(Circle c, Line l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d > c.r + EPS)
return res;
ld len = (d > c.r) ? 0 : sqrt(c.r * c.r - d * d);
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(projection(l, c.p) + len * nor);
if (abs(len) > EPS)
res.push_back(projection(l, c.p - len * nor));
return res;
}
vector<Point> is_cs(Circle c, Line s) {
vector<Point> v = is_cl(c, s), res;
REP(i, v.size()) {
if (ccw(s.a, v[i], s.b) == -2)
res.push_back(v[i]);
}
return res;
}
vector<Line> tangent_cp(Circle c, Point p) {
vector<Line> res;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) {
return res;
}
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
res.push_back(Line(p, p + v1));
if (l < EPS)
return res;
res.push_back(Line(p, p + v2));
return res;
}
vector<Line> tangent_cc(Circle c1, Circle c2) {
vector<Line> res;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -EPS) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
res = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > EPS) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nres = tangent_cp(c1, out);
res.insert(res.end(), nres.begin(), nres.end());
} else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
res.push_back(Line(q1, q1 + v));
res.push_back(Line(q2, q2 + v));
}
return res;
}
bool is_in(Circle c, Point p) {
ld dist = abs(c.p - p);
return c.r - dist > -EPS;
}
typedef pair<Point, ld> Data;
int main() {
cin.sync_with_stdio(false);
int n;
while (cin >> n, n) {
vector<Data> v;
REP(i, n) {
ld x, y, l;
cin >> x >> y >> l;
v.emplace_back(Point(x, y), l);
}
ld lb = 0, ub = INF;
REP(loop, 200) {
ld height = (lb + ub) / 2.0;
vector<Circle> vc;
REP(i, n) {
ld r = sqrt((v[i].second * v[i].second) - height * height);
vc.emplace_back(v[i].first, r);
}
vector<Point> ps;
REP(i, n) ps.emplace_back(vc[i].p);
REP(i, n) REP(j, i) {
if (isis_cc(vc[i], vc[j])) {
vector<Point> tmp = is_cc(vc[i], vc[j]);
for (auto k : tmp)
ps.push_back(k);
}
}
bool f = false;
REP(i, ps.size()) {
bool ff = true;
REP(j, vc.size()) {
if (!is_in(vc[j], ps[i]))
ff = false;
}
if (ff)
f = true;
}
if (f)
lb = height;
else
ub = height;
}
cout << D10 << lb << endl;
}
return 0;
} | replace | 233 | 234 | 233 | 234 | TLE | |
p00772 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
int dp[25][25][26][26];
int main() {
while (1) {
int n, m, r;
cin >> n >> m >> r;
if (!n)
break;
deque<int> a(n), b(m);
REP(i, n) cin >> a[i];
REP(i, m) cin >> b[i];
multimap<pair<int, int>, int> rules;
int count = 31;
REP(i, r) {
int k;
cin >> k;
vector<int> x(k);
REP(j, k) cin >> x[j];
int y;
cin >> y;
if (k >= 3) {
rules.emplace(make_pair(x[0], x[1]), count);
++count;
REP(j, k - 3) {
rules.emplace(make_pair(count - 1, x[j + 2]), count);
++count;
}
rules.emplace(make_pair(count - 1, x.back()), y);
++count;
} else {
rules.emplace(make_pair(x[0], x[1]), y);
}
}
using S = vector<int>;
S as, bs;
vector<vector<S>> cyka(n, vector<S>(n + 1));
REP(j, n) { cyka[j][1].push_back(a[j]); }
FOR(l, 2, n + 1) REP(j, n) {
FOR(k, 1, l) {
for (const auto &sl : cyka[j][k]) {
for (const auto &sr : cyka[(j + k) % n][l - k]) {
auto pitr = rules.equal_range(make_pair(sl, sr));
for (auto itr = pitr.first; itr != pitr.second; ++itr) {
cyka[j][l].push_back(itr->second);
}
}
}
}
sort(ALL(cyka[j][l]));
cyka[j][l].erase(unique(ALL(cyka[j][l])), end(cyka[j][l]));
}
vector<vector<S>> cykb(n, vector<S>(n + 1));
REP(j, m) { cykb[j][1].push_back(b[j]); }
FOR(l, 2, m + 1) REP(j, m) {
FOR(k, 1, l) {
for (const auto &sl : cykb[j][k]) {
for (const auto &sr : cykb[(j + k) % m][l - k]) {
auto pitr = rules.equal_range(make_pair(sl, sr));
for (auto itr = pitr.first; itr != pitr.second; ++itr) {
cykb[j][l].push_back(itr->second);
}
}
}
}
sort(ALL(cykb[j][l]));
cykb[j][l].erase(unique(ALL(cykb[j][l])), end(cykb[j][l]));
}
FOR(la, 1, n + 1) FOR(lb, 1, m + 1) REP(i, n) REP(j, m) {
vector<int> its;
set_intersection(ALL(cyka[i][la]), ALL(cykb[j][lb]), back_inserter(its));
if (its.empty()) {
dp[i][j][la][lb] = 0;
} else {
dp[i][j][la][lb] = 1;
}
}
FOR(la, 2, n + 1) FOR(lb, 2, m + 1) REP(i, n) REP(j, m) {
FOR(ka, 1, la) FOR(kb, 1, lb) {
if (dp[i][j][ka][kb] &&
dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]) {
dp[i][j][la][lb] =
max(dp[i][j][la][lb],
dp[i][j][ka][kb] +
dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]);
}
}
}
int mx = 0;
REP(i, n) REP(j, m) mx = max(mx, dp[i][j][n][m]);
if (mx) {
cout << mx << endl;
} else {
cout << -1 << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
int dp[25][25][26][26];
int main() {
while (1) {
int n, m, r;
cin >> n >> m >> r;
if (!n)
break;
deque<int> a(n), b(m);
REP(i, n) cin >> a[i];
REP(i, m) cin >> b[i];
multimap<pair<int, int>, int> rules;
int count = 31;
REP(i, r) {
int k;
cin >> k;
vector<int> x(k);
REP(j, k) cin >> x[j];
int y;
cin >> y;
if (k >= 3) {
rules.emplace(make_pair(x[0], x[1]), count);
++count;
REP(j, k - 3) {
rules.emplace(make_pair(count - 1, x[j + 2]), count);
++count;
}
rules.emplace(make_pair(count - 1, x.back()), y);
++count;
} else {
rules.emplace(make_pair(x[0], x[1]), y);
}
}
using S = vector<int>;
S as, bs;
vector<vector<S>> cyka(n, vector<S>(n + 1));
REP(j, n) { cyka[j][1].push_back(a[j]); }
FOR(l, 2, n + 1) REP(j, n) {
FOR(k, 1, l) {
for (const auto &sl : cyka[j][k]) {
for (const auto &sr : cyka[(j + k) % n][l - k]) {
auto pitr = rules.equal_range(make_pair(sl, sr));
for (auto itr = pitr.first; itr != pitr.second; ++itr) {
cyka[j][l].push_back(itr->second);
}
}
}
}
sort(ALL(cyka[j][l]));
cyka[j][l].erase(unique(ALL(cyka[j][l])), end(cyka[j][l]));
}
vector<vector<S>> cykb(m, vector<S>(m + 1));
REP(j, m) { cykb[j][1].push_back(b[j]); }
FOR(l, 2, m + 1) REP(j, m) {
FOR(k, 1, l) {
for (const auto &sl : cykb[j][k]) {
for (const auto &sr : cykb[(j + k) % m][l - k]) {
auto pitr = rules.equal_range(make_pair(sl, sr));
for (auto itr = pitr.first; itr != pitr.second; ++itr) {
cykb[j][l].push_back(itr->second);
}
}
}
}
sort(ALL(cykb[j][l]));
cykb[j][l].erase(unique(ALL(cykb[j][l])), end(cykb[j][l]));
}
FOR(la, 1, n + 1) FOR(lb, 1, m + 1) REP(i, n) REP(j, m) {
vector<int> its;
set_intersection(ALL(cyka[i][la]), ALL(cykb[j][lb]), back_inserter(its));
if (its.empty()) {
dp[i][j][la][lb] = 0;
} else {
dp[i][j][la][lb] = 1;
}
}
FOR(la, 2, n + 1) FOR(lb, 2, m + 1) REP(i, n) REP(j, m) {
FOR(ka, 1, la) FOR(kb, 1, lb) {
if (dp[i][j][ka][kb] &&
dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]) {
dp[i][j][la][lb] =
max(dp[i][j][la][lb],
dp[i][j][ka][kb] +
dp[(i + ka) % n][(j + kb) % m][la - ka][lb - kb]);
}
}
}
int mx = 0;
REP(i, n) REP(j, m) mx = max(mx, dp[i][j][n][m]);
if (mx) {
cout << mx << endl;
} else {
cout << -1 << endl;
}
}
return 0;
} | replace | 58 | 59 | 58 | 59 | 0 | |
p00773 | C++ | Runtime Error | #include <stdio.h>
int max(int m1, int m2) { return m1 < m2 ? m2 : m1; }
int main() {
int t1, t2, sum, a, i, j;
for (; scanf("%d %d %d", &t1, &t2, &sum), sum;) {
static int A[1001];
for (j = 0; j < 1001; j++)
A[j] = 0;
int m = 0;
for (i = 1; i < sum; i++) {
a = i * (100 + t1) / 100;
A[a] = i;
}
for (i = 1; i < sum; i++) {
if (A[i] == 0 && A[sum - i] == 0)
continue;
m = max(A[i] * (100 + t2) / 100 + A[sum - i] * (100 + t2) / 100, m);
}
printf("%d\n", m);
}
return 0;
} | #include <stdio.h>
int max(int m1, int m2) { return m1 < m2 ? m2 : m1; }
int main() {
int t1, t2, sum, a, i, j;
for (; scanf("%d %d %d", &t1, &t2, &sum), sum;) {
static int A[10000];
for (j = 0; j < 1001; j++)
A[j] = 0;
int m = 0;
for (i = 1; i < sum; i++) {
a = i * (100 + t1) / 100;
A[a] = i;
}
for (i = 1; i < sum; i++) {
if (A[i] == 0 && A[sum - i] == 0)
continue;
m = max(A[i] * (100 + t2) / 100 + A[sum - i] * (100 + t2) / 100, m);
}
printf("%d\n", m);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | -11 | |
p00773 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int a[100];
int i, j, k, n = 0, t;
int m1, m2;
int mm1, mm2;
int temp;
while (1) {
int x, y, s; // x???????¨????,y??´??°???????¨????,z??????????¨?
cin >> x >> y >> s;
if (x == 0 && y == 0 && s == 0)
break;
int max = 0;
for (m1 = 1; m1 < s; m1++) {
for (m2 = 1; m2 < s; m2++) {
if (m1 >= m2) {
// mm1 = (m1 * (100 + x)) / 100;
// mm2 = s - mm1;
// m2 = (100 * (mm2 + 0.5)) / (100 + x);
temp = 0;
if ((((m1 * (100 + x)) / 100) + ((m2 * (100 + x)) / 100)) == s) {
// cout << m1 << ' ' << m2 << endl;
temp = (m1 * (100 + y)) / 100;
temp += (m2 * (100 + y)) / 100;
if (temp > max) {
max = temp;
// cout << max << endl;
}
}
}
}
}
// cout << max << endl;
a[n++] = max;
}
for (i = 0; i < n; i++)
cout << a[i] << endl;
return (0);
} | #include <iostream>
using namespace std;
int main() {
int a[1000];
int i, j, k, n = 0, t;
int m1, m2;
int mm1, mm2;
int temp;
while (1) {
int x, y, s; // x???????¨????,y??´??°???????¨????,z??????????¨?
cin >> x >> y >> s;
if (x == 0 && y == 0 && s == 0)
break;
int max = 0;
for (m1 = 1; m1 < s; m1++) {
for (m2 = 1; m2 < s; m2++) {
if (m1 >= m2) {
// mm1 = (m1 * (100 + x)) / 100;
// mm2 = s - mm1;
// m2 = (100 * (mm2 + 0.5)) / (100 + x);
temp = 0;
if ((((m1 * (100 + x)) / 100) + ((m2 * (100 + x)) / 100)) == s) {
// cout << m1 << ' ' << m2 << endl;
temp = (m1 * (100 + y)) / 100;
temp += (m2 * (100 + y)) / 100;
if (temp > max) {
max = temp;
// cout << max << endl;
}
}
}
}
}
// cout << max << endl;
a[n++] = max;
}
for (i = 0; i < n; i++)
cout << a[i] << endl;
return (0);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00773 | C++ | Runtime Error | // 1192_TaxRateChanged.cpp
// 税率がx%の価格でループを回すのではなく、税抜き価格でループを回すのがポイント
#include <bits/stdc++.h>
using namespace std;
const int MAX_S = 1010;
int x, y, s;
int p[MAX_S / 2];
void solve() {
memset(p, 0, sizeof(p));
for (int i = 1; i < s; ++i)
for (int j = 1; j < s; ++j)
if (i * (100 + x) / 100 + j * (100 + x) / 100 == s)
p[i] = i * (100 + y) / 100 + j * (100 + y) / 100;
cout << *max_element(p, p + s) << endl;
}
int main() {
while (cin >> x >> y >> s) {
if (x == 0 && y == 0 && s == 0)
break;
solve();
}
return 0;
}
| // 1192_TaxRateChanged.cpp
// 税率がx%の価格でループを回すのではなく、税抜き価格でループを回すのがポイント
#include <bits/stdc++.h>
using namespace std;
const int MAX_S = 1010;
int x, y, s;
int p[MAX_S];
void solve() {
memset(p, 0, sizeof(p));
for (int i = 1; i < s; ++i)
for (int j = 1; j < s; ++j)
if (i * (100 + x) / 100 + j * (100 + x) / 100 == s)
p[i] = i * (100 + y) / 100 + j * (100 + y) / 100;
cout << *max_element(p, p + s) << endl;
}
int main() {
while (cin >> x >> y >> s) {
if (x == 0 && y == 0 && s == 0)
break;
solve();
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | -11 | |
p00773 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;
//////
int intax(int i, int p) { return floor((double)i * (100 + p) / 100); }
int main() {
int x, y, s;
while (cin >> x >> y >> s, x && y && s) {
int res = -1;
int mi, mj = 0;
for (int i = 1; i < s; i++) {
int xi = intax(i, x);
if (xi > s)
break;
int j = 1;
while (intax(j, x) + xi < s)
j++;
while (true) {
int xj = intax(j, x);
if (xi + xj > s)
break;
int yi = intax(i, y);
int yj = intax(j, y);
if (res < yi + yj) {
mi = i, mj = j;
}
// cerr << i << " " << j << " " << yi + yj << endl;
res = max(yi + yj, res);
j++;
}
}
cerr << mi << " " << mj << endl;
cout << res << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;
//////
int intax(int i, int p) { return floor((double)i * (100 + p) / 100); }
int main() {
int x, y, s;
while (cin >> x >> y >> s, x && y && s) {
int res = -1;
int mi, mj = 0;
for (int i = 1; i < s; i++) {
int xi = intax(i, x);
if (xi > s)
break;
int j = 1;
while (intax(j, x) + xi < s)
j++;
while (true) {
int xj = intax(j, x);
if (xi + xj > s)
break;
int yi = intax(i, y);
int yj = intax(j, y);
if (res < yi + yj) {
mi = i, mj = j;
}
// cerr << i << " " << j << " " << yi + yj << endl;
res = max(yi + yj, res);
j++;
}
}
// cerr << mi << " " << mj << endl;
cout << res << endl;
}
}
| replace | 36 | 37 | 36 | 37 | 0 | 13 88
12 87
1 23
1 12
1 23
1 22
1 199
25 75
25 25
11 50
50 75
92 899
1 502
5 500
1 10
1 6
|
p00774 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int H;
int b[10][5];
struct range {
int row;
int left;
int right;
};
void drop(int row, int col) {
for (int i = row; i > 0; i--) {
b[i][col] = b[i - 1][col];
}
b[0][col] = -1;
}
void print_board() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cerr << b[i][j] << ' ';
}
cerr << endl;
}
cerr << endl;
}
void solve() {
int ans = 0;
while (1) {
print_board();
vector<range> deleted;
for (int i = 0; i < H; i++) {
int left = 0;
for (int j = 1; j < 6; j++) {
if (j == 5 || b[i][j] != b[i][j - 1]) {
if (j >= left + 3) {
if (b[i][j - 1] != -1) {
range tmp = {i, left, j - 1};
deleted.push_back(tmp);
ans += b[i][j - 1] * (j - left);
}
break;
} else {
left = j;
}
}
}
}
if (deleted.size() == 0) {
break;
}
for (int k = 0; k < deleted.size(); k++) {
range d = deleted[k];
for (int j = d.left; j <= d.right; j++) {
drop(d.row, j);
}
}
}
cout << ans << endl;
}
int main() {
while (1) {
cin >> H;
if (H == 0) {
break;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cin >> b[i][j];
}
}
solve();
}
}
| #include <iostream>
#include <vector>
using namespace std;
int H;
int b[10][5];
struct range {
int row;
int left;
int right;
};
void drop(int row, int col) {
for (int i = row; i > 0; i--) {
b[i][col] = b[i - 1][col];
}
b[0][col] = -1;
}
void print_board() {
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cerr << b[i][j] << ' ';
}
cerr << endl;
}
cerr << endl;
}
void solve() {
int ans = 0;
while (1) {
/* print_board(); */
vector<range> deleted;
for (int i = 0; i < H; i++) {
int left = 0;
for (int j = 1; j < 6; j++) {
if (j == 5 || b[i][j] != b[i][j - 1]) {
if (j >= left + 3) {
if (b[i][j - 1] != -1) {
range tmp = {i, left, j - 1};
deleted.push_back(tmp);
ans += b[i][j - 1] * (j - left);
}
break;
} else {
left = j;
}
}
}
}
if (deleted.size() == 0) {
break;
}
for (int k = 0; k < deleted.size(); k++) {
range d = deleted[k];
for (int j = d.left; j <= d.right; j++) {
drop(d.row, j);
}
}
}
cout << ans << endl;
}
int main() {
while (1) {
cin >> H;
if (H == 0) {
break;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cin >> b[i][j];
}
}
solve();
}
}
| replace | 33 | 34 | 33 | 34 | 0 | 6 9 9 9 9
6 -1 -1 -1 -1
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
5 9 -1 -1 -1
5 5 5 5 9
4 6 6 9 9
3 3 3 6 9
2 2 2 9 9
-1 -1 -1 -1 -1
-1 -1 -1 -1 9
-1 -1 -1 9 9
5 9 -1 6 9
4 6 6 9 9
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
-1 -1 -1 -1 -1
-1 -1 -1 5 6
3 5 -1 8 3
6 2 6 9 2
4 6 5 6 1
8 9 6 4 9
5 6 1 1 8
6 8 1 8 1
9 6 2 1 2
5 3 3 3 5
-1 -1 -1 -1 -1
-1 -1 -1 -1 6
3 -1 -1 5 3
6 5 -1 8 2
4 2 6 9 1
8 6 5 6 9
5 9 6 4 8
6 6 1 1 1
9 8 1 8 2
5 6 2 1 5
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
3 -1 -1 -1 6
6 5 -1 5 3
4 2 -1 8 2
8 6 6 9 1
5 9 5 6 9
6 6 6 4 8
9 8 1 8 2
5 6 2 1 5
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 6
3 -1 -1 5 3
6 5 -1 8 2
4 2 -1 9 1
8 6 6 6 9
5 9 5 4 8
9 8 1 8 2
5 6 2 1 5
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 6
3 -1 -1 -1 3
6 -1 -1 5 2
4 5 -1 8 1
8 2 -1 9 9
5 9 5 4 8
9 8 1 8 2
5 6 2 1 5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
2 2 -1 -1 -1
6 5 -1 -1 -1
8 8 8 7 4
-1 -1 -1 -1 -1
2 2 -1 -1 -1
6 5 -1 7 4
|
p00774 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
int tile[11][5];
int main() {
int h;
while (cin >> h, h) {
REP(i, 11) REP(j, 5) { tile[i][j] = -1; }
REP(i, h) REP(j, 5) { cin >> tile[h - 1 - i][j]; }
int ans = 0;
bool update = false;
while (true) {
update = false;
REP(it, 11) {
bool t1 = tile[it][0] == tile[it][1] && tile[it][0] != -1;
bool t2 = tile[it][1] == tile[it][2] && tile[it][1] != -1;
bool t3 = tile[it][2] == tile[it][3] && tile[it][2] != -1;
bool t4 = tile[it][3] == tile[it][4] && tile[it][3] != -1;
bool y1 = t1 && t2;
bool y2 = t2 && t3;
bool y3 = t3 && t4;
if (y1 && y2 && y3) {
ans += tile[it][0] * 5;
REP(i, 5) { tile[it][i] = -1; }
update = true;
} else if (y1 && y2 || y2 && y3) {
ans += tile[it][y3] * 4;
REP(i, 4) { tile[it][i + y3] = -1; }
update = true;
} else if (y1 || y2 || y3) {
ans += tile[it][2] * 3;
REP(i, 3) { tile[it][i + y2 + ((int)y3) * 2] = -1; }
update = true;
}
}
REP(it, 11) {
REP(i, 5) {
if (tile[it][i] == -1) {
int itt = it;
while (tile[++itt][i] == -1 && itt != 10)
;
swap(tile[it][i], tile[itt][i]);
}
}
}
if (!update)
break;
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
int tile[11][5];
int main() {
int h;
while (cin >> h, h) {
REP(i, 11) REP(j, 5) { tile[i][j] = -1; }
REP(i, h) REP(j, 5) { cin >> tile[h - 1 - i][j]; }
int ans = 0;
bool update = false;
while (true) {
update = false;
REP(it, 11) {
bool t1 = tile[it][0] == tile[it][1] && tile[it][0] != -1;
bool t2 = tile[it][1] == tile[it][2] && tile[it][1] != -1;
bool t3 = tile[it][2] == tile[it][3] && tile[it][2] != -1;
bool t4 = tile[it][3] == tile[it][4] && tile[it][3] != -1;
bool y1 = t1 && t2;
bool y2 = t2 && t3;
bool y3 = t3 && t4;
if (y1 && y2 && y3) {
ans += tile[it][0] * 5;
REP(i, 5) { tile[it][i] = -1; }
update = true;
} else if (y1 && y2 || y2 && y3) {
ans += tile[it][y3] * 4;
REP(i, 4) { tile[it][i + y3] = -1; }
update = true;
} else if (y1 || y2 || y3) {
ans += tile[it][2] * 3;
REP(i, 3) { tile[it][i + y2 + ((int)y3) * 2] = -1; }
update = true;
}
}
REP(it, 10) {
REP(i, 5) {
if (tile[it][i] == -1) {
int itt = it;
while (tile[++itt][i] == -1 && itt != 10)
;
swap(tile[it][i], tile[itt][i]);
}
}
}
if (!update)
break;
}
cout << ans << endl;
}
} | replace | 39 | 40 | 39 | 40 | -11 | |
p00774 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define Line vector<char>
#define Puzzle vector<Line>
void newline() { cout << endl; }
template <typename T> void put(T input) { cout << input; }
template <typename T> void putl(T input) {
put(input);
newline();
}
int n;
int point;
Puzzle puzzle;
void swap_rec(int column, int row) {
if (0 < column) {
swap(puzzle[column][row], puzzle[column - 1][row]);
swap_rec(column - 1, row);
}
}
void drop() {
Puzzle prev_puzzle = puzzle;
for (int column = puzzle.size() - 1; 0 <= column; column--) {
for (int row = puzzle[column].size() - 1; 0 <= row; row--) {
if (puzzle[column][row] == '#')
swap_rec(column, row);
}
}
if (puzzle != prev_puzzle)
drop();
}
int clear(Line &line, int n, char c) {
if (line[n] == c) {
line[n] = '#';
if (1 <= n && n < line.size() - 1) {
return int(c) - int('0') + clear(line, n + 1, c) + clear(line, n - 1, c);
} else {
return int(c) - int('0');
}
} else {
return 0;
}
}
int solve() {
Puzzle prev_puzzle = puzzle;
for (Line &line : puzzle)
for (int k = 1; k < line.size() - 1; k++)
if (line[k] != '#' && line[k - 1] == line[k] && line[k] == line[k + 1])
point += clear(line, k, line[k]);
drop();
if (prev_puzzle != puzzle)
solve();
return point;
}
int main() {
while (cin >> n && n) {
point = 0;
puzzle = {};
puzzle.reserve(n);
for (int ic = 0; ic < n; ic++) {
Line line;
line.resize(5);
for (int lc = 0; lc < 5; lc++) {
char s;
cin >> s;
line[lc] = s;
}
puzzle.push_back(line);
}
putl(solve());
}
} | #include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using Line = vector<char>;
using Puzzle = vector<Line>;
void newline() { cout << endl; }
template <typename T> void put(T input) { cout << input; }
template <typename T> void putl(T input) {
put(input);
newline();
}
int n;
int point;
Puzzle puzzle;
void swap_rec(int column, int row) {
if (0 < column) {
swap(puzzle[column][row], puzzle[column - 1][row]);
swap_rec(column - 1, row);
}
}
void drop() {
Puzzle prev_puzzle = puzzle;
for (int column = puzzle.size() - 1; 0 <= column; column--) {
for (int row = puzzle[column].size() - 1; 0 <= row; row--) {
if (puzzle[column][row] == '#')
swap_rec(column, row);
}
}
if (puzzle != prev_puzzle)
drop();
}
int clear(Line &line, int n, char c) {
if (line[n] == c) {
line[n] = '#';
if (1 <= n && n < line.size() - 1) {
return int(c) - int('0') + clear(line, n + 1, c) + clear(line, n - 1, c);
} else {
return int(c) - int('0');
}
} else {
return 0;
}
}
int solve() {
Puzzle prev_puzzle = puzzle;
for (Line &line : puzzle)
for (int k = 1; k < line.size() - 1; k++)
if (line[k] != '#' && line[k - 1] == line[k] && line[k] == line[k + 1])
point += clear(line, k, line[k]);
drop();
if (prev_puzzle != puzzle)
solve();
return point;
}
int main() {
while (cin >> n && n) {
point = 0;
puzzle = {};
puzzle.reserve(n);
for (int ic = 0; ic < n; ic++) {
Line line;
line.resize(5);
for (int lc = 0; lc < 5; lc++) {
char s;
cin >> s;
line[lc] = s;
}
puzzle.push_back(line);
}
putl(solve());
}
} | replace | 5 | 7 | 5 | 7 | 0 | |
p00774 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int main() {
int H;
while (true) {
scanf("%d", &H);
if (H == 0) {
break;
}
vector<vector<int>> M(H, vector<int>(5));
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
scanf("%d", &M[i][j]);
}
}
int ret = 0;
while (true) {
for (int i = 0; i < H; i++) {
for (int j = 5; j >= 3; j--) {
for (int k = 0; k <= 5 - j; k++) {
bool ok = (M[i][k] != 0);
for (int l = k + 1; l < k + j; l++) {
if (M[i][l - 1] != M[i][l] || M[i][l] == 0) {
ok = false;
}
}
if (ok) {
ret += M[i][k] * j;
for (int l = k; l < k + j; l++) {
M[i][l] = 0;
}
}
}
}
}
bool ok = false;
for (int i = H - 1; i >= 1; i--) {
for (int j = 0; j < 5; j++) {
if (M[i][j] == 0) {
M[i][j] = M[i - 1][j];
M[i - 1][j] = 0;
ok = true;
}
}
}
if (!ok) {
break;
}
}
printf("%d\n", ret);
}
return 0;
} | #include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int main() {
int H;
while (true) {
scanf("%d", &H);
if (H == 0) {
break;
}
vector<vector<int>> M(H, vector<int>(5));
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
scanf("%d", &M[i][j]);
}
}
int ret = 0;
while (true) {
for (int i = 0; i < H; i++) {
for (int j = 5; j >= 3; j--) {
for (int k = 0; k <= 5 - j; k++) {
bool ok = (M[i][k] != 0);
for (int l = k + 1; l < k + j; l++) {
if (M[i][l - 1] != M[i][l] || M[i][l] == 0) {
ok = false;
}
}
if (ok) {
ret += M[i][k] * j;
for (int l = k; l < k + j; l++) {
M[i][l] = 0;
}
}
}
}
}
bool ok = false;
for (int rep = 0; rep < H; rep++) {
for (int i = H - 1; i >= 1; i--) {
for (int j = 0; j < 5; j++) {
if (M[i][j] == 0 && M[i - 1][j] != 0) {
M[i][j] = M[i - 1][j];
M[i - 1][j] = 0;
ok = true;
}
}
}
}
if (!ok) {
break;
}
}
printf("%d\n", ret);
}
return 0;
} | replace | 52 | 58 | 52 | 60 | TLE | |
p00774 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int x[2000][5], H;
int solve() {
int score = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < H; j++) {
if (x[j][0] == x[j][1] && x[j][1] == x[j][2] && x[j][2] == x[j][3] &&
x[j][3] == x[j][4] && x[j][0] >= 1) {
score += x[j][0] * 5;
x[j][0] = 0;
x[j][1] = 0;
x[j][2] = 0;
x[j][3] = 0;
x[j][4] = 0;
}
}
for (int j = 0; j < H; j++) {
for (int k = 0; k < 2; k++) {
if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] &&
x[j][k + 2] == x[j][k + 3] && x[j][k + 0] >= 1) {
score += x[j][k + 0] * 4;
x[j][k + 0] = 0;
x[j][k + 1] = 0;
x[j][k + 2] = 0;
x[j][k + 3] = 0;
}
}
}
for (int j = 0; j < H; j++) {
for (int k = 0; k < 3; k++) {
if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] &&
x[j][k + 0] >= 1) {
score += x[j][k + 0] * 3;
x[j][k + 0] = 0;
x[j][k + 1] = 0;
x[j][k + 2] = 0;
}
}
}
for (int j = 0; j < 500; j++) {
for (int j = 1; j < H; j++) {
for (int k = 0; k < 5; k++) {
if (x[j][k] == 0) {
x[j][k] = x[j - 1][k];
x[j - 1][k] = 0;
}
}
}
}
}
return score;
}
int main() {
while (true) {
cin >> H;
if (H == 0) {
break;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cin >> x[i][j];
}
}
cout << solve() << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int x[2000][5], H;
int solve() {
int score = 0;
for (int i = 0; i < 500; i++) {
for (int j = 0; j < H; j++) {
if (x[j][0] == x[j][1] && x[j][1] == x[j][2] && x[j][2] == x[j][3] &&
x[j][3] == x[j][4] && x[j][0] >= 1) {
score += x[j][0] * 5;
x[j][0] = 0;
x[j][1] = 0;
x[j][2] = 0;
x[j][3] = 0;
x[j][4] = 0;
}
}
for (int j = 0; j < H; j++) {
for (int k = 0; k < 2; k++) {
if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] &&
x[j][k + 2] == x[j][k + 3] && x[j][k + 0] >= 1) {
score += x[j][k + 0] * 4;
x[j][k + 0] = 0;
x[j][k + 1] = 0;
x[j][k + 2] = 0;
x[j][k + 3] = 0;
}
}
}
for (int j = 0; j < H; j++) {
for (int k = 0; k < 3; k++) {
if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] &&
x[j][k + 0] >= 1) {
score += x[j][k + 0] * 3;
x[j][k + 0] = 0;
x[j][k + 1] = 0;
x[j][k + 2] = 0;
}
}
}
for (int j = 0; j < 500; j++) {
for (int j = 1; j < H; j++) {
for (int k = 0; k < 5; k++) {
if (x[j][k] == 0) {
x[j][k] = x[j - 1][k];
x[j - 1][k] = 0;
}
}
}
}
}
return score;
}
int main() {
while (true) {
cin >> H;
if (H == 0) {
break;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < 5; j++) {
cin >> x[i][j];
}
}
cout << solve() << endl;
}
return 0;
} | replace | 7 | 8 | 7 | 8 | TLE | |
p00774 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
void fill_stone(vector<int> stone[5]) {
for (int i = 0; i < 5; ++i) {
vector<int>::iterator it = stone[i].begin();
while (it != stone[i].end()) {
if (*it == 0) {
stone[i].erase(it);
it = stone[i].begin();
} else
++it;
}
}
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 10 - stone[i].size(); ++j) {
stone[i].push_back(0);
}
}
}
int erase_stone(vector<int> stone[5]) {
int score = 0;
for (int i = 0; i < 10; ++i) {
int chain = 1;
int before = -1;
for (int j = 0; j < 5; ++j) {
if (stone[j][i] == before)
++chain;
else {
if (chain >= 3) {
score += before * chain;
for (int k = j - 1; k > j - chain - 1; --k) {
stone[k][i] = 0;
}
}
before = stone[j][i];
chain = 1;
}
}
if (chain >= 3) {
score += before * chain;
for (int k = 4; k > 4 - chain; --k) {
stone[k][i] = 0;
}
}
}
fill_stone(stone);
return score;
}
int main() {
while (true) {
int h;
vector<int> stone[5];
int cnt = 0;
int score = 0;
cin >> h;
for (int i = h - 1; i >= 0; --i) {
for (int j = 0; j < 5; ++j) {
int k;
cin >> k;
stone[j].insert(stone[j].begin(), k);
}
}
for (int i = h; i < 10; ++i) {
for (int j = 0; j < 5; ++j) {
stone[j].push_back(0);
}
}
while (true) {
score = erase_stone(stone);
if (score > 0)
cnt += score;
else
break;
}
cout << cnt << endl;
}
} | #include <iostream>
#include <vector>
using namespace std;
void fill_stone(vector<int> stone[5]) {
for (int i = 0; i < 5; ++i) {
vector<int>::iterator it = stone[i].begin();
while (it != stone[i].end()) {
if (*it == 0) {
stone[i].erase(it);
it = stone[i].begin();
} else
++it;
}
}
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 10 - stone[i].size(); ++j) {
stone[i].push_back(0);
}
}
}
int erase_stone(vector<int> stone[5]) {
int score = 0;
for (int i = 0; i < 10; ++i) {
int chain = 1;
int before = -1;
for (int j = 0; j < 5; ++j) {
if (stone[j][i] == before)
++chain;
else {
if (chain >= 3) {
score += before * chain;
for (int k = j - 1; k > j - chain - 1; --k) {
stone[k][i] = 0;
}
}
before = stone[j][i];
chain = 1;
}
}
if (chain >= 3) {
score += before * chain;
for (int k = 4; k > 4 - chain; --k) {
stone[k][i] = 0;
}
}
}
fill_stone(stone);
return score;
}
int main() {
while (true) {
int h;
vector<int> stone[5];
int cnt = 0;
int score = 0;
cin >> h;
if (!h)
break;
for (int i = h - 1; i >= 0; --i) {
for (int j = 0; j < 5; ++j) {
int k;
cin >> k;
stone[j].insert(stone[j].begin(), k);
}
}
for (int i = h; i < 10; ++i) {
for (int j = 0; j < 5; ++j) {
stone[j].push_back(0);
}
}
while (true) {
score = erase_stone(stone);
if (score > 0)
cnt += score;
else
break;
}
cout << cnt << endl;
}
} | insert | 58 | 58 | 58 | 60 | TLE | |
p00775 | C++ | Time Limit Exceeded | // #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int xl[25], xr[25], h[25];
signed main(void) {
while (true) {
int r, n;
cin >> r >> n;
if (!r && !n)
break;
REP(i, n) cin >> xl[i] >> xr[i] >> h[i];
double hi = 60, low = 0;
REP(i, 20) {
double mid = (hi + low) / 2;
// cout << hi << " " << mid << " " << low << endl;
bool flag = true;
REP(j, 300000) {
double x = -r + (double)(2 + r) * j / 300000;
int ma = 0;
REP(k, n) {
if (xl[k] <= x && x <= xr[k]) {
ma = max(ma, h[k]);
}
}
double H = sqrt(r * r - x * x) + mid - r;
// cout << ma << " " << H << endls
if (ma < H) {
flag = false;
break;
}
}
if (flag) {
low = mid;
} else {
hi = mid;
}
}
cout << fixed << setprecision(10) << low << endl;
}
return 0;
} | // #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int xl[25], xr[25], h[25];
signed main(void) {
while (true) {
int r, n;
cin >> r >> n;
if (!r && !n)
break;
REP(i, n) cin >> xl[i] >> xr[i] >> h[i];
double hi = 60, low = 0;
REP(i, 20) {
double mid = (hi + low) / 2;
// cout << hi << " " << mid << " " << low << endl;
bool flag = true;
REP(j, 200000) {
double x = -r + (double)(2 * r) * j / 200000;
int ma = 0;
REP(k, n) {
if (xl[k] <= x && x <= xr[k]) {
ma = max(ma, h[k]);
}
}
double H = sqrt(r * r - x * x) + mid - r;
// cout << ma << " " << H << endls
if (ma < H) {
flag = false;
break;
}
}
if (flag) {
low = mid;
} else {
hi = mid;
}
}
cout << fixed << setprecision(10) << low << endl;
}
return 0;
} | replace | 40 | 42 | 40 | 42 | TLE | |
p00776 | C++ | Memory Limit Exceeded | // tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i, a, b) for (int i = a; i < b; ++i)
#define for_rev(i, a, b) for (int i = a; i >= b; --i)
#define allof(a) a.begin(), a.end()
#define minit(a, b) memset(a, b, sizeof(a))
#define size_of(a) (int)a.size()
// typedef
typedef long long lint;
typedef double Double;
typedef pair<int, int> pii;
//
typedef vector<int> Array;
typedef vector<Array> Matrix;
typedef vector<Double> DArray;
typedef vector<DArray> DMatrix;
typedef vector<string> SArray;
typedef vector<pii> PArray;
// popcount
inline int POPCNT(int _x) { return __builtin_popcount(_x); }
inline int POPCNT(lint _x) { return __builtin_popcountll(_x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
Double EPS = 1e-9;
// in range
inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; }
inline bool in_range(Double _v, Double _mi, Double _mx) {
return -EPS < _v - _mi && _v - _mx < EPS;
}
inline bool in_range(int _x, int _y, int _W, int _H) {
return 0 <= _x && _x < _W && 0 <= _y && _y < _H;
}
// neighbor clockwise
const int DX[4] = {0, 1, 0, -1}, DY[4] = {-1, 0, 1, 0};
const int DX_[8] = {0, 1, 1, 1, 0, -1, -1, -1},
DY_[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
string S;
int N;
vector<string> ans;
bool use[26];
void rec(int i, string s) {
if (i == N) {
ans.push_back(s);
return;
}
int id = S[i] - 'a';
char nx = S[i] + 1;
if (use[id]) {
rec(i + 1, s + S[i]);
}
if (S[i] != 'z') {
use[id + 1] = true;
rec(i + 1, s + nx);
use[id + 1] = false;
}
}
void solve() {
N = size_of(S);
ans.clear();
minit(use, 0);
use[0] = true;
rec(0, "");
// sort(allof(ans));
int n = size_of(ans);
cout << n << endl;
if (n <= 10) {
for_(i, 0, n) cout << ans[i] << endl;
} else {
for_(i, 0, 5) cout << ans[i] << endl;
for_(i, n - 5, n) cout << ans[i] << endl;
}
}
int main() {
while (cin >> S) {
if (S == "#")
break;
solve();
}
return 0;
} | // tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i, a, b) for (int i = a; i < b; ++i)
#define for_rev(i, a, b) for (int i = a; i >= b; --i)
#define allof(a) a.begin(), a.end()
#define minit(a, b) memset(a, b, sizeof(a))
#define size_of(a) (int)a.size()
// typedef
typedef long long lint;
typedef double Double;
typedef pair<int, int> pii;
//
typedef vector<int> Array;
typedef vector<Array> Matrix;
typedef vector<Double> DArray;
typedef vector<DArray> DMatrix;
typedef vector<string> SArray;
typedef vector<pii> PArray;
// popcount
inline int POPCNT(int _x) { return __builtin_popcount(_x); }
inline int POPCNT(lint _x) { return __builtin_popcountll(_x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
Double EPS = 1e-9;
// in range
inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; }
inline bool in_range(Double _v, Double _mi, Double _mx) {
return -EPS < _v - _mi && _v - _mx < EPS;
}
inline bool in_range(int _x, int _y, int _W, int _H) {
return 0 <= _x && _x < _W && 0 <= _y && _y < _H;
}
// neighbor clockwise
const int DX[4] = {0, 1, 0, -1}, DY[4] = {-1, 0, 1, 0};
const int DX_[8] = {0, 1, 1, 1, 0, -1, -1, -1},
DY_[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
string S;
int N;
vector<string> ans;
bool use[26];
void rec(int i, string s) {
if (i == N) {
ans.push_back(s);
return;
}
int id = S[i] - 'a';
char nx = S[i] + 1;
if (use[id]) {
rec(i + 1, s + S[i]);
}
if (S[i] != 'z' && !use[id + 1]) {
use[id + 1] = true;
rec(i + 1, s + nx);
use[id + 1] = false;
}
}
void solve() {
N = size_of(S);
ans.clear();
minit(use, 0);
use[0] = true;
rec(0, "");
// sort(allof(ans));
int n = size_of(ans);
cout << n << endl;
if (n <= 10) {
for_(i, 0, n) cout << ans[i] << endl;
} else {
for_(i, 0, 5) cout << ans[i] << endl;
for_(i, n - 5, n) cout << ans[i] << endl;
}
}
int main() {
while (cin >> S) {
if (S == "#")
break;
solve();
}
return 0;
} | replace | 60 | 61 | 60 | 61 | MLE | |
p00776 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int main() {
string s;
while (cin >> s, s != "#") {
vector<string> ans;
int n = s.size();
REP(i, 1 << n) {
string tmp = s;
bool f = true;
REP(j, n) {
if ((i >> j) & 1) {
if (s[j] == 'z') {
f = false;
break;
} else
tmp[j]++;
}
}
if (f) {
string cp = tmp;
bool used[26] = {};
REP(i, cp.size()) {
if (!used[cp[i] - 97] && cp[i] != 'a') {
used[cp[i] - 97] = true;
cp[i]--;
}
}
if (s == cp)
ans.push_back(tmp);
}
}
sort(ALL(ans));
int sz = ans.size();
printf("%d\n", sz);
if (sz > 10) {
REP(i, 5) printf("%s\n", ans[i].c_str());
FOR(i, sz - 5, sz) printf("%s\n", ans[i].c_str());
} else {
REP(i, sz) printf("%s\n", ans[i].c_str());
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
int main() {
string s;
while (cin >> s, s != "#") {
vector<string> ans;
int n = s.size();
REP(i, 1 << n) {
string tmp = s;
bool f = true;
REP(j, n) {
if ((i >> j) & 1) {
if (s[j] == 'z') {
f = false;
break;
} else
tmp[j]++;
}
}
if (f) {
string cp = tmp;
bool used[26] = {};
REP(i, cp.size()) {
if (!used[cp[i] - 97] && cp[i] != 'a') {
used[cp[i] - 97] = true;
cp[i]--;
}
if (cp[i] != s[i])
break;
}
if (s == cp)
ans.push_back(tmp);
}
}
sort(ALL(ans));
int sz = ans.size();
printf("%d\n", sz);
if (sz > 10) {
REP(i, 5) printf("%s\n", ans[i].c_str());
FOR(i, sz - 5, sz) printf("%s\n", ans[i].c_str());
} else {
REP(i, sz) printf("%s\n", ans[i].c_str());
}
}
return 0;
} | insert | 45 | 45 | 45 | 47 | TLE | |
p00776 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S;
bool ok2(string T) {
bool used[26];
for (int i = 0; i < 26; i++)
used[i] = false;
for (int i = 0; i < S.size(); i++) {
if (!used[T[i] - 97] && T[i] != 'a') {
used[T[i] - 97] = true;
T[i]--;
}
}
return S == T;
}
int main() {
while (true) {
cin >> S;
if (S == "#")
break;
vector<string> lists;
for (int i = 0; i < (1 << S.size()); i++) {
string T = S;
bool ok = true;
for (int j = 0; j < S.size(); j++) {
if (i & (1 << j)) {
if (S[j] == 'z') {
ok = false;
break;
}
T[j]++;
}
}
if (ok) {
if (ok2(T)) {
lists.push_back(T);
}
}
}
sort(lists.begin(), lists.end());
printf("%u\n", lists.size());
if (lists.size() < 10) {
for (int i = 0; i < lists.size(); i++) {
printf("%s\n", lists[i].c_str());
}
} else {
for (int i = 0; i < 5; i++) {
printf("%s\n", lists[i].c_str());
}
for (int i = lists.size() - 5; i < lists.size(); i++) {
printf("%s\n", lists[i].c_str());
}
}
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S;
inline bool ok2(string T) {
bool used[26];
for (int i = 0; i < 26; i++)
used[i] = false;
for (int i = 0; i < S.size(); i++) {
if (!used[T[i] - 97] && T[i] != 'a') {
used[T[i] - 97] = true;
T[i]--;
}
}
return S == T;
}
int main() {
while (true) {
cin >> S;
if (S == "#")
break;
vector<string> lists;
for (int i = 0; i < (1 << S.size()); i++) {
string T = S;
bool ok = true;
for (int j = 0; j < S.size(); j++) {
if (i & (1 << j)) {
if (S[j] == 'z') {
ok = false;
break;
}
T[j]++;
}
}
if (ok) {
if (ok2(T)) {
lists.push_back(T);
}
}
}
sort(lists.begin(), lists.end());
printf("%u\n", lists.size());
if (lists.size() < 10) {
for (int i = 0; i < lists.size(); i++) {
printf("%s\n", lists[i].c_str());
}
} else {
for (int i = 0; i < 5; i++) {
printf("%s\n", lists[i].c_str());
}
for (int i = lists.size() - 5; i < lists.size(); i++) {
printf("%s\n", lists[i].c_str());
}
}
}
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p00777 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src_, int dst_, Weight weight_)
: src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
int const inf = 1 << 29;
typedef pair<Weight, int> Result;
Result visit(int p, int v, const Matrix &g) {
Result r(0, v);
int n = g.size();
rep(i, n) {
if (i != p) {
if (g[v][i] == inf)
continue;
Result t = visit(v, i, g);
t.first += g[v][i];
if (r.first < t.first)
r = t;
}
}
return r;
}
tuple<int, int, Weight> diameter(const Matrix &g, int start) {
Result r = visit(-1, start, g);
Result t = visit(-1, r.second, g);
return mt(r.second, t.second,
t.first); // (r.second, t.second) is farthest pair
}
Matrix mat;
int deg[1000];
int n;
int solve(int p, int v) {
int res = 0;
rep(i, n) {
if (i == p)
continue;
if (mat[v][i] == inf)
continue;
res += mat[v][i];
res += solve(v, i);
}
return res;
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
while (cin >> n && n) {
mat.assign(n, Array(n, inf));
int ans = 0;
rep(i, n) deg[i] = 0;
{
int p[800], d[100];
rep(i, n - 1) cin >> p[i];
rep(i, n - 1) cin >> d[i];
rep(i, n - 1) {
p[i]--;
mat[i + 1][p[i]] = mat[p[i]][i + 1] = d[i];
deg[i + 1]++;
deg[p[i]]++;
}
}
int t = 0;
rep(i, n) {
if (deg[i] == 1) {
rep(j, n) {
if (mat[i][j] != inf) {
t += mat[i][j];
mat[i][j] = inf;
mat[j][i] = inf;
}
}
}
}
ans += t;
int u = 0;
rep(i, n) {
deg[i] = 0;
rep(j, n) {
if (mat[i][j] != inf) {
deg[i]++;
u += mat[i][j];
}
}
}
ans += u / 2 * 3;
int start = -1;
rep(i, n) {
if (deg[i] == 0)
continue;
start = i;
break;
}
if (start == -1) {
cout << ans << endl;
} else {
int a, b, d;
vi dist, prev, path;
tie(a, b, d) = diameter(mat, start);
ans -= d;
cout << ans << endl;
}
}
} | #define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : " ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
_ot<0>(os, t);
return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
return os;
}
#ifdef DEBUG
#define dump(...) \
(cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
<< endl)
#else
#define dump(...)
#endif
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src_, int dst_, Weight weight_)
: src(src_), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
int const inf = 1 << 29;
typedef pair<Weight, int> Result;
Result visit(int p, int v, const Matrix &g) {
Result r(0, v);
int n = g.size();
rep(i, n) {
if (i != p) {
if (g[v][i] == inf)
continue;
Result t = visit(v, i, g);
t.first += g[v][i];
if (r.first < t.first)
r = t;
}
}
return r;
}
tuple<int, int, Weight> diameter(const Matrix &g, int start) {
Result r = visit(-1, start, g);
Result t = visit(-1, r.second, g);
return mt(r.second, t.second,
t.first); // (r.second, t.second) is farthest pair
}
Matrix mat;
int deg[1000];
int n;
int solve(int p, int v) {
int res = 0;
rep(i, n) {
if (i == p)
continue;
if (mat[v][i] == inf)
continue;
res += mat[v][i];
res += solve(v, i);
}
return res;
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
while (cin >> n && n) {
mat.assign(n, Array(n, inf));
int ans = 0;
rep(i, n) deg[i] = 0;
{
int p[1000], d[1000];
rep(i, n - 1) cin >> p[i];
rep(i, n - 1) cin >> d[i];
rep(i, n - 1) {
p[i]--;
mat[i + 1][p[i]] = mat[p[i]][i + 1] = d[i];
deg[i + 1]++;
deg[p[i]]++;
}
}
int t = 0;
rep(i, n) {
if (deg[i] == 1) {
rep(j, n) {
if (mat[i][j] != inf) {
t += mat[i][j];
mat[i][j] = inf;
mat[j][i] = inf;
}
}
}
}
ans += t;
int u = 0;
rep(i, n) {
deg[i] = 0;
rep(j, n) {
if (mat[i][j] != inf) {
deg[i]++;
u += mat[i][j];
}
}
}
ans += u / 2 * 3;
int start = -1;
rep(i, n) {
if (deg[i] == 0)
continue;
start = i;
break;
}
if (start == -1) {
cout << ans << endl;
} else {
int a, b, d;
vi dist, prev, path;
tie(a, b, d) = diameter(mat, start);
ans -= d;
cout << ans << endl;
}
}
} | replace | 114 | 115 | 114 | 115 | 0 | |
p00777 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
vector<vector<pair<int, int>>> graph;
int sum, D;
int dfs(int v, int r) {
vector<int> ch = {0, 0};
for (auto p : graph[v]) {
int sv, d;
tie(sv, d) = p;
if (sv == r)
continue;
int res = dfs(sv, v);
if (res < 0) {
ch.push_back(0);
sum += d;
} else {
sum += d * 3;
ch.push_back(res + d);
}
}
if (ch.size() <= 2)
return -1;
sort(ch.rbegin(), ch.rend());
D = max(D, ch[0] + ch[1]);
return ch[0];
}
bool solve() {
int N;
cin >> N;
if (N == 0)
return false;
vector<int> p(N - 1), d(N - 1);
for (int i = 1; i < N; ++i) {
cin >> p[i];
--p[i];
}
for (int i = 1; i < N; ++i) {
cin >> d[i];
}
graph.clear();
graph.resize(N);
for (int i = 1; i < N; ++i) {
graph[i].emplace_back(p[i], d[i]);
graph[p[i]].emplace_back(i, d[i]);
}
sum = 0, D = 0;
for (int v = 0; v < N; ++v) {
if (graph[v].size() <= 1)
continue;
dfs(v, -1);
break;
}
cout << sum - D << endl;
return true;
}
int main() {
while (solve()) {
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
vector<vector<pair<int, int>>> graph;
int sum, D;
int dfs(int v, int r) {
vector<int> ch = {0, 0};
for (auto p : graph[v]) {
int sv, d;
tie(sv, d) = p;
if (sv == r)
continue;
int res = dfs(sv, v);
if (res < 0) {
ch.push_back(0);
sum += d;
} else {
sum += d * 3;
ch.push_back(res + d);
}
}
if (ch.size() <= 2)
return -1;
sort(ch.rbegin(), ch.rend());
D = max(D, ch[0] + ch[1]);
return ch[0];
}
bool solve() {
int N;
cin >> N;
if (N == 0)
return false;
vector<int> p(N), d(N);
for (int i = 1; i < N; ++i) {
cin >> p[i];
--p[i];
}
for (int i = 1; i < N; ++i) {
cin >> d[i];
}
graph.clear();
graph.resize(N);
for (int i = 1; i < N; ++i) {
graph[i].emplace_back(p[i], d[i]);
graph[p[i]].emplace_back(i, d[i]);
}
sum = 0, D = 0;
for (int v = 0; v < N; ++v) {
if (graph[v].size() <= 1)
continue;
dfs(v, -1);
break;
}
cout << sum - D << endl;
return true;
}
int main() {
while (solve()) {
}
return 0;
}
| replace | 38 | 39 | 38 | 39 | 0 | |
p00777 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Edge {
public:
int to, cost;
Edge(int to, int cost) {
this->to = to;
this->cost = cost;
}
};
vector<vector<int>> memo;
int solve(const vector<vector<Edge>> edges, int curr, int prev, int isBack) {
if (edges[curr].size() == 1 && prev != -1)
return 0;
if (memo[isBack][curr] != -1)
return memo[isBack][curr];
int sum = 0;
int maxDiff = 0;
for (unsigned i = 0; i < edges[curr].size(); ++i) {
int next = edges[curr][i].to;
int cost = edges[curr][i].cost;
if (next == prev) {
sum += cost;
if (isBack == 1)
sum += cost;
} else {
sum += cost + solve(edges, next, curr, 1);
if (isBack == 0) {
int diff = solve(edges, next, curr, 1) - solve(edges, next, curr, 0);
maxDiff = max(maxDiff, diff);
}
}
}
int ret = sum - maxDiff;
return memo[isBack][curr] = ret;
}
int main() {
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<int> p(n + 11);
for (int i = 2; i <= n; ++i)
cin >> p[i];
vector<vector<Edge>> edges(n + 1);
for (int i = 2; i <= n; ++i) {
int d;
cin >> d;
edges[i].push_back(Edge(p[i], d));
edges[p[i]].push_back(Edge(i, d));
}
int ret = INT_MAX;
for (int i = 1; i <= n; ++i) {
memo.assign(2, vector<int>(n + 1, -1));
ret = min(ret, solve(edges, i, -1, 0));
}
cout << ret << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
class Edge {
public:
int to, cost;
Edge(int to, int cost) {
this->to = to;
this->cost = cost;
}
};
vector<vector<int>> memo;
int solve(const vector<vector<Edge>> &edges, int curr, int prev, int isBack) {
if (edges[curr].size() == 1 && prev != -1)
return 0;
if (memo[isBack][curr] != -1)
return memo[isBack][curr];
int sum = 0;
int maxDiff = 0;
for (unsigned i = 0; i < edges[curr].size(); ++i) {
int next = edges[curr][i].to;
int cost = edges[curr][i].cost;
if (next == prev) {
sum += cost;
if (isBack == 1)
sum += cost;
} else {
sum += cost + solve(edges, next, curr, 1);
if (isBack == 0) {
int diff = solve(edges, next, curr, 1) - solve(edges, next, curr, 0);
maxDiff = max(maxDiff, diff);
}
}
}
int ret = sum - maxDiff;
return memo[isBack][curr] = ret;
}
int main() {
for (;;) {
int n;
cin >> n;
if (n == 0)
return 0;
vector<int> p(n + 11);
for (int i = 2; i <= n; ++i)
cin >> p[i];
vector<vector<Edge>> edges(n + 1);
for (int i = 2; i <= n; ++i) {
int d;
cin >> d;
edges[i].push_back(Edge(p[i], d));
edges[p[i]].push_back(Edge(i, d));
}
int ret = INT_MAX;
for (int i = 1; i <= n; ++i) {
memo.assign(2, vector<int>(n + 1, -1));
ret = min(ret, solve(edges, i, -1, 0));
}
cout << ret << endl;
}
} | replace | 33 | 34 | 33 | 34 | TLE | |
p00778 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int ord[] = {0, 1, 3, 2};
string dir = "ENWS";
string rdir = "WSEN";
int roll[4][6] = {{4, 1, 0, 3, 5, 2},
{3, 0, 2, 5, 4, 1},
{2, 1, 5, 3, 0, 4},
{1, 5, 2, 0, 4, 3}};
bool check(const vector<int> &dice) {
if (dice[0] < 0)
return false;
int v1 = dice[0] + dice[5], v2 = dice[1] + dice[3], v3 = dice[2] + dice[4];
if (v1 >= v2 + v3 + 1)
return false;
if (v2 > v1 + v3 + 1)
return false;
if (v3 > v1 + v2 + 1)
return false;
return true;
}
vector<int> get_roll(const vector<int> &dice, int d) {
vector<int> res(6);
for (int i = 0; i < 6; i++)
res[roll[d][i]] = dice[i];
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
vector<int> t(6);
while (true) {
for (int i = 0; i < 6; i++) {
cin >> t[i];
}
if (accumulate(t.begin(), t.end(), 0) == 0)
break;
int p, q;
cin >> p >> q;
--p;
sort(t.begin(), t.end());
string res = "Z";
do {
if (!check(t))
continue;
auto dice = t;
string s;
while (*max_element(dice.begin(), dice.end()) > 0) {
for (int i = 0; i < 4; i++) {
char c = dir[ord[i]];
dice = get_roll(dice, ord[i]);
dice[0]--;
if (check(dice)) {
s.push_back(c);
break;
}
dice[0]++;
dice = get_roll(dice, (ord[i] + 2) % 4);
}
}
res = min(res, s);
} while (next_permutation(t.begin(), t.end()));
if (res == "Z") {
cout << "impossible" << endl;
} else {
cout << res.substr(p, q - p) << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int ord[] = {0, 1, 3, 2};
string dir = "ENWS";
string rdir = "WSEN";
int roll[4][6] = {{4, 1, 0, 3, 5, 2},
{3, 0, 2, 5, 4, 1},
{2, 1, 5, 3, 0, 4},
{1, 5, 2, 0, 4, 3}};
bool check(const vector<int> &dice) {
if (dice[0] < 0)
return false;
int v1 = dice[0] + dice[5], v2 = dice[1] + dice[3], v3 = dice[2] + dice[4];
if (v1 >= v2 + v3 + 1)
return false;
if (v2 > v1 + v3 + 1)
return false;
if (v3 > v1 + v2 + 1)
return false;
return true;
}
vector<int> get_roll(const vector<int> &dice, int d) {
vector<int> res(6);
for (int i = 0; i < 6; i++)
res[roll[d][i]] = dice[i];
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
vector<int> t(6);
while (true) {
for (int i = 0; i < 6; i++) {
cin >> t[i];
}
if (accumulate(t.begin(), t.end(), 0) == 0)
break;
int p, q;
cin >> p >> q;
--p;
sort(t.begin(), t.end());
string res = "Z";
do {
if (!check(t))
continue;
auto dice = t;
string s;
while (*max_element(dice.begin(), dice.end()) > 0) {
for (int i = 0; i < 4; i++) {
char c = dir[ord[i]];
dice = get_roll(dice, ord[i]);
dice[0]--;
if (check(dice)) {
s.push_back(c);
break;
}
dice[0]++;
dice = get_roll(dice, (ord[i] + 2) % 4);
}
if (s > res)
break;
}
res = min(res, s);
} while (next_permutation(t.begin(), t.end()));
if (res == "Z") {
cout << "impossible" << endl;
} else {
cout << res.substr(p, q - p) << endl;
}
}
return 0;
}
| insert | 64 | 64 | 64 | 66 | TLE | |
p00778 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const string dir = "ENSW";
const string rdir = "WSNW";
constexpr int rot[4][6] = {{2, 1, 5, 3, 0, 4},
{3, 0, 2, 5, 4, 1},
{1, 5, 2, 0, 4, 3},
{4, 1, 0, 3, 5, 2}};
vector<int> roll(vector<int> const &v, char d) {
const int p = find(begin(dir), end(dir), d) - begin(dir);
vector<int> res(6);
for (int i = 0; i < 6; ++i) {
res[i] = v[rot[p][i]];
}
return res;
}
bool check(vector<int> const &dice) {
int v1 = dice[0] + dice[5], v2 = dice[2] + dice[4], v3 = dice[1] + dice[3];
int cnt = 0;
if (dice[0] < 0)
return false;
if (v1 >= v2 + v3 + 1)
return false;
if (v2 > v1 + v3 + 1)
return false;
if (v2 == v1 + v3 + 1)
cnt++;
if (v3 > v1 + v2 + 1)
return false;
if (v3 == v1 + v2 + 1)
cnt++;
return cnt <= 1;
};
bool found = false;
string ans, t;
void dfs(vector<int> &dice) {
if (*max_element(begin(dice), end(dice)) == 0) {
if (found) {
ans = min(ans, t);
} else {
ans = t;
}
// cout << "t: " << t << endl;
found = true;
return;
}
for (int i = 0; i < 4; ++i) {
dice = roll(dice, dir[i]);
dice[0]--;
if (check(dice)) {
t += dir[i];
dfs(dice);
return;
}
dice[0]++;
dice = roll(dice, rdir[i]);
}
for (int i = 0; i < 6; ++i) {
cout << dice[i] << ' ';
}
cout << endl;
assert(false);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
vector<int> v(6);
while (true) {
ans.clear(), t.clear();
found = false;
for (int i = 0; i < 6; ++i)
cin >> v[i];
if (*max_element(begin(v), end(v)) == 0)
break;
sort(begin(v), end(v));
int p, q;
cin >> p >> q;
do {
auto tdice = v;
if (!check(v))
continue;
t.clear();
dfs(tdice);
// cout << "sum: " << accumulate(begin(v), end(v), 0) << endl;
} while (next_permutation(begin(v), end(v)));
if (!found) {
cout << "impossible" << endl;
} else {
// cout << "sz: " << ans.size() << endl;
cout << ans.substr(p - 1, q - p + 1) << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
const string dir = "ENSW";
const string rdir = "WSNW";
constexpr int rot[4][6] = {{2, 1, 5, 3, 0, 4},
{3, 0, 2, 5, 4, 1},
{1, 5, 2, 0, 4, 3},
{4, 1, 0, 3, 5, 2}};
vector<int> roll(vector<int> const &v, char d) {
const int p = find(begin(dir), end(dir), d) - begin(dir);
vector<int> res(6);
for (int i = 0; i < 6; ++i) {
res[i] = v[rot[p][i]];
}
return res;
}
bool check(vector<int> const &dice) {
int v1 = dice[0] + dice[5], v2 = dice[2] + dice[4], v3 = dice[1] + dice[3];
int cnt = 0;
if (dice[0] < 0)
return false;
if (v1 >= v2 + v3 + 1)
return false;
if (v2 > v1 + v3 + 1)
return false;
if (v2 == v1 + v3 + 1)
cnt++;
if (v3 > v1 + v2 + 1)
return false;
if (v3 == v1 + v2 + 1)
cnt++;
return cnt <= 1;
};
bool found = false;
string ans, t;
void dfs(vector<int> &dice) {
if (found && ans < t) {
return;
}
if (*max_element(begin(dice), end(dice)) == 0) {
if (found) {
ans = min(ans, t);
} else {
ans = t;
}
// cout << "t: " << t << endl;
found = true;
return;
}
for (int i = 0; i < 4; ++i) {
dice = roll(dice, dir[i]);
dice[0]--;
if (check(dice)) {
t += dir[i];
dfs(dice);
return;
}
dice[0]++;
dice = roll(dice, rdir[i]);
}
for (int i = 0; i < 6; ++i) {
cout << dice[i] << ' ';
}
cout << endl;
assert(false);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
vector<int> v(6);
while (true) {
ans.clear(), t.clear();
found = false;
for (int i = 0; i < 6; ++i)
cin >> v[i];
if (*max_element(begin(v), end(v)) == 0)
break;
sort(begin(v), end(v));
int p, q;
cin >> p >> q;
do {
auto tdice = v;
if (!check(v))
continue;
t.clear();
dfs(tdice);
// cout << "sum: " << accumulate(begin(v), end(v), 0) << endl;
} while (next_permutation(begin(v), end(v)));
if (!found) {
cout << "impossible" << endl;
} else {
// cout << "sz: " << ans.size() << endl;
cout << ans.substr(p - 1, q - p + 1) << endl;
}
}
}
| insert | 40 | 40 | 40 | 43 | TLE | |
p00780 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
template <int m> class Prime {
std::vector<bool> p;
std::vector<int> ps;
public:
Prime() {
p = std::vector<bool>(m, true);
assert(m > 1);
p[0] = p[1] = false;
for (int i = 4; i < m; i += 2)
p[i] = false;
ps.push_back(2);
for (int i = 3; i < m; i += 2) {
if (p[i]) {
ps.push_back(i);
for (int j = i + i; j < m; j += i)
p[j] = false;
}
}
}
bool isPrime(int n) {
assert(m > n);
return p[n];
}
int operator[](int n) {
if (n < ps.size())
return ps[n];
else
return 0;
}
int size() { return ps.size(); }
};
int main() {
Prime<(1 << 14) + 100> p;
int n;
while (cin >> n, n) {
int ans = 0;
for (int i = 0; p[i] + p[i] <= n; i++) {
if (p.isPrime(n - p[i]))
ans++;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
template <int m> class Prime {
std::vector<bool> p;
std::vector<int> ps;
public:
Prime() {
p = std::vector<bool>(m, true);
assert(m > 1);
p[0] = p[1] = false;
for (int i = 4; i < m; i += 2)
p[i] = false;
ps.push_back(2);
for (int i = 3; i < m; i += 2) {
if (p[i]) {
ps.push_back(i);
for (int j = i + i; j < m; j += i)
p[j] = false;
}
}
}
bool isPrime(int n) {
assert(m > n);
return p[n];
}
int operator[](int n) {
if (n < ps.size())
return ps[n];
else
return 0;
}
int size() { return ps.size(); }
};
int main() {
Prime<(1 << 15) + 100> p;
int n;
while (cin >> n, n) {
int ans = 0;
for (int i = 0; p[i] + p[i] <= n; i++) {
if (p.isPrime(n - p[i]))
ans++;
}
cout << ans << endl;
}
return 0;
} | replace | 66 | 67 | 66 | 67 | 0 | |
p00780 | C++ | Runtime Error | #include <iostream>
using namespace std;
bool primes[100000];
void Sieve() {
for (int i = 2; i < 1000000; i++) {
primes[i] = true;
}
primes[1] = false;
for (int i = 2; i <= 1000; i++) {
if (primes[i] == true)
for (int j = i; j * i <= 1000000; j++) {
primes[j * i] = false;
}
}
}
bool IsPrime(int x) { return primes[x]; }
int main(void) {
int k;
for (k = 1; k > 0; k++) {
Sieve();
int n, c = 0;
cin >> n;
if (n == 0)
break;
else {
for (int i = 0; i <= n / 2; i++) {
if (IsPrime(i) == true) {
if (IsPrime(n - i) == true)
c++;
}
}
cout << c << endl;
}
}
return 0;
}
| #include <iostream>
using namespace std;
bool primes[1000000];
void Sieve() {
for (int i = 2; i < 1000000; i++) {
primes[i] = true;
}
primes[1] = false;
for (int i = 2; i <= 1000; i++) {
if (primes[i] == true)
for (int j = i; j * i <= 1000000; j++) {
primes[j * i] = false;
}
}
}
bool IsPrime(int x) { return primes[x]; }
int main(void) {
int k;
for (k = 1; k > 0; k++) {
Sieve();
int n, c = 0;
cin >> n;
if (n == 0)
break;
else {
for (int i = 0; i <= n / 2; i++) {
if (IsPrime(i) == true) {
if (IsPrime(n - i) == true)
c++;
}
}
cout << c << endl;
}
}
return 0;
}
| replace | 2 | 3 | 2 | 3 | -11 | |
p00782 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) begin(v), end(v)
bool mas[444][444];
void color(int x, int y, int gx, int gy) {
mas[x][y] = true;
if (x + 1 < gx)
color(x + 1, y, gx, gy);
if (y + 1 < gy)
color(x, y + 1, gx, gy);
}
int main() {
int n;
int t = 0;
while (cin >> n, n) {
t++;
memset(mas, false, sizeof(mas));
vector<double> xx, yy;
vector<double> X, Y;
rep(i, n) {
double x, y, r;
cin >> x >> y >> r;
xx.push_back(x - r);
xx.push_back(x + r);
yy.push_back(y - r);
yy.push_back(y + r);
X.push_back(x - r);
X.push_back(x + r);
Y.push_back(y - r);
Y.push_back(y + r);
}
sort(all(X));
sort(all(Y));
X.erase(unique(all(X)), X.end());
Y.erase(unique(all(Y)), Y.end());
for (int i = 0; i < (int)xx.size(); i += 2) {
xx[i] = find(all(X), xx[i]) - begin(X);
yy[i] = find(all(Y), yy[i]) - begin(Y);
xx[i + 1] = find(all(X), xx[i + 1]) - begin(X);
yy[i + 1] = find(all(Y), yy[i + 1]) - begin(Y);
color(xx[i], yy[i], xx[i + 1], yy[i + 1]);
}
/*//
rep(i, Y.size()) {
rep(j, X.size()) cout << (int)mas[j][i];
cout << endl;
}
//*/
double ans = 0;
rep(i, X.size() - 1) rep(j, Y.size() - 1) {
if (!mas[i][j])
continue;
ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
// cout << X[i+1] << " " << X[i] << " " << Y[j+1] << " " << Y[j] << endl;
}
printf("%d %.2f\n", t, ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) begin(v), end(v)
bool mas[444][444];
void color(int x, int y, int gx, int gy) {
for (int i = x; i < gx; i++) {
for (int j = y; j < gy; j++)
mas[i][j] = true;
}
}
int main() {
int n;
int t = 0;
while (cin >> n, n) {
t++;
memset(mas, false, sizeof(mas));
vector<double> xx, yy;
vector<double> X, Y;
rep(i, n) {
double x, y, r;
cin >> x >> y >> r;
xx.push_back(x - r);
xx.push_back(x + r);
yy.push_back(y - r);
yy.push_back(y + r);
X.push_back(x - r);
X.push_back(x + r);
Y.push_back(y - r);
Y.push_back(y + r);
}
sort(all(X));
sort(all(Y));
X.erase(unique(all(X)), X.end());
Y.erase(unique(all(Y)), Y.end());
for (int i = 0; i < (int)xx.size(); i += 2) {
xx[i] = find(all(X), xx[i]) - begin(X);
yy[i] = find(all(Y), yy[i]) - begin(Y);
xx[i + 1] = find(all(X), xx[i + 1]) - begin(X);
yy[i + 1] = find(all(Y), yy[i + 1]) - begin(Y);
color(xx[i], yy[i], xx[i + 1], yy[i + 1]);
}
/*//
rep(i, Y.size()) {
rep(j, X.size()) cout << (int)mas[j][i];
cout << endl;
}
//*/
double ans = 0;
rep(i, X.size() - 1) rep(j, Y.size() - 1) {
if (!mas[i][j])
continue;
ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
// cout << X[i+1] << " " << X[i] << " " << Y[j+1] << " " << Y[j] << endl;
}
printf("%d %.2f\n", t, ans);
}
return 0;
} | replace | 8 | 13 | 8 | 12 | TLE | |
p00786 | C++ | Runtime Error | #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;
const int SIZE = 299;
struct but {
vector<shared_ptr<but>> chs;
char c;
but(char c_) : chs(), c(c_) {}
pair<int, vector<string>> getst() {
if (chs.empty()) {
return make_pair(0, vector<string>(1, string(1, c)));
} else if (chs.size() == 1) {
auto p = chs[0]->getst();
auto chst = p.second;
int chroot = p.first;
vector<string> nst(chst.size() + 2, string(chst[0].size(), ' '));
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = 0; j < nst[0].size(); ++j) {
nst[i][j] = chst[i][j];
}
}
for (int i = nst.size() - 2; i < nst.size(); ++i) {
for (int j = 0; j < nst[i].size(); ++j) {
if (j == chroot) {
if (i == nst.size() - 2) {
nst[i][j] = '-';
} else {
nst[i][j] = c;
}
} else {
nst[i][j] = ' ';
}
}
}
return make_pair(chroot, nst);
} else {
auto p1 = chs[0]->getst();
auto p2 = chs[1]->getst();
int chroot1 = p1.first;
int chroot2 = p2.first;
auto chst1 = p1.second;
auto chst2 = p2.second;
if (chst1.size() < chst2.size()) {
int n = chst2.size() - chst1.size();
for (int i = 0; i < n; ++i) {
chst1.insert(chst1.begin(), string(chst1[0].size(), ' '));
}
} else if (chst1.size() > chst2.size()) {
int n = chst1.size() - chst2.size();
for (int i = 0; i < n; ++i) {
chst2.insert(chst2.begin(), string(chst2[0].size(), ' '));
}
}
int bet = 0;
for (int i = 0; i < chst1.size(); ++i) {
int r = -200, l = -200;
for (int j = chst1[i].size() - 1; j >= 0; --j) {
if (chst1[i][j] != ' ') {
r = j - chroot1;
break;
}
}
for (int j = 0; j < chst2[i].size(); ++j) {
if (chst2[i][j] != ' ') {
l = chroot2 - j;
break;
}
}
bet = max(bet, l + r);
}
bet += 2;
while (1) {
int loff = bet / 2;
int roff = (bet + 1) / 2;
int asize = chroot1 + chst2[0].size() - chroot2;
vector<string> nst(chst1.size() + 2, string(SIZE, ' '));
const int center = SIZE / 2;
const int lm = center - loff;
const int ll = lm - chroot1;
const int lr = ll + chst1[0].size();
const int rm = center + roff;
const int rl = rm - chroot2;
const int rr = rl + chst2[0].size();
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = ll; j < lr; ++j) {
nst[i][j] = chst1[i][j - ll];
}
}
bool ok = true;
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = rl; j < rr; ++j) {
if (chst2[i][j - rl] != ' ') {
if (i && nst[i - 1][j] != ' ') {
ok = false;
}
if (nst[i + 1][j] != ' ') {
ok = false;
}
}
}
}
if (!ok) {
bet++;
continue;
}
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = rl; j < rr; ++j) {
if (nst[i][j] == ' ') {
nst[i][j] = chst2[i][j - rl];
}
}
}
for (int i = nst.size() - 2; i < nst.size() - 1; ++i) {
for (int j = lm; j <= rm; ++j) {
nst[i][j] = '-';
}
}
nst[nst.size() - 1][center] = c;
while (1) {
bool flag = true;
for (int i = 0; i < nst.size(); ++i) {
if (nst[i][0] != ' ')
flag = false;
}
if (!flag)
break;
for (int i = 0; i < nst.size(); ++i) {
nst[i].erase(nst[i].begin());
}
}
while (1) {
bool flag = true;
for (int i = 0; i < nst.size(); ++i) {
if (nst[i].back() != ' ')
flag = false;
}
if (!flag)
break;
for (int i = 0; i < nst.size(); ++i) {
nst[i].pop_back();
}
}
int aa;
for (int j = 0; j < nst[nst.size() - 1].size(); ++j) {
if (nst[nst.size() - 1][j] != ' ') {
aa = j;
break;
}
}
return make_pair(aa, nst);
}
}
}
};
shared_ptr<but> mkbut(string st, int &a) {
if (isalpha(st[a])) {
shared_ptr<but> bt(make_shared<but>(st[a]));
a++;
if (st.size() == a || st[a] == ')' || st[a] == ',') {
return bt;
} else if (st[a] == '(') {
a++;
shared_ptr<but> ch1(mkbut(st, a));
bt->chs.push_back(ch1);
if (st[a] == ')') {
a++;
return bt;
} else {
assert(st[a] == ',');
a++;
shared_ptr<but> ch2(mkbut(st, a));
bt->chs.push_back(ch2);
assert(st[a] == ')');
a++;
return bt;
}
}
} else {
assert(false);
}
}
void solve(string st) {
int a = 0;
shared_ptr<but> head(mkbut(st, a));
auto p = head->getst();
for (int i = 0; i < p.second.size(); ++i) {
while (!p.second[i].empty() && p.second[i].back() == ' ')
p.second.pop_back();
cout << p.second[i] << endl;
}
}
int main() {
string nst;
int num = 1;
while (1) {
string st;
cin >> st;
nst += st;
if (nst.back() == ';') {
nst.pop_back();
cout << num << ":" << endl;
solve(nst);
num++;
nst.clear();
} else if (nst.back() == '.') {
nst.pop_back();
cout << num << ":" << endl;
solve(nst);
num++;
nst.clear();
break;
}
}
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;
const int SIZE = 299;
struct but {
vector<shared_ptr<but>> chs;
char c;
but(char c_) : chs(), c(c_) {}
pair<int, vector<string>> getst() {
if (chs.empty()) {
return make_pair(0, vector<string>(1, string(1, c)));
} else if (chs.size() == 1) {
auto p = chs[0]->getst();
auto chst = p.second;
int chroot = p.first;
vector<string> nst(chst.size() + 2, string(chst[0].size(), ' '));
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = 0; j < nst[0].size(); ++j) {
nst[i][j] = chst[i][j];
}
}
for (int i = nst.size() - 2; i < nst.size(); ++i) {
for (int j = 0; j < nst[i].size(); ++j) {
if (j == chroot) {
if (i == nst.size() - 2) {
nst[i][j] = '-';
} else {
nst[i][j] = c;
}
} else {
nst[i][j] = ' ';
}
}
}
return make_pair(chroot, nst);
} else {
auto p1 = chs[0]->getst();
auto p2 = chs[1]->getst();
int chroot1 = p1.first;
int chroot2 = p2.first;
auto chst1 = p1.second;
auto chst2 = p2.second;
if (chst1.size() < chst2.size()) {
int n = chst2.size() - chst1.size();
for (int i = 0; i < n; ++i) {
chst1.insert(chst1.begin(), string(chst1[0].size(), ' '));
}
} else if (chst1.size() > chst2.size()) {
int n = chst1.size() - chst2.size();
for (int i = 0; i < n; ++i) {
chst2.insert(chst2.begin(), string(chst2[0].size(), ' '));
}
}
int bet = 0;
for (int i = 0; i < chst1.size(); ++i) {
int r = -200, l = -200;
for (int j = chst1[i].size() - 1; j >= 0; --j) {
if (chst1[i][j] != ' ') {
r = j - chroot1;
break;
}
}
for (int j = 0; j < chst2[i].size(); ++j) {
if (chst2[i][j] != ' ') {
l = chroot2 - j;
break;
}
}
bet = max(bet, l + r);
}
bet += 2;
while (1) {
int loff = bet / 2;
int roff = (bet + 1) / 2;
int asize = chroot1 + chst2[0].size() - chroot2;
vector<string> nst(chst1.size() + 2, string(SIZE, ' '));
const int center = SIZE / 2;
const int lm = center - loff;
const int ll = lm - chroot1;
const int lr = ll + chst1[0].size();
const int rm = center + roff;
const int rl = rm - chroot2;
const int rr = rl + chst2[0].size();
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = ll; j < lr; ++j) {
nst[i][j] = chst1[i][j - ll];
}
}
bool ok = true;
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = rl; j < rr; ++j) {
if (chst2[i][j - rl] != ' ') {
if (i && nst[i - 1][j] != ' ') {
ok = false;
}
if (nst[i + 1][j] != ' ') {
ok = false;
}
}
}
}
if (!ok) {
bet++;
continue;
}
for (int i = 0; i < nst.size() - 2; ++i) {
for (int j = rl; j < rr; ++j) {
if (nst[i][j] == ' ') {
nst[i][j] = chst2[i][j - rl];
}
}
}
for (int i = nst.size() - 2; i < nst.size() - 1; ++i) {
for (int j = lm; j <= rm; ++j) {
nst[i][j] = '-';
}
}
nst[nst.size() - 1][center] = c;
while (1) {
bool flag = true;
for (int i = 0; i < nst.size(); ++i) {
if (nst[i][0] != ' ')
flag = false;
}
if (!flag)
break;
for (int i = 0; i < nst.size(); ++i) {
nst[i].erase(nst[i].begin());
}
}
while (1) {
bool flag = true;
for (int i = 0; i < nst.size(); ++i) {
if (nst[i].back() != ' ')
flag = false;
}
if (!flag)
break;
for (int i = 0; i < nst.size(); ++i) {
nst[i].pop_back();
}
}
int aa;
for (int j = 0; j < nst[nst.size() - 1].size(); ++j) {
if (nst[nst.size() - 1][j] != ' ') {
aa = j;
break;
}
}
return make_pair(aa, nst);
}
}
}
};
shared_ptr<but> mkbut(string st, int &a) {
if (isalpha(st[a])) {
shared_ptr<but> bt(make_shared<but>(st[a]));
a++;
if (st.size() == a || st[a] == ')' || st[a] == ',') {
return bt;
} else if (st[a] == '(') {
a++;
shared_ptr<but> ch1(mkbut(st, a));
bt->chs.push_back(ch1);
if (st[a] == ')') {
a++;
return bt;
} else {
assert(st[a] == ',');
a++;
shared_ptr<but> ch2(mkbut(st, a));
bt->chs.push_back(ch2);
assert(st[a] == ')');
a++;
return bt;
}
}
} else {
assert(false);
}
}
void solve(string st) {
int a = 0;
shared_ptr<but> head(mkbut(st, a));
auto p = head->getst();
for (int i = 0; i < p.second.size(); ++i) {
while (!p.second[i].empty() && p.second[i].back() == ' ')
p.second[i].pop_back();
cout << p.second[i] << endl;
}
}
int main() {
string nst;
int num = 1;
while (1) {
string st;
cin >> st;
nst += st;
if (nst.back() == ';') {
nst.pop_back();
cout << num << ":" << endl;
solve(nst);
num++;
nst.clear();
} else if (nst.back() == '.') {
nst.pop_back();
cout << num << ":" << endl;
solve(nst);
num++;
nst.clear();
break;
}
}
return 0;
} | replace | 193 | 194 | 193 | 194 | -11 | |
p00788 | C++ | Time Limit Exceeded | #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;
long long gcd(long long a, long long b) { // Ååöñ
if (a == 0 || b == 0)
return 0;
if (b > a)
swap(a, b);
long long tmp;
while ((tmp = a % b) != 0) {
a = b;
b = tmp;
}
return b;
}
int main() {
for (;;) {
int p, n;
cin >> p >> n;
if (p == 0)
return 0;
int u = 0;
int v = 1;
int x = INT_MAX / 2;
int y = 1;
for (int i = 1; i <= n; ++i) { // ªq
for (int j = 1; j <= n; ++j) { // ªê
if (gcd(i, j) != 1)
continue;
if (i * i > p * j * j) {
if (i * y < j * x) {
x = i;
y = j;
}
} else {
if (i * v > j * u) {
u = i;
v = j;
}
}
}
}
cout << x << '/' << y << ' ' << u << '/' << v << endl;
}
} | #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;
long long gcd(long long a, long long b) { // Ååöñ
if (a == 0 || b == 0)
return 0;
if (b > a)
swap(a, b);
long long tmp;
while ((tmp = a % b) != 0) {
a = b;
b = tmp;
}
return b;
}
int main() {
for (;;) {
int p, n;
cin >> p >> n;
if (p == 0)
return 0;
int u = 0;
int v = 1;
int x = INT_MAX / 2;
int y = 1;
for (int i = 1; i <= n; ++i) { // ªê
int j = i * sqrt(double(p)); // ªq
if (j > n)
break;
if (gcd(i, j) == 1 && i * u < j * v) {
u = j;
v = i;
}
++j;
if (j > n)
break;
if (gcd(i, j) == 1 && i * x > j * y) {
x = j;
y = i;
}
}
cout << x << '/' << y << ' ' << u << '/' << v << endl;
}
} | replace | 43 | 58 | 43 | 58 | TLE | |
p00788 | C++ | Runtime Error | #include <cassert>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const double eps = 1e-11;
bool eq(double a, double b) { return abs(b - a) < eps; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const double inf = 1e30;
int main() {
int p, n;
while (cin >> p >> n) {
if (p == 0 && n == 0)
break;
double rp = sqrt(p);
double min_diff_lower = inf;
double min_diff_upper = inf;
int ans_lower_p, ans_lower_q, ans_upper_p, ans_upper_q;
for (int i = 1; i <= n; ++i) {
int r = n;
int l = 1;
int cnt = 0;
int m;
while (cnt++ < 20) {
m = (r + l) / 2;
if ((double)m / i > rp) {
r = m;
} else {
l = m;
}
}
++m;
if ((double)m / i <= rp)
--m;
assert(rp - (double)m / i > 0);
if (min_diff_lower > rp - (double)m / i) {
min_diff_lower = rp - (double)m / i;
ans_lower_p = m;
ans_lower_q = i;
}
for (int j = m + 1; j <= n; ++j) {
if ((double)j / i > rp) {
m = j;
break;
}
}
if (((double)m / i - rp) > 0 && min_diff_upper > ((double)m / i - rp)) {
min_diff_upper = (double)m / i - rp;
ans_upper_p = m;
ans_upper_q = i;
}
}
int lower_gcd = gcd(ans_lower_p, ans_lower_q);
int upper_gcd = gcd(ans_upper_p, ans_upper_q);
ans_lower_p /= lower_gcd;
ans_lower_q /= lower_gcd;
ans_upper_p /= upper_gcd;
ans_upper_q /= upper_gcd;
cout << ans_upper_p << "/" << ans_upper_q << " " << ans_lower_p << "/"
<< ans_lower_q << endl;
}
return 0;
} | #include <cassert>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const double eps = 1e-11;
bool eq(double a, double b) { return abs(b - a) < eps; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const double inf = 1e30;
int main() {
int p, n;
while (cin >> p >> n) {
if (p == 0 && n == 0)
break;
double rp = sqrt(p);
double min_diff_lower = inf;
double min_diff_upper = inf;
int ans_lower_p, ans_lower_q, ans_upper_p, ans_upper_q;
for (int i = 1; i <= n; ++i) {
int r = n;
int l = 1;
int cnt = 0;
int m;
while (cnt++ < 20) {
m = (r + l) / 2;
if ((double)m / i > rp) {
r = m;
} else {
l = m;
}
}
++m;
if (rp - (double)m / i < 0)
--m;
assert(rp - (double)m / i > 0);
if (min_diff_lower > rp - (double)m / i) {
min_diff_lower = rp - (double)m / i;
ans_lower_p = m;
ans_lower_q = i;
}
for (int j = m + 1; j <= n; ++j) {
if ((double)j / i > rp) {
m = j;
break;
}
}
if (((double)m / i - rp) > 0 && min_diff_upper > ((double)m / i - rp)) {
min_diff_upper = (double)m / i - rp;
ans_upper_p = m;
ans_upper_q = i;
}
}
int lower_gcd = gcd(ans_lower_p, ans_lower_q);
int upper_gcd = gcd(ans_upper_p, ans_upper_q);
ans_lower_p /= lower_gcd;
ans_lower_q /= lower_gcd;
ans_upper_p /= upper_gcd;
ans_upper_q /= upper_gcd;
cout << ans_upper_p << "/" << ans_upper_q << " " << ans_lower_p << "/"
<< ans_lower_q << endl;
}
return 0;
} | replace | 41 | 42 | 41 | 42 | -6 | f650f695-e169-473e-80d1-7169fc5eb39f.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00788/C++/s286752127.cpp:48: int main(): Assertion `rp-(double)m/i>0' failed.
|
p00789 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int dp[301][18];
int solve(int amount, int num) {
// cerr << "solve(" << amount << "," << num << ")" << endl;
if (amount == 0)
return 1;
if (num < 0)
return 0;
int &res = dp[amount][num];
if (res != -1)
return res;
res = 0;
for (int k = num; k >= 1; --k) {
if (amount - k * k >= 0)
res += solve(amount - k * k, k);
}
return res;
}
int main() {
memset(dp, -1, sizeof(dp));
while (true) {
int N;
cin >> N;
cout << solve(N, 17) << endl;
}
} | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int dp[301][18];
int solve(int amount, int num) {
// cerr << "solve(" << amount << "," << num << ")" << endl;
if (amount == 0)
return 1;
if (num < 0)
return 0;
int &res = dp[amount][num];
if (res != -1)
return res;
res = 0;
for (int k = num; k >= 1; --k) {
if (amount - k * k >= 0)
res += solve(amount - k * k, k);
}
return res;
}
int main() {
memset(dp, -1, sizeof(dp));
while (true) {
int N;
cin >> N;
if (N == 0)
break;
cout << solve(N, 17) << endl;
}
} | insert | 28 | 28 | 28 | 30 | TLE | |
p00791 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
char in[1002][1001];
map<int, int> ans;
int N;
inline bool valid(int x, int y) {
return 0 <= x && x < 1001 && 0 <= y && y < N;
}
int getlen_yaxis(int x, int y) {
int top_y = y;
while (top_y >= 0 && in[top_y][x] == '*') {
top_y--;
}
top_y++;
int bottom_y = y;
while (bottom_y < N && in[bottom_y][x] == '*') {
bottom_y++;
}
bottom_y--;
return bottom_y - top_y + 1;
}
int getlen_xaxis(int x, int y) {
int left_x = x;
while (left_x >= 0 && in[y][left_x] == '*') {
left_x--;
}
left_x++;
int right_x = x;
while (right_x < 1001 && in[y][right_x] == '*') {
right_x++;
}
right_x--;
return right_x - left_x + 1;
}
int const dx[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
int const dy[8] = {0, -1, 0, 1, -1, -1, 1, 1};
void move_to_xaxis(int &x, int &y) {
int dir;
if (x - 1 >= 0 && in[y][x - 1] == '*') {
dir = 0;
} else {
dir = 2;
}
bool turn = 0;
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
if (turn && (x + 1 == nx || x - 1 == nx) && y == ny)
return;
x = nx, y = ny;
} else {
rep(k, 8) {
nx = x + dx[k], ny = y + dy[k];
if (x - dx[dir] == nx && y - dy[dir] == ny)
continue;
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
x = nx, y = ny;
dir = k;
turn = 1;
break;
}
}
}
}
}
void move_to_yaxis(int &x, int &y) {
int dir;
if (y - 1 >= 0 && in[y - 1][x] == '*') {
dir = 1;
} else {
dir = 3;
}
bool turn = 0;
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
if (turn && (y + 1 == ny || y - 1 == ny) && x == nx)
return;
x = nx, y = ny;
} else {
rep(k, 8) {
nx = x + dx[k], ny = y + dy[k];
if (x - dx[dir] == nx && y - dy[dir] == ny)
continue;
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
x = nx, y = ny;
dir = k;
turn = 1;
break;
}
}
}
}
}
void calc_area_xaxis(int x, int y) {
int len1 = getlen_xaxis(x, y);
int tmp_y = y;
move_to_xaxis(x, y);
int len2 = getlen_xaxis(x, y);
ans[(len1 + len2) * abs(y - tmp_y + 1) / 2]++;
}
void calc_area_yaxis(int x, int y) {
int len1 = getlen_yaxis(x, y);
int tmp_x = x;
move_to_yaxis(x, y);
int len2 = getlen_yaxis(x, y);
ans[(len1 + len2) * abs(x - tmp_x + 1) / 2]++;
}
void eraser(int x, int y, int dir) {
in[y][x] = ' ';
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
in[ny][nx] = ' ';
x = nx, y = ny;
} else {
bool changed = 0;
rep(k, 8) {
int nx = x + dx[k], ny = y + dy[k];
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
in[ny][nx] = ' ';
x = nx, y = ny;
dir = k;
changed = 1;
break;
}
}
if (!changed) {
rep(k, 8) {
int nx = x + dx[k], ny = y + dy[k];
in[ny][nx] = ' ';
}
break;
}
}
}
}
int main() {
while (cin >> N && N) {
ans.clear();
memset(in, 0, sizeof in);
cin.ignore();
rep(i, N) {
rep(j, 1001) {
char c;
scanf("%c", &c);
if (c == '\n') {
break;
}
in[i][j] = c;
}
}
rep(i, N) {
rep(j, 1001) {
if (in[i][j] == 0)
break;
if (in[i][j] == '*' && (j - 1 >= 0 && in[i][j - 1] == '*')) {
calc_area_xaxis(j, i);
eraser(j, i, 0);
} else if (in[i][j] == '*' && (i - 1 >= 0 && in[i - 1][j] == '*')) {
calc_area_yaxis(j, i);
eraser(j, i, 1);
}
}
}
static bool flag = 0;
if (flag)
cout << "----------\n";
flag = 1;
map<int, int>::iterator it = ans.begin();
for (; it != ans.end(); it++) {
cout << it->first << ' ' << it->second << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
char in[1002][1001];
map<int, int> ans;
int N;
inline bool valid(int x, int y) {
return 0 <= x && x < 1001 && 0 <= y && y < N;
}
int getlen_yaxis(int x, int y) {
int top_y = y;
while (top_y >= 0 && in[top_y][x] == '*') {
top_y--;
}
top_y++;
int bottom_y = y;
while (bottom_y < N && in[bottom_y][x] == '*') {
bottom_y++;
}
bottom_y--;
return bottom_y - top_y + 1;
}
int getlen_xaxis(int x, int y) {
int left_x = x;
while (left_x >= 0 && in[y][left_x] == '*') {
left_x--;
}
left_x++;
int right_x = x;
while (right_x < 1001 && in[y][right_x] == '*') {
right_x++;
}
right_x--;
return right_x - left_x + 1;
}
int const dx[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
int const dy[8] = {0, -1, 0, 1, -1, -1, 1, 1};
void move_to_xaxis(int &x, int &y) {
int dir;
if (x - 1 >= 0 && in[y][x - 1] == '*') {
dir = 0;
} else {
dir = 2;
}
bool turn = 0;
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
if (turn && (x + 1 == nx || x - 1 == nx) && y == ny)
return;
x = nx, y = ny;
} else {
rep(k, 8) {
nx = x + dx[k], ny = y + dy[k];
if (x - dx[dir] == nx && y - dy[dir] == ny)
continue;
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
x = nx, y = ny;
dir = k;
turn = 1;
break;
}
}
}
}
}
void move_to_yaxis(int &x, int &y) {
int dir;
if (y - 1 >= 0 && in[y - 1][x] == '*') {
dir = 1;
} else {
dir = 3;
}
bool turn = 0;
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
if (turn && (y + 1 == ny || y - 1 == ny) && x == nx)
return;
x = nx, y = ny;
} else {
rep(k, 8) {
nx = x + dx[k], ny = y + dy[k];
if (x - dx[dir] == nx && y - dy[dir] == ny)
continue;
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
x = nx, y = ny;
dir = k;
turn = 1;
break;
}
}
}
}
}
void calc_area_xaxis(int x, int y) {
int len1 = getlen_xaxis(x, y);
int tmp_y = y;
move_to_xaxis(x, y);
int len2 = getlen_xaxis(x, y);
ans[(len1 + len2) * abs(y - tmp_y + 1) / 2]++;
}
void calc_area_yaxis(int x, int y) {
int len1 = getlen_yaxis(x, y);
int tmp_x = x;
move_to_yaxis(x, y);
int len2 = getlen_yaxis(x, y);
ans[(len1 + len2) * abs(x - tmp_x + 1) / 2]++;
}
void eraser(int x, int y, int dir) {
in[y][x] = ' ';
while (1) {
int nx = x + dx[dir], ny = y + dy[dir];
if (valid(nx, ny) && in[ny][nx] == '*') {
in[ny][nx] = ' ';
x = nx, y = ny;
} else {
bool changed = 0;
rep(k, 8) {
int nx = x + dx[k], ny = y + dy[k];
if (valid(nx, ny) && in[ny][nx] == '*' &&
valid(nx + dx[k], ny + dy[k]) &&
in[ny + dy[k]][nx + dx[k]] == '*') {
in[ny][nx] = ' ';
x = nx, y = ny;
dir = k;
changed = 1;
break;
}
}
if (!changed) {
rep(k, 8) {
int nx = x + dx[k], ny = y + dy[k];
if (!valid(nx, ny))
continue;
in[ny][nx] = ' ';
}
break;
}
}
}
}
int main() {
while (cin >> N && N) {
ans.clear();
memset(in, 0, sizeof in);
cin.ignore();
rep(i, N) {
rep(j, 1001) {
char c;
scanf("%c", &c);
if (c == '\n') {
break;
}
in[i][j] = c;
}
}
rep(i, N) {
rep(j, 1001) {
if (in[i][j] == 0)
break;
if (in[i][j] == '*' && (j - 1 >= 0 && in[i][j - 1] == '*')) {
calc_area_xaxis(j, i);
eraser(j, i, 0);
} else if (in[i][j] == '*' && (i - 1 >= 0 && in[i - 1][j] == '*')) {
calc_area_yaxis(j, i);
eraser(j, i, 1);
}
}
}
static bool flag = 0;
if (flag)
cout << "----------\n";
flag = 1;
map<int, int>::iterator it = ans.begin();
for (; it != ans.end(); it++) {
cout << it->first << ' ' << it->second << endl;
}
}
return 0;
} | insert | 161 | 161 | 161 | 163 | -11 | |
p00791 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-10)
using namespace std;
bool used[80][1000];
int dx[] = {-1, +0, +1, +1, +1, +0, -1, -1};
int dy[] = {+1, +1, +1, +0, -1, -1, -1, +0};
class Point {
public:
double x, y;
Point(double x = -1, double y = -1) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); } // ※イケメンに限る
bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; }
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1))
: p1(p1), p2(p2) {}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
void dfs(int x, int y, vector<Point> &vec, vector<string> &G, int H) {
if (used[y][x])
return;
used[y][x] = true;
vec.push_back(Point(x, y));
rep(i, 8) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!(0 <= ny && ny < H && 0 <= nx && nx < G[ny].size()))
continue;
if (G[ny][nx] == '*' && !used[ny][nx]) {
dfs(nx, ny, vec, G, H);
return;
}
}
}
int calc(vector<Point> &vec) {
int res = 0;
// cout << "vec[] = " << vec.size() << endl;
rep(i, vec.size()) {
int j, cnt = 0;
for (j = i; j < vec.size() && vec[j].y == vec[i].y; j++, cnt++)
;
assert(cnt >= 2);
if (cnt == 2)
res += vec[i + 1].x - vec[i].x + 1;
else
res += cnt;
i = j - 1;
}
return res;
}
int main() {
int n;
bool f = true;
while (cin >> n, n) {
cin.ignore();
vector<string> G(n);
string input;
rep(i, n) {
getline(cin, input);
G[i] = input;
}
rep(i, n) rep(j, G[i].size()) used[i][j] = false;
map<int, int> ans;
rep(i, n) {
rep(j, G[i].size()) {
if (G[i][j] != '*')
continue;
if (used[i][j])
continue;
vector<Point> vec;
dfs(j, i, vec, G, n);
/*
rep(k,vec.size())
cout << vec[k].x << "," << vec[k].y << endl;
cout << endl;
*/
sort(vec.begin(), vec.end());
int area = calc(vec);
ans[area]++;
}
}
if (f)
f = false;
else
cout << "----------" << endl;
for (map<int, int>::iterator it = ans.begin(); it != ans.end(); it++)
cout << it->first << " " << it->second << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-10)
using namespace std;
bool used[1000][80];
int dx[] = {-1, +0, +1, +1, +1, +0, -1, -1};
int dy[] = {+1, +1, +1, +0, -1, -1, -1, +0};
class Point {
public:
double x, y;
Point(double x = -1, double y = -1) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double a) { return Point(a * x, a * y); }
Point operator/(double a) { return Point(x / a, y / a); } // ※イケメンに限る
bool operator<(const Point &p) const { return y != p.y ? y < p.y : x < p.x; }
bool operator==(const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
struct Segment {
Point p1, p2;
Segment(Point p1 = Point(-1, -1), Point p2 = Point(-1, -1))
: p1(p1), p2(p2) {}
};
typedef Point Vector;
typedef Segment Line;
typedef vector<Point> Polygon;
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
double norm(Point a) { return a.x * a.x + a.y * a.y; }
void dfs(int x, int y, vector<Point> &vec, vector<string> &G, int H) {
if (used[y][x])
return;
used[y][x] = true;
vec.push_back(Point(x, y));
rep(i, 8) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!(0 <= ny && ny < H && 0 <= nx && nx < G[ny].size()))
continue;
if (G[ny][nx] == '*' && !used[ny][nx]) {
dfs(nx, ny, vec, G, H);
return;
}
}
}
int calc(vector<Point> &vec) {
int res = 0;
// cout << "vec[] = " << vec.size() << endl;
rep(i, vec.size()) {
int j, cnt = 0;
for (j = i; j < vec.size() && vec[j].y == vec[i].y; j++, cnt++)
;
assert(cnt >= 2);
if (cnt == 2)
res += vec[i + 1].x - vec[i].x + 1;
else
res += cnt;
i = j - 1;
}
return res;
}
int main() {
int n;
bool f = true;
while (cin >> n, n) {
cin.ignore();
vector<string> G(n);
string input;
rep(i, n) {
getline(cin, input);
G[i] = input;
}
rep(i, n) rep(j, G[i].size()) used[i][j] = false;
map<int, int> ans;
rep(i, n) {
rep(j, G[i].size()) {
if (G[i][j] != '*')
continue;
if (used[i][j])
continue;
vector<Point> vec;
dfs(j, i, vec, G, n);
/*
rep(k,vec.size())
cout << vec[k].x << "," << vec[k].y << endl;
cout << endl;
*/
sort(vec.begin(), vec.end());
int area = calc(vec);
ans[area]++;
}
}
if (f)
f = false;
else
cout << "----------" << endl;
for (map<int, int>::iterator it = ans.begin(); it != ans.end(); it++)
cout << it->first << " " << it->second << endl;
}
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00794 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
int W, H, div_row[4] = {-1, 0, 0, 1}, div_col[5] = {0, -1, 1, 0};
struct Info {
Info() { eat_after = row = col = hp = time = 0; }
void set(int arg_row, int arg_col, int arg_hp, int arg_time,
int arg_eat_after) {
row = arg_row;
col = arg_col;
hp = arg_hp;
time = arg_time;
eat_after = arg_eat_after;
}
int row, col, hp, time, eat_after;
short check[8][8];
};
bool rangeCheck(int row, int col) {
if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
return true;
else {
return false;
}
}
void func() {
int map[H][W];
int start_row, start_col, goal_row, goal_col;
for (int i = 0; i < H; i++) {
for (int k = 0; k < W; k++) {
scanf("%d", &map[i][k]);
if (map[i][k] == 2) {
start_row = i;
start_col = k;
map[i][k] = 1;
} else if (map[i][k] == 3) {
goal_row = i;
goal_col = k;
}
}
}
int ans = -1, limit = 2 * H * W;
queue<Info> Q;
Info current;
Info first;
for (int i = 0; i < H; i++) {
for (int k = 0; k < W; k++)
first.check[i][k] = 0;
}
first.check[start_row][start_col] = 1;
first.set(start_row, start_col, 6, 0, 3);
Q.push(first);
int debug = 0;
while (!Q.empty()) {
current = Q.front();
Q.pop();
debug++;
if (debug > 300000) {
break;
}
if (current.hp == 0)
continue;
if (map[current.row][current.col] == 3) {
ans = current.time;
break;
} else if (map[current.row][current.col] == 4) {
current.hp = 6;
current.eat_after = 0;
}
if (current.time + abs(current.row - goal_row) +
abs(current.col - goal_col) >
limit)
continue;
for (int i = 0; i < 4; i++) {
if (rangeCheck(current.row + div_row[i], current.col + div_col[i]) ==
true &&
map[current.row + div_row[i]][current.col + div_col[i]] != 0 &&
((current.check[current.row + div_row[i]][current.col + div_col[i]] ==
0) ||
(current.check[current.row + div_row[i]][current.col + div_col[i]] ==
1 &&
current.eat_after <= 1))) {
Info new_info;
for (int a = 0; a < H; a++) {
for (int b = 0; b < W; b++) {
new_info.check[a][b] = current.check[a][b];
}
}
new_info.check[current.row + div_row[i]][current.col + div_col[i]]++;
new_info.set(current.row + div_row[i], current.col + div_col[i],
current.hp - 1, current.time + 1, current.eat_after + 1);
Q.push(new_info);
}
}
}
printf("%d\n", ans);
}
int main() {
while (true) {
scanf("%d %d", &W, &H);
if (W == 0 && H == 0)
break;
func();
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
int W, H, div_row[4] = {-1, 0, 0, 1}, div_col[5] = {0, -1, 1, 0};
struct Info {
Info() { eat_after = row = col = hp = time = 0; }
void set(int arg_row, int arg_col, int arg_hp, int arg_time,
int arg_eat_after) {
row = arg_row;
col = arg_col;
hp = arg_hp;
time = arg_time;
eat_after = arg_eat_after;
}
int row, col, hp, time, eat_after;
short check[8][8];
};
bool rangeCheck(int row, int col) {
if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
return true;
else {
return false;
}
}
void func() {
int map[H][W];
int start_row, start_col, goal_row, goal_col;
for (int i = 0; i < H; i++) {
for (int k = 0; k < W; k++) {
scanf("%d", &map[i][k]);
if (map[i][k] == 2) {
start_row = i;
start_col = k;
map[i][k] = 1;
} else if (map[i][k] == 3) {
goal_row = i;
goal_col = k;
}
}
}
int ans = -1, limit = 2 * H * W;
queue<Info> Q;
Info current;
Info first;
for (int i = 0; i < H; i++) {
for (int k = 0; k < W; k++)
first.check[i][k] = 0;
}
first.check[start_row][start_col] = 1;
first.set(start_row, start_col, 6, 0, 3);
Q.push(first);
int debug = 0;
while (!Q.empty()) {
current = Q.front();
Q.pop();
debug++;
if (debug > 250000) {
break;
}
if (current.hp == 0)
continue;
if (map[current.row][current.col] == 3) {
ans = current.time;
break;
} else if (map[current.row][current.col] == 4) {
current.hp = 6;
current.eat_after = 0;
}
if (current.time + abs(current.row - goal_row) +
abs(current.col - goal_col) >
limit)
continue;
for (int i = 0; i < 4; i++) {
if (rangeCheck(current.row + div_row[i], current.col + div_col[i]) ==
true &&
map[current.row + div_row[i]][current.col + div_col[i]] != 0 &&
((current.check[current.row + div_row[i]][current.col + div_col[i]] ==
0) ||
(current.check[current.row + div_row[i]][current.col + div_col[i]] ==
1 &&
current.eat_after <= 1))) {
Info new_info;
for (int a = 0; a < H; a++) {
for (int b = 0; b < W; b++) {
new_info.check[a][b] = current.check[a][b];
}
}
new_info.check[current.row + div_row[i]][current.col + div_col[i]]++;
new_info.set(current.row + div_row[i], current.col + div_col[i],
current.hp - 1, current.time + 1, current.eat_after + 1);
Q.push(new_info);
}
}
}
printf("%d\n", ans);
}
int main() {
while (true) {
scanf("%d %d", &W, &H);
if (W == 0 && H == 0)
break;
func();
}
return 0;
} | replace | 80 | 81 | 80 | 81 | MLE | |
p00795 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string str, pat;
vector<char> num;
str.reserve(1000001);
num.reserve(1000001);
pat.reserve(73);
ios::sync_with_stdio(false);
cin.tie(0);
while (true) {
str.clear();
pat.clear();
while (true) {
getline(cin, pat);
if (pat.empty() || !cin)
break;
str += pat;
}
if (!cin)
break;
getline(cin, pat);
cin.ignore();
sort(pat.begin(), pat.end());
pat.erase(unique(pat.begin(), pat.end()), pat.end());
num.resize(str.size());
int j;
for (j = 0; j < str.size(); ++j) {
int x = lower_bound(pat.begin(), pat.end(), str[j]) - pat.begin();
if (x < pat.size() && str[j] == pat[x]) {
num[j] = x;
} else {
num[j] = pat.size();
}
}
int cnt[73] = {};
cnt[pat.size()] = 10000000;
int d = 0;
for (j = 0; j < num.size() && d < pat.size(); ++j) {
if (++cnt[num[j]] == 1) {
++d;
}
}
if (d < pat.size()) {
cout << "0\n\n";
continue;
}
int a = 0, b = j;
int shortest = 1;
for (int i = 0; j <= num.size();) {
char x = num[i++];
if (--cnt[x] == 0) {
bool found = false;
while (j < num.size()) {
++cnt[num[j]];
if (x == num[j++]) {
found = true;
break;
}
}
if (!found)
break;
}
if (b - a > j - i) {
a = i;
b = j;
shortest = 1;
} else if (b - a == j - i) {
++shortest;
}
}
cout << shortest << "\n\n";
while (a < b) {
int len = min(b - a, 72);
cout << str.substr(a, len) << '\n';
a += len;
}
cout << '\n';
}
} | #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string str, pat;
vector<char> num;
str.reserve(1000001);
num.reserve(1000001);
pat.reserve(73);
ios::sync_with_stdio(false);
cin.tie(0);
while (true) {
str.clear();
pat.clear();
while (true) {
getline(cin, pat);
if (pat.empty() || !cin)
break;
str += pat;
}
if (str.empty() || !cin)
break;
getline(cin, pat);
cin.ignore();
sort(pat.begin(), pat.end());
pat.erase(unique(pat.begin(), pat.end()), pat.end());
num.resize(str.size());
int j;
for (j = 0; j < str.size(); ++j) {
int x = lower_bound(pat.begin(), pat.end(), str[j]) - pat.begin();
if (x < pat.size() && str[j] == pat[x]) {
num[j] = x;
} else {
num[j] = pat.size();
}
}
int cnt[73] = {};
cnt[pat.size()] = 10000000;
int d = 0;
for (j = 0; j < num.size() && d < pat.size(); ++j) {
if (++cnt[num[j]] == 1) {
++d;
}
}
if (d < pat.size()) {
cout << "0\n\n";
continue;
}
int a = 0, b = j;
int shortest = 1;
for (int i = 0; j <= num.size();) {
char x = num[i++];
if (--cnt[x] == 0) {
bool found = false;
while (j < num.size()) {
++cnt[num[j]];
if (x == num[j++]) {
found = true;
break;
}
}
if (!found)
break;
}
if (b - a > j - i) {
a = i;
b = j;
shortest = 1;
} else if (b - a == j - i) {
++shortest;
}
}
cout << shortest << "\n\n";
while (a < b) {
int len = min(b - a, 72);
cout << str.substr(a, len) << '\n';
a += len;
}
cout << '\n';
}
} | replace | 27 | 28 | 27 | 28 | 0 | |
p00798 | C++ | Memory Limit Exceeded | #include <iostream>
#include <queue>
using namespace std;
int w, h, mp[8][8];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct po {
int x, y, xx, yy;
};
po mk(int a, int b, int c, int d) {
po res;
res.x = a, res.y = b, res.xx = c, res.yy = d;
return res;
}
int bfs(po start, po goal, int flg) {
int dis[8][8] = {}, visited[8][8][8][8] = {};
queue<po> Q;
Q.push(mk(start.x, start.y, start.xx, start.yy));
while (!Q.empty()) {
po t = Q.front();
Q.pop();
visited[t.x][t.y][t.xx][t.yy] = 1;
if (t.x == goal.x && t.y == goal.y)
return dis[t.y][t.x];
for (int i = 0; i < 4; i++) {
int nx = t.x + dx[i], ny = t.y + dy[i];
int mx = t.x - dx[i], my = t.y - dy[i];
po nstart = mk(t.xx, t.yy, t.x, t.y);
po ngoal = mk(mx, my, 0, 0);
if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx] == 1)
continue;
if ((flg == 0) &&
(mx < 0 || my < 0 || mx >= w || my >= h || mp[my][mx] == 1 ||
visited[ny][nx][my][mx] == 1 || (bfs(nstart, ngoal, 1)) == -1))
continue;
if (flg == 1 && (dis[ny][nx] != 0 || (start.yy == ny && start.xx == nx)))
continue;
if (flg == 1)
mx = my = 0;
Q.push(mk(nx, ny, t.x, t.y));
dis[ny][nx] = dis[t.y][t.x] + 1;
}
}
return -1;
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
po start, goal;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
cin >> mp[i][j];
if (mp[i][j] == 3)
goal.x = j, goal.y = i, mp[i][j] = 0;
if (mp[i][j] == 2)
start.x = j, start.y = i, mp[i][j] = 0;
if (mp[i][j] == 4)
start.xx = j, start.yy = i, mp[i][j] = 0;
}
cout << bfs(start, goal, 0) << endl;
}
return 0;
} | #include <iostream>
#include <queue>
using namespace std;
int w, h, mp[8][8];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
struct po {
int x, y, xx, yy;
};
po mk(int a, int b, int c, int d) {
po res;
res.x = a, res.y = b, res.xx = c, res.yy = d;
return res;
}
int bfs(po start, po goal, int flg) {
int dis[8][8] = {}, visited[8][8][8][8] = {};
queue<po> Q;
Q.push(mk(start.x, start.y, start.xx, start.yy));
while (!Q.empty()) {
po t = Q.front();
Q.pop();
visited[t.x][t.y][t.xx][t.yy] = 1;
if (t.x == goal.x && t.y == goal.y)
return dis[t.y][t.x];
for (int i = 0; i < 4; i++) {
int nx = t.x + dx[i], ny = t.y + dy[i];
int mx = t.x - dx[i], my = t.y - dy[i];
po nstart = mk(t.xx, t.yy, t.x, t.y);
po ngoal = mk(mx, my, 0, 0);
if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx] == 1)
continue;
if ((flg == 0) &&
(mx < 0 || my < 0 || mx >= w || my >= h || mp[my][mx] == 1 ||
visited[nx][ny][t.x][t.y] == 1 || (bfs(nstart, ngoal, 1)) == -1))
continue;
if (flg == 1 && (dis[ny][nx] != 0 || (start.yy == ny && start.xx == nx)))
continue;
if (flg == 1)
mx = my = 0;
Q.push(mk(nx, ny, t.x, t.y));
dis[ny][nx] = dis[t.y][t.x] + 1;
}
}
return -1;
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
po start, goal;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
cin >> mp[i][j];
if (mp[i][j] == 3)
goal.x = j, goal.y = i, mp[i][j] = 0;
if (mp[i][j] == 2)
start.x = j, start.y = i, mp[i][j] = 0;
if (mp[i][j] == 4)
start.xx = j, start.yy = i, mp[i][j] = 0;
}
cout << bfs(start, goal, 0) << endl;
}
return 0;
} | replace | 36 | 37 | 36 | 37 | MLE | |
p00806 | C++ | Time Limit Exceeded | #include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
void dfs(int i, int n, const string dic[100], const bool b[300][100],
vector<string> &tmp, vector<string> &ans) {
if (i < -1)
for (;;)
;
if (i == -1) {
string res;
for (int j = (int)tmp.size() - 1; j >= 0; j--) {
res += tmp[j];
if (j > 0)
res += ' ';
else
res += '.';
}
ans.push_back(res);
}
rep(j, n) if (b[i][j]) {
int m = dic[j].length();
tmp.push_back(dic[j]);
dfs(i - m, n, dic, b, tmp, ans);
tmp.pop_back();
}
}
int main() {
char f[128];
f['a'] = '2';
f['b'] = '2';
f['c'] = '2';
f['d'] = '3';
f['e'] = '3';
f['f'] = '3';
f['g'] = '4';
f['h'] = '4';
f['i'] = '4';
f['j'] = '5';
f['k'] = '5';
f['l'] = '5';
f['m'] = '6';
f['n'] = '6';
f['o'] = '6';
f['p'] = '7';
f['q'] = '7';
f['r'] = '7';
f['s'] = '7';
f['t'] = '8';
f['u'] = '8';
f['v'] = '8';
f['w'] = '9';
f['x'] = '9';
f['y'] = '9';
f['z'] = '9';
for (int n; cin >> n, n;) {
string dic[100];
rep(i, n) cin >> dic[i];
string seq;
cin >> seq;
string num[100];
rep(i, n) rep(j, dic[i].length()) num[i] += f[dic[i][j]];
bool b[300][100] =
{}; // b[i][j] := ( num[j] が seq[0..i] の suffix かどうか )
int cnt[300] = {};
rep(i, seq.length()) {
rep(j, n) {
int m = num[j].length();
if (m <= i + 1 && num[j] == seq.substr(i - m + 1, m) &&
(i - m == -1 || cnt[i - m] > 0)) {
b[i][j] = true;
cnt[i]++;
}
}
}
vector<string> ans, tmp;
dfs(seq.length() - 1, n, dic, b, tmp, ans);
rep(i, ans.size()) cout << ans[i] << endl;
cout << "--" << endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
void dfs(int i, int n, const string dic[100], const bool b[300][100],
vector<string> &tmp, vector<string> &ans) {
if (i < -1)
return;
if (i == -1) {
string res;
for (int j = (int)tmp.size() - 1; j >= 0; j--) {
res += tmp[j];
if (j > 0)
res += ' ';
else
res += '.';
}
ans.push_back(res);
}
rep(j, n) if (b[i][j]) {
int m = dic[j].length();
tmp.push_back(dic[j]);
dfs(i - m, n, dic, b, tmp, ans);
tmp.pop_back();
}
}
int main() {
char f[128];
f['a'] = '2';
f['b'] = '2';
f['c'] = '2';
f['d'] = '3';
f['e'] = '3';
f['f'] = '3';
f['g'] = '4';
f['h'] = '4';
f['i'] = '4';
f['j'] = '5';
f['k'] = '5';
f['l'] = '5';
f['m'] = '6';
f['n'] = '6';
f['o'] = '6';
f['p'] = '7';
f['q'] = '7';
f['r'] = '7';
f['s'] = '7';
f['t'] = '8';
f['u'] = '8';
f['v'] = '8';
f['w'] = '9';
f['x'] = '9';
f['y'] = '9';
f['z'] = '9';
for (int n; cin >> n, n;) {
string dic[100];
rep(i, n) cin >> dic[i];
string seq;
cin >> seq;
string num[100];
rep(i, n) rep(j, dic[i].length()) num[i] += f[dic[i][j]];
bool b[300][100] =
{}; // b[i][j] := ( num[j] が seq[0..i] の suffix かどうか )
int cnt[300] = {};
rep(i, seq.length()) {
rep(j, n) {
int m = num[j].length();
if (m <= i + 1 && num[j] == seq.substr(i - m + 1, m) &&
(i - m == -1 || cnt[i - m] > 0)) {
b[i][j] = true;
cnt[i]++;
}
}
}
vector<string> ans, tmp;
dfs(seq.length() - 1, n, dic, b, tmp, ans);
rep(i, ans.size()) cout << ans[i] << endl;
cout << "--" << endl;
}
return 0;
} | replace | 11 | 13 | 11 | 12 | TLE | |
p00810 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#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;
static const double EPS = 1e-8;
class Point {
public:
double _x;
double _y;
double _z;
Point(double x, double y, double z) : _x(x), _y(y), _z(z) {}
Point operator-(const Point &p) const {
return Point(_x - p._x, _y - p._y, _z - p._z);
}
Point operator+(const Point &p) const {
return Point(_x + p._x, _y + p._y, _z + p._z);
}
void operator+=(const Point &p) const {
_x + p._x;
_y + p._y;
_z + p._z;
}
Point operator*(const double t) const {
return Point(_x * t, _y * t, _z * t);
}
Point operator*(const Point &p) const {
return Point(_x * p._x, _y * p._y, _z * p._z);
}
Point operator/(const double t) const {
return Point(_x / t, _y / t, _z / t);
}
void operator/=(const double t) {
_x /= t;
_y /= t;
_z /= t;
}
void print_vec() const { printf("(%lf,%lf,%lf)\n", _x, _y, _z); }
};
class Line : public vector<Point> {
public:
Line(const Point &p1, const Point &p2) {
push_back(p1);
push_back(p2);
}
};
double norm(const Point &p) {
return sqrt(p._x * p._x + p._y * p._y + p._z * p._z);
}
Point unit(const Point &p) { return p / norm(p); }
double dot(const Point &p1, const Point &p2) {
return p1._x * p2._x + p1._y * p2._y + p1._z * p2._z;
}
Point cross(const Point &p1, const Point &p2) {
return Point(p1._y * p2._z - p1._z * p2._y, p1._z * p2._x - p1._x * p2._z,
p1._x * p2._y - p1._y * p2._x);
}
Point projection(const Line &l, const Point &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + unit(l[0] - l[1]) * t;
}
bool EQ(const Point &s, const Point &t) {
if ((t._x - EPS <= s._x && s._x <= t._x + EPS) &&
(t._y - EPS <= s._y && s._y <= t._y + EPS) &&
(t._z - EPS <= s._z && s._z <= t._z + EPS))
return true;
return false;
}
bool parallelLL(const Line &l, const Line &m) {
return EQ(cross(l[1] - l[0], m[1] - m[0]), Point(0, 0, 0));
}
bool intersectLP(const Line &l, const Point &p) {
return (norm(cross(l[1] - p, l[0] - p)) < EPS);
}
double distanceLP(const Line &l, const Point &p) {
if (intersectLP(l, p))
return 0;
return norm(p - projection(l, p));
}
double distanceLL(const Line &l, const Line &m) {
if (parallelLL(l, m))
return distanceLP(l, m[0]);
const Point V1 = l[1] - l[0];
const Point V2 = m[1] - m[0];
const Point V3 = m[0] - l[0];
return abs(dot(cross(V1, V2), V3) / norm(cross(V1, V2)));
}
double distancePP(const Point &s, const Point &t) {
if (EQ(s, t))
return 0;
return norm(Point(s._x - t._x, s._y - t._y, s._z - t._z));
}
const int sign[8][3] = {{+1, +1, +1}, {+1, +1, -1}, {+1, -1, +1}, {+1, -1, -1},
{-1, +1, +1}, {-1, +1, -1}, {-1, -1, +1}, {-1, -1, -1}};
class Cube {
public:
Point _p;
double _r;
Cube(Point p, double r) : _p(p), _r(r) {}
bool operator<(const Cube &c) const { return _r < c._r; }
bool operator>(const Cube &c) const { return _r > c._r; }
};
int main() {
int num_of_stars;
while (~scanf("%d", &num_of_stars)) {
if (num_of_stars == 0)
break;
vector<Point> stars;
Point center(0, 0, 0);
for (int star_i = 0; star_i < num_of_stars; star_i++) {
double x, y, z;
scanf("%lf %lf %lf", &x, &y, &z);
stars.push_back(Point(x, y, z));
}
priority_queue<Cube, vector<Cube>, greater<Cube>> candidates;
candidates.push(Cube(Point(50.0, 50.0, 50.0), 1000.0));
double min_radius = 1e15;
double size = 100.0;
while (size > 0.000002) {
size = size / 2.0;
priority_queue<Cube, vector<Cube>, greater<Cube>> next;
while (!candidates.empty()) {
for (int sign_i = 0; sign_i < 8; sign_i++) {
Cube cube = candidates.top();
cube._p._x += sign[sign_i][0] * size / 2.0;
cube._p._y += sign[sign_i][1] * size / 2.0;
cube._p._z += sign[sign_i][2] * size / 2.0;
double r = 0.0;
for (int star_i = 0; star_i < stars.size(); star_i++) {
r = max(r, distancePP(cube._p, stars[star_i]));
}
if (r < min_radius) {
min_radius = r;
}
if (min_radius <= r - sqrt(3.0) * size / 2.0) {
continue;
}
if (next.size() < 100000)
next.push(Cube(cube._p, r));
}
candidates.pop();
}
candidates = next;
}
printf("%.5lf\n", min_radius);
}
} | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#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;
static const double EPS = 1e-8;
class Point {
public:
double _x;
double _y;
double _z;
Point(double x, double y, double z) : _x(x), _y(y), _z(z) {}
Point operator-(const Point &p) const {
return Point(_x - p._x, _y - p._y, _z - p._z);
}
Point operator+(const Point &p) const {
return Point(_x + p._x, _y + p._y, _z + p._z);
}
void operator+=(const Point &p) const {
_x + p._x;
_y + p._y;
_z + p._z;
}
Point operator*(const double t) const {
return Point(_x * t, _y * t, _z * t);
}
Point operator*(const Point &p) const {
return Point(_x * p._x, _y * p._y, _z * p._z);
}
Point operator/(const double t) const {
return Point(_x / t, _y / t, _z / t);
}
void operator/=(const double t) {
_x /= t;
_y /= t;
_z /= t;
}
void print_vec() const { printf("(%lf,%lf,%lf)\n", _x, _y, _z); }
};
class Line : public vector<Point> {
public:
Line(const Point &p1, const Point &p2) {
push_back(p1);
push_back(p2);
}
};
double norm(const Point &p) {
return sqrt(p._x * p._x + p._y * p._y + p._z * p._z);
}
Point unit(const Point &p) { return p / norm(p); }
double dot(const Point &p1, const Point &p2) {
return p1._x * p2._x + p1._y * p2._y + p1._z * p2._z;
}
Point cross(const Point &p1, const Point &p2) {
return Point(p1._y * p2._z - p1._z * p2._y, p1._z * p2._x - p1._x * p2._z,
p1._x * p2._y - p1._y * p2._x);
}
Point projection(const Line &l, const Point &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + unit(l[0] - l[1]) * t;
}
bool EQ(const Point &s, const Point &t) {
if ((t._x - EPS <= s._x && s._x <= t._x + EPS) &&
(t._y - EPS <= s._y && s._y <= t._y + EPS) &&
(t._z - EPS <= s._z && s._z <= t._z + EPS))
return true;
return false;
}
bool parallelLL(const Line &l, const Line &m) {
return EQ(cross(l[1] - l[0], m[1] - m[0]), Point(0, 0, 0));
}
bool intersectLP(const Line &l, const Point &p) {
return (norm(cross(l[1] - p, l[0] - p)) < EPS);
}
double distanceLP(const Line &l, const Point &p) {
if (intersectLP(l, p))
return 0;
return norm(p - projection(l, p));
}
double distanceLL(const Line &l, const Line &m) {
if (parallelLL(l, m))
return distanceLP(l, m[0]);
const Point V1 = l[1] - l[0];
const Point V2 = m[1] - m[0];
const Point V3 = m[0] - l[0];
return abs(dot(cross(V1, V2), V3) / norm(cross(V1, V2)));
}
double distancePP(const Point &s, const Point &t) {
if (EQ(s, t))
return 0;
return norm(Point(s._x - t._x, s._y - t._y, s._z - t._z));
}
const int sign[8][3] = {{+1, +1, +1}, {+1, +1, -1}, {+1, -1, +1}, {+1, -1, -1},
{-1, +1, +1}, {-1, +1, -1}, {-1, -1, +1}, {-1, -1, -1}};
class Cube {
public:
Point _p;
double _r;
Cube(Point p, double r) : _p(p), _r(r) {}
bool operator<(const Cube &c) const { return _r < c._r; }
bool operator>(const Cube &c) const { return _r > c._r; }
};
int main() {
int num_of_stars;
while (~scanf("%d", &num_of_stars)) {
if (num_of_stars == 0)
break;
vector<Point> stars;
Point center(0, 0, 0);
for (int star_i = 0; star_i < num_of_stars; star_i++) {
double x, y, z;
scanf("%lf %lf %lf", &x, &y, &z);
stars.push_back(Point(x, y, z));
}
priority_queue<Cube, vector<Cube>, greater<Cube>> candidates;
candidates.push(Cube(Point(50.0, 50.0, 50.0), 1000.0));
double min_radius = 1e15;
double size = 100.0;
while (size > 0.000002) {
size = size / 2.0;
priority_queue<Cube, vector<Cube>, greater<Cube>> next;
while (!candidates.empty()) {
for (int sign_i = 0; sign_i < 8; sign_i++) {
Cube cube = candidates.top();
cube._p._x += sign[sign_i][0] * size / 2.0;
cube._p._y += sign[sign_i][1] * size / 2.0;
cube._p._z += sign[sign_i][2] * size / 2.0;
double r = 0.0;
for (int star_i = 0; star_i < stars.size(); star_i++) {
r = max(r, distancePP(cube._p, stars[star_i]));
}
if (r < min_radius) {
min_radius = r;
}
if (min_radius <= r - sqrt(3.0) * size / 2.0) {
continue;
}
if (next.size() < 50000)
next.push(Cube(cube._p, r));
}
candidates.pop();
}
candidates = next;
}
printf("%.5lf\n", min_radius);
}
} | replace | 184 | 185 | 184 | 185 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;
using namespace std;
template <class t> void vin(vector<t> &v, int n) {
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
}
const int MAX_N = 100001;
int prime[MAX_N];
bool is_prime[MAX_N + 1];
void eratos(int n) {
int p = 0;
for (int i = 0; i <= n; ++i)
is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; ++i) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i)
is_prime[j] = false;
}
}
}
int main() {
eratos(MAX_N);
double m, a, b;
while (cin >> m >> a >> b and (m or a or b)) {
double best = 0, P = 0, Q = 0;
for (double p = 2; p <= m; ++p) {
if (!is_prime[(int)p])
continue;
for (double q = 2; q <= m; ++q) {
if (!is_prime[(int)q])
continue;
if (a / b > p / q or p / q > 1.0)
continue;
if (p * q > m)
continue;
if (best < p * q) {
best = p * q;
P = p;
Q = q;
}
}
}
cout << P << ' ' << Q << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<ll, ll>;
using namespace std;
template <class t> void vin(vector<t> &v, int n) {
v.resize(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
}
const int MAX_N = 100001;
int prime[MAX_N];
bool is_prime[MAX_N + 1];
void eratos(int n) {
int p = 0;
for (int i = 0; i <= n; ++i)
is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; ++i) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i)
is_prime[j] = false;
}
}
}
int main() {
eratos(MAX_N);
double m, a, b;
while (cin >> m >> a >> b and (m or a or b)) {
double best = 0, P = 0, Q = 0;
for (double p = 2; p <= m; ++p) {
if (!is_prime[(int)p])
continue;
int qq = m / p;
for (double q = qq; q >= 2; --q) {
if (is_prime[(int)q]) {
if (a / b > p / q or p / q > 1.0)
continue;
if (best < p * q) {
best = p * q;
P = p;
Q = q;
}
break;
}
}
}
cout << P << ' ' << Q << endl;
}
return 0;
}
| replace | 41 | 52 | 41 | 52 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
bool isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
vector<ll> ps;
for (int i = 2; i <= 100000; i++) {
if (isPrime(i))
ps.push_back(i);
}
ll m, a, b;
while (cin >> m >> a >> b, m) {
ll mx = -1, mp = -1, mq = -1;
for (int i = 0; i < ps.size(); i++) {
for (int j = i; j < ps.size(); j++) {
ll p = ps[i], q = ps[j];
if (p * q <= m && a * q <= b * p) {
if (mx < p * q) {
mx = p * q;
mp = p;
mq = q;
}
}
}
}
cout << mp << " " << mq << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
bool isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
vector<ll> ps;
for (int i = 2; i <= 100000; i++) {
if (isPrime(i))
ps.push_back(i);
}
ll m, a, b;
while (cin >> m >> a >> b, m) {
ll mx = -1, mp = -1, mq = -1;
for (int i = 0; i < ps.size(); i++) {
for (int j = i; j < ps.size(); j++) {
ll p = ps[i], q = ps[j];
if (p * q <= m && a * q <= b * p) {
if (mx < p * q) {
mx = p * q;
mp = p;
mq = q;
}
} else
break;
}
}
cout << mp << " " << mq << endl;
}
} | replace | 41 | 42 | 41 | 43 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int m, a, b;
bool prime[100001];
vector<int> pp;
int main(void) {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i <= 100000; i++) {
if (prime[i]) {
pp.push_back(i);
for (int j = i * 2; j <= 100000; j += i) {
prime[j] = false;
}
}
}
while (1) {
scanf("%d %d %d", &m, &a, &b);
if (m + a + b == 0)
break;
int rp = 0, rq = 0;
for (int i = 0; i < pp.size(); i++) {
int k = lower_bound(pp.begin(), pp.end(), m / pp[i]) - pp.begin();
for (int j = max(k - 2, i); j < pp.size(); j++) {
if (pp[i] * pp[j] >= m)
break;
if (a * pp[j] > b * pp[i])
continue;
if (rp * rq < pp[i] * pp[j]) {
rp = pp[i];
rq = pp[j];
}
}
}
printf("%d %d\n", rp, rq);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int m, a, b;
bool prime[100001];
vector<int> pp;
int main(void) {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i <= 100000; i++) {
if (prime[i]) {
pp.push_back(i);
for (int j = i * 2; j <= 100000; j += i) {
prime[j] = false;
}
}
}
while (1) {
scanf("%d %d %d", &m, &a, &b);
if (m + a + b == 0)
break;
int rp = 0, rq = 0;
for (int i = 0; i < pp.size(); i++) {
if (pp[i] * pp[i] > m)
break;
for (int j = i; j < pp.size(); j++) {
if (pp[i] * pp[j] > m)
break;
if (a * pp[j] > b * pp[i])
continue;
if (rp * rq < pp[i] * pp[j]) {
rp = pp[i];
rq = pp[j];
}
}
}
printf("%d %d\n", rp, rq);
}
return 0;
} | replace | 25 | 28 | 25 | 29 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
#define MMAX 100000
#define YES 0
#define NO 1
struct S {
int p, q, pq;
};
int main() {
int m, a, b;
int hurui[MMAX + 1] = {YES};
int prime[MMAX + 1];
int cnt = 0;
S ans = {-1, -1, -1};
hurui[0] = NO, hurui[1] = NO;
for (int i = 2; i < MMAX; i++) {
if (hurui[i] == YES) {
prime[cnt] = i;
cnt++;
for (int j = i * 2; j < MMAX; j += i) {
hurui[j] = NO;
}
}
}
while (1) {
cin >> m >> a >> b;
if (a == 0 && b == 0 && m == 0)
break;
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < cnt; j++) {
if (prime[i] * prime[j] <= m &&
(double)prime[i] / prime[j] >= (double)a / b &&
(double)prime[i] / prime[j] <= 1) {
if (ans.pq < prime[i] * prime[j]) {
ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
}
}
}
}
cout << ans.p << " " << ans.q << endl;
ans.p = -1, ans.q = -1, ans.pq = -1;
}
return 0;
} | #include <iostream>
using namespace std;
#define MMAX 100000
#define YES 0
#define NO 1
struct S {
int p, q, pq;
};
int main() {
int m, a, b;
int hurui[MMAX + 1] = {YES};
int prime[MMAX + 1];
int cnt = 0;
S ans = {-1, -1, -1};
hurui[0] = NO, hurui[1] = NO;
for (int i = 2; i < MMAX; i++) {
if (hurui[i] == YES) {
prime[cnt] = i;
cnt++;
for (int j = i * 2; j < MMAX; j += i) {
hurui[j] = NO;
}
}
}
while (1) {
cin >> m >> a >> b;
if (a == 0 && b == 0 && m == 0)
break;
for (int i = 0; m >= prime[i] * prime[0]; i++) {
for (int j = 0; m >= prime[i] * prime[j]; j++) {
if ((double)prime[i] / prime[j] >= (double)a / b &&
(double)prime[i] / prime[j] <= 1) {
if (ans.pq < prime[i] * prime[j]) {
ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
}
}
}
}
cout << ans.p << " " << ans.q << endl;
ans.p = -1, ans.q = -1, ans.pq = -1;
}
return 0;
} | replace | 36 | 40 | 36 | 39 | TLE | |
p00811 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
int prime[SIZE + 1];
vector<int> V;
memset(prime, 1, sizeof(prime));
prime[0] = prime[1] = 0;
REP(i, SIZE + 1) {
if (prime[i]) {
for (int j = i * 2; j <= SIZE; j += i)
prime[j] = 0;
V.PB(i);
}
}
REP(i, SIZE) {
if (prime[i + 1])
prime[i + 1] = i + 1;
else
prime[i + 1] = prime[i];
}
while (1) {
int m, a, b;
cin >> m >> a >> b;
if (m + a + b == 0)
break;
pair<int, int> ans;
ans = MP(-1, 1);
REP(i, V.size()) {
if (m / V[i] <= 1)
break;
int p = V[i];
int q[3];
q[0] = p;
q[1] = prime[m / V[i]];
q[2] = prime[p * b / a];
REP(i, 3) {
if (q[i] >= p && p * b >= q[i] * a && q[i] * p <= m) {
if (ans.fir * ans.sec < p * q[i])
ans = MP(p, q[i]);
}
}
}
cout << ans.fir << " " << ans.sec << endl;
}
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
int prime[SIZE + 1];
vector<int> V;
memset(prime, 1, sizeof(prime));
prime[0] = prime[1] = 0;
REP(i, SIZE + 1) {
if (prime[i]) {
for (int j = i * 2; j <= SIZE; j += i)
prime[j] = 0;
V.PB(i);
}
}
REP(i, SIZE) {
if (prime[i + 1])
prime[i + 1] = i + 1;
else
prime[i + 1] = prime[i];
}
while (1) {
int m, a, b;
cin >> m >> a >> b;
if (m + a + b == 0)
break;
pair<int, int> ans;
ans = MP(-1, 1);
REP(i, V.size()) {
if (m / V[i] <= 1)
break;
int p = V[i];
int q[3];
q[0] = p;
q[1] = prime[m / V[i]];
q[2] = prime[min(SIZE, p * b / a)];
REP(j, 3) {
if (q[j] >= p && p * b >= q[j] * a && q[j] * p <= m) {
if (ans.fir * ans.sec < p * q[j])
ans = MP(p, q[j]);
}
}
}
cout << ans.fir << " " << ans.sec << endl;
}
} | replace | 40 | 45 | 40 | 45 | 0 | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100001;
vector<int> prime;
bool is_prime(int n) {
if (n == 2 || n == 3)
return true;
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
signed main() {
for (int i = 2; i < MAX; i++) {
if (is_prime(i))
prime.push_back(i);
}
while (true) {
int m, a, b;
cin >> m >> a >> b;
if (!m && !a && !b)
return 0;
int p, q;
p = q = 1;
for (int i = 0; i < prime.size(); i++) {
int ptmp = prime[i];
for (int j = i; j < prime.size(); j++) {
int qtmp = prime[j];
if (ptmp * qtmp > m)
break;
double res = (double)ptmp / qtmp;
if (res < (double)a / b)
break;
if (ptmp * qtmp > p * q) {
p = ptmp;
q = qtmp;
}
}
}
cout << p << " " << q << endl;
}
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 50000;
vector<int> prime;
bool is_prime(int n) {
if (n == 2 || n == 3)
return true;
if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
signed main() {
for (int i = 2; i < MAX; i++) {
if (is_prime(i))
prime.push_back(i);
}
while (true) {
int m, a, b;
cin >> m >> a >> b;
if (!m && !a && !b)
return 0;
int p, q;
p = q = 1;
for (int i = 0; i < prime.size(); i++) {
int ptmp = prime[i];
for (int j = i; j < prime.size(); j++) {
int qtmp = prime[j];
if (ptmp * qtmp > m)
break;
double res = (double)ptmp / qtmp;
if (res < (double)a / b)
break;
if (ptmp * qtmp > p * q) {
p = ptmp;
q = qtmp;
}
}
}
cout << p << " " << q << endl;
}
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cctype>
#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, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) \
(cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) \
(cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y \
<< ", " << z << ")" << endl)
typedef long long ll;
typedef pair<int, int> pii;
int m, a, b;
vector<int> primes;
#define MAX 100001
bool ps[MAX];
void erat() {
fill(ps, ps + MAX, true);
ps[0] = ps[1] = false;
REP(i, 2, MAX) {
if (ps[i])
primes.push_back(i);
for (int j = 2 * i; j < MAX; j += i)
ps[j] = false;
}
}
int main(void) {
erat();
while (cin >> m >> a >> b, m | a | b) {
int ans = 0;
pii ansp;
rep(i, primes.size()) {
int p = primes[i];
REP(j, i, primes.size()) {
int q = primes[j];
if (p * q > m)
break;
if (!(a * q <= p * b))
continue;
if (!(p <= q))
continue;
if (ans < p * q) {
ans = p * q;
ansp = pii(p, q);
}
}
}
printf("%d %d\n", ansp.first, ansp.second);
}
return 0;
} | #include <algorithm>
#include <cctype>
#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, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) \
(cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) \
(cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y \
<< ", " << z << ")" << endl)
typedef long long ll;
typedef pair<int, int> pii;
int m, a, b;
vector<int> primes;
#define MAX 50001
bool ps[MAX];
void erat() {
fill(ps, ps + MAX, true);
ps[0] = ps[1] = false;
REP(i, 2, MAX) {
if (ps[i])
primes.push_back(i);
for (int j = 2 * i; j < MAX; j += i)
ps[j] = false;
}
}
int main(void) {
erat();
while (cin >> m >> a >> b, m | a | b) {
int ans = 0;
pii ansp;
rep(i, primes.size()) {
int p = primes[i];
REP(j, i, primes.size()) {
int q = primes[j];
if (p * q > m)
break;
if (!(a * q <= p * b))
continue;
if (!(p <= q))
continue;
if (ans < p * q) {
ans = p * q;
ansp = pii(p, q);
}
}
}
printf("%d %d\n", ansp.first, ansp.second);
}
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int main() {
for (int i = 2; i < 50000; i++) {
bool ok = true;
for (int j = 0; j < v.size(); j++) {
if (i % v[j] == 0)
ok = false;
}
if (ok)
v.push_back(i);
}
int m, a, b;
while (cin >> m >> a >> b && m) {
int p = -1, q = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = i; j < v.size(); j++) {
if (v[j] <= b * v[i] / a && v[i] * v[j] <= m) {
if (p * q < v[i] * v[j])
p = v[i], q = v[j];
}
}
}
cout << p << " " << q << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int main() {
for (int i = 2; i < 50000; i++) {
bool ok = true;
for (int j = 0; j < v.size(); j++) {
if (i % v[j] == 0)
ok = false;
}
if (ok)
v.push_back(i);
}
int m, a, b;
while (cin >> m >> a >> b && m) {
int p = -1, q = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = i; j < v.size(); j++) {
if (v[i] * v[j] > m)
break;
if (v[j] <= b * v[i] / a) {
if (p * q < v[i] * v[j])
p = v[i], q = v[j];
}
}
}
cout << p << " " << q << endl;
}
return 0;
} | replace | 20 | 21 | 20 | 23 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
int m, a, b, i, j;
for (i = 2; i < 50000; i++) {
bool ok = 1;
for (j = 0; j < v.size(); j++)
if (!i % v[j])
ok = 0;
if (ok)
v.push_back(i);
}
while (cin >> m >> a >> b && m) {
int p = 0, q = 0;
for (i = 0; i < v.size(); i++) {
for (j = i; j < v.size(); j++) {
if (v[i] * v[j] > m)
break;
if (v[j] <= b * v[i] / a && p * q < v[i] * v[j])
p = v[i], q = v[j];
}
}
cout << p << " " << q << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
int m, a, b, i, j;
for (i = 2; i < 50000; i++) {
bool ok = 1;
for (j = 0; j < v.size(); j++)
if (i % v[j] == 0)
ok = 0;
if (ok)
v.push_back(i);
}
while (cin >> m >> a >> b && m) {
int p = 0, q = 0;
for (i = 0; i < v.size(); i++) {
for (j = i; j < v.size(); j++) {
if (v[i] * v[j] > m)
break;
if (v[j] <= b * v[i] / a && p * q < v[i] * v[j])
p = v[i], q = v[j];
}
}
cout << p << " " << q << endl;
}
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p00811 | C++ | Time Limit Exceeded |
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;
signed main(void) {
int i, j;
int m, a, b;
vi p(100005, true);
for (i = 2; i < 100005; i++)
if (p[i])
for (j = 2; i * j < 100005; j++)
p[i * j] = false;
vi prime;
loop(i, 2, 100005) if (p[i]) prime.push_back(i);
while (1) {
cin >> m >> a >> b;
if (m + a + b == 0)
break;
int g = __gcd(a, b);
a /= g;
b /= g;
int x, y;
int ans = 0;
rep(i, prime.size())
loop(j, i, prime.size()) if (prime[i] * prime[j] <= m &&
prime[i] * prime[j] > ans &&
b * prime[i] >= a * prime[j]) {
ans = prime[i] * prime[j];
x = prime[i];
y = prime[j];
// cout<<x<<" "<<y<<" "<<ans<<endl;
}
cout << x << " " << y << endl;
}
} |
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;
signed main(void) {
int i, j;
int m, a, b;
vi p(100005, true);
for (i = 2; i < 100005; i++)
if (p[i])
for (j = 2; i * j < 100005; j++)
p[i * j] = false;
vi prime;
loop(i, 2, 100005) if (p[i]) prime.push_back(i);
while (1) {
cin >> m >> a >> b;
if (m + a + b == 0)
break;
int g = __gcd(a, b);
a /= g;
b /= g;
int x, y;
int ans = 0;
rep(i, prime.size())
loop(j, i, prime.size()) if (prime[i] * prime[j] > m ||
b * prime[i] < a * prime[j]) break;
else if (prime[i] * prime[j] > ans) {
ans = prime[i] * prime[j];
x = prime[i];
y = prime[j];
// cout<<x<<" "<<y<<" "<<ans<<endl;
}
cout << x << " " << y << endl;
}
} | replace | 52 | 55 | 52 | 55 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
bool isprime[100001];
void eratos(int n) {
for (int i = 0; i <= n; i++) {
isprime[i] = true;
}
isprime[0] = isprime[1] = false;
for (int i = 2; i <= sqrt(n); i++) {
if (isprime[i]) {
int j = i + i;
while (j <= n) {
isprime[j] = false;
j = j + i;
}
}
}
}
int main() {
eratos(100000);
int m, a, b;
while (1) {
cin >> m >> a >> b;
if (m == 0 && a == 0 && b == 0)
break;
for (int i = m; i > 0; i--) {
bool flag = false;
for (int j = 1; j < i; j++) {
if (i % j != 0)
continue;
if (isprime[j] == 0 || isprime[i / j] == 0)
continue;
if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
continue;
cout << j << " " << i / j << endl;
flag = true;
break;
}
if (flag)
break;
}
}
return 0;
} | #include <cmath>
#include <iostream>
using namespace std;
bool isprime[100001];
void eratos(int n) {
for (int i = 0; i <= n; i++) {
isprime[i] = true;
}
isprime[0] = isprime[1] = false;
for (int i = 2; i <= sqrt(n); i++) {
if (isprime[i]) {
int j = i + i;
while (j <= n) {
isprime[j] = false;
j = j + i;
}
}
}
}
int main() {
eratos(100000);
int m, a, b;
while (1) {
cin >> m >> a >> b;
if (m == 0 && a == 0 && b == 0)
break;
for (int i = m; i > 0; i--) {
bool flag = false;
for (int j = 1; j <= sqrt(i); j++) {
if (i % j != 0)
continue;
if (isprime[j] == 0 || isprime[i / j] == 0)
continue;
if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
continue;
cout << j << " " << i / j << endl;
flag = true;
break;
}
if (flag)
break;
}
}
return 0;
} | replace | 36 | 37 | 36 | 37 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
#define M 100000
using namespace std;
bool prime[M];
void primeInit() {
for (int i = 0; i < M; i++) {
prime[i] = false;
}
}
void p() {
prime[2] = true;
for (int i = 3; i < M; i++) {
bool flag = false;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = true;
break;
}
}
if (!flag)
prime[i] = true;
}
}
int main() {
int m, a, b;
primeInit();
p();
while (cin >> m >> a >> b && m != 0 || a != 0 || b != 0) {
double ma = 0;
int mp = 0;
int mq = 0;
for (int p = 2; p <= m / 2; p++) {
if (!prime[p])
continue;
for (int q = 2; q <= m / 2; q++) {
if (!prime[q])
continue;
if (1.0 * p * q > ma && 1.0 * p * q <= m && (1.0 * p / q) <= 1 &&
(1.0 * p / q) >= (a * 1.0 / b)) {
ma = p * q;
mp = p;
mq = q;
}
}
}
cout << mp << " " << mq << endl;
}
return 0;
} | #include <iostream>
#include <vector>
#define M 100000
using namespace std;
bool prime[M];
void primeInit() {
for (int i = 0; i < M; i++) {
prime[i] = false;
}
}
void p() {
prime[2] = true;
for (int i = 3; i < M; i++) {
bool flag = false;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = true;
break;
}
}
if (!flag)
prime[i] = true;
}
}
int main() {
int m, a, b;
primeInit();
p();
while (cin >> m >> a >> b && m != 0 || a != 0 || b != 0) {
double ma = 0;
int mp = 0;
int mq = 0;
for (int p = 2; p <= m / 2; p++) {
if (!prime[p])
continue;
for (int q = 2; q <= m / p; q++) {
if (!prime[q])
continue;
if (1.0 * p * q > ma && 1.0 * p * q <= m && (1.0 * p / q) <= 1 &&
(1.0 * p / q) >= (a * 1.0 / b)) {
ma = p * q;
mp = p;
mq = q;
}
}
}
cout << mp << " " << mq << endl;
}
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// sieve of eratosthenes
vector<int> pnums(1000000, 0);
for (int i = 2; i < 1000000; i++)
pnums[i] = 1;
for (int i = 2; i * i < 1000000; i++) {
if (pnums[i]) {
for (int j = i * i; j < 1000000; j += i) {
pnums[j] = 0;
}
}
}
vector<int> primes;
for (int i = 2; i < 100000; i++)
if (pnums[i])
primes.push_back(i);
int m, a, b;
while (cin >> m >> a >> b, m || a || b) {
int p = 0, q = 0, ma = p * q;
for (int i = 0; i < (int)primes.size(); i++) {
for (int j = i; j < (int)primes.size(); j++) {
if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
continue;
if (ma < primes[i] * primes[j]) {
p = primes[i];
q = primes[j];
ma = primes[i] * primes[j];
}
}
}
cout << p << " " << q << endl;
}
} | #include "bits/stdc++.h"
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// sieve of eratosthenes
vector<int> pnums(1000000, 0);
for (int i = 2; i < 1000000; i++)
pnums[i] = 1;
for (int i = 2; i * i < 1000000; i++) {
if (pnums[i]) {
for (int j = i * i; j < 1000000; j += i) {
pnums[j] = 0;
}
}
}
vector<int> primes;
for (int i = 2; i < 10000; i++)
if (pnums[i])
primes.push_back(i);
int m, a, b;
while (cin >> m >> a >> b, m || a || b) {
int p = 0, q = 0, ma = p * q;
for (int i = 0; i < (int)primes.size(); i++) {
for (int j = i; j < (int)primes.size(); j++) {
if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
continue;
if (ma < primes[i] * primes[j]) {
p = primes[i];
q = primes[j];
ma = primes[i] * primes[j];
}
}
}
cout << p << " " << q << endl;
}
} | replace | 20 | 21 | 20 | 21 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
stringstream ss;
ss << x;
int tmp;
ss >> tmp;
return tmp;
}
string itos(int x) {
stringstream ss;
ss << x;
return ss.str();
}
vector<int> ta;
bool is(int n) {
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
void init() {
int i = 2;
while (ta.size() <= 100000) {
if (is(i))
ta.push_back(i);
i++;
}
}
int m, a, b, a1 = 0, a2 = 0;
int main() {
init();
// for(int i=0;i<10;i++)cout<<ta[i]<<" ";cout<<endl;
while (true) {
cin >> m >> a >> b;
if (m + a + b == 0)
break;
a1 = 0, a2 = 0;
for (int i = 0; ta[i] <= m; i++) {
for (int j = 0; ta[j] <= m; j++) {
// if(i>j)continue;
int p = ta[j], q = ta[i];
double a_b = (double)a / (double)b, p_q = (double)p / (double)q;
if (p * q <= m && a_b <= p_q && p_q <= 1.0 && a1 * a2 < p * q) {
a1 = min(q, p), a2 = max(p, q);
}
}
}
cout << a1 << " " << a2 << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
stringstream ss;
ss << x;
int tmp;
ss >> tmp;
return tmp;
}
string itos(int x) {
stringstream ss;
ss << x;
return ss.str();
}
vector<int> ta;
bool is(int n) {
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
void init() {
int i = 2;
while (ta.size() <= 100000) {
if (is(i))
ta.push_back(i);
i++;
}
}
int m, a, b, a1 = 0, a2 = 0;
int main() {
init();
// for(int i=0;i<10;i++)cout<<ta[i]<<" ";cout<<endl;
while (true) {
cin >> m >> a >> b;
if (m + a + b == 0)
break;
a1 = 0, a2 = 0;
for (int i = 0; ta[i] <= m; i++) {
for (int j = 0; ta[j] <= m; j++) {
// if(i>j)continue;
int p = ta[j], q = ta[i];
double a_b = (double)a / (double)b, p_q = (double)p / (double)q;
if (p * q <= m && a_b <= p_q && p_q <= 1.0 && a1 * a2 < p * q) {
a1 = min(q, p), a2 = max(p, q);
}
if (p * q > m)
break;
}
}
cout << a1 << " " << a2 << endl;
}
return 0;
} | insert | 55 | 55 | 55 | 57 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
bool is_prime[100001];
void prime(int n) {
for (int i = 0; i <= n; i++)
is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i] == true) {
for (int j = 2 * i; j <= n; j += i)
is_prime[j] = false;
}
}
}
int main() {
int m, a, b;
prime(100000);
while (cin >> m >> a >> b, m) {
int p = 0, q = 0;
for (int i = 1; i * i <= m; i++) {
if (!is_prime[i])
continue;
for (int j = i; j <= m; j++) {
if (!is_prime[j])
continue;
if (q * p < i * j && i * j <= m && a * j <= i * b && i * b <= b * j) {
p = j;
q = i;
}
}
}
cout << q << " " << p << endl;
}
return 0;
} | #include <iostream>
using namespace std;
bool is_prime[100001];
void prime(int n) {
for (int i = 0; i <= n; i++)
is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i] == true) {
for (int j = 2 * i; j <= n; j += i)
is_prime[j] = false;
}
}
}
int main() {
int m, a, b;
prime(100000);
while (cin >> m >> a >> b, m) {
int p = 0, q = 0;
for (int i = 1; i * i <= m; i++) {
if (!is_prime[i])
continue;
for (int j = i; j <= m / i + 1; j++) {
if (!is_prime[j])
continue;
if (q * p < i * j && i * j <= m && a * j <= i * b && i * b <= b * j) {
p = j;
q = i;
}
}
}
cout << q << " " << p << endl;
}
return 0;
} | replace | 24 | 25 | 24 | 25 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <climits>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const static int M_MAX = 999999;
int main() {
int era[M_MAX + 1] = {0};
vector<int> primes;
era[0] = era[1] = 1;
for (int i = 2; i <= M_MAX; i++) {
if (era[i]) {
continue;
}
primes.push_back(i);
for (int j = i + i; j <= M_MAX; j += i) {
era[j] = 1;
}
}
int pLen = primes.size();
while (1) {
int m, a, b;
cin >> m >> a >> b;
if (!m && !a && !b) {
return EXIT_SUCCESS;
}
int ansP = -1, ansQ = -1;
for (int i = 0; primes[i] <= m; i++) {
for (int j = i; primes[j] <= m; j++) {
int p = primes[i];
int q = primes[j];
if (p * q <= m && a * q <= b * p) {
if (ansP == -1 || p * q > ansP * ansQ) {
ansP = p;
ansQ = q;
}
}
}
}
cout << ansP << " " << ansQ << endl;
}
} | #include <climits>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const static int M_MAX = 999999;
int main() {
int era[M_MAX + 1] = {0};
vector<int> primes;
era[0] = era[1] = 1;
for (int i = 2; i <= M_MAX; i++) {
if (era[i]) {
continue;
}
primes.push_back(i);
for (int j = i + i; j <= M_MAX; j += i) {
era[j] = 1;
}
}
int pLen = primes.size();
while (1) {
int m, a, b;
cin >> m >> a >> b;
if (!m && !a && !b) {
return EXIT_SUCCESS;
}
int ansP = -1, ansQ = -1;
for (int i = 0; i < pLen; i++) {
for (int j = 0; j < i + 1; j++) {
int p = primes[j];
int q = primes[i];
if (p * q > m) {
break;
}
if (a * q <= b * p) {
if (ansP == -1 || p * q > ansP * ansQ) {
ansP = p;
ansQ = q;
}
}
}
}
cout << ansP << " " << ansQ << endl;
}
} | replace | 32 | 37 | 32 | 40 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const double EPS = 1.0e-9;
int main() {
vector<int> prime;
prime.push_back(2);
prime.push_back(3);
for (int i = 4; i < 100000; i++) {
bool flag = true;
for (int j = 0; j < prime.size(); j++) {
if (i % prime[j] == 0) {
flag = false;
break;
}
}
if (flag == true) {
prime.push_back(i);
}
}
while (true) {
int m, a, b;
cin >> m >> a >> b;
if (m == 0 && a == 0 && b == 0) {
break;
}
int ansp, ansq;
int maxarea = 0;
rep(i, prime.size()) {
for (int j = i; j < prime.size(); j++) {
if (prime[i] * prime[j] <= m &&
((double)a / b <= (double)prime[i] / prime[j] ||
fabs((double)a / b - (double)prime[i] / prime[j]) < EPS) &&
(double)prime[i] / prime[j] <= 1) {
if (prime[i] * prime[j] > maxarea) {
maxarea = prime[i] * prime[j];
ansp = prime[i];
ansq = prime[j];
}
}
}
}
cout << ansp << " " << ansq << endl;
}
return 0;
} | #include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const double EPS = 1.0e-9;
int main() {
vector<int> prime;
prime.push_back(2);
prime.push_back(3);
for (int i = 4; i < 10000; i++) {
bool flag = true;
for (int j = 0; j < prime.size(); j++) {
if (i % prime[j] == 0) {
flag = false;
break;
}
}
if (flag == true) {
prime.push_back(i);
}
}
while (true) {
int m, a, b;
cin >> m >> a >> b;
if (m == 0 && a == 0 && b == 0) {
break;
}
int ansp, ansq;
int maxarea = 0;
rep(i, prime.size()) {
for (int j = i; j < prime.size(); j++) {
if (prime[i] * prime[j] <= m &&
((double)a / b <= (double)prime[i] / prime[j] ||
fabs((double)a / b - (double)prime[i] / prime[j]) < EPS) &&
(double)prime[i] / prime[j] <= 1) {
if (prime[i] * prime[j] > maxarea) {
maxarea = prime[i] * prime[j];
ansp = prime[i];
ansq = prime[j];
}
}
}
}
cout << ansp << " " << ansq << endl;
}
return 0;
} | replace | 13 | 14 | 13 | 14 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
int from, to, cost;
bool operator<(const edge &e) const { return cost < e.cost; }
bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
const int N = 50005;
bool prime[N];
vector<int> primes;
void hurui() {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i * i < N; i++) {
for (int j = 2; i * j < N; j++) {
prime[i * j] = false;
}
}
REP(i, N + 1) {
if (prime[i])
primes.push_back(i);
}
}
int main() {
hurui();
int m, a, b;
while (cin >> m >> a >> b, m) {
struct ans {
int m, p, q;
};
ans t = {-1, -1, -1};
REP(i, primes.size()) {
if (primes[i] > m / 2)
break;
FOR(j, i, primes.size()) {
if (primes[i] * primes[j] <= m && a * primes[j] <= b * primes[i]) {
if (primes[i] * primes[j] > t.m)
t = {primes[i] * primes[j], primes[i], primes[j]};
}
}
}
cout << t.p << " " << t.q << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
int from, to, cost;
bool operator<(const edge &e) const { return cost < e.cost; }
bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///
const int N = 50005;
bool prime[N];
vector<int> primes;
void hurui() {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i * i < N; i++) {
for (int j = 2; i * j < N; j++) {
prime[i * j] = false;
}
}
REP(i, N + 1) {
if (prime[i])
primes.push_back(i);
}
}
int main() {
hurui();
int m, a, b;
while (cin >> m >> a >> b, m) {
struct ans {
int m, p, q;
};
ans t = {-1, -1, -1};
REP(i, primes.size()) {
if (primes[i] > m / 2)
break;
FOR(j, i, primes.size()) {
if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
break;
if (primes[i] * primes[j] <= m && a * primes[j] <= b * primes[i]) {
if (primes[i] * primes[j] > t.m)
t = {primes[i] * primes[j], primes[i], primes[j]};
}
}
}
cout << t.p << " " << t.q << endl;
}
} | insert | 55 | 55 | 55 | 58 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
bool isPrime(int x) {
if (x <= 1)
return false;
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0)
return false;
}
return true;
}
int main() {
vector<int> ps;
for (int x = 2; x <= 50000; ++x) {
if (isPrime(x))
ps.push_back(x);
}
for (int m, a, b; cin >> m >> a >> b && (m | a | b);) {
int p = 0, q = 0;
for (int i = 0; i < ps.size(); ++i) {
for (int j = i; j < ps.size(); ++j) {
if (ps[i] * ps[j] <= m && a * ps[j] <= b * ps[i] &&
ps[i] * ps[j] > p * q) {
p = ps[i];
q = ps[j];
}
}
}
cout << p << " " << q << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
bool isPrime(int x) {
if (x <= 1)
return false;
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0)
return false;
}
return true;
}
int main() {
vector<int> ps;
for (int x = 2; x <= 50000; ++x) {
if (isPrime(x))
ps.push_back(x);
}
for (int m, a, b; cin >> m >> a >> b && (m | a | b);) {
int p = 0, q = 0;
for (int i = 0; i < ps.size(); ++i) {
for (int j = i; j < ps.size() && ps[i] * ps[j] <= m; ++j) {
if (ps[i] * ps[j] <= m && a * ps[j] <= b * ps[i] &&
ps[i] * ps[j] > p * q) {
p = ps[i];
q = ps[j];
}
}
}
cout << p << " " << q << endl;
}
return 0;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p00811 | C++ | Runtime Error | #include <iostream>
using namespace std;
int isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
}
int main() {
int m, a, b;
int i, j, k;
int memo[200001] = {};
for (i = 2; i < 200001; i++)
memo[i] = isPrime(i);
cin >> m >> a >> b;
while ((m + a + b) != 0) {
int p, q, o = 0;
for (i = 1; i * i <= m; i++) {
for (j = i; j <= b * i / a; j++) {
if (memo[i] * memo[j] == 1) {
if (o < i * j && i * j <= m) {
o = i * j;
p = i;
q = j;
}
}
}
}
// cout << o << endl;
cout << p << " " << q << endl;
cin >> m >> a >> b;
}
return 0;
} | #include <iostream>
using namespace std;
int isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
}
int main() {
int m, a, b;
int i, j, k;
int memo[200001] = {};
for (i = 2; i < 200001; i++)
memo[i] = isPrime(i);
cin >> m >> a >> b;
while ((m + a + b) != 0) {
int p, q, o = 0;
for (i = 1; i * i <= m; i++) {
for (j = i; j <= b * i / a; j++) {
if (j < 100001) {
if (memo[i] * memo[j] == 1) {
if (o < i * j && i * j <= m) {
o = i * j;
p = i;
q = j;
}
}
}
}
}
// cout << o << endl;
cout << p << " " << q << endl;
cin >> m >> a >> b;
}
return 0;
} | replace | 20 | 25 | 20 | 27 | 0 | |
p00811 | C++ | Time Limit Exceeded | #define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
/* Inline functions */
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)
/*}}}*/
int main() {
std::ios_base::sync_with_stdio(false);
#define MAX 100000
VB prime_table(MAX, true);
prime_table[0] = prime_table[1] = false;
REP(i, MAX) {
if (prime_table[i]) {
for (int j = i * 2; j < MAX; j += i) {
prime_table[j] = false;
}
}
}
VI primes;
REP(i, prime_table.size()) {
if (prime_table[i]) {
primes.PB(i);
}
}
int m, a, b;
while (cin >> m >> a >> b, m) {
PII ans;
int max_area = 0;
REP(i, primes.size()) {
FOR(j, i, primes.size()) {
int p = primes[i], q = primes[j];
if (p * q <= m && (double)a / b <= (double)p / q) {
if (p * q > max_area) {
max_area = p * q;
ans = MP(p, q);
}
}
}
}
cout << ans.F << " " << ans.S << endl;
}
}
// vim: foldmethod=marker | #define DEBUG_ON
#define CONDITION true
using namespace std; /*{{{*/
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
#define INF (1e9)
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
typedef long long int LL;
typedef unsigned long long int ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef complex<double> P;
#define FOR(i, b, e) for (typeof(e) i = (b); i != (e); i < (e) ? ++i : --i)
#define REP(i, n) FOR(i, 0, n)
#define IFC(c) \
if (c) \
continue;
#define IFB(c) \
if (c) \
break;
#define IFR(c, r) \
if (c) \
return r;
#define OPOVER(_op, _type) inline bool operator _op(const _type &t) const
#define arrsz(a) (sizeof(a) / sizeof(a[0]))
#define F first
#define S second
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((LL)a.size())
#define PB(e) push_back(e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(c, it) \
for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n) (assert(n < 64), (1ULL << (n)))
#define BITOF(n, m) (assert(m < 64), ((ULL)(n) >> (m)&1))
#define RANGE(a, b, c) ((a) <= (b) && (b) <= (c))
#define PQ priority_queue
#define SC static_cast
#ifdef DEBUG_ON
#define dprt(fmt, ...) \
if (CONDITION) \
fprintf(stderr, fmt, ##__VA_ARGS__)
#define darr(a) \
if (CONDITION) \
copy((a), (a) + arrsz(a), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr_range(a, f, t) \
if (CONDITION) \
copy((a) + (f), (a) + (t), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define dvec(v) \
if (CONDITION) \
copy(ALL(v), ostream_iterator<int>(cerr, " ")); \
cerr << endl
#define darr2(a, n, m) \
if (CONDITION) \
FOR(i, 0, (n)) { darr_range((a)[i], 0, (m)); }
#define dvec2(v) \
if (CONDITION) \
FOR(i, 0, SZ(v)) { dvec((v)[i]); }
#define WAIT() \
if (CONDITION) { \
string _wait_; \
cerr << "(hit return to continue)" << endl; \
getline(cin, _wait_); \
}
#define dump(x) \
if (CONDITION) \
cerr << " [L" << __LINE__ << "] " << #x << " = " << (x) << endl;
#define dumpf() \
if (CONDITION) \
cerr << __PRETTY_FUNCTION__ << endl;
#define dumpv(x) \
if (CONDITION) \
cerr << " [L:" << __LINE__ << "] " << #x << " = "; \
REP(q, (x).size()) cerr << (x)[q] << " "; \
cerr << endl;
#define where() \
if (CONDITION) \
cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << " [L: " << __LINE__ \
<< "]" << endl;
#define show_bits(b, s) \
if (CONDITION) { \
REP(i, s) { \
cerr << BITOF(b, s - 1 - i); \
if (i % 4 == 3) \
cerr << ' '; \
} \
cerr << endl; \
}
#else
#define cerr \
if (0) \
cerr
#define dprt(fmt, ...)
#define darr(a)
#define darr_range(a, f, t)
#define dvec(v)
#define darr2(a, n, m)
#define dvec2(v)
#define WAIT()
#define dump(x)
#define dumpf()
#define dumpv(x)
#define where()
#define show_bits(b, s)
#endif
/* Inline functions */
inline int onbits_count(ULL b) {
int c = 0;
while (b != 0) {
c += (b & 1);
b >>= 1;
}
return c;
}
inline int bits_count(ULL b) {
int c = 0;
while (b != 0) {
++c;
b >>= 1;
}
return c;
}
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (static_cast<double>(tv.tv_sec) +
static_cast<double>(tv.tv_usec) * 1e-6);
}
inline VS split(string s, char delimiter) {
VS v;
string t;
REP(i, s.length()) {
if (s[i] == delimiter)
v.PB(t), t = "";
else
t += s[i];
}
v.PB(t);
return v;
}
/* Tweaks */
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
/* Frequent stuffs */
int n_dir = 4;
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; /* CSS order */
enum direction { UP, RIGHT, DOWN, LEFT };
// int n_dir = 8;
// int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
// enum direction {
// UP, UPRIGHT, RIGHT, DOWNRIGHT, DOWN, DOWNLEFT, LEFT, UPLEFT
// }
#define FORDIR(d) REP(d, n_dir)
/*}}}*/
int main() {
std::ios_base::sync_with_stdio(false);
#define MAX 100000
VB prime_table(MAX, true);
prime_table[0] = prime_table[1] = false;
REP(i, MAX) {
if (prime_table[i]) {
for (int j = i * 2; j < MAX; j += i) {
prime_table[j] = false;
}
}
}
VI primes;
REP(i, prime_table.size()) {
if (prime_table[i]) {
primes.PB(i);
}
}
int m, a, b;
while (cin >> m >> a >> b, m) {
PII ans;
int max_area = 0;
VI::iterator qitr = upper_bound(ALL(primes), m);
while (qitr > primes.begin()) {
--qitr;
VI::iterator pitr = upper_bound(ALL(primes), m / *qitr) - 1;
IFC(pitr < primes.begin());
if (*pitr > *qitr) {
pitr = qitr;
}
if ((double)a / b <= (double)(*pitr) / (*qitr)) {
if ((*pitr) * (*qitr) > max_area) {
max_area = (*pitr) * (*qitr);
ans = MP(*pitr, *qitr);
}
}
}
cout << ans.F << " " << ans.S << endl;
}
}
// vim: foldmethod=marker | replace | 244 | 252 | 244 | 256 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 100010;
int prime[MAX_N]; // i番目の素数
bool is_prime[MAX_N + 1]; // is_prime[i]がtrueならiは素数
// n以下の素数の数を返す
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = false;
is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
int main() {
sieve(100008);
double m, a, b;
while (cin >> m >> a >> b, m || a || b) {
double k = a / b;
int tmp = 0;
int ans = 0;
int ansx = 0;
int ansy = 0;
for (int i = 2; i <= sqrt(m); i++) {
if (!is_prime[i])
continue;
for (int j = i; j <= m; j++) {
if (!is_prime[j])
continue;
double l = (double)i / j;
if (m >= i * j && k <= l && l <= 1 && i * j > ans) {
ans = i * j;
// cout <<k<<" "<<l<<endl;
ansx = i;
ansy = j;
}
}
}
cout << ansx << " " << ansy << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 100010;
int prime[MAX_N]; // i番目の素数
bool is_prime[MAX_N + 1]; // is_prime[i]がtrueならiは素数
// n以下の素数の数を返す
int sieve(int n) {
int p = 0;
for (int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = false;
is_prime[1] = false;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) {
prime[p++] = i;
for (int j = 2 * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return p;
}
int main() {
sieve(100008);
double m, a, b;
while (cin >> m >> a >> b, m || a || b) {
double k = a / b;
int tmp = 0;
int ans = 0;
int ansx = 0;
int ansy = 0;
for (int i = 2; i <= sqrt(m); i++) {
if (!is_prime[i])
continue;
for (int j = i; j <= m / 2; j++) {
if (!is_prime[j])
continue;
double l = (double)i / j;
if (m >= i * j && k <= l && l <= 1 && i * j > ans) {
ans = i * j;
// cout <<k<<" "<<l<<endl;
ansx = i;
ansy = j;
}
}
}
cout << ansx << " " << ansy << endl;
}
return 0;
} | replace | 72 | 73 | 72 | 73 | TLE | |
p00811 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int m, a, b;
bool pp[100001];
vector<int> prime;
{
memset(pp, true, sizeof(pp));
pp[0] = pp[1] = false;
for (int i = 2; i < 100001; i++) {
if (pp[i]) {
prime.push_back(i);
for (int j = i + i; j < 100001; j += i)
pp[j] = false;
}
}
}
for (; cin >> m >> a >> b, m;) {
for (; m > 0;) {
for (int i = 0; i < prime.size(); i++) {
if (prime[i] > m)
continue;
if (m % prime[i] == 0 && pp[m / prime[i]] &&
((double)a / b) <= ((double)(m / prime[i]) / prime[i]) &&
((double)(m / prime[i]) / prime[i]) <= 1) {
cout << m / prime[i] << " " << prime[i] << endl;
m = -1;
i = -1;
}
}
m--;
}
}
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int m, a, b;
bool pp[100001];
vector<int> prime;
{
memset(pp, true, sizeof(pp));
pp[0] = pp[1] = false;
for (int i = 2; i < 100001; i++) {
if (pp[i]) {
prime.push_back(i);
for (int j = i + i; j < 100001; j += i)
pp[j] = false;
}
}
}
for (; cin >> m >> a >> b, m;) {
for (; m > 0;) {
for (int i = 0; i < prime.size(); i++) {
if (prime[i] > m / 2)
break;
if (m % prime[i] == 0 && pp[m / prime[i]] &&
((double)a / b) <= ((double)(m / prime[i]) / prime[i]) &&
((double)(m / prime[i]) / prime[i]) <= 1) {
cout << m / prime[i] << " " << prime[i] << endl;
m = -1;
i = -1;
}
}
m--;
}
}
} | replace | 27 | 29 | 27 | 29 | TLE | |
p00813 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define double long double
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<string, string> P1;
typedef pair<int, string> P2;
typedef pair<P, P2> P3;
int fac(int n) {
int res = 1;
for (int i = 1; i <= n; i++)
res *= i;
return res;
}
double get(int i) {
return fac(8) * pow(1.0 / 4, i) * pow(3.0 / 4, 8 - i) / (fac(i) * fac(8 - i));
}
string s[5];
string target;
double ans;
vector<P3> calcOrder(set<string> teams) {
map<string, int> points, diff, ggoals;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] != '-')
continue;
string t1 = s[i].substr(1, 3);
string t2 = s[0].substr(j - 1, 3);
if (teams.count(t1) == 0)
continue;
if (teams.count(t2) == 0)
continue;
int t1p = s[i][j - 1] - '0';
int t2p = s[i][j + 1] - '0';
if (t1p > t2p)
points[t1] += 3;
if (t1p == t2p)
points[t1]++, points[t2]++;
if (t1p < t2p)
points[t2] += 3;
diff[t1] += t1p - t2p;
diff[t2] += t2p - t1p;
ggoals[t1] += t1p;
ggoals[t2] += t2p;
}
}
vector<P3> res;
for (string team : teams) {
res.push_back(P3(P(points[team], diff[team]), P2(ggoals[team], team)));
}
sort(res.begin(), res.end(), greater<P3>());
return res;
}
void cal(double p) {
set<string> teams;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] != '-')
continue;
string t1 = s[i].substr(1, 3);
string t2 = s[0].substr(j - 1, 3);
teams.insert(t1);
teams.insert(t2);
}
}
vector<P3> order = calcOrder(teams);
int cnt = 10, add = 0, L, R;
while (cnt--) {
int A, B, C;
for (int i = 0; i < (int)order.size(); i++) {
if (order[i].second.second == target) {
A = order[i].first.first;
B = order[i].first.second;
C = order[i].second.first;
}
}
int l = 4, r = 0;
for (int i = 0; i < (int)order.size(); i++) {
if (A == order[i].first.first && B == order[i].first.second &&
C == order[i].second.first) {
l = min(l, i);
r = max(r, i);
}
}
teams.clear();
for (int i = l; i <= r; i++)
teams.insert(order[i].second.second);
add += l;
L = l, R = r;
order = calcOrder(teams);
}
if (L == R) {
if (add <= 1)
ans += p;
} else {
if (add <= 1) {
double chance = 1 - add + 1;
double all = R - L + 1;
ans += p * chance / all;
}
}
}
vector<P> YX;
signed main() {
int T;
cin >> T;
while (T--) {
YX.clear();
ans = 0;
for (int i = 0; i < 5; i++)
cin >> s[i];
for (int i = 0; i < (int)s[0].size(); i++) {
if (s[0][i] == '*')
target = s[0].substr(i + 1, 3);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] == '-' && s[i][j - 1] == '_')
YX.push_back(P(i, j - 1));
if (s[i][j] == '-' && s[i][j + 1] == '_')
YX.push_back(P(i, j + 1));
}
}
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
for (int k = 0; k <= 8; k++) {
for (int l = 0; l <= 8; l++) {
s[YX[0].first][YX[0].second] = i + '0';
s[YX[1].first][YX[1].second] = j + '0';
s[YX[2].first][YX[2].second] = k + '0';
s[YX[3].first][YX[3].second] = l + '0';
cal(get(i) * get(j) * get(k) * get(l));
}
}
}
}
printf("%.7Lf\n", ans);
}
return 0;
}
| #include <bits/stdc++.h>
#define double long double
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<string, string> P1;
typedef pair<int, string> P2;
typedef pair<P, P2> P3;
int fac(int n) {
int res = 1;
for (int i = 1; i <= n; i++)
res *= i;
return res;
}
double get(int i) {
return fac(8) * pow(1.0 / 4, i) * pow(3.0 / 4, 8 - i) / (fac(i) * fac(8 - i));
}
string s[5];
string target;
double ans;
vector<P3> calcOrder(set<string> teams) {
map<string, int> points, diff, ggoals;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] != '-')
continue;
string t1 = s[i].substr(1, 3);
string t2 = s[0].substr(j - 1, 3);
if (teams.count(t1) == 0)
continue;
if (teams.count(t2) == 0)
continue;
int t1p = s[i][j - 1] - '0';
int t2p = s[i][j + 1] - '0';
if (t1p > t2p)
points[t1] += 3;
if (t1p == t2p)
points[t1]++, points[t2]++;
if (t1p < t2p)
points[t2] += 3;
diff[t1] += t1p - t2p;
diff[t2] += t2p - t1p;
ggoals[t1] += t1p;
ggoals[t2] += t2p;
}
}
vector<P3> res;
for (string team : teams) {
res.push_back(P3(P(points[team], diff[team]), P2(ggoals[team], team)));
}
sort(res.begin(), res.end(), greater<P3>());
return res;
}
void cal(double p) {
set<string> teams;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] != '-')
continue;
string t1 = s[i].substr(1, 3);
string t2 = s[0].substr(j - 1, 3);
teams.insert(t1);
teams.insert(t2);
}
}
vector<P3> order = calcOrder(teams);
int cnt = 5, add = 0, L, R;
while (cnt--) {
int A, B, C;
for (int i = 0; i < (int)order.size(); i++) {
if (order[i].second.second == target) {
A = order[i].first.first;
B = order[i].first.second;
C = order[i].second.first;
}
}
int l = 4, r = 0;
for (int i = 0; i < (int)order.size(); i++) {
if (A == order[i].first.first && B == order[i].first.second &&
C == order[i].second.first) {
l = min(l, i);
r = max(r, i);
}
}
teams.clear();
for (int i = l; i <= r; i++)
teams.insert(order[i].second.second);
add += l;
L = l, R = r;
order = calcOrder(teams);
}
if (L == R) {
if (add <= 1)
ans += p;
} else {
if (add <= 1) {
double chance = 1 - add + 1;
double all = R - L + 1;
ans += p * chance / all;
}
}
}
vector<P> YX;
signed main() {
int T;
cin >> T;
while (T--) {
YX.clear();
ans = 0;
for (int i = 0; i < 5; i++)
cin >> s[i];
for (int i = 0; i < (int)s[0].size(); i++) {
if (s[0][i] == '*')
target = s[0].substr(i + 1, 3);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < (int)s[i].size(); j++) {
if (s[i][j] == '-' && s[i][j - 1] == '_')
YX.push_back(P(i, j - 1));
if (s[i][j] == '-' && s[i][j + 1] == '_')
YX.push_back(P(i, j + 1));
}
}
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
for (int k = 0; k <= 8; k++) {
for (int l = 0; l <= 8; l++) {
s[YX[0].first][YX[0].second] = i + '0';
s[YX[1].first][YX[1].second] = j + '0';
s[YX[2].first][YX[2].second] = k + '0';
s[YX[3].first][YX[3].second] = l + '0';
cal(get(i) * get(j) * get(k) * get(l));
}
}
}
}
printf("%.7Lf\n", ans);
}
return 0;
}
| replace | 94 | 95 | 94 | 95 | TLE | |
p00815 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
const int N = 50;
vector<int> G[N];
void solve() {
rep(i, N) G[i].clear();
vector<int> r;
while (1) {
int v;
scanf(" %d", &v);
if (v == 0)
break;
r.pb(v);
}
int R = r.size();
if (R == 0) {
printf("1\n");
return;
}
int num = 0;
rep(i, R) if (r[i] > 0)++ num;
assert(num < N);
// simulate dfs
int ct = 0;
int here = 0;
vector<int> vis;
vector<int> val;
assert(r[0] > 0);
vis.pb(ct++);
val.pb(r[0]);
for (int i = 1; i < R; ++i) {
while (val[here] <= 0) {
vis.pop_back();
if (vis.empty())
assert(false);
here = vis.back();
}
if (r[i] > 0) {
// dfs木の辺
int par = vis.back();
vis.pb(ct++);
here = vis.back();
val.pb(r[i]);
--val[par];
--val[here];
G[par].pb(here);
G[here].pb(par);
} else {
// 後退辺
int V = vis.size();
int idx = V - 1 + r[i];
assert(idx >= 0);
int par = vis[idx];
--val[par];
--val[here];
G[par].pb(here);
G[here].pb(par);
}
}
rep(i, num) {
printf("%d", i + 1);
sort(all(G[i]));
for (int j : G[i])
printf(" %d", j + 1);
printf("\n");
}
}
int main() {
int T;
scanf(" %d", &T);
while (T--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (T t : v) {
o << t << ",";
}
o << "]";
return o;
}
const int N = 110;
vector<int> G[N];
void solve() {
rep(i, N) G[i].clear();
vector<int> r;
while (1) {
int v;
scanf(" %d", &v);
if (v == 0)
break;
r.pb(v);
}
int R = r.size();
if (R == 0) {
printf("1\n");
return;
}
int num = 0;
rep(i, R) if (r[i] > 0)++ num;
assert(num < N);
// simulate dfs
int ct = 0;
int here = 0;
vector<int> vis;
vector<int> val;
assert(r[0] > 0);
vis.pb(ct++);
val.pb(r[0]);
for (int i = 1; i < R; ++i) {
while (val[here] <= 0) {
vis.pop_back();
if (vis.empty())
assert(false);
here = vis.back();
}
if (r[i] > 0) {
// dfs木の辺
int par = vis.back();
vis.pb(ct++);
here = vis.back();
val.pb(r[i]);
--val[par];
--val[here];
G[par].pb(here);
G[here].pb(par);
} else {
// 後退辺
int V = vis.size();
int idx = V - 1 + r[i];
assert(idx >= 0);
int par = vis[idx];
--val[par];
--val[here];
G[par].pb(here);
G[here].pb(par);
}
}
rep(i, num) {
printf("%d", i + 1);
sort(all(G[i]));
for (int j : G[i])
printf(" %d", j + 1);
printf("\n");
}
}
int main() {
int T;
scanf(" %d", &T);
while (T--)
solve();
return 0;
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p00816 | C++ | Memory Limit Exceeded | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int dp[7][1000000];
int pr[7][1000000];
int df[7][1000000];
int main() {
while (true) {
int n;
string s;
cin >> n >> s;
if (n == 0 && s == "0") {
break;
}
const int m = s.size();
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
dp[i][j] = pr[i][j] = df[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i = 0; i < m; ++i) {
int x = 0;
for (int j = i; j < m; ++j) {
x = (x * 10) + s[j] - '0';
if (x > n) {
break;
}
for (int k = 0; k + x <= n; ++k) {
if (dp[i][k] == 0) {
continue;
}
dp[j + 1][k + x] += dp[i][k];
pr[j + 1][k + x] = i;
df[j + 1][k + x] = x;
}
}
}
int answer = -1;
for (int i = n; answer < 0 && i >= 0; --i) {
if (dp[m][i] != 0) {
answer = i;
}
}
if (answer < 0) {
cout << "error" << endl;
} else if (dp[m][answer] >= 2) {
cout << "rejected" << endl;
} else {
cout << answer;
vector<int> cut;
int r = m, c = answer;
do {
const int d = df[r][c];
cut.push_back(d);
r = pr[r][c];
c -= d;
} while (r > 0);
for (int i = cut.size() - 1; i >= 0; --i) {
cout << " " << cut[i];
}
cout << endl;
}
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
unsigned short dp[7][1000000];
unsigned char pr[7][1000000];
int df[7][1000000];
int main() {
while (true) {
int n;
string s;
cin >> n >> s;
if (n == 0 && s == "0") {
break;
}
const int m = s.size();
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
dp[i][j] = pr[i][j] = df[i][j] = 0;
}
}
dp[0][0] = 1;
for (int i = 0; i < m; ++i) {
int x = 0;
for (int j = i; j < m; ++j) {
x = (x * 10) + s[j] - '0';
if (x > n) {
break;
}
for (int k = 0; k + x <= n; ++k) {
if (dp[i][k] == 0) {
continue;
}
dp[j + 1][k + x] += dp[i][k];
pr[j + 1][k + x] = i;
df[j + 1][k + x] = x;
}
}
}
int answer = -1;
for (int i = n; answer < 0 && i >= 0; --i) {
if (dp[m][i] != 0) {
answer = i;
}
}
if (answer < 0) {
cout << "error" << endl;
} else if (dp[m][answer] >= 2) {
cout << "rejected" << endl;
} else {
cout << answer;
vector<int> cut;
int r = m, c = answer;
do {
const int d = df[r][c];
cut.push_back(d);
r = pr[r][c];
c -= d;
} while (r > 0);
for (int i = cut.size() - 1; i >= 0; --i) {
cout << " " << cut[i];
}
cout << endl;
}
}
return 0;
} | replace | 6 | 8 | 6 | 8 | MLE | |
p00816 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int dig(int n) {
if (n < 10)
return 1;
else
return dig(n / 10) + 1;
}
int main() {
while (1) {
int t, n;
cin >> t >> n;
if (!n)
break;
int s = dig(n);
string ns = to_string(n);
vector<vector<int>> dp(s + 1, vector<int>(n + 1));
dp[0][0] = 1;
REP(i, s + 1) {
REP(j, i) {
auto sub = ns.substr(j, i - j);
int m = stoi(sub);
REP(k, n - m + 1) {
if (dp[i][k + m] == 0 && dp[j][k] > 0)
dp[i][k + m] = j + 1;
else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
dp[i][k + m] = -1;
}
}
}
bool ok = false;
for (int u = t; u > 0; --u) {
if (dp[s][u] > 0) {
vector<int> res;
int i = s;
int j = u;
do {
int oldi = i;
i = dp[i][j] - 1;
auto sub = ns.substr(i, oldi - i);
res.push_back(stoi(sub));
j -= stoi(sub);
} while (i > 0);
reverse(begin(res), end(res));
cout << u;
for (int v : res)
cout << ' ' << v;
cout << endl;
ok = true;
break;
} else if (dp[s][u] < 0) {
cout << "rejected" << endl;
ok = true;
break;
}
}
if (!ok)
cout << "error" << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int dig(int n) {
if (n < 10)
return 1;
else
return dig(n / 10) + 1;
}
int main() {
while (1) {
int t, n;
cin >> t >> n;
if (!n)
break;
t = min(t, n);
int s = dig(n);
string ns = to_string(n);
vector<vector<int>> dp(s + 1, vector<int>(n + 1));
dp[0][0] = 1;
REP(i, s + 1) {
REP(j, i) {
auto sub = ns.substr(j, i - j);
int m = stoi(sub);
REP(k, n - m + 1) {
if (dp[i][k + m] == 0 && dp[j][k] > 0)
dp[i][k + m] = j + 1;
else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
dp[i][k + m] = -1;
}
}
}
bool ok = false;
for (int u = t; u > 0; --u) {
if (dp[s][u] > 0) {
vector<int> res;
int i = s;
int j = u;
do {
int oldi = i;
i = dp[i][j] - 1;
auto sub = ns.substr(i, oldi - i);
res.push_back(stoi(sub));
j -= stoi(sub);
} while (i > 0);
reverse(begin(res), end(res));
cout << u;
for (int v : res)
cout << ' ' << v;
cout << endl;
ok = true;
break;
} else if (dp[s][u] < 0) {
cout << "rejected" << endl;
ok = true;
break;
}
}
if (!ok)
cout << "error" << endl;
}
return 0;
} | insert | 23 | 23 | 23 | 24 | 0 | |
p00817 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 305
#define MAX_E 1005
struct edge {
int to, cost;
};
vector<edge> G[MAX_V];
int V, E, A, B;
vector<vector<int>> t, u;
bool visited[MAX_V];
vector<int> a, b;
void dfs(int pos, bool flg) {
if (visited[pos])
return;
if (flg)
a.push_back(pos);
else
b.push_back(pos);
visited[pos] = true;
for (int i = 0; i < (int)G[pos].size(); i++) {
bool f = G[pos][i].cost;
dfs(G[pos][i].to, f ^ (!flg));
}
}
void make_data() {
for (int i = 0; i < MAX_V; i++)
visited[i] = false;
for (int i = 0; i < V; i++) {
if (visited[i])
continue;
a.clear();
b.clear();
dfs(i, true);
t.push_back(a);
u.push_back(b);
/*
for(int j=0;j<(int)a.size();j++)
cout<<a[j]+1<<",";
cout<<"=";
for(int j=0;j<(int)b.size();j++)
cout<<b[j]+1<<",";
cout<<endl;
*/
}
// cout<<"---"<<endl;
}
void solve() {
make_data();
int prev[MAX_V][MAX_V];
int dp[MAX_V][MAX_V], N = t.size();
for (int i = 0; i < MAX_V; i++)
for (int j = 0; j < MAX_V; j++)
dp[i][j] = 0;
dp[0][0] = 1;
for (int i = 0; i < N; i++) {
int a = t[i].size(), b = u[i].size();
for (int j = 0; j < MAX_V; j++) {
if (dp[i][j] == 0)
continue;
if (j + a < MAX_V) {
dp[i + 1][j + a] += dp[i][j];
prev[i + 1][j + a] = j;
}
if (j + b < MAX_V) {
dp[i + 1][j + b] += dp[i][j];
prev[i + 1][j + b] = j;
}
}
}
if (dp[N][A] != 1) {
cout << "no" << endl;
return;
}
vector<int> ans;
int j = A;
for (int i = N; i > 0; i--) {
int nj = prev[i][j];
if (j - nj == 0)
continue;
if (j - nj == (int)t[i - 1].size()) {
for (int k = 0; k < j - nj; k++)
ans.push_back(t[i - 1][k]);
} else {
for (int k = 0; k < j - nj; k++)
ans.push_back(u[i - 1][k]);
}
j = nj;
}
sort(ans.begin(), ans.end());
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] + 1 << endl;
cout << "end" << endl;
}
void init() {
for (int i = 0; i < MAX_V; i++) {
t.clear();
u.clear();
G[i].clear();
}
t.clear();
}
int main() {
while (cin >> E >> A >> B) {
if (E == 0 && A == 0 && B == 0)
break;
init();
V = A + B;
for (int i = 0; i < E; i++) {
int a, b;
string str;
cin >> a >> b >> str;
a--, b--;
G[a].push_back((edge){b, (str == "yes")});
G[b].push_back((edge){a, (str == "yes")});
}
// make_data();
solve();
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 605
#define MAX_E 1005
struct edge {
int to, cost;
};
vector<edge> G[MAX_V];
int V, E, A, B;
vector<vector<int>> t, u;
bool visited[MAX_V];
vector<int> a, b;
void dfs(int pos, bool flg) {
if (visited[pos])
return;
if (flg)
a.push_back(pos);
else
b.push_back(pos);
visited[pos] = true;
for (int i = 0; i < (int)G[pos].size(); i++) {
bool f = G[pos][i].cost;
dfs(G[pos][i].to, f ^ (!flg));
}
}
void make_data() {
for (int i = 0; i < MAX_V; i++)
visited[i] = false;
for (int i = 0; i < V; i++) {
if (visited[i])
continue;
a.clear();
b.clear();
dfs(i, true);
t.push_back(a);
u.push_back(b);
/*
for(int j=0;j<(int)a.size();j++)
cout<<a[j]+1<<",";
cout<<"=";
for(int j=0;j<(int)b.size();j++)
cout<<b[j]+1<<",";
cout<<endl;
*/
}
// cout<<"---"<<endl;
}
void solve() {
make_data();
int prev[MAX_V][MAX_V];
int dp[MAX_V][MAX_V], N = t.size();
for (int i = 0; i < MAX_V; i++)
for (int j = 0; j < MAX_V; j++)
dp[i][j] = 0;
dp[0][0] = 1;
for (int i = 0; i < N; i++) {
int a = t[i].size(), b = u[i].size();
for (int j = 0; j < MAX_V; j++) {
if (dp[i][j] == 0)
continue;
if (j + a < MAX_V) {
dp[i + 1][j + a] += dp[i][j];
prev[i + 1][j + a] = j;
}
if (j + b < MAX_V) {
dp[i + 1][j + b] += dp[i][j];
prev[i + 1][j + b] = j;
}
}
}
if (dp[N][A] != 1) {
cout << "no" << endl;
return;
}
vector<int> ans;
int j = A;
for (int i = N; i > 0; i--) {
int nj = prev[i][j];
if (j - nj == 0)
continue;
if (j - nj == (int)t[i - 1].size()) {
for (int k = 0; k < j - nj; k++)
ans.push_back(t[i - 1][k]);
} else {
for (int k = 0; k < j - nj; k++)
ans.push_back(u[i - 1][k]);
}
j = nj;
}
sort(ans.begin(), ans.end());
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] + 1 << endl;
cout << "end" << endl;
}
void init() {
for (int i = 0; i < MAX_V; i++) {
t.clear();
u.clear();
G[i].clear();
}
t.clear();
}
int main() {
while (cin >> E >> A >> B) {
if (E == 0 && A == 0 && B == 0)
break;
init();
V = A + B;
for (int i = 0; i < E; i++) {
int a, b;
string str;
cin >> a >> b >> str;
a--, b--;
G[a].push_back((edge){b, (str == "yes")});
G[b].push_back((edge){a, (str == "yes")});
}
// make_data();
solve();
}
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00817 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int p[302];
int r[302];
int s[302];
void init(int n) {
for (int i = 0; i < n; ++i) {
p[i] = i;
r[i] = 0;
s[i] = 1;
}
}
int find(int x) {
if (x != p[x]) {
p[x] = find(p[x]);
}
return p[x];
}
void union_set(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y]) {
p[x] = y;
s[y] += s[x];
} else {
p[y] = x;
s[x] += s[y];
if (r[x] == r[y])
r[x]++;
}
return;
}
int main() {
int n, m, p1, p2;
while (cin >> m >> p1 >> p2) {
if (m == 0 && p1 == 0 && p2 == 0)
break;
if (p1 == 0) {
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
}
cout << "end" << endl;
} else if (p2 == 0) {
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
}
for (int i = 1; i <= p1; i++) {
cout << i << endl;
}
cout << "end" << endl;
} else {
int n = p1 + p2;
init(n);
vector<pair<int, int>> node;
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
if (s == "no") {
node.PB(MP(x, y));
} else {
union_set(x, y);
}
}
vector<vector<int>> g(n);
for (auto x : node) {
g[find(x.first)].PB(find(x.second));
g[find(x.second)].PB(find(x.first));
}
vector<pair<int, int>> pa;
vector<bool> flag(n);
for (int i = 0; i < n; i++) {
if (find(i) != i) {
flag[i] = 1;
}
if (!flag[i]) {
queue<pair<int, int>> que;
vector<int> c1, c2;
que.push(MP(i, 0));
flag[i] = 1;
while (!que.empty()) {
auto x = que.front();
flag[x.first] = 1;
que.pop();
if (x.second % 2 == 0) {
c1.PB(x.first);
} else {
c2.PB(x.first);
}
for (auto y : g[x.first]) {
if (flag[y] == 0) {
que.push(MP(y, x.second + 1));
}
}
}
for (int j = 1; j < c1.size(); j++) {
union_set(c1[0], c1[j]);
}
for (int j = 1; j < c2.size(); j++) {
union_set(c2[0], c2[j]);
}
if (c2.size() == 0) {
pa.PB(MP(find(c1[0]), -1));
} else {
pa.PB(MP(find(c1[0]), find(c2[0])));
}
}
}
// for(auto x:pa){
// cout << x.first << " " << s[x.first] <<" " << x.second << " " <<
// s[x.second] << endl;
// }
int dp[601][601] = {};
dp[0][0] = 1;
int k = pa.size();
for (int i = 0; i < k; i++) {
auto x = pa[i];
if (x.second == -1) {
for (int j = 0; j <= 600; j++) {
if (dp[i][j] != 0) {
dp[i + 1][j + s[x.first]] += dp[i][j];
dp[i + 1][j] += dp[i][j];
}
}
} else {
for (int j = 0; j <= 600; j++) {
if (dp[i][j] != 0) {
dp[i + 1][j + s[x.first]] += dp[i][j];
dp[i + 1][j + s[x.second]] += dp[i][j];
}
}
}
}
if (dp[k][p1] != 1) {
cout << "no" << endl;
} else {
set<int> st;
int pos = p1;
for (int i = k - 1; i >= 0; i--) {
auto x = pa[i];
if (x.second == -1) {
if (pos - s[x.first] >= 0) {
if (dp[i][pos - s[x.first]] != 0) {
pos = pos - s[x.first];
st.insert(x.first);
}
}
} else {
bool fflag = 1;
if (pos - s[x.first] >= 0) {
if (dp[i][pos - s[x.first]] != 0) {
pos = pos - s[x.first];
st.insert(x.first);
fflag = 0;
}
}
if (fflag && pos - s[x.second] >= 0) {
if (dp[i][pos - s[x.second]] != 0) {
pos = pos - s[x.second];
st.insert(x.second);
}
}
}
}
for (int i = 0; i < n; i++) {
if (st.count(find(i))) {
cout << i + 1 << endl;
}
}
cout << "end" << endl;
}
}
}
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int p[602];
int r[602];
int s[602];
void init(int n) {
for (int i = 0; i < n; ++i) {
p[i] = i;
r[i] = 0;
s[i] = 1;
}
}
int find(int x) {
if (x != p[x]) {
p[x] = find(p[x]);
}
return p[x];
}
void union_set(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (r[x] < r[y]) {
p[x] = y;
s[y] += s[x];
} else {
p[y] = x;
s[x] += s[y];
if (r[x] == r[y])
r[x]++;
}
return;
}
int main() {
int n, m, p1, p2;
while (cin >> m >> p1 >> p2) {
if (m == 0 && p1 == 0 && p2 == 0)
break;
if (p1 == 0) {
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
}
cout << "end" << endl;
} else if (p2 == 0) {
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
}
for (int i = 1; i <= p1; i++) {
cout << i << endl;
}
cout << "end" << endl;
} else {
int n = p1 + p2;
init(n);
vector<pair<int, int>> node;
for (int i = 0; i < m; i++) {
int x, y;
string s;
cin >> x >> y >> s;
x--;
y--;
if (s == "no") {
node.PB(MP(x, y));
} else {
union_set(x, y);
}
}
vector<vector<int>> g(n);
for (auto x : node) {
g[find(x.first)].PB(find(x.second));
g[find(x.second)].PB(find(x.first));
}
vector<pair<int, int>> pa;
vector<bool> flag(n);
for (int i = 0; i < n; i++) {
if (find(i) != i) {
flag[i] = 1;
}
if (!flag[i]) {
queue<pair<int, int>> que;
vector<int> c1, c2;
que.push(MP(i, 0));
flag[i] = 1;
while (!que.empty()) {
auto x = que.front();
flag[x.first] = 1;
que.pop();
if (x.second % 2 == 0) {
c1.PB(x.first);
} else {
c2.PB(x.first);
}
for (auto y : g[x.first]) {
if (flag[y] == 0) {
que.push(MP(y, x.second + 1));
}
}
}
for (int j = 1; j < c1.size(); j++) {
union_set(c1[0], c1[j]);
}
for (int j = 1; j < c2.size(); j++) {
union_set(c2[0], c2[j]);
}
if (c2.size() == 0) {
pa.PB(MP(find(c1[0]), -1));
} else {
pa.PB(MP(find(c1[0]), find(c2[0])));
}
}
}
// for(auto x:pa){
// cout << x.first << " " << s[x.first] <<" " << x.second << " " <<
// s[x.second] << endl;
// }
int dp[601][601] = {};
dp[0][0] = 1;
int k = pa.size();
for (int i = 0; i < k; i++) {
auto x = pa[i];
if (x.second == -1) {
for (int j = 0; j <= 600; j++) {
if (dp[i][j] != 0) {
dp[i + 1][j + s[x.first]] += dp[i][j];
dp[i + 1][j] += dp[i][j];
}
}
} else {
for (int j = 0; j <= 600; j++) {
if (dp[i][j] != 0) {
dp[i + 1][j + s[x.first]] += dp[i][j];
dp[i + 1][j + s[x.second]] += dp[i][j];
}
}
}
}
if (dp[k][p1] != 1) {
cout << "no" << endl;
} else {
set<int> st;
int pos = p1;
for (int i = k - 1; i >= 0; i--) {
auto x = pa[i];
if (x.second == -1) {
if (pos - s[x.first] >= 0) {
if (dp[i][pos - s[x.first]] != 0) {
pos = pos - s[x.first];
st.insert(x.first);
}
}
} else {
bool fflag = 1;
if (pos - s[x.first] >= 0) {
if (dp[i][pos - s[x.first]] != 0) {
pos = pos - s[x.first];
st.insert(x.first);
fflag = 0;
}
}
if (fflag && pos - s[x.second] >= 0) {
if (dp[i][pos - s[x.second]] != 0) {
pos = pos - s[x.second];
st.insert(x.second);
}
}
}
}
for (int i = 0; i < n; i++) {
if (st.count(find(i))) {
cout << i + 1 << endl;
}
}
cout << "end" << endl;
}
}
}
return 0;
}
| replace | 20 | 23 | 20 | 23 | 0 | |
p00817 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, p1, p2;
while (scanf("%d%d%d", &n, &p1, &p2), n || p1 || p2) {
vector<vector<int>> G(p1 + p2 + 1);
for (int i = 0; i < n; ++i) {
int x, y;
char c;
scanf("%d%d %c%*s", &x, &y, &c);
G[x].push_back(y << 1 | (c != 'y'));
G[y].push_back(x << 1 | (c != 'y'));
}
queue<int> q;
vector<char> vis(p1 + p2 + 1);
vector<vector<int>> c[2];
for (int i = 1; i <= p1 + p2; ++i) {
if (!vis[i]) {
c[0].push_back(vector<int>());
c[1].push_back(vector<int>());
q.push(i << 1);
vis[i] = 1;
while (!q.empty()) {
int u = q.front() >> 1;
int a = q.front() & 1;
q.pop();
c[a].back().push_back(u);
for (size_t j = 0; j < G[u].size(); ++j) {
if (!vis[G[u][j] >> 1]) {
vis[G[u][j] >> 1] = 1;
q.push(G[u][j] ^ a);
}
}
}
}
}
vector<vector<int>> dp(c[0].size() + 1, vector<int>(p1 + 1));
vector<vector<int>> from(c[0].size() + 1, vector<int>(p1 + 1));
dp[0][0] = 1;
for (size_t i = 0; i < c[0].size(); ++i) {
for (int j = p1 + 1; j >= 0; --j) {
for (int k = 0; k <= 1; ++k) {
int t = j - (int)c[k][i].size();
if (t >= 0 && dp[i][t]) {
dp[i + 1][j] += dp[i][t];
from[i + 1][j] = k;
}
}
if (dp[i + 1][j] > 1) {
dp[i + 1][j] = 2;
}
}
}
if (dp.back()[p1] != 1) {
puts("no");
} else {
vector<int> ans;
int s = p1;
for (int i = c[0].size(); i > 0; --i) {
vector<int> &t = c[from[i][s]][i - 1];
ans.insert(ans.end(), t.begin(), t.end());
s -= t.size();
}
sort(ans.begin(), ans.end());
for (size_t i = 0; i < ans.size(); ++i) {
printf("%d\n", ans[i]);
}
puts("end");
}
}
} | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
int n, p1, p2;
while (scanf("%d%d%d", &n, &p1, &p2), n || p1 || p2) {
vector<vector<int>> G(p1 + p2 + 1);
for (int i = 0; i < n; ++i) {
int x, y;
char c;
scanf("%d%d %c%*s", &x, &y, &c);
G[x].push_back(y << 1 | (c != 'y'));
G[y].push_back(x << 1 | (c != 'y'));
}
queue<int> q;
vector<char> vis(p1 + p2 + 1);
vector<vector<int>> c[2];
for (int i = 1; i <= p1 + p2; ++i) {
if (!vis[i]) {
c[0].push_back(vector<int>());
c[1].push_back(vector<int>());
q.push(i << 1);
vis[i] = 1;
while (!q.empty()) {
int u = q.front() >> 1;
int a = q.front() & 1;
q.pop();
c[a].back().push_back(u);
for (size_t j = 0; j < G[u].size(); ++j) {
if (!vis[G[u][j] >> 1]) {
vis[G[u][j] >> 1] = 1;
q.push(G[u][j] ^ a);
}
}
}
}
}
vector<vector<int>> dp(c[0].size() + 1, vector<int>(p1 + 1));
vector<vector<int>> from(c[0].size() + 1, vector<int>(p1 + 1));
dp[0][0] = 1;
for (size_t i = 0; i < c[0].size(); ++i) {
for (int j = p1; j >= 0; --j) {
for (int k = 0; k <= 1; ++k) {
int t = j - (int)c[k][i].size();
if (t >= 0 && dp[i][t]) {
dp[i + 1][j] += dp[i][t];
from[i + 1][j] = k;
}
}
if (dp[i + 1][j] > 1) {
dp[i + 1][j] = 2;
}
}
}
if (dp.back()[p1] != 1) {
puts("no");
} else {
vector<int> ans;
int s = p1;
for (int i = c[0].size(); i > 0; --i) {
vector<int> &t = c[from[i][s]][i - 1];
ans.insert(ans.end(), t.begin(), t.end());
s -= t.size();
}
sort(ans.begin(), ans.end());
for (size_t i = 0; i < ans.size(); ++i) {
printf("%d\n", ans[i]);
}
puts("end");
}
}
} | replace | 46 | 47 | 46 | 47 | 0 | |
p00817 | C++ | Runtime Error | #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) (1LL << (n))
// #define DEBUG
#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf = 1LL << 62;
const ll mod = 1000000007LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll extgcd(ll a, ll b, ll &x, ll &y) {
x = 1, y = 0;
ll g = a;
if (b != 0)
g = extgcd(b, a % b, y, x), y -= a / b * x;
return g;
}
ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; }
ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; }
ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; }
ll DIV(const ll &a, const ll &b, const ll &mod) {
ll x, y;
extgcd(b, mod, x, y);
return MUL(a, x, mod);
}
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1, 6);
uniform_real_distribution<double> score(0.0, 10.0);
using Edge = tuple<int, bool>;
int n, p1, p2;
vector<vector<Edge>> edge;
vector<bool> used;
void dfs(int v, bool is_kind, map<int, bool> &dic) {
if (used[v])
return;
used[v] = true;
dic[v] = is_kind;
for (auto &e : edge[v]) {
int nv;
bool info;
tie(nv, info) = e;
bool nis_kind = is_kind ^ info;
dfs(nv, nis_kind, dic);
}
}
signed main(void) {
for (int m; cin >> m >> p1 >> p2, m or p1 or p2;) {
n = p1 + p2;
edge = vector<vector<Edge>>(n);
used = vector<bool>(n);
rep(loop, m) {
int x, y;
string a;
cin >> x >> y >> a;
x--, y--;
cerr << x << " " << y << endl;
edge[x].push_back(Edge(y, a == "no"));
edge[y].push_back(Edge(x, a == "no"));
}
vector<vi> dp(n + 1, vi(p1 + 1));
vector<map<int, bool>> dic(n + 1);
vector<vector<pair<int, bool>>> pre_idx(n + 1,
vector<pair<int, bool>>(p1 + 1));
dp[0][0] = 1;
int idx = 0;
rep(i, n) {
if (used[i])
continue;
idx++;
dfs(i, true, dic[idx]);
int cnt_kind = 0, cnt_rev = 0;
for (auto &e : dic[idx]) {
if (e.second)
cnt_kind++;
else
cnt_rev++;
}
rep(j, p1 + 1) {
if (j - cnt_kind >= 0 and
chmax(dp[idx][j], dp[idx][j] + dp[idx - 1][j - cnt_kind])) {
pre_idx[idx][j] = make_pair(j - cnt_kind, true);
}
if (j - cnt_rev >= 0 and
chmax(dp[idx][j], dp[idx][j] + dp[idx - 1][j - cnt_rev])) {
pre_idx[idx][j] = make_pair(j - cnt_rev, false);
}
}
}
if (dp[idx][p1] != 1) {
cout << "no" << endl;
continue;
}
set<int> res;
while (idx > 0) {
for (auto &e : dic[idx]) {
if (not(pre_idx[idx][p1].second ^ e.second))
res.insert(e.first);
}
p1 = pre_idx[idx][p1].first;
idx--;
}
for (auto &e : res) {
cout << e + 1 << endl;
}
cout << "end" << endl;
}
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) (1LL << (n))
// #define DEBUG
#ifdef DEBUG
#define dump(...) fprintf(stderr, __VA_ARGS__)
#else
#define dump(...)
#endif
template <class T> bool chmax(T &a, const T &b) {
return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
return (b < a) ? (a = b, 1) : 0;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf = 1LL << 62;
const ll mod = 1000000007LL;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll extgcd(ll a, ll b, ll &x, ll &y) {
x = 1, y = 0;
ll g = a;
if (b != 0)
g = extgcd(b, a % b, y, x), y -= a / b * x;
return g;
}
ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; }
ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; }
ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; }
ll DIV(const ll &a, const ll &b, const ll &mod) {
ll x, y;
extgcd(b, mod, x, y);
return MUL(a, x, mod);
}
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1, 6);
uniform_real_distribution<double> score(0.0, 10.0);
using Edge = tuple<int, bool>;
int n, p1, p2;
vector<vector<Edge>> edge;
vector<bool> used;
void dfs(int v, bool is_kind, map<int, bool> &dic) {
if (used[v])
return;
used[v] = true;
dic[v] = is_kind;
for (auto &e : edge[v]) {
int nv;
bool info;
tie(nv, info) = e;
bool nis_kind = is_kind ^ info;
dfs(nv, nis_kind, dic);
}
}
signed main(void) {
for (int m; cin >> m >> p1 >> p2, m or p1 or p2;) {
n = p1 + p2;
edge = vector<vector<Edge>>(n);
used = vector<bool>(n);
rep(loop, m) {
int x, y;
string a;
cin >> x >> y >> a;
x--, y--;
edge[x].push_back(Edge(y, a == "no"));
edge[y].push_back(Edge(x, a == "no"));
}
vector<vi> dp(n + 1, vi(p1 + 1));
vector<map<int, bool>> dic(n + 1);
vector<vector<pair<int, bool>>> pre_idx(n + 1,
vector<pair<int, bool>>(p1 + 1));
dp[0][0] = 1;
int idx = 0;
rep(i, n) {
if (used[i])
continue;
idx++;
dfs(i, true, dic[idx]);
int cnt_kind = 0, cnt_rev = 0;
for (auto &e : dic[idx]) {
if (e.second)
cnt_kind++;
else
cnt_rev++;
}
rep(j, p1 + 1) {
if (j - cnt_kind >= 0 and
chmax(dp[idx][j], dp[idx][j] + dp[idx - 1][j - cnt_kind])) {
pre_idx[idx][j] = make_pair(j - cnt_kind, true);
}
if (j - cnt_rev >= 0 and
chmax(dp[idx][j], dp[idx][j] + dp[idx - 1][j - cnt_rev])) {
pre_idx[idx][j] = make_pair(j - cnt_rev, false);
}
}
}
if (dp[idx][p1] != 1) {
cout << "no" << endl;
continue;
}
set<int> res;
while (idx > 0) {
for (auto &e : dic[idx]) {
if (not(pre_idx[idx][p1].second ^ e.second))
res.insert(e.first);
}
p1 = pre_idx[idx][p1].first;
idx--;
}
for (auto &e : res) {
cout << e + 1 << endl;
}
cout << "end" << endl;
}
return 0;
} | delete | 100 | 101 | 100 | 100 | 0 | 0 1
1 0
0 0
1 1
2 2
0 1
1 2
0 1
0 2
3 4
4 5
5 6
|
p00820 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<long long> sq;
// int memo[1<<15][200][4];
int dfs(long long sum, int pos, int num) {
if (sum == 0)
return 1;
else if (num == 4 || sum < 0)
return 0;
// if(memo[sum][pos][num]!=0) return memo[sum][pos][num];
int res = 0;
for (int i = pos; i < sq.size(); i++)
res += dfs(sum - sq[i], i, num + 1);
// return memo[sum][pos][num]=res;
return res;
}
int main() {
for (int i = 1; i * i < (1 << 15); i++)
sq.push_back(i * i);
long long n;
while (cin >> n, n) {
cout << dfs(n, 0, 0) << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
vector<long long> sq;
// int memo[1<<15][200][4];
int dfs(long long sum, int pos, int num) {
if (sum == 0)
return 1;
else if (num == 4 || sum < 0)
return 0;
// if(memo[sum][pos][num]!=0) return memo[sum][pos][num];
int res = 0;
for (int i = pos; sq[i] <= sum; i++)
res += dfs(sum - sq[i], i, num + 1);
// return memo[sum][pos][num]=res;
return res;
}
int main() {
for (int i = 1; i * i < (1 << 15); i++)
sq.push_back(i * i);
long long n;
while (cin >> n, n) {
cout << dfs(n, 0, 0) << endl;
}
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p00820 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
namespace std {
template <> struct hash<std::vector<int>> {
size_t operator()(const vector<int> &v) const {
std::hash<int> hasher;
size_t seed = 0;
for (int i : v) {
seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
} // namespace std
int main() {
int n;
unordered_set<vector<int>> used;
while (cin >> n, n) {
int ans = 0;
used.clear();
int nr = (int)sqrt(n) + 1;
rep(i, nr) {
int p = i * i;
if (p > n)
break;
rep(j, nr) {
int q = p + j * j;
if (q > n)
break;
rep(k, nr) {
int r = q + k * k;
if (r > n)
break;
rep(l, nr) {
int s = r + l * l;
if (s > n)
break;
if (s == n) {
vector<int> v = {i, j, k, l};
sort(v.begin(), v.end());
if (!used.count(v)) {
used.insert(v);
ans++;
}
}
}
}
}
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
namespace std {
template <> struct hash<std::vector<int>> {
size_t operator()(const vector<int> &v) const {
std::hash<int> hasher;
size_t seed = 0;
for (int i : v) {
seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
} // namespace std
int main() {
int n;
unordered_set<vector<int>> used;
while (cin >> n, n) {
int ans = 0;
used.clear();
int nr = (int)sqrt(n) + 1;
rep(i, nr) {
int p = i * i;
if (p > n)
break;
rep(j, nr) {
int q = p + j * j;
if (q > n)
break;
rep(k, nr) {
int r = q + k * k;
if (r > n)
break;
int l = sqrt(n - r);
if (r + l * l != n)
continue;
vector<int> v = {i, j, k, l};
sort(v.begin(), v.end());
if (!used.count(v)) {
used.insert(v);
ans++;
}
}
}
}
cout << ans << endl;
}
}
| replace | 38 | 50 | 38 | 46 | TLE | |
p00820 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int x;
while (cin >> x, x) {
int ans = 0;
FOR(i, 0, 200) {
FOR(j, i, 200) {
FOR(k, j, 200) {
FOR(l, k, 200) {
if (i * i + j * j + k * k + l * l == x)
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int x;
while (cin >> x, x) {
int ans = 0;
FOR(i, 0, 185) {
FOR(j, i, 185) {
if (i * i + j * j > x)
break;
FOR(k, j, 185) {
if (i * i + j * j + k * k > x)
break;
FOR(l, k, 185) {
if (i * i + j * j + k * k + l * l > x)
break;
if (i * i + j * j + k * k + l * l == x)
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
} | replace | 25 | 29 | 25 | 35 | TLE | |
p00820 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int dfs(int n, int sum, int k, int sn) {
int ret = 0;
if (k < 0) {
return 0;
} else {
if (sn == sum) {
return 1;
}
}
if (sn > sum || n == 0) {
return 0;
}
ret += dfs(n, sum, k - 1, sn + pow(n, 2));
ret += dfs(n - 1, sum, k, sn);
return ret;
}
int main(void) {
int n;
cin >> n;
int lim, ret;
bool find = false;
while (n != 0) {
find = false;
for (int i = 1; !(find); i++) {
if (!(pow(i, 2) < n)) {
lim = i;
find = true;
}
}
ret = dfs(lim, n, 4, 0);
cout << ret << endl;
cin >> n;
}
return 0;
} | #include <cmath>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int dfs(int n, int sum, int k, int sn) {
int ret = 0;
if (k < 0) {
return 0;
} else {
if (sn == sum) {
return 1;
}
}
if (sn > sum || n == 0) {
return 0;
}
if (sn + (k * pow(n, 2)) < sum) {
return 0;
}
ret += dfs(n, sum, k - 1, sn + pow(n, 2));
ret += dfs(n - 1, sum, k, sn);
return ret;
}
int main(void) {
int n;
cin >> n;
int lim, ret;
bool find = false;
while (n != 0) {
find = false;
for (int i = 1; !(find); i++) {
if (!(pow(i, 2) < n)) {
lim = i;
find = true;
}
}
ret = dfs(lim, n, 4, 0);
cout << ret << endl;
cin >> n;
}
return 0;
} | insert | 17 | 17 | 17 | 20 | TLE | |
p00820 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
double x, y;
Point() {
x = 0;
y = 0;
}
Point(double d_x, double d_y) { x = d_x, y = d_y; }
double operator*(Point obj) { return obj.x * x + obj.y * y; }
double operator%(Point obj) { return obj.y * x - obj.x * y; }
Point operator*(double b) {
Point tmp;
tmp.x = x * b;
tmp.y = y * b;
return tmp;
}
Point operator/(double b) {
Point tmp;
tmp.x = x / b;
tmp.y = y / b;
return tmp;
}
Point operator+(Point obj) {
Point tmp;
tmp.x = x + obj.x;
tmp.y = y + obj.y;
return tmp;
}
Point operator-() {
Point tmp;
tmp.x = -x;
tmp.y = -y;
return tmp;
}
Point operator-(Point obj) {
Point tmp;
tmp.x = x - obj.x;
tmp.y = y - obj.y;
return tmp;
}
Point operator-=(Point obj) {
x -= obj.x;
y -= obj.y;
return *this;
}
Point operator+=(Point obj) {
x += obj.x;
y += obj.y;
return *this;
}
Point operator/=(double b) {
x = x / b;
y = y / b;
return *this;
}
Point operator*=(double b) {
x = x * b;
y = y * b;
return *this;
}
double size() { return hypot(x, y); }
Point unit() { return Point(x / size(), y / size()); }
Point normal() { return Point(y, -x); }
double atan() { return atan2(y, x); }
};
bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }
bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }
int ccw(Point a, Point b, Point c) {
b = b - a;
c = c - a;
if (b % c > 0)
return +1;
else if (b % c < 0)
return -1;
else if (b * c < 0)
return +2;
else if (b.size() < c.size())
return -2;
else
return 0;
}
ll n;
int main(int argc, char const *argv[]) {
while (1) {
cin >> n;
int ans = 0;
for (ll i = 1; i * i <= n; i++) {
if (i * i == n)
ans++;
for (int j = i; j * j <= n; j++) {
if (i * i + j * j == n)
ans++;
for (int k = j; k * k <= n; k++) {
if (i * i + j * j + k * k == n)
ans++;
for (int l = k; l * l <= n; l++) {
if (i * i + j * j + k * k + l * l == n)
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
double x, y;
Point() {
x = 0;
y = 0;
}
Point(double d_x, double d_y) { x = d_x, y = d_y; }
double operator*(Point obj) { return obj.x * x + obj.y * y; }
double operator%(Point obj) { return obj.y * x - obj.x * y; }
Point operator*(double b) {
Point tmp;
tmp.x = x * b;
tmp.y = y * b;
return tmp;
}
Point operator/(double b) {
Point tmp;
tmp.x = x / b;
tmp.y = y / b;
return tmp;
}
Point operator+(Point obj) {
Point tmp;
tmp.x = x + obj.x;
tmp.y = y + obj.y;
return tmp;
}
Point operator-() {
Point tmp;
tmp.x = -x;
tmp.y = -y;
return tmp;
}
Point operator-(Point obj) {
Point tmp;
tmp.x = x - obj.x;
tmp.y = y - obj.y;
return tmp;
}
Point operator-=(Point obj) {
x -= obj.x;
y -= obj.y;
return *this;
}
Point operator+=(Point obj) {
x += obj.x;
y += obj.y;
return *this;
}
Point operator/=(double b) {
x = x / b;
y = y / b;
return *this;
}
Point operator*=(double b) {
x = x * b;
y = y * b;
return *this;
}
double size() { return hypot(x, y); }
Point unit() { return Point(x / size(), y / size()); }
Point normal() { return Point(y, -x); }
double atan() { return atan2(y, x); }
};
bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }
bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }
int ccw(Point a, Point b, Point c) {
b = b - a;
c = c - a;
if (b % c > 0)
return +1;
else if (b % c < 0)
return -1;
else if (b * c < 0)
return +2;
else if (b.size() < c.size())
return -2;
else
return 0;
}
ll n;
int main(int argc, char const *argv[]) {
while (1) {
cin >> n;
if (!n)
break;
int ans = 0;
for (ll i = 1; i * i <= n; i++) {
if (i * i == n)
ans++;
for (int j = i; j * j <= n; j++) {
if (i * i + j * j == n)
ans++;
for (int k = j; k * k <= n; k++) {
if (i * i + j * j + k * k == n)
ans++;
for (int l = k; l * l <= n; l++) {
if (i * i + j * j + k * k + l * l == n)
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
}
| insert | 119 | 119 | 119 | 121 | TLE | |
p00820 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
if (sum == n)
ans++;
else if (kai < 4 && sum < n)
for (int j = i; j * j <= n; j++)
saiki(kai + 1, sum + j * j, j);
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
ans = 0;
saiki(0, 0, 1);
cout << ans << endl;
}
return 0;
} | #include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
if (sum == n)
ans++;
else if (kai < 4 && sum < n)
for (int j = i; sum + j * j <= n; j++)
saiki(kai + 1, sum + j * j, j);
}
int main() {
while (1) {
cin >> n;
if (n == 0)
break;
ans = 0;
saiki(0, 0, 1);
cout << ans << endl;
}
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p00820 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()
// max:2192
unsigned short int dp[5][32770];
int main() {
unsigned short int i, j, k, n;
for (i = 1; i * i < (1 << 15); i++) {
dp[1][i * i] = 1;
for (j = i * i + 1; j < (1 << 15); j++) {
for (k = 1; k < 4; k++) {
dp[k + 1][j] += dp[k][j - i * i];
}
}
}
#if DEB
int mx = 0;
rep(i, 1 << 15) {
rep(j, 5) { mx = max(mx, dp[j][i]); }
}
printf("max:%d\n", mx);
#endif
while (scanf("%d", &n), n) {
printf("%d\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()
// max:2192
unsigned short int dp[5][32770];
int main() {
unsigned short int i, j, k;
int n;
for (i = 1; i * i < (1 << 15); i++) {
dp[1][i * i] = 1;
for (j = i * i + 1; j < (1 << 15); j++) {
for (k = 1; k < 4; k++) {
dp[k + 1][j] += dp[k][j - i * i];
}
}
}
#if DEB
int mx = 0;
rep(i, 1 << 15) {
rep(j, 5) { mx = max(mx, dp[j][i]); }
}
printf("max:%d\n", mx);
#endif
while (scanf("%d", &n), n) {
printf("%d\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
}
return 0;
} | replace | 15 | 16 | 15 | 17 | 0 | |
p00820 | C++ | Runtime Error | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
int n, ans[1 << 15];
void f(int cur, int n, int pre) {
if (n >= 1 << 15)
return;
ans[n]++;
if (cur == 4)
return;
rep2(i, pre, INF) { f(cur + 1, n + i * i, i); }
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
f(0, 0, 1);
while (cin >> n && n) {
cout << ans[n] << endl;
}
} | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
int n, ans[1 << 15];
void f(int cur, int n, int pre) {
if (n >= 1 << 15)
return;
ans[n]++;
if (cur == 4)
return;
rep2(i, pre, 182) { f(cur + 1, n + i * i, i); }
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
f(0, 0, 1);
while (cin >> n && n) {
cout << ans[n] << endl;
}
} | replace | 34 | 35 | 34 | 35 | -11 | |
p00820 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
#include <vector>
#define MAX 32768
using namespace std;
vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
int tmp;
for (int i = 0; i <= MAX; i++) {
isSqNum[i] = false;
}
sq.clear();
for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
sq.push_back(tmp);
isSqNum[tmp] = true;
}
}
int n;
int ans;
void rec(int cnt, int sum, int level) {
if (cnt >= 4)
return;
if (sum > n)
return;
if (n - sum >= sq[level] && isSqNum[n - sum]) {
ans++;
}
for (int i = level; i < sq.size(); i++) {
rec(cnt + 1, sum + sq[i], i);
}
}
int main() {
makeTable();
while (1) {
cin >> n;
if (n == 0)
break;
ans = 0;
rec(0, 0, 0);
cout << ans << endl;
}
return 0;
} | #include <cmath>
#include <iostream>
#include <vector>
#define MAX 32768
using namespace std;
vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
int tmp;
for (int i = 0; i <= MAX; i++) {
isSqNum[i] = false;
}
sq.clear();
for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
sq.push_back(tmp);
isSqNum[tmp] = true;
}
}
int n;
int ans;
void rec(int cnt, int sum, int level) {
if (cnt >= 4)
return;
if (sum > n)
return;
if (n - sum >= sq[level] && isSqNum[n - sum]) {
ans++;
}
for (int i = level; i < sq.size() && sum + sq[i] <= n; i++) {
rec(cnt + 1, sum + sq[i], i);
}
}
int main() {
makeTable();
while (1) {
cin >> n;
if (n == 0)
break;
ans = 0;
rec(0, 0, 0);
cout << ans << endl;
}
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p00820 | C++ | Memory Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
const int MAX = 33000;
const int SQRT = 200;
int dp[MAX][5][SQRT];
int main() {
dp[0][0][1] = 1;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 1; k < SQRT; k++)
if (dp[i][j][k] > 0) {
for (int l = k; l < SQRT; l++) { // l*lを加算
if (i + l * l >= MAX || j + 1 >= 5)
break;
dp[i + l * l][j + 1][l] += dp[i][j][k];
}
}
}
}
int n;
while (cin >> n, n) {
int ans = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < SQRT; j++)
ans += dp[n][i][j];
}
cout << ans << endl;
}
}
| #include <cmath>
#include <iostream>
using namespace std;
const int MAX = 32770; // 1<<15
const int SQRT = 182;
int dp[MAX][5][SQRT];
int main() {
dp[0][0][1] = 1;
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 1; k < SQRT; k++)
if (dp[i][j][k] > 0) {
for (int l = k; l < SQRT; l++) { // l*lを加算
if (i + l * l >= MAX || j + 1 >= 5)
break;
dp[i + l * l][j + 1][l] += dp[i][j][k];
}
}
}
}
int n;
while (cin >> n, n) {
int ans = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < SQRT; j++)
ans += dp[n][i][j];
}
cout << ans << endl;
}
}
| replace | 4 | 6 | 4 | 6 | MLE | |
p00820 | C++ | Memory Limit Exceeded | #define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#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;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x) \
int x; \
cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a) \
FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};
short memo[1 << 15][200][5];
int x;
int dp(int a, int n, int now) {
if (now <= 4 && a == x)
return 1;
if (now >= 4 || x - a < n * n || a > x)
return 0;
if (memo[a][n][now] != -1)
return memo[a][n][now];
int ans = 0;
for (int i = 0; i <= 4; i++) {
ans += dp(a + n * n * i, n + 1, now + i);
}
return memo[a][n][now] = ans;
}
signed main() {
while (cin >> x && x) {
memset(memo, -1, sizeof memo);
cout << dp(0, 1, 0) << endl;
}
} | #define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#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;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x) \
int x; \
cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a) \
FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};
short memo[1 << 15][182][5];
int x;
int dp(int a, int n, int now) {
if (now <= 4 && a == x)
return 1;
if (now >= 4 || x - a < n * n || a > x)
return 0;
if (memo[a][n][now] != -1)
return memo[a][n][now];
int ans = 0;
for (int i = 0; i <= 4; i++) {
ans += dp(a + n * n * i, n + 1, now + i);
}
return memo[a][n][now] = ans;
}
signed main() {
while (cin >> x && x) {
memset(memo, -1, sizeof memo);
cout << dp(0, 1, 0) << endl;
}
} | replace | 42 | 43 | 42 | 43 | MLE | |
p00820 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
int n;
int dfs(int sum, int d, int c) {
// printf("%d %d\n", sum, c);
if (sum == n)
return 1;
if (c >= 4)
return 0;
int res = 0;
REP(i, d, sqrt(n) + 1) {
if (sum + i * i > n)
break;
;
res += dfs(sum + i * i, i, c + 1);
}
return res;
}
int main() {
while (scanf("%d", &n) && n) {
printf("%d\n", dfs(0, 1, 0));
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
int n;
int dfs(int sum, int d, int c) {
// printf("%d %d\n", sum, c);
if (sum == n)
return 1;
if (c >= 4)
return 0;
int res = 0;
for (int i = d; i * i <= n; i++) {
if (sum + i * i > n)
break;
;
res += dfs(sum + i * i, i, c + 1);
}
return res;
}
int main() {
while (scanf("%d", &n) && n) {
printf("%d\n", dfs(0, 1, 0));
}
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p00821 | C++ | Memory Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
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"
struct point {
int x;
int y;
int id;
};
struct line {
int fromx;
int tox;
int id;
ld ny;
ld fromy;
ld toy;
ld getdy() const { return (toy - fromy) / (tox - fromx); }
};
bool operator<(const line &l, const line &r) {
return abs(l.ny - r.ny) < eps ? l.getdy() < r.getdy() : l.ny < r.ny;
}
const int aa = 2001;
const int bb = 0;
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
vector<vector<point>> xs(2 * aa + 1);
vector<point> ps;
for (int i = 0; i < N; ++i) {
int x, y;
cin >> x >> y;
x -= bb;
x += aa;
y += aa;
y = 2 * aa - y;
xs[x].push_back(point{x, y, i});
ps.push_back(point{x, y, i});
}
vector<line> nlines;
vector<vector<int>> oks(2 * aa, vector<int>(2 * aa));
for (int nx = 0; nx < aa * 2 + 1; ++nx) {
for (auto p : xs[nx]) {
for (int i = 0; i < 2; ++i) {
int tarid;
if (i == 0)
tarid = (p.id + 1) % N;
else
tarid = (p.id + N - 1) % N;
if (nx < ps[tarid].x) {
const line aline{p.x, ps[tarid].x, tarid,
ld(p.y), ld(p.y), ld(ps[tarid].y)};
auto it = lower_bound(nlines.begin(), nlines.end(), aline);
nlines.insert(it, aline);
} else if (nx > ps[tarid].x) {
nlines.erase(find_if(nlines.begin(), nlines.end(),
[=](const line &l) { return l.id == p.id; }));
}
}
}
for (auto &l : nlines) {
const ld dy = (l.toy - l.fromy) / (l.tox - l.fromx);
int fy, ty;
if (abs(dy) < eps)
continue;
else if (dy > 0) {
fy = int(l.ny + eps);
ty = int(l.ny + dy - eps);
} else {
fy = int(l.ny + dy + eps);
ty = int(l.ny - eps);
}
l.ny = l.ny + dy;
for (int y = fy; y <= ty; ++y) {
oks[y][nx] = true;
}
}
for (int i = 0; i < nlines.size(); i += 2) {
int fy = int(nlines[i].ny + eps);
int ty = int(nlines[i + 1].ny - eps);
for (int y = fy; y <= ty; ++y) {
oks[y][nx] = true;
}
}
}
int ans = 0;
for (int y = 0; y < 2 * aa; ++y) {
for (int x = 0; x < 2 * aa; ++x) {
ans += oks[y][x];
/*if (oks[y][x])cout << "#";
else cout << ".";*/
}
// cout << endl;
}
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
struct point {
int x;
int y;
int id;
};
struct line {
int fromx;
int tox;
int id;
ld ny;
ld fromy;
ld toy;
ld getdy() const { return (toy - fromy) / (tox - fromx); }
};
bool operator<(const line &l, const line &r) {
return abs(l.ny - r.ny) < eps ? l.getdy() < r.getdy() : l.ny < r.ny;
}
const int aa = 2001;
const int bb = 0;
int main() {
while (1) {
int N;
cin >> N;
if (!N)
break;
vector<vector<point>> xs(2 * aa + 1);
vector<point> ps;
for (int i = 0; i < N; ++i) {
int x, y;
cin >> x >> y;
x -= bb;
x += aa;
y += aa;
y = 2 * aa - y;
xs[x].push_back(point{x, y, i});
ps.push_back(point{x, y, i});
}
vector<line> nlines;
vector<vector<bool>> oks(2 * aa, vector<bool>(2 * aa));
for (int nx = 0; nx < aa * 2 + 1; ++nx) {
for (auto p : xs[nx]) {
for (int i = 0; i < 2; ++i) {
int tarid;
if (i == 0)
tarid = (p.id + 1) % N;
else
tarid = (p.id + N - 1) % N;
if (nx < ps[tarid].x) {
const line aline{p.x, ps[tarid].x, tarid,
ld(p.y), ld(p.y), ld(ps[tarid].y)};
auto it = lower_bound(nlines.begin(), nlines.end(), aline);
nlines.insert(it, aline);
} else if (nx > ps[tarid].x) {
nlines.erase(find_if(nlines.begin(), nlines.end(),
[=](const line &l) { return l.id == p.id; }));
}
}
}
for (auto &l : nlines) {
const ld dy = (l.toy - l.fromy) / (l.tox - l.fromx);
int fy, ty;
if (abs(dy) < eps)
continue;
else if (dy > 0) {
fy = int(l.ny + eps);
ty = int(l.ny + dy - eps);
} else {
fy = int(l.ny + dy + eps);
ty = int(l.ny - eps);
}
l.ny = l.ny + dy;
for (int y = fy; y <= ty; ++y) {
oks[y][nx] = true;
}
}
for (int i = 0; i < nlines.size(); i += 2) {
int fy = int(nlines[i].ny + eps);
int ty = int(nlines[i + 1].ny - eps);
for (int y = fy; y <= ty; ++y) {
oks[y][nx] = true;
}
}
}
int ans = 0;
for (int y = 0; y < 2 * aa; ++y) {
for (int x = 0; x < 2 * aa; ++x) {
ans += oks[y][x];
/*if (oks[y][x])cout << "#";
else cout << ".";*/
}
// cout << endl;
}
cout << ans << endl;
}
return 0;
} | replace | 52 | 53 | 52 | 53 | MLE | |
p00821 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <iostream>
#include <list>
#include <set>
#include <vector>
using namespace std;
typedef double elem;
typedef complex<elem> point, vec;
typedef pair<point, point> line, hline, seg, pp;
enum CCW { BACK = 0x01, FRONT = 0x02, OVER = 0x04, LEFT = 0x08, RIGHT = 0x10 };
const elem infty = 1e40;
const double eps = 1e-8;
bool eq(double a, double b) { return abs(b - a) < eps; }
elem cross(vec a, vec b) { return a.real() * b.imag() - a.imag() * b.real(); }
elem dot(vec a, vec b) { return a.real() * b.real() + a.imag() * b.imag(); }
elem dist_l(line l, point x) {
return abs(cross(l.second - l.first, x - l.first)) / abs(l.second - l.first);
}
inline int ccw(const point &a, point b, point x) {
b -= a;
x -= a;
if (eq(cross(b, x), 0) && dot(b, x) < 0)
return BACK;
if (eq(cross(b, x), 0) && abs(b) < abs(x))
return FRONT;
if (eq(cross(b, x), 0))
return OVER;
if (cross(b, x) > 0)
return LEFT;
else
return RIGHT;
}
inline bool intersectedSS(const seg &a, const seg &b) {
int cwaf = ccw(a.first, a.second, b.first);
int cwbf = ccw(b.first, b.second, a.first);
int cwas = ccw(a.first, a.second, b.second);
int cwbs = ccw(b.first, b.second, a.second);
if (cwaf == OVER || cwas == OVER || cwbf == OVER || cwbs == OVER)
return true;
return (cwaf | cwas) == (LEFT | RIGHT) && (cwbf | cwbs) == (LEFT | RIGHT);
}
point intersectionSS(seg a, seg b) {
elem d1 = dist_l(b, a.first);
elem d2 = dist_l(b, a.second);
return a.first + (d1 / (d1 + d2)) * (a.second - a.first);
}
bool intersectionSS(seg a, seg b, point &ret) {
return intersectedSS(a, b) ? ret = intersectionSS(a, b), true : false;
}
bool validPoint(const point &p) {
return -2002 <= p.real() && p.real() <= 2002 && -2002 <= p.imag() &&
p.imag() <= 2002;
}
bool compElemPair(const pair<elem, elem> &a, const pair<elem, elem> &b) {
if (!eq(a.first, b.first))
return a.first < b.first;
if (!eq(a.second, b.second))
return a.second < b.second;
assert(false);
return false;
}
int main() {
int m;
while (cin >> m && m) {
vector<point> vp;
int res = 0;
for (int i = 0; i < m; ++i) {
elem x, y;
cin >> x >> y;
// assert(-2000<=x&&x<=2000&&-2000<=y&&y<=2000);
vp.push_back(point(x, y));
}
for (int y = -2000; y < 2000; ++y) {
seg lower(point(-2001, y), point(2001, y));
seg center(point(-2001, y + 0.5), point(2001, y + 0.5));
seg upper(point(-2001, y + 1), point(2001, y + 1));
vector<pair<elem, elem>> vx;
for (int i = 0; i < (int)vp.size(); ++i) {
point a = vp[i];
point b = vp[(i + 1) % vp.size()];
seg s(a, b);
if (intersectedSS(s, center)) {
point is;
elem left, right;
if (intersectionSS(lower, s, is) && validPoint(is)) {
left = is.real();
right = is.real();
}
if (intersectionSS(upper, s, is) && validPoint(is)) {
left = min(left, is.real());
right = max(right, is.real());
}
vx.push_back(make_pair(left, right));
}
}
sort(vx.begin(), vx.end(), compElemPair);
int prev = -2001;
for (int i = 0; i < (int)vx.size() - 1; i += 2) {
int left_x = (int)floor(min(vx[i].first, vx[i].second) + eps);
int right_x = (int)ceil(max(vx[i + 1].first, vx[i + 1].second) - eps);
int tmp = right_x - max(left_x, prev);
if (tmp >= 0)
res += tmp;
prev = right_x;
}
}
cout << res << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <iostream>
#include <list>
#include <set>
#include <vector>
using namespace std;
typedef double elem;
typedef complex<elem> point, vec;
typedef pair<point, point> line, hline, seg, pp;
enum CCW { BACK = 0x01, FRONT = 0x02, OVER = 0x04, LEFT = 0x08, RIGHT = 0x10 };
const elem infty = 1e40;
const double eps = 1e-8;
bool eq(double a, double b) { return abs(b - a) < eps; }
elem cross(vec a, vec b) { return a.real() * b.imag() - a.imag() * b.real(); }
elem dot(vec a, vec b) { return a.real() * b.real() + a.imag() * b.imag(); }
elem dist_l(line l, point x) {
return abs(cross(l.second - l.first, x - l.first)) / abs(l.second - l.first);
}
inline int ccw(const point &a, point b, point x) {
b -= a;
x -= a;
if (eq(cross(b, x), 0) && dot(b, x) < 0)
return BACK;
if (eq(cross(b, x), 0) && abs(b) < abs(x))
return FRONT;
if (eq(cross(b, x), 0))
return OVER;
if (cross(b, x) > 0)
return LEFT;
else
return RIGHT;
}
inline bool intersectedSS(const seg &a, const seg &b) {
int cwaf = ccw(a.first, a.second, b.first);
int cwbf = ccw(b.first, b.second, a.first);
int cwas = ccw(a.first, a.second, b.second);
int cwbs = ccw(b.first, b.second, a.second);
if (cwaf == OVER || cwas == OVER || cwbf == OVER || cwbs == OVER)
return true;
return (cwaf | cwas) == (LEFT | RIGHT) && (cwbf | cwbs) == (LEFT | RIGHT);
}
point intersectionSS(seg a, seg b) {
elem d1 = dist_l(b, a.first);
elem d2 = dist_l(b, a.second);
return a.first + (d1 / (d1 + d2)) * (a.second - a.first);
}
bool intersectionSS(seg a, seg b, point &ret) {
return intersectedSS(a, b) ? ret = intersectionSS(a, b), true : false;
}
bool validPoint(const point &p) {
return -2002 <= p.real() && p.real() <= 2002 && -2002 <= p.imag() &&
p.imag() <= 2002;
}
bool compElemPair(const pair<elem, elem> &a, const pair<elem, elem> &b) {
if (!eq(a.first, b.first))
return a.first < b.first;
if (!eq(a.second, b.second))
return a.second < b.second;
// assert(false);
return false;
}
int main() {
int m;
while (cin >> m && m) {
vector<point> vp;
int res = 0;
for (int i = 0; i < m; ++i) {
elem x, y;
cin >> x >> y;
// assert(-2000<=x&&x<=2000&&-2000<=y&&y<=2000);
vp.push_back(point(x, y));
}
for (int y = -2000; y < 2000; ++y) {
seg lower(point(-2001, y), point(2001, y));
seg center(point(-2001, y + 0.5), point(2001, y + 0.5));
seg upper(point(-2001, y + 1), point(2001, y + 1));
vector<pair<elem, elem>> vx;
for (int i = 0; i < (int)vp.size(); ++i) {
point a = vp[i];
point b = vp[(i + 1) % vp.size()];
seg s(a, b);
if (intersectedSS(s, center)) {
point is;
elem left, right;
if (intersectionSS(lower, s, is) && validPoint(is)) {
left = is.real();
right = is.real();
}
if (intersectionSS(upper, s, is) && validPoint(is)) {
left = min(left, is.real());
right = max(right, is.real());
}
vx.push_back(make_pair(left, right));
}
}
sort(vx.begin(), vx.end(), compElemPair);
int prev = -2001;
for (int i = 0; i < (int)vx.size() - 1; i += 2) {
int left_x = (int)floor(min(vx[i].first, vx[i].second) + eps);
int right_x = (int)ceil(max(vx[i + 1].first, vx[i + 1].second) - eps);
int tmp = right_x - max(left_x, prev);
if (tmp >= 0)
res += tmp;
prev = right_x;
}
}
cout << res << endl;
}
return 0;
} | replace | 69 | 70 | 69 | 70 | 0 | |
p00821 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define X real()
#define Y imag()
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> Pol;
typedef pair<int, int> Pi;
D eps = 1e-9;
bool eq(D a, D b) { return abs(a - b) < eps; }
int sig(D x) { return eq(x, 0) ? 0 : (x > 0 ? 1 : -1); }
D dot(P a, P b) { return real(conj(a) * b); }
D cro(P a, P b) { return imag(conj(a) * b); }
int ccw(P a, P b, P c) {
if (sig(cro(b - a, c - a)) == 1)
return 1;
if (sig(cro(b - a, c - a)) == -1)
return -1;
if (eq(abs(a - c) + abs(c - b), abs(a - b)))
return 0;
if (eq(abs(a - b) + abs(c - b), abs(a - c)))
return -2;
return 2;
}
bool ispal(L a, L b) { return sig(cro(a.fs - a.sc, b.fs - b.sc)) == 0; }
bool iLSex(L l, L s) {
return sig(cro(l.sc - l.fs, s.fs - l.fs) * cro(l.sc - l.fs, s.sc - l.fs)) < 0;
}
P intLL(L a, L b) {
D t = cro(a.sc - a.fs, a.sc - b.fs) / cro(a.sc - a.fs, b.sc - b.fs);
return b.fs + t * (b.sc - b.fs);
}
int len(vector<Pi> vc) {
int N = vc.size();
if (N == 0)
return 0;
sort(all(vc));
int ret = 0;
int l = vc[0].fs, r = vc[0].sc;
rep(i, N) {
if (r < vc[i].fs) {
ret += r - l;
l = vc[i].fs;
r = vc[i].sc;
} else {
chmax(r, vc[i].sc);
}
}
ret += r - l;
return ret;
}
int N;
vector<Pi> segs[4001];
Pol pol;
vector<D> enumxs(Pol pol, D y) {
L l = L(P(0, y), P(1, y));
vector<D> ds;
int N = pol.size();
rep(i, N) {
L s = L(pol[i], (pol[(i + 1) % N]));
if (!ispal(l, s) && iLSex(l, s))
ds.pb(intLL(l, s).X);
}
sort(all(ds));
return ds;
}
int main() {
while (true) {
cin >> N;
if (N == 0)
break;
pol.clear();
rep(i, N) {
int x, y;
cin >> x >> y;
pol.pb(P(x, y));
}
int ans = 0;
for (int y = -2000; y < 2000; y++) {
vector<D> a = enumxs(pol, y + eps);
vector<D> b = enumxs(pol, y + 1 - eps);
assert(a.size() == b.size());
int K = a.size();
assert(K % 2 == 0);
vector<Pi> segs;
rep(i, K / 2) {
int le = floor(min(a[i * 2], b[i * 2]) + eps),
ri = ceil(max(a[i * 2 + 1], b[i * 2 + 1]) - eps);
segs.pb(Pi(le, ri));
}
ans += len(segs);
/* if(segs.size()){
show(y);
for(Pi p:segs) printf("(%d,%d)
",p.fs,p.sc); puts("");
}*/
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define X real()
#define Y imag()
using namespace std;
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> Pol;
typedef pair<int, int> Pi;
D eps = 1e-9;
bool eq(D a, D b) { return abs(a - b) < eps; }
int sig(D x) { return eq(x, 0) ? 0 : (x > 0 ? 1 : -1); }
D dot(P a, P b) { return real(conj(a) * b); }
D cro(P a, P b) { return imag(conj(a) * b); }
int ccw(P a, P b, P c) {
if (sig(cro(b - a, c - a)) == 1)
return 1;
if (sig(cro(b - a, c - a)) == -1)
return -1;
if (eq(abs(a - c) + abs(c - b), abs(a - b)))
return 0;
if (eq(abs(a - b) + abs(c - b), abs(a - c)))
return -2;
return 2;
}
bool ispal(L a, L b) { return sig(cro(a.fs - a.sc, b.fs - b.sc)) == 0; }
bool iLSex(L l, L s) {
return sig(cro(l.sc - l.fs, s.fs - l.fs) * cro(l.sc - l.fs, s.sc - l.fs)) < 0;
}
P intLL(L a, L b) {
D t = cro(a.sc - a.fs, a.sc - b.fs) / cro(a.sc - a.fs, b.sc - b.fs);
return b.fs + t * (b.sc - b.fs);
}
int len(vector<Pi> vc) {
int N = vc.size();
if (N == 0)
return 0;
sort(all(vc));
int ret = 0;
int l = vc[0].fs, r = vc[0].sc;
rep(i, N) {
if (r < vc[i].fs) {
ret += r - l;
l = vc[i].fs;
r = vc[i].sc;
} else {
chmax(r, vc[i].sc);
}
}
ret += r - l;
return ret;
}
int N;
vector<Pi> segs[4001];
Pol pol;
vector<D> enumxs(Pol pol, D y) {
L l = L(P(0, y), P(1, y));
vector<D> ds;
int N = pol.size();
rep(i, N) {
L s = L(pol[i], (pol[(i + 1) % N]));
if (!ispal(l, s) && iLSex(l, s))
ds.pb(intLL(l, s).X);
}
sort(all(ds));
return ds;
}
int main() {
while (true) {
cin >> N;
if (N == 0)
break;
pol.clear();
rep(i, N) {
int x, y;
cin >> x >> y;
pol.pb(P(x, y));
}
int ans = 0;
for (int y = -2000; y < 2000; y++) {
vector<D> a = enumxs(pol, y + eps * 100);
vector<D> b = enumxs(pol, y + 1 - eps * 100);
assert(a.size() == b.size());
int K = a.size();
assert(K % 2 == 0);
vector<Pi> segs;
rep(i, K / 2) {
int le = floor(min(a[i * 2], b[i * 2]) + eps),
ri = ceil(max(a[i * 2 + 1], b[i * 2 + 1]) - eps);
segs.pb(Pi(le, ri));
}
ans += len(segs);
/* if(segs.size()){
show(y);
for(Pi p:segs) printf("(%d,%d)
",p.fs,p.sc); puts("");
}*/
}
cout << ans << endl;
}
} | replace | 89 | 91 | 89 | 91 | 0 | |
p00822 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[9] = {1, 0, -1, 0, 0, 0, 2, -2, 0};
int dy[9] = {0, 1, 0, -1, 0, 2, 0, 0, -2};
int main() {
int D;
int sch[365][4][4] = {};
while (cin >> D && D) {
bool goal = false;
REP(i, D) REP(y, 4) REP(x, 4) cin >> sch[i][y][x];
static bool used[366][16][2401];
memset(used, 0, sizeof(used));
queue<int> qd, qy, qx;
queue<vector<int>> qv;
vector<int> s(4, 0);
qd.push(0);
qx.push(1);
qy.push(1);
qv.push(s);
while (!qd.empty()) {
int d = qd.front();
qd.pop();
int x = qx.front();
qx.pop();
int y = qy.front();
qy.pop();
// printf("d = %d x = %d y = %d\n", d, x, y);
vector<int> v = qv.front();
qv.pop();
if (d == D) {
goal = true;
break;
}
REP(i, 4) v[i]++;
bool ok = true;
REP(dy, 2) REP(dx, 2) if (sch[d][y + dy][x + dx] == 1) ok = false;
if (x == 0 && y == 0)
v[0] = 0;
if (x == 2 && y == 0)
v[1] = 0;
if (x == 0 && y == 2)
v[2] = 0;
if (x == 2 && y == 2)
v[3] = 0;
REP(i, 4) if (v[i] >= 7) ok = false;
if (!ok)
continue;
REP(r, 9) {
bool ok2 = true;
int nx = x + dx[r];
int ny = y + dy[r];
REP(dx, 2) REP(dy, 2) ok2 &= valid(nx + dx, ny + dy, 4, 4);
if (!ok2)
continue;
if (used[d + 1][ny + nx * 4][v[0] + 7 * v[1] + 49 * v[2] + 343 * v[3]])
continue;
qd.push(d + 1);
qx.push(nx);
qy.push(ny);
qv.push(v);
}
}
if (goal)
cout << 1 << endl;
else
cout << 0 << endl;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
template <class T> void debug(T begin, T end) {
for (T i = begin; i != end; ++i)
cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[9] = {1, 0, -1, 0, 0, 0, 2, -2, 0};
int dy[9] = {0, 1, 0, -1, 0, 2, 0, 0, -2};
int main() {
int D;
int sch[365][4][4] = {};
while (cin >> D && D) {
bool goal = false;
REP(i, D) REP(y, 4) REP(x, 4) cin >> sch[i][y][x];
static bool used[366][16][2401];
memset(used, 0, sizeof(used));
queue<int> qd, qy, qx;
queue<vector<int>> qv;
vector<int> s(4, 0);
qd.push(0);
qx.push(1);
qy.push(1);
qv.push(s);
while (!qd.empty()) {
int d = qd.front();
qd.pop();
int x = qx.front();
qx.pop();
int y = qy.front();
qy.pop();
// printf("d = %d x = %d y = %d\n", d, x, y);
vector<int> v = qv.front();
qv.pop();
if (d == D) {
goal = true;
break;
}
REP(i, 4) v[i]++;
bool ok = true;
REP(dy, 2) REP(dx, 2) if (sch[d][y + dy][x + dx] == 1) ok = false;
if (x == 0 && y == 0)
v[0] = 0;
if (x == 2 && y == 0)
v[1] = 0;
if (x == 0 && y == 2)
v[2] = 0;
if (x == 2 && y == 2)
v[3] = 0;
REP(i, 4) if (v[i] >= 7) ok = false;
if (!ok)
continue;
REP(r, 9) {
bool ok2 = true;
int nx = x + dx[r];
int ny = y + dy[r];
REP(dx, 2) REP(dy, 2) ok2 &= valid(nx + dx, ny + dy, 4, 4);
if (!ok2)
continue;
if (used[d + 1][ny + nx * 4][v[0] + 7 * v[1] + 49 * v[2] + 343 * v[3]])
continue;
used[d + 1][ny + nx * 4][v[0] + 7 * v[1] + 49 * v[2] + 343 * v[3]] =
true;
qd.push(d + 1);
qx.push(nx);
qy.push(ny);
qv.push(v);
}
}
if (goal)
cout << 1 << endl;
else
cout << 0 << endl;
}
return 0;
} | insert | 90 | 90 | 90 | 92 | MLE | |
p00822 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);
typedef vector<vvi> vvvi;
bool satisfied(vvi &field, int y, int x) {
FOR(i, y, y + 2) {
FOR(j, x, x + 2) {
if (field[i][j]) {
return false;
}
}
}
return true;
}
int h(int y, int x) { return y * 3 + x; }
bool thirsty(vi pos) {
vvi wetness(4, vi(4));
REP(i, pos.size()) {
int y = pos[i] / 3;
int x = pos[i] % 3;
FOR(j, y, y + 2) {
FOR(k, x, x + 2) { wetness[j][k]++; }
}
}
REP(i, 4) {
REP(j, 4) {
if (wetness[i][j] == 0) {
return true;
}
}
}
return false;
}
int dp[2][9][9][9][9][9][9];
int main() {
int N;
while (cin >> N, N) {
REP(day, 2)
REP(i, 9) REP(j, 9) REP(k, 9) REP(l, 9) REP(m, 9) REP(n, 9)
dp[day][i][j][k][l][m][n] = 0;
int d = 0;
REP(day, N) {
vvi field(4, vi(4));
REP(i, 4) {
REP(j, 4) { cin >> field[i][j]; }
}
REP(i, 9)
REP(j, 9) REP(k, 9) REP(l, 9) REP(m, 9) REP(n, 9)
dp[d][i][j][k][l][m][n] = 0;
if (day == 0) {
dp[d][0][0][0][0][0][h(1, 1)] = satisfied(field, 1, 1);
} else {
REP(i, 9) {
REP(j, 9) {
REP(k, 9) {
REP(l, 9) {
REP(m, 9) {
REP(n, 9) {
int py = n / 3, px = n % 3;
REP(o, 9) {
int cy = o / 3, cx = o % 3;
if (cy == py || cx == px) {
if (dp[(d + 1) % 2][i][j][k][l][m][n]) {
if (day >= 6) {
vi pos;
pos.push_back(i);
pos.push_back(j);
pos.push_back(k);
pos.push_back(l);
pos.push_back(m);
pos.push_back(n);
pos.push_back(o);
if (thirsty(pos))
continue;
}
dp[d][j][k][l][m][n][o] |= satisfied(field, cy, cx);
}
}
}
}
}
}
}
}
}
}
d = (d + 1) % 2;
}
bool ok = false;
REP(i, 9) {
REP(j, 9) {
REP(k, 9) {
REP(l, 9) {
REP(m, 9) {
REP(n, 9) { ok |= dp[(d + 1) % 2][i][j][k][l][m][n]; }
}
}
}
}
}
cout << ok << endl;
}
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);
typedef vector<vvi> vvvi;
bool satisfied(vvi &field, int y, int x) {
FOR(i, y, y + 2) {
FOR(j, x, x + 2) {
if (field[i][j]) {
return false;
}
}
}
return true;
}
int h(int y, int x) { return y * 3 + x; }
bool thirsty(vi pos) {
vvi wetness(4, vi(4));
REP(i, pos.size()) {
int y = pos[i] / 3;
int x = pos[i] % 3;
FOR(j, y, y + 2) {
FOR(k, x, x + 2) { wetness[j][k]++; }
}
}
REP(i, 4) {
REP(j, 4) {
if (wetness[i][j] == 0) {
return true;
}
}
}
return false;
}
int dp[2][9][9][9][9][9][9];
int main() {
int N;
while (cin >> N, N) {
REP(day, 2)
REP(i, 9) REP(j, 9) REP(k, 9) REP(l, 9) REP(m, 9) REP(n, 9)
dp[day][i][j][k][l][m][n] = 0;
int d = 0;
REP(day, N) {
vvi field(4, vi(4));
REP(i, 4) {
REP(j, 4) { cin >> field[i][j]; }
}
REP(i, 9)
REP(j, 9) REP(k, 9) REP(l, 9) REP(m, 9) REP(n, 9)
dp[d][i][j][k][l][m][n] = 0;
if (day == 0) {
dp[d][0][0][0][0][0][h(1, 1)] = satisfied(field, 1, 1);
} else {
REP(i, 9) {
REP(j, 9) {
REP(k, 9) {
REP(l, 9) {
REP(m, 9) {
REP(n, 9) {
if (dp[(d + 1) % 2][i][j][k][l][m][n]) {
int py = n / 3, px = n % 3;
REP(o, 9) {
int cy = o / 3, cx = o % 3;
if (cy == py || cx == px) {
if (day >= 6) {
vi pos;
pos.push_back(i);
pos.push_back(j);
pos.push_back(k);
pos.push_back(l);
pos.push_back(m);
pos.push_back(n);
pos.push_back(o);
if (thirsty(pos))
continue;
}
dp[d][j][k][l][m][n][o] |= satisfied(field, cy, cx);
}
}
}
}
}
}
}
}
}
}
d = (d + 1) % 2;
}
bool ok = false;
REP(i, 9) {
REP(j, 9) {
REP(k, 9) {
REP(l, 9) {
REP(m, 9) {
REP(n, 9) { ok |= dp[(d + 1) % 2][i][j][k][l][m][n]; }
}
}
}
}
}
cout << ok << endl;
}
} | replace | 107 | 112 | 107 | 112 | TLE | |
p00822 | C++ | Memory Limit Exceeded | #include <cstdio>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int to[9][5]; // to[i] : 雲が位置 i から移動できる位置
int n;
int sun[366][16]; // sun[t][j]==1 <=> t 日目にマス j は晴れていないといけない
int rain[9][16]; // rain[i][j]==1 <=> 雲が位置 i にいるとき, マス j は雨
set<vector<char>> vis[366][9];
bool dfs(int t, int i, vector<char> last) {
if (vis[t][i].count(last) == 1)
return false;
vis[t][i].insert(last);
rep(j, 16) {
if (rain[i][j] == 1) {
last[j] = 0;
if (sun[t][j] == 1)
return false;
} else {
last[j]++;
if (last[j] >= 7)
return false;
}
}
if (t == n)
return true;
rep(j, 5) if (dfs(t + 1, to[i][j], last)) return true;
return false;
}
int main() {
to[0][0] = 0;
to[0][1] = 1;
to[0][2] = 2;
to[0][3] = 3;
to[0][4] = 6;
to[1][0] = 1;
to[1][1] = 0;
to[1][2] = 2;
to[1][3] = 4;
to[1][4] = 7;
to[2][0] = 2;
to[2][1] = 0;
to[2][2] = 1;
to[2][3] = 5;
to[2][4] = 8;
to[3][0] = 3;
to[3][1] = 0;
to[3][2] = 4;
to[3][3] = 5;
to[3][4] = 6;
to[4][0] = 4;
to[4][1] = 1;
to[4][2] = 3;
to[4][3] = 5;
to[4][4] = 7;
to[5][0] = 5;
to[5][1] = 2;
to[5][2] = 3;
to[5][3] = 4;
to[5][4] = 8;
to[6][0] = 6;
to[6][1] = 0;
to[6][2] = 3;
to[6][3] = 7;
to[6][4] = 8;
to[7][0] = 7;
to[7][1] = 1;
to[7][2] = 4;
to[7][3] = 6;
to[7][4] = 8;
to[8][0] = 8;
to[8][1] = 2;
to[8][2] = 5;
to[8][3] = 6;
to[8][4] = 7;
rain[0][0] = rain[0][1] = rain[0][4] = rain[0][5] = 1;
rain[1][1] = rain[1][2] = rain[1][5] = rain[1][6] = 1;
rain[2][2] = rain[2][3] = rain[2][6] = rain[2][7] = 1;
rain[3][4] = rain[3][5] = rain[3][8] = rain[3][9] = 1;
rain[4][5] = rain[4][6] = rain[4][9] = rain[4][10] = 1;
rain[5][6] = rain[5][7] = rain[5][10] = rain[5][11] = 1;
rain[6][8] = rain[6][9] = rain[6][12] = rain[6][13] = 1;
rain[7][9] = rain[7][10] = rain[7][13] = rain[7][14] = 1;
rain[8][10] = rain[8][11] = rain[8][14] = rain[8][15] = 1;
while (scanf("%d", &n), n) {
rep(t, n) rep(j, 16) scanf("%d", sun[t] + j);
rep(t, n + 1) rep(i, 9) vis[t][i].clear();
vector<char> last(16); // 最後に雨が降ったのは何日前か ( int だと MLE した )
puts(dfs(0, 4, last) ? "1" : "0");
}
return 0;
} | #include <cstdio>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int to[9][5]; // to[i] : 雲が位置 i から移動できる位置
int n;
int sun[366][16]; // sun[t][j]==1 <=> t 日目にマス j は晴れていないといけない
int rain[9][16]; // rain[i][j]==1 <=> 雲が位置 i にいるとき, マス j は雨
set<vector<char>> vis[366][9];
bool dfs(int t, int i, vector<char> last) {
if (t % 2 == 0) { // MLE したので半分だけメモ化
if (vis[t][i].count(last) == 1)
return false;
vis[t][i].insert(last);
}
rep(j, 16) {
if (rain[i][j] == 1) {
last[j] = 0;
if (sun[t][j] == 1)
return false;
} else {
last[j]++;
if (last[j] >= 7)
return false;
}
}
if (t == n)
return true;
rep(j, 5) if (dfs(t + 1, to[i][j], last)) return true;
return false;
}
int main() {
to[0][0] = 0;
to[0][1] = 1;
to[0][2] = 2;
to[0][3] = 3;
to[0][4] = 6;
to[1][0] = 1;
to[1][1] = 0;
to[1][2] = 2;
to[1][3] = 4;
to[1][4] = 7;
to[2][0] = 2;
to[2][1] = 0;
to[2][2] = 1;
to[2][3] = 5;
to[2][4] = 8;
to[3][0] = 3;
to[3][1] = 0;
to[3][2] = 4;
to[3][3] = 5;
to[3][4] = 6;
to[4][0] = 4;
to[4][1] = 1;
to[4][2] = 3;
to[4][3] = 5;
to[4][4] = 7;
to[5][0] = 5;
to[5][1] = 2;
to[5][2] = 3;
to[5][3] = 4;
to[5][4] = 8;
to[6][0] = 6;
to[6][1] = 0;
to[6][2] = 3;
to[6][3] = 7;
to[6][4] = 8;
to[7][0] = 7;
to[7][1] = 1;
to[7][2] = 4;
to[7][3] = 6;
to[7][4] = 8;
to[8][0] = 8;
to[8][1] = 2;
to[8][2] = 5;
to[8][3] = 6;
to[8][4] = 7;
rain[0][0] = rain[0][1] = rain[0][4] = rain[0][5] = 1;
rain[1][1] = rain[1][2] = rain[1][5] = rain[1][6] = 1;
rain[2][2] = rain[2][3] = rain[2][6] = rain[2][7] = 1;
rain[3][4] = rain[3][5] = rain[3][8] = rain[3][9] = 1;
rain[4][5] = rain[4][6] = rain[4][9] = rain[4][10] = 1;
rain[5][6] = rain[5][7] = rain[5][10] = rain[5][11] = 1;
rain[6][8] = rain[6][9] = rain[6][12] = rain[6][13] = 1;
rain[7][9] = rain[7][10] = rain[7][13] = rain[7][14] = 1;
rain[8][10] = rain[8][11] = rain[8][14] = rain[8][15] = 1;
while (scanf("%d", &n), n) {
rep(t, n) rep(j, 16) scanf("%d", sun[t] + j);
rep(t, n + 1) rep(i, 9) vis[t][i].clear();
vector<char> last(16); // 最後に雨が降ったのは何日前か ( int だと MLE した )
puts(dfs(0, 4, last) ? "1" : "0");
}
return 0;
} | replace | 16 | 19 | 16 | 21 | MLE | |
p00824 | C++ | Memory 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 int INF = INT_MAX / 2;
map<vector<vector<int>>, int> memo;
vector<vector<int>> goal(4, vector<int>(8, 0));
int solve(vector<vector<int>> &card) {
if (card == goal)
return 0;
if (memo.find(card) != memo.end())
return memo[card];
int ret = INF;
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
if (card[i][j] != 0)
continue;
for (int k = 0; k < 4; ++k) {
for (int l = 1; l < 8; ++l) {
if (card[i][j - 1] + 1 == card[k][l]) {
swap(card[i][j], card[k][l]);
ret = min(ret, solve(card) + 1);
swap(card[i][j], card[k][l]);
}
}
}
}
}
return memo[card] = ret;
}
int main() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 7; ++j) {
goal[i][j] = 11 + i * 10 + j;
}
}
int d;
cin >> d;
while (--d >= 0) {
vector<vector<int>> card(4, vector<int>(8));
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
cin >> card[i][j];
if (card[i][j] % 10 == 1)
card[i][j] = 0;
}
}
for (int i = 0; i < 4; ++i)
card[i][0] = 11 + 10 * i;
int ret = solve(card);
if (ret == INF)
cout << -1 << endl;
else
cout << ret << endl;
}
return 0;
} | #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 int INF = INT_MAX / 2;
map<vector<vector<int>>, int> memo;
vector<vector<int>> goal(4, vector<int>(8, 0));
int solve(vector<vector<int>> &card) {
if (card == goal)
return 0;
if (memo.find(card) != memo.end())
return memo[card];
int ret = INF;
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
if (card[i][j] != 0)
continue;
for (int k = 0; k < 4; ++k) {
for (int l = 1; l < 8; ++l) {
if (card[i][j - 1] + 1 == card[k][l]) {
swap(card[i][j], card[k][l]);
ret = min(ret, solve(card) + 1);
swap(card[i][j], card[k][l]);
}
}
}
}
}
return memo[card] = ret;
}
int main() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 7; ++j) {
goal[i][j] = 11 + i * 10 + j;
}
}
int d;
cin >> d;
while (--d >= 0) {
vector<vector<int>> card(4, vector<int>(8));
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
cin >> card[i][j];
if (card[i][j] % 10 == 1)
card[i][j] = 0;
}
}
for (int i = 0; i < 4; ++i)
card[i][0] = 11 + 10 * i;
memo.clear();
int ret = solve(card);
if (ret == INF)
cout << -1 << endl;
else
cout << ret << endl;
}
return 0;
} | insert | 72 | 72 | 72 | 73 | MLE | |
p00824 | C++ | Memory Limit Exceeded | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
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"
map<vector<vector<int>>, int> mp;
vector<vector<int>> goal;
int getans(const vector<vector<int>> &field) {
if (field == goal)
return 0;
auto it = mp.find(field);
if (it != mp.end())
return it->second;
else {
int ans = 1e5;
vector<vector<int>> nfield(field);
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
if (!field[i][j]) {
int from = field[i][j - 1];
for (int k = 0; k < 4; ++k) {
for (int l = 0; l < 8; ++l) {
if (nfield[k][l] == from + 1) {
swap(nfield[i][j], nfield[k][l]);
ans = min(getans(nfield) + 1, ans);
swap(nfield[i][j], nfield[k][l]);
}
}
}
}
}
}
return mp[field] = ans;
}
}
int main() {
int N;
cin >> N;
while (N--) {
vector<vector<int>> field(4, vector<int>(8));
goal = field;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 7; ++j) {
goal[i][j] = 10 * (i + 1) + j + 1;
}
}
for (int i = 0; i < 4; ++i) {
field[i][0] = i * 10 + 11;
for (int j = 0; j < 7; ++j) {
int a;
cin >> a;
if (a % 10 != 1) {
field[i][j + 1] = a;
}
}
}
int ans = getans(field);
if (ans > 9999)
ans = -1;
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
map<vector<vector<int>>, int> mp;
vector<vector<int>> goal;
int getans(const vector<vector<int>> &field) {
if (field == goal)
return 0;
auto it = mp.find(field);
if (it != mp.end())
return it->second;
else {
int ans = 1e5;
vector<vector<int>> nfield(field);
for (int i = 0; i < 4; ++i) {
for (int j = 1; j < 8; ++j) {
if (!field[i][j]) {
int from = field[i][j - 1];
for (int k = 0; k < 4; ++k) {
for (int l = 0; l < 8; ++l) {
if (nfield[k][l] == from + 1) {
swap(nfield[i][j], nfield[k][l]);
ans = min(getans(nfield) + 1, ans);
swap(nfield[i][j], nfield[k][l]);
}
}
}
}
}
}
return mp[field] = ans;
}
}
int main() {
int N;
cin >> N;
while (N--) {
mp.clear();
vector<vector<int>> field(4, vector<int>(8));
goal = field;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 7; ++j) {
goal[i][j] = 10 * (i + 1) + j + 1;
}
}
for (int i = 0; i < 4; ++i) {
field[i][0] = i * 10 + 11;
for (int j = 0; j < 7; ++j) {
int a;
cin >> a;
if (a % 10 != 1) {
field[i][j + 1] = a;
}
}
}
int ans = getans(field);
if (ans > 9999)
ans = -1;
cout << ans << endl;
}
return 0;
} | insert | 47 | 47 | 47 | 48 | MLE | |
p00825 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
#define inf 100000000
using namespace std;
struct edge {
int to, cap, cost, rev;
edge(int a, int b, int c, int d) { to = a, cap = b, cost = c, rev = d; }
};
int n;
vector<edge> G[370];
const int S = 0, T = 366;
int prevv[370], preve[350];
void add_edge(int s, int t, int cap, int cost) {
G[s].push_back(edge(t, cap, cost, G[t].size()));
G[t].push_back(edge(s, 0, -cost, G[s].size() - 1));
}
void BellmanFord() {
int dist[370];
for (int i = S; i <= T; i++)
dist[i] = inf;
dist[S] = 0;
prevv[S] = -1;
bool update = true;
while (update) {
update = false;
for (int i = S; i <= T; i++) {
for (int j = 0; j < G[i].size(); j++) {
if (G[i][j].cap == 0)
continue;
if (dist[G[i][j].to] > dist[i] + G[i][j].cost) {
update = true;
dist[G[i][j].to] = dist[i] + G[i][j].cost;
prevv[G[i][j].to] = i;
preve[G[i][j].to] = j;
}
}
}
}
}
int main(void) {
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = S; i <= T; i++)
G[i].clear();
for (int i = S; i < T; i++)
add_edge(i, i + 1, inf, 0);
int ii, jj, ww;
for (int i = 0; i < n; i++) {
cin >> ii >> jj >> ww;
add_edge(ii, jj + 1, 1, -ww);
}
int F = 2, ans = 0;
while (F > 0) {
BellmanFord();
int flow = F;
int p = T;
while (prevv[p] != -1) {
flow = min(flow, G[prevv[p]][preve[p]].cap);
p = prevv[p];
}
p = T;
while (prevv[p] != -1) {
ans += G[prevv[p]][preve[p]].cost * flow;
G[prevv[p]][preve[p]].cap -= flow;
G[p][G[prevv[p]][preve[p]].rev].cap += flow;
p = prevv[p];
}
F -= flow;
}
cout << -ans << endl;
}
return 0;
} | #include <iostream>
#include <vector>
#define inf 100000000
using namespace std;
struct edge {
int to, cap, cost, rev;
edge(int a, int b, int c, int d) { to = a, cap = b, cost = c, rev = d; }
};
int n;
vector<edge> G[370];
const int S = 0, T = 366;
int prevv[370], preve[370];
void add_edge(int s, int t, int cap, int cost) {
G[s].push_back(edge(t, cap, cost, G[t].size()));
G[t].push_back(edge(s, 0, -cost, G[s].size() - 1));
}
void BellmanFord() {
int dist[370];
for (int i = S; i <= T; i++)
dist[i] = inf;
dist[S] = 0;
prevv[S] = -1;
bool update = true;
while (update) {
update = false;
for (int i = S; i <= T; i++) {
for (int j = 0; j < G[i].size(); j++) {
if (G[i][j].cap == 0)
continue;
if (dist[G[i][j].to] > dist[i] + G[i][j].cost) {
update = true;
dist[G[i][j].to] = dist[i] + G[i][j].cost;
prevv[G[i][j].to] = i;
preve[G[i][j].to] = j;
}
}
}
}
}
int main(void) {
while (1) {
cin >> n;
if (n == 0)
break;
for (int i = S; i <= T; i++)
G[i].clear();
for (int i = S; i < T; i++)
add_edge(i, i + 1, inf, 0);
int ii, jj, ww;
for (int i = 0; i < n; i++) {
cin >> ii >> jj >> ww;
add_edge(ii, jj + 1, 1, -ww);
}
int F = 2, ans = 0;
while (F > 0) {
BellmanFord();
int flow = F;
int p = T;
while (prevv[p] != -1) {
flow = min(flow, G[prevv[p]][preve[p]].cap);
p = prevv[p];
}
p = T;
while (prevv[p] != -1) {
ans += G[prevv[p]][preve[p]].cost * flow;
G[prevv[p]][preve[p]].cap -= flow;
G[p][G[prevv[p]][preve[p]].rev].cap += flow;
p = prevv[p];
}
F -= flow;
}
cout << -ans << endl;
}
return 0;
} | replace | 14 | 15 | 14 | 15 | TLE | |
p00825 | C++ | Runtime Error | #include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct PrimalDual {
const int INF = 1 << 28;
typedef pair<int, int> P;
struct edge {
int to, cap, cost, rev;
edge() {}
edge(int to, int cap, int cost, int rev)
: to(to), cap(cap), cost(cost), rev(rev) {}
};
int n;
vector<vector<edge>> G;
vector<int> h, dist, prevv, preve;
PrimalDual() {}
PrimalDual(int sz) : n(sz), G(sz), h(sz), dist(sz), prevv(sz), preve(sz) {}
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int flow(int s, int t, int f) {
int res = 0;
fill(h.begin(), h.end(), 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> que;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.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;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF)
return -1;
for (int v = 0; v < n; 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;
}
};
int main() {
int n;
while (cin >> n, n) {
PrimalDual p(367);
r(i, n) {
int a, b, c;
cin >> a >> b >> c;
p.add_edge(a, b + 1, 1, -c);
}
r(i, 367) p.add_edge(i, i + 1, 2, 0);
cout << -p.flow(0, 366, 2) << endl;
}
}
| #include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
struct PrimalDual {
const int INF = 1 << 28;
typedef pair<int, int> P;
struct edge {
int to, cap, cost, rev;
edge() {}
edge(int to, int cap, int cost, int rev)
: to(to), cap(cap), cost(cost), rev(rev) {}
};
int n;
vector<vector<edge>> G;
vector<int> h, dist, prevv, preve;
PrimalDual() {}
PrimalDual(int sz) : n(sz), G(sz), h(sz), dist(sz), prevv(sz), preve(sz) {}
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int flow(int s, int t, int f) {
int res = 0;
fill(h.begin(), h.end(), 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P>> que;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.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;
que.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == INF)
return -1;
for (int v = 0; v < n; 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;
}
};
int main() {
int n;
while (cin >> n, n) {
PrimalDual p(368);
r(i, n) {
int a, b, c;
cin >> a >> b >> c;
p.add_edge(a, b + 1, 1, -c);
}
r(i, 367) p.add_edge(i, i + 1, 2, 0);
cout << -p.flow(0, 366, 2) << endl;
}
}
| replace | 73 | 74 | 73 | 74 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p00826 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1 << 30
#define eps (1e-5)
#define MAX 10000
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double k) { return Point(x * k, y * k); }
Point operator/(double k) { return Point(x / k, y / k); }
bool operator<(Point p) const { return (x != p.x ? x < p.x : y < p.y); }
bool operator==(Point p) const {
return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
}
double abs() { return sqrt(norm()); }
double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;
class Segment {
public:
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
};
typedef Segment Line;
double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }
int ccw(Point p0, Point p1, Point p2) {
Vector a = p1 - p0;
Vector b = p2 - p0;
if (cross(a, b) > eps)
return 1;
if (cross(a, b) < -eps)
return -1;
if (dot(a, b) < -eps)
return 2;
if (a.norm() < b.norm())
return -2;
return 0;
}
bool intersect(Point p1, Point p2, Point p3, Point p4) {
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
bool intersect(Segment s1, Segment s2) {
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
Point getCrossPointLL(Line a, Line b) {
double A = cross(a.p2 - a.p1, b.p2 - b.p1);
double B = cross(a.p2 - a.p1, a.p2 - b.p1);
if (abs(A) < eps || abs(B) < eps)
return b.p1;
return b.p1 + (b.p2 - b.p1) * (B / A);
}
int contains(Polygon g, Point p) {
int n = g.size();
bool x = false;
for (int i = 0; i < n; i++) {
Vector a = g[i] - p, b = g[(i + 1) % n] - p;
if (abs(cross(a, b)) < eps && dot(a, b) < eps)
return 1;
if (a.y > b.y)
swap(a, b);
if (a.y < eps && eps < b.y && cross(a, b) > eps)
x = !x;
}
if (x)
return 2;
return 0;
}
typedef vector<vector<int>> Graph;
Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) {
for (int i = 0; i < v.size(); i++) {
ps.push_back(v[i].p1);
ps.push_back(v[i].p2);
for (int j = i + 1; j < v.size(); j++) {
if (intersect(v[i], v[j]))
ps.push_back(getCrossPointLL(v[i], v[j]));
}
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
Graph g(ps.size());
for (int i = 0; i < v.size(); i++) {
vector<pair<double, int>> list;
for (int j = 0; j < ps.size(); j++)
if (ccw(v[i].p1, v[i].p2, ps[j]) == 0)
list.push_back(mp(norm(v[i].p1 - ps[j]), j));
sort(list.begin(), list.end());
for (int j = 0; j < list.size() - 1; j++) {
int a = list[j].s, b = list[j + 1].s;
g[a].push_back(b);
g[b].push_back(a);
}
}
return g;
}
Graph e;
Polygon p;
vector<Point> vp;
int start = -1;
double getAngle(Vector a, Vector b) {
double tmp = dot(a, b) / (abs(a) * abs(b));
if (tmp < -1.0)
tmp = -1.0;
if (1.0 < tmp)
tmp = 1.0;
double r = acos(tmp) * 180.0 / pi;
if (cross(a, b) < -eps)
r = 360.0 - r;
return r;
}
void rec(int now, int before) {
if (now == start)
return;
pair<double, int> pdi(inf, -1);
for (int i = 0; i < e[now].size(); i++) {
int next = e[now][i];
double ang = getAngle(vp[before] - vp[now], vp[next] - vp[now]);
if (next == before)
ang = 361;
pdi = min(pdi, mp(ang, next));
}
p.push_back(vp[now]);
rec(pdi.s, now);
return;
}
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
vector<Segment> v;
for (int i = 0; i < n; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
v.push_back(Segment(Point(a, b), Point(c, d)));
}
vp.clear();
e = SegmentArrangement(v, vp);
Point s(inf, inf);
for (int i = 0; i < vp.size(); i++)
s = min(s, vp[i]);
for (int i = 0; i < vp.size(); i++)
if (vp[i] == s)
start = i;
string ans = "no";
for (int i = 0; i < e[start].size(); i++) {
p.clear();
p.push_back(vp[start]);
rec(e[start][i], start);
if (contains(p, Point(0, 0)) == 2)
ans = "yes";
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pi M_PI
#define inf 1 << 30
#define eps (1e-7)
#define MAX 20000
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
Point operator+(Point p) { return Point(x + p.x, y + p.y); }
Point operator-(Point p) { return Point(x - p.x, y - p.y); }
Point operator*(double k) { return Point(x * k, y * k); }
Point operator/(double k) { return Point(x / k, y / k); }
bool operator<(Point p) const { return (x != p.x ? x < p.x : y < p.y); }
bool operator==(Point p) const {
return fabs(x - p.x) < eps && fabs(y - p.y) < eps;
}
double abs() { return sqrt(norm()); }
double norm() { return (x * x + y * y); }
};
typedef Point Vector;
typedef vector<Point> Polygon;
class Segment {
public:
Point p1, p2;
Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {}
};
typedef Segment Line;
double norm(Vector a) { return (a.x * a.x + a.y * a.y); }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); }
double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); }
int ccw(Point p0, Point p1, Point p2) {
Vector a = p1 - p0;
Vector b = p2 - p0;
if (cross(a, b) > eps)
return 1;
if (cross(a, b) < -eps)
return -1;
if (dot(a, b) < -eps)
return 2;
if (a.norm() < b.norm())
return -2;
return 0;
}
bool intersect(Point p1, Point p2, Point p3, Point p4) {
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
bool intersect(Segment s1, Segment s2) {
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
Point getCrossPointLL(Line a, Line b) {
double A = cross(a.p2 - a.p1, b.p2 - b.p1);
double B = cross(a.p2 - a.p1, a.p2 - b.p1);
if (abs(A) < eps || abs(B) < eps)
return b.p1;
return b.p1 + (b.p2 - b.p1) * (B / A);
}
int contains(Polygon g, Point p) {
int n = g.size();
bool x = false;
for (int i = 0; i < n; i++) {
Vector a = g[i] - p, b = g[(i + 1) % n] - p;
if (abs(cross(a, b)) < eps && dot(a, b) < eps)
return 1;
if (a.y > b.y)
swap(a, b);
if (a.y < eps && eps < b.y && cross(a, b) > eps)
x = !x;
}
if (x)
return 2;
return 0;
}
typedef vector<vector<int>> Graph;
Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) {
for (int i = 0; i < v.size(); i++) {
ps.push_back(v[i].p1);
ps.push_back(v[i].p2);
for (int j = i + 1; j < v.size(); j++) {
if (intersect(v[i], v[j]))
ps.push_back(getCrossPointLL(v[i], v[j]));
}
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
Graph g(ps.size());
for (int i = 0; i < v.size(); i++) {
vector<pair<double, int>> list;
for (int j = 0; j < ps.size(); j++)
if (ccw(v[i].p1, v[i].p2, ps[j]) == 0)
list.push_back(mp(norm(v[i].p1 - ps[j]), j));
sort(list.begin(), list.end());
for (int j = 0; j < list.size() - 1; j++) {
int a = list[j].s, b = list[j + 1].s;
g[a].push_back(b);
g[b].push_back(a);
}
}
return g;
}
Graph e;
Polygon p;
vector<Point> vp;
int start = -1;
double getAngle(Vector a, Vector b) {
double tmp = dot(a, b) / (abs(a) * abs(b));
if (tmp < -1.0)
tmp = -1.0;
if (1.0 < tmp)
tmp = 1.0;
double r = acos(tmp) * 180.0 / pi;
if (cross(a, b) < -eps)
r = 360.0 - r;
return r;
}
void rec(int now, int before) {
if (now == start)
return;
pair<double, int> pdi(inf, -1);
for (int i = 0; i < e[now].size(); i++) {
int next = e[now][i];
double ang = getAngle(vp[before] - vp[now], vp[next] - vp[now]);
if (next == before)
ang = 361;
pdi = min(pdi, mp(ang, next));
}
p.push_back(vp[now]);
rec(pdi.s, now);
return;
}
int main() {
int n;
while (1) {
cin >> n;
if (n == 0)
break;
vector<Segment> v;
for (int i = 0; i < n; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
v.push_back(Segment(Point(a, b), Point(c, d)));
}
vp.clear();
e = SegmentArrangement(v, vp);
Point s(inf, inf);
for (int i = 0; i < vp.size(); i++)
s = min(s, vp[i]);
for (int i = 0; i < vp.size(); i++)
if (vp[i] == s)
start = i;
string ans = "no";
for (int i = 0; i < e[start].size(); i++) {
p.clear();
p.push_back(vp[start]);
rec(e[start][i], start);
if (contains(p, Point(0, 0)) == 2)
ans = "yes";
}
cout << ans << endl;
}
return 0;
} | replace | 6 | 8 | 6 | 8 | MLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.