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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
string t;
cin >> t;
long num = t.size() - 1;
rep(i, t.size()) {
if (t.at(i) == '?') {
if (i == 0) {
if (t.at(i + 1) == 'P') {
t.at(i) = 'D';
} else {
t.at(i) = 'D';
}
} else if (i == num) {
t.at(i) = 'D';
} else {
if (t.at(i - 1) == 'P') {
t.at(i) = 'D';
} else if (t.at(i + 1) == 'P') {
t.at(i) = 'D';
} else {
t.at(i) = 'D';
}
}
}
}
cout << t << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
string t;
cin >> t;
long num = t.size() - 1;
rep(i, t.size()) {
if (t.at(i) == '?') {
if (i == 0) {
t.at(i) = 'D';
} else if (i == num) {
t.at(i) = 'D';
} else {
if (t.at(i - 1) == 'P') {
t.at(i) = 'D';
} else if (t.at(i + 1) == 'P') {
t.at(i) = 'D';
} else {
t.at(i) = 'D';
}
}
}
}
cout << t << endl;
return 0;
} | replace | 12 | 17 | 12 | 13 | 0 | |
p02664 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 233
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
char s[N];
int n;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; ++i) {
putchar(s[i] == 'P' ? 'P' : 'D');
}
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 200020
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
char s[N];
int n;
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = 1; i <= n; ++i) {
putchar(s[i] == 'P' ? 'P' : 'D');
}
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02664 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll t = 1;
// cin >> t;
while (t--) {
string s;
cin >> s;
string ans = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == '?') {
ans = ans + 'D';
} else
ans = ans + s[i];
}
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll t = 1;
// cin >> t;
while (t--) {
string s;
cin >> s;
std::replace(s.begin(), s.end(), '?', 'D'); // replace all 'x' to 'y'
cout << s << endl;
}
return 0;
}
| replace | 17 | 25 | 17 | 19 | TLE | |
p02664 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int main() {
string T;
cin >> T;
for (int i = 0; T.length(); i++)
if (T[i] == '?')
T[i] = 'D';
cout << T << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int main() {
string t;
cin >> t;
for (int i = 0; i < t.length(); i++)
if (t[i] == '?')
t[i] = 'D';
cout << t << endl;
return 0;
} | replace | 18 | 24 | 18 | 24 | -11 | |
p02664 | C++ | Time Limit Exceeded | #include <iostream>
#include <string.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s.find("?") == string::npos) {
cout << s;
// cout << "? ga naiyo\n";
return 0;
}
size_t position_Phatena = 0;
size_t position_2hatena = 0;
size_t position_hatenaD = 0;
size_t position_hatena = 0;
while (s.find("?") != string::npos) {
if ((position_Phatena = s.find("P?")) != string::npos) {
s[position_hatenaD + 1] = 'D';
}
if ((position_2hatena = s.find("????")) != string::npos) {
s[position_2hatena] = 'P';
s[position_2hatena + 1] = 'D';
}
// if((position_hatenaD = s.find("?D") ) != string::npos){
// s[position_hatenaD] = 'P';
// }
if ((position_hatena = s.find("?")) != string::npos) {
s[position_hatena] = 'D';
}
}
// cout << s.find("??") << endl;
cout << s;
return 0;
} | #include <iostream>
#include <string.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s.find("?") == string::npos) {
cout << s;
// cout << "? ga naiyo\n";
return 0;
}
size_t position_Phatena = 0;
size_t position_2hatena = 0;
size_t position_hatenaD = 0;
size_t position_hatena = 0;
while (s.find("?") != string::npos) {
// if((position_Phatena = s.find("P?") ) != string::npos){
// s[position_hatenaD+1] = 'D';
// }
// if( (position_2hatena = s.find("??") ) != string::npos){
// s[position_2hatena] = 'P';
// s[position_2hatena+1] = 'D';
// }
// if((position_hatenaD = s.find("?D") ) != string::npos){
// s[position_hatenaD] = 'P';
// }
if ((position_hatena = s.find("?")) != string::npos) {
s[position_hatena] = 'D';
}
}
// cout << s.find("??") << endl;
cout << s;
return 0;
} | replace | 20 | 27 | 20 | 27 | TLE | |
p02664 | C++ | Time Limit Exceeded | #include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007
const double PI = 3.14159265358979323846;
using namespace std;
using ll = long long;
using P = pair<int, int>;
int cmpnum(const void *a, const void *b) {
long long *A = (long long *)a;
long long *B = (long long *)b;
if (*A > *B)
return 1;
if (*A < *B)
return -1;
return 0;
}
int char_sort_max(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
int char_sort_min(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
int gcd(int x, int y) {
int t;
while (y != 0) {
t = x % y;
x = y;
y = t;
}
return x;
} /*最大公約数*/
ll lcm(ll a, ll b) {
return (ll)((ll)(a * b) / (ll)(gcd(a, b)));
} /*最小公倍数*/
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
// 素数ではない
return false;
}
}
// 素数である
return true;
} /*素数判定*/
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1){};
int find(int x) {
if (d[x] < 0) {
return x;
} else {
return d[x] = find(d[x]);
}
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
struct mint {
ll x;
mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOOD)
x -= MOOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOOD - a.x) >= MOOD)
x -= MOOD;
return *this;
}
mint &operator*=(const mint a) {
if ((x *= a.x) %= MOOD)
;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inv() const { return pow(MOOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
mint kaijyo(int n) {
if (n == 0)
return 1;
mint x = kaijyo(n / 2);
x *= x;
if (n % 2 == 1)
x *= 2;
return x;
}
mint choose(int n, int a) {
mint x = 1, y = 1;
rep(i, a) {
x *= n - i;
y *= i + 1;
}
return x / y;
}
int shakutori(ll x, ll n, ll k, ll a[]) {
long long int ai, res, ok, ng, mid;
res = 0;
for (int i = 0; i < n; i++) {
// printf("%lld\n", res);
ai = a[i];
if (ai == 0) {
if (x < 0) {
res += 0;
} else {
res += n - 1;
}
} else {
if (ai <= -1) {
if (ai * ai <= x) {
res -= 1;
}
ok = n;
ng = -1;
while (ok - ng > 1) {
mid = (ok + ng) / 2;
if (a[mid] * ai <= x) {
ok = mid;
} else {
ng = mid;
}
}
res += n - ok;
} else {
if (ai * ai <= x) {
res -= 1;
}
ok = -1;
ng = n;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (a[mid] * ai <= x) {
ok = mid;
} else {
ng = mid;
}
}
res += ok + 1;
}
}
}
// printf("%lld\n", res);
return k <= res / 2;
}
int two_n(int tei, int jyousuu) {
if (jyousuu == 0)
return 1;
int x = two_n(tei, jyousuu / 2);
x *= x;
if (jyousuu % 2 == 1) {
x *= tei;
}
return x;
}
vector<vector<int>> bit_search(int N) {
vector<vector<int>> bit_sum(two_n(2, N));
int num = 0;
for (int bit = 0; bit < (1 << N); ++bit) {
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
bit_sum[num].push_back((i));
}
}
num++;
}
return bit_sum;
}
vector<vector<int>> bit_search_array(int N) {
vector<vector<int>> bit_sum(two_n(2, N), vector<int>(N, 0));
int num = 0;
for (int bit = 0; bit < (1 << N); ++bit) {
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
bit_sum[bit][i] = 1;
}
}
num++;
}
return bit_sum;
}
struct jyanken {
int tensuu;
char te;
char aite;
};
int cmp_tensu(const void *n1, const void *n2) {
if (((jyanken *)n1)->tensuu < ((jyanken *)n2)->tensuu) {
return 1;
} else if (((jyanken *)n1)->tensuu > ((jyanken *)n2)->tensuu) {
return -1;
} else {
return 0;
}
} /*structの要素でのソート*/
int main() {
char S[200000];
cin >> S;
for (int i = 0; i < strlen(S); ++i) {
if (S[i] == '?') {
S[i] = 'D';
}
}
printf("%s", S);
}
int A() {
int H1, M1, H2, M2, K;
cin >> H1 >> M1 >> H2 >> M2 >> K;
int A, B;
A = H1 * 60 + M1;
B = H2 * 60 + M2;
printf("%d", B - A - K);
}
| #include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOOD 1000000007
const double PI = 3.14159265358979323846;
using namespace std;
using ll = long long;
using P = pair<int, int>;
int cmpnum(const void *a, const void *b) {
long long *A = (long long *)a;
long long *B = (long long *)b;
if (*A > *B)
return 1;
if (*A < *B)
return -1;
return 0;
}
int char_sort_max(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
int char_sort_min(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
int gcd(int x, int y) {
int t;
while (y != 0) {
t = x % y;
x = y;
y = t;
}
return x;
} /*最大公約数*/
ll lcm(ll a, ll b) {
return (ll)((ll)(a * b) / (ll)(gcd(a, b)));
} /*最小公倍数*/
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
// 素数ではない
return false;
}
}
// 素数である
return true;
} /*素数判定*/
struct UnionFind {
vector<int> d;
UnionFind(int n = 0) : d(n, -1){};
int find(int x) {
if (d[x] < 0) {
return x;
} else {
return d[x] = find(d[x]);
}
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
if (d[x] > d[y])
swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -d[find(x)]; }
};
struct mint {
ll x;
mint(ll x) : x((x % MOOD + MOOD) % MOOD) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= MOOD)
x -= MOOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOOD - a.x) >= MOOD)
x -= MOOD;
return *this;
}
mint &operator*=(const mint a) {
if ((x *= a.x) %= MOOD)
;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
mint inv() const { return pow(MOOD - 2); }
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
mint kaijyo(int n) {
if (n == 0)
return 1;
mint x = kaijyo(n / 2);
x *= x;
if (n % 2 == 1)
x *= 2;
return x;
}
mint choose(int n, int a) {
mint x = 1, y = 1;
rep(i, a) {
x *= n - i;
y *= i + 1;
}
return x / y;
}
int shakutori(ll x, ll n, ll k, ll a[]) {
long long int ai, res, ok, ng, mid;
res = 0;
for (int i = 0; i < n; i++) {
// printf("%lld\n", res);
ai = a[i];
if (ai == 0) {
if (x < 0) {
res += 0;
} else {
res += n - 1;
}
} else {
if (ai <= -1) {
if (ai * ai <= x) {
res -= 1;
}
ok = n;
ng = -1;
while (ok - ng > 1) {
mid = (ok + ng) / 2;
if (a[mid] * ai <= x) {
ok = mid;
} else {
ng = mid;
}
}
res += n - ok;
} else {
if (ai * ai <= x) {
res -= 1;
}
ok = -1;
ng = n;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (a[mid] * ai <= x) {
ok = mid;
} else {
ng = mid;
}
}
res += ok + 1;
}
}
}
// printf("%lld\n", res);
return k <= res / 2;
}
int two_n(int tei, int jyousuu) {
if (jyousuu == 0)
return 1;
int x = two_n(tei, jyousuu / 2);
x *= x;
if (jyousuu % 2 == 1) {
x *= tei;
}
return x;
}
vector<vector<int>> bit_search(int N) {
vector<vector<int>> bit_sum(two_n(2, N));
int num = 0;
for (int bit = 0; bit < (1 << N); ++bit) {
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
bit_sum[num].push_back((i));
}
}
num++;
}
return bit_sum;
}
vector<vector<int>> bit_search_array(int N) {
vector<vector<int>> bit_sum(two_n(2, N), vector<int>(N, 0));
int num = 0;
for (int bit = 0; bit < (1 << N); ++bit) {
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
bit_sum[bit][i] = 1;
}
}
num++;
}
return bit_sum;
}
struct jyanken {
int tensuu;
char te;
char aite;
};
int cmp_tensu(const void *n1, const void *n2) {
if (((jyanken *)n1)->tensuu < ((jyanken *)n2)->tensuu) {
return 1;
} else if (((jyanken *)n1)->tensuu > ((jyanken *)n2)->tensuu) {
return -1;
} else {
return 0;
}
} /*structの要素でのソート*/
int main() {
char S[200000];
cin >> S;
int A = strlen(S);
for (int i = 0; i < A; ++i) {
if (S[i] == '?') {
S[i] = 'D';
}
}
printf("%s", S);
}
int A() {
int H1, M1, H2, M2, K;
cin >> H1 >> M1 >> H2 >> M2 >> K;
int A, B;
A = H1 * 60 + M1;
B = H2 * 60 + M2;
printf("%d", B - A - K);
}
| replace | 282 | 283 | 282 | 284 | TLE | |
p02664 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
char arr[200000 + 10];
int main() {
scanf("%s", arr);
for (int i = 0; i < strlen(arr); i++) {
if (arr[i] != '?')
printf("%c", arr[i]);
else
printf("D");
}
printf("\n");
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
char arr[200000 + 10];
int main() {
scanf("%s", arr);
int len = strlen(arr);
for (int i = 0; i < len; i++) {
if (arr[i] != '?')
printf("%c", arr[i]);
else
printf("D");
}
printf("\n");
return 0;
} | replace | 11 | 12 | 11 | 13 | TLE | |
p02664 | C++ | Runtime Error | // Postdocs
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define irep(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define nvarep(n, a) \
ll n; \
cin >> n; \
vector<ll> a(n); \
rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type) \
vector<type> a(n); \
rep(i, 0, n) cin >> a[i]
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\n"
#define spa " "
#define cout15(a) printf("%.15f\n", a)
template <class T> inline bool chmin(T &a, T b, bool f) {
if (f == 1) {
if (a > b) {
a = b;
return true;
}
return false;
} else if (f == 0) {
if (a < b) {
a = b;
return true;
}
return false;
}
return false;
}
typedef pair<ll, string> p;
using g = vector<vector<ll>>;
const int inf = 2147483647; // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18
signed main() {
string t;
cin >> t;
if (t[0] == '?')
t[0] = 'P';
if (t[t.size() - 1] == '?')
t[t.size() - 1] = 'D';
rep(i, 1, t.size() - 2) {
if (t[i] == '?') {
if (t[i - 1] == 'P')
t[i] = 'D';
else if (t[i + 1] = 'D')
t[i] = 'P';
else
t[i] = 'P';
}
}
cout << t << nnn;
return 0;
} | // Postdocs
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define irep(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define nvarep(n, a) \
ll n; \
cin >> n; \
vector<ll> a(n); \
rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type) \
vector<type> a(n); \
rep(i, 0, n) cin >> a[i]
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\n"
#define spa " "
#define cout15(a) printf("%.15f\n", a)
template <class T> inline bool chmin(T &a, T b, bool f) {
if (f == 1) {
if (a > b) {
a = b;
return true;
}
return false;
} else if (f == 0) {
if (a < b) {
a = b;
return true;
}
return false;
}
return false;
}
typedef pair<ll, string> p;
using g = vector<vector<ll>>;
const int inf = 2147483647; // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18
signed main() {
string t;
cin >> t;
rep(i, 0, t.size()) {
if (t[i] == '?')
t[i] = 'D';
}
cout << t << nnn;
return 0;
} | replace | 48 | 61 | 48 | 51 | 0 | |
p02664 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
namespace HABIT {
#define f(a, b, c, d) for (register int a = b, c = d; a <= c; a++)
#define g(a, b, c, d) for (register int a = b, c = d; a >= c; a--)
#define LOCAL
typedef int i32;
typedef unsigned int u32;
typedef long long int i64;
typedef unsigned long long int u64;
typedef pair<i32, i32> pii;
const i32 MAXN = 1024;
typedef i32 ar[MAXN];
template <typename T> inline T Max(T a) { return a; }
template <typename T, typename... Args> inline T Max(T a, Args... args) {
T b = Max(args...);
return (a > b) ? a : b;
}
template <typename T> inline T Min(T a) { return a; }
template <typename T, typename... Args> inline T Min(T a, Args... args) {
T b = Min(args...);
return (a < b) ? a : b;
}
#ifdef LOCAL
inline char gc() { return getchar(); }
#else
inline char gc() {
static char s[1 << 20 | 1] = {0}, *p1 = s, *p2 = s;
return (p1 == p2) && (p2 = (p1 = s) + fread(s, 1, 1 << 20, stdin), p1 == p2)
? EOF
: *(p1++);
}
#endif
inline i64 read() {
register i64 ans = 0;
register char c = gc();
register bool neg = 0;
while (c < 48 || c > 57)
neg ^= !(c ^ '-'), c = gc();
while (c >= 48 && c <= 57)
ans = (ans << 3) + (ans << 1) + (c ^ 48), c = gc();
return neg ? -ans : ans;
}
char Output_Ans[1 << 20 | 1], *Output_Cur = Output_Ans;
inline void output() {
Output_Cur -= fwrite(Output_Ans, 1, Output_Cur - Output_Ans, stdout);
}
inline void print(char c) {
if (Output_Cur - Output_Ans + 1 >> 20)
output();
*(Output_Cur++) = c;
}
inline void print(char *s) {
while (*s)
print(*(s++));
}
inline void print(u64 x) {
if (!x) {
print('0');
return;
}
char buf[30] = {0}, *p = buf + 28;
while (x)
*(p--) = x % 10 + 48, x /= 10;
print(p + 1);
}
inline void print(u32 x) { print((u64)x); }
inline void print(i64 x) {
if (x < 0)
print('-'), x = -x;
print((u64)x);
}
inline void print(i32 x) {
if (x < 0)
print('-'), x = -x;
print((u64)x);
}
} // namespace HABIT
using namespace HABIT;
char s[200010];
int main() {
scanf("%s", s);
for (int i = 0; i < strlen(s); i++)
if (s[i] == '?')
s[i] = 'D';
print(s);
output();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
namespace HABIT {
#define f(a, b, c, d) for (register int a = b, c = d; a <= c; a++)
#define g(a, b, c, d) for (register int a = b, c = d; a >= c; a--)
#define LOCAL
typedef int i32;
typedef unsigned int u32;
typedef long long int i64;
typedef unsigned long long int u64;
typedef pair<i32, i32> pii;
const i32 MAXN = 1024;
typedef i32 ar[MAXN];
template <typename T> inline T Max(T a) { return a; }
template <typename T, typename... Args> inline T Max(T a, Args... args) {
T b = Max(args...);
return (a > b) ? a : b;
}
template <typename T> inline T Min(T a) { return a; }
template <typename T, typename... Args> inline T Min(T a, Args... args) {
T b = Min(args...);
return (a < b) ? a : b;
}
#ifdef LOCAL
inline char gc() { return getchar(); }
#else
inline char gc() {
static char s[1 << 20 | 1] = {0}, *p1 = s, *p2 = s;
return (p1 == p2) && (p2 = (p1 = s) + fread(s, 1, 1 << 20, stdin), p1 == p2)
? EOF
: *(p1++);
}
#endif
inline i64 read() {
register i64 ans = 0;
register char c = gc();
register bool neg = 0;
while (c < 48 || c > 57)
neg ^= !(c ^ '-'), c = gc();
while (c >= 48 && c <= 57)
ans = (ans << 3) + (ans << 1) + (c ^ 48), c = gc();
return neg ? -ans : ans;
}
char Output_Ans[1 << 20 | 1], *Output_Cur = Output_Ans;
inline void output() {
Output_Cur -= fwrite(Output_Ans, 1, Output_Cur - Output_Ans, stdout);
}
inline void print(char c) {
if (Output_Cur - Output_Ans + 1 >> 20)
output();
*(Output_Cur++) = c;
}
inline void print(char *s) {
while (*s)
print(*(s++));
}
inline void print(u64 x) {
if (!x) {
print('0');
return;
}
char buf[30] = {0}, *p = buf + 28;
while (x)
*(p--) = x % 10 + 48, x /= 10;
print(p + 1);
}
inline void print(u32 x) { print((u64)x); }
inline void print(i64 x) {
if (x < 0)
print('-'), x = -x;
print((u64)x);
}
inline void print(i32 x) {
if (x < 0)
print('-'), x = -x;
print((u64)x);
}
} // namespace HABIT
using namespace HABIT;
char s[200010];
int main() {
char c;
while (c = gc(), c != EOF)
print((c == '?') ? 'D' : c);
output();
return 0;
} | replace | 87 | 92 | 87 | 90 | TLE | |
p02664 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define int long long
#define pb(x) push_back(x)
#define gcd(a, b) __gcd(a, b)
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b) / gcd(a, b)
#define bits(x) __builtin_popcountll(x)
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
int32_t main() {
fast;
#ifndef ONLINE_JUDGE
freopen("goodluckin.txt", "r", stdin);
freopen("goodluckout.txt", "w", stdout);
#endif
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < s.size();) {
if (s[i] == '?') {
s[i] = 'D';
}
}
cout << s;
}
| #include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define int long long
#define pb(x) push_back(x)
#define gcd(a, b) __gcd(a, b)
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b) / gcd(a, b)
#define bits(x) __builtin_popcountll(x)
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
using namespace std;
int32_t main() {
fast;
#ifndef ONLINE_JUDGE
freopen("goodluckin.txt", "r", stdin);
freopen("goodluckout.txt", "w", stdout);
#endif
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < s.size(); i++) {
if (s[i] == '?') {
s[i] = 'D';
}
}
cout << s;
}
| replace | 23 | 24 | 23 | 24 | TLE | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 100;
int main() {
char s[M];
while (scanf("%s", s) == 1) {
int n = strlen(s);
for (int i = 0; i < n; i++)
if (s[i] == '?')
s[i] = 'D';
printf("%s\n", s);
}
}
| #include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 100;
int main() {
char s[M];
while (scanf("%s", s) == 1) {
int n = strlen(s);
for (int i = 0; i < n; i++)
if (s[i] == '?')
s[i] = 'D';
printf("%s\n", s);
}
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
string T;
cin >> T;
if (T.at(0) == '?') {
if (T.at(1) != 'P')
T.at(0) = 'P';
}
for (int i = 1; i < T.size() - 1; i++) {
if (T.at(i) == '?') {
if (T.at(i + 1) != 'P' && T.at(i - 1) == 'D') {
T.at(i) = 'P';
} else
T.at(i) = 'D';
}
}
if (T.at(T.size() - 1) == '?') {
T.at(T.size() - 1) = 'D';
}
cout << T << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
string T;
cin >> T;
rep(i, T.size()) {
if (T.at(i) == '?')
T.at(i) = 'D';
}
cout << T << endl;
return 0;
} | replace | 8 | 23 | 8 | 11 | 0 | |
p02664 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char s[N];
int n;
int main() {
scanf("%s", s);
n = strlen(s);
int uk = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'D' || s[i] == '?')
printf("D");
else
printf("P");
}
printf("\n");
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char s[N];
int n;
int main() {
scanf("%s", s);
n = strlen(s);
int uk = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'D' || s[i] == '?')
printf("D");
else
printf("P");
}
printf("\n");
return 0;
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size() - 1)
#define ms(a, b) memset(a, b, sizeof a)
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define DF(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
inline int read() {
char ch = getchar();
int w = 1, c = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
w = -1;
for (; isdigit(ch); ch = getchar())
c = (c << 1) + (c << 3) + (ch ^ 48);
return w * c;
}
const int M = 1e5 + 10;
char ch[M];
int dp[M][2], fr[M][2];
void dg(int x, int o) {
if (!x)
return;
dg(x - 1, fr[x][o]);
cout << (o ? 'P' : 'D');
}
int n;
int main() {
scanf("%s", ch + 1);
n = strlen(ch + 1);
dp[0][0] = 0;
dp[0][1] = -1e9;
F(i, 1, n) {
F(j, 0, 1) dp[i][j] = -1e9;
if (ch[i] == 'D') {
if (dp[i - 1][1] + 2 > dp[i - 1][0] + 1)
fr[i][0] = 1;
else
fr[i][0] = 0;
dp[i][0] = max(dp[i - 1][1] + 2, dp[i - 1][0] + 1);
}
if (ch[i] == 'P') {
if (dp[i - 1][1] > dp[i - 1][0])
fr[i][1] = 1;
else
fr[i][1] = 0;
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]);
}
if (ch[i] == '?') {
if (dp[i - 1][1] + 2 > dp[i - 1][0] + 1)
fr[i][0] = 1;
else
fr[i][0] = 0;
dp[i][0] = max(dp[i - 1][1] + 2, dp[i - 1][0] + 1);
if (dp[i - 1][1] > dp[i - 1][0])
fr[i][1] = 1;
else
fr[i][1] = 0;
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]);
}
}
if (dp[n][0] > dp[n][1])
dg(n, 0);
else
dg(n, 1);
return 0;
}
| #include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size() - 1)
#define ms(a, b) memset(a, b, sizeof a)
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define DF(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
inline int read() {
char ch = getchar();
int w = 1, c = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
w = -1;
for (; isdigit(ch); ch = getchar())
c = (c << 1) + (c << 3) + (ch ^ 48);
return w * c;
}
const int M = 2e5 + 10;
char ch[M];
int dp[M][2], fr[M][2];
void dg(int x, int o) {
if (!x)
return;
dg(x - 1, fr[x][o]);
cout << (o ? 'P' : 'D');
}
int n;
int main() {
scanf("%s", ch + 1);
n = strlen(ch + 1);
dp[0][0] = 0;
dp[0][1] = -1e9;
F(i, 1, n) {
F(j, 0, 1) dp[i][j] = -1e9;
if (ch[i] == 'D') {
if (dp[i - 1][1] + 2 > dp[i - 1][0] + 1)
fr[i][0] = 1;
else
fr[i][0] = 0;
dp[i][0] = max(dp[i - 1][1] + 2, dp[i - 1][0] + 1);
}
if (ch[i] == 'P') {
if (dp[i - 1][1] > dp[i - 1][0])
fr[i][1] = 1;
else
fr[i][1] = 0;
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]);
}
if (ch[i] == '?') {
if (dp[i - 1][1] + 2 > dp[i - 1][0] + 1)
fr[i][0] = 1;
else
fr[i][0] = 0;
dp[i][0] = max(dp[i - 1][1] + 2, dp[i - 1][0] + 1);
if (dp[i - 1][1] > dp[i - 1][0])
fr[i][1] = 1;
else
fr[i][1] = 0;
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0]);
}
}
if (dp[n][0] > dp[n][1])
dg(n, 0);
else
dg(n, 1);
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string T;
cin >> T;
if (T.at(0) == '?') {
T.at(0) = 'D';
}
for (int i = 1; i < T.size(); i++) {
if (T.at(i) == '?') {
if ((T.at(i - 1)) == 'P') {
T.at(i) = 'D';
}
if ((T.at(i - 1)) == 'D') {
if (T.at(i + 1) == 'P') {
T.at(i) = 'D';
} else {
T.at(i) = 'P';
}
}
}
}
cout << T << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string T;
cin >> T;
if (T.at(0) == '?') {
T.at(0) = 'D';
}
if (T.at(T.size() - 1) == '?') {
T.at(T.size() - 1) = 'D';
}
for (int i = 1; i < T.size() - 1; i++) {
if (T.at(i) == '?') {
if ((T.at(i - 1)) == 'P') {
T.at(i) = 'D';
}
if ((T.at(i - 1)) == 'D') {
if (T.at(i + 1) == 'P') {
T.at(i) = 'D';
} else {
T.at(i) = 'P';
}
}
}
}
cout << T << endl;
} | replace | 9 | 10 | 9 | 13 | 0 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T> inline void chkmax(T &x, T y) {
if (x < y)
x = y;
}
template <typename T> inline void chkmin(T &x, T y) {
if (x > y)
x = y;
}
inline int read() {
#define nc getchar()
int x = 0;
char c = nc;
while (c < 48)
c = nc;
while (c > 47)
x = x * 10 + (c ^ 48), c = nc;
return x;
#undef nc
}
typedef long long ll;
const int maxn = 1e5 + 10;
char str[maxn];
void solve() {
scanf("%s", str + 1);
int n = strlen(str + 1);
int ans = 0;
rep(i, 1, n) {
if (str[i] == 'P') {
putchar('P');
ans += i < n && str[i + 1] != 'P';
} else {
putchar('D');
ans++;
}
}
// cout << ans<<endl;
}
int main() {
// int T = read();
// while (T--) solve();
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T> inline void chkmax(T &x, T y) {
if (x < y)
x = y;
}
template <typename T> inline void chkmin(T &x, T y) {
if (x > y)
x = y;
}
inline int read() {
#define nc getchar()
int x = 0;
char c = nc;
while (c < 48)
c = nc;
while (c > 47)
x = x * 10 + (c ^ 48), c = nc;
return x;
#undef nc
}
typedef long long ll;
const int maxn = 1e6 + 10;
char str[maxn];
void solve() {
scanf("%s", str + 1);
int n = strlen(str + 1);
int ans = 0;
rep(i, 1, n) {
if (str[i] == 'P') {
putchar('P');
ans += i < n && str[i + 1] != 'P';
} else {
putchar('D');
ans++;
}
}
// cout << ans<<endl;
}
int main() {
// int T = read();
// while (T--) solve();
solve();
return 0;
} | replace | 31 | 32 | 31 | 32 | 0 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
string t;
cin >> t;
for (int i = 0; i < t.size(); i++) {
if (t.at(i) == '?') {
if (i < t.size() - 1) {
if (t.at(i + 1) == 'D') {
t.at(i) = 'P';
}
if (i > 0) {
if (t.at(i - 1) == 'P') {
t.at(i) = 'D';
} else {
t.at(i) = 'P';
}
}
} else {
if (t.at(i - 1) == 'P') {
t.at(i) = 'D';
} else {
t.at(i) = 'P';
}
}
}
}
cout << t << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string t;
cin >> t;
for (int i = 0; i < t.size(); i++) {
if (t.at(i) == '?') {
t.at(i) = 'D';
}
}
cout << t << endl;
} | replace | 8 | 26 | 8 | 9 | 0 | |
p02664 | C++ | Runtime Error | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;
int main() {
string t;
cin >> t;
int N = t.size();
rep(0, i, N) {
if (t.at(i) == '?') {
if (i == 0) {
if (t.at(i + 1) == 'D') {
t.at(i) = 'P';
} else {
t.at(i) = 'D';
}
} else if (i == N - 1) {
t.at(i) = 'D';
} else if (t.at(i + 1) == 'D') {
t.at(i) = 'P';
} else {
t.at(i) = 'D';
}
}
}
cout << t << endl;
} | #include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;
int main() {
string t;
cin >> t;
int N = t.size();
rep(0, i, N) {
if (t.at(i) == '?') {
t.at(i) = 'D';
}
}
cout << t << endl;
} | replace | 34 | 48 | 34 | 35 | 0 | |
p02664 | C++ | Runtime Error | #include <stdio.h>
#include <string.h>
char x[100010];
int main() {
scanf("%s", x + 1);
int a = strlen(x + 1);
for (int i = 1; i <= a; i++)
if (x[i] == '?')
x[i] = 'D';
for (int i = 1; i <= a; i++)
printf("%c", x[i]);
} | #include <stdio.h>
#include <string.h>
char x[200010];
int main() {
scanf("%s", x + 1);
int a = strlen(x + 1);
for (int i = 1; i <= a; i++)
if (x[i] == '?')
x[i] = 'D';
for (int i = 1; i <= a; i++)
printf("%c", x[i]);
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02664 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string T;
cin >> T;
for (long long i = 0; i < 200001; i++) {
if (T[i] == '?') {
T[i] = 'D';
}
}
cout << T << endl;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string T;
cin >> T;
for (long long i = 0; i < T.length(); i++) {
if (T[i] == '?') {
T[i] = 'D';
}
}
cout << T << endl;
} | replace | 8 | 9 | 8 | 9 | -11 | |
p02664 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <cstring>
using namespace std;
int main() {
char T[20000];
cin >> T;
int i, n = strlen(T);
for (i = 0; i < n; i++) {
if (T[i] == '?') {
T[i] = 'D';
}
cout << T[i];
}
}
| #include <bits/stdc++.h>
#include <cstring>
using namespace std;
int main() {
char T[200000];
cin >> T;
int i, n = strlen(T);
for (i = 0; i < n; i++) {
if (T[i] == '?') {
T[i] = 'D';
}
cout << T[i];
}
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02664 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int len = s.size();
for (int i = 0; i < len;) {
if (s[i] == '?') {
s[i] = 'D';
}
}
cout << s << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++) {
if (s[i] == '?') {
s[i] = 'D';
}
}
cout << s << endl;
}
| replace | 6 | 7 | 6 | 7 | TLE | |
p02664 | C++ | Runtime Error |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define ALL(a) a.begin(), a.end()
#define ALLN(a, n) (a, a + n)
#define MAX 10000007
#define MIN -10000007
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t;
t = 1;
// cin>>t;
while (t--) {
string s;
getline(cin, s);
int n = s.length();
REP(i, 0, n - 1) {
if (s.at(i) == '?') {
if (i + 1 <= n - 1 && s.at(i - 1) == 'D')
s.at(i) = 'P';
else if (i + 1 <= n - 1 && s.at(i - 1) == 'P')
s.at(i) = 'D';
else if (i - 1 >= 0 && s.at(i - 1) == 'P')
s.at(i) = 'D';
else
s.at(i) = 'P';
}
}
cout << s;
}
cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define ALL(a) a.begin(), a.end()
#define ALLN(a, n) (a, a + n)
#define MAX 10000007
#define MIN -10000007
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t;
t = 1;
// cin>>t;
while (t--) {
string s;
getline(cin, s);
int n = s.length();
REP(i, 0, n - 1) {
if (s.at(i) == '?') {
s.at(i) = 'D';
}
}
cout << s;
}
cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}
| replace | 30 | 38 | 30 | 32 | 0 | |
p02664 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int find(string str, string word) {
int count = 0;
for (string::size_type pos = 0; (pos = str.find(word, pos)) != string::npos;
pos += word.size()) {
++count;
}
return count;
}
int main() {
string t;
cin >> t;
int q = count(t.cbegin(), t.cend(), '?');
rep(i, q) {
string t1 = t;
string t2 = t;
int p = t.rfind("?");
t1[p] = 'P';
t2[p] = 'D';
int totald1 = count(t1.cbegin(), t1.cend(), 'D');
totald1 += find(t1, "PD");
int totald2 = count(t2.cbegin(), t2.cend(), 'D');
totald2 += find(t2, "PD");
if (totald1 >= totald2) {
t = t1;
} else {
t = t2;
}
}
cout << t << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int find(string str, string word) {
int count = 0;
for (string::size_type pos = 0; (pos = str.find(word, pos)) != string::npos;
pos += word.size()) {
++count;
}
return count;
}
int main() {
string t;
cin >> t;
for (int i = 0; i < t.length(); i++)
if (t[i] == '?')
t[i] = 'D';
cout << t << endl;
return 0;
}
| replace | 18 | 35 | 18 | 21 | TLE | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
template <class T> inline void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
template <class T> inline void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll N;
cin >> N;
vector<ll> A(N + 1);
REP(i, N + 1) cin >> A[i];
if (N == 0) {
if (A[0] == 1) {
cout << 1 << "\n";
} else {
cout << -1 << "\n";
}
} else {
bool able = 1;
ll ablecount = 1;
REP(i, N + 1) {
ablecount -= A[i];
if (ablecount < 0) {
able = 0;
break;
}
ablecount *= 2;
if (ablecount > LLONG_MAX / 20) {
able = 1;
break;
}
}
if (able) {
return -1;
} else {
cout << -1 << "\n";
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
template <class T> inline void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
template <class T> inline void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll N;
cin >> N;
vector<ll> A(N + 1);
REP(i, N + 1) cin >> A[i];
if (N == 0) {
if (A[0] == 1) {
cout << 1 << "\n";
} else {
cout << -1 << "\n";
}
} else {
bool able = 1;
ll ablecount = 1;
REP(i, N + 1) {
ablecount -= A[i];
if (ablecount < 0) {
able = 0;
break;
}
ablecount *= 2;
if (ablecount > LLONG_MAX / 20) {
able = 1;
break;
}
}
if (able) {
ll maxi = 0;
vector<ll> nowwant(N + 1);
RREP(i, N + 1) {
if (i != N) {
nowwant[i] = nowwant[i + 1] + A[i];
} else {
nowwant[N] = A[i];
}
maxi += nowwant[i];
}
ll able = 1;
REP(i, N + 1) {
if (nowwant[i] - able >= 0) {
maxi -= nowwant[i] - able;
able -= A[i];
able *= 2;
} else {
break;
}
}
cout << maxi << "\n";
} else {
cout << -1 << "\n";
}
}
} | replace | 59 | 60 | 59 | 80 | 255 | |
p02665 | C++ | Runtime Error | #include <cstdio>
typedef long long LL;
const int MN = 200005;
int N;
LL A[MN], B[MN], C[MN];
int main() {
scanf("%d", &N);
for (int i = 0; i <= N; ++i)
scanf("%lld", &A[i]);
if (!N)
return puts(A[0] == 1 ? "1" : "-1");
for (int i = N; i >= 0; --i)
B[i] = B[i + 1] + A[i];
C[0] = 1 - A[0];
for (int i = 1; i <= N; ++i) {
C[i] = 2 * C[i - 1] - A[i];
if (C[i] < 0 || C[i] + A[i] == 0)
return puts("-1"), 0;
if (C[i] + A[i] > B[i])
C[i] = B[i] - A[i];
}
LL Ans = 0;
for (int i = 0; i <= N; ++i)
Ans += A[i] + C[i];
printf("%lld\n", Ans);
return 0;
} | #include <cstdio>
typedef long long LL;
const int MN = 200005;
int N;
LL A[MN], B[MN], C[MN];
int main() {
scanf("%d", &N);
for (int i = 0; i <= N; ++i)
scanf("%lld", &A[i]);
if (!N)
return puts(A[0] == 1 ? "1" : "-1"), 0;
for (int i = N; i >= 0; --i)
B[i] = B[i + 1] + A[i];
C[0] = 1 - A[0];
for (int i = 1; i <= N; ++i) {
C[i] = 2 * C[i - 1] - A[i];
if (C[i] < 0 || C[i] + A[i] == 0)
return puts("-1"), 0;
if (C[i] + A[i] > B[i])
C[i] = B[i] - A[i];
}
LL Ans = 0;
for (int i = 0; i <= N; ++i)
Ans += A[i] + C[i];
printf("%lld\n", Ans);
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02665 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define maxn 100005
using namespace std;
#define INF 1000000000000000LL
ll A[maxn];
ll lim[maxn];
int main() {
int n;
cin >> n;
rep(i, n + 1) cin >> A[i];
lim[0] = 1;
for (int i = 1; i < n + 1; i++) {
ll m = (lim[i - 1] - A[i - 1]) * 2LL;
if (m < 0) {
cout << -1 << endl;
return 0;
}
lim[i] = min(INF, m);
}
if (lim[n] < A[n]) {
cout << -1 << endl;
return 0;
}
ll ret = A[n], now = A[n];
for (int i = n - 1; i >= 0; i++) {
now += A[i];
now = min(now, lim[i]);
ret += now;
}
cout << ret << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define prtd(var, i) cout << fixed << setprecision(i) << var << endl;
#define ll long long
#define maxn 100005
using namespace std;
#define INF 1000000000000000LL
ll A[maxn];
ll lim[maxn];
int main() {
int n;
cin >> n;
rep(i, n + 1) cin >> A[i];
lim[0] = 1;
for (int i = 1; i < n + 1; i++) {
ll m = (lim[i - 1] - A[i - 1]) * 2LL;
if (m < 0) {
cout << -1 << endl;
return 0;
}
lim[i] = min(INF, m);
}
if (lim[n] < A[n]) {
cout << -1 << endl;
return 0;
}
ll ret = A[n], now = A[n];
for (int i = n - 1; i >= 0; i--) {
now += A[i];
now = min(now, lim[i]);
ret += now;
}
cout << ret << endl;
} | replace | 30 | 31 | 30 | 31 | TLE | |
p02665 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
ll a = 1;
while (p > 0) {
if (p % 2 == 0) {
x *= x;
p /= 2;
} else {
a *= x;
p--;
}
}
return a;
}
ll mpower(ll x, ll p) {
ll a = 1;
while (p > 0) {
if (p % 2 == 0) {
x = x * x % mod;
p /= 2;
} else {
a = a * x % mod;
p--;
}
}
return a;
}
ll ac(ll n, ll k) {
ll a = 1;
rep(i, 1, k) {
a *= n - i + 1;
a /= i;
}
return a;
}
ll mc(ll n, ll m) {
ll k = 1, l = 1;
rep(i, n - m + 1, n + 1) k = k * i % mod;
rep(i, 1, m + 1) l = l * i % mod;
l = mpower(l, mod - 2);
return k * l % mod;
}
int main() {
ll n, i;
cin >> n;
vector<ll> a(n + 1), l(n + 1), r(n + 1);
rep(i, 0, n + 1) cin >> a[i];
l[n] = r[n] = a[n];
for (i = n - 1; i >= 0ll; i--) {
l[i] = a[i] + (l[i + 1] + 1) / 2;
r[i] = a[i] + r[i + 1];
}
if (l[0] > 1)
return puts("-1");
ll c = 1, ans = 0;
for (i = 0ll; i < n; i++) {
ans += c;
c -= a[i];
c = min(c * 2, r[i + 1]);
}
printf("%lld\n", ans + c);
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
ll a = 1;
while (p > 0) {
if (p % 2 == 0) {
x *= x;
p /= 2;
} else {
a *= x;
p--;
}
}
return a;
}
ll mpower(ll x, ll p) {
ll a = 1;
while (p > 0) {
if (p % 2 == 0) {
x = x * x % mod;
p /= 2;
} else {
a = a * x % mod;
p--;
}
}
return a;
}
ll ac(ll n, ll k) {
ll a = 1;
rep(i, 1, k) {
a *= n - i + 1;
a /= i;
}
return a;
}
ll mc(ll n, ll m) {
ll k = 1, l = 1;
rep(i, n - m + 1, n + 1) k = k * i % mod;
rep(i, 1, m + 1) l = l * i % mod;
l = mpower(l, mod - 2);
return k * l % mod;
}
int main() {
ll n, i;
cin >> n;
vector<ll> a(n + 1), l(n + 1), r(n + 1);
rep(i, 0, n + 1) cin >> a[i];
l[n] = r[n] = a[n];
for (i = n - 1; i >= 0ll; i--) {
l[i] = a[i] + (l[i + 1] + 1) / 2;
r[i] = a[i] + r[i + 1];
}
if (l[0] > 1ll) {
puts("-1");
return 0;
}
ll c = 1, ans = 0;
for (i = 0ll; i < n; i++) {
ans += c;
c -= a[i];
c = min(c * 2, r[i + 1]);
}
printf("%lld\n", ans + c);
return 0;
}
| replace | 81 | 83 | 81 | 85 | 0 | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<double, int>;
const ll M = 1e9 + 7;
ll pou(ll x, ll a) {
ll an = 1;
while (a > 0) {
if (a & 1)
an = an * x;
x = x * x;
a >>= 1;
}
return an;
}
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i <= n; i++)
cin >> a[i];
ll sm = 0;
for (int i = 0; i <= n; i++)
sm += a[i];
ll nw = 1, an = 0;
for (ll i = 0; i <= n; i++) {
an += nw;
nw -= a[i];
sm -= a[i];
// cout<<i<<" "<<an<<" "<<nw<<" "<<sm<<endl;
if (nw < 0) {
cout << -1 << endl;
return 0;
}
nw = min(2 * nw, sm);
}
cout << an << endl;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<double, int>;
const ll M = 1e9 + 7;
ll pou(ll x, ll a) {
ll an = 1;
while (a > 0) {
if (a & 1)
an = an * x;
x = x * x;
a >>= 1;
}
return an;
}
int main() {
int n;
cin >> n;
ll a[n + 1];
for (int i = 0; i <= n; i++)
cin >> a[i];
ll sm = 0;
for (int i = 0; i <= n; i++)
sm += a[i];
ll nw = 1, an = 0;
for (ll i = 0; i <= n; i++) {
an += nw;
nw -= a[i];
sm -= a[i];
// cout<<i<<" "<<an<<" "<<nw<<" "<<sm<<endl;
if (nw < 0) {
cout << -1 << endl;
return 0;
}
nw = min(2 * nw, sm);
}
cout << an << endl;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int main() {
unsigned int n;
unsigned long ans = 0;
unsigned long now;
double check;
bool solved = true;
int i, j, k;
cin >> n;
unsigned int *a = new unsigned int[n + 1];
unsigned long *top = new unsigned long[n + 1];
cin >> a[0];
if (a[0] > 1) {
top[0] = 0;
solved = false;
} else {
top[0] = 1 - a[0];
}
for (i = 1; i < n; i++) {
cin >> a[i];
if (top[i - 1] < a[i - 1]) {
solved = false;
top[i] = 0;
} else {
top[i] = (top[i - 1] - a[i - 1]) * 2;
}
if (top[i] < a[i]) {
solved = false;
}
// top[i] = top[i] - a[i-1];
// cout << i << " -> " << top[i] << endl;
}
if (n > 0) {
cin >> a[n];
}
if (a[n] > top[n - 1] * 2) {
solved = false;
} else {
top[n] = a[n];
}
if (solved == false) {
cout << -1 << endl;
return 0;
}
ans = now = a[n];
for (i = n - 1; i >= 0; i--) {
now += a[i];
// cout << i << ": " << now << " / " << top[i] << endl;
now = min(now, top[i]);
ans += now;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
using namespace std;
int main() {
unsigned int n;
unsigned long ans = 0;
unsigned long now;
double check;
bool solved = true;
int i, j, k;
cin >> n;
unsigned int *a = new unsigned int[n + 1];
unsigned long *top = new unsigned long[n + 1];
cin >> a[0];
if (a[0] > 1) {
top[0] = 0;
solved = false;
} else {
top[0] = 1 - a[0];
}
for (i = 1; i < n; i++) {
cin >> a[i];
if (top[i - 1] < a[i - 1]) {
solved = false;
top[i] = 0;
} else {
top[i] = (top[i - 1] - a[i - 1]) * 2;
}
if (top[i] < a[i]) {
solved = false;
}
// top[i] = top[i] - a[i-1];
// cout << i << " -> " << top[i] << endl;
}
if (n > 0) {
cin >> a[n];
if (a[n] > top[n - 1] * 2) {
solved = false;
} else {
top[n] = a[n];
}
}
if (solved == false) {
cout << -1 << endl;
return 0;
}
ans = now = a[n];
for (i = n - 1; i >= 0; i--) {
now += a[i];
// cout << i << ": " << now << " / " << top[i] << endl;
now = min(now, top[i]);
ans += now;
}
cout << ans << endl;
return 0;
}
| replace | 39 | 44 | 39 | 44 | 0 | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
// #define MOD 1000000007
#define MOD 998244353
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()
template <typename T> void DEBUG(T e) {
if (DEBUG_OUT == false)
return;
std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
if (DEBUG_OUT == false)
return;
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
if (DEBUG_OUT == false)
return;
for (const auto &v : vv) {
DEBUG(v);
}
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
if (DEBUG_OUT == false)
return;
DEBUG(d);
DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
abort();
}
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
void solve(void) {
int N;
cin >> N;
int tot = 0, b = 0, ANS = 0;
vector<int> vec(N, 0);
for (int i = 0; i <= N; i++) {
cin >> vec[i];
tot += vec[i];
}
b = 1;
bool flag = true;
for (int i = 0; i <= N; i++) {
if (b < vec[i]) {
flag = false;
break;
}
ANS += b;
b -= vec[i];
tot -= vec[i];
b = min(b * 2, tot);
}
if (!flag)
cout << -1 << endl;
else
cout << ANS << endl;
}
int32_t main(int32_t argc, const char *argv[]) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed;
std::cout << std::setprecision(11);
solve();
return 0;
}
| #include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
// #define MOD 1000000007
#define MOD 998244353
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()
template <typename T> void DEBUG(T e) {
if (DEBUG_OUT == false)
return;
std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
if (DEBUG_OUT == false)
return;
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
if (DEBUG_OUT == false)
return;
for (const auto &v : vv) {
DEBUG(v);
}
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
if (DEBUG_OUT == false)
return;
DEBUG(d);
DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
abort();
}
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
void solve(void) {
int N;
cin >> N;
int tot = 0, b = 0, ANS = 0;
vector<int> vec(N + 1, 0);
for (int i = 0; i <= N; i++) {
cin >> vec[i];
tot += vec[i];
}
b = 1;
bool flag = true;
for (int i = 0; i <= N; i++) {
if (b < vec[i]) {
flag = false;
break;
}
ANS += b;
b -= vec[i];
tot -= vec[i];
b = min(b * 2, tot);
}
if (!flag)
cout << -1 << endl;
else
cout << ANS << endl;
}
int32_t main(int32_t argc, const char *argv[]) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed;
std::cout << std::setprecision(11);
solve();
return 0;
}
| replace | 83 | 84 | 83 | 84 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int uli;
typedef long double Lf;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define fastIO \
std::ios::sync_with_stdio(false); \
cin.tie(NULL)
#define mod 998244353
#define N 2005
#define NN 2005
#define SZ(x) ((lli)(x).size())
#define loop(i, s, n) for (int i = (s); i < (n); i++)
#define loopr(i, n, s) for (int i = (n)-1; i >= (s); i--)
#define pb push_back
#define o2(a, b) cout << (a) << " " << (b) << endl
#define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define o4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define cl cout << endl
#define r0 return 0
#define e0 exit(0)
#define x first
#define y second
inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; }
inline lli modsub(lli n, lli m, lli p = mod) {
return ((n - m + p) % p + p) % p;
}
inline lli modpro(lli n, lli m, lli p = mod) {
return (((n % p) * (m % p)) % p + p) % p;
}
uli powe(lli x, lli y) {
uli res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
lli modpow(lli x, lli y, lli p = mod) {
lli res = 1;
while (y > 0) {
if (y & 1)
res = modpro(res, x, p);
y = y >> 1;
x = modpro(x, x, p);
}
return res;
}
inline lli modInverse(lli n, lli p = mod) {
if (n == 1)
return 1;
return modpow(n, p - 2, p);
}
inline lli moddiv(lli n, lli m, lli p = mod) {
return modpro(n, modInverse(m, p), p);
}
inline lli modadd3(lli x, lli y, lli z, lli p = mod) {
return modadd(modadd(x, y, p), z, p);
}
inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) {
return modadd(modadd(x, y, p), modadd(z, w, p), p);
}
inline lli modnCr(lli fac[], int n, int r, lli p = mod) {
if (r == 0)
return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); }
template <typename T> inline T max4(T x, T y, T z, T w) {
return max(max3(x, y, w), z);
}
template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); }
template <typename T> inline T min4(T x, T y, T z, T w) {
return min(min3(x, y, w), z);
}
template <typename T> void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// template<class X, class Y, class Z>
// struct triple {
// X x;
// Y y;
// Z z;
//
// friend bool operator<(triple a, triple b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else return a.z<=b.z;
// }
// };
//
//
// template<class X, class Y, class Z, class W>
// class quad {
// public:
// X x;
// Y y;
// Z z;
// W w;
//
// friend bool operator<(quad a, quad b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else if(a.z!=b.z)return a.z<b.z;
// else return a.w<b.w;
// }
// };
// template <typename T>
// T gcd(T a, T b)
//{
// if (a == 0)
// return b;
// if(b==0)
// return a;
// T t;
// while((a>0)&&(b>0)){
// t = a;
// a=b%a;
// b=t;
// }
//
// return max(a,b);
// }
//
// uli choose(uli n, uli k){
// uli res = 1;
//
// // Since C(n, k) = C(n, n-k)
// if ( k > n - k )
// k = n - k;
//
// // Calculate value of
// // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
// for (uli i = 0; i < k; ++i)
// {
// res *= (n - i);
// res /= (i + 1);
// }
//
// return res;
//}
//
//
// vector<vector<pii>> adj;
// //vector<int> dist;
// vector<int> d;
//// vector<bool> visited;
//// vector<int> pa;
// //queue<int> q;
//
// class Graph{
//
// public:
// int V;
//
// Graph(int V){
// this->V = V;
// adj.resize(V);
//// visited.resize(V);
// //color.resize(V);
// //dist.resize(V);
// d.resize(V);
// //// dist.resize(V);
//// pa.resize(V);
//
// }
//
// ~Graph(){
// adj.clear();
// //visited.clear();
// // color.clear();
// // dist.clear();
//// pa.clear();
// //dist.clear();
//// visited.clear();
// d.clear();
//
//
// }
//
//
// void addEdge(int a, int b)
// {
// adj[a].pb(b);
// adj[b].pb(a);
// //adj[b].pb({a,in});
// }
//
// // void dfs(int x){
// // // color[x]=1;
// //
// // for(auto u: adj[x]){
// // if(visited[u]) continue;
// //
// // dfs(u);
// // }
// //
// // }
// //
// //
// //
// //
// //
// // void dfsTree(int x, int p){
// // for(auto u: adj[x]){
// // if(u==p) {continue;}
// // dfsTree(u,x);
// // }
// // }
//
//
// void dijkstra(int s) {
// d.assign(n, INF);
// d[s] = 0;
// set<pair<int, int>> q;
// q.insert({0, s});
// while (!q.empty()) {
// int v = q.begin()->second;
// q.erase(q.begin());
// for (auto edge : adj[v]) {
// int to = edge.first;
// int len = edge.second;
// if (d[v] + len < d[to]) {
// q.erase({d[to], to});
// d[to] = d[v] + len;
// q.insert({d[to], to});
// }
// }
// }
// }
//
//
//// void bfs(int x){
//// queue<int> q;
//// visited[x]=1;
//// q.push(x);
//// //dist[x]=0;
//// while(!q.empty()){
//// int s = q.front();
//// q.pop();
////
//// for(auto u : adj[s]){
////
//// if(visited[u]) continue;
//// pa[u]=s;
//// visited[u]=1;
//// //dist[u.x]=dist[s]+1;
//// q.push(u);
//// }
//// }
//// }
//
// //pair<int,int> farthest(int start){
// // vector<int> dist(V,INT_MAX);
// //
// // q.push(start);
// // dist[start]=0;
// // while(!q.empty()){
// // int u = q.front();
// // q.pop();
// // for(int child: adj[u]){
// // //if(keep[child])
// // if(dist[child] > dist[u]+1) {
// // dist[child] = dist[u]+1;
// // q.push(child);
// // }
// // }
// // }
// // int big=-1,mi=-1;
// // for(int i=1;i<n+1;i++){
// // //if(keep[i])
// // {
// // if(dist[i]>big){
// // big=dist[i];mi=i;
// // }
// // }
// // }
// // return make_pair(big,mi);
// //}
//
//
// // int connectedcomponents(int n){
// // int ans=0;
// // loop(i,1,n+1){
// // if(!visited[i]){
// // ans++;
// // dfs(i);
// // }
// //
// // }
// // return ans;
// // }
//
// };
// template <typename T>
// T findpowerfactorial(T n,T p)
//{
// T x = 0;
// while (n)
// {
// n /= p;
// x += n;
// }
// return x;
// }
//
// template <typename T>
// int getibit(T n, int i){
// //cout<<(n&(1LL<<i))<<endl;
// return (n&(1LL<<i))?1:0;
// }
//
// template <typename T>
// int findbits(T n, T p){
// int x=0;
// while(n>0){
// n/=p;
// x++;
// }
// return x;
// }
//
//
////
// vector<pair<lli,int>> v;
// void primeFactors(lli n)
//{
// v.clear();
// int c=0;
// while (n % 2 == 0)
// {
// n = n/2;
// c++;
// }
// if(c>0) v.pb({2,c});
//
//
// for (lli i = 3; i*i <= n; i = i + 2)
// { c=0;
//
// while (n % i == 0)
// {
// n = n/i;
// c++;
// }
// if(c>0)v.pb({i,c});
//
// }
// if (n > 2)
// v.pb({n,1});
// }
// lli fac_[N];
// void fac_init(int n){
// fac_[0]=1;
// loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i);
// }
//
/*BITMASK
for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) {
for ( int pos = 0; pos < n ; ++pos ) {
if ( inum & ( 1 << pos ) ){
results[inum] += s [pos] ; //DO SOMETHING
}
}
}
*/
int a[N];
lli ps[N];
int main() {
fastIO;
int erer = 1;
// cin>>erer;
loop(erer2, 1, erer + 1) {
int n;
cin >> n;
loop(i, 0, n + 1) cin >> a[i];
// if(a[0]==1) {
// if(n==0) cout<<1<<endl;
// else cout<<-1<<endl;
// r0;
// }
ps[n] = 0;
loopr(i, n, 0) ps[i] = ps[i + 1] + a[i + 1];
lli cn = 1, cur = 1;
// lli req = ps[0];
loop(i, 0, n + 1) {
// o4(cur,a[i],ps[i],cn);
if (cur < a[i]) {
cout << -1;
r0;
}
cur -= a[i];
lli req = ps[i];
if (req > cur) {
lli diff = req - cur;
lli hmd = min(diff, cur);
cn += (cur - hmd);
cur += hmd;
cn += (hmd * 2);
} else {
cn += cur;
}
// o4(cur,a[i],ps[i],cn);
}
cout << cn << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int uli;
typedef long double Lf;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define fastIO \
std::ios::sync_with_stdio(false); \
cin.tie(NULL)
#define mod 998244353
#define N 100005
#define NN 2005
#define SZ(x) ((lli)(x).size())
#define loop(i, s, n) for (int i = (s); i < (n); i++)
#define loopr(i, n, s) for (int i = (n)-1; i >= (s); i--)
#define pb push_back
#define o2(a, b) cout << (a) << " " << (b) << endl
#define o3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define o4(a, b, c, d) \
cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define cl cout << endl
#define r0 return 0
#define e0 exit(0)
#define x first
#define y second
inline lli modadd(lli n, lli m, lli p = mod) { return ((n + m) % p + p) % p; }
inline lli modsub(lli n, lli m, lli p = mod) {
return ((n - m + p) % p + p) % p;
}
inline lli modpro(lli n, lli m, lli p = mod) {
return (((n % p) * (m % p)) % p + p) % p;
}
uli powe(lli x, lli y) {
uli res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
lli modpow(lli x, lli y, lli p = mod) {
lli res = 1;
while (y > 0) {
if (y & 1)
res = modpro(res, x, p);
y = y >> 1;
x = modpro(x, x, p);
}
return res;
}
inline lli modInverse(lli n, lli p = mod) {
if (n == 1)
return 1;
return modpow(n, p - 2, p);
}
inline lli moddiv(lli n, lli m, lli p = mod) {
return modpro(n, modInverse(m, p), p);
}
inline lli modadd3(lli x, lli y, lli z, lli p = mod) {
return modadd(modadd(x, y, p), z, p);
}
inline lli modadd4(lli x, lli y, lli z, lli w, lli p = mod) {
return modadd(modadd(x, y, p), modadd(z, w, p), p);
}
inline lli modnCr(lli fac[], int n, int r, lli p = mod) {
if (r == 0)
return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T> inline T max3(T x, T y, T z) { return max(max(x, y), z); }
template <typename T> inline T max4(T x, T y, T z, T w) {
return max(max3(x, y, w), z);
}
template <typename T> inline T min3(T x, T y, T z) { return min(min(x, y), z); }
template <typename T> inline T min4(T x, T y, T z, T w) {
return min(min3(x, y, w), z);
}
template <typename T> void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// template<class X, class Y, class Z>
// struct triple {
// X x;
// Y y;
// Z z;
//
// friend bool operator<(triple a, triple b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else return a.z<=b.z;
// }
// };
//
//
// template<class X, class Y, class Z, class W>
// class quad {
// public:
// X x;
// Y y;
// Z z;
// W w;
//
// friend bool operator<(quad a, quad b)
// {
// if(a.x!=b.x) return a.x<b.x;
// else if(a.y!=b.y) return a.y<b.y;
// else if(a.z!=b.z)return a.z<b.z;
// else return a.w<b.w;
// }
// };
// template <typename T>
// T gcd(T a, T b)
//{
// if (a == 0)
// return b;
// if(b==0)
// return a;
// T t;
// while((a>0)&&(b>0)){
// t = a;
// a=b%a;
// b=t;
// }
//
// return max(a,b);
// }
//
// uli choose(uli n, uli k){
// uli res = 1;
//
// // Since C(n, k) = C(n, n-k)
// if ( k > n - k )
// k = n - k;
//
// // Calculate value of
// // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
// for (uli i = 0; i < k; ++i)
// {
// res *= (n - i);
// res /= (i + 1);
// }
//
// return res;
//}
//
//
// vector<vector<pii>> adj;
// //vector<int> dist;
// vector<int> d;
//// vector<bool> visited;
//// vector<int> pa;
// //queue<int> q;
//
// class Graph{
//
// public:
// int V;
//
// Graph(int V){
// this->V = V;
// adj.resize(V);
//// visited.resize(V);
// //color.resize(V);
// //dist.resize(V);
// d.resize(V);
// //// dist.resize(V);
//// pa.resize(V);
//
// }
//
// ~Graph(){
// adj.clear();
// //visited.clear();
// // color.clear();
// // dist.clear();
//// pa.clear();
// //dist.clear();
//// visited.clear();
// d.clear();
//
//
// }
//
//
// void addEdge(int a, int b)
// {
// adj[a].pb(b);
// adj[b].pb(a);
// //adj[b].pb({a,in});
// }
//
// // void dfs(int x){
// // // color[x]=1;
// //
// // for(auto u: adj[x]){
// // if(visited[u]) continue;
// //
// // dfs(u);
// // }
// //
// // }
// //
// //
// //
// //
// //
// // void dfsTree(int x, int p){
// // for(auto u: adj[x]){
// // if(u==p) {continue;}
// // dfsTree(u,x);
// // }
// // }
//
//
// void dijkstra(int s) {
// d.assign(n, INF);
// d[s] = 0;
// set<pair<int, int>> q;
// q.insert({0, s});
// while (!q.empty()) {
// int v = q.begin()->second;
// q.erase(q.begin());
// for (auto edge : adj[v]) {
// int to = edge.first;
// int len = edge.second;
// if (d[v] + len < d[to]) {
// q.erase({d[to], to});
// d[to] = d[v] + len;
// q.insert({d[to], to});
// }
// }
// }
// }
//
//
//// void bfs(int x){
//// queue<int> q;
//// visited[x]=1;
//// q.push(x);
//// //dist[x]=0;
//// while(!q.empty()){
//// int s = q.front();
//// q.pop();
////
//// for(auto u : adj[s]){
////
//// if(visited[u]) continue;
//// pa[u]=s;
//// visited[u]=1;
//// //dist[u.x]=dist[s]+1;
//// q.push(u);
//// }
//// }
//// }
//
// //pair<int,int> farthest(int start){
// // vector<int> dist(V,INT_MAX);
// //
// // q.push(start);
// // dist[start]=0;
// // while(!q.empty()){
// // int u = q.front();
// // q.pop();
// // for(int child: adj[u]){
// // //if(keep[child])
// // if(dist[child] > dist[u]+1) {
// // dist[child] = dist[u]+1;
// // q.push(child);
// // }
// // }
// // }
// // int big=-1,mi=-1;
// // for(int i=1;i<n+1;i++){
// // //if(keep[i])
// // {
// // if(dist[i]>big){
// // big=dist[i];mi=i;
// // }
// // }
// // }
// // return make_pair(big,mi);
// //}
//
//
// // int connectedcomponents(int n){
// // int ans=0;
// // loop(i,1,n+1){
// // if(!visited[i]){
// // ans++;
// // dfs(i);
// // }
// //
// // }
// // return ans;
// // }
//
// };
// template <typename T>
// T findpowerfactorial(T n,T p)
//{
// T x = 0;
// while (n)
// {
// n /= p;
// x += n;
// }
// return x;
// }
//
// template <typename T>
// int getibit(T n, int i){
// //cout<<(n&(1LL<<i))<<endl;
// return (n&(1LL<<i))?1:0;
// }
//
// template <typename T>
// int findbits(T n, T p){
// int x=0;
// while(n>0){
// n/=p;
// x++;
// }
// return x;
// }
//
//
////
// vector<pair<lli,int>> v;
// void primeFactors(lli n)
//{
// v.clear();
// int c=0;
// while (n % 2 == 0)
// {
// n = n/2;
// c++;
// }
// if(c>0) v.pb({2,c});
//
//
// for (lli i = 3; i*i <= n; i = i + 2)
// { c=0;
//
// while (n % i == 0)
// {
// n = n/i;
// c++;
// }
// if(c>0)v.pb({i,c});
//
// }
// if (n > 2)
// v.pb({n,1});
// }
// lli fac_[N];
// void fac_init(int n){
// fac_[0]=1;
// loop(i,1,n+1)fac_[i]=modpro(fac_[i-1],i);
// }
//
/*BITMASK
for( int inum = 0 ; inum < ( 1 << n ) ; ++ inum ) {
for ( int pos = 0; pos < n ; ++pos ) {
if ( inum & ( 1 << pos ) ){
results[inum] += s [pos] ; //DO SOMETHING
}
}
}
*/
int a[N];
lli ps[N];
int main() {
fastIO;
int erer = 1;
// cin>>erer;
loop(erer2, 1, erer + 1) {
int n;
cin >> n;
loop(i, 0, n + 1) cin >> a[i];
// if(a[0]==1) {
// if(n==0) cout<<1<<endl;
// else cout<<-1<<endl;
// r0;
// }
ps[n] = 0;
loopr(i, n, 0) ps[i] = ps[i + 1] + a[i + 1];
lli cn = 1, cur = 1;
// lli req = ps[0];
loop(i, 0, n + 1) {
// o4(cur,a[i],ps[i],cn);
if (cur < a[i]) {
cout << -1;
r0;
}
cur -= a[i];
lli req = ps[i];
if (req > cur) {
lli diff = req - cur;
lli hmd = min(diff, cur);
cn += (cur - hmd);
cur += hmd;
cn += (hmd * 2);
} else {
cn += cur;
}
// o4(cur,a[i],ps[i],cn);
}
cout << cn << endl;
}
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100000];
long long ruia[100001];
long long ans = 0;
long long nowleef = 1;
int main() {
cin >> n;
for (long long i = 0; i < n + 1; i++) {
cin >> a[i];
}
for (long long i = 0; i < n + 1; i++) {
ruia[n - i] = ruia[n - i + 1] + a[n - i];
}
for (long long i = 0; i < n + 1; i++) {
if (nowleef > ruia[i]) {
ans = -1;
break;
}
ans += nowleef;
if (nowleef < a[i]) {
ans = -1;
break;
}
nowleef -= a[i];
nowleef = min(nowleef * 2LL, ruia[i + 1]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100005];
long long ruia[100006];
long long ans = 0;
long long nowleef = 1;
int main() {
cin >> n;
for (long long i = 0; i < n + 1; i++) {
cin >> a[i];
}
for (long long i = 0; i < n + 1; i++) {
ruia[n - i] = ruia[n - i + 1] + a[n - i];
}
for (long long i = 0; i < n + 1; i++) {
if (nowleef > ruia[i]) {
ans = -1;
break;
}
ans += nowleef;
if (nowleef < a[i]) {
ans = -1;
break;
}
nowleef -= a[i];
nowleef = min(nowleef * 2LL, ruia[i + 1]);
}
cout << ans << endl;
return 0;
} | replace | 3 | 5 | 3 | 5 | 0 | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>
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;
}
using namespace std;
#define ALL(X) X.begin(), X.end()
using ll = long long int;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int MOD = 1000000007;
// const int MOD=998244353;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
/*ll N;
cin>>N;
vll A(N+1);
for(ll i=0;i<=N;i++)cin>>A[i];
if(N==0){
if(A[0]==1){
cout<<1<<endl;
return 0;
}else {
cout<<-1<<endl;
return 0;
}
}
if(A[0]!=0){
cout<<-1<<endl;
return 0;
}
vll B(N+1);
for(ll i=0;i<=N;i++){
if(i==0){
B[0]=1;
}else{
B[i]=(B[i-1]-A[i-1])*2;
if(B[i]<A[i]){
cout<<-1<<endl;
return 0;
}
}
}
vll C(N+1);
for(ll i=N;i>=0;i--){
if(i==N){
C[i]=A[i];
}else{
C[i]=C[i+1]+A[i];
}
}
ll ans=0;
for(ll i=0;i<=N;i++){ans+=min(B[i],C[i]);
}
cout<<ans<<endl;
return 0;
}
int main() {*/
// ll n,a[100005]={0},b[100005],c[100005],pv,ans=0;
ll n, pv;
vll a(n + 2), b(n + 2), c(n + 2);
bool ok = false;
cin >> n;
for (ll i = 0; i <= n; i++) {
cin >> a[i];
}
for (ll i = 0; i <= n; i++) {
if (i == 0) {
if (a[i] > 1) {
cout << -1 << endl;
return 0;
}
b[0] = 1;
} else {
if (ok)
b[i] = 2 * 1e17;
else
b[i] = (b[i - 1] - a[i - 1]) * 2;
if (b[i] >= 1e17)
ok = true;
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
}
}
b[n + 1] = 1;
for (ll i = n; i >= 0; i--) {
if (i == n) {
c[i] = a[i];
} else {
c[i] = c[i + 1] + a[i];
}
}
c[n + 1] = 0;
for (ll i = 0; i <= n + 1; i++) {
if (b[i] > c[i]) {
pv = i;
break;
}
}
ll ans = 0;
for (ll i = 0; i <= n; i++) {
if (i < pv) {
ans += b[i];
} else {
ans += c[i];
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>
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;
}
using namespace std;
#define ALL(X) X.begin(), X.end()
using ll = long long int;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int MOD = 1000000007;
// const int MOD=998244353;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
/*ll N;
cin>>N;
vll A(N+1);
for(ll i=0;i<=N;i++)cin>>A[i];
if(N==0){
if(A[0]==1){
cout<<1<<endl;
return 0;
}else {
cout<<-1<<endl;
return 0;
}
}
if(A[0]!=0){
cout<<-1<<endl;
return 0;
}
vll B(N+1);
for(ll i=0;i<=N;i++){
if(i==0){
B[0]=1;
}else{
B[i]=(B[i-1]-A[i-1])*2;
if(B[i]<A[i]){
cout<<-1<<endl;
return 0;
}
}
}
vll C(N+1);
for(ll i=N;i>=0;i--){
if(i==N){
C[i]=A[i];
}else{
C[i]=C[i+1]+A[i];
}
}
ll ans=0;
for(ll i=0;i<=N;i++){ans+=min(B[i],C[i]);
}
cout<<ans<<endl;
return 0;
}
int main() {*/
// ll n,a[100005]={0},b[100005],c[100005],pv,ans=0;
ll n, pv;
// vll a(n+2),b(n+2),c(n+2);
vll a(100005), b(100005), c(100005);
bool ok = false;
cin >> n;
for (ll i = 0; i <= n; i++) {
cin >> a[i];
}
for (ll i = 0; i <= n; i++) {
if (i == 0) {
if (a[i] > 1) {
cout << -1 << endl;
return 0;
}
b[0] = 1;
} else {
if (ok)
b[i] = 2 * 1e17;
else
b[i] = (b[i - 1] - a[i - 1]) * 2;
if (b[i] >= 1e17)
ok = true;
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
}
}
b[n + 1] = 1;
for (ll i = n; i >= 0; i--) {
if (i == n) {
c[i] = a[i];
} else {
c[i] = c[i + 1] + a[i];
}
}
c[n + 1] = 0;
for (ll i = 0; i <= n + 1; i++) {
if (b[i] > c[i]) {
pv = i;
break;
}
}
ll ans = 0;
for (ll i = 0; i <= n; i++) {
if (i < pv) {
ans += b[i];
} else {
ans += c[i];
}
}
cout << ans << endl;
return 0;
}
| replace | 94 | 95 | 94 | 96 | -6 | munmap_chunk(): invalid pointer
|
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
ll n;
cin >> n;
vector<ll> a(n + 1);
rep(i, n + 1) cin >> a[i];
if (a[0] != 0) {
cout << "-1" << endl;
return -1;
}
ll ans = 0;
vector<ll> num(n + 1);
for (ll i = n; i >= 0; i--) {
// ans += a[i] * (i+1);
if (i == n)
num[i] = a[i];
else
num[i] = num[i + 1] + a[i];
}
for (ll i = 0; i < n; i++) {
if (i == 0) {
// ans -= (num[i]-1);
num[i] = 1;
continue;
}
ll limit = (num[i - 1] - a[i - 1]) * 2LL;
ll prev = num[i];
num[i] = min(num[i], limit);
if (num[i] <= a[i]) {
cout << "-1" << endl;
return 0;
}
if (i == n - 1) {
if (num[i] - a[i] > a[n])
num[i] = a[i] + a[n];
if ((num[i] - a[i]) * 2 < a[n]) {
cout << "-1" << endl;
return 0;
}
}
// ans -= (prev-num[i]);
}
rep(i, n + 1) { ans += num[i]; }
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
ll n;
cin >> n;
vector<ll> a(n + 1);
rep(i, n + 1) cin >> a[i];
if (a[0] != 0) {
if (a[0] == 1 && n == 0) {
cout << 1 << endl;
}
else {
cout << -1 << endl;
}
return 0;
}
ll ans = 0;
vector<ll> num(n + 1);
for (ll i = n; i >= 0; i--) {
// ans += a[i] * (i+1);
if (i == n)
num[i] = a[i];
else
num[i] = num[i + 1] + a[i];
}
for (ll i = 0; i < n; i++) {
if (i == 0) {
// ans -= (num[i]-1);
num[i] = 1;
continue;
}
ll limit = (num[i - 1] - a[i - 1]) * 2LL;
ll prev = num[i];
num[i] = min(num[i], limit);
if (num[i] <= a[i]) {
cout << "-1" << endl;
return 0;
}
if (i == n - 1) {
if (num[i] - a[i] > a[n])
num[i] = a[i] + a[n];
if ((num[i] - a[i]) * 2 < a[n]) {
cout << "-1" << endl;
return 0;
}
}
// ans -= (prev-num[i]);
}
rep(i, n + 1) { ans += num[i]; }
cout << ans << endl;
return 0;
}
| replace | 21 | 23 | 21 | 29 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t
int main() {
ll N;
cin >> N;
ll SUM = 0;
vector<ll> C(N + 1);
for (int i = 0; i < N + 1; i++)
cin >> C[i];
if (C[N] == 0) {
cout << -1 << endl;
return 0;
}
vector<ll> P(N + 1);
P[N] = 0, P[N - 1] = C[N];
for (int i = N - 1; i >= 0; i--) {
P[i] = C[i + 1] + P[i + 1];
}
ll temp = 1;
for (int i = 0; i <= N; i++) {
P[i] = min(temp - C[i], P[i]);
temp = 2 * P[i];
if (temp <= -1) {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < N + 1; i++)
SUM += C[i] + P[i];
cout << SUM << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t
int main() {
ll N;
cin >> N;
ll SUM = 0;
vector<ll> C(N + 1);
for (int i = 0; i < N + 1; i++)
cin >> C[i];
if (C[N] == 0) {
cout << -1 << endl;
return 0;
}
vector<ll> P(N + 1);
P[N] = 0;
for (int i = N - 1; i >= 0; i--) {
P[i] = C[i + 1] + P[i + 1];
}
ll temp = 1;
for (int i = 0; i <= N; i++) {
P[i] = min(temp - C[i], P[i]);
temp = 2 * P[i];
if (temp <= -1) {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < N + 1; i++)
SUM += C[i] + P[i];
cout << SUM << endl;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()
template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
ll n = list.size();
rep(i, n) out << list[i] << ' ';
return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &list) {
ll n = list.size();
rep(i, n) in >> list[i];
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
ll n = list.size();
rep(i, n) out << list[i] << '\n';
return out;
}
/* ------------- ANSWER ------------- */
/* ---------------------------------- */
void solve() {
ll n;
cin >> n;
vl a(n + 1);
cin >> a;
vl ne_max(n + 1);
rep(i, n + 1) {
ll maxv = 0;
if (i > 50)
maxv = ll_inf;
else
maxv = pow(2, i);
if (i >= 1) {
ne_max[i] = max(0LL, min(ne_max[i - 1] * 2 - a[i], maxv - a[i]));
} else {
ne_max[i] = max(0LL, maxv - a[i]);
}
}
if (n == 0) {
throw runtime_error("");
}
if (a.back() == 0) {
cout << -1 << '\n';
return;
}
ll now_v = a.back();
ll ans = now_v;
for (ll i = n - 1; i >= 0; --i) {
if (now_v > ne_max[i] * 2) {
cout << -1 << '\n';
return;
}
now_v = min(ne_max[i] + a[i], now_v + a[i]);
ans += now_v;
}
if (ans < 0) {
cout << -1 << '\n';
} else
cout << ans << '\n';
}
int main() {
#ifdef MY_DEBUG
while (true) {
#endif
solve();
#ifdef MY_DEBUG
}
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()
template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
ll n = list.size();
rep(i, n) out << list[i] << ' ';
return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &list) {
ll n = list.size();
rep(i, n) in >> list[i];
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
ll n = list.size();
rep(i, n) out << list[i] << '\n';
return out;
}
/* ------------- ANSWER ------------- */
/* ---------------------------------- */
void solve() {
ll n;
cin >> n;
vl a(n + 1);
cin >> a;
vl ne_max(n + 1);
rep(i, n + 1) {
ll maxv = 0;
if (i > 50)
maxv = ll_inf;
else
maxv = pow(2, i);
if (i >= 1) {
ne_max[i] = max(0LL, min(ne_max[i - 1] * 2 - a[i], maxv - a[i]));
} else {
ne_max[i] = max(0LL, maxv - a[i]);
}
}
if (n == 0) {
if (a[0] == 0)
cout << 0 << '\n';
else if (a[0] == 1)
cout << 1 << '\n';
else
cout << -1 << '\n';
return;
}
if (a.back() == 0) {
cout << -1 << '\n';
return;
}
ll now_v = a.back();
ll ans = now_v;
for (ll i = n - 1; i >= 0; --i) {
if (now_v > ne_max[i] * 2) {
cout << -1 << '\n';
return;
}
now_v = min(ne_max[i] + a[i], now_v + a[i]);
ans += now_v;
}
if (ans < 0) {
cout << -1 << '\n';
} else
cout << ans << '\n';
}
int main() {
#ifdef MY_DEBUG
while (true) {
#endif
solve();
#ifdef MY_DEBUG
}
#endif
return 0;
}
| replace | 67 | 68 | 67 | 74 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
constexpr long long int mod = 1000000007, MOD = 998244353;
constexpr double PI = 3.14159265358979323846;
int main() {
long long N;
cin >> N;
vector<long long> U(N + 1), Usum(N + 2);
for (long long i = 0; i <= N; i++) {
cin >> U.at(i);
}
for (long long i = 1; i <= N + 1; i++) {
Usum.at(i) = Usum.at(i - 1) + U.at(i - 1);
}
vector<long long> maxe(N + 1); // たれてるふんどし
maxe.at(0) = 1;
for (long long i = 0; i < N; i++) {
if (maxe.at(i) < U.at(i)) {
cout << -1;
return 0;
} else {
// cout << Usum.at(N)-Usum.at(i) << endl;
maxe.at(i + 1) = min(
(maxe.at(i) - U.at(i)) * 2,
Usum.at(N + 1) -
Usum.at(
i +
1)); // 早いうちに広げる,でも広げすぎると片付けられなくなる
if (maxe.at(i + 1) == 0) {
cout << -1;
return 0;
}
}
maxe.at(N) = 0;
}
long long ans = 0;
for (long long i = 0; i <= N - 1; i++) {
// cout << maxe.at(i) << endl;
ans += maxe.at(i);
}
ans += U.at(N);
if (N == 0 && U.at(0) != 1)
ans = -1;
if ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0)
ans = -1;
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
constexpr long long int mod = 1000000007, MOD = 998244353;
constexpr double PI = 3.14159265358979323846;
int main() {
long long N;
cin >> N;
vector<long long> U(N + 1), Usum(N + 2);
for (long long i = 0; i <= N; i++) {
cin >> U.at(i);
}
for (long long i = 1; i <= N + 1; i++) {
Usum.at(i) = Usum.at(i - 1) + U.at(i - 1);
}
vector<long long> maxe(N + 1); // たれてるふんどし
maxe.at(0) = 1;
for (long long i = 0; i < N; i++) {
if (maxe.at(i) < U.at(i)) {
cout << -1;
return 0;
} else {
// cout << Usum.at(N)-Usum.at(i) << endl;
maxe.at(i + 1) = min(
(maxe.at(i) - U.at(i)) * 2,
Usum.at(N + 1) -
Usum.at(
i +
1)); // 早いうちに広げる,でも広げすぎると片付けられなくなる
if (maxe.at(i + 1) == 0) {
cout << -1;
return 0;
}
}
maxe.at(N) = 0;
}
long long ans = 0;
for (long long i = 0; i <= N - 1; i++) {
// cout << maxe.at(i) << endl;
ans += maxe.at(i);
}
ans += U.at(N);
if (N == 0 && U.at(0) != 1)
ans = -1;
if (N != 0 && ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0))
ans = -1;
cout << ans;
} | replace | 48 | 49 | 48 | 49 | 0 | |
p02665 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y) \
memset(x, y, \
sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18;
#define mod 1000000007
// #define mod 998244353
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
os_pair;
ll power(ll x, ll n) {
ll res = 1;
while (n > 0) {
if (n % 2 == 1) {
res = res * x;
}
x = x * x;
n = n / 2;
}
return res;
}
ll powm(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
// cout<< fixed << setprecision(10)
int main() {
fast;
int T;
T = 1;
// cin>>T;
while (T--) {
ll n, i;
cin >> n;
ll a[n + 1];
for (i = 0; i < n + 1; i++) {
cin >> a[i];
}
ll b[n + 1];
b[n] = a[n];
for (i = n - 1; i >= 0; i--) {
b[i] = b[i + 1] + a[i];
}
ll c[n + 1];
c[0] = 1;
for (i = 1; i < n + 1; i--) {
c[i] = min(b[i], 2 * (c[i - 1] - a[i]));
}
int x = 1;
ll ans = 0;
for (i = 0; i < n + 1; i++) {
if (c[i] < a[i]) {
x = 0;
}
ans += c[i];
}
if (x == 0) {
cout << -1 << "\n";
} else {
cout << ans;
}
}
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y) \
memset(x, y, \
sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18;
#define mod 1000000007
// #define mod 998244353
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
os_pair;
ll power(ll x, ll n) {
ll res = 1;
while (n > 0) {
if (n % 2 == 1) {
res = res * x;
}
x = x * x;
n = n / 2;
}
return res;
}
ll powm(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
// cout<< fixed << setprecision(10)
int main() {
fast;
int T;
T = 1;
// cin>>T;
while (T--) {
ll n, i;
cin >> n;
ll a[n + 1];
for (i = 0; i < n + 1; i++) {
cin >> a[i];
}
ll b[n + 1];
b[n] = a[n];
for (i = n - 1; i >= 0; i--) {
b[i] = b[i + 1] + a[i];
}
ll c[n + 1];
c[0] = 1;
for (i = 1; i < n + 1; i++) {
c[i] = min(b[i], 2 * (c[i - 1] - a[i - 1]));
}
int x = 1;
ll ans = 0;
for (i = 0; i < n + 1; i++) {
if (c[i] < a[i]) {
x = 0;
}
ans += c[i];
}
if (x == 0) {
cout << -1 << "\n";
} else {
cout << ans;
}
}
} | replace | 75 | 77 | 75 | 77 | TLE | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
const ll INF = 1e18;
vector<ll> two;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> A(N + 1);
rep(i, N + 1) cin >> A[i];
two.resize(N + 1, INF);
two[0] = 1;
for (int i = 1; i <= 60; i++)
two[i] = two[i - 1] * 2;
vector<ll> b(N + 1, 0);
b[0] = 1 - A[0];
if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
cout << -1 << endl;
return 0;
}
rep(i, N + 1) if (two[i] < A[i]) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= N; i++) {
if (A[i] > 2 * b[i - 1]) {
cout << -1 << endl;
return 0;
}
b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
}
b[N] = 0;
for (int i = N - 1; i >= 0; i--) {
chmin(b[i], A[i + 1] + b[i + 1]);
chmin(b[i], two[i] - A[i]);
}
ll ans = 0;
rep(i, N) ans += b[i];
rep(i, N + 1) ans += A[i];
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b)
a = b;
}
const ll INF = 1e18;
vector<ll> two;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> A(N + 1);
rep(i, N + 1) cin >> A[i];
two.resize(N + 1, INF);
two[0] = 1;
for (int i = 1; i <= min(N, 60ll); i++)
two[i] = two[i - 1] * 2;
vector<ll> b(N + 1, 0);
b[0] = 1 - A[0];
if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
cout << -1 << endl;
return 0;
}
rep(i, N + 1) if (two[i] < A[i]) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= N; i++) {
if (A[i] > 2 * b[i - 1]) {
cout << -1 << endl;
return 0;
}
b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
}
b[N] = 0;
for (int i = N - 1; i >= 0; i--) {
chmin(b[i], A[i + 1] + b[i + 1]);
chmin(b[i], two[i] - A[i]);
}
ll ans = 0;
rep(i, N) ans += b[i];
rep(i, N + 1) ans += A[i];
cout << ans << endl;
return 0;
}
| replace | 39 | 40 | 39 | 40 | -6 | Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p02665 | Python | Runtime Error | N = int(input())
A = list(map(int, input().split()))
B = []
if N == 0:
if A[0] != 1:
print(-1)
exit()
else:
print(1)
exit()
else:
if A[0] != 0:
print(-1)
exit()
else:
B.append(1)
for i in range(1, N + 1):
B.append((B[i - 1] - A[i - 1]) * 2)
if A[i] > B[i] or (A[i] == B[i] and i != N):
print(-1)
exit()
ans = 0
ans += A[N]
B[N] = A[N]
for i in range(N, -1, -1):
ans += min(B[i], B[i + 1] + A[i])
B[i] = min(B[i], B[i + 1] + A[i])
print(ans)
exit()
| N = int(input())
A = list(map(int, input().split()))
B = []
if N == 0:
if A[0] != 1:
print(-1)
exit()
else:
print(1)
exit()
else:
if A[0] != 0:
print(-1)
exit()
else:
B.append(1)
for i in range(1, N + 1):
B.append((B[i - 1] - A[i - 1]) * 2)
if A[i] > B[i] or (A[i] == B[i] and i != N):
print(-1)
exit()
ans = 0
ans += A[N]
B[N] = A[N]
for i in range(N - 1, -1, -1):
ans += min(B[i], B[i + 1] + A[i])
B[i] = min(B[i], B[i + 1] + A[i])
print(ans)
exit()
| replace | 28 | 29 | 28 | 29 | IndexError: list index out of range | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02665/Python/s441466077.py", line 30, in <module>
ans += min(B[i], B[i + 1] + A[i])
IndexError: list index out of range
|
p02665 | Python | Runtime Error | n = int(input())
a = list(map(int, input().split()))
a_min = [0] * (n + 1)
a_min[n] = a[n]
for i in range(n - 1, -1, -1):
a_min[i] = a[i] + a_min[i + 1]
b = [1]
for i in range(n):
tmp = b[i] - a[i]
if tmp < 1 or tmp * 2 < a[i + 1]:
print(-1)
break
else:
if n == 0:
ans = 1 if a[0] == 1 else -1
print(ans)
else:
print(sum(b))
| n = int(input())
a = list(map(int, input().split()))
a_min = [0] * (n + 1)
a_min[n] = a[n]
for i in range(n - 1, -1, -1):
a_min[i] = a[i] + a_min[i + 1]
b = [1]
for i in range(n):
tmp = b[i] - a[i]
b.append(min(tmp * 2, a_min[i + 1]))
if tmp < 1 or tmp * 2 < a[i + 1]:
print(-1)
break
else:
if n == 0:
ans = 1 if a[0] == 1 else -1
print(ans)
else:
print(sum(b))
| insert | 10 | 10 | 10 | 11 | IndexError: list index out of range | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02665/Python/s101181043.py", line 10, in <module>
tmp = b[i] - a[i]
IndexError: list index out of range
|
p02665 | C++ | Time Limit Exceeded | #include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;
int main() {
ll n, a[MAX + 100], node[MAX + 100], res = 0;
cin >> n;
for (ll i = 0; i <= n; ++i) {
cin >> a[i];
}
if (n == 0) {
cout << (a[0] == 1 ? 1 : -1) << endl;
return 0;
}
if (a[0] != 0) {
cout << -1 << endl;
return 0;
}
node[0] = 1;
for (ll i = 1; i <= n; ++i) {
if (a[i] > node[i - 1] * 2) {
cout << -1 << endl;
return 0;
}
node[i] = 2 * node[i - 1] - a[i];
}
node[n] = 0;
for (ll i = n; i >= 0; --i) {
node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
res += node[i] + a[i];
}
cout << res << endl;
} | #include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;
int main() {
ll n, a[MAX + 100], node[MAX + 100], res = 0;
cin >> n;
for (ll i = 0; i <= n; ++i) {
cin >> a[i];
}
if (n == 0) {
cout << (a[0] == 1 ? 1 : -1) << endl;
return 0;
}
if (a[0] != 0) {
cout << -1 << endl;
return 0;
}
node[0] = 1;
for (ll i = 1; i <= n; ++i) {
if (a[i] > node[i - 1] * 2) {
cout << -1 << endl;
return 0;
}
node[i] = 2 * node[i - 1] - a[i];
}
node[n] = 0;
for (ll i = n + 1; i-- > 0;) {
node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
res += node[i] + a[i];
}
cout << res << endl;
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
int n;
cin >> n;
bool b = 0;
v1li v(n + 1), u(n + 1, 1);
rep(i, n + 1) cin >> v[i];
lint a = 0, tmp = 0, tt = 0;
rep(i, n + 1) {
if (u[i] < v[i]) {
b = 1;
break;
}
u[i + 1] = (u[i] - v[i]) * 2;
if (u[i + 2] > 500000000)
u[i + 2] = 500000000;
}
rrep(i, n + 1) {
tt += v[i];
tmp = min(tt, u[i]);
a += tmp;
tt = tmp;
}
if (n == 0)
a = v[0];
if (b)
a = -1;
cout << a << "\n";
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;
int main() {
int n;
cin >> n;
bool b = 0;
v1li v(n + 1), u(n + 1, 1);
rep(i, n + 1) cin >> v[i];
lint a = 0, tmp = 0, tt = 0;
rep(i, n + 1) {
if (u[i] < v[i]) {
b = 1;
break;
}
if (i < n) {
u[i + 1] = (u[i] - v[i]) * 2;
if (u[i + 1] > 1e15)
u[i + 1] = 1e15;
}
}
rrep(i, n + 1) {
tt += v[i];
tmp = min(tt, u[i]);
a += tmp;
tt = tmp;
}
if (n == 0)
a = v[0];
if (b)
a = -1;
cout << a << "\n";
return 0;
}
| replace | 41 | 44 | 41 | 46 | 0 | |
p02665 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
if (n == 0) {
while (1) {
}
return 0;
}
if (a[0] != 0) {
cout << "-1\n";
return 0;
}
b[0] = 1;
for (int i = 1; i <= n; i++) {
ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
if (lim < b[i - 1] - a[i] || lim < 0) {
cout << "-1\n";
return 0;
}
b[i] = lim;
}
ll sol = 0;
for (int i = 0; i <= n; i++) {
sol += a[i] + b[i];
}
cout << sol << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
if (n == 0) {
if (a[0] == 1) {
cout << "1\n";
} else {
cout << "-1\n";
}
return 0;
}
if (a[0] != 0) {
cout << "-1\n";
return 0;
}
b[0] = 1;
for (int i = 1; i <= n; i++) {
ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
if (lim < b[i - 1] - a[i] || lim < 0) {
cout << "-1\n";
return 0;
}
b[i] = lim;
}
ll sol = 0;
for (int i = 0; i <= n; i++) {
sol += a[i] + b[i];
}
cout << sol << "\n";
return 0;
}
| replace | 25 | 26 | 25 | 29 | TLE | |
p02665 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define endl '\n'
template <typename T> inline bool chmin(T &x, T a) {
if (x >= a) {
x = a;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &x, T a) {
if (x <= a) {
x = a;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
int N;
int a;
cin >> N >> a;
if (N == 0) {
if (a != 1) {
cout << -1 << endl;
} else {
cout << 1 << endl;
}
return 0;
} else if (a != 0) {
cout << -1 << endl;
return -1;
}
vector<ll> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
B[i] = 0;
}
for (int i = 0; i < N; i++) {
if (i == 0) {
B[i] = 2;
} else {
B[i] = B[i - 1] * 2;
}
if (A[i] > B[i]) {
cout << -1 << endl;
return 0;
}
B[i] -= A[i];
if (B[i] > (1LL << 50)) {
B[i] = 1LL << 50;
}
}
ll ans = 0;
ll temp = 0;
for (int i = N - 1; i >= -1; i--) {
if (i == -1) {
ans++;
break;
}
temp += A[i];
ans += temp;
if (i > 0) {
chmin(temp, B[i - 1]);
} else {
chmin(temp, 1LL);
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define endl '\n'
template <typename T> inline bool chmin(T &x, T a) {
if (x >= a) {
x = a;
return true;
}
return false;
}
template <typename T> inline bool chmax(T &x, T a) {
if (x <= a) {
x = a;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
int N;
int a;
cin >> N >> a;
if (N == 0) {
if (a != 1) {
cout << -1 << endl;
} else {
cout << 1 << endl;
}
return 0;
} else if (a != 0) {
cout << -1 << endl;
return 0;
}
vector<ll> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
B[i] = 0;
}
for (int i = 0; i < N; i++) {
if (i == 0) {
B[i] = 2;
} else {
B[i] = B[i - 1] * 2;
}
if (A[i] > B[i]) {
cout << -1 << endl;
return 0;
}
B[i] -= A[i];
if (B[i] > (1LL << 50)) {
B[i] = 1LL << 50;
}
}
ll ans = 0;
ll temp = 0;
for (int i = N - 1; i >= -1; i--) {
if (i == -1) {
ans++;
break;
}
temp += A[i];
ans += temp;
if (i > 0) {
chmin(temp, B[i - 1]);
} else {
chmin(temp, 1LL);
}
}
cout << ans << endl;
return 0;
} | replace | 57 | 58 | 57 | 58 | 0 | |
p02665 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 0)
exit(1);
vector<long long> A(N + 1), B(N + 1);
for (int i = 0; i <= N; i++) {
cin >> A[i];
}
long long nodes = 1;
long long sum = A[N];
B[N] = 0ll;
for (int i = N - 1; i >= 0; i--) {
B[i] = sum;
sum += A[i];
if (i <= 30) {
B[i] = min((1ll << i) - A[i], B[i]);
}
}
nodes = 1ll;
for (int i = 1; i <= N; i++) {
B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
if (B[i] < 0ll) {
puts("-1");
return 0;
}
nodes += A[i] + B[i];
}
cout << nodes << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 0) {
int a;
cin >> a;
if (a == 1)
puts("1");
else
puts("-1");
return 0;
}
vector<long long> A(N + 1), B(N + 1);
for (int i = 0; i <= N; i++) {
cin >> A[i];
}
long long nodes = 1;
long long sum = A[N];
B[N] = 0ll;
for (int i = N - 1; i >= 0; i--) {
B[i] = sum;
sum += A[i];
if (i <= 30) {
B[i] = min((1ll << i) - A[i], B[i]);
}
}
nodes = 1ll;
for (int i = 1; i <= N; i++) {
B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
if (B[i] < 0ll) {
puts("-1");
return 0;
}
nodes += A[i] + B[i];
}
cout << nodes << endl;
return 0;
} | replace | 7 | 9 | 7 | 16 | 0 | |
p02665 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;
int main() {
int N;
cin >> N;
vector<ll> A(N + 1);
for (int i = 0; i < N + 1; i++) {
cin >> A[i];
}
if (N == 0) {
if (A[0] == 1) {
cout << 1 << endl;
} else {
cout << -1 << endl;
}
return 0;
}
vector<ll> max_nodes(N + 1);
max_nodes[0] = 1;
for (int i = 1; i <= N; i++) {
max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
}
vector<ll> l(N + 1), r(N + 1);
if (A[N] > max_nodes[N]) {
cout << -1 << endl;
return 0;
}
for (int i = N - 1; i >= 0; i--) {
l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
if (l[i] + A[i] > max_nodes[i] || l > r) {
cout << -1 << endl;
return 0;
}
}
ll ans = 1;
ll vs = 1;
for (int i = 1; i <= N; i++) {
vs = min(vs * 2 - A[i], r[i]);
ans += vs + A[i];
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;
int main() {
int N;
cin >> N;
vector<ll> A(N + 1);
for (int i = 0; i < N + 1; i++) {
cin >> A[i];
}
if (N == 0) {
if (A[0] == 1) {
cout << 1 << endl;
} else {
cout << -1 << endl;
}
return 0;
}
vector<ll> max_nodes(N + 1);
max_nodes[0] = 1;
for (int i = 1; i <= N; i++) {
max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
}
vector<ll> l(N + 1), r(N + 1);
if (A[N] > max_nodes[N]) {
cout << -1 << endl;
return 0;
}
for (int i = N - 1; i >= 0; i--) {
l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
if (l[i] + A[i] > max_nodes[i] || l[i] > r[i]) {
cout << -1 << endl;
return 0;
}
}
ll ans = 1;
ll vs = 1;
for (int i = 1; i <= N; i++) {
vs = min(vs * 2 - A[i], r[i]);
ans += vs + A[i];
}
cout << ans << endl;
return 0;
} | replace | 37 | 38 | 37 | 38 | TLE | |
p02665 | C++ | Runtime Error | // clang-format off
#include <bits/stdc++.h>
#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on
const int MAX_N = 100000;
int n;
ll a[MAX_N];
ll mx[MAX_N + 2];
ll mn[MAX_N + 1];
int main() {
fastio();
cin >> n;
forn(i, n + 1) cin >> a[i];
mx[n] = a[n];
for (int i = n - 1; i >= 0; i--) {
mx[i] = a[i] + mx[i + 1];
}
bool ok = true;
ll ans = mn[0] = 1;
ok &= mn[0] >= a[0];
for (int i = 1; i <= n; i++) {
mn[i] = min(2LL * (mn[i - 1] - a[i - 1]), mx[i]);
ok &= mn[i] >= a[i];
ans += mn[i];
}
ok &= mn[n] == a[n];
if (ok) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
} | // clang-format off
#include <bits/stdc++.h>
#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on
const int MAX_N = 100000;
int n;
ll a[MAX_N + 1];
ll mx[MAX_N + 1];
ll mn[MAX_N + 1];
int main() {
fastio();
cin >> n;
forn(i, n + 1) cin >> a[i];
mx[n] = a[n];
for (int i = n - 1; i >= 0; i--) {
mx[i] = a[i] + mx[i + 1];
}
bool ok = true;
ll ans = mn[0] = 1;
ok &= mn[0] >= a[0];
for (int i = 1; i <= n; i++) {
mn[i] = min(2LL * (mn[i - 1] - a[i - 1]), mx[i]);
ok &= mn[i] >= a[i];
ans += mn[i];
}
ok &= mn[n] == a[n];
if (ok) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
} | replace | 39 | 41 | 39 | 41 | 0 | |
p02666 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003, M = 1000000007;
int n;
int p[N];
int qmm;
vector<int> g[N];
int ast[N], f[N];
bool c[N];
int q;
void dfs(int x) {
c[x] = true;
++q;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
dfs(h);
}
if (p[x] != -1 && !c[p[x]])
dfs(p[x]);
}
int ans;
int dp[N][N];
void solv() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i]);
if (p[i] != -1)
g[p[i]].push_back(i);
else
++qmm;
}
ast[0] = 1;
for (int i = 1; i <= n; ++i)
ast[i] = (ast[i - 1] * 1LL * (n - 1)) % M;
f[0] = 1;
for (int i = 1; i <= n; ++i)
f[i] = (f[i - 1] * 1LL * i) % M;
int ans = 0;
vector<int> v;
for (int i = 1; i <= n; ++i) {
if (p[i] == -1) {
q = 0;
dfs(i);
v.push_back(q);
}
}
for (int i = 1; i <= n; ++i) {
if (!c[i]) {
dfs(i);
ans = (ans + ast[qmm]) % M;
}
}
dp[0][0] = 1;
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v.size(); ++j) {
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % M;
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * 1LL * v[i]) % M;
}
}
for (int j = 2; j <= v.size(); ++j) {
ans =
(ans + (dp[v.size()][j] * 1LL * f[j - 1]) % M * 1LL * ast[qmm - j]) % M;
}
for (int i = 0; i < v.size(); ++i) {
ans = (ans + (v[i] - 1) * 1LL * ast[qmm - 1]) % M;
}
ans = ((n * 1LL * ast[qmm]) % M - ans + M) % M;
printf("%d\n", ans);
}
int main() {
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
// ios_base::sync_with_stdio(false);
// cin.tie(0);
solv();
return 0;
}
// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
| #include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 5003, M = 1000000007;
int n;
int p[N];
int qmm;
vector<int> g[N];
int ast[N], f[N];
bool c[N];
int q;
void dfs(int x) {
c[x] = true;
++q;
for (int i = 0; i < g[x].size(); ++i) {
int h = g[x][i];
if (!c[h])
dfs(h);
}
if (p[x] != -1 && !c[p[x]])
dfs(p[x]);
}
int ans;
int dp[N][N];
void solv() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i]);
if (p[i] != -1)
g[p[i]].push_back(i);
else
++qmm;
}
ast[0] = 1;
for (int i = 1; i <= n; ++i)
ast[i] = (ast[i - 1] * 1LL * (n - 1)) % M;
f[0] = 1;
for (int i = 1; i <= n; ++i)
f[i] = (f[i - 1] * 1LL * i) % M;
int ans = 0;
vector<int> v;
for (int i = 1; i <= n; ++i) {
if (p[i] == -1) {
q = 0;
dfs(i);
v.push_back(q);
}
}
for (int i = 1; i <= n; ++i) {
if (!c[i]) {
dfs(i);
ans = (ans + ast[qmm]) % M;
}
}
dp[0][0] = 1;
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v.size(); ++j) {
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % M;
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * 1LL * v[i]) % M;
}
}
for (int j = 2; j <= v.size(); ++j) {
ans =
(ans + (dp[v.size()][j] * 1LL * f[j - 1]) % M * 1LL * ast[qmm - j]) % M;
}
for (int i = 0; i < v.size(); ++i) {
ans = (ans + (v[i] - 1) * 1LL * ast[qmm - 1]) % M;
}
ans = ((n * 1LL * ast[qmm]) % M - ans + M) % M;
printf("%d\n", ans);
}
int main() {
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
// ios_base::sync_with_stdio(false);
// cin.tie(0);
solv();
return 0;
}
// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
| replace | 26 | 27 | 26 | 28 | 127 | /tmp/a716a81f-5f76-4e0d-953e-f6aa9ec400fa.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p02666 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma comment(linker, "/STACK:526000000")
#include "bits/stdc++.h"
#define int ll
using namespace std;
typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()
int is_ok(int now) {
int cnts[10] = {};
while (now != 0) {
cnts[now % 10]++;
if (cnts[now % 10] == 2)
return false;
now /= 10;
}
return true;
}
int is_Prime(int now) {
if (now == 1)
return false;
for (int i = 2; i * i <= now; ++i) {
if (now % i == 0)
return false;
}
return true;
}
class EllysDifferentPrimes {
public:
static int getClosest(int n) {
REP(q, 1e9) {
if (n - q > 0 and is_ok(n - q) == 1 and is_Prime(n - q) == 1) {
return n - q;
}
if (is_ok(n + q) == 1 and is_Prime(n + q) == 1) {
return n + q;
}
}
return -1;
}
};
// geometry library
typedef complex<long double> Point;
typedef pair<complex<long double>, complex<long double>> Line;
typedef struct Circle {
complex<long double> center;
long double r;
} Circle;
long double dot(Point a, Point b) {
return (a.real() * b.real() + a.imag() * b.imag());
}
long double cross(Point a, Point b) {
return (a.real() * b.imag() - a.imag() * b.real());
}
long double Dist_Line_Point(Line a, Point b) {
if (dot(a.second - a.first, b - a.first) < eps)
return abs(b - a.first);
if (dot(a.first - a.second, b - a.second) < eps)
return abs(b - a.second);
return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);
}
int is_intersected_ls(Line a, Line b) {
return (cross(a.second - a.first, b.first - a.first) *
cross(a.second - a.first, b.second - a.first) <
eps) &&
(cross(b.second - b.first, a.first - b.first) *
cross(b.second - b.first, a.second - b.first) <
eps);
}
Point intersection_l(Line a, Line b) {
Point da = a.second - a.first;
Point db = b.second - b.first;
return a.first + da * cross(db, b.first - a.first) / cross(db, da);
}
long double Dist_Line_Line(Line a, Line b) {
if (is_intersected_ls(a, b) == 1) {
return 0;
}
return min({Dist_Line_Point(a, b.first), Dist_Line_Point(a, b.second),
Dist_Line_Point(b, a.first), Dist_Line_Point(b, a.second)});
}
pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {
long double dist = abs(a.center - b.center);
assert(dist <= eps + a.r + b.r);
assert(dist + eps >= abs(a.r - b.r));
Point target = b.center - a.center;
long double pointer =
target.real() * target.real() + target.imag() * target.imag();
long double aa = pointer + a.r * a.r - b.r * b.r;
aa /= 2.0L;
Point l{(aa * target.real() +
target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer,
(aa * target.imag() -
target.real() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer};
Point r{(aa * target.real() -
target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer,
(aa * target.imag() +
target.real() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer};
r = r + a.center;
l = l + a.center;
return mp(l, r);
}
// end of geometry
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename A> A pows(A val, ll b) {
assert(b >= 1);
A ans = val;
b--;
while (b) {
if (b % 2) {
ans *= val;
}
val *= val;
b /= 2LL;
}
return ans;
}
template <typename A> class Compressor {
public:
bool is_zipped = false;
map<A, ll> zipper;
map<ll, A> unzipper;
queue<A> fetcher;
Compressor() {
is_zipped = false;
zipper.clear();
unzipper.clear();
}
void add(A now) {
assert(is_zipped == false);
zipper[now] = 1;
fetcher.push(now);
}
void exec() {
assert(is_zipped == false);
int cnt = 0;
for (auto i = zipper.begin(); i != zipper.end(); ++i) {
i->second = cnt;
unzipper[cnt] = i->first;
cnt++;
}
is_zipped = true;
}
ll fetch() {
assert(is_zipped == true);
A hoge = fetcher.front();
fetcher.pop();
return zipper[hoge];
}
ll zip(A now) {
assert(is_zipped == true);
assert(zipper.find(now) != zipper.end());
return zipper[now];
}
A unzip(ll a) {
assert(is_zipped == true);
assert(a < unzipper.size());
return unzipper[a];
}
ll next(A now) {
auto x = zipper.upper_bound(now);
if (x == zipper.end())
return zipper.size();
return (ll)((*x).second);
}
ll back(A now) {
auto x = zipper.lower_bound(now);
if (x == zipper.begin())
return -1;
x--;
return (ll)((*x).second);
}
};
template <typename A> class Matrix {
public:
vector<vector<A>> data;
Matrix(vector<vector<A>> a) : data(a) {}
Matrix operator+(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data.size());
assert(obj.data[0].size() == this->data[0].size());
REP(i, obj.data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[i].size()) {
A hoge = obj.data[i][q] + (this->data[i][q]);
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix operator-(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data.size());
assert(obj.data[0].size() == this->data[0].size());
REP(i, obj.data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[i].size()) {
A hoge = this->data[i][q] - obj.data[i][q];
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix operator*(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data[0].size());
REP(i, this->data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[0].size()) {
A hoge = ((this->data[i][0]) * (obj.data[0][q]));
for (int t = 1; t < obj.data.size(); ++t) {
hoge += ((this->data[i][t]) * obj.data[t][q]);
}
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix &operator*=(const Matrix obj) {
*this = (*this * obj);
return *this;
}
Matrix &operator+=(const Matrix obj) {
*this = (*this + obj);
return *this;
}
Matrix &operator-=(const Matrix obj) {
*this = (*this - obj);
return *this;
}
};
struct Fraction {
ll a;
ll b;
Fraction() : a(0LL), b(1LL) {}
Fraction(ll c, ll d) {
int hoge = gcd(llabs(c), llabs(d));
c /= hoge;
d /= hoge;
if (d < 0) {
d *= -1;
c *= -1;
}
a = c;
b = d;
}
bool operator<(Fraction rhs) const { return a * rhs.b < rhs.a * b; }
};
template <std::uint_fast64_t mod> class modint {
public:
using u64 = std::uint_fast64_t;
u64 value = 0;
modint() : value(0LL) {}
modint(ll a) : value(((a % mod) + 2 * mod) % mod) {}
constexpr modint operator+(const modint rhs) const {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) {
value += rhs.value;
if (value >= mod) {
value -= mod;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) {
if (value < rhs.value) {
value += mod;
}
value -= rhs.value;
return *this;
}
constexpr modint &operator*=(const modint rhs) {
value = (value * rhs.value) % mod;
return *this;
}
constexpr modint &operator/=(modint rhs) {
ll rem = mod - 2;
while (rem) {
if (rem % 2) {
*this *= rhs;
}
rhs *= rhs;
rem /= 2LL;
}
return *this;
}
bool operator<(modint rhs) const { return value < rhs.value; }
friend ostream &operator<<(ostream &os, modint &p) {
os << p.value;
return (os);
}
};
class Dice {
public:
vector<ll> vertexs;
// Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5
Dice(vector<ll> init) : vertexs(init) {}
// Look from Center
void RtoL() {
for (int q = 1; q < 4; ++q) {
swap(vertexs[q], vertexs[q + 1]);
}
}
void LtoR() {
for (int q = 3; q >= 1; --q) {
swap(vertexs[q], vertexs[q + 1]);
}
}
void UtoD() {
swap(vertexs[5], vertexs[4]);
swap(vertexs[2], vertexs[5]);
swap(vertexs[0], vertexs[2]);
}
void DtoU() {
swap(vertexs[0], vertexs[2]);
swap(vertexs[2], vertexs[5]);
swap(vertexs[5], vertexs[4]);
}
bool ReachAble(Dice now) {
set<Dice> hoge;
queue<Dice> next;
next.push(now);
hoge.insert(now);
while (next.empty() == false) {
Dice seeing = next.front();
next.pop();
if (seeing == *this)
return true;
seeing.RtoL();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.LtoR();
seeing.LtoR();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.RtoL();
seeing.UtoD();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.DtoU();
seeing.DtoU();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
}
return false;
}
bool operator==(const Dice &a) {
for (int q = 0; q < 6; ++q) {
if (a.vertexs[q] != (*this).vertexs[q]) {
return false;
}
}
return true;
}
bool operator<(const Dice &a) const { return (*this).vertexs < a.vertexs; }
};
pair<Dice, Dice> TwoDimDice(int center, int up) {
int target = 1;
while (true) {
if (center != target && 7 - center != target && up != target &&
7 - up != target) {
break;
}
target++;
}
return mp(
Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}),
Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up}));
}
tuple<Dice, Dice, Dice, Dice> OneDimDice(int center) {
int bo = min(center, 7 - center);
pair<int, int> goa;
if (bo == 1) {
goa = mp(2, 3);
} else if (bo == 2) {
goa = mp(1, 3);
} else if (bo == 3) {
goa = mp(1, 2);
}
tuple<Dice, Dice, Dice, Dice> now =
make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second,
7 - center, 7 - goa.first}),
Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second,
7 - center, 7 - goa.first}),
Dice(vector<ll>{7 - goa.first, goa.second, center,
7 - goa.second, 7 - center, goa.first}),
Dice(vector<ll>{7 - goa.first, 7 - goa.second, center,
goa.second, 7 - center, goa.first}));
return now;
}
template <typename A, typename B> class Dijkstra {
public:
vector<vector<pair<int, A>>> vertexs;
B Cost_Function;
Dijkstra(int n, B cost) : Cost_Function(cost) {
vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
}
~Dijkstra() { vertexs.clear(); }
void add_edge(int a, int b, A c) { vertexs[a].push_back(mp(b, c)); }
vector<ll> build_result(int StartPoint) {
vector<ll> dist(vertexs.size(), 2e18);
dist[StartPoint] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
next;
next.push(make_pair(0, StartPoint));
while (next.empty() == false) {
pair<ll, int> now = next.top();
next.pop();
if (dist[now.second] != now.first)
continue;
for (auto x : vertexs[now.second]) {
ll now_cost = now.first + Cost_Function(x.second);
if (dist[x.first] > now_cost) {
dist[x.first] = now_cost;
next.push(mp(now_cost, x.first));
}
}
}
return dist;
}
};
class Dinic {
public:
struct edge {
int to;
int cap;
int rev;
};
vector<vector<edge>> Graph;
vector<int> level;
vector<int> itr;
Dinic(int n) { Graph = vector<vector<edge>>(n, vector<edge>()); }
void add_edge(int a, int b, int cap) {
Graph[a].push_back(edge{b, cap, (int)Graph[b].size()});
Graph[b].push_back(edge{a, 0, (int)Graph[a].size() - 1});
}
void bfs(int s) {
level = vector<int>(Graph.size(), -1);
level[s] = 0;
queue<int> next;
next.push(s);
while (next.empty() == false) {
int now = next.front();
next.pop();
for (auto x : Graph[now]) {
if (x.cap == 0)
continue;
if (level[x.to] == -1) {
level[x.to] = level[now] + 1;
next.push(x.to);
}
}
}
}
int dfs(int now, int goal, int val) {
if (goal == now)
return val;
for (int &i = itr[now]; i < (int)Graph[now].size(); ++i) {
edge &target = Graph[now][i];
if (target.cap > 0 && level[now] < level[target.to]) {
int d = dfs(target.to, goal, min(val, target.cap));
if (d > 0) {
target.cap -= d;
Graph[target.to][target.rev].cap += d;
return d;
}
}
}
return 0;
}
int run(int s, int t) {
int ans = 0;
int f = 0;
while (bfs(s), level[t] >= 0) {
itr = vector<int>(Graph.size(), 0);
while ((f = dfs(s, t, 1e9)) > 0) {
ans += f;
}
}
return ans;
}
};
class HLDecomposition {
public:
vector<vector<int>> vertexs;
vector<int> depth;
vector<int> backs;
vector<int> connections;
vector<int> zip, unzip;
HLDecomposition(int n) {
vertexs = vector<vector<int>>(n, vector<int>());
depth = vector<int>(n);
zip = vector<int>(n);
unzip = zip;
}
void add_edge(int a, int b) {
vertexs[a].push_back(b);
vertexs[b].push_back(a);
}
int depth_dfs(int now, int back) {
depth[now] = 0;
for (auto x : vertexs[now]) {
if (x == back)
continue;
depth[now] = max(depth[now], 1 + depth_dfs(x, now));
}
return depth[now];
}
void dfs(int now, int backing) {
zip[now] = backs.size();
unzip[backs.size()] = now;
backs.push_back(backing);
int now_max = -1;
int itr = -1;
for (auto x : vertexs[now]) {
if (depth[x] > depth[now])
continue;
if (now_max < depth[x]) {
now_max = depth[x];
itr = x;
}
}
if (itr == -1)
return;
connections.push_back(connections.back());
dfs(itr, backing);
for (auto x : vertexs[now]) {
if (depth[x] > depth[now])
continue;
if (x == itr)
continue;
connections.push_back(zip[now]);
dfs(x, backs.size());
}
return;
}
void build() {
depth_dfs(0, -1);
connections.push_back(-1);
dfs(0, -1);
}
vector<pair<int, int>> query(int a, int b) {
a = zip[a];
b = zip[b];
vector<pair<int, int>> ans;
while (backs[a] != backs[b]) {
if (a < b)
swap(a, b);
ans.push_back(mp(backs[a], a + 1));
a = connections[a];
}
if (a > b)
swap(a, b);
ans.push_back(mp(a, b + 1));
return ans;
}
int lca(int a, int b) {
a = zip[a];
b = zip[b];
while (backs[a] != backs[b]) {
if (a < b)
swap(a, b);
a = connections[a];
}
return unzip[min(a, b)];
}
};
// by ei1333
// https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz + 1, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz, height;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
const OperatorMonoid OM0)
: f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
height = 0;
while (sz < n)
sz <<= 1, height++;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
inline void propagate(int k) {
if (lazy[k] != OM0) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
data[k] = reflect(k);
lazy[k] = OM0;
}
}
inline Monoid reflect(int k) {
return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
}
inline void recalc(int k) {
while (k >>= 1)
data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
}
inline void thrust(int k) {
for (int i = height; i > 0; i--)
propagate(k >> i);
}
void update(int a, int b, const OperatorMonoid &x) {
thrust(a += sz);
thrust(b += sz - 1);
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
lazy[l] = h(lazy[l], x), ++l;
if (r & 1)
--r, lazy[r] = h(lazy[r], x);
}
recalc(a);
recalc(b);
}
Monoid query(int a, int b) {
thrust(a += sz);
thrust(b += sz - 1);
Monoid L = M1, R = M1;
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
L = f(L, reflect(l++));
if (r & 1)
R = f(reflect(--r), R);
}
return f(L, R);
}
Monoid operator[](const int &k) { return query(k, k + 1); }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
propagate(a);
Monoid nxt =
type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, reflect(1))))
return find_subtree(1, check, L, false);
return -1;
}
thrust(a + sz);
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, reflect(a));
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(reflect(1), R)))
return find_subtree(1, check, R, true);
return -1;
}
thrust(b + sz - 1);
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(reflect(--b), R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
class KMP {
public:
vector<ll> table;
vector<ll> Pattern;
KMP(vector<ll> a) { build(a); }
void build(vector<ll> a) {
Pattern = a;
table = vector<ll>(a.size() + 1, -1);
int j = -1;
for (int i = 0; i < a.size(); ++i) {
while (j >= 0 && Pattern[i] != Pattern[j]) {
j = table[j];
}
table[i + 1] = ++j;
}
return;
}
vector<ll> search(vector<ll> a) {
vector<ll> ans;
for (int i = 0, k = 0; i < a.size(); ++i) {
while (k >= 0 && a[i] != Pattern[k])
k = table[k];
++k;
if (k >= Pattern.size()) {
ans.push_back(i - Pattern.size() + 1);
k = table[k];
}
}
return ans;
}
};
unsigned long xor128() {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
void init() {
iostream::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
#define int ll
template <typename flow_t, typename cost_t> struct PrimalDual {
const cost_t INF;
struct edge {
int to;
flow_t cap;
cost_t cost;
int rev;
bool isrev;
};
vector<vector<edge>> graph;
vector<cost_t> potential, min_cost;
vector<int> prevv, preve;
PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {}
void add_edge(int from, int to, flow_t cap, cost_t cost) {
graph[from].emplace_back(edge{to, cap, cost, (int)graph[to].size(), false});
graph[to].emplace_back(
edge{from, 0, -cost, (int)graph[from].size() - 1, true});
}
cost_t min_cost_flow(int s, int t, flow_t f) {
int V = (int)graph.size();
cost_t ret = 0;
using Pi = pair<cost_t, int>;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
potential.assign(V, 0);
preve.assign(V, -1);
prevv.assign(V, -1);
while (f > 0) {
min_cost.assign(V, INF);
que.emplace(0, s);
min_cost[s] = 0;
while (!que.empty()) {
Pi p = que.top();
que.pop();
if (min_cost[p.second] < p.first)
continue;
for (int i = 0; i < graph[p.second].size(); i++) {
edge &e = graph[p.second][i];
cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] -
potential[e.to];
if (e.cap > 0 && min_cost[e.to] > nextCost) {
min_cost[e.to] = nextCost;
prevv[e.to] = p.second, preve[e.to] = i;
que.emplace(min_cost[e.to], e.to);
}
}
}
if (min_cost[t] == INF)
return -1;
for (int v = 0; v < V; v++)
potential[v] += min_cost[v];
flow_t addflow = f;
for (int v = t; v != s; v = prevv[v]) {
addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
}
f -= addflow;
ret += addflow * potential[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = graph[prevv[v]][preve[v]];
e.cap -= addflow;
graph[v][e.rev].cap += addflow;
}
}
return ret;
}
void output() {
for (int i = 0; i < graph.size(); i++) {
for (auto &e : graph[i]) {
if (e.isrev)
continue;
auto &rev_e = graph[e.to][e.rev];
cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/"
<< rev_e.cap + e.cap << ")" << endl;
}
}
}
};
// base64 エンコード
bool encode_base64(const std::vector<unsigned char> &src, std::string &dst) {
const std::string table(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::string cdst;
for (std::size_t i = 0; i < src.size(); ++i) {
switch (i % 3) {
case 0:
cdst.push_back(table[(src[i] & 0xFC) >> 2]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x03) << 4]);
cdst.push_back('=');
cdst.push_back('=');
}
break;
case 1:
cdst.push_back(
table[((src[i - 1] & 0x03) << 4) | ((src[i + 0] & 0xF0) >> 4)]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x0F) << 2]);
cdst.push_back('=');
}
break;
case 2:
cdst.push_back(
table[((src[i - 1] & 0x0F) << 2) | ((src[i + 0] & 0xC0) >> 6)]);
cdst.push_back(table[src[i] & 0x3F]);
break;
}
}
dst.swap(cdst);
return true;
}
int ut[30000];
int uf(int now) {
if (now == ut[now])
return now;
return ut[now] = uf(ut[now]);
}
int um(int a, int b) {
a = uf(a);
b = uf(b);
if (a == b)
return 0;
ut[a] = b;
return 1;
}
int cnter[30000];
modint<MAX_MOD> dp[6000];
void solve() {
int n;
cin >> n;
vector<int> inputs;
REP(i, n) {
int a;
cin >> a;
a--;
a = max(a, -1LL);
inputs.push_back(a);
ut[i] = i;
}
int base_cnt = 0;
REP(i, n) {
if (inputs[i] == -1)
continue;
base_cnt += 1 - um(i, inputs[i]);
}
REP(i, n) { cnter[uf(i)]++; }
vector<int> Finds;
REP(i, n) {
if (inputs[i] == -1) {
Finds.push_back(cnter[uf(i)]);
}
}
REP(i, Finds.size()) {
for (int q = i; q >= 0; --q) {
dp[q + 1] += dp[q] * Finds[i];
}
dp[0] += Finds[i];
}
modint<MAX_MOD> ans =
pows<modint<MAX_MOD>>(n - 1, Finds.size()) * (n - base_cnt);
modint<MAX_MOD> gekos = 1;
dp[0] -= Finds.size();
for (int i = 0; i < Finds.size(); ++i) {
modint<MAX_MOD> hoge = dp[i] * gekos;
if (Finds.size() - i - 1 != 0) {
hoge *= pows<modint<MAX_MOD>>(n - 1, Finds.size() - i - 1);
}
ans -= hoge;
gekos *= (i + 1);
}
cout << ans << endl;
}
#undef int
int main() {
init();
solve();
}
| #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma comment(linker, "/STACK:526000000")
#include "bits/stdc++.h"
#define int ll
using namespace std;
typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()
int is_ok(int now) {
int cnts[10] = {};
while (now != 0) {
cnts[now % 10]++;
if (cnts[now % 10] == 2)
return false;
now /= 10;
}
return true;
}
int is_Prime(int now) {
if (now == 1)
return false;
for (int i = 2; i * i <= now; ++i) {
if (now % i == 0)
return false;
}
return true;
}
class EllysDifferentPrimes {
public:
static int getClosest(int n) {
REP(q, 1e9) {
if (n - q > 0 and is_ok(n - q) == 1 and is_Prime(n - q) == 1) {
return n - q;
}
if (is_ok(n + q) == 1 and is_Prime(n + q) == 1) {
return n + q;
}
}
return -1;
}
};
// geometry library
typedef complex<long double> Point;
typedef pair<complex<long double>, complex<long double>> Line;
typedef struct Circle {
complex<long double> center;
long double r;
} Circle;
long double dot(Point a, Point b) {
return (a.real() * b.real() + a.imag() * b.imag());
}
long double cross(Point a, Point b) {
return (a.real() * b.imag() - a.imag() * b.real());
}
long double Dist_Line_Point(Line a, Point b) {
if (dot(a.second - a.first, b - a.first) < eps)
return abs(b - a.first);
if (dot(a.first - a.second, b - a.second) < eps)
return abs(b - a.second);
return abs(cross(a.second - a.first, b - a.first)) / abs(a.second - a.first);
}
int is_intersected_ls(Line a, Line b) {
return (cross(a.second - a.first, b.first - a.first) *
cross(a.second - a.first, b.second - a.first) <
eps) &&
(cross(b.second - b.first, a.first - b.first) *
cross(b.second - b.first, a.second - b.first) <
eps);
}
Point intersection_l(Line a, Line b) {
Point da = a.second - a.first;
Point db = b.second - b.first;
return a.first + da * cross(db, b.first - a.first) / cross(db, da);
}
long double Dist_Line_Line(Line a, Line b) {
if (is_intersected_ls(a, b) == 1) {
return 0;
}
return min({Dist_Line_Point(a, b.first), Dist_Line_Point(a, b.second),
Dist_Line_Point(b, a.first), Dist_Line_Point(b, a.second)});
}
pair<Point, Point> intersection_Circle_Circle(Circle a, Circle b) {
long double dist = abs(a.center - b.center);
assert(dist <= eps + a.r + b.r);
assert(dist + eps >= abs(a.r - b.r));
Point target = b.center - a.center;
long double pointer =
target.real() * target.real() + target.imag() * target.imag();
long double aa = pointer + a.r * a.r - b.r * b.r;
aa /= 2.0L;
Point l{(aa * target.real() +
target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer,
(aa * target.imag() -
target.real() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer};
Point r{(aa * target.real() -
target.imag() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer,
(aa * target.imag() +
target.real() * sqrt(pointer * a.r * a.r - aa * aa)) /
pointer};
r = r + a.center;
l = l + a.center;
return mp(l, r);
}
// end of geometry
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename A> A pows(A val, ll b) {
assert(b >= 1);
A ans = val;
b--;
while (b) {
if (b % 2) {
ans *= val;
}
val *= val;
b /= 2LL;
}
return ans;
}
template <typename A> class Compressor {
public:
bool is_zipped = false;
map<A, ll> zipper;
map<ll, A> unzipper;
queue<A> fetcher;
Compressor() {
is_zipped = false;
zipper.clear();
unzipper.clear();
}
void add(A now) {
assert(is_zipped == false);
zipper[now] = 1;
fetcher.push(now);
}
void exec() {
assert(is_zipped == false);
int cnt = 0;
for (auto i = zipper.begin(); i != zipper.end(); ++i) {
i->second = cnt;
unzipper[cnt] = i->first;
cnt++;
}
is_zipped = true;
}
ll fetch() {
assert(is_zipped == true);
A hoge = fetcher.front();
fetcher.pop();
return zipper[hoge];
}
ll zip(A now) {
assert(is_zipped == true);
assert(zipper.find(now) != zipper.end());
return zipper[now];
}
A unzip(ll a) {
assert(is_zipped == true);
assert(a < unzipper.size());
return unzipper[a];
}
ll next(A now) {
auto x = zipper.upper_bound(now);
if (x == zipper.end())
return zipper.size();
return (ll)((*x).second);
}
ll back(A now) {
auto x = zipper.lower_bound(now);
if (x == zipper.begin())
return -1;
x--;
return (ll)((*x).second);
}
};
template <typename A> class Matrix {
public:
vector<vector<A>> data;
Matrix(vector<vector<A>> a) : data(a) {}
Matrix operator+(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data.size());
assert(obj.data[0].size() == this->data[0].size());
REP(i, obj.data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[i].size()) {
A hoge = obj.data[i][q] + (this->data[i][q]);
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix operator-(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data.size());
assert(obj.data[0].size() == this->data[0].size());
REP(i, obj.data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[i].size()) {
A hoge = this->data[i][q] - obj.data[i][q];
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix operator*(const Matrix obj) {
vector<vector<A>> ans;
assert(obj.data.size() == this->data[0].size());
REP(i, this->data.size()) {
ans.push_back(vector<A>());
REP(q, obj.data[0].size()) {
A hoge = ((this->data[i][0]) * (obj.data[0][q]));
for (int t = 1; t < obj.data.size(); ++t) {
hoge += ((this->data[i][t]) * obj.data[t][q]);
}
ans.back().push_back(hoge);
}
}
return Matrix(ans);
}
Matrix &operator*=(const Matrix obj) {
*this = (*this * obj);
return *this;
}
Matrix &operator+=(const Matrix obj) {
*this = (*this + obj);
return *this;
}
Matrix &operator-=(const Matrix obj) {
*this = (*this - obj);
return *this;
}
};
struct Fraction {
ll a;
ll b;
Fraction() : a(0LL), b(1LL) {}
Fraction(ll c, ll d) {
int hoge = gcd(llabs(c), llabs(d));
c /= hoge;
d /= hoge;
if (d < 0) {
d *= -1;
c *= -1;
}
a = c;
b = d;
}
bool operator<(Fraction rhs) const { return a * rhs.b < rhs.a * b; }
};
template <std::uint_fast64_t mod> class modint {
public:
using u64 = std::uint_fast64_t;
u64 value = 0;
modint() : value(0LL) {}
modint(ll a) : value(((a % mod) + 2 * mod) % mod) {}
constexpr modint operator+(const modint rhs) const {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) {
value += rhs.value;
if (value >= mod) {
value -= mod;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) {
if (value < rhs.value) {
value += mod;
}
value -= rhs.value;
return *this;
}
constexpr modint &operator*=(const modint rhs) {
value = (value * rhs.value) % mod;
return *this;
}
constexpr modint &operator/=(modint rhs) {
ll rem = mod - 2;
while (rem) {
if (rem % 2) {
*this *= rhs;
}
rhs *= rhs;
rem /= 2LL;
}
return *this;
}
bool operator<(modint rhs) const { return value < rhs.value; }
friend ostream &operator<<(ostream &os, modint &p) {
os << p.value;
return (os);
}
};
class Dice {
public:
vector<ll> vertexs;
// Up: 0,Left: 1,Center: 2,Right: 3,Adj: 4, Down: 5
Dice(vector<ll> init) : vertexs(init) {}
// Look from Center
void RtoL() {
for (int q = 1; q < 4; ++q) {
swap(vertexs[q], vertexs[q + 1]);
}
}
void LtoR() {
for (int q = 3; q >= 1; --q) {
swap(vertexs[q], vertexs[q + 1]);
}
}
void UtoD() {
swap(vertexs[5], vertexs[4]);
swap(vertexs[2], vertexs[5]);
swap(vertexs[0], vertexs[2]);
}
void DtoU() {
swap(vertexs[0], vertexs[2]);
swap(vertexs[2], vertexs[5]);
swap(vertexs[5], vertexs[4]);
}
bool ReachAble(Dice now) {
set<Dice> hoge;
queue<Dice> next;
next.push(now);
hoge.insert(now);
while (next.empty() == false) {
Dice seeing = next.front();
next.pop();
if (seeing == *this)
return true;
seeing.RtoL();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.LtoR();
seeing.LtoR();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.RtoL();
seeing.UtoD();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
seeing.DtoU();
seeing.DtoU();
if (hoge.count(seeing) == 0) {
hoge.insert(seeing);
next.push(seeing);
}
}
return false;
}
bool operator==(const Dice &a) {
for (int q = 0; q < 6; ++q) {
if (a.vertexs[q] != (*this).vertexs[q]) {
return false;
}
}
return true;
}
bool operator<(const Dice &a) const { return (*this).vertexs < a.vertexs; }
};
pair<Dice, Dice> TwoDimDice(int center, int up) {
int target = 1;
while (true) {
if (center != target && 7 - center != target && up != target &&
7 - up != target) {
break;
}
target++;
}
return mp(
Dice(vector<ll>{up, target, center, 7 - target, 7 - center, 7 - up}),
Dice(vector<ll>{up, 7 - target, center, target, 7 - center, 7 - up}));
}
tuple<Dice, Dice, Dice, Dice> OneDimDice(int center) {
int bo = min(center, 7 - center);
pair<int, int> goa;
if (bo == 1) {
goa = mp(2, 3);
} else if (bo == 2) {
goa = mp(1, 3);
} else if (bo == 3) {
goa = mp(1, 2);
}
tuple<Dice, Dice, Dice, Dice> now =
make_tuple(Dice(vector<ll>{goa.first, goa.second, center, 7 - goa.second,
7 - center, 7 - goa.first}),
Dice(vector<ll>{goa.first, 7 - goa.second, center, goa.second,
7 - center, 7 - goa.first}),
Dice(vector<ll>{7 - goa.first, goa.second, center,
7 - goa.second, 7 - center, goa.first}),
Dice(vector<ll>{7 - goa.first, 7 - goa.second, center,
goa.second, 7 - center, goa.first}));
return now;
}
template <typename A, typename B> class Dijkstra {
public:
vector<vector<pair<int, A>>> vertexs;
B Cost_Function;
Dijkstra(int n, B cost) : Cost_Function(cost) {
vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
}
~Dijkstra() { vertexs.clear(); }
void add_edge(int a, int b, A c) { vertexs[a].push_back(mp(b, c)); }
vector<ll> build_result(int StartPoint) {
vector<ll> dist(vertexs.size(), 2e18);
dist[StartPoint] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
next;
next.push(make_pair(0, StartPoint));
while (next.empty() == false) {
pair<ll, int> now = next.top();
next.pop();
if (dist[now.second] != now.first)
continue;
for (auto x : vertexs[now.second]) {
ll now_cost = now.first + Cost_Function(x.second);
if (dist[x.first] > now_cost) {
dist[x.first] = now_cost;
next.push(mp(now_cost, x.first));
}
}
}
return dist;
}
};
class Dinic {
public:
struct edge {
int to;
int cap;
int rev;
};
vector<vector<edge>> Graph;
vector<int> level;
vector<int> itr;
Dinic(int n) { Graph = vector<vector<edge>>(n, vector<edge>()); }
void add_edge(int a, int b, int cap) {
Graph[a].push_back(edge{b, cap, (int)Graph[b].size()});
Graph[b].push_back(edge{a, 0, (int)Graph[a].size() - 1});
}
void bfs(int s) {
level = vector<int>(Graph.size(), -1);
level[s] = 0;
queue<int> next;
next.push(s);
while (next.empty() == false) {
int now = next.front();
next.pop();
for (auto x : Graph[now]) {
if (x.cap == 0)
continue;
if (level[x.to] == -1) {
level[x.to] = level[now] + 1;
next.push(x.to);
}
}
}
}
int dfs(int now, int goal, int val) {
if (goal == now)
return val;
for (int &i = itr[now]; i < (int)Graph[now].size(); ++i) {
edge &target = Graph[now][i];
if (target.cap > 0 && level[now] < level[target.to]) {
int d = dfs(target.to, goal, min(val, target.cap));
if (d > 0) {
target.cap -= d;
Graph[target.to][target.rev].cap += d;
return d;
}
}
}
return 0;
}
int run(int s, int t) {
int ans = 0;
int f = 0;
while (bfs(s), level[t] >= 0) {
itr = vector<int>(Graph.size(), 0);
while ((f = dfs(s, t, 1e9)) > 0) {
ans += f;
}
}
return ans;
}
};
class HLDecomposition {
public:
vector<vector<int>> vertexs;
vector<int> depth;
vector<int> backs;
vector<int> connections;
vector<int> zip, unzip;
HLDecomposition(int n) {
vertexs = vector<vector<int>>(n, vector<int>());
depth = vector<int>(n);
zip = vector<int>(n);
unzip = zip;
}
void add_edge(int a, int b) {
vertexs[a].push_back(b);
vertexs[b].push_back(a);
}
int depth_dfs(int now, int back) {
depth[now] = 0;
for (auto x : vertexs[now]) {
if (x == back)
continue;
depth[now] = max(depth[now], 1 + depth_dfs(x, now));
}
return depth[now];
}
void dfs(int now, int backing) {
zip[now] = backs.size();
unzip[backs.size()] = now;
backs.push_back(backing);
int now_max = -1;
int itr = -1;
for (auto x : vertexs[now]) {
if (depth[x] > depth[now])
continue;
if (now_max < depth[x]) {
now_max = depth[x];
itr = x;
}
}
if (itr == -1)
return;
connections.push_back(connections.back());
dfs(itr, backing);
for (auto x : vertexs[now]) {
if (depth[x] > depth[now])
continue;
if (x == itr)
continue;
connections.push_back(zip[now]);
dfs(x, backs.size());
}
return;
}
void build() {
depth_dfs(0, -1);
connections.push_back(-1);
dfs(0, -1);
}
vector<pair<int, int>> query(int a, int b) {
a = zip[a];
b = zip[b];
vector<pair<int, int>> ans;
while (backs[a] != backs[b]) {
if (a < b)
swap(a, b);
ans.push_back(mp(backs[a], a + 1));
a = connections[a];
}
if (a > b)
swap(a, b);
ans.push_back(mp(a, b + 1));
return ans;
}
int lca(int a, int b) {
a = zip[a];
b = zip[b];
while (backs[a] != backs[b]) {
if (a < b)
swap(a, b);
a = connections[a];
}
return unzip[min(a, b)];
}
};
// by ei1333
// https://ei1333.github.io/luzhiled/snippets/structure/segment-tree.html
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz + 1, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz, height;
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
const OperatorMonoid OM0)
: f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
height = 0;
while (sz < n)
sz <<= 1, height++;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) { data[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
inline void propagate(int k) {
if (lazy[k] != OM0) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
data[k] = reflect(k);
lazy[k] = OM0;
}
}
inline Monoid reflect(int k) {
return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
}
inline void recalc(int k) {
while (k >>= 1)
data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
}
inline void thrust(int k) {
for (int i = height; i > 0; i--)
propagate(k >> i);
}
void update(int a, int b, const OperatorMonoid &x) {
thrust(a += sz);
thrust(b += sz - 1);
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
lazy[l] = h(lazy[l], x), ++l;
if (r & 1)
--r, lazy[r] = h(lazy[r], x);
}
recalc(a);
recalc(b);
}
Monoid query(int a, int b) {
thrust(a += sz);
thrust(b += sz - 1);
Monoid L = M1, R = M1;
for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
L = f(L, reflect(l++));
if (r & 1)
R = f(reflect(--r), R);
}
return f(L, R);
}
Monoid operator[](const int &k) { return query(k, k + 1); }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
propagate(a);
Monoid nxt =
type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, reflect(1))))
return find_subtree(1, check, L, false);
return -1;
}
thrust(a + sz);
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, reflect(a));
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(reflect(1), R)))
return find_subtree(1, check, R, true);
return -1;
}
thrust(b + sz - 1);
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(reflect(--b), R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
class KMP {
public:
vector<ll> table;
vector<ll> Pattern;
KMP(vector<ll> a) { build(a); }
void build(vector<ll> a) {
Pattern = a;
table = vector<ll>(a.size() + 1, -1);
int j = -1;
for (int i = 0; i < a.size(); ++i) {
while (j >= 0 && Pattern[i] != Pattern[j]) {
j = table[j];
}
table[i + 1] = ++j;
}
return;
}
vector<ll> search(vector<ll> a) {
vector<ll> ans;
for (int i = 0, k = 0; i < a.size(); ++i) {
while (k >= 0 && a[i] != Pattern[k])
k = table[k];
++k;
if (k >= Pattern.size()) {
ans.push_back(i - Pattern.size() + 1);
k = table[k];
}
}
return ans;
}
};
unsigned long xor128() {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
void init() {
iostream::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
#define int ll
template <typename flow_t, typename cost_t> struct PrimalDual {
const cost_t INF;
struct edge {
int to;
flow_t cap;
cost_t cost;
int rev;
bool isrev;
};
vector<vector<edge>> graph;
vector<cost_t> potential, min_cost;
vector<int> prevv, preve;
PrimalDual(int V) : graph(V), INF(numeric_limits<cost_t>::max()) {}
void add_edge(int from, int to, flow_t cap, cost_t cost) {
graph[from].emplace_back(edge{to, cap, cost, (int)graph[to].size(), false});
graph[to].emplace_back(
edge{from, 0, -cost, (int)graph[from].size() - 1, true});
}
cost_t min_cost_flow(int s, int t, flow_t f) {
int V = (int)graph.size();
cost_t ret = 0;
using Pi = pair<cost_t, int>;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
potential.assign(V, 0);
preve.assign(V, -1);
prevv.assign(V, -1);
while (f > 0) {
min_cost.assign(V, INF);
que.emplace(0, s);
min_cost[s] = 0;
while (!que.empty()) {
Pi p = que.top();
que.pop();
if (min_cost[p.second] < p.first)
continue;
for (int i = 0; i < graph[p.second].size(); i++) {
edge &e = graph[p.second][i];
cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] -
potential[e.to];
if (e.cap > 0 && min_cost[e.to] > nextCost) {
min_cost[e.to] = nextCost;
prevv[e.to] = p.second, preve[e.to] = i;
que.emplace(min_cost[e.to], e.to);
}
}
}
if (min_cost[t] == INF)
return -1;
for (int v = 0; v < V; v++)
potential[v] += min_cost[v];
flow_t addflow = f;
for (int v = t; v != s; v = prevv[v]) {
addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
}
f -= addflow;
ret += addflow * potential[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = graph[prevv[v]][preve[v]];
e.cap -= addflow;
graph[v][e.rev].cap += addflow;
}
}
return ret;
}
void output() {
for (int i = 0; i < graph.size(); i++) {
for (auto &e : graph[i]) {
if (e.isrev)
continue;
auto &rev_e = graph[e.to][e.rev];
cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/"
<< rev_e.cap + e.cap << ")" << endl;
}
}
}
};
// base64 エンコード
bool encode_base64(const std::vector<unsigned char> &src, std::string &dst) {
const std::string table(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::string cdst;
for (std::size_t i = 0; i < src.size(); ++i) {
switch (i % 3) {
case 0:
cdst.push_back(table[(src[i] & 0xFC) >> 2]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x03) << 4]);
cdst.push_back('=');
cdst.push_back('=');
}
break;
case 1:
cdst.push_back(
table[((src[i - 1] & 0x03) << 4) | ((src[i + 0] & 0xF0) >> 4)]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x0F) << 2]);
cdst.push_back('=');
}
break;
case 2:
cdst.push_back(
table[((src[i - 1] & 0x0F) << 2) | ((src[i + 0] & 0xC0) >> 6)]);
cdst.push_back(table[src[i] & 0x3F]);
break;
}
}
dst.swap(cdst);
return true;
}
int ut[30000];
int uf(int now) {
if (now == ut[now])
return now;
return ut[now] = uf(ut[now]);
}
int um(int a, int b) {
a = uf(a);
b = uf(b);
if (a == b)
return 0;
ut[a] = b;
return 1;
}
int cnter[30000];
modint<MAX_MOD> dp[6000];
void solve() {
int n;
cin >> n;
vector<int> inputs;
REP(i, n) {
int a;
cin >> a;
a--;
a = max(a, -1LL);
inputs.push_back(a);
ut[i] = i;
}
int base_cnt = 0;
REP(i, n) {
if (inputs[i] == -1)
continue;
base_cnt += 1 - um(i, inputs[i]);
}
REP(i, n) { cnter[uf(i)]++; }
vector<int> Finds;
REP(i, n) {
if (inputs[i] == -1) {
Finds.push_back(cnter[uf(i)]);
}
}
REP(i, Finds.size()) {
for (int q = i; q >= 0; --q) {
dp[q + 1] += dp[q] * Finds[i];
}
dp[0] += Finds[i];
}
modint<MAX_MOD> ans = (n - base_cnt);
if (Finds.size() != 0)
ans *= pows<modint<MAX_MOD>>(n - 1, Finds.size());
modint<MAX_MOD> gekos = 1;
dp[0] -= Finds.size();
for (int i = 0; i < Finds.size(); ++i) {
modint<MAX_MOD> hoge = dp[i] * gekos;
if (Finds.size() - i - 1 != 0) {
hoge *= pows<modint<MAX_MOD>>(n - 1, Finds.size() - i - 1);
}
ans -= hoge;
gekos *= (i + 1);
}
cout << ans << endl;
}
#undef int
int main() {
init();
solve();
}
| replace | 1,089 | 1,091 | 1,089 | 1,092 | 0 | |
p02666 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 5010, mod = 1e9 + 7;
ll dp[N][N];
ll fac[N];
vector<int> g[N];
ll t = 0;
ll vis[N];
ll dfs(ll v) {
vis[v] = 1;
t++;
for (auto u : g[v]) {
if (vis[u])
continue;
dfs(u);
}
}
ll a[N];
ll mol[N];
int32_t main() {
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
}
ll power = 1;
ll n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] != -1) {
g[i].pb(a[i]);
g[a[i]].pb(i);
}
}
ll p2 = 1;
for (int i = 1; i <= n; i++) {
if (a[i] != -1)
continue;
t = 0;
dfs(i);
mol[p2++] = t;
}
p2--;
for (int i = 1; i <= p2; i++) {
// cout << mol[i] << endl;
power *= n - 1;
power %= mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
t = 0;
dfs(i);
ans += power;
}
}
// cout << ans << " " << mol[1] << endl;
dp[0][0] = 1;
for (int i = 1; i <= p2; i++) {
for (int j = 1; j <= i; j++) {
dp[i][j] =
dp[i - 1][j] * (n - 1) % mod + dp[i - 1][j - 1] * (mol[i]) % mod;
dp[i][j] %= mod;
}
dp[i][0] = dp[i - 1][0] * (n - 1);
dp[i][0] %= mod;
}
for (int i = 2; i <= p2; i++) {
ans += dp[p2][i] * fac[i - 1] % mod;
// cout << i << " " << dp[p2][i]*fac[i-1]%mod << endl;
ans %= mod;
}
ll powe = 1;
for (int i = 1; i < p2; i++) {
powe *= n - 1;
powe %= mod;
}
for (int i = 1; i <= p2; i++) {
if (mol[i] > 1) {
ans += powe * (mol[i] - 1);
ans %= mod;
}
}
ll z = power * (n);
z %= mod;
z -= ans;
z += mod;
cout << z % mod << endl;
}
| #include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 5010, mod = 1e9 + 7;
ll dp[N][N];
ll fac[N];
vector<int> g[N];
ll t = 0;
ll vis[N];
void dfs(ll v) {
vis[v] = 1;
t++;
for (auto u : g[v]) {
if (vis[u])
continue;
dfs(u);
}
}
ll a[N];
ll mol[N];
int32_t main() {
fac[0] = 1;
for (int i = 1; i < N; i++) {
fac[i] = fac[i - 1] * i % mod;
}
ll power = 1;
ll n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] != -1) {
g[i].pb(a[i]);
g[a[i]].pb(i);
}
}
ll p2 = 1;
for (int i = 1; i <= n; i++) {
if (a[i] != -1)
continue;
t = 0;
dfs(i);
mol[p2++] = t;
}
p2--;
for (int i = 1; i <= p2; i++) {
// cout << mol[i] << endl;
power *= n - 1;
power %= mod;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
t = 0;
dfs(i);
ans += power;
}
}
// cout << ans << " " << mol[1] << endl;
dp[0][0] = 1;
for (int i = 1; i <= p2; i++) {
for (int j = 1; j <= i; j++) {
dp[i][j] =
dp[i - 1][j] * (n - 1) % mod + dp[i - 1][j - 1] * (mol[i]) % mod;
dp[i][j] %= mod;
}
dp[i][0] = dp[i - 1][0] * (n - 1);
dp[i][0] %= mod;
}
for (int i = 2; i <= p2; i++) {
ans += dp[p2][i] * fac[i - 1] % mod;
// cout << i << " " << dp[p2][i]*fac[i-1]%mod << endl;
ans %= mod;
}
ll powe = 1;
for (int i = 1; i < p2; i++) {
powe *= n - 1;
powe %= mod;
}
for (int i = 1; i <= p2; i++) {
if (mol[i] > 1) {
ans += powe * (mol[i] - 1);
ans %= mod;
}
}
ll z = power * (n);
z %= mod;
z -= ans;
z += mod;
cout << z % mod << endl;
}
| replace | 22 | 23 | 22 | 23 | -11 | |
p02666 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
ll p[5005], tr[5005], fac[5005], par[5005], dp[5005][5005],
hell = pow(10, 9) + 7;
ll find(ll i) {
if (par[i] == i)
return i;
return par[i] = find(par[i]);
}
void merge(ll a, ll b) {
a = par[a], b = par[b];
if (a == b)
return;
par[b] = a;
}
void solve() {
ll n, res = 0, k = 0;
cin >> n;
tr[0] = 1;
for (ll i = 1; i <= n; i++) {
par[i] = i;
tr[i] = ((n - 1) * tr[i - 1]) % hell;
}
for (ll i = 1; i <= n; i++) {
cin >> p[i];
if (p[i] == (-1)) {
k++;
continue;
}
merge(i, p[i]);
}
vector<vector<ll>> g(5005);
for (ll i = 1; i <= n; i++)
g[find(i)].push_back(i);
vector<ll> v;
for (ll i = 1; i <= n; i++)
if (!g[i].empty()) {
ll f = 0;
for (ll j = 0; j < ll(g[i].size()); j++)
if (p[g[i][j]] == (-1))
f = 1;
if (!f)
res = (res - tr[k]) % hell;
else
v.push_back(ll(g[i].size()));
}
ll m = ll(v.size());
for (ll i = 0; i <= m; i++)
dp[i][0] = 1;
for (ll i = 1; i <= m; i++)
for (ll j = 1; j <= k; j++)
dp[i][j] = (dp[i - 1][j] + v[i - 1] * dp[i - 1][j - 1]) % hell;
for (ll i = 2; i <= k; i++) {
ll val = (dp[m][i] * fac[i - 1]) % hell;
val *= tr[k - i];
val %= hell;
res = (res - val) % hell;
}
for (ll i = 0; i < m; i++)
res = (res - (((v[i] - 1) * tr[k - 1]) % hell)) % hell;
res += (n * tr[k]) % hell;
res %= hell;
if (res < 0)
res += hell;
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fac[0] = 1;
for (ll i = 1; i < 5005; i++)
fac[i] = (i * fac[i - 1]) % hell;
ll qc = 1;
for (ll i = 1; i <= qc; i++)
solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
ll p[5005], tr[5005], fac[5005], par[5005], dp[5005][5005],
hell = pow(10, 9) + 7;
ll find(ll i) {
if (par[i] == i)
return i;
return par[i] = find(par[i]);
}
void merge(ll a, ll b) {
a = find(a), b = find(b);
if (a == b)
return;
par[b] = a;
}
void solve() {
ll n, res = 0, k = 0;
cin >> n;
tr[0] = 1;
for (ll i = 1; i <= n; i++) {
par[i] = i;
tr[i] = ((n - 1) * tr[i - 1]) % hell;
}
for (ll i = 1; i <= n; i++) {
cin >> p[i];
if (p[i] == (-1)) {
k++;
continue;
}
merge(i, p[i]);
}
vector<vector<ll>> g(5005);
for (ll i = 1; i <= n; i++)
g[find(i)].push_back(i);
vector<ll> v;
for (ll i = 1; i <= n; i++)
if (!g[i].empty()) {
ll f = 0;
for (ll j = 0; j < ll(g[i].size()); j++)
if (p[g[i][j]] == (-1))
f = 1;
if (!f)
res = (res - tr[k]) % hell;
else
v.push_back(ll(g[i].size()));
}
ll m = ll(v.size());
for (ll i = 0; i <= m; i++)
dp[i][0] = 1;
for (ll i = 1; i <= m; i++)
for (ll j = 1; j <= k; j++)
dp[i][j] = (dp[i - 1][j] + v[i - 1] * dp[i - 1][j - 1]) % hell;
for (ll i = 2; i <= k; i++) {
ll val = (dp[m][i] * fac[i - 1]) % hell;
val *= tr[k - i];
val %= hell;
res = (res - val) % hell;
}
for (ll i = 0; i < m; i++)
res = (res - (((v[i] - 1) * tr[k - 1]) % hell)) % hell;
res += (n * tr[k]) % hell;
res %= hell;
if (res < 0)
res += hell;
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fac[0] = 1;
for (ll i = 1; i < 5005; i++)
fac[i] = (i * fac[i - 1]) % hell;
ll qc = 1;
for (ll i = 1; i <= qc; i++)
solve();
}
| replace | 13 | 14 | 13 | 14 | -11 | |
p02667 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
typedef long long LL;
const int MN = 100005;
int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;
int main() {
scanf("%s", T + 1), N = strlen(T + 1);
for (int i = 1; i <= N; ++i)
++(T[i] & 1 ? C1 : C0);
Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
T[0] = T[N + 1] = '0';
for (int i = 0; i <= N + 1; ++i) {
int x = T[i] - '0';
if (!x) {
if (tp && !stk[tp])
++cnt[tp];
else
stk[++tp] = 0, cnt[tp] = 1;
} else {
if (tp && stk[tp])
--tp, Ans += C0;
else
stk[++tp] = 1, cnt[tp] = 1;
}
}
--cnt[1], --cnt[tp];
for (int i = 1, s = 0; 2 * i <= tp; ++i)
s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
printf("%lld\n", Ans);
return 0;
} | #include <cstdio>
#include <cstring>
typedef long long LL;
const int MN = 200005;
int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;
int main() {
scanf("%s", T + 1), N = strlen(T + 1);
for (int i = 1; i <= N; ++i)
++(T[i] & 1 ? C1 : C0);
Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
T[0] = T[N + 1] = '0';
for (int i = 0; i <= N + 1; ++i) {
int x = T[i] - '0';
if (!x) {
if (tp && !stk[tp])
++cnt[tp];
else
stk[++tp] = 0, cnt[tp] = 1;
} else {
if (tp && stk[tp])
--tp, Ans += C0;
else
stk[++tp] = 1, cnt[tp] = 1;
}
}
--cnt[1], --cnt[tp];
for (int i = 1, s = 0; 2 * i <= tp; ++i)
s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
printf("%lld\n", Ans);
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02667 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rrep(i, m, n) for (ll(i) = (ll)(m); (i) >= (ll)(n); (i)--)
#define rep(i, m, n) for (ll(i) = (ll)(m); i < (ll)(n); i++)
#define REP(i, n) rep(i, 0, n)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
constexpr long double m_pi = 3.1415926535897932L;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 61;
constexpr long double EPS = 1e-10;
template <typename T> using vector2 = vector<vector<T>>;
template <typename T> using vector3 = vector<vector2<T>>;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
string operator*(const string &s, int k) {
if (k == 0)
return "";
string p = (s + s) * (k / 2);
if (k % 2 == 1)
p += s;
return p;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge { // グラフ
int to, rev;
ll cap;
Edge(int _to, ll _cap, int _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, int from, int to, ll cap, bool revFlag,
ll revCap) { // 最大フロー求める Ford-fulkerson
G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to)));
if (revFlag)
G[to].push_back(Edge(
from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする
}
ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
Edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
// 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ
ll max_flow(Graph &G, ll s, ll t) // O(V(V+E))
{
ll flow = 0;
for (;;) {
vector<bool> used(G.size());
REP(i, used.size()) used[i] = false;
ll f = max_flow_dfs(G, s, t, INF, used);
if (f == 0) {
return flow;
}
flow += f;
}
}
void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|)
d.resize(G.size());
negative.resize(G.size());
REP(i, d.size()) d[i] = INF;
REP(i, d.size()) negative[i] = false;
d[s] = 0;
REP(k, G.size() - 1) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
}
}
}
}
REP(k, G.size() - 1) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
negative[G[i][j].to] = true;
}
if (negative[i] == true)
negative[G[i][j].to] = true;
}
}
}
}
void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|)
d.resize(G.size());
REP(i, d.size()) d[i] = INF;
d[s] = 0;
priority_queue<P, vector<P>, greater<P>> q;
q.push(make_pair(0, s));
while (!q.empty()) {
P a = q.top();
q.pop();
if (d[a.second] < a.first)
continue;
REP(i, G[a.second].size()) {
Edge e = G[a.second][i];
if (d[e.to] > d[a.second] + e.cap) {
d[e.to] = d[a.second] + e.cap;
q.push(make_pair(d[e.to], e.to));
}
}
}
}
void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3)
d.resize(G.size());
REP(i, d.size()) d[i].resize(G.size());
REP(i, d.size()) {
REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); }
}
REP(i, G.size()) {
REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); }
}
REP(i, G.size()) {
REP(j, G.size()) {
REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); }
}
}
}
bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V)
int n = graph.size(), k = 0;
Array in(n);
for (auto &es : graph)
for (auto &e : es)
in[e.to]++;
priority_queue<ll, Array, greater<ll>> que;
REP(i, n)
if (in[i] == 0)
que.push(i);
while (que.size()) {
int v = que.top();
que.pop();
order.push_back(v);
for (auto &e : graph[v])
if (--in[e.to] == 0)
que.push(e.to);
}
if (order.size() != n)
return false;
else
return true;
}
class Lca {
public:
const int n = 0;
const int log2_n = 0;
std::vector<std::vector<int>> parent;
std::vector<int> depth;
Lca() {}
Lca(const Graph &g, int root)
: n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)),
depth(n) {
dfs(g, root, -1, 0);
for (int k = 0; k + 1 < log2_n; k++) {
for (int v = 0; v < (int)g.size(); v++) {
if (parent[k][v] < 0)
parent[k + 1][v] = -1;
else
parent[k + 1][v] = parent[k][parent[k][v]];
}
}
}
void dfs(const Graph &g, int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (auto &e : g[v]) {
if (e.to != p)
dfs(g, e.to, v, d + 1);
}
}
int get(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
for (int k = 0; k < log2_n; k++) {
if ((depth[v] - depth[u]) >> k & 1) {
v = parent[k][v];
}
}
if (u == v)
return u;
for (int k = log2_n - 1; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
};
class UnionFind {
vector<int> data;
ll num;
public:
UnionFind(int size) : data(size, -1), num(size) {}
bool unite(int x, int y) { // xとyの集合を統合する
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
num -= (x != y);
return x != y;
}
bool findSet(int x, int y) { // xとyが同じ集合か返す
return root(x) == root(y);
}
int root(int x) { // xのルートを返す
return data[x] < 0 ? x : data[x] = root(data[x]);
}
ll size(int x) { // xの集合のサイズを返す
return -data[root(x)];
}
ll numSet() { // 集合の数を返す
return num;
}
};
template <typename T, typename F> class SegmentTree {
private:
T identity;
F merge;
ll n;
vector<T> dat;
public:
SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) {
int _n = v.size();
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, identity);
REP(i, _n) dat[n + i - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
}
SegmentTree(F f, T id, int _n) : merge(f), identity(id) {
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, identity);
}
void set_val(int i, T x) {
i += n - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
T query(int l, int r) {
T left = identity, right = identity;
l += n - 1;
r += n - 1;
while (l < r) {
if ((l & 1) == 0)
left = merge(left, dat[l]);
if ((r & 1) == 0)
right = merge(dat[r - 1], right);
l = l / 2;
r = (r - 1) / 2;
}
return merge(left, right);
}
};
template <typename T> class FenwickTree {
vector<T> data;
int n;
int p;
public:
FenwickTree(int n) : n(n) {
data.resize(n + 1LL, 0);
p = 1;
while (p < data.size())
p *= 2;
}
T sum(int k) {
T ret = 0;
for (; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
T sum(int a, int b) { return sum(b) - sum(a); } //[a,b)
void add(int k, T x) {
for (++k; k <= n; k += k & -k)
data[k] += x;
}
int lower_bound(ll w) {
if (w <= 0)
return -1;
int x = 0;
for (int k = p / 2; k > 0; k /= 2) {
if (x + k <= n && data[x + k] < w)
w -= data[x + k], x += k;
}
return x;
}
};
// 約数求める //約数
void divisor(ll n, vector<ll> &ret) {
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
}
void prime_factorization(ll n, vector<P> &ret) {
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back({i, 0});
while (n % i == 0) {
n /= i;
ret[ret.size() - 1].second++;
}
}
}
if (n != 1)
ret.push_back({n, 1});
}
inline ll mod_pow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
inline ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); }
class Combination {
public:
Array fact;
Array fact_inv;
ll mod;
// if n >= mod use lucas
ll nCr(ll n, ll r) {
if (n < r)
return 0;
if (n < mod)
return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod;
ll ret = 1;
while (n || r) {
ll _n = n % mod, _r = r % mod;
n /= mod;
r /= mod;
(ret *= nCr(_n, _r)) %= mod;
}
return ret;
}
ll nPr(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % mod; }
ll nHr(ll n, ll r) { return nCr(r + n - 1, r); }
Combination(ll _n, ll _mod) {
mod = _mod;
ll n = min(_n + 1, mod);
fact.resize(n);
fact[0] = 1;
REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; }
fact_inv.resize(n);
fact_inv[n - 1] = mod_inv(fact[n - 1], mod);
for (int i = n - 1; i > 0; i--) {
fact_inv[i - 1] = fact_inv[i] * i % mod;
}
}
};
ll popcount(ll x) {
x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F);
x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF);
x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF);
x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF);
return x;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
string s;
cin >> s;
ll n = s.size();
ll ans = 0;
vector<P> a;
ll cnt = 1;
rep(i, 1, n) {
if (s[i] != s[i - 1])
a.push_back({s[i - 1] - '0', cnt}), cnt = 1;
else
cnt++;
}
a.push_back({s.back() - '0', cnt});
ll x = 0, y = 0;
for (auto i : a) {
if (i.first == 1)
x += i.second;
else
y += i.second;
}
if (x == 0) {
cout << 0 << "\n";
return 0;
}
ans += ((x + 1) / 2) * ((x + 1) / 2) + (x % 2 == 0 ? x / 2 : 0);
REP(i, a.size()) {
if (a[i].first == 1) {
ans += y * (a[i].second / 2);
a[i].second %= 2;
}
}
vector<P> b;
for (auto i : a) {
if (i.second == 0)
continue;
if (b.size() == 0 || b.back().first != i.first)
b.push_back(i);
else
b.back().second += i.second;
}
swap(a, b);
ll pre = 0, suf = 0;
if (a[0].first == 0)
pre = a[0].second, a.erase(a.begin());
if (a.size() == 0) {
cout << ans << "\n";
return 0;
}
if (a.back().first == 0)
suf = a.back().second, a.pop_back();
Array c;
for (auto i : a)
if (i.first == 0)
c.push_back(i.second);
REP(i, c.size()) ans += i + 1 + (c.size() - i + 1) / 2, c[i]--;
ans += (c.size() + 1) * suf;
ll r = 0, odd = 0;
ll m = c.size() + 1;
if (c.size())
reverse(all(c));
REP(i, c.size()) {
r++;
odd++;
REP(j, c[i]) {
odd = r - odd;
ans += m - r + odd;
}
}
r++;
odd++;
REP(i, pre) {
odd = r - odd;
ans += odd;
}
cout << ans << "\n";
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rrep(i, m, n) for (ll(i) = (ll)(m); (i) >= (ll)(n); (i)--)
#define rep(i, m, n) for (ll(i) = (ll)(m); i < (ll)(n); i++)
#define REP(i, n) rep(i, 0, n)
#define FOR(i, c) \
for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
constexpr long double m_pi = 3.1415926535897932L;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 61;
constexpr long double EPS = 1e-10;
template <typename T> using vector2 = vector<vector<T>>;
template <typename T> using vector3 = vector<vector2<T>>;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
string operator*(const string &s, int k) {
if (k == 0)
return "";
string p = (s + s) * (k / 2);
if (k % 2 == 1)
p += s;
return p;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge { // グラフ
int to, rev;
ll cap;
Edge(int _to, ll _cap, int _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, int from, int to, ll cap, bool revFlag,
ll revCap) { // 最大フロー求める Ford-fulkerson
G[from].push_back(Edge(to, cap, (ll)G[to].size() + (from == to)));
if (revFlag)
G[to].push_back(Edge(
from, revCap, (ll)G[from].size() - 1)); // 最小カットの場合逆辺は0にする
}
ll max_flow_dfs(Graph &G, ll v, ll t, ll f, vector<bool> &used) {
if (v == t)
return f;
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
Edge &e = G[v][i];
if (!used[e.to] && e.cap > 0) {
ll d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
if (d > 0) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
// 二分グラフの最大マッチングを求めたりも出来る また二部グラフの最大独立集合は頂点数-最大マッチングのサイズ
ll max_flow(Graph &G, ll s, ll t) // O(V(V+E))
{
ll flow = 0;
for (;;) {
vector<bool> used(G.size());
REP(i, used.size()) used[i] = false;
ll f = max_flow_dfs(G, s, t, INF, used);
if (f == 0) {
return flow;
}
flow += f;
}
}
void BellmanFord(Graph &G, ll s, Array &d, Array &negative) { // O(|E||V|)
d.resize(G.size());
negative.resize(G.size());
REP(i, d.size()) d[i] = INF;
REP(i, d.size()) negative[i] = false;
d[s] = 0;
REP(k, G.size() - 1) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
}
}
}
}
REP(k, G.size() - 1) {
REP(i, G.size()) {
REP(j, G[i].size()) {
if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) {
d[G[i][j].to] = d[i] + G[i][j].cap;
negative[G[i][j].to] = true;
}
if (negative[i] == true)
negative[G[i][j].to] = true;
}
}
}
}
void Dijkstra(Graph &G, ll s, Array &d) { // O(|E|log|V|)
d.resize(G.size());
REP(i, d.size()) d[i] = INF;
d[s] = 0;
priority_queue<P, vector<P>, greater<P>> q;
q.push(make_pair(0, s));
while (!q.empty()) {
P a = q.top();
q.pop();
if (d[a.second] < a.first)
continue;
REP(i, G[a.second].size()) {
Edge e = G[a.second][i];
if (d[e.to] > d[a.second] + e.cap) {
d[e.to] = d[a.second] + e.cap;
q.push(make_pair(d[e.to], e.to));
}
}
}
}
void WarshallFloyd(Graph &G, Matrix &d) { // O(V^3)
d.resize(G.size());
REP(i, d.size()) d[i].resize(G.size());
REP(i, d.size()) {
REP(j, d[i].size()) { d[i][j] = ((i != j) ? INF : 0); }
}
REP(i, G.size()) {
REP(j, G[i].size()) { chmin(d[i][G[i][j].to], G[i][j].cap); }
}
REP(i, G.size()) {
REP(j, G.size()) {
REP(k, G.size()) { chmin(d[j][k], d[j][i] + d[i][k]); }
}
}
}
bool tsort(Graph &graph, Array &order) { // トポロジカルソートO(E+V)
int n = graph.size(), k = 0;
Array in(n);
for (auto &es : graph)
for (auto &e : es)
in[e.to]++;
priority_queue<ll, Array, greater<ll>> que;
REP(i, n)
if (in[i] == 0)
que.push(i);
while (que.size()) {
int v = que.top();
que.pop();
order.push_back(v);
for (auto &e : graph[v])
if (--in[e.to] == 0)
que.push(e.to);
}
if (order.size() != n)
return false;
else
return true;
}
class Lca {
public:
const int n = 0;
const int log2_n = 0;
std::vector<std::vector<int>> parent;
std::vector<int> depth;
Lca() {}
Lca(const Graph &g, int root)
: n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)),
depth(n) {
dfs(g, root, -1, 0);
for (int k = 0; k + 1 < log2_n; k++) {
for (int v = 0; v < (int)g.size(); v++) {
if (parent[k][v] < 0)
parent[k + 1][v] = -1;
else
parent[k + 1][v] = parent[k][parent[k][v]];
}
}
}
void dfs(const Graph &g, int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (auto &e : g[v]) {
if (e.to != p)
dfs(g, e.to, v, d + 1);
}
}
int get(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
for (int k = 0; k < log2_n; k++) {
if ((depth[v] - depth[u]) >> k & 1) {
v = parent[k][v];
}
}
if (u == v)
return u;
for (int k = log2_n - 1; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
};
class UnionFind {
vector<int> data;
ll num;
public:
UnionFind(int size) : data(size, -1), num(size) {}
bool unite(int x, int y) { // xとyの集合を統合する
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y);
data[x] += data[y];
data[y] = x;
}
num -= (x != y);
return x != y;
}
bool findSet(int x, int y) { // xとyが同じ集合か返す
return root(x) == root(y);
}
int root(int x) { // xのルートを返す
return data[x] < 0 ? x : data[x] = root(data[x]);
}
ll size(int x) { // xの集合のサイズを返す
return -data[root(x)];
}
ll numSet() { // 集合の数を返す
return num;
}
};
template <typename T, typename F> class SegmentTree {
private:
T identity;
F merge;
ll n;
vector<T> dat;
public:
SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) {
int _n = v.size();
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, identity);
REP(i, _n) dat[n + i - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
}
SegmentTree(F f, T id, int _n) : merge(f), identity(id) {
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, identity);
}
void set_val(int i, T x) {
i += n - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
T query(int l, int r) {
T left = identity, right = identity;
l += n - 1;
r += n - 1;
while (l < r) {
if ((l & 1) == 0)
left = merge(left, dat[l]);
if ((r & 1) == 0)
right = merge(dat[r - 1], right);
l = l / 2;
r = (r - 1) / 2;
}
return merge(left, right);
}
};
template <typename T> class FenwickTree {
vector<T> data;
int n;
int p;
public:
FenwickTree(int n) : n(n) {
data.resize(n + 1LL, 0);
p = 1;
while (p < data.size())
p *= 2;
}
T sum(int k) {
T ret = 0;
for (; k > 0; k -= k & -k)
ret += data[k];
return (ret);
}
T sum(int a, int b) { return sum(b) - sum(a); } //[a,b)
void add(int k, T x) {
for (++k; k <= n; k += k & -k)
data[k] += x;
}
int lower_bound(ll w) {
if (w <= 0)
return -1;
int x = 0;
for (int k = p / 2; k > 0; k /= 2) {
if (x + k <= n && data[x + k] < w)
w -= data[x + k], x += k;
}
return x;
}
};
// 約数求める //約数
void divisor(ll n, vector<ll> &ret) {
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
}
void prime_factorization(ll n, vector<P> &ret) {
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back({i, 0});
while (n % i == 0) {
n /= i;
ret[ret.size() - 1].second++;
}
}
}
if (n != 1)
ret.push_back({n, 1});
}
inline ll mod_pow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
inline ll mod_inv(ll x, ll mod) { return mod_pow(x, mod - 2, mod); }
class Combination {
public:
Array fact;
Array fact_inv;
ll mod;
// if n >= mod use lucas
ll nCr(ll n, ll r) {
if (n < r)
return 0;
if (n < mod)
return ((fact[n] * fact_inv[r] % mod) * fact_inv[n - r]) % mod;
ll ret = 1;
while (n || r) {
ll _n = n % mod, _r = r % mod;
n /= mod;
r /= mod;
(ret *= nCr(_n, _r)) %= mod;
}
return ret;
}
ll nPr(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % mod; }
ll nHr(ll n, ll r) { return nCr(r + n - 1, r); }
Combination(ll _n, ll _mod) {
mod = _mod;
ll n = min(_n + 1, mod);
fact.resize(n);
fact[0] = 1;
REP(i, n - 1) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; }
fact_inv.resize(n);
fact_inv[n - 1] = mod_inv(fact[n - 1], mod);
for (int i = n - 1; i > 0; i--) {
fact_inv[i - 1] = fact_inv[i] * i % mod;
}
}
};
ll popcount(ll x) {
x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F);
x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF);
x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF);
x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF);
return x;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
string s;
cin >> s;
ll n = s.size();
ll ans = 0;
vector<P> a;
ll cnt = 1;
rep(i, 1, n) {
if (s[i] != s[i - 1])
a.push_back({s[i - 1] - '0', cnt}), cnt = 1;
else
cnt++;
}
a.push_back({s.back() - '0', cnt});
ll x = 0, y = 0;
for (auto i : a) {
if (i.first == 1)
x += i.second;
else
y += i.second;
}
if (x == 0) {
cout << 0 << "\n";
return 0;
}
ans += ((x + 1) / 2) * ((x + 1) / 2) + (x % 2 == 0 ? x / 2 : 0);
REP(i, a.size()) {
if (a[i].first == 1) {
ans += y * (a[i].second / 2);
a[i].second %= 2;
}
}
vector<P> b;
for (auto i : a) {
if (i.second == 0)
continue;
if (b.size() == 0 || b.back().first != i.first)
b.push_back(i);
else
b.back().second += i.second;
}
swap(a, b);
ll pre = 0, suf = 0;
if (a.size() == 0) {
cout << ans << "\n";
return 0;
}
if (a[0].first == 0)
pre = a[0].second, a.erase(a.begin());
if (a.size() == 0) {
cout << ans << "\n";
return 0;
}
if (a.back().first == 0)
suf = a.back().second, a.pop_back();
Array c;
for (auto i : a)
if (i.first == 0)
c.push_back(i.second);
REP(i, c.size()) ans += i + 1 + (c.size() - i + 1) / 2, c[i]--;
ans += (c.size() + 1) * suf;
ll r = 0, odd = 0;
ll m = c.size() + 1;
if (c.size())
reverse(all(c));
REP(i, c.size()) {
r++;
odd++;
REP(j, c[i]) {
odd = r - odd;
ans += m - r + odd;
}
}
r++;
odd++;
REP(i, pre) {
odd = r - odd;
ans += odd;
}
cout << ans << "\n";
return 0;
} | insert | 505 | 505 | 505 | 509 | 0 | |
p02667 | C++ | Runtime Error | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << " "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 200005;
int n;
char s[N];
int a[N];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int cnt1 = 0;
For(i, 1, n) {
a[i] = s[i] == '1';
cnt1 += a[i];
}
if (cnt1 == 0)
return puts("0"), 0;
LL ans = (LL)cnt1 / 2 * (cnt1 / 2 + 1);
if (cnt1 & 1)
ans += (cnt1 + 1) / 2;
int cnt0 = n - cnt1;
// outval(ans);outval(cnt0);
vi v;
int c0 = 0, c1 = 0;
int p0 = 0, p1 = 0;
For(i, 1, n) if (a[i] == 0) {
if (c1) {
v.pb(c0), c1 = c0 = 0;
if ((i - 1) & 1)
p1++;
else
p0++;
}
c0++;
}
else {
if (c1)
ans += cnt0;
c1 ^= 1;
}
v.pb(c0);
if (c1) {
v.pb(0);
if (n & 1)
p1++;
else
p0++;
}
if (v.size() == 1)
return printf("%lld\n", ans), 0;
// outval(ans);
// outval(p0),outval(p1);
// outarr(v,0,(int)v.size()-1);
while (v[0] > 0) {
ans += p1;
swap(p0, p1);
v[0]--;
}
// outval(ans);
For(i, 1, (int)v.size() - 1) {
p1--;
while (v[i] > 1) {
ans += i;
ans += p1;
swap(p0, p1);
v[i]--;
}
}
// outtag();
// outval(p0),outval(p1);
// outval(ans);
// outarr(v,0,(int)v.size()-1);
int flag = 0;
if (v.back() == 0) {
if ((int)v.size() == 2)
return printf("%lld\n", ans);
ans += (int)v.size() - 1;
v.pop_back();
flag = 1;
}
int vsz = (int)v.size() - 1;
For(i, 0, vsz - 1 - flag) ans += (vsz + (vsz - i) + 1) / 2;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << " "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
const int N = 200005;
int n;
char s[N];
int a[N];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int cnt1 = 0;
For(i, 1, n) {
a[i] = s[i] == '1';
cnt1 += a[i];
}
if (cnt1 == 0)
return puts("0"), 0;
LL ans = (LL)cnt1 / 2 * (cnt1 / 2 + 1);
if (cnt1 & 1)
ans += (cnt1 + 1) / 2;
int cnt0 = n - cnt1;
// outval(ans);outval(cnt0);
vi v;
int c0 = 0, c1 = 0;
int p0 = 0, p1 = 0;
For(i, 1, n) if (a[i] == 0) {
if (c1) {
v.pb(c0), c1 = c0 = 0;
if ((i - 1) & 1)
p1++;
else
p0++;
}
c0++;
}
else {
if (c1)
ans += cnt0;
c1 ^= 1;
}
v.pb(c0);
if (c1) {
v.pb(0);
if (n & 1)
p1++;
else
p0++;
}
if (v.size() == 1)
return printf("%lld\n", ans), 0;
// outval(ans);
// outval(p0),outval(p1);
// outarr(v,0,(int)v.size()-1);
while (v[0] > 0) {
ans += p1;
swap(p0, p1);
v[0]--;
}
// outval(ans);
For(i, 1, (int)v.size() - 1) {
p1--;
while (v[i] > 1) {
ans += i;
ans += p1;
swap(p0, p1);
v[i]--;
}
}
// outtag();
// outval(p0),outval(p1);
// outval(ans);
// outarr(v,0,(int)v.size()-1);
int flag = 0;
if (v.back() == 0) {
if ((int)v.size() == 2)
return printf("%lld\n", ans), 0;
ans += (int)v.size() - 1;
v.pop_back();
flag = 1;
}
int vsz = (int)v.size() - 1;
For(i, 0, vsz - 1 - flag) ans += (vsz + (vsz - i) + 1) / 2;
cout << ans << endl;
return 0;
} | replace | 105 | 106 | 105 | 106 | 2 | |
p02669 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
if (!x)
return LINF;
if (x == 1)
return d;
if (m[x])
return m[x];
ll res = LINF;
if (res / d > x - 10)
chmin(res, x * d);
for (int i = 0; i < 3; i++) {
chmin(res, f((x + v[i] - 1) / v[i] + w[i] + (v[i] - x % v[i]) * d));
chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
}
return m[x] = res;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> w[0] >> w[1] >> w[2] >> d;
m.clear();
cout << f(n) << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
if (!x)
return LINF;
if (x == 1)
return d;
if (m[x])
return m[x];
ll res = LINF;
if (res / d > x - 10)
chmin(res, x * d);
for (int i = 0; i < 3; i++) {
chmin(res, f((x + v[i] - 1) / v[i]) + w[i] + (v[i] - x % v[i]) * d);
chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
}
return m[x] = res;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> w[0] >> w[1] >> w[2] >> d;
m.clear();
cout << f(n) << endl;
}
}
| replace | 23 | 24 | 23 | 24 | -11 | |
p02669 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define mm(a, n) memset(a, n, sizeof(a))
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define il inline
#define int long long
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define pi 3.14159265358979323
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) \
cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define debug3(x, y, z) \
cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl;
#define debug4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl;
using namespace std;
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T> void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar()))
(ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T> void Out(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
Out(x / 10);
putchar(x % 10 + '0');
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow_mod(ll x, ll n, ll mod) {
ll res = 1;
while (n) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll fact_pow(ll n, ll p) {
ll res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
ll mult(ll a, ll b, ll p) {
a %= p;
b %= p;
ll r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p)
r -= p;
}
v <<= 1;
if (v > p)
v -= p;
b >>= 1;
}
return r;
}
ll quick_pow(ll a, ll b, ll p) {
ll r = 1, v = a % p;
while (b) {
if (b & 1)
r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(ll a, ll n, ll x, ll t) {
ll r = quick_pow(a, x, n);
ll z = r;
for (ll i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1)
return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(ll n) {
if (n < 2)
return false;
if (n == 2)
return true;
if (!(n & 1))
return false;
ll x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
ll o = 8;
for (ll i = 0; i < o; i++) {
ll a = rand() % (n - 1) + 1;
if (CH(a, n, x, t))
return false;
}
return true;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1, y = 0;
return;
}
exgcd(b, a % b, x, y);
ll t = x;
x = y, y = t - (a / b) * y;
}
ll INV(ll a, ll b) {
ll x, y;
return exgcd(a, b, x, y), (x % b + b) % b;
}
ll crt(ll x, ll p, ll mod) { return INV(p / mod, mod) * (p / mod) * x; }
ll FAC(ll x, ll a, ll b) {
if (!x)
return 1;
ll ans = 1;
for (ll i = 1; i <= b; i++)
if (i % a)
ans *= i, ans %= b;
ans = pow_mod(ans, x / b, b);
for (ll i = 1; i <= x % b; i++)
if (i % a)
ans *= i, ans %= b;
return ans * FAC(x / a, a, b) % b;
}
ll C(ll n, ll m, ll a, ll b) {
ll N = FAC(n, a, b), M = FAC(m, a, b), Z = FAC(n - m, a, b), sum = 0, i;
for (i = n; i; i = i / a)
sum += i / a;
for (i = m; i; i = i / a)
sum -= i / a;
for (i = n - m; i; i = i / a)
sum -= i / a;
return N * pow_mod(a, sum, b) % b * INV(M, b) % b * INV(Z, b) % b;
}
ll exlucas(ll n, ll m, ll p) {
ll t = p, ans = 0, i;
for (i = 2; i * i <= p; i++) {
ll k = 1;
while (t % i == 0) {
k *= i, t /= i;
}
ans += crt(C(n, m, i, k), p, k), ans %= p;
}
if (t > 1)
ans += crt(C(n, m, t, t), p, t), ans %= p;
return ans % p;
}
/*
int prime[100000],minprime[100000];
void euler(int n)
{int
c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++)
{minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}}
struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[100000];
il void push_up(ll rt)
{tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn);
tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);}
il void push_down(ll rt , ll length)
{if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy;
tree[rt<<1].sum+=(length-(length>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(length>>1)*tree[rt].lazy;
tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy;
tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}}
il void build(ll l , ll r , ll rt , ll *aa)
{tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r)
{tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;}
ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);}
il void update_range(ll L , ll R , ll key , ll rt)
{if(tree[rt].r<L||tree[rt].l>R)return;if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key;
tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1);
ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,rt << 1);
if(R>mid)update_range(L,R,key,rt << 1 | 1);push_up(rt);}
il ll query_range(ll L, ll R, ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,rt << 1);
if(R>mid)ans+=query_range(L,R,rt << 1 | 1);return ans;}
il ll query_min(ll L,ll R,ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll
ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,rt << 1));
if(R>mid)ans=min(ans,query_min(L,R,rt << 1 | 1));return ans;}
il ll query_max(ll L, ll R, ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll
ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,rt << 1));
if(R>mid)ans=max(ans,query_max(L,R,rt << 1 | 1));return ans;}
*/
const int N = 3e5 + 10;
struct Edge {
int nex, to;
} edge[N << 1];
int head[N], tot;
void add_edge(int u, int v) {
edge[++tot].nex = head[u];
edge[tot].to = v;
head[u] = tot;
}
unordered_map<int, int> dp;
int n, a, b, c, d;
int dfs(int n) {
if (!n)
return 0;
if (n == 1)
return d;
if (dp.count(n))
return dp[n];
int res = INF;
if (n < res / d)
res = n * d;
int x = n / 2;
res = min(res, (n - 2 * x) * d + a + dfs(x));
x = (n + 1) / 2;
res = min(res, abs(n - 2 * x) * d + a + dfs(x));
x = n / 3;
res = min(res, (n - 3 * x) * d + b + dfs(x));
x = (n + 2) / 3;
res = min(res, abs(n - 3 * x) * d + b + dfs(x));
x = n / 5;
res = min(res, (n - 5 * x) * d + c + dfs(x));
x = (n + 4) / 5;
res = min(res, abs(n - 5 * x) * d + c + dfs(x));
return dp[n] = res;
}
signed main() {
ios;
int t;
cin >> t;
while (t--) {
dp.clear();
read(n), read(a), read(b), read(c), read(d);
Out(dfs(n));
puts("");
}
return 0;
}
| #include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define mm(a, n) memset(a, n, sizeof(a))
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define il inline
#define int long long
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define pi 3.14159265358979323
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) \
cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define debug3(x, y, z) \
cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl;
#define debug4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl;
using namespace std;
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T> void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar()))
(ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T> void Out(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
Out(x / 10);
putchar(x % 10 + '0');
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow_mod(ll x, ll n, ll mod) {
ll res = 1;
while (n) {
if (n & 1)
res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll fact_pow(ll n, ll p) {
ll res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
ll mult(ll a, ll b, ll p) {
a %= p;
b %= p;
ll r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p)
r -= p;
}
v <<= 1;
if (v > p)
v -= p;
b >>= 1;
}
return r;
}
ll quick_pow(ll a, ll b, ll p) {
ll r = 1, v = a % p;
while (b) {
if (b & 1)
r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(ll a, ll n, ll x, ll t) {
ll r = quick_pow(a, x, n);
ll z = r;
for (ll i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1)
return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(ll n) {
if (n < 2)
return false;
if (n == 2)
return true;
if (!(n & 1))
return false;
ll x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
ll o = 8;
for (ll i = 0; i < o; i++) {
ll a = rand() % (n - 1) + 1;
if (CH(a, n, x, t))
return false;
}
return true;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1, y = 0;
return;
}
exgcd(b, a % b, x, y);
ll t = x;
x = y, y = t - (a / b) * y;
}
ll INV(ll a, ll b) {
ll x, y;
return exgcd(a, b, x, y), (x % b + b) % b;
}
ll crt(ll x, ll p, ll mod) { return INV(p / mod, mod) * (p / mod) * x; }
ll FAC(ll x, ll a, ll b) {
if (!x)
return 1;
ll ans = 1;
for (ll i = 1; i <= b; i++)
if (i % a)
ans *= i, ans %= b;
ans = pow_mod(ans, x / b, b);
for (ll i = 1; i <= x % b; i++)
if (i % a)
ans *= i, ans %= b;
return ans * FAC(x / a, a, b) % b;
}
ll C(ll n, ll m, ll a, ll b) {
ll N = FAC(n, a, b), M = FAC(m, a, b), Z = FAC(n - m, a, b), sum = 0, i;
for (i = n; i; i = i / a)
sum += i / a;
for (i = m; i; i = i / a)
sum -= i / a;
for (i = n - m; i; i = i / a)
sum -= i / a;
return N * pow_mod(a, sum, b) % b * INV(M, b) % b * INV(Z, b) % b;
}
ll exlucas(ll n, ll m, ll p) {
ll t = p, ans = 0, i;
for (i = 2; i * i <= p; i++) {
ll k = 1;
while (t % i == 0) {
k *= i, t /= i;
}
ans += crt(C(n, m, i, k), p, k), ans %= p;
}
if (t > 1)
ans += crt(C(n, m, t, t), p, t), ans %= p;
return ans % p;
}
/*
int prime[100000],minprime[100000];
void euler(int n)
{int
c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++)
{minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}}
struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[100000];
il void push_up(ll rt)
{tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn);
tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);}
il void push_down(ll rt , ll length)
{if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy;
tree[rt<<1].sum+=(length-(length>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(length>>1)*tree[rt].lazy;
tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy;
tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}}
il void build(ll l , ll r , ll rt , ll *aa)
{tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r)
{tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;}
ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);}
il void update_range(ll L , ll R , ll key , ll rt)
{if(tree[rt].r<L||tree[rt].l>R)return;if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key;
tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1);
ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,rt << 1);
if(R>mid)update_range(L,R,key,rt << 1 | 1);push_up(rt);}
il ll query_range(ll L, ll R, ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,rt << 1);
if(R>mid)ans+=query_range(L,R,rt << 1 | 1);return ans;}
il ll query_min(ll L,ll R,ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll
ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,rt << 1));
if(R>mid)ans=min(ans,query_min(L,R,rt << 1 | 1));return ans;}
il ll query_max(ll L, ll R, ll rt)
{if(L<=tree[rt].l&&R>=tree[rt].r){return
tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll
mid=(tree[rt].r+tree[rt].l)>>1;ll
ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,rt << 1));
if(R>mid)ans=max(ans,query_max(L,R,rt << 1 | 1));return ans;}
*/
const int N = 3e5 + 10;
struct Edge {
int nex, to;
} edge[N << 1];
int head[N], tot;
void add_edge(int u, int v) {
edge[++tot].nex = head[u];
edge[tot].to = v;
head[u] = tot;
}
unordered_map<int, int> dp;
int n, a, b, c, d;
int dfs(int n) {
if (!n)
return 0;
if (n == 1)
return d;
if (dp.count(n))
return dp[n];
int res = INF;
if (n < res / d)
res = n * d;
int x = n / 2;
res = min(res, (n - 2 * x) * d + a + dfs(x));
x = (n + 1) / 2;
res = min(res, abs(n - 2 * x) * d + a + dfs(x));
x = n / 3;
res = min(res, (n - 3 * x) * d + b + dfs(x));
x = (n + 2) / 3;
res = min(res, abs(n - 3 * x) * d + b + dfs(x));
x = n / 5;
res = min(res, (n - 5 * x) * d + c + dfs(x));
x = (n + 4) / 5;
res = min(res, abs(n - 5 * x) * d + c + dfs(x));
return dp[n] = res;
}
signed main() {
ios;
int t;
cin >> t;
while (t--) {
dp.clear();
cin >> n >> a >> b >> c >> d;
cout << dfs(n) << '\n';
}
return 0;
}
| replace | 271 | 274 | 271 | 273 | TLE | |
p02669 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
map<i64, i64> memo;
i64 a, b, c, d;
i64 dp(i64 n) {
if (n < 0)
return 1e18;
if (n == 0)
return 0;
if (memo.count(n))
return memo[n];
i64 &res = memo[n];
res = 1e18;
if (abs(n) < 2e9)
res = abs(n) * d;
fore(delta, -4, 4) {
i64 x = n + delta;
if (x % 2 == 0)
uin(res, dp(x / 2) + a + abs(delta) * d);
if (x % 3 == 0)
uin(res, dp(x / 3) + b + abs(delta) * d);
if (x % 5 == 0)
uin(res, dp(x / 5) + c + abs(delta) * d);
}
return memo[n] = res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
freopen("input.txt", "r", stdin);
int T;
cin >> T;
while (T--) {
i64 n;
cin >> n >> a >> b >> c >> d;
memo.clear();
cout << dp(n) << '\n';
}
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
map<i64, i64> memo;
i64 a, b, c, d;
i64 dp(i64 n) {
if (n < 0)
return 1e18;
if (n == 0)
return 0;
if (memo.count(n))
return memo[n];
i64 &res = memo[n];
res = 1e18;
if (abs(n) < 2e9)
res = abs(n) * d;
fore(delta, -4, 4) {
i64 x = n + delta;
if (x % 2 == 0)
uin(res, dp(x / 2) + a + abs(delta) * d);
if (x % 3 == 0)
uin(res, dp(x / 3) + b + abs(delta) * d);
if (x % 5 == 0)
uin(res, dp(x / 5) + c + abs(delta) * d);
}
return memo[n] = res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
// freopen("input.txt", "r", stdin);
int T;
cin >> T;
while (T--) {
i64 n;
cin >> n >> a >> b >> c >> d;
memo.clear();
cout << dp(n) << '\n';
}
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| replace | 64 | 65 | 64 | 65 | TLE | |
p02669 | C++ | Time Limit Exceeded | // #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
#define fi first
#define se second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
ll N, A, B, C, D;
map<ll, ll> MP;
ll Solve(ll num) {
if (num <= 0)
return 0;
if (num == 1)
return MP[num] = D;
if (MP[num] > 0)
return MP[num];
ll ans = INF;
for (ll i = -3; i <= 5; i++) {
ll now = num - i;
if (now < 0)
continue;
if (now % 2 == 0 && now / 2 < num) {
ans = min(ans, Solve(now / 2) + A + abs(i) * D);
ll Lim = (INF - Solve(now / 2)) / D;
ll deg = abs(now / 2 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 2) + deg * D);
}
}
if (now % 3 == 0 && now / 3 < num) {
ans = min(ans, Solve(now / 3) + B + abs(i) * D);
ll Lim = (INF - Solve(now / 3)) / D;
ll deg = abs(now / 3 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 3) + deg * D);
}
}
if (now % 5 == 0 && now / 5 < num) {
ans = min(ans, Solve(now / 5) + C + abs(i) * D);
ll Lim = (INF - Solve(now / 5)) / D;
ll deg = abs(now / 5 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 5) + deg * D);
}
}
if (MP[now] > 0)
ans = min(ans, MP[now] + abs(i) * D);
}
return MP[num] = ans;
}
int Ans() {
MP = {};
cin >> N >> A >> B >> C >> D;
MP[1] = D;
cout << Solve(N) << endl;
return 0;
}
int main() {
int T;
cin >> T;
for (int i = 0; i < T; i++)
Ans();
return 0;
}
| // #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
#define fi first
#define se second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
ll N, A, B, C, D;
map<ll, ll> MP;
ll Solve(ll num) {
if (num <= 0)
return 0;
if (num == 1)
return MP[num] = D;
if (MP[num] > 0)
return MP[num];
ll ans = INF;
for (ll i = -3; i < 5; i++) {
ll now = num - i;
if (now < 0)
continue;
if (now % 2 == 0 && now / 2 < num) {
ans = min(ans, Solve(now / 2) + A + abs(i) * D);
ll Lim = (INF - Solve(now / 2)) / D;
ll deg = abs(now / 2 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 2) + deg * D);
}
}
if (now % 3 == 0 && now / 3 < num) {
ans = min(ans, Solve(now / 3) + B + abs(i) * D);
ll Lim = (INF - Solve(now / 3)) / D;
ll deg = abs(now / 3 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 3) + deg * D);
}
}
if (now % 5 == 0 && now / 5 < num) {
ans = min(ans, Solve(now / 5) + C + abs(i) * D);
ll Lim = (INF - Solve(now / 5)) / D;
ll deg = abs(now / 5 - num);
if (deg <= Lim) {
ans = min(ans, Solve(now / 5) + deg * D);
}
}
if (MP[now] > 0)
ans = min(ans, MP[now] + abs(i) * D);
}
return MP[num] = ans;
}
int Ans() {
MP = {};
cin >> N >> A >> B >> C >> D;
MP[1] = D;
cout << Solve(N) << endl;
return 0;
}
int main() {
int T;
cin >> T;
for (int i = 0; i < T; i++)
Ans();
return 0;
}
| replace | 55 | 56 | 55 | 56 | TLE | |
p02669 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define int long long
const int N = 1e5 + 5;
int k, a, b, c, d;
unordered_map<int, int> cache;
int work(int n) {
if (n == 0)
return 0;
if (n == 1)
return d;
if (cache.count(n))
return cache[n];
int &ans = cache[n];
ans = 4e18;
if (n <= 4e9)
ans = min(ans, n * d);
for (int i = -15; i <= 15; i++) {
int nxtN = n + i;
if (nxtN < 0)
continue;
if (nxtN % 2 == 0 && nxtN / 2 < n)
ans = min(ans, d * abs(i) + a + work(nxtN / 2));
if (nxtN % 3 == 0 && nxtN / 3 < n)
ans = min(ans, d * abs(i) + b + work(nxtN / 3));
if (nxtN % 5 == 0 && nxtN / 5 < n)
ans = min(ans, d * abs(i) + c + work(nxtN / 5));
if (nxtN % 6 == 0 && nxtN / 6 < n)
ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
if (nxtN % 15 == 0 && nxtN / 15 < n)
ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
if (nxtN % 10 == 0 && nxtN / 10 < n)
ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
}
return ans;
}
int32_t main() {
IOS;
int t;
cin >> t;
while (t--) {
cache.clear();
cin >> k >> a >> b >> c >> d;
int ans = work(k);
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define int long long
const int N = 1e5 + 5;
int k, a, b, c, d;
unordered_map<int, int> cache;
int work(int n) {
if (n == 0)
return 0;
if (n == 1)
return d;
if (cache.count(n))
return cache[n];
int &ans = cache[n];
ans = 4e18;
if (n <= 4e9)
ans = min(ans, n * d);
for (int i = -12; i <= 12; i++) {
int nxtN = n + i;
if (nxtN < 0)
continue;
if (nxtN % 2 == 0 && nxtN / 2 < n)
ans = min(ans, d * abs(i) + a + work(nxtN / 2));
if (nxtN % 3 == 0 && nxtN / 3 < n)
ans = min(ans, d * abs(i) + b + work(nxtN / 3));
if (nxtN % 5 == 0 && nxtN / 5 < n)
ans = min(ans, d * abs(i) + c + work(nxtN / 5));
if (nxtN % 6 == 0 && nxtN / 6 < n)
ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
if (nxtN % 15 == 0 && nxtN / 15 < n)
ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
if (nxtN % 10 == 0 && nxtN / 10 < n)
ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
}
return ans;
}
int32_t main() {
IOS;
int t;
cin >> t;
while (t--) {
cache.clear();
cin >> k >> a >> b >> c >> d;
int ans = work(k);
cout << ans << endl;
}
return 0;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02669 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
map<ll, ll> m;
ll A, B, C, D;
ll solve(ll N) {
if (N == 0)
return 0;
if (N == 1)
return D;
// if(m.count(N)) return m[N];
m[N] = 1e18;
if (N / 1e9 * D < 3e9)
m[N] = min(m[N], N * D);
m[N] = min(m[N], A + D * (N % 2) + solve(N / 2));
m[N] = min(m[N], A + D * (2 - N % 2) + solve((N + 2) / 2));
m[N] = min(m[N], B + D * (N % 3) + solve(N / 3));
m[N] = min(m[N], B + D * (3 - N % 3) + solve((N + 3) / 3));
m[N] = min(m[N], C + D * (N % 5) + solve(N / 5));
m[N] = min(m[N], C + D * (5 - N % 5) + solve((N + 5) / 5));
return m[N];
}
int main() {
ll T;
cin >> T;
vector<ll> ans;
rep(i, T) {
ll N;
cin >> N;
cin >> A >> B >> C >> D;
m.clear();
ll tmp = solve(N);
ans.pb(tmp);
}
rep(i, T) cout << ans[i] << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
map<ll, ll> m;
ll A, B, C, D;
ll solve(ll N) {
if (N == 0)
return 0;
if (N == 1)
return D;
if (m.count(N))
return m[N];
m[N] = 1e18;
if (N / 1e9 * D < 3e9)
m[N] = min(m[N], N * D);
m[N] = min(m[N], A + D * (N % 2) + solve(N / 2));
m[N] = min(m[N], A + D * (2 - N % 2) + solve((N + 2) / 2));
m[N] = min(m[N], B + D * (N % 3) + solve(N / 3));
m[N] = min(m[N], B + D * (3 - N % 3) + solve((N + 3) / 3));
m[N] = min(m[N], C + D * (N % 5) + solve(N / 5));
m[N] = min(m[N], C + D * (5 - N % 5) + solve((N + 5) / 5));
return m[N];
}
int main() {
ll T;
cin >> T;
vector<ll> ans;
rep(i, T) {
ll N;
cin >> N;
cin >> A >> B >> C >> D;
m.clear();
ll tmp = solve(N);
ans.pb(tmp);
}
rep(i, T) cout << ans[i] << endl;
}
| replace | 40 | 41 | 40 | 42 | -11 | |
p02669 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
auto calc = [&](long long n, long long a, long long b, long long c,
long long d) {
const int N = 100100;
const long long INF = 1LL << 62;
vector<long long> dp(N + 1, INF);
dp[0] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
greater<pair<long long, int>>>
pq;
pq.emplace(0, 0);
vector<long long> costs(10, 0);
costs[2] = a;
costs[3] = b;
costs[5] = c;
costs[1] = d;
while (!pq.empty()) {
long long x = pq.top().second;
long long d = pq.top().first;
pq.pop();
// cout << x << " " << d << endl;
if (d != dp[x]) {
continue;
}
{
for (auto p : {2, 3, 5}) {
long long nx = x * p;
if (nx <= N) {
if (dp[nx] > d + costs[p]) {
dp[nx] = d + costs[p];
pq.emplace(dp[nx], nx);
}
} else {
break;
}
}
}
{
for (int i = -1; i < 2; i += 2) {
long long nx = x + i;
if (nx > 0 && nx <= N) {
if (dp[nx] > d + costs[1]) {
dp[nx] = d + costs[1];
pq.emplace(dp[nx], nx);
}
}
}
}
}
vector<long long> mins(10, 0);
for (auto x : {2, 3, 5}) {
long long d = (costs[1] * (x - 1));
mins[x] = (costs[x] + d - 1) / d;
}
map<long long, long long> M;
M[0] = 0;
function<long long(long long)> dfs = [&](long long x) -> long long {
if (x * 10 <= N) {
return dp[x];
}
if (M.count(x) != 0) {
return M[x];
}
auto &ans = M[x];
ans = INF;
if (x < INF / costs[1]) {
ans = x * costs[1];
}
for (auto p : {2, 3, 5}) {
for (auto d : {x % p, x % p - p}) {
auto nx = x - d;
assert(nx % p == 0);
long long w = abs(x - nx) * costs[1];
if (nx > 0) {
// ans = min(ans, w + dfs(nx / p) + (nx >= mins[p] * p ? costs[p] :
// nx / p * (p - 1) * costs[1]));
ans = min(ans, w + dfs(nx / p) + costs[p]);
}
}
}
// {
// ans = min(ans, dfs(x / 3) + (x >= mins[3] ? costs[3] : x *
// costs[1]);
// }
// {
// ans = min(ans, dfs(x / 5) + (x >= mins[5] ? costs[5] : x *
// costs[1]);
// }
// cout << x << " " << ans << endl;
return ans;
};
return dfs(n);
};
int t;
cin >> t;
while (t-- > 0) {
long long n;
long long a, b, c, d;
cin >> n >> a >> b >> c >> d;
cout << calc(n, a, b, c, d) << '\n';
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
auto calc = [&](long long n, long long a, long long b, long long c,
long long d) {
const int N = 100100;
const long long INF = 1LL << 62;
vector<long long> dp(N + 1, INF);
dp[0] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
greater<pair<long long, int>>>
pq;
pq.emplace(0, 0);
vector<long long> costs(10, 0);
costs[2] = a;
costs[3] = b;
costs[5] = c;
costs[1] = d;
while (!pq.empty()) {
long long x = pq.top().second;
long long d = pq.top().first;
pq.pop();
// cout << x << " " << d << endl;
if (d != dp[x]) {
continue;
}
{
for (auto p : {2, 3, 5}) {
long long nx = x * p;
if (nx <= N) {
if (dp[nx] > d + costs[p]) {
dp[nx] = d + costs[p];
pq.emplace(dp[nx], nx);
}
} else {
break;
}
}
}
{
for (int i = -1; i < 2; i += 2) {
long long nx = x + i;
if (nx > 0 && nx <= N) {
if (dp[nx] > d + costs[1]) {
dp[nx] = d + costs[1];
pq.emplace(dp[nx], nx);
}
}
}
}
}
vector<long long> mins(10, 0);
for (auto x : {2, 3, 5}) {
long long d = (costs[1] * (x - 1));
mins[x] = (costs[x] + d - 1) / d;
}
map<long long, long long> M;
M[0] = 0;
function<long long(long long)> dfs = [&](long long x) -> long long {
if (x <= N / 10) {
return dp[x];
}
if (M.count(x) != 0) {
return M[x];
}
auto &ans = M[x];
ans = INF;
if (x < INF / costs[1]) {
ans = x * costs[1];
}
for (auto p : {2, 3, 5}) {
for (auto d : {x % p, x % p - p}) {
auto nx = x - d;
assert(nx % p == 0);
long long w = abs(x - nx) * costs[1];
if (nx > 0) {
// ans = min(ans, w + dfs(nx / p) + (nx >= mins[p] * p ? costs[p] :
// nx / p * (p - 1) * costs[1]));
ans = min(ans, w + dfs(nx / p) + costs[p]);
}
}
}
// {
// ans = min(ans, dfs(x / 3) + (x >= mins[3] ? costs[3] : x *
// costs[1]);
// }
// {
// ans = min(ans, dfs(x / 5) + (x >= mins[5] ? costs[5] : x *
// costs[1]);
// }
// cout << x << " " << ans << endl;
return ans;
};
return dfs(n);
};
int t;
cin >> t;
while (t-- > 0) {
long long n;
long long a, b, c, d;
cin >> n >> a >> b >> c >> d;
cout << calc(n, a, b, c, d) << '\n';
}
return 0;
} | replace | 88 | 89 | 88 | 89 | 0 | |
p02669 | C++ | Time Limit Exceeded | #pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
inline int topbit(int x) { return x == 0 ? -1 : 31 - __builtin_clz(x); }
inline int topbit(long long x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
inline int botbit(int x) { return x == 0 ? 32 : __builtin_ctz(x); }
inline int botbit(long long x) { return x == 0 ? 64 : __builtin_ctzll(x); }
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
inline void print() { cout << "\n"; }
template <class T> inline void print(const vector<T> &v) {
for (auto itr = v.begin(); itr != v.end(); ++itr)
cout << *itr << " ";
print();
}
template <class T, class... Args>
inline void print(const T &x, const Args &...args) {
cout << x << " ";
print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() { cerr << endl; }
template <class T> inline void debug(const vector<T> &v) {
for (auto itr = v.begin(); itr != v.end(); ++itr)
cerr << *itr << " ";
debug();
}
template <class T, class... Args>
inline void debug(const T &x, const Args &...args) {
cerr << x << " ";
debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template <class T> inline void debug(const vector<T> &v) {}
template <class T, class... Args>
inline void debug(const T &x, const Args &...args) {}
#endif
struct Fast_ios {
Fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll C[4];
ll arr[3] = {2, 3, 5};
const ll INF = 1e18;
void solve() {
ll N;
cin >> N;
rep(i, 4) cin >> C[i];
unordered_map<ll, ll> dic;
auto dfs = [&](auto &&self, ll n) -> ll {
if (dic.count(n))
return dic[n];
if (n == 1)
return dic[n] = C[3];
dic[n] = INF;
if (C[3] <= INF / n)
dic[n] = C[3] * n;
rep(i, 3) {
ll m1 = n / arr[i];
ll m2 = m1 + 1;
ll l = m1 * arr[i];
ll r = m2 * arr[i];
if (m1 and m1 < n) {
chmin(dic[n], self(self, m1) + (n - l) * C[3] + C[i]);
}
if (m2 < n) {
chmin(dic[n], self(self, m2) + (r - n) * C[3] + C[i]);
}
}
return dic[n];
};
cout << dfs(dfs, N) << ln;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
int Q;
cin >> Q;
while (Q--) {
solve();
}
} | #pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
inline int topbit(int x) { return x == 0 ? -1 : 31 - __builtin_clz(x); }
inline int topbit(long long x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
inline int botbit(int x) { return x == 0 ? 32 : __builtin_ctz(x); }
inline int botbit(long long x) { return x == 0 ? 64 : __builtin_ctzll(x); }
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
inline void print() { cout << "\n"; }
template <class T> inline void print(const vector<T> &v) {
for (auto itr = v.begin(); itr != v.end(); ++itr)
cout << *itr << " ";
print();
}
template <class T, class... Args>
inline void print(const T &x, const Args &...args) {
cout << x << " ";
print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() { cerr << endl; }
template <class T> inline void debug(const vector<T> &v) {
for (auto itr = v.begin(); itr != v.end(); ++itr)
cerr << *itr << " ";
debug();
}
template <class T, class... Args>
inline void debug(const T &x, const Args &...args) {
cerr << x << " ";
debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template <class T> inline void debug(const vector<T> &v) {}
template <class T, class... Args>
inline void debug(const T &x, const Args &...args) {}
#endif
struct Fast_ios {
Fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll C[4];
ll arr[3] = {2, 3, 5};
const ll INF = 1e18;
void solve() {
ll N;
cin >> N;
rep(i, 4) cin >> C[i];
unordered_map<ll, ll> dic;
auto dfs = [&](auto &&self, ll n) -> ll {
if (dic.count(n))
return dic[n];
if (n == 1)
return dic[n] = C[3];
dic[n] = INF;
if (C[3] <= INF / n)
dic[n] = C[3] * n;
rep(i, 3) {
ll m1 = n / arr[i];
ll m2 = m1 + 1;
ll l = m1 * arr[i];
ll r = m2 * arr[i];
if (m1 and m1 < n) {
chmin(dic[n], self(self, m1) + (n - l) * C[3] + C[i]);
}
if (m2 < n) {
chmin(dic[n], self(self, m2) + (r - n) * C[3] + C[i]);
}
}
return dic[n];
};
cout << dfs(dfs, N) << ln;
}
int main() {
int Q;
cin >> Q;
while (Q--) {
solve();
}
} | delete | 114 | 119 | 114 | 114 | TLE | |
p02669 | C++ | Runtime Error | #include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second
using namespace std;
template <typename T> string to_string(const vector<T> &vc, int w) {
if (vc.empty())
return "";
if (w + 1 == vc.size())
return to_string(vc[w]);
return to_string(vc[w]) + "," + to_string(vc, w + 1);
}
template <typename T> string to_string(const vector<T> &vc) {
return "{" + to_string(vc, 0) + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
return s;
}
#ifdef DEBUG
#define LOG clog
#endif
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e2 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
bs = 29;
ll dp[maxN][maxN][maxN][2], multi[maxN][maxN][maxN], n, a, b, c, d, ans;
ll cmlt(int as, int bs, int cs) {
if (!multi[as][bs][cs]) {
assert(as || bs || cs);
if (as)
multi[as][bs][cs] = cmlt(as - 1, bs, cs) * 2;
else if (bs)
multi[as][bs][cs] = cmlt(as, bs - 1, cs) * 3;
else
multi[as][bs][cs] = cmlt(as, bs, cs - 1) * 5;
}
return multi[as][bs][cs];
}
ll solve(int as, int bs, int cs, int tp) {
auto &mm = dp[as][bs][cs][tp];
if (mm == -1) {
mm = LLONG_MAX / 3;
if (as) {
ll dg = n / cmlt(as - 1, bs, cs) % 2;
if (tp == 0) {
mm = min(mm, dg * d + solve(as - 1, bs, cs, 0));
mm = min(mm, (dg + 1) * d + solve(as - 1, bs, cs, 1));
} else {
mm = min(mm, (2 - dg) * d + solve(as - 1, bs, cs, 0));
mm = min(mm, (2 - (dg + 1)) * d + solve(as - 1, bs, cs, 1));
}
}
if (bs) {
ll dg = n / cmlt(as, bs - 1, cs) % 3;
if (tp == 0) {
mm = min(mm, dg * d + solve(as, bs - 1, cs, 0));
mm = min(mm, (dg + 1) * d + solve(as, bs - 1, cs, 1));
} else {
mm = min(mm, (3 - dg) * d + solve(as, bs - 1, cs, 0));
mm = min(mm, (3 - (dg + 1)) * d + solve(as, bs - 1, cs, 1));
}
}
if (cs) {
ll dg = n / cmlt(as, bs, cs - 1) % 5;
if (tp == 0) {
mm = min(mm, dg * d + solve(as, bs, cs - 1, 0));
mm = min(mm, (dg + 1) * d + solve(as, bs, cs - 1, 1));
} else {
mm = min(mm, (5 - dg) * d + solve(as, bs, cs - 1, 0));
mm = min(mm, (5 - (dg + 1)) * d + solve(as, bs, cs - 1, 1));
}
}
}
return mm;
}
void inter(int i, int j, int k) {
{
ll cr = solve(i, j, k, 0) + i * a + j * b + k * c;
ll initi = n / cmlt(i, j, k);
if (d <= ll(1e12) / initi)
ans = min(ans, cr + initi * d);
}
{
ll cr = solve(i, j, k, 1) + i * a + j * b + k * c;
ll initi = n / cmlt(i, j, k) + 1;
if (d <= ll(1e12) / initi)
ans = min(ans, cr + initi * d);
}
}
void pre(int i, int j, int k) {
inter(i, j, k);
inter(i + 1, j, k);
inter(i, j + 1, k);
inter(i, j, k + 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int t;
cin >> t;
multi[0][0][0] = 1;
while (t--) {
memset(dp, -1, sizeof dp);
cin >> n >> a >> b >> c >> d;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = LLONG_MAX / 3;
ans = LLONG_MAX / 3;
int i = 13, j = 6, k = 16;
for (i = 0; cmlt(i, 0, 0) <= n; i++) {
for (j = 0; cmlt(i, j, 0) <= n; j++) {
for (k = 0; cmlt(i, j, k) <= n; k++) {
pre(i, j, k);
}
}
}
cout << ans << '\n';
}
}
| #include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second
using namespace std;
template <typename T> string to_string(const vector<T> &vc, int w) {
if (vc.empty())
return "";
if (w + 1 == vc.size())
return to_string(vc[w]);
return to_string(vc[w]) + "," + to_string(vc, w + 1);
}
template <typename T> string to_string(const vector<T> &vc) {
return "{" + to_string(vc, 0) + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
return s;
}
#ifdef DEBUG
#define LOG clog
#endif
typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e2 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
bs = 29;
ll dp[maxN][maxN][maxN][2], multi[maxN][maxN][maxN], n, a, b, c, d, ans;
ll cmlt(int as, int bs, int cs) {
if (!multi[as][bs][cs]) {
assert(as || bs || cs);
if (as)
multi[as][bs][cs] = cmlt(as - 1, bs, cs) * 2;
else if (bs)
multi[as][bs][cs] = cmlt(as, bs - 1, cs) * 3;
else
multi[as][bs][cs] = cmlt(as, bs, cs - 1) * 5;
}
return multi[as][bs][cs];
}
ll solve(int as, int bs, int cs, int tp) {
auto &mm = dp[as][bs][cs][tp];
if (mm == -1) {
mm = LLONG_MAX / 3;
if (as) {
ll dg = n / cmlt(as - 1, bs, cs) % 2;
if (tp == 0) {
mm = min(mm, dg * d + solve(as - 1, bs, cs, 0));
mm = min(mm, (dg + 1) * d + solve(as - 1, bs, cs, 1));
} else {
mm = min(mm, (2 - dg) * d + solve(as - 1, bs, cs, 0));
mm = min(mm, (2 - (dg + 1)) * d + solve(as - 1, bs, cs, 1));
}
}
if (bs) {
ll dg = n / cmlt(as, bs - 1, cs) % 3;
if (tp == 0) {
mm = min(mm, dg * d + solve(as, bs - 1, cs, 0));
mm = min(mm, (dg + 1) * d + solve(as, bs - 1, cs, 1));
} else {
mm = min(mm, (3 - dg) * d + solve(as, bs - 1, cs, 0));
mm = min(mm, (3 - (dg + 1)) * d + solve(as, bs - 1, cs, 1));
}
}
if (cs) {
ll dg = n / cmlt(as, bs, cs - 1) % 5;
if (tp == 0) {
mm = min(mm, dg * d + solve(as, bs, cs - 1, 0));
mm = min(mm, (dg + 1) * d + solve(as, bs, cs - 1, 1));
} else {
mm = min(mm, (5 - dg) * d + solve(as, bs, cs - 1, 0));
mm = min(mm, (5 - (dg + 1)) * d + solve(as, bs, cs - 1, 1));
}
}
}
return mm;
}
void inter(int i, int j, int k) {
{
ll cr = solve(i, j, k, 0) + i * a + j * b + k * c;
ll initi = n / cmlt(i, j, k);
if (initi == 0 || d <= ll(1e12) / initi)
ans = min(ans, cr + initi * d);
}
{
ll cr = solve(i, j, k, 1) + i * a + j * b + k * c;
ll initi = n / cmlt(i, j, k) + 1;
if (d <= ll(1e12) / initi)
ans = min(ans, cr + initi * d);
}
}
void pre(int i, int j, int k) {
inter(i, j, k);
inter(i + 1, j, k);
inter(i, j + 1, k);
inter(i, j, k + 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int t;
cin >> t;
multi[0][0][0] = 1;
while (t--) {
memset(dp, -1, sizeof dp);
cin >> n >> a >> b >> c >> d;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = LLONG_MAX / 3;
ans = LLONG_MAX / 3;
int i = 13, j = 6, k = 16;
for (i = 0; cmlt(i, 0, 0) <= n; i++) {
for (j = 0; cmlt(i, j, 0) <= n; j++) {
for (k = 0; cmlt(i, j, k) <= n; k++) {
pre(i, j, k);
}
}
}
cout << ans << '\n';
}
}
| replace | 109 | 110 | 109 | 110 | -8 | |
p02669 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
if (INF / (x - y) >= D)
return D * (x - y);
return INF;
}
void get(ll x, ll y) {
if (s[x] == 0) {
s[x] = y;
p.insert(x);
} else
s[x] = min(s[x], y);
}
int main() {
cin >> t;
while (t--) {
s.clear();
p.clear();
cin >> n >> A >> B >> C >> D;
p.insert(n);
s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
while (!p.empty()) {
auto g = p.end();
g--;
ll x = (*g);
p.erase(g);
ll y = s[x];
if (y > s[0])
continue;
get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
if (x % 2 != 0)
get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
if (x % 3 <= 1)
get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
else
get(x / 3 + 1,
y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
if (x % 5 < 3)
get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
else
get(x / 5 + 1,
y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
}
cout << s[0] << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
if (INF / (x - y) >= D)
return D * (x - y);
return INF;
}
void get(ll x, ll y) {
if (s[x] == 0) {
s[x] = y;
p.insert(x);
} else
s[x] = min(s[x], y);
}
int main() {
cin >> t;
while (t--) {
s.clear();
p.clear();
cin >> n >> A >> B >> C >> D;
p.insert(n);
if (n == 1) {
cout << D << endl;
continue;
}
s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
while (!p.empty()) {
auto g = p.end();
g--;
ll x = (*g);
p.erase(g);
ll y = s[x];
if (y > s[0])
continue;
get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
if (x % 2 != 0)
get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
if (x % 3 <= 1)
get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
else
get(x / 3 + 1,
y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
if (x % 5 < 3)
get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
else
get(x / 5 + 1,
y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
}
cout << s[0] << endl;
}
return 0;
} | insert | 27 | 27 | 27 | 31 | TLE | |
p02669 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
map<long long, long long> dp;
long long a, b, c, d;
long long rec(long long n) {
if (n == 0)
return 0;
if (n == 1)
return d;
auto I = dp.find(n);
if (I != dp.end())
return I->second;
long long best = LLONG_MAX;
if (n % 2 == 0) {
best = min(best, a + rec(n / 2));
}
if (n % 3 == 0) {
best = min(best, b + rec(n / 3));
}
if (n % 5 == 0) {
best = min(best, c + rec(n / 5));
}
if (LLONG_MAX / d > n) {
best = min(best, d * n);
}
for (int i = -10; i <= 10; i++) {
long long m = n + i;
if (m < 0)
continue;
if (m % 2 == 0 && m / 2 < n) {
best = min(best, abs(i) * d + a + rec(m / 2));
}
if (m % 3 == 0 && m / 3 < n) {
best = min(best, abs(i) * d + b + rec(m / 3));
}
if (m % 5 == 0 && m / 5 < n) {
best = min(best, abs(i) * d + c + rec(m / 5));
}
}
dp[n] = best;
return best;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T-- > 0) {
long long n;
cin >> n >> a >> b >> c >> d;
dp.clear();
long long ans = rec(n);
cout << ans << "\n";
}
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
map<long long, long long> dp;
long long a, b, c, d;
long long rec(long long n) {
if (n == 0)
return 0;
if (n == 1)
return d;
auto I = dp.find(n);
if (I != dp.end())
return I->second;
long long best = LLONG_MAX;
if (n % 2 == 0) {
best = min(best, a + rec(n / 2));
}
if (n % 3 == 0) {
best = min(best, b + rec(n / 3));
}
if (n % 5 == 0) {
best = min(best, c + rec(n / 5));
}
if (LLONG_MAX / d > n) {
best = min(best, d * n);
}
for (int i = -5; i <= 5; i++) {
long long m = n + i;
if (m < 0)
continue;
if (m % 2 == 0 && m / 2 < n) {
best = min(best, abs(i) * d + a + rec(m / 2));
}
if (m % 3 == 0 && m / 3 < n) {
best = min(best, abs(i) * d + b + rec(m / 3));
}
if (m % 5 == 0 && m / 5 < n) {
best = min(best, abs(i) * d + c + rec(m / 5));
}
}
dp[n] = best;
return best;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T-- > 0) {
long long n;
cin >> n >> a >> b >> c >> d;
dp.clear();
long long ans = rec(n);
cout << ans << "\n";
}
return 0;
}
| replace | 47 | 48 | 47 | 48 | TLE | |
p02669 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto &&i : n)
/* debug */
#define debug(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
// using D = double;
// using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
// const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
int x;
cin >> x;
return x;
}
inline ll IN() {
ll x;
cin >> x;
return x;
}
inline vs split(const string &t, char c) {
vs v;
stringstream s(t);
string b;
while (getline(s, b, c))
v.eb(b);
return v;
}
template <typename T> inline bool chmin(T &a, const T &b) {
if (a > b)
a = b;
return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
if (a < b)
a = b;
return a < b;
}
template <typename T, typename S> inline void print(const pair<T, S> &p) {
cout << p.first << " " << p.second << endl;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }
template <typename T, typename S>
inline void print(const vector<pair<T, S>> &v) {
for (auto &&p : v)
print(p);
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");
}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {
cout << (ok ? yes : no) << endl;
}
ll gmin;
void solve() {
ll N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
// gmin = LINF;
vi k = {2, 3, 5};
PQ<ll> q;
map<ll, ll> mp;
ll p, prevp, np, curcost, ncost, deltacost;
int mul;
ll mulcost;
q.push(N);
mp[N] = 0;
q.push(0);
mp[0] = LINF;
prevp = -1;
while (!q.empty()) {
p = q.top();
curcost = mp[p];
// cout << "got N:" << p << ", cost:" << curcost << endl;
if (p == 0) {
print(curcost);
break;
}
if (p == prevp)
continue;
if ((LINF - curcost) / D > p) {
mp[0] = min(mp[0], curcost + D * p);
}
REP(i, k.size()) {
mul = k[i];
if (mul == 2)
mulcost = A;
if (mul == 3)
mulcost = B;
if (mul == 5)
mulcost = C;
if (p % mul == 0) {
np = p / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(1):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
} else {
// lower
np = p - (p % mul);
deltacost = (p % mul) * D;
if (np > 0) {
np = np / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
ncost += deltacost;
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(2):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
}
// upper
np = p + (mul - (p % mul));
deltacost = (mul - (p % mul)) * D;
np = np / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
ncost += deltacost;
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(3):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
}
}
prevp = p;
q.pop();
}
// print(gmin);
}
signed main() {
// my-code
int T;
cin >> T;
REP(loop, T) { solve(); }
return 0;
}
// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々 | #include <bits/stdc++.h>
using namespace std;
// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {
Fast() {
std::cin.tie(0);
ios::sync_with_stdio(false);
}
} fast;
/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto &&i : n)
/* debug */
#define debug(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
// using D = double;
// using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
// const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {
return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
int x;
cin >> x;
return x;
}
inline ll IN() {
ll x;
cin >> x;
return x;
}
inline vs split(const string &t, char c) {
vs v;
stringstream s(t);
string b;
while (getline(s, b, c))
v.eb(b);
return v;
}
template <typename T> inline bool chmin(T &a, const T &b) {
if (a > b)
a = b;
return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
if (a < b)
a = b;
return a < b;
}
template <typename T, typename S> inline void print(const pair<T, S> &p) {
cout << p.first << " " << p.second << endl;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }
template <typename T, typename S>
inline void print(const vector<pair<T, S>> &v) {
for (auto &&p : v)
print(p);
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");
}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {
cout << (ok ? yes : no) << endl;
}
ll gmin;
void solve() {
ll N, A, B, C, D;
cin >> N >> A >> B >> C >> D;
if (N == 1) {
print(D);
return;
}
// gmin = LINF;
vi k = {2, 3, 5};
PQ<ll> q;
map<ll, ll> mp;
ll p, prevp, np, curcost, ncost, deltacost;
int mul;
ll mulcost;
q.push(N);
mp[N] = 0;
q.push(0);
mp[0] = LINF;
prevp = -1;
while (!q.empty()) {
p = q.top();
curcost = mp[p];
// cout << "got N:" << p << ", cost:" << curcost << endl;
if (p == 0) {
print(curcost);
break;
}
if (p == prevp)
continue;
if ((LINF - curcost) / D > p) {
mp[0] = min(mp[0], curcost + D * p);
}
REP(i, k.size()) {
mul = k[i];
if (mul == 2)
mulcost = A;
if (mul == 3)
mulcost = B;
if (mul == 5)
mulcost = C;
if (p % mul == 0) {
np = p / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(1):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
} else {
// lower
np = p - (p % mul);
deltacost = (p % mul) * D;
if (np > 0) {
np = np / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
ncost += deltacost;
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(2):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
}
// upper
np = p + (mul - (p % mul));
deltacost = (mul - (p % mul)) * D;
np = np / mul;
if (D < LINF / (np * (mul - 1)) && np * (mul - 1) * D < mulcost) {
ncost = curcost + np * (mul - 1) * D;
} else {
ncost = curcost + mulcost;
}
ncost += deltacost;
if (!mp[np]) {
mp[np] = ncost;
q.push(np);
// cout << "push(3):" << np << endl;
} else if (mp[np] > ncost) {
mp[np] = ncost;
}
}
}
prevp = p;
q.pop();
}
// print(gmin);
}
signed main() {
// my-code
int T;
cin >> T;
REP(loop, T) { solve(); }
return 0;
}
// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々 | insert | 110 | 110 | 110 | 114 | TLE | |
p02670 | C++ | Runtime Error | /*
No matter how long a night, dawn will always break.
Simular saying ... The darkest hour is just before the dawn.
*************************************** ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
int gcd(int a, int b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }
const int N = 55;
int grid[N][N], vis[N][N], dis[N][N], n;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int d, int x, int y) {
int i, a, b, c, xx;
// cout<<"("<<x<<","<<y<<") ";
fo(i, 0, 4) {
a = x + dx[i];
b = y + dy[i];
if ((a < 1) || (a > n) || (b < 1) || (b > n))
continue;
c = dis[a][b];
xx = d + grid[x][y];
// cout<<a<<","<<b<<","<<c<<"< ";
if (c > xx) {
// cout<<"["<<dis[a][b]<<","<<xx<<"] ";
dis[a][b] = xx;
dfs(xx, a, b);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, x;
cin >> n;
fo(i, 1, n + 1) {
fo(j, 1, n + 1) {
grid[i][j] = 1;
vis[i][j] = -1;
dis[i][j] = min({i - 1, n - i, j - 1, n - j});
// cout<<dis[i][j]<<" ";
}
// cout<<endl;
}
vi v;
fo(i, 0, n * n) {
cin >> x;
v.pb(x);
}
int ans = 0;
fo(i, 0, n * n) {
int a, b, c, d;
x = v[i];
a = ((x - 1) / n) + 1;
b = ((x - 1) % n) + 1;
// cout<<x<<" ";
// cout<<"("<<a<<","<<b<<")"<<" ";
ans += dis[a][b];
grid[a][b] = 0;
x = dis[a][b];
fo(j, 0, 4) {
c = a + dx[j];
d = b + dy[j];
if ((c < 1) || (c > n) || (d < 1) || (d > n))
continue;
x = min(dis[c][d] + grid[c][d], x);
}
dis[a][b] = x;
// cout<<dis[a][b]<<" ";
dfs(dis[a][b], a, b);
// cout<<endl;
}
cout << ans << endl;
return 0;
} | /*
No matter how long a night, dawn will always break.
Simular saying ... The darkest hour is just before the dawn.
*************************************** ************************8
**********************************
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define fo(i, l, u) for (i = l; i < u; i++)
#define rfo(i, l, u) for (i = l; i >= u; i--)
#define allfo(s) for (auto it = (s).begin(); it != (s).end(); it++)
#define _init(b) memset(b, -1, sizeof(b))
#define _init0(b) memset(b, 0, sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO") << endl;
int gcd(int a, int b) {
if (a > b)
swap(a, b);
if (a == 0)
return b;
return gcd(b % a, a);
}
bool mod(double a, double b) { return a / b - floor(a / b); }
const int N = 505;
int grid[N][N], vis[N][N], dis[N][N], n;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int d, int x, int y) {
int i, a, b, c, xx;
// cout<<"("<<x<<","<<y<<") ";
fo(i, 0, 4) {
a = x + dx[i];
b = y + dy[i];
if ((a < 1) || (a > n) || (b < 1) || (b > n))
continue;
c = dis[a][b];
xx = d + grid[x][y];
// cout<<a<<","<<b<<","<<c<<"< ";
if (c > xx) {
// cout<<"["<<dis[a][b]<<","<<xx<<"] ";
dis[a][b] = xx;
dfs(xx, a, b);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i, j, x;
cin >> n;
fo(i, 1, n + 1) {
fo(j, 1, n + 1) {
grid[i][j] = 1;
vis[i][j] = -1;
dis[i][j] = min({i - 1, n - i, j - 1, n - j});
// cout<<dis[i][j]<<" ";
}
// cout<<endl;
}
vi v;
fo(i, 0, n * n) {
cin >> x;
v.pb(x);
}
int ans = 0;
fo(i, 0, n * n) {
int a, b, c, d;
x = v[i];
a = ((x - 1) / n) + 1;
b = ((x - 1) % n) + 1;
// cout<<x<<" ";
// cout<<"("<<a<<","<<b<<")"<<" ";
ans += dis[a][b];
grid[a][b] = 0;
x = dis[a][b];
fo(j, 0, 4) {
c = a + dx[j];
d = b + dy[j];
if ((c < 1) || (c > n) || (d < 1) || (d > n))
continue;
x = min(dis[c][d] + grid[c][d], x);
}
dis[a][b] = x;
// cout<<dis[a][b]<<" ";
dfs(dis[a][b], a, b);
// cout<<endl;
}
cout << ans << endl;
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
ll dfs(ll x, ll y) {
ll z = out[x][y] + occ[x][y];
for (ll i = 0; i < 4; i++) {
ll x1 = x + dx[i];
ll y1 = y + dy[i];
if (out[x1][y1] > z) {
out[x1][y1] = z;
dfs(x1, y1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
occ[i][j] = 1;
}
}
ll m = n * n;
ll pr = 0;
while (m--) {
ll id;
cin >> id;
ll x = (id + n - 1) / n;
ll y = id % n;
if (y == 0) {
y = n;
}
occ[x][y] = 0;
pr += out[x][y];
dfs(x, y);
}
cout << pr;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
void dfs(ll x, ll y) {
ll z = out[x][y] + occ[x][y];
for (ll i = 0; i < 4; i++) {
ll x1 = x + dx[i];
ll y1 = y + dy[i];
if (out[x1][y1] > z) {
out[x1][y1] = z;
dfs(x1, y1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
occ[i][j] = 1;
}
}
ll m = n * n;
ll pr = 0;
while (m--) {
ll id;
cin >> id;
ll x = (id + n - 1) / n;
ll y = id % n;
if (y == 0) {
y = n;
}
occ[x][y] = 0;
pr += out[x][y];
dfs(x, y);
}
cout << pr;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02670 | C++ | Time Limit Exceeded | /*input
6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10
29 28 33 12 6 5 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
out << '(' << P.F << ',' << P.S << ')';
return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
return out;
}
//}}}
const ll maxn = 505;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;
ll mypow(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
int n;
int p[maxn * maxn];
int d[maxn][maxn];
bool vis[maxn][maxn];
vector<pii> q;
bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }
ll sumD;
int main() {
IOS;
cin >> n;
REP(i, n) REP(j, n) {
d[i][j] = min({i + 1, n - i, j + 1, n - j});
sumD += d[i][j];
}
REP(i, n) REP(j, n) vis[i][j] = 1;
int ans = 0;
REP(I, n * n) {
int u, v;
{
int tmp;
cin >> tmp;
tmp--;
u = tmp / n;
v = tmp % n;
}
ans += d[u][v];
d[u][v]--;
vis[u][v] = 0;
int pt = 0;
q.eb(u, v);
while (pt < SZ(q)) {
int x = q[pt].F, y = q[pt].S;
pt++;
{
int _x = x + 1, _y = y;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x - 1, _y = y;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x, _y = y + 1;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x, _y = y - 1;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
}
// REP(i,n) REP(j,n) cout<<d[i][j]<<" \n"[j==n-1];
// cout<<endl;
ll s = 0;
REP(i, n) REP(j, n) s += d[i][j];
assert(SZ(q) <= sumD - s);
sumD = s;
q.clear();
}
cout << ans - n * n << '\n';
return 0;
}
| /*input
6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10
29 28 33 12 6 5 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c) \
(sort(c.begin(), c.end()), \
c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
out << '(' << P.F << ',' << P.S << ')';
return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
return out;
}
//}}}
const ll maxn = 505;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;
ll mypow(ll a, ll b) {
ll res = 1LL;
while (b) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
int n;
int p[maxn * maxn];
int d[maxn][maxn];
bool vis[maxn][maxn];
vector<pii> q;
bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }
ll sumD;
int main() {
IOS;
cin >> n;
REP(i, n) REP(j, n) {
d[i][j] = min({i + 1, n - i, j + 1, n - j});
sumD += d[i][j];
}
REP(i, n) REP(j, n) vis[i][j] = 1;
int ans = 0;
REP(I, n * n) {
int u, v;
{
int tmp;
cin >> tmp;
tmp--;
u = tmp / n;
v = tmp % n;
}
ans += d[u][v];
d[u][v]--;
vis[u][v] = 0;
int pt = 0;
q.eb(u, v);
while (pt < SZ(q)) {
int x = q[pt].F, y = q[pt].S;
pt++;
{
int _x = x + 1, _y = y;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x - 1, _y = y;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x, _y = y + 1;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
{
int _x = x, _y = y - 1;
if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
d[_x][_y] = d[x][y] + vis[_x][_y];
q.eb(_x, _y);
}
}
}
// REP(i,n) REP(j,n) cout<<d[i][j]<<" \n"[j==n-1];
// cout<<endl;
q.clear();
}
cout << ans - n * n << '\n';
return 0;
}
| replace | 136 | 140 | 136 | 137 | TLE | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
#define mo 100000007
using namespace std;
long long read() {
long long xx = 0, flagg = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-')
ch = getchar();
if (ch == '-') {
flagg = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = xx * 10 + ch - '0';
ch = getchar();
}
return xx * flagg;
}
void pus(long long xx, long long flagg) {
if (xx < 0) {
putchar('-');
xx = -xx;
}
if (xx >= 10)
pus(xx / 10, 0);
putchar(xx % 10 + '0');
if (flagg == 1)
putchar(' ');
if (flagg == 2)
putchar('\n');
return;
}
int n, m, i, j, k, p, vis[505][505], head, tail, zhanx[100005], zhany[100005],
x, y;
int book[505][505], now1, now2, ans;
void BFS(int u, int v) {
vis[u][v]--;
ans += vis[u][v];
book[u][v] = 1;
head = 1;
tail = 1;
zhanx[1] = u;
zhany[1] = v;
now2 = 0;
while (tail != now2) {
now1 = head;
while (head <= tail) {
x = zhanx[head];
y = zhany[head];
if (book[x - 1][y] == 1 && x >= 1) {
if (vis[x - 1][y] > vis[x][y]) {
tail++;
zhanx[tail] = x - 1;
zhany[tail] = y;
vis[x - 1][y] = vis[x][y];
}
}
if (book[x + 1][y] == 1 && x <= n) {
if (vis[x + 1][y] > vis[x][y]) {
tail++;
zhanx[tail] = x + 1;
zhany[tail] = y;
vis[x + 1][y] = vis[x][y];
}
}
if (book[x][y - 1] == 1 && y >= 1) {
if (vis[x][y - 1] > vis[x][y]) {
tail++;
zhanx[tail] = x;
zhany[tail] = y - 1;
vis[x][y - 1] = vis[x][y];
}
}
if (book[x][y + 1] == 1 && y <= n) {
if (vis[x][y + 1] > vis[x][y]) {
tail++;
zhanx[tail] = x;
zhany[tail] = y + 1;
vis[x][y + 1] = vis[x][y];
}
}
head++;
}
head = now1;
now2 = tail;
while (head <= now2) {
x = zhanx[head];
y = zhany[head];
if (book[x - 1][y] == 0 && x >= 1) {
if (vis[x - 1][y] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x - 1;
zhany[tail] = y;
vis[x - 1][y] = vis[x][y] + 1;
}
}
if (book[x + 1][y] == 0 && x <= n) {
if (vis[x + 1][y] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x + 1;
zhany[tail] = y;
vis[x + 1][y] = vis[x][y] + 1;
}
}
if (book[x][y - 1] == 0 && y >= 1) {
if (vis[x][y - 1] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x;
zhany[tail] = y - 1;
vis[x][y - 1] = vis[x][y] + 1;
}
}
if (book[x][y + 1] == 0 && y <= n) {
if (vis[x][y + 1] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x;
zhany[tail] = y + 1;
vis[x][y + 1] = vis[x][y] + 1;
}
}
head++;
}
}
}
int main() {
// freopen("domino.in","r",stdin);
// freopen("domino.out","w",stdout);
n = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
vis[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
for (i = 1; i <= n * n; i++) {
p = read();
BFS((p - 1) / n + 1, (p - 1) % n + 1);
}
pus(ans, 2);
return 0;
} | #include <bits/stdc++.h>
#define mo 100000007
using namespace std;
long long read() {
long long xx = 0, flagg = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-')
ch = getchar();
if (ch == '-') {
flagg = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = xx * 10 + ch - '0';
ch = getchar();
}
return xx * flagg;
}
void pus(long long xx, long long flagg) {
if (xx < 0) {
putchar('-');
xx = -xx;
}
if (xx >= 10)
pus(xx / 10, 0);
putchar(xx % 10 + '0');
if (flagg == 1)
putchar(' ');
if (flagg == 2)
putchar('\n');
return;
}
int n, m, i, j, k, p, vis[505][505], head, tail, zhanx[250005], zhany[250005],
x, y;
int book[505][505], now1, now2, ans;
void BFS(int u, int v) {
vis[u][v]--;
ans += vis[u][v];
book[u][v] = 1;
head = 1;
tail = 1;
zhanx[1] = u;
zhany[1] = v;
now2 = 0;
while (tail != now2) {
now1 = head;
while (head <= tail) {
x = zhanx[head];
y = zhany[head];
if (book[x - 1][y] == 1 && x >= 1) {
if (vis[x - 1][y] > vis[x][y]) {
tail++;
zhanx[tail] = x - 1;
zhany[tail] = y;
vis[x - 1][y] = vis[x][y];
}
}
if (book[x + 1][y] == 1 && x <= n) {
if (vis[x + 1][y] > vis[x][y]) {
tail++;
zhanx[tail] = x + 1;
zhany[tail] = y;
vis[x + 1][y] = vis[x][y];
}
}
if (book[x][y - 1] == 1 && y >= 1) {
if (vis[x][y - 1] > vis[x][y]) {
tail++;
zhanx[tail] = x;
zhany[tail] = y - 1;
vis[x][y - 1] = vis[x][y];
}
}
if (book[x][y + 1] == 1 && y <= n) {
if (vis[x][y + 1] > vis[x][y]) {
tail++;
zhanx[tail] = x;
zhany[tail] = y + 1;
vis[x][y + 1] = vis[x][y];
}
}
head++;
}
head = now1;
now2 = tail;
while (head <= now2) {
x = zhanx[head];
y = zhany[head];
if (book[x - 1][y] == 0 && x >= 1) {
if (vis[x - 1][y] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x - 1;
zhany[tail] = y;
vis[x - 1][y] = vis[x][y] + 1;
}
}
if (book[x + 1][y] == 0 && x <= n) {
if (vis[x + 1][y] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x + 1;
zhany[tail] = y;
vis[x + 1][y] = vis[x][y] + 1;
}
}
if (book[x][y - 1] == 0 && y >= 1) {
if (vis[x][y - 1] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x;
zhany[tail] = y - 1;
vis[x][y - 1] = vis[x][y] + 1;
}
}
if (book[x][y + 1] == 0 && y <= n) {
if (vis[x][y + 1] > vis[x][y] + 1) {
tail++;
zhanx[tail] = x;
zhany[tail] = y + 1;
vis[x][y + 1] = vis[x][y] + 1;
}
}
head++;
}
}
}
int main() {
// freopen("domino.in","r",stdin);
// freopen("domino.out","w",stdout);
n = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
vis[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
for (i = 1; i <= n * n; i++) {
p = read();
BFS((p - 1) / n + 1, (p - 1) % n + 1);
}
pus(ans, 2);
return 0;
} | replace | 32 | 33 | 32 | 33 | 0 | |
p02670 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
int cost[500][500];
bool valid[500][500];
int N;
const int INF_COST = 500;
int get(int x, int y) {
if (x >= N || x < 0 || y >= N || y < 0)
return INF_COST;
else
return cost[x][y];
}
// calculate cost
int cc(int min, int x, int y) {
int c = valid[x][y] ? (cost[x][y] + 1) : (cost[x][y]);
if (min > c) {
return c;
} else {
return INF_COST;
}
}
void update(int x, int y) {
if (x >= N || x < 0 || y >= N || y < 0)
return;
int min = cost[x][y];
bool updated = false;
int cos = INF_COST;
if ((cos = cc(min, x + 1, y)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x, y + 1)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x - 1, y)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x, y - 1)) != INF_COST) {
min = cos;
updated = true;
}
cost[x][y] = min;
if (updated) {
// printf("update %d %d\n", x, y);
update(x + 1, y);
update(x, y + 1);
update(x - 1, y);
update(x, y - 1);
}
}
int main() {
scanf("%d", &N);
// init
for (int x = 0; x < N; ++x) {
for (int y = 0; y < N; ++y) {
cost[x][y] = min(min(x, N - 1 - x), min(y, N - 1 - y));
valid[x][y] = true;
}
}
int count = 0;
for (int i = 0; i < N * N; ++i) {
int id;
scanf("%d", &id);
int x = (id - 1) % N;
int y = (id - 1) / N;
count += cost[x][y];
valid[x][y] = false;
// printf("%d: %d\n",i,id);
// for(int x=0;x<N;++x){
// for(int y=0;y<N;++y){
// printf("%d ,",cost[x][y]);
// }
// printf("\n");
// }
update(x + 1, y);
update(x, y + 1);
update(x - 1, y);
update(x, y - 1);
// printf("%d: %d\n",i,id);
// for(int x=0;x<N;++x){
// for(int y=0;y<N;++y){
// printf("%d ,",cost[x][y]);
// }
// printf("\n");
// }
// printf("\n");
}
printf("%d", count);
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
int cost[500][500];
bool valid[500][500];
int N;
const int INF_COST = 500;
int get(int x, int y) {
if (x >= N || x < 0 || y >= N || y < 0)
return INF_COST;
else
return cost[x][y];
}
// calculate cost
int cc(int min, int x, int y) {
if (x >= N || x < 0 || y >= N || y < 0)
return INF_COST;
int c = valid[x][y] ? (cost[x][y] + 1) : (cost[x][y]);
if (min > c) {
return c;
} else {
return INF_COST;
}
}
void update(int x, int y) {
if (x >= N || x < 0 || y >= N || y < 0)
return;
int min = cost[x][y];
bool updated = false;
int cos = INF_COST;
if ((cos = cc(min, x + 1, y)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x, y + 1)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x - 1, y)) != INF_COST) {
min = cos;
updated = true;
} else if ((cos = cc(min, x, y - 1)) != INF_COST) {
min = cos;
updated = true;
}
cost[x][y] = min;
if (updated) {
// printf("update %d %d\n", x, y);
update(x + 1, y);
update(x, y + 1);
update(x - 1, y);
update(x, y - 1);
}
}
int main() {
scanf("%d", &N);
// init
for (int x = 0; x < N; ++x) {
for (int y = 0; y < N; ++y) {
cost[x][y] = min(min(x, N - 1 - x), min(y, N - 1 - y));
valid[x][y] = true;
}
}
int count = 0;
for (int i = 0; i < N * N; ++i) {
int id;
scanf("%d", &id);
int x = (id - 1) % N;
int y = (id - 1) / N;
count += cost[x][y];
valid[x][y] = false;
// printf("%d: %d\n",i,id);
// for(int x=0;x<N;++x){
// for(int y=0;y<N;++y){
// printf("%d ,",cost[x][y]);
// }
// printf("\n");
// }
update(x + 1, y);
update(x, y + 1);
update(x - 1, y);
update(x, y - 1);
// printf("%d: %d\n",i,id);
// for(int x=0;x<N;++x){
// for(int y=0;y<N;++y){
// printf("%d ,",cost[x][y]);
// }
// printf("\n");
// }
// printf("\n");
}
printf("%d", count);
}
| insert | 20 | 20 | 20 | 22 | 0 | |
p02670 | C++ | Runtime Error | /*
读题不规范,爆零两行泪。
数据不清空,爆零两行泪。
多测不读完,爆零两行泪。
边界不特判,爆零两行泪。
贪心不证明,爆零两行泪。
D P 顺序错,爆零两行泪。
大小少等号,爆零两行泪。
变量不统一,爆零两行泪。
越界不判断,爆零两行泪。
调试不注释,爆零两行泪。
溢出不 l l,爆零两行泪。
*/
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define X first
#define Y second
const int N = 500;
int n;
bool hav[N + 1][N + 1];
int ans[N + 1][N + 1];
pair<int, int> q[N * N + 1];
int head, tail;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
cin >> n;
int anss = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans[i][j] = min(min(i, n - i + 1), min(j, n - j + 1)), hav[i][j] = true;
// for(int j=1;j<=n;j++){for(int k=1;k<=n;k++)cout<<ans[j][k]<<"
//";puts("");} puts("------------------------------------------------------");
for (int i = 1; i <= n * n; i++) {
int ord;
cin >> ord;
int x = (ord + n - 1) / n, y = (ord - 1) % n + 1;
ans[x][y]--;
hav[x][y] = false;
anss += ans[x][y];
head = tail;
q[tail++] = mp(x, y);
while (head < tail) {
int xx = q[head].X, yy = q[head].Y;
head++;
for (int j = 0; j < 4; j++) {
int xxx = xx + dx[j], yyy = yy + dy[j];
if (1 <= xxx && xxx <= n && 1 <= yyy && yyy <= n &&
ans[xx][yy] + hav[xxx][yyy] < ans[xxx][yyy])
ans[xxx][yyy] = ans[xx][yy] + hav[xxx][yyy], q[tail++] = mp(xxx, yyy);
}
}
// for(int j=1;j<=n;j++){for(int k=1;k<=n;k++)cout<<ans[j][k]<<"
//";puts("");}
// puts("------------------------------------------------------");
}
cout << anss;
return 0;
} | /*
读题不规范,爆零两行泪。
数据不清空,爆零两行泪。
多测不读完,爆零两行泪。
边界不特判,爆零两行泪。
贪心不证明,爆零两行泪。
D P 顺序错,爆零两行泪。
大小少等号,爆零两行泪。
变量不统一,爆零两行泪。
越界不判断,爆零两行泪。
调试不注释,爆零两行泪。
溢出不 l l,爆零两行泪。
*/
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define X first
#define Y second
const int N = 500;
int n;
bool hav[N + 1][N + 1];
int ans[N + 1][N + 1];
pair<int, int> q[N * N + 1];
int head, tail;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
cin >> n;
int anss = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans[i][j] = min(min(i, n - i + 1), min(j, n - j + 1)), hav[i][j] = true;
// for(int j=1;j<=n;j++){for(int k=1;k<=n;k++)cout<<ans[j][k]<<"
//";puts("");} puts("------------------------------------------------------");
for (int i = 1; i <= n * n; i++) {
int ord;
cin >> ord;
int x = (ord + n - 1) / n, y = (ord - 1) % n + 1;
ans[x][y]--;
hav[x][y] = false;
anss += ans[x][y];
head = tail = 0;
q[tail++] = mp(x, y);
while (head < tail) {
int xx = q[head].X, yy = q[head].Y;
head++;
for (int j = 0; j < 4; j++) {
int xxx = xx + dx[j], yyy = yy + dy[j];
if (1 <= xxx && xxx <= n && 1 <= yyy && yyy <= n &&
ans[xx][yy] + hav[xxx][yyy] < ans[xxx][yyy])
ans[xxx][yyy] = ans[xx][yy] + hav[xxx][yyy], q[tail++] = mp(xxx, yyy);
}
}
// for(int j=1;j<=n;j++){for(int k=1;k<=n;k++)cout<<ans[j][k]<<"
//";puts("");}
// puts("------------------------------------------------------");
}
cout << anss;
return 0;
} | replace | 41 | 42 | 41 | 42 | 0 | |
p02670 | C++ | Runtime Error | #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int D[502][502];
bool E[502][502];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N + 2) rep(j, N * 2) D[i][j] = -10;
rep1(i, N) rep1(j, N) {
D[i][j] = min(i - 1, j - 1);
int kari = min(N - i, N - j);
chmin(D[i][j], kari);
E[i][j] = 1;
}
ll kotae = 0;
rep(k, N * N) {
int p;
cin >> p;
p--;
int i = p / N + 1;
int j = p % N + 1;
kotae += D[i][j];
D[i][j]--;
E[i][j] = 0;
queue<pair<int, int>> Q;
Q.push(mp(i, j));
while (Q.size()) {
auto p = Q.front();
Q.pop();
i = p.first;
j = p.second;
int d = D[i][j];
if (D[i - 1][j] > d + E[i - 1][j]) {
D[i - 1][j] = d + E[i - 1][j];
Q.push(mp(i - 1, j));
}
if (D[i + 1][j] > d + E[i + 1][j]) {
D[i + 1][j] = d + E[i + 1][j];
Q.push(mp(i + 1, j));
}
if (D[i][j - 1] > d + E[i][j - 1]) {
D[i][j - 1] = d + E[i][j - 1];
Q.push(mp(i, j - 1));
}
if (D[i][j + 1] > d + E[i][j + 1]) {
D[i][j + 1] = d + E[i][j + 1];
Q.push(mp(i, j + 1));
}
}
}
co(kotae);
Would you please return 0;
} | #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int D[502][502];
bool E[502][502];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N + 2) rep(j, N + 2) D[i][j] = -10;
rep1(i, N) rep1(j, N) {
D[i][j] = min(i - 1, j - 1);
int kari = min(N - i, N - j);
chmin(D[i][j], kari);
E[i][j] = 1;
}
ll kotae = 0;
rep(k, N * N) {
int p;
cin >> p;
p--;
int i = p / N + 1;
int j = p % N + 1;
kotae += D[i][j];
D[i][j]--;
E[i][j] = 0;
queue<pair<int, int>> Q;
Q.push(mp(i, j));
while (Q.size()) {
auto p = Q.front();
Q.pop();
i = p.first;
j = p.second;
int d = D[i][j];
if (D[i - 1][j] > d + E[i - 1][j]) {
D[i - 1][j] = d + E[i - 1][j];
Q.push(mp(i - 1, j));
}
if (D[i + 1][j] > d + E[i + 1][j]) {
D[i + 1][j] = d + E[i + 1][j];
Q.push(mp(i + 1, j));
}
if (D[i][j - 1] > d + E[i][j - 1]) {
D[i][j - 1] = d + E[i][j - 1];
Q.push(mp(i, j - 1));
}
if (D[i][j + 1] > d + E[i][j + 1]) {
D[i][j + 1] = d + E[i][j + 1];
Q.push(mp(i, j + 1));
}
}
}
co(kotae);
Would you please return 0;
} | replace | 27 | 28 | 27 | 28 | 0 | |
p02670 | C++ | Runtime Error | #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
unsigned char D[252004];
bool E[252004];
unsigned int Q[200000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int n = N + 2;
rep1(i, N) {
int ij = n * i + 1;
unsigned char kari = min(i - 1, N - i) + 2;
unsigned char kari2 = 2;
rep1(j, N / 2) { D[ij++] = min(kari, kari2++); }
if (N % 2 == 0)
kari2--;
for (int j = N / 2 + 1; j <= N; j++) {
D[ij++] = min(kari, kari2--);
}
}
int kotae = 0;
rep(k, N * N) {
int ij;
cin >> ij;
ij += (ij - 1) / N * 2 + N + 2;
kotae += D[ij]--;
E[ij] = 1;
int p = 0, q = 0;
Q[p++] = ij;
while (p > q) {
int ij = Q[q++];
int d = D[ij] + 1;
if (D[ij - n] > d - E[ij - n]) {
D[ij - n] = d - E[ij - n];
Q[p++] = ij - n;
}
if (D[ij + n] > d - E[ij + n]) {
D[ij + n] = d - E[ij + n];
Q[p++] = ij + n;
}
if (D[ij - 1] > d - E[ij - 1]) {
D[ij - 1] = d - E[ij - 1];
Q[p++] = ij - 1;
}
if (D[ij + 1] > d - E[ij + 1]) {
D[ij + 1] = d - E[ij + 1];
Q[p++] = ij + 1;
}
}
}
co(kotae - 2 * N * N);
Would you please return 0;
} | #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
unsigned char D[252004];
bool E[252004];
unsigned int Q[300000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int n = N + 2;
rep1(i, N) {
int ij = n * i + 1;
unsigned char kari = min(i - 1, N - i) + 2;
unsigned char kari2 = 2;
rep1(j, N / 2) { D[ij++] = min(kari, kari2++); }
if (N % 2 == 0)
kari2--;
for (int j = N / 2 + 1; j <= N; j++) {
D[ij++] = min(kari, kari2--);
}
}
int kotae = 0;
rep(k, N * N) {
int ij;
cin >> ij;
ij += (ij - 1) / N * 2 + N + 2;
kotae += D[ij]--;
E[ij] = 1;
int p = 0, q = 0;
Q[p++] = ij;
while (p > q) {
int ij = Q[q++];
int d = D[ij] + 1;
if (D[ij - n] > d - E[ij - n]) {
D[ij - n] = d - E[ij - n];
Q[p++] = ij - n;
}
if (D[ij + n] > d - E[ij + n]) {
D[ij + n] = d - E[ij + n];
Q[p++] = ij + n;
}
if (D[ij - 1] > d - E[ij - 1]) {
D[ij - 1] = d - E[ij - 1];
Q[p++] = ij - 1;
}
if (D[ij + 1] > d - E[ij + 1]) {
D[ij + 1] = d - E[ij + 1];
Q[p++] = ij + 1;
}
}
}
co(kotae - 2 * N * N);
Would you please return 0;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02670 | C++ | Runtime Error | // In the Name of Allah
// Ya Ali
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
const int maxn = 510;
const int INF = 1e18;
int n, p[maxn * maxn], dis[maxn * maxn], rr, ans = 0;
vector<int> g[maxn * maxn];
bool mark[maxn];
int ver(int i, int j) {
if (i < 1 || i > n || j < 1 || j > n)
return rr;
return ((i - 1) * n) + j;
}
void bfs(int root) {
deque<int> qu;
qu.pb(root);
while (qu.size()) {
int v = qu.front();
qu.pop_front();
for (auto uu : g[v]) {
int u = uu, w = mark[v];
if ((dis[v] + w) < dis[u]) {
dis[u] = dis[v] + w;
if (w == 0)
qu.push_front(u);
else
qu.push_back(u);
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n * n; i++) {
cin >> p[i];
dis[i] = INF;
mark[i] = true;
}
rr = (n * n) + 1;
dis[rr] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int v = ((i - 1) * n) + j;
int v1 = ver(i - 1, j), v2 = ver(i + 1, j), v3 = ver(i, j + 1),
v4 = ver(i, j - 1);
g[v].pb(v1);
g[v1].pb(v);
g[v].pb(v2);
g[v2].pb(v);
g[v].pb(v3);
g[v3].pb(v);
g[v].pb(v4);
g[v4].pb(v);
}
}
bfs(rr);
for (int i = 1; i <= n * n; i++) {
ans += dis[p[i]];
mark[p[i]] = false;
bfs(p[i]);
}
cout << ans;
return 0;
}
| // In the Name of Allah
// Ya Ali
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
const int maxn = 510;
const int INF = 1e18;
int n, p[maxn * maxn], dis[maxn * maxn], rr, ans = 0;
vector<int> g[maxn * maxn];
bool mark[maxn * maxn];
int ver(int i, int j) {
if (i < 1 || i > n || j < 1 || j > n)
return rr;
return ((i - 1) * n) + j;
}
void bfs(int root) {
deque<int> qu;
qu.pb(root);
while (qu.size()) {
int v = qu.front();
qu.pop_front();
for (auto uu : g[v]) {
int u = uu, w = mark[v];
if ((dis[v] + w) < dis[u]) {
dis[u] = dis[v] + w;
if (w == 0)
qu.push_front(u);
else
qu.push_back(u);
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n * n; i++) {
cin >> p[i];
dis[i] = INF;
mark[i] = true;
}
rr = (n * n) + 1;
dis[rr] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int v = ((i - 1) * n) + j;
int v1 = ver(i - 1, j), v2 = ver(i + 1, j), v3 = ver(i, j + 1),
v4 = ver(i, j - 1);
g[v].pb(v1);
g[v1].pb(v);
g[v].pb(v2);
g[v2].pb(v);
g[v].pb(v3);
g[v3].pb(v);
g[v].pb(v4);
g[v4].pb(v);
}
}
bfs(rr);
for (int i = 1; i <= n * n; i++) {
ans += dis[p[i]];
mark[p[i]] = false;
bfs(p[i]);
}
cout << ans;
return 0;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02670 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
int ans = 0;
vector<vector<int>> dist(n, vector<int>(n));
vector<vector<bool>> occupied(n, vector<bool>(n, true));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
}
}
vector<int> bfs;
bfs.reserve(n * n);
const int mx[4] = {0, 1, 0, -1};
const int my[4] = {1, 0, -1, 0};
for (int _ = 0; _ < n * n; _++) {
int cur;
cin >> cur;
cur--;
int sx = cur / n, sy = cur % n;
occupied[sx][sy] = false;
dist[sx][sy]--;
ans += dist[sx][sy];
bfs.push_back(cur);
for (int s = 0; s < int(bfs.size()); s++) {
int v = bfs[s];
int x = v / n, y = v % n;
for (int d = 0; d < 4; d++) {
int nx = x + mx[d], ny = y + my[d];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
int nd = occupied[nx][ny] + dist[x][y];
if (nd < dist[nx][ny]) {
dist[nx][ny] = nd;
bfs.push_back(nx * n + ny);
}
}
}
}
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
int ans = 0;
vector<vector<int>> dist(n, vector<int>(n));
vector<vector<bool>> occupied(n, vector<bool>(n, true));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
}
}
vector<int> bfs;
bfs.reserve(n * n);
const int mx[4] = {0, 1, 0, -1};
const int my[4] = {1, 0, -1, 0};
for (int _ = 0; _ < n * n; _++) {
int cur;
cin >> cur;
cur--;
int sx = cur / n, sy = cur % n;
occupied[sx][sy] = false;
dist[sx][sy]--;
ans += dist[sx][sy];
bfs.clear();
bfs.push_back(cur);
for (int s = 0; s < int(bfs.size()); s++) {
int v = bfs[s];
int x = v / n, y = v % n;
for (int d = 0; d < 4; d++) {
int nx = x + mx[d], ny = y + my[d];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
int nd = occupied[nx][ny] + dist[x][y];
if (nd < dist[nx][ny]) {
dist[nx][ny] = nd;
bfs.push_back(nx * n + ny);
}
}
}
}
}
cout << ans << '\n';
}
| insert | 27 | 27 | 27 | 28 | TLE | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int INF = INT32_MAX >> 2;
vector<bool> k(n, true);
vector<int> d(n * n, INF);
rep(i, n * n) {
int x = i % n;
int y = i / n;
chmin(d[i], x);
chmin(d[i], y);
chmin(d[i], n - x - 1);
chmin(d[i], n - y - 1);
}
int ans = 0;
rep(i, n * n) {
int p;
cin >> p;
p--;
ans += d[p];
k[p] = false;
queue<int> q;
q.emplace(p);
while (!q.empty()) {
int nowp = q.front();
q.pop();
int x = nowp % n;
int y = nowp / n;
rep(j, 4) {
int nx = x + dx[j];
int ny = y + dy[j];
int np = ny * n + nx;
if (nx < 0 || nx >= n || ny < 0 || ny >= n)
continue;
if (d[np] > d[nowp] + k[nowp]) {
d[np] = d[nowp] + k[nowp];
q.emplace(np);
}
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int INF = INT32_MAX >> 2;
vector<bool> k(n * n, true);
vector<int> d(n * n, INF);
rep(i, n * n) {
int x = i % n;
int y = i / n;
chmin(d[i], x);
chmin(d[i], y);
chmin(d[i], n - x - 1);
chmin(d[i], n - y - 1);
}
int ans = 0;
rep(i, n * n) {
int p;
cin >> p;
p--;
ans += d[p];
k[p] = false;
queue<int> q;
q.emplace(p);
while (!q.empty()) {
int nowp = q.front();
q.pop();
int x = nowp % n;
int y = nowp / n;
rep(j, 4) {
int nx = x + dx[j];
int ny = y + dy[j];
int np = ny * n + nx;
if (nx < 0 || nx >= n || ny < 0 || ny >= n)
continue;
if (d[np] > d[nowp] + k[nowp]) {
d[np] = d[nowp] + k[nowp];
q.emplace(np);
}
}
}
}
cout << ans << endl;
} | replace | 25 | 26 | 25 | 26 | 0 | |
p02670 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using Pr = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll N;
ll P[250010];
ll h[250010];
bool used[250010];
ll bfs(ll i, ll j) {
ll ret = h[i * N + j];
ll nh = h[i * N + j] + used[i * N + j];
rep(d, 4) {
ll ni = i + dy[d], nj = j + dx[d];
if (nh < h[ni * N + nj]) {
h[ni * N + nj] = nh;
bfs(ni, nj);
}
}
return ret;
}
int main() {
std::cin >> N;
rep(i, N * N) std::cin >> P[i], --P[i];
rep(i, N * N) {
ll r = i / N, c = i % N;
h[i] = std::min({r, N - r - 1, c, N - c - 1});
used[i] = true;
}
ll ans = 0;
rep(i, N * N) {
ll r = P[i] / N;
ll c = P[i] % N;
used[r * N + c] = false;
ans += bfs(r, c);
}
std::cout << ans << std::endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using Pr = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll N;
ll P[250010];
ll h[250010];
bool used[250010];
ll bfs(ll i, ll j) {
ll ret = h[i * N + j];
ll nh = h[i * N + j] + used[i * N + j];
rep(d, 4) {
ll ni = i + dy[d], nj = j + dx[d];
if (ni < 0 || ni >= N || nj < 0 || nj >= N)
continue;
if (nh < h[ni * N + nj]) {
h[ni * N + nj] = nh;
bfs(ni, nj);
}
}
return ret;
}
int main() {
std::cin >> N;
rep(i, N * N) std::cin >> P[i], --P[i];
rep(i, N * N) {
ll r = i / N, c = i % N;
h[i] = std::min({r, N - r - 1, c, N - c - 1});
used[i] = true;
}
ll ans = 0;
rep(i, N * N) {
ll r = P[i] / N;
ll c = P[i] % N;
used[r * N + c] = false;
ans += bfs(r, c);
}
std::cout << ans << std::endl;
return 0;
} | insert | 73 | 73 | 73 | 76 | 0 | |
p02670 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
const ll INF = 1LL << 61;
const ll MOD = 1000000007 /*988244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vl A, B;
vl g[MAX_N];
vl visited(MAX_N, 0), used(MAX_N, 0);
vl D(MAX_N);
void solve() {
rep(i, n) {
rep(j, n - 1) {
g[i * n + j].pb(i * n + j + 1);
g[i * n + j + 1].pb(i * n + j);
g[(j + 1) * n + i].pb(j * n + i);
g[j * n + i].pb((j + 1) * n + i);
}
}
rep(i, n) {
rep(j, n) { D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j}); }
}
rep(i, n * n) {
x = A[i];
// cout << x << endl;
used[x] = 1;
p = 0;
queue<ll> Q;
Q.push(x);
vl K;
visited[x] = 1;
K.pb(x);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
for (ll x : g[v]) {
if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
visited[x] = 1;
K.pb(x);
Q.push(x);
D[x] = D[v] + 1;
}
if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
visited[x] = 1;
Q.push(x);
K.pb(x);
D[x] = D[v];
}
}
}
for (ll i : K)
visited[i]--;
/*rep(j, n) {
rep(k, n) { cout << D[j * n + k]; }
cout << " ";
rep(k, n) { cout << used[j * n + k]; }
cout << endl;
}*/
ans += D[x];
// cout << endl;
}
cout << ans << endl;
}
int main() {
// cout<<fixed<<setprecision(15);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
rep(i, n * n) {
cin >> a;
a--;
A.pb(a);
}
solve();
}
| #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
const ll INF = 1LL << 61;
const ll MOD = 1000000007 /*988244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vl A, B;
vl g[MAX_N];
vl visited(MAX_N, 0), used(MAX_N, 0);
vl D(MAX_N);
void solve() {
rep(i, n) {
rep(j, n - 1) {
g[i * n + j].pb(i * n + j + 1);
g[i * n + j + 1].pb(i * n + j);
g[(j + 1) * n + i].pb(j * n + i);
g[j * n + i].pb((j + 1) * n + i);
}
}
rep(i, n) {
rep(j, n) { D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j}); }
}
rep(i, n * n) {
x = A[i];
// cout << x << endl;
used[x] = 1;
p = 0;
queue<ll> Q;
Q.push(x);
vl K;
visited[x] = 1;
K.pb(x);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
for (ll x : g[v]) {
if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
visited[x] = 1;
K.pb(x);
Q.push(x);
D[x] = D[v] + 1;
}
if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
visited[x] = 1;
Q.push(x);
K.pb(x);
D[x] = D[v];
}
}
}
for (ll i : K)
visited[i]--;
/*rep(j, n) {
rep(k, n) { cout << D[j * n + k]; }
cout << " ";
rep(k, n) { cout << used[j * n + k]; }
cout << endl;
}*/
ans += D[x];
// cout << endl;
}
cout << ans << endl;
}
int main() {
// cout<<fixed<<setprecision(15);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
rep(i, n * n) {
cin >> a;
a--;
A.pb(a);
}
solve();
}
| replace | 3 | 4 | 3 | 4 | TLE | |
p02670 | C++ | Runtime Error | #include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <iterator> //集合演算(積集合,和集合,差集合など)
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
using namespace std;
typedef long long ll;
typedef struct {
int arr[3];
} xyc;
int main(void) {
int N;
cin >> N;
int aa[505];
for (size_t i = 0; i < N * N; i++) {
cin >> aa[i];
}
int mm[N][N];
for (size_t i = 0; i < (N + 1) / 2; i++) {
int j = N - i - 1;
for (size_t x = i; x <= j; x++) {
mm[i][x] = i;
mm[j][x] = i;
mm[x][i] = i;
mm[x][j] = i;
}
}
bool is_none[N][N];
for (size_t i = 0; i < N; i++) {
for (size_t j = 0; j < N; j++) {
is_none[i][j] = false;
}
}
// for(size_t i = 0; i < N; i++) {
// for(size_t j = 0; j < N; j++) {
// cout << mm[j][i] << " ";
// }
// cout << endl;
// }
long long ans = 0;
for (size_t i = 0; i < N * N; i++) {
int a = aa[i];
int x = (a - 1 + N) % N;
int y = (a - 1) / N;
// cout << a << " " << x << " " << y << endl;
ans += mm[y][x];
is_none[y][x] = true;
mm[y][x] -= 1;
queue<xyc> q;
xyc tmpp = {{x, y, mm[y][x]}};
q.push(tmpp);
while (not q.empty()) {
xyc tmp = q.front();
q.pop();
int x0 = tmp.arr[0];
int y0 = tmp.arr[1];
int n = tmp.arr[2];
// cout << x0 << " " << y0 << endl;
int xx[4] = {x0, x0, x0 + 1, x0 - 1};
int yy[4] = {y0 + 1, y0 - 1, y0, y0};
for (size_t k = 0; k < 4; k++) {
int xt = xx[k];
int yt = yy[k];
if (xt < 0 or N <= xt or yt < 0 or N <= yt) {
continue;
}
if (is_none[yt][xt] == true) {
if (mm[yt][xt] > n) {
mm[yt][xt] = n;
xyc azu = {{xt, yt, mm[yt][xt]}};
q.push(azu);
}
} else {
if (mm[yt][xt] > n + 1) {
mm[yt][xt] = n + 1;
xyc azu = {{xt, yt, mm[yt][xt]}};
q.push(azu);
}
}
}
}
// for(size_t i = 0; i < N; i++) {
// for(size_t j = 0; j < N; j++) {
// cout << mm[j][i] << " ";
// }
// cout << endl;
// }
// cout << endl;
}
cout << ans << endl;
}
| #include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <iterator> //集合演算(積集合,和集合,差集合など)
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
using namespace std;
typedef long long ll;
typedef struct {
int arr[3];
} xyc;
int main(void) {
int N;
cin >> N;
int aa[500 * 500 + 10];
for (size_t i = 0; i < N * N; i++) {
cin >> aa[i];
}
int mm[N][N];
for (size_t i = 0; i < (N + 1) / 2; i++) {
int j = N - i - 1;
for (size_t x = i; x <= j; x++) {
mm[i][x] = i;
mm[j][x] = i;
mm[x][i] = i;
mm[x][j] = i;
}
}
bool is_none[N][N];
for (size_t i = 0; i < N; i++) {
for (size_t j = 0; j < N; j++) {
is_none[i][j] = false;
}
}
// for(size_t i = 0; i < N; i++) {
// for(size_t j = 0; j < N; j++) {
// cout << mm[j][i] << " ";
// }
// cout << endl;
// }
long long ans = 0;
for (size_t i = 0; i < N * N; i++) {
int a = aa[i];
int x = (a - 1 + N) % N;
int y = (a - 1) / N;
// cout << a << " " << x << " " << y << endl;
ans += mm[y][x];
is_none[y][x] = true;
mm[y][x] -= 1;
queue<xyc> q;
xyc tmpp = {{x, y, mm[y][x]}};
q.push(tmpp);
while (not q.empty()) {
xyc tmp = q.front();
q.pop();
int x0 = tmp.arr[0];
int y0 = tmp.arr[1];
int n = tmp.arr[2];
// cout << x0 << " " << y0 << endl;
int xx[4] = {x0, x0, x0 + 1, x0 - 1};
int yy[4] = {y0 + 1, y0 - 1, y0, y0};
for (size_t k = 0; k < 4; k++) {
int xt = xx[k];
int yt = yy[k];
if (xt < 0 or N <= xt or yt < 0 or N <= yt) {
continue;
}
if (is_none[yt][xt] == true) {
if (mm[yt][xt] > n) {
mm[yt][xt] = n;
xyc azu = {{xt, yt, mm[yt][xt]}};
q.push(azu);
}
} else {
if (mm[yt][xt] > n + 1) {
mm[yt][xt] = n + 1;
xyc azu = {{xt, yt, mm[yt][xt]}};
q.push(azu);
}
}
}
}
// for(size_t i = 0; i < N; i++) {
// for(size_t j = 0; j < N; j++) {
// cout << mm[j][i] << " ";
// }
// cout << endl;
// }
// cout << endl;
}
cout << ans << endl;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02670 | C++ | Runtime Error | // AtCoder.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
// AtCoder.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#define _USE_MATH_DEFINES
#include <math.h>
// #include<cmath>
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
// #include "Ants.h"
using namespace std;
typedef long long ll;
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < n; i++)
#define show(s) cout << s << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define LINF (1LL << 50)
#define MOD (1e9 + 7)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
typedef pair<ll, ll> P;
ll gcd(ll a, ll b) { // 最大公約数を求める///最大公倍数a*b/gcd(a,b)
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcd(ll a, ll b) { // 最小公倍数を求める b=0のときaを返す
if (b == 0)
return a;
return (a / gcd(a, b)) * b;
}
vector<int> divnum(ll num) {
int dig;
vector<int> p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p;
}
int digiter(ll num) {
int dig;
vector<int> p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p.size();
}
vector<int> convertstring(string s) {
vector<int> d;
ll n = s.size();
rep(i, n) { d.push_back(s[i] - '0'); }
return d;
}
map<ll, int> prime_factor(ll n) {
map<ll, int> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
vector<ll> divisor(ll n) {
vector<ll> res;
for (ll i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
res.push_back(i);
if (i * i != n)
res.push_back(n / i);
}
return res;
}
ll myPow(ll x, ll n, ll m) { // x^n
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
std::vector<int> Eratosthenes(const int N) // 素数列挙
{
std::vector<bool> is_prime(N + 1);
for (int i = 0; i <= N; i++) {
is_prime[i] = true;
}
std::vector<int> P;
for (int i = 2; i <= N; i++) {
if (is_prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
is_prime[j] = false;
}
P.emplace_back(i);
}
}
return P;
}
const ll mod = MOD;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
// operator long long() const { return x; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct combination { // comb(a,b)でよぶaCb
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
} comb(1000005); // comb貯め
mint f(ll n, ll k) {
if (n < 0)
return 0;
// nPk = nCk * k!
mint res = comb(n, k);
res *= comb.fact[k];
return res;
}
/*int Q;
struct SegmentTree {
private:
int n;
vector<int> node;
public:
SegmentTree(vector<int> v) {//SegmentTree seg(vector<int>
v(N,INF))で呼び出し int sz = v.size(); n = 1; while (n < sz) n *= 2;
node.resize(2 * n - 1, INF);
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) node[i] = min(node[2 * i + 1],
node[2 * i + 2]);
}
void update(int x, int val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
if (r <= a || b <= l) return INF;
if (a <= l && r <= b) return node[k];
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
// 1-indexedなので注意。
struct BIT {
private:
vector<int> bit;
int N;
public:
BIT(int size) {
N = size;
bit.resize(N + 1);
}
// 一点更新です
void add(int a, int w) {//a番目をwに更新
for (int x = a; x <= N; x += x & -x) bit[x] += w;
}
// 1~Nまでの和を求める。
int sum(int a) {
int ret = 0;
for (int x = a; x > 0; x -= x & -x) ret += bit[x];
return ret;
}
};*/
/****Union-Ford-Tree***/
int par[int(0x7ffffff)]; // Parent
int Rank[int(0x7ffffff)]; // Deep_of_the_Tree
// int a[100] = { 4,2,3,1,5 }, m[100] ={3,2,2};
// int n = 0;
// const ll mod = 1000000007;
// n要素で初期化
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
Rank[i] = 0;
}
}
// 木の根探し
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (Rank[x] < Rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
/************************************************************************************/
int N;
P watcher[250005];
int mass[505][505];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dist[505][505];
void dfs(int x, int y) {
int z = dist[x][y] + mass[x][y];
rep(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (dist[sx][sy] > z) {
dist[sx][sy] = z;
dfs(sx, sy);
}
}
}
int32_t main() {
cin >> N;
rep(i, N * N) {
int p;
cin >> p;
p--;
watcher[i] = P(p / N, p % N);
}
ll ans = 0;
rep(i, N) fill(mass[i], mass[i] + N, 1);
rep(i, N) rep(j, N) {
dist[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
}
rep(i, N * N) {
P p = watcher[i];
mass[p.first][p.second] = 0;
ans += dist[p.first][p.second];
dfs(p.first, p.second);
}
cout << ans << endl;
}
| // AtCoder.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
// AtCoder.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#define _USE_MATH_DEFINES
#include <math.h>
// #include<cmath>
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
// #include "Ants.h"
using namespace std;
typedef long long ll;
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i, n) for (int i = 0; i < n; i++)
#define show(s) cout << s << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define LINF (1LL << 50)
#define MOD (1e9 + 7)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
typedef pair<ll, ll> P;
ll gcd(ll a, ll b) { // 最大公約数を求める///最大公倍数a*b/gcd(a,b)
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcd(ll a, ll b) { // 最小公倍数を求める b=0のときaを返す
if (b == 0)
return a;
return (a / gcd(a, b)) * b;
}
vector<int> divnum(ll num) {
int dig;
vector<int> p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p;
}
int digiter(ll num) {
int dig;
vector<int> p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p.size();
}
vector<int> convertstring(string s) {
vector<int> d;
ll n = s.size();
rep(i, n) { d.push_back(s[i] - '0'); }
return d;
}
map<ll, int> prime_factor(ll n) {
map<ll, int> ret;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
vector<ll> divisor(ll n) {
vector<ll> res;
for (ll i = 1; i * i <= n; i++) {
if (n % i != 0)
continue;
res.push_back(i);
if (i * i != n)
res.push_back(n / i);
}
return res;
}
ll myPow(ll x, ll n, ll m) { // x^n
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
std::vector<int> Eratosthenes(const int N) // 素数列挙
{
std::vector<bool> is_prime(N + 1);
for (int i = 0; i <= N; i++) {
is_prime[i] = true;
}
std::vector<int> P;
for (int i = 2; i <= N; i++) {
if (is_prime[i]) {
for (int j = 2 * i; j <= N; j += i) {
is_prime[j] = false;
}
P.emplace_back(i);
}
}
return P;
}
const ll mod = MOD;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
// operator long long() const { return x; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct combination { // comb(a,b)でよぶaCb
vector<mint> fact, ifact;
combination(ll n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
} comb(1000005); // comb貯め
mint f(ll n, ll k) {
if (n < 0)
return 0;
// nPk = nCk * k!
mint res = comb(n, k);
res *= comb.fact[k];
return res;
}
/*int Q;
struct SegmentTree {
private:
int n;
vector<int> node;
public:
SegmentTree(vector<int> v) {//SegmentTree seg(vector<int>
v(N,INF))で呼び出し int sz = v.size(); n = 1; while (n < sz) n *= 2;
node.resize(2 * n - 1, INF);
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) node[i] = min(node[2 * i + 1],
node[2 * i + 2]);
}
void update(int x, int val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = n;
if (r <= a || b <= l) return INF;
if (a <= l && r <= b) return node[k];
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
// 1-indexedなので注意。
struct BIT {
private:
vector<int> bit;
int N;
public:
BIT(int size) {
N = size;
bit.resize(N + 1);
}
// 一点更新です
void add(int a, int w) {//a番目をwに更新
for (int x = a; x <= N; x += x & -x) bit[x] += w;
}
// 1~Nまでの和を求める。
int sum(int a) {
int ret = 0;
for (int x = a; x > 0; x -= x & -x) ret += bit[x];
return ret;
}
};*/
/****Union-Ford-Tree***/
int par[int(0x7ffffff)]; // Parent
int Rank[int(0x7ffffff)]; // Deep_of_the_Tree
// int a[100] = { 4,2,3,1,5 }, m[100] ={3,2,2};
// int n = 0;
// const ll mod = 1000000007;
// n要素で初期化
void init(int n) {
for (int i = 0; i < n; i++) {
par[i] = i;
Rank[i] = 0;
}
}
// 木の根探し
int find(int x) {
if (par[x] == x) {
return x;
} else {
return par[x] = find(par[x]);
}
}
// xとyの属する集合を併合
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (Rank[x] < Rank[y]) {
par[x] = y;
} else {
par[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++;
}
}
bool same(int x, int y) { return find(x) == find(y); }
/************************************************************************************/
int N;
P watcher[250005];
int mass[505][505];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dist[505][505];
void dfs(int x, int y) {
int z = dist[x][y] + mass[x][y];
rep(i, 4) {
int sx = x + dx[i], sy = y + dy[i];
if (sx >= 0 && sx < N && sy >= 0 && sy < N) {
if (dist[sx][sy] > z) {
dist[sx][sy] = z;
dfs(sx, sy);
}
}
}
}
int32_t main() {
cin >> N;
rep(i, N * N) {
int p;
cin >> p;
p--;
watcher[i] = P(p / N, p % N);
}
ll ans = 0;
rep(i, N) fill(mass[i], mass[i] + N, 1);
rep(i, N) rep(j, N) {
dist[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
}
rep(i, N * N) {
P p = watcher[i];
mass[p.first][p.second] = 0;
ans += dist[p.first][p.second];
dfs(p.first, p.second);
}
cout << ans << endl;
}
| replace | 320 | 323 | 320 | 325 | -11 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i, n) for (i = 0; i < n; i++)
#define FOR(i, j, n) for (i = j; i < n; i++)
#define FORD(i, j, k) for (i = j; i >= k; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define she_told_me_dont_worry \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2
// source.cpp
inline long long MAX2(long long a, long long int b) {
return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
const int N = 505;
int n;
int grid[N][N] = {};
int l[N][N] = {};
int p[N * N];
int xx[5] = {0, 0, -1, 1};
int yy[5] = {-1, 1, 0, 0};
int dfs(int i, int d) {
int r = i / n, c = i % n;
int j;
FOR0(j, 4) {
int xd = r + xx[j], yd = c + yy[j];
if (xd >= 0 && xd < n && yd >= 0 && yd < n && d < grid[xd][yd]) {
grid[xd][yd] = d;
int nd = d;
if (!l[xd][yd])
nd++;
dfs(xd * n + yd, nd);
}
}
}
int main() {
she_told_me_dont_worry cin >> n;
int i, j;
FOR0(i, n * n) {
cin >> p[i];
p[i]--;
}
FOR0(i, n)
FOR0(j, n)
grid[i][j] = MIN2(MIN2(i, n - 1 - i), MIN2(j, n - 1 - j));
int ans = 0;
FOR0(i, n * n) {
int r = p[i] / n, c = p[i] % n;
l[r][c] = 1;
ans += grid[r][c];
dfs(p[i], grid[r][c]);
}
cout << ans << endl;
cerr << endl
<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
<< '\n';
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i, n) for (i = 0; i < n; i++)
#define FOR(i, j, n) for (i = j; i < n; i++)
#define FORD(i, j, k) for (i = j; i >= k; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define she_told_me_dont_worry \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2
// source.cpp
inline long long MAX2(long long a, long long int b) {
return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
const int N = 505;
int n;
int grid[N][N] = {};
int l[N][N] = {};
int p[N * N];
int xx[5] = {0, 0, -1, 1};
int yy[5] = {-1, 1, 0, 0};
void dfs(int i, int d) {
int r = i / n, c = i % n;
int j;
FOR0(j, 4) {
int xd = r + xx[j], yd = c + yy[j];
if (xd >= 0 && xd < n && yd >= 0 && yd < n && d < grid[xd][yd]) {
grid[xd][yd] = d;
int nd = d;
if (!l[xd][yd])
nd++;
dfs(xd * n + yd, nd);
}
}
}
int main() {
she_told_me_dont_worry cin >> n;
int i, j;
FOR0(i, n * n) {
cin >> p[i];
p[i]--;
}
FOR0(i, n)
FOR0(j, n)
grid[i][j] = MIN2(MIN2(i, n - 1 - i), MIN2(j, n - 1 - j));
int ans = 0;
FOR0(i, n * n) {
int r = p[i] / n, c = p[i] % n;
l[r][c] = 1;
ans += grid[r][c];
dfs(p[i], grid[r][c]);
}
cout << ans << endl;
cerr << endl
<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
<< '\n';
} | replace | 50 | 51 | 50 | 51 | 0 |
Time elapsed : 27.225 ms
|
p02670 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
int P[N * N];
for (int i = 0; i < N * N; i++)
cin >> P[i], P[i]--;
auto node = [&](int i, int j) { return N * i + j; };
auto ij = [&](int v) { return make_pair(v / N, v % N); };
pair<int, int> dir[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int cost[N * N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cost[node(i, j)] = min({i + 1, N - i, j + 1, N - j});
}
}
bool rem[N * N];
fill(rem, rem + N * N, true);
// debugArray(cost, N * N);
int ans = 0;
for (int p : P) {
rem[p] = false;
queue<pair<int, int>> que;
int pc = cost[p];
int pci, pcj;
tie(pci, pcj) = ij(p);
for (int d = 0; d < 4; d++) {
int ni = pci + dir[d].first, nj = pcj + dir[d].second;
int c = 0;
if (!(ni < 0 || N <= ni || nj < 0 || N <= nj))
c = cost[node(ni, nj)];
if (pc > c + rem[p]) {
pc = c + rem[p];
}
}
que.push({p, pc});
while (!que.empty()) {
int v, c;
tie(v, c) = que.front();
que.pop();
if (cost[v] < c)
continue;
cost[v] = c;
int ci, cj;
tie(ci, cj) = ij(v);
for (int d = 0; d < 4; d++) {
int ni = ci + dir[d].first, nj = cj + dir[d].second;
if (ni < 0 || N <= ni || nj < 0 || N <= nj)
continue;
int next = node(ni, nj);
if (cost[next] > cost[v] + rem[next])
que.push({next, cost[v] + rem[next]});
}
}
ans += cost[p];
// debugArray(cost, N * N);
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
int P[N * N];
for (int i = 0; i < N * N; i++)
cin >> P[i], P[i]--;
auto node = [&](int i, int j) { return N * i + j; };
auto ij = [&](int v) { return make_pair(v / N, v % N); };
pair<int, int> dir[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int cost[N * N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cost[node(i, j)] = min({i + 1, N - i, j + 1, N - j});
}
}
bool rem[N * N];
fill(rem, rem + N * N, true);
// debugArray(cost, N * N);
int ans = 0;
for (int p : P) {
rem[p] = false;
queue<pair<int, int>> que;
int pc = cost[p];
int pci, pcj;
tie(pci, pcj) = ij(p);
for (int d = 0; d < 4; d++) {
int ni = pci + dir[d].first, nj = pcj + dir[d].second;
int c = 0;
if (!(ni < 0 || N <= ni || nj < 0 || N <= nj))
c = cost[node(ni, nj)];
if (pc > c + rem[p]) {
pc = c + rem[p];
}
}
que.push({p, pc});
while (!que.empty()) {
int v, c;
tie(v, c) = que.front();
que.pop();
if (cost[v] <= c)
continue;
cost[v] = c;
int ci, cj;
tie(ci, cj) = ij(v);
for (int d = 0; d < 4; d++) {
int ni = ci + dir[d].first, nj = cj + dir[d].second;
if (ni < 0 || N <= ni || nj < 0 || N <= nj)
continue;
int next = node(ni, nj);
if (cost[next] > cost[v] + rem[next])
que.push({next, cost[v] + rem[next]});
}
}
ans += cost[p];
// debugArray(cost, N * N);
}
cout << ans << '\n';
return 0;
}
| replace | 60 | 61 | 60 | 61 | TLE | |
p02670 | C++ | Runtime Error |
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = int(5e2), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];
bool inline upd(int &a, int b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void inline bfs(int sx, int sy) {
static int qx[msz + 5], qy[msz + 5];
int ql = 0, qr = 0;
qx[qr++] = sx, qy[qr++] = sy;
for (; ql != qr;) {
int x = qx[ql++], y = qy[ql++];
loop(dr, 4) {
int cx = x + dx[dr], cy = y + dy[dr];
if (cx < 1 || cy < 1 || cx > n || cy > n ||
!upd(d[cx][cy], d[x][y] + !vis[x][y]))
continue;
qx[qr++] = cx, qy[qr++] = cy;
}
}
}
int main() {
scanf("%d", &n);
m = n * n;
int s = 0;
cont(i, n) cont(j, n) {
fm[++s] = make_pair(i, j);
d[i][j] = n;
upd(d[i][j], i - 1);
upd(d[i][j], j - 1);
upd(d[i][j], n - i);
upd(d[i][j], n - j);
}
cont(i, m) {
int id, x, y;
scanf("%d", &id);
x = fm[id].first, y = fm[id].second;
ans += d[x][y];
vis[x][y] = 1;
bfs(x, y);
}
printf("%lld\n", ans);
} |
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = int(1e3), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];
bool inline upd(int &a, int b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void inline bfs(int sx, int sy) {
static int qx[msz + 5], qy[msz + 5];
int ql = 0, qr = 0;
qx[qr++] = sx, qy[qr++] = sy;
for (; ql != qr;) {
int x = qx[ql++], y = qy[ql++];
loop(dr, 4) {
int cx = x + dx[dr], cy = y + dy[dr];
if (cx < 1 || cy < 1 || cx > n || cy > n ||
!upd(d[cx][cy], d[x][y] + !vis[x][y]))
continue;
qx[qr++] = cx, qy[qr++] = cy;
}
}
}
int main() {
scanf("%d", &n);
m = n * n;
int s = 0;
cont(i, n) cont(j, n) {
fm[++s] = make_pair(i, j);
d[i][j] = n;
upd(d[i][j], i - 1);
upd(d[i][j], j - 1);
upd(d[i][j], n - i);
upd(d[i][j], n - j);
}
cont(i, m) {
int id, x, y;
scanf("%d", &id);
x = fm[id].first, y = fm[id].second;
ans += d[x][y];
vis[x][y] = 1;
bfs(x, y);
}
printf("%lld\n", ans);
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02670 | C++ | Runtime Error | #include <stdio.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline char CH() {
static char buf[100000], *ptr1 = buf, *ptr2 = buf;
return ptr1 == ptr2 && (ptr2 = (ptr1 = buf) + fread(buf, 1, 100000, stdin),
ptr1 == ptr2)
? EOF
: *ptr1++;
}
static inline int IN(void) {
int x = 0, f = 0, c = CH();
while (c < 48 || c > 57) {
f ^= c == 45, c = CH();
}
while (c > 47 && c < 58) {
x = x * 10 + c - 48, c = CH();
}
return f ? -x : x;
}
static inline void OUT(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10);
putchar(x % 10 + 48);
}
static inline int min(const int a, const int b) { return a < b ? a : b; }
int main(void) {
int N = IN(), a = 0, across, d, down, p, q, xy, que[251501], vacant[251501],
dist[252525];
rep(i, N) {
xy = (N + 2) * (i + 1) + 1;
across = min(i, N - 1 - i), down = 3;
rep(j, across) { dist[xy++] = down++; }
rep(j, N - 2 * across) { dist[xy++] = across + 3; }
rep(j, across) { dist[xy++] = --down; }
}
rep(k, N * N) {
xy = IN();
xy += (xy - 1) / N * 2 + N + 2;
a += dist[xy];
p = 0, q = 0;
que[p++] = xy;
vacant[xy] = 1;
while (p > q) {
xy = que[q++], d = dist[xy] - vacant[xy] + 1;
if (dist[xy - N - 2] > d) {
dist[xy - N - 2] = d;
que[p++] = xy - N - 2;
}
if (dist[xy + N + 2] > d) {
dist[xy + N + 2] = d;
que[p++] = xy + N + 2;
}
if (dist[xy - 1] > d) {
dist[xy - 1] = d;
que[p++] = xy - 1;
}
if (dist[xy + 1] > d) {
dist[xy + 1] = d;
que[p++] = xy + 1;
}
}
}
OUT(a - 3 * N * N);
return puts(""), 0;
} | #include <stdio.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline char CH() {
static char buf[100000], *ptr1 = buf, *ptr2 = buf;
return ptr1 == ptr2 && (ptr2 = (ptr1 = buf) + fread(buf, 1, 100000, stdin),
ptr1 == ptr2)
? EOF
: *ptr1++;
}
static inline int IN(void) {
int x = 0, f = 0, c = CH();
while (c < 48 || c > 57) {
f ^= c == 45, c = CH();
}
while (c > 47 && c < 58) {
x = x * 10 + c - 48, c = CH();
}
return f ? -x : x;
}
static inline void OUT(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10);
putchar(x % 10 + 48);
}
static inline int min(const int a, const int b) { return a < b ? a : b; }
int main(void) {
int N = IN(), a = 0, across, d, down, p, q, xy, que[251501], vacant[252525],
dist[252525];
rep(i, N) {
xy = (N + 2) * (i + 1) + 1;
across = min(i, N - 1 - i), down = 3;
rep(j, across) { dist[xy++] = down++; }
rep(j, N - 2 * across) { dist[xy++] = across + 3; }
rep(j, across) { dist[xy++] = --down; }
}
rep(k, N * N) {
xy = IN();
xy += (xy - 1) / N * 2 + N + 2;
a += dist[xy];
p = 0, q = 0;
que[p++] = xy;
vacant[xy] = 1;
while (p > q) {
xy = que[q++], d = dist[xy] - vacant[xy] + 1;
if (dist[xy - N - 2] > d) {
dist[xy - N - 2] = d;
que[p++] = xy - N - 2;
}
if (dist[xy + N + 2] > d) {
dist[xy + N + 2] = d;
que[p++] = xy + N + 2;
}
if (dist[xy - 1] > d) {
dist[xy - 1] = d;
que[p++] = xy - 1;
}
if (dist[xy + 1] > d) {
dist[xy + 1] = d;
que[p++] = xy + 1;
}
}
}
OUT(a - 3 * N * N);
return puts(""), 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02670 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) \
for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7;
lint score[500][500], N;
bool isexist[500][500];
lint arr[250000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
REP(i, N * N) {
cin >> arr[i];
arr[i]--;
}
REP(i, N) {
REP(j, N) {
score[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
isexist[i][j] = true;
}
}
lint cnt = 0;
lint dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
REP(i, N * N) {
lint row = arr[i] / N, col = arr[i] % N;
cnt += score[row][col];
queue<plint> que;
que.push(plint(row, col));
isexist[row][col] = false;
while (!que.empty()) {
plint curr = que.front();
que.pop();
lint y = curr.first, x = curr.second;
REP(i, 4) {
lint nextx = x + dx[i], nexty = y + dy[i];
if (0 <= nexty && nexty < N && 0 <= nextx && nextx < N &&
((isexist[y][x] && score[nexty][nextx] >= score[y][x] + 1) ||
(!isexist[y][x] && score[nexty][nextx] > score[y][x]))) {
if (!isexist[y][x])
score[nexty][nextx] = score[y][x];
else
score[nexty][nextx] = score[y][x] + 1;
que.push(plint(nexty, nextx));
}
}
}
}
cout << cnt;
}
| #include "bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) \
for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7;
lint score[500][500], N;
bool isexist[500][500];
lint arr[250000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
REP(i, N * N) {
cin >> arr[i];
arr[i]--;
}
REP(i, N) {
REP(j, N) {
score[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
isexist[i][j] = true;
}
}
lint cnt = 0;
lint dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
REP(i, N * N) {
lint row = arr[i] / N, col = arr[i] % N;
cnt += score[row][col];
queue<plint> que;
que.push(plint(row, col));
isexist[row][col] = false;
while (!que.empty()) {
plint curr = que.front();
que.pop();
lint y = curr.first, x = curr.second;
REP(i, 4) {
lint nextx = x + dx[i], nexty = y + dy[i];
if (0 <= nexty && nexty < N && 0 <= nextx && nextx < N &&
((isexist[y][x] && score[nexty][nextx] > score[y][x] + 1) ||
(!isexist[y][x] && score[nexty][nextx] > score[y][x]))) {
if (!isexist[y][x])
score[nexty][nextx] = score[y][x];
else
score[nexty][nextx] = score[y][x] + 1;
que.push(plint(nexty, nextx));
}
}
}
}
cout << cnt;
}
| replace | 70 | 71 | 70 | 71 | TLE | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> P(505);
bool a[505][505];
int d[505][505];
void dfs(int i, int j, int dst) {
if (dst >= d[i][j])
return;
d[i][j] = dst;
if (i > 0)
dfs(i - 1, j, dst + (1 - a[i - 1][j]));
if (i < N - 1)
dfs(i + 1, j, dst + (1 - a[i + 1][j]));
if (j > 0)
dfs(i, j - 1, dst + (1 - a[i][j - 1]));
if (j < N - 1)
dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}
void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << d[i][j] << " ";
}
cout << "\n";
}
}
int main() {
cin >> N;
for (int i = 0; i < N * N; i++)
cin >> P[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
}
}
int s = 0;
for (int r = 0; r < N * N; r++) {
int i = (P[r] - 1) / N;
int j = (P[r] - 1) % N;
a[i][j] = true;
int dst = d[i][j] - 1;
if (i > 0)
dst = min(dst, d[i - 1][j]);
if (i < N - 1)
dst = min(dst, d[i + 1][j]);
if (j > 0)
dst = min(dst, d[i][j - 1]);
if (j < N - 1)
dst = min(dst, d[i][j + 1]);
s += dst;
// cout << r+1 << " " << i << " " << j << " " << dst << "\n";
dfs(i, j, dst);
// if (r == 7) print();
}
cout << s << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> P(500 * 500 + 5);
bool a[505][505];
int d[505][505];
void dfs(int i, int j, int dst) {
if (dst >= d[i][j])
return;
d[i][j] = dst;
if (i > 0)
dfs(i - 1, j, dst + (1 - a[i - 1][j]));
if (i < N - 1)
dfs(i + 1, j, dst + (1 - a[i + 1][j]));
if (j > 0)
dfs(i, j - 1, dst + (1 - a[i][j - 1]));
if (j < N - 1)
dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}
void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << d[i][j] << " ";
}
cout << "\n";
}
}
int main() {
cin >> N;
for (int i = 0; i < N * N; i++)
cin >> P[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
}
}
int s = 0;
for (int r = 0; r < N * N; r++) {
int i = (P[r] - 1) / N;
int j = (P[r] - 1) % N;
a[i][j] = true;
int dst = d[i][j] - 1;
if (i > 0)
dst = min(dst, d[i - 1][j]);
if (i < N - 1)
dst = min(dst, d[i + 1][j]);
if (j > 0)
dst = min(dst, d[i][j - 1]);
if (j < N - 1)
dst = min(dst, d[i][j + 1]);
s += dst;
// cout << r+1 << " " << i << " " << j << " " << dst << "\n";
dfs(i, j, dst);
// if (r == 7) print();
}
cout << s << "\n";
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));
int n, x, y, a[N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
void update(int x, int y) {
v[x][y] = 0;
queue<ar2> Q;
Q.push({x, y});
while (!Q.empty()) {
ar2 ele = Q.front();
Q.pop();
int x = ele[0], y = ele[1];
rep(i, 0, 3) {
int x1 = x + dx[i], y1 = y + dy[i];
if (dp[x][y] + v[x][y] < dp[x1][y1]) {
dp[x1][y1] = dp[x][y] + v[x][y];
Q.push({x1, y1});
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
scanf("%d", &n);
rep(i, 1, n * n) scanf("%d", a + i);
rep(i, 1, n) rep(j, 1, n) dp[i][j] =
min(min(i - 1, n - i), min(j - 1, n - j)),
v[i][j] = 1;
int ans = 0;
rep(q, 1, n * n) {
x = a[q] - 1;
y = x % n + 1, x = x / n + 1;
ans += dp[x][y];
update(x, y);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));
int n, x, y, a[N * N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
void update(int x, int y) {
v[x][y] = 0;
queue<ar2> Q;
Q.push({x, y});
while (!Q.empty()) {
ar2 ele = Q.front();
Q.pop();
int x = ele[0], y = ele[1];
rep(i, 0, 3) {
int x1 = x + dx[i], y1 = y + dy[i];
if (dp[x][y] + v[x][y] < dp[x1][y1]) {
dp[x1][y1] = dp[x][y] + v[x][y];
Q.push({x1, y1});
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
scanf("%d", &n);
rep(i, 1, n * n) scanf("%d", a + i);
rep(i, 1, n) rep(j, 1, n) dp[i][j] =
min(min(i - 1, n - i), min(j - 1, n - j)),
v[i][j] = 1;
int ans = 0;
rep(q, 1, n * n) {
x = a[q] - 1;
y = x % n + 1, x = x / n + 1;
ans += dp[x][y];
update(x, y);
}
cout << ans << endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf (1ll << 60)
#define BG(x) ((x).begin())
#define ED(x) ((x).end())
#define rBG(x) ((x).rbegin())
#define rED(x) ((x).rend())
#define mp(a, b) make_pair((a), (b))
ll t;
ll n;
ll p[505];
ll dist[505][505];
ll occ[505][505];
struct cmp {
bool operator()(const pair<ll, pair<ll, ll>> &a,
const pair<ll, pair<ll, ll>> &b) {
return a.first > b.first;
}
};
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, cmp> di;
bitset<400000> visited;
ll ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// cin >> t;
t = 1;
for (int _ = 1; _ <= t; _++) {
// cout << "Case #" << _ << ": ";
cin >> n;
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
dist[i][j] = llinf;
}
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
occ[i][j] = 1;
dist[i][j] = min(min(i, j) - 1, n - max(i, j));
}
}
for (int i = 0; i < n * n; i++) {
cin >> p[i];
p[i]--;
ans += dist[p[i] / n + 1][p[i] % n + 1];
occ[p[i] / n + 1][p[i] % n + 1] = 0;
di.push(mp(0, mp(p[i] / n + 1, p[i] % n + 1)));
visited.reset();
/* for(int i = 0; i < n+2; i++) {
for(int j = 0; j < n+2; j++) {
printf("%2lld ", dist[i][j]);
}
printf("\n");
}
printf("\n"); */
while (di.size()) {
auto now = di.top().second;
di.pop();
if (visited[now.first * (n + 1) + now.second])
continue;
visited[now.first * (n + 1) + now.second] = 1;
if (now.first > 1) {
// ll best[3]{dist[now.first][now.second-1],
//dist[now.first][now.second+1], dist[now.first+1][now.second]};
// sort(best, best + 3);
if (dist[now.first - 1][now.second] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first - 1][now.second] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first - 1][now.second],
mp(now.first - 1, now.second)));
}
}
if (now.second > 1) {
// ll best[3]{dist[now.first-1][now.second],
//dist[now.first+1][now.second], dist[now.first][now.second+1]};
// sort(best, best + 3);
if (dist[now.first][now.second - 1] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first][now.second - 1] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first][now.second - 1],
mp(now.first, now.second - 1)));
}
}
if (now.first < n) {
// ll best[3]{dist[now.first][now.second-1],
//dist[now.first][now.second+1], dist[now.first+1][now.second]};
// sort(best, best + 3);
if (dist[now.first + 1][now.second] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first + 1][now.second] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first + 1][now.second],
mp(now.first + 1, now.second)));
}
}
if (now.second < n) {
// ll best[3]{dist[now.first-1][now.second],
//dist[now.first+1][now.second], dist[now.first][now.second+1]};
// sort(best, best + 3);
if (dist[now.first][now.second + 1] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first][now.second + 1] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first][now.second + 1],
mp(now.first, now.second + 1)));
}
}
}
}
cout << ans << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf (1ll << 60)
#define BG(x) ((x).begin())
#define ED(x) ((x).end())
#define rBG(x) ((x).rbegin())
#define rED(x) ((x).rend())
#define mp(a, b) make_pair((a), (b))
ll t;
ll n;
ll p[300000];
ll dist[505][505];
ll occ[505][505];
struct cmp {
bool operator()(const pair<ll, pair<ll, ll>> &a,
const pair<ll, pair<ll, ll>> &b) {
return a.first > b.first;
}
};
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, cmp> di;
bitset<400000> visited;
ll ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// cin >> t;
t = 1;
for (int _ = 1; _ <= t; _++) {
// cout << "Case #" << _ << ": ";
cin >> n;
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
dist[i][j] = llinf;
}
}
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
occ[i][j] = 1;
dist[i][j] = min(min(i, j) - 1, n - max(i, j));
}
}
for (int i = 0; i < n * n; i++) {
cin >> p[i];
p[i]--;
ans += dist[p[i] / n + 1][p[i] % n + 1];
occ[p[i] / n + 1][p[i] % n + 1] = 0;
di.push(mp(0, mp(p[i] / n + 1, p[i] % n + 1)));
visited.reset();
/* for(int i = 0; i < n+2; i++) {
for(int j = 0; j < n+2; j++) {
printf("%2lld ", dist[i][j]);
}
printf("\n");
}
printf("\n"); */
while (di.size()) {
auto now = di.top().second;
di.pop();
if (visited[now.first * (n + 1) + now.second])
continue;
visited[now.first * (n + 1) + now.second] = 1;
if (now.first > 1) {
// ll best[3]{dist[now.first][now.second-1],
//dist[now.first][now.second+1], dist[now.first+1][now.second]};
// sort(best, best + 3);
if (dist[now.first - 1][now.second] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first - 1][now.second] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first - 1][now.second],
mp(now.first - 1, now.second)));
}
}
if (now.second > 1) {
// ll best[3]{dist[now.first-1][now.second],
//dist[now.first+1][now.second], dist[now.first][now.second+1]};
// sort(best, best + 3);
if (dist[now.first][now.second - 1] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first][now.second - 1] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first][now.second - 1],
mp(now.first, now.second - 1)));
}
}
if (now.first < n) {
// ll best[3]{dist[now.first][now.second-1],
//dist[now.first][now.second+1], dist[now.first+1][now.second]};
// sort(best, best + 3);
if (dist[now.first + 1][now.second] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first + 1][now.second] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first + 1][now.second],
mp(now.first + 1, now.second)));
}
}
if (now.second < n) {
// ll best[3]{dist[now.first-1][now.second],
//dist[now.first+1][now.second], dist[now.first][now.second+1]};
// sort(best, best + 3);
if (dist[now.first][now.second + 1] >
dist[now.first][now.second] + occ[now.first][now.second]) {
dist[now.first][now.second + 1] =
dist[now.first][now.second] + occ[now.first][now.second];
di.push(mp(dist[now.first][now.second + 1],
mp(now.first, now.second + 1)));
}
}
}
}
cout << ans << '\n';
}
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << " "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
// const int mod=998244353;
// int Pow(int x,int y){
// int ans=1;
// for (;y;y>>=1,x=(LL)x*x%mod)
// if (y&1)
// ans=(LL)ans*x%mod;
// return ans;
// }
// void Add(int &x,int y){
// if ((x+=y)>=mod)
// x-=mod;
// }
// void Del(int &x,int y){
// if ((x-=y)<0)
// x+=mod;
// }
// int Add(int x){
// return x>=mod?x-mod:x;
// }
// int Del(int x){
// return x<0?x+mod:x;
// }
const int N = 505;
int n;
int p[N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dis[N][N];
int check(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int alive[N][N];
int vis[N][N], tag = 0;
void upd(int x, int y) {
if (vis[x][y] == tag)
return;
vis[x][y] = tag;
For(k, 0, 3) {
int a = x + dx[k], b = y + dy[k];
if (check(a, b)) {
if (dis[x][y] + alive[a][b] < dis[a][b]) {
dis[a][b] = dis[x][y] + alive[a][b];
upd(a, b);
}
}
}
}
int main() {
n = read();
For(i, 1, n * n) p[i] = read();
For(i, 1, n) For(j, 1, n) {
dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
alive[i][j] = 1;
}
int ans = 0;
For(i, 1, n * n) {
int x = (p[i] - 1) / n + 1;
int y = (p[i] - 1) % n + 1;
dis[x][y]--;
alive[x][y] = 0;
ans += dis[x][y];
tag++;
upd(x, y);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << " "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R) \
cerr << #a "[" << L << ".." << R << "] = "; \
For(_x, L, R) cerr << a[_x] << " "; \
cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
LL x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f = ch == '-', ch = getchar();
while (isdigit(ch))
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
// const int mod=998244353;
// int Pow(int x,int y){
// int ans=1;
// for (;y;y>>=1,x=(LL)x*x%mod)
// if (y&1)
// ans=(LL)ans*x%mod;
// return ans;
// }
// void Add(int &x,int y){
// if ((x+=y)>=mod)
// x-=mod;
// }
// void Del(int &x,int y){
// if ((x-=y)<0)
// x+=mod;
// }
// int Add(int x){
// return x>=mod?x-mod:x;
// }
// int Del(int x){
// return x<0?x+mod:x;
// }
const int N = 505;
int n;
int p[N * N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dis[N][N];
int check(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int alive[N][N];
int vis[N][N], tag = 0;
void upd(int x, int y) {
if (vis[x][y] == tag)
return;
vis[x][y] = tag;
For(k, 0, 3) {
int a = x + dx[k], b = y + dy[k];
if (check(a, b)) {
if (dis[x][y] + alive[a][b] < dis[a][b]) {
dis[a][b] = dis[x][y] + alive[a][b];
upd(a, b);
}
}
}
}
int main() {
n = read();
For(i, 1, n * n) p[i] = read();
For(i, 1, n) For(j, 1, n) {
dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
alive[i][j] = 1;
}
int ans = 0;
For(i, 1, n * n) {
int x = (p[i] - 1) / n + 1;
int y = (p[i] - 1) % n + 1;
dis[x][y]--;
alive[x][y] = 0;
ans += dis[x][y];
tag++;
upd(x, y);
}
cout << ans << endl;
return 0;
} | replace | 57 | 58 | 57 | 58 | 0 | |
p02670 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef pair<int, int> Pa;
int const veci[4] = {0, -1, 0, 1};
int const vecj[4] = {-1, 0, 1, 0};
int N;
int P[25000];
int G[500][500];
int S[500][500];
int main() {
cin >> N;
rep(i, N * N) cin >> P[i];
rep(i, N) {
rep(j, N) { G[i][j] = min(min(i, j), min(N - i - 1, N - j - 1)); }
}
rep(i, N) fill(S[i], S[i] + N, 1);
// rep(i,N){rep(j,N){cout<<G[i][j];}cout<<endl;}cout<<endl;
int ans = 0;
rep(i, N * N) {
int i1 = (P[i] - 1) / N;
int j1 = (P[i] - 1) % N;
ans += G[i1][j1];
// cout<<"P="<<P[i]<<" ans="<<ans<<endl;
S[i1][j1] = 0;
queue<Pa> que;
que.push(Pa(i1, j1));
while (!que.empty()) {
Pa p = que.front();
que.pop();
// cout<<"i="<<p.first<<", j="<<p.second<<endl;
rep(k, 4) {
int i2 = p.first + veci[k];
int j2 = p.second + vecj[k];
if (0 <= i2 && i2 < N && 0 <= j2 && j2 < N) {
if (S[p.first][p.second] == 0 && G[p.first][p.second] < G[i2][j2]) {
G[i2][j2] = G[p.first][p.second];
que.push(Pa(i2, j2));
}
if (S[p.first][p.second] == 1 &&
G[p.first][p.second] + 1 < G[i2][j2]) {
G[i2][j2] = G[p.first][p.second] + 1;
que.push(Pa(i2, j2));
}
}
}
}
// rep(x,N){rep(y,N){cout<<G[x][y];}cout<<endl;}cout<<endl;
// rep(x,N){rep(y,N){cout<<S[x][y];}cout<<endl;}cout<<'\n'<<endl;
}
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef pair<int, int> Pa;
int const veci[4] = {0, -1, 0, 1};
int const vecj[4] = {-1, 0, 1, 0};
int N;
int P[250000];
int G[500][500];
int S[500][500];
int main() {
cin >> N;
rep(i, N * N) cin >> P[i];
rep(i, N) {
rep(j, N) { G[i][j] = min(min(i, j), min(N - i - 1, N - j - 1)); }
}
rep(i, N) fill(S[i], S[i] + N, 1);
// rep(i,N){rep(j,N){cout<<G[i][j];}cout<<endl;}cout<<endl;
int ans = 0;
rep(i, N * N) {
int i1 = (P[i] - 1) / N;
int j1 = (P[i] - 1) % N;
ans += G[i1][j1];
// cout<<"P="<<P[i]<<" ans="<<ans<<endl;
S[i1][j1] = 0;
queue<Pa> que;
que.push(Pa(i1, j1));
while (!que.empty()) {
Pa p = que.front();
que.pop();
// cout<<"i="<<p.first<<", j="<<p.second<<endl;
rep(k, 4) {
int i2 = p.first + veci[k];
int j2 = p.second + vecj[k];
if (0 <= i2 && i2 < N && 0 <= j2 && j2 < N) {
if (S[p.first][p.second] == 0 && G[p.first][p.second] < G[i2][j2]) {
G[i2][j2] = G[p.first][p.second];
que.push(Pa(i2, j2));
}
if (S[p.first][p.second] == 1 &&
G[p.first][p.second] + 1 < G[i2][j2]) {
G[i2][j2] = G[p.first][p.second] + 1;
que.push(Pa(i2, j2));
}
}
}
}
// rep(x,N){rep(y,N){cout<<G[x][y];}cout<<endl;}cout<<endl;
// rep(x,N){rep(y,N){cout<<S[x][y];}cout<<endl;}cout<<'\n'<<endl;
}
cout << ans << endl;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define MX 100005
#define mod 1000000007
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define F first
#define S second
// #define mp make_pair
#define inf 2e18
#define PI acos(-1)
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int n;
int p[2505];
int dp[505][505];
int ok[505][505];
// int mark[505][505][4];
int ans = 0;
int cur;
int getMin(int ro, int col) {
int res = n;
res = min(res, dp[ro - 1][col] + ok[ro - 1][col]);
res = min(res, dp[ro + 1][col] + ok[ro + 1][col]);
res = min(res, dp[ro][col - 1] + ok[ro][col - 1]);
res = min(res, dp[ro][col + 1] + ok[ro][col + 1]);
return res;
}
void process(int ro, int col);
void tryToUpdate(int ro, int col, int val) {
if (dp[ro][col] <= val)
return;
// cout<<"updated "<<ro<<" "<<col<<" "<<" val "<<val<<endl;
dp[ro][col] = val;
process(ro, col);
}
void process(int ro, int col) {
// cout<<"process "<<ro<<" "<<col<<endl;
int need = getMin(ro, col) + ok[ro][col];
if (ro > 1)
tryToUpdate(ro - 1, col, need);
if (ro < n)
tryToUpdate(ro + 1, col, need);
if (col > 1)
tryToUpdate(ro, col - 1, need);
if (col < n)
tryToUpdate(ro, col + 1, need);
}
int main() {
fast();
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n;
for (int i = 1; i <= n * n; i++)
cin >> p[i];
for (int i = 1; i <= n * n; i++) {
int ro = p[i] / n;
int col = p[i] % n;
if (col)
ro++;
else
col = n;
dp[ro][col] = min(min(ro - 1, n - ro), min(col - 1, n - col));
ok[ro][col] = 1;
}
for (int i = 1; i <= n * n; i++) {
int ro = p[i] / n;
int col = p[i] % n;
if (col)
ro++;
else
col = n;
ans += dp[ro][col];
ok[ro][col] = 0;
process(ro, col);
/*
cout<<p[i]<<" "<<ro<< " "<<col<<endl;
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++) cout<<dp[j][k]<<" ";
cout<<endl;
}
cout<<endl;
*/
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MX 100005
#define mod 1000000007
#define ll long long
#define ld long double
#define mem(a, b) memset(a, b, sizeof(a))
#define fast() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define F first
#define S second
// #define mp make_pair
#define inf 2e18
#define PI acos(-1)
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int n;
int p[250005];
int dp[505][505];
int ok[505][505];
// int mark[505][505][4];
int ans = 0;
int cur;
int getMin(int ro, int col) {
int res = n;
res = min(res, dp[ro - 1][col] + ok[ro - 1][col]);
res = min(res, dp[ro + 1][col] + ok[ro + 1][col]);
res = min(res, dp[ro][col - 1] + ok[ro][col - 1]);
res = min(res, dp[ro][col + 1] + ok[ro][col + 1]);
return res;
}
void process(int ro, int col);
void tryToUpdate(int ro, int col, int val) {
if (dp[ro][col] <= val)
return;
// cout<<"updated "<<ro<<" "<<col<<" "<<" val "<<val<<endl;
dp[ro][col] = val;
process(ro, col);
}
void process(int ro, int col) {
// cout<<"process "<<ro<<" "<<col<<endl;
int need = getMin(ro, col) + ok[ro][col];
if (ro > 1)
tryToUpdate(ro - 1, col, need);
if (ro < n)
tryToUpdate(ro + 1, col, need);
if (col > 1)
tryToUpdate(ro, col - 1, need);
if (col < n)
tryToUpdate(ro, col + 1, need);
}
int main() {
fast();
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n;
for (int i = 1; i <= n * n; i++)
cin >> p[i];
for (int i = 1; i <= n * n; i++) {
int ro = p[i] / n;
int col = p[i] % n;
if (col)
ro++;
else
col = n;
dp[ro][col] = min(min(ro - 1, n - ro), min(col - 1, n - col));
ok[ro][col] = 1;
}
for (int i = 1; i <= n * n; i++) {
int ro = p[i] / n;
int col = p[i] % n;
if (col)
ro++;
else
col = n;
ans += dp[ro][col];
ok[ro][col] = 0;
process(ro, col);
/*
cout<<p[i]<<" "<<ro<< " "<<col<<endl;
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++) cout<<dp[j][k]<<" ";
cout<<endl;
}
cout<<endl;
*/
}
cout << ans << endl;
return 0;
}
| replace | 22 | 23 | 22 | 23 | 0 | |
p02670 | C++ | Runtime Error | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
const int INF = 1000;
int ans[505][505];
bool used[505][505];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in_field(int h, int w) { return h >= 0 && h < N && w >= 0 && w < N; }
int p[50000];
int to_h(int idx) { return idx / N; }
int to_w(int idx) { return idx % N; }
int calc_max(int h, int w) {
int ret = INF;
for (int i = 0; i < 4; i++) {
if (in_field(h + dx[i], w + dy[i])) {
ret = min(ret, ans[h + dx[i]][w + dy[i]]);
} else {
ret = 0;
}
}
if (used[h][w])
ret++;
return ret;
}
void dfs(int h, int w) {
ans[h][w] = calc_max(h, w);
for (int i = 0; i < 4; i++) {
if (in_field(h + dx[i], w + dy[i])) {
if (calc_max(h + dx[i], w + dy[i]) < ans[h + dx[i]][w + dy[i]])
dfs(h + dx[i], w + dy[i]);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> N;
for (int i = 0; i < N * N; i++) {
int s;
cin >> s;
s--;
p[i] = s;
}
ll ret = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
used[i][j] = true;
ans[i][j] = min({i, j, (N - i - 1), (N - j - 1)}) + 1;
}
}
for (int i = 0; i < N * N; i++) {
int idx = p[i];
int h = to_h(idx);
int w = to_w(idx);
ret += ans[h][w];
used[h][w] = false;
dfs(h, w);
// for(int j = 0; j < N; j++){
// for(int k = 0; k < N; k++){
// cout << ans[j][k] << ' ';
// }
// cout << endl;
// }
// cout << endl;
}
cout << ret - N * N << endl;
} | #include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
const int INF = 1000;
int ans[505][505];
bool used[505][505];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in_field(int h, int w) { return h >= 0 && h < N && w >= 0 && w < N; }
int p[250005];
int to_h(int idx) { return idx / N; }
int to_w(int idx) { return idx % N; }
int calc_max(int h, int w) {
int ret = INF;
for (int i = 0; i < 4; i++) {
if (in_field(h + dx[i], w + dy[i])) {
ret = min(ret, ans[h + dx[i]][w + dy[i]]);
} else {
ret = 0;
}
}
if (used[h][w])
ret++;
return ret;
}
void dfs(int h, int w) {
ans[h][w] = calc_max(h, w);
for (int i = 0; i < 4; i++) {
if (in_field(h + dx[i], w + dy[i])) {
if (calc_max(h + dx[i], w + dy[i]) < ans[h + dx[i]][w + dy[i]])
dfs(h + dx[i], w + dy[i]);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> N;
for (int i = 0; i < N * N; i++) {
int s;
cin >> s;
s--;
p[i] = s;
}
ll ret = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
used[i][j] = true;
ans[i][j] = min({i, j, (N - i - 1), (N - j - 1)}) + 1;
}
}
for (int i = 0; i < N * N; i++) {
int idx = p[i];
int h = to_h(idx);
int w = to_w(idx);
ret += ans[h][w];
used[h][w] = false;
dfs(h, w);
// for(int j = 0; j < N; j++){
// for(int k = 0; k < N; k++){
// cout << ans[j][k] << ' ';
// }
// cout << endl;
// }
// cout << endl;
}
cout << ret - N * N << endl;
} | replace | 19 | 20 | 19 | 20 | 0 | |
p02670 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
int a[N][N], n, ans, p[N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
for (int i = 0; i < 4; ++i) {
int nx = x + d[i][0], ny = y + d[i][1];
if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
a[nx][ny] = a[x][y] + (!vis[x][y]);
dfs(nx, ny);
}
}
}
int main() {
n = read();
for (int i = 1; i <= n * n; ++i) {
p[i] = read();
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
}
}
for (int i = 1; i <= n * n; ++i) {
int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
ans += a[x][y];
vis[x][y] = 1;
for (int k = 0; k < 4; ++k) {
int nx = x + d[k][0], ny = y + d[k][1];
if (check(nx, ny) && a[nx][ny] > a[x][y]) {
a[nx][ny] = a[x][y], dfs(nx, ny);
}
}
}
printf("%d\n", ans);
return 0;
}
| #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
int a[N][N], n, ans, p[N * N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
for (int i = 0; i < 4; ++i) {
int nx = x + d[i][0], ny = y + d[i][1];
if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
a[nx][ny] = a[x][y] + (!vis[x][y]);
dfs(nx, ny);
}
}
}
int main() {
n = read();
for (int i = 1; i <= n * n; ++i) {
p[i] = read();
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
}
}
for (int i = 1; i <= n * n; ++i) {
int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
ans += a[x][y];
vis[x][y] = 1;
for (int k = 0; k < 4; ++k) {
int nx = x + d[k][0], ny = y + d[k][1];
if (check(nx, ny) && a[nx][ny] > a[x][y]) {
a[nx][ny] = a[x][y], dfs(nx, ny);
}
}
}
printf("%d\n", ans);
return 0;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC
const int N = 507;
int dist[N][N];
vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
bad[i][j] = 1;
}
}
int ans = 0;
for (int t = 0; t < n * n; ++t) {
int who;
cin >> who;
--who;
int i = who / n;
int j = who % n;
bad[i][j] = 0;
ans += dist[i][j];
queue<ii> q;
q.push(mp(i, j));
while (q.size()) {
auto p = q.front();
q.pop();
int i = p.f, j = p.s;
for (auto e : v) {
int x = i + e.f, y = j + e.s;
if (dist[i][j] + bad[i][j] < dist[x][y]) {
dist[x][y] = dist[i][j] + bad[i][j];
q.push(mp(x, y));
}
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC
const int N = 507;
int dist[N][N];
vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
bad[i][j] = 1;
}
}
int ans = 0;
for (int t = 0; t < n * n; ++t) {
int who;
cin >> who;
--who;
int i = who / n;
int j = who % n;
bad[i][j] = 0;
ans += dist[i][j];
queue<ii> q;
q.push(mp(i, j));
while (q.size()) {
auto p = q.front();
q.pop();
int i = p.f, j = p.s;
for (auto e : v) {
int x = i + e.f, y = j + e.s;
if (0 <= x && x < n && 0 <= y && y < n &&
dist[i][j] + bad[i][j] < dist[x][y]) {
dist[x][y] = dist[i][j] + bad[i][j];
q.push(mp(x, y));
}
}
}
}
cout << ans << endl;
} | replace | 56 | 57 | 56 | 58 | -11 | |
p02670 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;
typedef tuple<ll, ll, ll> tl;
typedef tuple<ll, ll, ll, ll> ql;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define f first
#define s second
#define ins insert
#define e pb
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front
#define db cout << "YEET\n";
#define ct(x) cout << x << '\n';
const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 10 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
int main() {
FAST ll grid[MAXN][MAXN];
bool left[MAXN][MAXN];
ll n;
cin >> n;
FOR(i, 1, n) {
FOR(j, 1, n) {
ll vert = min(i - 1, n - i);
ll horz = min(j - 1, n - j);
ll minimum = min(vert, horz);
grid[i][j] = minimum;
left[i][j] = 0;
}
}
deque<ii> bfs;
ll ans = 0;
ll c1[4] = {-1, 0, 0, 1};
ll c2[4] = {0, -1, 1, 0};
FOR(i, 1, n * n) {
ll x;
cin >> x;
x--;
ll row = x / n + 1;
ll col = x % n + 1;
ans += grid[row][col];
grid[row][col]--;
left[row][col] = 1;
bfs.eb(row, col);
while (!bfs.empty()) {
ll r = bfs.front().f;
ll c = bfs.front().s;
bfs.ppf();
FOR(j, 0, 3) {
ll newr = r + c1[j];
ll newc = c + c2[j];
if (newr == 0 || newc == 0 || newr > n || newc > n)
continue;
if (grid[r][c] + 1 < grid[newr][newc] && !left[newr][newc]) {
grid[newr][newc] = grid[r][c] + 1;
bfs.eb(newr, newc);
} else if (left[newr][newc] && grid[r][c] < grid[newr][newc]) {
grid[newr][newc] = grid[r][c];
bfs.ef(newr, newc);
}
}
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ld, ld> id;
typedef tuple<ll, ll, ll> tl;
typedef tuple<ll, ll, ll, ll> ql;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define FILL(x, n, v) fill(x, x + n, v);
#define ALL(x) x.begin(), x.end()
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define f first
#define s second
#define ins insert
#define e pb
#define eb emplace_back
#define ef emplace_front
#define p push
#define pf push_front
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ft front
#define bk back
#define pp pop
#define ppb pop_back
#define ppf pop_front
#define db cout << "YEET\n";
#define ct(x) cout << x << '\n';
const ll MOD = 1e9 + 7; // 998244353
const ll MAXN = 500 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
int main() {
FAST ll grid[MAXN][MAXN];
bool left[MAXN][MAXN];
ll n;
cin >> n;
FOR(i, 1, n) {
FOR(j, 1, n) {
ll vert = min(i - 1, n - i);
ll horz = min(j - 1, n - j);
ll minimum = min(vert, horz);
grid[i][j] = minimum;
left[i][j] = 0;
}
}
deque<ii> bfs;
ll ans = 0;
ll c1[4] = {-1, 0, 0, 1};
ll c2[4] = {0, -1, 1, 0};
FOR(i, 1, n * n) {
ll x;
cin >> x;
x--;
ll row = x / n + 1;
ll col = x % n + 1;
ans += grid[row][col];
grid[row][col]--;
left[row][col] = 1;
bfs.eb(row, col);
while (!bfs.empty()) {
ll r = bfs.front().f;
ll c = bfs.front().s;
bfs.ppf();
FOR(j, 0, 3) {
ll newr = r + c1[j];
ll newc = c + c2[j];
if (newr == 0 || newc == 0 || newr > n || newc > n)
continue;
if (grid[r][c] + 1 < grid[newr][newc] && !left[newr][newc]) {
grid[newr][newc] = grid[r][c] + 1;
bfs.eb(newr, newc);
} else if (left[newr][newc] && grid[r][c] < grid[newr][newc]) {
grid[newr][newc] = grid[r][c];
bfs.ef(newr, newc);
}
}
}
}
cout << ans;
} | replace | 42 | 43 | 42 | 43 | 0 | |
p02670 | C++ | Time Limit Exceeded | #line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
template <class T> void say(bool val, T yes = "Yes", T no = "No") {
cout << (val ? yes : no) << "\n";
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
#line 2 "agc044_b.cpp"
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vi> grid(n, vi(n, n));
{
queue<pair<int, int>> q;
REP(i, n) {
grid[0][i] = 0;
grid[i][0] = 0;
grid[n - 1][i] = 0;
grid[i][n - 1] = 0;
q.push({0, i});
q.push({i, 0});
q.push({n - 1, i});
q.push({i, n - 1});
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
REP(k, 4) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
if (grid[nx][ny] > grid[x][y] + 1) {
chmin(grid[nx][ny], grid[x][y] + 1);
q.push({nx, ny});
}
}
}
}
}
int ans = 0;
vector<vector<bool>> empty(n, vector<bool>(n, false));
REP(i, n * n) {
int cur;
cin >> cur;
cur--;
int cx = cur / n, cy = cur % n;
ans += grid[cx][cy];
empty[cx][cy] = true;
queue<pair<int, int>> que;
que.push({cx, cy});
while (!que.empty()) {
auto [x, y] = que.front();
que.pop();
REP(k, 4) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
if (grid[nx][ny] > grid[x][y]) {
chmin(grid[nx][ny], grid[x][y] + (empty[x][y] ? 0 : 1));
que.push({nx, ny});
}
}
}
}
}
cout << ans << endl;
return 0;
}
| #line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
template <class T> void say(bool val, T yes = "Yes", T no = "No") {
cout << (val ? yes : no) << "\n";
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
#line 2 "agc044_b.cpp"
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vi> grid(n, vi(n, n));
{
queue<pair<int, int>> q;
REP(i, n) {
grid[0][i] = 0;
grid[i][0] = 0;
grid[n - 1][i] = 0;
grid[i][n - 1] = 0;
q.push({0, i});
q.push({i, 0});
q.push({n - 1, i});
q.push({i, n - 1});
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
REP(k, 4) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
if (grid[nx][ny] > grid[x][y] + 1) {
chmin(grid[nx][ny], grid[x][y] + 1);
q.push({nx, ny});
}
}
}
}
}
int ans = 0;
vector<vector<bool>> empty(n, vector<bool>(n, false));
REP(i, n * n) {
int cur;
cin >> cur;
cur--;
int cx = cur / n, cy = cur % n;
ans += grid[cx][cy];
empty[cx][cy] = true;
queue<pair<int, int>> que;
que.push({cx, cy});
while (!que.empty()) {
auto [x, y] = que.front();
que.pop();
REP(k, 4) {
int nx = x + dx[k], ny = y + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
if (grid[nx][ny] > grid[x][y] + (empty[x][y] ? 0 : 1)) {
chmin(grid[nx][ny], grid[x][y] + (empty[x][y] ? 0 : 1));
que.push({nx, ny});
}
}
}
}
}
cout << ans << endl;
return 0;
}
| replace | 86 | 88 | 86 | 87 | TLE | |
p02670 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];
void update(int x, int y) {
int xx, yy, t;
head = 0;
tail = 1;
memset(B, 0, sizeof(B));
list[1][1] = x;
list[1][2] = y;
B[x][y] = 1;
while (head < tail) {
head++;
x = list[head][1];
y = list[head][2];
for (t = 1; t <= 4; t++) {
xx = x + fx1[t];
yy = y + fx2[t];
if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
if (B[xx][yy] == 0) {
B[xx][yy] = 1;
tail++;
list[tail][1] = xx;
list[tail][2] = yy;
}
}
}
B[x][y] = 0;
}
}
int main() {
// freopen("read.in","r",stdin);
fx1[1] = 1;
fx1[2] = -1;
fx1[3] = 0;
fx1[4] = 0;
fx2[1] = 0;
fx2[2] = 0;
fx2[3] = 1;
fx2[4] = -1;
scanf("%d", &n);
for (i = 1; i <= n * n; i++)
scanf("%d", &p[i]);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
}
}
for (i = 1; i <= n * n; i++) {
x = (p[i] - 1) / n + 1;
y = (p[i] - 1) % n + 1;
bz[x][y] = 1;
dis[x][y]--;
ans += dis[x][y];
update(x, y);
}
printf("%lld", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];
void update(int x, int y) {
int xx, yy, t;
head = 0;
tail = 1;
list[1][1] = x;
list[1][2] = y;
B[x][y] = 1;
while (head < tail) {
head++;
x = list[head][1];
y = list[head][2];
for (t = 1; t <= 4; t++) {
xx = x + fx1[t];
yy = y + fx2[t];
if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
if (B[xx][yy] == 0) {
B[xx][yy] = 1;
tail++;
list[tail][1] = xx;
list[tail][2] = yy;
}
}
}
B[x][y] = 0;
}
}
int main() {
// freopen("read.in","r",stdin);
fx1[1] = 1;
fx1[2] = -1;
fx1[3] = 0;
fx1[4] = 0;
fx2[1] = 0;
fx2[2] = 0;
fx2[3] = 1;
fx2[4] = -1;
scanf("%d", &n);
for (i = 1; i <= n * n; i++)
scanf("%d", &p[i]);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
}
}
for (i = 1; i <= n * n; i++) {
x = (p[i] - 1) / n + 1;
y = (p[i] - 1) % n + 1;
bz[x][y] = 1;
dis[x][y]--;
ans += dis[x][y];
update(x, y);
}
printf("%lld", ans);
return 0;
} | delete | 16 | 17 | 16 | 16 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.