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
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; vector<int> mark(maxn); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; ++mark[a[i]]; } for (int i = n - 1; i >= 0; i--) { int e = a[i]; if (mark[e] > 1) mark[e] = -1; for (int j = e + e; j < maxn; j += e) { if (mark[j] == -1) continue; if (mark[j] > 0) mark[j] = -1; } } int ans = 0; for (int i = 0; i < n; i++) { if (mark[a[i]] != -1) ++ans; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; vector<int> mark(maxn); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; ++mark[a[i]]; } if (mark[1] > 0) { if (mark[1] > 1) cout << "0" << endl; else if (mark[1] == 1) cout << "1" << endl; return 0; } for (int i = n - 1; i >= 0; i--) { int e = a[i]; if (mark[e] > 1) mark[e] = -1; for (int j = e + e; j < maxn; j += e) { if (mark[j] == -1) continue; if (mark[j] > 0) mark[j] = -1; } } int ans = 0; for (int i = 0; i < n; i++) { if (mark[a[i]] != -1) ++ans; } cout << ans << endl; }
insert
19
19
19
26
TLE
p02642
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <math.h> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i < (n); i++) const int M = 1000005; int main() { int n; cin >> n; vector<int> a(n); vector<int> c(M); rep(i, n) cin >> a[i]; for (int j : a) { for (int i = j; i <= M; i += j) { c[i]++; } } int ans = 0; for (int j : a) { if (c[j] == 1) ans++; } cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <iostream> #include <math.h> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i < (n); i++) const int M = 1000005; int main() { int n; cin >> n; vector<int> a(n); vector<int> c(M); rep(i, n) cin >> a[i]; for (int j : a) { if (c[j] > 0) { c[j] = 2; continue; } for (int i = j; i <= M; i += j) { c[i]++; } } int ans = 0; for (int j : a) { if (c[j] == 1) ans++; } cout << ans << endl; }
insert
18
18
18
22
TLE
p02642
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> #define cuttime ios::sync_with_stdio(false) #define swap(x, y) x ^= y, y ^= x, x ^= y #define INF 0x3f3f3f3f #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define dep(i, a, b) for (int i = (a); i >= (b); --i) #define scan1(x) scanf("%d", &x) #define scan2(x, y) scanf("%d%d", &x, &y) #define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanll1(x) scanf("%lld", &x) #define scanll2(x, y) scanf("%lld%lld", &x, &y) #define scanll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define lowbit(x) (x & (-x)) #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 #define debug(x) cout << #x << ": " << x << endl using namespace std; typedef unsigned long long ull; typedef long long ll; const int N = 2e5 + 20; const int MAX = 10000007; inline ll read() { char c = getchar(); ll x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0', c = getchar(); } return x * f; } inline void out(ll x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); } int q[N]; map<int, int> mp, mp1; int main() { int n; n = read(); rep(i, 1, n) { q[i] = read(); mp[q[i]]++; mp1[q[i]] = 1; } int ans = 0; rep(i, 1, 1e6) { for (int j = 1; j <= 1e6 / i; ++j) { if (mp[i * j] != 0) { mp[i * j]--; if (mp[i] != 0) mp1[i * j] = 0; mp[i * j]++; } } } rep(i, 1, n) { if (mp1[q[i]]) ans++; } cout << ans; return 0; } /* */
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> #define cuttime ios::sync_with_stdio(false) #define swap(x, y) x ^= y, y ^= x, x ^= y #define INF 0x3f3f3f3f #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define dep(i, a, b) for (int i = (a); i >= (b); --i) #define scan1(x) scanf("%d", &x) #define scan2(x, y) scanf("%d%d", &x, &y) #define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanll1(x) scanf("%lld", &x) #define scanll2(x, y) scanf("%lld%lld", &x, &y) #define scanll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define lowbit(x) (x & (-x)) #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 #define debug(x) cout << #x << ": " << x << endl using namespace std; typedef unsigned long long ull; typedef long long ll; const int N = 2e5 + 20; const int MAX = 10000007; inline ll read() { char c = getchar(); ll x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0', c = getchar(); } return x * f; } inline void out(ll x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); } int q[N]; int mp[1000005]; bool mp1[1000005]; int main() { int n; n = read(); rep(i, 1, n) { q[i] = read(); mp[q[i]]++; mp1[q[i]] = 1; } int ans = 0; rep(i, 1, 1e6) { for (int j = 1; j <= 1e6 / i; ++j) { if (mp[i * j] != 0) { mp[i * j]--; if (mp[i] != 0) mp1[i * j] = 0; mp[i * j]++; } } } rep(i, 1, n) { if (mp1[q[i]]) ans++; } cout << ans; return 0; } /* */
replace
53
54
53
55
TLE
p02642
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; int y[1000010]; int same[1000010]; bool divisor(int n) { for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (y[i] == 1 || y[n / i] == 1) { return false; } } } return true; } int main() { int n; cin >> n; int a[100010]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); memset(y, 0, sizeof y); memset(same, 0, sizeof same); for (int i = 0; i < n; i++) { same[a[i]]++; } int ans = 0; for (int i = 0; i < n; i++) { if (divisor(a[i]) == true && same[a[i]] <= 1) { ans++; } y[a[i]] = 1; } cout << ans << endl; }
#include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; int y[1000010]; int same[1000010]; bool divisor(int n) { for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (y[i] == 1 || y[n / i] == 1) { return false; } } } return true; } int main() { int n; cin >> n; int a[200010]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); memset(y, 0, sizeof y); memset(same, 0, sizeof same); for (int i = 0; i < n; i++) { same[a[i]]++; } int ans = 0; for (int i = 0; i < n; i++) { if (divisor(a[i]) == true && same[a[i]] <= 1) { ans++; } y[a[i]] = 1; } cout << ans << endl; }
replace
23
24
23
24
0
p02642
C++
Runtime Error
#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[i]; } sort(a.begin(), a.end()); long long M = a[n - 1]; long long d[200000] = {0}; for (int i = 0; i < n; i++) { for (int j = 1; j * a[i] <= M; j++) { d[a[i] * j]++; } } /* for(int i = 1;i <= M;i++){ cout << d[i] << " "; } cout << endl; */ int ans = 0; for (int i = 0; i < n; i++) { if (d[a[i]] == 1) { ans += 1; } } cout << ans << 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[i]; } sort(a.begin(), a.end()); long long M = a[n - 1]; long long d[1000000 + 10] = {0}; for (int i = 0; i < n; i++) { for (int j = 1; j * a[i] <= M; j++) { d[a[i] * j]++; } } /* for(int i = 1;i <= M;i++){ cout << d[i] << " "; } cout << endl; */ int ans = 0; for (int i = 0; i < n; i++) { if (d[a[i]] == 1) { ans += 1; } } cout << ans << endl; }
replace
13
14
13
14
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define endl "\n" #define loop(i, s, e) for (int i = s; i < e; i++) #define int long long #define vi vector<int> #define S(v) sort(v.begin(), v.end()) #define RS(v) sort(v.rbegin(), v.rend()) #define R(v) reverse(v.begin(), v.end()) #define mxpq(T) priority_queue<T> #define mnpq(T) priority_queue<T, vector<T>, greater<T>> #define prv(v) \ for (auto &x : v) \ cout << x << " "; #define piirv(v) \ for (auto &x : v) \ cout << x.ff << " " << x.ss << endl; #define prvv(v) \ for (auto &x : v) \ for (auto &y : x.ss) \ cout << y << " "; #define mp make_pair #define pii pair<int, int> #define mset(a, f) memset(a, f, sizeof(a)) #define pb push_back #define inf INT_MAX #define INF LLONG_MAX typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; const int N = 1e6 + 6; const int M = 1e9 + 7; int A[N]; int lp[N]; int n, m; vector<int> prime; void linear_sieve() { mset(lp, 0); prime.clear(); loop(i, 2, N) { if (lp[i] == 0) { prime.pb(i); lp[i] = i; } for (int j = 0; j < (int)prime.size() && i * prime[j] < N; j++) { lp[i * prime[j]] = prime[j]; } } } void solve() { int n; cin >> n; map<int, int> mm; map<int, int> id; linear_sieve(); loop(i, 0, n) { cin >> A[i]; mm[A[i]]++; } map<int, int> frq; vector<int> ans; int cnt = 0; if (mm.find(1) != mm.end()) { if (mm[1] == 1) cout << 1 << endl; else cout << 0 << endl; return; } for (auto &it : mm) { if (it.ss > 1) { frq[it.ff]++; continue; } if (lp[it.ff] == it.ff) { cnt++; frq[it.ff]++; continue; } int ok = 0; for (int i = 1; i * i <= it.ff; i++) { if (it.ff % i == 0 && (frq[i] > 0 || frq[it.ff / i] > 0)) { ok = 1; break; } } frq[it.ff]++; if (!ok) { cnt++; // cout << it.ff << " " << it.ss << endl; } } cout << cnt; } int32_t main() { cout << fixed << setprecision(16); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define endl "\n" #define loop(i, s, e) for (int i = s; i < e; i++) #define int long long #define vi vector<int> #define S(v) sort(v.begin(), v.end()) #define RS(v) sort(v.rbegin(), v.rend()) #define R(v) reverse(v.begin(), v.end()) #define mxpq(T) priority_queue<T> #define mnpq(T) priority_queue<T, vector<T>, greater<T>> #define prv(v) \ for (auto &x : v) \ cout << x << " "; #define piirv(v) \ for (auto &x : v) \ cout << x.ff << " " << x.ss << endl; #define prvv(v) \ for (auto &x : v) \ for (auto &y : x.ss) \ cout << y << " "; #define mp make_pair #define pii pair<int, int> #define mset(a, f) memset(a, f, sizeof(a)) #define pb push_back #define inf INT_MAX #define INF LLONG_MAX typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; const int N = 1e6 + 6; const int M = 1e9 + 7; int A[N]; int lp[N]; int n, m; vector<int> prime; void linear_sieve() { mset(lp, 0); prime.clear(); loop(i, 2, N) { if (lp[i] == 0) { prime.pb(i); lp[i] = i; } for (int j = 0; j < (int)prime.size() && i * prime[j] < N; j++) { lp[i * prime[j]] = prime[j]; } } } void solve() { int n; cin >> n; map<int, int> mm; map<int, int> id; linear_sieve(); loop(i, 0, n) { cin >> A[i]; mm[A[i]]++; } map<int, int> frq; vector<int> ans; int cnt = 0; if (mm.find(1) != mm.end()) { if (mm[1] == 1) cout << 1 << endl; else cout << 0 << endl; return; } for (auto &it : mm) { if (it.ss > 1) { frq[it.ff]++; continue; } if (lp[it.ff] == it.ff) { cnt++; frq[it.ff]++; continue; } int ok = 0; for (int i = 1; i * i <= it.ff; i++) { if (it.ff % i == 0 && (frq.find(i) != frq.end() || frq.find(it.ff / i) != frq.end())) { ok = 1; break; } } frq[it.ff]++; if (!ok) { cnt++; // cout << it.ff << " " << it.ss << endl; } } cout << cnt; } int32_t main() { cout << fixed << setprecision(16); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); }
replace
99
100
99
101
TLE
p02642
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; const int INF = 5000; int main() { int n; cin >> n; int M = 1e05 + 5; vector<int> a(n), cnt(M); rep(i, n) cin >> a[i]; for (int x : a) { if (cnt[x] != 0) { cnt[x] = 2; continue; } for (int i = x; i < M; i += x) cnt[i]++; } int ans = 0; for (int x : a) if (cnt[x] == 1) ans++; cout << ans << endl; return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; const int INF = 5000; int main() { int n; cin >> n; int M = 1e06 + 5; vector<int> a(n), cnt(M); rep(i, n) cin >> a[i]; for (int x : a) { if (cnt[x] != 0) { cnt[x] = 2; continue; } for (int i = x; i < M; i += x) cnt[i]++; } int ans = 0; for (int x : a) if (cnt[x] == 1) ans++; cout << ans << endl; return 0; }
replace
10
11
10
11
0
p02642
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++) int main() { ll N; cin >> N; vector<ll> A(N); map<ll, ll> mA; ll ma = 0; rep(i, 0, N) { cin >> A[i]; mA[A[i]]++; ma = max(ma, A[i]); } sort(A.begin(), A.end()); vector<bool> dp(1000010, true); // dp[i] : iより小さいiの約数が存在しなかったらtrue for (auto &a : A) { ll cur = a; int cnt = 2; while (cur <= ma) { cur = a * cnt; dp[cur] = false; cnt++; } } ll ans = 0; for (auto &a : A) { if (dp[a] && mA[a] < 2) { ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define rep(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++) int main() { ll N; cin >> N; vector<ll> A(N); map<ll, ll> mA; ll ma = 0; rep(i, 0, N) { cin >> A[i]; mA[A[i]]++; ma = max(ma, A[i]); } sort(A.begin(), A.end()); vector<bool> dp(10000100, true); // dp[i] : iより小さいiの約数が存在しなかったらtrue for (auto &a : A) { ll cur = a; int cnt = 2; while (cur <= ma) { cur = a * cnt; dp[cur] = false; cnt++; } } ll ans = 0; for (auto &a : A) { if (dp[a] && mA[a] < 2) { ans++; } } cout << ans << endl; }
replace
18
19
18
19
0
p02642
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #define N 200010 using namespace std; int a[N]; map<int, int> mp; int main() { // x+y=a // 2x+4y=b int n; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); mp.clear(); for (int i = 0; i < n; i++) { if (i != 0 && a[i] == a[i - 1]) { mp[a[i]] = 1; continue; } for (int j = a[i] + a[i]; j <= 1000000; j += a[i]) mp[j] = 1; } int ans = 0; for (int i = 0; i < n; i++) { if (!mp[a[i]]) ans++; } printf("%d\n", ans); } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #define N 200010 using namespace std; int a[N]; map<int, int> mp; int main() { // x+y=a // 2x+4y=b int n; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); mp.clear(); for (int i = 0; i < n; i++) { if (i != 0 && a[i] == a[i - 1]) { mp[a[i]] = 1; continue; } if (!mp[a[i]]) for (int j = a[i] + a[i]; j <= a[n - 1]; j += a[i]) mp[j] = 1; } int ans = 0; for (int i = 0; i < n; i++) { if (!mp[a[i]]) ans++; } printf("%d\n", ans); } return 0; }
replace
25
27
25
28
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define l_ength size #pragma GCC target("avx") #pragma GCC optimize("unroll-loops") const int inf = (1 << 30); const int mod = 1000000007; // 998244353 using ll = long long; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (auto &k : a) cin >> k; sort(a.begin(), a.end()); int bound = a.back(); if (a[0] == 1) { if (a[1] == 1) { cout << 0 << endl; return 0; } else { cout << 1 << endl; return 0; } } vector<int> ans(n, 1); for (auto itr = a.begin(); itr != a.end(); ++itr) { int k = *itr; auto tmp = itr; ++tmp; if (tmp != a.end() and *itr == *tmp) { ans[itr - a.begin()] = ans[tmp - a.begin()] = 0; } int cnt = 1; while (k * cnt <= bound) { int l = lower_bound(tmp, a.end(), k * cnt) - a.begin(); int r = upper_bound(tmp, a.end(), k * cnt) - a.begin(); for (int t = l; l != r and t < r; ++t) { ans[t] = 0; } ++cnt; } } cout << accumulate(ans.begin(), ans.end(), 0) << endl; }
#include <bits/stdc++.h> #define l_ength size #pragma GCC target("avx") #pragma GCC optimize("unroll-loops") const int inf = (1 << 30); const int mod = 1000000007; // 998244353 using ll = long long; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (auto &k : a) cin >> k; sort(a.begin(), a.end()); int bound = a.back(); if (a[0] == 1) { if (a[1] == 1) { cout << 0 << endl; return 0; } else { cout << 1 << endl; return 0; } } vector<int> ans(n, 1); for (auto itr = a.begin(); itr != a.end(); ++itr) { int k = *itr; auto tmp = itr; ++tmp; if (ans[itr - a.begin()] == 0) continue; if (tmp != a.end() and *itr == *tmp) { ans[itr - a.begin()] = ans[tmp - a.begin()] = 0; } int cnt = 1; while (k * cnt <= bound) { int l = lower_bound(tmp, a.end(), k * cnt) - a.begin(); int r = upper_bound(tmp, a.end(), k * cnt) - a.begin(); for (int t = l; l != r and t < r; ++t) { ans[t] = 0; } ++cnt; } } cout << accumulate(ans.begin(), ans.end(), 0) << endl; }
insert
33
33
33
35
TLE
p02642
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define rep(i, a, b) for (i = a; i < b; i++) ll int large = (ll int)1e18; ll int mod = (ll int)1e9 + 7; ll int const N = (ll int)1e6 + 10; ll int mod_exp(ll int x, ll int n) { ll int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % mod; x = (x * x) % mod; n = n / 2; } return result; } ll int mod_inverse(ll int a) { return mod_exp(a, mod - 2); } ll int const sz = 200005; ll int seive[sz]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll int T, i, j, ij; T = 1; // cin>>T; while (T--) { ll int n; cin >> n; ll int a[n]; rep(i, 0, n) cin >> a[i]; ll int num1 = count(a, a + n, 1); if (num1 > 1) { cout << 0; } else if (num1 == 1) { cout << 1; } else { for (ll int i = 0; i < n; i++) { for (ll int j = a[i]; j < sz; j += a[i]) seive[j]++; } ll int ans = 0; rep(i, 0, n) { if (seive[a[i]] == 1) ans++; } cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define rep(i, a, b) for (i = a; i < b; i++) ll int large = (ll int)1e18; ll int mod = (ll int)1e9 + 7; ll int const N = (ll int)1e6 + 10; ll int mod_exp(ll int x, ll int n) { ll int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % mod; x = (x * x) % mod; n = n / 2; } return result; } ll int mod_inverse(ll int a) { return mod_exp(a, mod - 2); } ll int const sz = N; ll int seive[sz]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll int T, i, j, ij; T = 1; // cin>>T; while (T--) { ll int n; cin >> n; ll int a[n]; rep(i, 0, n) cin >> a[i]; ll int num1 = count(a, a + n, 1); if (num1 > 1) { cout << 0; } else if (num1 == 1) { cout << 1; } else { for (ll int i = 0; i < n; i++) { for (ll int j = a[i]; j < sz; j += a[i]) seive[j]++; } ll int ans = 0; rep(i, 0, n) { if (seive[a[i]] == 1) ans++; } cout << ans << endl; } } return 0; }
replace
22
23
22
23
0
p02642
C++
Runtime Error
#include <iostream> #include <vector> const int MAXVAL = 1e2; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); vector<int> cnt(MAXVAL + 1); vector<int> ans(MAXVAL + 1, 1); for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; } for (int i = 1; i <= MAXVAL; i++) { if (cnt[i] > 1) ans[i] = 0; if (!cnt[i]) continue; for (int j = 2 * i; j <= MAXVAL; j += i) ans[j] = 0; } int answer = 0; for (int i = 0; i < n; i++) answer += ans[a[i]]; cout << answer; return 0; }
#include <iostream> #include <vector> const int MAXVAL = 1e6; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); vector<int> cnt(MAXVAL + 1); vector<int> ans(MAXVAL + 1, 1); for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; } for (int i = 1; i <= MAXVAL; i++) { if (cnt[i] > 1) ans[i] = 0; if (!cnt[i]) continue; for (int j = 2 * i; j <= MAXVAL; j += i) ans[j] = 0; } int answer = 0; for (int i = 0; i < n; i++) answer += ans[a[i]]; cout << answer; return 0; }
replace
3
4
3
4
0
p02642
C++
Runtime Error
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define debug(x) cout << #x << ':' << x << endl; #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, n) for (int i = 1; i <= n; i++) #define make_unique(v) \ sort(all(v)); \ v.erase(unique(all(v)), v.end()); #define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end()); #define pb push_back #define mp make_pair #define endl '\n' #define fi first #define se second #define ajs(a) (a < 0) ? (-1 * a) : a using namespace std; typedef long long ll; typedef vector<long long> vll; typedef pair<ll, ll> pll; typedef vector<int> vi; const ll MOD = 1e9 + 7; const int dx4[4] = {0, 1, 0, -1}; const int dy4[4] = {-1, 0, 1, 0}; const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; bool cmp(int X, int Y) { return X > Y; } ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } ll modpow(ll a, ll b, ll m = MOD) { a %= m; ll res = 1; while (b) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } ll bpow(ll a, ll b) { if (b < 0) { return 0; } ll res = 1; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int N = 200005; vi adj[N]; ll present[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; ll a[n]; rep(i, n) { cin >> a[i]; } sort(a, a + n); map<ll, ll> cnt; rep(i, n) { cnt[a[i]]++; } for (ll i = 0; i < n - 1; i++) { for (ll j = a[i] * 2; j <= a[n - 1]; j = j + a[i]) { present[j] = 1; } } ll ans = 0; for (ll i = 0; i < n; i++) { if (cnt[a[i]] == 1) { if (present[a[i]] == 0) { ans++; } } } cout << ans; }
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define debug(x) cout << #x << ':' << x << endl; #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, n) for (int i = 1; i <= n; i++) #define make_unique(v) \ sort(all(v)); \ v.erase(unique(all(v)), v.end()); #define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end()); #define pb push_back #define mp make_pair #define endl '\n' #define fi first #define se second #define ajs(a) (a < 0) ? (-1 * a) : a using namespace std; typedef long long ll; typedef vector<long long> vll; typedef pair<ll, ll> pll; typedef vector<int> vi; const ll MOD = 1e9 + 7; const int dx4[4] = {0, 1, 0, -1}; const int dy4[4] = {-1, 0, 1, 0}; const int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; const int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; bool cmp(int X, int Y) { return X > Y; } ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } ll modpow(ll a, ll b, ll m = MOD) { a %= m; ll res = 1; while (b) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } ll bpow(ll a, ll b) { if (b < 0) { return 0; } ll res = 1; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int N = 1e6 + 5; vi adj[N]; ll present[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; ll a[n]; rep(i, n) { cin >> a[i]; } sort(a, a + n); map<ll, ll> cnt; rep(i, n) { cnt[a[i]]++; } for (ll i = 0; i < n - 1; i++) { for (ll j = a[i] * 2; j <= a[n - 1]; j = j + a[i]) { present[j] = 1; } } ll ans = 0; for (ll i = 0; i < n; i++) { if (cnt[a[i]] == 1) { if (present[a[i]] == 0) { ans++; } } } cout << ans; }
replace
57
58
57
58
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long int #define MOD 1000000007 #define N 1000003 #define pii pair<int, int> #define MAX 1000000000000000000 #define _mp make_pair #define pb push_back #define _sort(v) sort(v.begin(), v.end()) #define _rsort(v) sort(v.rbegin(), v.rend()) using namespace std; int32_t main() { int n; cin >> n; vector<int> v(N, 0), a(n); for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = a[i]; j < N; j += a[i]) { ++v[j]; } } int ans = 0; for (int i = 0; i < n; i++) { if (v[a[i]] == 1) ans++; } cout << ans; return 0; }
#include <bits/stdc++.h> #define int long long int #define MOD 1000000007 #define N 1000003 #define pii pair<int, int> #define MAX 1000000000000000000 #define _mp make_pair #define pb push_back #define _sort(v) sort(v.begin(), v.end()) #define _rsort(v) sort(v.rbegin(), v.rend()) using namespace std; int32_t main() { int n; cin >> n; vector<int> v(N, 0), a(n); for (int i = 0; i < n; i++) { cin >> a[i]; if (v[a[i]]) { ++v[a[i]]; continue; } for (int j = a[i]; j < N; j += a[i]) { ++v[j]; } } int ans = 0; for (int i = 0; i < n; i++) { if (v[a[i]] == 1) ans++; } cout << ans; return 0; }
insert
20
20
20
24
TLE
p02642
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = INT32_MAX; const ll INFL = INT64_MAX; const int mod = 1e9 + 7; template <typename T> void print_vec(vector<T> v) { for (T i : v) cout << i << " "; cout << endl; } const int MAX_N = 200002; int f[MAX_N]; set<int> st; map<int, bool> mp; int mx; int dd; void sieve(vector<int> a) { f[0] = -1; for (int i : a) { if (mp[i]) { dd++; st.erase(i); continue; } if (f[i]) continue; st.insert(i); mp[i] = true; f[i] = i; for (ll j = i; j <= mx; j += i) if (!f[j]) f[j] = i; } } int main() { int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); mx = a[n - 1]; sieve(a); int ans = st.size(); // if (dd > 0) ans -= dd + 1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = INT32_MAX; const ll INFL = INT64_MAX; const int mod = 1e9 + 7; template <typename T> void print_vec(vector<T> v) { for (T i : v) cout << i << " "; cout << endl; } const int MAX_N = 2000002; int f[MAX_N]; set<int> st; map<int, bool> mp; int mx; int dd; void sieve(vector<int> a) { f[0] = -1; for (int i : a) { if (mp[i]) { dd++; st.erase(i); continue; } if (f[i]) continue; st.insert(i); mp[i] = true; f[i] = i; for (ll j = i; j <= mx; j += i) if (!f[j]) f[j] = i; } } int main() { int n; cin >> n; vector<int> a(n); rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); mx = a[n - 1]; sieve(a); int ans = st.size(); // if (dd > 0) ans -= dd + 1; cout << ans << endl; return 0; }
replace
14
15
14
15
0
p02642
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef pair<ll, P> PP; static const double EPS = 1e-8; static const double PI = 4.0 * atan(1.0); static const ll INF = 1023456789; #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, s, n) for (int i = s; i < n; ++i) #define FORR(i, s, n) for (int i = n - 1; i >= s; --i) #define ALL(c) (c).begin(), (c).end() #define CLEAR(v) memset(v, 0, sizeof(v)) #define MP(a, b) make_pair((a), (b)) #define ABS(a) ((a) > 0 ? (a) : -(a)) #define F first #define S second ll n, a[200000]; bool t[1000001]; int main(int argc, char **argv) { CLEAR(t); cin >> n; set<ll> s, d; REP(i, n) { cin >> a[i]; if (s.count(a[i]) != 0) d.insert(a[i]); s.insert(a[i]); } if (s.count(1) != 0) { cout << (d.count(1) != 0 ? 0 : 1) << endl; return 0; } sort(a, a + n); FOR(i, 2, 1000001) if (!t[i] && s.count(i) != 0 && (i == 0 || a[i] != a[i - 1])) { for (ll b = 2 * i; b < 1000001; b += i) t[b] = true; } ll res = 0; REP(i, n) if (!t[a[i]] && d.count(a[i]) == 0)++ res; cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef pair<ll, P> PP; static const double EPS = 1e-8; static const double PI = 4.0 * atan(1.0); static const ll INF = 1023456789; #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, s, n) for (int i = s; i < n; ++i) #define FORR(i, s, n) for (int i = n - 1; i >= s; --i) #define ALL(c) (c).begin(), (c).end() #define CLEAR(v) memset(v, 0, sizeof(v)) #define MP(a, b) make_pair((a), (b)) #define ABS(a) ((a) > 0 ? (a) : -(a)) #define F first #define S second ll n, a[200000]; bool t[1000001]; int main(int argc, char **argv) { CLEAR(t); cin >> n; set<ll> s, d; REP(i, n) { cin >> a[i]; if (s.count(a[i]) != 0) d.insert(a[i]); s.insert(a[i]); } if (s.count(1) != 0) { cout << (d.count(1) != 0 ? 0 : 1) << endl; return 0; } FOR(i, 2, 1000001) if (!t[i] && s.count(i) != 0) { for (ll b = 2 * i; b < 1000001; b += i) t[b] = true; } ll res = 0; REP(i, n) if (!t[a[i]] && d.count(a[i]) == 0)++ res; cout << res << endl; return 0; }
replace
41
44
41
42
0
p02642
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int M = 200010; signed main() { int N; cin >> N; vector<int> A(N); rep(i, N) cin >> A[i]; vector<int> cnt(M); for (int x : A) { if (cnt[x] != 0) { cnt[x] = 2; continue; } for (int i = x; i < M; i += x) cnt[i]++; } int ans = 0; for (int x : A) { if (cnt[x] == 1) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int M = 1000010; signed main() { int N; cin >> N; vector<int> A(N); rep(i, N) cin >> A[i]; vector<int> cnt(M); for (int x : A) { if (cnt[x] != 0) { cnt[x] = 2; continue; } for (int i = x; i < M; i += x) cnt[i]++; } int ans = 0; for (int x : A) { if (cnt[x] == 1) ans++; } cout << ans << endl; return 0; }
replace
5
6
5
6
0
p02642
C++
Time Limit Exceeded
/*#include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> #include <boost/rational.hpp> */ #include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using ll = long long; using ld = long double; using pll = pair<ll, ll>; #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define rep3(i, n) for (ll i = 1; i < (ll)(n + 1); i++) #define rep4(i, s, n) for (ll i = (s); i < (ll)(n + 1); i++) #define repr(i, n) for (ll i = (n - 1); i >= 0; i--) #define repr3(i, n) for (ll i = (n); i > 0; i--) #define repr4(i, s, n) for (ll i = (n); i >= (s); i--) #define stlen(s) ll s.size() - 1 #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define cout(n) cout << std::fixed << std::setprecision(n) using Graph = vector<vector<int>>; using Graphw = vector<vector<pair<ll, ll>>>; using mat = vector<vector<ll>>; using vec = vector<ll>; #define INF1 INT_MAX; #define INF2 LLONG_MAX; #define PI 3.14159265358979323846; #define MOD 1000000007; /* namespace mp = boost::multiprecision; // 任意長整数型 using Bint = mp::cpp_int; // 仮数部長が32の浮動小数点数型 using Real32 = mp::number<mp::cpp_dec_float<32>>; // 仮数部長が1024の浮動小数点数型 using Real1024 = mp::number<mp::cpp_dec_float<1024>>; // 有理数型 using Rat = boost::rational<Bint>; */ const int mod = 1000000007; int main() { int N; cin >> N; int A[N], table[1000001]; rep(i, N) cin >> A[i]; memset(table, 0, sizeof(table)); rep(i, N) { for (int j = 1; A[i] * j <= 1000000; j++) { table[A[i] * j]++; } } int count = 0; rep(i, N) if (table[A[i]] == 1) count++; cout << count << endl; return 0; }
/*#include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> #include <boost/rational.hpp> */ #include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using ll = long long; using ld = long double; using pll = pair<ll, ll>; #define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++) #define rep3(i, n) for (ll i = 1; i < (ll)(n + 1); i++) #define rep4(i, s, n) for (ll i = (s); i < (ll)(n + 1); i++) #define repr(i, n) for (ll i = (n - 1); i >= 0; i--) #define repr3(i, n) for (ll i = (n); i > 0; i--) #define repr4(i, s, n) for (ll i = (n); i >= (s); i--) #define stlen(s) ll s.size() - 1 #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define cout(n) cout << std::fixed << std::setprecision(n) using Graph = vector<vector<int>>; using Graphw = vector<vector<pair<ll, ll>>>; using mat = vector<vector<ll>>; using vec = vector<ll>; #define INF1 INT_MAX; #define INF2 LLONG_MAX; #define PI 3.14159265358979323846; #define MOD 1000000007; /* namespace mp = boost::multiprecision; // 任意長整数型 using Bint = mp::cpp_int; // 仮数部長が32の浮動小数点数型 using Real32 = mp::number<mp::cpp_dec_float<32>>; // 仮数部長が1024の浮動小数点数型 using Real1024 = mp::number<mp::cpp_dec_float<1024>>; // 有理数型 using Rat = boost::rational<Bint>; */ const int mod = 1000000007; int main() { int N; cin >> N; int A[N], table[1000001]; rep(i, N) cin >> A[i]; memset(table, 0, sizeof(table)); rep(i, N) { if (table[A[i]] != 0) table[A[i]]++; else { for (int j = 1; A[i] * j <= 1000000; j++) { table[A[i] * j]++; } } } int count = 0; rep(i, N) if (table[A[i]] == 1) count++; cout << count << endl; return 0; }
replace
50
52
50
56
TLE
p02642
C++
Runtime Error
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int x = (fr); x < (to); x++) #define rep(x, to) for (int x = 0; x < (to); x++) #define repr(x, fr, to) for (int x = (fr); x >= (to); x--) #define all(c) c.begin(), c.end() #define sz(v) (int)v.size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<ll> VL; const int MD = 1e9 + 7; void dbg() { cerr << "\n"; } template <class F, class... S> void dbg(const F &f, const S &...s) { cerr << f << ": "; dbg(s...); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; VI a(n); rep(i, n) cin >> a[i]; sort(all(a)); const int MX = a.back(); VI ck(MX + 100); rep2(i, 2, MX + 1) { rep(j, n) { if (a[j] * i > MX) break; ck[a[j] * i]++; } } rep(i, n - 1) if (a[i] == a[i + 1]) ck[a[i]]++; int ans = 0; rep(i, n) if (ck[a[i]] == 0) ans++; cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep2(x, fr, to) for (int x = (fr); x < (to); x++) #define rep(x, to) for (int x = 0; x < (to); x++) #define repr(x, fr, to) for (int x = (fr); x >= (to); x--) #define all(c) c.begin(), c.end() #define sz(v) (int)v.size() using namespace std; typedef long long ll; typedef vector<int> VI; typedef pair<int, int> pii; typedef vector<ll> VL; const int MD = 1e9 + 7; void dbg() { cerr << "\n"; } template <class F, class... S> void dbg(const F &f, const S &...s) { cerr << f << ": "; dbg(s...); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; VI a(n); rep(i, n) cin >> a[i]; sort(all(a)); const int MX = a.back(); VI ck(MX + 100); rep2(i, 2, MX + 1) { rep(j, n) { if ((ll)a[j] * i > MX) break; ck[a[j] * i]++; } } rep(i, n - 1) if (a[i] == a[i + 1]) ck[a[i]]++; int ans = 0; rep(i, n) if (ck[a[i]] == 0) ans++; cout << ans << '\n'; return 0; }
replace
32
33
32
33
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < n; i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define fi first #define se second #define pb push_back #define debug(x) cerr << #x << ": " << x << endl #define debug_vec(v) \ cerr << #v << ":"; \ rep(i, v.size()) cerr << " " << v[i]; \ cerr << endl 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; } const int INF = (1 << 30) - 1; const ll LINF = (1LL << 60) - 1; bool table[1010101]; int cnt[1010101]; void table_init(vector<int> &P) { sort(all(P)); for (auto p : P) { if (table[p]) continue; for (int i = 2 * p; i < 1010101; i += p) { table[i] = 1; } } } void solve() { int n; cin >> n; vector<int> A(n); rep(i, n) { cin >> A[i]; cnt[A[i]]++; } table_init(A); int ans = 0; for (auto a : A) { if (cnt[a] == 1 && !table[a]) { ans++; } } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define rep(i, n) for (ll i = 0; i < n; i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define fi first #define se second #define pb push_back #define debug(x) cerr << #x << ": " << x << endl #define debug_vec(v) \ cerr << #v << ":"; \ rep(i, v.size()) cerr << " " << v[i]; \ cerr << endl 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; } const int INF = (1 << 30) - 1; const ll LINF = (1LL << 60) - 1; bool table[1010101]; int cnt[1010101]; void table_init(vector<int> &P) { sort(all(P)); P.erase(unique(all(P)), P.end()); for (auto p : P) { if (table[p]) continue; for (int i = 2 * p; i < 1010101; i += p) { table[i] = 1; } } } void solve() { int n; cin >> n; vector<int> A(n); rep(i, n) { cin >> A[i]; cnt[A[i]]++; } table_init(A); int ans = 0; for (auto a : A) { if (cnt[a] == 1 && !table[a]) { ans++; } } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
insert
38
38
38
39
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; const int max_n = 2 * 1e5; cin >> n; int a[max_n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; vector<int> dp(2 * 1e6, 0); for (int i = 0; i < n; i++) { int now = a[i]; if (dp[now] == 1) { dp[now]++; // continue; now = 1e7; } while (now <= 1e6) { // if(dp[now] == 1) break; dp[now]++; now += a[i]; } } int ans = 0; for (int i = 0; i < n; i++) { if (dp[a[i]] == 1) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; const int max_n = 2 * 1e5; cin >> n; int a[max_n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; vector<int> dp(2 * 1e6, 0); for (int i = 0; i < n; i++) { int now = a[i]; if (dp[now] != 0) { dp[now]++; // continue; now = 1e7; } while (now <= 1e6) { // if(dp[now] == 1) break; dp[now]++; now += a[i]; } } int ans = 0; for (int i = 0; i < n; i++) { if (dp[a[i]] == 1) ans++; } cout << ans << endl; return 0; }
replace
14
15
14
15
TLE
p02642
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; int const MAX = 1000001; int a[MAX + 5]; int main() { int n; cin >> n; rep(i, n) { int x; cin >> x; a[x]++; } int ans = 0; for (int i = 1; i < MAX; i++) { if (a[i] == 1) ans++; if (a[i] > 0) { for (int j = 2; j * j <= MAX; j++) { a[i * j] = 0; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; int const MAX = 1000001; int a[MAX + 5]; int main() { int n; cin >> n; rep(i, n) { int x; cin >> x; a[x]++; } int ans = 0; for (int i = 1; i < MAX; i++) { if (a[i] == 1) ans++; if (a[i] > 0) { for (int j = 2; i * j <= MAX; j++) { a[i * j] = 0; } } } cout << ans << endl; return 0; }
replace
22
23
22
23
0
p02642
C++
Runtime Error
#include "bits/stdc++.h" #define rep(i, N) for (int i = 0; i < N; i++) typedef long long ll; const int mod = 1e9 + 7; using namespace std; const int MAXA = 100000; int main(void) { ll N; cin >> N; vector<ll> A(N); rep(i, N) cin >> A[i]; vector<int> sieve(MAXA + 1, 0); for (auto x : A) { if (sieve[x] > 0) { sieve[x]++; continue; } for (ll i = x; i <= MAXA; i += x) { sieve[i]++; } } int ans = 0; for (auto x : A) { if (sieve[x] == 1) ans++; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #define rep(i, N) for (int i = 0; i < N; i++) typedef long long ll; const int mod = 1e9 + 7; using namespace std; const int MAXA = 1000005; int main(void) { ll N; cin >> N; vector<ll> A(N); rep(i, N) cin >> A[i]; vector<int> sieve(MAXA + 1, 0); for (auto x : A) { if (sieve[x] > 0) { sieve[x]++; continue; } for (ll i = x; i <= MAXA; i += x) { sieve[i]++; } } int ans = 0; for (auto x : A) { if (sieve[x] == 1) ans++; } cout << ans << endl; return 0; }
replace
10
11
10
11
0
p02642
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define loop(i, x, z) for (int i = x; i < z; i++) int n, A[200002], cnt[200002], ans = 0; bool vis[1000002]; int main() { cin >> n; loop(i, 0, n) cin >> A[i], cnt[A[i]]++; sort(A, A + n); if (A[0] == 1) { if (cnt[1] == 1) cout << 1; else cout << 0; return 0; } loop(i, 0, n) { if (!vis[A[i]]) { if (cnt[A[i]] == 1) ans++; for (int j = A[i]; j <= 1000000; j += A[i]) { vis[j] = 1; } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define loop(i, x, z) for (int i = x; i < z; i++) int n, A[200002], cnt[1000002], ans = 0; bool vis[1000002]; int main() { cin >> n; loop(i, 0, n) cin >> A[i], cnt[A[i]]++; sort(A, A + n); if (A[0] == 1) { if (cnt[1] == 1) cout << 1; else cout << 0; return 0; } loop(i, 0, n) { if (!vis[A[i]]) { if (cnt[A[i]] == 1) ans++; for (int j = A[i]; j <= 1000000; j += A[i]) { vis[j] = 1; } } } cout << ans; }
replace
3
4
3
4
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define repp(i, l, r) for (long long i = (l); i < (r); i++) #define rep(i, n) for (long long i = 0; i < (n); ++i) #define per(i, n) for (long long i = (n); i >= 0; --i) const int INF = 2147483647; // int max const long long int MOD = 1000000007; using namespace std; using ll = long long; using P = pair<int, int>; 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; } // ミョ(-ω- ?) int main() { ll n; cin >> n; vector<ll> a(n, 0); vector<ll> cnt(1001010, 0); rep(i, n) { cin >> a[i]; cnt[a[i]]++; } vector<bool> ok(1000005, true); for (ll i = 0; i < n; i++) { // if(cnt[x] < 1)continue; ll x = a[i]; if (cnt[x] > 1) ok[x] = false; for (ll i = 2 * x; i < 1000001; i += x) ok[i] = false; } ll res = 0; rep(i, n) if (ok[a[i]]) res++; cout << res << "\n"; return 0; }
#include <bits/stdc++.h> #define repp(i, l, r) for (long long i = (l); i < (r); i++) #define rep(i, n) for (long long i = 0; i < (n); ++i) #define per(i, n) for (long long i = (n); i >= 0; --i) const int INF = 2147483647; // int max const long long int MOD = 1000000007; using namespace std; using ll = long long; using P = pair<int, int>; 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; } // ミョ(-ω- ?) int main() { ll n; cin >> n; vector<ll> a(n, 0); vector<ll> cnt(1001010, 0); rep(i, n) { cin >> a[i]; cnt[a[i]]++; } vector<bool> ok(1000005, true); ll x = 0; for (ll i = 0; i < 1000001; i++) { x = i; if (cnt[x] < 1) continue; // ll x = a[i]; if (cnt[x] > 1) ok[x] = false; for (ll i = 2 * x; i < 1000001; i += x) ok[i] = false; } ll res = 0; rep(i, n) if (ok[a[i]]) res++; cout << res << "\n"; return 0; }
replace
35
38
35
41
TLE
p02642
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int n, a[110000]; int f[1100000]; int main() { cin >> n; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + n + 1); int cnt = 0; for (int i = 1; i <= n; ++i) { if (f[a[i]] == 0 && (i == n || a[i] != a[i + 1])) ++cnt; for (int j = 1; a[i] * j <= a[n]; ++j) f[a[i] * j] = 1; } cout << cnt << endl; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; int n, a[210000]; int f[1100000]; int main() { cin >> n; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + n + 1); int cnt = 0; for (int i = 1; i <= n; ++i) { if (f[a[i]] == 0 && (i == n || a[i] != a[i + 1])) ++cnt; for (int j = 1; a[i] * j <= a[n]; ++j) f[a[i] * j] = 1; } cout << cnt << endl; return 0; }
replace
4
5
4
5
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long const int N = 1000001; int n, cnt[N]; vector<int> a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a.resize(n); for (auto &i : a) cin >> i; map<int, int> f; for (auto &i : a) { f[i]++; if (f[i] == 3) continue; for (int j = i; j < N; j += i) { cnt[j]++; } } int ans = 0; for (auto &i : a) { if (cnt[i] == 1) ans++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long const int N = 1000001; int n, cnt[N]; vector<int> a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a.resize(n); for (auto &i : a) cin >> i; map<int, int> f; for (auto &i : a) { f[i]++; if (f[i] >= 3) continue; for (int j = i; j < N; j += i) { cnt[j]++; } } int ans = 0; for (auto &i : a) { if (cnt[i] == 1) ans++; } cout << ans << endl; }
replace
20
21
20
21
TLE
p02642
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <cstdlib> #include <ctype.h> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define vint vector<int> int main() { int n; cin >> n; vint a(n); vector<pair<int, int>> aa; bool b[(int)1e6]; fill(b, b + (int)1e6, true); REP(i, n) cin >> a[i]; sort(a.begin(), a.end()); int ans = 0; REP(i, n) { int j = a[i] * 2; while (j <= (int)1e6) { b[j - 1] = false; j += a[i]; } if (i != 0 && a[i] == a[i - 1]) b[a[i] - 1] = false; } REP(i, n) { if (b[a[i] - 1]) ans++; } cout << ans << endl; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdlib> #include <ctype.h> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define vint vector<int> int main() { int n; cin >> n; vint a(n); vector<pair<int, int>> aa; bool b[(int)1e6]; fill(b, b + (int)1e6, true); REP(i, n) cin >> a[i]; sort(a.begin(), a.end()); int ans = 0; REP(i, n) { if (b[a[i] - 1] == false) continue; int j = a[i] * 2; while (j <= (int)1e6) { b[j - 1] = false; j += a[i]; } if (i != 0 && a[i] == a[i - 1]) b[a[i] - 1] = false; } REP(i, n) { if (b[a[i] - 1]) ans++; } cout << ans << endl; return 0; }
insert
29
29
29
31
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; constexpr int MAX = 1e6; int main() { int n; cin >> n; vector<int> a(n); for (auto &e : a) cin >> e; sort(a.begin(), a.end()); int r = 0; vector<bool> v(MAX + 1, true); for (int i = 0; i < n; i++) { if ((i == n || a[i] != a[i + 1]) && v[a[i]]) r++; for (int j = a[i]; j <= (int)MAX; j += a[i]) v[j] = false; } cout << r << endl; }
#include <bits/stdc++.h> using namespace std; constexpr int MAX = 1e6; int main() { int n; cin >> n; vector<int> a(n); for (auto &e : a) cin >> e; sort(a.begin(), a.end()); int r = 0; vector<bool> v(MAX + 1, true); for (int i = 0; i < n; i++) { if (!v[a[i]]) continue; if (i == n || a[i] != a[i + 1]) r++; for (int j = a[i]; j <= (int)MAX; j += a[i]) v[j] = false; } cout << r << endl; }
replace
15
16
15
18
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define dbg puts("It's Just Begining.Wait For The End!!!!!\n") #define CHECK(x) cout << (#x) << " is " << (x) << "\n"; #define endl printf("\n") typedef pair<ll, ll> pii; #define pb push_back #define ms(a, b) memset(a, b, sizeof(a)) #define in(x) scanf("%lld", &x) #define out(x) printf("%lld", x) #define X first #define Y second #define sps printf(" ") #define dou(x) cout << fixed << setprecision(6) << x #define all(v) v.begin(), v.end() #define dis(it, it1) distance(it, it1) + 1 #define MAX 1000005 #define INF 1000000000 #define MOD 1000000007 #define print(cs) printf("Case %lld:\n", cs) #define Yesno(ok) \ if (ok) \ puts("YES"); \ else \ puts("NO") #define yesno(ok) \ if (ok) \ puts("Yes"); \ else \ puts("No") ll lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); } int main() { ll i, j, k, l, n, m, r; in(n); ll arr[n]; map<ll, ll> visit; for (i = 0; i < n; i++) { in(arr[i]); visit[arr[i]]++; } ll cnt = 0; if (visit[1LL]) { if (visit[1] == 1) puts("1"); else puts("0"); exit(0); } for (i = 0; i < n; i++) { if (visit[arr[i]] > 1) continue; bool ok = false; for (j = 2; j * j <= arr[i]; j++) { if (arr[i] % j == 0 && (visit[j] || visit[arr[i] / j])) { ok = true; break; } } if (!ok) cnt++; } out(cnt); endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define dbg puts("It's Just Begining.Wait For The End!!!!!\n") #define CHECK(x) cout << (#x) << " is " << (x) << "\n"; #define endl printf("\n") typedef pair<ll, ll> pii; #define pb push_back #define ms(a, b) memset(a, b, sizeof(a)) #define in(x) scanf("%lld", &x) #define out(x) printf("%lld", x) #define X first #define Y second #define sps printf(" ") #define dou(x) cout << fixed << setprecision(6) << x #define all(v) v.begin(), v.end() #define dis(it, it1) distance(it, it1) + 1 #define MAX 1000005 #define INF 1000000000 #define MOD 1000000007 #define print(cs) printf("Case %lld:\n", cs) #define Yesno(ok) \ if (ok) \ puts("YES"); \ else \ puts("NO") #define yesno(ok) \ if (ok) \ puts("Yes"); \ else \ puts("No") ll lcm(ll a, ll b) { return ((a * b) / __gcd(a, b)); } int main() { ll i, j, k, l, n, m, r; in(n); ll arr[n]; unordered_map<ll, ll> visit; for (i = 0; i < n; i++) { in(arr[i]); visit[arr[i]]++; } ll cnt = 0; if (visit[1LL]) { if (visit[1] == 1) puts("1"); else puts("0"); exit(0); } for (i = 0; i < n; i++) { if (visit[arr[i]] > 1) continue; bool ok = false; for (j = 2; j * j <= arr[i]; j++) { if (arr[i] % j == 0 && (visit[j] || visit[arr[i] / j])) { ok = true; break; } } if (!ok) cnt++; } out(cnt); endl; }
replace
36
37
36
37
TLE
p02642
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define COS 200005 int main() { int n; cin >> n; vector<ll> a(n); vector<bool> b(COS, false); ll i, j; ll ans = 0; rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); rep(i, n - 1) { if (b[a[i]] == true) continue; if (a[i] == a[i + 1]) { b[a[i]] = true; // continue; } for (j = 2; j < a[n - 1] / a[i] + 1; j++) { b[j * a[i]] = true; } } rep(i, n) { if (b[a[i]] == false) ans++; } cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define COS 2000005 int main() { int n; cin >> n; vector<ll> a(n); vector<bool> b(COS, false); ll i, j; ll ans = 0; rep(i, n) { cin >> a[i]; } sort(a.begin(), a.end()); rep(i, n - 1) { if (b[a[i]] == true) continue; if (a[i] == a[i + 1]) { b[a[i]] = true; // continue; } for (j = 2; j < a[n - 1] / a[i] + 1; j++) { b[j * a[i]] = true; } } rep(i, n) { if (b[a[i]] == false) ans++; } cout << ans << endl; return 0; }
replace
5
6
5
6
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define PI 3.14159265358979323 #define ll long long int #define vi vector<int> #define vl vector<ll> #define all(v) (v).begin(), (v).end() #define pb push_back ll power(ll a, ll b) { // a^b ll res = 1; while (b > 0) { if (b & 1) { res = (res * a); b--; } a = (a * a); b >>= 1; } return res; } ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); } map<ll, ll> mp; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vl v(n); for (ll i = 0; i < n; i++) { cin >> v[i]; mp[v[i]]++; } ll c = 0; for (ll i = 0; i < n; i++) { ll d = v[i]; mp[d]--; int f = 0; for (ll j = 1; j * j <= d; j++) { if (d % j == 0) { if (mp[j] > 0 || mp[d / j] > 0) { f = 1; break; } } } mp[d]++; if (f == 0) c++; } cout << c << "\n"; }
#include <bits/stdc++.h> using namespace std; #define PI 3.14159265358979323 #define ll long long int #define vi vector<int> #define vl vector<ll> #define all(v) (v).begin(), (v).end() #define pb push_back ll power(ll a, ll b) { // a^b ll res = 1; while (b > 0) { if (b & 1) { res = (res * a); b--; } a = (a * a); b >>= 1; } return res; } ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); } vl mp(1000006, 0); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; vl v(n); for (ll i = 0; i < n; i++) { cin >> v[i]; mp[v[i]]++; } ll c = 0; for (ll i = 0; i < n; i++) { ll d = v[i]; mp[d]--; int f = 0; for (ll j = 1; j * j <= d; j++) { if (d % j == 0) { if (mp[j] > 0 || mp[d / j] > 0) { f = 1; break; } } } mp[d]++; if (f == 0) c++; } cout << c << "\n"; }
replace
23
24
23
24
TLE
p02642
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; int main() { int n; cin >> n; int a[200001]; for (int i = 1; i <= 2000000; i++) { a[i] = 0; } rep(i, n) { int x; cin >> x; a[x]++; } int ans = 0; for (int i = 1; i <= 2000000; i++) { if (a[i]) { if (a[i] == 1) { ans++; } for (int j = i; j <= 2000000; j += i) { a[j] = 0; } } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; int main() { int n; cin >> n; int a[2000001]; for (int i = 1; i <= 2000000; i++) { a[i] = 0; } rep(i, n) { int x; cin >> x; a[x]++; } int ans = 0; for (int i = 1; i <= 2000000; i++) { if (a[i]) { if (a[i] == 1) { ans++; } for (int j = i; j <= 2000000; j += i) { a[j] = 0; } } } cout << ans << endl; }
replace
9
10
9
10
-11
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define INF LONG_MAX #define MOD 1000000007 #define rng(a) a.begin(), a.end() #define rrng(a) a.end(), a.begin() #define endl "\n" #define int ll signed main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(rng(A)); vector<bool> b(1000001, true); bool ng = true; ll ans = 0; for (int i = 0; i < N; i++) { if (A[i] == A[i + 1]) for (int j = A[i]; j <= 1000000; j += A[i]) b[j] = false; if (b[A[i]]) { ans++; for (int j = A[i]; j <= 1000000; j += A[i]) b[j] = false; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define INF LONG_MAX #define MOD 1000000007 #define rng(a) a.begin(), a.end() #define rrng(a) a.end(), a.begin() #define endl "\n" #define int ll signed main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(rng(A)); vector<bool> b(1000001, true); bool ng = true; ll ans = 0; for (int i = 0; i < N; i++) { if (!b[A[i]]) continue; if (A[i] == A[i + 1]) for (int j = A[i]; j <= 1000000; j += A[i]) b[j] = false; if (b[A[i]]) { ans++; for (int j = A[i]; j <= 1000000; j += A[i]) b[j] = false; } } cout << ans << endl; return 0; }
insert
28
28
28
30
TLE
p02642
C++
Runtime Error
#include <algorithm> #include <complex> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <regex> #include <set> #include <sstream> #include <string> #include <thread> #include <tuple> #include <type_traits> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> t(200'002); sort(a.begin(), a.end()); for (int i : a) { t[i]++; if (t[i] > 1) continue; for (int j = i * 2; j < t.size(); j += i) { t[j]++; } } int ans = 0; for (int i : a) { if (t[i] == 1) ans++; } cout << ans << endl; return 0; }
#include <algorithm> #include <complex> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <regex> #include <set> #include <sstream> #include <string> #include <thread> #include <tuple> #include <type_traits> #include <vector> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> t(1'000'001); sort(a.begin(), a.end()); for (int i : a) { t[i]++; if (t[i] > 1) continue; for (int j = i * 2; j < t.size(); j += i) { t[j]++; } } int ans = 0; for (int i : a) { if (t[i] == 1) ans++; } cout << ans << endl; return 0; }
replace
29
30
29
30
0
p02642
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define all(x) begin(x), end(x) #define gets(s) fgets(s, size(s), stdin) using namespace std; typedef long long ll; typedef string str; template <typename T> using vec = vector<T>; template <typename T> using uset = unordered_set<T>; template <typename T, typename S> using umap = unordered_map<T, S>; const size_t mbs = sizeof(bitset<1>) * 8; template <size_t N> using bit = bitset<mbs *((N + mbs - 1) / mbs)>; const int mod = 1000000007; const double pi = 3.1415926535; const int maxn = 200001; vec<int> v(maxn); bit<maxn> b; int main() { int n; scanf("%d", &n); vec<int> a(n); rep(i, n) { scanf("%d", &a[i]); v[a[i]]++; } auto m = *max_element(all(a)); rep(i, n) { if (v[a[i]] > 1 && !b[a[i]]) b[a[i]] = true; for (int j = a[i] * 2; j <= m; j += a[i]) if (!b[j]) b[j] = true; } int ans = 0; rep(i, n) if (!b[a[i]]) ans++; printf("%d", ans); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) #define all(x) begin(x), end(x) #define gets(s) fgets(s, size(s), stdin) using namespace std; typedef long long ll; typedef string str; template <typename T> using vec = vector<T>; template <typename T> using uset = unordered_set<T>; template <typename T, typename S> using umap = unordered_map<T, S>; const size_t mbs = sizeof(bitset<1>) * 8; template <size_t N> using bit = bitset<mbs *((N + mbs - 1) / mbs)>; const int mod = 1000000007; const double pi = 3.1415926535; const int maxn = 1000001; vec<int> v(maxn); bit<maxn> b; int main() { int n; scanf("%d", &n); vec<int> a(n); rep(i, n) { scanf("%d", &a[i]); v[a[i]]++; } auto m = *max_element(all(a)); rep(i, n) { if (v[a[i]] > 1 && !b[a[i]]) b[a[i]] = true; for (int j = a[i] * 2; j <= m; j += a[i]) if (!b[j]) b[j] = true; } int ans = 0; rep(i, n) if (!b[a[i]]) ans++; printf("%d", ans); return 0; }
replace
15
16
15
16
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int N; constexpr int maxA = 1000001; vector<int> input; vector<bool> table(maxA, true); int main() { cin >> N; input.resize(N); for (int i = 0; i < N; i++) cin >> input[i]; sort(input.begin(), input.end()); for (int i = 0; i < N; i++) { for (int j = 2; j * input[i] < maxA; j++) { table[j * input[i]] = false; } } long long counter = 0; for (int i = 0; i < N; i++) { if (table[input[i]]) { if (i != 0 && input[i] == input[i - 1]) continue; if (i != N - 1 && input[i] == input[i + 1]) continue; counter++; } } cout << counter << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int N; constexpr int maxA = 1000001; vector<int> input; vector<bool> table(maxA, true); int main() { cin >> N; input.resize(N); for (int i = 0; i < N; i++) cin >> input[i]; sort(input.begin(), input.end()); for (int i = 0; i < N; i++) { if (!table[input[i]] || (i != 0 && input[i] == input[i - 1])) continue; for (int j = 2; j * input[i] < maxA; j++) { table[j * input[i]] = false; } } long long counter = 0; for (int i = 0; i < N; i++) { if (table[input[i]]) { if (i != 0 && input[i] == input[i - 1]) continue; if (i != N - 1 && input[i] == input[i + 1]) continue; counter++; } } cout << counter << endl; return 0; }
insert
15
15
15
17
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define lli long long int #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define fr(i, n) for (i = 0; i < n; i++) #define F first #define S second #define fast std::ios_base::sync_with_stdio(false) #define mod 1000000007 #define pi 3.14159265 map<lli, lli> m; bool factors(lli n) { lli i; for (i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i == n) { if (m[i] >= 1) { return 0; } } else { if (m[i] >= 1 || m[n / i] >= 1) return 0; } } } return 1; } int main() { fast; cin.tie(0); cout.tie(0); lli n; cin >> n; lli a[n], i; fr(i, n) { cin >> a[i]; m[a[i]]++; } if (m[1] == 1) { cout << 1; return 0; } else if (m[1] > 1) { cout << 0; return 0; } lli count = 0; fr(i, n) { if (m[a[i]] == 1 && factors(a[i])) { count++; } } cout << count; return 0; }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define fr(i, n) for (i = 0; i < n; i++) #define F first #define S second #define fast std::ios_base::sync_with_stdio(false) #define mod 1000000007 #define pi 3.14159265 lli m[1000005]; bool factors(lli n) { lli i; for (i = 2; i * i <= n; i++) { if (n % i == 0) { if (i * i == n) { if (m[i] >= 1) { return 0; } } else { if (m[i] >= 1 || m[n / i] >= 1) return 0; } } } return 1; } int main() { fast; cin.tie(0); cout.tie(0); lli n; cin >> n; lli a[n], i; fr(i, n) { cin >> a[i]; m[a[i]]++; } if (m[1] == 1) { cout << 1; return 0; } else if (m[1] > 1) { cout << 0; return 0; } lli count = 0; fr(i, n) { if (m[a[i]] == 1 && factors(a[i])) { count++; } } cout << count; return 0; }
replace
11
12
11
12
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define d long double #define scf(n) scanf("%d", &n) #define lscf(n) scanf("%lld", &n) #define dscf(n) scanf("%Lf", &n) #define pri(n) printf("%d ", (int)n) #define lpri(n) printf("%lld ", n) #define dpri(n) printf("%Lf ", n) #define prin(n) printf("%d\n", (int)n) #define lprin(n) printf("%lld\n", n) #define dprin(n) printf("%Lf\n", n) #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define rep(i, ini, n) for (ll i = ini; i < (int)n; i++) #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define bitcount(n) __builtin_popcount(n) #define tc \ int tt; \ scf(tt); \ while (tt--) #define gcd __gcd #define inf INT_MAX #define ninf INT_MIN #define pb push_back #define mp make_pair #define F first #define S second #define PI 3.14159265358979323846264 const ll M = 1e9 + 7; const int N = 1e6 + 7; int ans; bool visited[N]; int main() { int n; cin >> n; int a[n]; map<int, int> m; rep(i, 0, n) { cin >> a[i]; m[a[i]]++; } sort(a, a + n); rep(i, 0, n) { if (!visited[a[i]] && m[a[i]] == 1) { // cout<<a[i]<<endl; ans++; } visited[a[i]] = true; rep(j, 1, 1000001) { if (a[i] * j > 1000001) break; visited[a[i] * j] = true; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define d long double #define scf(n) scanf("%d", &n) #define lscf(n) scanf("%lld", &n) #define dscf(n) scanf("%Lf", &n) #define pri(n) printf("%d ", (int)n) #define lpri(n) printf("%lld ", n) #define dpri(n) printf("%Lf ", n) #define prin(n) printf("%d\n", (int)n) #define lprin(n) printf("%lld\n", n) #define dprin(n) printf("%Lf\n", n) #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define rep(i, ini, n) for (ll i = ini; i < (int)n; i++) #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define bitcount(n) __builtin_popcount(n) #define tc \ int tt; \ scf(tt); \ while (tt--) #define gcd __gcd #define inf INT_MAX #define ninf INT_MIN #define pb push_back #define mp make_pair #define F first #define S second #define PI 3.14159265358979323846264 const ll M = 1e9 + 7; const int N = 1e6 + 7; int ans; bool visited[N]; int main() { int n; cin >> n; int a[n]; map<int, int> m; rep(i, 0, n) { cin >> a[i]; m[a[i]]++; } sort(a, a + n); rep(i, 0, n) { if (!visited[a[i]] && m[a[i]] == 1) { // cout<<a[i]<<endl; ans++; } if (!visited[a[i]]) { rep(j, 1, 1000001) { if (a[i] * j > 1000001) break; visited[a[i] * j] = true; } } } cout << ans; return 0; }
replace
62
67
62
69
TLE
p02642
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) #define revrep(i, n) for (int i = (n)-1; i >= 0; i--) #define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--) #define debug(x) cerr << #x << ": " << x << "\n" #define popcnt(x) __builtin_popcount(x) const double PI = acos(-1.0); using ll = long long; using P = pair<int, int>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { cout << '(' << p.first << ", " << p.second << ')'; return os; } template <class T> void print(const vector<T> &v, const string &delimiter = " ") { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; } template <class T> void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v : vv) print(v, delimiter); } template <class T> vector<T> divisor(T n) { vector<T> res; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (i != n / i) res.push_back(n / i); } } return res; } int main() { int n; cin >> n; int cnt_divided = 0; vector<int> a(n); cin >> a; vector<int> cnt(200001); rep(i, n) { cnt[a[i]]++; } rep(i, n) { for (int d : divisor(a[i])) { if (d == a[i]) { if (2 <= cnt[d]) { cnt_divided++; break; } } else if (1 <= cnt[d]) { cnt_divided++; break; } } } cout << n - cnt_divided << endl; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, s, n) for (int i = (s); i < (int)(n); i++) #define revrep(i, n) for (int i = (n)-1; i >= 0; i--) #define revrepr(i, s, n) for (int i = (n)-1; i >= s; i--) #define debug(x) cerr << #x << ": " << x << "\n" #define popcnt(x) __builtin_popcount(x) const double PI = acos(-1.0); using ll = long long; using P = pair<int, int>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i); return is; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { cout << '(' << p.first << ", " << p.second << ')'; return os; } template <class T> void print(const vector<T> &v, const string &delimiter = " ") { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; } template <class T> void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v : vv) print(v, delimiter); } template <class T> vector<T> divisor(T n) { vector<T> res; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (i != n / i) res.push_back(n / i); } } return res; } int main() { int n; cin >> n; int cnt_divided = 0; vector<int> a(n); cin >> a; vector<int> cnt(1000010); rep(i, n) { cnt[a[i]]++; } rep(i, n) { for (int d : divisor(a[i])) { if (d == a[i]) { if (2 <= cnt[d]) { cnt_divided++; break; } } else if (1 <= cnt[d]) { cnt_divided++; break; } } } cout << n - cnt_divided << endl; }
replace
85
86
85
86
0
p02642
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; const ll mod = 1e9 + 7; ll solve(void) { ll n; cin >> n; vll A(n); sort(A.begin(), A.end()); for (auto &a : A) { cin >> a; } map<ll, ll> m; for (auto a : A) { m[a]++; } vector<bool> B(10e6 + 10, true); for (auto a : A) { if (!B[a]) { continue; } for (ll i = 2; i * a < B.size(); i++) B[i * a] = false; } ll ans = 0; for (auto a : A) ans += (m[a] == 1 && B[a] == true); cout << ans << endl; return 0; } int main(void) { while (solve()) { } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; const ll mod = 1e9 + 7; ll solve(void) { ll n; cin >> n; vll A(n); sort(A.begin(), A.end()); for (auto &a : A) { cin >> a; } map<ll, ll> m; for (auto a : A) { m[a]++; } vector<bool> B(10e6 + 10, true); for (auto v : m) { auto a = v.first; if (!B[a]) { continue; } for (ll i = 2; i * a < B.size(); i++) B[i * a] = false; } ll ans = 0; for (auto a : A) ans += (m[a] == 1 && B[a] == true); cout << ans << endl; return 0; } int main(void) { while (solve()) { } return 0; }
replace
25
26
25
27
TLE
p02642
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; vector<long long> A(N); vector<int> B(100001, 0); for (int i = 0; i < N; i++) cin >> A[i], B[A[i]]++; int ANS = 0; for (int i = 0; i < N; i++) { bool check = true; B[A[i]]--; for (int j = 1; j * j <= A[i]; j++) { if (A[i] % j == 0) { if (B[j] != 0) check = false; if (B[A[i] / j] != 0) check = false; } } B[A[i]]++; if (check) ANS++; } cout << ANS << endl; }
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; vector<long long> A(N); vector<int> B(1000001, 0); for (int i = 0; i < N; i++) cin >> A[i], B[A[i]]++; int ANS = 0; for (int i = 0; i < N; i++) { bool check = true; B[A[i]]--; for (int j = 1; j * j <= A[i]; j++) { if (A[i] % j == 0) { if (B[j] != 0) check = false; if (B[A[i] / j] != 0) check = false; } } B[A[i]]++; if (check) ANS++; } cout << ANS << endl; }
replace
7
8
7
8
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using Graph = vector<vector<int>>; typedef pair<int, int> P; typedef long long ll; int main() { ll n; cin >> n; vector<ll> a(n); bool dp[1000005]; rep(i, 1000005) dp[i] = false; map<ll, ll> mp; rep(i, n) { cin >> a[i]; mp[a[i]]++; } sort(all(a)); ll ans = 0; rep(i, n) { if (dp[a[i]]) { continue; } if (mp[a[i]] == 1) { ans++; } ll num = a[i] * 2; while (num <= 1000001) { dp[num] = true; num += a[i]; } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using Graph = vector<vector<int>>; typedef pair<int, int> P; typedef long long ll; int main() { ll n; cin >> n; vector<ll> a(n); bool dp[1000005]; rep(i, 1000005) dp[i] = false; map<ll, ll> mp; rep(i, n) { cin >> a[i]; mp[a[i]]++; } sort(all(a)); ll ans = 0; rep(i, n) { if (dp[a[i]]) { continue; } if (mp[a[i]] == 1) { ans++; } ll num = a[i]; while (num <= 1000001) { dp[num] = true; num += a[i]; } } cout << ans << endl; }
replace
33
34
33
34
TLE
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define Int int32_t #define all(c) c.begin(), c.end() #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL); #define pii pair<int, int> #define pb push_back #define F first #define S second #define endl "\n" #define bitcnt(n) __builtin_popcountll(n) #define setpre(n) cout << fixed << setprecision(n) #define tr(c) \ for (auto x : c) \ cout << x << " "; #define ol(c, s, e) \ for (int pos = s; pos < e; pos++) \ cout << c[pos] << " "; #define PI acos(-1l) const int M = 1000000007; const int N = 3e5 + 5; const int INF = 2e18 + 5; // #define rohit #ifdef rohit template <typename... T> void bug(T... __VA_ARGS__) { ((cerr << __VA_ARGS__ << " "), ...); } #define debug(...) \ cerr << "[" << #__VA_ARGS__ << "]:", bug(__VA_ARGS__), cerr << endl; #endif void solve() { int n; cin >> n; int a[n], mx = 0; map<int, int> mp; map<int, bool> used; for (int i = 0; i < n; i++) { cin >> a[i]; mx = max(a[i], mx); mp[a[i]]++; used[a[i]] = true; } for (auto x : mp) { if (x.second > 1) used[x.first] = false; for (int i = x.first; i <= mx; i += x.first) { if (i != x.first && mp.count(i)) { used[i] = false; } } } int ans = 0; for (auto x : mp) { if (used[x.first]) ans++; } cout << ans; } signed main() { FAST; // TODO: check test cases. int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } } /* Extra Cares * 1)In graphs or dp problems sometimes long long creates MLE. * 2)Check whether int to long long typecasting is commented or not? * 3)Check overflows. */
#include <bits/stdc++.h> using namespace std; #define int long long #define Int int32_t #define all(c) c.begin(), c.end() #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL); #define pii pair<int, int> #define pb push_back #define F first #define S second #define endl "\n" #define bitcnt(n) __builtin_popcountll(n) #define setpre(n) cout << fixed << setprecision(n) #define tr(c) \ for (auto x : c) \ cout << x << " "; #define ol(c, s, e) \ for (int pos = s; pos < e; pos++) \ cout << c[pos] << " "; #define PI acos(-1l) const int M = 1000000007; const int N = 3e5 + 5; const int INF = 2e18 + 5; // #define rohit #ifdef rohit template <typename... T> void bug(T... __VA_ARGS__) { ((cerr << __VA_ARGS__ << " "), ...); } #define debug(...) \ cerr << "[" << #__VA_ARGS__ << "]:", bug(__VA_ARGS__), cerr << endl; #endif void solve() { int n; cin >> n; int a[n], mx = 0; map<int, int> mp; map<int, bool> used; for (int i = 0; i < n; i++) { cin >> a[i]; mx = max(a[i], mx); mp[a[i]]++; used[a[i]] = true; } if (mp.count(1)) { if (mp[1] == 1) { cout << 1; } else { cout << 0; } return; } for (auto x : mp) { if (x.second > 1) used[x.first] = false; for (int i = x.first; i <= mx; i += x.first) { if (i != x.first && mp.count(i)) { used[i] = false; } } } int ans = 0; for (auto x : mp) { if (used[x.first]) ans++; } cout << ans; } signed main() { FAST; // TODO: check test cases. int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } } /* Extra Cares * 1)In graphs or dp problems sometimes long long creates MLE. * 2)Check whether int to long long typecasting is commented or not? * 3)Check overflows. */
insert
44
44
44
52
TLE
p02642
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> typedef long long ll; typedef unsigned long long ull; typedef long double LD; using namespace std; const int INF = 2147483647; const int INF2 = 0x3f3f3f3f; const ll INF64 = 3e18; const double INFD = 1e30; const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1000000007; template <typename T> inline T read() { T X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } const int MAXN = 5005; int n, m, k; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0}; int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1}; int CASE = 1; int dcmp(double x) { if (fabs(x) < EPS) return 0; return x > 0 ? 1 : -1; } struct Vector2 { double x, y; Vector2() {} Vector2(double x, double y) : x(x), y(y) {} Vector2(double rad) : x(cos(rad)), y(sin(rad)) {} friend istream &operator>>(istream &input, Vector2 &v) { input >> v.x >> v.y; return input; } friend ostream &operator<<(ostream &output, const Vector2 &v) { output << '(' << v.x << ',' << v.y << ')'; return output; } static double dot(const Vector2 &a, const Vector2 &b) { return a.x * b.x + a.y * b.y; } static double angle(const Vector2 &a, const Vector2 &b) { return acos(dot(a, b) / a.length() / b.length()); } static double cross(const Vector2 &a, const Vector2 &b) { return a.x * b.y - a.y * b.x; } static double area2(const Vector2 &a, const Vector2 &b, const Vector2 &c) { return cross(b - a, c - a); } double length() const { return sqrt(x * x + y * y); } double lengthSq() const { return x * x + y * y; } double getRad() const { return atan2(y, x); } Vector2 rotate(double rad) const { return Vector2(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad)); } Vector2 unit() const { double l = this->length(); return Vector2(x / l, y / l); } Vector2 normal() const { double l = this->length(); return Vector2(-y / l, x / l); } bool operator<(const Vector2 &other) const { return x < other.x || (x == other.x && y < other.y); } bool operator==(const Vector2 &other) const { return !dcmp(x - other.x) && !dcmp(y - other.y); } Vector2 &operator+=(const Vector2 &vec) { this->x += vec.x, this->y += vec.y; return *this; } Vector2 operator+(const Vector2 &vec) const { return Vector2(this->x + vec.x, this->y + vec.y); } Vector2 &operator-=(const Vector2 &vec) { this->x -= vec.x, this->y -= vec.y; return *this; } Vector2 operator-(const Vector2 &vec) const { return Vector2(this->x - vec.x, this->y - vec.y); } Vector2 &operator*=(double v) { this->x *= v, this->y *= v; return *this; } Vector2 &operator*=(const Vector2 &v) { this->x *= v.x, this->y *= v.y; return *this; } Vector2 operator*(double v) const { return Vector2(this->x * v, this->y * v); } Vector2 operator*(const Vector2 &vec) const { return Vector2(this->x * vec.x, this->y * vec.y); } Vector2 &operator/=(double v) { this->x /= v, this->y /= v; return *this; } Vector2 &operator/=(const Vector2 &vec) { this->x /= vec.x, this->y /= vec.y; return *this; } Vector2 operator/(double v) const { return Vector2(this->x / v, this->y / v); } Vector2 operator/(const Vector2 &vec) const { return Vector2(this->x / vec.x, this->y / vec.y); } }; struct Line { Vector2 p, dir; Line() {} Line(const Vector2 &p, const Vector2 &dir) : p(p), dir(dir) {} static Vector2 lineIntersection(const Line &l1, const Line &l2) { double t = Vector2::cross(l2.dir, l1.p - l2.p) / Vector2::cross(l1.dir, l2.dir); return l1.p + l1.dir * t; } static double distanceToLine(const Vector2 &p, const Line &line) { return fabs(Vector2::cross(line.dir, p - line.p)) / line.dir.length(); } static Vector2 lineProjection(const Vector2 &p, const Line &l) { return l.p + l.dir * Vector2::dot(l.dir, p - l.p) / Vector2::dot(l.dir, l.dir); } }; struct Segment { Vector2 p1, p2; Segment() {} Segment(const Vector2 &p1, const Vector2 &p2) : p1(p1), p2(p2) {} static double distanceToSegment(const Vector2 &p, const Segment &seg) { if (seg.p1 == seg.p2) return (p - seg.p1).length(); Vector2 ab = seg.p2 - seg.p1, ap = p - seg.p1, bp = p - seg.p2; if (dcmp(Vector2::dot(ab, ap)) < 0) return ap.length(); else if (dcmp(Vector2::dot(ab, bp)) > 0) return bp.length(); else return fabs(Vector2::cross(ab, ap) / ab.length()); } static bool properIntersection(const Segment &s1, const Segment &s2) { double c1 = Vector2::cross(s1.p2 - s1.p1, s2.p1 - s1.p1), c2 = Vector2::cross(s1.p2 - s1.p1, s2.p2 - s1.p1), c3 = Vector2::cross(s2.p2 - s2.p1, s1.p1 - s2.p1), c4 = Vector2::cross(s2.p2 - s2.p1, s1.p2 - s2.p1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } static bool onSegment(const Vector2 &p, const Segment &s) { return !dcmp(Vector2::cross(s.p1 - p, s.p2 - p)) && dcmp(Vector2::dot(s.p1 - p, s.p2 - p)) < 0; } }; int arr[MAXN]; int main() { #ifdef LOCALLL freopen("in", "r", stdin); freopen("out", "w", stdout); #endif scanf("%d", &n); map<int, int> cntt; for (int i = 0; i < n; i++) { cntt[arr[i] = read<int>()]++; } int ans = 0; for (int i = 0; i < n; i++) { int x = arr[i]; cntt[x]--; bool flag = true; for (int j = 1; j * j <= x; j++) { if (x % j == 0) { if (cntt.count(j) && cntt[j] > 0) { flag = false; break; } if (cntt.count(x / j) && cntt[x / j] > 0) { flag = false; break; } } } if (flag) { ans++; } cntt[x]++; } printf("%d\n", ans); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> typedef long long ll; typedef unsigned long long ull; typedef long double LD; using namespace std; const int INF = 2147483647; const int INF2 = 0x3f3f3f3f; const ll INF64 = 3e18; const double INFD = 1e30; const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1000000007; template <typename T> inline T read() { T X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } const int MAXN = 200005; int n, m, k; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0}; int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1}; int CASE = 1; int dcmp(double x) { if (fabs(x) < EPS) return 0; return x > 0 ? 1 : -1; } struct Vector2 { double x, y; Vector2() {} Vector2(double x, double y) : x(x), y(y) {} Vector2(double rad) : x(cos(rad)), y(sin(rad)) {} friend istream &operator>>(istream &input, Vector2 &v) { input >> v.x >> v.y; return input; } friend ostream &operator<<(ostream &output, const Vector2 &v) { output << '(' << v.x << ',' << v.y << ')'; return output; } static double dot(const Vector2 &a, const Vector2 &b) { return a.x * b.x + a.y * b.y; } static double angle(const Vector2 &a, const Vector2 &b) { return acos(dot(a, b) / a.length() / b.length()); } static double cross(const Vector2 &a, const Vector2 &b) { return a.x * b.y - a.y * b.x; } static double area2(const Vector2 &a, const Vector2 &b, const Vector2 &c) { return cross(b - a, c - a); } double length() const { return sqrt(x * x + y * y); } double lengthSq() const { return x * x + y * y; } double getRad() const { return atan2(y, x); } Vector2 rotate(double rad) const { return Vector2(x * cos(rad) - y * sin(rad), x * sin(rad) + y * cos(rad)); } Vector2 unit() const { double l = this->length(); return Vector2(x / l, y / l); } Vector2 normal() const { double l = this->length(); return Vector2(-y / l, x / l); } bool operator<(const Vector2 &other) const { return x < other.x || (x == other.x && y < other.y); } bool operator==(const Vector2 &other) const { return !dcmp(x - other.x) && !dcmp(y - other.y); } Vector2 &operator+=(const Vector2 &vec) { this->x += vec.x, this->y += vec.y; return *this; } Vector2 operator+(const Vector2 &vec) const { return Vector2(this->x + vec.x, this->y + vec.y); } Vector2 &operator-=(const Vector2 &vec) { this->x -= vec.x, this->y -= vec.y; return *this; } Vector2 operator-(const Vector2 &vec) const { return Vector2(this->x - vec.x, this->y - vec.y); } Vector2 &operator*=(double v) { this->x *= v, this->y *= v; return *this; } Vector2 &operator*=(const Vector2 &v) { this->x *= v.x, this->y *= v.y; return *this; } Vector2 operator*(double v) const { return Vector2(this->x * v, this->y * v); } Vector2 operator*(const Vector2 &vec) const { return Vector2(this->x * vec.x, this->y * vec.y); } Vector2 &operator/=(double v) { this->x /= v, this->y /= v; return *this; } Vector2 &operator/=(const Vector2 &vec) { this->x /= vec.x, this->y /= vec.y; return *this; } Vector2 operator/(double v) const { return Vector2(this->x / v, this->y / v); } Vector2 operator/(const Vector2 &vec) const { return Vector2(this->x / vec.x, this->y / vec.y); } }; struct Line { Vector2 p, dir; Line() {} Line(const Vector2 &p, const Vector2 &dir) : p(p), dir(dir) {} static Vector2 lineIntersection(const Line &l1, const Line &l2) { double t = Vector2::cross(l2.dir, l1.p - l2.p) / Vector2::cross(l1.dir, l2.dir); return l1.p + l1.dir * t; } static double distanceToLine(const Vector2 &p, const Line &line) { return fabs(Vector2::cross(line.dir, p - line.p)) / line.dir.length(); } static Vector2 lineProjection(const Vector2 &p, const Line &l) { return l.p + l.dir * Vector2::dot(l.dir, p - l.p) / Vector2::dot(l.dir, l.dir); } }; struct Segment { Vector2 p1, p2; Segment() {} Segment(const Vector2 &p1, const Vector2 &p2) : p1(p1), p2(p2) {} static double distanceToSegment(const Vector2 &p, const Segment &seg) { if (seg.p1 == seg.p2) return (p - seg.p1).length(); Vector2 ab = seg.p2 - seg.p1, ap = p - seg.p1, bp = p - seg.p2; if (dcmp(Vector2::dot(ab, ap)) < 0) return ap.length(); else if (dcmp(Vector2::dot(ab, bp)) > 0) return bp.length(); else return fabs(Vector2::cross(ab, ap) / ab.length()); } static bool properIntersection(const Segment &s1, const Segment &s2) { double c1 = Vector2::cross(s1.p2 - s1.p1, s2.p1 - s1.p1), c2 = Vector2::cross(s1.p2 - s1.p1, s2.p2 - s1.p1), c3 = Vector2::cross(s2.p2 - s2.p1, s1.p1 - s2.p1), c4 = Vector2::cross(s2.p2 - s2.p1, s1.p2 - s2.p1); return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } static bool onSegment(const Vector2 &p, const Segment &s) { return !dcmp(Vector2::cross(s.p1 - p, s.p2 - p)) && dcmp(Vector2::dot(s.p1 - p, s.p2 - p)) < 0; } }; int arr[MAXN]; int main() { #ifdef LOCALLL freopen("in", "r", stdin); freopen("out", "w", stdout); #endif scanf("%d", &n); map<int, int> cntt; for (int i = 0; i < n; i++) { cntt[arr[i] = read<int>()]++; } int ans = 0; for (int i = 0; i < n; i++) { int x = arr[i]; cntt[x]--; bool flag = true; for (int j = 1; j * j <= x; j++) { if (x % j == 0) { if (cntt.count(j) && cntt[j] > 0) { flag = false; break; } if (cntt.count(x / j) && cntt[x / j] > 0) { flag = false; break; } } } if (flag) { ans++; } cntt[x]++; } printf("%d\n", ans); return 0; }
replace
34
35
34
35
0
p02642
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 #define print(A, n) \ for (ll i = 0; i < n; ++i) \ cout << A[i] << ' '; \ cout << endl; #define take(A, n) \ for (ll i = 0; i < n; ++i) \ cin >> A[i]; // for(auto& it : m) void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } // priority_queue<ll int> pq; // priority_queue <ll int, vector<ll int>, greater<ll int> > pq; // vector<ll int> v; // vector< pair<ll int,ll int> > v; // set<ll int>::iterator it=s.begin(); // unordered_map< pair<ll int,ll int>, ll int> mp; ll int power(ll int x, ll int y, ll int p) { ll int res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void computeLPSArray(string pat, int M, int *lps) { // length of the previous longest prefix suffix int len = 0; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to M-1 int i = 1; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { // This is tricky. Consider the example. // AAACAAAA and i = 7. The idea is similar // to search step. if (len != 0) { len = lps[len - 1]; // Also, note that we do not increment // i here } else // if (len == 0) { lps[i] = 0; i++; } } } } ll int nCrModp(ll int n, ll int r, ll int p) { // Optimization for the cases when r is large if (r > n - r) r = n - r; // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll int j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } #define MAXN 1000005 // stores smallest prime factor for every number int spf[MAXN]; void sieve() { spf[1] = 1; for (int i = 2; i < MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i = 4; i < MAXN; i += 2) spf[i] = 2; for (int i = 3; i * i < MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j = i * i; j < MAXN; j += i) // marking spf[j] if it is not // previously marked if (spf[j] == j) spf[j] = i; } } } int main() { fastio(); ll int T; T = 1; while (T--) { ll int n, i, j; cin >> n; ll int a[n]; map<ll int, ll int> mp; for (i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; } ll int cnt = 0; for (i = 0; i < n; i++) { ll int temp = a[i]; int flag = 0; mp[a[i]]--; for (j = 1; j * j <= a[i]; j++) { if (a[i] % j == 0) { if (mp[j] > 0) flag = 1; if (flag) break; if (mp[a[i] / j] > 0) flag = 1; if (flag) break; } } if (flag == 0) cnt++; mp[a[i]]++; } cout << cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 #define print(A, n) \ for (ll i = 0; i < n; ++i) \ cout << A[i] << ' '; \ cout << endl; #define take(A, n) \ for (ll i = 0; i < n; ++i) \ cin >> A[i]; // for(auto& it : m) void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } // priority_queue<ll int> pq; // priority_queue <ll int, vector<ll int>, greater<ll int> > pq; // vector<ll int> v; // vector< pair<ll int,ll int> > v; // set<ll int>::iterator it=s.begin(); // unordered_map< pair<ll int,ll int>, ll int> mp; ll int power(ll int x, ll int y, ll int p) { ll int res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void computeLPSArray(string pat, int M, int *lps) { // length of the previous longest prefix suffix int len = 0; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to M-1 int i = 1; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { // This is tricky. Consider the example. // AAACAAAA and i = 7. The idea is similar // to search step. if (len != 0) { len = lps[len - 1]; // Also, note that we do not increment // i here } else // if (len == 0) { lps[i] = 0; i++; } } } } ll int nCrModp(ll int n, ll int r, ll int p) { // Optimization for the cases when r is large if (r > n - r) r = n - r; // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll int j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } #define MAXN 1000005 // stores smallest prime factor for every number int spf[MAXN]; void sieve() { spf[1] = 1; for (int i = 2; i < MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i = 4; i < MAXN; i += 2) spf[i] = 2; for (int i = 3; i * i < MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j = i * i; j < MAXN; j += i) // marking spf[j] if it is not // previously marked if (spf[j] == j) spf[j] = i; } } } int main() { fastio(); ll int T; T = 1; while (T--) { ll int n, i, j; cin >> n; ll int a[n]; int mp[1000006] = {0}; for (i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; } ll int cnt = 0; for (i = 0; i < n; i++) { ll int temp = a[i]; int flag = 0; mp[a[i]]--; for (j = 1; j * j <= a[i]; j++) { if (a[i] % j == 0) { if (mp[j] > 0) flag = 1; if (flag) break; if (mp[a[i] / j] > 0) flag = 1; if (flag) break; } } if (flag == 0) cnt++; mp[a[i]]++; } cout << cnt << endl; } return 0; }
replace
141
142
141
142
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" const int N = 1e5 + 5; int n, m, k; int x11, y11, x2, y2; int dx[4] = {-1, +1, 0, 0}; int dy[4] = {0, 0, +1, -1}; vector<vector<char>> a; vector<vector<int>> vis, ways, dist; void bfs(int x, int y) { queue<pair<int, int>> q; q.push({x, y}); vis[x][y] = 1; dist[x][y] = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); // cout << x << " " << y << endl; for (int dir = 0; dir < 4; dir++) { for (int len = 1; len <= k; len++) { int nx = x + dx[dir] * len; int ny = y + dy[dir] * len; if (nx < 1 || ny < 1 || nx > n || ny > m) break; if (dist[nx][ny] <= dist[x][y] && vis[nx][ny] == 1) { break; } if (a[nx][ny] == '@') { break; } dist[nx][ny] = dist[x][y] + 1; vis[nx][ny] = 1; q.push({nx, ny}); } } // for (int i = 1; i <= n; i++) // { for (int j = 1; j <= m; j++) // { // cout << dist[i][j] << " "; // } // cout << " "; // for (int j = 1; j <= m; j++) // { // cout << vis[i][j] << " "; // } // cout << endl; // } } } int32_t main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n >> m >> k; cin >> x11 >> y11 >> x2 >> y2; a = vector<vector<char>>(n + 1, vector<char>(m + 1)); vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; bfs(x11, y11); if (!vis[x2][y2]) cout << -1; else cout << dist[x2][y2]; return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl "\n" const int N = 1e5 + 5; int n, m, k; int x11, y11, x2, y2; int dx[4] = {-1, +1, 0, 0}; int dy[4] = {0, 0, +1, -1}; vector<vector<char>> a; vector<vector<int>> vis, ways, dist; void bfs(int x, int y) { queue<pair<int, int>> q; q.push({x, y}); vis[x][y] = 1; dist[x][y] = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); // cout << x << " " << y << endl; for (int dir = 0; dir < 4; dir++) { for (int len = 1; len <= k; len++) { int nx = x + dx[dir] * len; int ny = y + dy[dir] * len; if (nx < 1 || ny < 1 || nx > n || ny > m) break; if (dist[nx][ny] <= dist[x][y] && vis[nx][ny] == 1) { break; } if (a[nx][ny] == '@') { break; } if (vis[nx][ny] == 1) continue; dist[nx][ny] = dist[x][y] + 1; vis[nx][ny] = 1; q.push({nx, ny}); } } // for (int i = 1; i <= n; i++) // { for (int j = 1; j <= m; j++) // { // cout << dist[i][j] << " "; // } // cout << " "; // for (int j = 1; j <= m; j++) // { // cout << vis[i][j] << " "; // } // cout << endl; // } } } int32_t main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n >> m >> k; cin >> x11 >> y11 >> x2 >> y2; a = vector<vector<char>>(n + 1, vector<char>(m + 1)); vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; bfs(x11, y11); if (!vis[x2][y2]) cout << -1; else cout << dist[x2][y2]; return 0; }
replace
41
42
41
43
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <bitset> #include <complex> #include <cstdio> #include <list> //< in.txt > out.txt using namespace std; // std::ios::sync_with_stdio(false); // std::cin.tie(0); const long long MOD = 998244353; const long long INF = 1e18; typedef long long LL; typedef long double LD; // typedef boost::multiprecision::cpp_int bigint; typedef pair<LL, LL> PLL; typedef pair<LD, LL> pdl; typedef pair<LD, LD> pdd; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef unsigned long long ULL; // TT(T,T):=モノイドの乗算 // require Monoid template <class T> class Segtree { private: vector<T> seg; LL RN; typedef function<T(T, T)> TT; TT f; T te; public: Segtree(LL N, TT _f, T e) : te(e) { RN = 1; while (RN < N) RN *= 2; seg.resize(2 * RN, te); f = _f; } Segtree(vector<T> &V, TT _f, T e) : te(e) { LL N = V.size(); RN = 1; while (RN < N) RN *= 2; seg.resize(2 * RN, te); f = _f; for (LL n = 0; n < N; n++) seg[RN + n] = V[n]; for (LL k = RN - 1; k >= 1; k--) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } // set n-th as x(0 index!!!!!) void set(LL n, T x) { seg[RN + n] = x; n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // change n-th number p to x*p(0 index!!!!!) void addl(LL n, T x) { seg[RN + n] = f(x, seg[RN + n]); n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // change n-th number p to p*x(0 index!!!!!) void addr(LL n, T x) { seg[RN + n] = f(seg[RN + n], x); n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // get [l,r] (0 index!!!!!) T get(LL l, LL r) { T ansl = te, ansr = te; r++; l += RN; r += RN; while (l < r) { if (l & 1) { ansl = f(ansl, seg[l]); l++; } if (r & 1) { r--; ansr = f(seg[r], ansr); } l >>= 1; r >>= 1; } return f(ansl, ansr); } // get n-th number(0 index!!!!!) T get(LL n) { return seg[RN + n]; } T operator[](LL n) { return seg[RN + n]; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); LL H, W, K; cin >> H >> W >> K; LL x0, y0, x1, y1; cin >> y0 >> x0 >> y1 >> x1; vector<vector<bool>> map; map.resize(H + 2, vector<bool>(W + 2, false)); for (LL y = 1; y <= H; y++) { string S; cin >> S; for (LL x = 1; x <= W; x++) { if (S[x - 1] == '.') map[y][x] = true; } } VVLL res; res.resize(H + 2, VLL(W + 2, 1e10)); res[y0][x0] = 0; queue<PLL> q; q.push(PLL(y0, x0)); LL vx[4] = {0, 1, 0, -1}; LL vy[4] = {1, 0, -1, 0}; while (!q.empty()) { LL cy = q.front().first; LL cx = q.front().second; q.pop(); LL d = res[cy][cx]; for (LL v = 0; v < 4; v++) { for (LL k = 1; k <= K; k++) { LL nx = cx + vx[v] * k; LL ny = cy + vy[v] * k; if (!map[ny][nx]) break; if (res[ny][nx] <= d) break; res[ny][nx] = d + 1; q.push(PLL(ny, nx)); } } } if (res[y1][x1] == 1e10) cout << -1 << "\n"; else cout << res[y1][x1] << "\n"; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <functional> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <bitset> #include <complex> #include <cstdio> #include <list> //< in.txt > out.txt using namespace std; // std::ios::sync_with_stdio(false); // std::cin.tie(0); const long long MOD = 998244353; const long long INF = 1e18; typedef long long LL; typedef long double LD; // typedef boost::multiprecision::cpp_int bigint; typedef pair<LL, LL> PLL; typedef pair<LD, LL> pdl; typedef pair<LD, LD> pdd; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef unsigned long long ULL; // TT(T,T):=モノイドの乗算 // require Monoid template <class T> class Segtree { private: vector<T> seg; LL RN; typedef function<T(T, T)> TT; TT f; T te; public: Segtree(LL N, TT _f, T e) : te(e) { RN = 1; while (RN < N) RN *= 2; seg.resize(2 * RN, te); f = _f; } Segtree(vector<T> &V, TT _f, T e) : te(e) { LL N = V.size(); RN = 1; while (RN < N) RN *= 2; seg.resize(2 * RN, te); f = _f; for (LL n = 0; n < N; n++) seg[RN + n] = V[n]; for (LL k = RN - 1; k >= 1; k--) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } // set n-th as x(0 index!!!!!) void set(LL n, T x) { seg[RN + n] = x; n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // change n-th number p to x*p(0 index!!!!!) void addl(LL n, T x) { seg[RN + n] = f(x, seg[RN + n]); n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // change n-th number p to p*x(0 index!!!!!) void addr(LL n, T x) { seg[RN + n] = f(seg[RN + n], x); n = (RN + n) / 2; while (n >= 1) { seg[n] = f(seg[2 * n], seg[2 * n + 1]); n /= 2; } } // get [l,r] (0 index!!!!!) T get(LL l, LL r) { T ansl = te, ansr = te; r++; l += RN; r += RN; while (l < r) { if (l & 1) { ansl = f(ansl, seg[l]); l++; } if (r & 1) { r--; ansr = f(seg[r], ansr); } l >>= 1; r >>= 1; } return f(ansl, ansr); } // get n-th number(0 index!!!!!) T get(LL n) { return seg[RN + n]; } T operator[](LL n) { return seg[RN + n]; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); LL H, W, K; cin >> H >> W >> K; LL x0, y0, x1, y1; cin >> y0 >> x0 >> y1 >> x1; vector<vector<bool>> map; map.resize(H + 2, vector<bool>(W + 2, false)); for (LL y = 1; y <= H; y++) { string S; cin >> S; for (LL x = 1; x <= W; x++) { if (S[x - 1] == '.') map[y][x] = true; } } VVLL res; res.resize(H + 2, VLL(W + 2, 1e10)); res[y0][x0] = 0; queue<PLL> q; q.push(PLL(y0, x0)); LL vx[4] = {0, 1, 0, -1}; LL vy[4] = {1, 0, -1, 0}; while (!q.empty()) { LL cy = q.front().first; LL cx = q.front().second; q.pop(); LL d = res[cy][cx]; for (LL v = 0; v < 4; v++) { for (LL k = 1; k <= K; k++) { LL nx = cx + vx[v] * k; LL ny = cy + vy[v] * k; if (!map[ny][nx]) break; if (res[ny][nx] <= d) break; if (res[ny][nx] != d + 1) { res[ny][nx] = d + 1; q.push(PLL(ny, nx)); } } } } if (res[y1][x1] == 1e10) cout << -1 << "\n"; else cout << res[y1][x1] << "\n"; return 0; }
replace
157
159
157
161
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define LL long long #define pb push_back #define mp make_pair #define INF 0x3f3f3f3f #define Inf 1000000000000000000LL #define F first #define S second using namespace std; typedef pair<int, int> pii; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n, m, k; int sx, sy, ex, ey; vector<vector<int>> a; vector<vector<int>> d; bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; } int main() { cin >> n >> m >> k; cin >> sx >> sy >> ex >> ey; vector<int> tp(m + 10, INF); for (int i = 1; i <= n + 10; i++) d.pb(tp); for (int i = 1; i <= n + 10; i++) a.pb(tp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; a[i][j] = (c == '@'); } } queue<pii> pq; pq.push(mp(sx, sy)); d[sx][sy] = 0; while (pq.size()) { pii x = pq.front(); pq.pop(); int nstep = d[x.F][x.S] + 1; for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j; if (outboard(tx, ty) || a[tx][ty]) break; if (d[tx][ty] > nstep) { d[tx][ty] = nstep; pq.push(mp(tx, ty)); } } } } if (d[ex][ey] == INF) puts("-1"); else cout << d[ex][ey] << endl; return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #define LL long long #define pb push_back #define mp make_pair #define INF 0x3f3f3f3f #define Inf 1000000000000000000LL #define F first #define S second using namespace std; typedef pair<int, int> pii; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n, m, k; int sx, sy, ex, ey; vector<vector<int>> a; vector<vector<int>> d; bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; } int main() { cin >> n >> m >> k; cin >> sx >> sy >> ex >> ey; vector<int> tp(m + 10, INF); for (int i = 1; i <= n + 10; i++) d.pb(tp); for (int i = 1; i <= n + 10; i++) a.pb(tp); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; a[i][j] = (c == '@'); } } queue<pii> pq; pq.push(mp(sx, sy)); d[sx][sy] = 0; while (pq.size()) { pii x = pq.front(); pq.pop(); int nstep = d[x.F][x.S] + 1; for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j; if (outboard(tx, ty) || a[tx][ty] || d[tx][ty] < nstep) break; if (d[tx][ty] > nstep) { d[tx][ty] = nstep; pq.push(mp(tx, ty)); } } } } if (d[ex][ey] == INF) puts("-1"); else cout << d[ex][ey] << endl; return 0; }
replace
47
48
47
48
TLE
p02644
C++
Time Limit Exceeded
/** * author: boutarou * created: 14.06.2020 21:50:21 **/ #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define fcout cout << fixed << setprecision(15) using lint = long long; using P = pair<int, int>; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const double PI = 3.1415926535897932; const lint INF = 3e18; const int dy[] = {0, 0, -1, 1}; const int dx[] = {1, -1, 0, 0}; int h, w, k; int sy, sx, gy, gx; vector<char> c[1000005]; vector<lint> shortest[1000005]; queue<P> que; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> k; cin >> sy >> sx >> gy >> gx; sy--, sx--, gy--, gx--; rep(i, h) { rep(j, w) { shortest[i].push_back(INF); } } rep(i, h) { rep(j, w) { char now; cin >> now; c[i].push_back(now); if (now == '@') { shortest[i][j] = -1; } } } shortest[sy][sx] = 0; que.push({sy, sx}); while (!que.empty()) { P now = que.front(); que.pop(); int now_y = now.first, now_x = now.second; rep(i, 4) { int ny = now_y + dy[i], nx = now_x + dx[i]; rep(j, k) { if (ny < 0 || ny >= h || nx < 0 || nx >= w) break; if (shortest[ny][nx] <= shortest[now_y][now_x]) break; que.push({ny, nx}); shortest[ny][nx] = shortest[now_y][now_x] + 1; ny += dy[i], nx += dx[i]; } } } if (shortest[gy][gx] == INF) { cout << -1 << endl; } else { cout << shortest[gy][gx] << endl; } return 0; }
/** * author: boutarou * created: 14.06.2020 21:50:21 **/ #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define fcout cout << fixed << setprecision(15) using lint = long long; using P = pair<int, int>; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const double PI = 3.1415926535897932; const lint INF = 3e18; const int dy[] = {0, 0, -1, 1}; const int dx[] = {1, -1, 0, 0}; int h, w, k; int sy, sx, gy, gx; vector<char> c[1000005]; vector<lint> shortest[1000005]; queue<P> que; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> k; cin >> sy >> sx >> gy >> gx; sy--, sx--, gy--, gx--; rep(i, h) { rep(j, w) { shortest[i].push_back(INF); } } rep(i, h) { rep(j, w) { char now; cin >> now; c[i].push_back(now); if (now == '@') { shortest[i][j] = -1; } } } shortest[sy][sx] = 0; que.push({sy, sx}); while (!que.empty()) { P now = que.front(); que.pop(); int now_y = now.first, now_x = now.second; rep(i, 4) { int ny = now_y + dy[i], nx = now_x + dx[i]; rep(j, k) { if (ny < 0 || ny >= h || nx < 0 || nx >= w) break; if (shortest[ny][nx] <= shortest[now_y][now_x]) break; if (shortest[ny][nx] == shortest[now_y][now_x] + 1) { ny += dy[i], nx += dx[i]; continue; } que.push({ny, nx}); shortest[ny][nx] = shortest[now_y][now_x] + 1; ny += dy[i], nx += dx[i]; } } } if (shortest[gy][gx] == INF) { cout << -1 << endl; } else { cout << shortest[gy][gx] << endl; } return 0; }
insert
72
72
72
76
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0); // int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv // ./prog.exe") >> 8), 0); template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const int DX[4] = {0, 0, +1, -1}; const int DY[4] = {+1, -1, 0, 0}; const int TYPE[4] = {1, 2, 4, 8}; int main() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> C(H); for (auto &c : C) cin >> c; vector<vector<int>> done(H, vector<int>(W)); vector<vector<int>> cost(H, vector<int>(W, 1e9)); MinHeap<tuple<int, int, int>> Q; Q.emplace(0, x1, y1); cost[x1][y1] = 0; while (!Q.empty()) { auto [c, x, y] = Q.top(); Q.pop(); if (cost[x][y] != c) continue; if (x == x2 && y == y2) { cout << c << '\n'; return 0; } for (int i = 0; i < 4; i++) { if ((done[x][y] & TYPE[i]) != 0) continue; int nx = x, ny = y, rest = K; while (rest) { nx += DX[i], ny += DY[i], rest--; if (!(0 <= nx && nx < H && 0 <= ny && ny < W)) break; if (C[nx][ny] != '.') break; if (cost[nx][ny] == c && (done[nx][ny] & TYPE[i]) == 0) { done[nx][ny] |= TYPE[i]; rest = K; } if (cost[nx][ny] > c + 1) { cost[nx][ny] = c + 1; Q.emplace(c + 1, nx, ny); } } done[x][y] |= TYPE[i]; } } cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0); // int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv // ./prog.exe") >> 8), 0); template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const int DX[4] = {0, 0, +1, -1}; const int DY[4] = {+1, -1, 0, 0}; const int TYPE[4] = {1, 2, 4, 8}; int main() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> C(H); for (auto &c : C) cin >> c; vector<vector<int>> done(H, vector<int>(W)); vector<vector<int>> cost(H, vector<int>(W, 1e9)); MinHeap<tuple<int, int, int>> Q; Q.emplace(0, x1, y1); cost[x1][y1] = 0; while (!Q.empty()) { auto [c, x, y] = Q.top(); Q.pop(); if (cost[x][y] != c) continue; if (x == x2 && y == y2) { cout << c << '\n'; return 0; } for (int i = 0; i < 4; i++) { if ((done[x][y] & TYPE[i]) != 0) continue; int nx = x, ny = y, rest = K; while (rest) { nx += DX[i], ny += DY[i], rest--; if (!(0 <= nx && nx < H && 0 <= ny && ny < W)) break; if (C[nx][ny] != '.') break; if (cost[nx][ny] < c) break; if (cost[nx][ny] == c && (done[nx][ny] & TYPE[i]) == 0) { done[nx][ny] |= TYPE[i]; rest = K; } if (cost[nx][ny] > c + 1) { cost[nx][ny] = c + 1; Q.emplace(c + 1, nx, ny); } } done[x][y] |= TYPE[i]; } } cout << -1 << '\n'; return 0; }
insert
44
44
44
46
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < n; ++i) #define rep2(i, i0, n) for (int i = i0; i < n; ++i) int main() { int h, w, k; cin >> h >> w >> k; int x1, x2, y1, y2; cin >> y1 >> x1 >> y2 >> x2; x1--; y1--; x2--; y2--; vector<vector<bool>> c(h, vector<bool>(w, false)); rep(i, h) { string s; cin >> s; rep(j, w) { if (s[j] == '@') c[i][j] = true; } } vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<pair<int, int>, int>> que; que.push({{x1, y1}, 0}); d[y1][x1] = 0; while (!que.empty()) { int cx = que.front().first.first; int cy = que.front().first.second; int cd = que.front().second; que.pop(); if (cx == x2 && cy == y2) { cout << cd << endl; return 0; } int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; rep(i, 4) { rep2(j, 1, k + 1) { int nx = cx + j * shift[i][0]; int ny = cy + j * shift[i][1]; int nd = cd + 1; if (nx < 0 || nx >= w || ny < 0 || ny >= h) break; if (c[ny][nx]) break; if (d[ny][nx] >= 0) continue; que.push({{nx, ny}, nd}); d[ny][nx] = nd; } } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < n; ++i) #define rep2(i, i0, n) for (int i = i0; i < n; ++i) int main() { int h, w, k; cin >> h >> w >> k; int x1, x2, y1, y2; cin >> y1 >> x1 >> y2 >> x2; x1--; y1--; x2--; y2--; vector<vector<bool>> c(h, vector<bool>(w, false)); rep(i, h) { string s; cin >> s; rep(j, w) { if (s[j] == '@') c[i][j] = true; } } vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<pair<int, int>, int>> que; que.push({{x1, y1}, 0}); d[y1][x1] = 0; while (!que.empty()) { int cx = que.front().first.first; int cy = que.front().first.second; int cd = que.front().second; que.pop(); if (cx == x2 && cy == y2) { cout << cd << endl; return 0; } int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; rep(i, 4) { rep2(j, 1, k + 1) { int nx = cx + j * shift[i][0]; int ny = cy + j * shift[i][1]; int nd = cd + 1; if (nx < 0 || nx >= w || ny < 0 || ny >= h) break; if (c[ny][nx]) break; if (d[ny][nx] >= 0 && d[ny][nx] <= cd) break; if (d[ny][nx] < 0 || d[ny][nx] > nd) { que.push({{nx, ny}, nd}); d[ny][nx] = nd; } } } } cout << -1 << endl; return 0; }
replace
53
58
53
59
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define P_B push_back #define PO_B pop_back #define E_B emplace_back #define M_P make_pair #define fi first #define se second #define debug(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; using VI = vector<int>; using VL = vector<ll>; using VVI = vector<vector<int>>; using VVL = vector<vector<ll>>; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; // const ll LINF = (ll)1e18 - 1; // const int INF = 1e9 - 1; // int:±2*10^9くらい ll:±9*10^18くらい void Main() { int H, W, K, x1, x2, y1, y2; bool done = false; cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2; vector<string> c(H + 2); VVI D(H + 2, VI(W + 2, -1)); rep(i, W + 2) { c[0] += '@'; c[H + 1] += '@'; } rep(i, H) { string C; cin >> C; c[i + 1] = '@' + C + '@'; } queue<Pii> Q; D[x1][y1] = 0; Q.push(M_P(x1, y1)); while (!Q.empty()) { int xp, yp, xn, yn; xp = Q.front().fi; yp = Q.front().se; Q.pop(); rep(i, 4) { xn = xp; yn = yp; rep(k, K) { xn += dx[i]; yn += dy[i]; if (c[xn][yn] == '@') break; if (D[xn][yn] >= 0 && D[xn][yn] < D[xp][yp]) break; if (D[xn][yn] != -1) continue; D[xn][yn] = D[xp][yp] + 1; if (x2 == xn && y2 == yn) { done = true; break; } Q.push(M_P(xn, yn)); } if (done) break; } if (done) break; } if (done) cout << D[x2][y2] << endl; else cout << -1 << endl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (ll i = 0; i < (n); ++i) #define all(x) (x).begin(), (x).end() #define P_B push_back #define PO_B pop_back #define E_B emplace_back #define M_P make_pair #define fi first #define se second #define debug(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; using VI = vector<int>; using VL = vector<ll>; using VVI = vector<vector<int>>; using VVL = vector<vector<ll>>; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; // const ll LINF = (ll)1e18 - 1; // const int INF = 1e9 - 1; // int:±2*10^9くらい ll:±9*10^18くらい void Main() { int H, W, K, x1, x2, y1, y2; bool done = false; cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2; vector<string> c(H + 2); VVI D(H + 2, VI(W + 2, -1)); rep(i, W + 2) { c[0] += '@'; c[H + 1] += '@'; } rep(i, H) { string C; cin >> C; c[i + 1] = '@' + C + '@'; } queue<Pii> Q; D[x1][y1] = 0; Q.push(M_P(x1, y1)); while (!Q.empty()) { int xp, yp, xn, yn; xp = Q.front().fi; yp = Q.front().se; Q.pop(); rep(i, 4) { xn = xp; yn = yp; rep(k, K) { xn += dx[i]; yn += dy[i]; if (c[xn][yn] == '@') break; if (D[xn][yn] >= 0 && D[xn][yn] <= D[xp][yp]) break; if (D[xn][yn] != -1) continue; D[xn][yn] = D[xp][yp] + 1; if (x2 == xn && y2 == yn) { done = true; break; } Q.push(M_P(xn, yn)); } if (done) break; } if (done) break; } if (done) cout << D[x2][y2] << endl; else cout << -1 << endl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
replace
71
72
71
72
TLE
p02644
C++
Time Limit Exceeded
#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; #define ll long long #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<ll> #define pp push_pop #define pll pair<ll, ll> #define in insert #define ini(a, n) memset(a, n, sizeof(a)) #define ff first #define ss second #define inf 10000000000000000 #define all(p) p.begin(), p.end() #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define print(a) \ for (auto i : a) { \ cout << i << " "; \ } #define print1(a) \ for (auto i : a) { \ cout << i.ff << " " << i.ss << endl; \ } typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> Set; unordered_map<ll, set<pll, greater<pll>>> g; unordered_map<ll, ll> vis; const ll mod = 1e9 + 7; ll power(ll x, ll y, ll p) { ll r = 1; x = x % p; while (y > 0) { if (y & 1) r = (r * x) % p; y = y >> 1; x = (x * x) % p; } return r; } const ll N = 2e5 + 10; bool imp = false; ll n, m, k; ll xx1, yy1, x1, x2; vector<vector<char>> a; vector<vi> v, dp; ll dx[4] = {-1, +1, 0, 0}; ll dy[4] = {0, 0, +1, -1}; void bfs(ll x, ll y) { queue<pll> q; q.push({x, y}); v[x][y] = 1; dp[x][y] = 0; while (!q.empty()) { x = q.front().ff; y = q.front().ss; ll d = dp[x][y]; q.pop(); for (ll i = 0; i < 4; i++) { for (ll j = 1; j <= k; j++) { ll xp = x + dx[i] * j; ll yp = y + dy[i] * j; if (xp < 0 or xp >= n or yp < 0 or yp >= m) break; if (a[xp][yp] == '@') break; // if() break; if (v[xp][yp] == 1 and (d + 1) > dp[xp][yp]) break; v[xp][yp] = 1; dp[xp][yp] = d + 1; q.push({xp, yp}); } } } } void solve() { cin >> n >> m >> k; a.resize(n + 1, vector<char>(m + 1)); cin >> xx1 >> yy1 >> x1 >> x2; v.resize(n + 1, vi(m + 1, -1)); dp.resize(n + 1, vi(m + 1, INT_MAX)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) cin >> a[i][j]; } bfs(xx1 - 1, yy1 - 1); if (v[x1 - 1][x2 - 1] == -1) { cout << -1 << endl; } else { cout << dp[x1 - 1][x2 - 1] << endl; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fio int t = 1; if (imp) cin >> t; while (t--) { // cout<<"Case #"<<tc<<": "; g.clear(); vis.clear(); solve(); // tc++; } }
#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; #define ll long long #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pb push_back #define vi vector<ll> #define pp push_pop #define pll pair<ll, ll> #define in insert #define ini(a, n) memset(a, n, sizeof(a)) #define ff first #define ss second #define inf 10000000000000000 #define all(p) p.begin(), p.end() #define que_max priority_queue<int> #define que_min priority_queue<int, vi, greater<int>> #define print(a) \ for (auto i : a) { \ cout << i << " "; \ } #define print1(a) \ for (auto i : a) { \ cout << i.ff << " " << i.ss << endl; \ } typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> Set; unordered_map<ll, set<pll, greater<pll>>> g; unordered_map<ll, ll> vis; const ll mod = 1e9 + 7; ll power(ll x, ll y, ll p) { ll r = 1; x = x % p; while (y > 0) { if (y & 1) r = (r * x) % p; y = y >> 1; x = (x * x) % p; } return r; } const ll N = 2e5 + 10; bool imp = false; ll n, m, k; ll xx1, yy1, x1, x2; vector<vector<char>> a; vector<vi> v, dp; ll dx[4] = {-1, +1, 0, 0}; ll dy[4] = {0, 0, +1, -1}; void bfs(ll x, ll y) { queue<pll> q; q.push({x, y}); v[x][y] = 1; dp[x][y] = 0; while (!q.empty()) { x = q.front().ff; y = q.front().ss; ll d = dp[x][y]; q.pop(); for (ll i = 0; i < 4; i++) { for (ll j = 1; j <= k; j++) { ll xp = x + dx[i] * j; ll yp = y + dy[i] * j; if (xp < 0 or xp >= n or yp < 0 or yp >= m) break; if (a[xp][yp] == '@') break; // if() break; if (v[xp][yp] == 1 and (d + 1) > dp[xp][yp]) break; if (v[xp][yp] == 1) continue; v[xp][yp] = 1; dp[xp][yp] = d + 1; q.push({xp, yp}); } } } } void solve() { cin >> n >> m >> k; a.resize(n + 1, vector<char>(m + 1)); cin >> xx1 >> yy1 >> x1 >> x2; v.resize(n + 1, vi(m + 1, -1)); dp.resize(n + 1, vi(m + 1, INT_MAX)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) cin >> a[i][j]; } bfs(xx1 - 1, yy1 - 1); if (v[x1 - 1][x2 - 1] == -1) { cout << -1 << endl; } else { cout << dp[x1 - 1][x2 - 1] << endl; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fio int t = 1; if (imp) cin >> t; while (t--) { // cout<<"Case #"<<tc<<": "; g.clear(); vis.clear(); solve(); // tc++; } }
insert
89
89
89
92
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define syosu(x) fixed << setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<P, int> pip; typedef vector<pip> vip; const int inf = 1 << 30; const ll INF = 1ll << 60; const double pi = acos(-1); const double eps = 1e-8; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; ll Pow(ll n, ll p) { ll r = 1; for (; p > 0; p >>= 1) { if (p & 1) r = (r * n) % mod; n = (n * n) % mod; } return r; } const int M = 2000005; ll F[M]; void Init() { F[0] = 1; for (int i = 1; i < M; i++) F[i] = F[i - 1] * i % mod; } ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; } ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); } ll nPk(ll n, ll k) { return nCk(n, k) * F[k] % mod; } int h, w, k, sx, sy, gx, gy; vs a; vvi c; int main() { cin >> h >> w >> k >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--; a = vs(h); c = vvi(h, vi(w, inf)); for (int i = 0; i < h; i++) cin >> a[i]; c[sx][sy] = 0; queue<P> q; q.push({sx, sy}); while (!q.empty()) { P p = q.front(); q.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int X = x, Y = y; for (int j = 1; j <= k; j++) { X += dx[i], Y += dy[i]; if (X < 0 || X >= h || Y < 0 || Y >= w || a[X][Y] == '@' || c[X][Y] <= c[x][y]) break; c[X][Y] = c[x][y] + 1; q.push({X, Y}); } } } /* for(int i=0;i<h;i++){ for(auto j:c[i]) cout<<(j==inf?9:j)<<' '; cout<<endl; }*/ int t = c[gx][gy]; if (t == inf) t = -1; cout << t << endl; }
#include <bits/stdc++.h> #define syosu(x) fixed << setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> P; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<P, int> pip; typedef vector<pip> vip; const int inf = 1 << 30; const ll INF = 1ll << 60; const double pi = acos(-1); const double eps = 1e-8; const ll mod = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; ll Pow(ll n, ll p) { ll r = 1; for (; p > 0; p >>= 1) { if (p & 1) r = (r * n) % mod; n = (n * n) % mod; } return r; } const int M = 2000005; ll F[M]; void Init() { F[0] = 1; for (int i = 1; i < M; i++) F[i] = F[i - 1] * i % mod; } ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; } ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); } ll nPk(ll n, ll k) { return nCk(n, k) * F[k] % mod; } int h, w, k, sx, sy, gx, gy; vs a; vvi c; int main() { cin >> h >> w >> k >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--; a = vs(h); c = vvi(h, vi(w, inf)); for (int i = 0; i < h; i++) cin >> a[i]; c[sx][sy] = 0; queue<P> q; q.push({sx, sy}); while (!q.empty()) { P p = q.front(); q.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int X = x, Y = y; for (int j = 1; j <= k; j++) { X += dx[i], Y += dy[i]; if (X < 0 || X >= h || Y < 0 || Y >= w || a[X][Y] == '@' || c[X][Y] <= c[x][y]) break; if (c[X][Y] == inf) { c[X][Y] = c[x][y] + 1; q.push({X, Y}); } } } } /* for(int i=0;i<h;i++){ for(auto j:c[i]) cout<<(j==inf?9:j)<<' '; cout<<endl; }*/ int t = c[gx][gy]; if (t == inf) t = -1; cout << t << endl; }
replace
78
80
78
82
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; // マクロ&定数&関数 ================================================ typedef unsigned int uint; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<bool> vbool; typedef vector<string> vstring; typedef vector<pair<int, int>> vpint; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<double, double>> vpdouble; typedef vector<vector<int>> vvint; typedef vector<vector<ll>> vvll; typedef vector<vpint> vvpint; typedef vector<vpll> vvpll; typedef vector<vector<double>> vvdouble; typedef vector<vector<string>> vvstring; typedef vector<vector<bool>> vvbool; typedef vector<vector<vector<ll>>> vvvll; const int INF = 1e9 + 1; const ll LLINF = 1e17 + 1; const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍 const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む const ll SEG_LEN = 1 << 25; const ll MAX = 1e9; const double PI = 3.14159265358979323846264338327950288; // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV const ll MOD = 1000000007; // 10^9 + 7 // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV //--------------------------------------------------------------- // オーバーフローチェック //--------------------------------------------------------------- bool is_overflow(ll a, ll b) { return ((a * b) / b != a); } //--------------------------------------------------------------- // 約数列挙 //--------------------------------------------------------------- vll divisor(ll n) { vll ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } //--------------------------------------------------------------- // N以下のすべての素数を列挙する(エラトステネスの篩) //--------------------------------------------------------------- vbool searchSosuu(ll N) { vbool sosuu; for (ll i = 0; i < N; i++) { sosuu.emplace_back(true); } sosuu[0] = false; sosuu[1] = false; for (ll i = 2; i < N; i++) { if (sosuu[i]) { for (ll j = 2; i * j < N; j++) { sosuu[i * j] = false; } } } return sosuu; } //--------------------------------------------------------------- // 素因数分解 O(√N) //--------------------------------------------------------------- vpll div_prime(ll n) { vpll prime_factor; for (ll i = 2; i * i <= n; i++) { ll count = 0; while (n % i == 0) { count++; n /= i; } if (count) { pair<ll, ll> temp = {i, count}; prime_factor.emplace_back(temp); } } if (n != 1) { pair<ll, ll> temp = {n, 1}; prime_factor.emplace_back(temp); } return prime_factor; } //--------------------------------------------------------------- // 素数判定 //--------------------------------------------------------------- bool is_sosuu(ll N) { if (N < 2) return false; else if (N == 2) return true; else if (N % 2 == 0) return false; for (ll i = 3; i <= sqrt(N); i += 2) { if (N % i == 0) return false; } return true; } //--------------------------------------------------------------- // 最大公約数(ユークリッドの互除法) //--------------------------------------------------------------- ll gcd(ll a, ll b) { if (a < b) { ll tmp = a; a = b; b = tmp; } ll r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } //--------------------------------------------------------------- // 最小公倍数 //--------------------------------------------------------------- ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp * (a / temp) * (b / temp); } //--------------------------------------------------------------- // 階乗 //--------------------------------------------------------------- ll factorial(ll n) { if (n <= 1) return 1; return (n * (factorial(n - 1))) % MOD; } //--------------------------------------------------------------- // 高速コンビネーション計算(前処理:O(N) 計算:O(1)) //--------------------------------------------------------------- // テーブルを作る前処理 ll comb_const = 3000005; vll fac(comb_const), finv(comb_const), inv(comb_const); bool COMineted = false; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < comb_const; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } COMineted = true; } // 二項係数計算 ll COM(ll n, ll k) { if (COMineted == false) COMinit(); if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD; } //--------------------------------------------------------------- // 繰り返し2乗法 base^sisuu //--------------------------------------------------------------- ll RepeatSquaring(ll base, ll sisuu) { if (sisuu < 0) { cout << "RepeatSquaring: 指数が負です!" << endl; return 0; } if (sisuu == 0) return 1; if (sisuu % 2 == 0) { ll t = RepeatSquaring(base, sisuu / 2) % MOD; return (t * t) % MOD; } return (base * RepeatSquaring(base, sisuu - 1)) % MOD; } //--------------------------------------------------------------- // 高速単発コンビネーション計算(O(logN)) //--------------------------------------------------------------- ll fast_com(ll a, ll b) { ll bunshi = 1; ll bunbo = 1; for (ll i = 1; i <= b; i++) { bunbo *= i; bunbo %= MOD; bunshi *= (a - i + 1); bunshi %= MOD; } ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2); ret %= MOD; while (ret < 0) { ret += MOD; } return ret; } //--------------------------------------------------------------- // 座標圧縮(O(NlogN)) 0スタートであることに注意! //--------------------------------------------------------------- vll pos_press(vll a) { vll ret = a; sort(a.begin(), a.end()); map<ll, ll> func; func[a[0]] = 0; ll next = 1; for (int i = 1; i < a.size(); i++) { if (a[i] != a[i - 1]) { func[a[i]] = next; next++; } } for (int i = 0; i < a.size(); i++) { ret[i] = func[ret[i]]; } return ret; } //--------------------------------------------------------------- // 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2)) //--------------------------------------------------------------- bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) { ll dx_ai = X1 - x1; ll dy_ai = Y1 - y1; ll dx_bi = X1 - x2; ll dy_bi = Y1 - y2; ll dx_ai2 = X2 - x1; ll dy_ai2 = Y2 - y1; ll dx_bi2 = X2 - x2; ll dy_bi2 = Y2 - y2; ll si = dx_ai * dy_bi - dy_ai * dx_bi; ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2; ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2; ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2; return (si * si2 < 0 && si3 * si4 < 0); } //--------------------------------------------------------------- // 最長増加部分列の長さ(O(NlogN)) //--------------------------------------------------------------- ll LSI(vll vec) { ll size = vec.size(); vll lsi(size + 1); // 長さjを作った時の右端の最小値 for (ll i = 0; i <= size; i++) { lsi[i] = LLINF; } lsi[0] = 0; lsi[1] = vec[0]; for (ll i = 1; i < size; i++) { // 初めてvec[i]の方が小さくなるところを探す auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]); ll idx = Iter - lsi.begin(); if (idx > 0 && lsi[idx - 1] < vec[i]) { lsi[idx] = vec[i]; } } for (ll i = size; i >= 0; i--) { if (lsi[i] < LLINF) { return i; } } } //--------------------------------------------------------------- // 木の根からの深さ //--------------------------------------------------------------- vll tree_depth(vvll edge, ll start_node, ll n_node) { vll dist(n_node, LLINF); dist[start_node] = 0; stack<pll> S; S.push({start_node, 0}); while (!S.empty()) { ll node = S.top().first; ll d = S.top().second; dist[node] = d; S.pop(); for (int i = 0; i < edge[node].size(); i++) { if (dist[edge[node][i]] == LLINF) { S.push({edge[node][i], d + 1}); } } } return dist; } //--------------------------------------------------------------- // ワーシャルフロイド法(O(N^3)) d: 正方行列(N*N) //--------------------------------------------------------------- vvll warshall_floyd(vvll d) { ll n = d.size(); for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return d; } //--------------------------------------------------------------- // Union Find //--------------------------------------------------------------- class UnionFind { public: /* UnionFind uf(要素の個数); for(int i = 0;i < 関係の個数; i++) { uf.merge(A[i], B[i]); } nを含む集合の大きさ = uf.size(n) nを含む集合の代表者 = uf.root(n) 集合の個数 = uf.n_group */ vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) ll n_group; // 集合の数 // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 n_group = sz_; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; n_group--; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; //--------------------------------------------------------------- // ダイクストラ(O(ElogV)) edge: 隣接リスト from->pair{to, cost} //--------------------------------------------------------------- template <typename T> vector<T> dijkstra(vector<vector<pair<ll, T>>> edge, int start) { vector<T> dist(edge.size(), LLINF); ll n = dist.size(); using Pi = pair<T, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; dist[start] = 0; que.emplace(dist[start], start); while (!que.empty()) { T cost; int idx; tie(cost, idx) = que.top(); que.pop(); if (dist[idx] < cost) continue; for (int i = 0; i < edge[idx].size(); i++) { auto next_cost = cost + edge[idx][i].second; if (dist[edge[idx][i].first] <= next_cost) continue; dist[edge[idx][i].first] = next_cost; que.emplace(dist[edge[idx][i].first], edge[idx][i].first); } } return dist; } //======================================================================== //======================================================================== //======================================================================== //======================================================================== int main() { ////================================== cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); ////================================== /* ~思いついたことはとりあえず絶対メモする!!~ */ ll H, W, K; cin >> H >> W >> K; ll sw, sh, gw, gh; cin >> sh >> sw >> gh >> gw; sw--; sh--; gw--; gh--; vstring S(H); for (int i = 0; i < H; i++) { cin >> S[i]; } vvll dis(H, vll(W, LLINF)); dis[sh][sw] = 0; queue<pll> q; pll start = {sh, sw}; q.push(start); while (!q.empty()) { ll nowH = q.front().first; ll nowW = q.front().second; ll nowD = dis[nowH][nowW]; q.pop(); for (int i = 0; i < 4; i++) { for (int d = 1; d <= K; d++) { ll nextH = nowH + DX[i] * d; ll nextW = nowW + DY[i] * d; ll nextD = nowD + 1; if (0 <= nextH && nextH < H && 0 <= nextW && nextW < W && nextD <= dis[nextH][nextW] && S[nextH][nextW] == '.') { dis[nextH][nextW] = nextD; q.push({nextH, nextW}); } else { break; } } } } // for (int h = 0; h < H; h++) //{ // for (int w = 0; w < W; w++) // { // cout << dis[h][w] << " "; // } // cout << endl; // } ll ans = dis[gh][gw]; if (ans >= LLINF) { cout << -1; } else { cout << ans; } }
#include <algorithm> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <vector> using namespace std; // マクロ&定数&関数 ================================================ typedef unsigned int uint; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<bool> vbool; typedef vector<string> vstring; typedef vector<pair<int, int>> vpint; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<double, double>> vpdouble; typedef vector<vector<int>> vvint; typedef vector<vector<ll>> vvll; typedef vector<vpint> vvpint; typedef vector<vpll> vvpll; typedef vector<vector<double>> vvdouble; typedef vector<vector<string>> vvstring; typedef vector<vector<bool>> vvbool; typedef vector<vector<vector<ll>>> vvvll; const int INF = 1e9 + 1; const ll LLINF = 1e17 + 1; const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;4近傍 const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:8近傍 9:(0,0)を含む const ll SEG_LEN = 1 << 25; const ll MAX = 1e9; const double PI = 3.14159265358979323846264338327950288; // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV const ll MOD = 1000000007; // 10^9 + 7 // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV //--------------------------------------------------------------- // オーバーフローチェック //--------------------------------------------------------------- bool is_overflow(ll a, ll b) { return ((a * b) / b != a); } //--------------------------------------------------------------- // 約数列挙 //--------------------------------------------------------------- vll divisor(ll n) { vll ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } //--------------------------------------------------------------- // N以下のすべての素数を列挙する(エラトステネスの篩) //--------------------------------------------------------------- vbool searchSosuu(ll N) { vbool sosuu; for (ll i = 0; i < N; i++) { sosuu.emplace_back(true); } sosuu[0] = false; sosuu[1] = false; for (ll i = 2; i < N; i++) { if (sosuu[i]) { for (ll j = 2; i * j < N; j++) { sosuu[i * j] = false; } } } return sosuu; } //--------------------------------------------------------------- // 素因数分解 O(√N) //--------------------------------------------------------------- vpll div_prime(ll n) { vpll prime_factor; for (ll i = 2; i * i <= n; i++) { ll count = 0; while (n % i == 0) { count++; n /= i; } if (count) { pair<ll, ll> temp = {i, count}; prime_factor.emplace_back(temp); } } if (n != 1) { pair<ll, ll> temp = {n, 1}; prime_factor.emplace_back(temp); } return prime_factor; } //--------------------------------------------------------------- // 素数判定 //--------------------------------------------------------------- bool is_sosuu(ll N) { if (N < 2) return false; else if (N == 2) return true; else if (N % 2 == 0) return false; for (ll i = 3; i <= sqrt(N); i += 2) { if (N % i == 0) return false; } return true; } //--------------------------------------------------------------- // 最大公約数(ユークリッドの互除法) //--------------------------------------------------------------- ll gcd(ll a, ll b) { if (a < b) { ll tmp = a; a = b; b = tmp; } ll r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } //--------------------------------------------------------------- // 最小公倍数 //--------------------------------------------------------------- ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp * (a / temp) * (b / temp); } //--------------------------------------------------------------- // 階乗 //--------------------------------------------------------------- ll factorial(ll n) { if (n <= 1) return 1; return (n * (factorial(n - 1))) % MOD; } //--------------------------------------------------------------- // 高速コンビネーション計算(前処理:O(N) 計算:O(1)) //--------------------------------------------------------------- // テーブルを作る前処理 ll comb_const = 3000005; vll fac(comb_const), finv(comb_const), inv(comb_const); bool COMineted = false; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i < comb_const; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } COMineted = true; } // 二項係数計算 ll COM(ll n, ll k) { if (COMineted == false) COMinit(); if (n < k) return 0; if (n < 0 || k < 0) return 0; return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD; } //--------------------------------------------------------------- // 繰り返し2乗法 base^sisuu //--------------------------------------------------------------- ll RepeatSquaring(ll base, ll sisuu) { if (sisuu < 0) { cout << "RepeatSquaring: 指数が負です!" << endl; return 0; } if (sisuu == 0) return 1; if (sisuu % 2 == 0) { ll t = RepeatSquaring(base, sisuu / 2) % MOD; return (t * t) % MOD; } return (base * RepeatSquaring(base, sisuu - 1)) % MOD; } //--------------------------------------------------------------- // 高速単発コンビネーション計算(O(logN)) //--------------------------------------------------------------- ll fast_com(ll a, ll b) { ll bunshi = 1; ll bunbo = 1; for (ll i = 1; i <= b; i++) { bunbo *= i; bunbo %= MOD; bunshi *= (a - i + 1); bunshi %= MOD; } ll ret = bunshi * RepeatSquaring(bunbo, MOD - 2); ret %= MOD; while (ret < 0) { ret += MOD; } return ret; } //--------------------------------------------------------------- // 座標圧縮(O(NlogN)) 0スタートであることに注意! //--------------------------------------------------------------- vll pos_press(vll a) { vll ret = a; sort(a.begin(), a.end()); map<ll, ll> func; func[a[0]] = 0; ll next = 1; for (int i = 1; i < a.size(); i++) { if (a[i] != a[i - 1]) { func[a[i]] = next; next++; } } for (int i = 0; i < a.size(); i++) { ret[i] = func[ret[i]]; } return ret; } //--------------------------------------------------------------- // 2直線の交差判定(直線(x1, y1)->(x2, y2) と 直線(X1, Y1)->(X2, Y2)) //--------------------------------------------------------------- bool is_cross(ll x1, ll y1, ll x2, ll y2, ll X1, ll Y1, ll X2, ll Y2) { ll dx_ai = X1 - x1; ll dy_ai = Y1 - y1; ll dx_bi = X1 - x2; ll dy_bi = Y1 - y2; ll dx_ai2 = X2 - x1; ll dy_ai2 = Y2 - y1; ll dx_bi2 = X2 - x2; ll dy_bi2 = Y2 - y2; ll si = dx_ai * dy_bi - dy_ai * dx_bi; ll si2 = dx_ai2 * dy_bi2 - dy_ai2 * dx_bi2; ll si3 = dx_ai * dy_ai2 - dy_ai * dx_ai2; ll si4 = dx_bi * dy_bi2 - dy_bi * dx_bi2; return (si * si2 < 0 && si3 * si4 < 0); } //--------------------------------------------------------------- // 最長増加部分列の長さ(O(NlogN)) //--------------------------------------------------------------- ll LSI(vll vec) { ll size = vec.size(); vll lsi(size + 1); // 長さjを作った時の右端の最小値 for (ll i = 0; i <= size; i++) { lsi[i] = LLINF; } lsi[0] = 0; lsi[1] = vec[0]; for (ll i = 1; i < size; i++) { // 初めてvec[i]の方が小さくなるところを探す auto Iter = lower_bound(lsi.begin(), lsi.end(), vec[i]); ll idx = Iter - lsi.begin(); if (idx > 0 && lsi[idx - 1] < vec[i]) { lsi[idx] = vec[i]; } } for (ll i = size; i >= 0; i--) { if (lsi[i] < LLINF) { return i; } } } //--------------------------------------------------------------- // 木の根からの深さ //--------------------------------------------------------------- vll tree_depth(vvll edge, ll start_node, ll n_node) { vll dist(n_node, LLINF); dist[start_node] = 0; stack<pll> S; S.push({start_node, 0}); while (!S.empty()) { ll node = S.top().first; ll d = S.top().second; dist[node] = d; S.pop(); for (int i = 0; i < edge[node].size(); i++) { if (dist[edge[node][i]] == LLINF) { S.push({edge[node][i], d + 1}); } } } return dist; } //--------------------------------------------------------------- // ワーシャルフロイド法(O(N^3)) d: 正方行列(N*N) //--------------------------------------------------------------- vvll warshall_floyd(vvll d) { ll n = d.size(); for (int k = 0; k < n; k++) { // 経由する頂点 for (int i = 0; i < n; i++) { // 始点 for (int j = 0; j < n; j++) { // 終点 d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } return d; } //--------------------------------------------------------------- // Union Find //--------------------------------------------------------------- class UnionFind { public: /* UnionFind uf(要素の個数); for(int i = 0;i < 関係の個数; i++) { uf.merge(A[i], B[i]); } nを含む集合の大きさ = uf.size(n) nを含む集合の代表者 = uf.root(n) 集合の個数 = uf.n_group */ vector<ll> par; // 各元の親を表す配列 vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化) ll n_group; // 集合の数 // Constructor UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) { for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 n_group = sz_; } void init(ll sz_) { par.resize(sz_); siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身 } // Member Function // Find ll root(ll x) { // 根の検索 while (par[x] != x) { x = par[x] = par[par[x]]; // x の親の親を x の親とする } return x; } // Union(Unite, Merge) bool merge(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; // merge technique(データ構造をマージするテク.小を大にくっつける) if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; n_group--; return true; } bool issame(ll x, ll y) { // 連結判定 return root(x) == root(y); } ll size(ll x) { // 素集合のサイズ return siz[root(x)]; } }; //--------------------------------------------------------------- // ダイクストラ(O(ElogV)) edge: 隣接リスト from->pair{to, cost} //--------------------------------------------------------------- template <typename T> vector<T> dijkstra(vector<vector<pair<ll, T>>> edge, int start) { vector<T> dist(edge.size(), LLINF); ll n = dist.size(); using Pi = pair<T, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; dist[start] = 0; que.emplace(dist[start], start); while (!que.empty()) { T cost; int idx; tie(cost, idx) = que.top(); que.pop(); if (dist[idx] < cost) continue; for (int i = 0; i < edge[idx].size(); i++) { auto next_cost = cost + edge[idx][i].second; if (dist[edge[idx][i].first] <= next_cost) continue; dist[edge[idx][i].first] = next_cost; que.emplace(dist[edge[idx][i].first], edge[idx][i].first); } } return dist; } //======================================================================== //======================================================================== //======================================================================== //======================================================================== int main() { ////================================== cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(30); ////================================== /* ~思いついたことはとりあえず絶対メモする!!~ */ ll H, W, K; cin >> H >> W >> K; ll sw, sh, gw, gh; cin >> sh >> sw >> gh >> gw; sw--; sh--; gw--; gh--; vstring S(H); for (int i = 0; i < H; i++) { cin >> S[i]; } vvll dis(H, vll(W, LLINF)); dis[sh][sw] = 0; queue<pll> q; pll start = {sh, sw}; q.push(start); while (!q.empty()) { ll nowH = q.front().first; ll nowW = q.front().second; ll nowD = dis[nowH][nowW]; q.pop(); for (int i = 0; i < 4; i++) { for (int d = 1; d <= K; d++) { ll nextH = nowH + DX[i] * d; ll nextW = nowW + DY[i] * d; ll nextD = nowD + 1; if (0 <= nextH && nextH < H && 0 <= nextW && nextW < W && nextD <= dis[nextH][nextW] && S[nextH][nextW] == '.') { if (dis[nextH][nextW] < LLINF) continue; dis[nextH][nextW] = nextD; q.push({nextH, nextW}); } else { break; } } } } // for (int h = 0; h < H; h++) //{ // for (int w = 0; w < W; w++) // { // cout << dis[h][w] << " "; // } // cout << endl; // } ll ans = dis[gh][gw]; if (ans >= LLINF) { cout << -1; } else { cout << ans; } }
insert
526
526
526
529
TLE
p02644
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; int H, W, K; pair<int, int> st, ter; vector<vector<char>> c; vector<vector<int>> d; queue<pair<long long, long long>> q; vector<pair<int, int>> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; long long bfs() { pair<long long, long long> node; q.push(st); d[st.first][st.second] = 0; while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { node = q.front(); // cout<<node.first<<" "<<node.second<<endl; q.pop(); for (int j = 0; j < 4; j++) { for (long long k = 1; k <= K; k++) { long long a = node.first + k * direction[j].first; long long b = node.second + k * direction[j].second; if (a < 0 || a >= H || b < 0 || b >= W || c[a][b] == '@' || d[a][b] <= d[node.first][node.second]) { break; } else { q.push({a, b}); d[a][b] = d[node.first][node.second] + 1; if (a == ter.first && b == ter.second) { return d[a][b]; } } } } } } return -1; } int main() { cin >> H >> W >> K; cin >> st.first >> st.second >> ter.first >> ter.second; st.first--; st.second--; ter.first--; ter.second--; c.assign(H, vector<char>(W)); d.assign(H, vector<int>(W, H * W + 1)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c[i][j]; } } cout << bfs() << endl; return 0; }
#include <iostream> #include <queue> #include <string> #include <utility> #include <vector> using namespace std; int H, W, K; pair<int, int> st, ter; vector<vector<char>> c; vector<vector<int>> d; queue<pair<long long, long long>> q; vector<pair<int, int>> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; long long bfs() { pair<long long, long long> node; q.push(st); d[st.first][st.second] = 0; while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { node = q.front(); // cout<<node.first<<" "<<node.second<<endl; q.pop(); for (int j = 0; j < 4; j++) { for (long long k = 1; k <= K; k++) { long long a = node.first + k * direction[j].first; long long b = node.second + k * direction[j].second; if (a < 0 || a >= H || b < 0 || b >= W || c[a][b] == '@' || d[a][b] <= d[node.first][node.second]) { break; } else { if (d[a][b] > d[node.first][node.second] + 1) { q.push({a, b}); d[a][b] = d[node.first][node.second] + 1; if (a == ter.first && b == ter.second) { return d[a][b]; } } } } } } } return -1; } int main() { cin >> H >> W >> K; cin >> st.first >> st.second >> ter.first >> ter.second; st.first--; st.second--; ter.first--; ter.second--; c.assign(H, vector<char>(W)); d.assign(H, vector<int>(W, H * W + 1)); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c[i][j]; } } cout << bfs() << endl; return 0; }
replace
32
36
32
38
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void solve() { ll H, W, K; cin >> H >> W >> K; ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; x2--; y1--; y2--; vector<string> grid(H); rep(i, H) cin >> grid[i]; vector<vector<ll>> cost(H, vector<ll>(W, 0)); queue<pair<P, ll>> que; cost[x1][y1] = INF; que.push({{x1, y1}, 0}); while (!que.empty()) { auto q = que.front(); que.pop(); P p = q.first; ll c = q.second; rep(i, 4) { rep2(k, 1, K + 1) { ll x = p.first + k * dx[i]; ll y = p.second + k * dy[i]; if (x >= 0 && x < H && y >= 0 && y < W) { if (grid[x][y] == '.') { if (cost[x][y] == 0) { cost[x][y] = c + 1; que.push({{x, y}, cost[x][y]}); } } else { break; } } else { break; } } } } ll ans = cost[x2][y2]; if (ans == 0) { cout << -1 << '\n'; } else { cout << ans << '\n'; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using Graph = vector<vector<ll>>; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++) #define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const ll MOD = 1000000007; const ll INF = 1000000000000000000L; #ifdef __DEBUG /** * For DEBUG * https://github.com/ta7uw/cpp-pyprint */ #include "cpp-pyprint/pyprint.h" #endif void solve() { ll H, W, K; cin >> H >> W >> K; ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; x2--; y1--; y2--; vector<string> grid(H); rep(i, H) cin >> grid[i]; vector<vector<ll>> cost(H, vector<ll>(W, 0)); queue<pair<P, ll>> que; cost[x1][y1] = INF; que.push({{x1, y1}, 0}); while (!que.empty()) { auto q = que.front(); que.pop(); P p = q.first; ll c = q.second; rep(i, 4) { rep2(k, 1, K + 1) { ll x = p.first + k * dx[i]; ll y = p.second + k * dy[i]; if (x >= 0 && x < H && y >= 0 && y < W) { if (grid[x][y] == '.') { if (cost[x][y] == 0) { cost[x][y] = c + 1; que.push({{x, y}, cost[x][y]}); } else { if (cost[x][y] < q.second + 1) { break; } } } else { break; } } else { break; } } } } ll ans = cost[x2][y2]; if (ans == 0) { cout << -1 << '\n'; } else { cout << ans << '\n'; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); return 0; }
insert
56
56
56
60
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll maxn = 1e6 + 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, m, k, sx, sy, tx, ty; char ch[maxn]; inline ll id(ll x, ll y) { return (x - 1) * m + y; } inline ll north(ll x) { return x - m; } inline ll south(ll x) { return x + m; } inline ll west(ll x) { return x - 1; } inline ll east(ll x) { return x + 1; } inline ll row(ll x) { return (x + m - 1) / m; } inline ll col(ll x) { return (x - 1) % m + 1; } inline bool chknth(ll x) { return row(x) > 1; } inline bool chksth(ll x) { return row(x) < n; } inline bool chkwst(ll x) { return col(x) > 1; } inline bool chkest(ll x) { return col(x) < m; } ll nth[maxn], sth[maxn], wst[maxn], est[maxn]; void init() { for (int i = 1; i <= n * m; i++) { nth[i] = sth[i] = wst[i] = est[i] = i; } return; } ll findnth(ll x) { return nth[x] == x ? x : nth[x] = findnth(nth[x]); } ll findsth(ll x) { return sth[x] == x ? x : sth[x] = findnth(sth[x]); } ll findwst(ll x) { return wst[x] == x ? x : wst[x] = findnth(wst[x]); } ll findest(ll x) { return est[x] == x ? x : est[x] = findnth(est[x]); } void mgenth(ll x, ll y) { nth[findnth(x)] = findnth(y); return; } void mgesth(ll x, ll y) { sth[findsth(x)] = findsth(y); return; } void mgewst(ll x, ll y) { wst[findwst(x)] = findwst(y); return; } void mgeest(ll x, ll y) { est[findest(x)] = findest(y); return; } queue<ll> q; ll d[maxn]; int main() { memset(d, -1, sizeof(d)); cin.tie(0); cin >> n >> m >> k >> sx >> sy >> tx >> ty; init(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ch[id(i, j)]; } } d[id(sx, sy)] = 0; q.push(id(sx, sy)); while (!q.empty()) { ll fr = q.front(); q.pop(); ll x = findnth(fr); while (row(fr) - row(x) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chknth(x)) { mgenth(x, north(x)); x = findnth(x); } else { break; } } x = findsth(fr); while (row(x) - row(fr) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chksth(x)) { mgesth(x, south(x)); x = findsth(x); } else { break; } } x = findwst(fr); while (col(fr) - col(x) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chkwst(x)) { mgewst(x, west(x)); x = findwst(x); } else { break; } } x = findest(fr); while (col(x) - col(fr) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chkest(x)) { mgeest(x, east(x)); x = findest(x); } else { break; } } } cout << d[id(tx, ty)] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll maxn = 1e6 + 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, m, k, sx, sy, tx, ty; char ch[maxn]; inline ll id(ll x, ll y) { return (x - 1) * m + y; } inline ll north(ll x) { return x - m; } inline ll south(ll x) { return x + m; } inline ll west(ll x) { return x - 1; } inline ll east(ll x) { return x + 1; } inline ll row(ll x) { return (x + m - 1) / m; } inline ll col(ll x) { return (x - 1) % m + 1; } inline bool chknth(ll x) { return row(x) > 1; } inline bool chksth(ll x) { return row(x) < n; } inline bool chkwst(ll x) { return col(x) > 1; } inline bool chkest(ll x) { return col(x) < m; } ll nth[maxn], sth[maxn], wst[maxn], est[maxn]; void init() { for (int i = 1; i <= n * m; i++) { nth[i] = sth[i] = wst[i] = est[i] = i; } return; } ll findnth(ll x) { return nth[x] == x ? x : nth[x] = findnth(nth[x]); } ll findsth(ll x) { return sth[x] == x ? x : sth[x] = findsth(sth[x]); } ll findwst(ll x) { return wst[x] == x ? x : wst[x] = findwst(wst[x]); } ll findest(ll x) { return est[x] == x ? x : est[x] = findest(est[x]); } void mgenth(ll x, ll y) { nth[findnth(x)] = findnth(y); return; } void mgesth(ll x, ll y) { sth[findsth(x)] = findsth(y); return; } void mgewst(ll x, ll y) { wst[findwst(x)] = findwst(y); return; } void mgeest(ll x, ll y) { est[findest(x)] = findest(y); return; } queue<ll> q; ll d[maxn]; int main() { memset(d, -1, sizeof(d)); cin.tie(0); cin >> n >> m >> k >> sx >> sy >> tx >> ty; init(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ch[id(i, j)]; } } d[id(sx, sy)] = 0; q.push(id(sx, sy)); while (!q.empty()) { ll fr = q.front(); q.pop(); ll x = findnth(fr); while (row(fr) - row(x) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chknth(x)) { mgenth(x, north(x)); x = findnth(x); } else { break; } } x = findsth(fr); while (row(x) - row(fr) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chksth(x)) { mgesth(x, south(x)); x = findsth(x); } else { break; } } x = findwst(fr); while (col(fr) - col(x) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chkwst(x)) { mgewst(x, west(x)); x = findwst(x); } else { break; } } x = findest(fr); while (col(x) - col(fr) <= k && ch[x] != '@') { if (d[x] == -1) { d[x] = d[fr] + 1; q.push(x); } if (chkest(x)) { mgeest(x, east(x)); x = findest(x); } else { break; } } } cout << d[id(tx, ty)] << endl; return 0; }
replace
78
81
78
81
TLE
p02644
C++
Time Limit Exceeded
/* * Lang : C++ * Author : little_angel */ #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int H, W, K; cin >> H >> W >> K; vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; const int inf = 1e9; vector<vector<int>> stroke(H, vector<int>(W, inf)); vector<string> g(H); int sx, sy, ex, ey, dx, dy, mx, my, x, y; cin >> sx >> sy >> ex >> ey; for (auto &i : g) cin >> i; queue<pair<int, int>> q; q.push({sx - 1, sy - 1}); stroke[sx - 1][sy - 1] = 0; while (not q.empty()) { tie(x, y) = q.front(); q.pop(); for (auto diff : del) { tie(dx, dy) = diff; for (int k = 1; k <= K; ++k) { mx = x + k * dx; my = y + k * dy; if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@') break; if (stroke[mx][my] > 1 + stroke[x][y]) { stroke[mx][my] = 1 + stroke[x][y]; if (mx == ex - 1 and my == ey - 1) { cout << stroke[mx][my] << endl; return 0; } q.push({mx, my}); } } } } cout << -1 << endl; }
/* * Lang : C++ * Author : little_angel */ #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int H, W, K; cin >> H >> W >> K; vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; const int inf = 1e9; vector<vector<int>> stroke(H, vector<int>(W, inf)); vector<string> g(H); int sx, sy, ex, ey, dx, dy, mx, my, x, y; cin >> sx >> sy >> ex >> ey; for (auto &i : g) cin >> i; queue<pair<int, int>> q; q.push({sx - 1, sy - 1}); stroke[sx - 1][sy - 1] = 0; while (not q.empty()) { tie(x, y) = q.front(); q.pop(); for (auto diff : del) { tie(dx, dy) = diff; for (int k = 1; k <= K; ++k) { mx = x + k * dx; my = y + k * dy; if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@' or 1 + stroke[x][y] > stroke[mx][my]) break; if (stroke[mx][my] > 1 + stroke[x][y]) { stroke[mx][my] = 1 + stroke[x][y]; if (mx == ex - 1 and my == ey - 1) { cout << stroke[mx][my] << endl; return 0; } q.push({mx, my}); } } } } cout << -1 << endl; }
replace
36
37
36
38
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF 1000000007 #define LINF (1LL << 61) #define PI 3.14159265358979 typedef long long i64; typedef pair<i64, i64> P; inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } i64 h, w, k, a, b, c, d; string s[1010101]; void solve() { cin >> h >> w >> k; cin >> a >> b >> c >> d; a--; b--; c--; d--; for (int i = 0; i < h; i++) { cin >> s[i]; } vector<vector<int>> v(h, vector<int>(w, INF)); queue<pair<P, int>> q; q.push(make_pair(P(a, b), 0)); v[a][b] = 0; int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first.first, y = p.first.second, cnt = p.second; if (v[x][y] != cnt) continue; if (x == c && y == d) { cout << cnt << endl; return; } for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int nx = x + dx[i] * j, ny = y + dy[i] * j; if (nx < 0 || ny < 0 || nx >= h || ny >= w) break; // if(v[nx][ny] <= cnt+1) break; if (s[nx][ny] == '@') break; if (chmin(v[nx][ny], cnt + 1)) { q.push(make_pair(P(nx, ny), cnt + 1)); } } } } cout << -1 << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 1000000007 #define LINF (1LL << 61) #define PI 3.14159265358979 typedef long long i64; typedef pair<i64, i64> P; inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } i64 h, w, k, a, b, c, d; string s[1010101]; void solve() { cin >> h >> w >> k; cin >> a >> b >> c >> d; a--; b--; c--; d--; for (int i = 0; i < h; i++) { cin >> s[i]; } vector<vector<int>> v(h, vector<int>(w, INF)); queue<pair<P, int>> q; q.push(make_pair(P(a, b), 0)); v[a][b] = 0; int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first.first, y = p.first.second, cnt = p.second; if (v[x][y] != cnt) continue; if (x == c && y == d) { cout << cnt << endl; return; } for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int nx = x + dx[i] * j, ny = y + dy[i] * j; if (nx < 0 || ny < 0 || nx >= h || ny >= w) break; if (v[nx][ny] < cnt + 1) break; if (s[nx][ny] == '@') break; if (chmin(v[nx][ny], cnt + 1)) { q.push(make_pair(P(nx, ny), cnt + 1)); } } } } cout << -1 << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
replace
61
62
61
63
TLE
p02644
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define _USE_MATH_DEFINES #include <math.h> using namespace std; using ll = long long; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vvvll = std::vector<vvll>; using ld = long double; using vld = std::vector<ld>; using vvld = std::vector<vld>; using vvvld = std::vector<vvld>; using bl = bool; using vbl = std::vector<bl>; using vvbl = std::vector<vbl>; using vvvbl = std::vector<vvbl>; using ss = string; using vss = std::vector<ss>; #define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); (i)++) #define repm(i, n, m) for (ll(i) = m; (i) < (ll)(n); (i)++) #define repi(i, n) for (ll(i) = (n - 1); (i) >= (ll)(0); (i)--) #define repit(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define all(v) v.begin(), v.end() #define println(v) \ { \ ll i = 0; \ for (auto(a) : v) { \ cerr << setw(2) << i << " : " << a << endl; \ i++; \ } \ } template <class T> void printsp(vector<T> v, size_t w = 3) { for (auto(a) : v) { cerr << setw(w) << a << " "; } cerr << endl; } 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; } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } template <class T1, class T2> void bye(T1 a, T2 b) { cout << a << " " << b << '\n'; exit(0); } template <class T1, class T2, class T3> void bye(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << '\n'; exit(0); } template <class T> void bye(vector<T> A) { rep(i, (ll)A.size()) { cout << A[i]; if (i != (ll)A.size() - 1) { cout << " "; } } cout << endl; exit(0); } // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector<string> ostream &operator<<(ostream &os, const vector<string> &vec) { ll N = to_string((ll)vec.size()).length(); string head; rep(i, N + 2) { head += "-"; } rep(i, 40) { head += to_string(i % 10); } os << setw(N) << head << endl; for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; } return os; } // vector<ll> ostream &operator<<(ostream &os, const vector<ll> &vec) { ll N = to_string((ll)vec.size()).length(); for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; } return os; } // vector<ld> ostream &operator<<(ostream &os, const vector<ld> &vec) { ll N = to_string((ll)vec.size()).length(); for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; // os <<vec[i] << endl; } return os; } // vvll ostream &operator<<(ostream &os, const vector<vector<ll>> &mat) { ll keta = 1; rep(i, mat.size()) { rep(j, mat[i].size()) { ll x = mat[i][j]; chmax(keta, (ll)to_string(x).length()); } } ll N = to_string((ll)mat.size()).length(); rep(i, mat.size()) { os << setw(N) << i << ": "; rep(j, mat[i].size()) { ll x = mat[i][j]; os << setw(keta + 1) << x; } os << endl; } return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (ll i = 0; i < (ll)vec.size(); i++) { // os << setw(3) <<i <<" : "<< setw(5) << vec[i] << endl; os << setw(5) << vec[i]; } os << endl; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repit(itr, map_var) { // for(auto itr : map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repit(itr, set_var) { // for( auto itr : set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr #define DUMPCOLOR ("\033[36m") void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << DUMPCOLOR; DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); DUMPOUT << "\033[m"; } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << DUMPCOLOR << "" << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << "\033[m" \ << "", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; inline vll cinvll(ll N, ll minus = 0) { vll A(N); rep(i, N) { cin >> A[i]; A[i] -= minus; } return move(A); } inline vvll zerosll(ll H, ll W, ll val = 0) { vvll A(H, vll(W, val)); return move(A); } inline vvvll zerosll3(ll H, ll W, ll C, ll val = 0) { vvvll A(H, vvll(W, vll(C, val))); return move(A); } vvll cinvll2(ll H, ll W, ll bias = 0) { vvll A = zerosll(H, W); rep(i, H) { rep(j, W) { cin >> A[i][j]; A[i][j] += bias; } } return move(A); } tuple<vll, vll> aho2(ll N) { vll A(N), B(N); rep(i, N) { cin >> A[i] >> B[i]; } return (make_tuple(A, B)); } template <class T = ll> inline vector<T> cinv(ll N, T val = 0) { vector<T> v(N); rep(i, N) { cin >> v[i]; v[i] += val; } return move(v); } template <class T = ll> inline vector<vector<T>> zeros(ll H, ll W, T val = 0) { vector<vector<T>> A(H, vector<T>(W, val)); return move(A); } template <class T = ll> inline vector<vector<vector<T>>> zeros3(ll H, ll W, ll C, T val = 0) { vector<vector<vector<T>>> A(H, vector<vector<T>>(W, vector<T>(C, val))); return move(A); } queue<vll> Q; ll H, W, K; ll sx, sy, ex, ey; vss C; vvll D; void bfs_sub(ll dd, ll y, ll x, ll my, ll mx) { repm(k, K + 1, 1) { ll yy = y + k * my; ll xx = x + k * mx; // dump(yy,xx); if (yy < 0 or yy >= W or xx < 0 or xx >= H) { break; } if (C[xx][yy] == '@') { break; } if (D[xx][yy] != -1) { continue; } D[xx][yy] = dd; Q.push({dd, yy, xx}); } } void bfs() { Q.push({0, sy, sx}); D[sx][sy] = 0; while (not Q.empty()) { vll q = Q.front(); Q.pop(); ll d = q[0]; ll y = q[1]; ll x = q[2]; // dump(d,y,x); ///////////////// bfs_sub(d + 1, y, x, -1, 0); bfs_sub(d + 1, y, x, +1, 0); bfs_sub(d + 1, y, x, 0, -1); bfs_sub(d + 1, y, x, 0, +1); } } void func() { cin >> H >> W >> K; cin >> sx >> sy >> ex >> ey; sx--; sy--; ex--; ey--; C.resize(H); D = zeros(H, W, -1ll); rep(i, H) { cin >> C[i]; } dump(C); dump(D); dump(sx, sy, ex, ey); bfs(); dump(D); cout << D[ex][ey] << endl; } int main() { dump("start"); func(); return 0; }
#include "bits/stdc++.h" #define _USE_MATH_DEFINES #include <math.h> using namespace std; using ll = long long; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vvvll = std::vector<vvll>; using ld = long double; using vld = std::vector<ld>; using vvld = std::vector<vld>; using vvvld = std::vector<vvld>; using bl = bool; using vbl = std::vector<bl>; using vvbl = std::vector<vbl>; using vvvbl = std::vector<vvbl>; using ss = string; using vss = std::vector<ss>; #define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); (i)++) #define repm(i, n, m) for (ll(i) = m; (i) < (ll)(n); (i)++) #define repi(i, n) for (ll(i) = (n - 1); (i) >= (ll)(0); (i)--) #define repit(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define all(v) v.begin(), v.end() #define println(v) \ { \ ll i = 0; \ for (auto(a) : v) { \ cerr << setw(2) << i << " : " << a << endl; \ i++; \ } \ } template <class T> void printsp(vector<T> v, size_t w = 3) { for (auto(a) : v) { cerr << setw(w) << a << " "; } cerr << endl; } 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; } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } template <class T1, class T2> void bye(T1 a, T2 b) { cout << a << " " << b << '\n'; exit(0); } template <class T1, class T2, class T3> void bye(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << '\n'; exit(0); } template <class T> void bye(vector<T> A) { rep(i, (ll)A.size()) { cout << A[i]; if (i != (ll)A.size() - 1) { cout << " "; } } cout << endl; exit(0); } // vector template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector<string> ostream &operator<<(ostream &os, const vector<string> &vec) { ll N = to_string((ll)vec.size()).length(); string head; rep(i, N + 2) { head += "-"; } rep(i, 40) { head += to_string(i % 10); } os << setw(N) << head << endl; for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; } return os; } // vector<ll> ostream &operator<<(ostream &os, const vector<ll> &vec) { ll N = to_string((ll)vec.size()).length(); for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; } return os; } // vector<ld> ostream &operator<<(ostream &os, const vector<ld> &vec) { ll N = to_string((ll)vec.size()).length(); for (ll i = 0; i < (ll)vec.size(); i++) { os << setw(N) << i << ": " << vec[i] << endl; // os <<vec[i] << endl; } return os; } // vvll ostream &operator<<(ostream &os, const vector<vector<ll>> &mat) { ll keta = 1; rep(i, mat.size()) { rep(j, mat[i].size()) { ll x = mat[i][j]; chmax(keta, (ll)to_string(x).length()); } } ll N = to_string((ll)mat.size()).length(); rep(i, mat.size()) { os << setw(N) << i << ": "; rep(j, mat[i].size()) { ll x = mat[i][j]; os << setw(keta + 1) << x; } os << endl; } return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (ll i = 0; i < (ll)vec.size(); i++) { // os << setw(3) <<i <<" : "<< setw(5) << vec[i] << endl; os << setw(5) << vec[i]; } os << endl; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; repit(itr, map_var) { // for(auto itr : map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; repit(itr, set_var) { // for( auto itr : set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr #define DUMPCOLOR ("\033[36m") void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << DUMPCOLOR; DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); DUMPOUT << "\033[m"; } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << DUMPCOLOR << "" << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << "\033[m" \ << "", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; inline vll cinvll(ll N, ll minus = 0) { vll A(N); rep(i, N) { cin >> A[i]; A[i] -= minus; } return move(A); } inline vvll zerosll(ll H, ll W, ll val = 0) { vvll A(H, vll(W, val)); return move(A); } inline vvvll zerosll3(ll H, ll W, ll C, ll val = 0) { vvvll A(H, vvll(W, vll(C, val))); return move(A); } vvll cinvll2(ll H, ll W, ll bias = 0) { vvll A = zerosll(H, W); rep(i, H) { rep(j, W) { cin >> A[i][j]; A[i][j] += bias; } } return move(A); } tuple<vll, vll> aho2(ll N) { vll A(N), B(N); rep(i, N) { cin >> A[i] >> B[i]; } return (make_tuple(A, B)); } template <class T = ll> inline vector<T> cinv(ll N, T val = 0) { vector<T> v(N); rep(i, N) { cin >> v[i]; v[i] += val; } return move(v); } template <class T = ll> inline vector<vector<T>> zeros(ll H, ll W, T val = 0) { vector<vector<T>> A(H, vector<T>(W, val)); return move(A); } template <class T = ll> inline vector<vector<vector<T>>> zeros3(ll H, ll W, ll C, T val = 0) { vector<vector<vector<T>>> A(H, vector<vector<T>>(W, vector<T>(C, val))); return move(A); } queue<vll> Q; ll H, W, K; ll sx, sy, ex, ey; vss C; vvll D; void bfs_sub(ll dd, ll y, ll x, ll my, ll mx) { repm(k, K + 1, 1) { ll yy = y + k * my; ll xx = x + k * mx; // dump(yy,xx); if (yy < 0 or yy >= W or xx < 0 or xx >= H) { break; } if (D[xx][yy] == D[x][y]) break; if (C[xx][yy] == '@') { break; } if (D[xx][yy] != -1) { continue; } D[xx][yy] = dd; Q.push({dd, yy, xx}); } } void bfs() { Q.push({0, sy, sx}); D[sx][sy] = 0; while (not Q.empty()) { vll q = Q.front(); Q.pop(); ll d = q[0]; ll y = q[1]; ll x = q[2]; // dump(d,y,x); ///////////////// bfs_sub(d + 1, y, x, -1, 0); bfs_sub(d + 1, y, x, +1, 0); bfs_sub(d + 1, y, x, 0, -1); bfs_sub(d + 1, y, x, 0, +1); } } void func() { cin >> H >> W >> K; cin >> sx >> sy >> ex >> ey; sx--; sy--; ex--; ey--; C.resize(H); D = zeros(H, W, -1ll); rep(i, H) { cin >> C[i]; } dump(C); dump(D); dump(sx, sy, ex, ey); bfs(); dump(D); cout << D[ex][ey] << endl; } int main() { dump("start"); func(); return 0; }
insert
279
279
279
281
TLE
p02644
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; // template #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define ALL(v) (v).begin(), (v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps = 1e-12; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // end typedef pair<int, int> P; typedef pair<int, P> T; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<vector<char>> g(h, vector<char>(w)); rep(i, 0, h) rep(j, 0, w) cin >> g[i][j]; vector<vector<int>> dist(h, vector<int>(w, inf)); dist[x1][y1] = 0; queue<P> que; que.push(P(x1, y1)); while (!que.empty()) { auto [nx, ny] = que.front(); que.pop(); rep(i, 0, 4) rep(add, 1, k + 1) { int tx = nx + dx[i] * add, ty = ny + dy[i] * add; if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@') break; if (dist[tx][ty] == inf) { dist[tx][ty] = dist[nx][ny] + 1; que.push(P(tx, ty)); } } } if (dist[x2][y2] == inf) puts("-1"); else cout << dist[x2][y2] << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; // template #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define ALL(v) (v).begin(), (v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps = 1e-12; template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // end typedef pair<int, int> P; typedef pair<int, P> T; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<vector<char>> g(h, vector<char>(w)); rep(i, 0, h) rep(j, 0, w) cin >> g[i][j]; vector<vector<int>> dist(h, vector<int>(w, inf)); dist[x1][y1] = 0; queue<P> que; que.push(P(x1, y1)); while (!que.empty()) { auto [nx, ny] = que.front(); que.pop(); rep(i, 0, 4) rep(add, 1, k + 1) { int tx = nx + dx[i] * add, ty = ny + dy[i] * add; if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@') break; if (dist[tx][ty] <= dist[nx][ny]) break; if (dist[tx][ty] == inf) { dist[tx][ty] = dist[nx][ny] + 1; que.push(P(tx, ty)); } } } if (dist[x2][y2] == inf) puts("-1"); else cout << dist[x2][y2] << endl; return 0; }
insert
54
54
54
56
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; constexpr const ll dx[4] = {1, 0, -1, 0}; constexpr const ll dy[4] = {0, 1, 0, -1}; constexpr const ll MOD = 1e9 + 7; constexpr const ll mod = 998244353; constexpr const ll INF = 1LL << 60; constexpr const ll inf = 1 << 30; constexpr const char rt = '\n'; constexpr const char sp = ' '; #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define elif else if #define all(a, v, ...) \ ([&](decltype((v)) w) { return (a)(begin(w), end(w), ##__VA_ARGS__); })(v) #define rall(a, v, ...) \ ([&](decltype((v)) w) { return (a)(rbegin(w), rend(w), ##__VA_ARGS__); })(v) #define fi first #define se second template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << rt; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << rt; \ rep(_i, h) { \ rep(_j, w) { \ if (a[_i][_j] <= -INF / 2) \ cerr << '-'; \ if (abs(a[_i][_j]) >= INF / 2) \ cerr << "∞" << sp; \ else \ cerr << a[_i][_j] << sp; \ } \ cerr << rt; \ } \ cerr << "]" << rt; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(_i, n) { \ if (_i) \ cerr << sp; \ if (a[_i] <= -INF / 2) \ cerr << '-'; \ if (abs(a[_i]) >= INF / 2) \ cerr << "∞" << sp; \ else \ cerr << a[_i]; \ } \ cerr << "]" << rt; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) // Stream #define fout(n) cout << fixed << setprecision(n) struct io { io() { cin.tie(nullptr), ios::sync_with_stdio(false); } } io; // Speed #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // Math inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; rep(i, 64) { if (n & (1LL << i)) r *= a; chmod(r, m); a *= a; chmod(a, m); } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } signed main() { ll H, W, K; cin >> H >> W >> K; ll A, B, C, D2; cin >> A >> B >> C >> D2; A--, B--, C--, D2--; string S[H]; rep(i, H) cin >> S[i]; ll D[H][W]; rep(i, H) rep(j, W) D[i][j] = INF; using P = pair<ll, ll>; queue<P> Q; D[A][B] = 0; Q.push(mp(A, B)); while (Q.size()) { ll i, j; tie(i, j) = Q.front(); Q.pop(); rep(k, 4) { inc(l, 1, K) { ll x = i + dx[k] * l, y = j + dy[k] * l; debug(i, j, x, y); if (not(0 <= x and x < H)) break; if (not(0 <= y and y < W)) break; if (S[x][y] != '.') break; if (D[x][y] <= D[i][j]) break; if (D[x][y] > D[i][j] + 1) { Q.push(mp(x, y)); D[x][y] = D[i][j] + 1; } } } } S[A][B] = 'S'; S[C][D2] = 'G'; if (D[C][D2] >= INF) { cout << -1 << rt; } else { cout << D[C][D2] << rt; } } // -g -D_GLIBCXX_DEBUG -fsanitize=undefined
#include <bits/stdc++.h> using namespace std; // Define using ll = long long; using ull = unsigned long long; using ld = long double; template <class T> using pvector = vector<pair<T, T>>; template <class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>; constexpr const ll dx[4] = {1, 0, -1, 0}; constexpr const ll dy[4] = {0, 1, 0, -1}; constexpr const ll MOD = 1e9 + 7; constexpr const ll mod = 998244353; constexpr const ll INF = 1LL << 60; constexpr const ll inf = 1 << 30; constexpr const char rt = '\n'; constexpr const char sp = ' '; #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define elif else if #define all(a, v, ...) \ ([&](decltype((v)) w) { return (a)(begin(w), end(w), ##__VA_ARGS__); })(v) #define rall(a, v, ...) \ ([&](decltype((v)) w) { return (a)(rbegin(w), rend(w), ##__VA_ARGS__); })(v) #define fi first #define se second template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } // Debug #define debug(...) \ { \ cerr << __LINE__ << ": " << #__VA_ARGS__ << " = "; \ for (auto &&X : {__VA_ARGS__}) \ cerr << "[" << X << "] "; \ cerr << rt; \ } #define dump(a, h, w) \ { \ cerr << __LINE__ << ": " << #a << " = [" << rt; \ rep(_i, h) { \ rep(_j, w) { \ if (a[_i][_j] <= -INF / 2) \ cerr << '-'; \ if (abs(a[_i][_j]) >= INF / 2) \ cerr << "∞" << sp; \ else \ cerr << a[_i][_j] << sp; \ } \ cerr << rt; \ } \ cerr << "]" << rt; \ } #define vdump(a, n) \ { \ cerr << __LINE__ << ": " << #a << " = ["; \ rep(_i, n) { \ if (_i) \ cerr << sp; \ if (a[_i] <= -INF / 2) \ cerr << '-'; \ if (abs(a[_i]) >= INF / 2) \ cerr << "∞" << sp; \ else \ cerr << a[_i]; \ } \ cerr << "]" << rt; \ } // Loop #define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i) #define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i) #define rep(i, n) for (ll i = 0, _##i = (n); i < _##i; ++i) #define each(i, a) for (auto &&i : a) // Stream #define fout(n) cout << fixed << setprecision(n) struct io { io() { cin.tie(nullptr), ios::sync_with_stdio(false); } } io; // Speed #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // Math inline constexpr ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; } inline constexpr ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; } inline constexpr ll modulo(const ll n, const ll m = MOD) { ll k = n % m; return k + m * (k < 0); } inline constexpr ll chmod(ll &n, const ll m = MOD) { n %= m; return n += m * (n < 0); } inline constexpr ll mpow(ll a, ll n, const ll m = MOD) { ll r = 1; rep(i, 64) { if (n & (1LL << i)) r *= a; chmod(r, m); a *= a; chmod(a, m); } return r; } inline ll inv(const ll n, const ll m = MOD) { ll a = n, b = m, x = 1, y = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); x -= t * y; swap(x, y); } return modulo(x, m); } signed main() { ll H, W, K; cin >> H >> W >> K; ll A, B, C, D2; cin >> A >> B >> C >> D2; A--, B--, C--, D2--; string S[H]; rep(i, H) cin >> S[i]; ll D[H][W]; rep(i, H) rep(j, W) D[i][j] = INF; using P = pair<ll, ll>; queue<P> Q; D[A][B] = 0; Q.push(mp(A, B)); while (Q.size()) { ll i, j; tie(i, j) = Q.front(); Q.pop(); rep(k, 4) { inc(l, 1, K) { ll x = i + dx[k] * l, y = j + dy[k] * l; if (not(0 <= x and x < H)) break; if (not(0 <= y and y < W)) break; if (S[x][y] != '.') break; if (D[x][y] <= D[i][j]) break; if (D[x][y] > D[i][j] + 1) { Q.push(mp(x, y)); D[x][y] = D[i][j] + 1; } } } } S[A][B] = 'S'; S[C][D2] = 'G'; if (D[C][D2] >= INF) { cout << -1 << rt; } else { cout << D[C][D2] << rt; } } // -g -D_GLIBCXX_DEBUG -fsanitize=undefined
delete
166
167
166
166
TLE
p02644
C++
Time Limit Exceeded
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using vint = vector<int>; using vvint = vector<vint>; using vll = vector<ll>; using vvll = vector<vll>; using vchar = vector<char>; using vvchar = vector<vchar>; using ld = long double; using vld = vector<ld>; using vvld = vector<vld>; using vp = vector<P>; using vpp = vector<pair<P, P>>; using vvp = vector<vp>; #define rep(i, n) for (int i = 0; i < n; ++i) #pragma region Debug istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; } ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.first << "," << a.second << ")"; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } #pragma endregion #define fst first #define snd second const int INF = 1e9; struct edge { int c; P p; edge(int c, P p) : c(c), p(p) {} bool operator>(const edge &r) const { return c > r.c; } }; int main() { int h, w, k, x1, y1, x2, y2; cin >> h >> w >> k; cin >> y1 >> x1 >> y2 >> x2; x1--; y1--; x2--; y2--; vvchar g(h, vchar(w)); rep(y, h) rep(x, w) cin >> g[y][x]; vvint v(h, vint(w, INF)); priority_queue<edge, vector<edge>, greater<edge>> q; P start = P(y1, x1), goal = P(y2, x2); q.push(edge(0, start)); v[y1][x1] = 0; vint dy = {0, 1, 0, -1}, dx = {1, 0, -1, 0}; while (q.size()) { edge e = q.top(); q.pop(); P p = e.p; if (p == goal) { break; } int now = e.c; rep(dir, 4) for (int i = 1; i <= k; i++) { int ny = p.fst + dy[dir] * i; int nx = p.snd + dx[dir] * i; if (ny < 0 || nx < 0 || ny > h - 1 || nx > w - 1 || g[ny][nx] == '@') { break; } if (v[ny][nx] > now + 1) { v[ny][nx] = now + 1; q.push(edge(now + 1, P(ny, nx))); } } } ld ans = ceil(v[y2][x2]); if (ans == INF) { ans = -1; } cout << ans << endl; return 0; }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using vint = vector<int>; using vvint = vector<vint>; using vll = vector<ll>; using vvll = vector<vll>; using vchar = vector<char>; using vvchar = vector<vchar>; using ld = long double; using vld = vector<ld>; using vvld = vector<vld>; using vp = vector<P>; using vpp = vector<pair<P, P>>; using vvp = vector<vp>; #define rep(i, n) for (int i = 0; i < n; ++i) #pragma region Debug istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; } ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.first << "," << a.second << ")"; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } #pragma endregion #define fst first #define snd second const int INF = 1e9; struct edge { int c; P p; edge(int c, P p) : c(c), p(p) {} bool operator>(const edge &r) const { return c > r.c; } }; int main() { int h, w, k, x1, y1, x2, y2; cin >> h >> w >> k; cin >> y1 >> x1 >> y2 >> x2; x1--; y1--; x2--; y2--; vvchar g(h, vchar(w)); rep(y, h) rep(x, w) cin >> g[y][x]; vvint v(h, vint(w, INF)); priority_queue<edge, vector<edge>, greater<edge>> q; P start = P(y1, x1), goal = P(y2, x2); q.push(edge(0, start)); v[y1][x1] = 0; vint dy = {0, 1, 0, -1}, dx = {1, 0, -1, 0}; while (q.size()) { edge e = q.top(); q.pop(); P p = e.p; if (p == goal) { break; } int now = e.c; rep(dir, 4) for (int i = 1; i <= k; i++) { int ny = p.fst + dy[dir] * i; int nx = p.snd + dx[dir] * i; if (ny < 0 || nx < 0 || ny > h - 1 || nx > w - 1 || g[ny][nx] == '@' || v[ny][nx] <= now) { break; } if (v[ny][nx] > now + 1) { v[ny][nx] = now + 1; q.push(edge(now + 1, P(ny, nx))); } } } ld ans = ceil(v[y2][x2]); if (ans == INF) { ans = -1; } cout << ans << endl; return 0; }
replace
80
81
80
82
TLE
p02644
C++
Time Limit Exceeded
#include "algorithm" #include "cmath" #include "cstdio" #include "cstring" #include "iostream" #include "map" #include "queue" #include "set" #include "vector" using namespace std; #define MAXN 1000006 // #define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int, int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin(), (x).end() #define mem(a) memset(a, 0, sizeof a) typedef long long ll; #define P 1000000007 int n, m, k; int sx, sy, xx, yy; char ch[MAXN]; int mm[MAXN], vis[MAXN], dis[MAXN]; int getid(int x, int y) { return (x - 1) * m + y; } int dirx[] = {0, 1, -1, 0}; int diry[] = {1, 0, 0, -1}; queue<pii> Q; void bfs() { memset(dis, 0x3f, sizeof dis); Q.push(mp(sx, sy)); dis[getid(sx, sy)] = 0; while (!Q.empty()) { auto u = Q.front(); Q.pop(); int x = u.fi, y = u.se, xx, yy; // cout << x << ' ' << y << ' ' << dis[getid( x , y )] << endl; for (int i = 0; i < 4; ++i) { xx = x, yy = y; for (int j = 0; j < k; ++j) { xx = xx + dirx[i], yy = yy + diry[i]; int idd = getid(xx, yy); if (xx < 1 || yy < 1 || xx > n || yy > m || mm[idd] || (vis[idd] && dis[getid(x, y)] + 1 > dis[idd])) break; else { Q.push(mp(xx, yy)), vis[idd] = 1, dis[idd] = min(dis[idd], dis[getid(x, y)] + 1); } } } } } void solve() { cin >> n >> m >> k; cin >> sx >> sy >> xx >> yy; rep(i, 1, n) { scanf("%s", ch + 1); rep(j, 1, m) mm[getid(i, j)] = (ch[j] == '@' ? 1 : 0); } bfs(); if (dis[getid(xx, yy)] > 1e9) return puts("-1"), void(); else printf("%d\n", dis[getid(xx, yy)]); } signed main() { // int T;cin >> T;while( T-- ) solve(); solve(); }
#include "algorithm" #include "cmath" #include "cstdio" #include "cstring" #include "iostream" #include "map" #include "queue" #include "set" #include "vector" using namespace std; #define MAXN 1000006 // #define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int, int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin(), (x).end() #define mem(a) memset(a, 0, sizeof a) typedef long long ll; #define P 1000000007 int n, m, k; int sx, sy, xx, yy; char ch[MAXN]; int mm[MAXN], vis[MAXN], dis[MAXN]; int getid(int x, int y) { return (x - 1) * m + y; } int dirx[] = {0, 1, -1, 0}; int diry[] = {1, 0, 0, -1}; queue<pii> Q; void bfs() { memset(dis, 0x3f, sizeof dis); Q.push(mp(sx, sy)); dis[getid(sx, sy)] = 0; while (!Q.empty()) { auto u = Q.front(); Q.pop(); int x = u.fi, y = u.se, xx, yy; // cout << x << ' ' << y << ' ' << dis[getid( x , y )] << endl; for (int i = 0; i < 4; ++i) { xx = x, yy = y; for (int j = 0; j < k; ++j) { xx = xx + dirx[i], yy = yy + diry[i]; int idd = getid(xx, yy); if (xx < 1 || yy < 1 || xx > n || yy > m || mm[idd] || (vis[idd] && dis[getid(x, y)] + 1 > dis[idd])) break; else { if (!vis[idd]) Q.push(mp(xx, yy)); vis[idd] = 1, dis[idd] = min(dis[idd], dis[getid(x, y)] + 1); } } } } } void solve() { cin >> n >> m >> k; cin >> sx >> sy >> xx >> yy; rep(i, 1, n) { scanf("%s", ch + 1); rep(j, 1, m) mm[getid(i, j)] = (ch[j] == '@' ? 1 : 0); } bfs(); if (dis[getid(xx, yy)] > 1e9) return puts("-1"), void(); else printf("%d\n", dis[getid(xx, yy)]); } signed main() { // int T;cin >> T;while( T-- ) solve(); solve(); }
replace
53
55
53
56
TLE
p02644
C++
Time Limit Exceeded
// f-pond-skater.yml #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; template <typename V> using uset = unordered_set<V>; template <typename K, typename V> using umap = unordered_map<K, V>; template <typename C> int sz(C &v) { return v.size(); } const int DIR[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct Pos { int x, y; }; struct Ent { int x, y, predir; }; int R, C, K; Pos st, en; vector<vector<bool>> grid; vector<vector<ll>> dis[5]; deque<Ent> q; // priority_queue<Ent> q; ll make_dp(ll dis, ll k) { return (k << 32LL) | dis; } const ll MSK = (1LL << 32) - 1; inline ll gdis(ll dp) { return dp & MSK; } inline ll gk(ll dp) { return dp >> 32LL; } bool cmp(const ll a, const ll b) { return gdis(a) == gdis(b) ? gk(a) < gk(b) : gdis(a) < gdis(b); } inline void tryPush(int x, int y, int predir, int ndis, int k, bool inc) { ll disPair = make_dp(ndis, k); if (x < 0 || x >= R || y < 0 || y >= C || !grid[x][y] || cmp(dis[predir][x][y], disPair)) return; Ent ent{x, y, predir}; if (inc) q.push_back(ent); else q.push_front(ent); dis[predir][x][y] = disPair; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> R >> C >> K; cin >> st.x >> st.y >> en.x >> en.y; grid.assign(R, vector<bool>(C, true)); for (int i = 0; i < R; i++) { string tmp; cin >> tmp; for (int j = 0; j < C; j++) grid[i][j] = tmp[j] == '.'; } st.x--; st.y--; en.x--; en.y--; for (int i = 0; i < 5; i++) { dis[i].assign(R, vector<ll>(C, make_dp(INF, 0))); // inq[i].assign(R, vector<bool>(C, false)); } // assert(0); // cout << st.x<<' '<<st.y<<endl; dis[4][st.x][st.y] = make_dp(0, 0); q.push_back({st.x, st.y, 4}); // assert(0); while (!q.empty()) { auto c = q.front(); q.pop_front(); int cdis = gdis(dis[c.predir][c.x][c.y]), ck = gk(dis[c.predir][c.x][c.y]); // inq[c.predir][c.x][c.y] = false; // if (c.x == en.x && c.y == en.y) { // cout << cdis << '\n'; // return 0; // } if (cmp(dis[c.predir][c.x][c.y], make_dp(cdis, ck))) continue; // cout << "pos=("<<c.x<<", "<<c.y<<") dir="<<c.predir<<" dis=("<<c.dis<<", // "<<c.k<<")"<<endl; if (c.predir == 4) { for (int i = 0; i < 4; i++) { tryPush(c.x + DIR[i][0], c.y + DIR[i][1], i, cdis + 1, 1, true); } } else { for (int i = 0; i < 4; i++) { bool inc = i != c.predir || ck == K; int newK = inc ? 1 : (ck + 1); tryPush(c.x + DIR[i][0], c.y + DIR[i][1], i, cdis + inc, newK, inc); } } } // cout << "-1\n"; int ans = INF; for (int i = 0; i < 5; i++) ans = min(ans, (int)gdis(dis[i][en.x][en.y])); cout << (ans == INF ? -1 : ans) << '\n'; return 0; }
// f-pond-skater.yml #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; template <typename V> using uset = unordered_set<V>; template <typename K, typename V> using umap = unordered_map<K, V>; template <typename C> int sz(C &v) { return v.size(); } const int DIR[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct Pos { int x, y; }; struct Ent { int x, y, predir; }; int R, C, K; Pos st, en; vector<vector<bool>> grid; vector<vector<ll>> dis[5]; deque<Ent> q; // priority_queue<Ent> q; ll make_dp(ll dis, ll k) { return (k << 32LL) | dis; } const ll MSK = (1LL << 32) - 1; inline ll gdis(ll dp) { return dp & MSK; } inline ll gk(ll dp) { return dp >> 32LL; } bool cmp(const ll a, const ll b) { return gdis(a) == gdis(b) ? gk(a) < gk(b) : gdis(a) < gdis(b); } inline void tryPush(int x, int y, int predir, int ndis, int k, bool inc) { ll disPair = make_dp(ndis, k); if (x < 0 || x >= R || y < 0 || y >= C || !grid[x][y] || cmp(dis[predir][x][y], disPair)) return; Ent ent{x, y, predir}; if (gdis(dis[predir][x][y]) > ndis) { if (inc) q.push_back(ent); else q.push_front(ent); } dis[predir][x][y] = disPair; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> R >> C >> K; cin >> st.x >> st.y >> en.x >> en.y; grid.assign(R, vector<bool>(C, true)); for (int i = 0; i < R; i++) { string tmp; cin >> tmp; for (int j = 0; j < C; j++) grid[i][j] = tmp[j] == '.'; } st.x--; st.y--; en.x--; en.y--; for (int i = 0; i < 5; i++) { dis[i].assign(R, vector<ll>(C, make_dp(INF, 0))); // inq[i].assign(R, vector<bool>(C, false)); } // assert(0); // cout << st.x<<' '<<st.y<<endl; dis[4][st.x][st.y] = make_dp(0, 0); q.push_back({st.x, st.y, 4}); // assert(0); while (!q.empty()) { auto c = q.front(); q.pop_front(); int cdis = gdis(dis[c.predir][c.x][c.y]), ck = gk(dis[c.predir][c.x][c.y]); // inq[c.predir][c.x][c.y] = false; // if (c.x == en.x && c.y == en.y) { // cout << cdis << '\n'; // return 0; // } if (cmp(dis[c.predir][c.x][c.y], make_dp(cdis, ck))) continue; // cout << "pos=("<<c.x<<", "<<c.y<<") dir="<<c.predir<<" dis=("<<c.dis<<", // "<<c.k<<")"<<endl; if (c.predir == 4) { for (int i = 0; i < 4; i++) { tryPush(c.x + DIR[i][0], c.y + DIR[i][1], i, cdis + 1, 1, true); } } else { for (int i = 0; i < 4; i++) { bool inc = i != c.predir || ck == K; int newK = inc ? 1 : (ck + 1); tryPush(c.x + DIR[i][0], c.y + DIR[i][1], i, cdis + inc, newK, inc); } } } // cout << "-1\n"; int ans = INF; for (int i = 0; i < 5; i++) ans = min(ans, (int)gdis(dis[i][en.x][en.y])); cout << (ans == INF ? -1 : ans) << '\n'; return 0; }
replace
46
50
46
52
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REP1(i, a, b) for (int i = a; i <= (int)(b); i++) #define ALL(x) begin(x), end(x) #define PB push_back using namespace std; typedef int64_t LL; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> inline bool chmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); } const int INF = 1e9; int dx[4] = {1, 0, -1, 0}; // D,R,U,L; int dy[4] = {0, 1, 0, -1}; int h, w, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --y1; --x2; --y2; vector<string> G(h); REP(i, h) cin >> G[i]; vector<VI> d(h, VI(w, INF)); d[x1][y1] = 0; queue<PII> q; q.emplace(x1, y1); while (q.size()) { int x = q.front().first; int y = q.front().second; q.pop(); REP(i, 4) { int nx = x; int ny = y; REP(_, k) { nx = nx + dx[i]; ny = ny + dy[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w || G[nx][ny] == '@') break; if (d[nx][ny] > d[x][y] + 1) { d[nx][ny] = d[x][y] + 1; q.emplace(nx, ny); } } } } cout << (d[x2][y2] < INF ? d[x2][y2] : -1) << '\n'; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REP1(i, a, b) for (int i = a; i <= (int)(b); i++) #define ALL(x) begin(x), end(x) #define PB push_back using namespace std; typedef int64_t LL; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> inline bool chmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); } const int INF = 1e9; int dx[4] = {1, 0, -1, 0}; // D,R,U,L; int dy[4] = {0, 1, 0, -1}; int h, w, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --y1; --x2; --y2; vector<string> G(h); REP(i, h) cin >> G[i]; vector<VI> d(h, VI(w, INF)); d[x1][y1] = 0; queue<PII> q; q.emplace(x1, y1); while (q.size()) { int x = q.front().first; int y = q.front().second; q.pop(); REP(i, 4) { int nx = x; int ny = y; REP(_, k) { nx = nx + dx[i]; ny = ny + dy[i]; if (nx < 0 || nx >= h || ny < 0 || ny >= w || G[nx][ny] == '@') break; if (d[nx][ny] <= d[x][y]) break; if (d[nx][ny] > d[x][y] + 1) { d[nx][ny] = d[x][y] + 1; q.emplace(nx, ny); } } } } cout << (d[x2][y2] < INF ? d[x2][y2] : -1) << '\n'; return 0; }
insert
56
56
56
58
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // #define int long long int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int32_t main() { ios::sync_with_stdio(0), cin.tie(0); int h, w, k; cin >> h >> w >> k; int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; int l[h][w]; memset(l, -1, sizeof l); char g[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) cin >> g[i][j]; } queue<pair<int, int>> q; q.push({a, b}), l[a][b] = 0; while (!q.empty()) { a = q.front().first, b = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int x = a + j * dx[i], y = b + j * dy[i]; if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@') break; if (l[x][y] == -1) { q.push({x, y}); l[x][y] = l[a][b] + 1; } l[x][y] = min(l[x][y], l[a][b] + 1); } } } cout << l[c][d]; }
#include <bits/stdc++.h> using namespace std; // #define int long long int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int32_t main() { ios::sync_with_stdio(0), cin.tie(0); int h, w, k; cin >> h >> w >> k; int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; int l[h][w]; memset(l, -1, sizeof l); char g[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) cin >> g[i][j]; } queue<pair<int, int>> q; q.push({a, b}), l[a][b] = 0; while (!q.empty()) { a = q.front().first, b = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int x = a + j * dx[i], y = b + j * dy[i]; if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@') break; if (l[x][y] == -1) { q.push({x, y}); l[x][y] = l[a][b] + 1; } if (l[x][y] < l[a][b] + 1) break; l[x][y] = min(l[x][y], l[a][b] + 1); } } } cout << l[c][d]; }
insert
35
35
35
37
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } int di[] = {0, 1, 0, -1}; int dj[] = {1, 0, -1, 0}; int main() { ios_base::sync_with_stdio(0); // finput("./input"); int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<string> s(h); rep(i, h) cin >> s[i]; vvi d(h, vi(w, INF)); queue<pii> Q; Q.push(pii(x1 - 1, y1 - 1)); d[x1 - 1][y1 - 1] = 0; while (!Q.empty()) { int i = Q.front().fi; int j = Q.front().se; Q.pop(); rep(l, 4) { REP(m, 1, k + 1) { int ni = i + di[l] * m; int nj = j + dj[l] * m; if (ni < 0 || h <= ni) continue; if (nj < 0 || w <= nj) continue; if (s[ni][nj] == '@') break; if (d[ni][nj] <= d[i][j]) break; d[ni][nj] = d[i][j] + 1; Q.push(pii(ni, nj)); } } } if (d[x2 - 1][y2 - 1] == INF) cout << -1 << endl; else cout << d[x2 - 1][y2 - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } int di[] = {0, 1, 0, -1}; int dj[] = {1, 0, -1, 0}; int main() { ios_base::sync_with_stdio(0); // finput("./input"); int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<string> s(h); rep(i, h) cin >> s[i]; vvi d(h, vi(w, INF)); queue<pii> Q; Q.push(pii(x1 - 1, y1 - 1)); d[x1 - 1][y1 - 1] = 0; while (!Q.empty()) { int i = Q.front().fi; int j = Q.front().se; Q.pop(); rep(l, 4) { REP(m, 1, k + 1) { int ni = i + di[l] * m; int nj = j + dj[l] * m; if (ni < 0 || h <= ni) continue; if (nj < 0 || w <= nj) continue; if (s[ni][nj] == '@') break; if (d[ni][nj] <= d[i][j]) break; if (d[ni][nj] == INF) { d[ni][nj] = d[i][j] + 1; Q.push(pii(ni, nj)); } } } } if (d[x2 - 1][y2 - 1] == INF) cout << -1 << endl; else cout << d[x2 - 1][y2 - 1] << endl; return 0; }
replace
94
96
94
98
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; ////////////////////////////// Begin Macros #define all(x) (x).begin(), (x).end() #define rep(i, N) for (int i = 0; i < (int)(N); i++) #define reprev(i, N) for (int i = (int)(N)-1; i >= 0; i--) #define rep1(i, N) for (int i = 1; i <= (int)(N); i++) #define rep1rev(i, N) for (int i = (int)(N); i >= 0; i--) #define forbe(i, b, e) for (int i = (b); i < (e); i++) #define forberev(i, b, e) for (int i = (e)-1; i >= (b); i--) #define forfl(i, f, l) for (int i = (f); i <= (l); i++) #define forflrev(i, f, l) for (int i = (l); i >= (f); i--) using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first + r.first, l.second + r.second}; } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first - r.first, l.second - r.second}; } template <typename T> pair<T, T> operator*(const pair<T, T> &l, const T &r) { return {l.first * r, l.second * r}; } template <typename T> pair<T, T> operator/(const pair<T, T> &l, const T &r) { return {l.first / r, l.second / r}; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &pa) { is >> pa.first >> pa.second; return is; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename... Ts> istream &operator>>(istream &is, tuple<Ts...> &theTuple) { apply([&is](Ts &...tupleArgs) { ((is >> tupleArgs), ...); }, theTuple); return is; } template <typename... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &theTuple) { apply( [&os](const Ts &...tupleArgs) { os << '('; size_t n(0); ((os << tupleArgs << (++n < sizeof...(Ts) ? "," : "")), ...); os << ')'; }, theTuple); return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void reset(vector<T> &v, const T reset_to) { for (auto &x : v) x = reset_to; } inline int popcount(const unsigned int x) { return __builtin_popcount(x); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ll nC2(ll n) { return n * (n - 1) / 2; } const int intinf = numeric_limits<int>::max(); const ll llinf = numeric_limits<ll>::max(); const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; ////////////////////////////// End Macros void solve() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<vector<char>> c(H, vector<char>(W)); cin >> c; vector<vector<int>> d(H, vector<int>(W, -1)); queue<pii> que; d[x1][y1] = 0; que.push({x1, y1}); while (!que.empty()) { pii txy = que.front(); que.pop(); auto [tx, ty] = txy; int td = d[tx][ty]; rep(i, 4) { pii nxy = txy; rep1(k, K) { nxy = nxy + udlr[i]; auto [nx, ny] = nxy; if (not(0 <= nx and nx < H and 0 <= ny and ny < W)) break; if (c[nx][ny] == '@') break; if (d[nx][ny] != -1) continue; d[nx][ny] = td + 1; que.push(nxy); } } } cout << d[x2][y2] << endl; } int main() { cerr << "start" << endl; // srand(time(0)); cout << fixed << setprecision(15); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; ////////////////////////////// Begin Macros #define all(x) (x).begin(), (x).end() #define rep(i, N) for (int i = 0; i < (int)(N); i++) #define reprev(i, N) for (int i = (int)(N)-1; i >= 0; i--) #define rep1(i, N) for (int i = 1; i <= (int)(N); i++) #define rep1rev(i, N) for (int i = (int)(N); i >= 0; i--) #define forbe(i, b, e) for (int i = (b); i < (e); i++) #define forberev(i, b, e) for (int i = (e)-1; i >= (b); i--) #define forfl(i, f, l) for (int i = (f); i <= (l); i++) #define forflrev(i, f, l) for (int i = (l); i >= (f); i--) using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first + r.first, l.second + r.second}; } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first - r.first, l.second - r.second}; } template <typename T> pair<T, T> operator*(const pair<T, T> &l, const T &r) { return {l.first * r, l.second * r}; } template <typename T> pair<T, T> operator/(const pair<T, T> &l, const T &r) { return {l.first / r, l.second / r}; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &pa) { is >> pa.first >> pa.second; return is; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename... Ts> istream &operator>>(istream &is, tuple<Ts...> &theTuple) { apply([&is](Ts &...tupleArgs) { ((is >> tupleArgs), ...); }, theTuple); return is; } template <typename... Ts> ostream &operator<<(ostream &os, const tuple<Ts...> &theTuple) { apply( [&os](const Ts &...tupleArgs) { os << '('; size_t n(0); ((os << tupleArgs << (++n < sizeof...(Ts) ? "," : "")), ...); os << ')'; }, theTuple); return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void reset(vector<T> &v, const T reset_to) { for (auto &x : v) x = reset_to; } inline int popcount(const unsigned int x) { return __builtin_popcount(x); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ll nC2(ll n) { return n * (n - 1) / 2; } const int intinf = numeric_limits<int>::max(); const ll llinf = numeric_limits<ll>::max(); const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; ////////////////////////////// End Macros void solve() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<vector<char>> c(H, vector<char>(W)); cin >> c; vector<vector<int>> d(H, vector<int>(W, -1)); queue<pii> que; d[x1][y1] = 0; que.push({x1, y1}); while (!que.empty()) { pii txy = que.front(); que.pop(); auto [tx, ty] = txy; int td = d[tx][ty]; rep(i, 4) { pii nxy = txy; rep1(k, K) { nxy = nxy + udlr[i]; auto [nx, ny] = nxy; if (not(0 <= nx and nx < H and 0 <= ny and ny < W)) break; if (c[nx][ny] == '@') break; if (d[nx][ny] != -1) { if (d[nx][ny] <= td) break; if (d[nx][ny] == td + 1) continue; } d[nx][ny] = td + 1; que.push(nxy); } } } cout << d[x2][y2] << endl; } int main() { cerr << "start" << endl; // srand(time(0)); cout << fixed << setprecision(15); solve(); return 0; }
replace
191
193
191
197
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> field(h); rep(i, h) cin >> field[i]; vector<vector<int>> dist(h, vector<int>(w, -1)); dist[x1][y1] = 0; queue<pair<int, int>> que; que.push(make_pair(x1, y1)); while (!que.empty()) { pair<int, int> current_pos = que.front(); int x = current_pos.first; int y = current_pos.second; que.pop(); rep(d, 4) { rep(i, k) { int next_x = x + (i + 1) * dx[d]; int next_y = y + (i + 1) * dy[d]; if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w) break; if (field[next_x][next_y] == '@') break; if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1) break; que.push(make_pair(next_x, next_y)); dist[next_x][next_y] = dist[x][y] + 1; } } } /*for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cout << dist[i][j] << ' '; } cout << endl; }*/ cout << dist[x2][y2] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> field(h); rep(i, h) cin >> field[i]; vector<vector<int>> dist(h, vector<int>(w, -1)); dist[x1][y1] = 0; queue<pair<int, int>> que; que.push(make_pair(x1, y1)); while (!que.empty()) { pair<int, int> current_pos = que.front(); int x = current_pos.first; int y = current_pos.second; que.pop(); rep(d, 4) { rep(i, k) { int next_x = x + (i + 1) * dx[d]; int next_y = y + (i + 1) * dy[d]; if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w) break; if (field[next_x][next_y] == '@') break; if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1) break; if (dist[next_x][next_y] != -1) continue; que.push(make_pair(next_x, next_y)); dist[next_x][next_y] = dist[x][y] + 1; } } } /*for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cout << dist[i][j] << ' '; } cout << endl; }*/ cout << dist[x2][y2] << endl; }
insert
38
38
38
40
TLE
p02644
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; #define rep(i, begin, n) for (int i = begin; i < n; i++) #define repe(i, begin, n) for (int i = begin; i <= n; i++) #define repr(i, begin, n) for (int i = begin; i > begin - n; i--) #define repre(i, begin, end) for (int i = begin; i >= end; 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; } const int inf = INT_MAX; const int MOD = 1000000007; const long long INF = LLONG_MAX; // ------------------------------------------------------- ll H, W, K; ll sx, sy, gx, gy; vector<ll> c[1010101]; ll DX[] = {0, 1, 0, -1}; ll DY[] = {1, 0, -1, 0}; int main() { cin >> H >> W >> K; cin >> sx >> sy >> gx >> gy; repe(i, 1, H) { string s; cin >> s; c[i].push_back(-1); for (auto e : s) { if (e == '@') { c[i].push_back(-1); } else { c[i].push_back(inf); } } c[i].push_back(-1); } repe(i, 0, W + 1) { c[0].push_back(-1); c[H + 1].push_back(-1); } queue<Pll> que; que.push(Pll(sx, sy)); c[sx][sy] = 0; while (!que.empty()) { auto elem = que.front(); que.pop(); ll cx = elem.first; ll cy = elem.second; rep(i, 0, 4) { ll dx = DX[i]; ll dy = DY[i]; repe(j, 1, K) { ll nx = cx + dx * j; ll ny = cy + dy * j; // 移動中に蓮にあたった場合 if (c[nx][ny] == -1) { break; } if (c[nx][ny] != inf) { continue; } c[nx][ny] = c[cx][cy] + 1; que.push(Pll(nx, ny)); } } } if (c[gx][gy] == inf) { cout << -1; return 0; } cout << c[gx][gy]; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; #define rep(i, begin, n) for (int i = begin; i < n; i++) #define repe(i, begin, n) for (int i = begin; i <= n; i++) #define repr(i, begin, n) for (int i = begin; i > begin - n; i--) #define repre(i, begin, end) for (int i = begin; i >= end; 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; } const int inf = INT_MAX; const int MOD = 1000000007; const long long INF = LLONG_MAX; // ------------------------------------------------------- ll H, W, K; ll sx, sy, gx, gy; vector<ll> c[1010101]; ll DX[] = {0, 1, 0, -1}; ll DY[] = {1, 0, -1, 0}; int main() { cin >> H >> W >> K; cin >> sx >> sy >> gx >> gy; repe(i, 1, H) { string s; cin >> s; c[i].push_back(-1); for (auto e : s) { if (e == '@') { c[i].push_back(-1); } else { c[i].push_back(inf); } } c[i].push_back(-1); } repe(i, 0, W + 1) { c[0].push_back(-1); c[H + 1].push_back(-1); } queue<Pll> que; que.push(Pll(sx, sy)); c[sx][sy] = 0; while (!que.empty()) { auto elem = que.front(); que.pop(); ll cx = elem.first; ll cy = elem.second; rep(i, 0, 4) { ll dx = DX[i]; ll dy = DY[i]; repe(j, 1, K) { ll nx = cx + dx * j; ll ny = cy + dy * j; // 移動中に蓮にあたった場合 if (c[nx][ny] == -1) { break; } if (c[nx][ny] != inf) { if (c[nx][ny] <= c[cx][cy]) { break; } continue; } c[nx][ny] = c[cx][cy] + 1; que.push(Pll(nx, ny)); } } } if (c[gx][gy] == inf) { cout << -1; return 0; } cout << c[gx][gy]; }
insert
82
82
82
85
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using P = pair<int, int>; #define debug(var) cerr << "[" << #var << "] " << var << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) const ll mod = 1000000007; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; const int INF = 1001001001; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<string> c(h); rep(i, h) cin >> c[i]; vector<vector<int>> dist(h, vector<int>(w, INF)); queue<P> q; q.emplace(x1, y1); dist[x1][y1] = 0; while (!q.empty()) { P now = q.front(); q.pop(); int nx = now.first; int ny = now.second; int d = dist[nx][ny]; rep(i, 4) { for (int j = 1; j <= k; j++) { int ix = nx + dx[i] * j; int iy = ny + dy[i] * j; if (ix < 0 || ix >= h || iy < 0 || iy >= w) break; if (dist[ix][iy] <= d) break; if (c[ix][iy] == '@') break; dist[ix][iy] = d + 1; q.emplace(ix, iy); } } /* rep(i, h) { rep(j, w) { cout << dist[i][j]; } cout << endl; } */ } int ans = dist[x2][y2]; if (ans == INF) ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(), (x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using P = pair<int, int>; #define debug(var) cerr << "[" << #var << "] " << var << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) const ll mod = 1000000007; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; const int INF = 1001001001; int main() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; vector<string> c(h); rep(i, h) cin >> c[i]; vector<vector<int>> dist(h, vector<int>(w, INF)); queue<P> q; q.emplace(x1, y1); dist[x1][y1] = 0; while (!q.empty()) { P now = q.front(); q.pop(); int nx = now.first; int ny = now.second; int d = dist[nx][ny]; rep(i, 4) { for (int j = 1; j <= k; j++) { int ix = nx + dx[i] * j; int iy = ny + dy[i] * j; if (ix < 0 || ix >= h || iy < 0 || iy >= w) break; if (dist[ix][iy] <= d) break; if (c[ix][iy] == '@') break; if (dist[ix][iy] == d + 1) continue; dist[ix][iy] = d + 1; q.emplace(ix, iy); } } /* rep(i, h) { rep(j, w) { cout << dist[i][j]; } cout << endl; } */ } int ans = dist[x2][y2]; if (ans == INF) ans = -1; cout << ans << endl; return 0; }
insert
47
47
47
49
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() const ll INF = 1e18; int main() { int h, w, k, x, y, nx, ny, K; cin >> h >> w >> k; int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; vector<string> g(h); rep(i, h) cin >> g[i]; vector<int> dx = {1, -1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; vector<vector<ll>> depth(h, vector<ll>(w, INF)); queue<P> q; q.push(make_pair(b, a)); depth[a][b] = 0; while (!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); if (x == d and y == c) break; rep(i, 4) { nx = x; ny = y; K = k; while (K--) { nx += dx[i]; ny += dy[i]; if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and depth[ny][nx] > depth[y][x]) { depth[ny][nx] = depth[y][x] + 1; q.push(make_pair(nx, ny)); } else break; } } } if (depth[c][d] == INF) cout << -1 << endl; else cout << depth[c][d] << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() const ll INF = 1e18; int main() { int h, w, k, x, y, nx, ny, K; cin >> h >> w >> k; int a, b, c, d; cin >> a >> b >> c >> d; a--; b--; c--; d--; vector<string> g(h); rep(i, h) cin >> g[i]; vector<int> dx = {1, -1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; vector<vector<ll>> depth(h, vector<ll>(w, INF)); queue<P> q; q.push(make_pair(b, a)); depth[a][b] = 0; while (!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); if (x == d and y == c) break; rep(i, 4) { nx = x; ny = y; K = k; while (K--) { nx += dx[i]; ny += dy[i]; if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and depth[ny][nx] > depth[y][x]) { if (depth[ny][nx] != depth[y][x] + 1) { depth[ny][nx] = depth[y][x] + 1; q.push(make_pair(nx, ny)); } } else break; } } } if (depth[c][d] == INF) cout << -1 << endl; else cout << depth[c][d] << endl; }
replace
41
43
41
45
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, m, n) for (int i = int(m); i < int(n); i++) #define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) // #define int long long #ifdef LOCAL #define DEBUG(s) cout << (s) << endl #define dump(x) cerr << #x << " = " << (x) << endl #define BR cout << endl; #else #define DEBUG(s) \ do { \ } while (0) #define dump(x) \ do { \ } while (0) #define BR #endif using namespace std; using UI = unsigned int; using UL = unsigned long; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VS = vector<string>; using PII = pair<int, int>; using VP = vector<PII>; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; void solve() { LL h, w, k; cin >> h >> w >> k; VLL x(2), y(2); REP(i, 0, 2) cin >> x[i] >> y[i]; REP(i, 0, 2) { --x[i]; --y[i]; } VS a(h); REP(i, 0, h) cin >> a[i]; priority_queue<pair<int, PII>, vector<pair<int, PII>>, greater<pair<int, PII>>> q; q.emplace(0, MP(x[0], y[0])); VVI d(h, VI(w, 1 << 30)); while (!q.empty()) { auto p = q.top(); q.pop(); int dis = p.first, px = p.second.first, py = p.second.second; if (d[px][py] < dis) continue; REP(i, 0, 4) { REP(j, 1, k + 1) { int nx = px + dx[i] * j; int ny = py + dy[i] * j; if (nx < 0 || nx >= h || ny < 0 || ny >= w) break; if (a[nx][ny] == '@') break; if (d[nx][ny] <= dis + 1) continue; d[nx][ny] = dis + 1; q.emplace(dis + 1, MP(nx, ny)); } } } if (d[x[1]][y[1]] == 1 << 30) cout << -1 << endl; else cout << d[x[1]][y[1]] << endl; } signed main() { solve(); return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, m, n) for (int i = int(m); i < int(n); i++) #define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i) #define EACH(i, c) for (auto &(i) : c) #define all(c) begin(c), end(c) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(begin(c), end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) // #define int long long #ifdef LOCAL #define DEBUG(s) cout << (s) << endl #define dump(x) cerr << #x << " = " << (x) << endl #define BR cout << endl; #else #define DEBUG(s) \ do { \ } while (0) #define dump(x) \ do { \ } while (0) #define BR #endif using namespace std; using UI = unsigned int; using UL = unsigned long; using LL = long long; using ULL = unsigned long long; using VI = vector<int>; using VVI = vector<VI>; using VLL = vector<LL>; using VVLL = vector<VLL>; using VS = vector<string>; using PII = pair<int, int>; using VP = vector<PII>; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; void solve() { LL h, w, k; cin >> h >> w >> k; VLL x(2), y(2); REP(i, 0, 2) cin >> x[i] >> y[i]; REP(i, 0, 2) { --x[i]; --y[i]; } VS a(h); REP(i, 0, h) cin >> a[i]; priority_queue<pair<int, PII>, vector<pair<int, PII>>, greater<pair<int, PII>>> q; q.emplace(0, MP(x[0], y[0])); VVI d(h, VI(w, 1 << 30)); while (!q.empty()) { auto p = q.top(); q.pop(); int dis = p.first, px = p.second.first, py = p.second.second; if (d[px][py] < dis) continue; REP(i, 0, 4) { REP(j, 1, k + 1) { int nx = px + dx[i] * j; int ny = py + dy[i] * j; if (nx < 0 || nx >= h || ny < 0 || ny >= w) break; if (a[nx][ny] == '@') break; if (d[nx][ny] < dis + 1) break; if (d[nx][ny] <= dis + 1) continue; d[nx][ny] = dis + 1; q.emplace(dis + 1, MP(nx, ny)); } } } if (d[x[1]][y[1]] == 1 << 30) cout << -1 << endl; else cout << d[x[1]][y[1]] << endl; } signed main() { solve(); return 0; }
insert
102
102
102
104
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, k; cin >> h >> w >> k; int sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty; vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9)); for (int i = 0; i < w + 2; i++) { d[0][i] = d[h + 1][i] = 0; } for (int i = 1; i <= h; i++) { string s; cin >> s; d[i][0] = d[i][w + 1] = 0; for (int j = 0; j < w; j++) { if (s[j] == '@') { d[i][j + 1] = 0; } } } d[sx][sy] = 0; queue<pair<int, int>> que; que.emplace(sx, sy); vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; vector<vector<bool>> was(h + 2, vector<bool>(w + 2)); was[sx][sy] = true; while (!que.empty()) { int x, y; tie(x, y) = que.front(); que.pop(); for (int kk = 0; kk < 4; kk++) { int i = x, j = y; for (int l = 0; l < k; l++) { i += dx[kk], j += dy[kk]; if (d[i][j] <= d[x][y]) { break; } if (was[i][j]) { continue; } d[i][j] = d[x][y] + 1; if (i == tx && j == ty) { cout << d[i][j] << '\n'; return 0; } que.emplace(i, j); } } } cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, k; cin >> h >> w >> k; int sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty; vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9)); for (int i = 0; i < w + 2; i++) { d[0][i] = d[h + 1][i] = 0; } for (int i = 1; i <= h; i++) { string s; cin >> s; d[i][0] = d[i][w + 1] = 0; for (int j = 0; j < w; j++) { if (s[j] == '@') { d[i][j + 1] = 0; } } } d[sx][sy] = 0; queue<pair<int, int>> que; que.emplace(sx, sy); vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1}; vector<vector<bool>> was(h + 2, vector<bool>(w + 2)); was[sx][sy] = true; while (!que.empty()) { int x, y; tie(x, y) = que.front(); que.pop(); for (int kk = 0; kk < 4; kk++) { int i = x, j = y; for (int l = 0; l < k; l++) { i += dx[kk], j += dy[kk]; if (d[i][j] <= d[x][y]) { break; } if (was[i][j]) { continue; } was[i][j] = true; d[i][j] = d[x][y] + 1; if (i == tx && j == ty) { cout << d[i][j] << '\n'; return 0; } que.emplace(i, j); } } } cout << -1 << '\n'; return 0; }
insert
50
50
50
51
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define all(x) (x).begin(), (x).end() #define V vector typedef V<int> vi; typedef V<vi> vvi; typedef long long ll; typedef pair<int, int> P; typedef tuple<int, int, int> T; constexpr int INF = INT_MAX >> 1; constexpr ll LINF = 5000000000000000LL; constexpr int MOD = 1000000007; int main() { int h, w, k, sy, sx, gy, gx; cin >> h >> w >> k >> sy >> sx >> gy >> gx; sy--, sx--, gy--, gx--; V<string> s(h); rep(i, h) cin >> s[i]; vvi dist(h, vi(w, INF)); dist[sy][sx] = 0; queue<P> q; q.push(P(sy, sx)); while (!q.empty()) { P p = q.front(); q.pop(); int y = p.first, x = p.second; vi dy = {0, 0, 1, -1}; vi dx = {1, -1, 0, 0}; rep(i, 4) rep1(j, k) { int ey, ex; ey = y + dy[i] * j; ex = x + dx[i] * j; if (ey < 0 || h <= ey) break; if (ex < 0 || w <= ex) break; if (s[ey][ex] == '@') break; if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1) continue; dist[ey][ex] = dist[y][x] + 1; q.push(P(ey, ex)); } } if (dist[gy][gx] == INF) dist[gy][gx] = -1; cout << dist[gy][gx] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define all(x) (x).begin(), (x).end() #define V vector typedef V<int> vi; typedef V<vi> vvi; typedef long long ll; typedef pair<int, int> P; typedef tuple<int, int, int> T; constexpr int INF = INT_MAX >> 1; constexpr ll LINF = 5000000000000000LL; constexpr int MOD = 1000000007; int main() { int h, w, k, sy, sx, gy, gx; cin >> h >> w >> k >> sy >> sx >> gy >> gx; sy--, sx--, gy--, gx--; V<string> s(h); rep(i, h) cin >> s[i]; vvi dist(h, vi(w, INF)); dist[sy][sx] = 0; queue<P> q; q.push(P(sy, sx)); while (!q.empty()) { P p = q.front(); q.pop(); int y = p.first, x = p.second; vi dy = {0, 0, 1, -1}; vi dx = {1, -1, 0, 0}; rep(i, 4) rep1(j, k) { int ey, ex; ey = y + dy[i] * j; ex = x + dx[i] * j; if (ey < 0 || h <= ey) break; if (ex < 0 || w <= ex) break; if (s[ey][ex] == '@') break; if (dist[ey][ex] != INF && dist[ey][ex] < dist[y][x] + 1) break; if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1) continue; dist[ey][ex] = dist[y][x] + 1; q.push(P(ey, ex)); } } if (dist[gy][gx] == INF) dist[gy][gx] = -1; cout << dist[gy][gx] << endl; }
insert
44
44
44
46
TLE
p02644
C++
Time Limit Exceeded
/**Bismillahir Rahmanir Raheem Author:Refatul Fahad**/ #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ff first #define ss second #define pb push_back #define vi vector<int> #define pii pair<int, int> #define pil pair<int, ll> const int N = 1e6 + 5; const ll M = (ll)1e8 + 7; const double pi = 2 * acos(0.0); const double esp = 1e-9; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; queue<pii> q; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /// freopen("input.txt","r",stdin); /// freopen("output.txt","w",stdout); int test = 1; /// cin>>test; for (int cs = 1; cs <= test; ++cs) { int h, w, k, x1, y1, x2, y2; cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; string s[h + 5]; bool visit[h + 5][w + 5]; for (int i = 0; i < h; ++i) { cin >> s[i]; } vector<vi> dis(h + 5, vi(w + 5, M)); memset(visit, 0, sizeof(visit)); dis[x1][y1] = 0; q.push({x1, y1}); while (!q.empty()) { pii p = q.front(); q.pop(); if (!visit[p.first][p.second]) { visit[p.first][p.ss] = true; for (int i = 0; i < 4; ++i) { for (int j = 1; j <= k; ++j) { int x = p.first + (j * dx[i]), y = p.second + (j * dy[i]); if (x < 0 || x >= h || y < 0 || y >= w) break; if (s[x][y] == '@') break; if (dis[x][y] > dis[p.first][p.second] + 1) { dis[x][y] = dis[p.first][p.second] + 1; } q.push({x, y}); } } } } if (dis[x2][y2] == M) { cout << -1 << endl; } else { cout << dis[x2][y2] << endl; } } return 0; }
/**Bismillahir Rahmanir Raheem Author:Refatul Fahad**/ #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ff first #define ss second #define pb push_back #define vi vector<int> #define pii pair<int, int> #define pil pair<int, ll> const int N = 1e6 + 5; const ll M = (ll)1e8 + 7; const double pi = 2 * acos(0.0); const double esp = 1e-9; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; queue<pii> q; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); /// freopen("input.txt","r",stdin); /// freopen("output.txt","w",stdout); int test = 1; /// cin>>test; for (int cs = 1; cs <= test; ++cs) { int h, w, k, x1, y1, x2, y2; cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; string s[h + 5]; bool visit[h + 5][w + 5]; for (int i = 0; i < h; ++i) { cin >> s[i]; } vector<vi> dis(h + 5, vi(w + 5, M)); memset(visit, 0, sizeof(visit)); dis[x1][y1] = 0; q.push({x1, y1}); while (!q.empty()) { pii p = q.front(); q.pop(); if (!visit[p.first][p.second]) { visit[p.first][p.ss] = true; for (int i = 0; i < 4; ++i) { for (int j = 1; j <= k; ++j) { int x = p.first + (j * dx[i]), y = p.second + (j * dy[i]); if (x < 0 || x >= h || y < 0 || y >= w) break; if (s[x][y] == '@' || dis[x][y] <= dis[p.ff][p.ss]) break; if (dis[x][y] > dis[p.first][p.second] + 1) { dis[x][y] = dis[p.first][p.second] + 1; } q.push({x, y}); } } } } if (dis[x2][y2] == M) { cout << -1 << endl; } else { cout << dis[x2][y2] << endl; } } return 0; }
replace
59
60
59
60
TLE
p02644
C++
Time Limit Exceeded
// F. #include <algorithm> #include <iostream> #include <queue> #include <sstream> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> II; typedef pair<LL, II> III; int main(int argc, char *argv[]) { #ifdef _MSC_VER while (true) #endif { int h = -1, w, k, sx, sy, gx, gy; LL ans = -1; cin >> h >> w >> k >> sy >> sx >> gy >> gx; --sx, --sy, --gx, --gy; if (h < 0) return 0; vector<string> b(h); for (int i = 0; i < h; ++i) { cin >> b[i]; } priority_queue<III, vector<III>, greater<>> q; vector<int> dist(h * w, 1 << 30); q.emplace(III(0, II(sy, sx))); dist[sy * w + sx] = 0; while (!q.empty()) { III top = q.top(); q.pop(); int y = top.second.first, x = top.second.second; if (x == gx && y == gy) { ans = top.first; break; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; for (int dir = 0; dir < 4; ++dir) { for (int d = 1; d <= k; ++d) { int nx = x + dx[dir] * d, ny = y + dy[dir] * d; if (nx < 0 || nx >= w || ny < 0 || ny >= h) break; if (b[ny][nx] != '.') break; int cost = top.first + 1; if (cost > dist[ny * w + nx]) break; dist[ny * w + nx] = cost; q.emplace(III(cost, II(ny, nx))); } } } cout << ans << endl; } return 0; }
// F. #include <algorithm> #include <iostream> #include <queue> #include <sstream> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> II; typedef pair<LL, II> III; int main(int argc, char *argv[]) { #ifdef _MSC_VER while (true) #endif { int h = -1, w, k, sx, sy, gx, gy; LL ans = -1; cin >> h >> w >> k >> sy >> sx >> gy >> gx; --sx, --sy, --gx, --gy; if (h < 0) return 0; vector<string> b(h); for (int i = 0; i < h; ++i) { cin >> b[i]; } priority_queue<III, vector<III>, greater<>> q; vector<int> dist(h * w, 1 << 30); q.emplace(III(0, II(sy, sx))); dist[sy * w + sx] = 0; while (!q.empty()) { III top = q.top(); q.pop(); int y = top.second.first, x = top.second.second; if (x == gx && y == gy) { ans = top.first; break; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; for (int dir = 0; dir < 4; ++dir) { for (int d = 1; d <= k; ++d) { int nx = x + dx[dir] * d, ny = y + dy[dir] * d; if (nx < 0 || nx >= w || ny < 0 || ny >= h) break; if (b[ny][nx] != '.') break; int cost = top.first + 1; if (cost > dist[ny * w + nx]) break; if (cost < dist[ny * w + nx]) { dist[ny * w + nx] = cost; q.emplace(III(cost, II(ny, nx))); } } } } cout << ans << endl; } return 0; }
replace
52
54
52
56
TLE
p02644
C++
Time Limit Exceeded
// #pragma GCC optimize(2) // #pragma G++ optimize(2) // #pragma comment(linker,"/STACK:102400000,102400000") #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned int ui; typedef unsigned long long ull; typedef float fl; typedef long double ld; typedef pair<int, int> pii; #if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \ (WINDOWS) #define lld "%I64d" #define llu "%I64u" #else #define lld "%lld" #define llu "%llu" #endif #define ui(n) ((unsigned int)(n)) #define LL(n) ((long long)(n)) #define ull(n) ((unsigned long long)(n)) #define fl(n) ((float)(n)) #define ld(n) ((long double)(n)) #define char(n) ((char)(n)) #define Bool(n) ((bool)(n)) #define fixpoint(n) fixed << setprecision(n) const int INF = 1061109567; const int NINF = -1044266559; const LL LINF = 4557430888798830399; const ld eps = 1e-15; #define MOD (1000000007) #define PI (3.1415926535897932384626433832795028841971) #define MP make_pair #define MT make_tuple #define All(a) (a).begin(), (a).end() #define pall(a) (a).rbegin(), (a).rend() #define Log(x, y) log(x) / log(y) #define SZ(a) ((int)(a).size()) #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++) #define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--) #define repv(itr, v) \ for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++) #define repV(i, v) for (auto i : v) #define repE(i, v) for (auto &i : v) #define MS(x, y) memset(x, y, sizeof(x)) #define MC(x) MS(x, 0) #define MINF(x) MS(x, 63) #define MCP(x, y) memcpy(x, y, sizeof(y)) #define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end()) #define filein(x) freopen(x, "r", stdin) #define fileout(x) freopen(x, "w", stdout) #define fileio(x) \ freopen(x ".in", "r", stdin); \ freopen(x ".out", "w", stdout) #define filein2(filename, name) ifstream name(filename, ios::in) #define fileout2(filename, name) ofstream name(filename, ios::out) #define fileio2(filename, name) fstream name(filename, ios::in | ios::out) #define sqr(x) ((x) * (x)) #define Pause system("pause") #define Cls system("cls") #define fs first #define sc second #define SF scanf #define PF printf const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; const int maxn = 1000010; int n, m, K, sx, sy, tx, ty; int main() { cin >> n >> m >> K >> sx >> sy >> tx >> ty; vector<string> str(n + 1); for (int i = 1; i <= n; i++) { cin >> str[i]; str[i] = " " + str[i]; } vector<vector<int>> dist(n + 1, vector<int>(m + 1, INF)); queue<pair<int, int>> q; q.push({sx, sy}); dist[sx][sy] = 0; while (!q.empty()) { int x = q.front().fs, y = q.front().sc; q.pop(); for (int k = 0; k < 4; k++) for (int l = 1; l <= K; l++) { int cx = x + dx[k] * l, cy = y + dy[k] * l; if (cx < 1 || cx > n || cy < 1 || cy > m || str[cx][cy] == '@') break; if (dist[x][y] + 1 < dist[cx][cy]) { dist[cx][cy] = dist[x][y] + 1; q.push({cx, cy}); } } } cout << (dist[tx][ty] == INF ? -1 : dist[tx][ty]); return 0; }
// #pragma GCC optimize(2) // #pragma G++ optimize(2) // #pragma comment(linker,"/STACK:102400000,102400000") #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned int ui; typedef unsigned long long ull; typedef float fl; typedef long double ld; typedef pair<int, int> pii; #if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \ (WINDOWS) #define lld "%I64d" #define llu "%I64u" #else #define lld "%lld" #define llu "%llu" #endif #define ui(n) ((unsigned int)(n)) #define LL(n) ((long long)(n)) #define ull(n) ((unsigned long long)(n)) #define fl(n) ((float)(n)) #define ld(n) ((long double)(n)) #define char(n) ((char)(n)) #define Bool(n) ((bool)(n)) #define fixpoint(n) fixed << setprecision(n) const int INF = 1061109567; const int NINF = -1044266559; const LL LINF = 4557430888798830399; const ld eps = 1e-15; #define MOD (1000000007) #define PI (3.1415926535897932384626433832795028841971) #define MP make_pair #define MT make_tuple #define All(a) (a).begin(), (a).end() #define pall(a) (a).rbegin(), (a).rend() #define Log(x, y) log(x) / log(y) #define SZ(a) ((int)(a).size()) #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++) #define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--) #define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--) #define repv(itr, v) \ for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++) #define repV(i, v) for (auto i : v) #define repE(i, v) for (auto &i : v) #define MS(x, y) memset(x, y, sizeof(x)) #define MC(x) MS(x, 0) #define MINF(x) MS(x, 63) #define MCP(x, y) memcpy(x, y, sizeof(y)) #define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end()) #define filein(x) freopen(x, "r", stdin) #define fileout(x) freopen(x, "w", stdout) #define fileio(x) \ freopen(x ".in", "r", stdin); \ freopen(x ".out", "w", stdout) #define filein2(filename, name) ifstream name(filename, ios::in) #define fileout2(filename, name) ofstream name(filename, ios::out) #define fileio2(filename, name) fstream name(filename, ios::in | ios::out) #define sqr(x) ((x) * (x)) #define Pause system("pause") #define Cls system("cls") #define fs first #define sc second #define SF scanf #define PF printf const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; const int maxn = 1000010; int n, m, K, sx, sy, tx, ty; int main() { cin >> n >> m >> K >> sx >> sy >> tx >> ty; vector<string> str(n + 1); for (int i = 1; i <= n; i++) { cin >> str[i]; str[i] = " " + str[i]; } vector<vector<int>> dist(n + 1, vector<int>(m + 1, INF)); queue<pair<int, int>> q; q.push({sx, sy}); dist[sx][sy] = 0; while (!q.empty()) { int x = q.front().fs, y = q.front().sc; q.pop(); for (int k = 0; k < 4; k++) for (int l = 1; l <= K; l++) { int cx = x + dx[k] * l, cy = y + dy[k] * l; if (cx < 1 || cx > n || cy < 1 || cy > m || str[cx][cy] == '@' || dist[x][y] + 1 > dist[cx][cy]) break; if (dist[x][y] + 1 < dist[cx][cy]) { dist[cx][cy] = dist[x][y] + 1; q.push({cx, cy}); } } } cout << (dist[tx][ty] == INF ? -1 : dist[tx][ty]); return 0; }
replace
101
102
101
103
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define pb push_back #define endl '\n' #define snd second #define fst first #define fastio cin.tie(NULL), cout.sync_with_stdio(true) typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef pair<ll, int> ii; typedef pair<ii, int> iii; const int mod = 1e9 + 7; const int N = 100005; int n, m, k; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; bool valid(int x, int y) { return x >= 1 and x <= n and y >= 1 and y <= m; } void solve() { scanf("%d%d%d", &n, &m, &k); queue<ii> q; char v[n + 1][m + 1]; int see[n + 1][m + 1], x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); for (int i = 1; i <= n; i++) { getchar(); for (int j = 1; j <= m; j++) { scanf("%c", &v[i][j]); see[i][j] = mod; } } q.push({x1, y1}); see[x1][y1] = 0; while (!q.empty()) { int i = q.front().fst, j = q.front().snd; q.pop(); for (int t = 0; t < 4; t++) { for (int l = 1; l <= k; l++) { int ni = i + dx[t] * l, nj = j + dy[t] * l; if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1) break; see[ni][nj] = see[i][j] + 1; q.push({ni, nj}); } } } if (see[x2][y2] == mod) printf("-1\n"); else printf("%d\n", see[x2][y2]); } int main() { // fastio; int t = 1; // cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define pb push_back #define endl '\n' #define snd second #define fst first #define fastio cin.tie(NULL), cout.sync_with_stdio(true) typedef long long int ll; typedef unsigned long long int ull; typedef vector<int> vi; typedef pair<ll, int> ii; typedef pair<ii, int> iii; const int mod = 1e9 + 7; const int N = 100005; int n, m, k; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; bool valid(int x, int y) { return x >= 1 and x <= n and y >= 1 and y <= m; } void solve() { scanf("%d%d%d", &n, &m, &k); queue<ii> q; char v[n + 1][m + 1]; int see[n + 1][m + 1], x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); for (int i = 1; i <= n; i++) { getchar(); for (int j = 1; j <= m; j++) { scanf("%c", &v[i][j]); see[i][j] = mod; } } q.push({x1, y1}); see[x1][y1] = 0; while (!q.empty()) { int i = q.front().fst, j = q.front().snd; q.pop(); for (int t = 0; t < 4; t++) { for (int l = 1; l <= k; l++) { int ni = i + dx[t] * l, nj = j + dy[t] * l; if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1) break; if (see[ni][nj] > see[i][j] + 1) { see[ni][nj] = see[i][j] + 1; q.push({ni, nj}); } } } } if (see[x2][y2] == mod) printf("-1\n"); else printf("%d\n", see[x2][y2]); } int main() { // fastio; int t = 1; // cin >> t; while (t--) solve(); return 0; }
replace
50
52
50
54
TLE
p02644
C++
Time Limit Exceeded
#include <fstream> #include <iostream> #include <set> #include <vector> // BE CAREFUL WITH HASH TABLE & UNORDERED MAP // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; using namespace std; #define MOD 998244353 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, b) FOR(i, 0, b) #define RFO(i, a, b) for (int i = (b - 1); i >= (a); i--) #define RF0(i, b) RFO(i, 0, b) #define ll long long #define pii pair<int, int> #define ppp pair<pair<pii, pii>, int> #define add(a, b) ((int)(((ll)(a) + (b)) % MOD)) #define mul(a, b) ((int)(((ll)(a) * (b)) % MOD)) #define PB emplace_back #define MP make_pair #define F first #define S second // ifstream cin ("cinput.in"); // ofstream cout ("coutput.out"); int H, W, K, x1, y1, x2, y2; int moves[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; char a; vector<vector<int>> grid; set<ppp> queue; // ((priority, cost), (r,c)) bool valid(int r, int c) { return (0 <= r && r < H && 0 <= c && c < W); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> H >> W >> K; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; F0R(i, H) { grid.PB(vector<int>()); F0R(j, W) grid[i].PB(2000000); } F0R(i, H) F0R(j, W) { cin >> a; if (a == '@') grid[i][j] = -1; } // Dijkstra's grid[x1][y1] = 0; queue.emplace(MP(MP(MP(4, 0), MP(x1, y1)), 0)); while (!queue.empty()) { ppp cur = *queue.begin(); queue.erase(queue.begin()); if (cur.F.F.F != 4 && cur.S >= 0) queue.emplace(MP(MP(MP(4, cur.F.F.S), MP(cur.F.S.F, cur.F.S.S)), 0)); cur.S = abs(cur.S); F0R(i, 4) { int nr = cur.F.S.F + moves[i][0], nc = cur.F.S.S + moves[i][1], nm = cur.F.F.S + (cur.F.F.F == 4), ns = cur.S + 1; if ((cur.F.F.F != 4 && i != cur.F.F.F) || !valid(nr, nc) || grid[nr][nc] == -1 || cur.S == K) continue; if (grid[nr][nc] > nm) grid[nr][nc] = nm; else ns *= -1; queue.emplace(MP(MP(MP(i, nm), MP(nr, nc)), ns)); } } if (grid[x2][y2] == 2000000) cout << "-1\n"; else cout << grid[x2][y2] << '\n'; }
#include <fstream> #include <iostream> #include <set> #include <vector> // BE CAREFUL WITH HASH TABLE & UNORDERED MAP // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; using namespace std; #define MOD 998244353 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, b) FOR(i, 0, b) #define RFO(i, a, b) for (int i = (b - 1); i >= (a); i--) #define RF0(i, b) RFO(i, 0, b) #define ll long long #define pii pair<int, int> #define ppp pair<pair<pii, pii>, int> #define add(a, b) ((int)(((ll)(a) + (b)) % MOD)) #define mul(a, b) ((int)(((ll)(a) * (b)) % MOD)) #define PB emplace_back #define MP make_pair #define F first #define S second // ifstream cin ("cinput.in"); // ofstream cout ("coutput.out"); int H, W, K, x1, y1, x2, y2; int moves[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; char a; vector<vector<int>> grid; set<ppp> queue; // ((priority, cost), (r,c)) bool valid(int r, int c) { return (0 <= r && r < H && 0 <= c && c < W); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> H >> W >> K; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; F0R(i, H) { grid.PB(vector<int>()); F0R(j, W) grid[i].PB(2000000); } F0R(i, H) F0R(j, W) { cin >> a; if (a == '@') grid[i][j] = -1; } // Dijkstra's grid[x1][y1] = 0; queue.emplace(MP(MP(MP(4, 0), MP(x1, y1)), 0)); while (!queue.empty()) { ppp cur = *queue.begin(); queue.erase(queue.begin()); if (cur.F.F.F != 4 && cur.S >= 0) queue.emplace(MP(MP(MP(4, cur.F.F.S), MP(cur.F.S.F, cur.F.S.S)), 0)); cur.S = abs(cur.S); F0R(i, 4) { int nr = cur.F.S.F + moves[i][0], nc = cur.F.S.S + moves[i][1], nm = cur.F.F.S + (cur.F.F.F == 4), ns = cur.S + 1; if ((cur.F.F.F != 4 && i != cur.F.F.F) || !valid(nr, nc) || grid[nr][nc] == -1 || cur.S == K || grid[nr][nc] <= nm - 1) continue; if (grid[nr][nc] > nm) grid[nr][nc] = nm; else ns *= -1; queue.emplace(MP(MP(MP(i, nm), MP(nr, nc)), ns)); } } if (grid[x2][y2] == 2000000) cout << "-1\n"; else cout << grid[x2][y2] << '\n'; }
replace
68
69
68
69
TLE
p02644
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; template <typename T> string to_string(const vector<T> &vc, int w) { if (vc.empty()) return ""; if (w + 1 == vc.size()) return to_string(vc[w]); return to_string(vc[w]) + "," + to_string(vc, w + 1); } template <typename T> string to_string(const vector<T> &vc) { return "{" + to_string(vc, 0) + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef DEBUG #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif class DebugStream { } LOG; template <typename T> DebugStream &operator<<(DebugStream &s, const T &) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29; int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int n, m, k; cin >> n >> m >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> mat(n); vector<vector<int>> shr(n, vector<int>(m, -1)); for (auto &el : mat) cin >> el; auto ins = [&](int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }; queue<ii> q; q.push(ii(x1, y1)); shr[x1][y1] = 0; while (q.size()) { auto cr = q.front(); q.pop(); for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int x = cr.fi + j * dr[i][0], y = cr.se + j * dr[i][1]; if (!ins(x, y) || mat[x][y] == '@') break; if (shr[x][y] == -1) { shr[x][y] = shr[cr.fi][cr.se] + 1; q.push(ii(x, y)); } else if (shr[x][y] < shr[cr.fi][cr.se]) break; } } } int ky = shr[x2][y2]; cout << ky << '\n'; }
#include "bits/stdc++.h" #define FI first.first #define SE first.second #define TH second #define fi first #define se second #define th second using namespace std; template <typename T> string to_string(const vector<T> &vc, int w) { if (vc.empty()) return ""; if (w + 1 == vc.size()) return to_string(vc[w]); return to_string(vc[w]) + "," + to_string(vc, w + 1); } template <typename T> string to_string(const vector<T> &vc) { return "{" + to_string(vc, 0) + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef DEBUG #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif class DebugStream { } LOG; template <typename T> DebugStream &operator<<(DebugStream &s, const T &) { return s; } #ifdef DEBUG #define LOG clog #endif typedef long long ll; typedef pair<ll, ll> ii; typedef long double ld; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29; int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif int n, m, k; cin >> n >> m >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; vector<string> mat(n); vector<vector<int>> shr(n, vector<int>(m, -1)); for (auto &el : mat) cin >> el; auto ins = [&](int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }; queue<ii> q; q.push(ii(x1, y1)); shr[x1][y1] = 0; while (q.size()) { auto cr = q.front(); q.pop(); for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { int x = cr.fi + j * dr[i][0], y = cr.se + j * dr[i][1]; if (!ins(x, y) || mat[x][y] == '@') break; if (shr[x][y] == -1) { shr[x][y] = shr[cr.fi][cr.se] + 1; q.push(ii(x, y)); } else if (shr[x][y] <= shr[cr.fi][cr.se]) break; } } } int ky = shr[x2][y2]; cout << ky << '\n'; }
replace
87
88
87
88
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #include <atcoder/all> #define ll long long int #define ld long double #define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl; #define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repi(i, n) for (ll i = 1; i < (ll)(n); i++) #define all(x) (x).begin(), (x).end() #define put(x) std::cout << x << endl; #define println(x) std::cout << x << endl; template <typename T> bool mins(T &x, const T &y) { if (x > y) { x = y; return true; } else return false; } template <typename T> bool maxs(T &x, const T &y) { if (x < y) { x = y; return true; } else return false; } using namespace std; // using namespace atcoder; void Main() { ll H, W, K; cin >> H >> W >> K; ll sx, sy; cin >> sy >> sx; sx--; sy--; ll gx, gy; cin >> gy >> gx; gx--; gy--; vector<string> S(H); rep(i, H) cin >> S[i]; // BFS ll INF = 1e18; vector<vector<ll>> d(H, vector<ll>(W, INF)); typedef pair<ll, ll> P; queue<P> que; que.push(P(sx, sy)); d[sy][sx] = 0; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; // BFS: グリッドの最短路 while (que.empty() == false) { P q = que.front(); ll x = q.first; ll y = q.second; que.pop(); rep(i, 4) { repi(j, K + 1) { ll x2 = dx[i] * j + x; ll y2 = dy[i] * j + y; if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) { break; } if (S[y2][x2] == '@') { break; } if (d[y2][x2] != INF) { // already visited continue; } d[y2][x2] = d[y][x] + 1; que.push(P(x2, y2)); } } } ll res = d[gy][gx]; if (res == INF) { put(-1); } else { put(res); } } signed main() { Main(); return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define ll long long int #define ld long double #define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl; #define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repi(i, n) for (ll i = 1; i < (ll)(n); i++) #define all(x) (x).begin(), (x).end() #define put(x) std::cout << x << endl; #define println(x) std::cout << x << endl; template <typename T> bool mins(T &x, const T &y) { if (x > y) { x = y; return true; } else return false; } template <typename T> bool maxs(T &x, const T &y) { if (x < y) { x = y; return true; } else return false; } using namespace std; // using namespace atcoder; void Main() { ll H, W, K; cin >> H >> W >> K; ll sx, sy; cin >> sy >> sx; sx--; sy--; ll gx, gy; cin >> gy >> gx; gx--; gy--; vector<string> S(H); rep(i, H) cin >> S[i]; // BFS ll INF = 1e18; vector<vector<ll>> d(H, vector<ll>(W, INF)); typedef pair<ll, ll> P; queue<P> que; que.push(P(sx, sy)); d[sy][sx] = 0; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; // BFS: グリッドの最短路 while (que.empty() == false) { P q = que.front(); ll x = q.first; ll y = q.second; que.pop(); rep(i, 4) { repi(j, K + 1) { ll x2 = dx[i] * j + x; ll y2 = dy[i] * j + y; if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) { break; } if (S[y2][x2] == '@') { break; } if (d[y2][x2] <= d[y][x]) { break; } if (d[y2][x2] != INF) { // already visited continue; } d[y2][x2] = d[y][x] + 1; que.push(P(x2, y2)); } } } ll res = d[gy][gx]; if (res == INF) { put(-1); } else { put(res); } } signed main() { Main(); return 0; }
insert
73
73
73
76
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const int MaxRC = 1e3; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; struct DATA { int x, y; DATA(int x = 0, int y = 0) : x(x), y(y) {} } st, ed, now, nxt; int main() { int R, C, K; while (scanf("%d %d %d\n", &R, &C, &K) != EOF) { scanf("%d %d %d %d\n", &st.x, &st.y, &ed.x, &ed.y); st.x--, st.y--, ed.x--, ed.y--; vector<vector<char>> mat(R, vector<char>(C)); for (int i = 0; i < R; i++, getchar()) for (int j = 0; j < C; j++) mat[i][j] = getchar(); vector<vector<int>> step(R, vector<int>(C, INF)); queue<DATA> Q; Q.push(DATA(st.x, st.y)); step[st.x][st.y] = 0; while (Q.empty() == 0) { now = Q.front(); Q.pop(); if (now.x == ed.x and now.y == ed.y) break; for (int i = 0; i < 4; i++) for (int k = 1; k <= K; k++) { nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]); if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or mat[nxt.x][nxt.y] == '@' or step[nxt.x][nxt.y] <= step[now.x][now.y]) break; step[nxt.x][nxt.y] = step[now.x][now.y] + 1; Q.push(nxt); } } printf("%d\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]); } }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const int MaxRC = 1e3; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; struct DATA { int x, y; DATA(int x = 0, int y = 0) : x(x), y(y) {} } st, ed, now, nxt; int main() { int R, C, K; while (scanf("%d %d %d\n", &R, &C, &K) != EOF) { scanf("%d %d %d %d\n", &st.x, &st.y, &ed.x, &ed.y); st.x--, st.y--, ed.x--, ed.y--; vector<vector<char>> mat(R, vector<char>(C)); for (int i = 0; i < R; i++, getchar()) for (int j = 0; j < C; j++) mat[i][j] = getchar(); vector<vector<int>> step(R, vector<int>(C, INF)); queue<DATA> Q; Q.push(DATA(st.x, st.y)); step[st.x][st.y] = 0; while (Q.empty() == 0) { now = Q.front(); Q.pop(); if (now.x == ed.x and now.y == ed.y) break; for (int i = 0; i < 4; i++) for (int k = 1; k <= K; k++) { nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]); if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or mat[nxt.x][nxt.y] == '@' or step[nxt.x][nxt.y] <= step[now.x][now.y]) break; else if (step[nxt.x][nxt.y] > step[now.x][now.y] + 1) { step[nxt.x][nxt.y] = step[now.x][now.y] + 1; Q.push(nxt); } } } printf("%d\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]); } }
replace
39
41
39
43
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; /* typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vpii; */ typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef pair<long long, long long> pll; typedef vector<pll> vpll; typedef long double ld; typedef vector<ld> vld; typedef vector<bool> vb; #define rep(i, n) for (ll i = 0; i < (n); i++) #define reps(i, n) for (ll i = 1; i <= (n); i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, n) for (ll i = (n); i >= 1; i--) #define all(v) (v).begin(), (v).end() template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } constexpr int INF = 1 << 30; constexpr ll INFL = 1LL << 60; constexpr ll MOD = 1000000007; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); void solve() { ll h, w, k; cin >> h >> w >> k; ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; vector<string> c(h); rep(i, h) cin >> c[i]; vvll d(h, vll(w, INFL)); d[x1][y1] = 0; queue<pll> q; q.push(make_pair(x1, y1)); ll dx[] = {1, -1, 0, 0}; ll dy[] = {0, 0, 1, -1}; while (!q.empty()) { pll p = q.front(); q.pop(); for (ll i = 0; i < 4; i++) { for (ll j = 1; j <= k; j++) { ll x = p.first + j * dx[i]; if (x < 0 || x >= h) break; ll y = p.second + j * dy[i]; if (y < 0 || y >= w) break; if (c[x][y] == '@') break; if (d[x][y] <= d[p.first][p.second]) break; d[x][y] = d[p.first][p.second] + 1; q.push(make_pair(x, y)); } } } ll ans = d[x2][y2]; if (ans == INFL) ans = -1; cout << ans << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; /* typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vpii; */ typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef pair<long long, long long> pll; typedef vector<pll> vpll; typedef long double ld; typedef vector<ld> vld; typedef vector<bool> vb; #define rep(i, n) for (ll i = 0; i < (n); i++) #define reps(i, n) for (ll i = 1; i <= (n); i++) #define rrep(i, n) for (ll i = (n)-1; i >= 0; i--) #define rreps(i, n) for (ll i = (n); i >= 1; i--) #define all(v) (v).begin(), (v).end() template <class T> void chmin(T &a, T b) { a = min(a, b); } template <class T> void chmax(T &a, T b) { a = max(a, b); } constexpr int INF = 1 << 30; constexpr ll INFL = 1LL << 60; constexpr ll MOD = 1000000007; constexpr ld EPS = 1e-12; ld PI = acos(-1.0); void solve() { ll h, w, k; cin >> h >> w >> k; ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; vector<string> c(h); rep(i, h) cin >> c[i]; vvll d(h, vll(w, INFL)); d[x1][y1] = 0; queue<pll> q; q.push(make_pair(x1, y1)); ll dx[] = {1, -1, 0, 0}; ll dy[] = {0, 0, 1, -1}; while (!q.empty()) { pll p = q.front(); q.pop(); for (ll i = 0; i < 4; i++) { for (ll j = 1; j <= k; j++) { ll x = p.first + j * dx[i]; if (x < 0 || x >= h) break; ll y = p.second + j * dy[i]; if (y < 0 || y >= w) break; if (c[x][y] == '@') break; if (d[x][y] <= d[p.first][p.second]) break; else if (d[x][y] <= d[p.first][p.second] + 1) continue; d[x][y] = d[p.first][p.second] + 1; q.push(make_pair(x, y)); } } } ll ans = d[x2][y2]; if (ans == INFL) ans = -1; cout << ans << endl; return; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); solve(); }
insert
85
85
85
87
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long using namespace std; const int INF = 1e9; int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w, kk, x1, y1, x2, y2; cin >> h >> w >> kk >> x1 >> y1 >> x2 >> y2; vector<vector<int>> dp(h + 1, vector<int>(w + 1, INF)); vector<string> a(h); for (int i = 0; i < h; i++) cin >> a[i]; queue<pair<int, int>> q; q.push({x1, y1}); dp[x1][y1] = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int k = 0; k < 4; k++) { for (int i = 1; i <= kk; i++) { int xx = x + i * d[k][0]; int yy = y + i * d[k][1]; if (xx <= 0 || xx > h || yy <= 0 || yy > w) break; if (a[xx - 1][yy - 1] == '@') break; // if (dp[xx][yy] == dp[x][y]) break; if (dp[xx][yy] == INF) { dp[xx][yy] = dp[x][y] + 1; q.push({xx, yy}); } } } } if (dp[x2][y2] == INF) cout << "-1\n"; else { cout << dp[x2][y2] << '\n'; } return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int INF = 1e9; int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int h, w, kk, x1, y1, x2, y2; cin >> h >> w >> kk >> x1 >> y1 >> x2 >> y2; vector<vector<int>> dp(h + 1, vector<int>(w + 1, INF)); vector<string> a(h); for (int i = 0; i < h; i++) cin >> a[i]; queue<pair<int, int>> q; q.push({x1, y1}); dp[x1][y1] = 0; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int k = 0; k < 4; k++) { for (int i = 1; i <= kk; i++) { int xx = x + i * d[k][0]; int yy = y + i * d[k][1]; if (xx <= 0 || xx > h || yy <= 0 || yy > w) break; if (a[xx - 1][yy - 1] == '@') break; if (dp[xx][yy] == dp[x][y]) break; if (dp[xx][yy] == INF) { dp[xx][yy] = dp[x][y] + 1; q.push({xx, yy}); } } } } if (dp[x2][y2] == INF) cout << "-1\n"; else { cout << dp[x2][y2] << '\n'; } return 0; }
replace
36
37
36
38
TLE
p02644
C++
Runtime Error
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <assert.h> #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <climits> #include <complex> // complex #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <deque> // deque #include <iomanip> // setprecision #include <iostream> // cout, endl, cin #include <map> // map #include <math.h> #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <utility> // pair, make_pair #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; constexpr ll INF = 1e18; constexpr int inf = 1e9; constexpr ll mod = 1000000007; constexpr ll mod2 = 998244353; const double PI = 3.1415926535897932384626433832795028841971; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; 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; } // --------------------------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W, K; cin >> H >> W >> K; int sx, sy, gx, gy; cin >> sy >> sx >> gy >> gx; sx--; sy--; gx--; gy--; vector<string> G(H); for (int i = 0; i < H; i++) cin >> G[i]; queue<P> que; que.emplace(sy, sx); vector<vector<int>> dist(H, vector<int>(W, inf)); dist[sy][sx] = 0; while (que.size()) { int y, x; tie(y, x) = que.front(); que.pop(); for (int d = 0; d < 4; d++) { for (int k = 1; k <= K; k++) { ll ny = y + dy[d] * k; ll nx = x + dx[d] * k; if (ny < 0 || ny >= H || nx < 0 || nx >= W) break; if (G[ny][nx] == '@') break; if (dist[ny][nx] <= dist[y][x]) break; if (dist[x][y] == inf) { dist[ny][nx] = dist[y][x] + 1; que.emplace(ny, nx); } } } } if (dist[gy][gx] == inf) { cout << -1 << "\n"; } else { cout << dist[gy][gx] << "\n"; } return 0; }
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <assert.h> #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <climits> #include <complex> // complex #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <deque> // deque #include <iomanip> // setprecision #include <iostream> // cout, endl, cin #include <map> // map #include <math.h> #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <utility> // pair, make_pair #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; constexpr ll INF = 1e18; constexpr int inf = 1e9; constexpr ll mod = 1000000007; constexpr ll mod2 = 998244353; const double PI = 3.1415926535897932384626433832795028841971; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; 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; } // --------------------------------------------------------------------------- int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H, W, K; cin >> H >> W >> K; int sx, sy, gx, gy; cin >> sy >> sx >> gy >> gx; sx--; sy--; gx--; gy--; vector<string> G(H); for (int i = 0; i < H; i++) cin >> G[i]; queue<P> que; que.emplace(sy, sx); vector<vector<int>> dist(H, vector<int>(W, inf)); dist[sy][sx] = 0; while (que.size()) { int y, x; tie(y, x) = que.front(); que.pop(); for (int d = 0; d < 4; d++) { for (int k = 1; k <= K; k++) { ll ny = y + dy[d] * k; ll nx = x + dx[d] * k; if (ny < 0 || ny >= H || nx < 0 || nx >= W) break; if (G[ny][nx] == '@') break; if (dist[ny][nx] <= dist[y][x]) break; if (dist[ny][nx] == inf) { dist[ny][nx] = dist[y][x] + 1; que.emplace(ny, nx); } } } } if (dist[gy][gx] == inf) { cout << -1 << "\n"; } else { cout << dist[gy][gx] << "\n"; } return 0; }
replace
81
82
81
82
0
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> #define DB cerr << "D" << endl #define pb push_back using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const ll LINF = 1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6; template <typename T, typename U, typename O> void caut(T a, U b, O c) { cout << "(" << a << "," << b << "," << c << ") "; } template <typename T, typename U> void caut(T a, U b) { cout << "(" << a << "," << b << ") "; } template <typename T> void caut(T a) { cout << "(" << a << ") "; } using P = pair<int, int>; const ll M = 1e9 + 7, mx = 200000; void add(ll &x, ll y) { x += y; x %= M; } void chmax(ll &x, ll y) { if (x < y) x = y; } const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1}; bool vaild(int x, int y, int hh, int ww) { return 0 <= x && x < hh && 0 <= y && y < ww; } struct tup { ll x, y, c; bool operator<(const tup tt) const { if (c != tt.c) return c < tt.c; else if (x != tt.x) return x < tt.x; else return y < tt.y; } bool operator>(const tup tt) const { if (c != tt.c) return c > tt.c; else if (x != tt.x) return x > tt.x; else return y > tt.y; } }; #include <ctime> #include <random> namespace r { random_device rd; mt19937 mt(rd()); // mt19937 mt(time(NULL)); int nd(int max) { return mt() % max + 1; } int nd2(int min, int max) { return mt() % (max - min + 1) + min; } } // namespace r int main() { int h, w, k; // h=100, w=100, k=100; cin >> h >> w >> k; int fx, fy, lx, ly; cin >> fx >> fy >> lx >> ly; fx--; fy--; lx--; ly--; // fx=0, fy=0, lx=h-1, ly=w-1; string s[h]; for (int i = 0; i < h; i++) cin >> s[i]; /*for(int i=0;i<h;i++) { s[i].assign('a',w); for(int j=0;j<w;j++) { if (i==fx&&j==fy||i==lx&&j==ly) s[i][j]='.';//cout<<"."; else if (r::nd(5)==1) s[i][j]='@'; else s[i][j]='.'; } }*/ ll an[h][w]; fill(an[0], an[h], LINF); queue<tup> que; an[fx][fy] = 0; que.push({fx, fy, 0}); while (!que.empty()) { tup tp = que.front(); que.pop(); int cs = tp.c + 1; for (int i = 0; i < 4; i++) { int nx = tp.x, ny = tp.y; for (int j = 0; j < k; j++) { nx += up[i]; ny += lf[i]; if (vaild(nx, ny, h, w) && s[nx][ny] == '.') { if (cs < an[nx][ny]) { an[nx][ny] = cs; que.push({nx, ny, cs}); } else if (cs == an[nx][ny]) { an[nx][ny] = cs; } } else { break; } } } } /* for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { cout<<an[i][j]<<" "; }cout<<endl; }*/ if (an[lx][ly] == LINF) { cout << -1 << endl; } else { cout << an[lx][ly] << endl; } }
#include <algorithm> #include <functional> #include <iostream> #include <queue> #include <set> #include <string> #include <vector> #define DB cerr << "D" << endl #define pb push_back using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const ll LINF = 1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6; template <typename T, typename U, typename O> void caut(T a, U b, O c) { cout << "(" << a << "," << b << "," << c << ") "; } template <typename T, typename U> void caut(T a, U b) { cout << "(" << a << "," << b << ") "; } template <typename T> void caut(T a) { cout << "(" << a << ") "; } using P = pair<int, int>; const ll M = 1e9 + 7, mx = 200000; void add(ll &x, ll y) { x += y; x %= M; } void chmax(ll &x, ll y) { if (x < y) x = y; } const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1}; bool vaild(int x, int y, int hh, int ww) { return 0 <= x && x < hh && 0 <= y && y < ww; } struct tup { ll x, y, c; bool operator<(const tup tt) const { if (c != tt.c) return c < tt.c; else if (x != tt.x) return x < tt.x; else return y < tt.y; } bool operator>(const tup tt) const { if (c != tt.c) return c > tt.c; else if (x != tt.x) return x > tt.x; else return y > tt.y; } }; #include <ctime> #include <random> namespace r { random_device rd; mt19937 mt(rd()); // mt19937 mt(time(NULL)); int nd(int max) { return mt() % max + 1; } int nd2(int min, int max) { return mt() % (max - min + 1) + min; } } // namespace r int main() { int h, w, k; // h=100, w=100, k=100; cin >> h >> w >> k; int fx, fy, lx, ly; cin >> fx >> fy >> lx >> ly; fx--; fy--; lx--; ly--; // fx=0, fy=0, lx=h-1, ly=w-1; string s[h]; for (int i = 0; i < h; i++) cin >> s[i]; /*for(int i=0;i<h;i++) { s[i].assign('a',w); for(int j=0;j<w;j++) { if (i==fx&&j==fy||i==lx&&j==ly) s[i][j]='.';//cout<<"."; else if (r::nd(5)==1) s[i][j]='@'; else s[i][j]='.'; } }*/ ll an[h][w]; fill(an[0], an[h], LINF); queue<tup> que; an[fx][fy] = 0; que.push({fx, fy, 0}); while (!que.empty()) { tup tp = que.front(); que.pop(); int cs = tp.c + 1; for (int i = 0; i < 4; i++) { int nx = tp.x, ny = tp.y; for (int j = 0; j < k; j++) { nx += up[i]; ny += lf[i]; if (vaild(nx, ny, h, w) && s[nx][ny] == '.') { if (cs < an[nx][ny]) { an[nx][ny] = cs; que.push({nx, ny, cs}); } else if (cs == an[nx][ny]) { an[nx][ny] = cs; } else { break; } } else { break; } } } } /* for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { cout<<an[i][j]<<" "; }cout<<endl; }*/ if (an[lx][ly] == LINF) { cout << -1 << endl; } else { cout << an[lx][ly] << endl; } }
insert
117
117
117
119
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (ll i = (x); i < (y); i++) #define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--) #define all(x) (x).begin(), (x).end() #ifdef LOCAL #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl #define itrdebug(x) \ cerr << #x << " "; \ for (auto &el : (x)) { \ cerr << (el) << " "; \ } \ cerr << endl #define dassert(...) assert(__VA_ARGS__) #else #define debug(x) #define itrdebug(x) #define dassert(...) #endif // #define int long long typedef long long ll; const ll MOD = 1e9 + 7; const long double EPS = 1e-8; template <class NUMBER> struct Point { NUMBER x, y; Point(NUMBER x, NUMBER y) : x(x), y(y) {} Point() = default; Point operator+(const Point &point) const { return Point(x + point.x, y + point.y); } Point operator-(const Point &point) const { return Point(x - point.x, y, -point.y); } Point operator*(const NUMBER d) const { return Point(x * d, y * d); } Point operator/(const NUMBER d) const { return Point(x / d, y / d); } Point &operator+=(const Point &point) { x += point.x; y += point.y; return *this; } Point &operator-=(const Point &point) { x -= point.x; y -= point.y; return *this; } Point &operator*=(const NUMBER d) { x *= d; y *= d; return *this; } Point &operator/=(const NUMBER d) { x /= d; y /= d; return *this; } bool operator<(const Point &point) const { return x < point.x; } }; signed main() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<vector<char>> grids(H + 1, vector<char>(W + 1)); rep(i, 1, H + 1) rep(j, 1, W + 1) cin >> grids[i][j]; vector<vector<int>> distances(H + 1, vector<int>(W + 1, INT_MAX)); priority_queue<pair<int, Point<int>>, vector<pair<int, Point<int>>>, greater<>> que; if (grids[x1][y1] != '@') { distances[x1][y1] = 0; que.push({0, {x1, y1}}); } const auto isValid = [H, W](const Point<int> &point) -> bool { return point.x >= 1 && point.y >= 1 && point.x <= H && point.y <= W; }; while (!que.empty()) { auto distance = que.top().first; auto base = que.top().second; que.pop(); vector<Point<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (const auto &direction : directions) { auto current = base; rep(i, 0, K) { current += direction; if (!isValid(current) || grids[current.x][current.y] == '@') break; if (distances[current.x][current.y] > distance + 1) { distances[current.x][current.y] = distance + 1; que.push({distance + 1, current}); } } } } int answer = distances[x2][y2]; cout << (answer == INT_MAX ? -1 : answer) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (ll i = (x); i < (y); i++) #define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--) #define all(x) (x).begin(), (x).end() #ifdef LOCAL #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl #define itrdebug(x) \ cerr << #x << " "; \ for (auto &el : (x)) { \ cerr << (el) << " "; \ } \ cerr << endl #define dassert(...) assert(__VA_ARGS__) #else #define debug(x) #define itrdebug(x) #define dassert(...) #endif // #define int long long typedef long long ll; const ll MOD = 1e9 + 7; const long double EPS = 1e-8; template <class NUMBER> struct Point { NUMBER x, y; Point(NUMBER x, NUMBER y) : x(x), y(y) {} Point() = default; Point operator+(const Point &point) const { return Point(x + point.x, y + point.y); } Point operator-(const Point &point) const { return Point(x - point.x, y, -point.y); } Point operator*(const NUMBER d) const { return Point(x * d, y * d); } Point operator/(const NUMBER d) const { return Point(x / d, y / d); } Point &operator+=(const Point &point) { x += point.x; y += point.y; return *this; } Point &operator-=(const Point &point) { x -= point.x; y -= point.y; return *this; } Point &operator*=(const NUMBER d) { x *= d; y *= d; return *this; } Point &operator/=(const NUMBER d) { x /= d; y /= d; return *this; } bool operator<(const Point &point) const { return x < point.x; } }; signed main() { int H, W, K; cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<vector<char>> grids(H + 1, vector<char>(W + 1)); rep(i, 1, H + 1) rep(j, 1, W + 1) cin >> grids[i][j]; vector<vector<int>> distances(H + 1, vector<int>(W + 1, INT_MAX)); priority_queue<pair<int, Point<int>>, vector<pair<int, Point<int>>>, greater<>> que; if (grids[x1][y1] != '@') { distances[x1][y1] = 0; que.push({0, {x1, y1}}); } const auto isValid = [H, W](const Point<int> &point) -> bool { return point.x >= 1 && point.y >= 1 && point.x <= H && point.y <= W; }; while (!que.empty()) { auto distance = que.top().first; auto base = que.top().second; que.pop(); vector<Point<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; for (const auto &direction : directions) { auto current = base; rep(i, 0, K) { current += direction; if (!isValid(current) || grids[current.x][current.y] == '@') break; if (distances[current.x][current.y] <= distance) break; if (distances[current.x][current.y] > distance + 1) { distances[current.x][current.y] = distance + 1; que.push({distance + 1, current}); } } } } int answer = distances[x2][y2]; cout << (answer == INT_MAX ? -1 : answer) << endl; return 0; }
insert
93
93
93
95
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define mod 1'000'000'007 typedef long long ll; template <typename T> void deb(initializer_list<T> l) { for (auto &e : l) cout << e << ' '; cout << endl; } int n, m, k; vector<pair<int, int>> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool ok(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; } void solve() { cin >> n >> m >> k; k = min(k, max(n - 1, m - 1)); vector<vector<bool>> a(n, vector<bool>(m)); int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --y1; --x2; --y2; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { char c; cin >> c; a[i][j] = c == '.'; } } vector<vector<int>> dist(n, vector<int>(m, -1)); vector<vector<bool>> vis(n, vector<bool>(m, false)); vis[x1][y1] = true; dist[x1][y1] = 0; queue<pair<int, int>> q; q.push({x1, y1}); while (!q.empty()) { auto [i, j] = q.front(); q.pop(); // deb({i, j, dist[i][j]}); for (auto &[di, dj] : dir) { int x = i; int y = j; for (int o = 1; o <= k; ++o) { x += di; y += dj; if (!ok(x, y)) break; if (!a[x][y]) break; if (vis[x][y]) continue; vis[x][y] = true; dist[x][y] = dist[i][j] + 1; q.push({x, y}); } } } // vector<pair<int, int>> x{{2, 1}, {2, 0}, {1, }} cout << dist[x2][y2] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1'000'000'007 typedef long long ll; template <typename T> void deb(initializer_list<T> l) { for (auto &e : l) cout << e << ' '; cout << endl; } int n, m, k; vector<pair<int, int>> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool ok(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; } void solve() { cin >> n >> m >> k; k = min(k, max(n - 1, m - 1)); vector<vector<bool>> a(n, vector<bool>(m)); int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --y1; --x2; --y2; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { char c; cin >> c; a[i][j] = c == '.'; } } vector<vector<int>> dist(n, vector<int>(m, -1)); vector<vector<bool>> vis(n, vector<bool>(m, false)); vis[x1][y1] = true; dist[x1][y1] = 0; queue<pair<int, int>> q; q.push({x1, y1}); while (!q.empty()) { auto [i, j] = q.front(); q.pop(); // deb({i, j, dist[i][j]}); for (auto &[di, dj] : dir) { int x = i; int y = j; for (int o = 1; o <= k; ++o) { x += di; y += dj; if (!ok(x, y)) break; if (!a[x][y]) break; if (vis[x][y] && dist[x][y] <= dist[i][j]) break; if (vis[x][y]) continue; vis[x][y] = true; dist[x][y] = dist[i][j] + 1; q.push({x, y}); } } } // vector<pair<int, int>> x{{2, 1}, {2, 0}, {1, }} cout << dist[x2][y2] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
insert
58
58
58
60
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; using ll = long long; using P = pair<int, int>; const ll INF = 1e18; const int di[4] = {-1, 0, 1, 0}; const int dj[4] = {0, 1, 0, -1}; int main() { int h, w, k, si, sj, gi, gj; cin >> h >> w >> k >> si >> sj >> gi >> gj; si--; sj--; gi--; gj--; vector<string> g(h); rep(i, h) cin >> g[i]; // 各マスについてスタートから到達までのコスト(かく回数) vector<vector<ll>> cost(h, vector<ll>(w, INF)); cost[si][sj] = 0; // スタートマスはコスト0 // 調べるマスを入れるキュー queue<P> q; // 調べるマスをキューに追加する関数 auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); }; // スタート地点から調べる add_g(si, sj); while (!q.empty()) { // キューが空でない限りループ // キューから調べるマスの座標を取り出す P p = q.front(); int cur_i = p.first; int cur_j = p.second; const ll c = cost[cur_i][cur_j]; q.pop(); // キューに調べるマスを追加 rep(i, 4) rep(j, k) { // 行先のマスの座標 int to_i = cur_i + di[i] * (j + 1); int to_j = cur_j + dj[i] * (j + 1); if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0) break; // 範囲外だったら追加しない if (g[to_i][to_j] == '@') break; // ハスがあったら追加しない if (c + 1 < cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する add_g(to_i, to_j); cost[to_i][to_j] = c + 1; // 行先のコストを更新 } } } ll ans = cost[gi][gj]; if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; using ll = long long; using P = pair<int, int>; const ll INF = 1e18; const int di[4] = {-1, 0, 1, 0}; const int dj[4] = {0, 1, 0, -1}; int main() { int h, w, k, si, sj, gi, gj; cin >> h >> w >> k >> si >> sj >> gi >> gj; si--; sj--; gi--; gj--; vector<string> g(h); rep(i, h) cin >> g[i]; // 各マスについてスタートから到達までのコスト(かく回数) vector<vector<ll>> cost(h, vector<ll>(w, INF)); cost[si][sj] = 0; // スタートマスはコスト0 // 調べるマスを入れるキュー queue<P> q; // 調べるマスをキューに追加する関数 auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); }; // スタート地点から調べる add_g(si, sj); while (!q.empty()) { // キューが空でない限りループ // キューから調べるマスの座標を取り出す P p = q.front(); int cur_i = p.first; int cur_j = p.second; const ll c = cost[cur_i][cur_j]; q.pop(); // キューに調べるマスを追加 rep(i, 4) rep(j, k) { // 行先のマスの座標 int to_i = cur_i + di[i] * (j + 1); int to_j = cur_j + dj[i] * (j + 1); if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0) break; // 範囲外だったら追加しない if (c >= cost[to_i][to_j]) break; // 行先のコストの方が小さかったら追加しない if (g[to_i][to_j] == '@') break; // ハスがあったら追加しない if (c + 1 < cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する add_g(to_i, to_j); cost[to_i][to_j] = c + 1; // 行先のコストを更新 } } } ll ans = cost[gi][gj]; if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
insert
49
49
49
51
TLE
p02644
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define FOR(__I, __N) for (int __I = 0; __I < __N; ++__I) typedef long long int64; typedef pair<int, int> pii; typedef pair<int64, int64> pll; typedef double db; typedef long double ldb; const int INF = 0x3f3f3f3f; const int64 INF64 = 0x3f3f3f3f3f3f3f3fLL; const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const int MOD = 1000000007; const int MAX_H = 1000000; const int MAX_W = 1000000; map<int, bool> maze[MAX_W]; map<int, int> dist[MAX_W]; int H, W, K; pii que[5 * MAX_H]; int solve(int x1, int y1, int x2, int y2) { int qs = 0, qt = 0; FOR(i, H) FOR(j, W) dist[i][j] = INF; que[qt++] = pii(x1, y1); dist[x1][y1] = 0; while (qs < qt) { pii p = que[qs++]; int x = p.first, y = p.second; int d = dist[x][y]; if (x == x2 && y == y2) return d; FOR(k, 4) { int xx = x, yy = y; FOR(step, K) { xx += dir[k][0]; yy += dir[k][1]; if (xx < 0 || xx > H) break; if (yy < 0 || yy > W) break; if (maze[xx][yy]) break; int &dd = dist[xx][yy]; if (dd > d + 1) { dd = d + 1; que[qt++] = pii(xx, yy); } if (dd + 1 <= d) { break; } } } } return -1; } char buff[MAX_H + 1]; int main() { cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1; --x2, --y2; FOR(i, H) { scanf(" %s", buff); FOR(j, W) maze[i][j] = buff[j] == '@'; } int ans = solve(x1, y1, x2, y2); printf("%d\n", ans); }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; #define FOR(__I, __N) for (int __I = 0; __I < __N; ++__I) typedef long long int64; typedef pair<int, int> pii; typedef pair<int64, int64> pll; typedef double db; typedef long double ldb; const int INF = 0x3f3f3f3f; const int64 INF64 = 0x3f3f3f3f3f3f3f3fLL; const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const int MOD = 1000000007; const int MAX_H = 1000000; const int MAX_W = 1000000; map<int, bool> maze[MAX_W]; map<int, int> dist[MAX_W]; int H, W, K; pii que[5 * MAX_H]; int solve(int x1, int y1, int x2, int y2) { int qs = 0, qt = 0; FOR(i, H) FOR(j, W) dist[i][j] = INF; que[qt++] = pii(x1, y1); dist[x1][y1] = 0; while (qs < qt) { pii p = que[qs++]; int x = p.first, y = p.second; int d = dist[x][y]; if (x == x2 && y == y2) return d; FOR(k, 4) { int xx = x, yy = y; FOR(step, K) { xx += dir[k][0]; yy += dir[k][1]; if (xx < 0 || xx > H) break; if (yy < 0 || yy > W) break; if (maze[xx][yy]) break; int &dd = dist[xx][yy]; if (dd > d + 1) { dd = d + 1; que[qt++] = pii(xx, yy); } if (dd <= d) { break; } if (dd + 1 <= d) { break; } } } } return -1; } char buff[MAX_H + 1]; int main() { cin >> H >> W >> K; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1; --x2, --y2; FOR(i, H) { scanf(" %s", buff); FOR(j, W) maze[i][j] = buff[j] == '@'; } int ans = solve(x1, y1, x2, y2); printf("%d\n", ans); }
insert
67
67
67
72
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; string S[h]; for (int i = 0; i < h; i++) cin >> S[i]; constexpr int inf = 1e9; vector<vector<int>> D(h, vector<int>(w, inf)); D[x1][y1] = 0; queue<tuple<int, int>> Q; Q.emplace(x1, y1); auto in = [&](int x, int y) { return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@'; }; while (!Q.empty()) { auto [x, y] = Q.front(); Q.pop(); int d = D[x][y] + 1; for (int i = 0; i < 4; i++) { int s = x, t = y; for (int j = 0; j < k; j++) { s += dx[i], t += dy[i]; if (!in(s, t)) break; if (D[s][t] > d) { D[s][t] = d; Q.emplace(s, t); } } } } if (D[x2][y2] == inf) D[x2][y2] = -1; cout << D[x2][y2] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; string S[h]; for (int i = 0; i < h; i++) cin >> S[i]; constexpr int inf = 1e9; vector<vector<int>> D(h, vector<int>(w, inf)); D[x1][y1] = 0; queue<tuple<int, int>> Q; Q.emplace(x1, y1); auto in = [&](int x, int y) { return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@'; }; while (!Q.empty()) { auto [x, y] = Q.front(); Q.pop(); int d = D[x][y] + 1; for (int i = 0; i < 4; i++) { int s = x, t = y; for (int j = 0; j < k; j++) { s += dx[i], t += dy[i]; if (!in(s, t)) break; if (D[s][t] < d) break; if (D[s][t] > d) { D[s][t] = d; Q.emplace(s, t); } } } } if (D[x2][y2] == inf) D[x2][y2] = -1; cout << D[x2][y2] << "\n"; return 0; }
insert
35
35
35
37
TLE
p02644
C++
Time Limit Exceeded
// Remeber Choose Compiler: GCC9 #include <bits/stdc++.h> using namespace std; // Debug Output Utility #ifdef LOCAL #define dbg(args...) \ do { \ cout << #args << " -> "; \ err(args); \ } while (0) void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } template <template <typename...> class T, typename t, typename... A> void err(const T<t> &arg, const A &...args) { for (auto &v : arg) std::cout << v << ' '; err(args...); } #else #define dbg(...) #endif // Macro Definitions: #define mp make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() // Type Alias: using int64 = long long; using PII = pair<int, int>; // Global Const Variable const int64 INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1000000007; const int MAXN = 200005; const int M = 1000005; const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void run() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<vector<char>> mat(h + 1, vector<char>(w + 1)); vector<vector<int>> dis(h + 1, vector<int>(w + 1, -1)); string s; for (int i = 1; i <= h; i++) { cin >> s; for (int j = 1; j <= w; j++) mat[i][j] = s[j - 1]; } if (mat[x1][y1] == '@' || mat[x2][y2] == '@') { cout << -1 << endl; return; } queue<PII> q; q.emplace(x1, y1); dis[x1][y1] = 0; while (!q.empty()) { int x = q.front().fi, y = q.front().se; q.pop(); for (int p = 0; p < 4; p++) { for (int j = 1; j <= k; j++) { int xx = x + j * dir[p][0], yy = y + j * dir[p][1]; if (1 <= xx && xx <= h && 1 <= yy && yy <= w) { if (mat[xx][yy] == '@') break; if (dis[xx][yy] != -1) continue; q.emplace(xx, yy); dis[xx][yy] = dis[x][y] + 1; if (xx == x2 && yy == y2) { cout << dis[xx][yy] << endl; return; } } else { break; } } } } cout << -1 << endl; } // Debug Input Redirection int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL freopen("in.txt", "r", stdin); #endif run(); }
// Remeber Choose Compiler: GCC9 #include <bits/stdc++.h> using namespace std; // Debug Output Utility #ifdef LOCAL #define dbg(args...) \ do { \ cout << #args << " -> "; \ err(args); \ } while (0) void err() { std::cout << std::endl; } template <typename T, typename... Args> void err(T a, Args... args) { std::cout << a << ' '; err(args...); } template <template <typename...> class T, typename t, typename... A> void err(const T<t> &arg, const A &...args) { for (auto &v : arg) std::cout << v << ' '; err(args...); } #else #define dbg(...) #endif // Macro Definitions: #define mp make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() // Type Alias: using int64 = long long; using PII = pair<int, int>; // Global Const Variable const int64 INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1000000007; const int MAXN = 200005; const int M = 1000005; const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void run() { int h, w, k; cin >> h >> w >> k; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; vector<vector<char>> mat(h + 1, vector<char>(w + 1)); vector<vector<int>> dis(h + 1, vector<int>(w + 1, -1)); string s; for (int i = 1; i <= h; i++) { cin >> s; for (int j = 1; j <= w; j++) mat[i][j] = s[j - 1]; } if (mat[x1][y1] == '@' || mat[x2][y2] == '@') { cout << -1 << endl; return; } queue<PII> q; q.emplace(x1, y1); dis[x1][y1] = 0; while (!q.empty()) { int x = q.front().fi, y = q.front().se; q.pop(); for (int p = 0; p < 4; p++) { for (int j = 1; j <= k; j++) { int xx = x + j * dir[p][0], yy = y + j * dir[p][1]; if (1 <= xx && xx <= h && 1 <= yy && yy <= w) { if (mat[xx][yy] == '@') break; if (dis[xx][yy] != -1) { if (dis[xx][yy] > dis[x][y]) continue; else break; } q.emplace(xx, yy); dis[xx][yy] = dis[x][y] + 1; if (xx == x2 && yy == y2) { cout << dis[xx][yy] << endl; return; } } else { break; } } } } cout << -1 << endl; } // Debug Input Redirection int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL freopen("in.txt", "r", stdin); #endif run(); }
replace
74
76
74
80
TLE
p02644
C++
Time Limit Exceeded
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <fstream> #include <iostream> #include <bits/stdc++.h> #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; class FPondSkater { public: int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; void solve(std::istream &cin, std::ostream &cout) { int H, W, K; cin >> H >> W >> K; int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--; vector<vector<char>> c(H, vector<char>(W)); REP(i, H) REP(j, W) cin >> c[i][j]; vector<vector<int>> dist(H, vector<int>(W, INT_MAX)); dist[sx][sy] = 0; queue<tuple<int, int, int>> q; q.push({sx, sy, 0}); while (!q.empty()) { int x, y, d; tie(x, y, d) = q.front(); q.pop(); if (dist[x][y] < d) continue; dist[x][y] = d; REP(k, 4) { int nx = x + dx[k]; int ny = y + dy[k]; while (0 <= nx && nx < H && 0 <= ny && ny < W) { if (dist[nx][ny] <= d) break; if (abs(nx - x) > K || abs(ny - y) > K) break; if (c[nx][ny] == '@') break; q.push({nx, ny, d + 1}); dist[nx][ny] = d + 1; nx += dx[k]; ny += dy[k]; } } } if (dist[gx][gy] == INT_MAX) { cout << -1 << endl; return; } cout << dist[gx][gy] << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream &in(std::cin); std::ostream &out(std::cout); FPondSkater solver; solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <fstream> #include <iostream> #include <bits/stdc++.h> #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; typedef pair<int, int> P; class FPondSkater { public: int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; void solve(std::istream &cin, std::ostream &cout) { int H, W, K; cin >> H >> W >> K; int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--; vector<vector<char>> c(H, vector<char>(W)); REP(i, H) REP(j, W) cin >> c[i][j]; vector<vector<int>> dist(H, vector<int>(W, INT_MAX)); dist[sx][sy] = 0; queue<tuple<int, int, int>> q; q.push({sx, sy, 0}); while (!q.empty()) { int x, y, d; tie(x, y, d) = q.front(); q.pop(); if (dist[x][y] < d) continue; dist[x][y] = d; REP(k, 4) { int nx = x + dx[k]; int ny = y + dy[k]; while (0 <= nx && nx < H && 0 <= ny && ny < W) { if (dist[nx][ny] <= d) break; if (abs(nx - x) > K || abs(ny - y) > K) break; if (c[nx][ny] == '@') break; if (dist[nx][ny] == INT_MAX) { q.push({nx, ny, d + 1}); dist[nx][ny] = d + 1; } nx += dx[k]; ny += dy[k]; } } } if (dist[gx][gy] == INT_MAX) { cout << -1 << endl; return; } cout << dist[gx][gy] << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream &in(std::cin); std::ostream &out(std::cout); FPondSkater solver; solver.solve(in, out); return 0; }
replace
61
63
61
65
TLE
p02644
C++
Time Limit Exceeded
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; 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; } int h, w, k; int xs, ys, xg, yg; vector<vector<char>> c; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; void solve() { cin >> h >> w >> k; cin >> xs >> ys >> xg >> yg; --xs, --ys, --xg, --yg; c.assign(h, vector<char>(w)); rep(i, h) { rep(j, w) { cin >> c[i][j]; } } queue<P> que; vector<vector<int>> dist(h, vector<int>(w, INF)); que.push(P{xs, ys}); dist[xs][ys] = 0; while (!que.empty()) { P p = que.front(); que.pop(); int x = p.fr, y = p.sc; rep(i, 4) { int nx = x, ny = y; for (int j = 0; j < k; ++j) { nx += dx[i], ny += dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') { if (chmin(dist[nx][ny], dist[x][y] + 1)) { que.push(P{nx, ny}); } } else { break; } } } } if (dist[xg][yg] == INF) { cout << -1 << endl; return; } cout << dist[xg][yg] << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
/** * author: otera **/ #include <algorithm> #include <bitset> #include <cassert> #include <ciso646> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const int inf = 1e9 + 7; const ll INF = 1LL << 60; const ll mod = 1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define Rep(i, sta, n) for (int i = sta; i < n; i++) #define rep1(i, n) for (int i = 1; i <= n; i++) #define per1(i, n) for (int i = n; i >= 1; i--) #define Rep1(i, sta, n) for (int i = sta; i <= n; i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; #define fr first #define sc second #define all(c) c.begin(), c.end() #define pb push_back #define debug(x) cerr << #x << " = " << (x) << endl; 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; } int h, w, k; int xs, ys, xg, yg; vector<vector<char>> c; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; void solve() { cin >> h >> w >> k; cin >> xs >> ys >> xg >> yg; --xs, --ys, --xg, --yg; c.assign(h, vector<char>(w)); rep(i, h) { rep(j, w) { cin >> c[i][j]; } } queue<P> que; vector<vector<int>> dist(h, vector<int>(w, INF)); que.push(P{xs, ys}); dist[xs][ys] = 0; while (!que.empty()) { P p = que.front(); que.pop(); int x = p.fr, y = p.sc; rep(i, 4) { int nx = x, ny = y; for (int j = 0; j < k; ++j) { nx += dx[i], ny += dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] == '.') { if (chmin(dist[nx][ny], dist[x][y] + 1)) { que.push(P{nx, ny}); } else if (dist[nx][ny] < dist[x][y] + 1) { break; } } else { break; } } } } if (dist[xg][yg] == INF) { cout << -1 << endl; return; } cout << dist[xg][yg] << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); // cout << fixed << setprecision(10); // int t; cin >> t; rep(i, t)solve(); solve(); return 0; }
insert
96
96
96
98
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define endl '\n' #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define zy -2147382637 #define bql 2147483647 #define ll long long #define ull unsigned long long #define ld long double #define il inline #define sz(x) x.size() #define maxn 100010 #define rp(i, l, r) for (int i = l; i <= r; i++) #define rb(i, r, l) for (int i = r; i >= l; i--) #define fi first #define se second #define pb push_back #define mp make_pair using namespace std; ll max(ll a, ll b) { if (a > b) return a; else return b; } ll min(ll a, ll b) { if (a < b) return a; else return b; } ll lowbit(ll x) { return x & (-x); } ll prime(ll x) { if (x <= 1) return false; for (int i = 2; i <= int(sqrt(x)); i++) { if (x % i == 0) return false; } return true; } bool cmp(ll a, ll b) { return a > b; } ll gcd(ll a, ll b) { ll r; while (b > 0) { r = a % b; a = b; b = r; } return a; } ll powmod(ll a, ll b, ll mod) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } inline int readint() { char c = getchar(); int w = 1; int x = 0; while (!isdigit(c)) (c == '-') && (w = -w), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); x *= w; return x; } inline ll readll() { char c = getchar(); ll w = 1; ll x = 0; while (!isdigit(c)) (c == '-') && (w = -w), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); x *= w; return x; } void fl(string name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } const int dx[8] = {-1, 1, 0, 0, -1, 1, -1, 1}, dy[8] = {0, 0, -1, 1, -1, -1, 1, 1}; struct node { int x, y; }; int h, w, k, xx1, yy1, x2, y2; bool ok(int x, int y) { if (x <= h && x >= 1 && y <= w && y >= 1) return true; else return false; } queue<node> Q; int main() { cin >> h >> w >> k; char g[h + 1][w + 1]; int vis[h + 1][w + 1], step[h + 1][w + 1]; memset(vis, 0, sizeof(vis)); memset(step, 0, sizeof(step)); cin >> xx1 >> yy1 >> x2 >> y2; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> g[i][j]; } } node a, b; a.x = xx1, a.y = yy1; step[xx1][yy1] = 0; vis[xx1][yy1] = 1; Q.push(a); int ans = bql; while (!Q.empty()) { a = Q.front(); Q.pop(); if (a.x == x2 && a.y == y2) { ans = min(ans, step[x2][y2]); } for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { b.x = dx[i] * j + a.x; b.y = dy[i] * j + a.y; if (ok(b.x, b.y) == 0 /*||(step[b.x][b.y]<=step[a.x][a.y]&&step[b.x][b.y])*/) break; else { if (g[b.x][b.y] == '@') break; if (g[b.x][b.y] == '.' && !vis[b.x][b.y]) { vis[b.x][b.y] = 1; step[b.x][b.y] = step[a.x][a.y] + 1; Q.push(b); } } } } } if (ans == bql) cout << -1 << endl; else cout << ans << endl; return 0; } /* stuff to remember * int overflow, array bounds * special cases (n=1? n=0?) * do something instead of nothing and stay organized * USE STATIC ARRAYS * DEFINING ARRAYS BEFORE main() * DO NOT DEFINE ARRAYS IN main() * USE INT INSTEAD OF LL,NOTICE THE MLE */
#include <bits/stdc++.h> #define endl '\n' #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define zy -2147382637 #define bql 2147483647 #define ll long long #define ull unsigned long long #define ld long double #define il inline #define sz(x) x.size() #define maxn 100010 #define rp(i, l, r) for (int i = l; i <= r; i++) #define rb(i, r, l) for (int i = r; i >= l; i--) #define fi first #define se second #define pb push_back #define mp make_pair using namespace std; ll max(ll a, ll b) { if (a > b) return a; else return b; } ll min(ll a, ll b) { if (a < b) return a; else return b; } ll lowbit(ll x) { return x & (-x); } ll prime(ll x) { if (x <= 1) return false; for (int i = 2; i <= int(sqrt(x)); i++) { if (x % i == 0) return false; } return true; } bool cmp(ll a, ll b) { return a > b; } ll gcd(ll a, ll b) { ll r; while (b > 0) { r = a % b; a = b; b = r; } return a; } ll powmod(ll a, ll b, ll mod) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } inline int readint() { char c = getchar(); int w = 1; int x = 0; while (!isdigit(c)) (c == '-') && (w = -w), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); x *= w; return x; } inline ll readll() { char c = getchar(); ll w = 1; ll x = 0; while (!isdigit(c)) (c == '-') && (w = -w), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); x *= w; return x; } void fl(string name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } const int dx[8] = {-1, 1, 0, 0, -1, 1, -1, 1}, dy[8] = {0, 0, -1, 1, -1, -1, 1, 1}; struct node { int x, y; }; int h, w, k, xx1, yy1, x2, y2; bool ok(int x, int y) { if (x <= h && x >= 1 && y <= w && y >= 1) return true; else return false; } queue<node> Q; int main() { cin >> h >> w >> k; char g[h + 1][w + 1]; int vis[h + 1][w + 1], step[h + 1][w + 1]; memset(vis, 0, sizeof(vis)); memset(step, 0, sizeof(step)); cin >> xx1 >> yy1 >> x2 >> y2; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> g[i][j]; } } node a, b; a.x = xx1, a.y = yy1; step[xx1][yy1] = 0; vis[xx1][yy1] = 1; Q.push(a); int ans = bql; while (!Q.empty()) { a = Q.front(); Q.pop(); if (a.x == x2 && a.y == y2) { ans = min(ans, step[x2][y2]); } for (int i = 0; i < 4; i++) { for (int j = 1; j <= k; j++) { b.x = dx[i] * j + a.x; b.y = dy[i] * j + a.y; if (ok(b.x, b.y) == 0 || (step[b.x][b.y] <= step[a.x][a.y] && step[b.x][b.y])) break; else { if (g[b.x][b.y] == '@') break; if (g[b.x][b.y] == '.' && !vis[b.x][b.y]) { vis[b.x][b.y] = 1; step[b.x][b.y] = step[a.x][a.y] + 1; Q.push(b); } } } } } if (ans == bql) cout << -1 << endl; else cout << ans << endl; return 0; } /* stuff to remember * int overflow, array bounds * special cases (n=1? n=0?) * do something instead of nothing and stay organized * USE STATIC ARRAYS * DEFINING ARRAYS BEFORE main() * DO NOT DEFINE ARRAYS IN main() * USE INT INSTEAD OF LL,NOTICE THE MLE */
replace
129
131
129
131
TLE
p02644
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { int H, W, K, x1, y1, x2, y2, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2; vector<string> c(H); rep(i, H) cin >> c[i]; vector<vector<int>> d(H + 2, vector<int>(W + 2, 1e9)); queue<vector<int>> q; q.push({x1, y1, 0}); d[x1][y1] = 0; while (!q.empty()) { auto p = q.front(); q.pop(); int x0 = p[0], y0 = p[1], d0 = p[2]; rep(i, 4) { rep(j, K) { int nx = x0 + dx[i] * (j + 1), ny = y0 + dy[i] * (j + 1); if (nx < 1 || nx > H || ny < 1 || ny > W) break; if (c[nx - 1][ny - 1] == '@') break; if (d[nx][ny] < d[x0][y0]) break; if (d[nx][ny] != 1e9) continue; q.push({nx, ny, d0 + 1}); d[nx][ny] = d0 + 1; } } } if (d[x2][y2] == 1e9) cout << "-1\n"; else cout << d[x2][y2] << "\n"; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long ll; int main() { int H, W, K, x1, y1, x2, y2, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2; vector<string> c(H); rep(i, H) cin >> c[i]; vector<vector<int>> d(H + 2, vector<int>(W + 2, 1e9)); queue<vector<int>> q; q.push({x1, y1, 0}); d[x1][y1] = 0; while (!q.empty()) { auto p = q.front(); q.pop(); int x0 = p[0], y0 = p[1], d0 = p[2]; rep(i, 4) { rep(j, K) { int nx = x0 + dx[i] * (j + 1), ny = y0 + dy[i] * (j + 1); if (nx < 1 || nx > H || ny < 1 || ny > W) break; if (c[nx - 1][ny - 1] == '@') break; if (d[nx][ny] <= d[x0][y0]) break; if (d[nx][ny] != 1e9) continue; q.push({nx, ny, d0 + 1}); d[nx][ny] = d0 + 1; } } } if (d[x2][y2] == 1e9) cout << "-1\n"; else cout << d[x2][y2] << "\n"; }
replace
26
27
26
27
TLE