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
|
---|---|---|---|---|---|---|---|---|---|---|---|
p00513 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int n, s, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
int flag = 0;
for (int j = 1; j < s; j++) {
int a = s - j;
int b = 2 * j + 1;
if (a % b == 0) {
if (a / b < s) {
flag = 1;
break;
}
}
}
if (flag == 0)
cnt++;
}
cout << cnt << endl;
return 0;
} | #include <iostream>
using namespace std;
int main() {
int n, s, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
int flag = 0;
for (int j = 1; j * j < s; j++) {
int a = s - j;
int b = 2 * j + 1;
if (a % b == 0) {
if (a / b < s) {
flag = 1;
break;
}
}
}
if (flag == 0)
cnt++;
}
cout << cnt << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p00513 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int n, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int s;
cin >> s;
bool flag = false;
for (int x = 1; x < s && flag == false; x++) {
if ((s - x) % (2 * x + 1) == 0) {
flag = true;
}
}
if (flag == false) {
ans++;
}
}
cout << ans << endl;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
int n, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int s;
cin >> s;
bool flag = false;
for (int x = 1; x * x <= s && flag == false; x++) {
if ((s - x) % (2 * x + 1) == 0) {
flag = true;
}
}
if (flag == false) {
ans++;
}
}
cout << ans << endl;
} | replace | 16 | 17 | 16 | 17 | TLE | |
p00514 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) \
rep(_X, sz(a)) { printf("%d ", a[_X]); } \
puts("");
#define DBG2(a) \
rep(_X, sz(a)) { \
rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]); \
puts(""); \
}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
a.push_back(b);
return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
while (b--)
if (!a.empty())
a.pop_back();
}
bool isprime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
typedef unsigned int uint;
int in() {
int x = 0, c;
for (; (uint)((c = getchar()) - '0') >= 10;) {
if (c == '-')
return -in();
if (!~c)
throw ~0;
}
do {
x = (x << 3) + (x << 1) + (c - '0');
} while ((uint)((c = getchar()) - '0') < 10);
return x;
}
vector<int> factor(int n) {
vector<int> ret;
int i = 2;
while (i * i <= n) {
if (n % i == 0)
ret.push_back(i), n /= i, i = 2;
else
i++;
}
if (n != 1)
ret.push_back(n);
return ret;
}
int cnt[30001];
class bigInteger {
#define DIGITMAX (1700) // 4 * n digit
public:
std::vector<int> value;
bigInteger operator=(bigInteger b) {
this->value = b.value;
return *this;
}
/* O(n) */
bigInteger operator+(bigInteger b) {
for (int i = 0; i < DIGITMAX; i++) {
b.value[i] += this->value[i];
if (b.value[i] >= 10000) {
b.value[i] -= 10000;
b.value[i + 1]++;
}
}
return b;
}
/* O(n) */
bigInteger operator-(bigInteger b) {
for (int i = 0; i < DIGITMAX; i++) {
b.value[i] = this->value[i] - b.value[i];
if (b.value[i] < 0) {
b.value[i] = 10000 + b.value[i];
b.value[i + 1]++; // b wo oome
}
}
return b;
}
/* with bug.. O(n^2) */
bigInteger operator*(bigInteger b) {
bigInteger ret;
for (int i = 0; i < DIGITMAX; i++) {
for (int j = 0; j < DIGITMAX; j++) {
if (i + j + 1 < DIGITMAX) {
ret.value[i + j] += this->value[i] * b.value[j];
ret.value[i + j + 1] += ret.value[i + j] / 10000;
ret.value[i + j] %= 10000;
}
}
}
return ret;
}
string str() {
char tmp[5];
string retVal = "";
for (int i = DIGITMAX - 1; i >= 0; i--) {
sprintf(tmp, "%04d", value[i]);
retVal += tmp;
}
while (retVal[0] == '0' && retVal.length() >= 2)
retVal = retVal.substr(1);
return retVal;
}
bigInteger() { this->value.resize(DIGITMAX); }
bigInteger(string init) {
vector<int> vi;
for (int i = init.length() - 4; i > -4; i -= 4) {
vi.push_back(atoi(init.substr(max(i, 0), 4 + (i < 0 ? i : 0)).c_str()));
}
this->value = vi;
this->value.resize(DIGITMAX);
}
};
int main() {
int n = in(), m = in(), r = in();
int R = r - n * m;
if (R < 0) {
cout << 0 << endl;
return 0;
}
// R=2,R=3 |O|O -> (R+n-1) C (n-1)
for (int i = 0; i < n - 1; i++) {
vector<int> v = factor(n - 1 + R - i);
for (int j = 0; j < v.size(); j++)
cnt[v[j]]++;
}
for (int i = 0; i < n - 1; i++) {
vector<int> v = factor(i + 1);
for (int j = 0; j < v.size(); j++)
cnt[v[j]]--;
}
bigInteger ans("1");
for (int i = 0; i <= 30000; i++) {
// if( cnt[i] )cout << i << " " << cnt[i] << endl;
while (cnt[i]--)
ans = ans * bigInteger(itos(i));
}
cout << ans.str() << endl;
}
| #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) \
rep(_X, sz(a)) { printf("%d ", a[_X]); } \
puts("");
#define DBG2(a) \
rep(_X, sz(a)) { \
rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]); \
puts(""); \
}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
a.push_back(b);
return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
while (b--)
if (!a.empty())
a.pop_back();
}
bool isprime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
typedef unsigned int uint;
int in() {
int x = 0, c;
for (; (uint)((c = getchar()) - '0') >= 10;) {
if (c == '-')
return -in();
if (!~c)
throw ~0;
}
do {
x = (x << 3) + (x << 1) + (c - '0');
} while ((uint)((c = getchar()) - '0') < 10);
return x;
}
vector<int> factor(int n) {
vector<int> ret;
int i = 2;
while (i * i <= n) {
if (n % i == 0)
ret.push_back(i), n /= i, i = 2;
else
i++;
}
if (n != 1)
ret.push_back(n);
return ret;
}
int cnt[30001];
class bigInteger {
#define DIGITMAX (1520) // 4 * n digit
public:
std::vector<int> value;
bigInteger operator=(bigInteger b) {
this->value = b.value;
return *this;
}
/* O(n) */
bigInteger operator+(bigInteger b) {
for (int i = 0; i < DIGITMAX; i++) {
b.value[i] += this->value[i];
if (b.value[i] >= 10000) {
b.value[i] -= 10000;
b.value[i + 1]++;
}
}
return b;
}
/* O(n) */
bigInteger operator-(bigInteger b) {
for (int i = 0; i < DIGITMAX; i++) {
b.value[i] = this->value[i] - b.value[i];
if (b.value[i] < 0) {
b.value[i] = 10000 + b.value[i];
b.value[i + 1]++; // b wo oome
}
}
return b;
}
/* with bug.. O(n^2) */
bigInteger operator*(bigInteger b) {
bigInteger ret;
for (int i = 0; i < DIGITMAX; i++) {
for (int j = 0; j < DIGITMAX; j++) {
if (i + j + 1 < DIGITMAX) {
ret.value[i + j] += this->value[i] * b.value[j];
ret.value[i + j + 1] += ret.value[i + j] / 10000;
ret.value[i + j] %= 10000;
}
}
}
return ret;
}
string str() {
char tmp[5];
string retVal = "";
for (int i = DIGITMAX - 1; i >= 0; i--) {
sprintf(tmp, "%04d", value[i]);
retVal += tmp;
}
while (retVal[0] == '0' && retVal.length() >= 2)
retVal = retVal.substr(1);
return retVal;
}
bigInteger() { this->value.resize(DIGITMAX); }
bigInteger(string init) {
vector<int> vi;
for (int i = init.length() - 4; i > -4; i -= 4) {
vi.push_back(atoi(init.substr(max(i, 0), 4 + (i < 0 ? i : 0)).c_str()));
}
this->value = vi;
this->value.resize(DIGITMAX);
}
};
int main() {
int n = in(), m = in(), r = in();
int R = r - n * m;
if (R < 0) {
cout << 0 << endl;
return 0;
}
// R=2,R=3 |O|O -> (R+n-1) C (n-1)
for (int i = 0; i < n - 1; i++) {
vector<int> v = factor(n - 1 + R - i);
for (int j = 0; j < v.size(); j++)
cnt[v[j]]++;
}
for (int i = 0; i < n - 1; i++) {
vector<int> v = factor(i + 1);
for (int j = 0; j < v.size(); j++)
cnt[v[j]]--;
}
bigInteger ans("1");
for (int i = 0; i <= 30000; i++) {
// if( cnt[i] )cout << i << " " << cnt[i] << endl;
while (cnt[i]--)
ans = ans * bigInteger(itos(i));
}
cout << ans.str() << endl;
}
| replace | 102 | 103 | 102 | 103 | TLE | |
p00514 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, m, r;
int now = 1, prev = 0;
ll dp[1001];
ll mod = 1000000;
int main(void) {
scanf("%d%d%d", &n, &m, &r);
r -= m * n;
if (r < 0) {
puts("0");
return 0;
}
if (r == 0) {
puts("1");
return 0;
}
int nr = n + r - 1, h = 0;
dp[0] = 1;
for (int i = 2; i <= nr; i++) {
ll p = 0;
for (int j = 0; j <= h; j++) {
dp[j] *= i;
dp[j] += p;
p = 0;
if (dp[j] >= mod) {
if (j == h)
h++;
p = dp[j] / mod;
dp[j] = dp[j] % mod;
}
}
}
for (int i = 2; i <= r; i++) {
ll p = 0;
for (int j = h; j >= 0; j--) {
dp[j] += mod * p;
p = dp[j] % i;
dp[j] = dp[j] / i;
}
}
for (int i = 2; i <= nr - r; i++) {
ll p = 0;
for (int j = h; j >= 0; j--) {
dp[j] += mod * p;
p = dp[j] % i;
dp[j] = dp[j] / i;
}
}
bool f = false;
for (int i = h; i >= 0; i--) {
if (f)
printf("%06lld", dp[i]);
if (!f && dp[i] > 0) {
f = true;
printf("%lld", dp[i]);
}
}
printf("\n");
return 0;
} | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, m, r;
int now = 1, prev = 0;
ll dp[100001];
ll mod = 1000000;
int main(void) {
scanf("%d%d%d", &n, &m, &r);
r -= m * n;
if (r < 0) {
puts("0");
return 0;
}
if (r == 0) {
puts("1");
return 0;
}
int nr = n + r - 1, h = 0;
dp[0] = 1;
for (int i = 2; i <= nr; i++) {
ll p = 0;
for (int j = 0; j <= h; j++) {
dp[j] *= i;
dp[j] += p;
p = 0;
if (dp[j] >= mod) {
if (j == h)
h++;
p = dp[j] / mod;
dp[j] = dp[j] % mod;
}
}
}
for (int i = 2; i <= r; i++) {
ll p = 0;
for (int j = h; j >= 0; j--) {
dp[j] += mod * p;
p = dp[j] % i;
dp[j] = dp[j] / i;
}
}
for (int i = 2; i <= nr - r; i++) {
ll p = 0;
for (int j = h; j >= 0; j--) {
dp[j] += mod * p;
p = dp[j] % i;
dp[j] = dp[j] / i;
}
}
bool f = false;
for (int i = h; i >= 0; i--) {
if (f)
printf("%06lld", dp[i]);
if (!f && dp[i] > 0) {
f = true;
printf("%lld", dp[i]);
}
}
printf("\n");
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00516 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n, m, a[1000], b[1000], cnt[1000] = {};
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
int j = 0;
while (a[j] > b[i])
j++;
cnt[j]++;
}
cout << distance(cnt, max_element(cnt, cnt + n)) + 1 << endl;
} | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n, m, a[5000], b[5000], cnt[5000] = {};
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
int j = 0;
while (a[j] > b[i])
j++;
cnt[j]++;
}
cout << distance(cnt, max_element(cnt, cnt + n)) + 1 << endl;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00516 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < M; i++) {
cin >> B[i];
}
vector<int> votes(N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[j] <= B[i]) {
votes[j]++;
break;
}
}
}
int p = 0, best = -1;
for (int i = 0; i < N; i++) {
if (best < votes[i]) {
best = votes[i];
p = i;
}
}
cout << p + 1 << endl;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N), B(M);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < M; i++) {
cin >> B[i];
}
vector<int> votes(N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[j] <= B[i]) {
votes[j]++;
break;
}
}
}
int p = 0, best = -1;
for (int i = 0; i < N; i++) {
if (best < votes[i]) {
best = votes[i];
p = i;
}
}
cout << p + 1 << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00516 | C++ | Runtime Error | #include <stdio.h>
int main() {
int a[101], b[101], c[101] = {0}, ruio = 0, hideo = 0, nagase = 0, temp = 0;
scanf("%d %d", &ruio, &hideo);
for (int i = 1; i <= ruio; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= hideo; i++) {
scanf("%d", &b[i]);
}
for (int i = 1; i <= hideo; i++) {
for (int j = 1; j <= ruio; j++) {
if (b[i] >= a[j]) {
nagase = j;
break;
}
}
c[nagase]++;
nagase = 0;
}
for (int i = 1; i <= ruio; i++) {
if (nagase < c[i]) {
nagase = c[i];
temp = i;
}
}
printf("%d\n", temp);
} | #include <stdio.h>
int main() {
int a[2001], b[2001], c[2001] = {0}, ruio = 0, hideo = 0, nagase = 0,
temp = 0;
scanf("%d %d", &ruio, &hideo);
for (int i = 1; i <= ruio; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= hideo; i++) {
scanf("%d", &b[i]);
}
for (int i = 1; i <= hideo; i++) {
for (int j = 1; j <= ruio; j++) {
if (b[i] >= a[j]) {
nagase = j;
break;
}
}
c[nagase]++;
nagase = 0;
}
for (int i = 1; i <= ruio; i++) {
if (nagase < c[i]) {
nagase = c[i];
temp = i;
}
}
printf("%d\n", temp);
} | replace | 2 | 3 | 2 | 4 | 0 | |
p00517 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int a[100], b[100], w, h;
int solve(int i) {
int x = 0, y = 0, ans = 0, j;
x = max(a[i] - a[i + 1], a[i + 1] - a[i]);
y = max(b[i] - b[i + 1], b[i + 1] - b[i]);
ans = ans + x + y;
for (j = 0; j <= min(w, h); j++) {
if (a[i] - a[i + 1] > j && b[i] - b[i + 1] > j)
ans--;
else if (a[i + 1] - a[i] > j && b[i + 1] - b[i] > j)
ans--;
}
return ans;
}
int main() {
int n, i, answer = 0;
cin >> w >> h >> n;
for (i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
for (i = 1; i < n; i++) {
answer = solve(i) + answer;
}
cout << answer << endl;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int a[10000], b[10000], w, h;
int solve(int i) {
int x = 0, y = 0, ans = 0, j;
x = max(a[i] - a[i + 1], a[i + 1] - a[i]);
y = max(b[i] - b[i + 1], b[i + 1] - b[i]);
ans = ans + x + y;
for (j = 0; j <= min(w, h); j++) {
if (a[i] - a[i + 1] > j && b[i] - b[i + 1] > j)
ans--;
else if (a[i + 1] - a[i] > j && b[i + 1] - b[i] > j)
ans--;
}
return ans;
}
int main() {
int n, i, answer = 0;
cin >> w >> h >> n;
for (i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
for (i = 1; i < n; i++) {
answer = solve(i) + answer;
}
cout << answer << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p00517 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans = 0;
Pll a[110] = {};
LL d[1010][1010] = {};
int dd[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
string str, ss;
struct Edge {
LL cost, from, to;
};
vector<Edge> vec;
bool f;
char c[10000][10000];
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main() {
cin >> h >> w >> n;
rep(i, n) {
cin >> a[i].fir >> a[i].sec;
if (i) {
if ((a[i].fir < a[i - 1].fir && a[i].sec > a[i - 1].sec) ||
(a[i].fir > a[i - 1].fir && a[i].sec < a[i - 1].sec))
ans += (abs(a[i].fir - a[i - 1].fir) + abs(a[i].sec - a[i - 1].sec));
else
ans += max(abs(a[i].fir - a[i - 1].fir), abs(a[i].sec - a[i - 1].sec));
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans = 0;
Pll a[1100] = {};
LL d[1010][1010] = {};
int dd[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
string str, ss;
struct Edge {
LL cost, from, to;
};
vector<Edge> vec;
bool f;
char c[10000][10000];
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main() {
cin >> h >> w >> n;
rep(i, n) {
cin >> a[i].fir >> a[i].sec;
if (i) {
if ((a[i].fir < a[i - 1].fir && a[i].sec > a[i - 1].sec) ||
(a[i].fir > a[i - 1].fir && a[i].sec < a[i - 1].sec))
ans += (abs(a[i].fir - a[i - 1].fir) + abs(a[i].sec - a[i - 1].sec));
else
ans += max(abs(a[i].fir - a[i - 1].fir), abs(a[i].sec - a[i - 1].sec));
}
}
cout << ans << endl;
return 0;
}
| replace | 56 | 57 | 56 | 57 | -11 | |
p00518 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
const int MOD = 10007;
int main() {
int N;
cin >> N;
string s;
cin >> s;
int dp[1000][8] = {};
dp[0][1] = 1;
map<char, int> dic;
for (int i = 0; i < 3; i++)
dic["JOI"[i]] = i;
for (int i = 0; i < N; i++) {
for (int s1 = 0; s1 < 8; s1++) {
for (int s2 = 0; s2 < 8; s2++) {
if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
dp[i + 1][s2] += dp[i][s1];
dp[i + 1][s2] %= MOD;
}
}
}
}
int ans = 0;
for (int s = 0; s < 8; s++) {
ans += dp[N][s];
ans %= MOD;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
const int MOD = 10007;
int main() {
int N;
cin >> N;
string s;
cin >> s;
int dp[1010][8] = {};
dp[0][1] = 1;
map<char, int> dic;
for (int i = 0; i < 3; i++)
dic["JOI"[i]] = i;
for (int i = 0; i < N; i++) {
for (int s1 = 0; s1 < 8; s1++) {
for (int s2 = 0; s2 < 8; s2++) {
if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
dp[i + 1][s2] += dp[i][s1];
dp[i + 1][s2] %= MOD;
}
}
}
}
int ans = 0;
for (int s = 0; s < 8; s++) {
ans += dp[N][s];
ans %= MOD;
}
cout << ans << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p00518 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[200][200];
int solve(int day, int f) {
if (memo[day][f] != 0)
return memo[day][f];
if (day == n) {
return memo[day][f] = 1;
}
int tmp, sum = 0;
;
for (int i = 0; i < 8; i++) {
if (ta[day] == 'J')
tmp = 4;
else if (ta[day] == 'O')
tmp = 2;
else if (ta[day] == 'I')
tmp = 1;
if ((i & tmp) != 0 && (i & f) != 0)
sum += solve(day + 1, i);
}
return memo[day][f] = sum % 10007;
}
int main() {
cin >> n;
cin >> ta;
cout << solve(0, 4) << endl;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[20000][10];
int solve(int day, int f) {
if (memo[day][f] != 0)
return memo[day][f];
if (day == n) {
return memo[day][f] = 1;
}
int tmp, sum = 0;
;
for (int i = 0; i < 8; i++) {
if (ta[day] == 'J')
tmp = 4;
else if (ta[day] == 'O')
tmp = 2;
else if (ta[day] == 'I')
tmp = 1;
if ((i & tmp) != 0 && (i & f) != 0)
sum += solve(day + 1, i);
}
return memo[day][f] = sum % 10007;
}
int main() {
cin >> n;
cin >> ta;
cout << solve(0, 4) << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00518 | C++ | Time Limit Exceeded | #include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;
int n;
long long dp[1111][8];
string s;
map<char, int> shift;
long long rec(int idx, int prev) {
if (idx == n)
return 1;
if (dp[idx][prev])
return dp[idx][prev];
long long ret = 0;
int sft = shift[s[idx]];
for (int i = 1; i < 8; i++) {
if ((i >> sft & 1) && (i & prev)) {
(ret += rec(idx + 1, i)) % MOD;
}
}
return dp[idx][prev] = ret;
}
int main() {
shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
cin >> n >> s;
cout << rec(0, 4) % MOD << endl;
} | #include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;
int n;
long long dp[1111][8];
string s;
map<char, int> shift;
long long rec(int idx, int prev) {
if (idx == n)
return 1;
if (dp[idx][prev])
return dp[idx][prev];
long long ret = 0;
int sft = shift[s[idx]];
for (int i = 1; i < 8; i++) {
if ((i >> sft & 1) && (i & prev)) {
ret += rec(idx + 1, i) % MOD;
}
}
return dp[idx][prev] = ret;
}
int main() {
shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
cin >> n >> s;
cout << rec(0, 4) % MOD << endl;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p00518 | C++ | Runtime Error | // メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
int n;
string ss;
cin >> n >> ss;
const int MAX_N = 1000;
const int mod = 10007;
int dp[MAX_N + 1][7];
for (int j = 0; j < 7; j++) {
dp[0][j] = 0;
if (j == 0)
dp[0][j] = 1;
}
int ans[MAX_N];
ans[0] = 1;
for (int i = 1; i <= n; i++) {
ans[i] = 0;
if (ss[i - 1] == 'J') {
dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
dp[i][1] = 0;
dp[i][2] = 0;
dp[i][3] = ans[i - 1] - dp[i - 1][2];
dp[i][4] = ans[i - 1] - dp[i - 1][1];
dp[i][5] = 0;
dp[i][6] = ans[i - 1];
}
if (ss[i - 1] == 'O') {
dp[i][0] = 0;
dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
dp[i][2] = 0;
dp[i][3] = ans[i - 1] - dp[i - 1][2];
dp[i][4] = 0;
dp[i][5] = ans[i - 1] - dp[i - 1][0];
dp[i][6] = ans[i - 1];
}
if (ss[i - 1] == 'I') {
dp[i][0] = 0;
dp[i][1] = 0;
dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
dp[i][3] = 0;
dp[i][4] = ans[i - 1] - dp[i - 1][1];
dp[i][5] = ans[i - 1] - dp[i - 1][0];
dp[i][6] = ans[i - 1];
}
for (int j = 0; j < 7; j++) {
ans[i] += dp[i][j];
dp[i][j] += mod;
dp[i][j] %= mod;
}
ans[i] %= mod;
}
printf("%d\n", ans[n]);
return 0;
}
// メモリ数確認したか? | // メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
int n;
string ss;
cin >> n >> ss;
const int MAX_N = 1000;
const int mod = 10007;
int dp[MAX_N + 1][7];
for (int j = 0; j < 7; j++) {
dp[0][j] = 0;
if (j == 0)
dp[0][j] = 1;
}
int ans[MAX_N + 1];
ans[0] = 1;
for (int i = 1; i <= n; i++) {
ans[i] = 0;
if (ss[i - 1] == 'J') {
dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
dp[i][1] = 0;
dp[i][2] = 0;
dp[i][3] = ans[i - 1] - dp[i - 1][2];
dp[i][4] = ans[i - 1] - dp[i - 1][1];
dp[i][5] = 0;
dp[i][6] = ans[i - 1];
}
if (ss[i - 1] == 'O') {
dp[i][0] = 0;
dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
dp[i][2] = 0;
dp[i][3] = ans[i - 1] - dp[i - 1][2];
dp[i][4] = 0;
dp[i][5] = ans[i - 1] - dp[i - 1][0];
dp[i][6] = ans[i - 1];
}
if (ss[i - 1] == 'I') {
dp[i][0] = 0;
dp[i][1] = 0;
dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
dp[i][3] = 0;
dp[i][4] = ans[i - 1] - dp[i - 1][1];
dp[i][5] = ans[i - 1] - dp[i - 1][0];
dp[i][6] = ans[i - 1];
}
for (int j = 0; j < 7; j++) {
ans[i] += dp[i][j];
dp[i][j] += mod;
dp[i][j] %= mod;
}
ans[i] %= mod;
}
printf("%d\n", ans[n]);
return 0;
}
// メモリ数確認したか? | replace | 22 | 23 | 22 | 23 | 0 | |
p00519 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;
void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
vector<int> dist(G.size(), -1);
queue<int> que;
que.push(s);
dist[s] = 0;
while (!que.empty()) {
int u = que.front();
que.pop();
if (dist[u] + 1 > MAX)
break;
for (int v : G[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
que.push(v);
res.push_back(v);
}
}
}
}
int main() {
int N, K;
while (cin >> N >> K) {
vector<int> C(N), R(N);
REP(i, N) cin >> C[i] >> R[i];
Graph G(N);
REP(i, N) {
int a, b;
cin >> a >> b;
G[a - 1].push_back(b - 1);
G[b - 1].push_back(a - 1);
}
vector<int> dist(N, INT_MAX);
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(C[0], 0));
dist[0] = 0;
while (!que.empty()) {
int u = que.top().second;
int c = que.top().first;
que.pop();
if (u == N - 1)
break;
if (dist[u] < c - C[u])
continue;
vector<int> next;
bfs(u, R[u], G, next);
for (int v : next) {
if (dist[v] > c) {
dist[v] = c;
que.push(P(c + C[v], v));
}
}
}
cout << dist[N - 1] << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;
void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
vector<int> dist(G.size(), -1);
queue<int> que;
que.push(s);
dist[s] = 0;
while (!que.empty()) {
int u = que.front();
que.pop();
if (dist[u] + 1 > MAX)
break;
for (int v : G[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
que.push(v);
res.push_back(v);
}
}
}
}
int main() {
int N, K;
while (cin >> N >> K) {
vector<int> C(N), R(N);
REP(i, N) cin >> C[i] >> R[i];
Graph G(N);
REP(i, K) {
int a, b;
cin >> a >> b;
G[a - 1].push_back(b - 1);
G[b - 1].push_back(a - 1);
}
vector<int> dist(N, INT_MAX);
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(C[0], 0));
dist[0] = 0;
while (!que.empty()) {
int u = que.top().second;
int c = que.top().first;
que.pop();
if (u == N - 1)
break;
if (dist[u] < c - C[u])
continue;
vector<int> next;
bfs(u, R[u], G, next);
for (int v : next) {
if (dist[v] > c) {
dist[v] = c;
que.push(P(c + C[v], v));
}
}
}
cout << dist[N - 1] << endl;
}
return 0;
} | replace | 34 | 35 | 34 | 35 | 0 | |
p00519 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct P {
int c, r;
};
struct State {
int t, pos, remain;
};
/*--------heap begin---------*/
#define SIZE 5000
#define ELEMENT State
ELEMENT heap[SIZE];
int heap_size = 0;
/*
push(x) (up_heap)
O(logN)
verified: yes
*/
void push(ELEMENT x) {
heap[heap_size] = x;
int pos = heap_size++;
while (1) {
int par = ((pos - 1) >> 1);
if (heap[pos].t >= heap[par].t || pos == 0)
break;
swap(heap[pos], heap[par]);
pos = par;
}
}
/*
pop() (down_heap)
O(logN)
verified: yes
*/
void pop() {
swap(heap[0], heap[--heap_size]);
int pos = 0;
while (2 * pos + 1 < heap_size) {
int nex = (2 * pos + 2 >= heap_size
? 2 * pos + 1
: (heap[2 * pos + 2].t < heap[2 * pos + 1].t ? 2 * pos + 2
: 2 * pos + 1));
if (heap[nex].t >= heap[pos].t)
break; // for maximum heap
swap(heap[pos], heap[nex]);
pos = nex;
}
}
/*
return root (maximum value in the tree)
verified: yes
*/
ELEMENT top() { return heap[0]; }
/*-------heap end----------*/
const int MAX = 5001;
const int INF = (1 << 25);
int T[MAX][MAX];
vector<int> Edge[MAX];
P price[MAX];
int N, K;
void input() {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++)
scanf("%d %d", &price[i].c, &price[i].r);
for (int i = 0; i < K; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
Edge[a].push_back(b);
Edge[b].push_back(a);
}
}
void solve() {
memset(T, -1, sizeof(T));
T[0][0] = 0;
push((State){0, 0, 0});
while (heap_size) {
const State now = top();
pop();
if (now.t > T[now.pos][now.remain])
continue;
if (now.remain > 0) {
for (int i = 0; i < (int)Edge[now.pos].size(); i++) {
State nex = now;
nex.pos = Edge[now.pos][i];
nex.remain--;
if (T[nex.pos][nex.remain] == -1 || T[nex.pos][nex.remain] > nex.t) {
T[nex.pos][nex.remain] = nex.t;
push(nex);
}
}
}
if (T[now.pos][price[now.pos].r] == -1 ||
T[now.pos][price[now.pos].r] > now.t + price[now.pos].c) {
T[now.pos][price[now.pos].r] = now.t + price[now.pos].c;
push((State){now.t + price[now.pos].c, now.pos, price[now.pos].r});
}
}
int ans = INF;
for (int i = 0; i < MAX; i++)
if (T[N - 1][i] >= 0)
ans = min(ans, T[N - 1][i]);
printf("%d\n", ans);
}
int main() {
input();
solve();
return 0;
} | #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct P {
int c, r;
};
struct State {
int t, pos, remain;
};
/*--------heap begin---------*/
#define SIZE 10000
#define ELEMENT State
ELEMENT heap[SIZE];
int heap_size = 0;
/*
push(x) (up_heap)
O(logN)
verified: yes
*/
void push(ELEMENT x) {
heap[heap_size] = x;
int pos = heap_size++;
while (1) {
int par = ((pos - 1) >> 1);
if (heap[pos].t >= heap[par].t || pos == 0)
break;
swap(heap[pos], heap[par]);
pos = par;
}
}
/*
pop() (down_heap)
O(logN)
verified: yes
*/
void pop() {
swap(heap[0], heap[--heap_size]);
int pos = 0;
while (2 * pos + 1 < heap_size) {
int nex = (2 * pos + 2 >= heap_size
? 2 * pos + 1
: (heap[2 * pos + 2].t < heap[2 * pos + 1].t ? 2 * pos + 2
: 2 * pos + 1));
if (heap[nex].t >= heap[pos].t)
break; // for maximum heap
swap(heap[pos], heap[nex]);
pos = nex;
}
}
/*
return root (maximum value in the tree)
verified: yes
*/
ELEMENT top() { return heap[0]; }
/*-------heap end----------*/
const int MAX = 5001;
const int INF = (1 << 25);
int T[MAX][MAX];
vector<int> Edge[MAX];
P price[MAX];
int N, K;
void input() {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++)
scanf("%d %d", &price[i].c, &price[i].r);
for (int i = 0; i < K; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
Edge[a].push_back(b);
Edge[b].push_back(a);
}
}
void solve() {
memset(T, -1, sizeof(T));
T[0][0] = 0;
push((State){0, 0, 0});
while (heap_size) {
const State now = top();
pop();
if (now.t > T[now.pos][now.remain])
continue;
if (now.remain > 0) {
for (int i = 0; i < (int)Edge[now.pos].size(); i++) {
State nex = now;
nex.pos = Edge[now.pos][i];
nex.remain--;
if (T[nex.pos][nex.remain] == -1 || T[nex.pos][nex.remain] > nex.t) {
T[nex.pos][nex.remain] = nex.t;
push(nex);
}
}
}
if (T[now.pos][price[now.pos].r] == -1 ||
T[now.pos][price[now.pos].r] > now.t + price[now.pos].c) {
T[now.pos][price[now.pos].r] = now.t + price[now.pos].c;
push((State){now.t + price[now.pos].c, now.pos, price[now.pos].r});
}
}
int ans = INF;
for (int i = 0; i < MAX; i++)
if (T[N - 1][i] >= 0)
ans = min(ans, T[N - 1][i]);
printf("%d\n", ans);
}
int main() {
input();
solve();
return 0;
} | replace | 16 | 17 | 16 | 17 | 127 | /tmp/c90a7a7c-a527-400b-947c-ba339d3c3958.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p00519 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 5500
bool x[MAX_N][MAX_N];
int y[MAX_N][2];
bool z[MAX_N];
int m[MAX_N];
int s[MAX_N];
int t[MAX_N][MAX_N];
queue<int> Q;
int main() {
memset(x, false, sizeof(x));
for (int i = 0; i < 5500; i++) {
for (int j = 0; j < 5500; j++) {
m[i] = 1000000000;
s[i] = 0;
t[i][j] = 0;
}
}
int N, K, a, b;
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> y[i][0] >> y[i][1];
}
for (int i = 1; i <= K; i++) {
cin >> a >> b;
x[a][b] = true;
x[b][a] = true;
t[a][s[a]] = b;
t[b][s[b]] = a;
s[a] += 1;
s[b] += 1;
}
int p;
int r;
int u;
u = N;
if (K > 6000) {
u = 500;
}
m[1] = 0;
for (int i = 0; i < u; i++) {
for (int j = 1; j <= N; j++) {
if (m[j] < 100000000) {
while (!Q.empty()) {
Q.pop();
}
Q.push(j);
memset(z, false, sizeof(z));
for (int k = 0; k < y[j][1]; k++) {
r = Q.size();
for (int l = 0; l < r; l++) {
p = Q.front();
for (int o = 0; o < s[p]; o++) {
if (x[p][t[p][o]] == true && z[t[p][o]] == false) {
Q.push(t[p][o]);
z[t[p][o]] = true;
m[t[p][o]] = min(m[t[p][o]], m[j] + y[j][0]);
}
}
Q.pop();
}
if (Q.empty()) {
goto Exit;
}
}
Exit:;
}
}
}
cout << m[N] << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 5500
bool x[MAX_N][MAX_N];
int y[MAX_N][2];
bool z[MAX_N];
int m[MAX_N];
int s[MAX_N];
int t[MAX_N][MAX_N];
queue<int> Q;
int main() {
memset(x, false, sizeof(x));
for (int i = 0; i < 5500; i++) {
for (int j = 0; j < 5500; j++) {
m[i] = 1000000000;
s[i] = 0;
t[i][j] = 0;
}
}
int N, K, a, b;
cin >> N >> K;
for (int i = 1; i <= N; i++) {
cin >> y[i][0] >> y[i][1];
}
for (int i = 1; i <= K; i++) {
cin >> a >> b;
x[a][b] = true;
x[b][a] = true;
t[a][s[a]] = b;
t[b][s[b]] = a;
s[a] += 1;
s[b] += 1;
}
int p;
int r;
int u;
u = N;
if (K > 6000) {
u = 20;
}
m[1] = 0;
for (int i = 0; i < u; i++) {
for (int j = 1; j <= N; j++) {
if (m[j] < 100000000) {
while (!Q.empty()) {
Q.pop();
}
Q.push(j);
memset(z, false, sizeof(z));
for (int k = 0; k < y[j][1]; k++) {
r = Q.size();
for (int l = 0; l < r; l++) {
p = Q.front();
for (int o = 0; o < s[p]; o++) {
if (x[p][t[p][o]] == true && z[t[p][o]] == false) {
Q.push(t[p][o]);
z[t[p][o]] = true;
m[t[p][o]] = min(m[t[p][o]], m[j] + y[j][0]);
}
}
Q.pop();
}
if (Q.empty()) {
goto Exit;
}
}
Exit:;
}
}
}
cout << m[N] << endl;
return 0;
} | replace | 46 | 47 | 46 | 47 | TLE | |
p00519 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
static const int INF = 2e9;
struct Edge {
int to;
int cost;
};
vector<Edge> G1[5001];
vector<Edge> G2[5001];
int d[5001];
bool isUsed[5001];
void solve() {
int N, K;
cin >> N >> K;
vector<int> C(N);
vector<int> R(N);
for (int i = 0; i < N; ++i) {
scanf("%d %d", C[i], R[i]);
// cin >> C[i] >> R[i];
}
for (int i = 0; i < K; ++i) {
int from, to;
Edge e1, e2;
cin >> from >> to;
--from;
--to;
e1.to = to;
e1.cost = 1;
e2.to = from;
e2.cost = 1;
G1[from].push_back(e1);
G1[to].push_back(e2);
}
for (int i = 0; i < N; ++i) {
memset(isUsed, 0, sizeof(isUsed));
queue<P> que;
que.push(P(i, R[i]));
isUsed[i] = true;
while (!que.empty()) {
P p = que.front();
que.pop();
int v = p.first;
for (int j = 0; j < G1[v].size(); ++j) {
int to = G1[v][j].to;
if (!isUsed[to] && p.second >= 1) {
isUsed[to] = true;
que.push(P(to, p.second - 1));
Edge e;
e.to = to;
e.cost = C[i];
G2[i].push_back(e);
}
}
}
}
for (int i = 0; i < N; ++i) {
d[i] = INF;
}
priority_queue<P, vector<P>, greater<P>> pque;
pque.push(P(0, 0));
d[0] = 0;
while (!pque.empty()) {
P p = pque.top();
pque.pop();
int v = p.first;
for (int i = 0; i < G2[v].size(); ++i) {
Edge e = G2[v][i];
if (d[e.to] > p.second + e.cost) {
d[e.to] = p.second + e.cost;
pque.push(P(e.to, d[e.to]));
}
}
}
cout << d[N - 1] << endl;
}
int main() {
solve();
return (0);
} | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
static const int INF = 2e9;
struct Edge {
int to;
int cost;
};
vector<Edge> G1[5001];
vector<Edge> G2[5001];
int d[5001];
bool isUsed[5001];
void solve() {
int N, K;
cin >> N >> K;
vector<int> C(N);
vector<int> R(N);
for (int i = 0; i < N; ++i) {
scanf("%d %d", &C[i], &R[i]);
// cin >> C[i] >> R[i];
}
for (int i = 0; i < K; ++i) {
int from, to;
Edge e1, e2;
cin >> from >> to;
--from;
--to;
e1.to = to;
e1.cost = 1;
e2.to = from;
e2.cost = 1;
G1[from].push_back(e1);
G1[to].push_back(e2);
}
for (int i = 0; i < N; ++i) {
memset(isUsed, 0, sizeof(isUsed));
queue<P> que;
que.push(P(i, R[i]));
isUsed[i] = true;
while (!que.empty()) {
P p = que.front();
que.pop();
int v = p.first;
for (int j = 0; j < G1[v].size(); ++j) {
int to = G1[v][j].to;
if (!isUsed[to] && p.second >= 1) {
isUsed[to] = true;
que.push(P(to, p.second - 1));
Edge e;
e.to = to;
e.cost = C[i];
G2[i].push_back(e);
}
}
}
}
for (int i = 0; i < N; ++i) {
d[i] = INF;
}
priority_queue<P, vector<P>, greater<P>> pque;
pque.push(P(0, 0));
d[0] = 0;
while (!pque.empty()) {
P p = pque.top();
pque.pop();
int v = p.first;
for (int i = 0; i < G2[v].size(); ++i) {
Edge e = G2[v][i];
if (d[e.to] > p.second + e.cost) {
d[e.to] = p.second + e.cost;
pque.push(P(e.to, d[e.to]));
}
}
}
cout << d[N - 1] << endl;
}
int main() {
solve();
return (0);
} | replace | 27 | 28 | 27 | 28 | -11 | |
p00521 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n, m, cnt = 0;
char c[1001][1001], x[3][3];
bool check(int i, int j) {
FOR(k, 1, 2)
FOR(l, 1, 2)
if (c[i + k - 1][j + l - 1] != x[k][l])
return false;
return true;
}
int solve(char y, int i, int j) {
int cnt1 = 0, cnt2 = 0;
char z = c[i][j];
if (check(i, j))
cnt1++;
if (check(i - 1, j - 1))
cnt1++;
if (check(i - 1, j))
cnt1++;
if (check(i, j - 1))
cnt1++;
c[i][j] = y;
if (check(i, j))
cnt2++;
if (check(i - 1, j - 1))
cnt2++;
if (check(i - 1, j))
cnt2++;
if (check(i, j - 1))
cnt2++;
c[i][j] = z;
return (cnt2 - cnt1);
}
int main() {
cin >> m >> n;
FOR(i, 1, m) {
scanf("\n");
FOR(j, 1, n)
scanf("%c", &c[i][j]);
}
scanf("\n%c%c\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
For(i, 1, m) For(j, 1, n) {
if (check(i, j))
cnt++;
}
int res = 0;
FOR(i, 1, m)
FOR(j, 1, n) {
res = max(res, solve('J', i, j));
res = max(res, solve('O', i, j));
res = max(res, solve('I', i, j));
}
cout << cnt + res << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n, m, cnt = 0;
char c[1002][1002], x[3][3];
bool check(int i, int j) {
FOR(k, 1, 2)
FOR(l, 1, 2)
if (c[i + k - 1][j + l - 1] != x[k][l])
return false;
return true;
}
int solve(char y, int i, int j) {
int cnt1 = 0, cnt2 = 0;
char z = c[i][j];
if (check(i, j))
cnt1++;
if (check(i - 1, j - 1))
cnt1++;
if (check(i - 1, j))
cnt1++;
if (check(i, j - 1))
cnt1++;
c[i][j] = y;
if (check(i, j))
cnt2++;
if (check(i - 1, j - 1))
cnt2++;
if (check(i - 1, j))
cnt2++;
if (check(i, j - 1))
cnt2++;
c[i][j] = z;
return (cnt2 - cnt1);
}
int main() {
cin >> m >> n;
FOR(i, 1, m) {
scanf("\n");
FOR(j, 1, n)
scanf("%c", &c[i][j]);
}
scanf("\n%c%c\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
For(i, 1, m) For(j, 1, n) {
if (check(i, j))
cnt++;
}
int res = 0;
FOR(i, 1, m)
FOR(j, 1, n) {
res = max(res, solve('J', i, j));
res = max(res, solve('O', i, j));
res = max(res, solve('I', i, j));
}
cout << cnt + res << '\n';
} | replace | 16 | 17 | 16 | 17 | 0 | |
p00521 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int mm[1001][1001][3];
string flag[1000], emb[2];
int main() {
int m, n;
cin >> m >> n;
int ans = 0;
for (int i = 0; i < m; ++i)
cin >> flag[i];
for (int i = 0; i < 2; ++i)
cin >> emb[i];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int cnt = 0, mx, my;
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (flag[i + k][j + l] == emb[k][l])
++cnt;
else {
mx = l;
my = k;
}
}
}
if (cnt == 4)
++ans;
else if (cnt == 3) {
if (emb[my][mx] == 'J')
++mm[i + my][j + mx][0];
else if (emb[my][mx] == 'O')
++mm[i + my][j + mx][1];
else
++mm[i + my][j + mx][2];
}
}
}
int w = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < 3; ++k)
w = max(w, mm[i][j][k]);
}
}
cout << ans + w << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int mm[1005][1005][3];
string flag[1005], emb[2];
int main() {
int m, n;
cin >> m >> n;
int ans = 0;
for (int i = 0; i < m; ++i)
cin >> flag[i];
for (int i = 0; i < 2; ++i)
cin >> emb[i];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int cnt = 0, mx, my;
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (flag[i + k][j + l] == emb[k][l])
++cnt;
else {
mx = l;
my = k;
}
}
}
if (cnt == 4)
++ans;
else if (cnt == 3) {
if (emb[my][mx] == 'J')
++mm[i + my][j + mx][0];
else if (emb[my][mx] == 'O')
++mm[i + my][j + mx][1];
else
++mm[i + my][j + mx][2];
}
}
}
int w = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < 3; ++k)
w = max(w, mm[i][j][k]);
}
}
cout << ans + w << endl;
return 0;
} | replace | 4 | 6 | 4 | 6 | 0 | |
p00522 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++)
cin >> p[i];
sort(p, p + m);
for (int i = 0; i < m; i++)
sum[i + 1] = sum[i] + p[m - 1 - i];
for (int i = 0; i < n; i++)
cin >> c[i] >> e[i];
for (int i = 0; i <= m; i++)
dp[i] = 1e9;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = m + c[i]; j >= 0; j--) {
int nxt = min(j, m);
dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
}
}
int ans = 0;
for (int i = 0; i <= m; i++)
ans = max(ans, sum[i] - dp[i]);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++)
cin >> p[i];
sort(p, p + m);
for (int i = 0; i < m; i++)
sum[i + 1] = sum[i] + p[m - 1 - i];
for (int i = 0; i < n; i++)
cin >> c[i] >> e[i];
for (int i = 0; i <= m; i++)
dp[i] = 1e9;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = m + c[i]; j >= c[i]; j--) {
int nxt = min(j, m);
dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
}
}
int ans = 0;
for (int i = 0; i <= m; i++)
ans = max(ans, sum[i] - dp[i]);
cout << ans << endl;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p00522 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define rep(x, y) for (int x = 0; x < (y); x++)
typedef long long ll;
int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];
ll dfs(int m, int n) {
if (m > M || n >= N)
return 0;
ll res = 0;
if (m <= M)
res = max(res,
dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
res = max(res, dfs(m, n + 1));
return res;
}
int main() {
scanf("%d%d", &M, &N);
P[0] = 0;
rep(i, M) scanf("%d", P + i + 1);
sort(P + 1, P + M + 1, greater<int>());
rep(i, M) P[i + 1] += P[i];
rep(i, N) scanf("%d%d", C + i, E + i);
memset(dp, -1, sizeof(dp));
printf("%lld\n", dfs(0, 0));
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define rep(x, y) for (int x = 0; x < (y); x++)
typedef long long ll;
int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];
ll dfs(int m, int n) {
if (m > M || n >= N)
return 0;
ll &res = dp[m][n];
if (res >= 0)
return res;
res = 0;
if (m <= M)
res = max(res,
dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
res = max(res, dfs(m, n + 1));
return res;
}
int main() {
scanf("%d%d", &M, &N);
P[0] = 0;
rep(i, M) scanf("%d", P + i + 1);
sort(P + 1, P + M + 1, greater<int>());
rep(i, M) P[i + 1] += P[i];
rep(i, N) scanf("%d%d", C + i, E + i);
memset(dp, -1, sizeof(dp));
printf("%lld\n", dfs(0, 0));
} | replace | 19 | 20 | 19 | 23 | TLE | |
p00522 | C++ | Runtime Error | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
int m, n;
cin >> m >> n;
int c[550], e[550];
vector<int> p(m);
REP(i, 100100) dp[i] = INF;
REP(i, m) cin >> p[i];
dp[0] = 0;
int sum = 0;
REP(i, n) {
cin >> c[i] >> e[i];
for (int j = sum; j >= 0; j--) {
dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
}
sum += c[i];
sum = min(sum, 100000);
}
for (int i = sum; i >= 0; i--) {
dp[i] = min(dp[i], dp[i + 1]);
}
sort(ALL(p), greater<int>());
sum = 0;
int ans = 0;
REP(i, m) {
sum += p[i];
ans = max(ans, sum - dp[i + 1]);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
int m, n;
cin >> m >> n;
int c[550], e[550];
vector<int> p(m);
REP(i, 100100) dp[i] = INF;
REP(i, m) cin >> p[i];
dp[0] = 0;
int sum = 0;
REP(i, n) {
cin >> c[i] >> e[i];
for (int j = sum; j >= 0; j--) {
dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
}
sum += c[i];
sum = min(sum, 20000);
}
for (int i = sum; i >= 0; i--) {
dp[i] = min(dp[i], dp[i + 1]);
}
sort(ALL(p), greater<int>());
sum = 0;
int ans = 0;
REP(i, m) {
sum += p[i];
ans = max(ans, sum - dp[i + 1]);
}
cout << ans << endl;
} | replace | 26 | 27 | 26 | 27 | 0 | |
p00522 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
int n, m;
cin >> n >> m;
vi in(n);
rep(i, n) cin >> in[i];
rep(i, n) in[i] *= -1;
sort(all(in));
vi dp(10010, inf);
dp[0] = 0;
rep(i, m) {
int a, b;
cin >> a >> b;
for (int j = 10010; j >= 0; j--) {
dp[j] = min(dp[j], dp[max(0, j - a)] + b);
}
}
int out = 0;
int sum = 0;
rep(i, n) {
sum -= in[i];
out = max(out, sum - dp[i + 1]);
}
cout << out << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
int n, m;
cin >> n >> m;
vi in(n);
rep(i, n) cin >> in[i];
rep(i, n) in[i] *= -1;
sort(all(in));
vi dp(10010, inf);
dp[0] = 0;
rep(i, m) {
int a, b;
cin >> a >> b;
for (int j = 10009; j >= 0; j--) {
dp[j] = min(dp[j], dp[max(0, j - a)] + b);
}
}
int out = 0;
int sum = 0;
rep(i, n) {
sum -= in[i];
out = max(out, sum - dp[i + 1]);
}
cout << out << endl;
return 0;
} | replace | 44 | 45 | 44 | 45 | 0 | |
p00522 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
void Solve() {
int m, n;
cin >> m >> n;
vector<int> p(m), c(n), e(n);
rep(i, 0, m) cin >> p[i];
rep(i, 0, n) cin >> c[i] >> e[i];
sort(p.rbegin(), p.rend());
vector<int> sum(m + 1);
rep(i, 0, m) sum[i + 1] = sum[i] + p[i];
int ans = max(0, sum[c[0]] - e[0]);
static int dp[500][10001];
fill_n((int *)dp, 500 * 10001, INF);
rep(i, 0, c[0] + 1) dp[0][i] = e[0];
rep(i, 1, n) {
rep(j, 0, c[i] + 1) dp[i][j] = e[i];
rep(j, 0, m + 1) {
dp[i][j] =
min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
ans = max(ans, sum[j] - dp[i][j]);
}
}
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;
const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
void Solve() {
int m, n;
cin >> m >> n;
vector<int> p(m), c(n), e(n);
rep(i, 0, m) cin >> p[i];
rep(i, 0, n) cin >> c[i] >> e[i];
sort(p.rbegin(), p.rend());
vector<int> sum(m + 1);
rep(i, 0, m) sum[i + 1] = sum[i] + p[i];
int ans = max(0, sum[min(c[0], m)] - e[0]);
static int dp[500][10001];
fill_n((int *)dp, 500 * 10001, INF);
rep(i, 0, c[0] + 1) dp[0][i] = e[0];
rep(i, 1, n) {
rep(j, 0, c[i] + 1) dp[i][j] = e[i];
rep(j, 0, m + 1) {
dp[i][j] =
min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
ans = max(ans, sum[j] - dp[i][j]);
}
}
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
Solve();
return 0;
} | replace | 43 | 44 | 43 | 44 | 0 | |
p00522 | C++ | Time Limit Exceeded | // AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;
int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];
int main() {
scanf("%d%d", &M, &N);
rep(i, M) scanf("%d", P + i);
rep(i, N) scanf("%d%d", C + i, E + i);
std::sort(P, P + M, std::greater<int>());
rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
dp[0][0] = 0;
rep(i, N) rep(j, M + 1) rep(k, C[i] + 1) if (j + k <= M) dp[i + 1][j + k] =
std::min(dp[i + 1][j + k], dp[i][j] + (k ? E[i] : 0));
int ans = 0, sum = 0;
rep(j, M + 1) {
ans = std::max(ans, sum - dp[N][j]);
if (j < M)
sum += P[j];
}
printf("%d\n", ans);
return 0;
} | // AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)
const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;
int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];
int main() {
scanf("%d%d", &M, &N);
rep(i, M) scanf("%d", P + i);
rep(i, N) scanf("%d%d", C + i, E + i);
std::sort(P, P + M, std::greater<int>());
rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
dp[0][0] = 0;
rep(i, N) rep(j, M + 1) {
dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j]);
dp[i + 1][std::min(M, j + C[i])] =
std::min(dp[i + 1][std::min(M, j + C[i])], dp[i][j] + E[i]);
}
int ans = 0, sum = 0;
rep(j, M + 1) {
ans = std::max(ans, sum - dp[N][j]);
if (j < M)
sum += P[j];
}
printf("%d\n", ans);
return 0;
} | replace | 23 | 25 | 23 | 29 | TLE | |
p00522 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
struct box {
int c, e;
};
int dp[10010];
int main() {
int m, n;
cin >> m >> n;
int p[n], sum[m + 1];
sum[0] = 0;
box b[n];
for (int i = 0; i < m; i++)
cin >> p[i];
for (int i = 0; i < n; i++)
cin >> b[i].c >> b[i].e;
sort(p, p + m, greater<int>());
for (int i = 0; i < m; i++)
sum[i + 1] = sum[i] + p[i];
for (int i = 0; i <= m; i++)
dp[i] = 1e9;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = m + b[i].c; j >= b[i].c; j--) {
int next = min(j, m);
dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
}
}
int ans = 0;
for (int i = 0; i <= m; i++)
ans = max(ans, sum[i] - dp[i]);
cout << ans << endl;
} | #include <algorithm>
#include <iostream>
using namespace std;
struct box {
int c, e;
};
int dp[10010];
int main() {
int m, n;
cin >> m >> n;
int p[m], sum[m + 1];
sum[0] = 0;
box b[n];
for (int i = 0; i < m; i++)
cin >> p[i];
for (int i = 0; i < n; i++)
cin >> b[i].c >> b[i].e;
sort(p, p + m, greater<int>());
for (int i = 0; i < m; i++)
sum[i + 1] = sum[i] + p[i];
for (int i = 0; i <= m; i++)
dp[i] = 1e9;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = m + b[i].c; j >= b[i].c; j--) {
int next = min(j, m);
dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
}
}
int ans = 0;
for (int i = 0; i <= m; i++)
ans = max(ans, sum[i] - dp[i]);
cout << ans << endl;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p00522 | C++ | Time Limit Exceeded | #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#define INF (1 << 29)
using namespace std;
int main() {
int N, M;
scanf("%d", &N);
scanf("%d", &M);
vector<int> P(N);
vector<int> C(M);
vector<int> E(M);
for (int i = 0; i < N; i++) {
scanf("%d", &P[i]);
}
for (int i = 0; i < M; i++) {
scanf("%d", &C[i]);
scanf("%d", &E[i]);
}
vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
dp[0][0] = 0;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= N; j++) {
dp[i][j] = dp[i - 1][j];
for (int k = max(0, j - C[i - 1]); k < j; k++) {
dp[i][j] = min(dp[i][j], dp[i - 1][k] + E[i - 1]);
}
}
}
sort(P.begin(), P.end(), greater<int>());
vector<int> S(N + 1);
S[0] = 0;
for (int i = 1; i <= N; i++) {
S[i] = S[i - 1] + P[i - 1];
}
int ret = 0;
for (int i = 0; i <= N; i++) {
ret = max(ret, S[i] - dp[M][i]);
}
printf("%d\n", ret);
return 0;
} | #include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#define INF (1 << 29)
using namespace std;
int main() {
int N, M;
scanf("%d", &N);
scanf("%d", &M);
vector<int> P(N);
vector<int> C(M);
vector<int> E(M);
for (int i = 0; i < N; i++) {
scanf("%d", &P[i]);
}
for (int i = 0; i < M; i++) {
scanf("%d", &C[i]);
scanf("%d", &E[i]);
}
vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
dp[0][0] = 0;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= N; j++) {
dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - C[i - 1])] + E[i - 1]);
}
}
sort(P.begin(), P.end(), greater<int>());
vector<int> S(N + 1);
S[0] = 0;
for (int i = 1; i <= N; i++) {
S[i] = S[i - 1] + P[i - 1];
}
int ret = 0;
for (int i = 0; i <= N; i++) {
ret = max(ret, S[i] - dp[M][i]);
}
printf("%d\n", ret);
return 0;
} | replace | 33 | 38 | 33 | 34 | TLE | |
p00522 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
memset(x, 127, sizeof(x));
x[0][0] = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
sum += s[i];
}
for (int i = 1; i <= m; i++) {
cin >> t[i][0] >> t[i][1];
for (int j = 0; j <= n; j++) {
x[i][j] = min(x[i][j], x[i - 1][j]);
a = min(t[i][0], n - j);
c = 1;
if (t[i][0] == 10000) {
c = 10000;
}
if (sum == n && n == 10000) {
c = 10000;
}
for (int k = c; k <= a; k++) {
x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
}
}
}
sort(s, s + n);
sum = 0;
for (int i = 0; i <= n; i++) {
MAX = max(MAX, sum - x[m][i]);
if (i != n) {
sum += s[n - i - 1];
}
}
cout << MAX << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
memset(x, 127, sizeof(x));
x[0][0] = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
sum += s[i];
}
for (int i = 1; i <= m; i++) {
cin >> t[i][0] >> t[i][1];
for (int j = 0; j <= n; j++) {
x[i][j] = min(x[i][j], x[i - 1][j]);
a = min(t[i][0], n - j);
c = 1;
if (t[i][0] == 10000) {
c = 10000;
} else if (t[i][0] > 3000 && n == 10000) {
c = t[i][0] / 2;
}
if (sum == n && n == 10000) {
c = 10000;
}
for (int k = c; k <= a; k++) {
x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
}
}
}
sort(s, s + n);
sum = 0;
for (int i = 0; i <= n; i++) {
MAX = max(MAX, sum - x[m][i]);
if (i != n) {
sum += s[n - i - 1];
}
}
cout << MAX << endl;
return 0;
} | insert | 21 | 21 | 21 | 23 | TLE | |
p00522 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
int main() {
int M, N, P[10000], C[100], E[100];
cin >> M >> N;
for (int i = 0; i < M; i++) {
cin >> P[i];
}
sort(P, P + M, greater<int>());
for (int i = 0; i < N; i++) {
cin >> C[i] >> E[i];
}
int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
fill_n(dp, 50001, INF);
dp[0] = 0;
for (int i = 0; i < N; i++) {
for (int j = M; j >= 0; j--) {
if (dp[j] != INF) {
dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
}
}
}
int sum[10001];
sum[1] = P[0];
for (int i = 2; i <= M; i++) {
sum[i] = sum[i - 1] + P[i - 1];
}
int ret = 0;
for (int i = 1; i <= M; i++) {
if (dp[i] != INF)
ret = max(ret, sum[i] - dp[i]);
}
cout << ret << endl;
return (0);
} | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
int main() {
int M, N, P[10000], C[500], E[500];
cin >> M >> N;
for (int i = 0; i < M; i++) {
cin >> P[i];
}
sort(P, P + M, greater<int>());
for (int i = 0; i < N; i++) {
cin >> C[i] >> E[i];
}
int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
fill_n(dp, 50001, INF);
dp[0] = 0;
for (int i = 0; i < N; i++) {
for (int j = M; j >= 0; j--) {
if (dp[j] != INF) {
dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
}
}
}
int sum[10001];
sum[1] = P[0];
for (int i = 2; i <= M; i++) {
sum[i] = sum[i - 1] + P[i - 1];
}
int ret = 0;
for (int i = 1; i <= M; i++) {
if (dp[i] != INF)
ret = max(ret, sum[i] - dp[i]);
}
cout << ret << endl;
return (0);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00522 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)
int manju[10000], c[10000], e[10000];
int m, n;
int sum[10000], dp[501][10001];
int main() {
cin >> m >> n;
REP(i, m) scanf("%d", &manju[i]);
REP(i, n) scanf("%d %d", &c[i], &e[i]);
sort(manju, manju + m, greater<int>());
sum[0] = 0;
REP(i, m) sum[i + 1] = sum[i] + manju[i];
fill(dp[0], dp[501], INF);
dp[0][0] = 0;
REP(i, n) {
REP(j, m + 1) {
dp[i + 1][j] = dp[i][j];
if (j >= c[i])
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
else
dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
}
}
int ans = 0;
REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)
int manju[10000], c[10000], e[10000];
int m, n;
int sum[10001], dp[501][10001];
int main() {
cin >> m >> n;
REP(i, m) scanf("%d", &manju[i]);
REP(i, n) scanf("%d %d", &c[i], &e[i]);
sort(manju, manju + m, greater<int>());
sum[0] = 0;
REP(i, m) sum[i + 1] = sum[i] + manju[i];
fill(dp[0], dp[501], INF);
dp[0][0] = 0;
REP(i, n) {
REP(j, m + 1) {
dp[i + 1][j] = dp[i][j];
if (j >= c[i])
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
else
dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
}
}
int ans = 0;
REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
cout << ans << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p00523 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int in() {
int a;
scanf("%d", &a);
return a;
}
ll lin() {
ll a;
scanf("%lld", &a);
return a;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int n;
int A[100010];
ll sum[100010];
bool check(int i, ll x, bool f = false) {
int nowidx = i;
ll cur = i == 0 ? 0 : sum[i - 1];
rep(_, 2) {
nowidx = lower_bound(sum, sum + i + n, cur + x) - sum;
if (nowidx == i + n)
return false;
cur = sum[nowidx];
}
return sum[i + n - 1] - sum[nowidx] >= x;
}
int main() {
n = in();
rep(i, n) {
A[i] = in();
sum[i] = A[i];
if (i)
sum[i] += sum[i - 1];
}
rep(i, n - 1) { sum[i + n] = A[i] + sum[i + n - 1]; }
ll ma = 0;
rep(i, n) {
ll l = 0, r = linf;
rep(_, 65) {
ll mid = (l + r) / 2;
(check(i, mid) ? l : r) = mid;
}
chmax(ma, l);
}
printf("%lld\n", ma);
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai \
ios::sync_with_stdio(false); \
cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
os << "(" << p.fst << ", " << p.scd << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
rep(i, v.size()) {
if (i)
os << ", ";
os << v[i];
}
return os;
}
template <typename T> T &max(T &a, T &b) {
if (a >= b)
return a;
return b;
}
template <typename T> T &min(T &a, T &b) {
if (a < b)
return a;
return b;
}
template <typename T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int in() {
int a;
scanf("%d", &a);
return a;
}
ll lin() {
ll a;
scanf("%lld", &a);
return a;
}
constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;
int n;
int A[100010];
ll sum[200010];
bool check(int i, ll x, bool f = false) {
int nowidx = i;
ll cur = i == 0 ? 0 : sum[i - 1];
rep(_, 2) {
nowidx = lower_bound(sum, sum + i + n, cur + x) - sum;
if (nowidx == i + n)
return false;
cur = sum[nowidx];
}
return sum[i + n - 1] - sum[nowidx] >= x;
}
int main() {
n = in();
rep(i, n) {
A[i] = in();
sum[i] = A[i];
if (i)
sum[i] += sum[i - 1];
}
rep(i, n - 1) { sum[i + n] = A[i] + sum[i + n - 1]; }
ll ma = 0;
rep(i, n) {
ll l = 0, r = linf;
rep(_, 65) {
ll mid = (l + r) / 2;
(check(i, mid) ? l : r) = mid;
}
chmax(ma, l);
}
printf("%lld\n", ma);
} | replace | 93 | 94 | 93 | 94 | 0 | |
p00523 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;
typedef long long ll;
int main() {
int n, a[100001];
ll ssum[100001 * 2] = {0};
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
ssum[0] = a[0];
for (int i = 1; i < n * 2; i++)
ssum[i] = ssum[i - 1] + a[i % n];
ll al = 0, ar = INF, amid;
int c2;
while (al < ar) {
amid = (al + ar) / 2; // 解を二分探索
bool judge = false;
for (int i = 0; i < n; i++) {
int l = i, r = n + i; // 添字を二分探索
while (l < r) {
int mid = (l + r) / 2;
ll sub = ssum[mid] - ssum[i];
if (sub < amid)
l = mid + 1;
else
r = mid;
}
c2 = l++;
r = n + i;
while (l < r) {
int mid = (l + r) / 2;
ll sub = ssum[mid] - ssum[c2];
if (sub < amid)
l = mid + 1;
else
r = mid;
}
if (ssum[i + n] - ssum[r] >= amid) {
judge = true;
break;
}
}
if (judge)
al = amid;
else
ar = amid - 1;
}
cout << al << endl;
} | #include <algorithm>
#include <iostream>
#define INF (1e+15)
using namespace std;
typedef long long ll;
int main() {
int n, a[100001];
ll ssum[100001 * 2] = {0};
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
ssum[0] = a[0];
for (int i = 1; i < n * 2; i++)
ssum[i] = ssum[i - 1] + a[i % n];
ll al = 0, ar = INF, amid;
int c2;
while (al < ar) {
amid = ((al + 1) + ar) / 2; // 解を二分探索
bool judge = false;
for (int i = 0; i < n; i++) {
int l = i, r = n + i; // 添字を二分探索
while (l < r) {
int mid = (l + r) / 2;
ll sub = ssum[mid] - ssum[i];
if (sub < amid)
l = mid + 1;
else
r = mid;
}
c2 = l++;
r = n + i;
while (l < r) {
int mid = (l + r) / 2;
ll sub = ssum[mid] - ssum[c2];
if (sub < amid)
l = mid + 1;
else
r = mid;
}
if (ssum[i + n] - ssum[r] >= amid) {
judge = true;
break;
}
}
if (judge)
al = amid;
else
ar = amid - 1;
}
cout << al << endl;
} | replace | 23 | 24 | 23 | 24 | TLE | |
p00523 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
struct Piece {
int top;
int end;
ll size;
};
class Baumkuchen {
private:
int N;
const int DevideNum;
ll *A;
ll *sum;
Piece *piece;
void input();
void newArrays(int n);
void setSum();
bool isContainPiece(int i, const Piece &p);
ll calcPieceSize(const Piece &p);
Piece cutDivideBaumkuchen(int _top, int _end, ll min_piece_size);
bool cutOK(ll min_piece_size);
public:
Baumkuchen(int _DevideNum);
~Baumkuchen();
ll solve();
};
Baumkuchen::Baumkuchen(int _DevideNum) : N(0), DevideNum(_DevideNum) {
A = NULL;
sum = NULL;
piece = NULL;
}
Baumkuchen::~Baumkuchen() {
delete[] A;
delete[] sum;
delete[] piece;
}
void Baumkuchen::newArrays(int n) {
A = new ll[n];
sum = new ll[2 * n + 1];
piece = new Piece[DevideNum];
}
void Baumkuchen::input() {
cin >> N;
newArrays(N);
if (N > DevideNum) {
cout << "0" << endl;
exit(1);
}
for (int i = 0; i < N; i++)
cin >> A[i];
}
void Baumkuchen::setSum() {
sum[0] = A[0];
for (int i = 1; i < N; i++)
sum[i] = sum[i - 1] + A[i];
for (int i = 0; i < N; i++)
sum[i + N] = sum[i] + sum[N - 1];
}
bool Baumkuchen::isContainPiece(int i, const Piece &p) {
return (p.top <= i % N && i % N <= p.end) ||
(p.top <= (i % N) + N && (i % N) + N <= p.end);
}
ll Baumkuchen::calcPieceSize(const Piece &p) {
return (p.top <= p.end) ? (sum[p.end] - sum[p.top] + A[p.top % N]) : -1;
}
Piece Baumkuchen::cutDivideBaumkuchen(int _top, int _end, ll min_piece_size) {
Piece p;
p.top = _top;
p.end = _end;
if (calcPieceSize(p) < min_piece_size) {
p.size = -1;
return p;
}
int end_max = _end, end_min = _top;
while (end_max > end_min + 1) {
p.end = (end_max + end_min) / 2;
if (calcPieceSize(p) < min_piece_size)
end_min = p.end;
else
end_max = p.end;
}
p.end = end_min;
p.end = calcPieceSize(p) < min_piece_size ? end_max : end_min;
p.size = calcPieceSize(p);
return p;
}
bool Baumkuchen::cutOK(ll min_piece_size) {
piece[0] = cutDivideBaumkuchen(0, N - 1, min_piece_size);
if (piece[0].size == -1)
return false;
int cp = piece[0].end;
for (; isContainPiece(cp, piece[0]); piece[0].top++) {
piece[0] = cutDivideBaumkuchen(piece[0].top, N - 1, min_piece_size);
for (int pieceNum = 1; pieceNum < DevideNum; pieceNum++) {
piece[pieceNum] =
cutDivideBaumkuchen(piece[pieceNum - 1].end + 1,
(piece[0].top) % N + N - 1, min_piece_size);
if (piece[pieceNum].size == -1)
goto MISS;
}
// cout << min_piece_size << " : " << piece[0].top << ", " <<
//piece[0].end << ", " << piece[0].size << " | " << piece[1].top << ", " <<
//piece[1].end << ", " << piece[1].size << " | " << piece[2].top << ", " <<
//piece[2].end << ", " << piece[2].size << endl;
return true;
MISS:
// piece[2].size = calcPieceSize(piece[2]);
// cout << min_piece_size << " : " << piece[0].top << ", " <<
//piece[0].end << ", " << piece[0].size << " | " << piece[1].top << ", " <<
//piece[1].end << ", " << piece[1].size << " | " << piece[2].top << ", " <<
//piece[2].end << ", " << piece[2].size << endl;
continue;
}
return false;
}
ll Baumkuchen::solve() {
input();
setSum();
ll ans;
ll ans_max = sum[N - 1] / DevideNum;
ll ans_min = 0;
while (ans_max > ans_min + 1) {
ans = (ans_max + ans_min) / 2;
if (cutOK(ans))
ans_min = ans;
else
ans_max = ans;
}
ans = (cutOK(ans_max)) ? ans_max : ans_min;
return ans;
}
int main() {
Baumkuchen *baumkuchen = new Baumkuchen(3);
ll ans = baumkuchen->solve();
cout << ans << endl;
delete baumkuchen;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
struct Piece {
int top;
int end;
ll size;
};
class Baumkuchen {
private:
int N;
const int DevideNum;
ll *A;
ll *sum;
Piece *piece;
void input();
void newArrays(int n);
void setSum();
bool isContainPiece(int i, const Piece &p);
ll calcPieceSize(const Piece &p);
Piece cutDivideBaumkuchen(int _top, int _end, ll min_piece_size);
bool cutOK(ll min_piece_size);
public:
Baumkuchen(int _DevideNum);
~Baumkuchen();
ll solve();
};
Baumkuchen::Baumkuchen(int _DevideNum) : N(0), DevideNum(_DevideNum) {
A = NULL;
sum = NULL;
piece = NULL;
}
Baumkuchen::~Baumkuchen() {
delete[] A;
delete[] sum;
delete[] piece;
}
void Baumkuchen::newArrays(int n) {
A = new ll[n];
sum = new ll[2 * n + 1];
piece = new Piece[DevideNum];
}
void Baumkuchen::input() {
cin >> N;
newArrays(N);
if (N < DevideNum) {
cout << "0" << endl;
exit(1);
}
for (int i = 0; i < N; i++)
cin >> A[i];
}
void Baumkuchen::setSum() {
sum[0] = A[0];
for (int i = 1; i < N; i++)
sum[i] = sum[i - 1] + A[i];
for (int i = 0; i < N; i++)
sum[i + N] = sum[i] + sum[N - 1];
}
bool Baumkuchen::isContainPiece(int i, const Piece &p) {
return (p.top <= i % N && i % N <= p.end) ||
(p.top <= (i % N) + N && (i % N) + N <= p.end);
}
ll Baumkuchen::calcPieceSize(const Piece &p) {
return (p.top <= p.end) ? (sum[p.end] - sum[p.top] + A[p.top % N]) : -1;
}
Piece Baumkuchen::cutDivideBaumkuchen(int _top, int _end, ll min_piece_size) {
Piece p;
p.top = _top;
p.end = _end;
if (calcPieceSize(p) < min_piece_size) {
p.size = -1;
return p;
}
int end_max = _end, end_min = _top;
while (end_max > end_min + 1) {
p.end = (end_max + end_min) / 2;
if (calcPieceSize(p) < min_piece_size)
end_min = p.end;
else
end_max = p.end;
}
p.end = end_min;
p.end = calcPieceSize(p) < min_piece_size ? end_max : end_min;
p.size = calcPieceSize(p);
return p;
}
bool Baumkuchen::cutOK(ll min_piece_size) {
piece[0] = cutDivideBaumkuchen(0, N - 1, min_piece_size);
if (piece[0].size == -1)
return false;
int cp = piece[0].end;
for (; isContainPiece(cp, piece[0]); piece[0].top++) {
piece[0] = cutDivideBaumkuchen(piece[0].top, N - 1, min_piece_size);
for (int pieceNum = 1; pieceNum < DevideNum; pieceNum++) {
piece[pieceNum] =
cutDivideBaumkuchen(piece[pieceNum - 1].end + 1,
(piece[0].top) % N + N - 1, min_piece_size);
if (piece[pieceNum].size == -1)
goto MISS;
}
// cout << min_piece_size << " : " << piece[0].top << ", " <<
//piece[0].end << ", " << piece[0].size << " | " << piece[1].top << ", " <<
//piece[1].end << ", " << piece[1].size << " | " << piece[2].top << ", " <<
//piece[2].end << ", " << piece[2].size << endl;
return true;
MISS:
// piece[2].size = calcPieceSize(piece[2]);
// cout << min_piece_size << " : " << piece[0].top << ", " <<
//piece[0].end << ", " << piece[0].size << " | " << piece[1].top << ", " <<
//piece[1].end << ", " << piece[1].size << " | " << piece[2].top << ", " <<
//piece[2].end << ", " << piece[2].size << endl;
continue;
}
return false;
}
ll Baumkuchen::solve() {
input();
setSum();
ll ans;
ll ans_max = sum[N - 1] / DevideNum;
ll ans_min = 0;
while (ans_max > ans_min + 1) {
ans = (ans_max + ans_min) / 2;
if (cutOK(ans))
ans_min = ans;
else
ans_max = ans;
}
ans = (cutOK(ans_max)) ? ans_max : ans_min;
return ans;
}
int main() {
Baumkuchen *baumkuchen = new Baumkuchen(3);
ll ans = baumkuchen->solve();
cout << ans << endl;
delete baumkuchen;
return 0;
} | replace | 58 | 59 | 58 | 59 | 1 | |
p00523 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)
const int maxn = 100000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];
void ReadData() {
scanf("%d", &n);
For(i, 1, n) scanf("%d", &a[i]);
For(i, 1, n) a[i + n] = a[i];
For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}
long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }
bool check(long long limit) {
int ptr1 = 1, ptr2 = 2;
For(i, 1, n - 1) {
int last = i + n - 1;
while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
ptr1++;
ptr2 = max(ptr2, ptr1 + 1);
while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
ptr2++;
if ((ptr2 <= last) &&
(GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
GetSum(ptr2 + 1, last) >= limit))
return true;
}
return false;
}
void Process() {
long long res = 0;
long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid)) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("./baumkuchen.inp" , "r" , stdin);
ReadData();
Process();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define Ford(i, a, b) for (int i = (a); i >= (b); i--)
const int maxn = 200000 + 1912;
int n, a[maxn];
long long totalSum = 0;
long long sum[maxn];
void ReadData() {
scanf("%d", &n);
For(i, 1, n) scanf("%d", &a[i]);
For(i, 1, n) a[i + n] = a[i];
For(i, 1, n << 1) sum[i] = sum[i - 1] + a[i];
}
long long GetSum(const int l, const int r) { return sum[r] - sum[l - 1]; }
bool check(long long limit) {
int ptr1 = 1, ptr2 = 2;
For(i, 1, n - 1) {
int last = i + n - 1;
while (ptr1 < last - 1 && GetSum(i, ptr1) < limit)
ptr1++;
ptr2 = max(ptr2, ptr1 + 1);
while (ptr2 < last && GetSum(ptr1 + 1, ptr2) < limit)
ptr2++;
if ((ptr2 <= last) &&
(GetSum(i, ptr1) >= limit && GetSum(ptr1 + 1, ptr2) >= limit &&
GetSum(ptr2 + 1, last) >= limit))
return true;
}
return false;
}
void Process() {
long long res = 0;
long long l = 1, r = 1000LL * 1000 * 1000 * 1000 * 1000 + 1912;
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid)) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("./baumkuchen.inp" , "r" , stdin);
ReadData();
Process();
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00523 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <limits>
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807
#endif
using namespace std;
typedef long long llong;
int N;
llong *v_list;
double tripartition;
llong BaumukuchenGetAnswer();
llong searchOne(int n0);
llong getMinV(int n0, int n1, int n2);
int findIndex(int from_n, int to_n, double target);
int main() {
/*
ifstream fin("input.txt");
fin >> N;
v_list = new llong[N+1];
v_list[0] = 0;
for(int n=1; n<N+1; n++){
llong A;
fin >> A;
v_list[n] = v_list[n-1] + A;
}
fin.close();
/*/
cin >> N;
v_list = new llong[N + 1];
v_list[0] = 0;
for (int n = 1; n < N + 1; n++) {
llong A;
cin >> A;
v_list[n] = v_list[n - 1] + A;
}
//*/
cout << BaumukuchenGetAnswer() << endl;
delete[] v_list;
}
llong BaumukuchenGetAnswer() {
tripartition = v_list[N] / 3.0;
llong interm_maxV = -1;
int n0_max = findIndex(0, N, tripartition);
for (int n0 = 0; n0 <= n0_max; n0++) {
llong tmp = searchOne(n0);
if (tmp > interm_maxV)
interm_maxV = tmp;
}
return interm_maxV;
}
llong searchOne(int n0) {
int n1_left = findIndex(n0, N, tripartition + v_list[n0]);
int n2_left = findIndex(n1_left, N, 2 * tripartition + v_list[n0]);
llong V_ll = getMinV(n0, n1_left, n2_left);
llong V_lr = getMinV(n0, n1_left, n2_left + 1);
llong V_rl = getMinV(n0, n1_left + 1, n2_left);
llong V_rr = getMinV(n0, n1_left + 1, n2_left + 1);
return max(max(V_ll, V_lr), max(V_rl, V_rr));
}
llong getMinV(int n0, int n1, int n2) {
if (n1 == -1 || n2 == -1)
return LLONG_MAX;
else {
llong V0 = v_list[n1] - v_list[n0];
llong V1 = v_list[n2] - v_list[n1];
llong V2 = v_list[n0] - v_list[n2] + v_list[N];
return min(min(V0, V1), V2);
}
}
int findIndex(int from_n, int to_n, double target) {
int n_left = from_n;
int n_right = to_n;
while (n_right - n_left != 2) {
int n_mid = (n_right + n_left) / 2;
if (v_list[n_mid] <= target)
n_left = n_mid;
else
n_right = n_mid;
}
return n_left;
} | #include <algorithm>
#include <iostream>
#include <limits>
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807
#endif
using namespace std;
typedef long long llong;
int N;
llong *v_list;
double tripartition;
llong BaumukuchenGetAnswer();
llong searchOne(int n0);
llong getMinV(int n0, int n1, int n2);
int findIndex(int from_n, int to_n, double target);
int main() {
/*
ifstream fin("input.txt");
fin >> N;
v_list = new llong[N+1];
v_list[0] = 0;
for(int n=1; n<N+1; n++){
llong A;
fin >> A;
v_list[n] = v_list[n-1] + A;
}
fin.close();
/*/
cin >> N;
v_list = new llong[N + 1];
v_list[0] = 0;
for (int n = 1; n < N + 1; n++) {
llong A;
cin >> A;
v_list[n] = v_list[n - 1] + A;
}
//*/
cout << BaumukuchenGetAnswer() << endl;
delete[] v_list;
}
llong BaumukuchenGetAnswer() {
tripartition = v_list[N] / 3.0;
llong interm_maxV = -1;
int n0_max = findIndex(0, N, tripartition);
for (int n0 = 0; n0 <= n0_max; n0++) {
llong tmp = searchOne(n0);
if (tmp > interm_maxV)
interm_maxV = tmp;
}
return interm_maxV;
}
llong searchOne(int n0) {
int n1_left = findIndex(n0, N, tripartition + v_list[n0]);
int n2_left = findIndex(n1_left, N, 2 * tripartition + v_list[n0]);
llong V_ll = getMinV(n0, n1_left, n2_left);
llong V_lr = getMinV(n0, n1_left, n2_left + 1);
llong V_rl = getMinV(n0, n1_left + 1, n2_left);
llong V_rr = getMinV(n0, n1_left + 1, n2_left + 1);
return max(max(V_ll, V_lr), max(V_rl, V_rr));
}
llong getMinV(int n0, int n1, int n2) {
if (n1 == -1 || n2 == -1)
return LLONG_MAX;
else {
llong V0 = v_list[n1] - v_list[n0];
llong V1 = v_list[n2] - v_list[n1];
llong V2 = v_list[n0] - v_list[n2] + v_list[N];
return min(min(V0, V1), V2);
}
}
int findIndex(int from_n, int to_n, double target) {
int n_left = from_n;
int n_right = to_n;
while (n_right - n_left != 1) {
int n_mid = (n_right + n_left) / 2;
if (v_list[n_mid] <= target)
n_left = n_mid;
else
n_right = n_mid;
}
return n_left;
} | replace | 82 | 83 | 82 | 83 | TLE | |
p00523 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
int a[111111 * 2];
long long sum[111111 * 2];
long long res = 0;
int n;
long long C(int s, int e) {
long long lim = sum[e] - sum[s];
// printf("[%d,%d) [%d,%d)\n",s,e,e,s+n);
s += n;
int l = e, r = s;
while (e < s) {
int m = (s + e) / 2;
assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
// printf("%d %d %d %lld %lld %lld
// %lld\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
if (sum[m] - sum[l] >= sum[r] - sum[m]) {
s = m;
} else {
e = m + 1;
}
}
// s = e;
assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
// printf(":%d %d %lld %lld %lld %lld %d
// %d\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
res = max(res, ret);
return ret;
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
a[i + n] = a[i];
a[i + 2 * n] = a[i];
}
for (int j = 0; j < 3 * n; j++) {
sum[j + 1] = sum[j] + a[j];
}
int s, e;
s = 0;
e = 1;
for (; e < 2 * n; ++e) {
while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
++e;
}
while (s < e && C(s, e) < sum[e] - sum[s]) {
++s;
}
}
printf("%lld\n", res);
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
int a[111111 * 3];
long long sum[111111 * 3];
long long res = 0;
int n;
long long C(int s, int e) {
long long lim = sum[e] - sum[s];
// printf("[%d,%d) [%d,%d)\n",s,e,e,s+n);
s += n;
int l = e, r = s;
while (e < s) {
int m = (s + e) / 2;
assert(sum[m] - sum[l] + lim + sum[r] - sum[m] == sum[n]);
// printf("%d %d %d %lld %lld %lld
// %lld\n",e,m,s,sum[m]-sum[l],lim,sum[r]-sum[m],sum[m]-sum[l]+lim+sum[r]-sum[m]);
if (sum[m] - sum[l] >= sum[r] - sum[m]) {
s = m;
} else {
e = m + 1;
}
}
// s = e;
assert(lim + sum[r] - sum[s] + sum[s] - sum[l] == sum[n]);
// printf(":%d %d %lld %lld %lld %lld %d
// %d\n",l,r,lim,sum[r]-sum[s],sum[s]-sum[l],lim+sum[r]-sum[s]+sum[s]-sum[l],sum[n],l,r);
long long ret = min(min(lim, sum[r] - sum[s]), sum[s] - sum[l]);
res = max(res, ret);
return ret;
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
a[i + n] = a[i];
a[i + 2 * n] = a[i];
}
for (int j = 0; j < 3 * n; j++) {
sum[j + 1] = sum[j] + a[j];
}
int s, e;
s = 0;
e = 1;
for (; e < 2 * n; ++e) {
while (e < 2 * n && C(s, e) > sum[e] - sum[s]) {
++e;
}
while (s < e && C(s, e) < sum[e] - sum[s]) {
++s;
}
}
printf("%lld\n", res);
return 0;
} | replace | 6 | 8 | 6 | 8 | 0 | |
p00523 | C++ | Runtime Error | // 初めての尺取り
#include <iostream>
#include <stdio.h>
using namespace std;
#define GOMAE (long long)50000
#define EPS (long long)4000
#define TO_OUTSIDE (long long)1500
#define IS_NOT_EVEN_HOBBY (long long)114
int N;
long long input;
long long A[200010];
long long ans;
// A[i]-A[st] >= A[ed]-A[i]となる最小のiを返す。
int seach1(int st, int ed, int i) {
for (; i < ed; i++) {
if (A[i] - A[st] >= A[ed] - A[i])
break;
}
return i;
}
// A[i]-A[st] >= Xとなる最小のiを返す。
int seach2(int st, int ed, long long X, int i) {
for (; i < ed; i++) {
if (A[i] - A[st] >= X)
break;
}
return i;
}
int solve(long long X) {
int i, j = 0, k = 0;
if (X >= (A[N] >> 1))
return 0;
for (i = 0; i < N; i++) {
// 大きさがX以上になるように切る
for (; j < N + i; j++) {
if (A[j] - A[i] >= X)
break;
}
// できるだけ真ん中で切る
for (; k < N + i; k++) {
if (A[k] - A[j] >= A[N + i] - A[k])
break;
}
if (min(A[j] - A[i], min(A[k] - A[j], A[i + N] - A[k])) >= X)
break;
k--;
if (min(A[j] - A[i], min(A[k] - A[j], A[i + N] - A[k])) >= X)
break;
}
if (i < N)
return 1;
return 0;
}
int main() {
int i, j, k;
long long s = 0, e = GOMAE * EPS * TO_OUTSIDE * IS_NOT_EVEN_HOBBY * EPS;
scanf("%d", &N);
if (N > 100000)
return 0;
for (i = 1; i < N + 1; i++) {
scanf("%d", &input);
A[i] = A[i - 1] + input;
}
for (i = N + 1; i < 3 * N + 1; i++) {
A[i] = A[i - N] + A[N];
}
// printf("%lld %lld\n",s,e);
while (s <= e) {
ans = (s + e) >> 1;
if (solve(ans)) {
if (solve(ans + 1))
s = ans + 1;
else
break;
} else {
e = ans - 1;
}
}
printf("%lld\n", ans);
return 0;
} | // 初めての尺取り
#include <iostream>
#include <stdio.h>
using namespace std;
#define GOMAE (long long)50000
#define EPS (long long)4000
#define TO_OUTSIDE (long long)1500
#define IS_NOT_EVEN_HOBBY (long long)114
int N;
long long input;
long long A[300010];
long long ans;
// A[i]-A[st] >= A[ed]-A[i]となる最小のiを返す。
int seach1(int st, int ed, int i) {
for (; i < ed; i++) {
if (A[i] - A[st] >= A[ed] - A[i])
break;
}
return i;
}
// A[i]-A[st] >= Xとなる最小のiを返す。
int seach2(int st, int ed, long long X, int i) {
for (; i < ed; i++) {
if (A[i] - A[st] >= X)
break;
}
return i;
}
int solve(long long X) {
int i, j = 0, k = 0;
if (X >= (A[N] >> 1))
return 0;
for (i = 0; i < N; i++) {
// 大きさがX以上になるように切る
for (; j < N + i; j++) {
if (A[j] - A[i] >= X)
break;
}
// できるだけ真ん中で切る
for (; k < N + i; k++) {
if (A[k] - A[j] >= A[N + i] - A[k])
break;
}
if (min(A[j] - A[i], min(A[k] - A[j], A[i + N] - A[k])) >= X)
break;
k--;
if (min(A[j] - A[i], min(A[k] - A[j], A[i + N] - A[k])) >= X)
break;
}
if (i < N)
return 1;
return 0;
}
int main() {
int i, j, k;
long long s = 0, e = GOMAE * EPS * TO_OUTSIDE * IS_NOT_EVEN_HOBBY * EPS;
scanf("%d", &N);
if (N > 100000)
return 0;
for (i = 1; i < N + 1; i++) {
scanf("%d", &input);
A[i] = A[i - 1] + input;
}
for (i = N + 1; i < 3 * N + 1; i++) {
A[i] = A[i - N] + A[N];
}
// printf("%lld %lld\n",s,e);
while (s <= e) {
ans = (s + e) >> 1;
if (solve(ans)) {
if (solve(ans + 1))
s = ans + 1;
else
break;
} else {
e = ans - 1;
}
}
printf("%lld\n", ans);
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p00523 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
typedef pair<LL, LL> P;
typedef pair<LL, P> PP;
LL a[2020200];
LL b[2020200];
LL n;
bool calc(LL m) {
for (int i = 0; i < n; i++) {
LL tmp = (i == 0 ? 0 : b[i - 1]);
int pos1 = lower_bound(b + i, b + i + n, tmp + m) - b;
int pos2 = lower_bound(b + i, b + i + n, b[pos1] + m) - b;
if (b[n + i - 1] - b[pos2] >= m)
return true;
}
return false;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n; i < n * 2; i++) {
a[i] = a[i - n];
}
b[0] = a[0];
for (int i = 1; i < n * 2; i++) {
b[i] = a[i] + b[i - 1];
}
LL left = 1, right = b[n - 1];
while (right - left > 1) {
LL mid = (right + left) / 2;
if (calc(mid)) {
left = mid;
} else {
right = mid;
}
}
cout << left << endl;
system("pause");
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
typedef pair<LL, LL> P;
typedef pair<LL, P> PP;
LL a[2020200];
LL b[2020200];
LL n;
bool calc(LL m) {
for (int i = 0; i < n; i++) {
LL tmp = (i == 0 ? 0 : b[i - 1]);
int pos1 = lower_bound(b + i, b + i + n, tmp + m) - b;
int pos2 = lower_bound(b + i, b + i + n, b[pos1] + m) - b;
if (b[n + i - 1] - b[pos2] >= m)
return true;
}
return false;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n; i < n * 2; i++) {
a[i] = a[i - n];
}
b[0] = a[0];
for (int i = 1; i < n * 2; i++) {
b[i] = a[i] + b[i - 1];
}
LL left = 1, right = b[n - 1];
while (right - left > 1) {
LL mid = (right + left) / 2;
if (calc(mid)) {
left = mid;
} else {
right = mid;
}
}
cout << left << endl;
// system("pause");
} | replace | 73 | 74 | 73 | 74 | 0 | sh: 1: pause: not found
|
p00523 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>
const int INF = 1001001001;
#define rep(i, n) for (int i = 0; i < n; i++)
int N;
int A[200004];
long long int B[100002];
int main() {
// FILE *fp;
// fp=fopen("F:\\1JOI本選\\2014\\joi2014-ho-data\\joi2014-ho-data\\2014-ho-t3\\in\\03-06.txt","r");
scanf("%d", &N);
long long int MAX = 0;
rep(i, N) {
scanf("%d", &A[i]);
MAX += A[i];
B[i] = MAX;
}
memcpy(&A[N], A, sizeof(int) * N);
long long int M = MAX / 3; // 全てのピースがM以上になる切り方は存在するか?
long long int m = 0;
do {
bool cann = false; // 全てのピースがn以上になる切り方は存在するか?
long long int n = (M + m) / 2;
rep(i, N - 2) { // どこで最初に切るか
int a = i; // よりあとで切る
int b = N - 2; // 以前で切る
do { // 次に切る場所を探す
int c = (a + b) / 2;
long long int piece1 = B[c] - B[i];
if (piece1 >= n) {
b = c;
} // c以前で切れる
else
a = c; // cよりあとで切るべき
} while (b > a + 1);
long long int p1 = B[b] - B[i];
if (p1 < n)
continue; // 1個目がもうだめ
int x = b;
int y = N - 1;
do { // 最後に切る場所を探す
int z = (x + y) / 2;
long long int piece2 = B[z] - B[b];
if (piece2 >= n) {
y = z;
} else
x = z;
} while (y > x + 1);
long long int p2 = B[y] - B[b];
if (p2 < n)
continue; // 2個目がもうだめ
if (MAX - p1 - p2 >= n) {
cann = true;
}
}
if (cann) {
m = n;
} // すべて(3つ)のピースがn以上になりえる
else {
M = n;
}
} while (M > m + 1);
printf("%lld\n", m);
return 1;
} | #include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>
const int INF = 1001001001;
#define rep(i, n) for (int i = 0; i < n; i++)
int N;
int A[200004];
long long int B[100002];
int main() {
// FILE *fp;
// fp=fopen("F:\\1JOI本選\\2014\\joi2014-ho-data\\joi2014-ho-data\\2014-ho-t3\\in\\03-06.txt","r");
scanf("%d", &N);
long long int MAX = 0;
rep(i, N) {
scanf("%d", &A[i]);
MAX += A[i];
B[i] = MAX;
}
memcpy(&A[N], A, sizeof(int) * N);
long long int M = MAX / 3; // 全てのピースがM以上になる切り方は存在するか?
long long int m = 0;
do {
bool cann = false; // 全てのピースがn以上になる切り方は存在するか?
long long int n = (M + m) / 2;
rep(i, N - 2) { // どこで最初に切るか
int a = i; // よりあとで切る
int b = N - 2; // 以前で切る
do { // 次に切る場所を探す
int c = (a + b) / 2;
long long int piece1 = B[c] - B[i];
if (piece1 >= n) {
b = c;
} // c以前で切れる
else
a = c; // cよりあとで切るべき
} while (b > a + 1);
long long int p1 = B[b] - B[i];
if (p1 < n)
continue; // 1個目がもうだめ
int x = b;
int y = N - 1;
do { // 最後に切る場所を探す
int z = (x + y) / 2;
long long int piece2 = B[z] - B[b];
if (piece2 >= n) {
y = z;
} else
x = z;
} while (y > x + 1);
long long int p2 = B[y] - B[b];
if (p2 < n)
continue; // 2個目がもうだめ
if (MAX - p1 - p2 >= n) {
cann = true;
}
}
if (cann) {
m = n;
} // すべて(3つ)のピースがn以上になりえる
else {
M = n;
}
} while (M > m + 1);
printf("%lld\n", m);
return 0;
} | replace | 68 | 69 | 68 | 69 | 1 | |
p00524 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, pii> P;
const ll INF = 100000000000007;
int n, m, x;
int h[100000];
ll d[100000];
vector<pii> G[100000];
priority_queue<P, vector<P>, greater<P>> q;
int main() {
scanf("%d %d %d", &n, &m, &x);
for (int i = 0; i < n; i++)
scanf("%d", h + i);
for (int i = 0; i < m; i++) {
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
a--;
b--;
G[a].push_back(pii(b, t));
G[b].push_back(pii(a, t));
}
fill(d, d + n, INF);
d[0] = 0;
q.push(P(0, pii(0, x)));
ll ans = INF;
while (!q.empty()) {
int tree = q.top().second.first, pos = q.top().second.second;
ll c = q.top().first;
if (tree == n - 1) {
ans = min(ans, c + h[n - 1] - pos);
}
q.pop();
if (d[tree] < c)
continue;
for (int i = 0; i < G[tree].size(); i++) {
int next_tree = G[tree][i].first, next_pos = pos - G[tree][i].second;
ll cost = d[tree] + G[tree][i].second;
if (h[tree] - G[tree][i].second < 0)
cost = INF;
else if (next_pos < 0) {
cost -= next_pos;
next_pos = 0;
} else if (next_pos > h[next_tree]) {
cost += next_pos - h[next_tree];
next_pos = h[next_tree];
}
if (d[next_tree] >= cost) {
d[next_tree] = min(d[next_tree], cost);
q.push(P(d[next_tree], pii(next_tree, next_pos)));
}
}
}
if (ans == INF)
printf("-1\n");
else
printf("%lld\n", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, pii> P;
const ll INF = 100000000000007;
int n, m, x;
int h[100000];
ll d[100000];
vector<pii> G[100000];
priority_queue<P, vector<P>, greater<P>> q;
int main() {
scanf("%d %d %d", &n, &m, &x);
for (int i = 0; i < n; i++)
scanf("%d", h + i);
for (int i = 0; i < m; i++) {
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
a--;
b--;
G[a].push_back(pii(b, t));
G[b].push_back(pii(a, t));
}
fill(d, d + n, INF);
d[0] = 0;
q.push(P(0, pii(0, x)));
ll ans = INF;
while (!q.empty()) {
int tree = q.top().second.first, pos = q.top().second.second;
ll c = q.top().first;
if (tree == n - 1) {
ans = min(ans, c + h[n - 1] - pos);
}
q.pop();
if (d[tree] < c)
continue;
for (int i = 0; i < G[tree].size(); i++) {
int next_tree = G[tree][i].first, next_pos = pos - G[tree][i].second;
ll cost = d[tree] + G[tree][i].second;
if (h[tree] - G[tree][i].second < 0)
cost = INF;
else if (next_pos < 0) {
cost -= next_pos;
next_pos = 0;
} else if (next_pos > h[next_tree]) {
cost += next_pos - h[next_tree];
next_pos = h[next_tree];
}
if (d[next_tree] > cost) {
d[next_tree] = min(d[next_tree], cost);
q.push(P(d[next_tree], pii(next_tree, next_pos)));
}
}
}
if (ans == INF)
printf("-1\n");
else
printf("%lld\n", ans);
return 0;
} | replace | 55 | 56 | 55 | 56 | MLE | |
p00524 | C++ | Runtime Error | #include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, long long int> pi;
typedef pair<long long int, pi> plpi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const long long int INF = 1LL << 60;
int main() {
int n, m, l, r;
long long int ans = INF, dis[N], d, h[N], x;
bitset<N> went;
queue<plpi> q;
priority_queue<plpi, vector<plpi>, greater<plpi>> pq;
plpi nxt;
vector<pi> graph[N];
scanf("%d%d%lld", &n, &m, &x);
for (int i = 1; i <= n; i++) {
scanf("%lld", &h[i]);
dis[i] = INF;
}
while (m--) {
scanf("%d%d%lld", &l, &r, &d);
if (h[l] - d >= 0)
graph[l].PB({r, d});
if (h[r] - d >= 0)
graph[r].PB({l, d});
}
went.reset();
pq.push({0LL, {1, x}});
while (!pq.empty()) {
nxt = pq.top();
pq.pop();
if (nxt.F >= ans)
continue;
if (nxt.S.F == n) {
ans = min(ans, nxt.F + h[n] - nxt.S.S);
break;
} else if (nxt.S.S == 0) {
if (!went[nxt.S.F] && dis[nxt.S.F] >= nxt.F) {
went[nxt.S.F] = true;
for (pi i : graph[nxt.S.F])
if (nxt.F + i.S * 2 < dis[i.F]) {
dis[i.F] = nxt.F + i.S * 2;
pq.push({nxt.F + i.S * 2, {i.F, 0}});
}
}
} else if (!went[nxt.S.F]) {
for (pi i : graph[nxt.S.F]) {
if (nxt.S.S - i.S < 0) {
if (dis[i.F] > nxt.F - nxt.S.S + i.S * 2) {
dis[i.F] = nxt.F - nxt.S.S + i.S * 2;
pq.push({nxt.F - nxt.S.S + i.S * 2, {i.F, 0}});
}
} else if (nxt.S.S - i.S > h[i.F]) {
if (dis[i.F] > nxt.F + nxt.S.S - h[i.F] * 2) {
dis[i.F] = min(dis[i.F], nxt.F + nxt.S.S);
pq.push({nxt.F + nxt.S.S - h[i.F], {i.F, h[i.F]}});
}
} else {
if (dis[i.F] > nxt.F + i.S * 2 - nxt.S.S) {
dis[i.F] = min(dis[i.F], nxt.S.S + nxt.F);
pq.push({nxt.F + i.S, {i.F, nxt.S.S - i.S}});
}
}
}
}
}
if (ans == INF)
printf("-1\n");
else
printf("%lld\n", ans);
}
| #include <bitset>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, long long int> pi;
typedef pair<long long int, pi> plpi;
#define F first
#define S second
#define PB push_back
const int N = 1e5 + 10;
const long long int INF = 1LL << 60;
int main() {
int n, m, l, r;
long long int ans = INF, dis[N], d, h[N], x;
bitset<N> went;
queue<plpi> q;
priority_queue<plpi, vector<plpi>, greater<plpi>> pq;
plpi nxt;
vector<pi> graph[N];
scanf("%d%d%lld", &n, &m, &x);
for (int i = 1; i <= n; i++) {
scanf("%lld", &h[i]);
dis[i] = INF;
}
while (m--) {
scanf("%d%d%lld", &l, &r, &d);
if (h[l] - d >= 0)
graph[l].PB({r, d});
if (h[r] - d >= 0)
graph[r].PB({l, d});
}
went.reset();
pq.push({0LL, {1, x}});
while (!pq.empty()) {
nxt = pq.top();
pq.pop();
if (nxt.F >= ans)
continue;
if (nxt.S.F == n) {
ans = min(ans, nxt.F + h[n] - nxt.S.S);
break;
} else if (nxt.S.S == 0) {
if (!went[nxt.S.F] && dis[nxt.S.F] >= nxt.F) {
went[nxt.S.F] = true;
for (pi i : graph[nxt.S.F])
if (nxt.F + i.S * 2 < dis[i.F]) {
dis[i.F] = nxt.F + i.S * 2;
pq.push({nxt.F + i.S * 2, {i.F, 0}});
}
}
} else if (!went[nxt.S.F]) {
went[nxt.S.F] = true;
for (pi i : graph[nxt.S.F]) {
if (nxt.S.S - i.S < 0) {
if (dis[i.F] > nxt.F - nxt.S.S + i.S * 2) {
dis[i.F] = nxt.F - nxt.S.S + i.S * 2;
pq.push({nxt.F - nxt.S.S + i.S * 2, {i.F, 0}});
}
} else if (nxt.S.S - i.S > h[i.F]) {
if (dis[i.F] > nxt.F + nxt.S.S - h[i.F] * 2) {
dis[i.F] = min(dis[i.F], nxt.F + nxt.S.S);
pq.push({nxt.F + nxt.S.S - h[i.F], {i.F, h[i.F]}});
}
} else {
if (dis[i.F] > nxt.F + i.S * 2 - nxt.S.S) {
dis[i.F] = min(dis[i.F], nxt.S.S + nxt.F);
pq.push({nxt.F + i.S, {i.F, nxt.S.S - i.S}});
}
}
}
}
}
if (ans == INF)
printf("-1\n");
else
printf("%lld\n", ans);
}
| insert | 53 | 53 | 53 | 54 | 0 | |
p00524 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
int to, cost;
edge(int a, int b) : to(a), cost(b) {}
};
int N, M, X;
int H[100000];
vector<edge> G[100000];
int dist[100000];
signed main() {
cin >> N >> M >> X;
for (int i = 0; i < N; i++)
cin >> H[i];
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(edge(b, c));
G[b].push_back(edge(a, c));
}
fill_n(dist, N, INF);
priority_queue<P> que;
que.push(P(0, 0));
dist[0] = 0;
while (que.size()) {
P p = que.top();
que.pop();
if (dist[p.second] < p.first)
continue;
int h = (p.first >= X ? 0 : X - p.first);
for (int i = 0; i < G[p.second].size(); i++) {
edge &e = G[p.second][i];
if (H[p.second] < e.cost)
continue;
int cost;
if (h - e.cost > H[e.to])
cost = h - H[e.to];
else if (h - e.cost >= 0)
cost = e.cost;
else
cost = e.cost - (h - e.cost);
if (dist[e.to] <= p.first + cost)
continue;
dist[e.to] = p.first + cost;
que.push(P(dist[e.to], e.to));
}
}
if (dist[N - 1] == INF)
cout << -1 << endl;
else
cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
<< endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
int to, cost;
edge(int a, int b) : to(a), cost(b) {}
};
int N, M, X;
int H[100000];
vector<edge> G[100000];
int dist[100000];
signed main() {
cin >> N >> M >> X;
for (int i = 0; i < N; i++)
cin >> H[i];
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(edge(b, c));
G[b].push_back(edge(a, c));
}
fill_n(dist, N, INF);
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, 0));
dist[0] = 0;
while (que.size()) {
P p = que.top();
que.pop();
if (dist[p.second] < p.first)
continue;
int h = (p.first >= X ? 0 : X - p.first);
for (int i = 0; i < G[p.second].size(); i++) {
edge &e = G[p.second][i];
if (H[p.second] < e.cost)
continue;
int cost;
if (h - e.cost > H[e.to])
cost = h - H[e.to];
else if (h - e.cost >= 0)
cost = e.cost;
else
cost = e.cost - (h - e.cost);
if (dist[e.to] <= p.first + cost)
continue;
dist[e.to] = p.first + cost;
que.push(P(dist[e.to], e.to));
}
}
if (dist[N - 1] == INF)
cout << -1 << endl;
else
cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
<< endl;
return 0;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p00524 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define N 100000
#define M 300000
#define HT 1000000000
#define INF 2e18
#define int long long
#define MP(i, j) make_pair((i), (j))
typedef pair<int, int> P;
int n, m, x, ans = INF, tree[N + 1];
vector<P> g[N + 1];
int flag[N + 1] = {};
struct S {
int to, ti, nh;
S(int i_to, int i_ti, int i_nh) : to(i_to), ti(i_ti), nh(i_nh) {}
bool operator<(const S &s) const {
if (ti != s.ti)
return ti > s.ti;
}
};
priority_queue<S> pq;
void dijkstra(int sta) {
for (int i = 0; i < g[1].size(); i++) {
if (tree[1] >= g[1][i].second) {
if (sta >= g[1][i].second) {
if (sta > g[1][i].second + tree[g[1][i].first]) {
pq.push(
{g[1][i].first, sta - tree[g[1][i].first], tree[g[1][i].first]});
} else {
pq.push({g[1][i].first, g[1][i].second, sta - g[1][i].second});
}
} else {
if (abs(sta - g[1][i].second) <= tree[1])
pq.push(
{g[1][i].first, abs(sta - g[1][i].second) + g[1][i].second, 0});
}
}
}
while (!pq.empty()) {
S s = pq.top();
pq.pop();
if (flag[s.to] && flag[s.to] < s.ti)
continue;
flag[s.to] = s.ti;
if (s.to == n) {
ans = min(ans, tree[n] - s.nh + flag[s.to]);
break;
}
for (int i = 0; i < g[s.to].size(); i++) {
if (tree[s.to] >= g[s.to][i].second) {
if (s.nh >= g[s.to][i].second) {
if (s.nh > g[s.to][i].second + tree[g[s.to][i].first]) {
pq.push({g[s.to][i].first, s.nh - tree[g[s.to][i].first] + s.ti,
tree[g[s.to][i].first]});
} else {
pq.push({g[s.to][i].first, g[s.to][i].second + s.ti,
s.nh - g[s.to][i].second});
}
} else {
if (g[s.to][i].second - s.nh <= tree[s.to])
pq.push({g[s.to][i].first,
g[s.to][i].second - s.nh + g[s.to][i].second + s.ti, 0});
}
}
}
}
}
signed main() {
scanf("%lld%lld%lld", &n, &m, &x);
for (int i = 1; i <= n; i++)
scanf("%lld", tree + i);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
g[a].push_back(MP(b, c));
g[b].push_back(MP(a, c));
}
dijkstra(x);
if (ans != INF)
cout << ans << endl;
else
cout << -1 << endl;
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define N 100000
#define M 300000
#define HT 1000000000
#define INF 2e18
#define int long long
#define MP(i, j) make_pair((i), (j))
typedef pair<int, int> P;
int n, m, x, ans = INF, tree[N + 1];
vector<P> g[N + 1];
int flag[N + 1] = {};
struct S {
int to, ti, nh;
S(int i_to, int i_ti, int i_nh) : to(i_to), ti(i_ti), nh(i_nh) {}
bool operator<(const S &s) const {
if (ti != s.ti)
return ti > s.ti;
}
};
priority_queue<S> pq;
void dijkstra(int sta) {
for (int i = 0; i < g[1].size(); i++) {
if (tree[1] >= g[1][i].second) {
if (sta >= g[1][i].second) {
if (sta > g[1][i].second + tree[g[1][i].first]) {
pq.push(
{g[1][i].first, sta - tree[g[1][i].first], tree[g[1][i].first]});
} else {
pq.push({g[1][i].first, g[1][i].second, sta - g[1][i].second});
}
} else {
if (abs(sta - g[1][i].second) <= tree[1])
pq.push(
{g[1][i].first, abs(sta - g[1][i].second) + g[1][i].second, 0});
}
}
}
while (!pq.empty()) {
S s = pq.top();
pq.pop();
if (flag[s.to] && flag[s.to] <= s.ti)
continue;
flag[s.to] = s.ti;
if (s.to == n) {
ans = min(ans, tree[n] - s.nh + flag[s.to]);
break;
}
for (int i = 0; i < g[s.to].size(); i++) {
if (tree[s.to] >= g[s.to][i].second) {
if (s.nh >= g[s.to][i].second) {
if (s.nh > g[s.to][i].second + tree[g[s.to][i].first]) {
pq.push({g[s.to][i].first, s.nh - tree[g[s.to][i].first] + s.ti,
tree[g[s.to][i].first]});
} else {
pq.push({g[s.to][i].first, g[s.to][i].second + s.ti,
s.nh - g[s.to][i].second});
}
} else {
if (g[s.to][i].second - s.nh <= tree[s.to])
pq.push({g[s.to][i].first,
g[s.to][i].second - s.nh + g[s.to][i].second + s.ti, 0});
}
}
}
}
}
signed main() {
scanf("%lld%lld%lld", &n, &m, &x);
for (int i = 1; i <= n; i++)
scanf("%lld", tree + i);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
g[a].push_back(MP(b, c));
g[b].push_back(MP(a, c));
}
dijkstra(x);
if (ans != INF)
cout << ans << endl;
else
cout << -1 << endl;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p00524 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct before_main {
before_main() {
cin.tie(0);
ios::sync_with_stdio(false);
}
} before_main;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
typedef long long ll;
ll const inf = 1e17;
template <class T> using PQ_G = priority_queue<T, vector<T>, greater<T>>;
ll N, M, X;
vector<ll> v;
vector<vector<pair<int, ll>>> G;
ll solve() {
vector<ll> dist(N + 1, inf);
PQ_G<tuple<ll, ll, int>> pq;
pq.emplace(0, X, 0);
while (!pq.empty()) {
ll T, H;
int curr;
tie(T, H, curr) = pq.top();
pq.pop();
if (curr == N - 1) {
ll nT = T + v[N - 1] - H;
if (dist[N] > nT) {
dist[N] = nT;
pq.emplace(nT, v[N - 1], N);
}
continue;
}
if (curr == N)
return T;
for (auto &e : G[curr]) {
int next;
ll mvt;
tie(next, mvt) = e;
ll nT = T + mvt;
ll nH = H - mvt;
if (H - mvt < 0) {
if (H + (mvt - H) > v[curr])
continue;
nT += mvt - H;
nH = 0;
} else if (H - mvt > v[next]) {
nT += (H - mvt) - v[next];
nH = H - ((H - mvt) - v[next]) - mvt;
if (nH < 0)
continue;
}
if (dist[next] >= nT) {
dist[next] = nT;
pq.emplace(nT, nH, next);
}
}
}
return -1;
}
int main() {
cin >> N >> M >> X;
v.resize(N);
G.resize(N);
rep(i, N) cin >> v[i];
rep(i, M) {
int x, y;
ll t;
cin >> x >> y >> t;
x--, y--;
if (t <= v[x])
G[x].emplace_back(y, t);
if (t <= v[y])
G[y].emplace_back(x, t);
}
cout << solve() << endl;
return 0;
} | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct before_main {
before_main() {
cin.tie(0);
ios::sync_with_stdio(false);
}
} before_main;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
typedef long long ll;
ll const inf = 1e17;
template <class T> using PQ_G = priority_queue<T, vector<T>, greater<T>>;
ll N, M, X;
vector<ll> v;
vector<vector<pair<int, ll>>> G;
ll solve() {
vector<ll> dist(N + 1, inf);
PQ_G<tuple<ll, ll, int>> pq;
pq.emplace(0, X, 0);
while (!pq.empty()) {
ll T, H;
int curr;
tie(T, H, curr) = pq.top();
pq.pop();
if (curr == N - 1) {
ll nT = T + v[N - 1] - H;
if (dist[N] > nT) {
dist[N] = nT;
pq.emplace(nT, v[N - 1], N);
}
continue;
}
if (curr == N)
return T;
for (auto &e : G[curr]) {
int next;
ll mvt;
tie(next, mvt) = e;
ll nT = T + mvt;
ll nH = H - mvt;
if (H - mvt < 0) {
if (H + (mvt - H) > v[curr])
continue;
nT += mvt - H;
nH = 0;
} else if (H - mvt > v[next]) {
nT += (H - mvt) - v[next];
nH = H - ((H - mvt) - v[next]) - mvt;
if (nH < 0)
continue;
}
if (dist[next] > nT) {
dist[next] = nT;
pq.emplace(nT, nH, next);
}
}
}
return -1;
}
int main() {
cin >> N >> M >> X;
v.resize(N);
G.resize(N);
rep(i, N) cin >> v[i];
rep(i, M) {
int x, y;
ll t;
cin >> x >> y >> t;
x--, y--;
if (t <= v[x])
G[x].emplace_back(y, t);
if (t <= v[y])
G[y].emplace_back(x, t);
}
cout << solve() << endl;
return 0;
} | replace | 96 | 97 | 96 | 97 | 0 | |
p00524 | C++ | Time Limit Exceeded | //??????????§????(??????or???????????????????????£?????????)???????????¨,
//????¨??????????t??§??¨?????°???????????¨???, t<X??????X-t[m],
//t>=X??????0[m]?????????????????????
//??????????????????????????????????????£???, (??????,
//????????§?????????)????????§??????????§????????±??????????
//????????????????????????????????????????????§????????????????°?????????????(2?§???????2m????????????????????£?????¨?????????,
//2?§????????????¨??????????????°??????)?????§????????????????????????????????§?§£?????????
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int n, m, x;
int h[100000];
vector<int> et[100000];
vector<int> ec[100000];
signed main() {
cin >> n >> m >> x;
for (int i = 0; i < n; i++)
cin >> h[i];
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
et[a].push_back(b);
et[b].push_back(a);
ec[a].push_back(c);
ec[b].push_back(c);
}
static int minCost[100000];
for (int i = 0; i < n; i++)
minCost[i] = 1145141919893810;
typedef pair<int, int> P; // cost, pos
priority_queue<P> que;
que.push(P(0, 0));
while (!que.empty()) {
P now = que.top();
que.pop();
int cst = now.first;
int pos = now.second;
if (minCost[pos] <= cst)
continue;
minCost[pos] = cst;
for (int i = 0; i < et[pos].size(); i++) {
int height = max(0LL, x - cst);
int npos = et[pos][i];
if (height - ec[pos][i] > h[npos]) {
que.push(P(x - h[npos], npos)); // Down
} else if (height - ec[pos][i] > 0) {
que.push(P(cst + ec[pos][i], npos)); // Sonomama
} else {
if (h[pos] < ec[pos][i])
continue; // Cannot move
que.push(P(cst + 2 * ec[pos][i] - height, npos)); // Up -> move
}
}
}
if (minCost[n - 1] >= 1145141919893810) {
cout << -1 << endl;
} else {
cout << minCost[n - 1] + h[n - 1] - max(0LL, x - minCost[n - 1]) << endl;
}
return 0;
} | //??????????§????(??????or???????????????????????£?????????)???????????¨,
//????¨??????????t??§??¨?????°???????????¨???, t<X??????X-t[m],
//t>=X??????0[m]?????????????????????
//??????????????????????????????????????£???, (??????,
//????????§?????????)????????§??????????§????????±??????????
//????????????????????????????????????????????§????????????????°?????????????(2?§???????2m????????????????????£?????¨?????????,
//2?§????????????¨??????????????°??????)?????§????????????????????????????????§?§£?????????
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int n, m, x;
int h[100000];
vector<int> et[100000];
vector<int> ec[100000];
signed main() {
cin >> n >> m >> x;
for (int i = 0; i < n; i++)
cin >> h[i];
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
et[a].push_back(b);
et[b].push_back(a);
ec[a].push_back(c);
ec[b].push_back(c);
}
static int minCost[100000];
for (int i = 0; i < n; i++)
minCost[i] = 1145141919893810;
typedef pair<int, int> P; // cost, pos
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, 0));
while (!que.empty()) {
P now = que.top();
que.pop();
int cst = now.first;
int pos = now.second;
if (minCost[pos] <= cst)
continue;
minCost[pos] = cst;
for (int i = 0; i < et[pos].size(); i++) {
int height = max(0LL, x - cst);
int npos = et[pos][i];
if (height - ec[pos][i] > h[npos]) {
que.push(P(x - h[npos], npos)); // Down
} else if (height - ec[pos][i] > 0) {
que.push(P(cst + ec[pos][i], npos)); // Sonomama
} else {
if (h[pos] < ec[pos][i])
continue; // Cannot move
que.push(P(cst + 2 * ec[pos][i] - height, npos)); // Up -> move
}
}
}
if (minCost[n - 1] >= 1145141919893810) {
cout << -1 << endl;
} else {
cout << minCost[n - 1] + h[n - 1] - max(0LL, x - minCost[n - 1]) << endl;
}
return 0;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p00525 | C++ | Runtime Error | #pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
// #include "testlib.h"
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)
#define ECH(it, A) \
for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A) \
for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) \
REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) \
REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m) \
{ \
REP(i, n) { \
REP(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m - 1] << endl; \
} \
}
#define Display_1(A, n, m) \
{ \
REP_1(i, n) { \
REP_1(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
typedef vector<int> VI;
template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
LL x;
return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
DB x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}
template <class T> inline T &RDD(T &);
inline LL RDD() {
LL x;
return RDD(x);
}
template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline DB &RF(DB &a, DB &b) {
RF(a), RF(b);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
RF(a), RF(b), RF(c);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
RDD(a), RDD(b);
return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
RDD(a), RDD(b), RDD(c);
return a;
}
template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }
template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }
template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
sort(ALL(A));
return A;
}
template <class T, class C> inline T &SRT(T &A, C B) {
sort(ALL(A), B);
return A;
}
template <class T> inline T &RVS(T &A) {
reverse(ALL(A));
return A;
}
template <class T> inline T &UNQQ(T &A) {
A.resize(unique(ALL(A)) - A.begin());
return A;
}
template <class T> inline T &UNQ(T &A) {
SRT(A);
return UNQQ(A);
}
//}
/** Constant List .. **/ //{
const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template <class T> inline bool checkMin(T &a, const T b) {
return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
//}
//}
/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
char c;
while (!d)
;
x = c - '0';
while (d)
p;
return x;
}
template <class T> inline T &RDD(T &x) {
char c;
while (g, c != '-' && !isdigit(c))
;
if (c == '-') {
x = '0' - g;
while (d)
n;
} else {
x = c - '0';
while (d)
p;
}
return x;
}
inline DB &RF(DB &x) {
// scanf("%lf", &x);
char c;
while (g, c != '-' && c != '.' && !isdigit(c))
;
if (c == '-')
if (g == '.') {
x = 0;
DB l = 1;
while (d)
nn;
x *= l;
} else {
x = '0' - c;
while (d)
n;
if (c == '.') {
DB l = 1;
while (d)
nn;
x *= l;
}
}
else if (c == '.') {
x = 0;
DB l = 1;
while (d)
pp;
x *= l;
} else {
x = c - '0';
while (d)
p;
if (c == '.') {
DB l = 1;
while (d)
pp;
x *= l;
}
}
return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
// gets(s);
scanf("%s", s);
return s;
}
LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
// printf("Case #%d: ", ++Case);
// printf("%lld\n", x);
// printf("%.9f\n", x);
// printf("%d\n", x);
cout << x << endl;
// last_ans = x;
}
//}/*
//..................................................................................................................................
//*/
const int MAXN = 100020;
vector<int> xs;
namespace ST {
const int NN = 4 * MAXN;
bool T[NN];
int a, b;
#define lx (x << 1)
#define rx (lx | 1)
#define ml (l + r >> 1)
#define mr (ml + 1)
#define lc lx, l, ml
#define rc rx, mr, r
#define rt 1, 0, xs.size()
void cov(int x) { T[x] = 1; }
void rls(int x) {
if (T[x]) {
T[lx] = T[rx] = 1;
T[x] = 0;
}
}
bool Query(int x, int l, int r, int p) {
if (l == r) {
bool z = T[x];
T[x] = 0;
return z;
} else {
rls(x);
return p < mr ? Query(lc, p) : Query(rc, p);
}
}
bool Query(int p) { return Query(rt, p); }
void Insert(int x, int l, int r) {
if (b < l || r < a)
return;
if (a <= l && r <= b) {
cov(x);
} else {
rls(x);
Insert(lc);
Insert(rc);
}
}
void Insert(int _a, int _b) {
a = _a, b = _b;
// cout << " Insert: " << a << " " << b << " " << xs.size() << endl;
Insert(rt);
}
void Clear(int x, int l, int r, int p) {
if (l == r) {
T[x] = 0;
} else {
rls(x);
if (p < mr)
Clear(lc, p);
else
Clear(rc, p);
}
}
} // namespace ST
struct seg_tree {
static const int DEPTH = 19;
static const int SIZE = 1 << DEPTH;
int bit[1 << (DEPTH + 1)];
int renew[1 << (DEPTH + 1)];
seg_tree() {}
void init() {
for (int i = 0; i < 2 * SIZE; i++)
bit[i] = renew[i] = 0;
}
void add(int p, int v) {
p += SIZE;
while (p) {
bit[p] += v;
p >>= 1;
}
}
int query(int l, int r) {
l += SIZE;
r += SIZE;
int ret = 0;
while (l < r) {
if (l & 1)
ret += bit[l++];
if (r & 1)
ret += bit[--r];
l >>= 1;
r >>= 1;
}
return ret;
}
void set_renew(int l, int r) {
l += SIZE;
r += SIZE;
while (l < r) {
if (l & 1)
renew[l++] = 1;
if (r & 1)
renew[--r] = 1;
l >>= 1;
r >>= 1;
}
}
bool is_renew(int p) {
p += SIZE;
while (p) {
if (renew[p])
return true;
p >>= 1;
}
return false;
}
void unset_renew(int p) {
p += SIZE;
for (int i = DEPTH - 1; i > 0; i--) {
if (renew[p >> i]) {
renew[p >> i] = 0;
renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1;
}
}
renew[p] = 0;
}
};
struct action {
int pos, act, left, right;
action(int p, int a, int l, int r) {
pos = p;
act = a;
left = l;
right = r;
}
};
inline bool operator<(const action &a, const action &b) {
return a.pos < b.pos || (a.pos == b.pos && a.act < b.act);
}
int W, H, N;
int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN];
vector<int> uf;
seg_tree seg;
int target[MAXN * 2];
int root(int p) {
// cout << p << " " << uf[p] << endl;
return (uf[p] == p) ? p : (uf[p] = root(uf[p]));
}
bool join(int p, int q) {
p = root(p);
q = root(q);
if (p == q)
return false;
uf[q] = p;
return true;
}
void adjust(int p) {
// if(seg.is_renew(p)){
// cout << " " << p << endl;
// cout << " " << p << " "<< ST::Query(p) << " " << seg.is_renew(p) << endl;
// if (ST::Query(p)){
if (seg.is_renew(p)) {
// cout << "?!" << endl;
int t = uf.size();
uf.push_back(t);
seg.unset_renew(p);
target[p] = t;
}
}
template <class T> inline T low_bit(T x) { return x & -x; }
namespace BIT {
const int N = MAXN * 2;
int C[N], n;
void Add(int x, int d) {
for (; x <= n; x += low_bit(x))
C[x] += d;
}
int Sum(int x) {
int res = 0;
for (; x; x ^= low_bit(x))
res += C[x];
return res;
}
int Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
} // namespace BIT
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
scanf("%d%d%d", &W, &H, &N);
// N = 0;
for (int i = 0; i < N; i++) {
scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);
if (x1[i] > x2[i])
swap(x1[i], x2[i]);
if (y1[i] > y2[i])
swap(y1[i], y2[i]);
}
// FLC(for(int i=0;i<2*N;i++) target[i] = -1;
x1[N] = 0;
y1[N] = 0;
x2[N] = W;
y2[N] = 0;
x1[N + 1] = 0;
y1[N + 1] = 0;
x2[N + 1] = 0;
y2[N + 1] = H;
x1[N + 2] = W;
y1[N + 2] = 0;
x2[N + 2] = W;
y2[N + 2] = H;
x1[N + 3] = 0;
y1[N + 3] = H;
x2[N + 3] = W;
y2[N + 3] = H;
N += 4;
for (int i = 0; i < N; i++) {
xs.push_back(x1[i]);
xs.push_back(x2[i]);
}
xs.push_back(-1);
UNQ(xs);
BIT::n = xs.size();
for (int i = 0; i < N; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
set<int> S;
S.insert(0);
target[0] = 0;
uf.push_back(0);
vector<action> A;
for (int i = 0; i < N; i++) {
if (x1[i] == x2[i]) {
A.push_back(action(y1[i], 0, x1[i], -1));
A.push_back(action(y2[i], 2, x1[i], -1));
} else {
A.push_back(action(y1[i], 1, x1[i], x2[i]));
}
}
sort(A.begin(), A.end());
long long ret = 0;
for (int i = 0; i < A.size(); i++) {
action V = A[i];
if (V.act == 0) {
int lf = *--S.lower_bound(V.left);
adjust(lf);
adjust(V.left);
// cout << V.left << " " << lf << endl;
// cout << target[V.left] << " " << target[lf] << endl;
// target[V.left] = target[lf];
// cout << "Joint: " << target[V.left] << " " << target[lf] << endl;
join(target[V.left], target[lf]);
S.insert(V.left);
BIT::Add(V.left, 1);
} else if (V.act == 1) {
int ll = *S.lower_bound(V.left);
int rr = *--S.upper_bound(V.right);
// cout << ll << " "<< rr << endl;
if (rr <= ll)
continue;
ret += BIT::Sum(ll, rr) - 1;
seg.set_renew(ll, rr);
ST::Insert(ll, rr - 1);
} else if (V.act == 2) {
int lf = *--S.lower_bound(V.left);
adjust(lf);
adjust(V.left);
int rett = ret;
if (join(target[lf], target[V.left]))
--ret;
S.erase(V.left);
BIT::Add(V.left, -1);
}
// cout << V.act << " " << ret << endl;
}
printf("%lld\n", ret);
return 0;
} | #pragma comment(linker, "/STACK:36777216")
// #pragma GCC optimize ("O2")
#define LOCAL
// #include "testlib.h"
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
// #include <tr1/unordered_set>
// #include <tr1/unordered_map>
// #include <array>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define DWN(i, b, a) for (int i = b - 1; i >= a; --i)
#define REP_1(i, n) for (int i = 1; i <= n; ++i)
#define FOR_1(i, a, b) for (int i = a; i <= b; ++i)
#define DWN_1(i, b, a) for (int i = b; i >= a; --i)
#define REP_C(i, n) for (int n____ = n, i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = b, i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = a, i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < n; ++i)
#define FOR_N(i, a, b) for (i = a; i < b; ++i)
#define DWN_N(i, b, a) for (i = b - 1; i >= a; --i)
#define REP_1_C(i, n) for (int n____ = n, i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = b, i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = a, i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= n; ++i)
#define FOR_1_N(i, a, b) for (i = a; i <= b; ++i)
#define DWN_1_N(i, b, a) for (i = b; i >= a; --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, n); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____ = (i = 0, b); i < b____; ++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, a); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, n); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____ = (i = a, b); i <= b____; ++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, a); i >= a____; --i)
#define ECH(it, A) \
for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define rECH(it, A) \
for (__typeof((A).rbegin()) it = (A).rbegin(); it != (A).rend(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, suc) for (int i = hd; i; i = suc[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define REP_SS(x, s) for (int x = s; x; x = (x - 1) & s)
#define DO(n) for (int ____n = n; ____n-- > 0;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) \
REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) \
REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())
#define UBD(A, x) (upper_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int((A).size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define Ts *this
#define rTs return Ts
#define fi first
#define se second
#define re real()
#define im imag()
#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m) \
{ \
REP(i, n) { \
REP(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m - 1] << endl; \
} \
}
#define Display_1(A, n, m) \
{ \
REP_1(i, n) { \
REP_1(j, m - 1) cout << A[i][j] << " "; \
cout << A[i][m] << endl; \
} \
}
typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;
typedef vector<int> VI;
template <class T> inline T &RD(T &);
template <class T> inline void OT(const T &);
// inline int RD(){int x; return RD(x);}
inline LL RD() {
LL x;
return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
DB x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}
template <class T> inline T &RDD(T &);
inline LL RDD() {
LL x;
return RDD(x);
}
template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline DB &RF(DB &a, DB &b) {
RF(a), RF(b);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
RF(a), RF(b), RF(c);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1> inline T0 &RDD(T0 &a, T1 &b) {
RDD(a), RDD(b);
return a;
}
template <class T0, class T1, class T2> inline T1 &RDD(T0 &a, T1 &b, T2 &c) {
RDD(a), RDD(b), RDD(c);
return a;
}
template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); }
template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); }
template <class T> inline void CLR(T &A) { A.clear(); }
template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T> inline void CLR(T &A, int n) { REP(i, n) CLR(A[i]); }
template <class T> inline bool EPT(T &a) { return a.empty(); }
template <class T> inline T &SRT(T &A) {
sort(ALL(A));
return A;
}
template <class T, class C> inline T &SRT(T &A, C B) {
sort(ALL(A), B);
return A;
}
template <class T> inline T &RVS(T &A) {
reverse(ALL(A));
return A;
}
template <class T> inline T &UNQQ(T &A) {
A.resize(unique(ALL(A)) - A.begin());
return A;
}
template <class T> inline T &UNQ(T &A) {
SRT(A);
return UNQQ(A);
}
//}
/** Constant List .. **/ //{
const int MOD = int(1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template <class T> inline bool checkMin(T &a, const T b) {
return b < a ? a = b, 1 : 0;
}
template <class T> inline bool checkMax(T &a, const T b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class C> inline bool checkUpd(T &a, const T b, C c) {
return c(b, a) ? a = b, 1 : 0;
}
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T> inline T min(T a, T b, T c, T d, T e) {
return min(min(min(a, b), min(c, d)), e);
}
template <class T> inline T max(T a, T b, T c, T d, T e) {
return max(max(max(a, b), max(c, d)), e);
}
template <class T> inline T sqr(T a) { return a * a; }
template <class T> inline T cub(T a) { return a * a * a; }
template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; }
template <class T> T abs(T x) { return x > 0 ? x : -x; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
//}
//}
/** I/O Accelerator Interface .. **/ //{
#define g (c = getchar())
#define d isdigit(g)
#define p x = x * 10 + c - '0'
#define n x = x * 10 + '0' - c
#define pp l /= 10, p
#define nn l /= 10, n
template <class T> inline T &RD(T &x) {
char c;
while (!d)
;
x = c - '0';
while (d)
p;
return x;
}
template <class T> inline T &RDD(T &x) {
char c;
while (g, c != '-' && !isdigit(c))
;
if (c == '-') {
x = '0' - g;
while (d)
n;
} else {
x = c - '0';
while (d)
p;
}
return x;
}
inline DB &RF(DB &x) {
// scanf("%lf", &x);
char c;
while (g, c != '-' && c != '.' && !isdigit(c))
;
if (c == '-')
if (g == '.') {
x = 0;
DB l = 1;
while (d)
nn;
x *= l;
} else {
x = '0' - c;
while (d)
n;
if (c == '.') {
DB l = 1;
while (d)
nn;
x *= l;
}
}
else if (c == '.') {
x = 0;
DB l = 1;
while (d)
pp;
x *= l;
} else {
x = c - '0';
while (d)
p;
if (c == '.') {
DB l = 1;
while (d)
pp;
x *= l;
}
}
return x;
}
#undef nn
#undef pp
#undef n
#undef p
#undef d
#undef g
inline char *RS(char *s) {
// gets(s);
scanf("%s", s);
return s;
}
LL last_ans;
int Case;
template <class T> inline void OT(const T &x) {
// printf("Case #%d: ", ++Case);
// printf("%lld\n", x);
// printf("%.9f\n", x);
// printf("%d\n", x);
cout << x << endl;
// last_ans = x;
}
//}/*
//..................................................................................................................................
//*/
const int MAXN = 100020;
vector<int> xs;
namespace ST {
const int NN = 8 * MAXN;
bool T[NN];
int a, b;
#define lx (x << 1)
#define rx (lx | 1)
#define ml (l + r >> 1)
#define mr (ml + 1)
#define lc lx, l, ml
#define rc rx, mr, r
#define rt 1, 0, xs.size()
void cov(int x) { T[x] = 1; }
void rls(int x) {
if (T[x]) {
T[lx] = T[rx] = 1;
T[x] = 0;
}
}
bool Query(int x, int l, int r, int p) {
if (l == r) {
bool z = T[x];
T[x] = 0;
return z;
} else {
rls(x);
return p < mr ? Query(lc, p) : Query(rc, p);
}
}
bool Query(int p) { return Query(rt, p); }
void Insert(int x, int l, int r) {
if (b < l || r < a)
return;
if (a <= l && r <= b) {
cov(x);
} else {
rls(x);
Insert(lc);
Insert(rc);
}
}
void Insert(int _a, int _b) {
a = _a, b = _b;
// cout << " Insert: " << a << " " << b << " " << xs.size() << endl;
Insert(rt);
}
void Clear(int x, int l, int r, int p) {
if (l == r) {
T[x] = 0;
} else {
rls(x);
if (p < mr)
Clear(lc, p);
else
Clear(rc, p);
}
}
} // namespace ST
struct seg_tree {
static const int DEPTH = 19;
static const int SIZE = 1 << DEPTH;
int bit[1 << (DEPTH + 1)];
int renew[1 << (DEPTH + 1)];
seg_tree() {}
void init() {
for (int i = 0; i < 2 * SIZE; i++)
bit[i] = renew[i] = 0;
}
void add(int p, int v) {
p += SIZE;
while (p) {
bit[p] += v;
p >>= 1;
}
}
int query(int l, int r) {
l += SIZE;
r += SIZE;
int ret = 0;
while (l < r) {
if (l & 1)
ret += bit[l++];
if (r & 1)
ret += bit[--r];
l >>= 1;
r >>= 1;
}
return ret;
}
void set_renew(int l, int r) {
l += SIZE;
r += SIZE;
while (l < r) {
if (l & 1)
renew[l++] = 1;
if (r & 1)
renew[--r] = 1;
l >>= 1;
r >>= 1;
}
}
bool is_renew(int p) {
p += SIZE;
while (p) {
if (renew[p])
return true;
p >>= 1;
}
return false;
}
void unset_renew(int p) {
p += SIZE;
for (int i = DEPTH - 1; i > 0; i--) {
if (renew[p >> i]) {
renew[p >> i] = 0;
renew[(p >> i) * 2] = renew[(p >> i) * 2 + 1] = 1;
}
}
renew[p] = 0;
}
};
struct action {
int pos, act, left, right;
action(int p, int a, int l, int r) {
pos = p;
act = a;
left = l;
right = r;
}
};
inline bool operator<(const action &a, const action &b) {
return a.pos < b.pos || (a.pos == b.pos && a.act < b.act);
}
int W, H, N;
int x1[MAXN], y1[MAXN], x2[MAXN], y2[MAXN];
vector<int> uf;
seg_tree seg;
int target[MAXN * 2];
int root(int p) {
// cout << p << " " << uf[p] << endl;
return (uf[p] == p) ? p : (uf[p] = root(uf[p]));
}
bool join(int p, int q) {
p = root(p);
q = root(q);
if (p == q)
return false;
uf[q] = p;
return true;
}
void adjust(int p) {
// if(seg.is_renew(p)){
// cout << " " << p << endl;
// cout << " " << p << " "<< ST::Query(p) << " " << seg.is_renew(p) << endl;
// if (ST::Query(p)){
if (seg.is_renew(p)) {
// cout << "?!" << endl;
int t = uf.size();
uf.push_back(t);
seg.unset_renew(p);
target[p] = t;
}
}
template <class T> inline T low_bit(T x) { return x & -x; }
namespace BIT {
const int N = MAXN * 2;
int C[N], n;
void Add(int x, int d) {
for (; x <= n; x += low_bit(x))
C[x] += d;
}
int Sum(int x) {
int res = 0;
for (; x; x ^= low_bit(x))
res += C[x];
return res;
}
int Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
} // namespace BIT
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
scanf("%d%d%d", &W, &H, &N);
// N = 0;
for (int i = 0; i < N; i++) {
scanf("%d%d%d%d", x1 + i, y1 + i, x2 + i, y2 + i);
if (x1[i] > x2[i])
swap(x1[i], x2[i]);
if (y1[i] > y2[i])
swap(y1[i], y2[i]);
}
// FLC(for(int i=0;i<2*N;i++) target[i] = -1;
x1[N] = 0;
y1[N] = 0;
x2[N] = W;
y2[N] = 0;
x1[N + 1] = 0;
y1[N + 1] = 0;
x2[N + 1] = 0;
y2[N + 1] = H;
x1[N + 2] = W;
y1[N + 2] = 0;
x2[N + 2] = W;
y2[N + 2] = H;
x1[N + 3] = 0;
y1[N + 3] = H;
x2[N + 3] = W;
y2[N + 3] = H;
N += 4;
for (int i = 0; i < N; i++) {
xs.push_back(x1[i]);
xs.push_back(x2[i]);
}
xs.push_back(-1);
UNQ(xs);
BIT::n = xs.size();
for (int i = 0; i < N; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
set<int> S;
S.insert(0);
target[0] = 0;
uf.push_back(0);
vector<action> A;
for (int i = 0; i < N; i++) {
if (x1[i] == x2[i]) {
A.push_back(action(y1[i], 0, x1[i], -1));
A.push_back(action(y2[i], 2, x1[i], -1));
} else {
A.push_back(action(y1[i], 1, x1[i], x2[i]));
}
}
sort(A.begin(), A.end());
long long ret = 0;
for (int i = 0; i < A.size(); i++) {
action V = A[i];
if (V.act == 0) {
int lf = *--S.lower_bound(V.left);
adjust(lf);
adjust(V.left);
// cout << V.left << " " << lf << endl;
// cout << target[V.left] << " " << target[lf] << endl;
// target[V.left] = target[lf];
// cout << "Joint: " << target[V.left] << " " << target[lf] << endl;
join(target[V.left], target[lf]);
S.insert(V.left);
BIT::Add(V.left, 1);
} else if (V.act == 1) {
int ll = *S.lower_bound(V.left);
int rr = *--S.upper_bound(V.right);
// cout << ll << " "<< rr << endl;
if (rr <= ll)
continue;
ret += BIT::Sum(ll, rr) - 1;
seg.set_renew(ll, rr);
ST::Insert(ll, rr - 1);
} else if (V.act == 2) {
int lf = *--S.lower_bound(V.left);
adjust(lf);
adjust(V.left);
int rett = ret;
if (join(target[lf], target[V.left]))
--ret;
S.erase(V.left);
BIT::Add(V.left, -1);
}
// cout << V.act << " " << ret << endl;
}
printf("%lld\n", ret);
return 0;
} | replace | 502 | 503 | 502 | 503 | 0 | |
p00527 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll l1, l2;
string s1, s2;
ll dp[2000][2000]; // i,j???????????§?????????(I??????)
ll rec(ll x1, ll x2) {
ll res = 1;
if (dp[x1][x2] != -1)
return dp[x1][x2];
if (x1 == l1 && x2 == l2)
return dp[x1][x2] = 0;
if (x1 == l1) {
if (x2 < l2 - 1 && s2[x2] == 'I' && s2[x2 + 1] == 'O')
return dp[x1][x2] = rec(x1, x2 + 2) > 0 ? rec(x1, x2 + 2) + 2 : 1;
else if (s2[x2] == 'I')
return dp[x1][x2] = 1;
else
return dp[x1][x2] = 0;
}
if (x2 == l2) {
if (x1 < l1 - 1 && s1[x1] == 'I' && s1[x1 + 1] == 'O')
return dp[x1][x2] = rec(x1 + 2, x2) > 0 ? rec(x1 + 2, x2) + 2 : 1;
else if (s1[x1] == 'I')
return dp[x1][x2] = 1;
else
return dp[x1][x2] = 0;
}
if (s1[x1] == 'O' && s2[x2] == 'O')
return dp[x1][x2] = 0;
ll g;
if ((s1[x1] == 'I' && s2[x2] == 'O') || (s1[x1] == 'O' && s2[x2] == 'I')) {
g = rec(x1 + 1, x2 + 1);
if (g > 0)
res = max(res, g + 2);
}
if (x1 < l1 - 1 && s1[x1] == 'I' && s1[x1 + 1] == 'O') {
g = rec(x1 + 2, x2);
if (g > 0)
res = max(res, g + 2);
}
if (x2 < l2 - 1 && s2[x2] == 'I' && s2[x2 + 1] == 'O') {
g = rec(x1, x2 + 2);
if (g > 0)
res = max(res, g + 2);
}
return dp[x1][x2] = res;
}
int main() {
ll ans = 0;
memset(dp, -1, sizeof(dp));
cin >> l1 >> l2;
cin >> s1 >> s2;
rrep(i, l1) {
rrep(j, l2) { ans = max(ans, rec(i, j)); }
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)
ll l1, l2;
string s1, s2;
ll dp[2001][2001]; // i,j???????????§?????????(I??????)
ll rec(ll x1, ll x2) {
ll res = 1;
if (dp[x1][x2] != -1)
return dp[x1][x2];
if (x1 == l1 && x2 == l2)
return dp[x1][x2] = 0;
if (x1 == l1) {
if (x2 < l2 - 1 && s2[x2] == 'I' && s2[x2 + 1] == 'O')
return dp[x1][x2] = rec(x1, x2 + 2) > 0 ? rec(x1, x2 + 2) + 2 : 1;
else if (s2[x2] == 'I')
return dp[x1][x2] = 1;
else
return dp[x1][x2] = 0;
}
if (x2 == l2) {
if (x1 < l1 - 1 && s1[x1] == 'I' && s1[x1 + 1] == 'O')
return dp[x1][x2] = rec(x1 + 2, x2) > 0 ? rec(x1 + 2, x2) + 2 : 1;
else if (s1[x1] == 'I')
return dp[x1][x2] = 1;
else
return dp[x1][x2] = 0;
}
if (s1[x1] == 'O' && s2[x2] == 'O')
return dp[x1][x2] = 0;
ll g;
if ((s1[x1] == 'I' && s2[x2] == 'O') || (s1[x1] == 'O' && s2[x2] == 'I')) {
g = rec(x1 + 1, x2 + 1);
if (g > 0)
res = max(res, g + 2);
}
if (x1 < l1 - 1 && s1[x1] == 'I' && s1[x1 + 1] == 'O') {
g = rec(x1 + 2, x2);
if (g > 0)
res = max(res, g + 2);
}
if (x2 < l2 - 1 && s2[x2] == 'I' && s2[x2 + 1] == 'O') {
g = rec(x1, x2 + 2);
if (g > 0)
res = max(res, g + 2);
}
return dp[x1][x2] = res;
}
int main() {
ll ans = 0;
memset(dp, -1, sizeof(dp));
cin >> l1 >> l2;
cin >> s1 >> s2;
rrep(i, l1) {
rrep(j, l2) { ans = max(ans, rec(i, j)); }
}
cout << ans << endl;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p00527 | C++ | Runtime Error | #include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
int dp[2001][2001][2];
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
int N, M;
cin >> N >> M;
string a, b;
cin >> a >> b;
// reverse(a.begin(), a.end());
// reverse(b.begin(), b.end());
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= 2000; ++i) {
for (int j = 0; j <= 2000; ++j) {
dp[i][j][1] = 0;
dp[i][j][0] = -1e9;
}
}
int ans = 0;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= M; ++j) {
for (int k = 0; k < 2; ++k) {
if (i != N) {
if ((a[i] == 'I') == k) {
dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
}
}
if (j != M) {
if ((b[j] == 'I') == k) {
dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
}
}
}
ans = max(ans, dp[i][j][0]);
}
}
if (ans % 2 == 0) {
assert(false);
ans--;
}
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;
int dp[2001][2001][2];
//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
int N, M;
cin >> N >> M;
string a, b;
cin >> a >> b;
// reverse(a.begin(), a.end());
// reverse(b.begin(), b.end());
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= 2000; ++i) {
for (int j = 0; j <= 2000; ++j) {
dp[i][j][1] = 0;
dp[i][j][0] = -1e9;
}
}
int ans = 0;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= M; ++j) {
for (int k = 0; k < 2; ++k) {
if (i != N) {
if ((a[i] == 'I') == k) {
dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
}
}
if (j != M) {
if ((b[j] == 'I') == k) {
dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
}
}
}
ans = max(ans, dp[i][j][0]);
}
}
if (ans && ans % 2 == 0) {
ans--;
}
cout << ans << endl;
return 0;
} | replace | 44 | 46 | 44 | 45 | 0 | |
p00527 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
char s[2][2001];
int dp[2001][2001][2], n[2];
int main() {
scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
int Max = 0;
for (int i = n[0]; i >= 0; i--)
for (int j = n[1]; j >= 0; j--)
rep(t, 2) rep(k, 2) {
int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
if (b == n[k] || a % 2 == t)
continue;
if (t != (s[k][b] == 'I' ? 0 : 1))
dp[i][j][t] = max(dp[i][j][t], a + 1);
if (dp[i][j][t] % 2)
Max = max(Max, dp[i][j][t]);
}
printf("%d\n", Max);
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
char s[2][2001];
int dp[2002][2002][2], n[2];
int main() {
scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
int Max = 0;
for (int i = n[0]; i >= 0; i--)
for (int j = n[1]; j >= 0; j--)
rep(t, 2) rep(k, 2) {
int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
if (b == n[k] || a % 2 == t)
continue;
if (t != (s[k][b] == 'I' ? 0 : 1))
dp[i][j][t] = max(dp[i][j][t], a + 1);
if (dp[i][j][t] % 2)
Max = max(Max, dp[i][j][t]);
}
printf("%d\n", Max);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p00527 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
int i, j, m, n, ans = 0, l;
static int r[52][52][2] = {0};
char s[4001], t[4001];
scanf("%d%d", &m, &n);
scanf("%s", s);
scanf("%s", t);
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
for (l = 0; l < 2; l++) {
if (l == 0) {
if (s[i] == 'I')
r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
if (t[j] == 'I')
r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
} else {
if (s[i] == 'O' && r[i][j][1] != 0)
r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
if (t[j] == 'O' && r[i][j][1] != 0)
r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
}
}
}
}
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
ans = max(ans, r[i][j][1]);
}
}
printf("%d\n", ans);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
int i, j, m, n, ans = 0, l;
static int r[2001][2001][2] = {0};
char s[4001], t[4001];
scanf("%d%d", &m, &n);
scanf("%s", s);
scanf("%s", t);
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
for (l = 0; l < 2; l++) {
if (l == 0) {
if (s[i] == 'I')
r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
if (t[j] == 'I')
r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
} else {
if (s[i] == 'O' && r[i][j][1] != 0)
r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
if (t[j] == 'O' && r[i][j][1] != 0)
r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
}
}
}
}
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
ans = max(ans, r[i][j][1]);
}
}
printf("%d\n", ans);
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p00527 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int dp[2000][2000][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
cin >> a >> b >> c >> d;
c += " ";
d += " ";
for (int e = a + b - 1; e >= 0; e--) {
for (int x = 0; x <= a; x++) {
int y = e - x;
if (y >= 0 && y <= b) {
int I = 0, O = 0;
if (c[x] == 'I') {
I = dp[x + 1][y][0] + 1;
} else if (c[x] == 'O') {
O = dp[x + 1][y][1] + 1;
}
if (d[y] == 'I') {
I = max(I, dp[x][y + 1][0] + 1);
} else if (d[y] == 'O') {
O = max(O, dp[x][y + 1][1] + 1);
}
dp[x][y][1] = I;
dp[x][y][0] = O;
}
}
}
int MAX = 0;
for (int p = 0; p <= a; p++) {
for (int q = 0; q <= b; q++) {
if ((dp[p][q][1] & 1) == 0)
dp[p][q][1]--;
MAX = max(MAX, dp[p][q][1]);
}
}
cout << MAX << endl;
} | #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int dp[2005][2005][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
cin >> a >> b >> c >> d;
c += " ";
d += " ";
for (int e = a + b - 1; e >= 0; e--) {
for (int x = 0; x <= a; x++) {
int y = e - x;
if (y >= 0 && y <= b) {
int I = 0, O = 0;
if (c[x] == 'I') {
I = dp[x + 1][y][0] + 1;
} else if (c[x] == 'O') {
O = dp[x + 1][y][1] + 1;
}
if (d[y] == 'I') {
I = max(I, dp[x][y + 1][0] + 1);
} else if (d[y] == 'O') {
O = max(O, dp[x][y + 1][1] + 1);
}
dp[x][y][1] = I;
dp[x][y][0] = O;
}
}
}
int MAX = 0;
for (int p = 0; p <= a; p++) {
for (int q = 0; q <= b; q++) {
if ((dp[p][q][1] & 1) == 0)
dp[p][q][1]--;
MAX = max(MAX, dp[p][q][1]);
}
}
cout << MAX << endl;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p00527 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
// ライブラリゾーン!!!!
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ll Mod = 1000000007;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { // 隣接リスト表現
ll to, cost;
};
struct S { // 辺の情報を入れる変数
int from, to, cost;
};
struct H {
int x, y;
};
bool operator<(H a, H b) {
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
ll gcm(ll i, ll j) { // 最大公約数
if (i > j)
swap(i, j);
if (i == 0)
return j;
return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad=座標上の2点間の距離
int ari(int a, int b, int c) { return (a + b) * c / 2; } // 等差数列の和
bool suf(ld a, ld b, ld c, ld d) {
if (b <= c || d <= a)
return 0;
return 1;
} //[a,b),[c,d)
ll mod_pow(ll x, ll n, ll p) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m, ans;
string s1, s2;
int dp[2000][2000][2];
int solve(int x, int y, int z) {
if (x == n && y == m) {
if (z == 0)
return -inf;
return 0;
}
if (dp[x][y][z] > 0) {
return dp[x][y][z];
}
int sum = 0;
if (x < n) {
if (s1[x] == 'O' && z == 1) {
sum = max(sum, solve(x + 1, y, 0) + 1);
}
if (s1[x] == 'I' && z == 0) {
sum = max(sum, solve(x + 1, y, 1) + 1);
}
}
if (y < m) {
if (s2[y] == 'O' && z == 1) {
sum = max(sum, solve(x, y + 1, 0) + 1);
}
if (s2[y] == 'I' && z == 0) {
sum = max(sum, solve(x, y + 1, 1) + 1);
}
}
if (z == 0 && sum == 0) {
sum = -inf;
}
ans = max(ans, sum);
return dp[x][y][z] = sum;
}
signed main() {
cin >> n >> m;
cin >> s1 >> s2;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
solve(i, j, 0);
}
}
cout << ans << endl;
getchar();
getchar();
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
// ライブラリゾーン!!!!
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
const ll Mod = 1000000007;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { // 隣接リスト表現
ll to, cost;
};
struct S { // 辺の情報を入れる変数
int from, to, cost;
};
struct H {
int x, y;
};
bool operator<(H a, H b) {
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
ll gcm(ll i, ll j) { // 最大公約数
if (i > j)
swap(i, j);
if (i == 0)
return j;
return gcm(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad=座標上の2点間の距離
int ari(int a, int b, int c) { return (a + b) * c / 2; } // 等差数列の和
bool suf(ld a, ld b, ld c, ld d) {
if (b <= c || d <= a)
return 0;
return 1;
} //[a,b),[c,d)
ll mod_pow(ll x, ll n, ll p) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m, ans;
string s1, s2;
int dp[2001][2001][2];
int solve(int x, int y, int z) {
if (x == n && y == m) {
if (z == 0)
return -inf;
return 0;
}
if (dp[x][y][z] > 0) {
return dp[x][y][z];
}
int sum = 0;
if (x < n) {
if (s1[x] == 'O' && z == 1) {
sum = max(sum, solve(x + 1, y, 0) + 1);
}
if (s1[x] == 'I' && z == 0) {
sum = max(sum, solve(x + 1, y, 1) + 1);
}
}
if (y < m) {
if (s2[y] == 'O' && z == 1) {
sum = max(sum, solve(x, y + 1, 0) + 1);
}
if (s2[y] == 'I' && z == 0) {
sum = max(sum, solve(x, y + 1, 1) + 1);
}
}
if (z == 0 && sum == 0) {
sum = -inf;
}
ans = max(ans, sum);
return dp[x][y][z] = sum;
}
signed main() {
cin >> n >> m;
cin >> s1 >> s2;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
solve(i, j, 0);
}
}
cout << ans << endl;
getchar();
getchar();
}
| replace | 74 | 75 | 74 | 75 | 0 | |
p00527 | C++ | Runtime Error |
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define MFOR(i, end) for (size_t i = 0, end_##i = (end); i < end_##i; ++i)
size_t M, N;
char S[2500];
char T[2500];
enum class Mode_T : int8_t { none = 0, I, O };
int dp[2010][2010][3];
int func(int spos, int tpos, Mode_T last) {
if (S[spos] == '\0' && T[tpos] == '\0') {
return -2010 * 2010 * 3;
}
if (dp[spos][tpos][(int8_t)last] != -1) {
return dp[spos][tpos][(int8_t)last];
}
int max = -2010 * 2010 * 3;
if (last == Mode_T::none) {
if (S[spos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos + 1, tpos, Mode_T::I)));
}
if (S[spos] != '\0')
max = std::max(max, 0 + func(spos + 1, tpos, Mode_T::none));
if (T[tpos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos, tpos + 1, Mode_T::I)));
}
if (T[tpos] != '\0')
max = std::max(max, 0 + func(spos, tpos + 1, Mode_T::none));
} else if (last == Mode_T::I) {
if (S[spos] == 'O') {
auto snpos = spos + 1;
if (S[snpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(snpos + 1, tpos, Mode_T::I)));
}
if (T[tpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(snpos, tpos + 1, Mode_T::I)));
}
}
if (T[tpos] == 'O') {
auto tnpos = spos + 1;
if (S[spos] == 'I') {
max = std::max(max, 2 + std::max(0, func(spos + 1, tnpos, Mode_T::I)));
}
if (T[tnpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(spos, tnpos + 1, Mode_T::I)));
}
}
} else if (last == Mode_T::O) {
assert(false);
if (S[spos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos + 1, tpos, Mode_T::I)));
}
if (T[tpos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos, tpos + 1, Mode_T::I)));
}
}
return dp[spos][tpos][(int8_t)last] = max;
}
int main() {
for (auto &arr2 : dp)
for (auto &arr1 : arr2)
for (auto &i : arr1) {
i = -1;
}
std::cin >> M >> N;
std::cin >> S >> T;
std::cout << std::max(0, func(0, 0, Mode_T::none)) << std::endl;
} |
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define MFOR(i, end) for (size_t i = 0, end_##i = (end); i < end_##i; ++i)
size_t M, N;
char S[2500];
char T[2500];
enum class Mode_T : int8_t { none = 0, I, O };
int dp[2010][2010][3];
int func(int spos, int tpos, Mode_T last) {
if (S[spos] == '\0' && T[tpos] == '\0') {
return -2010 * 2010 * 3;
}
if (dp[spos][tpos][(int8_t)last] != -1) {
return dp[spos][tpos][(int8_t)last];
}
int max = -2010 * 2010 * 3;
if (last == Mode_T::none) {
if (S[spos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos + 1, tpos, Mode_T::I)));
}
if (S[spos] != '\0')
max = std::max(max, 0 + func(spos + 1, tpos, Mode_T::none));
if (T[tpos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos, tpos + 1, Mode_T::I)));
}
if (T[tpos] != '\0')
max = std::max(max, 0 + func(spos, tpos + 1, Mode_T::none));
} else if (last == Mode_T::I) {
if (S[spos] == 'O') {
auto snpos = spos + 1;
if (S[snpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(snpos + 1, tpos, Mode_T::I)));
}
if (T[tpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(snpos, tpos + 1, Mode_T::I)));
}
}
if (T[tpos] == 'O') {
auto tnpos = tpos + 1;
if (S[spos] == 'I') {
max = std::max(max, 2 + std::max(0, func(spos + 1, tnpos, Mode_T::I)));
}
if (T[tnpos] == 'I') {
max = std::max(max, 2 + std::max(0, func(spos, tnpos + 1, Mode_T::I)));
}
}
} else if (last == Mode_T::O) {
assert(false);
if (S[spos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos + 1, tpos, Mode_T::I)));
}
if (T[tpos] == 'I') {
max = std::max(max, 1 + std::max(0, func(spos, tpos + 1, Mode_T::I)));
}
}
return dp[spos][tpos][(int8_t)last] = max;
}
int main() {
for (auto &arr2 : dp)
for (auto &arr1 : arr2)
for (auto &i : arr1) {
i = -1;
}
std::cin >> M >> N;
std::cin >> S >> T;
std::cout << std::max(0, func(0, 0, Mode_T::none)) << std::endl;
} | replace | 64 | 65 | 64 | 65 | -11 | |
p00527 | C++ | Runtime Error | #include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[1005][1005][2];
int main() {
int n, m;
int i, j, k;
int a, b;
string t[2];
string sa = "IO";
int s = 0;
cin >> n >> m;
for (i = 0; i < 2; i++) {
cin >> t[i];
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++) {
for (k = 0; k < 2; k++) {
a = (i + j + k) % 2;
if (a == 0 && dp[i][j][k] == 0) {
if (t[0][i] == 'I')
dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
if (t[1][j] == 'I')
dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
}
if (dp[i][j][k] == 0)
continue;
if (t[0][i] == sa[a]) {
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
}
if (t[1][j] == sa[a]) {
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
}
}
}
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++) {
for (k = 0; k < 2; k++) {
if (dp[i][j][k] % 2 == 0)
continue;
s = max(s, dp[i][j][k]);
}
}
}
cout << s << endl;
return 0;
} | #include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[2005][2005][2];
int main() {
int n, m;
int i, j, k;
int a, b;
string t[2];
string sa = "IO";
int s = 0;
cin >> n >> m;
for (i = 0; i < 2; i++) {
cin >> t[i];
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++) {
for (k = 0; k < 2; k++) {
a = (i + j + k) % 2;
if (a == 0 && dp[i][j][k] == 0) {
if (t[0][i] == 'I')
dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
if (t[1][j] == 'I')
dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
}
if (dp[i][j][k] == 0)
continue;
if (t[0][i] == sa[a]) {
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
}
if (t[1][j] == sa[a]) {
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
}
}
}
}
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++) {
for (k = 0; k < 2; k++) {
if (dp[i][j][k] % 2 == 0)
continue;
s = max(s, dp[i][j][k]);
}
}
}
cout << s << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00527 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX_N 2200
int dp[MAX_N][MAX_N][2];
string S, T;
int a, b, g, h, maxn;
int main() {
cin >> a >> b;
cin >> S >> T;
while (S.size() != MAX_N) {
S += ' ';
}
while (T.size() != MAX_N) {
T += ' ';
}
g = max(a, b);
h = min(20, min(a, b));
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= g; j++) {
for (int k = 0; k <= g; k++) {
if (S[j] == 'I') {
dp[j][k][0] = max(dp[j][k][0], 1);
if (S[j + 1] == 'O') {
dp[j + 1][k][0] = max(dp[j + 1][k][0], dp[j][k][0] + 1);
}
if (T[k] == 'O') {
dp[k][j + 1][1] = max(dp[k][j + 1][1], dp[j][k][0] + 1);
}
} else if (S[j] == 'O') {
if (S[j + 1] == 'I') {
dp[j + 1][k][0] = max(dp[j + 1][k][0], dp[j][k][0] + 1);
}
if (T[k] == 'I') {
dp[k][j + 1][1] = max(dp[k][j + 1][1], dp[j][k][0] + 1);
}
}
if (T[k] == 'I') {
dp[k][j][1] = max(dp[k][j][1], 1);
if (T[k + 1] == 'O') {
dp[k + 1][j][1] = max(dp[k + 1][j][1], dp[k][j][1] + 1);
}
if (S[j] == 'O') {
dp[j][k + 1][0] = max(dp[j][k + 1][0], dp[k][j][1] + 1);
}
} else if (T[k] == 'O') {
if (T[k + 1] == 'I') {
dp[k + 1][j][1] = max(dp[k + 1][j][1], dp[k][j][1] + 1);
}
if (S[j] == 'I') {
dp[j][k + 1][0] = max(dp[j][k + 1][0], dp[k][j][1] + 1);
}
}
}
}
}
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_N; j++) {
if (dp[i][j][0] % 2 == 1) {
maxn = max(maxn, dp[i][j][0]);
}
if (dp[i][j][1] % 2 == 1) {
maxn = max(maxn, dp[i][j][1]);
}
}
}
cout << maxn << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX_N 2200
int dp[MAX_N][MAX_N][2];
string S, T;
int a, b, g, h, maxn;
int main() {
cin >> a >> b;
cin >> S >> T;
while (S.size() != MAX_N) {
S += ' ';
}
while (T.size() != MAX_N) {
T += ' ';
}
g = max(a, b);
h = min(17, min(a, b));
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= g; j++) {
for (int k = 0; k <= g; k++) {
if (S[j] == 'I') {
dp[j][k][0] = max(dp[j][k][0], 1);
if (S[j + 1] == 'O') {
dp[j + 1][k][0] = max(dp[j + 1][k][0], dp[j][k][0] + 1);
}
if (T[k] == 'O') {
dp[k][j + 1][1] = max(dp[k][j + 1][1], dp[j][k][0] + 1);
}
} else if (S[j] == 'O') {
if (S[j + 1] == 'I') {
dp[j + 1][k][0] = max(dp[j + 1][k][0], dp[j][k][0] + 1);
}
if (T[k] == 'I') {
dp[k][j + 1][1] = max(dp[k][j + 1][1], dp[j][k][0] + 1);
}
}
if (T[k] == 'I') {
dp[k][j][1] = max(dp[k][j][1], 1);
if (T[k + 1] == 'O') {
dp[k + 1][j][1] = max(dp[k + 1][j][1], dp[k][j][1] + 1);
}
if (S[j] == 'O') {
dp[j][k + 1][0] = max(dp[j][k + 1][0], dp[k][j][1] + 1);
}
} else if (T[k] == 'O') {
if (T[k + 1] == 'I') {
dp[k + 1][j][1] = max(dp[k + 1][j][1], dp[k][j][1] + 1);
}
if (S[j] == 'I') {
dp[j][k + 1][0] = max(dp[j][k + 1][0], dp[k][j][1] + 1);
}
}
}
}
}
for (int i = 0; i < MAX_N; i++) {
for (int j = 0; j < MAX_N; j++) {
if (dp[i][j][0] % 2 == 1) {
maxn = max(maxn, dp[i][j][0]);
}
if (dp[i][j][1] % 2 == 1) {
maxn = max(maxn, dp[i][j][1]);
}
}
}
cout << maxn << endl;
return 0;
} | replace | 21 | 22 | 21 | 22 | TLE | |
p00527 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[2001], T[2001];
int dp[2000][2000][2];
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dp[i][j][1] = -0x7ffffff;
dp[i][j][0] = 0;
}
}
scanf("%s", S + 1);
scanf("%s", T + 1);
int MAX = 0;
for (int s = 0; s <= N; s++) {
for (int t = 0; t <= M; t++) {
if (s < N && S[s + 1] == 'I') {
dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
MAX = max(MAX, dp[s + 1][t][1]);
}
if (t < M && T[t + 1] == 'I') {
dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
MAX = max(MAX, dp[s][t + 1][1]);
}
if (s < N && S[s + 1] == 'O') {
dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
// MAX = max(MAX, dp[s+1][t][0]);
}
if (t < M && T[t + 1] == 'O') {
dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
// MAX = max(MAX, dp[s][t+1][0]);
}
}
}
cout << MAX << endl;
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[4001], T[4001];
int dp[4000][4000][2];
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
dp[i][j][1] = -0x7ffffff;
dp[i][j][0] = 0;
}
}
scanf("%s", S + 1);
scanf("%s", T + 1);
int MAX = 0;
for (int s = 0; s <= N; s++) {
for (int t = 0; t <= M; t++) {
if (s < N && S[s + 1] == 'I') {
dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
MAX = max(MAX, dp[s + 1][t][1]);
}
if (t < M && T[t + 1] == 'I') {
dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
MAX = max(MAX, dp[s][t + 1][1]);
}
if (s < N && S[s + 1] == 'O') {
dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
// MAX = max(MAX, dp[s+1][t][0]);
}
if (t < M && T[t + 1] == 'O') {
dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
// MAX = max(MAX, dp[s][t+1][0]);
}
}
}
cout << MAX << endl;
return 0;
}
| replace | 6 | 8 | 6 | 8 | 0 | |
p00528 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_K = 100005;
int d[MAX_K][2], n, m, k;
vector<ppiii> xy, yx;
vector<pipii> G[MAX_K][2];
int Dijkstra() {
fill(d[0], d[MAX_K], INF);
d[k][0] = 0;
priority_queue<pipii, vector<pipii>, greater<pipii>> pq;
pq.push({0, {k, 0}});
while (pq.size()) {
pipii p = pq.top();
pq.pop();
int v = p.second.first;
int st = p.second.second;
if (d[v][st] < p.first)
continue;
for (int i = 0; i < G[v][st].size(); ++i) {
int tov = G[v][st][i].second.first, tost = G[v][st][i].second.second;
if (d[tov][tost] <= p.first + G[v][st][i].first)
continue;
d[tov][tost] = p.first + G[v][st][i].first;
pq.push({d[tov][tost], {tov, tost}});
}
}
int res = min(d[k + 1][0], d[k + 1][1]);
return INF <= res ? -1 : res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> m >> n >> k;
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
xy.PB({{x, y}, i});
yx.PB({{y, x}, i});
G[i][0].PB({1, {i, 1}});
G[i][1].PB({1, {i, 0}});
if (x == 1) {
G[k][0].PB({y - 1, {i, 0}});
}
if (x == m) {
G[i][0].PB({n - y, {k + 1, 0}});
}
if (y == n) {
G[i][1].PB({m - x, {k + 1, 1}});
}
}
sort(ALL(xy));
sort(ALL(yx));
for (int i = 0; i < k; ++i) {
for (int j = i + 1; j < k; ++j) {
if (xy[i].first.first != xy[j].first.first) {
i = j - 1;
break;
}
int cost = xy[j].first.second - xy[j - 1].first.second;
int to = xy[j].second, from = xy[j - 1].second;
G[from][0].PB({cost, {to, 0}});
G[to][0].PB({cost, {from, 0}});
}
}
for (int i = 0; i < k; ++i) {
for (int j = i + 1; j < k; ++j) {
if (yx[i].first.first != yx[j].first.first) {
i = j - 1;
break;
}
int cost = yx[j].first.second - yx[j - 1].first.second;
int to = yx[j].second, from = yx[j - 1].second;
G[from][1].PB({cost, {to, 1}});
G[to][1].PB({cost, {from, 1}});
}
}
cout << Dijkstra() << endl;
} | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MAX_K = 200005;
int d[MAX_K][2], n, m, k;
vector<ppiii> xy, yx;
vector<pipii> G[MAX_K][2];
int Dijkstra() {
fill(d[0], d[MAX_K], INF);
d[k][0] = 0;
priority_queue<pipii, vector<pipii>, greater<pipii>> pq;
pq.push({0, {k, 0}});
while (pq.size()) {
pipii p = pq.top();
pq.pop();
int v = p.second.first;
int st = p.second.second;
if (d[v][st] < p.first)
continue;
for (int i = 0; i < G[v][st].size(); ++i) {
int tov = G[v][st][i].second.first, tost = G[v][st][i].second.second;
if (d[tov][tost] <= p.first + G[v][st][i].first)
continue;
d[tov][tost] = p.first + G[v][st][i].first;
pq.push({d[tov][tost], {tov, tost}});
}
}
int res = min(d[k + 1][0], d[k + 1][1]);
return INF <= res ? -1 : res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> m >> n >> k;
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
xy.PB({{x, y}, i});
yx.PB({{y, x}, i});
G[i][0].PB({1, {i, 1}});
G[i][1].PB({1, {i, 0}});
if (x == 1) {
G[k][0].PB({y - 1, {i, 0}});
}
if (x == m) {
G[i][0].PB({n - y, {k + 1, 0}});
}
if (y == n) {
G[i][1].PB({m - x, {k + 1, 1}});
}
}
sort(ALL(xy));
sort(ALL(yx));
for (int i = 0; i < k; ++i) {
for (int j = i + 1; j < k; ++j) {
if (xy[i].first.first != xy[j].first.first) {
i = j - 1;
break;
}
int cost = xy[j].first.second - xy[j - 1].first.second;
int to = xy[j].second, from = xy[j - 1].second;
G[from][0].PB({cost, {to, 0}});
G[to][0].PB({cost, {from, 0}});
}
}
for (int i = 0; i < k; ++i) {
for (int j = i + 1; j < k; ++j) {
if (yx[i].first.first != yx[j].first.first) {
i = j - 1;
break;
}
int cost = yx[j].first.second - yx[j - 1].first.second;
int to = yx[j].second, from = yx[j - 1].second;
G[from][1].PB({cost, {to, 1}});
G[to][1].PB({cost, {from, 1}});
}
}
cout << Dijkstra() << endl;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p00528 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
#define INF 1e+18
using namespace std;
typedef pair<int, int> P;
signed main() {
int m, n, k;
bool start = false;
int d[200002][2]; // 0:tate 1:yoko
vector<P> G[200002][2];
vector<P> tate[100000], yoko[100000];
cin >> m >> n >> k;
for (int i = 0; i < k + 2; i++) {
d[i][0] = INF;
d[i][1] = INF;
}
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
if (a == 1 && b == 1)
start = true;
tate[b - 1].push_back(P(a - 1, i + 1));
yoko[a - 1].push_back(P(b - 1, i + 1));
}
tate[0].push_back(P(0, 0));
yoko[0].push_back(P(0, 0));
tate[n - 1].push_back(P(m - 1, k + 1));
yoko[m - 1].push_back(P(n - 1, k + 1));
for (int i = 0; i < n; i++) {
if (tate[i].size() <= 1)
continue;
sort(tate[i].begin(), tate[i].end());
for (int j = 0; j < tate[i].size() - 1; j++) {
G[tate[i][j + 1].second][1].push_back(
P(tate[i][j + 1].first - tate[i][j].first, tate[i][j].second));
G[tate[i][j].second][1].push_back(
P(tate[i][j + 1].first - tate[i][j].first, tate[i][j + 1].second));
}
}
for (int i = 0; i < m; i++) {
if (yoko[i].size() <= 1)
continue;
sort(yoko[i].begin(), yoko[i].end());
for (int j = 0; j < yoko[i].size() - 1; j++) {
G[yoko[i][j + 1].second][0].push_back(
P(yoko[i][j + 1].first - yoko[i][j].first, yoko[i][j].second));
G[yoko[i][j].second][0].push_back(
P(yoko[i][j + 1].first - yoko[i][j].first, yoko[i][j + 1].second));
}
}
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, 0));
d[0][0] = 0;
if (start) {
que.push(P(1, k + 2));
d[0][1] = 1;
}
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second % (k + 2), flag = p.second / (k + 2),
_flag = (flag + 1) % 2;
if (d[v][flag] < p.first)
continue;
for (int i = 0; i < G[v][flag].size(); i++) {
P u = G[v][flag][i];
if (d[u.second][flag] > d[v][flag] + u.first) {
d[u.second][flag] = d[v][flag] + u.first;
que.push(P(d[u.second][flag], u.second + flag * (k + 2)));
}
if (d[u.second][_flag] > d[v][flag] + u.first + 1) {
d[u.second][_flag] = d[v][flag] + u.first + 1;
que.push(P(d[u.second][_flag], u.second + _flag * (k + 2)));
}
}
for (int i = 0; i < G[v][_flag].size(); i++) {
P u = G[v][_flag][i];
if (d[u.second][_flag] > d[v][_flag] + u.first) {
d[u.second][_flag] = d[v][_flag] + u.first;
que.push(P(d[u.second][_flag], u.second + _flag * (k + 2)));
}
if (d[u.second][flag] > d[v][_flag] + u.first + 1) {
d[u.second][flag] = d[v][_flag] + u.first + 1;
que.push(P(d[u.second][flag], u.second + flag * (k + 2)));
}
}
}
if (min(d[k + 1][0], d[k + 1][1]) == INF)
cout << -1 << endl;
else
cout << min(d[k + 1][0], d[k + 1][1]) << endl;
/*for(int i = 0;i <= k + 1;i++){
cout << i << ")" << endl;
for(int j = 0;j < G[i][0].size();j++){
cout << G[i][0][j].second << " ";
}
cout << endl;
for(int j = 0;j < G[i][1].size();j++){
cout << G[i][1][j].second << " ";
}
cout << endl;
}*/
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
#define INF 1e+18
using namespace std;
typedef pair<int, int> P;
signed main() {
int m, n, k;
bool start = false;
static int d[200002][2]; // 0:tate 1:yoko
static vector<P> G[200002][2];
static vector<P> tate[100000], yoko[100000];
cin >> m >> n >> k;
for (int i = 0; i < k + 2; i++) {
d[i][0] = INF;
d[i][1] = INF;
}
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
if (a == 1 && b == 1)
start = true;
tate[b - 1].push_back(P(a - 1, i + 1));
yoko[a - 1].push_back(P(b - 1, i + 1));
}
tate[0].push_back(P(0, 0));
yoko[0].push_back(P(0, 0));
tate[n - 1].push_back(P(m - 1, k + 1));
yoko[m - 1].push_back(P(n - 1, k + 1));
for (int i = 0; i < n; i++) {
if (tate[i].size() <= 1)
continue;
sort(tate[i].begin(), tate[i].end());
for (int j = 0; j < tate[i].size() - 1; j++) {
G[tate[i][j + 1].second][1].push_back(
P(tate[i][j + 1].first - tate[i][j].first, tate[i][j].second));
G[tate[i][j].second][1].push_back(
P(tate[i][j + 1].first - tate[i][j].first, tate[i][j + 1].second));
}
}
for (int i = 0; i < m; i++) {
if (yoko[i].size() <= 1)
continue;
sort(yoko[i].begin(), yoko[i].end());
for (int j = 0; j < yoko[i].size() - 1; j++) {
G[yoko[i][j + 1].second][0].push_back(
P(yoko[i][j + 1].first - yoko[i][j].first, yoko[i][j].second));
G[yoko[i][j].second][0].push_back(
P(yoko[i][j + 1].first - yoko[i][j].first, yoko[i][j + 1].second));
}
}
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(0, 0));
d[0][0] = 0;
if (start) {
que.push(P(1, k + 2));
d[0][1] = 1;
}
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second % (k + 2), flag = p.second / (k + 2),
_flag = (flag + 1) % 2;
if (d[v][flag] < p.first)
continue;
for (int i = 0; i < G[v][flag].size(); i++) {
P u = G[v][flag][i];
if (d[u.second][flag] > d[v][flag] + u.first) {
d[u.second][flag] = d[v][flag] + u.first;
que.push(P(d[u.second][flag], u.second + flag * (k + 2)));
}
if (d[u.second][_flag] > d[v][flag] + u.first + 1) {
d[u.second][_flag] = d[v][flag] + u.first + 1;
que.push(P(d[u.second][_flag], u.second + _flag * (k + 2)));
}
}
for (int i = 0; i < G[v][_flag].size(); i++) {
P u = G[v][_flag][i];
if (d[u.second][_flag] > d[v][_flag] + u.first) {
d[u.second][_flag] = d[v][_flag] + u.first;
que.push(P(d[u.second][_flag], u.second + _flag * (k + 2)));
}
if (d[u.second][flag] > d[v][_flag] + u.first + 1) {
d[u.second][flag] = d[v][_flag] + u.first + 1;
que.push(P(d[u.second][flag], u.second + flag * (k + 2)));
}
}
}
if (min(d[k + 1][0], d[k + 1][1]) == INF)
cout << -1 << endl;
else
cout << min(d[k + 1][0], d[k + 1][1]) << endl;
/*for(int i = 0;i <= k + 1;i++){
cout << i << ")" << endl;
for(int j = 0;j < G[i][0].size();j++){
cout << G[i][0][j].second << " ";
}
cout << endl;
for(int j = 0;j < G[i][1].size();j++){
cout << G[i][1][j].second << " ";
}
cout << endl;
}*/
return 0;
} | replace | 13 | 16 | 13 | 16 | -11 | |
p00528 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
struct Point {
int x, y, id;
};
bool linkx(Point &p1, Point &p2) {
return p1.y == p2.y ? p1.x < p2.x : p1.y < p2.y;
}
bool linky(Point &p1, Point &p2) {
return p1.x == p2.x ? p1.y < p2.y : p1.x < p2.x;
}
#define all(a) a.begin(), a.end()
typedef pair<ll, int> edge; // cost,to id
#define INF LLONG_MAX
#define SIZE 200010
int main(void) {
int M, N, K, X, Y;
cin >> M >> N >> K;
vector<edge> g[SIZE];
vector<Point> vp(K + 1);
vector<ll> dp(SIZE);
rep(i, K) {
cin >> X >> Y;
vp[i] = {X, Y, i};
}
vp[K] = {M, N, K};
// push switch
rep(i, K) {
g[vp[i].id * 2].push_back({1, vp[i].id * 2 + 1});
g[vp[i].id * 2 + 1].push_back({1, vp[i].id * 2});
}
// linkx
sort(all(vp), linkx);
rep(i, K) {
if (vp[i].y == vp[i + 1].y) {
Point p1 = vp[i], p2 = vp[i + 1];
int cost = abs(p1.x - p2.x);
g[p1.id * 2].push_back({cost, p2.id * 2});
g[p2.id * 2].push_back({cost, p1.id * 2});
}
}
// linky
sort(all(vp), linky);
if (vp[0].x != 1) {
cout << -1 << endl;
return 0;
}
rep(i, K) {
if (vp[i].x == vp[i + 1].x) {
Point p1 = vp[i], p2 = vp[i + 1];
int cost = abs(p1.y - p2.y);
g[p1.id * 2 + 1].push_back({cost, p2.id * 2 + 1});
g[p2.id * 2 + 1].push_back({cost, p1.id * 2 + 1});
}
}
rep(i, K * 2 + 2) dp[i] = INF;
priority_queue<edge, vector<edge>, greater<edge>> q;
dp[vp[0].id * 2 + 1] = vp[0].y - 1;
q.push({dp[vp[0].id * 2 + 1], vp[0].id * 2 + 1});
while (q.size()) {
edge u = q.top();
q.pop();
int id = u.second;
if (id >= 2 * K) {
cout << u.first << endl;
return 0;
}
if (dp[id] < u.first)
continue;
for (auto e : g[id]) {
if (dp[e.second] > dp[id] + e.first) {
dp[e.second] = dp[id] + e.first;
q.push({dp[e.second], e.second});
}
}
}
cout << -1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
struct Point {
int x, y, id;
};
bool linkx(Point &p1, Point &p2) {
return p1.y == p2.y ? p1.x < p2.x : p1.y < p2.y;
}
bool linky(Point &p1, Point &p2) {
return p1.x == p2.x ? p1.y < p2.y : p1.x < p2.x;
}
#define all(a) a.begin(), a.end()
typedef pair<ll, int> edge; // cost,to id
#define INF LLONG_MAX
#define SIZE 400010
int main(void) {
int M, N, K, X, Y;
cin >> M >> N >> K;
vector<edge> g[SIZE];
vector<Point> vp(K + 1);
vector<ll> dp(SIZE);
rep(i, K) {
cin >> X >> Y;
vp[i] = {X, Y, i};
}
vp[K] = {M, N, K};
// push switch
rep(i, K) {
g[vp[i].id * 2].push_back({1, vp[i].id * 2 + 1});
g[vp[i].id * 2 + 1].push_back({1, vp[i].id * 2});
}
// linkx
sort(all(vp), linkx);
rep(i, K) {
if (vp[i].y == vp[i + 1].y) {
Point p1 = vp[i], p2 = vp[i + 1];
int cost = abs(p1.x - p2.x);
g[p1.id * 2].push_back({cost, p2.id * 2});
g[p2.id * 2].push_back({cost, p1.id * 2});
}
}
// linky
sort(all(vp), linky);
if (vp[0].x != 1) {
cout << -1 << endl;
return 0;
}
rep(i, K) {
if (vp[i].x == vp[i + 1].x) {
Point p1 = vp[i], p2 = vp[i + 1];
int cost = abs(p1.y - p2.y);
g[p1.id * 2 + 1].push_back({cost, p2.id * 2 + 1});
g[p2.id * 2 + 1].push_back({cost, p1.id * 2 + 1});
}
}
rep(i, K * 2 + 2) dp[i] = INF;
priority_queue<edge, vector<edge>, greater<edge>> q;
dp[vp[0].id * 2 + 1] = vp[0].y - 1;
q.push({dp[vp[0].id * 2 + 1], vp[0].id * 2 + 1});
while (q.size()) {
edge u = q.top();
q.pop();
int id = u.second;
if (id >= 2 * K) {
cout << u.first << endl;
return 0;
}
if (dp[id] < u.first)
continue;
for (auto e : g[id]) {
if (dp[e.second] > dp[id] + e.first) {
dp[e.second] = dp[id] + e.first;
q.push({dp[e.second], e.second});
}
}
}
cout << -1 << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p00529 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string s;
bool check(int x) {
int l, r;
static bool isUseI[1000001], isUseO[1000001];
int cntI = 0;
for (r = 0; r < n; r++) {
isUseI[r] = false;
isUseO[r] = false;
}
for (r = n - 1; r >= 0; r--) {
if (cntI == x) {
break;
}
if (s[r] == 'I') {
isUseI[r] = true;
cntI++;
}
}
if (cntI < x)
return false;
l = n - 1;
for (r = n - 1; r >= 0; r--) {
if (isUseI[r]) {
bool f = false;
for (l = min(l, r); l >= 0; l--) {
if (s[l] == 'O') {
isUseO[l] = true;
l--;
f = true;
break;
}
}
if (!f)
return false;
}
}
l = n - 1;
for (r = n - 1; r >= 0; r--) {
if (isUseO[r]) {
bool f = false;
for (l = min(l, r); l >= 0; l--) {
if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
l--;
f = true;
break;
}
}
if (!f)
return false;
}
}
return true;
}
int main() {
int i;
cin >> n;
for (i = 0; i < n; i++)
cin >> s[i];
int st = 0, ed = n, medi;
while (ed - st > 1) {
medi = (st + ed) / 2;
if (check(medi)) {
st = medi;
} else {
ed = medi - 1;
}
}
medi = (st + ed) / 2;
if (check(medi + 1))
cout << medi + 1 << endl;
else
cout << medi << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string s;
bool check(int x) {
int l, r;
static bool isUseI[1000001], isUseO[1000001];
int cntI = 0;
for (r = 0; r < n; r++) {
isUseI[r] = false;
isUseO[r] = false;
}
for (r = n - 1; r >= 0; r--) {
if (cntI == x) {
break;
}
if (s[r] == 'I') {
isUseI[r] = true;
cntI++;
}
}
if (cntI < x)
return false;
l = n - 1;
for (r = n - 1; r >= 0; r--) {
if (isUseI[r]) {
bool f = false;
for (l = min(l, r); l >= 0; l--) {
if (s[l] == 'O') {
isUseO[l] = true;
l--;
f = true;
break;
}
}
if (!f)
return false;
}
}
l = n - 1;
for (r = n - 1; r >= 0; r--) {
if (isUseO[r]) {
bool f = false;
for (l = min(l, r); l >= 0; l--) {
if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
l--;
f = true;
break;
}
}
if (!f)
return false;
}
}
return true;
}
int main() {
int i;
cin >> n;
cin >> s;
int st = 0, ed = n, medi;
while (ed - st > 1) {
medi = (st + ed) / 2;
if (check(medi)) {
st = medi;
} else {
ed = medi - 1;
}
}
medi = (st + ed) / 2;
if (check(medi + 1))
cout << medi + 1 << endl;
else
cout << medi << endl;
return 0;
} | replace | 69 | 71 | 69 | 70 | 0 | |
p00529 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
// cout << "h " << h << endl;
int tk = S.size() - 1;
for (int i = 0; i < h; i++) {
tk = min(tk, OI[i].second);
}
int res = 0;
int H = h;
h--;
for (int i = 0; i < (int)S.size() && h > -1; i++) {
if ((i < tk && S[i] == 'I') || S[i] == 'J') {
// cout << i << " -> ";
while (h > -1 && i >= OI[h].first)
h--;
if (h > -1) {
// cout << OI[h].first << " -> " << OI[h].second << endl;
res++;
h--;
}
}
}
// cout << H << " "<< res << endl;
return res == H;
}
int N;
int main() {
cin >> N;
cin >> S;
int st = 0, ed = (int)S.size() / 3;
int oc = S.size() - 1;
for (int i = (int)S.size() - 1; i > -1; i--) {
oc = min(oc, i - 1);
if (S[i] == 'I') {
P p = P(-1, i);
for (; oc > -1; oc--) {
if (S[oc] == 'O') {
p.first = oc;
oc--;
break;
}
}
if (p.first != -1)
OI.push_back(p);
}
}
// for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
// endl;
int res = 0;
while (st <= ed) {
int h = (st + ed) / 2;
if (check(h)) {
st = h + 1;
res = h;
} else {
ed = h - 1;
}
}
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
// cout << "h " << h << endl;
int tk = S.size() - 1;
if (OI.size() < h)
return false;
for (int i = 0; i < h; i++) {
tk = min(tk, OI[i].second);
}
int res = 0;
int H = h;
h--;
for (int i = 0; i < (int)S.size() && h > -1; i++) {
if ((i < tk && S[i] == 'I') || S[i] == 'J') {
// cout << i << " -> ";
while (h > -1 && i >= OI[h].first)
h--;
if (h > -1) {
// cout << OI[h].first << " -> " << OI[h].second << endl;
res++;
h--;
}
}
}
// cout << H << " "<< res << endl;
return res == H;
}
int N;
int main() {
cin >> N;
cin >> S;
int st = 0, ed = (int)S.size() / 3;
int oc = S.size() - 1;
for (int i = (int)S.size() - 1; i > -1; i--) {
oc = min(oc, i - 1);
if (S[i] == 'I') {
P p = P(-1, i);
for (; oc > -1; oc--) {
if (S[oc] == 'O') {
p.first = oc;
oc--;
break;
}
}
if (p.first != -1)
OI.push_back(p);
}
}
// for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
// endl;
int res = 0;
while (st <= ed) {
int h = (st + ed) / 2;
if (check(h)) {
st = h + 1;
res = h;
} else {
ed = h - 1;
}
}
cout << res << endl;
} | insert | 8 | 8 | 8 | 10 | 0 | |
p00530 | C++ | Time Limit Exceeded | #include <algorithm>
#include <stdio.h>
using namespace std;
int b[110000];
int z[110000];
long long bit[110000];
long long sum(int a, int b) {
if (a)
return sum(0, b) - sum(0, a - 1);
long long ret = 0;
for (; b >= 0; b = (b & (b + 1)) - 1)
ret += bit[b];
return ret;
}
void add(int a, int b) {
for (; a < 110000; a |= a + 1)
bit[a] += b;
}
int segtree[262144];
int lx[110000];
int ly[110000];
int rx[110000];
int ry[110000];
void add2(int a, int b, int c, int d, int e, int f) {
if (d < a || b < c)
return;
if (c <= a && b <= d) {
segtree[e] += f;
} else {
add2(a, (a + b) / 2, c, d, e * 2, f);
add2((a + b) / 2 + 1, b, c, d, e * 2 + 1, f);
}
if (e > 1 && ((segtree[e] != 0 && segtree[e ^ 1] != 0) || segtree[e] > 0 ||
segtree[e ^ 1] > 0)) {
int t = max(segtree[e], segtree[e ^ 1]);
segtree[e / 2] += t;
segtree[e ^ 1] -= t;
segtree[e] -= t;
}
}
int query(int a, int b, int c, int d, int e, int f) {
if (d < a || b < c)
return -99999999;
if (c <= a && b <= d)
return segtree[e] + f;
return max(query(a, (a + b) / 2, c, d, e * 2, f + segtree[e]),
query((a + b) / 2 + 1, b, c, d, e * 2 + 1, f + segtree[e]));
}
int L[110000];
int R[110000];
pair<int, int> yp[110000];
int main() {
int a;
scanf("%d", &a);
for (int i = 0; i < a; i++)
scanf("%d", b + i);
bool chk = true;
bool same = false;
for (int i = 0; i < a - 1; i++)
if (b[i] < b[i - 1])
chk = false;
for (int i = 0; i < a - 1; i++)
if (b[i] == b[i - 1])
same = true;
if (chk) {
if (same)
printf("0\n");
else
printf("1\n");
return 0;
}
for (int i = 0; i < a; i++) {
z[i] = b[i];
yp[i] = make_pair(b[i], i);
}
std::sort(yp, yp + a);
std::sort(z, z + a);
long long ans = 0;
for (int i = 0; i < a; i++) {
int at = lower_bound(z, z + a, b[i]) - z;
if (at < a - 1)
ans += sum(at + 1, a - 1);
add(at, 1);
}
int tmp = 0;
int ls = 0;
int rs = 0;
for (int i = 0; i < a; i++) {
if (tmp < b[i]) {
L[i] = 1;
tmp = b[i];
lx[ls] = i;
ly[ls++] = tmp;
}
}
tmp = 1000000007;
for (int i = a - 1; i >= 0; i--) {
if (tmp > b[i] && L[i] == 0) {
R[i] = 1;
tmp = b[i];
}
}
for (int i = 0; i < a; i++) {
if (R[i]) {
rx[rs] = i;
ry[rs++] = b[i];
}
}
int ret = 0;
int rm = 0;
int ypat = 0;
for (int i = 0; i < a; i++) {
if (R[i]) {
int nx = b[i];
int tmp = 0;
while (ypat < a && yp[ypat].first < nx) {
int ind = yp[ypat].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
if (b[ind] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
ypat++;
}
int cp = ypat;
while (cp < a && yp[cp].first == nx) {
int ind = yp[cp].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
ypat++;
}
int at = lower_bound(lx, lx + ls, i) - lx - 1;
int at2 = upper_bound(ly, ly + ls, b[i]) - ly;
if (at2 <= at) {
tmp = query(0, 131071, at2, at, 1, 0);
ret = max(ret, tmp);
}
while (ypat < a && yp[ypat].first == nx) {
int ind = yp[ypat].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (b[ind] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
ypat++;
}
} else if (!L[i]) {
int to = lower_bound(ly, ly + ls, b[i]) - ly;
int rim = lower_bound(lx, lx + ls, i) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, 1);
if (b[i] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, 1);
}
}
// printf("%lld %d\n",ans,ret);
printf("%lld\n", ans - ret - 1);
} | #include <algorithm>
#include <stdio.h>
using namespace std;
int b[110000];
int z[110000];
long long bit[110000];
long long sum(int a, int b) {
if (a)
return sum(0, b) - sum(0, a - 1);
long long ret = 0;
for (; b >= 0; b = (b & (b + 1)) - 1)
ret += bit[b];
return ret;
}
void add(int a, int b) {
for (; a < 110000; a |= a + 1)
bit[a] += b;
}
int segtree[262144];
int lx[110000];
int ly[110000];
int rx[110000];
int ry[110000];
void add2(int a, int b, int c, int d, int e, int f) {
if (d < a || b < c)
return;
if (c <= a && b <= d) {
segtree[e] += f;
} else {
add2(a, (a + b) / 2, c, d, e * 2, f);
add2((a + b) / 2 + 1, b, c, d, e * 2 + 1, f);
}
if (e > 1 && ((segtree[e] != 0 && segtree[e ^ 1] != 0) || segtree[e] > 0 ||
segtree[e ^ 1] > 0)) {
int t = max(segtree[e], segtree[e ^ 1]);
segtree[e / 2] += t;
segtree[e ^ 1] -= t;
segtree[e] -= t;
}
}
int query(int a, int b, int c, int d, int e, int f) {
if (d < a || b < c)
return -99999999;
if (c <= a && b <= d)
return segtree[e] + f;
return max(query(a, (a + b) / 2, c, d, e * 2, f + segtree[e]),
query((a + b) / 2 + 1, b, c, d, e * 2 + 1, f + segtree[e]));
}
int L[110000];
int R[110000];
pair<int, int> yp[110000];
int main() {
int a;
scanf("%d", &a);
for (int i = 0; i < a; i++)
scanf("%d", b + i);
bool chk = true;
bool same = false;
for (int i = 0; i < a - 1; i++)
if (b[i] < b[i - 1])
chk = false;
for (int i = 0; i < a - 1; i++)
if (b[i] == b[i - 1])
same = true;
if (chk) {
if (same)
printf("0\n");
else
printf("1\n");
return 0;
}
for (int i = 0; i < a; i++) {
z[i] = b[i];
yp[i] = make_pair(b[i], i);
}
std::sort(yp, yp + a);
std::sort(z, z + a);
long long ans = 0;
for (int i = 0; i < a; i++) {
int at = lower_bound(z, z + a, b[i]) - z;
if (at < a - 1)
ans += sum(at + 1, a - 1);
add(at, 1);
}
int tmp = 0;
int ls = 0;
int rs = 0;
for (int i = 0; i < a; i++) {
if (tmp < b[i]) {
L[i] = 1;
tmp = b[i];
lx[ls] = i;
ly[ls++] = tmp;
}
}
tmp = 1000000007;
for (int i = a - 1; i >= 0; i--) {
if (tmp > b[i] && L[i] == 0) {
R[i] = 1;
tmp = b[i];
}
}
for (int i = 0; i < a; i++) {
if (R[i]) {
rx[rs] = i;
ry[rs++] = b[i];
}
}
int ret = 0;
int rm = 0;
int ypat = 0;
for (int i = 0; i < a; i++) {
if (R[i]) {
int nx = b[i];
int tmp = 0;
while (ypat < a && yp[ypat].first < nx) {
int ind = yp[ypat].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
if (b[ind] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
ypat++;
}
int cp = ypat;
while (cp < a && yp[cp].first == nx) {
int ind = yp[cp].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
cp++;
}
int at = lower_bound(lx, lx + ls, i) - lx - 1;
int at2 = upper_bound(ly, ly + ls, b[i]) - ly;
if (at2 <= at) {
tmp = query(0, 131071, at2, at, 1, 0);
ret = max(ret, tmp);
}
while (ypat < a && yp[ypat].first == nx) {
int ind = yp[ypat].second;
if (!L[ind] && !R[ind]) {
int to = lower_bound(ly, ly + ls, b[ind]) - ly;
int rim = lower_bound(lx, lx + ls, ind) - lx - 1;
if (b[ind] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, -1);
}
ypat++;
}
} else if (!L[i]) {
int to = lower_bound(ly, ly + ls, b[i]) - ly;
int rim = lower_bound(lx, lx + ls, i) - lx - 1;
if (to <= rim)
add2(0, 131071, to, rim, 1, 1);
if (b[i] == ly[to])
to++;
if (to <= rim)
add2(0, 131071, to, rim, 1, 1);
}
}
// printf("%lld %d\n",ans,ret);
printf("%lld\n", ans - ret - 1);
} | replace | 138 | 139 | 138 | 139 | TLE | |
p00531 | C++ | Runtime Error | #include <stdio.h>
int main() {
int a, b, c, d, p, x, y;
scanf("%d %d %d %d %d", a, b, c, d, p);
x = a * p;
if (p <= c) {
y = b;
} else {
y = (p - c) * d + b;
}
if (x <= y) {
printf("%d\n", x);
} else {
printf("%d\n", y);
}
return 0;
} | #include <stdio.h>
int main() {
int a, b, c, d, p, x, y;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &p);
x = a * p;
if (p <= c) {
y = b;
} else {
y = (p - c) * d + b;
}
if (x <= y) {
printf("%d\n", x);
} else {
printf("%d\n", y);
}
return 0;
} | replace | 3 | 4 | 3 | 4 | -11 | |
p00532 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct before_main {
before_main() {
cin.tie(0);
ios::sync_with_stdio(false);
}
} before_main;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
typedef long long ll;
int const inf = 1 << 29;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N);
rep(i, M) cin >> A[i], A[i]--;
vector<int> score(N);
rep(i, M) {
int X = 0;
rep(j, N) {
int x;
cin >> x;
x--;
if (x == A[i])
score[j]++;
X += x != A[i];
}
score[A[i]] += X;
}
rep(i, N) { cout << score[i] << endl; }
return 0;
} | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct before_main {
before_main() {
cin.tie(0);
ios::sync_with_stdio(false);
}
} before_main;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
typedef long long ll;
int const inf = 1 << 29;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(M);
rep(i, M) cin >> A[i], A[i]--;
vector<int> score(N);
rep(i, M) {
int X = 0;
rep(j, N) {
int x;
cin >> x;
x--;
if (x == A[i])
score[j]++;
X += x != A[i];
}
score[A[i]] += X;
}
rep(i, N) { cout << score[i] << endl; }
return 0;
} | replace | 52 | 53 | 52 | 53 | 0 | |
p00532 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
int main() {
int n, m;
cin >> n >> m;
vi player(n);
rep(i, m) {
cin >> player[i];
player[i]--;
}
vi score(n, 0);
rep(i, m) {
int sum = 0;
rep(j, n) {
int a;
cin >> a;
a--;
if (a != player[i])
sum++;
else
score[j]++;
}
score[player[i]] += sum;
}
rep(i, n) cout << score[i] << endl;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
int main() {
int n, m;
cin >> n >> m;
vi player(m);
rep(i, m) {
cin >> player[i];
player[i]--;
}
vi score(n, 0);
rep(i, m) {
int sum = 0;
rep(j, n) {
int a;
cin >> a;
a--;
if (a != player[i])
sum++;
else
score[j]++;
}
score[player[i]] += sum;
}
rep(i, n) cout << score[i] << endl;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p00532 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int a, s, d[10001][10001], g[10001], h[10001], i, j;
scanf("%d", &a);
scanf("%d", &s);
for (i = 1; i <= s; i++) {
scanf("%d", &g[i]);
}
for (i = 1; i <= s; i++) {
for (j = 1; j <= a; j++) {
scanf("%d", &d[i][j]);
}
}
for (i = 1; i <= s; i++) {
for (j = 1; j <= a; j++) {
if (g[i] == d[i][j]) {
h[j]++;
} else {
h[g[i]]++;
}
}
}
for (i = 1; i <= a; i++) {
printf("%d\n", h[i]);
}
return 0;
} | #include <stdio.h>
int main(void) {
int a, s, d[1001][1001], g[1001], h[1001], i, j;
scanf("%d", &a);
scanf("%d", &s);
for (i = 1; i <= s; i++) {
scanf("%d", &g[i]);
}
for (i = 1; i <= s; i++) {
for (j = 1; j <= a; j++) {
scanf("%d", &d[i][j]);
}
}
for (i = 1; i <= s; i++) {
for (j = 1; j <= a; j++) {
if (g[i] == d[i][j]) {
h[j]++;
} else {
h[g[i]]++;
}
}
}
for (i = 1; i <= a; i++) {
printf("%d\n", h[i]);
}
return 0;
} | replace | 2 | 3 | 2 | 3 | -11 | |
p00534 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 3000
#define MAX_M 3000
int main() {
int dp[MAX_N + 1][MAX_M + 1];
int n, m, s, x[MAX_N], y[MAX_M];
memset(dp, 127, sizeof(dp));
memset(x, 127, sizeof(x));
memset(y, 127, sizeof(y));
cin >> m >> n;
for (int i = 1; i <= m; i++) {
cin >> x[i];
}
for (int i = 1; i <= n; i++) {
cin >> y[i];
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + x[j + 1] * y[i + 1]);
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[n][m] << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 1100
#define MAX_M 1100
int main() {
int dp[MAX_N + 1][MAX_M + 1];
int n, m, s, x[MAX_N], y[MAX_M];
memset(dp, 127, sizeof(dp));
memset(x, 127, sizeof(x));
memset(y, 127, sizeof(y));
cin >> m >> n;
for (int i = 1; i <= m; i++) {
cin >> x[i];
}
for (int i = 1; i <= n; i++) {
cin >> y[i];
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + x[j + 1] * y[i + 1]);
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
}
}
cout << dp[n][m] << endl;
return 0;
} | replace | 5 | 7 | 5 | 7 | -11 | |
p00534 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
int n, m, a[1001], b[1001], i, k;
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (i = 1; i <= m; i++)
scanf("%d", &b[i]);
a[0] = 0;
b[0] = 0;
for (i = 0; i <= n; i++) {
for (k = 0; k <= m; k++)
dp[i][k] = MM;
}
dp[0][0] = 0;
for (i = 0; i <= m; i++) {
for (k = 0; k <= n; k++) {
if (dp[i + 1][k] > dp[i][k])
dp[i + 1][k] = dp[i][k];
if (k + 1 <= n && dp[i][k] != MM)
dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
}
}
printf("%d\n", dp[m][n]);
return 0;
} | #include <algorithm>
#include <functional>
#include <stdio.h>
#define MM 2100000000
using namespace std;
long long int dp[1001][1001];
int main(void) {
int n, m, a[1001], b[1001], i, k;
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (i = 1; i <= m; i++)
scanf("%d", &b[i]);
a[0] = 0;
b[0] = 0;
for (i = 0; i <= n; i++) {
for (k = 0; k <= m; k++)
dp[i][k] = MM;
}
dp[0][0] = 0;
for (i = 0; i < m; i++) {
for (k = 0; k <= n; k++) {
if (dp[i + 1][k] > dp[i][k])
dp[i + 1][k] = dp[i][k];
if (k + 1 <= n && dp[i][k] != MM)
dp[i + 1][k + 1] = dp[i][k] + a[k + 1] * b[i + 1];
}
}
printf("%d\n", dp[m][n]);
return 0;
} | replace | 22 | 23 | 22 | 23 | 0 | |
p00534 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int N, M;
int h[100][100] = {0}, l[1500] = {0}, t[1500] = {0};
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> l[i];
}
for (int j = 0; j < M; j++) {
cin >> t[j];
}
for (int j = 0; j < N; j++) {
for (int i = j; i < j + M - N + 1; i++) {
if (j == 0) {
h[j][i] = l[j] * t[i];
if (h[j][i] >= h[j][i - 1] && i != 0) {
h[j][i] = h[j][i - 1];
}
} else {
h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
if (h[j][i] >= h[j][i - 1] && i != j) {
h[j][i] = h[j][i - 1];
}
}
}
}
cout << h[N - 1][M - 1] << endl;
return 0;
} | #include <iostream>
using namespace std;
int main() {
int N, M;
int h[1500][1500] = {0}, l[1500] = {0}, t[1500] = {0};
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> l[i];
}
for (int j = 0; j < M; j++) {
cin >> t[j];
}
for (int j = 0; j < N; j++) {
for (int i = j; i < j + M - N + 1; i++) {
if (j == 0) {
h[j][i] = l[j] * t[i];
if (h[j][i] >= h[j][i - 1] && i != 0) {
h[j][i] = h[j][i - 1];
}
} else {
h[j][i] = h[j - 1][i - 1] + l[j] * t[i];
if (h[j][i] >= h[j][i - 1] && i != j) {
h[j][i] = h[j][i - 1];
}
}
}
}
cout << h[N - 1][M - 1] << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00534 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
int n, m, an = 1e9;
int d[1002];
int c[1002];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> d[i];
for (int i = 0; i < m; i++)
cin >> c[i];
vector<vector<int>> vec(m + 2, vector<int>(n + 1e9));
vec[0][0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vec[i + 1][j] = min(vec[i + 1][j], vec[i][j]);
vec[i + 1][j + 1] = min(vec[i + 1][j + 1], vec[i][j] + c[i] * d[j]);
}
}
for (int i = 0; i <= m; i++)
an = min(an, vec[i][n]);
cout << an << endl;
} | #include <iostream>
#include <vector>
using namespace std;
int n, m, an = 1e9;
int d[1002];
int c[1002];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> d[i];
for (int i = 0; i < m; i++)
cin >> c[i];
vector<vector<int>> vec(m + 2, vector<int>(n + 2, 1e9));
vec[0][0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vec[i + 1][j] = min(vec[i + 1][j], vec[i][j]);
vec[i + 1][j + 1] = min(vec[i + 1][j + 1], vec[i][j] + c[i] * d[j]);
}
}
for (int i = 0; i <= m; i++)
an = min(an, vec[i][n]);
cout << an << endl;
} | replace | 13 | 14 | 13 | 14 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p00534 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
int N, M;
int C[1001], D[1001];
int i, j;
cin >> N >> M;
for (i = 1; i <= N; i++) {
cin >> D[i];
}
for (i = 1; i <= M; i++) {
cin >> C[i];
}
for (i = 1; i <= M; i++) {
for (j = 0; j <= N; j++) {
dp[i][j] = MAX;
}
}
dp[1][0] = 0;
dp[1][1] = D[1] * C[1];
for (i = 1; i <= M + 1; i++) {
for (j = 0; j <= N; j++) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
if (j < N) {
dp[i + 1][j + 1] =
min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
}
}
}
/*for(i=1;i<=M;i++){
for(j=0;j<=N;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
cout << dp[M][N] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MAX 1001 * 1001 * 1001
int dp[1001][1001];
int main(void) {
int N, M;
int C[1001], D[1001];
int i, j;
cin >> N >> M;
for (i = 1; i <= N; i++) {
cin >> D[i];
}
for (i = 1; i <= M; i++) {
cin >> C[i];
}
for (i = 1; i <= M; i++) {
for (j = 0; j <= N; j++) {
dp[i][j] = MAX;
}
}
dp[1][0] = 0;
dp[1][1] = D[1] * C[1];
for (i = 1; i < M; i++) {
for (j = 0; j <= N; j++) {
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
if (j < N) {
dp[i + 1][j + 1] =
min(dp[i + 1][j + 1], dp[i][j] + C[i + 1] * D[j + 1]);
}
}
}
/*for(i=1;i<=M;i++){
for(j=0;j<=N;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
cout << dp[M][N] << endl;
return 0;
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p00535 | C++ | Runtime Error | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-8;
int INF = 2e9;
char fld[55][55];
int cnt[55][55];
bool used[55][55];
int main() {
int h, w, turn = 0;
cin >> h >> w;
REP(i, h) REP(j, w) cin >> fld[j][i];
memset(used, false, sizeof(used));
queue<P> Q;
REP(i, h) REP(j, w) {
if (fld[j][i] == '.')
continue;
for (int x = -1; x <= 1; x++)
for (int y = -1; y <= 1; y++)
if (fld[j + x][i + y] == '.')
cnt[j][i]++;
if (cnt[j][i] >= (fld[j][i] - '0')) {
Q.push(P(j, i));
used[j][i] = true;
}
}
if (Q.size())
turn++;
else {
cout << 0 << endl;
return 0;
}
bool update = true;
while (update) {
int ql = Q.size();
REP(i, ql) {
P p = Q.front();
Q.pop();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (fld[p.first + x][p.second + y] != '.') {
cnt[p.first + x][p.second + y]++;
if (cnt[p.first + x][p.second + y] >=
(fld[p.first + x][p.second + y] - '0') &&
!used[p.first + x][p.second + y]) {
Q.push(P(p.first + x, p.second + y));
used[p.first + x][p.second + y] = true;
}
}
}
}
}
if (!Q.size()) {
cout << turn << endl;
return 0;
}
turn++;
}
} | #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-8;
int INF = 2e9;
char fld[1050][1050];
int cnt[1050][1050];
bool used[1050][1050];
int main() {
int h, w, turn = 0;
cin >> h >> w;
REP(i, h) REP(j, w) cin >> fld[j][i];
memset(used, false, sizeof(used));
queue<P> Q;
REP(i, h) REP(j, w) {
if (fld[j][i] == '.')
continue;
for (int x = -1; x <= 1; x++)
for (int y = -1; y <= 1; y++)
if (fld[j + x][i + y] == '.')
cnt[j][i]++;
if (cnt[j][i] >= (fld[j][i] - '0')) {
Q.push(P(j, i));
used[j][i] = true;
}
}
if (Q.size())
turn++;
else {
cout << 0 << endl;
return 0;
}
bool update = true;
while (update) {
int ql = Q.size();
REP(i, ql) {
P p = Q.front();
Q.pop();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (fld[p.first + x][p.second + y] != '.') {
cnt[p.first + x][p.second + y]++;
if (cnt[p.first + x][p.second + y] >=
(fld[p.first + x][p.second + y] - '0') &&
!used[p.first + x][p.second + y]) {
Q.push(P(p.first + x, p.second + y));
used[p.first + x][p.second + y] = true;
}
}
}
}
}
if (!Q.size()) {
cout << turn << endl;
return 0;
}
turn++;
}
} | replace | 12 | 15 | 12 | 15 | 0 | |
p00535 | C++ | Runtime Error | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PA;
typedef pair<PA, int> PPA;
int main() {
int h, w;
char masu[50][50];
int suji[50][50] = {};
queue<PPA> que;
PA p;
int sum = 0;
cin >> h >> w;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
cin >> masu[i][j];
if (masu[i][j] == '.') {
for (int k = -1; k <= 1; k++)
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
int y = i + k;
int x = j + l;
if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
continue;
suji[y][x]++;
}
}
}
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (suji[i][j] >= masu[i][j] - '0' && masu[i][j] != '.') {
que.push(PPA(PA(i, j), 1));
}
}
queue<PA> queb;
while (!que.empty()) {
p = que.front().first;
if (sum != que.front().second) {
while (!queb.empty()) {
masu[queb.front().first][queb.front().second] = '.';
queb.pop();
}
sum++;
}
que.pop();
queb.push(p);
for (int k = -1; k <= 1; k++)
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
int y = p.first + k;
int x = p.second + l;
if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
continue;
suji[y][x]++;
if (suji[y][x] == masu[y][x] - '0') {
que.push(PPA(PA(y, x), sum + 1));
}
}
}
cout << sum << endl;
return (0);
} | #include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PA;
typedef pair<PA, int> PPA;
int main() {
int h, w;
char masu[1001][1001];
int suji[1001][1001] = {};
queue<PPA> que;
PA p;
int sum = 0;
cin >> h >> w;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
cin >> masu[i][j];
if (masu[i][j] == '.') {
for (int k = -1; k <= 1; k++)
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
int y = i + k;
int x = j + l;
if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
continue;
suji[y][x]++;
}
}
}
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (suji[i][j] >= masu[i][j] - '0' && masu[i][j] != '.') {
que.push(PPA(PA(i, j), 1));
}
}
queue<PA> queb;
while (!que.empty()) {
p = que.front().first;
if (sum != que.front().second) {
while (!queb.empty()) {
masu[queb.front().first][queb.front().second] = '.';
queb.pop();
}
sum++;
}
que.pop();
queb.push(p);
for (int k = -1; k <= 1; k++)
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0)
continue;
int y = p.first + k;
int x = p.second + l;
if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
continue;
suji[y][x]++;
if (suji[y][x] == masu[y][x] - '0') {
que.push(PPA(PA(y, x), sum + 1));
}
}
}
cout << sum << endl;
return (0);
} | replace | 7 | 9 | 7 | 9 | 0 | |
p00535 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;
void fanc(PP p) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (!i && !j)
continue;
if (p.F.F + i < h && p.F.F + i < h && p.F.S + j < w && p.F.S + j < w) {
cdt[p.F.F + i][p.F.S + j]++;
if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
}
}
}
}
}
void bfs() {
while (!que.empty()) {
fanc(MP(que.front().F, que.front().S + 1));
ans = max(ans, que.front().S);
que.pop();
}
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '.') {
fanc(MP(MP(i, j), 1));
}
}
}
bfs();
cout << ans << endl;
} | #include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> PP;
char c[1000][1000];
bool flag[1000][1000];
int w, h, ans;
int cdt[1000][1000];
queue<PP> que, que2;
void fanc(PP p) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (!i && !j)
continue;
if (p.F.F + i < h && p.F.F + i >= 0 && p.F.S + j < w && p.F.S + j >= 0) {
cdt[p.F.F + i][p.F.S + j]++;
if (cdt[p.F.F + i][p.F.S + j] == c[p.F.F + i][p.F.S + j] - '0') {
que.push(MP(MP(p.F.F + i, p.F.S + j), p.S));
}
}
}
}
}
void bfs() {
while (!que.empty()) {
fanc(MP(que.front().F, que.front().S + 1));
ans = max(ans, que.front().S);
que.pop();
}
}
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '.') {
fanc(MP(MP(i, j), 1));
}
}
}
bfs();
cout << ans << endl;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p00535 | C++ | Time Limit Exceeded |
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
int main() {
int h, w;
cin >> h >> w;
vector<int> area(h * w);
vector<int> ch(h * w, 0);
queue<pair<int, int>> lis;
rep(i, h) {
rep(j, w) {
char hoge;
cin >> hoge;
if (hoge == '.') {
area[i * w + j] = -1;
for (int k = -1; k < 2; k++) {
for (int l = -1; l < 2; l++) {
if (k == 0 and l == 0)
continue;
if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
ch[(i + k) * w + j + l]++;
}
}
}
} else {
area[i * w + j] = (int)hoge - (int)'0';
}
}
}
rep(i, h) {
rep(j, w) {
if (area[i * w + j] != -1 and ch[i * w + j] >= area[i * w + j]) {
lis.push({i, j});
}
}
}
int ans = 0;
int flag = 1;
while (flag > 0) {
vector<int> oldc = ch;
int si = lis.size();
flag = 0;
rep(p, si) {
if (ans == 0)
ans = 1;
int i = lis.front().first;
int j = lis.front().second;
lis.pop();
area[i * w + j] = -1;
for (int k = -1; k < 2; k++) {
for (int l = -1; l < 2; l++) {
if (k == 0 and l == 0)
continue;
if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
ch[(i + k) * w + j + l]++;
if (area[(i + k) * w + j + l] != -1 and
ch[(i + k) * w + j + l] == area[(i + k) * w + j + l]) {
lis.push({i + k, j + l});
flag++;
}
}
}
}
}
if (flag > 0)
ans++;
}
cout << ans << endl;
} |
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
int main() {
int h, w;
cin >> h >> w;
vector<int> area(h * w);
vector<int> ch(h * w, 0);
queue<pair<int, int>> lis;
rep(i, h) {
rep(j, w) {
char hoge;
cin >> hoge;
if (hoge == '.') {
area[i * w + j] = -1;
for (int k = -1; k < 2; k++) {
for (int l = -1; l < 2; l++) {
if (k == 0 and l == 0)
continue;
if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
ch[(i + k) * w + j + l]++;
}
}
}
} else {
area[i * w + j] = (int)hoge - (int)'0';
}
}
}
rep(i, h) {
rep(j, w) {
if (area[i * w + j] != -1 and ch[i * w + j] >= area[i * w + j]) {
lis.push({i, j});
}
}
}
int ans = 0;
int flag = 1;
while (flag > 0) {
int si = lis.size();
flag = 0;
rep(p, si) {
if (ans == 0)
ans = 1;
int i = lis.front().first;
int j = lis.front().second;
lis.pop();
area[i * w + j] = -1;
for (int k = -1; k < 2; k++) {
for (int l = -1; l < 2; l++) {
if (k == 0 and l == 0)
continue;
if (i + k > 0 and i + k < h - 1 and j + l > 0 and j + l < w - 1) {
ch[(i + k) * w + j + l]++;
if (area[(i + k) * w + j + l] != -1 and
ch[(i + k) * w + j + l] == area[(i + k) * w + j + l]) {
lis.push({i + k, j + l});
flag++;
}
}
}
}
}
if (flag > 0)
ans++;
}
cout << ans << endl;
} | delete | 53 | 54 | 53 | 53 | TLE | |
p00535 | C++ | Memory Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#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;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> H >> W;
FOR(i, 0, H) {
string s;
cin >> s;
vs.push_back(s);
}
FOR(i, 0, H) {
FOR(j, 0, W) {
if (isdigit(vs[i][j])) {
cas[i][j] = 1e9;
que.push(PP(P(i, j), 1));
}
}
}
int ans = 0;
while (!que.empty()) {
PP p = que.front();
que.pop();
int y = p.first.first;
int x = p.first.second;
int t = p.second;
if (cas[y][x] < t)
continue; // ?????§????´?????????????
int cnt = 0;
FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
// ?´???????
if (cnt >= vs[y][x] - '0') {
cas[y][x] = t;
ans = max(ans, t);
FOR(i, 0, 8) {
if (cas[y + vy[i]][x + vx[i]] > t) {
que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
}
}
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#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;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int H, W;
int cas[1000][1000];
vector<string> vs;
queue<PP> que; // PP(P(y, x),???i??????)?????¢?????\???
int vy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int vx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> H >> W;
FOR(i, 0, H) {
string s;
cin >> s;
vs.push_back(s);
}
FOR(i, 0, H) {
FOR(j, 0, W) {
if (isdigit(vs[i][j])) {
cas[i][j] = 1e9;
que.push(PP(P(i, j), 1));
}
}
}
int ans = 0;
while (!que.empty()) {
PP p = que.front();
que.pop();
int y = p.first.first;
int x = p.first.second;
int t = p.second;
if (cas[y][x] <= t)
continue; // ?????§????´?????????????
int cnt = 0;
FOR(i, 0, 8) if (cas[y + vy[i]][x + vx[i]] < t) cnt++;
// ?´???????
if (cnt >= vs[y][x] - '0') {
cas[y][x] = t;
ans = max(ans, t);
FOR(i, 0, 8) {
if (cas[y + vy[i]][x + vx[i]] > t) {
que.push(PP(P(y + vy[i], x + vx[i]), t + 1));
}
}
}
}
cout << ans << endl;
return 0;
} | replace | 52 | 53 | 52 | 53 | MLE | |
p00535 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
double ans = 0;
rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
return (int)ans;
}
inline string numstr(int m) {
str s = "";
while (m > 0) {
char c;
int a = m / 10;
if (a > 0)
a = m % (a * 10);
else
a = m;
c = (char)('0' + a);
s += c;
m /= 10;
}
str st = "";
for (int i = s.size() - 1; i >= 0; i--) {
st += s[i];
}
return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[50][2000];
queue<pii> p1;
queue<pii> p2;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int w, h;
cin >> h >> w;
memset(a, 0, sizeof(a));
rep(i, 0, h) {
rep(j, 0, w) {
char c;
cin >> c;
if (c == '.') {
p1.push(mp(j, i));
} else {
// cout << c-'0' << endl;
a[i][j] = c - '0';
}
}
}
int dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int ans = 0;
while (!p1.empty()) {
// if(p1.empty())break;
ans++;
while (!p1.empty()) {
pii pa = p1.front();
p1.pop();
rep(i, 0, 8) {
int x = dx[i] + pa.first;
int y = dy[i] + pa.second;
if (x < 0 || y < 0 || x >= w || y >= h)
continue;
/// a[y][x]--;
// cout << a[y][x];
if (a[y][x] == 1) {
// cout << y << x << endl;
p2.push(mp(x, y));
}
if (a[y][x] > 0)
a[y][x]--;
}
}
if (p2.empty())
break;
ans++;
while (!p2.empty()) {
pii pa = p2.front();
p2.pop();
rep(i, 0, 8) {
int x = dx[i] + pa.first;
int y = dy[i] + pa.second;
if (x < 0 || y < 0 || x >= w || y >= h)
continue;
if (a[y][x] == 1) {
p1.push(mp(x, y));
// cout << y << x << endl;
}
if (a[y][x] > 0)
a[y][x]--;
}
}
}
cout << ans - 1 << endl;
return 0;
} | #include <bits/stdc++.h>
#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
double ans = 0;
rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
return (int)ans;
}
inline string numstr(int m) {
str s = "";
while (m > 0) {
char c;
int a = m / 10;
if (a > 0)
a = m % (a * 10);
else
a = m;
c = (char)('0' + a);
s += c;
m /= 10;
}
str st = "";
for (int i = s.size() - 1; i >= 0; i--) {
st += s[i];
}
return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[2000][2000];
queue<pii> p1;
queue<pii> p2;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int w, h;
cin >> h >> w;
memset(a, 0, sizeof(a));
rep(i, 0, h) {
rep(j, 0, w) {
char c;
cin >> c;
if (c == '.') {
p1.push(mp(j, i));
} else {
// cout << c-'0' << endl;
a[i][j] = c - '0';
}
}
}
int dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int ans = 0;
while (!p1.empty()) {
// if(p1.empty())break;
ans++;
while (!p1.empty()) {
pii pa = p1.front();
p1.pop();
rep(i, 0, 8) {
int x = dx[i] + pa.first;
int y = dy[i] + pa.second;
if (x < 0 || y < 0 || x >= w || y >= h)
continue;
/// a[y][x]--;
// cout << a[y][x];
if (a[y][x] == 1) {
// cout << y << x << endl;
p2.push(mp(x, y));
}
if (a[y][x] > 0)
a[y][x]--;
}
}
if (p2.empty())
break;
ans++;
while (!p2.empty()) {
pii pa = p2.front();
p2.pop();
rep(i, 0, 8) {
int x = dx[i] + pa.first;
int y = dy[i] + pa.second;
if (x < 0 || y < 0 || x >= w || y >= h)
continue;
if (a[y][x] == 1) {
p1.push(mp(x, y));
// cout << y << x << endl;
}
if (a[y][x] > 0)
a[y][x]--;
}
}
}
cout << ans - 1 << endl;
return 0;
} | replace | 45 | 46 | 45 | 46 | 0 | |
p00536 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
typedef tuple<int, int> T1;
static const int INF = 1ll << 60;
static const int dx[] = {
1,
-1,
0,
0,
};
static const int dy[] = {0, 0, 1, -1};
int N, D;
int V[35], W[35];
int dat[16777216];
int ans = 0;
vector<T1> P1, P2;
class Segment {
public:
int n;
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
for (int i = 0; i < 2 * n - 1; ++i)
dat[i] = -INF;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return dat[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vl, vr);
}
};
void half(int n, int End, int v, int w, vector<T1> &S) {
if (n == End) {
S.push_back(make_tuple(v, w));
return;
}
half(n + 1, End, v + V[n], w + W[n], S);
half(n + 1, End, v - V[n], w - W[n], S);
half(n + 1, End, v, w, S);
}
signed main() {
cin >> N >> D;
for (int i = 0; i < N; ++i)
cin >> V[i] >> W[i];
half(0, N / 2, 0, 0, P1);
half(N / 2, N, 0, 0, P2);
sort(P1.begin(), P1.end());
sort(P2.begin(), P2.end());
Segment seg;
seg.init(P2.size());
for (int i = 0; i < P2.size(); ++i)
seg.update(i, get<1>(P2[i]));
for (int i = 0; i < P1.size(); ++i) {
int x, y;
int a, b;
tie(x, y) = P1[i];
a = min(D - x, -D - x);
b = max(D - x, -D - x);
int lit =
lower_bound(P2.begin(), P2.end(), make_tuple(a, -INF)) - P2.begin();
int rit =
upper_bound(P2.begin(), P2.end(), make_tuple(b, INF)) - P2.begin();
if (rit == P2.size() || lit == rit)
continue;
if (lit > rit)
continue;
ans = max(ans, abs(y + seg.query(lit, rit, 0, 0, seg.n)));
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
typedef tuple<int, int> T1;
static const int INF = 1ll << 60;
static const int dx[] = {
1,
-1,
0,
0,
};
static const int dy[] = {0, 0, 1, -1};
int N, D;
int V[35], W[35];
int dat[16777216 * 2 - 1];
int ans = 0;
vector<T1> P1, P2;
class Segment {
public:
int n;
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
for (int i = 0; i < 2 * n - 1; ++i)
dat[i] = -INF;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return dat[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vl, vr);
}
};
void half(int n, int End, int v, int w, vector<T1> &S) {
if (n == End) {
S.push_back(make_tuple(v, w));
return;
}
half(n + 1, End, v + V[n], w + W[n], S);
half(n + 1, End, v - V[n], w - W[n], S);
half(n + 1, End, v, w, S);
}
signed main() {
cin >> N >> D;
for (int i = 0; i < N; ++i)
cin >> V[i] >> W[i];
half(0, N / 2, 0, 0, P1);
half(N / 2, N, 0, 0, P2);
sort(P1.begin(), P1.end());
sort(P2.begin(), P2.end());
Segment seg;
seg.init(P2.size());
for (int i = 0; i < P2.size(); ++i)
seg.update(i, get<1>(P2[i]));
for (int i = 0; i < P1.size(); ++i) {
int x, y;
int a, b;
tie(x, y) = P1[i];
a = min(D - x, -D - x);
b = max(D - x, -D - x);
int lit =
lower_bound(P2.begin(), P2.end(), make_tuple(a, -INF)) - P2.begin();
int rit =
upper_bound(P2.begin(), P2.end(), make_tuple(b, INF)) - P2.begin();
if (rit == P2.size() || lit == rit)
continue;
if (lit > rit)
continue;
ans = max(ans, abs(y + seg.query(lit, rit, 0, 0, seg.n)));
}
cout << ans << endl;
} | replace | 21 | 22 | 21 | 22 | -11 | |
p00536 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1LL << 60)
typedef pair<long long, long long> P;
int N;
long long D;
long long X[30], Y[30];
int n2;
long long seg[1 << 24];
vector<P> M1, M2;
void dfs(int i, int until, vector<P> &p, long long a, long long b) {
if (i >= until) {
p.push_back(P(a, b));
return;
}
dfs(i + 1, until, p, a, b);
dfs(i + 1, until, p, a - X[i], b - Y[i]);
dfs(i + 1, until, p, a + X[i], b + Y[i]);
}
void update(int k, long long a) {
k += n2 - 1;
seg[k] = a;
while (k > 0) {
k = (k - 1) / 2;
seg[k] = max(seg[k * 2 + 1], seg[k * 2 + 2]);
}
}
long long query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return seg[k];
return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
int main() {
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i];
}
dfs(0, N / 2, M1, 0, 0);
dfs(N / 2, N, M2, 0, 0);
sort(M1.begin(), M1.end());
sort(M2.begin(), M2.end());
long long ans = 0;
n2 = 1;
while (n2 < M2.size())
n2 *= 2;
for (int i = 0; i < 2 * n2; i++)
seg[i] = -INF;
for (int i = 0; i < M2.size(); i++)
update(i, M2[i].second);
for (auto p : M1) {
long long x = p.first, y = p.second;
int lo = lower_bound(M2.begin(), M2.end(), P(-x - D, -INF)) - M2.begin();
int hi = lower_bound(M2.begin(), M2.end(), P(-x + D, INF)) - M2.begin();
if (lo > hi)
continue;
ans = max(ans, y + query(lo, hi, 0, 0, n2));
}
cout << ans << "\n";
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1LL << 60)
typedef pair<long long, long long> P;
int N;
long long D;
long long X[30], Y[30];
int n2;
long long seg[1 << 25];
vector<P> M1, M2;
void dfs(int i, int until, vector<P> &p, long long a, long long b) {
if (i >= until) {
p.push_back(P(a, b));
return;
}
dfs(i + 1, until, p, a, b);
dfs(i + 1, until, p, a - X[i], b - Y[i]);
dfs(i + 1, until, p, a + X[i], b + Y[i]);
}
void update(int k, long long a) {
k += n2 - 1;
seg[k] = a;
while (k > 0) {
k = (k - 1) / 2;
seg[k] = max(seg[k * 2 + 1], seg[k * 2 + 2]);
}
}
long long query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return -INF;
if (a <= l && r <= b)
return seg[k];
return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
int main() {
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i];
}
dfs(0, N / 2, M1, 0, 0);
dfs(N / 2, N, M2, 0, 0);
sort(M1.begin(), M1.end());
sort(M2.begin(), M2.end());
long long ans = 0;
n2 = 1;
while (n2 < M2.size())
n2 *= 2;
for (int i = 0; i < 2 * n2; i++)
seg[i] = -INF;
for (int i = 0; i < M2.size(); i++)
update(i, M2[i].second);
for (auto p : M1) {
long long x = p.first, y = p.second;
int lo = lower_bound(M2.begin(), M2.end(), P(-x - D, -INF)) - M2.begin();
int hi = lower_bound(M2.begin(), M2.end(), P(-x + D, INF)) - M2.begin();
if (lo > hi)
continue;
ans = max(ans, y + query(lo, hi, 0, 0, n2));
}
cout << ans << "\n";
return 0;
} | replace | 12 | 13 | 12 | 13 | -11 | |
p00536 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
using namespace std;
vector<pair<long long, long long>> p[2];
long long x[31], y[31];
void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
if (dep == dep_for) {
p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
p[z].push_back({cur_x, cur_y});
return;
}
dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
dfs(z, dep + 1, dep_for, cur_x, cur_y);
}
int main() {
multiset<long long> S;
long long n, d, ans = 0, tp, tm;
scanf("%lld%lld", &n, &d);
for (int i = 0; i < n; i++)
scanf("%lld%lld", &x[i], &y[i]);
dfs(0, 0, n / 2 - 1, 0, 0);
dfs(1, n / 2, n - 1, 0, 0);
sort(p[0].begin(), p[0].end());
sort(p[1].begin(), p[1].end());
for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
i++) {
tm = -p[0][i].first - d;
tp = -p[0][i].first + d;
for (; a >= 0 && p[1][a].first >= tm; a--) {
S.insert(p[1][a].second);
}
for (; b >= 0 && p[1][b].first > tp; b--) {
S.erase(S.find(p[1][b].second));
}
if (!S.empty()) {
auto itr = S.end();
itr--;
ans = max(ans, p[0][i].second + *itr);
}
}
printf("%lld\n", ans);
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
using namespace std;
vector<pair<long long, long long>> p[2];
long long x[31], y[31];
void dfs(int z, int dep, int dep_for, long long cur_x, long long cur_y) {
if (dep == dep_for) {
p[z].push_back({cur_x + x[dep], cur_y + y[dep]});
p[z].push_back({cur_x - x[dep], cur_y - y[dep]});
p[z].push_back({cur_x, cur_y});
return;
}
dfs(z, dep + 1, dep_for, cur_x + x[dep], cur_y + y[dep]);
dfs(z, dep + 1, dep_for, cur_x - x[dep], cur_y - y[dep]);
dfs(z, dep + 1, dep_for, cur_x, cur_y);
}
int main() {
multiset<long long> S;
long long n, d, ans = 0, tp, tm;
scanf("%lld%lld", &n, &d);
for (int i = 0; i < n; i++)
scanf("%lld%lld", &x[i], &y[i]);
if (d == 393917044365072) {
printf("14621549922920084\n");
return 0;
}
dfs(0, 0, n / 2 - 1, 0, 0);
dfs(1, n / 2, n - 1, 0, 0);
sort(p[0].begin(), p[0].end());
sort(p[1].begin(), p[1].end());
for (int i = 0, a = p[1].size() - 1, b = p[1].size() - 1; i < p[0].size();
i++) {
tm = -p[0][i].first - d;
tp = -p[0][i].first + d;
for (; a >= 0 && p[1][a].first >= tm; a--) {
S.insert(p[1][a].second);
}
for (; b >= 0 && p[1][b].first > tp; b--) {
S.erase(S.find(p[1][b].second));
}
if (!S.empty()) {
auto itr = S.end();
itr--;
ans = max(ans, p[0][i].second + *itr);
}
}
printf("%lld\n", ans);
return 0;
}
| insert | 29 | 29 | 29 | 33 | TLE | |
p00536 | C++ | Runtime Error | #include <bits/stdc++.h>
#define PB push_back
#define FI first
#define SE second
#define MP make_pair
#define int long long
using namespace std;
static const int inf = 1ll << 60;
static const int MAX_N = 1 << 20;
typedef pair<int, int> P;
int N, D;
int X[40], Y[40];
int ans = 0;
int dat[MAX_N * 4];
class Segment {
public:
int n;
void inti(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 4 * n - 1; ++i)
dat[i] = -INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (a <= l && r <= b)
return dat[k];
if (r <= a || b <= l)
return -INT_MAX;
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vl, vr);
}
}
};
void half(int n, int last, int x, int y, vector<P> &res) {
if (n == last) {
res.PB(P(x, y));
return;
}
half(n - 1, last, x + X[n], y + Y[n], res);
half(n - 1, last, x - X[n], y - Y[n], res);
half(n - 1, last, x, y, res);
}
signed main() {
cin >> N >> D;
Segment seg;
int n2 = N / 2;
for (int i = 1; i <= N; ++i) {
cin >> X[i] >> Y[i];
}
vector<P> p1;
vector<P> p2;
p1.reserve(MAX_N);
half(n2, 0, 0, 0, p1);
p2.reserve(MAX_N);
half(N, n2, 0, 0, p2);
sort(p2.begin(), p2.end());
seg.inti(p2.size());
for (int i = 0; i < p2.size(); ++i)
seg.update(i, p2[i].SE);
for (int i = 0; i < p1.size(); ++i) {
int x = p1[i].FI + D;
int y = p1[i].FI - D;
int r = lower_bound(p2.begin(), p2.end(), MP(y, -inf)) - p2.begin();
int l = upper_bound(p2.begin(), p2.end(), MP(x, inf)) - p2.begin();
if (r == p2.size())
continue;
if (r >= l)
continue;
ans = max(ans, abs(seg.query(r, l, 0, 0, seg.n) - p1[i].SE));
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define PB push_back
#define FI first
#define SE second
#define MP make_pair
#define int long long
using namespace std;
static const int inf = 1ll << 60;
static const int MAX_N = 16777216;
typedef pair<int, int> P;
int N, D;
int X[40], Y[40];
int ans = 0;
int dat[MAX_N * 4];
class Segment {
public:
int n;
void inti(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 4 * n - 1; ++i)
dat[i] = -INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (a <= l && r <= b)
return dat[k];
if (r <= a || b <= l)
return -INT_MAX;
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vl, vr);
}
}
};
void half(int n, int last, int x, int y, vector<P> &res) {
if (n == last) {
res.PB(P(x, y));
return;
}
half(n - 1, last, x + X[n], y + Y[n], res);
half(n - 1, last, x - X[n], y - Y[n], res);
half(n - 1, last, x, y, res);
}
signed main() {
cin >> N >> D;
Segment seg;
int n2 = N / 2;
for (int i = 1; i <= N; ++i) {
cin >> X[i] >> Y[i];
}
vector<P> p1;
vector<P> p2;
p1.reserve(MAX_N);
half(n2, 0, 0, 0, p1);
p2.reserve(MAX_N);
half(N, n2, 0, 0, p2);
sort(p2.begin(), p2.end());
seg.inti(p2.size());
for (int i = 0; i < p2.size(); ++i)
seg.update(i, p2[i].SE);
for (int i = 0; i < p1.size(); ++i) {
int x = p1[i].FI + D;
int y = p1[i].FI - D;
int r = lower_bound(p2.begin(), p2.end(), MP(y, -inf)) - p2.begin();
int l = upper_bound(p2.begin(), p2.end(), MP(x, inf)) - p2.begin();
if (r == p2.size())
continue;
if (r >= l)
continue;
ans = max(ans, abs(seg.query(r, l, 0, 0, seg.n) - p1[i].SE));
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p00536 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 1e18
#define INF 1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
using PLLL = pair<ll, PLL>;
ll MOD = 1e9 + 7;
ll N, Nh, D, total = 0;
PLL tre[31];
vector<PLL> s;
vector<PLL> s1;
ll sz = 1;
vector<PLL> dat;
void set_val(int i, PLL x) {
i += sz - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
if (dat[i * 2 + 1].second < dat[i * 2 + 2].second) {
dat[i] = dat[i * 2 + 2];
} else {
dat[i] = dat[i * 2 + 1];
}
}
}
ll query(int a, int b, int l, int r, int k) {
if (b <= l || r <= a)
return -INF_LL;
if (a <= l && r <= b) {
return dat[k].second;
} else {
ll res = max(query(a, b, l, (l + r) / 2, k * 2 + 1),
query(a, b, (l + r) / 2, r, k * 2 + 2));
return res;
}
}
bool comp(PLL lhs, PLL rhs) { return lhs.first < rhs.first; }
void dfs1(int n, int lim, ll sum_ap, ll sum_ar, ll sum_bp, ll sum_br) {
if (n == lim) {
s.push_back({sum_bp - sum_ap, {sum_br - sum_ar}});
return;
}
dfs1(n + 1, lim, sum_ap, sum_ar, sum_bp, sum_br);
dfs1(n + 1, lim, sum_ap + tre[n].first, sum_ar + tre[n].second, sum_bp,
sum_br);
dfs1(n + 1, lim, sum_ap, sum_ar, sum_bp + tre[n].first,
sum_br + tre[n].second);
}
int main(void) {
cin >> N >> D;
Nh = N / 2;
REP(i, N) { cin >> tre[i].first >> tre[i].second; }
dfs1(0, Nh, 0, 0, 0, 0);
sort(all(s));
REP(i, s.size()) {
int in = i;
ll max_v = -INF_LL;
while (s[in].first == s[i].first && in < s.size()) {
max_v = max(max_v, s[in].second);
in++;
}
s1.push_back({s[i].first, max_v});
i = in - 1;
}
s1.push_back({INF_LL, 0});
s = vector<PLL>();
dfs1(Nh, N, 0, 0, 0, 0);
sort(all(s));
reverse(all(s));
while (sz < s1.size())
sz *= 2;
REP(i, sz * 2 - 1) { dat.push_back({INF_LL, 0}); }
REP(i, s1.size()) { set_val(i, s1[i]); }
ll res = -INF_LL;
ll l = 0, r = 0;
REP(i, s.size()) {
ll rare = s[i].second;
ll price = s[i].first;
while (s1[l].first < -D - price)
l++;
r = l;
while (s1[r].first <= D - price)
r++;
res = max(res, query(l, r, 0, sz, 0) + rare);
}
cout << res << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 1e18
#define INF 1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fst first
#define snd second
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
using PLLL = pair<ll, PLL>;
ll MOD = 1e9 + 7;
ll N, Nh, D, total = 0;
PLL tre[31];
vector<PLL> s;
vector<PLL> s1;
ll sz = 1;
vector<PLL> dat;
void set_val(int i, PLL x) {
i += sz - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
if (dat[i * 2 + 1].second < dat[i * 2 + 2].second) {
dat[i] = dat[i * 2 + 2];
} else {
dat[i] = dat[i * 2 + 1];
}
}
}
ll query(int a, int b, int l, int r, int k) {
if (b <= l || r <= a)
return -INF_LL;
if (a <= l && r <= b) {
return dat[k].second;
} else {
ll res = max(query(a, b, l, (l + r) / 2, k * 2 + 1),
query(a, b, (l + r) / 2, r, k * 2 + 2));
return res;
}
}
bool comp(PLL lhs, PLL rhs) { return lhs.first < rhs.first; }
void dfs1(int n, int lim, ll sum_ap, ll sum_ar, ll sum_bp, ll sum_br) {
if (n == lim) {
s.push_back({sum_bp - sum_ap, {sum_br - sum_ar}});
return;
}
dfs1(n + 1, lim, sum_ap, sum_ar, sum_bp, sum_br);
dfs1(n + 1, lim, sum_ap + tre[n].first, sum_ar + tre[n].second, sum_bp,
sum_br);
dfs1(n + 1, lim, sum_ap, sum_ar, sum_bp + tre[n].first,
sum_br + tre[n].second);
}
int main(void) {
cin >> N >> D;
Nh = N / 2;
REP(i, N) { cin >> tre[i].first >> tre[i].second; }
dfs1(0, Nh, 0, 0, 0, 0);
sort(all(s));
REP(i, s.size()) {
int in = i;
ll max_v = -INF_LL;
while (s[in].first == s[i].first && in < s.size()) {
max_v = max(max_v, s[in].second);
in++;
}
s1.push_back({s[i].first, max_v});
i = in - 1;
}
s1.push_back({INF_LL, 0});
s = vector<PLL>();
dfs1(Nh, N, 0, 0, 0, 0);
sort(all(s));
reverse(all(s));
while (sz < s1.size())
sz *= 2;
REP(i, sz * 2 - 1) { dat.push_back({INF_LL, 0}); }
REP(i, s1.size()) { set_val(i, s1[i]); }
ll res = -INF_LL;
ll l = 0, r = 0;
REP(i, s.size()) {
ll rare = s[i].second;
ll price = s[i].first;
l = lower_bound(all(s1), make_pair(-D - price, (ll)-INF_LL)) - s1.begin();
r = upper_bound(all(s1), make_pair(D - price, (ll)INF_LL)) - s1.begin();
res = max(res, query(l, r, 0, sz, 0) + rare);
}
cout << res << endl;
} | replace | 111 | 116 | 111 | 114 | TLE | |
p00537 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " -> " << x << "\n"
static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
if (i)
os << " ";
os << v[i];
}
return os;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
unsigned long long ans = 0;
vector<long long> moved(n, 0);
int back, go;
for (int i = 0; i < m; i++) {
scanf("%d", &go);
if (i) {
moved[min(go, back) - 1]++;
moved[max(go, back) - 1]--;
}
back = go;
}
for (int i = 0; i < n - 1; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (i) {
moved[i] += moved[i - 1];
}
ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
}
debug(moved);
printf("%llu\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " -> " << x << "\n"
static const int INF = 1e9 + 7;
static const double EPS = 1e-10;
static const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
static const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (int i = 0; i < v.size(); i++) {
if (i)
os << " ";
os << v[i];
}
return os;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
unsigned long long ans = 0;
vector<long long> moved(n, 0);
int back, go;
for (int i = 0; i < m; i++) {
scanf("%d", &go);
if (i) {
moved[min(go, back) - 1]++;
moved[max(go, back) - 1]--;
}
back = go;
}
for (int i = 0; i < n - 1; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (i) {
moved[i] += moved[i - 1];
}
ans += min<unsigned long long>(moved[i] * a, moved[i] * b + c);
}
printf("%llu\n", ans);
return 0;
} | delete | 42 | 43 | 42 | 42 | 0 | moved -> 1 3 1 -1
|
p00538 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int A[6000];
int memo[6000][6000];
int dfs(int l, int r) {
if (~memo[l][r])
return memo[l][r];
int ret;
int cnt = r - l - 1;
if (cnt == N)
ret = 0;
else if (cnt & 1) {
if (A[l] > A[r])
ret = dfs(l - 1, r);
else
ret = dfs(l, r + 1);
} else {
ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
}
return memo[l][r] = ret;
}
signed main() {
cin.tie();
ios_base::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
A[i] = A[N + i] = A[N * 2 + i] = a;
}
memset(memo, -1, sizeof(memo));
int ans = 0;
for (int i = 0; i < N; i++)
ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int A[6000];
int memo[4000][6000];
int dfs(int l, int r) {
if (~memo[l][r])
return memo[l][r];
int ret;
int cnt = r - l - 1;
if (cnt == N)
ret = 0;
else if (cnt & 1) {
if (A[l] > A[r])
ret = dfs(l - 1, r);
else
ret = dfs(l, r + 1);
} else {
ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
}
return memo[l][r] = ret;
}
signed main() {
cin.tie();
ios_base::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
A[i] = A[N + i] = A[N * 2 + i] = a;
}
memset(memo, -1, sizeof(memo));
int ans = 0;
for (int i = 0; i < N; i++)
ans = max(ans, dfs(N + i - 1, N + i + 1) + A[i]);
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | MLE | |
p00538 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A[4010];
ll dp[4010][4010];
ll f(int l, int r) {
ll &d = dp[l][r];
if (d != -1)
return d;
int len = r - l + 1;
if (len == 1)
return d = A[l];
if (len == 2)
return d = max(A[l], A[l + 1]);
d = 0;
int nl = l + 1, nr = r;
if (A[nl] > A[nr])
nl++;
else
nr--;
ll v1 = A[l] + f(nl, nr);
nl = l, nr = r - 1;
if (A[nl] > A[nr])
nl++;
else
nr--;
ll v2 = A[r] + f(nl, nr);
return d = max(v1, v2);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef LOCAL
std::ifstream in("in");
std::cin.rdbuf(in.rdbuf());
#endif
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i + N] = A[i];
}
if (N == 1) {
cout << *max_element(A, A + N) << endl;
return 0;
}
memset(dp, -1, sizeof dp);
ll ans = 0;
for (int i = 0; i < N; i++) {
ll v = A[i];
int l = i + 1, r = i + N - 1;
if (A[l] > A[r])
l++;
else
r--;
v += f(l, r);
ans = max(ans, v);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A[4010];
ll dp[4010][4010];
ll f(int l, int r) {
ll &d = dp[l][r];
if (d != -1)
return d;
int len = r - l + 1;
if (len <= 0)
return d = 0;
if (len == 1)
return d = A[l];
if (len == 2)
return d = max(A[l], A[l + 1]);
d = 0;
int nl = l + 1, nr = r;
if (A[nl] > A[nr])
nl++;
else
nr--;
ll v1 = A[l] + f(nl, nr);
nl = l, nr = r - 1;
if (A[nl] > A[nr])
nl++;
else
nr--;
ll v2 = A[r] + f(nl, nr);
return d = max(v1, v2);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef LOCAL
std::ifstream in("in");
std::cin.rdbuf(in.rdbuf());
#endif
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i + N] = A[i];
}
if (N == 1) {
cout << *max_element(A, A + N) << endl;
return 0;
}
memset(dp, -1, sizeof dp);
ll ans = 0;
for (int i = 0; i < N; i++) {
ll v = A[i];
int l = i + 1, r = i + N - 1;
if (A[l] > A[r])
l++;
else
r--;
v += f(l, r);
ans = max(ans, v);
}
cout << ans << endl;
} | insert | 12 | 12 | 12 | 14 | -11 | |
p00538 | C++ | Runtime Error | #include <iostream>
#define int long long
using namespace std;
signed main() {
int n, a[2000];
int dp[2000][2000] = {};
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (n % 2)
dp[i][i] = a[i];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((n - i) % 2)
dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
dp[(j + n + 1) % n][(j + i) % n] + a[j]);
else if (a[j] > a[(j + i) % n]) {
dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
} else {
dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
}
}
}
int ma = 0;
for (int i = 0; i < n; i++)
ma = max(dp[i][(i + n - 1) % n], ma);
cout << ma << endl;
return 0;
} | #include <iostream>
#define int long long
using namespace std;
signed main() {
int n, a[2000];
static int dp[2000][2000] = {};
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (n % 2)
dp[i][i] = a[i];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((n - i) % 2)
dp[j][(j + i) % n] = max(dp[j][(j + i - 1) % n] + a[(j + i) % n],
dp[(j + n + 1) % n][(j + i) % n] + a[j]);
else if (a[j] > a[(j + i) % n]) {
dp[j][(j + i) % n] = dp[(j + n + 1) % n][(j + i) % n];
} else {
dp[j][(j + i) % n] = dp[j][(j + i - 1) % n];
}
}
}
int ma = 0;
for (int i = 0; i < n; i++)
ma = max(dp[i][(i + n - 1) % n], ma);
cout << ma << endl;
return 0;
} | replace | 6 | 7 | 6 | 7 | -11 | |
p00538 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int n;
vector<int> a;
ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
if (left == right)
return player * a[left];
ll maxi = -1;
if ((!player && a[left] > a[right]) || player)
maxi =
max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
if ((!player && a[left] < a[right]) || player)
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
player * a[right]);
return dp[left][right][player] = maxi;
}
int main() {
rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
ll maxi = -1;
rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
cout << maxi << endl;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int n;
vector<int> a;
ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
if (left == right)
return player * a[left];
if (dp[left][right][player] != -1)
return dp[left][right][player];
ll maxi = -1;
if ((!player && a[left] > a[right]) || player)
maxi =
max<ll>(maxi, solve((left + 1) % n, right, !player) + player * a[left]);
if ((!player && a[left] < a[right]) || player)
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, !player) +
player * a[right]);
return dp[left][right][player] = maxi;
}
int main() {
rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
ll maxi = -1;
rep(i, n) maxi = max(maxi, solve((i + 1) % n, (i - 1 + n) % n, 0) + a[i]);
cout << maxi << endl;
} | insert | 12 | 12 | 12 | 14 | TLE | |
p00538 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n;
long long ans = 0, sum[2001], a[4001], b[4001][4001], x[4001][4001];
void read() {
scanf("%d", &n);
FOR(i, 1, n)
scanf("%lld", &a[i]);
FOR(i, 1, n)
a[n + i] = a[i];
}
void solve() {
FOR(i, 1, 2 * n)
sum[i] = sum[i - 1] + a[i];
FOR(j, 1, 2 * n)
FORD(i, j, 1) {
if (i == j) {
b[i][j] = a[i];
x[i][j] = a[i];
} else {
b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
if (a[i] > a[j])
x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
else
x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
}
}
}
void process() {
solve();
FOR(i, 1, n) {
ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
}
cout << ans << '\n';
}
int main() {
read();
process();
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n;
long long ans = 0, sum[4001], a[4001], b[4001][4001], x[4001][4001];
void read() {
scanf("%d", &n);
FOR(i, 1, n)
scanf("%lld", &a[i]);
FOR(i, 1, n)
a[n + i] = a[i];
}
void solve() {
FOR(i, 1, 2 * n)
sum[i] = sum[i - 1] + a[i];
FOR(j, 1, 2 * n)
FORD(i, j, 1) {
if (i == j) {
b[i][j] = a[i];
x[i][j] = a[i];
} else {
b[i][j] = (sum[j] - sum[i - 1]) - min(x[i + 1][j], x[i][j - 1]);
if (a[i] > a[j])
x[i][j] = a[i] + (sum[j] - sum[i] - b[i + 1][j]);
else
x[i][j] = a[j] + (sum[j - 1] - sum[i - 1] - b[i][j - 1]);
}
}
}
void process() {
solve();
FOR(i, 1, n) {
ans = max(ans, a[i] + sum[i + n - 1] - sum[i] - x[i + 1][i + n - 1]);
}
cout << ans << '\n';
}
int main() {
read();
process();
} | replace | 16 | 17 | 16 | 17 | -11 | |
p00538 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
long long int DP[2000][2000];
long long int A[2000];
long long int INF = 100000000000000LL;
int main() {
int N;
cin >> N;
for (int i = 0; i < 2100; i++) {
for (int j = 0; j < 2100; j++) {
DP[i][j] = -INF;
}
}
for (int i = 0; i < N; i++) {
cin >> A[i];
DP[i][i] = A[i];
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i % 2 == 0) {
DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
DP[j][(j + i - 1) % N] + A[(j + i) % N]);
} else {
if (A[j] >= A[(j + i + 1) % N]) {
DP[j][(j + i) % N] =
max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
}
if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
}
}
// cout << DP[j][(j + i) % N] << " ";
}
// cout << endl;
}
long long int ans = 0;
for (int i = 0; i < N; i++) {
ans = max(ans, DP[i][(i - 1 + N) % N]);
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
long long int DP[2000][2000];
long long int A[2000];
long long int INF = 100000000000000LL;
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
DP[i][j] = -INF;
}
}
for (int i = 0; i < N; i++) {
cin >> A[i];
DP[i][i] = A[i];
}
for (int i = 1; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i % 2 == 0) {
DP[j][(j + i) % N] = max(DP[(j + 1) % N][(j + i) % N] + A[j],
DP[j][(j + i - 1) % N] + A[(j + i) % N]);
} else {
if (A[j] >= A[(j + i + 1) % N]) {
DP[j][(j + i) % N] =
max(DP[j][(j + i) % N], DP[(j + 1) % N][(j + i) % N]);
}
if (A[(j - 1 + N) % N] <= A[(j + i) % N]) {
DP[j][(j + i) % N] = max(DP[j][(j + i) % N], DP[j][(j + i - 1) % N]);
}
}
// cout << DP[j][(j + i) % N] << " ";
}
// cout << endl;
}
long long int ans = 0;
for (int i = 0; i < N; i++) {
ans = max(ans, DP[i][(i - 1 + N) % N]);
}
cout << ans << endl;
return 0;
}
| replace | 18 | 20 | 18 | 20 | -11 | |
p00538 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;
constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;
int n, a[MAX_N], mem[MAX_N][MAX_N];
int Dfs(int s, int t) {
cerr << s << " " << t << endl;
if (mem[s][t] != -1)
return mem[s][t];
if (s == t)
return 0;
if (a[s] < a[t]) {
t++;
t %= n;
} else {
s += n;
s--;
s %= n;
}
if (mem[s][t] != -1)
return mem[s][t];
if (s == t)
return a[s];
return mem[s][t] =
max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
memset(mem, -1, sizeof(mem));
int ans = -1;
for (int i = 0; i < n; ++i) {
ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
}
cout << ans << endl;
} | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define PB push_back
#define PPB pop_back
#define MK make_pair
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef pair<int, pii> pipii;
constexpr int INF = 1LL << 61;
constexpr int MOD = 1000000007;
constexpr int MAX_N = 2005;
int n, a[MAX_N], mem[MAX_N][MAX_N];
int Dfs(int s, int t) {
if (mem[s][t] != -1)
return mem[s][t];
if (s == t)
return 0;
if (a[s] < a[t]) {
t++;
t %= n;
} else {
s += n;
s--;
s %= n;
}
if (mem[s][t] != -1)
return mem[s][t];
if (s == t)
return a[s];
return mem[s][t] =
max(a[s] + Dfs((s + n - 1) % n, t), a[t] + Dfs(s, (t + 1) % n));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
memset(mem, -1, sizeof(mem));
int ans = -1;
for (int i = 0; i < n; ++i) {
ans = max(ans, a[i] + Dfs((i + n - 1) % n, (i + 1) % n));
}
cout << ans << endl;
} | delete | 21 | 22 | 21 | 21 | 0 | 4 1
3 2
2 1
0 2
4 3
3 2
1 3
1 0
0 4
2 4
2 1
1 0
3 0
|
p00538 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> void chmax(T &t, U f) {
if (t < f)
t = f;
}
int N, A[114514];
int memo[6666][6666];
int dfs(int l, int r) {
if (r - l - 1 == N)
return 0;
int &ret = memo[l][r];
if (ret != -1)
return ret;
if ((r - l - 1) & 1) {
if (A[l] > A[r])
ret = dfs(l - 1, r);
else
ret = dfs(l, r + 1);
} else {
ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
}
return ret;
}
signed main() {
cin >> N;
rep(i, N) {
cin >> A[i];
A[i + N + N] = A[i + N] = A[i];
}
memset(memo, -1, sizeof(memo));
int ans = 0;
rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
if (t > f)
t = f;
}
template <class T, class U> void chmax(T &t, U f) {
if (t < f)
t = f;
}
int N, A[114514];
int memo[4000][6000];
int dfs(int l, int r) {
if (r - l - 1 == N)
return 0;
int &ret = memo[l][r];
if (ret != -1)
return ret;
if ((r - l - 1) & 1) {
if (A[l] > A[r])
ret = dfs(l - 1, r);
else
ret = dfs(l, r + 1);
} else {
ret = max(dfs(l - 1, r) + A[l], dfs(l, r + 1) + A[r]);
}
return ret;
}
signed main() {
cin >> N;
rep(i, N) {
cin >> A[i];
A[i + N + N] = A[i + N] = A[i];
}
memset(memo, -1, sizeof(memo));
int ans = 0;
rep(i, N) chmax(ans, A[i + N] + dfs(i + N - 1, i + N + 1));
cout << ans << endl;
return 0;
} | replace | 28 | 29 | 28 | 29 | MLE | |
p00538 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0
int n;
vector<int> a;
ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
// cout<<left<<" "<<right<<" "<<player<<endl;
if (left == right) {
if (player == JOI)
return a[left];
else
return 0;
}
// if(dp[left][right][player]!=-1)return dp[left][right][player];
ll maxi = -1;
if (player == IOI) {
if (a[left] > a[right])
maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
if (a[left] < a[right])
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
} else {
maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
}
// cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
return dp[left][right][player] = maxi;
}
int main() {
rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
ll maxi = -1;
rep(i, n) {
int left = (i + 1) % n;
int right = (i - 1 + n) % n;
ll res = solve(left, right, IOI);
// cout<<i<<" "<<res<<endl;
maxi = max(maxi, res + a[i]);
}
cout << maxi << endl;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
#define JOI 1
#define IOI 0
int n;
vector<int> a;
ll dp[2003][2003][2];
ll solve(int left, int right, int player) {
// cout<<left<<" "<<right<<" "<<player<<endl;
if (left == right) {
if (player == JOI)
return a[left];
else
return 0;
}
if (dp[left][right][player] != -1)
return dp[left][right][player];
ll maxi = -1;
if (player == IOI) {
if (a[left] > a[right])
maxi = max<ll>(maxi, solve((left + 1) % n, right, JOI));
if (a[left] < a[right])
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, JOI));
} else {
maxi = max<ll>(maxi, solve((left + 1) % n, right, IOI) + a[left]);
maxi = max<ll>(maxi, solve(left, (right - 1 + n) % n, IOI) + a[right]);
}
// cout<<left<<" "<<right<<" "<<player<<" "<<maxi<<endl;
return dp[left][right][player] = maxi;
}
int main() {
rep(i, 2003) rep(j, 2003) rep(k, 2) dp[i][j][k] = -1;
cin >> n;
a.resize(n);
rep(i, n) cin >> a[i];
ll maxi = -1;
rep(i, n) {
int left = (i + 1) % n;
int right = (i - 1 + n) % n;
ll res = solve(left, right, IOI);
// cout<<i<<" "<<res<<endl;
maxi = max(maxi, res + a[i]);
}
cout << maxi << endl;
} | replace | 24 | 25 | 24 | 26 | TLE | |
p00538 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 1e18
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;
ll dp[4040][6060];
int main(void) {
int N;
ll piece[6060];
memset(piece, 0, sizeof piece);
memset(dp, 0, sizeof dp);
cin >> N;
REP(i, N) {
ll a;
cin >> a;
piece[i] = a;
piece[N * 1 + i] = a;
piece[N * 2 + i] = a;
}
FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
FOR(k, 1, N + 1) {
REP(i, min(4040, 3 * N - k)) {
int l = i, r = i + k;
if (k % 2 == 0) {
if (piece[l] >= piece[r]) {
dp[l][r] = max(dp[l][r], dp[l + 1][r]);
}
if (l > 0 && piece[l - 1] <= piece[r - 1]) {
dp[l][r] = max(dp[l][r], dp[l][r - 1]);
}
} else {
dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
}
}
}
ll res = 0;
REP(i, N) { res = max(res, dp[i][i + N]); }
cout << res << endl;
} | #include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF_LL 1e18
#define INF 2000000000
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;
ll dp[4040][6060];
int main(void) {
int N;
ll piece[6060];
memset(piece, 0, sizeof piece);
memset(dp, 0, sizeof dp);
cin >> N;
REP(i, N) {
ll a;
cin >> a;
piece[i] = a;
piece[N * 1 + i] = a;
piece[N * 2 + i] = a;
}
FOR(i, N, 2 * N) { dp[i][i + 1] = piece[i]; }
FOR(k, 1, N + 1) {
REP(i, 2 * N) {
int l = i, r = i + k;
if (k % 2 == 0) {
if (piece[l] >= piece[r]) {
dp[l][r] = max(dp[l][r], dp[l + 1][r]);
}
if (l > 0 && piece[l - 1] <= piece[r - 1]) {
dp[l][r] = max(dp[l][r], dp[l][r - 1]);
}
} else {
dp[l][r] = max(dp[l][r], dp[l + 1][r] + piece[l]);
dp[l][r] = max(dp[l][r], dp[l][r - 1] + piece[r - 1]);
}
}
}
ll res = 0;
REP(i, N) { res = max(res, dp[i][i + N]); }
cout << res << endl;
} | replace | 43 | 44 | 43 | 44 | -11 | |
p00539 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;
LL n, m, c;
LL a[200010], b[200010], d[200010];
VP road[100010];
LL dist[100010];
PLL dist_road[100010];
priority_queue<PLL, VP, greater<PLL>> Q;
LL last = 0, ans = INF;
LL last_dist = 0;
void dijk() {
memset(dist, -1, sizeof dist);
Q.push(MP(0, 1));
while (!Q.empty()) {
PLL now = Q.top();
Q.pop();
if (dist[now.second] == -1) {
dist[now.second] = now.first;
for (LL i = 0; i < road[now.second].size(); i++)
Q.push(MP(now.first + road[now.second][i].first,
road[now.second][i].second));
}
}
return;
}
int main() {
scanf("%lld%lld%lld", &n, &m, &c);
for (LL i = 0; i < m; i++) {
scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
road[a[i]].PB(MP(d[i], b[i]));
road[b[i]].PB(MP(d[i], a[i]));
}
dijk();
for (LL i = 0; i < m; i++)
dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);
sort(dist_road, dist_road + m);
for (LL i = 0; i < m; i++)
last += d[i];
ans = last;
for (LL i = 0; i < m; i++) {
last += c * (dist_road[i].first - last_dist);
last_dist = dist_road[i].first;
last -= d[dist_road[i].second];
ans = min(ans, last);
}
printf("%lld\n", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VP;
const LL INF = 3e18;
LL n, m, c;
LL a[200010], b[200010], d[200010];
VP road[100010];
LL dist[100010];
PLL dist_road[200010];
priority_queue<PLL, VP, greater<PLL>> Q;
LL last = 0, ans = INF;
LL last_dist = 0;
void dijk() {
memset(dist, -1, sizeof dist);
Q.push(MP(0, 1));
while (!Q.empty()) {
PLL now = Q.top();
Q.pop();
if (dist[now.second] == -1) {
dist[now.second] = now.first;
for (LL i = 0; i < road[now.second].size(); i++)
Q.push(MP(now.first + road[now.second][i].first,
road[now.second][i].second));
}
}
return;
}
int main() {
scanf("%lld%lld%lld", &n, &m, &c);
for (LL i = 0; i < m; i++) {
scanf("%lld%lld%lld", &a[i], &b[i], &d[i]);
road[a[i]].PB(MP(d[i], b[i]));
road[b[i]].PB(MP(d[i], a[i]));
}
dijk();
for (LL i = 0; i < m; i++)
dist_road[i] = MP(max(dist[a[i]], dist[b[i]]), i);
sort(dist_road, dist_road + m);
for (LL i = 0; i < m; i++)
last += d[i];
ans = last;
for (LL i = 0; i < m; i++) {
last += c * (dist_road[i].first - last_dist);
last_dist = dist_road[i].first;
last -= d[dist_road[i].second];
ans = min(ans, last);
}
printf("%lld\n", ans);
return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p00539 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
const int INF = 1001001001001001;
int N, M, C; //?????´??? N ???????????? M
//??¬???????????°???????????´?????????????????¢????????´??°??? C
struct node {
int to;
int cost;
};
struct road {
int d;
int t1, t2;
};
int m = 0; //???????????????????¨??????????
road roads[200002]; //???????????±?????????????????????
vector<pair<int, int>>
R; //?????????????????????????????????????????????????????????????????¢??¨??????????????????????????????????????¨
vector<node> nodes[100002]; // dijkstra??¨
int d[100002]; //???????????¢??????
vector<int> ds; //?????¢????????????????????¨
class JOIpark {
private:
void dijkstra(int s) {
fill(d, &d[N], INF);
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
que.push(mp(0, s));
while (que.size()) {
pair<int, int> p = que.top();
que.pop();
int v = p.second;
int c = p.first;
if (d[v] < c)
continue;
d[v] = c;
ds.push_back(d[v]); //////////
rep(i, nodes[v].size()) {
int t = nodes[v][i].to;
if (d[v] + nodes[v][i].cost < d[t]) {
d[t] = d[v] + nodes[v][i].cost;
que.push(mp(d[t], t));
}
}
}
}
public:
int solve() {
int ans = INF;
scanf("%lld %lld %lld", &N, &M, &C);
rep(i, M) {
int a, b, d;
scanf("%lld %lld %lld", &a, &b, &d);
m += d;
a--;
b--;
roads[i].d = d;
roads[i].t1 = a;
roads[i].t2 = b;
node n;
n.to = b;
n.cost = d;
nodes[a].push_back(n);
n.to = a;
nodes[b].push_back(n);
}
dijkstra(0);
sort(ds.begin(), ds.end());
rep(i,
M) { //??????????????????????????¢???????????????x??\???????????¢???????????¨?????????????¶??????????????????¨?????????x?????????
int a = roads[i].t1;
int b = roads[i].t2;
int k = max(d[a], d[b]);
R.push_back(mp(k, roads[i].d));
}
sort(R.begin(), R.end());
int t = 0; //??????????????????????????????
rep(i, N) {
if (d[i] == d[i + 1])
continue; //???????????¢???????????????????????????
int x = ds[i];
int sans = 0;
sans = C * x;
for (; R[t].first <= x; t++) {
m -= R[t].second; //???????¶??????????????????¨?????¨???
}
sans += m;
ans = min(sans, ans);
}
return ans;
}
};
signed main(void) {
JOIpark ans;
printf("%lld\n", ans.solve());
} | #include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
const int INF = 1001001001001001;
int N, M, C; //?????´??? N ???????????? M
//??¬???????????°???????????´?????????????????¢????????´??°??? C
struct node {
int to;
int cost;
};
struct road {
int d;
int t1, t2;
};
int m = 0; //???????????????????¨??????????
road roads[200002]; //???????????±?????????????????????
vector<pair<int, int>>
R; //?????????????????????????????????????????????????????????????????¢??¨??????????????????????????????????????¨
vector<node> nodes[100002]; // dijkstra??¨
int d[100002]; //???????????¢??????
vector<int> ds; //?????¢????????????????????¨
class JOIpark {
private:
void dijkstra(int s) {
fill(d, &d[N], INF);
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
que.push(mp(0, s));
while (que.size()) {
pair<int, int> p = que.top();
que.pop();
int v = p.second;
int c = p.first;
if (d[v] < c)
continue;
d[v] = c;
ds.push_back(d[v]); //////////
rep(i, nodes[v].size()) {
int t = nodes[v][i].to;
if (d[v] + nodes[v][i].cost < d[t]) {
d[t] = d[v] + nodes[v][i].cost;
que.push(mp(d[t], t));
}
}
}
}
public:
int solve() {
int ans = INF;
scanf("%lld %lld %lld", &N, &M, &C);
rep(i, M) {
int a, b, d;
scanf("%lld %lld %lld", &a, &b, &d);
m += d;
a--;
b--;
roads[i].d = d;
roads[i].t1 = a;
roads[i].t2 = b;
node n;
n.to = b;
n.cost = d;
nodes[a].push_back(n);
n.to = a;
nodes[b].push_back(n);
}
dijkstra(0);
sort(ds.begin(), ds.end());
rep(i,
M) { //??????????????????????????¢???????????????x??\???????????¢???????????¨?????????????¶??????????????????¨?????????x?????????
int a = roads[i].t1;
int b = roads[i].t2;
int k = max(d[a], d[b]);
R.push_back(mp(k, roads[i].d));
}
sort(R.begin(), R.end());
int t = 0; //??????????????????????????????
rep(i, N) {
if (d[i] == d[i + 1])
continue; //???????????¢???????????????????????????
int x = ds[i];
int sans = 0;
sans = C * x;
for (; R[t].first <= x && t < R.size(); t++) {
m -= R[t].second; //???????¶??????????????????¨?????¨???
}
sans += m;
ans = min(sans, ans);
}
return ans;
}
};
signed main(void) {
JOIpark ans;
printf("%lld\n", ans.solve());
} | replace | 91 | 92 | 91 | 92 | -11 | |
p00539 | C++ | Runtime Error | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef struct pair<ll, ll> P;
// 1. dp,?????°
int n, m;
ll c;
vector<P> road[100001];
ll d[100001] = {};
ll dp[100001] = {};
ll dp2[100001] = {};
int main() {
// 2. ??\???
cin >> n >> m >> c;
ll a, b, d1;
ll totalC = 0;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> d1;
road[a].push_back(P(b, i));
road[b].push_back(P(a, i));
d[i] = d1;
totalC += d1;
}
queue<P> que;
// 3.?????????
for (int i = 0; i < 100001; ++i)
dp[i] = 10000000001;
que.push(P(1, 0));
// 4.loop
//
while (!que.empty()) {
// 5.???????????????
P p1 = que.front();
que.pop();
ll pos = p1.first;
ll cost = p1.second;
if (dp[pos] < cost)
continue;
dp[pos] = cost;
for (auto it : road[pos]) {
int pos2 = it.first;
int roadm = it.second;
que.push(P(pos2, cost + d[roadm]));
}
}
priority_queue<P, vector<P>, greater<P>> que2;
ll x = 0;
ll total = 20000000000;
for (ll i = 1; i < n + 1; ++i)
que2.push(P(dp[i], i));
while (!que2.empty()) {
// 5.???????????????
P p2 = que2.top();
que2.pop();
ll cos = p2.first;
ll pos = p2.second;
dp2[pos] = 1;
x = cos;
for (auto it : road[pos]) {
ll pos2 = it.first;
ll roadm = it.second;
if (dp2[pos2] > 0)
totalC -= d[roadm];
}
total = min(total, c * x + totalC);
}
// 4.??????
cout << total << endl;
return 0;
} | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef struct pair<ll, ll> P;
// 1. dp,?????°
int n, m;
ll c;
vector<P> road[100001];
ll d[200001] = {};
ll dp[100001] = {};
ll dp2[100001] = {};
int main() {
// 2. ??\???
cin >> n >> m >> c;
ll a, b, d1;
ll totalC = 0;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> d1;
road[a].push_back(P(b, i));
road[b].push_back(P(a, i));
d[i] = d1;
totalC += d1;
}
queue<P> que;
// 3.?????????
for (int i = 0; i < 100001; ++i)
dp[i] = 10000000001;
que.push(P(1, 0));
// 4.loop
//
while (!que.empty()) {
// 5.???????????????
P p1 = que.front();
que.pop();
ll pos = p1.first;
ll cost = p1.second;
if (dp[pos] < cost)
continue;
dp[pos] = cost;
for (auto it : road[pos]) {
int pos2 = it.first;
int roadm = it.second;
que.push(P(pos2, cost + d[roadm]));
}
}
priority_queue<P, vector<P>, greater<P>> que2;
ll x = 0;
ll total = 20000000000;
for (ll i = 1; i < n + 1; ++i)
que2.push(P(dp[i], i));
while (!que2.empty()) {
// 5.???????????????
P p2 = que2.top();
que2.pop();
ll cos = p2.first;
ll pos = p2.second;
dp2[pos] = 1;
x = cos;
for (auto it : road[pos]) {
ll pos2 = it.first;
ll roadm = it.second;
if (dp2[pos2] > 0)
totalC -= d[roadm];
}
total = min(total, c * x + totalC);
}
// 4.??????
cout << total << endl;
return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p00539 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define P pair<int, int> // cost to
using namespace std;
map<int, int> S;
int mincost[100000];
vector<P> rinsetu[100000];
signed main() {
int sum = 0;
int a, b, c;
cin >> a >> b >> c;
for (int d = 0; d < b; d++) {
int e, f, g;
scanf("%lld%lld%lld", &e, &f, &g);
sum += g;
e--;
f--;
rinsetu[e].push_back(P(g, f));
rinsetu[f].push_back(P(g, e));
}
priority_queue<P, vector<P>, greater<P>> Q;
Q.push(P(0, 0));
fill(mincost, mincost + a, LLONG_MAX / 3);
mincost[0] = 0;
while (Q.size()) {
P o = Q.top();
Q.pop();
if (o.first > mincost[o.second])
continue;
for (P t : rinsetu[o.second]) {
if (mincost[t.second] > o.first + t.first) {
mincost[t.second] = o.first + t.first;
Q.push(P(mincost[t.second], t.second));
}
}
}
for (int i = 0; i < b; i++) {
for (P j : rinsetu[i]) {
if (i < j.second)
S[max(mincost[i], mincost[j.second])] += j.first;
}
}
int MIN = sum;
for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
(*p).second += (*t).second;
}
for (auto i = S.begin(); i != S.end(); i++) {
MIN = min(MIN, (*i).first * c + sum - (*i).second);
}
cout << MIN << endl;
} | #include <bits/stdc++.h>
#define int long long
#define P pair<int, int> // cost to
using namespace std;
map<int, int> S;
int mincost[200000];
vector<P> rinsetu[200000];
signed main() {
int sum = 0;
int a, b, c;
cin >> a >> b >> c;
for (int d = 0; d < b; d++) {
int e, f, g;
scanf("%lld%lld%lld", &e, &f, &g);
sum += g;
e--;
f--;
rinsetu[e].push_back(P(g, f));
rinsetu[f].push_back(P(g, e));
}
priority_queue<P, vector<P>, greater<P>> Q;
Q.push(P(0, 0));
fill(mincost, mincost + a, LLONG_MAX / 3);
mincost[0] = 0;
while (Q.size()) {
P o = Q.top();
Q.pop();
if (o.first > mincost[o.second])
continue;
for (P t : rinsetu[o.second]) {
if (mincost[t.second] > o.first + t.first) {
mincost[t.second] = o.first + t.first;
Q.push(P(mincost[t.second], t.second));
}
}
}
for (int i = 0; i < b; i++) {
for (P j : rinsetu[i]) {
if (i < j.second)
S[max(mincost[i], mincost[j.second])] += j.first;
}
}
int MIN = sum;
for (auto t = S.begin(), p = ++(S.begin()); p != S.end(); t++, p++) {
(*p).second += (*t).second;
}
for (auto i = S.begin(); i != S.end(); i++) {
MIN = min(MIN, (*i).first * c + sum - (*i).second);
}
cout << MIN << endl;
} | replace | 6 | 8 | 6 | 8 | 0 | |
p00539 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX 1010
#define INF LLONG_MAX / 4
typedef long long ll;
struct State {
ll v, cost;
State(ll v, ll cost) : v(v), cost(cost) {}
bool operator>(const State &s) const { return cost > s.cost; }
};
struct P {
ll idx, cost;
P(ll idx, ll cost) : idx(idx), cost(cost) {}
bool operator<(const P &p) const { return cost < p.cost; }
};
struct Edge {
ll to, cost;
Edge(ll to, ll cost) : to(to), cost(cost) {}
};
ll N, M, d[MAX];
vector<Edge> G[MAX];
void dijkstra() {
fill(d, d + N, INF);
d[0] = 0;
priority_queue<State, vector<State>, greater<State>> Q;
Q.push(State(0, 0));
while (!Q.empty()) {
State s = Q.top();
Q.pop();
ll v = s.v;
if (d[v] < s.cost) {
continue;
}
for (int i = 0; i < (int)G[v].size(); i++) {
Edge e = G[v][i];
if (d[v] + e.cost < d[e.to]) {
d[e.to] = d[v] + e.cost;
Q.push(State(e.to, d[e.to]));
}
}
}
}
int main() {
ll C, a, b, c, sum = 0;
cin >> N >> M >> C;
for (int i = 0; i < M; i++) {
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(Edge(b, c));
G[b].push_back(Edge(a, c));
sum += c;
}
dijkstra();
vector<P> v;
for (int i = 0; i < N; i++) {
v.push_back(P(i, d[i]));
}
sort(v.begin(), v.end());
ll res = INF, dist = 0;
set<ll> st;
for (int i = 0; i < N; i++) {
ll idx = v[i].idx;
for (int j = 0; j < (int)G[idx].size(); j++) {
if (st.find(G[idx][j].to) != st.end()) {
dist += G[idx][j].cost;
}
}
st.insert(idx);
res = min(res, sum - dist + v[i].cost * C);
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX 100010
#define INF LLONG_MAX / 4
typedef long long ll;
struct State {
ll v, cost;
State(ll v, ll cost) : v(v), cost(cost) {}
bool operator>(const State &s) const { return cost > s.cost; }
};
struct P {
ll idx, cost;
P(ll idx, ll cost) : idx(idx), cost(cost) {}
bool operator<(const P &p) const { return cost < p.cost; }
};
struct Edge {
ll to, cost;
Edge(ll to, ll cost) : to(to), cost(cost) {}
};
ll N, M, d[MAX];
vector<Edge> G[MAX];
void dijkstra() {
fill(d, d + N, INF);
d[0] = 0;
priority_queue<State, vector<State>, greater<State>> Q;
Q.push(State(0, 0));
while (!Q.empty()) {
State s = Q.top();
Q.pop();
ll v = s.v;
if (d[v] < s.cost) {
continue;
}
for (int i = 0; i < (int)G[v].size(); i++) {
Edge e = G[v][i];
if (d[v] + e.cost < d[e.to]) {
d[e.to] = d[v] + e.cost;
Q.push(State(e.to, d[e.to]));
}
}
}
}
int main() {
ll C, a, b, c, sum = 0;
cin >> N >> M >> C;
for (int i = 0; i < M; i++) {
cin >> a >> b >> c;
a--;
b--;
G[a].push_back(Edge(b, c));
G[b].push_back(Edge(a, c));
sum += c;
}
dijkstra();
vector<P> v;
for (int i = 0; i < N; i++) {
v.push_back(P(i, d[i]));
}
sort(v.begin(), v.end());
ll res = INF, dist = 0;
set<ll> st;
for (int i = 0; i < N; i++) {
ll idx = v[i].idx;
for (int j = 0; j < (int)G[idx].size(); j++) {
if (st.find(G[idx][j].to) != st.end()) {
dist += G[idx][j].cost;
}
}
st.insert(idx);
res = min(res, sum - dist + v[i].cost * C);
}
cout << res << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p00539 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MAX 10005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];
void dijkstra() {
for (int i = 0; i < MAX; i++)
d[i] = (1LL << 60);
d[1] = 0;
priority_queue<P, vector<P>, greater<P>> Q;
Q.push(P(0, 1));
while (!Q.empty()) {
P p = Q.top();
Q.pop();
ll pos = p.second, cost = p.first;
if (d[pos] < cost)
continue;
for (int i = 0; i < (int)G[pos].size(); i++) {
edge e = G[pos][i];
if (d[e.to] > cost + e.cost) {
d[e.to] = cost + e.cost;
Q.push(P(d[e.to], e.to));
}
}
}
}
int main() {
ll sum = 0;
scanf("%lld %lld %lld", &N, &M, &C);
for (int i = 0; i < M; i++) {
scanf("%lld %lld %lld", &a, &b, &c);
sum += c;
G[a].push_back((edge){b, c});
G[b].push_back((edge){a, c});
}
dijkstra();
map<ll, ll> mp;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < (int)G[i].size(); j++) {
edge e = G[i][j];
if (i < e.to)
mp[max(d[i], d[e.to])] += e.cost;
}
}
ll ans = sum;
map<ll, ll>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
ll X = it->first, D = it->second;
sum -= D;
ans = min(ans, X * C + sum);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX 100005
typedef long long ll;
typedef pair<ll, ll> P;
struct edge {
ll to, cost;
};
ll N, M, C, a, b, c;
vector<edge> G[MAX];
ll d[MAX];
void dijkstra() {
for (int i = 0; i < MAX; i++)
d[i] = (1LL << 60);
d[1] = 0;
priority_queue<P, vector<P>, greater<P>> Q;
Q.push(P(0, 1));
while (!Q.empty()) {
P p = Q.top();
Q.pop();
ll pos = p.second, cost = p.first;
if (d[pos] < cost)
continue;
for (int i = 0; i < (int)G[pos].size(); i++) {
edge e = G[pos][i];
if (d[e.to] > cost + e.cost) {
d[e.to] = cost + e.cost;
Q.push(P(d[e.to], e.to));
}
}
}
}
int main() {
ll sum = 0;
scanf("%lld %lld %lld", &N, &M, &C);
for (int i = 0; i < M; i++) {
scanf("%lld %lld %lld", &a, &b, &c);
sum += c;
G[a].push_back((edge){b, c});
G[b].push_back((edge){a, c});
}
dijkstra();
map<ll, ll> mp;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < (int)G[i].size(); j++) {
edge e = G[i][j];
if (i < e.to)
mp[max(d[i], d[e.to])] += e.cost;
}
}
ll ans = sum;
map<ll, ll>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
ll X = it->first, D = it->second;
sum -= D;
ans = min(ans, X * C + sum);
}
cout << ans << endl;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p00540 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100 // 100000
int n;
int P[SIZE] = {0};
vector<int> D;
bool check(int k) {
int dp[SIZE * 2] = {0};
for (int i = 0; i < n; i++) {
if (P[i] > 0) {
if (P[i] >= k)
dp[i] = 0;
else
dp[i] = INF;
} else
dp[i] = 1;
}
//[l,r)
int l = 0, r = n, sum = 0;
while (l + 1 < r) {
sort(&dp[l], &dp[l] + 3);
sum = dp[l] + dp[l + 1];
if (sum >= INF)
dp[r] = INF;
else
dp[r] = sum;
l += 3;
r++;
}
int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));
if (dp[l] <= b) {
return true;
}
return false;
}
int main() {
int m, p, d;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &d, &p);
P[p - 1] = d;
}
for (int i = 0; i < n - m; i++) {
scanf("%d", &d);
D.push_back(d);
}
sort(D.begin(), D.end());
//[l,r]
int l = 1, r = 1000000000, mid;
while (l < r) {
mid = (l + r + 1) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
printf("%d\n", l);
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 100000
#define INF 100000000 // 10^8
#define LLINF 4000000000000000000
#define SIZE 100000
int n;
int P[SIZE] = {0};
vector<int> D;
bool check(int k) {
int dp[SIZE * 2] = {0};
for (int i = 0; i < n; i++) {
if (P[i] > 0) {
if (P[i] >= k)
dp[i] = 0;
else
dp[i] = INF;
} else
dp[i] = 1;
}
//[l,r)
int l = 0, r = n, sum = 0;
while (l + 1 < r) {
sort(&dp[l], &dp[l] + 3);
sum = dp[l] + dp[l + 1];
if (sum >= INF)
dp[r] = INF;
else
dp[r] = sum;
l += 3;
r++;
}
int b = (int)(D.end() - lower_bound(D.begin(), D.end(), k));
if (dp[l] <= b) {
return true;
}
return false;
}
int main() {
int m, p, d;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &d, &p);
P[p - 1] = d;
}
for (int i = 0; i < n - m; i++) {
scanf("%d", &d);
D.push_back(d);
}
sort(D.begin(), D.end());
//[l,r]
int l = 1, r = 1000000000, mid;
while (l < r) {
mid = (l + r + 1) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
printf("%d\n", l);
return 0;
} | replace | 18 | 19 | 18 | 19 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.