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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02576 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, X, T;
cin >> N >> X >> T;
int count;
if (N == X) {
cout << T << endl;
}
else if ((N / X) % 0) {
cout << (N / X) * T << endl;
}
else if (X != 1) {
count = N / X;
cout << (count + 1) * T << endl;
}
else {
cout << N * T << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, X, T;
cin >> N >> X >> T;
int count;
if (N == X) {
cout << T << endl;
}
else if (N % X == 0) {
cout << (N / X) * T << endl;
}
else if (X != 1) {
count = N / X;
cout << (count + 1) * T << endl;
}
else {
cout << N * T << endl;
}
} | replace | 13 | 14 | 13 | 14 | -8 | |
p02576 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef vector<string> vsi;
typedef map<ll, ll> mape;
#define rep(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define per(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define fastio \
{ \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
}
#define N 1000010
#define MAX 1234567890
ll solve() {
ll n, x, t;
cin >> n >> x >> t;
cout << (n + x - 1) / x * t << endl;
}
int main() {
fastio;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef vector<string> vsi;
typedef map<ll, ll> mape;
#define rep(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define per(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define fastio \
{ \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
}
#define N 1000010
#define MAX 1234567890
void solve() {
ll n, x, t;
cin >> n >> x >> t;
cout << (n + x - 1) / x * t << endl;
}
int main() {
fastio;
solve();
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02576 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n, x, t, c = 0;
cin >> n >> x >> t;
while (n > 0) {
n -= x;
c++;
}
return c * t;
} | #include <iostream>
using namespace std;
int main() {
int n, x, t, c = 0;
cin >> n >> x >> t;
while (n > 0) {
n -= x;
c++;
}
cout << c * t << endl;
return 0;
} | replace | 9 | 10 | 9 | 11 | 12 | |
p02576 | Python | Runtime Error | import math
N, X, T = map(int(input().split()))
print(T * math.ceil(N / X))
| import math
N, X, T = map(int, input().split())
print(T * math.ceil(N / X))
| replace | 2 | 3 | 2 | 3 | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02576/Python/s954741282.py", line 2, in <module>
N, X, T = map(int(input().split()))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
|
p02576 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n, x, t;
cin >> n >> x >> t;
cout << (n / (x - 1) * t + t) << endl;
}
| #include <iostream>
using namespace std;
int main() {
int n, x, t;
cin >> n >> x >> t;
int ans = 0;
ans += n / x * t + !!(n - n / x * x) * t;
cout << ans << endl;
}
| replace | 6 | 7 | 6 | 9 | 0 | |
p02577 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[200001];
int n;
int main() {
scanf("%s", a);
for (int i = 0; i < strlen(a); i++) {
n += a[i] - 48;
}
// printf("%d\n",n);
if (n % 9 == 0) {
printf("Yes");
} else {
printf("No");
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[200001];
int n;
int main() {
scanf("%s", a);
for (int i = 0; i < 200001; i++) {
if (a[i] == 0) {
continue;
} else {
n += a[i] - 48;
}
}
// printf("%d\n",n);
if (n % 9 == 0) {
printf("Yes");
} else {
printf("No");
}
return 0;
}
| replace | 9 | 11 | 9 | 15 | TLE | |
p02577 | Python | Runtime Error | N = input()
tmp = 0
while N > 0:
tmp += N % 10
N /= 10
if tmp % 9 == 0:
print("Yes")
else:
print("No")
| print("No" if int(input()) % 9 else "Yes")
| replace | 0 | 9 | 0 | 1 | TypeError: '>' not supported between instances of 'str' and 'int' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02577/Python/s474365797.py", line 3, in <module>
while N > 0:
TypeError: '>' not supported between instances of 'str' and 'int'
|
p02577 | Python | Time Limit Exceeded | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
total = 0
while n:
total += n % 10
n //= 10
if total % 9 == 0:
print("Yes")
else:
print("No")
| import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
if n % 9 == 0:
print("Yes")
else:
print("No")
| replace | 7 | 13 | 7 | 8 | TLE | |
p02577 | Python | Time Limit Exceeded | n = int(input())
s = 0
for i in range(len(str(n))):
s += n % 10
n //= 10
s %= 9
print("Yes" if s == 0 else "No")
| n = int(input())
print("Yes" if n % 9 == 0 else "No")
| replace | 2 | 10 | 2 | 3 | TLE | |
p02577 | Python | Runtime Error | def main():
num = input()
if num % 9 == 0:
return "Yes"
else:
return "No"
if __name__ == "__main__":
ans = main()
print(ans)
| def main():
num = int(input())
if num % 9 == 0:
return "Yes"
else:
return "No"
if __name__ == "__main__":
ans = main()
print(ans)
| replace | 1 | 2 | 1 | 2 | TypeError: not all arguments converted during string formatting | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02577/Python/s351043563.py", line 10, in <module>
ans = main()
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02577/Python/s351043563.py", line 3, in main
if num % 9 == 0:
TypeError: not all arguments converted during string formatting
|
p02577 | Python | Time Limit Exceeded | N = int(input())
s = 0
while N != 0:
s = s + N % 10
N = N // 10
if s % 9 == 0:
print("Yes")
else:
print("No")
| N = int(input())
s = 0
s = sum(list(map(int, str(N))))
if s % 9 == 0:
print("Yes")
else:
print("No")
| replace | 3 | 6 | 3 | 4 | TLE | |
p02577 | C++ | Runtime Error | #include <stdio.h>
#include <string.h>
int main() {
char s[100000];
long long int i, j, p = 0, a, k;
scanf("%s", s);
a = strlen(s); // printf("%d",a);
for (i = 0; i < a; i++) {
p = p + (s[i] - '0');
p = p % 9;
}
if (p % 9 == 0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
| #include <stdio.h>
#include <string.h>
int main() {
char s[1000000];
long long int i, j, p = 0, a, k;
scanf("%s", s);
a = strlen(s); // printf("%d",a);
for (i = 0; i < a; i++) {
p = p + (s[i] - '0');
p = p % 9;
}
if (p % 9 == 0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02577 | C++ | Runtime Error |
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define IOS \
ios::sync_with_stdio(); \
cin.tie(0);
#define tc() \
int tc; \
scanf("%d", &tc); \
while (tc--)
#define endl "\n"
#define FLOAT_COM(a, b) if (abs(a - b) == 1e-9)
#define LOOP(a, b) for (i = a; i < b; i++)
#define VI vector<int>
#define VLL vector<long long>
#define PB push_back
#define MP make_pair
using namespace std;
// int i,j,k,l,temp;
void shomoy() {
#ifndef ONLINE_JUDGE
cerr << "\nTime :" << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
#endif
}
bool isprime(int n) {
int f;
for (f = 2; f * f <= n; f++) {
if (n % f == 0)
return false;
}
return true;
}
/*Driver Code Startes Here*/
int main() {
IOS int n, i, j, count = 0;
char line[100000];
cin >> line;
n = strlen(line);
for (i = 0; i < n; i++) {
count += (line[i] - '0');
}
if (count % 9) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
// shomoy();
return 0;
} |
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define IOS \
ios::sync_with_stdio(); \
cin.tie(0);
#define tc() \
int tc; \
scanf("%d", &tc); \
while (tc--)
#define endl "\n"
#define FLOAT_COM(a, b) if (abs(a - b) == 1e-9)
#define LOOP(a, b) for (i = a; i < b; i++)
#define VI vector<int>
#define VLL vector<long long>
#define PB push_back
#define MP make_pair
using namespace std;
// int i,j,k,l,temp;
void shomoy() {
#ifndef ONLINE_JUDGE
cerr << "\nTime :" << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
#endif
}
bool isprime(int n) {
int f;
for (f = 2; f * f <= n; f++) {
if (n % f == 0)
return false;
}
return true;
}
/*Driver Code Startes Here*/
int main() {
IOS int i, n, count = 0;
char line[10000000];
cin >> line;
n = strlen(line);
for (i = 0; i < n; i++) {
count += (line[i] - '0');
}
if (count % 9) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
// shomoy();
return 0;
} | replace | 46 | 48 | 46 | 48 | 0 | |
p02577 | Python | Time Limit Exceeded | n = int(input())
sum = 0
while n > 0:
sum += n % 10
n = n // 10
if sum % 9 == 0:
print("Yes")
else:
print("No")
| n = int(input())
if n % 9 == 0:
print("Yes")
else:
print("No")
| replace | 1 | 6 | 1 | 2 | TLE | |
p02577 | Python | Time Limit Exceeded | N = int(input())
sum = 0
while N > 0:
sum += N % 10
N = N // 10
if sum % 9 == 0:
print("Yes")
else:
print("No")
| N = input()
s = [char for char in N]
sum_ = 0
for char in s:
sum_ += int(char)
if sum_ % 9 == 0:
print("Yes")
else:
print("No")
| replace | 0 | 6 | 0 | 6 | TLE | |
p02577 | C++ | Runtime Error | #include <cstring>
#include <iostream>
using namespace std;
char s[100009];
int main() {
cin >> s;
long long sum = 0;
for (int i = 0; i < strlen(s); i++)
sum += int(s[i] - '0');
if (sum % 9 == 0)
cout << "Yes";
else {
cout << "No";
}
system("pause");
} | #include <cstring>
#include <iostream>
using namespace std;
char s[10000009];
int main() {
cin >> s;
long long sum = 0;
for (int i = 0; i < strlen(s); i++)
sum += int(s[i] - '0');
if (sum % 9 == 0)
cout << "Yes";
else {
cout << "No";
}
system("pause");
} | replace | 3 | 4 | 3 | 4 | 0 | sh: 1: pause: not found
|
p02577 | C++ | Runtime Error | // #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 1e5 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
ll kr = 1, xs = 0;
char ls;
ls = getchar();
while (!isdigit(ls)) {
if (!(ls ^ 45))
kr = -1;
ls = getchar();
}
while (isdigit(ls)) {
xs = (xs << 1) + (xs << 3) + (ls ^ 48);
ls = getchar();
}
return xs * kr;
}
// inline __int128 read128()
// {
// __int128 kr=1,xs=0;char ls;ls=getchar();
// while(!isdigit(ls)){
// if(!(ls^45)) kr=-1;ls=getchar();
// }
// while(isdigit(ls)){
// xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
// }
// return xs*kr;
// }
inline ull readull() {
ull xs = 0;
char ls;
ls = getchar();
while (!isdigit(ls))
ls = getchar();
while (isdigit(ls)) {
xs = (xs << 1) + (xs << 3) + (ls ^ 48);
ls = getchar();
}
return xs;
}
inline void writeull(ull x) {
if (x < 0) {
x *= -1;
putchar('-');
}
if (x >= 10)
writeull(x / 10);
putchar('0' + x % 10);
}
inline void write(ll x) {
if (x < 0) {
x *= -1;
putchar('-');
}
if (x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
// inline void write(__int128 x){
// if(x<0){x*=-1;putchar('-');}
// if(x>=10) write(x/10);
// putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
char a[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// t = read();
while (t--) {
scanf("%s", a);
int now = 0;
for (int i = 0; a[i]; i++) {
now += (a[i] - '0');
now %= 9;
}
if (!now)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| // #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 9;
const int infint = 1e9 + 7;
const int MAXN = 2e5 + 7;
const double eps = 1e-9;
const long double pi = 3.1415926535897932384626433832795;
const long double e = 2.71828182845904523536028747135266;
inline ll read() {
ll kr = 1, xs = 0;
char ls;
ls = getchar();
while (!isdigit(ls)) {
if (!(ls ^ 45))
kr = -1;
ls = getchar();
}
while (isdigit(ls)) {
xs = (xs << 1) + (xs << 3) + (ls ^ 48);
ls = getchar();
}
return xs * kr;
}
// inline __int128 read128()
// {
// __int128 kr=1,xs=0;char ls;ls=getchar();
// while(!isdigit(ls)){
// if(!(ls^45)) kr=-1;ls=getchar();
// }
// while(isdigit(ls)){
// xs=(xs<<1)+(xs<<3)+(ls^48);ls=getchar();
// }
// return xs*kr;
// }
inline ull readull() {
ull xs = 0;
char ls;
ls = getchar();
while (!isdigit(ls))
ls = getchar();
while (isdigit(ls)) {
xs = (xs << 1) + (xs << 3) + (ls ^ 48);
ls = getchar();
}
return xs;
}
inline void writeull(ull x) {
if (x < 0) {
x *= -1;
putchar('-');
}
if (x >= 10)
writeull(x / 10);
putchar('0' + x % 10);
}
inline void write(ll x) {
if (x < 0) {
x *= -1;
putchar('-');
}
if (x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
// inline void write(__int128 x){
// if(x<0){x*=-1;putchar('-');}
// if(x>=10) write(x/10);
// putchar('0'+x%10);
// }
///////////////////////////////////////////////////////////////////////////////////////
char a[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// t = read();
while (t--) {
scanf("%s", a);
int now = 0;
for (int i = 0; a[i]; i++) {
now += (a[i] - '0');
now %= 9;
}
if (!now)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02577 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int a;
char c[200005];
int sum = 1;
while (cin >> c[sum])
sum++;
int ans = 0;
for (int i = 1; i <= sum; i++) {
int num = (int)(c[i] - '0');
sum += num;
}
if (sum % 9 == 0)
cout << "Yes";
else
cout << "No";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
string n;
cin >> n;
int sum = 0;
for (int i = 0; i < n.length(); i++) {
sum += (n[i] - '0');
}
if (sum % 9 == 0)
cout << "Yes";
else
cout << "No";
return 0;
}
| replace | 3 | 12 | 3 | 8 | 0 | |
p02577 | C++ | Runtime Error | #include <iostream>
#include <string>
using namespace std;
int main() {
char s[100000];
unsigned long long int n = 0;
cin >> s;
for (int i = 0; s[i] != '\0'; i++) {
n += s[i] - '0';
n %= 9;
}
if (n)
cout << "No";
else
cout << "Yes";
return 0;
} | #include <iostream>
#include <string>
using namespace std;
int main() {
char s[300000];
unsigned long long int n = 0;
cin >> s;
for (int i = 0; s[i] != '\0'; i++) {
n += s[i] - '0';
n %= 9;
}
if (n)
cout << "No";
else
cout << "Yes";
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02577 | Python | Time Limit Exceeded | N = input()
cnt = 0
for s in N:
cnt += int(N)
if cnt % 9 == 0:
print("Yes")
else:
print("No")
| N = input()
cnt = 0
for s in N:
cnt += int(s)
if cnt % 9 == 0:
print("Yes")
else:
print("No")
| replace | 4 | 5 | 4 | 5 | TLE | |
p02577 | Python | Runtime Error | N = input()
ans = 0
while N:
ans += int(N[:-1])
ans = ans % 9
N = N[:-1]
if ans == 0:
print("Yes")
else:
print("No")
| N = input()
ans = 0
while N:
ans += int(N[-1])
ans = ans % 9
N = N[:-1]
if ans == 0:
print("Yes")
else:
print("No")
| replace | 3 | 4 | 3 | 4 | ValueError: invalid literal for int() with base 10: '' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02577/Python/s521471712.py", line 4, in <module>
ans += int(N[:-1])
ValueError: invalid literal for int() with base 10: ''
|
p02577 | C++ | Runtime Error | #include <stdio.h>
#include <string.h>
int main() {
int i, sm = 0;
char s[100000];
scanf("%s", s);
for (i = 0; i < strlen(s); i++)
sm += s[i] - '0';
if (sm % 9 == 0)
printf("Yes\n");
else
printf("No\n");
} | #include <stdio.h>
#include <string.h>
int main() {
int i, sm = 0;
char s[2000009];
scanf("%s", s);
for (i = 0; i < strlen(s); i++)
sm += s[i] - '0';
if (sm % 9 == 0)
printf("Yes\n");
else
printf("No\n");
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02577 | C++ | Time Limit Exceeded | #include <cstdio>
#include <cstring>
using namespace std;
int sum;
char ch[200005];
int main() {
scanf("%s", ch);
for (int i = 0; i < (int)strlen(ch); ++i)
sum += ch[i] - '0';
if (sum % 9)
printf("No\n");
else
printf("Yes\n");
return 0;
} | #include <cstdio>
#include <cstring>
using namespace std;
int sum;
char ch[200005];
int main() {
scanf("%s", ch);
int t = (int)strlen(ch);
for (int i = 0; i < t; ++i)
sum += ch[i] - '0';
if (sum % 9)
printf("No\n");
else
printf("Yes\n");
return 0;
} | replace | 7 | 8 | 7 | 9 | TLE | |
p02577 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
char s[100005];
int main() {
scanf("%s", s);
int len = strlen(s);
int m, a;
m = 0;
for (int i = 0; i < len; i++) {
a = s[i] - '0';
m += a;
}
if (m % 9 == 0)
cout << "Yes";
else
cout << "No";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
char s[100000005];
int main() {
scanf("%s", s);
int len = strlen(s);
int m, a;
m = 0;
for (int i = 0; i < len; i++) {
a = s[i] - '0';
m += a;
}
if (m % 9 == 0)
cout << "Yes";
else
cout << "No";
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02577 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 14, lg = 15;
/*
######################################################################
####################### THE BIG INT ##########################
*/
const int base = 1000000000;
const int base_digits = 9;
struct bigint {
vector<int> a;
int sign;
/*<arpa>*/
int size() {
if (a.empty())
return 0;
int ans = (a.size() - 1) * base_digits;
int ca = a.back();
while (ca)
ans++, ca /= 10;
return ans;
}
bigint operator^(const bigint &v) {
bigint ans = 1, a = *this, b = v;
while (!b.isZero()) {
if (b % 2)
ans *= a;
a *= a, b /= 2;
}
return ans;
}
string to_string() {
stringstream ss;
ss << *this;
string s;
ss >> s;
return s;
}
int sumof() {
string s = to_string();
int ans = 0;
for (auto c : s)
ans += c - '0';
return ans;
}
/*</arpa>*/
bigint() : sign(1) {}
bigint(long long v) { *this = v; }
bigint(const string &s) { read(s); }
void operator=(const bigint &v) {
sign = v.sign;
a = v.a;
}
void operator=(long long v) {
sign = 1;
a.clear();
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) || carry;
++i) {
if (i == (int)res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int)v.a.size() || carry; ++i) {
res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i) {
if (i == (int)a.size())
a.push_back(0);
long long cur = a[i] * (long long)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
// asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
void operator*=(long long v) {
if (v < 0)
sign = -sign, v = -v;
if (v > base) {
*this = *this * (v / base) * base + *this * (v % base);
return;
}
for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i) {
if (i == (int)a.size())
a.push_back(0);
long long cur = a[i] * (long long)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
// asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
}
trim();
}
bigint operator*(long long v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((long long)base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const { return divmod(*this, v).first; }
bigint operator%(const bigint &v) const { return divmod(*this, v).second; }
void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int)a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long)base;
a[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long)base) % v;
return m * sign;
}
void operator+=(const bigint &v) { *this = *this + v; }
void operator-=(const bigint &v) { *this = *this - v; }
void operator*=(const bigint &v) { *this = *this * v; }
void operator/=(const bigint &v) { *this = *this / v; }
bool operator<(const bigint &v) const {
if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const { return v < *this; }
bool operator<=(const bigint &v) const { return !(v < *this); }
bool operator>=(const bigint &v) const { return !(*this < v); }
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const { return *this < v || v < *this; }
void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}
bool isZero() const { return a.empty() || (a.size() == 1 && !a[0]); }
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
long long longValue() const {
long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) {
return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream &operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream &operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int)v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits,
int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int)p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int)a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}
typedef vector<long long> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++)
a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int)a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++)
r[i] -= a2b2[i];
for (int i = 0; i < (int)r.size(); i++)
res[i + k] += r[i];
for (int i = 0; i < (int)a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int)c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int)(cur % 1000000));
carry = (int)(cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
int main() {
bigint n;
while (cin >> n) {
long long sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
if (sum % 9 == 0) {
cout << "Yes";
} else {
cout << "No";
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 14, lg = 15;
/*
######################################################################
####################### THE BIG INT ##########################
*/
const int base = 1000000000;
const int base_digits = 9;
struct bigint {
vector<int> a;
int sign;
/*<arpa>*/
int size() {
if (a.empty())
return 0;
int ans = (a.size() - 1) * base_digits;
int ca = a.back();
while (ca)
ans++, ca /= 10;
return ans;
}
bigint operator^(const bigint &v) {
bigint ans = 1, a = *this, b = v;
while (!b.isZero()) {
if (b % 2)
ans *= a;
a *= a, b /= 2;
}
return ans;
}
string to_string() {
stringstream ss;
ss << *this;
string s;
ss >> s;
return s;
}
int sumof() {
string s = to_string();
int ans = 0;
for (auto c : s)
ans += c - '0';
return ans;
}
/*</arpa>*/
bigint() : sign(1) {}
bigint(long long v) { *this = v; }
bigint(const string &s) { read(s); }
void operator=(const bigint &v) {
sign = v.sign;
a = v.a;
}
void operator=(long long v) {
sign = 1;
a.clear();
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) || carry;
++i) {
if (i == (int)res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int)v.a.size() || carry; ++i) {
res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i) {
if (i == (int)a.size())
a.push_back(0);
long long cur = a[i] * (long long)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
// asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
void operator*=(long long v) {
if (v < 0)
sign = -sign, v = -v;
if (v > base) {
*this = *this * (v / base) * base + *this * (v % base);
return;
}
for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i) {
if (i == (int)a.size())
a.push_back(0);
long long cur = a[i] * (long long)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
// asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
}
trim();
}
bigint operator*(long long v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((long long)base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const { return divmod(*this, v).first; }
bigint operator%(const bigint &v) const { return divmod(*this, v).second; }
void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int)a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long)base;
a[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long)base) % v;
return m * sign;
}
void operator+=(const bigint &v) { *this = *this + v; }
void operator-=(const bigint &v) { *this = *this - v; }
void operator*=(const bigint &v) { *this = *this * v; }
void operator/=(const bigint &v) { *this = *this / v; }
bool operator<(const bigint &v) const {
if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const { return v < *this; }
bool operator<=(const bigint &v) const { return !(v < *this); }
bool operator>=(const bigint &v) const { return !(*this < v); }
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const { return *this < v || v < *this; }
void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}
bool isZero() const { return a.empty() || (a.size() == 1 && !a[0]); }
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
long long longValue() const {
long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) {
return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream &operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream &operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int)v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits,
int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int)p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int)a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}
typedef vector<long long> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++)
a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int)a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++)
r[i] -= a2b2[i];
for (int i = 0; i < (int)r.size(); i++)
res[i + k] += r[i];
for (int i = 0; i < (int)a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int)c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int)(cur % 1000000));
carry = (int)(cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
int main() {
bigint n;
while (cin >> n) {
long long sum = 0;
sum = n.sumof();
if (sum % 9 == 0) {
cout << "Yes";
} else {
cout << "No";
}
}
} | replace | 391 | 396 | 391 | 392 | TLE | |
p02577 | C++ | Runtime Error | //| #######################|
//| ADARSH SINGH KUSHWAHA |
//| ###################### |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Bahut_tej ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frb(i, a, b) for (ll i = a - 1; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define mp map<ll, ll>
#define v vector<ll>
#define srt(v) sort(v.begin(), v.end())
#define mod 1000000007
#define max 1000001
bool prime[max];
void seive() {
memset(prime, true, sizeof(prime));
prime[0] = false;
prime[1] = false;
for (int i = 2; i * i <= max; i++) {
if (prime[i]) {
for (int j = i * i; j <= max; j += i) {
prime[j] = false;
}
}
}
}
void solve() {
string s;
cin >> s;
ll n = s.size();
ll sum = 0;
for (ll i = 0; i < n; i++) {
sum += (s[i - '0']);
}
if (sum % 9 == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
Bahut_tej;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// int t;
// cin>>t;
// while(t--){
solve();
//}
return 0;
}
| //| #######################|
//| ADARSH SINGH KUSHWAHA |
//| ###################### |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Bahut_tej ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define frb(i, a, b) for (ll i = a - 1; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define mp map<ll, ll>
#define v vector<ll>
#define srt(v) sort(v.begin(), v.end())
#define mod 1000000007
#define max 1000001
bool prime[max];
void seive() {
memset(prime, true, sizeof(prime));
prime[0] = false;
prime[1] = false;
for (int i = 2; i * i <= max; i++) {
if (prime[i]) {
for (int j = i * i; j <= max; j += i) {
prime[j] = false;
}
}
}
}
void solve() {
string s;
cin >> s;
ll n = s.size();
ll sum = 0;
for (ll i = 0; i < n; i++) {
sum += (s[i] - '0');
}
if (sum % 9 == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
Bahut_tej;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// int t;
// cin>>t;
// while(t--){
solve();
//}
return 0;
}
| replace | 36 | 37 | 36 | 37 | 0 | |
p02577 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define nl endl
#define in cin
#define out cout
#define ll long long
#define charToInt(c) (c - '0')
int main() {
ll a, b, c = 0, d, i, j;
char s[1000];
in >> s;
for (i = 0; s[i]; i++) {
s[i] = s[i] - 48;
c += s[i];
}
if (c % 9 == 0) {
cout << "Yes" << nl;
} else {
cout << "No" << nl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define nl endl
#define in cin
#define out cout
#define ll long long
#define charToInt(c) (c - '0')
int main() {
ll a, b, c = 0, d, i, j;
string s;
in >> s;
for (i = 0; s[i]; i++) {
s[i] = s[i] - 48;
c += s[i];
}
if (c % 9 == 0) {
cout << "Yes" << nl;
} else {
cout << "No" << nl;
}
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02577 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
char S[20010];
int main() {
cin >> S;
int len = strlen(S);
long long sum = 0;
for (int i = 0; i < len; i++) {
sum += S[i] - '0';
}
if (sum % 9 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
;
} | #include <bits/stdc++.h>
using namespace std;
char S[200100];
int main() {
cin >> S;
int len = strlen(S);
long long sum = 0;
for (int i = 0; i < len; i++) {
sum += S[i] - '0';
}
if (sum % 9 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02577 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long int ll;
int main() {
char s[100000];
scanf("%s", s);
ll sum = 0;
rep(i, strlen(s)) sum += s[i] - '0';
if (sum % 9 == 0)
printf("Yes");
else
printf("No");
} | #include <bits/stdc++.h>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long int ll;
int main() {
char s[200000];
scanf("%s", s);
ll sum = 0;
rep(i, strlen(s)) sum += s[i] - '0';
if (sum % 9 == 0)
printf("Yes");
else
printf("No");
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02577 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define FUP(i, x, y) for (int i = (x); i <= (y); i++)
#define FDW(i, x, y) for (int i = (x); i >= (y); i--)
#define MAXN 100010
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define ll long long
#define db double
using namespace std;
int read() {
int w = 0, flg = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
flg = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
w = w * 10 - '0' + ch, ch = getchar();
}
return w * flg;
}
int sum;
char s[20010];
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
FUP(i, 1, len) sum += s[i] - '0';
sum % 9 == 0 ? puts("Yes") : puts("No");
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define FUP(i, x, y) for (int i = (x); i <= (y); i++)
#define FDW(i, x, y) for (int i = (x); i >= (y); i--)
#define MAXN 100010
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define ll long long
#define db double
using namespace std;
int read() {
int w = 0, flg = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
flg = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
w = w * 10 - '0' + ch, ch = getchar();
}
return w * flg;
}
int sum;
char s[200010];
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
FUP(i, 1, len) sum += s[i] - '0';
sum % 9 == 0 ? puts("Yes") : puts("No");
return 0;
} | replace | 28 | 29 | 28 | 29 | 0 | |
p02577 | C++ | Runtime Error | #include <stdio.h>
int main() {
char S[100100];
scanf("%s", S);
int s = 0;
for (int i = 0; S[i]; i++)
s += S[i] - '0';
puts(s % 9 == 0 ? "Yes" : "No");
return 0;
} | #include <stdio.h>
int main() {
char S[200100];
scanf("%s", S);
int s = 0;
for (int i = 0; S[i]; i++)
s += S[i] - '0';
puts(s % 9 == 0 ? "Yes" : "No");
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02577 | Python | Time Limit Exceeded | N = int(input())
summer = 0
while N > 0:
digit = N % 10
summer += digit
N = N // 10
if summer % 9 == 0:
print("Yes")
else:
print("No")
| N = int(input())
if N % 9 == 0:
print("Yes")
else:
print("No")
| replace | 1 | 8 | 1 | 2 | TLE | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
int n, a[200000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
long long ans = 0;
for (int i = n - 2; i >= 0; i++) {
long long up = max(0, a[i + 1] - a[i]);
a[i] += up, ans += up;
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
int n, a[200000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
long long ans = 0;
for (int i = 1; i < n; i++) {
long long up = max(0, a[i - 1] - a[i]);
a[i] += up, ans += up;
}
cout << ans << '\n';
return 0;
}
| replace | 15 | 17 | 15 | 17 | -11 | |
p02578 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long int n, x[2000001], t, ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> x[i];
for (int i = 1; i < n; i++) {
for (int j = i - 1; j >= 0; j--)
if (t < x[j])
t = x[j];
if (x[i] < t) {
ans += t - x[i];
x[i] += t - x[i];
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
long long int n, x[2000001], t, ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> x[i];
for (int i = 0; i < n; i++) {
if (t < x[i])
t = x[i];
if (x[i] < t) {
ans += t - x[i];
x[i] += t - x[i];
}
}
cout << ans;
} | replace | 7 | 11 | 7 | 10 | TLE | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[101010], n, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[202020], n, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
}
cout << ans << '\n';
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5 + 5;
const ll MOD = 1e9 + 7;
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pis pair<ll, string>
#define F first
#define S second
#define LCM(a, b) ((a * b) / __gcd(a, b))
#define inf 1e15
#define test \
ll cse; \
cin >> cse; \
for (ll _i = 1; _i <= cse; _i++)
#define PI 3.14159265
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const double EPS = 1E-9;
typedef vector<vector<double>> matrix;
typedef vector<int> vi;
ll ar[N];
int main() {
fast;
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> ar[i];
ll sum = 0, m = ar[0];
for (int i = 1; i < n; i++) {
m = max(m, ar[i]);
sum += (m - ar[i]);
}
cout << sum << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 5;
const ll MOD = 1e9 + 7;
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pis pair<ll, string>
#define F first
#define S second
#define LCM(a, b) ((a * b) / __gcd(a, b))
#define inf 1e15
#define test \
ll cse; \
cin >> cse; \
for (ll _i = 1; _i <= cse; _i++)
#define PI 3.14159265
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
const double EPS = 1E-9;
typedef vector<vector<double>> matrix;
typedef vector<int> vi;
ll ar[N];
int main() {
fast;
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> ar[i];
ll sum = 0, m = ar[0];
for (int i = 1; i < n; i++) {
m = max(m, ar[i]);
sum += (m - ar[i]);
}
cout << sum << "\n";
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
int main(void) {
ll n, mx = 0, sum = 0;
scanf("%lld", &n);
for (ll i = 1; i <= n; i++)
scanf("%lld", &a[i]);
mx = a[1];
for (ll i = 2; i <= n; i++) {
if (mx < a[i])
mx = a[i];
sum += mx - a[i];
}
printf("%lld", sum);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200005];
int main(void) {
ll n, mx = 0, sum = 0;
scanf("%lld", &n);
for (ll i = 1; i <= n; i++)
scanf("%lld", &a[i]);
mx = a[1];
for (ll i = 2; i <= n; i++) {
if (mx < a[i])
mx = a[i];
sum += mx - a[i];
}
printf("%lld", sum);
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02578 | Python | Runtime Error | N = input()
tmp = 0
while N > 0:
tmp += N % 10
N /= 10
if tmp % 9 == 0:
print("Yes")
else:
print("No")
| # # Make IO faster
# import sys
# input = sys.stdin.readline
# # get single (or) multiple str
# X = input()
# # get single int
# N = int(input())
# # get multiple int (e.g., 2)
# X, Y = map(int, input().split())
# # get multiple int (e.g., 2) for N lines
# XY = [list(map(int, input().split())) for _ in range(N)]
# from IPython import embed; embed(); exit();
# 全部入り
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input():
return sys.stdin.readline().strip()
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST():
return list(map(int, input().split()))
def ZIP(n):
return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10**9)
INF = float("inf")
mod = 10**9 + 7
N = INT()
A = MAP()
prev = -INF
ans = 0
for a in A:
if a < prev:
ans += prev - a
else:
prev = a
print(ans)
| replace | 0 | 9 | 0 | 65 | TypeError: '>' not supported between instances of 'str' and 'int' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02578/Python/s628445536.py", line 3, in <module>
while N > 0:
TypeError: '>' not supported between instances of 'str' and 'int'
|
p02578 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main(void) {
int n, ans;
int h[100010];
cin >> n;
for (int i = 0; i < n; i++)
cin >> h[i];
ans = 0;
for (int i = 1; i < n; i++) {
if (h[i - 1] > h[i]) {
ans += h[i - 1] - h[i];
h[i] = h[i - 1];
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
using namespace std;
int main(void) {
int n;
long long ans;
int h[200010];
cin >> n;
for (int i = 0; i < n; i++)
cin >> h[i];
ans = 0;
for (int i = 1; i < n; i++) {
if (h[i - 1] > h[i]) {
ans += h[i - 1] - h[i];
h[i] = h[i - 1];
}
}
cout << ans << endl;
return 0;
} | replace | 4 | 6 | 4 | 7 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
int sum = 0;
for (int i = 1; i < N + 1; i++) {
if (vec.at(i - 1) > vec.at(i)) {
sum += (vec.at(i - 1) - vec.at(i));
vec.at(i) = vec.at(i - 1);
}
}
cout << sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
int64_t sum = 0;
for (int i = 1; i < N; i++) {
if (vec.at(i - 1) > vec.at(i)) {
sum += (vec.at(i - 1) - vec.at(i));
vec.at(i) = vec.at(i - 1);
}
}
cout << sum << endl;
}
| replace | 10 | 12 | 10 | 12 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)
|
p02578 | C++ | Runtime Error | //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef pair<int, pint> ppint;
typedef pair<ll, pll> ppll;
typedef vector<int> vint;
typedef vector<ll> vll;
const double pi = 3.141592653589793;
const int INF10 = 1000000001;
const ll INF15 = 1e15 + 1;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;
const double EPS = 0.00001;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (ll)(n); i++)
#define rep2(i, start, end) for (int i = (ll)start; i <= (ll)end; i++)
#define vrep(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (ll)n; i > 0; i--)
#define all(n) n.begin(), n.end()
#define pb push_back
#define debug(x) cerr << #x << ": " << x << '\n'
#define arep(it, a) for (auto it : a)
struct edge {
edge(int s, int e, long long c) {
to = e;
from = s;
cost = c;
}
edge(int i, long long c = 1) { edge(-1, i, c); }
int from;
int to;
long cost;
};
typedef vector<edge> edges;
// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし
// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
int m = 0;
for (m; (1 << m) <= n; m++)
;
m = m - 1;
return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は0)(O(log n))
long myPow(long x, long n, long m = mod) {
if (n < 0)
return 0;
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;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
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; }
bool operator!=(const int a) { return this->x != a; }
bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <class T> bool maxin(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool minin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class M, class N> common_type_t<M, N> mygcd(M a, N b) {
a = abs(a);
b = abs(b);
if (a < b)
return mygcd(b, a);
M r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
template <class M, class N> common_type_t<M, N> mylcm(M a, N b) {
return (a / mygcd(a, b)) * b;
}
const int N_MAX = 100005;
int n;
int a[N_MAX];
void Main() {
int x = 0, y = INF10, z = 1;
// 入力
cin >> n;
rep(i, n) cin >> a[i];
// 処理
ll ans = 0, now = a[0];
rep1(i, n - 1) {
if (now >= a[i]) {
ans += now - a[i];
} else {
now = a[i];
}
}
// 出力
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
cout << fixed << setprecision(12);
Main();
} | //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef pair<int, pint> ppint;
typedef pair<ll, pll> ppll;
typedef vector<int> vint;
typedef vector<ll> vll;
const double pi = 3.141592653589793;
const int INF10 = 1000000001;
const ll INF15 = 1e15 + 1;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;
const double EPS = 0.00001;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (ll)(n); i++)
#define rep2(i, start, end) for (int i = (ll)start; i <= (ll)end; i++)
#define vrep(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (ll)n; i > 0; i--)
#define all(n) n.begin(), n.end()
#define pb push_back
#define debug(x) cerr << #x << ": " << x << '\n'
#define arep(it, a) for (auto it : a)
struct edge {
edge(int s, int e, long long c) {
to = e;
from = s;
cost = c;
}
edge(int i, long long c = 1) { edge(-1, i, c); }
int from;
int to;
long cost;
};
typedef vector<edge> edges;
// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし
// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
int m = 0;
for (m; (1 << m) <= n; m++)
;
m = m - 1;
return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は0)(O(log n))
long myPow(long x, long n, long m = mod) {
if (n < 0)
return 0;
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;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
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; }
bool operator!=(const int a) { return this->x != a; }
bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <class T> bool maxin(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool minin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class M, class N> common_type_t<M, N> mygcd(M a, N b) {
a = abs(a);
b = abs(b);
if (a < b)
return mygcd(b, a);
M r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
template <class M, class N> common_type_t<M, N> mylcm(M a, N b) {
return (a / mygcd(a, b)) * b;
}
const int N_MAX = 200005;
int n;
int a[N_MAX];
void Main() {
int x = 0, y = INF10, z = 1;
// 入力
cin >> n;
rep(i, n) cin >> a[i];
// 処理
ll ans = 0, now = a[0];
rep1(i, n - 1) {
if (now >= a[i]) {
ans += now - a[i];
} else {
now = a[i];
}
}
// 出力
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
cout << fixed << setprecision(12);
Main();
} | replace | 162 | 163 | 162 | 163 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[100005], sum = 0;
signed main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum;
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
int n, a[200005], sum = 0;
signed main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum;
return 0;
} | replace | 3 | 4 | 3 | 5 | 0 | |
p02578 | C++ | Runtime Error | #include <iostream>
using namespace std;
long long a[2 * 10 ^ 5 + 5];
int main() {
long long n, min_sum = 0;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n - 1; ++i) {
if (a[i] > a[i + 1]) {
min_sum += a[i] - a[i + 1];
a[i + 1] += a[i] - a[i + 1];
}
}
cout << min_sum << endl;
} | #include <iostream>
using namespace std;
long long a[2 * 100000 + 5];
int main() {
long long n, min_sum = 0;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n - 1; ++i) {
if (a[i] > a[i + 1]) {
min_sum += a[i] - a[i + 1];
a[i + 1] += a[i] - a[i + 1];
}
}
cout << min_sum << endl;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | #include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
int N;
long int A[20000], ans = 0;
cin >> N;
rep(i, N) cin >> A[i];
for (int i = 1; i < N; i++) {
if (A[i] < A[i - 1]) {
ans += (A[i - 1] - A[i]);
A[i] = A[i - 1];
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
int N;
long int A[200001], ans = 0;
cin >> N;
rep(i, N) cin >> A[i];
for (int i = 1; i < N; i++) {
if (A[i] < A[i - 1]) {
ans += (A[i - 1] - A[i]);
A[i] = A[i - 1];
}
}
cout << ans << endl;
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int INF = 1001001001;
using ll = long long;
int main() {
int N;
cin >> N;
vector<int> A(N);
vector<int> B(N);
ll ans = 0;
rep(i, N) cin >> A.at(i);
B.at(1) = A.at(0);
ans += B.at(1) - A.at(1);
for (int i = 1; i < N - 1; i++) {
int maxtmp = 0;
maxtmp = max(A.at(i + 1), B.at(i));
B.at(i + 1) = maxtmp;
ans += B.at(i + 1) - A.at(i + 1);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int INF = 1001001001;
using ll = long long;
int main() {
int N;
cin >> N;
vector<int> A(N);
vector<int> B(N);
ll ans = 0;
rep(i, N) cin >> A.at(i);
int maxtmp = 0;
rep(i, N) {
maxtmp = max(maxtmp, A.at(i));
ans += maxtmp - A.at(i);
}
cout << ans << endl;
return 0;
} | replace | 14 | 21 | 14 | 18 | 0 | |
p02578 | C++ | Runtime Error | /* author:hellojim */
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define forn(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define fore(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define rep(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x) {
short negative = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
negative = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= negative;
}
ll quickpower(ll n, ll k) {
ll ans = 1;
while (k) {
if (k % 2) {
ans *= n;
ans %= mod;
}
n *= n;
n %= mod;
k /= 2;
}
return ans;
}
string int_to_string(int n) {
string s = "";
while (n) {
int now = n % 10;
s += now + '0';
n /= 10;
}
reverse(s.begin(), s.end());
return s;
}
int string_to_int(string s) {
int n = 0;
rep(i, s.size()) {
n *= 10;
n += s[i] - '0';
}
return n;
}
int n;
ll a[100100];
ll ans;
int main() {
ios::sync_with_stdio(0);
// think twice,code once
cin >> n;
rep(i, n) cin >> a[i];
forn(i, 1, n - 1) {
if (a[i] < a[i - 1]) {
ans += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
cout << ans << endl;
return 0;
}
| /* author:hellojim */
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define forn(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define fore(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define rep(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x) {
short negative = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
negative = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= negative;
}
ll quickpower(ll n, ll k) {
ll ans = 1;
while (k) {
if (k % 2) {
ans *= n;
ans %= mod;
}
n *= n;
n %= mod;
k /= 2;
}
return ans;
}
string int_to_string(int n) {
string s = "";
while (n) {
int now = n % 10;
s += now + '0';
n /= 10;
}
reverse(s.begin(), s.end());
return s;
}
int string_to_int(string s) {
int n = 0;
rep(i, s.size()) {
n *= 10;
n += s[i] - '0';
}
return n;
}
int n;
ll a[200100];
ll ans;
int main() {
ios::sync_with_stdio(0);
// think twice,code once
cin >> n;
rep(i, n) cin >> a[i];
forn(i, 1, n - 1) {
if (a[i] < a[i - 1]) {
ans += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
cout << ans << endl;
return 0;
}
| replace | 88 | 89 | 88 | 89 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
long long ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
ans += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
long long ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
ans += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
cout << ans << '\n';
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int64_t sum = 0;
int A[100000];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int64_t sum = 0;
int A[200010];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll N;
ll sum = 0, mx;
vi A(30000);
cin >> N;
rep(i, N) cin >> A[i];
mx = A[0];
rep(i, N) {
if (mx > A[i])
sum += mx - A[i];
if (A[i] > mx)
mx = A[i];
}
cout << sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll N;
ll sum = 0, mx;
vi A(300000);
cin >> N;
rep(i, N) cin >> A[i];
mx = A[0];
rep(i, N) {
if (mx > A[i])
sum += mx - A[i];
if (A[i] > mx)
mx = A[i];
}
cout << sum << endl;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
#define sort stable_sort
using namespace std;
int _;
int n, a[100050], maxn;
long long ans;
int main() {
// for(scanf("%d",&_);_;_--)
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
maxn = max(maxn, a[i]);
ans += max(maxn - a[i], 0);
}
printf("%lld", ans);
return 0;
} | #include <bits/stdc++.h>
#define sort stable_sort
using namespace std;
int _;
int n, a[200050], maxn;
long long ans;
int main() {
// for(scanf("%d",&_);_;_--)
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
maxn = max(maxn, a[i]);
ans += max(maxn - a[i], 0);
}
printf("%lld", ans);
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02578 | C++ | Runtime Error | #include <iostream>
using namespace std;
int N;
int A[100005];
long long ans;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long long mn = 1LL * A[0];
for (int i = 1; i < N; i++) {
if (A[i] < mn) {
ans += mn - 1LL * A[i];
} else {
mn = 1LL * A[i];
}
}
cout << ans;
return 0;
} | #include <iostream>
using namespace std;
int N;
int A[200005];
long long ans;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long long mn = 1LL * A[0];
for (int i = 1; i < N; i++) {
if (A[i] < mn) {
ans += mn - 1LL * A[i];
} else {
mn = 1LL * A[i];
}
}
cout << ans;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(a) a.begin(), a.end()
const int maxn = 100010;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
int n, m, a[maxn];
string s, ss;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
ll cnt = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++)
if (a[i] < a[i - 1])
cnt += (ll)a[i - 1] - (ll)a[i], a[i] += (ll)a[i - 1] - (ll)a[i];
cout << cnt << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(a) a.begin(), a.end()
const int maxn = 200010;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
int n, m, a[maxn];
string s, ss;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
ll cnt = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++)
if (a[i] < a[i - 1])
cnt += (ll)a[i - 1] - (ll)a[i], a[i] += (ll)a[i - 1] - (ll)a[i];
cout << cnt << "\n";
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long int N;
cin >> N;
if (N > 0) {
vector<long int> vec(N, 0);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
vector<long int> dai(N - 1, 0);
if (vec.at(0) > vec.at(1)) {
dai.at(0) = vec.at(0) - vec.at(1);
}
for (int j = 0; j < N - 2; j++) {
if (vec.at(j + 1) + dai.at(j) > vec.at(j + 2)) {
dai.at(j + 1) = vec.at(j + 1) + dai.at(j) - vec.at(j + 2);
}
}
long int s = 0;
for (int k = 0; k < N - 1; k++) {
s += dai.at(k);
}
cout << s << endl;
} else {
cout << 0 << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long int N;
cin >> N;
if (N > 1) {
vector<long int> vec(N, 0);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
vector<long int> dai(N - 1, 0);
if (vec.at(0) > vec.at(1)) {
dai.at(0) = vec.at(0) - vec.at(1);
}
for (int j = 0; j < N - 2; j++) {
if (vec.at(j + 1) + dai.at(j) > vec.at(j + 2)) {
dai.at(j + 1) = vec.at(j + 1) + dai.at(j) - vec.at(j + 2);
}
}
long int s = 0;
for (int k = 0; k < N - 1; k++) {
s += dai.at(k);
}
cout << s << endl;
} else {
cout << 0 << endl;
}
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2> inline void qread(T1 &IEE, T2 &...ls) {
register T1 __ = 0, ___ = 1;
register char ch;
while (!isdigit(ch = gc()))
___ = (ch == '-') ? -___ : ___;
do {
__ = (__ << 1) + (__ << 3) + (ch ^ 48);
} while (isdigit(ch = gc()));
__ *= ___;
IEE = __;
qread(ls...);
return;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
return;
}
inline void putc_(char _x) {
*oS++ = _x;
if (oS == oT)
flush();
}
inline void qwrite() {}
template <class T1, class... T2> inline void qwrite(T1 IEE, T2... ls) {
if (!IEE)
putc_('0');
if (IEE < 0)
putc_('-'), IEE = -IEE;
while (IEE)
_st[++_qr] = IEE % 10 + '0', IEE /= 10;
while (_qr)
putc_(_st[_qr--]);
qwrite(ls...);
return;
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher;
} // namespace IO
using namespace IO;
ll n, a[maxn];
int main() {
cin.tie(0);
cin >> n;
ll ma = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ma = a[1];
ll ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < ma) {
ans += ma - a[i];
}
ma = max(ma, a[i]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 2e5 + 5;
const ll maxm = 1e3 + 5;
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2> inline void qread(T1 &IEE, T2 &...ls) {
register T1 __ = 0, ___ = 1;
register char ch;
while (!isdigit(ch = gc()))
___ = (ch == '-') ? -___ : ___;
do {
__ = (__ << 1) + (__ << 3) + (ch ^ 48);
} while (isdigit(ch = gc()));
__ *= ___;
IEE = __;
qread(ls...);
return;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
return;
}
inline void putc_(char _x) {
*oS++ = _x;
if (oS == oT)
flush();
}
inline void qwrite() {}
template <class T1, class... T2> inline void qwrite(T1 IEE, T2... ls) {
if (!IEE)
putc_('0');
if (IEE < 0)
putc_('-'), IEE = -IEE;
while (IEE)
_st[++_qr] = IEE % 10 + '0', IEE /= 10;
while (_qr)
putc_(_st[_qr--]);
qwrite(ls...);
return;
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher;
} // namespace IO
using namespace IO;
ll n, a[maxn];
int main() {
cin.tie(0);
cin >> n;
ll ma = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ma = a[1];
ll ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < ma) {
ans += ma - a[i];
}
ma = max(ma, a[i]);
}
cout << ans << endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02578 | C++ | Runtime Error | #define boost
#ifdef boost
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
#define __ read()
using namespace std;
const int maxn(1e5 + 5);
void rda(int *a, int n) {
for (int i = 1; i <= n; ++i)
a[i] = __;
}
void print(int *a, int n) {
for (int i = 1; i <= n; ++i)
printf("%lld ", a[i]);
puts("");
}
int n, a[maxn];
signed main() {
n = __;
rda(a, n);
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans;
}
| #define boost
#ifdef boost
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
#define __ read()
using namespace std;
const int maxn(2e5 + 5);
void rda(int *a, int n) {
for (int i = 1; i <= n; ++i)
a[i] = __;
}
void print(int *a, int n) {
for (int i = 1; i <= n; ++i)
printf("%lld ", a[i]);
puts("");
}
int n, a[maxn];
signed main() {
n = __;
rda(a, n);
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans;
}
| replace | 24 | 25 | 24 | 25 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pint pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define vint vector<int>
#define vpint vector<pair<int, int>>
#define mp make_pair
void reader() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve() {
int n;
cin >> n;
int a[n];
rep(i, 0, n) cin >> a[i];
int b[n];
rep(i, 0, n) b[i] = a[i];
int sum = 0;
rep(i, 0, n - 1) {
if (a[i] > a[i + 1]) {
sum += abs(a[i + 1] - a[i]);
a[i + 1] = a[i];
}
}
cout << sum;
cout << "\n";
}
signed main() {
reader();
fastIO();
long long t = 1;
cin >> t;
while (t--)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pint pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define vint vector<int>
#define vpint vector<pair<int, int>>
#define mp make_pair
void reader() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve() {
int n;
cin >> n;
int a[n];
rep(i, 0, n) cin >> a[i];
int b[n];
rep(i, 0, n) b[i] = a[i];
int sum = 0;
rep(i, 0, n - 1) {
if (a[i] > a[i + 1]) {
sum += abs(a[i + 1] - a[i]);
a[i + 1] = a[i];
}
}
cout << sum;
cout << "\n";
}
signed main() {
reader();
fastIO();
long long t = 1;
// cin>>t;
while (t--)
solve();
return 0;
} | replace | 59 | 60 | 59 | 60 | -11 | |
p02578 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define MOD 1000000007
#define pb push_back
#define loop(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loope(i, b) for (int i = 1; i <= (int)b; i++)
#define loopl(i, a, b) for (ll i = ll(a); i < (ll)b; i++)
int main() {
int n;
cin >> n;
int hei[n];
loop(i, 0, n) { cin >> hei[i]; }
ll ans = 0;
loop(i, 0, n) {
int max = hei[i];
loop(j, i + 1, n) {
if (hei[j] < max) {
ans = ans + (max - hei[j]);
hei[j] = max;
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define MOD 1000000007
#define pb push_back
#define loop(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define loope(i, b) for (int i = 1; i <= (int)b; i++)
#define loopl(i, a, b) for (ll i = ll(a); i < (ll)b; i++)
int main() {
int n;
cin >> n;
int hei[n];
loop(i, 0, n) { cin >> hei[i]; }
ll ans = 0;
loop(i, 0, n - 1) {
if (hei[i] > hei[i + 1]) {
ans += hei[i] - hei[i + 1];
hei[i + 1] = hei[i];
}
}
cout << ans << endl;
return 0;
} | replace | 18 | 25 | 18 | 22 | TLE | |
p02578 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
int n;
int a[100000];
int sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum << endl;
return 0;
} | #include <iostream>
using namespace std;
int main() {
int n;
int a[200000];
long long int sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum << endl;
return 0;
} | replace | 7 | 9 | 7 | 9 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int sum = 0;
int A[100000000];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int64_t sum = 0;
int A[1000000];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
} | replace | 5 | 7 | 5 | 7 | -11 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int64_t sum = 0;
int A[100000000];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int64_t u;
int64_t sum = 0;
int A[200000];
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N == 1) {
cout << sum << endl;
} else {
for (int i = 0; i < N - 1; i++) {
int s = i + 1;
// cout<<A[i]<<A[s]<<endl;
if (A[i] > A[s]) {
u = A[i] - A[s];
A[s] = A[i];
sum += u;
// cout << u <<endl;
}
}
cout << sum << endl;
}
} | replace | 6 | 7 | 6 | 7 | -11 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[112345], ans;
int main() {
cin >> n;
for (ll i = 0; i < n; ++i)
cin >> a[i];
ll mx = a[0];
for (ll i = 1; i < n; ++i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[212345], ans;
int main() {
cin >> n;
for (ll i = 0; i < n; ++i)
cin >> a[i];
ll mx = a[0];
for (ll i = 1; i < n; ++i) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll t, n, k, a[maxn], res;
int main() {
// freopen("in.txt","r",stdin);
cin >> t;
for (int i = 0; i < t; i++)
cin >> a[i];
for (int i = 0; i < t - 1; i++) {
if (a[i] > a[i + 1]) {
res += a[i] - a[i + 1];
a[i + 1] = a[i];
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll t, n, k, a[maxn], res;
int main() {
// freopen("in.txt","r",stdin);
cin >> t;
for (int i = 0; i < t; i++)
cin >> a[i];
for (int i = 0; i < t - 1; i++) {
if (a[i] > a[i + 1]) {
res += a[i] - a[i + 1];
a[i + 1] = a[i];
}
}
cout << res << endl;
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
long long a[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
long long sum = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
long long a[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
long long sum = 0;
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
sum += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << sum << endl;
return 0;
}
| replace | 2 | 3 | 2 | 3 | 0 | |
p02578 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
long long cnt = 0;
for (int i = 0; i < N; i++) {
for (int k = i + 1; k < N; k++) {
if (A.at(i) - A.at(k) > 0) {
cnt += (A.at(i) - A.at(k));
A.at(k) = A.at(i);
}
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
long long cnt = 0;
for (int i = 0; i < (N - 1); i++) {
if (A.at(i) > A.at(i + 1)) {
cnt += (A.at(i) - A.at(i + 1));
A.at(i + 1) = A.at(i);
}
}
cout << cnt << endl;
}
| replace | 15 | 21 | 15 | 19 | TLE | |
p02578 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
using namespace std;
int N;
long long A[20005];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lld", A + i);
}
long long m = A[0];
long long ans = 0;
for (int i = 0; i < N; i++) {
ans += max(0LL, m - A[i]);
m = max(m, A[i]);
}
printf("%lld\n", ans);
}
| #include <algorithm>
#include <cstdio>
using namespace std;
int N;
long long A[200005];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lld", A + i);
}
long long m = A[0];
long long ans = 0;
for (int i = 0; i < N; i++) {
ans += max(0LL, m - A[i]);
m = max(m, A[i]);
}
printf("%lld\n", ans);
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define rep(i, n) for (int i = 0; i < (n); i++)
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;
}
typedef long long ll;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;
int main() {
ll n;
ll a[n];
cin >> n;
rep(i, n) { cin >> a[i]; }
ll ans = 0;
ll high = a[0];
for (int i = 1; i < n; i++) {
if (high > a[i]) {
ans += high - a[i];
}
else if (high < a[i]) {
high = a[i];
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define rep(i, n) for (int i = 0; i < (n); i++)
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;
}
typedef long long ll;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;
int main() {
ll n;
ll a[10001000];
cin >> n;
rep(i, n) { cin >> a[i]; }
ll ans = 0;
ll high = a[0];
for (int i = 1; i < n; i++) {
if (high > a[i]) {
ans += high - a[i];
}
else if (high < a[i]) {
high = a[i];
}
}
cout << ans << endl;
} | replace | 25 | 26 | 25 | 26 | -11 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int a[114514];
int main() {
int cnt = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n; i++) {
int added = a[i - 1] - a[i];
if (added < 0)
added = 0;
a[i] += added;
cnt += added;
}
cout << cnt;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
int a[11114514];
signed main() {
int cnt = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 2; i <= n; i++) {
int added = a[i - 1] - a[i];
if (added < 0)
added = 0;
a[i] += added;
cnt += added;
}
cout << cnt;
return 0;
}
| replace | 2 | 4 | 2 | 5 | 0 | |
p02578 | C++ | Runtime Error | /*
The Island Was Silent before.
.....
And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe \
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
int main() {
ioso
#ifndef ONLINE_JUDGE
ffe
#endif
ll n;
cin >> n;
int a[100010];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll s = 0;
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
// cout << a[i-1]-a[i] << endl;
s += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << s << endl;
} | /*
The Island Was Silent before.
.....
And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe \
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
int main() {
ioso
#ifndef ONLINE_JUDGE
ffe
#endif
ll n;
cin >> n;
int a[200010];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll s = 0;
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
// cout << a[i-1]-a[i] << endl;
s += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << s << endl;
} | replace | 24 | 25 | 24 | 25 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll arr[100000];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
ll ans = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
ans += arr[i] - arr[i + 1];
arr[i + 1] = arr[i];
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll arr[1000000];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
ll ans = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
ans += arr[i] - arr[i + 1];
arr[i + 1] = arr[i];
}
}
cout << ans;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02578 | C++ | Time Limit Exceeded | // ┏━━┓┏━━┓┏┓┏┓┏┳━┳┓┏━━┓┏━┳┓┏━━┓ ┏┳┓┏┳┓┏━┳━┓┏━━┓┏━┓
// ┃┏┓┃┃━━┫┃┗┛┃┃┃┃┃┃┃┏┓┃┃┃┃┃┗┃┃┛ ┃┏┛┃┃┃┃┃┃┃┃┃┏┓┃┃╋┃
// ┃┣┫┃┣━━┃┃┏┓┃┃┃┃┃┃┃┣┫┃┃┃┃┃┏┃┃┓ ┃┗┓┃┃┃┃┃┃┃┃┃┣┫┃┃┓┫
// ┗┛┗┛┗━━┛┗┛┗┛┗━┻━┛┗┛┗┛┗┻━┛┗━━┛ ┗┻┛┗━┛┗┻━┻┛┗┛┗┛┗┻┛
#include "bits/stdc++.h"
using namespace std;
#define mod 1e9 + 7
#define FOR(a, c) for (int a = 0; a < c; a++)
#define FORL(a, b, c) for (int a = b; a < c; a++)
#define FORR(a, b, c) for (int a = b; a > c; a--)
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define PB push_back
#define POB pop_back
#define MP make_pair
int maxm(int a[], int i) {
int ma = INT_MIN;
for (int j = 0; j < i; j++) {
if (a[j] > ma) {
ma = a[j];
}
}
return ma;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n];
FOR(i, n) { cin >> a[i]; }
ll c = 0;
FORL(i, 1, n) {
int ma = maxm(a, i);
if (a[i] < ma) {
c += (ma - a[i]);
}
}
cout << c;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.
// ╔══╗╔══╗╔══╗ ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
// ╚║║╝╚║║╝╚╗╔╝ ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
// ╔║║╗╔║║╗─║║─ ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
// ╚══╝╚══╝─╚╝─ ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝ | // ┏━━┓┏━━┓┏┓┏┓┏┳━┳┓┏━━┓┏━┳┓┏━━┓ ┏┳┓┏┳┓┏━┳━┓┏━━┓┏━┓
// ┃┏┓┃┃━━┫┃┗┛┃┃┃┃┃┃┃┏┓┃┃┃┃┃┗┃┃┛ ┃┏┛┃┃┃┃┃┃┃┃┃┏┓┃┃╋┃
// ┃┣┫┃┣━━┃┃┏┓┃┃┃┃┃┃┃┣┫┃┃┃┃┃┏┃┃┓ ┃┗┓┃┃┃┃┃┃┃┃┃┣┫┃┃┓┫
// ┗┛┗┛┗━━┛┗┛┗┛┗━┻━┛┗┛┗┛┗┻━┛┗━━┛ ┗┻┛┗━┛┗┻━┻┛┗┛┗┛┗┻┛
#include "bits/stdc++.h"
using namespace std;
#define mod 1e9 + 7
#define FOR(a, c) for (int a = 0; a < c; a++)
#define FORL(a, b, c) for (int a = b; a < c; a++)
#define FORR(a, b, c) for (int a = b; a > c; a--)
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define PB push_back
#define POB pop_back
#define MP make_pair
int maxm(int a[], int i) {
int ma = INT_MIN;
for (int j = 0; j < i; j++) {
if (a[j] > ma) {
ma = a[j];
}
}
return ma;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n];
ll ma = 0, c = 0;
;
FOR(i, n) {
cin >> a[i];
if (a[i] > ma) {
ma = a[i];
} else {
c += ma - a[i];
}
}
cout << c;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.
// ╔══╗╔══╗╔══╗ ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
// ╚║║╝╚║║╝╚╗╔╝ ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
// ╔║║╗╔║║╗─║║─ ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
// ╚══╝╚══╝─╚╝─ ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝ | replace | 32 | 38 | 32 | 40 | TLE | |
p02578 | C++ | Runtime Error | /**
* CODE
* BY
* VIKAS VERMA
*
* $$Always Check for Constraints
*/
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, n) for (int i = 1; k <= n; i++)
#define f_range(l, r) for (int i = l; i < r; i++)
#define jaldi ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef long long ll;
//=======================
const int MOD = 1'000'000'007;
const int N = 2e6 + 13;
//=======================
vi g[N];
ll a[N];
//=======================
void solve() {
int n;
cin >> n;
fo(i, n) { cin >> a[i]; }
ll count = 0;
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
ll diff = a[i - 1] - a[i];
a[i] += diff;
count += diff;
;
}
}
cout << count << endl;
}
int main() {
jaldi
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
} | /**
* CODE
* BY
* VIKAS VERMA
*
* $$Always Check for Constraints
*/
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, n) for (int i = 1; k <= n; i++)
#define f_range(l, r) for (int i = l; i < r; i++)
#define jaldi ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef long long ll;
//=======================
const int MOD = 1'000'000'007;
const int N = 2e6 + 13;
//=======================
vi g[N];
ll a[N];
//=======================
void solve() {
int n;
cin >> n;
fo(i, n) { cin >> a[i]; }
ll count = 0;
for (int i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
ll diff = a[i - 1] - a[i];
a[i] += diff;
count += diff;
;
}
}
cout << count << endl;
}
int main() {
jaldi
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
} | replace | 59 | 60 | 59 | 60 | 0 | |
p02578 | C++ | Runtime Error | // #pragma region ALL(include define etc)
#include <algorithm> // max(a, b) min(a, b) reverse() sort()
#include <cmath> // abs(絶対値) sin cos tan(角度 / 180.0 * 3.141592)
#include <functional> // sort(a, a + N, greater<型>());
#include <iomanip>
#include <iostream> // swap(a, b)
#include <limits>
#include <math.h>
#include <string>
#include <vector> // vec.push_back(a) vec.pop_back()
#define int long long
// #define int unsigned long long
#define ll long long
#define double long double
using namespace std;
// #pragma endregion
signed main() {
// 10^18 = 1000000000000000000
// int h1, h2, m1, m2, k;
// int box, box1,box2;
// int N;
int n;
// char n;
// int a, b, c;
// int m;
int h, w, x;
int k;
int d;
// int t;
// string s;
// string t;
// int m1, m2;
// int a, b, c, k;
// int x, y;
string s;
// string s[100000];
// string c[7];
// int a[100000];
// int b[200000];
// int a[2000000] = { 0 };
// int b[2000000] = { 0 };
// int a[20000] = { 0 };
// int b[20000] = { 0 };
// vector<int>a(2000000);
// vector<int>b(2000000);
// int aaa = 0;
// int bbb = 0;
// int box[20000] = { 0 };
char box[20000];
int a[20000] = {0};
// int box = 0;
// int box[7][7] = { 0 };
// int box2;
// int p[1111];
// int x[2000], y[2000];
// int l[100];
// long double aaa;
int count = 0;
// int k, d;
// int a = 0;
int all = 0;
// double all;
// double a, b;
// double h, m;
// int a[30000] = { 0 };
// double c, d, e;
// double mbox, hbox;
// double box, box2;
bool flag = true;
// bool flag = false;
// string str;
// cin >> h1 >> m1 >> h2 >> m2 >> k;
cin >> n;
// cin >> a >> b >> c;
// cin >> s;
// cin >> h >> w >> k;
// cin >> n >> x >> t;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
/*for (int i = 0;i<n; i++)
{
if (i == 0)
{
}
else
{
for (int j = 0; a[i - 1] > a[i]; j++)
{
a[i]++;
all++;
}
}
}*/
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
all += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
// getline(cin, s);
/*for (int i = 0; i < count; i++)
{
all += s[i] - '0';
}*/
// n = s.length();
// sort(p, p + n);
// n = n - 'Z';
// box = n % 26;
// for (int i = 0; i < k; i++)
//{
// all += p[i];
// }
// all = n + (n * n) + (n * n * n);
/*if (n > 0)
{
cout << "a\n";
}
else
{
cout << "A\n";
}*/
// n = t.length();
/*cout << "AC x " << ac << "\n";
cout << "WA x " << wa << "\n";
cout << "TLE x " << tle << "\n";
cout << "RE x " << re << "\n";*/
// cout << x << "\n";
cout << all << "\n";
// cout << box << "\n";
// cout << a << b << c << "\n";
/*if (flag == true)
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}*/
// cout << fixed << setprecision(111) << all;
// return 0;
}
| // #pragma region ALL(include define etc)
#include <algorithm> // max(a, b) min(a, b) reverse() sort()
#include <cmath> // abs(絶対値) sin cos tan(角度 / 180.0 * 3.141592)
#include <functional> // sort(a, a + N, greater<型>());
#include <iomanip>
#include <iostream> // swap(a, b)
#include <limits>
#include <math.h>
#include <string>
#include <vector> // vec.push_back(a) vec.pop_back()
#define int long long
// #define int unsigned long long
#define ll long long
#define double long double
using namespace std;
// #pragma endregion
signed main() {
// 10^18 = 1000000000000000000
// int h1, h2, m1, m2, k;
// int box, box1,box2;
// int N;
int n;
// char n;
// int a, b, c;
// int m;
int h, w, x;
int k;
int d;
// int t;
// string s;
// string t;
// int m1, m2;
// int a, b, c, k;
// int x, y;
string s;
// string s[100000];
// string c[7];
// int a[100000];
// int b[200000];
// int a[2000000] = { 0 };
// int b[2000000] = { 0 };
// int a[20000] = { 0 };
// int b[20000] = { 0 };
// vector<int>a(2000000);
// vector<int>b(2000000);
// int aaa = 0;
// int bbb = 0;
// int box[20000] = { 0 };
char box[20000];
int a[2000000] = {0};
// int box = 0;
// int box[7][7] = { 0 };
// int box2;
// int p[1111];
// int x[2000], y[2000];
// int l[100];
// long double aaa;
int count = 0;
// int k, d;
// int a = 0;
int all = 0;
// double all;
// double a, b;
// double h, m;
// int a[30000] = { 0 };
// double c, d, e;
// double mbox, hbox;
// double box, box2;
bool flag = true;
// bool flag = false;
// string str;
// cin >> h1 >> m1 >> h2 >> m2 >> k;
cin >> n;
// cin >> a >> b >> c;
// cin >> s;
// cin >> h >> w >> k;
// cin >> n >> x >> t;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
/*for (int i = 0;i<n; i++)
{
if (i == 0)
{
}
else
{
for (int j = 0; a[i - 1] > a[i]; j++)
{
a[i]++;
all++;
}
}
}*/
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
all += (a[i - 1] - a[i]);
a[i] = a[i - 1];
}
}
// getline(cin, s);
/*for (int i = 0; i < count; i++)
{
all += s[i] - '0';
}*/
// n = s.length();
// sort(p, p + n);
// n = n - 'Z';
// box = n % 26;
// for (int i = 0; i < k; i++)
//{
// all += p[i];
// }
// all = n + (n * n) + (n * n * n);
/*if (n > 0)
{
cout << "a\n";
}
else
{
cout << "A\n";
}*/
// n = t.length();
/*cout << "AC x " << ac << "\n";
cout << "WA x " << wa << "\n";
cout << "TLE x " << tle << "\n";
cout << "RE x " << re << "\n";*/
// cout << x << "\n";
cout << all << "\n";
// cout << box << "\n";
// cout << a << b << c << "\n";
/*if (flag == true)
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}*/
// cout << fixed << setprecision(111) << all;
// return 0;
}
| replace | 55 | 56 | 55 | 56 | 0 | |
p02578 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define fix(a) \
cout << setprecision(a); \
cout << fixed;
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define clr(a) memset(a, 0, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
int main() {
fast;
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
ll ans = 0;
for (int it = n - 1; it > 0; it--) {
int idx = max_element(arr.begin(), arr.begin() + (it + 1)) - arr.begin();
while (it != idx) {
ans += arr[idx] - arr[it];
it--;
}
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define fix(a) \
cout << setprecision(a); \
cout << fixed;
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define clr(a) memset(a, 0, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
int main() {
fast;
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
ll ans = 0;
for (int i = 0; i < n - 1; i++) {
int del = 0;
if (arr[i] > arr[i + 1])
del = arr[i] - arr[i + 1];
ans += del;
arr[i + 1] += del;
}
cout << ans;
} | replace | 39 | 45 | 39 | 45 | TLE | |
p02578 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sum = 0;
for (int i = 0, maxnum = 0; i < n;) {
maxnum = max(a.at(i), maxnum);
sum += maxnum - a.at(i);
}
cout << sum << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long;
#define ld long double
#define pb push_back
const int mod = 1e9 + 7;
const int mxN = 2e6 + 3;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sum = 0;
for (int i = 0, maxnum = 0; i < n; i++) {
maxnum = max(a.at(i), maxnum);
sum += maxnum - a.at(i);
}
cout << sum << endl;
return 0;
} | replace | 17 | 18 | 17 | 18 | TLE | |
p02578 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
int main() {
ll n;
std::cin >> n;
std::vector<ll> a(n);
rep(i, n) { std::cin >> a[i]; }
ll sum = 0;
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
if (a[i] > a[j]) {
sum += a[i] - a[j];
a[j] += a[i] - a[j];
}
}
}
std::cout << sum << std::endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
int main() {
ll n;
std::cin >> n;
std::vector<ll> a(n);
rep(i, n) { std::cin >> a[i]; }
ll sum = 0;
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
if (a[i] > a[j]) {
sum += a[i] - a[j];
a[j] += a[i] - a[j];
} else {
break;
}
}
}
std::cout << sum << std::endl;
return 0;
}
| insert | 17 | 17 | 17 | 19 | TLE | |
p02578 | C++ | Runtime Error | #include <iostream>
using namespace std;
const int kMaxN = 10;
long long n, ans;
int a[kMaxN];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n; i++) {
ans += max(a[i - 1] - a[i], 0);
a[i] = max(a[i], a[i - 1]);
}
cout << ans;
return 0;
} | #include <iostream>
using namespace std;
const int kMaxN = 2000001;
long long n, ans;
int a[kMaxN];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n; i++) {
ans += max(a[i - 1] - a[i], 0);
a[i] = max(a[i], a[i - 1]);
}
cout << ans;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02578 | C++ | Runtime Error | #pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define fi first
#define se second
#define re register
#define ls i << 1
#define rs i << 1 | 1
#define pb push_back
#define pii pair<int, int>
#define ios \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
inline int rd() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void out(int a) {
if (a < 0)
putchar('-'), a = -a;
if (a >= 10)
out(a / 10);
putchar(a % 10 + '0');
}
const int maxn = 1e5 + 10;
int a[maxn];
signed main() {
// int main() {
int n = rd();
for (int i = 1; i <= n; i++)
a[i] = rd();
int ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] - a[i - 1] < 0) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans << '\n';
return 0;
} | #pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define fi first
#define se second
#define re register
#define ls i << 1
#define rs i << 1 | 1
#define pb push_back
#define pii pair<int, int>
#define ios \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
inline int rd() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void out(int a) {
if (a < 0)
putchar('-'), a = -a;
if (a >= 10)
out(a / 10);
putchar(a % 10 + '0');
}
const int maxn = 2e5 + 10;
int a[maxn];
signed main() {
// int main() {
int n = rd();
for (int i = 1; i <= n; i++)
a[i] = rd();
int ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] - a[i - 1] < 0) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << ans << '\n';
return 0;
} | replace | 65 | 66 | 65 | 66 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a[100000], i;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
long long int max = a[0], ans = 0;
for (i = 1; i < n; i++) {
if (max < a[i])
max = a[i];
else
ans = ans + max - a[i];
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a[1000000], i;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
long long int max = a[0], ans = 0;
for (i = 1; i < n; i++) {
if (max < a[i])
max = a[i];
else
ans = ans + max - a[i];
}
cout << ans;
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02578 | C++ | Runtime Error | /* CF_CC_
4U7H0R:_C4551DY */
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long ll;
#define IC \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define rep(a, b, c) for (ll a = b; a < c; a++)
#define lb lower_bound
#define pii pair<int, int>
#define vec vector<int>
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define len length()
#define rev reverse
#define con continue
const int mxi = 1e5 + 666;
const ll mod = 1e9;
const ll inf = 1e18;
ll n, c, a[mxi];
int main() {
IC cin >> n;
rep(i, 0, n) {
cin >> a[i];
if (i > 0 && a[i] < a[i - 1]) {
c += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << c;
}
| /* CF_CC_
4U7H0R:_C4551DY */
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long ll;
#define IC \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define rep(a, b, c) for (ll a = b; a < c; a++)
#define lb lower_bound
#define pii pair<int, int>
#define vec vector<int>
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define len length()
#define rev reverse
#define con continue
const int mxi = 2e5 + 666;
const ll mod = 1e9;
const ll inf = 1e18;
ll n, c, a[mxi];
int main() {
IC cin >> n;
rep(i, 0, n) {
cin >> a[i];
if (i > 0 && a[i] < a[i - 1]) {
c += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
cout << c;
}
| replace | 26 | 27 | 26 | 27 | 0 | |
p02578 | C++ | Runtime Error | #include <cstdio>
#include <iostream>
using namespace std;
long long a[100005], ans = 0;
int main() {
long long n;
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
printf("%lld", ans);
} | #include <cstdio>
#include <iostream>
using namespace std;
long long a[10000005], ans = 0;
int main() {
long long n;
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 2; i <= n; i++) {
if (a[i] < a[i - 1]) {
ans += a[i - 1] - a[i];
a[i] = a[i - 1];
}
}
printf("%lld", ans);
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02578 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, a[100000000], sum = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
sum = sum + (a[i] - a[i + 1]);
a[i + 1] = a[i];
}
}
cout << sum << "\n";
}
| #include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, a[10000000], sum = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
sum = sum + (a[i] - a[i + 1]);
a[i + 1] = a[i];
}
}
cout << sum << "\n";
}
| replace | 12 | 13 | 12 | 13 | -11 | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;
using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;
#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF ((1LL << 62) - 1)
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME ((1ll << 61) - 1)
#define len length()
#define pushb push_back
#define fi first
#define se second
#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())
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;
}
template <class T> inline void init(T &v) {
for (auto &a : v)
cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
for (auto &a : v)
cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a.first >> a.second;
}
inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
cout << a << " ";
out(forward<U>(alist)...);
}
template <class N> void resiz(N n) {
// empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
hd.resize(n);
resiz(n, forward<U>(tl)...);
}
bool isout(int h, int w, int H, int W) {
return (h < 0 || h >= H || w < 0 || w >= W);
}
ll binpow(ll a, ll ex, ll p) {
ll result = 1;
while (ex > 0) {
if (ex & 1)
result = result * a % p;
ex >>= 1;
a = a * a % p;
}
return result;
}
template <class T> class Matrix {
vector<vector<T>> mat;
public:
Matrix() : Matrix(0, 0) {}
Matrix(int h, int w) { make(h, w); }
Matrix(int h, int w, T init) { make(h, w, init); }
void make(int h, int w) { mat = vector<vector<T>>(h, vector<T>(w)); }
void make(int h, int w, T init) {
mat = vector<vector<T>>(h, vector<T>(w, init));
};
void in() {
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[i].size(); j++) {
cin >> mat[i][j];
}
}
void out() {
for (int i = 0; i < mat.size(); i++) {
int wm = mat[i].size();
for (int j = 0; j < wm; j++) {
cout << mat[i][j] << (wm == j + 1 ? '\n' : ' ');
}
}
cout << flush;
}
inline vector<T> &operator[](int idx) {
assert(0 <= idx && idx < mat.size());
return mat[idx];
}
};
int H, W, sh, sw, gh, gw;
Matrix<char> mp(1001, 1001, '#');
Matrix<int> cost(1001, 1001, INTINF);
int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
int ddy[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2},
ddx[] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
void input() {
cin >> H >> W >> sh >> sw >> gh >> gw;
for (int i = 1; i <= H; i++)
for (int j = 1; j <= W; j++) {
cin >> mp[i][j];
}
}
void solve() {
intq qh, qw, qqh, qqw;
qh.push(sh), qqw.push(sw);
cost[sh][sw] = 0;
while (!qh.empty() || !qqh.empty()) {
while (!qh.empty()) {
int h = qh.front(), w = qw.front();
qh.pop(), qw.pop();
for (int i = 0; i < 4; i++) {
int nh = h + dy[i], nw = w + dx[i];
if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
cost[nh][nw] <= cost[h][w])
continue;
cost[nh][nw] = cost[h][w];
qh.push(nh), qw.push(nw);
qqh.push(nh), qqw.push(nw);
}
qqh.push(h), qqw.push(w);
}
while (!qqh.empty()) {
int h = qqh.front(), w = qqw.front();
qqh.pop(), qqw.pop();
for (int i = 0; i < 20; i++) {
int nh = h + ddy[i], nw = w + ddx[i];
if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
cost[nh][nw] <= cost[h][w] + 1)
continue;
cost[nh][nw] = cost[h][w] + 1;
qh.push(nh), qw.push(nw);
}
}
}
cout << (cost[gh][gw] == INTINF ? -1 : cost[gh][gw]) << endl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
int t = 1;
while (t) {
input();
solve();
t--;
}
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U> using pv = vector<pair<T, U>>;
template <typename T> using matrix = vector<vector<T>>;
template <typename T> using pque = priority_queue<T>;
template <typename T> using lpque = priority_queue<T, vector<T>, greater<T>>;
using ll = long long;
using intpair = pair<int, int>;
using llpair = pair<ll, ll>;
using ilpair = pair<int, ll>;
using lipair = pair<ll, int>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;
#define PI 3.141592653589793
#define INTINF ((1 << 30) - 1)
#define LLINF ((1LL << 62) - 1)
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME ((1ll << 61) - 1)
#define len length()
#define pushb push_back
#define fi first
#define se second
#define setpr fixed << setprecision(15)
#define all(name) name.begin(), name.end()
#define rall(name) name.rbegin(), name.rend()
#define gsort(vbeg, vend) sort(vbeg, vend, greater<>())
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;
}
template <class T> inline void init(T &v) {
for (auto &a : v)
cin >> a;
}
template <class T, class U> inline void init(vector<pair<T, U>> &v) {
for (auto &a : v)
cin >> a.first >> a.second;
}
template <class T, class N> inline void init(T &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a;
}
template <class T, class U, class N>
inline void init(vector<pair<T, U>> &v, N n) {
v.resize(n);
for (auto &a : v)
cin >> a.first >> a.second;
}
inline void out() { cout << endl; }
template <class T, class... U> inline void out(T a, U... alist) {
cout << a << " ";
out(forward<U>(alist)...);
}
template <class N> void resiz(N n) {
// empty
}
template <class N, class T, class... U> void resiz(N n, T &&hd, U &&...tl) {
hd.resize(n);
resiz(n, forward<U>(tl)...);
}
bool isout(int h, int w, int H, int W) {
return (h < 0 || h >= H || w < 0 || w >= W);
}
ll binpow(ll a, ll ex, ll p) {
ll result = 1;
while (ex > 0) {
if (ex & 1)
result = result * a % p;
ex >>= 1;
a = a * a % p;
}
return result;
}
template <class T> class Matrix {
vector<vector<T>> mat;
public:
Matrix() : Matrix(0, 0) {}
Matrix(int h, int w) { make(h, w); }
Matrix(int h, int w, T init) { make(h, w, init); }
void make(int h, int w) { mat = vector<vector<T>>(h, vector<T>(w)); }
void make(int h, int w, T init) {
mat = vector<vector<T>>(h, vector<T>(w, init));
};
void in() {
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[i].size(); j++) {
cin >> mat[i][j];
}
}
void out() {
for (int i = 0; i < mat.size(); i++) {
int wm = mat[i].size();
for (int j = 0; j < wm; j++) {
cout << mat[i][j] << (wm == j + 1 ? '\n' : ' ');
}
}
cout << flush;
}
inline vector<T> &operator[](int idx) {
assert(0 <= idx && idx < mat.size());
return mat[idx];
}
};
int H, W, sh, sw, gh, gw;
Matrix<char> mp(1001, 1001, '#');
Matrix<int> cost(1001, 1001, INTINF);
int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
int ddy[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
0, 1, 1, 1, 1, 2, 2, 2, 2, 2},
ddx[] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
void input() {
cin >> H >> W >> sh >> sw >> gh >> gw;
for (int i = 1; i <= H; i++)
for (int j = 1; j <= W; j++) {
cin >> mp[i][j];
}
}
void solve() {
intq qh, qw, qqh, qqw;
qh.push(sh), qw.push(sw);
cost[sh][sw] = 0;
while (!qh.empty() || !qqh.empty()) {
while (!qh.empty()) {
int h = qh.front(), w = qw.front();
qh.pop(), qw.pop();
for (int i = 0; i < 4; i++) {
int nh = h + dy[i], nw = w + dx[i];
if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
cost[nh][nw] <= cost[h][w])
continue;
cost[nh][nw] = cost[h][w];
qh.push(nh), qw.push(nw);
qqh.push(nh), qqw.push(nw);
}
qqh.push(h), qqw.push(w);
}
while (!qqh.empty()) {
int h = qqh.front(), w = qqw.front();
qqh.pop(), qqw.pop();
for (int i = 0; i < 20; i++) {
int nh = h + ddy[i], nw = w + ddx[i];
if (nh < 0 || nh > H || nw < 0 || nw > W || mp[nh][nw] == '#' ||
cost[nh][nw] <= cost[h][w] + 1)
continue;
cost[nh][nw] = cost[h][w] + 1;
qh.push(nh), qw.push(nw);
}
}
}
cout << (cost[gh][gw] == INTINF ? -1 : cost[gh][gw]) << endl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
int t = 1;
while (t) {
input();
solve();
t--;
}
}
| replace | 157 | 158 | 157 | 158 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
using namespace std;
#define int long long
const int N = 1e3 + 10;
int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};
void dijkstra() {
priority_queue<pair<int, pair<int, int>>> q;
q.push({0, {ch, cw}});
while (!q.empty()) {
auto z = q.top();
int x = z.second.first;
int y = z.second.second;
q.pop();
if (!vis[x][y]) {
vis[x][y] = 1;
} else if (dp[x][y] < -z.first) {
continue;
}
dp[x][y] = -z.first;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
int u = x - 2 + i;
int v = y - 2 + j;
if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
continue;
}
if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
}
}
}
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> h >> w >> ch >> cw;
int dh, dw;
cin >> dh >> dw;
for (int i = 1; i <= h; ++i) {
cin >> s[i] + 1;
}
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
dp[i][j] = 1e18;
}
}
dijkstra();
if (dp[dh][dw] == 1e18) {
cout << -1;
} else {
cout << dp[dh][dw];
}
return 0;
}
| #include <iostream>
#include <queue>
using namespace std;
#define int long long
const int N = 1e3 + 10;
int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};
void dijkstra() {
priority_queue<pair<int, pair<int, int>>> q;
q.push({0, {ch, cw}});
while (!q.empty()) {
auto z = q.top();
int x = z.second.first;
int y = z.second.second;
q.pop();
if (!vis[x][y]) {
vis[x][y] = 1;
} else if (dp[x][y] <= -z.first) {
continue;
}
dp[x][y] = -z.first;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
int u = x - 2 + i;
int v = y - 2 + j;
if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
continue;
}
if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
}
}
}
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> h >> w >> ch >> cw;
int dh, dw;
cin >> dh >> dw;
for (int i = 1; i <= h; ++i) {
cin >> s[i] + 1;
}
for (int i = 1; i <= h; ++i) {
for (int j = 1; j <= w; ++j) {
dp[i][j] = 1e18;
}
}
dijkstra();
if (dp[dh][dw] == 1e18) {
cout << -1;
} else {
cout << dp[dh][dw];
}
return 0;
}
| replace | 26 | 27 | 26 | 27 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) \
for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b)); \
((a) < (b) ? ++i : --i))
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
int cnt = 0;
for (const auto &elem : a) {
cerr << (cnt++ ? ' ' : '>');
cerr << elem;
}
cerr << endl;
}
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
template <class Type_a, class Type_b>
inline bool chmax(Type_a &a, const Type_b &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class Type_a, class Type_b>
inline bool chmin(Type_a &a, const Type_b &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
bool jud(int y, int x, int h, int w) {
return (0 <= y and y < h) and (0 <= x and x < w);
}
int main() {
using type = tuple<int, int, int, int, int>; // tuple(0 or 1,cnt,y,x,dp).
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<string> s(h);
REP(i, h) cin >> s[i];
vvi dp(h, vi(w, INF));
dp[ch][cw] = 0;
priority_queue<type> pque;
int cnt = 0;
pque.push(type(1, cnt--, ch, cw, 0));
while (!pque.empty()) {
int y, x, value;
tie(ignore, ignore, y, x, value) = pque.top();
pque.pop();
if (value > dp[y][x])
continue;
FOR(i, -2, 2) FOR(j, -2, 2) {
int ny = y + i, nx = x + j;
if (jud(ny, nx, h, w)) {
bool flag = false;
REP(k, 4) {
if (i == dy[k] and j == dx[k])
flag = true;
}
if (s[ny][nx] == '.') {
if (chmin(dp[ny][nx], dp[y][x] + (flag ? 0 : 1)))
pque.push(type((flag ? 0 : 1), cnt--, ny, nx, dp[ny][nx]));
}
}
}
}
if (dp[dh][dw] == INF)
cout << -1 << endl;
else
cout << dp[dh][dw] << endl;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) \
for (long long i = (a); ((a) < (b) ? i <= (b) : i >= (b)); \
((a) < (b) ? ++i : --i))
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
using namespace std;
using llong = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
constexpr int INF = 1e9;
constexpr double EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
template <class Type> void line(const Type &a) {
int cnt = 0;
for (const auto &elem : a) {
cerr << (cnt++ ? ' ' : '>');
cerr << elem;
}
cerr << endl;
}
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
template <class Type_a, class Type_b>
inline bool chmax(Type_a &a, const Type_b &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class Type_a, class Type_b>
inline bool chmin(Type_a &a, const Type_b &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
bool jud(int y, int x, int h, int w) {
return (0 <= y and y < h) and (0 <= x and x < w);
}
int main() {
using type = tuple<int, int, int, int, int>; // tuple(0 or 1,cnt,y,x,dp).
int h, w;
cin >> h >> w;
int ch, cw, dh, dw;
cin >> ch >> cw >> dh >> dw;
ch--, cw--, dh--, dw--;
vector<string> s(h);
REP(i, h) cin >> s[i];
vvi dp(h, vi(w, INF));
dp[ch][cw] = 0;
priority_queue<type> pque;
int cnt = 0;
pque.push(type(1, cnt--, ch, cw, 0));
while (!pque.empty()) {
int y, x, value;
tie(ignore, ignore, y, x, value) = pque.top();
pque.pop();
if (value > dp[y][x])
continue;
FOR(i, -2, 2) FOR(j, -2, 2) {
int ny = y + i, nx = x + j;
if (jud(ny, nx, h, w)) {
bool flag = false;
REP(k, 4) {
if (i == dy[k] and j == dx[k])
flag = true;
}
if (s[ny][nx] == '.') {
if (chmin(dp[ny][nx], dp[y][x] + (flag ? 0 : 1)))
pque.push(type((flag ? 1 : 0), cnt--, ny, nx, dp[ny][nx]));
}
}
}
}
if (dp[dh][dw] == INF)
cout << -1 << endl;
else
cout << dp[dh][dw] << endl;
} | replace | 85 | 86 | 85 | 86 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
#define Fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define ll long long int
#define pb push
#define mp make_pair
#define pi acos(-1.0)
#define Mod 10000007
#define limit 1008
using namespace std;
int fx[] = {-1, 0, 1, 0};
int fy[] = {0, 1, 0, -1};
int fxx[] = {-1, 1, 1, -1, -2, -2, -2, -1, 0, 1,
2, 2, 2, 2, 2, 1, 0, -1, -2, -2};
int fyy[] = {1, 1, -1, -1, 0, 1, 2, 2, 2, 2,
2, 1, 0, -1, -2, -2, -2, -2, -2, -1};
int ct[limit][limit];
int n, m, su, sv, du, dv;
queue<pair<int, int>> que;
pair<int, int> z;
void dfs(int u, int v) {
int i, x, y; // cout <<u <<" "<<v<<endl;
que.pb(mp(u, v));
for (i = 0; i < 4; i++) {
x = u + fx[i];
y = v + fy[i];
if (x > 0 && x <= n && y > 0 && y <= m) {
if (ct[x][y] == -1 || ct[x][y] <= ct[u][v] || (u == du && v == dv))
continue;
ct[x][y] = min(ct[x][y], ct[u][v]);
dfs(x, y);
}
}
}
void solve(int t) {
int i, j, x, y;
char ch;
cin >> n >> m;
for (i = 0; i <= n + 5; i++) {
for (j = 0; j <= m + 5; j++) {
ct[i][j] = Mod;
}
}
cin >> su >> sv >> du >> dv;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> ch;
if (ch == '#')
ct[i][j] = -1;
}
}
ct[su][sv] = 0;
dfs(su, sv);
while (!que.empty()) {
z = que.front();
que.pop();
su = z.first;
sv = z.second;
for (i = 0; i < 20; i++) {
x = su + fx[i];
y = sv + fy[i];
if (x > 0 && x <= n && y > 0 && y <= m) {
if (ct[x][y] == -1 || (ct[x][y] - 1) <= ct[su][sv] ||
(su == du && sv == dv))
continue;
ct[x][y] = min(ct[x][y], ct[su][sv] + 1);
dfs(x, y);
}
}
}
if (ct[du][dv] == Mod)
cout << -1 << endl;
else
cout << ct[du][dv] << endl;
return;
}
int main() {
Fast
// Freed
// Fout
int t,
tt = 1;
// cin >> tt;
for (t = 1; t <= tt; t++)
solve(t);
return 0;
}
| #include <bits/stdc++.h>
#define Fast \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define Freed freopen("0in.txt", "r", stdin);
#define Fout freopen("0out.txt", "w", stdout);
#define ll long long int
#define pb push
#define mp make_pair
#define pi acos(-1.0)
#define Mod 10000007
#define limit 1008
using namespace std;
int fx[] = {-1, 0, 1, 0};
int fy[] = {0, 1, 0, -1};
int fxx[] = {-1, 1, 1, -1, -2, -2, -2, -1, 0, 1,
2, 2, 2, 2, 2, 1, 0, -1, -2, -2};
int fyy[] = {1, 1, -1, -1, 0, 1, 2, 2, 2, 2,
2, 1, 0, -1, -2, -2, -2, -2, -2, -1};
int ct[limit][limit];
int n, m, su, sv, du, dv;
queue<pair<int, int>> que;
pair<int, int> z;
void dfs(int u, int v) {
int i, x, y; // cout <<u <<" "<<v<<endl;
que.pb(mp(u, v));
for (i = 0; i < 4; i++) {
x = u + fx[i];
y = v + fy[i];
if (x > 0 && x <= n && y > 0 && y <= m) {
if (ct[x][y] == -1 || ct[x][y] <= ct[u][v] || (u == du && v == dv))
continue;
ct[x][y] = min(ct[x][y], ct[u][v]);
dfs(x, y);
}
}
}
void solve(int t) {
int i, j, x, y;
char ch;
cin >> n >> m;
for (i = 0; i <= n + 5; i++) {
for (j = 0; j <= m + 5; j++) {
ct[i][j] = Mod;
}
}
cin >> su >> sv >> du >> dv;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> ch;
if (ch == '#')
ct[i][j] = -1;
}
}
ct[su][sv] = 0;
dfs(su, sv);
while (!que.empty()) {
z = que.front();
que.pop();
su = z.first;
sv = z.second;
for (i = 0; i < 20; i++) {
x = su + fxx[i];
y = sv + fyy[i];
if (x > 0 && x <= n && y > 0 && y <= m) {
if (ct[x][y] == -1 || (ct[x][y] - 1) <= ct[su][sv] ||
(su == du && sv == dv))
continue;
ct[x][y] = min(ct[x][y], ct[su][sv] + 1);
dfs(x, y);
}
}
}
if (ct[du][dv] == Mod)
cout << -1 << endl;
else
cout << ct[du][dv] << endl;
return;
}
int main() {
Fast
// Freed
// Fout
int t,
tt = 1;
// cin >> tt;
for (t = 1; t <= tt; t++)
solve(t);
return 0;
}
| replace | 63 | 65 | 63 | 65 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dst[1010][1010];
string s[1010];
bool good(int nx, int ny, int n, int m) {
return 1 <= nx && nx <= n && 1 <= ny && ny <= m;
}
void solve() {
int n, m;
cin >> n >> m;
int sx, sy, fx, fy;
cin >> sx >> sy >> fx >> fy;
for (int i = 1; i <= n; i++) {
cin >> s[i];
s[i] = "#" + s[i];
}
memset(dst, -1, sizeof dst);
dst[sx][sy] = 0;
deque<pair<pair<int, int>, int>> q;
q.push_front({{sx, sy}, 0});
while (!q.empty()) {
int x, y;
tie(x, y) = q.front().first;
int dist = q.front().second;
q.pop_front();
if (dst[x][y] != dist)
continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
(dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
dst[nx][ny] = dst[x][y];
q.push_back({{nx, ny}, dst[x][y]});
}
}
for (int mx = -2; mx <= 2; mx++) {
for (int my = -2; my <= 2; my++) {
int nx = x + mx, ny = y + my;
if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
(dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
dst[nx][ny] = dst[x][y] + 1;
q.push_back({{nx, ny}, dst[x][y] + 1});
}
}
}
}
cout << dst[fx][fy];
}
// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
// cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dst[1010][1010];
string s[1010];
bool good(int nx, int ny, int n, int m) {
return 1 <= nx && nx <= n && 1 <= ny && ny <= m;
}
void solve() {
int n, m;
cin >> n >> m;
int sx, sy, fx, fy;
cin >> sx >> sy >> fx >> fy;
for (int i = 1; i <= n; i++) {
cin >> s[i];
s[i] = "#" + s[i];
}
memset(dst, -1, sizeof dst);
dst[sx][sy] = 0;
deque<pair<pair<int, int>, int>> q;
q.push_front({{sx, sy}, 0});
while (!q.empty()) {
int x, y;
tie(x, y) = q.front().first;
int dist = q.front().second;
q.pop_front();
if (dst[x][y] != dist)
continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
(dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
dst[nx][ny] = dst[x][y];
q.push_front({{nx, ny}, dst[x][y]});
}
}
for (int mx = -2; mx <= 2; mx++) {
for (int my = -2; my <= 2; my++) {
int nx = x + mx, ny = y + my;
if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
(dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
dst[nx][ny] = dst[x][y] + 1;
q.push_back({{nx, ny}, dst[x][y] + 1});
}
}
}
}
cout << dst[fx][fy];
}
// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
// cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
solve();
}
| replace | 42 | 43 | 42 | 43 | TLE | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define IOS \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
cout.precision(dbl::max_digits10);
#define pb push_back
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
using namespace std;
const long long N = 100005, INF = (int)1e9;
int h, w;
string s[1001];
int dp[1002][1002];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
return true;
return false;
}
int32_t main() {
cin >> h >> w;
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
for (int i = 0; i < h; i++)
cin >> s[i];
rep(i, 0, h) rep(j, 0, w) dp[i][j] = INF;
deque<pair<pii, int>> q;
vector<pii> temp;
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
if (i != 0 || j != 0) {
temp.pb({i, j});
// cout<<i<<" "<<j<<"\n";
}
}
}
q.push_front({{x1, y1}, 0});
dp[x1][y1] = 0;
while (!q.empty()) {
pair<pii, int> p = q.front();
int x = p.ff.ff, y = p.ff.ss;
q.pop_front();
for (int i = 0; i < 4; i++) {
if (valid(x + dx[i], y + dy[i]) && dp[x + dx[i]][y + dy[i]] > p.ss) {
dp[x + dx[i]][y + dy[i]] = p.ss;
q.push_back({{x + dx[i], y + dy[i]}, p.ss});
}
}
for (int i = 0; i < 24; i++) {
if (valid(x + temp[i].ff, y + temp[i].ss) &&
dp[x + temp[i].ff][y + temp[i].ss] > (p.ss + 1)) {
dp[x + temp[i].ff][y + temp[i].ss] = p.ss + 1;
q.push_back({{x + temp[i].ff, y + temp[i].ss}, p.ss + 1});
}
}
}
if (dp[x2][y2] == INF)
cout << -1;
else
cout << dp[x2][y2];
}
| #include <bits/stdc++.h>
#define IOS \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
cout.precision(dbl::max_digits10);
#define pb push_back
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = x; i < y; i++)
#define fill(a, b) memset(a, b, sizeof(a))
#define vi vector<int>
using namespace std;
const long long N = 100005, INF = (int)1e9;
int h, w;
string s[1001];
int dp[1002][1002];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
bool valid(int x, int y) {
if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
return true;
return false;
}
int32_t main() {
cin >> h >> w;
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
for (int i = 0; i < h; i++)
cin >> s[i];
rep(i, 0, h) rep(j, 0, w) dp[i][j] = INF;
deque<pair<pii, int>> q;
vector<pii> temp;
for (int i = -2; i < 3; i++) {
for (int j = -2; j < 3; j++) {
if (i != 0 || j != 0) {
temp.pb({i, j});
// cout<<i<<" "<<j<<"\n";
}
}
}
q.push_front({{x1, y1}, 0});
dp[x1][y1] = 0;
while (!q.empty()) {
pair<pii, int> p = q.front();
int x = p.ff.ff, y = p.ff.ss;
q.pop_front();
for (int i = 0; i < 4; i++) {
if (valid(x + dx[i], y + dy[i]) && dp[x + dx[i]][y + dy[i]] > p.ss) {
dp[x + dx[i]][y + dy[i]] = p.ss;
q.push_front({{x + dx[i], y + dy[i]}, p.ss});
}
}
for (int i = 0; i < 24; i++) {
if (valid(x + temp[i].ff, y + temp[i].ss) &&
dp[x + temp[i].ff][y + temp[i].ss] > (p.ss + 1)) {
dp[x + temp[i].ff][y + temp[i].ss] = p.ss + 1;
q.push_back({{x + temp[i].ff, y + temp[i].ss}, p.ss + 1});
}
}
}
if (dp[x2][y2] == INF)
cout << -1;
else
cout << dp[x2][y2];
}
| replace | 60 | 61 | 60 | 61 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 1010
char grid[MAX + 10][MAX + 10];
int visited[MAX + 10][MAX + 10];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, +1, 0, -1};
vector<pii> vec[100000];
int h, w, x, y, dx, dy, p = 0, flag = 0;
void bfs(int x, int y, int pos) {
visited[x][y] = 1;
queue<pii> q;
q.push(pii(x, y));
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int u = x + mx[i];
int v = y + my[i];
if (u >= 0 && u < h && v >= 0 && v < w) {
if (visited[u][v] == 0 && grid[u][v] == '.') {
visited[u][v] = 1;
q.push(pii(u, v));
} else if (grid[u][v] == '#' && visited[x][y] <= 1) {
vec[pos].push_back(pii(x, y));
visited[x][y] = 2;
}
}
}
}
}
void printVisited() {
// for(int i=0;i<h;i++){
// for(int j=0;j<w;j++) cout<<visited[i][j]<<" ";
// cout<<endl;
// }
// cout<<endl;
}
int main() {
// input;
// output;
cin >> h >> w;
cin >> x >> y;
cin >> dx >> dy;
x--;
y--;
dx--;
dy--;
for (int i = 0; i < h; i++)
cin >> grid[i];
bfs(x, y, p);
printVisited();
if (visited[dx][dy] >= 1) {
cout << 0 << endl;
return 0;
} else {
while (!vec[p].empty()) {
for (int k = 0; k < vec[p].size(); k++) {
x = vec[p][k].first;
y = vec[p][k].second;
// p++;
// cout<<x<<" "<<y<<endl;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int u = x + i;
int v = y + j;
// if(x==2 && y==2)
if (u < 0 || u > h || v < 0 || v > w)
continue;
if (visited[u][v] == 0 && grid[u][v] == '.') {
// cout<<u<<" -> "<<v<<endl;
bfs(u, v, p + 1);
if (visited[dx][dy] >= 1) {
flag = 1;
break;
}
}
}
printVisited();
if (flag == 1)
break;
}
if (flag == 1)
break;
}
if (flag == 1)
break;
p++;
}
}
if (visited[dx][dy] >= 1) {
cout << p + 1 << endl;
} else
cout << -1 << endl;
return 0;
}
/**
*/
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pi(a) printf("%d", a)
#define spc printf(" ")
#define bl printf("\n")
#define here(a) printf("#### %d ####", a)
#define pii pair<int, int>
#define fr(i, n) for (int i = 0; i < n; i++)
#define case(num) printf("Case %d:", num)
#define inf 1000000050
#define MAX 1010
char grid[MAX + 10][MAX + 10];
int visited[MAX + 10][MAX + 10];
int mx[4] = {-1, 0, 1, 0};
int my[4] = {0, +1, 0, -1};
vector<pii> vec[10000000];
int h, w, x, y, dx, dy, p = 0, flag = 0;
void bfs(int x, int y, int pos) {
visited[x][y] = 1;
queue<pii> q;
q.push(pii(x, y));
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int u = x + mx[i];
int v = y + my[i];
if (u >= 0 && u < h && v >= 0 && v < w) {
if (visited[u][v] == 0 && grid[u][v] == '.') {
visited[u][v] = 1;
q.push(pii(u, v));
} else if (grid[u][v] == '#' && visited[x][y] <= 1) {
vec[pos].push_back(pii(x, y));
visited[x][y] = 2;
}
}
}
}
}
void printVisited() {
// for(int i=0;i<h;i++){
// for(int j=0;j<w;j++) cout<<visited[i][j]<<" ";
// cout<<endl;
// }
// cout<<endl;
}
int main() {
// input;
// output;
cin >> h >> w;
cin >> x >> y;
cin >> dx >> dy;
x--;
y--;
dx--;
dy--;
for (int i = 0; i < h; i++)
cin >> grid[i];
bfs(x, y, p);
printVisited();
if (visited[dx][dy] >= 1) {
cout << 0 << endl;
return 0;
} else {
while (!vec[p].empty()) {
for (int k = 0; k < vec[p].size(); k++) {
x = vec[p][k].first;
y = vec[p][k].second;
// p++;
// cout<<x<<" "<<y<<endl;
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++) {
int u = x + i;
int v = y + j;
// if(x==2 && y==2)
if (u < 0 || u > h || v < 0 || v > w)
continue;
if (visited[u][v] == 0 && grid[u][v] == '.') {
// cout<<u<<" -> "<<v<<endl;
bfs(u, v, p + 1);
if (visited[dx][dy] >= 1) {
flag = 1;
break;
}
}
}
printVisited();
if (flag == 1)
break;
}
if (flag == 1)
break;
}
if (flag == 1)
break;
p++;
}
}
if (visited[dx][dy] >= 1) {
cout << p + 1 << endl;
} else
cout << -1 << endl;
return 0;
}
/**
*/
| replace | 25 | 26 | 25 | 26 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define REP(i, s, n) for (long long i = (s); i <= (n); i++)
#define repr(i, n) for (long long i = (n - 1); i >= 0; i--)
#define REPR(i, s, n) for (long long i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sumvec(v) accumulate(all(v), 0LL)
#define DOUBLE fixed << setprecision(15)
#define OK cerr << "OK\n"
#define OK1 cerr << "OK1\n"
#define OK2 cerr << "OK2\n"
#define sz(s) (long long)s.size()
#define zero(x, n) setw(x) << setfill('0') << n
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
typedef pair<ll, ll> P;
typedef vector<P> vP;
void debug() { cerr << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
cerr << x << " ";
debug(args...);
}
template <class A, class B>
ostream &operator<<(ostream &ost, const pair<A, B> &p) {
ost << "{" << p.first << ", " << p.second << "} ";
return ost;
}
template <class T> ostream &operator<<(ostream &ost, const vector<T> &v) {
ost << "{";
for (int i = 0; i < (int)v.size(); i++) {
if (i)
ost << " ";
ost << v[i];
}
ost << "} \n";
return ost;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]\n";
return os;
}
template <class A, class B>
ostream &operator<<(ostream &ost, const map<A, B> &v) {
ost << "{";
for (auto p : v) {
ost << "{" << p.first << ", " << p.second << "} ";
}
ost << "}\n";
return ost;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &e : vec)
is >> e;
return is;
}
template <typename A, typename B>
istream &operator>>(istream &is, pair<A, B> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> string join(vector<T> &vec, string sep = " ") {
stringstream ss;
for (int i = 0; i < (int)vec.size(); i++) {
ss << vec[i] << (i + 1 == (int)vec.size() ? "\n" : sep);
}
return ss.str();
}
template <typename A, typename B>
pair<A, B> operator+(const pair<A, B> &l, const pair<A, B> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename A, typename B>
pair<A, B> operator-(const pair<A, B> &l, const pair<A, B> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}\n";
return os;
}
template <class T> auto min(const T &a) {
return *min_element(a.begin(), a.end());
}
template <class T> auto max(const T &a) {
return *max_element(a.begin(), a.end());
}
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;
}
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
long long __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
long double __VA_ARGS__; \
IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
void YES(bool b) { cout << ((b) ? "YES\n" : "NO\n"); }
void Yes(bool b) { cout << ((b) ? "Yes\n" : "No\n"); }
void yes(bool b) { cout << ((b) ? "yes\n" : "no\n"); }
void Yay(bool b) { cout << ((b) ? "Yay!\n" : ":(\n"); }
void possible(bool b) { cout << ((b) ? "possible" : "impossible"); }
void Possible(bool b) { cout << ((b) ? "Possible" : "Impossible"); }
void POSSIBLE(bool b) { cout << ((b) ? "POSSIBLE" : "IMPOSSIBLE"); }
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
const int dy8[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dx8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int inf = 1001001001;
const long long INF = ((1LL << 62) - (1LL << 31));
const long double pi = acos(-1.0);
const long long mod = 1000000007;
// const long long mod=998244353;
ll powmod(ll a, ll b) {
ll c = 1;
while (b > 0) {
if (b & 1) {
c = a * c % mod;
}
a = a * a % mod;
b >>= 1;
}
return c;
}
ll nCrmod(ll n, ll r) {
ll x = 1, y = 1;
for (ll i = 0; i < r; i++) {
x = x * (n - i) % mod;
y = y * (i + 1) % mod;
}
return x * powmod(y, mod - 2) % mod;
}
ll gcd(ll a, ll b) {
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
vll divisor(ll x) {
vll v;
for (ll i = 1; i * i <= x; i++)
if (x % i == 0) {
v.push_back(i);
if (i * i != x)
v.push_back(x / i);
}
sort(v.begin(), v.end());
return v;
};
map<ll, ll> prime_factor(ll n) {
map<ll, ll> m;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
m[i]++;
n /= i;
}
}
if (n != 1)
m[n] = 1;
return m;
}
int main() {
LL(h, w);
LL(sy, sx, gy, gx);
sy--;
sx--;
gy--;
gx--;
vvll cost(h, vll(w, INF));
vs s(h);
cin >> s;
deque<P> q;
cost[sy][sx] = 0;
q.push_back({sy, sx});
while (!q.empty()) {
ll y = q.front().first;
ll x = q.front().second;
q.pop_front();
for (ll ny = y - 2; ny <= y + 2; ny++) {
for (ll nx = x - 2; nx <= x + 2; nx++) {
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
bool warp = (abs(y - ny) + abs(x - nx) != 1);
ll c = cost[y][x];
if (warp)
c++;
if (c < cost[ny][nx]) {
cost[ny][nx] = c;
if (warp)
q.push_back({ny, nx});
else
q.push_back({ny, nx});
}
}
}
}
if (cost[gy][gx] == INF)
cout << -1 << endl;
else
cout << cost[gy][gx] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define REP(i, s, n) for (long long i = (s); i <= (n); i++)
#define repr(i, n) for (long long i = (n - 1); i >= 0; i--)
#define REPR(i, s, n) for (long long i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sumvec(v) accumulate(all(v), 0LL)
#define DOUBLE fixed << setprecision(15)
#define OK cerr << "OK\n"
#define OK1 cerr << "OK1\n"
#define OK2 cerr << "OK2\n"
#define sz(s) (long long)s.size()
#define zero(x, n) setw(x) << setfill('0') << n
#define dbg(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
typedef pair<ll, ll> P;
typedef vector<P> vP;
void debug() { cerr << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
cerr << x << " ";
debug(args...);
}
template <class A, class B>
ostream &operator<<(ostream &ost, const pair<A, B> &p) {
ost << "{" << p.first << ", " << p.second << "} ";
return ost;
}
template <class T> ostream &operator<<(ostream &ost, const vector<T> &v) {
ost << "{";
for (int i = 0; i < (int)v.size(); i++) {
if (i)
ost << " ";
ost << v[i];
}
ost << "} \n";
return ost;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec)
os << v << ",";
os << "]\n";
return os;
}
template <class A, class B>
ostream &operator<<(ostream &ost, const map<A, B> &v) {
ost << "{";
for (auto p : v) {
ost << "{" << p.first << ", " << p.second << "} ";
}
ost << "}\n";
return ost;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &e : vec)
is >> e;
return is;
}
template <typename A, typename B>
istream &operator>>(istream &is, pair<A, B> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T> string join(vector<T> &vec, string sep = " ") {
stringstream ss;
for (int i = 0; i < (int)vec.size(); i++) {
ss << vec[i] << (i + 1 == (int)vec.size() ? "\n" : sep);
}
return ss.str();
}
template <typename A, typename B>
pair<A, B> operator+(const pair<A, B> &l, const pair<A, B> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename A, typename B>
pair<A, B> operator-(const pair<A, B> &l, const pair<A, B> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << "{";
for (auto v : vec)
os << v << ",";
os << "}\n";
return os;
}
template <class T> auto min(const T &a) {
return *min_element(a.begin(), a.end());
}
template <class T> auto max(const T &a) {
return *max_element(a.begin(), a.end());
}
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;
}
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
long long __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
long double __VA_ARGS__; \
IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
void YES(bool b) { cout << ((b) ? "YES\n" : "NO\n"); }
void Yes(bool b) { cout << ((b) ? "Yes\n" : "No\n"); }
void yes(bool b) { cout << ((b) ? "yes\n" : "no\n"); }
void Yay(bool b) { cout << ((b) ? "Yay!\n" : ":(\n"); }
void possible(bool b) { cout << ((b) ? "possible" : "impossible"); }
void Possible(bool b) { cout << ((b) ? "Possible" : "Impossible"); }
void POSSIBLE(bool b) { cout << ((b) ? "POSSIBLE" : "IMPOSSIBLE"); }
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};
const int dy8[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dx8[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int inf = 1001001001;
const long long INF = ((1LL << 62) - (1LL << 31));
const long double pi = acos(-1.0);
const long long mod = 1000000007;
// const long long mod=998244353;
ll powmod(ll a, ll b) {
ll c = 1;
while (b > 0) {
if (b & 1) {
c = a * c % mod;
}
a = a * a % mod;
b >>= 1;
}
return c;
}
ll nCrmod(ll n, ll r) {
ll x = 1, y = 1;
for (ll i = 0; i < r; i++) {
x = x * (n - i) % mod;
y = y * (i + 1) % mod;
}
return x * powmod(y, mod - 2) % mod;
}
ll gcd(ll a, ll b) {
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
vll divisor(ll x) {
vll v;
for (ll i = 1; i * i <= x; i++)
if (x % i == 0) {
v.push_back(i);
if (i * i != x)
v.push_back(x / i);
}
sort(v.begin(), v.end());
return v;
};
map<ll, ll> prime_factor(ll n) {
map<ll, ll> m;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
m[i]++;
n /= i;
}
}
if (n != 1)
m[n] = 1;
return m;
}
int main() {
LL(h, w);
LL(sy, sx, gy, gx);
sy--;
sx--;
gy--;
gx--;
vvll cost(h, vll(w, INF));
vs s(h);
cin >> s;
deque<P> q;
cost[sy][sx] = 0;
q.push_back({sy, sx});
while (!q.empty()) {
ll y = q.front().first;
ll x = q.front().second;
q.pop_front();
for (ll ny = y - 2; ny <= y + 2; ny++) {
for (ll nx = x - 2; nx <= x + 2; nx++) {
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (s[ny][nx] == '#')
continue;
bool warp = (abs(y - ny) + abs(x - nx) != 1);
ll c = cost[y][x];
if (warp)
c++;
if (c < cost[ny][nx]) {
cost[ny][nx] = c;
if (warp)
q.push_back({ny, nx});
else
q.push_front({ny, nx});
}
}
}
}
if (cost[gy][gx] == INF)
cout << -1 << endl;
else
cout << cost[gy][gx] << endl;
return 0;
}
| replace | 269 | 270 | 269 | 270 | TLE | |
p02579 | C++ | Time Limit Exceeded |
// #pragma GCC optimize ("-O3")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define popcnt __popcnt64
// # define __builtin_popcount __popcnt
#else
#define popcnt __builtin_popcountll
#endif
// #include "boost/variant.hpp"
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using lll = mp::cpp_int;
using namespace std;
// typedef long long ll;
using ll = int;
constexpr ll MOD = 1000000007ll;
// constexpr ll INF = 1LL << 60;
constexpr ll INF = 1LL << 30;
#define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i)
#define rep_skip(i, N, M, ...) \
for (ll i = N, i##_len = (M); i < i##_len; i += (skip))
#define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i)
#define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG)))
#define pb push_back
#define fir first
#define sec second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define perm(c) \
sort(all(c)); \
for (bool c##perm = 1; c##perm; \
c##perm = next_permutation( \
all(c))) // perm(c){write(c)} writes all permutation of c
constexpr ll dceil(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? (x + y - 1) / y : x / y;
} // ceil for x/y
constexpr ll dfloor(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? x / y : -dceil((-x), y);
} // floor for x/y
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
template <typename T>
using pq_greater = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using vpt = vector<complex<T>>;
template <int n> struct tll_impl {
using type = decltype(tuple_cat(tuple<ll>(),
declval<typename tll_impl<n - 1>::type>()));
};
template <> struct tll_impl<1> {
using type = tuple<ll>;
};
template <int n> using tll = typename tll_impl<n>::type;
template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); };
template <int n, typename T> struct vec_t_impl {
using type = vector<typename vec_t_impl<n - 1, T>::type>;
};
template <typename T> struct vec_t_impl<1, T> {
using type = vector<T>;
};
template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type;
// check
static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, "");
// decompose vector into basetype and dimension.
template <typename T> struct vec_dec {
static constexpr int dim = 0;
using type = T;
};
template <typename T> struct vec_dec<vector<T>> {
static constexpr int dim = vec_dec<T>::dim + 1;
using type = typename vec_dec<T>::type;
};
static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, "");
static_assert(vec_dec<vec_t<3, ll>>::dim == 3, "");
template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); }
template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) {
return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...));
} // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5));
template <typename T>
struct is_vector : std::false_type {}; // check if T is vector
template <typename T> struct is_vector<vector<T>> : std::true_type {};
static_assert(is_vector<vector<ll>>::value == true &&
is_vector<ll>::value == false,
"");
// check if T is vector
template <typename T> struct is_pair : std::false_type {};
template <typename T, typename S>
struct is_pair<pair<T, S>> : std::true_type {};
static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, "");
template <typename T, typename V,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
for (auto &&x : t)
fill_v(x, v);
} // ex: fill_v(dp, INF);
namespace std {
template <class T> bool operator<(const complex<T> &a, const complex<T> &b) {
return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
}; // namespace std
template <typename T, typename S>
istream &operator>>(istream &istr, pair<T, S> &x) {
return istr >> x.first >> x.second;
}
template <typename T> istream &operator>>(istream &istr, vector<T> &x) {
rep(i, 0, x.size()) istr >> x[i];
return istr;
}
template <typename T> istream &operator>>(istream &istr, complex<T> &x) {
T r, i;
istr >> r >> i;
x.real(r);
x.imag(i);
return istr;
}
template <typename T, typename Delim_t = string,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
cout << x << delim;
}
template <typename T, typename Delim_t = string,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim));
cout << '\n';
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
vll seq(ll i, ll j) {
vll res(j - i);
rep(k, i, j) res[k] = i + k;
return res;
}
constexpr ll POW_0(ll x, ll y) {
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == 2)
return x * x;
if (y % 2 == 0)
return POW_0(POW_0(x, y / 2), 2LL);
return ((POW_0(POW_0(x, y / 2), 2LL)) * (x));
}
constexpr ll POW(ll x, ll y, ll mod = 0) {
if (mod == 0)
return POW_0(x, y);
if (y == 0)
return 1;
if (y == 1)
return x % mod;
if (y == 2)
return x * x % mod;
if (y % 2 == 0)
return POW(POW(x, y / 2, mod), 2LL, mod) % mod;
return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod;
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs, Functor f) {
std::sort(std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <int pos, typename Inputs, typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs) {
std::sort(
std::begin(inputs), std::end(inputs),
[](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); });
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void stable_sort_by(Inputs &inputs, Functor f) {
std::stable_sort(
std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <typename Inputs> void sort_uniq(Inputs &inputs) {
sort(all(inputs));
inputs.erase(unique(all(inputs)), inputs.end());
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
if (s.size() > 0 && s.front() == delim) {
elems.push_back("");
}
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
if (s.size() > 0 && s.back() == delim) {
elems.push_back("");
}
return elems;
}
template <class T> map<T, ll> inv_map(vector<T> &x) {
map<T, ll> res;
rep(i, 0, x.size()) { res[x[i]] = i; }
return res;
}
template <class K, class V> map<V, K> inv_map(map<K, V> &m) {
map<V, K> res;
for (const auto &x : m) {
res[x.second] = x.first;
}
return res;
}
template <class T>
constexpr bool exist(const vector<T> &container, const T &val) {
return find(all(container), val) != container.end();
}
template <class T> constexpr bool exist(const set<T> &container, const T &val) {
return container.find(val) != container.end();
}
template <class T, class S>
constexpr bool exist(const map<T, S> &container, const T &val) {
return container.find(val) != container.end();
}
// inner prod: |a||b|cos(theta)
template <class T> T dot(complex<T> a, complex<T> b) {
return a.real() * b.real() + a.imag() * b.imag();
}
// outer prod |a||b|sin(theta)
template <class T> T cross(complex<T> a, complex<T> b) {
return a.real() * b.imag() - a.imag() * b.real();
}
struct UnionFind {
vector<ll> data;
vll querySize_;
set<ll> roots;
vll diff_weight;
UnionFind(ll size)
: data(size, -1), querySize_(size, 0), diff_weight(size, 0) {
rep(i, 0, size) roots.insert(i);
}
// return : pair {new root, old root}
pll unite(ll x, ll y, ll w = 0) {
// return: root
w += weight(x);
w -= weight(y);
x = get_root(x);
y = get_root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y), w = -w;
diff_weight[y] = w;
data[x] += data[y];
data[y] = x;
querySize_[x] += querySize_[y] + 1;
roots.erase(y);
return {x, y};
} else {
querySize_[x]++;
return {x, y};
}
}
bool is_same(ll x, ll y) {
// check whether x and y are connected
return get_root(x) == get_root(y);
}
ll get_root(ll x) {
// get root and compress path
if (data[x] < 0) {
return x;
} else {
auto root = get_root(data[x]);
diff_weight[x] += diff_weight[data[x]];
return data[x] = root;
}
}
ll size(ll x) { return -data[get_root(x)]; }
ll query_size(ll x) { return querySize_[get_root(x)]; }
const set<ll> &get_roots() { return roots; }
void initialize() {
for (auto &i : data) {
i = -1;
}
}
ll weight(ll x) {
get_root(x);
return diff_weight[x];
}
ll weight_diff(ll x, ll y) { return weight(y) - weight(x); }
};
#include <fstream>
#ifdef _WIN64
#include <direct.h>
#endif
enum class GraphDir { dir, undir };
template <class cost_t> struct Edge_Base {
ll from;
ll to;
cost_t cost;
Edge_Base reverse() const { return Edge_Base{to, from, cost}; }
Edge_Base(ll from, ll to, cost_t cost = 1) : from(from), to(to), cost(cost){};
Edge_Base(pll e) : from(e.first), to(e.second), cost(1) {}
Edge_Base() : from(0), to(0), cost(1){};
bool operator<(const Edge_Base &e) const { return cost < e.cost; }
bool operator>(const Edge_Base &e) const { return cost > e.cost; }
bool operator==(const Edge_Base &e) const {
return cost == e.cost && from == e.from && to == e.to;
}
string dot(GraphDir dir, bool weighted) const {
return to_string(from) + (dir == GraphDir::dir ? "->" : "--") +
to_string(to) +
(weighted ? "[label = " + to_string(cost) + "]" : "");
}
};
using Edge = Edge_Base<ll>;
template <class EdgeType, class EdgeContainerType> struct Edge_Itr_Base {
constexpr Edge_Itr_Base() : index(), edges(nullptr) {}
constexpr Edge_Itr_Base(ll index, EdgeContainerType &edges_)
: index(index), edges(&edges_) {}
constexpr Edge_Itr_Base &operator++() {
++index;
return *this;
}
constexpr bool operator==(const Edge_Itr_Base &rhs) const {
return index == rhs.index;
}
constexpr bool operator!=(const Edge_Itr_Base &rhs) const {
return index != rhs.index;
}
constexpr EdgeType *operator->() const { return &(*edges)[index]; }
constexpr EdgeType &operator*() const { return (*edges)[index]; }
constexpr Edge_Itr_Base &operator+=(ll n) {
index += n;
return *this;
}
ll index;
EdgeContainerType *edges;
};
auto nullAction = [](const auto &) {};
template <GraphDir dir, class cost_t> struct Graph_Base {
using Edge = Edge_Base<cost_t>;
using Edge_Itr = Edge_Itr_Base<Edge_Base<cost_t>, vector<Edge_Base<cost_t>>>;
using Edge_CItr =
Edge_Itr_Base<const Edge_Base<cost_t>, const vector<Edge_Base<cost_t>>>;
ll nodeSize;
vector<Edge> edges;
vector<vector<Edge_Itr>> out_edges;
vector<vector<Edge_Itr>> in_edges;
Graph_Base(ll nodeSize, const vector<Edge> &edges_ = vector<Edge>(),
GraphDir dirct = GraphDir::dir)
: nodeSize(nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
for (const Edge &e : edges_)
push(e);
}
Graph_Base(ll nodeSize, vector<pll> edges_)
: nodeSize(nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
for (const pll &e : edges_)
push(Edge(e));
}
Graph_Base(vvll ajacency_matrix, ll default_value)
: nodeSize(ajacency_matrix.size()), out_edges(nodeSize),
in_edges(nodeSize) {
ll n = ajacency_matrix.size();
rep(i, 0, n) rep(j, 0, n) {
if (ajacency_matrix[i][j] != default_value)
push(Edge(i, j, ajacency_matrix[i][j]));
}
}
Graph_Base(const Graph_Base &g)
: nodeSize(g.nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
this->push(g.edges);
}
Graph_Base &operator=(const Graph_Base &g) {
nodeSize = g.nodeSize;
out_edges.resize(nodeSize);
in_edges.resize(nodeSize);
push(g.edges);
return *this;
}
Edge &operator[](ll ind) { return this->edges[ind]; }
const Edge &operator[](ll ind) const { return this->edges[ind]; }
vector<Edge_Itr> &out(ll ind) { return this->out_edges[ind]; }
const vector<Edge_Itr> &out(ll ind) const { return this->out_edges[ind]; }
vector<Edge_Itr> &in(ll ind) { return this->in_edges[ind]; }
const vector<Edge_Itr> &in(ll ind) const { return this->in_edges[ind]; }
Edge_Itr begin() { return Edge_Itr(0, edges); }
Edge_Itr end() { return Edge_Itr(edges.size(), edges); }
Edge_CItr begin() const { return Edge_CItr(0, edges); }
Edge_CItr end() const { return Edge_CItr(edges.size(), edges); }
ll size() const { return nodeSize; }
ll sizeEdges() const { return edges.size(); }
void _push(const Edge &edge) {
assert(max(edge.from, edge.to) < nodeSize);
edges.emplace_back(edge);
out_edges[edge.from].emplace_back(Edge_Itr(edges.size() - 1, edges));
in_edges[edge.to].emplace_back(Edge_Itr(edges.size() - 1, edges));
}
public:
template <class T = void>
void push(const Edge &edge,
enable_if_t<dir == GraphDir::undir, T *> = nullptr) {
_push(edge);
_push(edge.reverse());
}
template <class T = void>
void push(const Edge &edge,
enable_if_t<dir == GraphDir::dir, T *> = nullptr) {
_push(edge);
}
void push(vector<Edge> edges) {
for (const Edge &e : edges) {
push(e);
}
}
vvll adjacency_matrix() const {
vvll d(size(), vll(size(), INF));
for (auto &e : edges) {
d[e.from][e.to] = e.cost;
}
rep(i, 0, size()) { d[i][i] = 0; }
return d;
}
Graph_Base reverse() const {
Graph_Base g(size());
for (const auto &e : this->edges) {
g.push(e.reverse());
}
return g;
}
vll get_topologically_sorted_nodes() const {
// graph needs to be represented by adjacent list.
// complexity: O( node size + edge size)
ll nodeSize = this->size();
// find root
vll roots;
vll inDegree(nodeSize);
rep(i, 0, nodeSize) {
for (auto &sibling : this->out(i)) {
inDegree[sibling->to]++;
}
}
rep(i, 0, nodeSize) {
if (inDegree[i] == 0) {
roots.push_back(i);
}
}
stack<ll> parents;
for (ll i : roots)
parents.push(i);
vll sortedNodes;
while (!parents.empty()) {
ll parent = parents.top();
parents.pop();
sortedNodes.push_back(parent);
for (auto &sibling : this->out(parent)) {
inDegree[sibling->to]--;
if (inDegree[sibling->to] == 0) {
parents.push(sibling->to);
}
}
}
return sortedNodes;
}
// not safe for Edge_Itr
// void topological_sort() {
// vll sorted = get_topologically_sorted_nodes();
// vll new_ind(sorted.size());
// vector<Edge> new_edges;
// rep(i, 0, sorted.size()) {
// new_ind[sorted[i]] = i;
// }
// for (Edge& e : edges) {
// new_edges.emplace_back(Edge{ new_ind[e.from], new_ind[e.to],e.cost
//});
// }
// *this = Graph_Base(this->size(), new_edges);
//}
cost_t diameter() const {
// require : graph is tree
// calculate the diameter ( longest path length ) in O(N)
vector<cost_t> dp(size(), -1);
cost_t m = 0;
ll ind;
function<void(ll)> dfs = [&](ll x) {
for (auto &e : this->out(x)) {
ll nextnode = e->to;
if (dp[nextnode] == -1) {
dp[nextnode] = dp[x] + e->cost;
if (dp[nextnode] > m) {
m = dp[nextnode];
ind = nextnode;
}
dfs(nextnode);
}
}
};
dp[0] = 0;
ind = 0;
dfs(0);
ll first = ind;
fill_v(dp, -1);
dp[first] = 0;
dfs(first);
return m;
// remark two end points of diameter are 'first' and 'ind';
}
cost_t max_length() const {
// calculate the max lenth of path in the graph
vector<cost_t> dp(size(), -1);
auto dfs = [&](auto dfs, ll x) -> void {
for (auto &e : this->out(x)) {
if (dp[e->to] == -1) {
dp[e->to] = 0;
dfs(dfs, e->to);
}
chmax(dp[e->from], dp[e->to] + e->cost);
}
};
rep(node, 0, size()) dfs(dfs, node);
return *max_element(all(dp));
}
vll leaves() const {
vll res;
rep(i, 0, nodeSize) {
if (out(i).size() <= 1)
res.push_back(i);
}
return res;
}
template <class T, class S = decltype(nullAction)>
void dfs(ll startNode, T before_act, S after_act = nullAction) const {
// Impliment func: void(const Edge&) representing what this should do, when
// target node moves from visited node (e.from) to unvisited node (e.to).
const auto &graph = *this;
vb visited(graph.size());
auto dfs_impl = [&](auto dfs_impl, ll startNode) -> void {
visited[startNode] = 1;
for (auto &e : graph.out(startNode)) {
if (visited[e->to])
continue;
before_act(*e);
dfs_impl(dfs_impl, e->to);
after_act(*e);
}
};
dfs_impl(dfs_impl, startNode);
};
template <class T, class S = decltype(nullAction)>
void dfs_node(ll startNode, T before_act, S after_act = nullAction) const {
// Impliment func: void(ll node_ind) representing what this should do, when
// target node moves from visited node to unvisited node (node_ind).
const auto &graph = *this;
vb visited(graph.size());
auto dfs_impl = [&](auto dfs_impl, ll startNode) -> void {
before_act(startNode);
visited[startNode] = 1;
for (auto &e : graph.out(startNode)) {
if (visited[e->to])
continue;
dfs_impl(dfs_impl, e->to);
}
after_act(startNode);
};
dfs_impl(dfs_impl, startNode);
};
template <class T, class S = decltype(nullAction)>
void bfs(ll startNode, T before_act, S after_act = nullAction) const {
const auto &graph = *this;
vb visited(graph.size());
auto bfs_impl = [&](ll startNode) {
// if (visited[startNode] != 0) return;
visited[startNode] = 1;
queue<Edge> toVisit;
for (auto &e : graph.out(startNode))
toVisit.push(*e);
while (toVisit.size()) {
auto next = toVisit.front();
toVisit.pop();
if (visited[next.to])
continue;
visited[next.to] = 1;
before_act(next);
for (auto &e : graph.out(next.to)) {
if (!visited[e->to])
toVisit.push(*e);
}
after_act(next);
}
};
bfs_impl(startNode);
};
vector<cost_t> dijkstra(ll start) const {
vector<cost_t> fromList;
return dijkstra(start, fromList);
}
vector<cost_t> dijkstra(ll start, vector<cost_t> &from_list) const {
// graph: weighted directed graph of adjacent representation
// start: index of start point
// return1: minimum path length from start
// complexity : E*log(V)
const auto &graph = *this;
ll node_size = graph.size();
vector<cost_t> dist(node_size, INF);
from_list.resize(node_size);
fill_v(from_list, -1);
dist[start] = 0;
pq_greater<pair<cost_t, pll>> pq;
pq.push({0, {start, start}});
while (!pq.empty()) {
auto node = pq.top();
pq.pop();
// if not shortest path fixed, fix
ll from = node.second.first;
ll to = node.second.second;
if (from_list[to] != -1)
continue;
from_list[to] = from;
for (auto &edge : graph.out(to)) {
ll adj = edge->to;
cost_t cost = dist[to] + edge->cost;
if (dist[adj] > cost) {
dist[adj] = min(dist[adj], cost);
pq.push({cost, {to, adj}});
}
}
}
return dist;
}
vll euler_tour(ll start) const {
vll res;
res.push_back(start);
dfs(
start, [&](const Edge &e) { res.push_back(e.to); },
[&](const Edge &e) { res.push_back(e.from); });
return res;
}
template <GraphDir out_dir> Graph_Base<out_dir, cost_t> kruskal() const {
// returns minimal spanning tree
Graph_Base<out_dir, cost_t> res(nodeSize);
vpll sortedEdges;
rep(i, 0, edges.size()) { sortedEdges.push_back({edges[i].cost, i}); }
sort(all(sortedEdges));
UnionFind uf(nodeSize);
rep(i, 0, sortedEdges.size()) {
ll cost, eInd;
tie(cost, eInd) = sortedEdges[i];
ll from = (*this)[eInd].from;
ll to = (*this)[eInd].to;
if (!uf.is_same(from, to)) {
res.push((*this)[eInd]);
}
uf.unite(from, to);
}
return res;
}
vvll warshall_floyd() const {
// O(|V|^3)
const Graph_Base &g = *this;
ll n = g.size();
vvll d = g.adjacency_matrix();
rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
if (d[i][j] > d[i][k] + d[k][j])
d[i][j] = d[i][k] + d[k][j];
}
return d;
}
vll bellman_ford(ll start, ll negative_closed_loop_value = -INF) const {
vll from_list;
return bellman_ford(start, from_list, negative_closed_loop_value);
}
vll bellman_ford(ll start, vll &from_list,
ll negative_closed_loop_value = -INF) const {
// O(|E| * |V|)
const Graph_Base &g = *this;
vll dist(g.size(), INF);
dist[start] = 0;
from_list.resize(g.size());
rep(i, 0, g.size()) {
for (const Edge &e : g) {
if (dist[e.from] != INF && dist[e.to] > dist[e.from] + e.cost) {
dist[e.to] = dist[e.from] + e.cost;
from_list[e.to] = e.from;
if (i == g.size() - 1 && dist[e.to] != INF) {
// check negative closed loop
dist[e.to] = negative_closed_loop_value;
}
}
}
}
// propagate negative path
rep(i, 0, g.size()) {
rep(j, 0, g.edges.size()) {
auto &e = g.edges[j];
if (dist[e.from] == negative_closed_loop_value && dist[e.from] != INF)
dist[e.to] = negative_closed_loop_value;
}
}
return dist;
}
bool is_bipartite() const {
vll even(size(), -1);
even[0] = 0;
bool ok = true;
dfs_node(0, [&](ll node) {
for (auto &e : out(node)) {
if (even[e->to] != -1) {
if (even[e->from] == even[e->to]) {
ok = false;
break;
}
} else {
even[e->to] = !even[e->from];
}
}
});
return ok;
}
bool acyclic() const {
vll loop;
return acyclic(loop);
}
bool acyclic(vll &loop) const {
// check whether directed graph has cycle in O(|V| + |E|)
// found loop is stored to "loop"
auto &g = *this;
vll visited(size());
vll stack;
auto dfs = [&](auto dfs, ll node, ll par) -> bool {
visited[node] = 1;
stack.push_back(node);
for (auto &e : g.out(node)) {
if ((dir == GraphDir::dir || par != e->to) && visited[e->to] == 1) {
if (loop.empty()) {
auto it = find(all(stack), e->to);
copy(it, stack.end(), back_inserter(loop));
}
return false;
} else if (visited[e->to] == 0 && !dfs(dfs, e->to, e->from))
return false;
}
visited[node] = 2;
stack.pop_back();
return true;
};
rep(i, 0, g.size()) {
if (!dfs(dfs, i, i))
return false;
}
return true;
}
Graph_Base scc(vll &components) const {
// strongly connected components decomposition algorithm in O(|V| + |E|)
// @ return : contracted DAG
// @ in (components) : node i is in components[i]-th component in DAG
static_assert(dir == GraphDir::dir, "scc is valid for directed graph");
components.resize(size());
vpll time(size());
ll now = 0;
vll visited(size());
auto dfs = [&](auto dfs, ll node) -> void {
visited[node] = 1;
for (auto &&e : this->out(node)) {
if (visited[e->to] == 0) {
dfs(dfs, e->to);
}
}
time[node] = {now++, node};
};
rep(i, 0, size()) {
if (!visited[i]) {
dfs(dfs, i);
}
}
fill_v(visited, 0);
sort(all(time));
ll cur_comp = 0;
auto dfs_rev = [&](auto dfs_rev, ll node) -> void {
visited[node] = 1;
components[node] = cur_comp;
for (auto &&e : this->in(node)) {
if (visited[e->from] == 0) {
dfs_rev(dfs_rev, e->from);
}
}
};
rrep(i, 0, time.size()) {
ll node = time[i].second;
if (visited[node] == 0)
dfs_rev(dfs_rev, node);
cur_comp++;
}
// create contracted DAG
Graph_Base res(cur_comp);
set<pll> check;
for (const Edge &e : edges) {
ll from = components[e.from];
ll to = components[e.to];
if (from != to && exist(check, {from, to})) {
res.push({from, to});
}
}
return res;
}
string dot(bool weighted = false) const {
// export graph as dot file
string res = (dir == GraphDir::dir ? "digraph {" : "graph {");
res +=
"node [ style=filled shape=circle fontname=\"Fira Code\" "
"fontcolor=darkslategray color=darkslategray fillcolor=lightcyan];\n";
res += "edge [ color=darkslategray ]";
rep(i, 0, size()) { res += to_string(i) + ";\n"; }
set<pll> used;
for (auto &e : edges) {
if (!exist(used, {e.from, e.to}) &&
(dir == GraphDir::dir || !exist(used, {e.to, e.from}))) {
used.insert({e.from, e.to});
res += e.dot(dir, weighted);
res += ";\n";
}
}
res += "}\n";
return res;
}
void show(bool weighted = false, string ext = "pdf") const {
// show graph as png file
#ifdef _WIN64
srand(time(nullptr));
static auto init = []() { // make dir tmp (if not exisit) and delete all
// files in it only once.
return system("mkdir .\\tmp > NUL 2>&1") &&
system("del /Q .\\tmp\\* > NUL 2>&1");
}();
string tmpdot = "./tmp/";
string tmppng = "./tmp/";
tmpdot += to_string(rand());
tmppng += to_string(rand()) + "." + ext;
ofstream of(tmpdot);
of << dot(weighted) << endl;
(int)system(("dot -T" + ext + " -o " + tmppng + " " + tmpdot).c_str());
(int)system(("powershell start " + tmppng).c_str());
#endif // _WIN64
}
};
using Graph = Graph_Base<GraphDir::undir, ll>;
using Digraph = Graph_Base<GraphDir::dir, ll>;
vll shortest_path_generator(const vll &from_list, ll start, ll goal) {
// usage : vll path = shortest_path(dijkstra(g,s).second, s, g);
vll path;
path.emplace_back(goal);
while (true) {
ll from = from_list[goal];
path.emplace_back(from);
if (from == start) {
break;
}
goal = from;
}
reverse(all(path));
return path;
}
class FordFulkerson {
vb usedNode;
using Edge = Edge_Base<ll>;
struct RevEdge {
ll from, to, cap, rev;
};
vec_t<2, RevEdge> G;
void add_revedge(const Edge &e) {
G[e.from].push_back(RevEdge{e.from, e.to, e.cost, SZ(G[e.to])});
G[e.to].push_back(RevEdge{e.to, e.from, 0, SZ(G[e.from]) - 1});
}
ll single_flow(ll from, ll to, ll flow) {
// make a single flow
if (from == to)
return flow;
usedNode[from] = 1;
rep(i, 0, G[from].size()) {
RevEdge &e = G[from][i];
if (usedNode[e.to] || e.cap <= 0)
continue;
ll flow_from_e = single_flow(e.to, to, min(flow, e.cap));
if (flow_from_e > 0) {
e.cap -= flow_from_e;
assert(e.cap >= 0);
G[e.to][e.rev].cap += flow_from_e;
// get a larger flow
return flow_from_e;
}
}
// if we already visited all edges or cap = 0 flow = 0;
return 0;
}
public:
template <GraphDir dir, class cost_t>
FordFulkerson(const Graph_Base<dir, cost_t> &graph)
: usedNode(graph.size()), G(vec_t<2, RevEdge>(graph.size())) {
rep(i, 0, graph.size()) {
for (auto &e : graph.out(i)) {
add_revedge(*e);
}
}
}
ll max_flow(ll from, ll to) {
ll flow = 0;
while (true) {
fill_v(usedNode, 0);
ll f = single_flow(from, to, INF);
if (f == 0)
return flow;
else
flow += f;
}
}
};
// Least Common Ancestor
class LCA {
public:
LCA(const Graph &graph, ll root)
: max_par(ll(ceil(log2(graph.size()) + 2))),
parent(graph.size(), vll(max_par, -1)), depth() {
// parent[root][0] = root;
graph.dfs(root, [&](const Edge &e) {
ll to = e.to;
parent[to][0] = e.from;
rep(i, 1, parent[to].size()) {
if (parent[to][i - 1] == -1)
return;
else
parent[to][i] = (parent[parent[to][i - 1]][i - 1]);
}
});
depth = graph.dijkstra(root);
}
ll operator()(ll node1, ll node2) {
if (depth[node1] > depth[node2])
swap(node1, node2);
rrep(i, 0, max_par) {
if (((depth[node2] - depth[node1]) >> i) & 1) {
node2 = parent[node2][i];
}
}
if (node1 == node2)
return node1;
rrep(i, 0, max_par) {
if (parent[node1][i] != parent[node2][i]) {
node1 = parent[node1][i];
node2 = parent[node2][i];
}
}
return parent[node1][0];
}
private:
ll max_par;
vvll parent;
vll depth;
};
class BipartiteMatching {
// O(V*E)
int n, left, right;
vector<vector<int>> graph;
vector<int> used;
int timestamp;
public:
BipartiteMatching(int left, int right)
: n(left + right), left(left), right(right), graph(n), used(n, 0),
timestamp(0) {}
void push(int u, int v) {
graph[u].push_back(v + left);
graph[size_t(v) + left].push_back(u);
}
bool dfs(int idx, vector<int> &match) {
used[idx] = timestamp;
for (auto &to : graph[idx]) {
int to_match = match[to];
if (to_match == -1 ||
(used[to_match] != timestamp && dfs(to_match, match))) {
match[idx] = to;
match[to] = idx;
return true;
}
}
return false;
}
int bipartite_match(vector<int> &match) {
match.resize(n);
fill_v(match, -1);
int ret = 0;
for (int i = 0; i < SZ(graph); i++) {
if (match[i] == -1) {
++timestamp;
ret += dfs(i, match);
}
}
return ret;
}
int bipartite_match() {
vector<int> match;
return bipartite_match(match);
}
};
// tree dfs
// auto dfs = [&](auto dfs, ll from, ll to) ->void {
// for (auto& e : g.out(to)) {
// if (e->to != from) {
// // do something
// }
// }
//};
// dfs(dfs, 0, 0);
struct D2 {
enum Dir { U, D, L, R };
static inline vector<Dir> Dirs = {U, D, L, R};
D2(ll h, ll w) : h(h), w(w){};
bool in(ll n, D2::Dir d, ll k = 1) {
if ((W(n) <= k - 1) && d == L)
return false;
if ((W(n) >= w - 1 - (k - 1)) && d == R)
return false;
if ((H(n) <= k - 1) && d == D)
return false;
if ((H(n) >= h - 1 - (k - 1)) && d == U)
return false;
return true;
};
ll next(Dir d, ll k = 1) {
switch (d) {
case U:
return w * k;
case D:
return -w * k;
case L:
return -k;
case R:
return k;
default:
throw invalid_argument("not direction");
}
}
ll H(ll n) { return n / w; }
ll W(ll n) { return n % w; }
ll h, w;
};
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll h, w;
cin >> h >> w;
pll cp, dp;
cin >> cp >> dp;
ll C = (cp.first - 1) * w + cp.second - 1;
ll D = (dp.first - 1) * w + dp.second - 1;
vs _S(h);
cin >> _S;
string SS;
rep(i, 0, h) { SS.insert(SS.end(), all(_S[i])); }
ll N = h * w;
Graph g(N);
D2 d2(h, w);
rep(i, 0, N) {
if (SS[i] == '.') {
for (auto d : d2.Dirs) {
ll next = i + d2.next(d);
if (d2.in(i, d) && SS[next] == '.') {
g.push(Edge(i, next, 0));
}
}
}
}
rep(i, 0, N) {
if (SS[i] == '.') {
for (auto dir1 : {D2::L, D2::R}) {
rep(i1, 0, 3) {
for (auto dir2 : {D2::U, D2::D}) {
rep(i2, 0, 3) {
if (((i1 == 0 || i1 == 1 || i1 == -1) && i2 == 0) ||
((i2 == 0 || i2 == 1 || i2 == -1) && i1 == 0)) {
continue;
}
if (d2.in(i, dir1, i1) &&
d2.in(i + d2.next(dir1, i1), dir2, i2)) {
ll next = i + d2.next(dir1, i1) + d2.next(dir2, i2);
if (SS[next] == '.') {
g.push(Edge(i, next, 1));
}
}
}
}
}
}
}
}
auto dist = g.dijkstra(C);
cout << (dist[D] == INF ? -1 : dist[D]) << endl;
return 0;
}
|
#pragma GCC optimize("-O3")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define popcnt __popcnt64
// # define __builtin_popcount __popcnt
#else
#define popcnt __builtin_popcountll
#endif
// #include "boost/variant.hpp"
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using lll = mp::cpp_int;
using namespace std;
// typedef long long ll;
using ll = int;
constexpr ll MOD = 1000000007ll;
// constexpr ll INF = 1LL << 60;
constexpr ll INF = 1LL << 30;
#define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i)
#define rep_skip(i, N, M, ...) \
for (ll i = N, i##_len = (M); i < i##_len; i += (skip))
#define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i)
#define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG)))
#define pb push_back
#define fir first
#define sec second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define perm(c) \
sort(all(c)); \
for (bool c##perm = 1; c##perm; \
c##perm = next_permutation( \
all(c))) // perm(c){write(c)} writes all permutation of c
constexpr ll dceil(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? (x + y - 1) / y : x / y;
} // ceil for x/y
constexpr ll dfloor(ll x, ll y) {
if (y < 0) {
x *= -1;
y *= -1;
};
return x > 0 ? x / y : -dceil((-x), y);
} // floor for x/y
typedef pair<double, double> pd;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
template <typename T>
using pq_greater = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using vpt = vector<complex<T>>;
template <int n> struct tll_impl {
using type = decltype(tuple_cat(tuple<ll>(),
declval<typename tll_impl<n - 1>::type>()));
};
template <> struct tll_impl<1> {
using type = tuple<ll>;
};
template <int n> using tll = typename tll_impl<n>::type;
template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); };
template <int n, typename T> struct vec_t_impl {
using type = vector<typename vec_t_impl<n - 1, T>::type>;
};
template <typename T> struct vec_t_impl<1, T> {
using type = vector<T>;
};
template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type;
// check
static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, "");
// decompose vector into basetype and dimension.
template <typename T> struct vec_dec {
static constexpr int dim = 0;
using type = T;
};
template <typename T> struct vec_dec<vector<T>> {
static constexpr int dim = vec_dec<T>::dim + 1;
using type = typename vec_dec<T>::type;
};
static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, "");
static_assert(vec_dec<vec_t<3, ll>>::dim == 3, "");
template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); }
template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) {
return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...));
} // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5));
template <typename T>
struct is_vector : std::false_type {}; // check if T is vector
template <typename T> struct is_vector<vector<T>> : std::true_type {};
static_assert(is_vector<vector<ll>>::value == true &&
is_vector<ll>::value == false,
"");
// check if T is vector
template <typename T> struct is_pair : std::false_type {};
template <typename T, typename S>
struct is_pair<pair<T, S>> : std::true_type {};
static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, "");
template <typename T, typename V,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void fill_v(T &t, const V &v) {
for (auto &&x : t)
fill_v(x, v);
} // ex: fill_v(dp, INF);
namespace std {
template <class T> bool operator<(const complex<T> &a, const complex<T> &b) {
return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real();
}
}; // namespace std
template <typename T, typename S>
istream &operator>>(istream &istr, pair<T, S> &x) {
return istr >> x.first >> x.second;
}
template <typename T> istream &operator>>(istream &istr, vector<T> &x) {
rep(i, 0, x.size()) istr >> x[i];
return istr;
}
template <typename T> istream &operator>>(istream &istr, complex<T> &x) {
T r, i;
istr >> r >> i;
x.real(r);
x.imag(i);
return istr;
}
template <typename T, typename Delim_t = string,
typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
cout << x << delim;
}
template <typename T, typename Delim_t = string,
typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr>
void write(T &x, Delim_t delim = " ") {
rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim));
cout << '\n';
}
template <typename T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
vll seq(ll i, ll j) {
vll res(j - i);
rep(k, i, j) res[k] = i + k;
return res;
}
constexpr ll POW_0(ll x, ll y) {
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == 2)
return x * x;
if (y % 2 == 0)
return POW_0(POW_0(x, y / 2), 2LL);
return ((POW_0(POW_0(x, y / 2), 2LL)) * (x));
}
constexpr ll POW(ll x, ll y, ll mod = 0) {
if (mod == 0)
return POW_0(x, y);
if (y == 0)
return 1;
if (y == 1)
return x % mod;
if (y == 2)
return x * x % mod;
if (y % 2 == 0)
return POW(POW(x, y / 2, mod), 2LL, mod) % mod;
return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod;
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs, Functor f) {
std::sort(std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <int pos, typename Inputs, typename T = typename Inputs::value_type>
void sort_by(Inputs &inputs) {
std::sort(
std::begin(inputs), std::end(inputs),
[](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); });
}
template <typename Inputs, typename Functor,
typename T = typename Inputs::value_type>
void stable_sort_by(Inputs &inputs, Functor f) {
std::stable_sort(
std::begin(inputs), std::end(inputs),
[&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); });
}
template <typename Inputs> void sort_uniq(Inputs &inputs) {
sort(all(inputs));
inputs.erase(unique(all(inputs)), inputs.end());
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string item;
if (s.size() > 0 && s.front() == delim) {
elems.push_back("");
}
while (getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
if (s.size() > 0 && s.back() == delim) {
elems.push_back("");
}
return elems;
}
template <class T> map<T, ll> inv_map(vector<T> &x) {
map<T, ll> res;
rep(i, 0, x.size()) { res[x[i]] = i; }
return res;
}
template <class K, class V> map<V, K> inv_map(map<K, V> &m) {
map<V, K> res;
for (const auto &x : m) {
res[x.second] = x.first;
}
return res;
}
template <class T>
constexpr bool exist(const vector<T> &container, const T &val) {
return find(all(container), val) != container.end();
}
template <class T> constexpr bool exist(const set<T> &container, const T &val) {
return container.find(val) != container.end();
}
template <class T, class S>
constexpr bool exist(const map<T, S> &container, const T &val) {
return container.find(val) != container.end();
}
// inner prod: |a||b|cos(theta)
template <class T> T dot(complex<T> a, complex<T> b) {
return a.real() * b.real() + a.imag() * b.imag();
}
// outer prod |a||b|sin(theta)
template <class T> T cross(complex<T> a, complex<T> b) {
return a.real() * b.imag() - a.imag() * b.real();
}
struct UnionFind {
vector<ll> data;
vll querySize_;
set<ll> roots;
vll diff_weight;
UnionFind(ll size)
: data(size, -1), querySize_(size, 0), diff_weight(size, 0) {
rep(i, 0, size) roots.insert(i);
}
// return : pair {new root, old root}
pll unite(ll x, ll y, ll w = 0) {
// return: root
w += weight(x);
w -= weight(y);
x = get_root(x);
y = get_root(y);
if (x != y) {
if (data[y] < data[x])
swap(x, y), w = -w;
diff_weight[y] = w;
data[x] += data[y];
data[y] = x;
querySize_[x] += querySize_[y] + 1;
roots.erase(y);
return {x, y};
} else {
querySize_[x]++;
return {x, y};
}
}
bool is_same(ll x, ll y) {
// check whether x and y are connected
return get_root(x) == get_root(y);
}
ll get_root(ll x) {
// get root and compress path
if (data[x] < 0) {
return x;
} else {
auto root = get_root(data[x]);
diff_weight[x] += diff_weight[data[x]];
return data[x] = root;
}
}
ll size(ll x) { return -data[get_root(x)]; }
ll query_size(ll x) { return querySize_[get_root(x)]; }
const set<ll> &get_roots() { return roots; }
void initialize() {
for (auto &i : data) {
i = -1;
}
}
ll weight(ll x) {
get_root(x);
return diff_weight[x];
}
ll weight_diff(ll x, ll y) { return weight(y) - weight(x); }
};
#include <fstream>
#ifdef _WIN64
#include <direct.h>
#endif
enum class GraphDir { dir, undir };
template <class cost_t> struct Edge_Base {
ll from;
ll to;
cost_t cost;
Edge_Base reverse() const { return Edge_Base{to, from, cost}; }
Edge_Base(ll from, ll to, cost_t cost = 1) : from(from), to(to), cost(cost){};
Edge_Base(pll e) : from(e.first), to(e.second), cost(1) {}
Edge_Base() : from(0), to(0), cost(1){};
bool operator<(const Edge_Base &e) const { return cost < e.cost; }
bool operator>(const Edge_Base &e) const { return cost > e.cost; }
bool operator==(const Edge_Base &e) const {
return cost == e.cost && from == e.from && to == e.to;
}
string dot(GraphDir dir, bool weighted) const {
return to_string(from) + (dir == GraphDir::dir ? "->" : "--") +
to_string(to) +
(weighted ? "[label = " + to_string(cost) + "]" : "");
}
};
using Edge = Edge_Base<ll>;
template <class EdgeType, class EdgeContainerType> struct Edge_Itr_Base {
constexpr Edge_Itr_Base() : index(), edges(nullptr) {}
constexpr Edge_Itr_Base(ll index, EdgeContainerType &edges_)
: index(index), edges(&edges_) {}
constexpr Edge_Itr_Base &operator++() {
++index;
return *this;
}
constexpr bool operator==(const Edge_Itr_Base &rhs) const {
return index == rhs.index;
}
constexpr bool operator!=(const Edge_Itr_Base &rhs) const {
return index != rhs.index;
}
constexpr EdgeType *operator->() const { return &(*edges)[index]; }
constexpr EdgeType &operator*() const { return (*edges)[index]; }
constexpr Edge_Itr_Base &operator+=(ll n) {
index += n;
return *this;
}
ll index;
EdgeContainerType *edges;
};
auto nullAction = [](const auto &) {};
template <GraphDir dir, class cost_t> struct Graph_Base {
using Edge = Edge_Base<cost_t>;
using Edge_Itr = Edge_Itr_Base<Edge_Base<cost_t>, vector<Edge_Base<cost_t>>>;
using Edge_CItr =
Edge_Itr_Base<const Edge_Base<cost_t>, const vector<Edge_Base<cost_t>>>;
ll nodeSize;
vector<Edge> edges;
vector<vector<Edge_Itr>> out_edges;
vector<vector<Edge_Itr>> in_edges;
Graph_Base(ll nodeSize, const vector<Edge> &edges_ = vector<Edge>(),
GraphDir dirct = GraphDir::dir)
: nodeSize(nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
for (const Edge &e : edges_)
push(e);
}
Graph_Base(ll nodeSize, vector<pll> edges_)
: nodeSize(nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
for (const pll &e : edges_)
push(Edge(e));
}
Graph_Base(vvll ajacency_matrix, ll default_value)
: nodeSize(ajacency_matrix.size()), out_edges(nodeSize),
in_edges(nodeSize) {
ll n = ajacency_matrix.size();
rep(i, 0, n) rep(j, 0, n) {
if (ajacency_matrix[i][j] != default_value)
push(Edge(i, j, ajacency_matrix[i][j]));
}
}
Graph_Base(const Graph_Base &g)
: nodeSize(g.nodeSize), out_edges(nodeSize), in_edges(nodeSize) {
this->push(g.edges);
}
Graph_Base &operator=(const Graph_Base &g) {
nodeSize = g.nodeSize;
out_edges.resize(nodeSize);
in_edges.resize(nodeSize);
push(g.edges);
return *this;
}
Edge &operator[](ll ind) { return this->edges[ind]; }
const Edge &operator[](ll ind) const { return this->edges[ind]; }
vector<Edge_Itr> &out(ll ind) { return this->out_edges[ind]; }
const vector<Edge_Itr> &out(ll ind) const { return this->out_edges[ind]; }
vector<Edge_Itr> &in(ll ind) { return this->in_edges[ind]; }
const vector<Edge_Itr> &in(ll ind) const { return this->in_edges[ind]; }
Edge_Itr begin() { return Edge_Itr(0, edges); }
Edge_Itr end() { return Edge_Itr(edges.size(), edges); }
Edge_CItr begin() const { return Edge_CItr(0, edges); }
Edge_CItr end() const { return Edge_CItr(edges.size(), edges); }
ll size() const { return nodeSize; }
ll sizeEdges() const { return edges.size(); }
void _push(const Edge &edge) {
assert(max(edge.from, edge.to) < nodeSize);
edges.emplace_back(edge);
out_edges[edge.from].emplace_back(Edge_Itr(edges.size() - 1, edges));
in_edges[edge.to].emplace_back(Edge_Itr(edges.size() - 1, edges));
}
public:
template <class T = void>
void push(const Edge &edge,
enable_if_t<dir == GraphDir::undir, T *> = nullptr) {
_push(edge);
_push(edge.reverse());
}
template <class T = void>
void push(const Edge &edge,
enable_if_t<dir == GraphDir::dir, T *> = nullptr) {
_push(edge);
}
void push(vector<Edge> edges) {
for (const Edge &e : edges) {
push(e);
}
}
vvll adjacency_matrix() const {
vvll d(size(), vll(size(), INF));
for (auto &e : edges) {
d[e.from][e.to] = e.cost;
}
rep(i, 0, size()) { d[i][i] = 0; }
return d;
}
Graph_Base reverse() const {
Graph_Base g(size());
for (const auto &e : this->edges) {
g.push(e.reverse());
}
return g;
}
vll get_topologically_sorted_nodes() const {
// graph needs to be represented by adjacent list.
// complexity: O( node size + edge size)
ll nodeSize = this->size();
// find root
vll roots;
vll inDegree(nodeSize);
rep(i, 0, nodeSize) {
for (auto &sibling : this->out(i)) {
inDegree[sibling->to]++;
}
}
rep(i, 0, nodeSize) {
if (inDegree[i] == 0) {
roots.push_back(i);
}
}
stack<ll> parents;
for (ll i : roots)
parents.push(i);
vll sortedNodes;
while (!parents.empty()) {
ll parent = parents.top();
parents.pop();
sortedNodes.push_back(parent);
for (auto &sibling : this->out(parent)) {
inDegree[sibling->to]--;
if (inDegree[sibling->to] == 0) {
parents.push(sibling->to);
}
}
}
return sortedNodes;
}
// not safe for Edge_Itr
// void topological_sort() {
// vll sorted = get_topologically_sorted_nodes();
// vll new_ind(sorted.size());
// vector<Edge> new_edges;
// rep(i, 0, sorted.size()) {
// new_ind[sorted[i]] = i;
// }
// for (Edge& e : edges) {
// new_edges.emplace_back(Edge{ new_ind[e.from], new_ind[e.to],e.cost
//});
// }
// *this = Graph_Base(this->size(), new_edges);
//}
cost_t diameter() const {
// require : graph is tree
// calculate the diameter ( longest path length ) in O(N)
vector<cost_t> dp(size(), -1);
cost_t m = 0;
ll ind;
function<void(ll)> dfs = [&](ll x) {
for (auto &e : this->out(x)) {
ll nextnode = e->to;
if (dp[nextnode] == -1) {
dp[nextnode] = dp[x] + e->cost;
if (dp[nextnode] > m) {
m = dp[nextnode];
ind = nextnode;
}
dfs(nextnode);
}
}
};
dp[0] = 0;
ind = 0;
dfs(0);
ll first = ind;
fill_v(dp, -1);
dp[first] = 0;
dfs(first);
return m;
// remark two end points of diameter are 'first' and 'ind';
}
cost_t max_length() const {
// calculate the max lenth of path in the graph
vector<cost_t> dp(size(), -1);
auto dfs = [&](auto dfs, ll x) -> void {
for (auto &e : this->out(x)) {
if (dp[e->to] == -1) {
dp[e->to] = 0;
dfs(dfs, e->to);
}
chmax(dp[e->from], dp[e->to] + e->cost);
}
};
rep(node, 0, size()) dfs(dfs, node);
return *max_element(all(dp));
}
vll leaves() const {
vll res;
rep(i, 0, nodeSize) {
if (out(i).size() <= 1)
res.push_back(i);
}
return res;
}
template <class T, class S = decltype(nullAction)>
void dfs(ll startNode, T before_act, S after_act = nullAction) const {
// Impliment func: void(const Edge&) representing what this should do, when
// target node moves from visited node (e.from) to unvisited node (e.to).
const auto &graph = *this;
vb visited(graph.size());
auto dfs_impl = [&](auto dfs_impl, ll startNode) -> void {
visited[startNode] = 1;
for (auto &e : graph.out(startNode)) {
if (visited[e->to])
continue;
before_act(*e);
dfs_impl(dfs_impl, e->to);
after_act(*e);
}
};
dfs_impl(dfs_impl, startNode);
};
template <class T, class S = decltype(nullAction)>
void dfs_node(ll startNode, T before_act, S after_act = nullAction) const {
// Impliment func: void(ll node_ind) representing what this should do, when
// target node moves from visited node to unvisited node (node_ind).
const auto &graph = *this;
vb visited(graph.size());
auto dfs_impl = [&](auto dfs_impl, ll startNode) -> void {
before_act(startNode);
visited[startNode] = 1;
for (auto &e : graph.out(startNode)) {
if (visited[e->to])
continue;
dfs_impl(dfs_impl, e->to);
}
after_act(startNode);
};
dfs_impl(dfs_impl, startNode);
};
template <class T, class S = decltype(nullAction)>
void bfs(ll startNode, T before_act, S after_act = nullAction) const {
const auto &graph = *this;
vb visited(graph.size());
auto bfs_impl = [&](ll startNode) {
// if (visited[startNode] != 0) return;
visited[startNode] = 1;
queue<Edge> toVisit;
for (auto &e : graph.out(startNode))
toVisit.push(*e);
while (toVisit.size()) {
auto next = toVisit.front();
toVisit.pop();
if (visited[next.to])
continue;
visited[next.to] = 1;
before_act(next);
for (auto &e : graph.out(next.to)) {
if (!visited[e->to])
toVisit.push(*e);
}
after_act(next);
}
};
bfs_impl(startNode);
};
vector<cost_t> dijkstra(ll start) const {
vector<cost_t> fromList;
return dijkstra(start, fromList);
}
vector<cost_t> dijkstra(ll start, vector<cost_t> &from_list) const {
// graph: weighted directed graph of adjacent representation
// start: index of start point
// return1: minimum path length from start
// complexity : E*log(V)
const auto &graph = *this;
ll node_size = graph.size();
vector<cost_t> dist(node_size, INF);
from_list.resize(node_size);
fill_v(from_list, -1);
dist[start] = 0;
pq_greater<pair<cost_t, pll>> pq;
pq.push({0, {start, start}});
while (!pq.empty()) {
auto node = pq.top();
pq.pop();
// if not shortest path fixed, fix
ll from = node.second.first;
ll to = node.second.second;
if (from_list[to] != -1)
continue;
from_list[to] = from;
for (auto &edge : graph.out(to)) {
ll adj = edge->to;
cost_t cost = dist[to] + edge->cost;
if (dist[adj] > cost) {
dist[adj] = min(dist[adj], cost);
pq.push({cost, {to, adj}});
}
}
}
return dist;
}
vll euler_tour(ll start) const {
vll res;
res.push_back(start);
dfs(
start, [&](const Edge &e) { res.push_back(e.to); },
[&](const Edge &e) { res.push_back(e.from); });
return res;
}
template <GraphDir out_dir> Graph_Base<out_dir, cost_t> kruskal() const {
// returns minimal spanning tree
Graph_Base<out_dir, cost_t> res(nodeSize);
vpll sortedEdges;
rep(i, 0, edges.size()) { sortedEdges.push_back({edges[i].cost, i}); }
sort(all(sortedEdges));
UnionFind uf(nodeSize);
rep(i, 0, sortedEdges.size()) {
ll cost, eInd;
tie(cost, eInd) = sortedEdges[i];
ll from = (*this)[eInd].from;
ll to = (*this)[eInd].to;
if (!uf.is_same(from, to)) {
res.push((*this)[eInd]);
}
uf.unite(from, to);
}
return res;
}
vvll warshall_floyd() const {
// O(|V|^3)
const Graph_Base &g = *this;
ll n = g.size();
vvll d = g.adjacency_matrix();
rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
if (d[i][j] > d[i][k] + d[k][j])
d[i][j] = d[i][k] + d[k][j];
}
return d;
}
vll bellman_ford(ll start, ll negative_closed_loop_value = -INF) const {
vll from_list;
return bellman_ford(start, from_list, negative_closed_loop_value);
}
vll bellman_ford(ll start, vll &from_list,
ll negative_closed_loop_value = -INF) const {
// O(|E| * |V|)
const Graph_Base &g = *this;
vll dist(g.size(), INF);
dist[start] = 0;
from_list.resize(g.size());
rep(i, 0, g.size()) {
for (const Edge &e : g) {
if (dist[e.from] != INF && dist[e.to] > dist[e.from] + e.cost) {
dist[e.to] = dist[e.from] + e.cost;
from_list[e.to] = e.from;
if (i == g.size() - 1 && dist[e.to] != INF) {
// check negative closed loop
dist[e.to] = negative_closed_loop_value;
}
}
}
}
// propagate negative path
rep(i, 0, g.size()) {
rep(j, 0, g.edges.size()) {
auto &e = g.edges[j];
if (dist[e.from] == negative_closed_loop_value && dist[e.from] != INF)
dist[e.to] = negative_closed_loop_value;
}
}
return dist;
}
bool is_bipartite() const {
vll even(size(), -1);
even[0] = 0;
bool ok = true;
dfs_node(0, [&](ll node) {
for (auto &e : out(node)) {
if (even[e->to] != -1) {
if (even[e->from] == even[e->to]) {
ok = false;
break;
}
} else {
even[e->to] = !even[e->from];
}
}
});
return ok;
}
bool acyclic() const {
vll loop;
return acyclic(loop);
}
bool acyclic(vll &loop) const {
// check whether directed graph has cycle in O(|V| + |E|)
// found loop is stored to "loop"
auto &g = *this;
vll visited(size());
vll stack;
auto dfs = [&](auto dfs, ll node, ll par) -> bool {
visited[node] = 1;
stack.push_back(node);
for (auto &e : g.out(node)) {
if ((dir == GraphDir::dir || par != e->to) && visited[e->to] == 1) {
if (loop.empty()) {
auto it = find(all(stack), e->to);
copy(it, stack.end(), back_inserter(loop));
}
return false;
} else if (visited[e->to] == 0 && !dfs(dfs, e->to, e->from))
return false;
}
visited[node] = 2;
stack.pop_back();
return true;
};
rep(i, 0, g.size()) {
if (!dfs(dfs, i, i))
return false;
}
return true;
}
Graph_Base scc(vll &components) const {
// strongly connected components decomposition algorithm in O(|V| + |E|)
// @ return : contracted DAG
// @ in (components) : node i is in components[i]-th component in DAG
static_assert(dir == GraphDir::dir, "scc is valid for directed graph");
components.resize(size());
vpll time(size());
ll now = 0;
vll visited(size());
auto dfs = [&](auto dfs, ll node) -> void {
visited[node] = 1;
for (auto &&e : this->out(node)) {
if (visited[e->to] == 0) {
dfs(dfs, e->to);
}
}
time[node] = {now++, node};
};
rep(i, 0, size()) {
if (!visited[i]) {
dfs(dfs, i);
}
}
fill_v(visited, 0);
sort(all(time));
ll cur_comp = 0;
auto dfs_rev = [&](auto dfs_rev, ll node) -> void {
visited[node] = 1;
components[node] = cur_comp;
for (auto &&e : this->in(node)) {
if (visited[e->from] == 0) {
dfs_rev(dfs_rev, e->from);
}
}
};
rrep(i, 0, time.size()) {
ll node = time[i].second;
if (visited[node] == 0)
dfs_rev(dfs_rev, node);
cur_comp++;
}
// create contracted DAG
Graph_Base res(cur_comp);
set<pll> check;
for (const Edge &e : edges) {
ll from = components[e.from];
ll to = components[e.to];
if (from != to && exist(check, {from, to})) {
res.push({from, to});
}
}
return res;
}
string dot(bool weighted = false) const {
// export graph as dot file
string res = (dir == GraphDir::dir ? "digraph {" : "graph {");
res +=
"node [ style=filled shape=circle fontname=\"Fira Code\" "
"fontcolor=darkslategray color=darkslategray fillcolor=lightcyan];\n";
res += "edge [ color=darkslategray ]";
rep(i, 0, size()) { res += to_string(i) + ";\n"; }
set<pll> used;
for (auto &e : edges) {
if (!exist(used, {e.from, e.to}) &&
(dir == GraphDir::dir || !exist(used, {e.to, e.from}))) {
used.insert({e.from, e.to});
res += e.dot(dir, weighted);
res += ";\n";
}
}
res += "}\n";
return res;
}
void show(bool weighted = false, string ext = "pdf") const {
// show graph as png file
#ifdef _WIN64
srand(time(nullptr));
static auto init = []() { // make dir tmp (if not exisit) and delete all
// files in it only once.
return system("mkdir .\\tmp > NUL 2>&1") &&
system("del /Q .\\tmp\\* > NUL 2>&1");
}();
string tmpdot = "./tmp/";
string tmppng = "./tmp/";
tmpdot += to_string(rand());
tmppng += to_string(rand()) + "." + ext;
ofstream of(tmpdot);
of << dot(weighted) << endl;
(int)system(("dot -T" + ext + " -o " + tmppng + " " + tmpdot).c_str());
(int)system(("powershell start " + tmppng).c_str());
#endif // _WIN64
}
};
using Graph = Graph_Base<GraphDir::undir, ll>;
using Digraph = Graph_Base<GraphDir::dir, ll>;
vll shortest_path_generator(const vll &from_list, ll start, ll goal) {
// usage : vll path = shortest_path(dijkstra(g,s).second, s, g);
vll path;
path.emplace_back(goal);
while (true) {
ll from = from_list[goal];
path.emplace_back(from);
if (from == start) {
break;
}
goal = from;
}
reverse(all(path));
return path;
}
class FordFulkerson {
vb usedNode;
using Edge = Edge_Base<ll>;
struct RevEdge {
ll from, to, cap, rev;
};
vec_t<2, RevEdge> G;
void add_revedge(const Edge &e) {
G[e.from].push_back(RevEdge{e.from, e.to, e.cost, SZ(G[e.to])});
G[e.to].push_back(RevEdge{e.to, e.from, 0, SZ(G[e.from]) - 1});
}
ll single_flow(ll from, ll to, ll flow) {
// make a single flow
if (from == to)
return flow;
usedNode[from] = 1;
rep(i, 0, G[from].size()) {
RevEdge &e = G[from][i];
if (usedNode[e.to] || e.cap <= 0)
continue;
ll flow_from_e = single_flow(e.to, to, min(flow, e.cap));
if (flow_from_e > 0) {
e.cap -= flow_from_e;
assert(e.cap >= 0);
G[e.to][e.rev].cap += flow_from_e;
// get a larger flow
return flow_from_e;
}
}
// if we already visited all edges or cap = 0 flow = 0;
return 0;
}
public:
template <GraphDir dir, class cost_t>
FordFulkerson(const Graph_Base<dir, cost_t> &graph)
: usedNode(graph.size()), G(vec_t<2, RevEdge>(graph.size())) {
rep(i, 0, graph.size()) {
for (auto &e : graph.out(i)) {
add_revedge(*e);
}
}
}
ll max_flow(ll from, ll to) {
ll flow = 0;
while (true) {
fill_v(usedNode, 0);
ll f = single_flow(from, to, INF);
if (f == 0)
return flow;
else
flow += f;
}
}
};
// Least Common Ancestor
class LCA {
public:
LCA(const Graph &graph, ll root)
: max_par(ll(ceil(log2(graph.size()) + 2))),
parent(graph.size(), vll(max_par, -1)), depth() {
// parent[root][0] = root;
graph.dfs(root, [&](const Edge &e) {
ll to = e.to;
parent[to][0] = e.from;
rep(i, 1, parent[to].size()) {
if (parent[to][i - 1] == -1)
return;
else
parent[to][i] = (parent[parent[to][i - 1]][i - 1]);
}
});
depth = graph.dijkstra(root);
}
ll operator()(ll node1, ll node2) {
if (depth[node1] > depth[node2])
swap(node1, node2);
rrep(i, 0, max_par) {
if (((depth[node2] - depth[node1]) >> i) & 1) {
node2 = parent[node2][i];
}
}
if (node1 == node2)
return node1;
rrep(i, 0, max_par) {
if (parent[node1][i] != parent[node2][i]) {
node1 = parent[node1][i];
node2 = parent[node2][i];
}
}
return parent[node1][0];
}
private:
ll max_par;
vvll parent;
vll depth;
};
class BipartiteMatching {
// O(V*E)
int n, left, right;
vector<vector<int>> graph;
vector<int> used;
int timestamp;
public:
BipartiteMatching(int left, int right)
: n(left + right), left(left), right(right), graph(n), used(n, 0),
timestamp(0) {}
void push(int u, int v) {
graph[u].push_back(v + left);
graph[size_t(v) + left].push_back(u);
}
bool dfs(int idx, vector<int> &match) {
used[idx] = timestamp;
for (auto &to : graph[idx]) {
int to_match = match[to];
if (to_match == -1 ||
(used[to_match] != timestamp && dfs(to_match, match))) {
match[idx] = to;
match[to] = idx;
return true;
}
}
return false;
}
int bipartite_match(vector<int> &match) {
match.resize(n);
fill_v(match, -1);
int ret = 0;
for (int i = 0; i < SZ(graph); i++) {
if (match[i] == -1) {
++timestamp;
ret += dfs(i, match);
}
}
return ret;
}
int bipartite_match() {
vector<int> match;
return bipartite_match(match);
}
};
// tree dfs
// auto dfs = [&](auto dfs, ll from, ll to) ->void {
// for (auto& e : g.out(to)) {
// if (e->to != from) {
// // do something
// }
// }
//};
// dfs(dfs, 0, 0);
struct D2 {
enum Dir { U, D, L, R };
static inline vector<Dir> Dirs = {U, D, L, R};
D2(ll h, ll w) : h(h), w(w){};
bool in(ll n, D2::Dir d, ll k = 1) {
if ((W(n) <= k - 1) && d == L)
return false;
if ((W(n) >= w - 1 - (k - 1)) && d == R)
return false;
if ((H(n) <= k - 1) && d == D)
return false;
if ((H(n) >= h - 1 - (k - 1)) && d == U)
return false;
return true;
};
ll next(Dir d, ll k = 1) {
switch (d) {
case U:
return w * k;
case D:
return -w * k;
case L:
return -k;
case R:
return k;
default:
throw invalid_argument("not direction");
}
}
ll H(ll n) { return n / w; }
ll W(ll n) { return n % w; }
ll h, w;
};
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
ll h, w;
cin >> h >> w;
pll cp, dp;
cin >> cp >> dp;
ll C = (cp.first - 1) * w + cp.second - 1;
ll D = (dp.first - 1) * w + dp.second - 1;
vs _S(h);
cin >> _S;
string SS;
rep(i, 0, h) { SS.insert(SS.end(), all(_S[i])); }
ll N = h * w;
Graph g(N);
D2 d2(h, w);
rep(i, 0, N) {
if (SS[i] == '.') {
for (auto d : d2.Dirs) {
ll next = i + d2.next(d);
if (d2.in(i, d) && SS[next] == '.') {
g.push(Edge(i, next, 0));
}
}
}
}
rep(i, 0, N) {
if (SS[i] == '.') {
for (auto dir1 : {D2::L, D2::R}) {
rep(i1, 0, 3) {
for (auto dir2 : {D2::U, D2::D}) {
rep(i2, 0, 3) {
if (((i1 == 0 || i1 == 1 || i1 == -1) && i2 == 0) ||
((i2 == 0 || i2 == 1 || i2 == -1) && i1 == 0)) {
continue;
}
if (d2.in(i, dir1, i1) &&
d2.in(i + d2.next(dir1, i1), dir2, i2)) {
ll next = i + d2.next(dir1, i1) + d2.next(dir2, i2);
if (SS[next] == '.') {
g.push(Edge(i, next, 1));
}
}
}
}
}
}
}
}
auto dist = g.dijkstra(C);
cout << (dist[D] == INF ? -1 : dist[D]) << endl;
return 0;
}
| replace | 1 | 2 | 1 | 2 | TLE | |
p02579 | C++ | Runtime Error | #include <memory.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define MOD 1000000007
char c[1010][1010];
vector<vector<pair<int, int>>> graph(1010 * 1010);
int main() {
int h, w;
cin >> h >> w;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
sx--;
sy--;
tx--;
ty--;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '#')
continue;
for (int l = max(0, i - 2); l < min(h, i + 3); l++) {
for (int k = max(0, j - 2); k < min(w, j + 3); k++) {
if ((l == i && k == j) || c[l][k] == '#')
continue;
if (abs(l - i) + abs(k - j) == 1) {
graph[w * i + j].push_back(make_pair(w * l + k, 0));
} else {
graph[w * i + j].push_back(make_pair(w * l + k, 1));
}
}
}
}
}
priority_queue<pair<int, int>> qu;
qu.push(make_pair(0, w * sx + sy));
vector<int> res(w * w, MOD);
while (!qu.empty()) {
int now = (qu.top()).second;
int cost = -(qu.top()).first;
qu.pop();
if (cost > res[now])
continue;
for (int i = 0; i < graph[now].size(); i++) {
int next = graph[now][i].first;
int nextc = cost + graph[now][i].second;
if (res[next] > nextc) {
res[next] = nextc;
qu.push(make_pair(-nextc, next));
}
}
}
if (res[w * tx + ty] == MOD)
cout << -1 << endl;
else
cout << res[w * tx + ty] << endl;
} | #include <memory.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define MOD 1000000007
char c[1010][1010];
vector<vector<pair<int, int>>> graph(1010 * 1010);
int main() {
int h, w;
cin >> h >> w;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
sx--;
sy--;
tx--;
ty--;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '#')
continue;
for (int l = max(0, i - 2); l < min(h, i + 3); l++) {
for (int k = max(0, j - 2); k < min(w, j + 3); k++) {
if ((l == i && k == j) || c[l][k] == '#')
continue;
if (abs(l - i) + abs(k - j) == 1) {
graph[w * i + j].push_back(make_pair(w * l + k, 0));
} else {
graph[w * i + j].push_back(make_pair(w * l + k, 1));
}
}
}
}
}
priority_queue<pair<int, int>> qu;
qu.push(make_pair(0, w * sx + sy));
vector<int> res(h * w, MOD);
while (!qu.empty()) {
int now = (qu.top()).second;
int cost = -(qu.top()).first;
qu.pop();
if (cost > res[now])
continue;
for (int i = 0; i < graph[now].size(); i++) {
int next = graph[now][i].first;
int nextc = cost + graph[now][i].second;
if (res[next] > nextc) {
res[next] = nextc;
qu.push(make_pair(-nextc, next));
}
}
}
if (res[w * tx + ty] == MOD)
cout << -1 << endl;
else
cout << res[w * tx + ty] << endl;
} | replace | 61 | 62 | 61 | 62 | 0 | |
p02579 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", \
"inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2") // Enable AVX
#include <bits/stdc++.h>
using namespace std;
#define db(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
int dy[] = {0, 1, 0, -1};
int dx[] = {-1, 0, 1, 0};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);
void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }
int n, m;
bool ok(int x, int y) {
if (x >= 0 and x < n and y >= 0 and y < m)
return 1;
else
return 0;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M, sx, sy, ex, ey;
cin >> N >> M >> sx >> sy >> ex >> ey;
n = N, m = M;
sx--, sy--, ex--, ey--;
vector<vector<char>> v(N, vector<char>(M));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> v[i][j];
vector<vector<int>> dist(N, vector<int>(M, OO));
dist[sx][sy] = 0;
priority_queue<pair<int, pii>> pq;
pq.push({0, {sx, sy}});
while (pq.size()) {
int cost = pq.top().F;
int x = pq.top().S.F;
int y = pq.top().S.S;
pq.pop();
if (cost != dist[x][y])
continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (ok(nx, ny) and dist[nx][ny] > dist[x][y] and v[nx][ny] == '.') {
dist[nx][ny] = dist[x][y];
pq.push({dist[nx][ny], {nx, ny}});
}
}
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
int nx = x + i;
int ny = y + j;
if (ok(nx, ny) and dist[nx][ny] > dist[x][y] + 1 and v[nx][ny] == '.') {
dist[nx][ny] = dist[x][y] + 1;
pq.push({dist[nx][ny], {nx, ny}});
}
}
}
if (dist[ex][ey] != OO)
cout << dist[ex][ey];
else
cout << -1;
return 0;
}
| #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", \
"inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2") // Enable AVX
#include <bits/stdc++.h>
using namespace std;
#define db(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
int dy[] = {0, 1, 0, -1};
int dx[] = {-1, 0, 1, 0};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);
void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }
int n, m;
bool ok(int x, int y) {
if (x >= 0 and x < n and y >= 0 and y < m)
return 1;
else
return 0;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M, sx, sy, ex, ey;
cin >> N >> M >> sx >> sy >> ex >> ey;
n = N, m = M;
sx--, sy--, ex--, ey--;
vector<vector<char>> v(N, vector<char>(M));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> v[i][j];
vector<vector<int>> dist(N, vector<int>(M, OO));
dist[sx][sy] = 0;
priority_queue<pair<int, pii>, vector<pair<int, pii>>,
greater<pair<int, pii>>>
pq;
pq.push({0, {sx, sy}});
while (pq.size()) {
int cost = pq.top().F;
int x = pq.top().S.F;
int y = pq.top().S.S;
pq.pop();
if (cost != dist[x][y])
continue;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (ok(nx, ny) and dist[nx][ny] > dist[x][y] and v[nx][ny] == '.') {
dist[nx][ny] = dist[x][y];
pq.push({dist[nx][ny], {nx, ny}});
}
}
for (int i = -2; i <= 2; i++)
for (int j = -2; j <= 2; j++) {
int nx = x + i;
int ny = y + j;
if (ok(nx, ny) and dist[nx][ny] > dist[x][y] + 1 and v[nx][ny] == '.') {
dist[nx][ny] = dist[x][y] + 1;
pq.push({dist[nx][ny], {nx, ny}});
}
}
}
if (dist[ex][ey] != OO)
cout << dist[ex][ey];
else
cout << -1;
return 0;
}
| replace | 63 | 64 | 63 | 66 | TLE | |
p02579 | C++ | Runtime Error | #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;
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
// rb_tree_tag, tree_order_statistics_node_update>
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
string char_to_str(char c) {
string tem(1, c);
return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
if (a > b) {
return a;
}
return b;
}
ll min(ll a, ll b) {
if (a < b) {
return a;
}
return b;
}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n, m;
ll ch, cw;
ll dh, dw;
char a[1005][1005];
ll vis[1005][1005];
bool isSafe(ll r, ll c) {
if (r >= 0 && c >= 0 && r < n && c < m) {
return true;
}
return false;
}
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, -1, 1};
ll hdx[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
ll hdy[24] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
int main() {
fastio;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n >> m >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
vis[i][j] = false;
}
}
queue<pair<ii, ll>> q;
ll ans = 0;
q.push({{ch, cw}, ans});
vis[ch][cw] = 1;
ll fg = 0;
while (true) {
fg = 0;
vector<ii> v1;
while (!q.empty()) {
ll r = q.front().F.F;
ll c = q.front().F.S;
ll jump = q.front().S;
v1.push_back({r, c});
q.pop();
for (int i = 0; i < 4; ++i) {
if (isSafe(r + dx[i], c + dy[i])) {
if (!vis[r + dx[i]][c + dy[i]]) {
if (a[r + dx[i]][c + dy[i]] == '.') {
vis[r + dx[i]][c + dy[i]] = 1;
q.push({{r + dx[i], c + dy[i]}, jump});
}
}
}
}
}
if (v1.empty()) {
fg = -1;
break;
}
for (int i = 0; i < v1.size(); ++i) {
if ((v1[i].F == dh) && (v1[i].S == dw)) {
fg = 1;
break;
}
}
if (fg == 1) {
break;
}
ans++;
for (int i = 0; i < v1.size(); ++i) {
for (int j = 0; j < 24; ++j) {
if (!vis[v1[i].F + hdx[j]][v1[i].S + hdy[j]]) {
if (a[v1[i].F + hdx[j]][v1[i].S + hdy[j]] == '.') {
vis[v1[i].F + hdx[j]][v1[i].S + hdy[j]] = 1;
q.push({{v1[i].F + hdx[j], v1[i].S + hdy[j]}, ans});
}
}
}
}
}
if (fg == -1) {
cout << -1;
} else {
cout << ans;
}
return 0;
} | #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;
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>,
// rb_tree_tag, tree_order_statistics_node_update>
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
string char_to_str(char c) {
string tem(1, c);
return tem;
}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a, ll b) {
if (a > b) {
return a;
}
return b;
}
ll min(ll a, ll b) {
if (a < b) {
return a;
}
return b;
}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n, m;
ll ch, cw;
ll dh, dw;
char a[1005][1005];
ll vis[1005][1005];
bool isSafe(ll r, ll c) {
if (r >= 0 && c >= 0 && r < n && c < m) {
return true;
}
return false;
}
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, -1, 1};
ll hdx[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
ll hdy[24] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
int main() {
fastio;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n >> m >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
vis[i][j] = false;
}
}
queue<pair<ii, ll>> q;
ll ans = 0;
q.push({{ch, cw}, ans});
vis[ch][cw] = 1;
ll fg = 0;
while (true) {
fg = 0;
vector<ii> v1;
while (!q.empty()) {
ll r = q.front().F.F;
ll c = q.front().F.S;
ll jump = q.front().S;
v1.push_back({r, c});
q.pop();
for (int i = 0; i < 4; ++i) {
if (isSafe(r + dx[i], c + dy[i])) {
if (!vis[r + dx[i]][c + dy[i]]) {
if (a[r + dx[i]][c + dy[i]] == '.') {
vis[r + dx[i]][c + dy[i]] = 1;
q.push({{r + dx[i], c + dy[i]}, jump});
}
}
}
}
}
if (v1.empty()) {
fg = -1;
break;
}
for (int i = 0; i < v1.size(); ++i) {
if ((v1[i].F == dh) && (v1[i].S == dw)) {
fg = 1;
break;
}
}
if (fg == 1) {
break;
}
ans++;
for (int i = 0; i < v1.size(); ++i) {
for (int j = 0; j < 24; ++j) {
if (isSafe(v1[i].F + hdx[j], v1[i].S + hdy[j])) {
if (!vis[v1[i].F + hdx[j]][v1[i].S + hdy[j]]) {
if (a[v1[i].F + hdx[j]][v1[i].S + hdy[j]] == '.') {
vis[v1[i].F + hdx[j]][v1[i].S + hdy[j]] = 1;
q.push({{v1[i].F + hdx[j], v1[i].S + hdy[j]}, ans});
}
}
}
}
}
}
if (fg == -1) {
cout << -1;
} else {
cout << ans;
}
return 0;
} | replace | 123 | 127 | 123 | 129 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define INF int(1e9)
#ifdef SHUBHAM107
#include "../trace.h"
#else
#define trace(args...)
#endif
// vector<int> dx = {-1, 0, 0, 1};
// vector<int> dy = {0, 1, -1, 0};
void solve() {
int h, w;
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--, cw--;
int dh, dw;
cin >> dh >> dw;
dh--, dw--;
vector<string> mat(h);
for (string &s : mat)
cin >> s;
// trace(mat);
deque<pair<int, int>> q;
vector<vector<int>> d(h, vector<int>(w, INF));
d[ch][cw] = 0;
q.push_back({ch, cw});
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
// trace(x, y);
q.pop_front();
for (int dx = -2; dx <= 2; dx++) {
for (int dy = -2; dy <= 2; dy++) {
int w1 = 1;
if (dx * dx + dy * dy == 1)
w1 = 0;
int nx = x + dx;
int ny = y + dy;
if (nx < h && ny < w && nx >= 0 && ny >= 0) {
// trace(nx, ny, 'a');
if (mat[nx][ny] == '.') {
// trace(nx, ny, 'b');
if (d[nx][ny] > w1 + d[x][y]) {
// trace(nx, ny);
d[nx][ny] = w1 + d[x][y];
if (w == 0) {
q.push_front({nx, ny});
} else {
q.push_back({nx, ny});
}
}
}
}
}
}
}
if (d[dh][dw] >= INF) {
cout << -1;
} else {
cout << d[dh][dw];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef SHUBHAM107
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define INF int(1e9)
#ifdef SHUBHAM107
#include "../trace.h"
#else
#define trace(args...)
#endif
// vector<int> dx = {-1, 0, 0, 1};
// vector<int> dy = {0, 1, -1, 0};
void solve() {
int h, w;
cin >> h >> w;
int ch, cw;
cin >> ch >> cw;
ch--, cw--;
int dh, dw;
cin >> dh >> dw;
dh--, dw--;
vector<string> mat(h);
for (string &s : mat)
cin >> s;
// trace(mat);
deque<pair<int, int>> q;
vector<vector<int>> d(h, vector<int>(w, INF));
d[ch][cw] = 0;
q.push_back({ch, cw});
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
// trace(x, y);
q.pop_front();
for (int dx = -2; dx <= 2; dx++) {
for (int dy = -2; dy <= 2; dy++) {
int w1 = 1;
if (dx * dx + dy * dy == 1)
w1 = 0;
int nx = x + dx;
int ny = y + dy;
if (nx < h && ny < w && nx >= 0 && ny >= 0 && mat[nx][ny] == '.') {
if (d[nx][ny] > w1 + d[x][y]) {
d[nx][ny] = w1 + d[x][y];
if (w1 == 0) {
q.push_front({nx, ny});
} else {
q.push_back({nx, ny});
}
}
}
}
}
}
if (d[dh][dw] >= INF) {
cout << -1;
} else {
cout << d[dh][dw];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef SHUBHAM107
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
| replace | 61 | 73 | 61 | 68 | TLE | |
p02579 | C++ | Runtime Error | // #pragma GCC optimize("trapv")
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#define int long long
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9 * 1e9 + 10;
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int sz = 1e3 + 200;
char g[sz][sz];
bool used[sz][sz];
int dist[sz][sz];
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
signed main() {
fast();
int h, w;
cin >> h >> w;
pair<int, int> st, nd;
cin >> st.first >> st.second;
cin >> nd.first >> nd.second;
--st.first;
--st.second;
--nd.first;
--nd.second;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> g[i][j];
dist[i][j] = INF;
}
}
dist[st.first][st.second] = 0;
deque<pair<int, int>> q;
q.push_back(st);
while (q.size() > 0) {
pair<int, int> v = q.front();
q.pop_front();
for (auto cur : dir) {
if (v.first + cur.first < h && v.second + cur.second >= 0 &&
v.second + cur.second < w &&
dist[v.first + cur.first][v.second + cur.second] >
dist[v.first][v.second] &&
v.first + cur.first >= 0 &&
g[v.first + cur.first][v.second + cur.second] == '.') {
used[v.first + cur.first][v.second + cur.second] = 1;
dist[v.first + cur.first][v.second + cur.second] =
dist[v.first][v.second];
q.push_front(make_pair(v.first + cur.first, v.second + cur.second));
}
}
for (int j = -2; j < 3; ++j) {
for (int i = -2; i < 3; ++i) {
pair<int, int> cur = {j, i};
if (v.first + cur.first < h && v.first + cur.first >= 0 &&
v.second + cur.second >= 0 && v.second + cur.second < w &&
dist[v.first + cur.first][v.second + cur.second] >
dist[v.first][v.second] + 1 &&
g[v.first + cur.first][v.second + cur.second] == '.') {
used[v.first + cur.first][v.second + cur.second] = 1;
dist[v.first + cur.first][v.second + cur.second] =
dist[v.first][v.second] + 1;
q.push_back(make_pair(v.first + cur.first, v.second + cur.second));
}
}
}
}
if (dist[nd.first][nd.second] == INF) {
cout << "-1\n";
} else {
cout << dist[nd.first][nd.second] << '\n';
}
return 0;
}
| // #pragma GCC optimize("trapv")
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#define int long long
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9 * 1e9 + 10;
void fast() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int sz = 1e3 + 200;
char g[sz][sz];
bool used[sz][sz];
int dist[sz][sz];
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
signed main() {
fast();
int h, w;
cin >> h >> w;
pair<int, int> st = {0, 0}, nd = {0, 0};
cin >> st.first >> st.second;
cin >> nd.first >> nd.second;
--st.first;
--st.second;
--nd.first;
--nd.second;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> g[i][j];
dist[i][j] = INF;
}
}
dist[st.first][st.second] = 0;
deque<pair<int, int>> q;
q.push_back(st);
while (q.size() > 0) {
pair<int, int> v = q.front();
q.pop_front();
for (auto cur : dir) {
if (v.first + cur.first < h && v.second + cur.second >= 0 &&
v.second + cur.second < w &&
dist[v.first + cur.first][v.second + cur.second] >
dist[v.first][v.second] &&
v.first + cur.first >= 0 &&
g[v.first + cur.first][v.second + cur.second] == '.') {
used[v.first + cur.first][v.second + cur.second] = 1;
dist[v.first + cur.first][v.second + cur.second] =
dist[v.first][v.second];
q.push_front(make_pair(v.first + cur.first, v.second + cur.second));
}
}
for (int j = -2; j < 3; ++j) {
for (int i = -2; i < 3; ++i) {
pair<int, int> cur = {j, i};
if (v.first + cur.first < h && v.first + cur.first >= 0 &&
v.second + cur.second >= 0 && v.second + cur.second < w &&
dist[v.first + cur.first][v.second + cur.second] >
dist[v.first][v.second] + 1 &&
g[v.first + cur.first][v.second + cur.second] == '.') {
used[v.first + cur.first][v.second + cur.second] = 1;
dist[v.first + cur.first][v.second + cur.second] =
dist[v.first][v.second] + 1;
q.push_back(make_pair(v.first + cur.first, v.second + cur.second));
}
}
}
}
if (dist[nd.first][nd.second] == INF) {
cout << "-1\n";
} else {
cout << dist[nd.first][nd.second] << '\n';
}
return 0;
}
| replace | 39 | 40 | 39 | 40 | 0 | |
p02579 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ii = pair<int, int>;
int dijkstra(vector<vector<char>> graph, int sourcex, int sourcey, int endx,
int endy) {
priority_queue<pair<int, ii>> pq;
set<pair<int, int>> visited;
pq.push(make_pair(0, ii(sourcex, sourcey)));
while (!pq.empty()) {
pair<int, ii> val = pq.top();
pq.pop();
int d = val.first;
int nodex = val.second.first;
int nodey = val.second.second;
if (visited.find(make_pair(nodex, nodey)) != visited.end()) {
continue;
}
if (nodex == endx && nodey == endy) {
return -d;
}
if (nodex > 0 && graph[nodex - 1][nodey] == '.') {
pq.push(make_pair(d, ii(nodex - 1, nodey)));
}
if (nodex + 1 < graph.size() && graph[nodex + 1][nodey] == '.') {
pq.push(make_pair(d, ii(nodex + 1, nodey)));
}
if (nodey > 0 && graph[nodex][nodey - 1] == '.') {
pq.push(make_pair(d, ii(nodex, nodey - 1)));
}
if (nodey + 1 < graph[0].size() && graph[nodex][nodey + 1] == '.') {
pq.push(make_pair(d, ii(nodex, nodey + 1)));
}
for (int i = nodex - 2; i <= nodex + 2; i++) {
if (i < 0 || i >= graph.size()) {
continue;
}
for (int j = nodey - 2; j <= nodey + 2; j++) {
if (j < 0 || j >= graph[0].size()) {
continue;
}
if (graph[i][j] == '.') {
pq.push(make_pair(d - 1, ii(i, j)));
}
}
}
visited.insert(make_pair(nodex, nodey));
}
}
int main() {
int H, W, Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
cin.get();
vector<vector<char>> maze(W, vector<char>(H));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin.get(maze[j][i]);
}
cin.get();
}
cout << dijkstra(maze, Cw, Ch, Dw, Dh);
} | #include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ii = pair<int, int>;
int dijkstra(vector<vector<char>> graph, int sourcex, int sourcey, int endx,
int endy) {
priority_queue<pair<int, ii>> pq;
set<pair<int, int>> visited;
pq.push(make_pair(0, ii(sourcex, sourcey)));
while (!pq.empty()) {
pair<int, ii> val = pq.top();
pq.pop();
int d = val.first;
int nodex = val.second.first;
int nodey = val.second.second;
if (visited.find(make_pair(nodex, nodey)) != visited.end()) {
continue;
}
if (nodex == endx && nodey == endy) {
return -d;
}
if (nodex > 0 && graph[nodex - 1][nodey] == '.') {
pq.push(make_pair(d, ii(nodex - 1, nodey)));
}
if (nodex + 1 < graph.size() && graph[nodex + 1][nodey] == '.') {
pq.push(make_pair(d, ii(nodex + 1, nodey)));
}
if (nodey > 0 && graph[nodex][nodey - 1] == '.') {
pq.push(make_pair(d, ii(nodex, nodey - 1)));
}
if (nodey + 1 < graph[0].size() && graph[nodex][nodey + 1] == '.') {
pq.push(make_pair(d, ii(nodex, nodey + 1)));
}
for (int i = nodex - 2; i <= nodex + 2; i++) {
if (i < 0 || i >= graph.size()) {
continue;
}
for (int j = nodey - 2; j <= nodey + 2; j++) {
if (j < 0 || j >= graph[0].size()) {
continue;
}
if (graph[i][j] == '.') {
pq.push(make_pair(d - 1, ii(i, j)));
}
}
}
visited.insert(make_pair(nodex, nodey));
}
return -1;
}
int main() {
int H, W, Ch, Cw, Dh, Dw;
cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
cin.get();
vector<vector<char>> maze(W, vector<char>(H));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin.get(maze[j][i]);
}
cin.get();
}
cout << dijkstra(maze, Cw, Ch, Dw, Dh);
} | insert | 55 | 55 | 55 | 56 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define UP(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define UU(a, b, c) for (ll(a) = (b); (a) <= (c); (a)++)
#define DN(a, b, c) for (ll(a) = (b); (a) > (c); (a)--)
#define DU(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)
int r, c, sx, sy, ex, ey;
char grid[3005][3005];
void reset() {}
void input() {
cin >> r >> c;
cin >> sy >> sx;
cin >> ey >> ex;
UU(j, 1, r) {
UU(i, 1, c) { cin >> grid[j][i]; }
}
}
int mx[] = {1, -1, 0, 0};
int my[] = {0, 0, 1, -1};
bool vis[3005][3005] = {0};
int djikstra() {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>>
pq;
pq.push({0, {sx, sy}});
while (!pq.empty()) {
int cost = pq.top().fi, x = pq.top().se.fi, y = pq.top().se.se;
pq.pop();
vis[y][x] = 1;
if (y == ey && x == ex) {
return cost;
}
UU(j, -2, 2) {
UU(i, -2, 2) {
int tx = x + i, ty = y + j;
if (tx < 0 || tx > c || ty < 0 || ty > r) {
continue;
} else if (vis[ty][tx] == 0 && grid[ty][tx] == '.') {
if (abs(j) + abs(i) == 1) {
pq.push({cost, {tx, ty}});
} else {
pq.push({cost + 1, {tx, ty}});
}
}
}
}
}
return -1;
}
void solve() { cout << djikstra() << endl; }
void LetsRock() { solve(); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
input();
reset();
LetsRock();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define UP(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define UU(a, b, c) for (ll(a) = (b); (a) <= (c); (a)++)
#define DN(a, b, c) for (ll(a) = (b); (a) > (c); (a)--)
#define DU(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)
int r, c, sx, sy, ex, ey;
char grid[3005][3005];
void reset() {}
void input() {
cin >> r >> c;
cin >> sy >> sx;
cin >> ey >> ex;
UU(j, 1, r) {
UU(i, 1, c) { cin >> grid[j][i]; }
}
}
int mx[] = {1, -1, 0, 0};
int my[] = {0, 0, 1, -1};
bool vis[3005][3005] = {0};
int djikstra() {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>>
pq;
pq.push({0, {sx, sy}});
while (!pq.empty()) {
int cost = pq.top().fi, x = pq.top().se.fi, y = pq.top().se.se;
pq.pop();
if (vis[y][x] == 0) {
vis[y][x] = 1;
if (y == ey && x == ex) {
return cost;
}
UU(j, -2, 2) {
UU(i, -2, 2) {
int tx = x + i, ty = y + j;
if (tx < 0 || tx > c || ty < 0 || ty > r) {
continue;
} else if (vis[ty][tx] == 0 && grid[ty][tx] == '.') {
if (abs(j) + abs(i) == 1) {
pq.push({cost, {tx, ty}});
} else {
pq.push({cost + 1, {tx, ty}});
}
}
}
}
}
}
return -1;
}
void solve() { cout << djikstra() << endl; }
void LetsRock() { solve(); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
input();
reset();
LetsRock();
return 0;
}
| replace | 40 | 54 | 40 | 56 | TLE | |
p02579 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define int ll
const ll MOD = 1000000007;
const ll INF_LL = (ll)1000000007 * 1000000007;
const int INF_INT = (int)1000000007;
const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[20] = {2, 2, 2, 2, 2, 1, 1, 1, 1, 0,
0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int dy2[20] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define m0(x) memset((x), 0, sizeof((x)))
#define m1(x) memset((x), -1, sizeof((x)))
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define RALL(x) (x).rbegin(), (x).rend() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める
#define all(x) (x).begin(), (x).end()
// 略記
#define pb emplace_back // vectorヘの挿入
#define mp make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
#define BITCOUNT __builtin_popcount
#define BITCOUNT_LL(x) __builtin_popcountll(x)
#define perm(c) \
sort(all(c)); \
for (bool c##p = 1; c##p; \
c##p = next_permutation(all(c))) // 順列 123 132 213 231 312 321
#define BIT(n) (1LL << (n))
#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ": " << (A) << std::endl;
#else
#define PRINT(A)
#endif
// 入力高速化
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
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;
int cost;
Edge(int a, int b) {
to = a;
cost = b;
}
};
int H, W;
int Ch, Cw, Dh, Dw;
vector<string> S(100);
// sに行先を入れると、dist[to]にコストが入ります
const ll MAX_NODE_COUNT = 1000000; // nodeの最大数 更新必要 CINなどで更新でも可
vector<ll> dist;
vector<Edge> edge;
void dijkstra(int s) {
// greater<P>
// の指定で、Pを小さい順に取り出せる。P.firstに距離やコストを入れておけばOK。
priority_queue<P, vector<P>, greater<P>> que;
dist.assign(
H * W + 5,
INF_LL); // 本当は+5いらないけど、nodeが0ではなく1~の時ように余裕もたせておく。
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
// 現時点で一番低コストになっているQueを読み込む
P p = que.top();
que.pop();
// 現在見てる頂点番号を確認する
int v = p.second;
// 過去の最低コストを確認し、現在のコストより低コストの時点で探索済みだったら帰る。
if (dist[v] < p.first)
continue;
rep(i, 4) {
// 今のNodeからいける範囲で、値を更新できそうなところをQueに入れとく。
int nowX = v % W;
int nowY = v / W;
int distX = nowX + dx[i];
int distY = nowY + dy[i];
if (distX < 0 || distX >= W || distY < 0 || distY >= H)
continue;
if (S[distY][distX] == '#')
continue;
if (dist[distX + distY * W] > dist[v]) {
dist[distX + distY * W] = dist[v];
que.push(P(dist[distX + distY * W], distX + distY * W));
}
}
rep(i, 20) {
// 今のNodeからいける範囲で、値を更新できそうなところをQueに入れとく。
int nowX = v % W;
int nowY = v / W;
int distX = nowX + dx2[i];
int distY = nowY + dy2[i];
if (distX < 0 || distX >= W || distY < 0 || distY >= H)
continue;
if (S[distY][distX] == '#')
continue;
if (dist[distX + distY * W] > dist[v] + 1) {
dist[distX + distY * W] = dist[v] + 1;
que.push(P(dist[distX + distY * W], distX + distY * W));
}
}
}
}
signed main() {
cin >> H >> W;
cin >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
rep(i, H) { cin >> S[i]; }
dijkstra(W * Ch + Cw);
if (dist[Dh * W + Dw] == INF_LL)
dist[Dh * W + Dw] = -1;
cout << dist[Dh * W + Dw] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define int ll
const ll MOD = 1000000007;
const ll INF_LL = (ll)1000000007 * 1000000007;
const int INF_INT = (int)1000000007;
const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[20] = {2, 2, 2, 2, 2, 1, 1, 1, 1, 0,
0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int dy2[20] = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2,
2, -2, -1, 1, 2, -2, -1, 0, 1, 2};
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define m0(x) memset((x), 0, sizeof((x)))
#define m1(x) memset((x), -1, sizeof((x)))
// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define RALL(x) (x).rbegin(), (x).rend() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) // 最大値を求める
#define MIN(x) *min_element(ALL(x)) // 最小値を求める
#define all(x) (x).begin(), (x).end()
// 略記
#define pb emplace_back // vectorヘの挿入
#define mp make_pair // pairのコンストラクタ
#define F first // pairの一つ目の要素
#define S second // pairの二つ目の要素
#define BITCOUNT __builtin_popcount
#define BITCOUNT_LL(x) __builtin_popcountll(x)
#define perm(c) \
sort(all(c)); \
for (bool c##p = 1; c##p; \
c##p = next_permutation(all(c))) // 順列 123 132 213 231 312 321
#define BIT(n) (1LL << (n))
#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ": " << (A) << std::endl;
#else
#define PRINT(A)
#endif
// 入力高速化
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
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;
int cost;
Edge(int a, int b) {
to = a;
cost = b;
}
};
int H, W;
int Ch, Cw, Dh, Dw;
vector<string> S(10000);
// sに行先を入れると、dist[to]にコストが入ります
const ll MAX_NODE_COUNT = 1000000; // nodeの最大数 更新必要 CINなどで更新でも可
vector<ll> dist;
vector<Edge> edge;
void dijkstra(int s) {
// greater<P>
// の指定で、Pを小さい順に取り出せる。P.firstに距離やコストを入れておけばOK。
priority_queue<P, vector<P>, greater<P>> que;
dist.assign(
H * W + 5,
INF_LL); // 本当は+5いらないけど、nodeが0ではなく1~の時ように余裕もたせておく。
dist[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
// 現時点で一番低コストになっているQueを読み込む
P p = que.top();
que.pop();
// 現在見てる頂点番号を確認する
int v = p.second;
// 過去の最低コストを確認し、現在のコストより低コストの時点で探索済みだったら帰る。
if (dist[v] < p.first)
continue;
rep(i, 4) {
// 今のNodeからいける範囲で、値を更新できそうなところをQueに入れとく。
int nowX = v % W;
int nowY = v / W;
int distX = nowX + dx[i];
int distY = nowY + dy[i];
if (distX < 0 || distX >= W || distY < 0 || distY >= H)
continue;
if (S[distY][distX] == '#')
continue;
if (dist[distX + distY * W] > dist[v]) {
dist[distX + distY * W] = dist[v];
que.push(P(dist[distX + distY * W], distX + distY * W));
}
}
rep(i, 20) {
// 今のNodeからいける範囲で、値を更新できそうなところをQueに入れとく。
int nowX = v % W;
int nowY = v / W;
int distX = nowX + dx2[i];
int distY = nowY + dy2[i];
if (distX < 0 || distX >= W || distY < 0 || distY >= H)
continue;
if (S[distY][distX] == '#')
continue;
if (dist[distX + distY * W] > dist[v] + 1) {
dist[distX + distY * W] = dist[v] + 1;
que.push(P(dist[distX + distY * W], distX + distY * W));
}
}
}
}
signed main() {
cin >> H >> W;
cin >> Ch >> Cw >> Dh >> Dw;
Ch--;
Cw--;
Dh--;
Dw--;
rep(i, H) { cin >> S[i]; }
dijkstra(W * Ch + Cw);
if (dist[Dh * W + Dw] == INF_LL)
dist[Dh * W + Dw] = -1;
cout << dist[Dh * W + Dw] << endl;
} | replace | 100 | 101 | 100 | 101 | 0 | |
p02579 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
int main() {
ll H, W, ch, cw, dh, dw;
cin >> H >> W >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
string field[1000];
for (ll i = 0; i < H; i++) {
cin >> field[i];
}
deque<pair<ll, ll>> Q;
ll cost[1020][1020];
for (ll i = 0; i < 1020; i++) {
for (ll j = 0; j < 1020; j++) {
cost[i][j] = INF;
}
}
cost[ch][cw] = 0;
Q.push_front({ch, cw});
while (!Q.empty()) {
pair<ll, ll> t = Q.front();
Q.pop_front();
ll i = t.first, j = t.second;
for (ll k = 0; k < 4; k++) {
ll x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= H)
continue;
if (y < 0 || y >= W)
continue;
if (field[x][y] != '.')
continue;
if (cost[x][y] <= cost[i][j])
continue;
Q.push_front({x, y});
cost[x][y] = cost[i][j];
}
for (ll k = -2; k <= 2; k++) {
for (ll l = -2; l <= 2; l++) {
ll x = i + k, y = j + l;
if (x < 0 || x >= H)
continue;
if (y < 0 || y >= W)
continue;
if (field[x][y] != '.')
continue;
if (cost[x][y] <= cost[i][j] + 1)
continue;
Q.push_front({x, y});
cost[x][y] = cost[i][j] + 1;
}
}
}
if (cost[dh][dw] == INF) {
cout << -1 << endl;
} else {
cout << cost[dh][dw] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
int main() {
ll H, W, ch, cw, dh, dw;
cin >> H >> W >> ch >> cw >> dh >> dw;
ch--;
cw--;
dh--;
dw--;
string field[1000];
for (ll i = 0; i < H; i++) {
cin >> field[i];
}
deque<pair<ll, ll>> Q;
ll cost[1020][1020];
for (ll i = 0; i < 1020; i++) {
for (ll j = 0; j < 1020; j++) {
cost[i][j] = INF;
}
}
cost[ch][cw] = 0;
Q.push_front({ch, cw});
while (!Q.empty()) {
pair<ll, ll> t = Q.front();
Q.pop_front();
ll i = t.first, j = t.second;
for (ll k = 0; k < 4; k++) {
ll x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= H)
continue;
if (y < 0 || y >= W)
continue;
if (field[x][y] != '.')
continue;
if (cost[x][y] <= cost[i][j])
continue;
Q.push_front({x, y});
cost[x][y] = cost[i][j];
}
for (ll k = -2; k <= 2; k++) {
for (ll l = -2; l <= 2; l++) {
ll x = i + k, y = j + l;
if (x < 0 || x >= H)
continue;
if (y < 0 || y >= W)
continue;
if (field[x][y] != '.')
continue;
if (cost[x][y] <= cost[i][j] + 1)
continue;
Q.push_back({x, y});
cost[x][y] = cost[i][j] + 1;
}
}
}
if (cost[dh][dw] == INF) {
cout << -1 << endl;
} else {
cout << cost[dh][dw] << endl;
}
} | replace | 57 | 58 | 57 | 58 | TLE | |
p02579 | 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 ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
using PP = pair<int, P>;
typedef long long ll;
bool visited[1000][1000];
int dist[1000][1000];
string grid[1000];
int h, w, sy, sx, gy, gx;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int t_dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0, 2, 2, -2, -2};
const int t_dy[16] = {0, 1, 0, -1, 1, -1, 1, -1, 0, 2, 0, -2, 2, -2, 2, -2};
const int INF = (1 << 30);
// 二次元グリッドグラフでのダイクストラ法
void dijkstra(int y, int x) {
dist[y][x] = 0;
priority_queue<PP, vector<PP>, greater<PP>> que;
que.push(PP(0, P(y, x)));
while (!que.empty()) {
PP p = que.top();
que.pop();
int cy = p.second.first, cx = p.second.second;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i], nx = cx + dx[i];
if (ny < 0 || h <= ny || nx < 0 || w <= nx)
continue;
if (grid[ny][nx] == '#')
continue;
if (dist[ny][nx] <= dist[cy][cx])
continue;
dist[ny][nx] = dist[cy][cx];
que.push(PP(dist[ny][nx], P(ny, nx)));
}
for (int i = cy - 2; i <= cy + 2; i++) {
for (int j = cx - 2; j <= cx + 2; j++) {
if (i < 0 || h <= i || i < 0 || w <= j)
continue;
if (grid[i][j] == '#')
continue;
if (dist[i][j] <= dist[cy][cx] + 1)
continue;
dist[i][j] = dist[cy][cx] + 1;
que.push(PP(dist[i][j], P(i, j)));
}
}
}
return;
}
int main() {
cin >> h >> w;
cin >> sy >> sx;
sy--, sx--;
cin >> gy >> gx;
gy--;
gx--;
rep(i, h) {
rep(j, w) { dist[i][j] = INF; }
}
rep(i, h) cin >> grid[i];
dijkstra(sy, sx);
if (dist[gy][gx] == INF) {
cout << -1 << endl;
} else {
cout << dist[gy][gx] << endl;
}
} | #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 ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
using PP = pair<int, P>;
typedef long long ll;
bool visited[1000][1000];
int dist[1000][1000];
string grid[1000];
int h, w, sy, sx, gy, gx;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int t_dx[16] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0, 2, 2, -2, -2};
const int t_dy[16] = {0, 1, 0, -1, 1, -1, 1, -1, 0, 2, 0, -2, 2, -2, 2, -2};
const int INF = (1 << 30);
// 二次元グリッドグラフでのダイクストラ法
void dijkstra(int y, int x) {
dist[y][x] = 0;
priority_queue<PP, vector<PP>, greater<PP>> que;
que.push(PP(0, P(y, x)));
while (!que.empty()) {
PP p = que.top();
que.pop();
int cy = p.second.first, cx = p.second.second;
for (int i = 0; i < 4; i++) {
int ny = cy + dy[i], nx = cx + dx[i];
if (ny < 0 || h <= ny || nx < 0 || w <= nx)
continue;
if (grid[ny][nx] == '#')
continue;
if (dist[ny][nx] <= dist[cy][cx])
continue;
dist[ny][nx] = dist[cy][cx];
que.push(PP(dist[ny][nx], P(ny, nx)));
}
for (int i = cy - 2; i <= cy + 2; i++) {
for (int j = cx - 2; j <= cx + 2; j++) {
if (i < 0 || h <= i || j < 0 || w <= j)
continue;
if (grid[i][j] == '#')
continue;
if (dist[i][j] <= dist[cy][cx] + 1)
continue;
dist[i][j] = dist[cy][cx] + 1;
que.push(PP(dist[i][j], P(i, j)));
}
}
}
return;
}
int main() {
cin >> h >> w;
cin >> sy >> sx;
sy--, sx--;
cin >> gy >> gx;
gy--;
gx--;
rep(i, h) {
rep(j, w) { dist[i][j] = INF; }
}
rep(i, h) cin >> grid[i];
dijkstra(sy, sx);
if (dist[gy][gx] == INF) {
cout << -1 << endl;
} else {
cout << dist[gy][gx] << endl;
}
} | replace | 41 | 42 | 41 | 42 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.