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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define start_routine() int begtime = clock();
#define end_routine() \
int endtime = clock(); \
cerr << endl \
<< "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC \
<< " ms"; \
return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define print(stuff) cout << stuff << endl
#define printc(stuff) \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff) \
cout << #stuff << ": "; \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define len length
#define ret0 return 0
#define ret return
#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
// typedef tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN (ll)1e5 + 5
// ll f2(ll n){
// // for(int i=0; i<n; i++){
// // cout<<b[i]<<" ";
// // }
// // cout<<endl;
// while(!q.empty() && b[q.top().se] == a[q.top().se]){
// // debug(q.top().se);
// // debug(b[q.top().se]);
// // debug(a[q.top().se]);
// q.pop();
// }
// if(q.empty()){
// return 0;
// }
// if(b[q.top().se] < a[q.top().se]){
// return -1;
// }
// else {
// ll x = q.top().se;
// ll y = b[(x-1+n)%n] + b[(x+1+n)%n];
// if(b[x] <= y){
// return -1;
// }
// else {
// ll tim = (b[x]-a[x])/y;
// ans2+=tim;
// b[x]-=tim*y;
// q.pop();
// q.push(mp(b[x],x));
// return f2(n);
// }
// }
// }
void solve() {
ll n;
cin >> n;
ll a[n], b[n];
priority_queue<pll> q;
loop(i, 0, n) {
cin >> a[i];
// q1.push(mp(a[i], i));
}
loop(i, 0, n) {
cin >> b[i];
q.push(mp(b[i], i));
}
ll ans = 0;
while (!q.empty()) {
if (a[q.top().se] > b[q.top().se]) {
print(-1);
ret;
}
while (!q.empty() && b[q.top().se] == a[q.top().se]) {
q.pop();
}
if (q.empty()) {
break;
}
if (a[q.top().se] > b[q.top().se]) {
print(-1);
ret;
} else {
ll x = q.top().se;
ll y = b[(x - 1 + n) % n] + b[(x + 1 + n) % n];
// debug(x);
// debug(y);
if (b[x] <= y) {
print(-1);
ret;
}
ll tim = (b[x] - a[x]) / y;
ans += tim;
// debug(tim);
q.pop();
b[x] -= tim * y;
q.push(mp(b[x], x));
}
}
print(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define start_routine() int begtime = clock();
#define end_routine() \
int endtime = clock(); \
cerr << endl \
<< "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC \
<< " ms"; \
return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define print(stuff) cout << stuff << endl
#define printc(stuff) \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff) \
cout << #stuff << ": "; \
for (auto x : stuff) \
cout << x << " "; \
cout << endl;
#define len length
#define ret0 return 0
#define ret return
#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>
// typedef tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN (ll)1e5 + 5
// ll f2(ll n){
// // for(int i=0; i<n; i++){
// // cout<<b[i]<<" ";
// // }
// // cout<<endl;
// while(!q.empty() && b[q.top().se] == a[q.top().se]){
// // debug(q.top().se);
// // debug(b[q.top().se]);
// // debug(a[q.top().se]);
// q.pop();
// }
// if(q.empty()){
// return 0;
// }
// if(b[q.top().se] < a[q.top().se]){
// return -1;
// }
// else {
// ll x = q.top().se;
// ll y = b[(x-1+n)%n] + b[(x+1+n)%n];
// if(b[x] <= y){
// return -1;
// }
// else {
// ll tim = (b[x]-a[x])/y;
// ans2+=tim;
// b[x]-=tim*y;
// q.pop();
// q.push(mp(b[x],x));
// return f2(n);
// }
// }
// }
void solve() {
ll n;
cin >> n;
ll a[n], b[n];
priority_queue<pll> q;
loop(i, 0, n) {
cin >> a[i];
// q1.push(mp(a[i], i));
}
loop(i, 0, n) {
cin >> b[i];
q.push(mp(b[i], i));
}
ll ans = 0;
while (!q.empty()) {
if (a[q.top().se] > b[q.top().se]) {
print(-1);
ret;
}
while (!q.empty() && b[q.top().se] == a[q.top().se]) {
q.pop();
}
if (q.empty()) {
break;
}
if (a[q.top().se] > b[q.top().se]) {
print(-1);
ret;
} else {
ll x = q.top().se;
ll y = b[(x - 1 + n) % n] + b[(x + 1 + n) % n];
// debug(x);
// debug(y);
if (b[x] <= y) {
print(-1);
ret;
}
ll tim = (b[x] - a[x]) / y;
if (tim <= 0) {
print(-1);
ret;
}
ans += tim;
// debug(tim);
q.pop();
b[x] -= tim * y;
q.push(mp(b[x], x));
}
}
print(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin>>t;
while (t--) {
solve();
}
}
| insert | 137 | 137 | 137 | 141 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
using ld = long double;
int const nmax = 200000;
int target[1 + nmax], v[1 + nmax];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> target[i];
for (int i = 0; i < n; i++)
cin >> v[i];
queue<int> q;
for (int i = 0; i < n; i++)
q.push(i);
ll result = 0;
while (0 < q.size()) {
int pos = q.front();
q.pop();
if (v[pos] < v[(pos + n - 1) % n] || v[pos] < v[(pos + 1) % n])
continue;
if (v[pos] <= target[pos])
continue;
int stop = max(target[pos], max(v[(pos + 1) % n], v[(pos + (n - 1)) % n]));
int jumps = (v[pos] - stop) / (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
jumps = max(jumps, 1);
if (0 < jumps) {
v[pos] -= jumps * (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
q.push((pos + 1) % n);
q.push((pos + n - 1) % n);
q.push(pos);
result += jumps;
}
}
for (int i = 0; i < n; i++)
if (v[i] != target[i]) {
cout << -1;
return 0;
}
cout << result;
return 0;
}
| #include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
using ld = long double;
int const nmax = 200000;
int target[1 + nmax], v[1 + nmax];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> target[i];
for (int i = 0; i < n; i++)
cin >> v[i];
queue<int> q;
for (int i = 0; i < n; i++)
q.push(i);
ll result = 0;
while (0 < q.size()) {
int pos = q.front();
q.pop();
if (v[pos] < v[(pos + n - 1) % n] || v[pos] < v[(pos + 1) % n])
continue;
if (v[pos] <= target[pos])
continue;
int stop = max(target[pos], max(v[(pos + 1) % n], v[(pos + (n - 1)) % n]));
int jumps = (v[pos] - stop) / (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
jumps = max(jumps, 1);
if (0 < jumps) {
v[pos] -= jumps * (v[(pos + 1) % n] + v[(pos + (n - 1)) % n]);
q.push((pos + 1) % n);
q.push((pos + n - 1) % n);
q.push(pos);
result += jumps;
}
if (v[pos] < target[pos])
break;
}
for (int i = 0; i < n; i++)
if (v[i] != target[i]) {
cout << -1;
return 0;
}
cout << result;
return 0;
}
| insert | 47 | 47 | 47 | 49 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, l, r) for (int i = int(l); i < int(r); i++)
#define rep(i, r) rng(i, 0, r)
#define rrng(i, l, r) for (int i = int(r) - 1; i >= int(l); i--)
#define rrep(i, r) rrng(i, 0, r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x, -1, sizeof(x))
#define zero(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define si(x) int(x.size())
#define inf INT_MAX / 2 - 100
#define infl LLONG_MAX / 3
#ifdef LOCAL
#define dmp(x) cerr << __LINE__ << ' ' << #x << ' ' << x << endl
#else
#define dmp(x) void(0)
#endif
template <class t, class u> void chmax(t &a, u b) {
if (a < b)
a = b;
}
template <class t, class u> void chmin(t &a, u b) {
if (b < a)
a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vector<vector<t>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
ll readl(void) {
ll x;
cin >> x;
return x;
}
int readi(void) {
int x;
cin >> x;
return x;
}
string readstr() {
string s;
cin >> s;
return s;
}
vi readvi(int n, int off = 0) {
vi v(n);
rep(i, n) v[i] = readi(), v[i] += off;
return v;
}
vl readvl(int n, int off = 0) {
vl v(n);
rep(i, n) v[i] = readl(), v[i] += off;
return v;
}
template <class t> void print(t x, int suc = 1) {
cout << x;
if (suc == 1)
cout << "\n";
if (suc == 2)
cout << " ";
}
template <class t> void print(const vc<t> &v, int suc = 1) {
rep(i, si(v)) print(v[i], i == int(si(v)) - 1 ? 1 : suc);
}
template <class t> bool inc(t a, t b, t c) { return !(c < b || b < a); }
template <class t> void compress(vc<t> &v) {
sort(all(v));
v.erase(unique(all(v)), v.ed);
}
template <class t> int lwb(const vc<t> &v, const t &a) {
return lower_bound(all(v), a) - v.bg;
}
template <class t> struct Compress {
vc<t> v;
Compress() = default;
Compress(const vc<t> &x) { add(x); }
Compress(const initializer_list<vc<t>> &x) {
for (auto &p : x)
add(p);
}
void add(const t &x) { v.eb(x); }
void add(const vc<t> &x) { copy(all(x), back_inserter(v)); }
void build() { compress(v); }
int get(const t &x) const { return lwb(v, x); }
vc<t> get(const vc<t> &x) const {
vc<t> res(x);
for (auto &p : res)
p = get(p);
return res;
}
const t &operator[](int x) const { return v[x]; }
};
void Yes(bool ex = true) {
cout << "Yes\n";
if (ex)
exit(0);
}
void YES(bool ex = true) {
cout << "YES\n";
if (ex)
exit(0);
}
void No(bool ex = true) {
cout << "No\n";
if (ex)
exit(0);
}
void NO(bool ex = true) {
cout << "NO\n";
if (ex)
exit(0);
}
void orYes(bool x, bool ex = true) {
if (x)
Yes(ex);
else
No(ex);
}
void orYES(bool x, bool ex = true) {
if (x)
YES(ex);
else
NO(ex);
}
void Possible(bool ex = true) {
cout << "Possible\n";
if (ex)
exit(0);
}
void POSSIBLE(bool ex = true) {
cout << "POSSIBLE\n";
if (ex)
exit(0);
}
void Impossible(bool ex = true) {
cout << "Impossible\n";
if (ex)
exit(0);
}
void IMPOSSIBLE(bool ex = true) {
cout << "IMPOSSIBLE\n";
if (ex)
exit(0);
}
void orPossible(bool x, bool ex = true) {
if (x)
Possible(ex);
else
Impossible(ex);
}
void orPOSSIBLE(bool x, bool ex = true) {
if (x)
POSSIBLE(ex);
else
IMPOSSIBLE(ex);
}
ll n, a[100005], b[100005], ans;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n;
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
priority_queue<pl> pq;
rep(i, n) {
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
pq.push(mp(b[i], i));
}
while (!pq.empty()) {
ll y = pq.top().S;
pq.pop();
ll x = (y - 1 + n) % n, z = (y + 1) % n;
ll s = b[x] + b[z];
/*
rep(i,n)cout<<b[i]<<' ';
cout<<endl;
cout<<y<<' '<<b[y]<<' '<<s<<endl;
*/
if (b[y] <= s)
continue;
if ((b[y] - a[y]) % s == 0) {
ans += (b[y] - a[y]) / s;
b[y] = a[y];
} else {
ans += b[y] / s;
b[y] %= s;
if (b[y] < b[x] || b[y] < b[z]) {
pq.push(mp(b[y], y));
}
}
}
rep(i, n) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
print(ans);
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, l, r) for (int i = int(l); i < int(r); i++)
#define rep(i, r) rng(i, 0, r)
#define rrng(i, l, r) for (int i = int(r) - 1; i >= int(l); i--)
#define rrep(i, r) rrng(i, 0, r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x, -1, sizeof(x))
#define zero(x) memset(x, 0, sizeof(x))
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define si(x) int(x.size())
#define inf INT_MAX / 2 - 100
#define infl LLONG_MAX / 3
#ifdef LOCAL
#define dmp(x) cerr << __LINE__ << ' ' << #x << ' ' << x << endl
#else
#define dmp(x) void(0)
#endif
template <class t, class u> void chmax(t &a, u b) {
if (a < b)
a = b;
}
template <class t, class u> void chmin(t &a, u b) {
if (b < a)
a = b;
}
template <class t> using vc = vector<t>;
template <class t> using vvc = vector<vector<t>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
ll readl(void) {
ll x;
cin >> x;
return x;
}
int readi(void) {
int x;
cin >> x;
return x;
}
string readstr() {
string s;
cin >> s;
return s;
}
vi readvi(int n, int off = 0) {
vi v(n);
rep(i, n) v[i] = readi(), v[i] += off;
return v;
}
vl readvl(int n, int off = 0) {
vl v(n);
rep(i, n) v[i] = readl(), v[i] += off;
return v;
}
template <class t> void print(t x, int suc = 1) {
cout << x;
if (suc == 1)
cout << "\n";
if (suc == 2)
cout << " ";
}
template <class t> void print(const vc<t> &v, int suc = 1) {
rep(i, si(v)) print(v[i], i == int(si(v)) - 1 ? 1 : suc);
}
template <class t> bool inc(t a, t b, t c) { return !(c < b || b < a); }
template <class t> void compress(vc<t> &v) {
sort(all(v));
v.erase(unique(all(v)), v.ed);
}
template <class t> int lwb(const vc<t> &v, const t &a) {
return lower_bound(all(v), a) - v.bg;
}
template <class t> struct Compress {
vc<t> v;
Compress() = default;
Compress(const vc<t> &x) { add(x); }
Compress(const initializer_list<vc<t>> &x) {
for (auto &p : x)
add(p);
}
void add(const t &x) { v.eb(x); }
void add(const vc<t> &x) { copy(all(x), back_inserter(v)); }
void build() { compress(v); }
int get(const t &x) const { return lwb(v, x); }
vc<t> get(const vc<t> &x) const {
vc<t> res(x);
for (auto &p : res)
p = get(p);
return res;
}
const t &operator[](int x) const { return v[x]; }
};
void Yes(bool ex = true) {
cout << "Yes\n";
if (ex)
exit(0);
}
void YES(bool ex = true) {
cout << "YES\n";
if (ex)
exit(0);
}
void No(bool ex = true) {
cout << "No\n";
if (ex)
exit(0);
}
void NO(bool ex = true) {
cout << "NO\n";
if (ex)
exit(0);
}
void orYes(bool x, bool ex = true) {
if (x)
Yes(ex);
else
No(ex);
}
void orYES(bool x, bool ex = true) {
if (x)
YES(ex);
else
NO(ex);
}
void Possible(bool ex = true) {
cout << "Possible\n";
if (ex)
exit(0);
}
void POSSIBLE(bool ex = true) {
cout << "POSSIBLE\n";
if (ex)
exit(0);
}
void Impossible(bool ex = true) {
cout << "Impossible\n";
if (ex)
exit(0);
}
void IMPOSSIBLE(bool ex = true) {
cout << "IMPOSSIBLE\n";
if (ex)
exit(0);
}
void orPossible(bool x, bool ex = true) {
if (x)
Possible(ex);
else
Impossible(ex);
}
void orPOSSIBLE(bool x, bool ex = true) {
if (x)
POSSIBLE(ex);
else
IMPOSSIBLE(ex);
}
ll n, a[200005], b[200005], ans;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n;
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
priority_queue<pl> pq;
rep(i, n) {
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
pq.push(mp(b[i], i));
}
while (!pq.empty()) {
ll y = pq.top().S;
pq.pop();
ll x = (y - 1 + n) % n, z = (y + 1) % n;
ll s = b[x] + b[z];
/*
rep(i,n)cout<<b[i]<<' ';
cout<<endl;
cout<<y<<' '<<b[y]<<' '<<s<<endl;
*/
if (b[y] <= s)
continue;
if ((b[y] - a[y]) % s == 0) {
ans += (b[y] - a[y]) / s;
b[y] = a[y];
} else {
ans += b[y] / s;
b[y] %= s;
if (b[y] < b[x] || b[y] < b[z]) {
pq.push(mp(b[y], y));
}
}
}
rep(i, n) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
print(ans);
}
| replace | 181 | 182 | 181 | 182 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int N = (int)2e5 + 9;
int a[N], b[N];
bool is[N];
int n;
priority_queue<pii> tf;
void check(int x) {
if (is[x]) {
return;
}
if (b[x] > a[x] && b[x] > b[(x - 1 + n) % n] + b[(x + 1) % n]) {
tf.push(mp(b[x], x));
is[x] = true;
}
}
int main() {
fastIO;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
check(i);
}
ll res = 0;
int idx;
ll dd;
while (!tf.empty()) {
idx = tf.top().se;
tf.pop();
is[idx] = false;
dd = b[idx] - a[idx];
res += dd / (b[(idx + 1) % n] + b[(idx - 1 + n) % n]);
b[idx] = a[idx] + (dd % (b[(idx + 1) % n] + b[(idx - 1 + n) % n]));
check(idx);
check((idx - 1 + n) % n);
check((idx + 1) % n);
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1;
return 0;
}
}
cout << res << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
const int N = (int)2e5 + 9;
int a[N], b[N];
bool is[N];
int n;
priority_queue<pii> tf;
void check(int x) {
if (is[x]) {
return;
}
if (b[x] > a[x] && b[x] > b[(x - 1 + n) % n] + b[(x + 1) % n]) {
tf.push(mp(b[x], x));
is[x] = true;
}
}
int main() {
fastIO;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
check(i);
}
ll res = 0;
int idx;
ll dd;
while (!tf.empty()) {
idx = tf.top().se;
tf.pop();
is[idx] = false;
dd = b[idx] - a[idx];
res += dd / (b[(idx + 1) % n] + b[(idx - 1 + n) % n]);
b[idx] = a[idx] + (dd % (b[(idx + 1) % n] + b[(idx - 1 + n) % n]));
check((idx - 1 + n) % n);
check((idx + 1) % n);
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1;
return 0;
}
}
cout << res << "\n";
return 0;
}
| delete | 54 | 55 | 54 | 54 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl '\n'
#define PI 3.14159265359d
#define sz(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define magic 109
#define INF INT_MAX
using namespace std;
const int maxn = 200000;
int n;
int a[maxn], b[maxn];
priority_queue<int> pq;
void check_insert(int i) {
int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
if (b[i] - a[i] >= b[i_prev] + b[i_next]) {
pq.push(i);
}
}
void solve() {
cin >> n;
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
int res = 0;
rep(i, 0, n) { check_insert(i); }
while (!pq.empty()) {
int i = pq.top();
pq.pop();
int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
int k = (b[i] - a[i]) / (b[i_prev] + b[i_next]);
b[i] -= k * (b[i_prev] + b[i_next]);
res += k;
check_insert(i_prev);
check_insert(i);
check_insert(i_next);
}
rep(i, 0, n) if (a[i] != b[i]) {
cout << -1;
return;
}
cout << res;
}
signed main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl '\n'
#define PI 3.14159265359d
#define sz(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define magic 109
#define INF INT_MAX
using namespace std;
const int maxn = 200000;
int n;
int a[maxn], b[maxn];
priority_queue<int> pq;
void check_insert(int i) {
int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
if (b[i] - a[i] >= b[i_prev] + b[i_next]) {
pq.push(i);
}
}
void solve() {
cin >> n;
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
int res = 0;
rep(i, 0, n) { check_insert(i); }
while (!pq.empty()) {
int i = pq.top();
pq.pop();
int i_prev = (i + n - 1) % n, i_next = (i + 1) % n;
int k = (b[i] - a[i]) / (b[i_prev] + b[i_next]);
b[i] -= k * (b[i_prev] + b[i_next]);
res += k;
check_insert(i_prev);
check_insert(i);
check_insert(i_next);
}
rep(i, 0, n) if (a[i] != b[i]) {
cout << -1;
return;
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
} | replace | 54 | 60 | 54 | 55 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
signed main() {
int n;
cin >> n;
vector<i64> a(n), v(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> v[i];
priority_queue<pair<i64, int>> que;
for (int i = 0; i < n; ++i)
que.emplace(v[i], i);
i64 ans = 0;
while (!que.empty()) {
i64 val;
int i;
tie(val, i) = que.top();
que.pop();
i64 su = v[(i + 1) % n] + v[(i + n - 1) % n];
i64 ma = max({v[(i + 1) % n], v[(i + n - 1) % n], a[i]});
i64 diff = val - ma;
i64 cnt = (diff + su - 1) / su;
ans += cnt;
val -= su * cnt;
v[i] = val;
if (val > a[i])
que.emplace(val, i);
}
cout << (a == v ? ans : -1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 MOD = 998244353;
const i64 INF = i64(1e18) + 7;
template <typename T> bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T> bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
signed main() {
int n;
cin >> n;
vector<i64> a(n), v(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> v[i];
priority_queue<pair<i64, int>> que;
for (int i = 0; i < n; ++i)
que.emplace(v[i], i);
i64 ans = 0;
while (!que.empty()) {
i64 val;
int i;
tie(val, i) = que.top();
que.pop();
i64 su = v[(i + 1) % n] + v[(i + n - 1) % n];
i64 ma = max({v[(i + 1) % n], v[(i + n - 1) % n], a[i]});
i64 diff = val - ma;
i64 cnt = (diff + su - 1) / su;
ans += cnt;
val -= su * cnt;
v[i] = val;
if (val > a[i])
que.emplace(val, i);
if (val < a[i]) {
cout << -1 << endl;
return 0;
}
}
cout << (a == v ? ans : -1) << endl;
}
| insert | 52 | 52 | 52 | 56 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<int> a(n), b(n);
set<pii> sa, sb;
f(i, 0, n) {
cin >> a[i];
sa.insert({a[i], i});
}
f(i, 0, n) {
cin >> b[i];
sb.insert({b[i], i});
}
vector<int> val1(n, 0), val2(n, 0);
while (!sa.empty()) {
auto k = sa.end();
k--;
sa.erase(k);
int in = (*k).S;
int sm = a[(in - 1 + n) % n] + a[(in + 1) % n];
if (!sm)
continue;
int kk = a[in] / sm;
if (kk) {
a[in] -= kk * sm;
val1[in] += kk;
sa.insert({a[in], in});
}
}
while (!sb.empty()) {
auto k = sb.end();
k--;
sb.erase(k);
int in = (*k).S;
int sm = b[(in - 1 + n) % n] + b[(in + 1) % n];
if (!sm)
continue;
int kk = b[in] / sm;
if (kk) {
b[in] -= kk * sm;
val2[in] += kk;
sb.insert({b[in], in});
}
}
int ans = 0;
f(i, 0, n) {
if (a[i] != b[i]) {
cout << "-1\n";
return 0;
}
ans += val2[i];
ans -= val1[i];
}
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int n;
cin >> n;
vector<int> a(n), b(n);
set<pii> sa, sb;
f(i, 0, n) {
cin >> a[i];
sa.insert({a[i], i});
}
f(i, 0, n) {
cin >> b[i];
sb.insert({b[i], i});
}
vector<int> val1(n, 0), val2(n, 0);
while (!sa.empty()) {
auto k = sa.end();
k--;
sa.erase(k);
int in = (*k).S;
int sm = a[(in - 1 + n) % n] + a[(in + 1) % n];
if (!sm)
continue;
int kk = a[in] / sm;
if (kk) {
a[in] -= kk * sm;
val1[in] += kk;
sa.insert({a[in], in});
}
}
while (!sb.empty()) {
auto k = sb.end();
k--;
sb.erase(k);
int in = (*k).S;
int sm = b[(in - 1 + n) % n] + b[(in + 1) % n];
if (!sm)
continue;
int kk = b[in] / sm;
if (kk) {
b[in] -= kk * sm;
val2[in] += kk;
sb.insert({b[in], in});
}
}
int ans = 0;
f(i, 0, n) {
if (a[i] != b[i]) {
cout << "-1\n";
return 0;
}
ans += val2[i];
ans -= val1[i];
}
cout << ans << '\n';
return 0;
} | replace | 17 | 21 | 17 | 21 | 0 | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef stack<int> S;
const int N = 200200;
int n;
int a[N];
int b[N];
S s;
void gg() {
puts("-1");
exit(0);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i < n; i++) {
s.push(i);
}
LL ans = 0;
while (!s.empty()) {
int x = s.top();
s.pop();
int l = (x - 1 + n) % n;
int r = (x + 1) % n;
LL db = b[l] + b[r];
LL da = b[x] - a[x];
if (b[x] <= db) {
continue;
}
int o = da / db;
if (o == 0) {
continue;
}
ans += o;
b[x] -= o * db;
assert(b[x] >= a[x]);
s.push(l);
s.push(r);
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
gg();
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef stack<int> S;
const int N = 200200;
int n;
int a[N];
int b[N];
S s;
void gg() {
puts("-1");
exit(0);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (b[i] < a[i]) {
gg();
}
}
for (int i = 0; i < n; i++) {
s.push(i);
}
LL ans = 0;
while (!s.empty()) {
int x = s.top();
s.pop();
int l = (x - 1 + n) % n;
int r = (x + 1) % n;
LL db = b[l] + b[r];
LL da = b[x] - a[x];
if (b[x] <= db) {
continue;
}
int o = da / db;
if (o == 0) {
continue;
}
ans += o;
b[x] -= o * db;
assert(b[x] >= a[x]);
s.push(l);
s.push(r);
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
gg();
}
}
cout << ans << endl;
return 0;
} | insert | 20 | 20 | 20 | 23 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
void solve() {}
int a[200009], b[200009];
bool vis[200009];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;
cin >> n;
rep(i, n) cin >> a[i];
priority_queue<pii> q;
ll ans = 0;
rep(i, n) {
int x;
cin >> x;
b[i] = x;
if (b[i] == a[i])
vis[i] = 1;
else
q.push(mp(x, i));
}
while (!q.empty()) {
pii z = q.top();
q.pop();
int i = z.snd, j = (i + n - 1) % n, k = (i + 1) % n;
if (z.fst - b[j] - b[k] < a[i]) {
cout << -1;
return 0;
}
int lst = a[i];
if ((!vis[j] || !vis[k]) && sz(q))
lst = max(lst, q.top().fst - 1);
int d = (z.fst - lst + b[j] + b[k] - 1) / (b[j] + b[k]);
d = max(1, d);
ans += d;
b[i] -= d * (b[j] + b[k]);
if (b[i] < a[i]) {
cout << -1;
return 0;
} else if (b[i] == a[i])
vis[i] = 1;
else
q.push(mp(b[i], i));
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
void solve() {}
int a[200009], b[200009];
bool vis[200009];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;
cin >> n;
rep(i, n) cin >> a[i];
priority_queue<pii> q;
ll ans = 0;
rep(i, n) {
int x;
cin >> x;
b[i] = x;
if (b[i] == a[i])
vis[i] = 1;
else
q.push(mp(x, i));
}
while (!q.empty()) {
pii z = q.top();
q.pop();
int i = z.snd, j = (i + n - 1) % n, k = (i + 1) % n;
if (z.fst - b[j] - b[k] < a[i]) {
cout << -1;
return 0;
}
int lst = a[i];
if (!vis[j])
lst = max(lst, b[j] - 1);
if (!vis[k])
lst = max(lst, b[k] - 1);
int d = (z.fst - lst + b[j] + b[k] - 1) / (b[j] + b[k]);
d = max(1, d);
ans += d;
b[i] -= d * (b[j] + b[k]);
if (b[i] < a[i]) {
cout << -1;
return 0;
} else if (b[i] == a[i])
vis[i] = 1;
else
q.push(mp(b[i], i));
}
cout << ans;
return 0;
}
| replace | 69 | 71 | 69 | 73 | TLE | |
p02941 | C++ | Time Limit Exceeded | /*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o:
.-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
`+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -`
:+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+`
`-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -`
`+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-`
`:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-`
`oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-`
.-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-.
````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `-
`.:--h.``..```
-.-`.- .- `+:--..`````..--//` `-
/s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.-
`//:--------://`
-` .-`.-`
-.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `-
``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `-
`:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o.
`.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `...
``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define umap unordered_map
#define uset unordered_set
#define elif else if
#define len(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act) \
for (auto &x : v) { \
act; \
}
#define log(args...) \
{ \
string s = #args; \
deque<string> deq; \
string buf = ""; \
int bal = 0; \
for (char c : s) { \
if (c == '(' || c == '[' || c == '{') { \
bal++; \
} else if (c == ')' || c == ']' || c == '}') { \
bal--; \
} else { \
if (bal == 0) { \
if (c == ',') { \
deq.pb(buf); \
buf = ""; \
} else { \
if (c != ' ') { \
buf += c; \
} \
} \
} \
} \
} \
if (!buf.empty()) { \
deq.pb(buf); \
} \
smart_io::precall_print(); \
smart_io::_print(deq, args); \
}
inline int min(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x;
}
inline int max(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
inline long long min(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x;
}
inline long long max(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y;
}
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
_ptr += _x;
assert(_ptr < MAXMEM);
return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T> void fast_scan(vector<T> &v) {
for (auto &x : v)
fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const char &x) { printf("%c", x); };
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T> void fast_print(const vector<T> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T> void fast_print(const vector<vector<T>> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T> void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T> ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T> istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
}; // namespace random
#define int long long
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs
namespace numbers_operation {
template <class T> T floor_mod(T a, T b) {
if (a >= 0 && b >= 0)
return a % b;
if (a <= 0 && b <= 0)
return a % b;
return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
int n;
vector<int> a, b;
signed main(signed argc, char *argv[]) {
scan n;
a.resize(n);
b.resize(n);
scan a, b;
auto cmp = [](int i, int j) { return b[i] < b[j]; };
priority_queue<int, vector<int>, decltype(cmp)> deq(cmp);
for (int i = 0; i < n; i++)
deq.push(i);
ll rez = 0;
while (!deq.empty()) {
int i = deq.top();
deq.pop();
int j = (i + 1) % n;
int k = (i - 1 + n) % n;
if (b[i] >= a[i] && b[i] >= b[j] && b[i] >= b[k]) {
int c = (b[i] - a[i]) / (b[j] + b[k]);
b[i] -= c * (b[j] + b[k]);
rez += c;
deq.push(j);
deq.push(k);
}
}
if (b != a) {
print - 1;
} else {
print rez;
}
}
| /*
`-:://:::-
`//:-------:/:`
.+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.o:--...```..---+/`
`/y+o/---....---:+o.
`...````-os+/:---:/+o/--.`
`-/+++++/:. `...` :h+d+oooo+/+-` ...
`/++//:::://++-`....` -.`//````````:` `..`
`o+/::------://o/` `-` -. -` `..`
`---.-o/:./o/::-..``..-ЗАПУСКАЕМ .. .. -` `... ``..``
`....o+:-++/:--.```..-://s. `-` .- -` `-o:
.-//::::/:-`
`:s+/:--....-::/+s-` .- `- -` -///:--------:/:`
./s+//:::::://oo-``..НЕЙРОННУЮ: СЕТЬ:::::::-`РАБОТЯГИ
`+:--........--:/`
.:ooo+++++osso-` `.:-...`/` ./::-------:/:` -`
:+--..``````.--:+:...-+:-`
`.-/+++++/+-.-` -. ``:so:/:--.......--:+`
`-```````o+/+--..`````..--:o/-..:s+:.
```````:``.. `-` -`
`+:--..`````..--/+-.../.`````..-o:--.......---/o. `
`: `:- -. .o:--..`` ``..--:o` `-`
`:o+:--------:+o-`
`-`-... .. .o/--...```..--:+/` `-`
`oy/so/////++o/.`
-/` `-` `- ``+s/o/:---...---:++. `-`
.-../d://///:-.`
`.---..``-..- .-/..`````-oo+/:::::/+o+- `-``-` `-.
````
`:++++/+++++- ..``.-/:` /y-:/++o++/:.`..` ./. `-
-++/::::::://+/..:-``:` .. `-.` ```.``` `..` `..`-` `-
`` -o//:--....-::/++` -.-` `-`.-` `..`..` `-.-
-----ss+:++/:--.```..-://s. /. `:: `-:. ./`
`````/:..+o/::-..``.--:/+s. ..-` `-``-` ..` `-` `-`-`
`-s+/::-----::/+oo---``-` .. .:- ``` .-` .-.- `-`
`:oo+//::://+os/..:`..-/:` :y.-:::::::.`.-` ./-` `-`
`./+oooooooo+/.`- .-:...`.. .//:-------://` `- `..` `:.
``.-::::-.``-/` `-` `- `oo:+:--.......--:/` `-
`.:--h.``..```
-.-`.- .- `+:--..`````..--//` `-
/s-//::::::::.
-` `/- .. .o:--..`` ``..--:o.```.-
`//:--------://`
-` .-`.-`
-.`-o/--...```..--:+/.``-:....``:-.+:--....`...--:+`
..`-. `-. ``:os:o/:---...---:++. `-
``///+:-..``````.--:+-````-.`
`.:///////.-` .:-..` -``-+o+/:::::/+o/. `-
`:+:-..`````..--:o/:--/ys+-
`-++///////+o/. ``....`-. :` `.:++++++/:.` .- -o/---......---/o.
`.`
`++//:-----::/+o:..` .-` : ``````` .- `+so+:--------:++-`
`````:-``:o/::-..`..--:/+o` -. `- .- `../../+o+////+o+:.`
-----syo/o+/:--.```..-://s. .-` `- .- `...
``-:////:-``
.` `/s//:--....-::/+s. -. `-` .- `..`
.+o+/:::--:://+s/-..` .::+y ``` .- `..`
./oo++////+oso-` `.... :y-+:::::::/` ...
`.:+oooooo/-` `....-. .//:-------:/:-.`
``...`` /+:+:--.......--:+`
`+:--..`````..--//`
.o:--..`` ``..--:o`
.+/--...```..--:+/`
`-o/:---...---:++.
`-+o+/:---:/+o/.
`.:+oooo+/-.`
``````
*/
#ifdef aimbot
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#endif
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define random escape__from__random__aetuhoetnuhshe
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define umap unordered_map
#define uset unordered_set
#define elif else if
#define len(v) ((int)v.size())
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n)-1); i >= 0; i--)
#define apply(v, act) \
for (auto &x : v) { \
act; \
}
#define log(args...) \
{ \
string s = #args; \
deque<string> deq; \
string buf = ""; \
int bal = 0; \
for (char c : s) { \
if (c == '(' || c == '[' || c == '{') { \
bal++; \
} else if (c == ')' || c == ']' || c == '}') { \
bal--; \
} else { \
if (bal == 0) { \
if (c == ',') { \
deq.pb(buf); \
buf = ""; \
} else { \
if (c != ' ') { \
buf += c; \
} \
} \
} \
} \
} \
if (!buf.empty()) { \
deq.pb(buf); \
} \
smart_io::precall_print(); \
smart_io::_print(deq, args); \
}
inline int min(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x;
}
inline int max(const int &x, const int &y) {
return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
inline long long min(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ x;
}
inline long long max(const long long &x, const long long &y) {
return (((y - x) >> (64 - 1)) & (x ^ y)) ^ y;
}
#define print \
smart_io::precall_print(); \
cout,
#define scan cin,
#ifdef fast_allocator
const int MAXMEM = 200 * 1000 * 1024;
char _memory[MAXMEM];
size_t _ptr = 0;
void *operator new(size_t _x) {
_ptr += _x;
assert(_ptr < MAXMEM);
return _memory + _ptr - _x;
}
void operator delete(void *) noexcept {}
#endif
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &x) { scanf("%d", &x); }
void fast_scan(long long &x) { scanf("%lld", &x); }
void fast_scan(unsigned long long &x) { scanf("%llu", &x); }
void fast_scan(double &x) { scanf("%lf", &x); }
void fast_scan(long double &x) { scanf("%Lf", &x); }
void fast_scan(char &x) {
scanf("%c", &x);
if (x == '\n') {
fast_scan(x);
}
}
void fast_scan(string &x) {
scanf("%s", string_in_buffer);
x = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T> void fast_scan(vector<T> &v) {
for (auto &x : v)
fast_scan(x);
}
void fast_print(const int &x) { printf("%d", x); }
void fast_print(const unsigned int &x) { printf("%u", x); }
void fast_print(const long long &x) { printf("%lld", x); }
void fast_print(const unsigned long long &x) { printf("%llu", x); }
void fast_print(const double &x) { printf("%.15lf", x); }
void fast_print(const long double &x) { printf("%.15Lf", x); }
void fast_print(const char &x) { printf("%c", x); };
void fast_print(const string &x) { printf("%s", x.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T> void fast_print(const vector<T> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T> void fast_print(const vector<vector<T>> &v) {
if (v.empty())
return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T> void fast_print(const T &v) {
for (const auto &x : v) {
fast_print(x);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T> ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T> istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace random {
using namespace std::chrono;
mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count());
uniform_real_distribution<> prob_dist(0.0, 1.0);
}; // namespace random
#define int long long
namespace typedefs {
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long double ld;
} // namespace typedefs
namespace numbers_operation {
template <class T> T floor_mod(T a, T b) {
if (a >= 0 && b >= 0)
return a % b;
if (a <= 0 && b <= 0)
return a % b;
return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
using namespace random;
int n;
vector<int> a, b;
signed main(signed argc, char *argv[]) {
scan n;
a.resize(n);
b.resize(n);
scan a, b;
auto cmp = [](int i, int j) { return b[i] < b[j]; };
priority_queue<int, vector<int>, decltype(cmp)> deq(cmp);
for (int i = 0; i < n; i++)
deq.push(i);
ll rez = 0;
while (!deq.empty()) {
int i = deq.top();
deq.pop();
int j = (i + 1) % n;
int k = (i - 1 + n) % n;
if (b[i] >= a[i] && b[i] >= b[j] && b[i] >= b[k]) {
int c = (b[i] - a[i]) / (b[j] + b[k]);
if (c == 0)
continue;
b[i] -= c * (b[j] + b[k]);
rez += c;
deq.push(j);
deq.push(k);
}
}
if (b != a) {
print - 1;
} else {
print rez;
}
}
| insert | 353 | 353 | 353 | 355 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define pb push_back
#define fi first
#define se second
typedef double lf;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 998244353;
int add(int x, int y) {
x += y;
if (x >= mod)
return x - mod;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0)
return x + mod;
return x;
}
int mul(int x, int y) { return (ll)x * y % mod; }
const int MAXN = 2e5 + 5;
int a[MAXN], f[MAXN];
int n;
int M(int i) { return (i + n) % n; }
bool check(int i) { return a[i] > a[M(i + 1)] && a[i] > a[M(i - 1)]; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
REP(i, n) cin >> f[i];
REP(i, n) cin >> a[i];
vector<int> good;
REP(i, n) {
if (check(i)) {
good.pb(i);
}
}
ll sol = 0;
while (good.size()) {
int i = good.back();
good.pop_back();
int sums = a[M(i + 1)] + a[M(i - 1)];
int times =
max(0, (a[i] - max(f[i], max(a[M(i + 1)], a[M(i - 1)])) - 1)) / sums;
times++;
sol += times;
a[i] -= times * sums;
if (check(M(i + 1)))
good.pb(M(i + 1));
if (check(M(i - 1)))
good.pb(M(i - 1));
}
REP(i, n) if (a[i] != f[i]) {
cout << -1 << endl;
return 0;
}
cout << sol << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define pb push_back
#define fi first
#define se second
typedef double lf;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 998244353;
int add(int x, int y) {
x += y;
if (x >= mod)
return x - mod;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0)
return x + mod;
return x;
}
int mul(int x, int y) { return (ll)x * y % mod; }
const int MAXN = 2e5 + 5;
int a[MAXN], f[MAXN];
int n;
int M(int i) { return (i + n) % n; }
bool check(int i) { return a[i] > a[M(i + 1)] && a[i] > a[M(i - 1)]; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
REP(i, n) cin >> f[i];
REP(i, n) cin >> a[i];
vector<int> good;
REP(i, n) {
if (check(i)) {
good.pb(i);
}
}
ll sol = 0;
while (good.size()) {
int i = good.back();
good.pop_back();
int sums = a[M(i + 1)] + a[M(i - 1)];
int times =
max(0, (a[i] - max(f[i], max(a[M(i + 1)], a[M(i - 1)])) - 1)) / sums;
if (a[i] == f[i])
times--;
times++;
sol += times;
a[i] -= times * sums;
if (check(M(i + 1)))
good.pb(M(i + 1));
if (check(M(i - 1)))
good.pb(M(i - 1));
}
REP(i, n) if (a[i] != f[i]) {
cout << -1 << endl;
return 0;
}
cout << sol << endl;
}
| insert | 62 | 62 | 62 | 65 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
#define PI (acos(-1))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAXN 1123456
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vi a(n), b(n);
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
ll c = 0;
priority_queue<pii> pq;
rep(i, 0, n) pq.push({b[i], i});
bool good = true;
while (!pq.empty()) {
int wtf = pq.top().fst;
int m = pq.top().snd;
pq.pop();
if (b[m] != wtf)
continue;
if (b[m] == a[m])
continue;
int m1 = -1;
if (!pq.empty())
m1 = pq.top().snd;
int y = a[m];
if (m1 >= 0)
y = max(y, b[m1]);
int sum = b[(m - 1 + n) % n] + b[(m + 1) % n];
int k = ((b[m] - y + sum - 1) / sum);
b[m] -= k * sum;
c += k;
if (b[m] != a[m])
pq.push({b[m], m});
// cerr << m << " " << b[m] << endl;
if (b[m] < a[m])
goto fail;
}
rep(i, 0, n) {
if (a[i] != b[i]) {
good = false;
if (b[i] < a[i])
goto fail;
}
}
if (good)
cout << c << endl;
else
cout << -1 << endl;
if (good)
return 0;
fail:
cout << -1 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
#define PI (acos(-1))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAXN 1123456
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vi a(n), b(n);
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
ll c = 0;
priority_queue<pii> pq;
rep(i, 0, n) pq.push({b[i], i});
bool good = true;
while (!pq.empty()) {
int wtf = pq.top().fst;
int m = pq.top().snd;
pq.pop();
if (b[m] != wtf)
continue;
if (b[m] == a[m])
continue;
int y = max(a[m], max(b[(m - 1 + n) % n], b[(m + 1) % n]));
int sum = b[(m - 1 + n) % n] + b[(m + 1) % n];
int k = ((b[m] - y + sum - 1) / sum);
b[m] -= k * sum;
c += k;
if (b[m] != a[m])
pq.push({b[m], m});
// cerr << m << " " << b[m] << endl;
if (b[m] < a[m])
goto fail;
}
rep(i, 0, n) {
if (a[i] != b[i]) {
good = false;
if (b[i] < a[i])
goto fail;
}
}
if (good)
cout << c << endl;
else
cout << -1 << endl;
if (good)
return 0;
fail:
cout << -1 << endl;
}
| replace | 49 | 55 | 49 | 50 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<ll, ll> PL;
typedef vector<PL> vpl;
typedef vector<vector<PL>> vvpl;
typedef vector<vvpl> vvvpl;
typedef pair<ll, PL> TL;
typedef vector<TL> vtl;
typedef vector<vtl> vvtl;
typedef long double ld;
typedef vector<ld> vld;
const ll LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for (ll i = (a); i < b; i++)
#define RFOR(i, a, b) for (ll i = (a); i > b; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back
#define ALL(X) (X).begin(), (X).end()
template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; };
bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; };
bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; };
bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; };
template <typename T> vector<T> uniquen(vector<T> vec /* copy */) {
sort(ALL(vec));
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
inline ll popcnt(ll x) { return __builtin_popcountll((unsigned long long)x); };
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// friend bool operator<(const tpl& lhs, const tpl& rhs){
// return std::tie(lhs.l, lhs.r) < std::tie(rhs.l, rhs.r);
// }
bool bit(ll st, ll b) { return ((st >> b) & 1) == 1; }
int main() {
ifstream file("../../../../../in.txt");
if (file.is_open())
cin.rdbuf(file.rdbuf());
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
vl A(N);
rep(i, N) cin >> A[i];
vl B(N);
rep(i, N) cin >> B[i];
set<PL, greater<PL>> s;
rep(i, N) {
if (B[i] != A[i])
s.insert({B[i], i});
}
if (s.empty()) {
cout << 0 << endstr;
return 0;
}
ll cnt = 0;
while (!s.empty()) {
ll idx = s.begin()->ss;
ll l = (idx - 1 + N) % N, r = (idx + 1 + N) % N;
if (s.size() < 2) {
ll dif = B[idx] - A[idx], div = B[l] + B[r];
if (dif % div != 0) {
cout << -1 << endstr;
return 0;
} else {
cnt += dif / div;
cout << cnt << endstr;
return 0;
}
} else {
auto nmx = next(s.begin())->ff;
if (A[idx] >= nmx) {
ll dif = B[idx] - A[idx], div = B[l] + B[r];
if (dif % div != 0) {
cout << -1 << endstr;
return 0;
} else {
cnt += dif / div;
B[idx] = A[idx];
s.erase(s.begin());
}
} else {
ll dif = B[idx] - nmx, div = B[l] + B[r];
ll add = (dif + div - 1) / div;
cnt += add;
B[idx] -= add * div;
if (B[idx] < A[idx]) {
cout << -1 << endstr;
return 0;
}
s.erase(s.begin());
if (B[idx] != A[idx]) {
s.insert({B[idx], idx});
}
}
}
}
cout << -1 << endstr;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<ll, ll> PL;
typedef vector<PL> vpl;
typedef vector<vector<PL>> vvpl;
typedef vector<vvpl> vvvpl;
typedef pair<ll, PL> TL;
typedef vector<TL> vtl;
typedef vector<vtl> vvtl;
typedef long double ld;
typedef vector<ld> vld;
const ll LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for (ll i = (a); i < b; i++)
#define RFOR(i, a, b) for (ll i = (a); i > b; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define FORMAP(it, m) for (auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back
#define ALL(X) (X).begin(), (X).end()
template <typename T> T gcd(T a, T b) { return (a == 0) ? b : gcd(b % a, a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
bool p_comp_fs(const PL p1, const PL p2) { return p1.first < p2.first; };
bool p_comp_fg(const PL p1, const PL p2) { return p1.first > p2.first; };
bool p_comp_ss(const PL p1, const PL p2) { return p1.second < p2.second; };
bool p_comp_sg(const PL p1, const PL p2) { return p1.second > p2.second; };
template <typename T> vector<T> uniquen(vector<T> vec /* copy */) {
sort(ALL(vec));
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
inline ll popcnt(ll x) { return __builtin_popcountll((unsigned long long)x); };
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// friend bool operator<(const tpl& lhs, const tpl& rhs){
// return std::tie(lhs.l, lhs.r) < std::tie(rhs.l, rhs.r);
// }
bool bit(ll st, ll b) { return ((st >> b) & 1) == 1; }
int main() {
ifstream file("../../../../../in.txt");
if (file.is_open())
cin.rdbuf(file.rdbuf());
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
vl A(N);
rep(i, N) cin >> A[i];
vl B(N);
rep(i, N) cin >> B[i];
set<PL, greater<PL>> s;
rep(i, N) {
if (B[i] != A[i])
s.insert({B[i], i});
}
if (s.empty()) {
cout << 0 << endstr;
return 0;
}
ll cnt = 0;
while (!s.empty()) {
ll idx = s.begin()->ss;
ll l = (idx - 1 + N) % N, r = (idx + 1 + N) % N;
if (s.size() < 2) {
ll dif = B[idx] - A[idx], div = B[l] + B[r];
if (dif % div != 0) {
cout << -1 << endstr;
return 0;
} else {
cnt += dif / div;
cout << cnt << endstr;
return 0;
}
} else {
ll to = max(B[l] + B[r], A[idx]);
ll dif = B[idx] - to;
ll div = B[l] + B[r];
ll add = (dif + div - 1) / div;
if (add <= 0) {
cout << -1 << endstr;
return 0;
}
cnt += add;
B[idx] -= add * div;
if (B[idx] < A[idx]) {
cout << -1 << endstr;
return 0;
}
s.erase(s.begin());
if (B[idx] != A[idx]) {
s.insert({B[idx], idx});
}
}
}
cout << -1 << endstr;
return 0;
}
| replace | 136 | 160 | 136 | 153 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#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;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
os << "(" << v.first << ", " << v.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << " ";
}
os << v[i];
}
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << endl;
}
os << v[i];
}
return os;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cerr << v[i][0];
for (ll j = 1; j < w; j++)
cerr spa v[i][j];
cerr << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cerr << v[0];
for (ll i = 1; i < n; i++)
cerr spa v[i];
cerr << endl;
};
string num2bit(ll num, ll len) {
string bit = "";
REP(i, len) { bit += char('0' + (num >> i & 1)); }
return bit;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> A(N), B(N);
REP(i, N) cin >> A[i];
REP(i, N) cin >> B[i];
priority_queue<pll> pq;
REP(i, N) if (B[i] > A[i]) pq.push({B[i], i});
ll cnt = 0;
while (!pq.empty()) {
// cout << B << endl;
pll p = pq.top();
pq.pop();
ll idx = p.second;
ll left = B[(idx + 1) % N];
ll right = B[(idx - 1 + N) % N];
ll waru = left + right;
ll r1 = (B[idx] - A[idx]) / waru;
ll r2 = (B[idx] - left - right + waru) / waru;
ll r = min(r1, r2);
B[idx] -= r * waru;
cnt += r;
if (B[idx] < A[idx]) {
cout << -1 << endl;
return 0;
} else if (B[idx] > A[idx]) {
pq.push({B[idx], idx});
}
}
bool flag = true;
REP(i, N) if (B[i] != A[i]) flag = false;
cout << (flag ? cnt : -1) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
// typedef vector<vector<ll>> Graph;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#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;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
os << "(" << v.first << ", " << v.second << ")";
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << " ";
}
os << v[i];
}
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
for (int i = 0; i < (int)v.size(); i++) {
if (i > 0) {
os << endl;
}
os << v[i];
}
return os;
}
template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
for (ll i = 0; i < h; i++) {
cerr << v[i][0];
for (ll j = 1; j < w; j++)
cerr spa v[i][j];
cerr << endl;
}
};
template <typename T> void debug(vector<T> &v, ll n) {
if (n != 0)
cerr << v[0];
for (ll i = 1; i < n; i++)
cerr spa v[i];
cerr << endl;
};
string num2bit(ll num, ll len) {
string bit = "";
REP(i, len) { bit += char('0' + (num >> i & 1)); }
return bit;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> A(N), B(N);
REP(i, N) cin >> A[i];
REP(i, N) cin >> B[i];
priority_queue<pll> pq;
REP(i, N) if (B[i] > A[i]) pq.push({B[i], i});
ll cnt = 0;
while (!pq.empty()) {
// cout << B << endl;
pll p = pq.top();
pq.pop();
ll idx = p.second;
ll left = B[(idx + 1) % N];
ll right = B[(idx - 1 + N) % N];
ll waru = left + right;
ll r1 = (B[idx] - A[idx]) / waru;
ll r2 = (B[idx] - left - right + waru) / waru;
ll r = min(r1, r2);
B[idx] -= r * waru;
cnt += r;
if (B[idx] < A[idx]) {
cout << -1 << endl;
return 0;
} else if (B[idx] > A[idx] and r != 0) {
pq.push({B[idx], idx});
}
}
bool flag = true;
REP(i, N) if (B[i] != A[i]) flag = false;
cout << (flag ? cnt : -1) << endl;
return 0;
} | replace | 113 | 114 | 113 | 114 | TLE | |
p02941 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
void read(int &x) {
char ch;
bool ok;
for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
ok = 1;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
if (ok)
x = -x;
}
#define rg register
const int maxn = 6e5 + 10;
struct oo {
int x, y;
};
priority_queue<oo> q;
int n, a[maxn], b[maxn], flag;
long long ans;
bool operator<(oo u, oo v) { return u.y > v.y; }
int main() {
read(n);
for (rg int i = 0; i < n; i++)
read(a[i]);
for (rg int i = 0; i < n; i++)
read(b[i]);
for (rg int i = 0; i < n; i++)
if (b[i] >= b[(i + 1) % n] + b[(i - 1 + n) % n])
q.push((oo){i, b[i]});
while (q.size()) {
int i = q.top().x, x = (i - 1 + n) % n, y = (i + 1) % n, now = b[x] + b[y];
ans += b[i] / now, b[i] %= now;
q.pop();
if (b[x] >= b[(x + 1) % n] + b[(x - 1 + n) % n])
q.push((oo){x, b[x]});
if (b[y] >= b[(y - 1 + n) % n] + b[(y + 1) % n])
q.push((oo){y, b[y]});
}
for (rg int i = 1; i <= n; i++)
if (a[i] != b[i])
flag = 1;
printf("%lld\n", flag ? -1 : ans);
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
void read(int &x) {
char ch;
bool ok;
for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
ok = 1;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
if (ok)
x = -x;
}
#define rg register
const int maxn = 6e5 + 10;
struct oo {
int x, y;
};
priority_queue<oo> q;
int n, a[maxn], b[maxn], flag;
long long ans;
bool operator<(oo u, oo v) { return u.y > v.y; }
int main() {
read(n);
for (rg int i = 0; i < n; i++)
read(a[i]);
for (rg int i = 0; i < n; i++)
read(b[i]);
for (rg int i = 0; i < n; i++)
if (b[i] >= b[(i + 1) % n] + b[(i - 1 + n) % n])
q.push((oo){i, b[i]});
while (q.size()) {
int t, i = q.top().x, x = (i - 1 + n) % n, y = (i + 1) % n,
now = b[x] + b[y];
ans += (t = (b[i] - a[i]) / now), b[i] -= t * now;
q.pop();
if (b[x] >= b[(x + 1) % n] + b[(x - 1 + n) % n])
q.push((oo){x, b[x]});
if (b[y] >= b[(y - 1 + n) % n] + b[(y + 1) % n])
q.push((oo){y, b[y]});
}
for (rg int i = 1; i <= n; i++)
if (a[i] != b[i])
flag = 1;
printf("%lld\n", flag ? -1 : ans);
}
| replace | 36 | 38 | 36 | 39 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define N 200010
ll a[N], b[N], n, ans;
void calc() {
for (int i = 0; i < n; i++) {
int j = (i + 1) % n, k = (i + 2) % n;
ll p = b[i] + b[k];
ll cnt = (b[j] - a[j]) / p;
ans += cnt;
b[j] -= cnt * p;
// cout<<p<<" "<<cnt<<" "<<(b[j]-a[j])<<":";
}
// for(int i=0;i<n;i++)cout<<b[i]<<" ";cout<<endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
ans = 0;
for (int i = 0; i < 300; i++)
calc();
bool ok = 1;
for (int i = 0; i < n; i++)
ok &= (a[i] == b[i]);
if (ok)
cout << ans << endl;
else
cout << -1 << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define N 200010
ll a[N], b[N], n, ans;
void calc() {
for (int i = 0; i < n; i++) {
int j = (i + 1) % n, k = (i + 2) % n;
ll p = b[i] + b[k];
ll cnt = (b[j] - a[j]) / p;
ans += cnt;
b[j] -= cnt * p;
// cout<<p<<" "<<cnt<<" "<<(b[j]-a[j])<<":";
}
// for(int i=0;i<n;i++)cout<<b[i]<<" ";cout<<endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
ans = 0;
for (int i = 0; i < 100; i++)
calc();
bool ok = 1;
for (int i = 0; i < n; i++)
ok &= (a[i] == b[i]);
if (ok)
cout << ans << endl;
else
cout << -1 << endl;
return 0;
}
| replace | 26 | 27 | 26 | 27 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define VIN(n, v) \
for (int64_t i = 0; i < (n); i++) { \
cin >> (v).at(i); \
}
using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;
I MOD = pow(10, 9) + 7;
// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }
// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }
int ctoi(char c) { return c - '0'; }
int main() {
I n;
cin >> n;
V a(n), b(n);
VIN(n, a);
VIN(n, b);
REP(i, n) {
if (a.at(i) > b.at(i)) {
cout << -1 << endl;
return 0;
}
I prev;
I next;
if (i == 0) {
prev = n - 1;
next = 1;
} else if (i == n - 1) {
prev = n - 2;
next = 0;
} else {
prev = i - 1;
next = i + 1;
}
if (a.at(i) != b.at(i) && a.at(prev) + a.at(i) + a.at(next) > b.at(i)) {
cout << -1 << endl;
return 0;
}
}
I t = 0;
while (true) {
bool chk = true;
REP(i, n) {
I prev;
I next;
if (i == 0) {
prev = n - 1;
next = 1;
} else if (i == n - 1) {
prev = n - 2;
next = 0;
} else {
prev = i - 1;
next = i + 1;
}
if (b.at(prev) + b.at(next) < b.at(i) && b.at(i) != a.at(i)) {
b.at(i) -= b.at(prev) + b.at(next);
t++;
chk = false;
}
}
if (chk == true)
break;
}
bool chk2 = true;
REP(i, n) {
if (a.at(i) != b.at(i))
chk2 = false;
}
if (chk2) {
cout << t << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define VIN(n, v) \
for (int64_t i = 0; i < (n); i++) { \
cin >> (v).at(i); \
}
using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;
I MOD = pow(10, 9) + 7;
// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }
// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }
int ctoi(char c) { return c - '0'; }
int main() {
I n;
cin >> n;
V a(n), b(n);
VIN(n, a);
VIN(n, b);
REP(i, n) {
if (a.at(i) > b.at(i)) {
cout << -1 << endl;
return 0;
}
I prev;
I next;
if (i == 0) {
prev = n - 1;
next = 1;
} else if (i == n - 1) {
prev = n - 2;
next = 0;
} else {
prev = i - 1;
next = i + 1;
}
if (a.at(i) != b.at(i) && a.at(prev) + a.at(i) + a.at(next) > b.at(i)) {
cout << -1 << endl;
return 0;
}
}
I t = 0;
while (true) {
bool chk = true;
REP(i, n) {
I prev;
I next;
if (i == 0) {
prev = n - 1;
next = 1;
} else if (i == n - 1) {
prev = n - 2;
next = 0;
} else {
prev = i - 1;
next = i + 1;
}
if (b.at(prev) + b.at(next) <= (b.at(i) - a.at(i))) {
t += (b.at(i) - a.at(i)) / (b.at(prev) + b.at(next));
b.at(i) -= (b.at(i) - a.at(i)) / (b.at(prev) + b.at(next)) *
(b.at(prev) + b.at(next));
chk = false;
}
}
if (chk == true)
break;
}
bool chk2 = true;
REP(i, n) {
if (a.at(i) != b.at(i))
chk2 = false;
}
if (chk2) {
cout << t << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| replace | 73 | 76 | 73 | 77 | TLE | |
p02941 | C++ | Time Limit Exceeded | /*
* じょえチャンネル
* 高評価・チャンネル登録よろしくおねがいします!
* https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// here!!!
// define int long long !!!!!
#define int long long
// define int long long !!!!!
#define mod (int)((1e9) + 7)
// constexpr int mod = 998244353ll;
#ifdef int
#define inf (int)(3e18)
#else
#define inf (int)(5e8)
#endif
#define intt long long
#define itn long long
#define P pair<long long, long long>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define rev_rep(i, n) for (int i = n - 1; i >= 0; i--)
#define REV_REP(i, n) for (int i = n; i >= 1; i--)
#define ALL(v) v.begin(), v.end()
#define smallpriority_queue(x) priority_queue<x, vector<x>, greater<x>>
using namespace std;
// Library
// モッドパウ
inline int modpow(int x, int y, int m = mod) {
int res = 1;
while (y) {
if (y & 1) {
res *= x;
res %= m;
}
x = x * x % m;
y /= 2;
}
return res;
}
int mypow(int x, int y) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
}
x = x * x;
y /= 2;
}
return res;
}
// is the number (x) a prime number?
bool prime(int x) {
if (!x || x == 1) {
return false;
}
for (int i = 2; i * i <= x; i++) {
if (!(x % i)) {
return false;
}
}
return true;
}
// saidai-kouyakusuu
inline int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
// number of keta
int keta(int x) {
int ans = 0;
while (x) {
x /= 10;
ans++;
}
return ans;
}
// sum of keta
int ketasum(int x) {
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
inline int lcm(int x, int y) {
int ans = x / gcd(x, y) * y;
return ans;
}
int twobeki(int x) {
int ans = 0;
while (1) {
if (!(x & 1)) {
ans++;
x >>= 1;
} else {
break;
}
}
return ans;
}
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld", i)
#define fendl printf("\n")
int kai(int x, int y) {
int res = 1;
for (int i = x - y + 1; i <= x; i++) {
res *= i;
res %= mod;
}
return res;
}
int comb(int x, int y) {
if (y > x)
return 0;
// cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl;
return kai(x, y) * modpow(kai(y, y), mod - 2) % mod;
}
// Library-End
#define vecin(v) \
for (int i = 0; i < v.size(); i++) \
scanf("%lld", &v[i]);
#define vecout(v) \
{ \
for (int i = 0; i < (int)v.size(); i++) \
printf("%lld ", v[i]); \
printf("\n"); \
}
template <typename T> class kaageSegTree {
protected:
unsigned int n = 1, rank = 0;
std::vector<T> node;
T nodee;
virtual T nodef(const T &, const T &) const = 0;
public:
kaageSegTree(unsigned int m, T init, T nodee) : nodee(nodee) {
while (n < m) {
n *= 2;
rank++;
}
node.resize(2 * n);
for (unsigned int i = n; i < 2 * n; i++)
node[i] = init;
}
kaageSegTree(const std::vector<T> &initvec, T nodee) : nodee(nodee) {
unsigned int m = initvec.size();
while (n < m) {
n *= 2;
rank++;
}
node.resize(2 * n);
for (unsigned int i = n; i < 2 * n; i++) {
if (i - n < m)
node[i] = initvec[i - n];
}
}
virtual void update(int i, T x) {
i += n;
node[i] = x;
while (i != 1) {
i >>= 1;
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
}
virtual T query(int l, int r) {
l += n;
r += n;
T ls = nodee, rs = nodee;
while (l < r) {
if (l & 1) {
ls = nodef(ls, node[l]);
l++;
}
if (r & 1) {
r--;
rs = nodef(node[r], rs);
}
l >>= 1;
r >>= 1;
}
return nodef(ls, rs);
}
virtual T operator[](const int &x) { return node[n + x]; }
void fill(T x) { std::fill(ALL(node), x); }
void print() {
rep(i, n) std::cout << operator[](i) << " ";
std::cout << std::endl;
}
};
class RSQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return lhs + rhs; }
public:
RSQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, 0) {}
RSQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, 0) {}
};
class RMiQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return std::min(lhs, rhs); }
public:
RMiQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, inf) {}
RMiQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, inf) {}
};
class RMaQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return std::max(lhs, rhs); }
public:
RMaQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, -inf) {}
RMaQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, -inf) {}
};
template <typename T, typename U>
class IntervalSegTree : public kaageSegTree<T> {
protected:
using kaageSegTree<T>::n;
using kaageSegTree<T>::rank;
using kaageSegTree<T>::node;
using kaageSegTree<T>::nodef;
using kaageSegTree<T>::nodee;
std::vector<U> lazy;
std::vector<bool> lazyflag;
std::vector<int> width;
virtual void lazyf(U &, const U &) = 0;
virtual void updf(T &, const U &, const unsigned int &) = 0;
void eval(int k) {
for (int i = rank; i > 0; i--) {
int nk = k >> i;
if (lazyflag[nk]) {
updf(node[2 * nk], lazy[nk], width[2 * nk]);
updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]);
if (lazyflag[2 * nk])
lazyf(lazy[2 * nk], lazy[nk]);
else
lazy[2 * nk] = lazy[nk];
if (lazyflag[2 * nk + 1])
lazyf(lazy[2 * nk + 1], lazy[nk]);
else
lazy[2 * nk + 1] = lazy[nk];
lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true;
lazyflag[nk] = false;
}
}
}
public:
IntervalSegTree(unsigned int m, T init, T nodee)
: kaageSegTree<T>(m, init, nodee) {
lazy.resize(2 * n);
lazyflag.resize(2 * n);
width.resize(2 * n);
width[1] = n;
for (unsigned int i = 2; i < 2 * n; i++) {
width[i] = width[i >> 1] >> 1;
}
}
IntervalSegTree(T nodee, const std::vector<T> &initvec)
: kaageSegTree<T>(initvec, nodee) {
lazy.resize(2 * n);
lazyflag.resize(2 * n);
width.resize(2 * n);
width[1] = n;
for (unsigned int i = 2; i < 2 * n; i++) {
width[i] = width[i >> 1] >> 1;
}
}
void update(int i, U x) {
i += n;
eval(i);
updf(node[i], x, width[i]);
if (lazyflag[i])
lazyf(lazy[i], x);
else {
lazyflag[i] = true;
lazy[i] = x;
}
while (i /= 2)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
void update(int l, int r, U x) {
l += n;
r += n;
int nl = l, nr = r;
while (!(nl & 1))
nl >>= 1;
while (!(nr & 1))
nr >>= 1;
nr--;
eval(nl);
eval(nr);
while (l < r) {
if (l & 1) {
updf(node[l], x, width[l]);
if (lazyflag[l])
lazyf(lazy[l], x);
else {
lazyflag[l] = true;
lazy[l] = x;
}
l++;
}
if (r & 1) {
r--;
updf(node[r], x, width[r]);
if (lazyflag[r])
lazyf(lazy[r], x);
else {
lazyflag[r] = true;
lazy[r] = x;
}
}
l >>= 1;
r >>= 1;
}
while (nl >>= 1)
node[nl] = nodef(node[2 * nl], node[2 * nl + 1]);
while (nr >>= 1)
node[nr] = nodef(node[2 * nr], node[2 * nr + 1]);
}
T query(int l, int r) {
l += n;
r += n;
eval(l);
eval(r - 1);
int ls = nodee, rs = nodee;
while (l < r) {
if (l & 1) {
ls = nodef(ls, node[l]);
l++;
}
if (r & 1) {
r--;
rs = nodef(node[r], rs);
}
l >>= 1;
r >>= 1;
}
return nodef(ls, rs);
}
T operator[](const int &x) {
eval(n + x);
return node[n + x];
}
T queryForAll() { return node[1]; }
};
class RAQRSQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return a + b; }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += width * b; }
public:
RAQRSQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, 0) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRSQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>((int)0, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RAQRMiQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::min(a, b); }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += b; }
public:
RAQRMiQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRMiQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RAQRMaQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::max(a, b); }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += b; }
public:
RAQRMaQ(unsigned int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, -inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRMaQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(-inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRSQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return a + b; }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = width * b; }
public:
RUQRSQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, 0) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRSQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>((int)0, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRMiQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::min(a, b); }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = b; }
public:
RUQRMiQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRMiQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRMaQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::max(a, b); }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = b; }
public:
RUQRMaQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, -inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRMaQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(-inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
////SegTree
// template <class T>
// class SegTree {
// int n; // 葉の数
// vector<T> node; // データを格納するvector
// T def; // 初期値かつ単位元
// function<T(T, T)> operation; // 区間クエリで使う処理
// function<T(T, T)> update; // 点更新で使う処理
//
// // 区間[a,b)の総和。ノードk=[l,r)に着目している。
// T _query(int a, int b, int k, int l, int r) {
// if (r <= a || b <= l) return def; // 交差しない
// if (a <= l && r <= b)
// return node[k]; // a,l,r,bの順で完全に含まれる
// else {
// T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
// T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
// return operation(c1, c2);
// }
// }
//
// public:
// // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数,
// // _update:更新関数
// SegTree(size_t _n, T _def, function<T(T, T)> _operation,
// function<T(T, T)> _update)
// : def(_def), operation(_operation), update(_update) {
// n = 1;
// while (n < _n) {
// n *= 2;
// }
// node = vector<T>(2 * n , def);
// }
//
// // 場所i(0-indexed)の値をxで更新
// void change(int i, T x) {
// i += n - 1;
// node[i] = update(node[i], x);
// while (i > 0) {
// i = (i - 1) / 2;
// node[i] = operation(node[i * 2 + 1], node[i * 2 + 2]);
// }
// }
//
// // [a, b)の区間クエリを実行
// T query(int a, int b) {
// return _query(a, b, 0, 0, n);
// }
//
// // 添字でアクセス
// T operator[](int i) {
// return node[i + n - 1];
// }
// };
template <class T> class SegTree {
int n;
vector<T> node;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
public:
SegTree(unsigned int _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
node = vector<T>(n * 2, def);
}
SegTree(vector<int> &initvec, function<T(T, T)> _operation,
function<T(T, T)> _update)
: operation(_operation), update(_update) {
n = 1;
while (n < initvec.size()) {
n *= 2;
}
node = vector<T>(n * 2, def);
for (int i = n; i < n + initvec.size(); i++) {
node[i] = initvec[i - n];
}
for (int i = n - 1; i >= 1; i--) {
node[i] = operation(node[i * 2], node[i * 2 + 1]);
}
}
void change(int i, T x) {
i += n;
node[i] = update(node[i], x);
while (i >= 1) {
i >>= 1;
node[i] = operation(node[i * 2], node[i * 2 + 1]);
}
}
T query(int l, int r) {
l += n;
r += n;
T rx = def, lx = def;
while (l < r) {
if (l & 1) {
lx = operation(lx, node[l]);
l++;
}
if (r & 1) {
r--;
rx = operation(node[r], rx);
}
l >>= 1;
r >>= 1;
}
return operation(lx, rx);
}
T operator[](const int &x) { return node[x - n]; }
void fill(T x) { std::fill(ALL(node), x); }
void print() {
rep(i, n) std::cout << operator[](i) << " ";
std::cout << std::endl;
}
};
#define R_MIN ([](long long a, long long b) { return min(a, b); })
#define R_MAX ([](long long a, long long b) { return max(a, b); })
#define R_SUM ([](long long a, long long b) { return a + b; })
#define NORMAL_UPDATE ([](long long a, long long b) { return b; })
#define ADD_UPDATE ([](long long a, long long b) { return a + b; })
#define MINUS_UPDATE ([](long long a, long long b) { return a - b; }
class Union_Find {
vector<int> par;
vector<int> rankmy;
vector<int> ookisa;
public:
Union_Find(int size) {
par = vector<int>(size);
rankmy = vector<int>(size);
ookisa = vector<int>(size);
for (int i = 0; i < size; i++) {
par[i] = i;
ookisa[i] = 1;
}
}
int find(int x) {
if (par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rankmy[x] < rankmy[y]) {
par[x] = y;
ookisa[y] += ookisa[x];
ookisa[x] = 0;
} else {
par[y] = x;
ookisa[x] += ookisa[y];
ookisa[y] = 0;
if (rankmy[x] == rankmy[y]) {
rankmy[x]++;
}
}
}
int size(int i) {
i = find(i);
return ookisa[i];
}
bool same(int x, int y) { return find(x) == find(y); }
};
class BIT {
vector<int> data;
int size = 0;
public:
BIT(int _size) {
data = vector<int>(_size + 1);
size = _size;
}
void add(int i, int x) {
while (i <= size) {
data[i] += x;
i += i & -i;
}
}
int sum(int i) {
assert(i <= size);
int s = 0;
while (i > 0) {
s += data[i];
i -= i & -i;
}
return s;
}
int lower_bound(int x) {
if (x <= 0) {
return 0;
} else {
int i = 0;
int r = 1;
while (r < size)
r = r << 1;
for (int len = r; len > 0; len = len >> 1) {
if (i + len < size && data[i + len] < x) {
x -= data[i + len];
i += len;
}
}
return i + 1;
}
}
};
// Union-Find-End
int perm[2000005];
void init_perm() {
perm[0] = 1;
REP(i, 2000004) { perm[i] = perm[i - 1] * i % mod; }
}
int nCk(int x, int y) {
if (y > x) {
return 0;
}
if (x < 0) {
return 0;
}
return perm[x] * modpow(perm[x - y], mod - 2) % mod *
modpow(perm[y], mod - 2) % mod;
}
double kyori(pair<int, int> f, pair<int, int> s) {
double ans = 0;
double t = fabs(f.first - s.first);
double y = fabs(f.second - s.second);
ans = sqrt(t * t + y * y);
return ans;
}
inline string stringmax(string &x, string &y) {
if (x.size() > y.size()) {
return x;
}
if (x.size() < y.size()) {
return y;
}
rep(i, x.size()) {
if (x[i] > y[i]) {
return x;
}
if (x[i] < y[i]) {
return y;
}
}
return x;
}
vector<int> RollingHash(string &s, string &t) {
vector<int> ans;
__int128 h = 0, hamod = 0, ki = 0, kim = 0, hikaku = 0, one = 0;
one = 1;
ki = 1000000007ll;
hamod = (one << 61) - 1;
kim = 1;
rep(i, t.size()) {
hikaku *= ki;
h *= ki;
kim *= ki;
hikaku += t[i];
h += s[i];
hikaku %= hamod;
h %= hamod;
kim %= hamod;
}
rep(i, (int)s.size() - (int)t.size() + 1) {
if (h == hikaku) {
ans.emplace_back(i);
}
h *= ki;
h %= hamod;
h += s[i + (int)t.size()];
h %= hamod;
h += hamod;
h -= s[i] * kim % hamod;
h %= hamod;
}
return ans;
}
struct edge {
int to;
int length;
edge(int _to, int _length) {
to = _to;
length = _length;
}
};
vector<int> djkstra(vector<vector<edge>> &road, int start) {
vector<int> kyo(road.size(), inf);
smallpriority_queue(P) q;
q.push({0, start});
kyo[start] = 0;
while (q.size()) {
int x = q.top().second;
itn now = q.top().first;
q.pop();
if (kyo[x] < now) {
continue;
}
for (auto &i : road[x]) {
if (kyo[i.to] > now + i.length) {
kyo[i.to] = now + i.length;
q.push({kyo[i.to], i.to});
}
}
}
return kyo;
}
#define endl "\n" // interactive の時に注意!!!
#define printd cout << fixed << setprecision(10)
int n, a[200004], b[200004], ans;
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
priority_queue<P> q;
rep(i, n) { cin >> a[i]; }
rep(i, n) {
cin >> b[i];
q.push({b[i], i});
}
while (q.size()) {
int now = q.top().first;
int x = q.top().second;
q.pop();
if (a[x] == now) {
continue;
}
int yoko = 0;
if (x) {
yoko += b[x - 1];
} else {
yoko += b[n - 1];
}
if (x < n - 1) {
yoko += b[x + 1];
} else {
yoko += b[0];
}
if (yoko <= b[x]) {
int k = (b[x] - a[x]) / yoko;
b[x] -= k * yoko;
q.push({b[x], x});
ans += k;
} else {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
}
| /*
* じょえチャンネル
* 高評価・チャンネル登録よろしくおねがいします!
* https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// here!!!
// define int long long !!!!!
#define int long long
// define int long long !!!!!
#define mod (int)((1e9) + 7)
// constexpr int mod = 998244353ll;
#ifdef int
#define inf (int)(3e18)
#else
#define inf (int)(5e8)
#endif
#define intt long long
#define itn long long
#define P pair<long long, long long>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define rev_rep(i, n) for (int i = n - 1; i >= 0; i--)
#define REV_REP(i, n) for (int i = n; i >= 1; i--)
#define ALL(v) v.begin(), v.end()
#define smallpriority_queue(x) priority_queue<x, vector<x>, greater<x>>
using namespace std;
// Library
// モッドパウ
inline int modpow(int x, int y, int m = mod) {
int res = 1;
while (y) {
if (y & 1) {
res *= x;
res %= m;
}
x = x * x % m;
y /= 2;
}
return res;
}
int mypow(int x, int y) {
int res = 1;
while (y) {
if (y % 2) {
res *= x;
}
x = x * x;
y /= 2;
}
return res;
}
// is the number (x) a prime number?
bool prime(int x) {
if (!x || x == 1) {
return false;
}
for (int i = 2; i * i <= x; i++) {
if (!(x % i)) {
return false;
}
}
return true;
}
// saidai-kouyakusuu
inline int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
// number of keta
int keta(int x) {
int ans = 0;
while (x) {
x /= 10;
ans++;
}
return ans;
}
// sum of keta
int ketasum(int x) {
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
inline int lcm(int x, int y) {
int ans = x / gcd(x, y) * y;
return ans;
}
int twobeki(int x) {
int ans = 0;
while (1) {
if (!(x & 1)) {
ans++;
x >>= 1;
} else {
break;
}
}
return ans;
}
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld", i)
#define fendl printf("\n")
int kai(int x, int y) {
int res = 1;
for (int i = x - y + 1; i <= x; i++) {
res *= i;
res %= mod;
}
return res;
}
int comb(int x, int y) {
if (y > x)
return 0;
// cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl;
return kai(x, y) * modpow(kai(y, y), mod - 2) % mod;
}
// Library-End
#define vecin(v) \
for (int i = 0; i < v.size(); i++) \
scanf("%lld", &v[i]);
#define vecout(v) \
{ \
for (int i = 0; i < (int)v.size(); i++) \
printf("%lld ", v[i]); \
printf("\n"); \
}
template <typename T> class kaageSegTree {
protected:
unsigned int n = 1, rank = 0;
std::vector<T> node;
T nodee;
virtual T nodef(const T &, const T &) const = 0;
public:
kaageSegTree(unsigned int m, T init, T nodee) : nodee(nodee) {
while (n < m) {
n *= 2;
rank++;
}
node.resize(2 * n);
for (unsigned int i = n; i < 2 * n; i++)
node[i] = init;
}
kaageSegTree(const std::vector<T> &initvec, T nodee) : nodee(nodee) {
unsigned int m = initvec.size();
while (n < m) {
n *= 2;
rank++;
}
node.resize(2 * n);
for (unsigned int i = n; i < 2 * n; i++) {
if (i - n < m)
node[i] = initvec[i - n];
}
}
virtual void update(int i, T x) {
i += n;
node[i] = x;
while (i != 1) {
i >>= 1;
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
}
virtual T query(int l, int r) {
l += n;
r += n;
T ls = nodee, rs = nodee;
while (l < r) {
if (l & 1) {
ls = nodef(ls, node[l]);
l++;
}
if (r & 1) {
r--;
rs = nodef(node[r], rs);
}
l >>= 1;
r >>= 1;
}
return nodef(ls, rs);
}
virtual T operator[](const int &x) { return node[n + x]; }
void fill(T x) { std::fill(ALL(node), x); }
void print() {
rep(i, n) std::cout << operator[](i) << " ";
std::cout << std::endl;
}
};
class RSQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return lhs + rhs; }
public:
RSQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, 0) {}
RSQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, 0) {}
};
class RMiQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return std::min(lhs, rhs); }
public:
RMiQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, inf) {}
RMiQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, inf) {}
};
class RMaQ : public kaageSegTree<int> {
int nodef(const int &lhs, const int &rhs) const { return std::max(lhs, rhs); }
public:
RMaQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, -inf) {}
RMaQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, -inf) {}
};
template <typename T, typename U>
class IntervalSegTree : public kaageSegTree<T> {
protected:
using kaageSegTree<T>::n;
using kaageSegTree<T>::rank;
using kaageSegTree<T>::node;
using kaageSegTree<T>::nodef;
using kaageSegTree<T>::nodee;
std::vector<U> lazy;
std::vector<bool> lazyflag;
std::vector<int> width;
virtual void lazyf(U &, const U &) = 0;
virtual void updf(T &, const U &, const unsigned int &) = 0;
void eval(int k) {
for (int i = rank; i > 0; i--) {
int nk = k >> i;
if (lazyflag[nk]) {
updf(node[2 * nk], lazy[nk], width[2 * nk]);
updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]);
if (lazyflag[2 * nk])
lazyf(lazy[2 * nk], lazy[nk]);
else
lazy[2 * nk] = lazy[nk];
if (lazyflag[2 * nk + 1])
lazyf(lazy[2 * nk + 1], lazy[nk]);
else
lazy[2 * nk + 1] = lazy[nk];
lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true;
lazyflag[nk] = false;
}
}
}
public:
IntervalSegTree(unsigned int m, T init, T nodee)
: kaageSegTree<T>(m, init, nodee) {
lazy.resize(2 * n);
lazyflag.resize(2 * n);
width.resize(2 * n);
width[1] = n;
for (unsigned int i = 2; i < 2 * n; i++) {
width[i] = width[i >> 1] >> 1;
}
}
IntervalSegTree(T nodee, const std::vector<T> &initvec)
: kaageSegTree<T>(initvec, nodee) {
lazy.resize(2 * n);
lazyflag.resize(2 * n);
width.resize(2 * n);
width[1] = n;
for (unsigned int i = 2; i < 2 * n; i++) {
width[i] = width[i >> 1] >> 1;
}
}
void update(int i, U x) {
i += n;
eval(i);
updf(node[i], x, width[i]);
if (lazyflag[i])
lazyf(lazy[i], x);
else {
lazyflag[i] = true;
lazy[i] = x;
}
while (i /= 2)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
void update(int l, int r, U x) {
l += n;
r += n;
int nl = l, nr = r;
while (!(nl & 1))
nl >>= 1;
while (!(nr & 1))
nr >>= 1;
nr--;
eval(nl);
eval(nr);
while (l < r) {
if (l & 1) {
updf(node[l], x, width[l]);
if (lazyflag[l])
lazyf(lazy[l], x);
else {
lazyflag[l] = true;
lazy[l] = x;
}
l++;
}
if (r & 1) {
r--;
updf(node[r], x, width[r]);
if (lazyflag[r])
lazyf(lazy[r], x);
else {
lazyflag[r] = true;
lazy[r] = x;
}
}
l >>= 1;
r >>= 1;
}
while (nl >>= 1)
node[nl] = nodef(node[2 * nl], node[2 * nl + 1]);
while (nr >>= 1)
node[nr] = nodef(node[2 * nr], node[2 * nr + 1]);
}
T query(int l, int r) {
l += n;
r += n;
eval(l);
eval(r - 1);
int ls = nodee, rs = nodee;
while (l < r) {
if (l & 1) {
ls = nodef(ls, node[l]);
l++;
}
if (r & 1) {
r--;
rs = nodef(node[r], rs);
}
l >>= 1;
r >>= 1;
}
return nodef(ls, rs);
}
T operator[](const int &x) {
eval(n + x);
return node[n + x];
}
T queryForAll() { return node[1]; }
};
class RAQRSQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return a + b; }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += width * b; }
public:
RAQRSQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, 0) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRSQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>((int)0, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RAQRMiQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::min(a, b); }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += b; }
public:
RAQRMiQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRMiQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RAQRMaQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::max(a, b); }
void lazyf(int &a, const int &b) { a += b; }
void updf(int &a, const int &b, const unsigned int &width) { a += b; }
public:
RAQRMaQ(unsigned int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, -inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RAQRMaQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(-inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRSQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return a + b; }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = width * b; }
public:
RUQRSQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, 0) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRSQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>((int)0, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRMiQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::min(a, b); }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = b; }
public:
RUQRMiQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRMiQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
class RUQRMaQ : public IntervalSegTree<int, int> {
int nodef(const int &a, const int &b) const { return std::max(a, b); }
void lazyf(int &a, const int &b) { a = b; }
void updf(int &a, const int &b, const unsigned int &width) { a = b; }
public:
RUQRMaQ(int size, const int &def = 0)
: IntervalSegTree<int, int>(size, def, -inf) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
RUQRMaQ(const std::vector<int> &initvec)
: IntervalSegTree<int, int>(-inf, initvec) {
for (int i = n - 1; i > 0; i--)
node[i] = nodef(node[2 * i], node[2 * i + 1]);
}
};
////SegTree
// template <class T>
// class SegTree {
// int n; // 葉の数
// vector<T> node; // データを格納するvector
// T def; // 初期値かつ単位元
// function<T(T, T)> operation; // 区間クエリで使う処理
// function<T(T, T)> update; // 点更新で使う処理
//
// // 区間[a,b)の総和。ノードk=[l,r)に着目している。
// T _query(int a, int b, int k, int l, int r) {
// if (r <= a || b <= l) return def; // 交差しない
// if (a <= l && r <= b)
// return node[k]; // a,l,r,bの順で完全に含まれる
// else {
// T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
// T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
// return operation(c1, c2);
// }
// }
//
// public:
// // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数,
// // _update:更新関数
// SegTree(size_t _n, T _def, function<T(T, T)> _operation,
// function<T(T, T)> _update)
// : def(_def), operation(_operation), update(_update) {
// n = 1;
// while (n < _n) {
// n *= 2;
// }
// node = vector<T>(2 * n , def);
// }
//
// // 場所i(0-indexed)の値をxで更新
// void change(int i, T x) {
// i += n - 1;
// node[i] = update(node[i], x);
// while (i > 0) {
// i = (i - 1) / 2;
// node[i] = operation(node[i * 2 + 1], node[i * 2 + 2]);
// }
// }
//
// // [a, b)の区間クエリを実行
// T query(int a, int b) {
// return _query(a, b, 0, 0, n);
// }
//
// // 添字でアクセス
// T operator[](int i) {
// return node[i + n - 1];
// }
// };
template <class T> class SegTree {
int n;
vector<T> node;
T def;
function<T(T, T)> operation;
function<T(T, T)> update;
public:
SegTree(unsigned int _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
node = vector<T>(n * 2, def);
}
SegTree(vector<int> &initvec, function<T(T, T)> _operation,
function<T(T, T)> _update)
: operation(_operation), update(_update) {
n = 1;
while (n < initvec.size()) {
n *= 2;
}
node = vector<T>(n * 2, def);
for (int i = n; i < n + initvec.size(); i++) {
node[i] = initvec[i - n];
}
for (int i = n - 1; i >= 1; i--) {
node[i] = operation(node[i * 2], node[i * 2 + 1]);
}
}
void change(int i, T x) {
i += n;
node[i] = update(node[i], x);
while (i >= 1) {
i >>= 1;
node[i] = operation(node[i * 2], node[i * 2 + 1]);
}
}
T query(int l, int r) {
l += n;
r += n;
T rx = def, lx = def;
while (l < r) {
if (l & 1) {
lx = operation(lx, node[l]);
l++;
}
if (r & 1) {
r--;
rx = operation(node[r], rx);
}
l >>= 1;
r >>= 1;
}
return operation(lx, rx);
}
T operator[](const int &x) { return node[x - n]; }
void fill(T x) { std::fill(ALL(node), x); }
void print() {
rep(i, n) std::cout << operator[](i) << " ";
std::cout << std::endl;
}
};
#define R_MIN ([](long long a, long long b) { return min(a, b); })
#define R_MAX ([](long long a, long long b) { return max(a, b); })
#define R_SUM ([](long long a, long long b) { return a + b; })
#define NORMAL_UPDATE ([](long long a, long long b) { return b; })
#define ADD_UPDATE ([](long long a, long long b) { return a + b; })
#define MINUS_UPDATE ([](long long a, long long b) { return a - b; }
class Union_Find {
vector<int> par;
vector<int> rankmy;
vector<int> ookisa;
public:
Union_Find(int size) {
par = vector<int>(size);
rankmy = vector<int>(size);
ookisa = vector<int>(size);
for (int i = 0; i < size; i++) {
par[i] = i;
ookisa[i] = 1;
}
}
int find(int x) {
if (par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rankmy[x] < rankmy[y]) {
par[x] = y;
ookisa[y] += ookisa[x];
ookisa[x] = 0;
} else {
par[y] = x;
ookisa[x] += ookisa[y];
ookisa[y] = 0;
if (rankmy[x] == rankmy[y]) {
rankmy[x]++;
}
}
}
int size(int i) {
i = find(i);
return ookisa[i];
}
bool same(int x, int y) { return find(x) == find(y); }
};
class BIT {
vector<int> data;
int size = 0;
public:
BIT(int _size) {
data = vector<int>(_size + 1);
size = _size;
}
void add(int i, int x) {
while (i <= size) {
data[i] += x;
i += i & -i;
}
}
int sum(int i) {
assert(i <= size);
int s = 0;
while (i > 0) {
s += data[i];
i -= i & -i;
}
return s;
}
int lower_bound(int x) {
if (x <= 0) {
return 0;
} else {
int i = 0;
int r = 1;
while (r < size)
r = r << 1;
for (int len = r; len > 0; len = len >> 1) {
if (i + len < size && data[i + len] < x) {
x -= data[i + len];
i += len;
}
}
return i + 1;
}
}
};
// Union-Find-End
int perm[2000005];
void init_perm() {
perm[0] = 1;
REP(i, 2000004) { perm[i] = perm[i - 1] * i % mod; }
}
int nCk(int x, int y) {
if (y > x) {
return 0;
}
if (x < 0) {
return 0;
}
return perm[x] * modpow(perm[x - y], mod - 2) % mod *
modpow(perm[y], mod - 2) % mod;
}
double kyori(pair<int, int> f, pair<int, int> s) {
double ans = 0;
double t = fabs(f.first - s.first);
double y = fabs(f.second - s.second);
ans = sqrt(t * t + y * y);
return ans;
}
inline string stringmax(string &x, string &y) {
if (x.size() > y.size()) {
return x;
}
if (x.size() < y.size()) {
return y;
}
rep(i, x.size()) {
if (x[i] > y[i]) {
return x;
}
if (x[i] < y[i]) {
return y;
}
}
return x;
}
vector<int> RollingHash(string &s, string &t) {
vector<int> ans;
__int128 h = 0, hamod = 0, ki = 0, kim = 0, hikaku = 0, one = 0;
one = 1;
ki = 1000000007ll;
hamod = (one << 61) - 1;
kim = 1;
rep(i, t.size()) {
hikaku *= ki;
h *= ki;
kim *= ki;
hikaku += t[i];
h += s[i];
hikaku %= hamod;
h %= hamod;
kim %= hamod;
}
rep(i, (int)s.size() - (int)t.size() + 1) {
if (h == hikaku) {
ans.emplace_back(i);
}
h *= ki;
h %= hamod;
h += s[i + (int)t.size()];
h %= hamod;
h += hamod;
h -= s[i] * kim % hamod;
h %= hamod;
}
return ans;
}
struct edge {
int to;
int length;
edge(int _to, int _length) {
to = _to;
length = _length;
}
};
vector<int> djkstra(vector<vector<edge>> &road, int start) {
vector<int> kyo(road.size(), inf);
smallpriority_queue(P) q;
q.push({0, start});
kyo[start] = 0;
while (q.size()) {
int x = q.top().second;
itn now = q.top().first;
q.pop();
if (kyo[x] < now) {
continue;
}
for (auto &i : road[x]) {
if (kyo[i.to] > now + i.length) {
kyo[i.to] = now + i.length;
q.push({kyo[i.to], i.to});
}
}
}
return kyo;
}
#define endl "\n" // interactive の時に注意!!!
#define printd cout << fixed << setprecision(10)
int n, a[200004], b[200004], ans;
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
priority_queue<P> q;
rep(i, n) { cin >> a[i]; }
rep(i, n) {
cin >> b[i];
q.push({b[i], i});
}
while (q.size()) {
int now = q.top().first;
int x = q.top().second;
q.pop();
if (a[x] == now) {
continue;
}
int yoko = 0;
if (x) {
yoko += b[x - 1];
} else {
yoko += b[n - 1];
}
if (x < n - 1) {
yoko += b[x + 1];
} else {
yoko += b[0];
}
if (yoko <= b[x]) {
int k = (b[x] - a[x]) / yoko;
b[x] -= k * yoko;
if (!k) {
cout << -1 << endl;
return 0;
}
q.push({b[x], x});
ans += k;
} else {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
}
| insert | 858 | 858 | 858 | 862 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007ll
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li a[200200];
li b[200200];
li n;
int main() {
cin >> n;
rin { cin >> a[i]; }
rin { cin >> b[i]; }
priority_queue<PI> pq;
rin { pq.push({b[i], i}); }
li res = 0;
while (!pq.empty()) {
li now = pq.top().S;
li now_cost = pq.top().F;
pq.pop();
if (b[now] == a[now]) {
continue;
}
if (b[now] < now_cost) {
continue;
}
li next = (now + 1) % n;
li prev = (now + n - 1) % n;
li new_cost = b[now] - b[next] - b[prev];
li top2 = pq.top().F;
li diff = b[now] - max(top2, a[now]);
li cnt = diff / (b[next] + b[prev]);
cnt = max(cnt, 1ll);
res += cnt;
// dump(b, n);
// p2(now, cnt);
b[now] = b[now] - (b[next] + b[prev]) * cnt;
if (b[now] < a[now]) {
puts("-1");
return 0;
}
pq.push({b[now], now});
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007ll
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li a[200200];
li b[200200];
li n;
int main() {
cin >> n;
rin { cin >> a[i]; }
rin { cin >> b[i]; }
priority_queue<PI> pq;
rin { pq.push({b[i], i}); }
li res = 0;
while (!pq.empty()) {
li now = pq.top().S;
li now_cost = pq.top().F;
pq.pop();
if (b[now] == a[now]) {
continue;
}
if (b[now] < now_cost) {
continue;
}
li next = (now + 1) % n;
li prev = (now + n - 1) % n;
li new_cost = b[now] - b[next] - b[prev];
li top2 = pq.top().F;
li diff = b[now] - max(a[now], a[now]);
li cnt = diff / (b[next] + b[prev]);
cnt = max(cnt, 1ll);
res += cnt;
// dump(b, n);
// p2(now, cnt);
b[now] = b[now] - (b[next] + b[prev]) * cnt;
if (b[now] < a[now]) {
puts("-1");
return 0;
}
pq.push({b[now], now});
}
cout << res << endl;
return 0;
}
| replace | 111 | 112 | 111 | 112 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
typedef pair<int, int> P;
int A[200010];
int B[200010];
signed main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
priority_queue<P> pq;
rep(i, N) {
int L = (i + N - 1) % N;
int M = (i + N) % N;
int R = (i + N + 1) % N;
pq.push({B[M] - B[L] - B[R], i});
}
int cnt = 0;
while (pq.size()) {
P num = pq.top();
pq.pop();
// cout << num.first << endl;
if (num.first < 0)
break;
int i = num.second;
int L = (i + N - 1) % N;
int M = (i + N) % N;
int R = (i + N + 1) % N;
if (B[L] + B[R] < B[M] && B[M] > A[M]) {
int times = (B[M] - A[M]) / (B[L] + B[R]);
B[M] -= (B[L] + B[R]) * times;
cnt += times;
// rep(i, N) cout << B[i] << " ";
// cout << endl;
REP(j, -1, 1) {
int i2 = num.second + j + N * 2;
int L1 = (i2 + N - 1) % N;
int M1 = (i2 + N) % N;
int R1 = (i2 + N + 1) % N;
pq.push({B[M1] - B[L1] - B[R1], M1});
}
}
}
rep(i, N) {
if (B[i] != A[i]) {
cout << -1 << endl;
return 0;
}
}
cout << cnt << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
typedef pair<int, int> P;
int A[200010];
int B[200010];
signed main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
priority_queue<P> pq;
rep(i, N) {
int L = (i + N - 1) % N;
int M = (i + N) % N;
int R = (i + N + 1) % N;
pq.push({B[M] - B[L] - B[R], i});
}
int cnt = 0;
while (pq.size()) {
P num = pq.top();
pq.pop();
// cout << num.first << endl;
if (num.first < 0)
break;
int i = num.second;
int L = (i + N - 1) % N;
int M = (i + N) % N;
int R = (i + N + 1) % N;
if (B[L] + B[R] < B[M] && B[M] > A[M]) {
int times = (B[M] - A[M]) / (B[L] + B[R]);
if (times <= 0)
break;
B[M] -= (B[L] + B[R]) * times;
cnt += times;
// rep(i, N) cout << B[i] << " ";
// cout << endl;
REP(j, -1, 1) {
int i2 = num.second + j + N * 2;
int L1 = (i2 + N - 1) % N;
int M1 = (i2 + N) % N;
int R1 = (i2 + N + 1) % N;
pq.push({B[M1] - B[L1] - B[R1], M1});
}
}
}
rep(i, N) {
if (B[i] != A[i]) {
cout << -1 << endl;
return 0;
}
}
cout << cnt << endl;
return 0;
}
| insert | 42 | 42 | 42 | 44 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
const int mx = 200010;
const ll mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
rep(i, n) { cin >> a[i]; }
priority_queue<pair<ll, int>> q;
rep(i, n) {
cin >> b[i];
q.push({b[i], i});
}
ll cnt = 0;
while (!q.empty()) {
ll p;
int pos;
tie(p, pos) = q.top();
q.pop();
int posl = pos - 1;
if (posl < 0)
posl += n;
int posr = pos + 1;
if (posr >= n)
posr -= n;
ll tmp = (b[pos] - a[pos]) / (b[posl] + b[posr]);
b[pos] -= (b[posl] + b[posr]) * tmp;
cnt += tmp;
if (a[pos] != b[pos]) {
q.push({b[pos], pos});
}
}
if (a == b) {
cout << cnt << endl;
} else {
cout << -1 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
const int mx = 200010;
const ll mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
rep(i, n) { cin >> a[i]; }
priority_queue<pair<ll, int>> q;
rep(i, n) {
cin >> b[i];
q.push({b[i], i});
}
ll cnt = 0;
while (!q.empty()) {
ll p;
int pos;
tie(p, pos) = q.top();
q.pop();
int posl = pos - 1;
if (posl < 0)
posl += n;
int posr = pos + 1;
if (posr >= n)
posr -= n;
ll tmp = (b[pos] - a[pos]) / (b[posl] + b[posr]);
b[pos] -= (b[posl] + b[posr]) * tmp;
cnt += tmp;
if (a[pos] != b[pos] && tmp) {
q.push({b[pos], pos});
}
}
if (a == b) {
cout << cnt << endl;
} else {
cout << -1 << endl;
}
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *std::min_element((a).begin(), (a).end())
#define MAX(a) *std::max_element((a).begin(), (a).end())
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0LL)
#define REP(i, n) for (std::size_t(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (std::size_t(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (std::size_t(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (std::size_t(i) = (n)-1; (i) >= (m); i--)
#define debug(x) std::cerr << #x << " = " << x << '\n'
// #define int long long
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vl = std::vector<long long>;
using vvl = std::vector<std::vector<long long>>;
// using bint = boost::multiprecision::cpp_int;
// using rat = boost::rational<bint>;
constexpr int inf = (1 << 30) - 1;
constexpr long long INF = 1LL << 60;
constexpr double eps = 1e-9;
inline void ios_() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
}
template <typename T> inline T Div(const T &a, const T &b) {
return (a + b - 1) / b;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline void finish(const T &msg) {
std::cout << msg << std::endl;
std::exit(0);
}
template <typename T>
long long binarySearch(long long ng, long long ok, const T &fn) {
while (std::abs(ok - ng) > 1) {
long long mid = (ok + ng) / 2;
if (fn(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
struct ModInt {
long long value;
static constexpr long long mod = 1e9 + 7;
// static constexpr long long mod = 998244353;
constexpr ModInt(long long v = 0) : value(((v % mod) + mod) % mod) {}
constexpr ModInt pow(long long n) const {
long long x = value, res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return ModInt(res);
}
constexpr ModInt inverse() const { return pow(mod - 2); }
constexpr ModInt operator-() const { return ModInt((mod - value) % mod); }
constexpr ModInt &operator+=(const ModInt &a) {
value += a.value;
value %= mod;
return *this;
}
constexpr ModInt &operator-=(const ModInt &a) {
value += (-a).value;
value %= mod;
return *this;
}
constexpr ModInt &operator*=(const ModInt &a) {
value *= a.value;
value %= mod;
return *this;
}
constexpr ModInt &operator/=(const ModInt &a) {
return (*this) *= a.inverse();
}
constexpr ModInt operator+(const ModInt &a) const {
return ModInt(*this) += a;
}
constexpr ModInt operator-(const ModInt &a) const {
return ModInt(*this) -= a;
}
constexpr ModInt operator*(const ModInt &a) const {
return ModInt(*this) *= a;
}
constexpr ModInt operator/(const ModInt &a) const {
return ModInt(*this) /= a;
}
constexpr bool operator==(const ModInt &a) const { return value == a.value; }
constexpr bool operator!=(const ModInt &a) const { return value != a.value; }
friend std::istream &operator>>(std::istream &is, ModInt &a) {
long long v;
is >> v;
a = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) {
os << a.value;
return os;
}
static ModInt factorial(int n) {
static std::vector<ModInt> fact(1 << 18);
if (fact.size() <= n)
fact.resize(n + 1);
if (n < 0)
return 0;
if (n == 0)
return 1;
if (fact[n] != 0)
return fact[n];
return fact[n] = factorial(n - 1) * n;
}
static ModInt combin(int n, int k) {
if (n < 0 || k < 0 || n < k)
return 0;
return factorial(n) / (factorial(k) * factorial(n - k));
}
};
using mint = ModInt;
signed main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
for (auto &&i : a)
cin >> i;
for (auto &&i : b)
cin >> i;
REP(i, n) {
if (a[i] > b[i]) {
finish(-1);
}
}
priority_queue<pair<ll, int>> que;
REP(i, n) { que.push({b[i], i}); }
ll ans = 0;
while (!que.empty()) {
auto p = que.top();
que.pop();
int l = (p.second + n - 1) % n, r = (p.second + 1) % n;
ll x = (que.empty() ? a[p.second] : que.top().first);
ll d = Div(b[p.second] - x, b[l] + b[r]);
b[p.second] -= d * (b[l] + b[r]);
ans += d;
if (b[p.second] < a[p.second]) {
finish(-1);
} else if (b[p.second] > a[p.second]) {
que.push({b[p.second], p.second});
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *std::min_element((a).begin(), (a).end())
#define MAX(a) *std::max_element((a).begin(), (a).end())
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0LL)
#define REP(i, n) for (std::size_t(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (std::size_t(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (std::size_t(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (std::size_t(i) = (n)-1; (i) >= (m); i--)
#define debug(x) std::cerr << #x << " = " << x << '\n'
// #define int long long
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vl = std::vector<long long>;
using vvl = std::vector<std::vector<long long>>;
// using bint = boost::multiprecision::cpp_int;
// using rat = boost::rational<bint>;
constexpr int inf = (1 << 30) - 1;
constexpr long long INF = 1LL << 60;
constexpr double eps = 1e-9;
inline void ios_() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
}
template <typename T> inline T Div(const T &a, const T &b) {
return (a + b - 1) / b;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline void finish(const T &msg) {
std::cout << msg << std::endl;
std::exit(0);
}
template <typename T>
long long binarySearch(long long ng, long long ok, const T &fn) {
while (std::abs(ok - ng) > 1) {
long long mid = (ok + ng) / 2;
if (fn(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
struct ModInt {
long long value;
static constexpr long long mod = 1e9 + 7;
// static constexpr long long mod = 998244353;
constexpr ModInt(long long v = 0) : value(((v % mod) + mod) % mod) {}
constexpr ModInt pow(long long n) const {
long long x = value, res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return ModInt(res);
}
constexpr ModInt inverse() const { return pow(mod - 2); }
constexpr ModInt operator-() const { return ModInt((mod - value) % mod); }
constexpr ModInt &operator+=(const ModInt &a) {
value += a.value;
value %= mod;
return *this;
}
constexpr ModInt &operator-=(const ModInt &a) {
value += (-a).value;
value %= mod;
return *this;
}
constexpr ModInt &operator*=(const ModInt &a) {
value *= a.value;
value %= mod;
return *this;
}
constexpr ModInt &operator/=(const ModInt &a) {
return (*this) *= a.inverse();
}
constexpr ModInt operator+(const ModInt &a) const {
return ModInt(*this) += a;
}
constexpr ModInt operator-(const ModInt &a) const {
return ModInt(*this) -= a;
}
constexpr ModInt operator*(const ModInt &a) const {
return ModInt(*this) *= a;
}
constexpr ModInt operator/(const ModInt &a) const {
return ModInt(*this) /= a;
}
constexpr bool operator==(const ModInt &a) const { return value == a.value; }
constexpr bool operator!=(const ModInt &a) const { return value != a.value; }
friend std::istream &operator>>(std::istream &is, ModInt &a) {
long long v;
is >> v;
a = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) {
os << a.value;
return os;
}
static ModInt factorial(int n) {
static std::vector<ModInt> fact(1 << 18);
if (fact.size() <= n)
fact.resize(n + 1);
if (n < 0)
return 0;
if (n == 0)
return 1;
if (fact[n] != 0)
return fact[n];
return fact[n] = factorial(n - 1) * n;
}
static ModInt combin(int n, int k) {
if (n < 0 || k < 0 || n < k)
return 0;
return factorial(n) / (factorial(k) * factorial(n - k));
}
};
using mint = ModInt;
signed main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
for (auto &&i : a)
cin >> i;
for (auto &&i : b)
cin >> i;
REP(i, n) {
if (a[i] > b[i]) {
finish(-1);
}
}
priority_queue<pair<ll, int>> que;
REP(i, n) { que.push({b[i], i}); }
ll ans = 0;
while (!que.empty()) {
auto p = que.top();
que.pop();
int l = (p.second + n - 1) % n, r = (p.second + 1) % n;
ll x = (que.empty() ? a[p.second] : max({a[p.second], b[l], b[r]}));
ll d = Div(b[p.second] - x, b[l] + b[r]);
b[p.second] -= d * (b[l] + b[r]);
ans += d;
if (b[p.second] < a[p.second]) {
finish(-1);
} else if (b[p.second] > a[p.second]) {
que.push({b[p.second], p.second});
}
}
cout << ans << endl;
return 0;
}
| replace | 195 | 196 | 195 | 196 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node {
int v, p;
bool operator<(const node &a) const { return v < a.v; }
};
int a[200005], b[200005];
priority_queue<node> q;
int main(void) {
int n, i, x, p, l, tmp;
long long ans = 0;
node k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
q.push((node){b[i], i});
}
while (!q.empty()) {
k = q.top();
q.pop();
if (k.p == 0) {
p = n - 1;
l = 1;
} else if (k.p == n - 1) {
p = n - 2;
l = 0;
} else {
p = k.p - 1;
l = k.p + 1;
}
if (k.v != a[k.p]) {
if (k.v < b[p] + b[l]) {
printf("-1");
return 0;
} else {
ans += k.v / (b[p] + b[l]);
k.v = k.v % (b[p] + b[l]);
if (k.v < a[k.p]) {
tmp = (a[k.p] - k.v) / (b[p] + b[l]);
if ((a[k.p] - k.v) % (b[p] + b[l]) != 0)
tmp++;
ans -= tmp;
k.v += tmp * (b[p] + b[l]);
}
b[k.p] = k.v;
q.push(k);
}
}
}
printf("%lld", ans);
return 0;
} | #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node {
int v, p;
bool operator<(const node &a) const { return v < a.v; }
};
int a[200005], b[200005];
priority_queue<node> q;
int main(void) {
int n, i, x, p, l, tmp;
long long ans = 0;
node k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
q.push((node){b[i], i});
}
while (!q.empty()) {
k = q.top();
q.pop();
if (k.p == 0) {
p = n - 1;
l = 1;
} else if (k.p == n - 1) {
p = n - 2;
l = 0;
} else {
p = k.p - 1;
l = k.p + 1;
}
if (k.v != a[k.p]) {
if (k.v < b[p] + b[l]) {
printf("-1");
return 0;
} else {
ans += k.v / (b[p] + b[l]);
k.v = k.v % (b[p] + b[l]);
if (k.v < a[k.p]) {
tmp = (a[k.p] - k.v) / (b[p] + b[l]);
if ((a[k.p] - k.v) % (b[p] + b[l]) != 0) {
printf("-1");
return 0;
}
ans -= tmp;
k.v += tmp * (b[p] + b[l]);
}
b[k.p] = k.v;
q.push(k);
}
}
}
printf("%lld", ans);
return 0;
} | replace | 47 | 49 | 47 | 51 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 100010;
int n, a[N], b[N];
set<pii> st;
inline void no() {
puts("-1");
exit(0);
}
int main() {
n = rd();
fo(i, 1, n) a[i] = rd();
fo(i, 1, n) b[i] = rd();
fo(i, 1, n) if (a[i] != b[i]) {
if (a[i] > b[i])
no();
st.insert(pii(b[i], i));
}
ll ans = 0;
while (!st.empty()) {
pii x = *st.rbegin();
st.erase(x);
int pre = x.second - 1, now = pre + 1, nxt = now + 1;
if (pre <= 0)
pre += n;
if (nxt > n)
nxt -= n;
int hh = max(a[now], b[pre] + b[nxt]);
int step = (b[now] - hh) / (b[pre] + b[nxt]);
ans += step;
b[now] -= step * (b[pre] + b[nxt]);
if (b[now] == a[now])
continue;
b[now] -= b[pre] + b[nxt];
++ans;
if (b[now] == a[now])
continue;
if (b[now] > a[now]) {
st.insert(pii(b[now], now));
continue;
}
no();
}
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar(' ')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
char str[20];
sprintf(str, "%s.in", s);
freopen(str, "r", stdin);
// sprintf(str,"%s.out",s);
// freopen(str,"w",stdout);
#endif
}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-')
f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 200010;
int n, a[N], b[N];
set<pii> st;
inline void no() {
puts("-1");
exit(0);
}
int main() {
n = rd();
fo(i, 1, n) a[i] = rd();
fo(i, 1, n) b[i] = rd();
fo(i, 1, n) if (a[i] != b[i]) {
if (a[i] > b[i])
no();
st.insert(pii(b[i], i));
}
ll ans = 0;
while (!st.empty()) {
pii x = *st.rbegin();
st.erase(x);
int pre = x.second - 1, now = pre + 1, nxt = now + 1;
if (pre <= 0)
pre += n;
if (nxt > n)
nxt -= n;
int hh = max(a[now], b[pre] + b[nxt]);
int step = (b[now] - hh) / (b[pre] + b[nxt]);
ans += step;
b[now] -= step * (b[pre] + b[nxt]);
if (b[now] == a[now])
continue;
b[now] -= b[pre] + b[nxt];
++ans;
if (b[now] == a[now])
continue;
if (b[now] > a[now]) {
st.insert(pii(b[now], now));
continue;
}
no();
}
printf("%lld\n", ans);
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02941 | C++ | Runtime Error | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 2e5 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int A[N], B[N], n;
int q[60 * N], cur, sz;
inline int prv(int x) { return (x - 1 + n) % n; }
inline int nxt(int x) { return (x + 1) % n; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", A + i);
for (int i = 0; i < n; i++)
scanf("%d", B + i);
for (int i = 0; i < n; i++) {
if (B[i] > B[prv(i)] + B[nxt(i)] && B[i] > A[i])
q[sz++] = i;
}
ll sol = 0;
for (; cur < sz;) {
int i = q[cur++];
sol += (ll)(B[i] - A[i]) / (B[prv(i)] + B[nxt(i)]);
B[i] = B[i] -
((B[i] - A[i]) / (B[prv(i)] + B[nxt(i)])) * (B[prv(i)] + B[nxt(i)]);
if (B[prv(i)] > A[prv(i)] && B[prv(i)] > B[prv(prv(i))] + B[i])
q[sz++] = prv(i);
if (B[nxt(i)] > A[nxt(i)] && B[nxt(i)] > B[nxt(nxt(i))] + B[i])
q[sz++] = nxt(i);
if (B[i] > A[i] && B[i] > B[prv(i)] + B[nxt(i)])
q[sz++] = i;
}
bool ans = 1;
for (int i = 0; i < n; i++)
ans &= (A[i] == B[i]);
printf("%lld\n", ans ? sol : -1);
return 0;
} | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 2e5 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int A[N], B[N], n;
int q[60 * N], cur, sz;
inline int prv(int x) { return (x - 1 + n) % n; }
inline int nxt(int x) { return (x + 1) % n; }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", A + i);
for (int i = 0; i < n; i++)
scanf("%d", B + i);
for (int i = 0; i < n; i++) {
if (B[i] > B[prv(i)] + B[nxt(i)] && B[i] > A[i])
q[sz++] = i;
}
ll sol = 0;
for (; cur < sz;) {
int i = q[cur++];
int potez =
(ll)(B[i] - A[i] + B[prv(i)] + B[nxt(i)] - 1) / (B[prv(i)] + B[nxt(i)]);
if (B[i] <= potez * (B[prv(i)] + B[nxt(i)]))
potez--;
sol += potez;
B[i] = B[i] - potez * (B[prv(i)] + B[nxt(i)]);
if (B[prv(i)] > A[prv(i)] && B[prv(i)] > B[prv(prv(i))] + B[i])
q[sz++] = prv(i);
if (B[nxt(i)] > A[nxt(i)] && B[nxt(i)] > B[nxt(nxt(i))] + B[i])
q[sz++] = nxt(i);
if (B[i] > A[i] && B[i] > B[prv(i)] + B[nxt(i)])
q[sz++] = i;
}
bool ans = 1;
for (int i = 0; i < n; i++)
ans &= (A[i] == B[i]);
printf("%lld\n", ans ? sol : -1);
return 0;
} | replace | 42 | 46 | 42 | 48 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
long solve(int n, vector<long> &a, vector<long> &b) {
long ret = 0;
while (1) {
bool f = false;
for (int i = 0; i < n; i++) {
if (a[i] != b[i])
break;
if (i == n - 1)
return ret;
}
for (int i = 0; i < n; i++) {
long t = b[i % n] + b[(i + 2) % n];
if (b[(i + 1) % n] > t) {
long k = (b[(i + 1) % n] - a[(i + 1) % n]) / t;
ret += k;
b[(i + 1) % n] -= t * k;
f = true;
}
}
if (!f)
return -1;
}
}
int main() {
int n;
cin >> n;
vector<long> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
if (b[i] < a[i]) {
puts("-1");
return 0;
}
}
cout << solve(n, a, b) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
long solve(int n, vector<long> &a, vector<long> &b) {
long ret = 0;
while (1) {
bool f = false;
for (int i = 0; i < n; i++) {
if (a[i] != b[i])
break;
if (i == n - 1)
return ret;
}
for (int i = 0; i < n; i++) {
long t = b[i % n] + b[(i + 2) % n];
if (b[(i + 1) % n] > t) {
long k = (b[(i + 1) % n] - a[(i + 1) % n]) / t;
ret += k;
b[(i + 1) % n] -= t * k;
if (k)
f = true;
}
}
if (!f)
return -1;
}
}
int main() {
int n;
cin >> n;
vector<long> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
if (b[i] < a[i]) {
puts("-1");
return 0;
}
}
cout << solve(n, a, b) << endl;
return 0;
}
| replace | 33 | 34 | 33 | 35 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
struct N {
int i;
ll v;
};
bool operator<(const N &l, const N &r) { return l.v < r.v; }
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
priority_queue<N> q;
for (int i = 0; i < n; i++) {
if (b[i] > a[i])
q.push(N{i, b[i]});
else if (b[i] < a[i]) {
cout << "-1" << endl;
return 0;
}
}
ll ans = 0;
while (q.size()) {
auto now = q.top();
q.pop();
ll side = b[(now.i + n - 1) % n] + b[(now.i + 1) % n];
if (side == 0 || now.v - side < a[now.i]) {
cout << -1 << endl;
return 0;
}
ll sidemax = max(b[(now.i + n - 1) % n], b[(now.i + 1) % n]);
ll skip = (sidemax + side - 1) / side;
b[now.i] -= skip * side;
if (b[now.i] > a[now.i]) {
q.push(N{now.i, b[now.i]});
}
ans += skip;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
struct N {
int i;
ll v;
};
bool operator<(const N &l, const N &r) { return l.v < r.v; }
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
priority_queue<N> q;
for (int i = 0; i < n; i++) {
if (b[i] > a[i])
q.push(N{i, b[i]});
else if (b[i] < a[i]) {
cout << "-1" << endl;
return 0;
}
}
ll ans = 0;
while (q.size()) {
auto now = q.top();
q.pop();
ll side = b[(now.i + n - 1) % n] + b[(now.i + 1) % n];
if (side == 0 || now.v - side < a[now.i]) {
cout << -1 << endl;
return 0;
}
ll skip = (now.v - side) / side;
if (skip == 0)
skip++;
skip = min(skip, (now.v - a[now.i] + side - 1) / side);
b[now.i] -= skip * side;
if (b[now.i] > a[now.i]) {
q.push(N{now.i, b[now.i]});
}
ans += skip;
}
cout << ans << endl;
return 0;
}
| replace | 45 | 47 | 45 | 49 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;
using namespace std;
bool match[200000] = {};
ll a[200000] = {};
ll b[200000] = {};
priority_queue<pair<ll, ll>> q;
int n;
ll dfs(int i) {
ll left = b[(i - 1 + n) % n];
ll right = b[(i + 1 + n) % n];
if (left + right == 0) {
return 0;
}
ll ans = (abs(b[i] - a[i]) / (left + right));
b[i] = b[i] - (abs(b[i] - a[i]) / (left + right)) * (left + right);
if (b[i] == 0) {
return 0;
}
if (a[i] == b[i])
match[i] = true;
if (b[(i - 2 + n) % n] + b[i] < b[(i - 1 + n) % n] &&
!match[(i - 1 + n) % n]) {
if (b[(i - 2 + n) % n] + b[i] != 0) {
q.push(make_pair(b[(i - 1 + n) % n] % (b[(i - 2 + n) % n] + b[i]),
(i - 1 + n) % n));
}
}
if (b[i] + b[(i + 2 + n) % n] < b[(i + 1 + n) % n] &&
!match[(i + 1 + n) % n]) {
if (b[i] + b[(i + 2 + n) % n] != 0) {
q.push(make_pair(b[(i + 1 + n) % n] % (b[i] + b[(i + 2 + n) % n]),
(i + 1 + n) % n));
}
}
return ans;
}
int main() {
cin >> n;
REP(i, n) { cin >> a[i]; }
REP(i, n) {
cin >> b[i];
if (a[i] == b[i]) {
match[i] = true;
}
}
ll ans = 0;
REP(i, n) {
ll left = b[(i - 1 + n) % n];
ll right = b[(i + 1 + n) % n];
if (left + right < b[i] && !match[i]) {
q.push(make_pair(b[i] % (left + right), i));
}
}
while (!q.empty()) {
auto top = q.top();
q.pop();
ans += dfs(top.second);
}
REP(i, n) {
if (!match[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;
using namespace std;
bool match[200000] = {};
ll a[200000] = {};
ll b[200000] = {};
priority_queue<pair<ll, ll>> q;
int n;
ll dfs(int i) {
ll left = b[(i - 1 + n) % n];
ll right = b[(i + 1 + n) % n];
if (left + right == 0) {
return 0;
}
ll ans = (abs(b[i] - a[i]) / (left + right));
if (ans == 0) {
return 0;
}
b[i] = b[i] - (abs(b[i] - a[i]) / (left + right)) * (left + right);
if (b[i] == 0) {
return 0;
}
if (a[i] == b[i])
match[i] = true;
if (b[(i - 2 + n) % n] + b[i] < b[(i - 1 + n) % n] &&
!match[(i - 1 + n) % n]) {
if (b[(i - 2 + n) % n] + b[i] != 0) {
q.push(make_pair(b[(i - 1 + n) % n] % (b[(i - 2 + n) % n] + b[i]),
(i - 1 + n) % n));
}
}
if (b[i] + b[(i + 2 + n) % n] < b[(i + 1 + n) % n] &&
!match[(i + 1 + n) % n]) {
if (b[i] + b[(i + 2 + n) % n] != 0) {
q.push(make_pair(b[(i + 1 + n) % n] % (b[i] + b[(i + 2 + n) % n]),
(i + 1 + n) % n));
}
}
return ans;
}
int main() {
cin >> n;
REP(i, n) { cin >> a[i]; }
REP(i, n) {
cin >> b[i];
if (a[i] == b[i]) {
match[i] = true;
}
}
ll ans = 0;
REP(i, n) {
ll left = b[(i - 1 + n) % n];
ll right = b[(i + 1 + n) % n];
if (left + right < b[i] && !match[i]) {
q.push(make_pair(b[i] % (left + right), i));
}
}
while (!q.empty()) {
auto top = q.top();
q.pop();
ans += dfs(top.second);
}
REP(i, n) {
if (!match[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
}
| insert | 21 | 21 | 21 | 24 | TLE | |
p02941 | C++ | Time Limit Exceeded | // #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int N;
ll A[int(2e5 + 5)], B[int(2e5 + 5)];
priority_queue<Pli> que;
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%lld", A + i);
for (int i = 0; i < N; i++) {
scanf("%lld", B + i);
que.push({B[i], i});
}
ll ans = 0;
while (!que.empty()) {
Pli Q = que.top();
que.pop();
int now = Q.se;
ll sum = 0;
if (B[now] == A[now])
continue;
if (now == 0)
sum = B[N - 1] + B[1];
else if (now == N - 1)
sum = B[N - 2] + B[0];
else
sum = B[now - 1] + B[now + 1];
if (sum >= B[now]) {
printf("-1\n");
return 0;
}
ll turn;
if (A[now] >= sum) {
turn = (B[now] - A[now]) / sum;
} else {
turn = (B[now] / sum);
if (B[now] % sum == 0)
turn--;
}
ans += turn;
B[now] -= turn * sum;
if (A[now] == B[now])
continue;
if (A[now] > B[now]) {
printf("-1\n");
return 0;
}
que.push({B[now], now});
}
printf("%lld\n", ans);
return 0;
}
| // #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int N;
ll A[int(2e5 + 5)], B[int(2e5 + 5)];
priority_queue<Pli> que;
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%lld", A + i);
for (int i = 0; i < N; i++) {
scanf("%lld", B + i);
que.push({B[i], i});
}
ll ans = 0;
while (!que.empty()) {
Pli Q = que.top();
que.pop();
int now = Q.se;
ll sum = 0;
if (B[now] == A[now])
continue;
if (now == 0)
sum = B[N - 1] + B[1];
else if (now == N - 1)
sum = B[N - 2] + B[0];
else
sum = B[now - 1] + B[now + 1];
if (sum >= B[now]) {
printf("-1\n");
return 0;
}
ll turn;
if (A[now] >= sum) {
turn = (B[now] - A[now]) / sum;
} else {
turn = (B[now] / sum);
if (B[now] % sum == 0)
turn--;
}
ans += turn;
B[now] -= turn * sum;
if (turn <= 0) {
printf("-1\n");
return 0;
}
if (A[now] == B[now])
continue;
if (A[now] > B[now]) {
printf("-1\n");
return 0;
}
que.push({B[now], now});
}
printf("%lld\n", ans);
return 0;
}
| insert | 89 | 89 | 89 | 94 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define Z class
#define ll long long
#define ln cout << '\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
template <Z A> void pr(A a) {
cout << a;
ln;
}
template <Z A, Z B> void pr(A a, B b) {
cout << a << ' ';
pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
cout << a << ' ';
pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
cout << a << ' ';
pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
rep(i, n) {
if (i)
cout << ' ';
cout << a[i];
}
ln;
}
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};
typedef pair<ll, ll> P;
void Main() {
ll n;
R n;
ll a[n], b[n];
rep(i, n) R a[i];
rep(i, n) R b[i];
priority_queue<P> que;
que.push(P(0, -1));
rep(i, n) if (b[i] > a[i]) que.push(P(b[i], i));
ll ans = 0;
while (que.size() > 1) {
P p = que.top();
que.pop();
ll x = p.S;
P q = que.top();
ll z = b[(x - 1 + n) % n] + b[(x + 1) % n];
ll m = max(1LL, min((b[x] - a[x]) / z, (ll)ceil((double)(b[x] - q.F) / z)));
b[x] -= m * z;
ans += m;
if (b[x] < a[x]) {
pr(-1);
return;
}
if (b[x] > a[x])
que.push(P(b[x], x));
}
pr(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Main();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define Z class
#define ll long long
#define ln cout << '\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
template <Z A> void pr(A a) {
cout << a;
ln;
}
template <Z A, Z B> void pr(A a, B b) {
cout << a << ' ';
pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
cout << a << ' ';
pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
cout << a << ' ';
pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
rep(i, n) {
if (i)
cout << ' ';
cout << a[i];
}
ln;
}
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
dy[4] = {0, 1, 0, -1};
typedef pair<ll, ll> P;
void Main() {
ll n;
R n;
ll a[n], b[n];
rep(i, n) R a[i];
rep(i, n) R b[i];
priority_queue<P> que;
que.push(P(0, -1));
rep(i, n) if (b[i] > a[i]) que.push(P(b[i], i));
ll ans = 0;
while (que.size() > 1) {
P p = que.top();
que.pop();
ll x = p.S;
P q = que.top();
ll z = b[(x - 1 + n) % n] + b[(x + 1) % n];
// ll m=max(1LL,min((b[x]-a[x])/z,(ll)ceil((double)(b[x]-q.F)/z)));
ll m = max(1LL, (b[x] - a[x]) / z);
b[x] -= m * z;
ans += m;
if (b[x] < a[x]) {
pr(-1);
return;
}
if (b[x] > a[x])
que.push(P(b[x], x));
}
pr(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Main();
return 0;
}
| replace | 63 | 64 | 63 | 65 | TLE | |
p02941 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <queue>
#define st first
#define nd second
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e5 + 10;
int a[N], b[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
a[0] = a[n];
priority_queue<pii> pq;
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
if (b[i] != a[i])
pq.push({b[i], i % n});
}
b[0] = b[n];
ll ans = 0;
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int x = (p.nd + 1) % n, y = (p.nd - 1 + n) % n;
if (b[x] + b[y] >= b[p.nd])
return !printf("-1");
int sum = b[x] + b[y];
int ma = max(a[p.nd], sum);
int k = (b[p.nd] - ma + sum - 1) / sum;
b[p.nd] -= sum * k;
if (b[p.nd] < a[p.nd])
return !printf("-1");
ans += k;
if (b[p.nd] != a[p.nd])
pq.push({b[p.nd], p.nd});
}
printf("%lld", ans);
}
| #include <algorithm>
#include <cstdio>
#include <queue>
#define st first
#define nd second
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 2e5 + 10;
int a[N], b[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
a[0] = a[n];
priority_queue<pii> pq;
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
if (b[i] != a[i])
pq.push({b[i], i % n});
}
b[0] = b[n];
ll ans = 0;
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int x = (p.nd + 1) % n, y = (p.nd - 1 + n) % n;
if (b[x] + b[y] >= b[p.nd])
return !printf("-1");
int sum = b[x] + b[y];
int ma = max(a[p.nd], sum);
int k = (b[p.nd] - ma + sum - 1) / sum;
b[p.nd] -= sum * k;
if (b[p.nd] < a[p.nd])
return !printf("-1");
ans += k;
if (b[p.nd] != a[p.nd])
pq.push({b[p.nd], p.nd});
}
printf("%lld", ans);
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <assert.h>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define vi vector<int>
#define pii pair<int, int>
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define pi acos(-1)
#define mod 998244353 // 1000000007
#define inf 1000000007
#define ll long long
#define ull unsigned long long
#define DBG(x) cerr << (#x) << "=" << x << "\n";
#define N 200005
template <class U, class T> void Max(U &x, T y) {
if (x < y)
x = y;
}
template <class U, class T> void Min(U &x, T y) {
if (x > y)
x = y;
}
// template <class T> void add(int &a,T b){a=(a+b)%mod;}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
int pow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = 1LL * ans * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return ans;
}
int pow(int a, int b, int c) {
int ans = 1;
while (b) {
if (b & 1)
ans = 1LL * ans * a % c;
a = 1LL * a * a % c;
b >>= 1;
}
return ans;
}
int a[N], b[N];
int main() {
int T, m, i, j, k, n, K;
scanf("%d", &n);
set<pii> s;
rep(i, 0, n) scanf("%d", &a[i]);
rep(i, 0, n) { scanf("%d", &b[i]); }
rep(i, 0, n) {
if (b[i] < a[i]) {
puts("-1");
return 0;
} else if (b[i] > a[i]) {
s.insert({b[i], i});
}
}
ll ans = 0;
while (!s.empty()) {
auto it = s.end();
it--;
i = it->y;
s.erase(it);
j = (i - 1 + n) % n;
k = (i + 1) % n;
int dv = b[j] + b[k];
int v = b[i] - a[i];
if (a[i] >= dv) {
if (v % dv) {
puts("-1");
return 0;
}
ans += v / dv;
b[i] = a[i];
continue;
} else {
K = b[i] / dv;
ans += K;
b[i] -= K * dv;
}
if (b[i] < a[i]) {
puts("-1");
return 0;
} else if (b[i] > a[i])
s.insert({b[i], i});
}
printf("%lld\n", ans);
}
| #include <algorithm>
#include <assert.h>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define vi vector<int>
#define pii pair<int, int>
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) (int)x.size()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define pi acos(-1)
#define mod 998244353 // 1000000007
#define inf 1000000007
#define ll long long
#define ull unsigned long long
#define DBG(x) cerr << (#x) << "=" << x << "\n";
#define N 200005
template <class U, class T> void Max(U &x, T y) {
if (x < y)
x = y;
}
template <class U, class T> void Min(U &x, T y) {
if (x > y)
x = y;
}
// template <class T> void add(int &a,T b){a=(a+b)%mod;}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
int pow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1)
ans = 1LL * ans * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return ans;
}
int pow(int a, int b, int c) {
int ans = 1;
while (b) {
if (b & 1)
ans = 1LL * ans * a % c;
a = 1LL * a * a % c;
b >>= 1;
}
return ans;
}
int a[N], b[N];
int main() {
int T, m, i, j, k, n, K;
scanf("%d", &n);
set<pii> s;
rep(i, 0, n) scanf("%d", &a[i]);
rep(i, 0, n) { scanf("%d", &b[i]); }
rep(i, 0, n) {
if (b[i] < a[i]) {
puts("-1");
return 0;
} else if (b[i] > a[i]) {
s.insert({b[i], i});
}
}
ll ans = 0;
while (!s.empty()) {
auto it = s.end();
it--;
i = it->y;
s.erase(it);
j = (i - 1 + n) % n;
k = (i + 1) % n;
int dv = b[j] + b[k];
int v = b[i] - a[i];
if (a[i] >= dv) {
if (v % dv) {
puts("-1");
return 0;
}
ans += v / dv;
b[i] = a[i];
continue;
} else {
K = b[i] / dv;
if (K == 0)
K = 1;
ans += K;
b[i] -= K * dv;
}
if (b[i] < a[i]) {
puts("-1");
return 0;
} else if (b[i] > a[i])
s.insert({b[i], i});
}
printf("%lld\n", ans);
}
| insert | 101 | 101 | 101 | 103 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <map>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
// Attributes
typedef long long int lld;
int n;
lld operation = 0;
std::map<lld, std::vector<int>> indices;
std::vector<lld> init, result;
// Pop largest indices one. Return {result value, index}
std::pair<lld, int> pop_largest() {
auto largest_pointer = indices.rbegin();
lld largest = largest_pointer->first;
int index_candidate = largest_pointer->second.back();
largest_pointer->second.pop_back();
if (largest_pointer->second.empty())
indices.erase(largest);
return {largest, index_candidate};
}
// Push
void push(lld value, int index) { indices[value].push_back(index); }
void push(std::pair<lld, int> info) { push(info.first, info.second); }
// Near values {prev, next}
std::pair<lld, lld> near_values(int index) {
int previndex = (index + n - 1) % n, nextindex = (index + 1) % n;
return {result[previndex], result[nextindex]};
}
// Reduce given index by count.
void reduce(int index, lld count) {
std::pair<lld, lld> near_values_ = near_values(index);
result[index] -= count * (near_values_.first + near_values_.second);
}
void printResult() {
printf("Current result after %lld operation: [", operation);
for (int i = 0; i < n; i++)
printf("%lld, ", result[i]);
printf("]\n");
}
int main(void) {
// freopen("VScode/IO/input.txt", "r", stdin);
// freopen("VScode/IO/output.txt", "w", stdout);
// Get input
bool ok = true;
scanf("%d", &n);
init.resize(n);
result.resize(n);
for (int i = 0; i < n; i++)
scanf("%lld", &init[i]);
for (int i = 0; i < n; i++) {
scanf("%lld", &result[i]);
if (result[i] < init[i])
ok = false;
else if (result[i] > init[i])
push(result[i], i);
}
// Reduce from the largest
// printResult();
while (!indices.empty()) {
// Pop two largest values
std::pair<lld, int> largest_first = pop_largest();
lld current_value = largest_first.first;
int i = largest_first.second;
lld targetValue = init[i];
std::pair<lld, lld> near = near_values(i); // near values
if (!indices.empty()) {
std::pair<lld, int> largest_second = pop_largest();
lld secondaryTarget = largest_second.first;
if (targetValue < secondaryTarget)
targetValue = secondaryTarget;
push(largest_second);
}
// Go
lld reducable_count =
(current_value - targetValue) / (near.first + near.second);
// printf("Largest: (%lld, index %d), near values (%lld, %lld), targetValue
// %lld\n", result[i], i, near.first, near.second, targetValue);
if (reducable_count <= 0) {
if (current_value - near.first - near.second >= init[i]) {
reducable_count = 1;
} else {
// printf("Reducable count is %lld, break program.\n", reducable_count);
ok = false;
break;
}
}
// printf("Reducable count %lld\n", reducable_count);
reduce(i, reducable_count);
operation += reducable_count;
// printResult();
// Push again
if (result[i] > init[i])
push(result[i], i); // Push if only still remaining
}
printf("%lld\n", ok ? operation : -1);
return 0;
} | #include <map>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
// Attributes
typedef long long int lld;
int n;
lld operation = 0;
std::map<lld, std::vector<int>> indices;
std::vector<lld> init, result;
// Pop largest indices one. Return {result value, index}
std::pair<lld, int> pop_largest() {
auto largest_pointer = indices.rbegin();
lld largest = largest_pointer->first;
int index_candidate = largest_pointer->second.back();
largest_pointer->second.pop_back();
if (largest_pointer->second.empty())
indices.erase(largest);
return {largest, index_candidate};
}
// Push
void push(lld value, int index) { indices[value].push_back(index); }
void push(std::pair<lld, int> info) { push(info.first, info.second); }
// Near values {prev, next}
std::pair<lld, lld> near_values(int index) {
int previndex = (index + n - 1) % n, nextindex = (index + 1) % n;
return {result[previndex], result[nextindex]};
}
// Reduce given index by count.
void reduce(int index, lld count) {
std::pair<lld, lld> near_values_ = near_values(index);
result[index] -= count * (near_values_.first + near_values_.second);
}
void printResult() {
printf("Current result after %lld operation: [", operation);
for (int i = 0; i < n; i++)
printf("%lld, ", result[i]);
printf("]\n");
}
int main(void) {
// freopen("VScode/IO/input.txt", "r", stdin);
// freopen("VScode/IO/output.txt", "w", stdout);
// Get input
bool ok = true;
scanf("%d", &n);
init.resize(n);
result.resize(n);
for (int i = 0; i < n; i++)
scanf("%lld", &init[i]);
for (int i = 0; i < n; i++) {
scanf("%lld", &result[i]);
if (result[i] < init[i])
ok = false;
else if (result[i] > init[i])
push(result[i], i);
}
// Reduce from the largest
// printResult();
while (!indices.empty()) {
// Pop two largest values
std::pair<lld, int> largest_first = pop_largest();
lld current_value = largest_first.first;
int i = largest_first.second;
lld targetValue = init[i];
std::pair<lld, lld> near = near_values(i); // near values
/*if(!indices.empty()){
std::pair<lld, int> largest_second = pop_largest();
lld secondaryTarget = largest_second.first;
if(targetValue < secondaryTarget) targetValue = secondaryTarget;
push(largest_second);
}*/
if (targetValue < near.first + near.second + 1)
targetValue = near.first + near.second + 1;
// Go
lld reducable_count =
(current_value - targetValue) / (near.first + near.second);
// printf("Largest: (%lld, index %d), near values (%lld, %lld), targetValue
// %lld\n", result[i], i, near.first, near.second, targetValue);
if (reducable_count <= 0) {
if (current_value - near.first - near.second >= init[i]) {
reducable_count = 1;
} else {
// printf("Reducable count is %lld, break program.\n", reducable_count);
ok = false;
break;
}
}
// printf("Reducable count %lld\n", reducable_count);
reduce(i, reducable_count);
operation += reducable_count;
// printResult();
// Push again
if (result[i] > init[i])
push(result[i], i); // Push if only still remaining
}
printf("%lld\n", ok ? operation : -1);
return 0;
} | replace | 77 | 84 | 77 | 85 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <iostream>
#include <queue>
#include <utility>
#define llint long long
using namespace std;
typedef pair<llint, llint> P;
llint n;
llint a[200005], b[200005];
priority_queue<P> Q;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
for (int i = 0; i < n; i++) {
Q.push(make_pair(b[i], i));
}
llint ans = 0;
while (Q.size()) {
llint id = Q.top().second;
// cout << id << endl;
// for(int i = 0; i < n; i++) cout << b[i] << " "; cout << endl;
Q.pop();
if (b[id] == a[id])
continue;
if (b[id] < a[id]) {
cout << -1 << endl;
return 0;
}
llint d = b[(id + 1) % n] + b[(id - 1 + n) % n],
m = max(a[id], max(b[(id + 1) % n], b[(id - 1 + n) % n]));
llint c = (b[id] - m + d - 1) / d;
ans += c;
b[id] -= c * d;
Q.push(make_pair(b[id], id));
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <queue>
#include <utility>
#define llint long long
using namespace std;
typedef pair<llint, llint> P;
llint n;
llint a[200005], b[200005];
priority_queue<P> Q;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
for (int i = 0; i < n; i++) {
Q.push(make_pair(b[i], i));
}
llint ans = 0;
while (Q.size()) {
llint id = Q.top().second;
// cout << id << endl;
// for(int i = 0; i < n; i++) cout << b[i] << " "; cout << endl;
Q.pop();
if (b[id] == a[id])
continue;
if (b[id] < a[id]) {
cout << -1 << endl;
return 0;
}
llint d = b[(id + 1) % n] + b[(id - 1 + n) % n],
m = max(a[id], max(b[(id + 1) % n], b[(id - 1 + n) % n]));
llint c = (b[id] - m + d - 1) / d;
ans += c;
b[id] -= c * d;
Q.push(make_pair(b[id], id));
if (b[id] == a[id])
continue;
if (b[id] < a[id]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | insert | 44 | 44 | 44 | 51 | TLE | |
p02941 | C++ | Time Limit Exceeded | #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << "-"; \
} \
cout << endl
#define printLine2(l, c) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << c; \
} \
cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l) \
cout << #a << ": "; \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << a[__i__] << " "; \
} \
cout << endl
#define print2dArr(a, r, c) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
#define print2dArr2(a, r, c, l) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << setw(l) << setfill(' ') << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template <typename T> _Debug &operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[200000], B[200000];
priority_queue<pii> H;
int main() {
int i;
int N, d = 0;
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &A[i]);
for (i = 0; i < N; i++)
scanf("%d", &B[i]), H.push(mp(B[i], i)), d += (A[i] != B[i]);
for (i = 0; i < N; i++) {
if (B[i] < A[i]) {
printf("-1\n");
return 0;
}
}
LLI ans = 0;
while (d > 0) {
if (H.empty()) {
printf("-1\n");
return 0;
}
int u = H.top().second;
H.pop();
if (B[u] == A[u])
continue;
int c = B[(u + N - 1) % N] + B[(u + 1) % N];
if (c + A[u] > B[u])
continue;
d -= (B[u] != A[u]);
ans += (B[u] - A[u]) / c;
B[u] -= ((B[u] - A[u]) / c) * c;
d += (B[u] != A[u]);
H.push(mp(B[u], u));
}
printf("%lld\n", ans);
return 0;
}
| #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__, __j__;
#define printLine(l) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << "-"; \
} \
cout << endl
#define printLine2(l, c) \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << c; \
} \
cout << endl
#define printVar(n) cout << #n << ": " << n << endl
#define printArr(a, l) \
cout << #a << ": "; \
for (__i__ = 0; __i__ < l; __i__++) { \
cout << a[__i__] << " "; \
} \
cout << endl
#define print2dArr(a, r, c) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
#define print2dArr2(a, r, c, l) \
cout << #a << ":\n"; \
for (__i__ = 0; __i__ < r; __i__++) { \
for (__j__ = 0; __j__ < c; __j__++) { \
cout << setw(l) << setfill(' ') << a[__i__][__j__] << " "; \
} \
cout << endl; \
}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template <typename T> _Debug &operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l, c)
#define printVar(n)
#define printArr(a, l)
#define print2dArr(a, r, c)
#define print2dArr2(a, r, c, l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int, int> pii;
typedef pair<LLI, LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[200000], B[200000];
priority_queue<pii> H;
int main() {
int i;
int N, d = 0;
scanf("%d", &N);
for (i = 0; i < N; i++)
scanf("%d", &A[i]);
for (i = 0; i < N; i++)
scanf("%d", &B[i]), H.push(mp(B[i], i)), d += (A[i] != B[i]);
for (i = 0; i < N; i++) {
if (B[i] < A[i]) {
printf("-1\n");
return 0;
}
}
LLI ans = 0;
while (d > 0) {
if (H.empty()) {
printf("-1\n");
return 0;
}
int u = H.top().second;
H.pop();
if (B[u] == A[u])
continue;
int c = B[(u + N - 1) % N] + B[(u + 1) % N];
if ((LLI)c + A[u] > B[u])
continue;
d -= (B[u] != A[u]);
ans += (B[u] - A[u]) / c;
B[u] -= ((B[u] - A[u]) / c) * c;
d += (B[u] != A[u]);
H.push(mp(B[u], u));
}
printf("%lld\n", ans);
return 0;
}
| replace | 111 | 112 | 111 | 112 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
int len = d.size();
rep(i, len) {
s << d[i];
if (i < len - 1)
s << " ";
}
return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
int len = d.size();
rep(i, len) { s << d[i] << endl; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
s << "{ ";
for (auto itr = v.begin(); itr != v.end(); ++itr) {
if (itr != v.begin()) {
s << ", ";
}
s << (*itr);
}
s << " }";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
s << "{ ";
for (auto itr = v.begin(); itr != v.end(); ++itr) {
if (itr != v.begin()) {
s << ", ";
}
s << (*itr);
}
s << " }";
return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
s << "{" << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << " " << (*itr).first << " : " << (*itr).second << endl;
}
s << "}" << endl;
return s;
}
const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double EPS = 1e-10;
const double PI = acos(-1);
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a(n + 2), b(n + 2);
FOR(i, 1, n + 1) { cin >> a[i]; }
bool flag = true;
set<P> st;
FOR(i, 1, n + 1) {
cin >> b[i];
if (b[i] < a[i]) {
flag = false;
}
if (b[i] != a[i])
st.insert(P(b[i], i));
}
if (!flag) {
cout << -1 << endl;
return 0;
}
b[0] = b[n], b[n + 1] = b[1];
ll ans = 0;
while (true) {
bool flag = false;
FOR(i, 1, n + 1) {
if (a[i] != b[i])
flag = true;
}
if (!flag)
break;
auto itr = st.end();
itr--;
st.erase(itr);
auto [num, ind] = *itr;
ll diff = b[ind - 1] + b[ind + 1];
if (b[ind] <= diff) {
cout << -1 << endl;
return 0;
}
if ((b[ind] - a[ind]) % diff) {
ans += b[ind] / diff;
b[ind] %= diff;
if (b[ind] < a[ind]) {
cout << -1 << endl;
return 0;
}
st.insert(P(b[ind], ind));
} else {
ans += (b[ind] - a[ind]) / diff;
b[ind] = a[ind];
}
b[0] = b[n], b[n + 1] = b[1];
// cout<<b<<endl;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
int len = d.size();
rep(i, len) {
s << d[i];
if (i < len - 1)
s << " ";
}
return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
int len = d.size();
rep(i, len) { s << d[i] << endl; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
s << "{ ";
for (auto itr = v.begin(); itr != v.end(); ++itr) {
if (itr != v.begin()) {
s << ", ";
}
s << (*itr);
}
s << " }";
return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
s << "{ ";
for (auto itr = v.begin(); itr != v.end(); ++itr) {
if (itr != v.begin()) {
s << ", ";
}
s << (*itr);
}
s << " }";
return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
s << "{" << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
s << " " << (*itr).first << " : " << (*itr).second << endl;
}
s << "}" << endl;
return s;
}
const ll mod = 1e9 + 7;
const ll inf = 1e17;
const int INF = 1e9;
const double EPS = 1e-10;
const double PI = acos(-1);
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a(n + 2), b(n + 2);
FOR(i, 1, n + 1) { cin >> a[i]; }
bool flag = true;
set<P> st;
FOR(i, 1, n + 1) {
cin >> b[i];
if (b[i] < a[i]) {
flag = false;
}
if (b[i] != a[i])
st.insert(P(b[i], i));
}
if (!flag) {
cout << -1 << endl;
return 0;
}
b[0] = b[n], b[n + 1] = b[1];
ll ans = 0;
while (!st.empty()) {
auto itr = st.end();
itr--;
st.erase(itr);
auto [num, ind] = *itr;
ll diff = b[ind - 1] + b[ind + 1];
if (b[ind] <= diff) {
cout << -1 << endl;
return 0;
}
if ((b[ind] - a[ind]) % diff) {
ans += b[ind] / diff;
b[ind] %= diff;
if (b[ind] < a[ind]) {
cout << -1 << endl;
return 0;
}
st.insert(P(b[ind], ind));
} else {
ans += (b[ind] - a[ind]) / diff;
b[ind] = a[ind];
}
b[0] = b[n], b[n + 1] = b[1];
// cout<<b<<endl;
}
cout << ans << endl;
}
| replace | 109 | 117 | 109 | 110 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)
const ll MOD = 1000000007;
const ll INF = (ll)1e15;
ll A[200005];
ll B[200005];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
REP(i, N) { cin >> A[i]; }
REP(i, N) { cin >> B[i]; }
priority_queue<pll, vector<pll>> q;
REP(i, N) { q.push(pll(B[i], i)); }
ll ans = 0;
while (!q.empty()) {
pll t = q.top();
q.pop();
if (B[t.second] <= A[t.second])
continue;
ans++;
ll s = B[(t.second + 1) % N] + B[(N + t.second - 1) % N];
B[t.second] = std::max(0LL, t.first - s);
if (B[t.second] == 0)
break;
q.push(pll(B[t.second], t.second));
}
REP(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)
const ll MOD = 1000000007;
const ll INF = (ll)1e15;
ll A[200005];
ll B[200005];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
REP(i, N) { cin >> A[i]; }
REP(i, N) { cin >> B[i]; }
priority_queue<pll, vector<pll>> q;
REP(i, N) { q.push(pll(B[i], i)); }
ll ans = 0;
while (!q.empty()) {
pll t = q.top();
q.pop();
if (B[t.second] <= A[t.second])
continue;
ll left = B[(t.second - 1 + N) % N];
ll right = B[(t.second + 1) % N];
ll s = left + right;
ll n = std::min((t.first - std::max(left, right) + s - 1) / s,
(t.first - A[t.second] + s - 1) / s);
ans += n;
B[t.second] = t.first - n * s;
if (B[t.second] <= 0)
break;
q.push(pll(B[t.second], t.second));
}
REP(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | replace | 33 | 37 | 33 | 41 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
#define VEC(type, name, size) \
V<type> name(size); \
IN(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
IN(name)
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
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;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
const ll INF = 1e18;
#pragma endregion
using P = pair<int, int>;
int main() {
ios;
INT(n);
VEC(int, a, n);
VEC(int, b, n);
priority_queue<P> q;
REP(i, n) {
if (b[i] > a[i])
q.push({b[i], i});
if (b[i] < a[i]) {
print(-1);
return 0;
}
}
ll cnt = 0;
while (SZ(q)) {
P p = q.top();
q.pop();
ll s = p.second;
ll sum = b[(s ? s - 1 : n - 1)] + b[(s == n - 1 ? 0 : s + 1)];
cnt += (b[s] - a[s]) / sum;
b[s] -= (b[s] - a[s]) / sum * sum;
if (a[s] != b[s])
q.push({b[s], s});
}
print(cnt);
} | #include <bits/stdc++.h>
using namespace std;
#pragma region Macros
#define ios \
ios::sync_with_stdio(false); \
cin.tie(nullptr);
#define VEC(type, name, size) \
V<type> name(size); \
IN(name)
#define VVEC(type, name, h, w) \
VV<type> name(h, V<type>(w)); \
IN(name)
#define INT(...) \
int __VA_ARGS__; \
IN(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
IN(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
IN(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
IN(__VA_ARGS__)
#define DOUBLE(...) \
DOUBLE __VA_ARGS__; \
IN(__VA_ARGS__)
#define LD(...) \
LD __VA_ARGS__; \
IN(__VA_ARGS__)
template <class T> void scan(T &a) { cin >> a; }
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
scan(head);
IN(tail...);
}
template <class T> inline void print(T x) { cout << x << '\n'; }
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
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;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
const ll INF = 1e18;
#pragma endregion
using P = pair<int, int>;
int main() {
ios;
INT(n);
VEC(int, a, n);
VEC(int, b, n);
priority_queue<P> q;
REP(i, n) {
if (b[i] > a[i])
q.push({b[i], i});
if (b[i] < a[i]) {
print(-1);
return 0;
}
}
ll cnt = 0;
while (SZ(q)) {
P p = q.top();
q.pop();
ll s = p.second;
ll sum = b[(s ? s - 1 : n - 1)] + b[(s == n - 1 ? 0 : s + 1)];
ll t = (b[s] - a[s]) / sum;
if (!t) {
print(-1);
return 0;
}
cnt += t;
b[s] -= t * sum;
if (a[s] != b[s])
q.push({b[s], s});
}
print(cnt);
} | replace | 124 | 126 | 124 | 131 | TLE | |
p02941 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define P pair<ll, ll>
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define sortAl(v) sort(v.begin(), v.end())
#define sortAlr(v) \
sort(v.begin(), v.end()); \
reverse(v.begin(), v.end())
#define cout(n) cout << fixed << setprecision(n)
ll f(ll a, ll n) { return (a + n) % n; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
auto compare = [](P a, P b) { return a.first < b.first; };
std::priority_queue<P, std::vector<P>,
decltype(compare) // 比較関数オブジェクトを指定
>
q{compare};
ll n;
cin >> n;
vector<ll> in(n);
FOR(i, 0, n) cin >> in[i];
vector<ll> out(n);
FOR(i, 0, n) cin >> out[i];
FOR(i, 0, n) q.push(P(out[i], i));
ll res = 0;
ll pre = -1;
while (!q.empty() && pre != q.top().second) {
ll val = q.top().first;
ll idx = q.top().second;
pre = idx;
q.pop();
if (val == in[idx])
continue;
if (val <= out[f(idx - 1, n)] + out[f(idx + 1, n)])
continue;
while (out[idx] > out[f(idx - 1, n)] + out[f(idx + 1, n)] &&
out[idx] > in[idx]) {
out[idx] -= out[f(idx - 1, n)] + out[f(idx + 1, n)];
res++;
}
ll dif = out[idx] - in[idx];
ll t_res = dif / (out[f(idx - 1, n)] + out[f(idx + 1, n)]);
res += t_res;
out[idx] -= t_res * (out[f(idx - 1, n)] + out[f(idx + 1, n)]);
if (out[idx] > in[idx])
q.push(P(out[idx], idx));
}
// FOR(i,0,n)cout<<out[i]<<" ";
// cout<<endl;
FOR(i, 0, n) if (in[i] != out[i]) res = -1;
cout << res << endl;
} | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define P pair<ll, ll>
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define FORr(i, m, n) for (ll i = n; i >= (ll)m; i--)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define sortAl(v) sort(v.begin(), v.end())
#define sortAlr(v) \
sort(v.begin(), v.end()); \
reverse(v.begin(), v.end())
#define cout(n) cout << fixed << setprecision(n)
ll f(ll a, ll n) { return (a + n) % n; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
auto compare = [](P a, P b) { return a.first < b.first; };
std::priority_queue<P, std::vector<P>,
decltype(compare) // 比較関数オブジェクトを指定
>
q{compare};
ll n;
cin >> n;
vector<ll> in(n);
FOR(i, 0, n) cin >> in[i];
vector<ll> out(n);
FOR(i, 0, n) cin >> out[i];
FOR(i, 0, n) q.push(P(out[i], i));
ll res = 0;
ll pre = -1;
while (!q.empty() && pre != q.top().second) {
ll val = q.top().first;
ll idx = q.top().second;
pre = idx;
q.pop();
if (val == in[idx])
continue;
if (val <= out[f(idx - 1, n)] + out[f(idx + 1, n)])
continue;
ll dif = out[idx] - in[idx];
ll t_res = dif / (out[f(idx - 1, n)] + out[f(idx + 1, n)]);
res += t_res;
out[idx] -= t_res * (out[f(idx - 1, n)] + out[f(idx + 1, n)]);
if (out[idx] > in[idx])
q.push(P(out[idx], idx));
}
// FOR(i,0,n)cout<<out[i]<<" ";
// cout<<endl;
FOR(i, 0, n) if (in[i] != out[i]) res = -1;
cout << res << endl;
} | delete | 48 | 53 | 48 | 48 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <cstdio>
#include <queue>
using namespace std;
int a[200000];
int b[200000];
int main() {
int n, i;
long long ans = 0;
priority_queue<pair<int, int>> q;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
scanf("%d", &b[i]);
for (i = 0; i < n; i++)
q.push(make_pair(b[i], i));
while (!q.empty()) {
int x = q.top().second, l, r, c;
q.pop();
if (b[x] < a[x]) {
puts("-1");
return 0;
} else if (b[x] == a[x]) {
continue;
}
l = (x - 1 + n) % n;
r = (x + 1) % n;
if (b[x] <= b[l] + b[r]) {
puts("-1");
return 0;
}
c = (b[x] - a[x]) / (b[l] + b[r]);
ans += c;
b[x] -= (b[l] + b[r]) * c;
q.push(make_pair(b[x], x));
}
printf("%lld\n", ans);
return 0;
}
| #include <cstdio>
#include <queue>
using namespace std;
int a[200000];
int b[200000];
int main() {
int n, i;
long long ans = 0;
priority_queue<pair<int, int>> q;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
scanf("%d", &b[i]);
for (i = 0; i < n; i++)
q.push(make_pair(b[i], i));
while (!q.empty()) {
int x = q.top().second, l, r, c;
q.pop();
if (b[x] < a[x]) {
puts("-1");
return 0;
} else if (b[x] == a[x]) {
continue;
}
l = (x - 1 + n) % n;
r = (x + 1) % n;
if (b[x] <= b[l] + b[r]) {
puts("-1");
return 0;
}
c = (b[x] - a[x]) / (b[l] + b[r]);
if (c == 0) {
puts("-1");
return 0;
}
ans += c;
b[x] -= (b[l] + b[r]) * c;
q.push(make_pair(b[x], x));
}
printf("%lld\n", ans);
return 0;
}
| insert | 44 | 44 | 44 | 50 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int n, a[200010], b[200010];
int norm(int x) {
x = ((x % n) + n) % n;
return x ? x : n;
}
int &A(int i) { return a[norm(i)]; }
int &B(int i) { return b[norm(i)]; }
bool f(int i) { return B(i) >= B(i - 1) + B(i + 1) + A(i); }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
priority_queue<pii> q;
long long ans = 0;
for (int i = 1; i <= n; i++)
if (f(i))
q.push({B(i), i});
while (!q.empty()) {
int i = q.top().second;
q.pop();
int k = (B(i) - A(i)) / (B(i - 1) + B(i + 1));
ans += k;
B(i) -= k * (B(i - 1) + B(i + 1));
if (f(i - 1))
q.push({B(i - 1), i - 1});
if (f(i + 1))
q.push({B(i + 1), i + 1});
}
for (int i = 1; i <= n; i++)
if (A(i) != B(i)) {
puts("-1");
return 0;
}
printf("%lld", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int n, a[200010], b[200010];
int norm(int x) {
x = ((x % n) + n) % n;
return x ? x : n;
}
int &A(int i) { return a[norm(i)]; }
int &B(int i) { return b[norm(i)]; }
bool f(int i) { return B(i) >= B(i - 1) + B(i + 1) + A(i); }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
priority_queue<pii> q;
long long ans = 0;
for (int i = 1; i <= n; i++)
if (f(i))
q.push({B(i), i});
while (!q.empty()) {
int i = q.top().second;
q.pop();
int k = (B(i) - A(i)) / (B(i - 1) + B(i + 1));
if (!k) {
puts("-1");
return 0;
}
ans += k;
B(i) -= k * (B(i - 1) + B(i + 1));
if (f(i - 1))
q.push({B(i - 1), i - 1});
if (f(i + 1))
q.push({B(i + 1), i + 1});
}
for (int i = 1; i <= n; i++)
if (A(i) != B(i)) {
puts("-1");
return 0;
}
printf("%lld", ans);
return 0;
} | insert | 29 | 29 | 29 | 33 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n;
cin >> n;
long long a[n], b[n];
multiset<pair<long long, int>> memo;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
if (a[i] != b[i]) {
memo.insert(make_pair(b[i], i));
}
}
long long ans = 0LL;
while (memo.size() > 0) {
for (auto i = memo.end();; --i) {
if (i == memo.end()) {
--i;
}
auto p = *i;
int l = (p.second - 1 + n) % n, r = (p.second + 1) % n;
if (b[p.second] > b[l] + b[r]) {
memo.erase(i);
b[p.second] -= a[p.second];
ans += b[p.second] / (b[l] + b[r]);
b[p.second] %= (b[l] + b[r]);
b[p.second] += a[p.second];
if (a[p.second] != b[p.second]) {
memo.insert(make_pair(b[p.second], p.second));
}
break;
}
if (i == memo.begin()) {
cout << "-1\n";
return 0;
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n;
cin >> n;
long long a[n], b[n];
multiset<pair<long long, int>> memo;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
if (a[i] != b[i]) {
memo.insert(make_pair(b[i], i));
}
}
long long ans = 0LL;
while (memo.size() > 0) {
auto i = memo.end();
--i;
auto p = *i;
int l = (p.second - 1 + n) % n, r = (p.second + 1) % n;
memo.erase(i);
b[p.second] -= a[p.second];
if (b[p.second] / (b[l] + b[r]) == 0) {
cout << "-1\n";
return 0;
}
ans += b[p.second] / (b[l] + b[r]);
b[p.second] %= (b[l] + b[r]);
b[p.second] += a[p.second];
if (a[p.second] != b[p.second]) {
memo.insert(make_pair(b[p.second], p.second));
}
}
cout << ans << endl;
return 0;
}
| replace | 19 | 40 | 19 | 34 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, a[100100], b[100100];
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
set<pair<int, int>> s;
for (int i = 0; i < n; i++) {
cin >> b[i];
s.insert({b[i], i});
}
long long ans = 0;
while (!s.empty()) {
int i = s.rbegin()->second;
s.erase(*s.rbegin());
if (b[i] <= a[i])
continue;
int sum = b[(i + 1) % n] + b[(i + n - 1) % n];
int op = (b[i] - a[i]) / sum;
if (op) {
ans += op;
b[i] -= op * sum;
s.insert({b[i], i});
} else {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < n; i++)
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, a[200100], b[200100];
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
set<pair<int, int>> s;
for (int i = 0; i < n; i++) {
cin >> b[i];
s.insert({b[i], i});
}
long long ans = 0;
while (!s.empty()) {
int i = s.rbegin()->second;
s.erase(*s.rbegin());
if (b[i] <= a[i])
continue;
int sum = b[(i + 1) % n] + b[(i + n - 1) % n];
int op = (b[i] - a[i]) / sum;
if (op) {
ans += op;
b[i] -= op * sum;
s.insert({b[i], i});
} else {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < n; i++)
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
cout << ans << endl;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ll long long
typedef pair<ll, ll> pll;
typedef vector<pll> vi;
#define out(x) return cout << x, 0;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define coutif(a, b, c) cout << (a ? b : c)
int main() {
ll n, m, j, k = 0, ans = 0, t = 0, x, y, i;
cin >> n;
ll a[n], b[n];
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n; i++)
cin >> b[i];
for (j = 0; j < 1000; j++) {
for (i = 0; i < n; i++) {
x = (i + n - 1) % n, y = (i + 1) % n;
k = b[x] + b[y];
if (k < b[i]) {
t = (b[i] - a[i]) / k;
b[i] -= k * t;
ans += t;
}
}
}
for (i = 0; i < n; i++)
if (a[i] != b[i])
out(-1);
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define ll long long
typedef pair<ll, ll> pll;
typedef vector<pll> vi;
#define out(x) return cout << x, 0;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define coutif(a, b, c) cout << (a ? b : c)
int main() {
ll n, m, j, k = 0, ans = 0, t = 0, x, y, i;
cin >> n;
ll a[n], b[n];
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n; i++)
cin >> b[i];
for (j = 0; j < 100; j++) {
for (i = 0; i < n; i++) {
x = (i + n - 1) % n, y = (i + 1) % n;
k = b[x] + b[y];
if (k < b[i]) {
t = (b[i] - a[i]) / k;
b[i] -= k * t;
ans += t;
}
}
}
for (i = 0; i < n; i++)
if (a[i] != b[i])
out(-1);
cout << ans;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1e9 + 7;
const int BASE = 27;
const int N = 100001;
const ll oo = 1e18;
const double EPS = 1e-9;
int n, a[N], b[N];
priority_queue<pii> q;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", b + i);
q.push(mp(b[i], i));
}
ll ans = 0;
while (q.size()) {
pii f = q.top();
q.pop();
if (b[f.y] == a[f.y])
continue;
if (a[f.y] > b[f.y]) {
puts("-1");
return 0;
}
int nxt_idx = (f.y + 1) % n;
int prv_idx = (f.y - 1 + n) % n;
int rem = b[nxt_idx] + b[prv_idx];
int g = (b[f.y] - a[f.y]) / rem;
int newVal = f.x - g * rem;
b[f.y] = newVal;
ans += g;
if (!g && newVal != a[f.y]) {
puts("-1");
return 0;
}
q.push(make_pair(newVal, f.y));
}
printf("%lld\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1e9 + 7;
const int BASE = 27;
const int N = 200001;
const ll oo = 1e18;
const double EPS = 1e-9;
int n, a[N], b[N];
priority_queue<pii> q;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", b + i);
q.push(mp(b[i], i));
}
ll ans = 0;
while (q.size()) {
pii f = q.top();
q.pop();
if (b[f.y] == a[f.y])
continue;
if (a[f.y] > b[f.y]) {
puts("-1");
return 0;
}
int nxt_idx = (f.y + 1) % n;
int prv_idx = (f.y - 1 + n) % n;
int rem = b[nxt_idx] + b[prv_idx];
int g = (b[f.y] - a[f.y]) / rem;
int newVal = f.x - g * rem;
b[f.y] = newVal;
ans += g;
if (!g && newVal != a[f.y]) {
puts("-1");
return 0;
}
q.push(make_pair(newVal, f.y));
}
printf("%lld\n", ans);
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
int main() {
using namespace std;
unsigned long N;
cin >> N;
vector<unsigned long> A(N), B(N);
copy_n(istream_iterator<unsigned long>(cin), N, A.begin());
priority_queue<pair<unsigned long, unsigned long>> pq;
for (unsigned long i = 0; i < N; ++i) {
cin >> B[i];
if (B[i] != A[i])
pq.emplace(B[i], i);
}
unsigned long ans{0};
while (!pq.empty()) {
auto t = pq.top().second;
pq.pop();
auto bf = (t + N - 1) % N, nx = (t + 1) % N;
if (B[t] < B[bf] + B[nx] + A[t])
return 0 & puts("-1");
auto tmp = (B[t] - A[t]) / (B[bf] + B[nx]);
if (!pq.empty())
tmp = min(tmp,
(B[t] - pq.top().first + B[bf] + B[nx] - 1) / (B[bf], B[nx]));
ans += tmp;
if ((B[t] -= tmp * (B[bf] + B[nx])) > A[t])
pq.emplace(B[t], t);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
int main() {
using namespace std;
unsigned long N;
cin >> N;
vector<unsigned long> A(N), B(N);
copy_n(istream_iterator<unsigned long>(cin), N, A.begin());
priority_queue<pair<unsigned long, unsigned long>> pq;
for (unsigned long i = 0; i < N; ++i) {
cin >> B[i];
if (B[i] != A[i])
pq.emplace(B[i], i);
}
unsigned long ans{0};
while (!pq.empty()) {
auto t = pq.top().second;
pq.pop();
auto bf = (t + N - 1) % N, nx = (t + 1) % N;
if (B[t] < B[bf] + B[nx] + A[t])
return 0 & puts("-1");
auto tmp = (B[t] - A[t]) / (B[bf] + B[nx]);
ans += tmp;
if ((B[t] -= tmp * (B[bf] + B[nx])) > A[t])
pq.emplace(B[t], t);
}
cout << ans << endl;
return 0;
} | delete | 25 | 28 | 25 | 25 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 mt(736);
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto &it : a)
cin >> it;
priority_queue<pair<int, int>> pq;
for (size_t i = 0; i < b.size(); i++) {
cin >> b[i];
if (b[i] < a[i]) {
cout << -1 << endl;
return;
}
pq.emplace(b[i], i);
}
ll cnt = 0;
while (!pq.empty()) {
int wh = pq.top().second;
pq.pop();
assert(b[wh] >= a[wh]);
if (b[wh] > a[wh]) {
auto sub = b[wh == 0 ? n - 1 : wh - 1] + b[wh + 1 == n ? 0 : wh + 1];
if ((b[wh] - a[wh]) % sub == 0) {
cnt += (b[wh] - a[wh]) / sub;
b[wh] = a[wh];
} else {
cnt += b[wh] / sub;
b[wh] %= sub;
}
if (b[wh] < a[wh]) {
cout << -1 << endl;
return;
}
pq.emplace(b[wh], wh);
}
}
cout << cnt << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
ifstream fin("../input.txt");
solve(fin);
cout << setprecision(4);
cout << "clock: " << clock() / (double)CLOCKS_PER_SEC << endl;
#else
solve();
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 mt(736);
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto &it : a)
cin >> it;
priority_queue<pair<int, int>> pq;
for (size_t i = 0; i < b.size(); i++) {
cin >> b[i];
if (b[i] < a[i]) {
cout << -1 << endl;
return;
}
pq.emplace(b[i], i);
}
ll cnt = 0;
while (!pq.empty()) {
int wh = pq.top().second;
pq.pop();
assert(b[wh] >= a[wh]);
if (b[wh] > a[wh]) {
auto sub = b[wh == 0 ? n - 1 : wh - 1] + b[wh + 1 == n ? 0 : wh + 1];
if (sub > b[wh]) {
cout << -1 << endl;
return;
}
if ((b[wh] - a[wh]) % sub == 0) {
cnt += (b[wh] - a[wh]) / sub;
b[wh] = a[wh];
} else {
cnt += b[wh] / sub;
b[wh] %= sub;
}
if (b[wh] < a[wh]) {
cout << -1 << endl;
return;
}
pq.emplace(b[wh], wh);
}
}
cout << cnt << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
ifstream fin("../input.txt");
solve(fin);
cout << setprecision(4);
cout << "clock: " << clock() / (double)CLOCKS_PER_SEC << endl;
#else
solve();
#endif
return 0;
}
| insert | 43 | 43 | 43 | 49 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> a, b;
priority_queue<pair<int, int>> pq;
long long solve();
int main() {
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> b[i];
cout << solve() << endl;
return 0;
}
long long solve() {
long long cnt = 0;
for (int i = 0; i < n; ++i)
if (b[i] != a[i])
pq.push({b[i] - a[i], i});
while (!pq.empty()) {
pair<int, int> now = pq.top();
pq.pop();
if (now.first == 0)
break;
++cnt;
b[now.second] -= b[(now.second + 1) % n];
b[now.second] -= b[(now.second - 1 + n) % n];
if (b[now.second] - a[now.second] < 0)
return -1;
if (b[now.second] == a[now.second])
continue;
pq.push({b[now.second] - a[now.second], now.second});
}
return cnt;
}
| #include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> a, b;
priority_queue<pair<int, int>> pq;
long long solve();
int main() {
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> b[i];
cout << solve() << endl;
return 0;
}
long long solve() {
long long cnt = 0;
for (int i = 0; i < n; ++i)
if (b[i] != a[i])
pq.push({b[i] - a[i], i});
while (!pq.empty()) {
pair<int, int> now = pq.top();
pq.pop();
if (now.first == 0)
break;
long long mnum = b[(now.second + 1) % n] + b[(now.second - 1 + n) % n],
nowc;
nowc = (b[now.second] - a[now.second]) / mnum;
cnt += nowc;
b[now.second] -= nowc * mnum;
if (nowc <= 0)
return -1;
if (b[now.second] == a[now.second])
continue;
pq.push({b[now.second] - a[now.second], now.second});
}
return cnt;
}
| replace | 31 | 35 | 31 | 37 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = 2e5;
int n;
num a[nsz + 5], b[nsz + 5], ans;
priority_queue<pair<num, int>> q;
int inline pre(int id) { return id != 1 ? id - 1 : n; }
int inline nxt(int id) { return id != n ? id + 1 : 1; }
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
cont(i, n) { cin >> a[i]; }
cont(i, n) {
cin >> b[i];
if (a[i] != b[i]) {
q.push(make_pair(b[i], i));
}
}
for (; !q.empty();) {
int id = q.top().second;
num cur = q.top().first;
q.pop();
num del = b[pre(id)] + b[nxt(id)], cnt = (b[id] - a[id]) / del;
ans += cnt;
b[id] = cur - cnt * del;
if (a[id] > b[id]) {
ans = -1;
break;
} else if (a[id] != b[id]) {
q.push(make_pair(b[id], id));
}
}
cout << ans << ln;
}
| #include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fr, to) for (int i = fr; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = 2e5;
int n;
num a[nsz + 5], b[nsz + 5], ans;
priority_queue<pair<num, int>> q;
int inline pre(int id) { return id != 1 ? id - 1 : n; }
int inline nxt(int id) { return id != n ? id + 1 : 1; }
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
cont(i, n) { cin >> a[i]; }
cont(i, n) {
cin >> b[i];
if (a[i] != b[i]) {
q.push(make_pair(b[i], i));
}
}
for (; !q.empty();) {
int id = q.top().second;
num cur = q.top().first;
q.pop();
num del = b[pre(id)] + b[nxt(id)], cnt = max(1LL, (b[id] - a[id]) / del);
ans += cnt;
b[id] = cur - cnt * del;
if (a[id] > b[id]) {
ans = -1;
break;
} else if (a[id] != b[id]) {
q.push(make_pair(b[id], id));
}
}
cout << ans << ln;
}
| replace | 37 | 38 | 37 | 38 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<long long>;
using ld = long double;
#define unordered_map __fast_unordered_map
template <class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;
template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }
template <class TH> void _dbg(const char *sdbg, TH h) {
cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',')
cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V)
os << vv << ",";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.st << "," << P.nd << ")";
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr \
if (0) \
cout
#endif
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n;
cin >> n;
vi a(n), b(n);
for (int &x : a) {
cin >> x;
}
for (int &x : b) {
cin >> x;
}
multiset<pii> b_set;
for (int i = 0; i < n; ++i) {
if (b[i] > a[i]) {
b_set.emplace(b[i], i);
}
}
ll num_ops = 0;
while (!b_set.empty()) {
int val, loc;
tie(val, loc) = *b_set.rbegin();
debug(val, loc, a, b);
b_set.erase(prev(b_set.end()));
assert(val > a[loc]);
const ll x = b[(loc + n - 1) % n];
const ll y = b[(loc + 1) % n];
const ll diff = x + y;
if (diff > b[loc]) {
cout << "-1\n";
return 0;
}
const int bound = max<ll>({a[loc], x - diff, y - diff});
const int max_steps = (b[loc] - bound) / diff;
num_ops += max_steps;
b[loc] -= max_steps * diff;
if (b[loc] > a[loc]) {
b_set.emplace(b[loc], loc);
}
}
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
cout << "-1\n";
return 0;
}
}
cout << num_ops << "\n";
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<long long>;
using ld = long double;
#define unordered_map __fast_unordered_map
template <class Key, class Value, class Hash = std::hash<Key>>
using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>;
template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }
template <class TH> void _dbg(const char *sdbg, TH h) {
cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',')
cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V)
os << vv << ",";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.st << "," << P.nd << ")";
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr \
if (0) \
cout
#endif
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n;
cin >> n;
vi a(n), b(n);
for (int &x : a) {
cin >> x;
}
for (int &x : b) {
cin >> x;
}
multiset<pii> b_set;
for (int i = 0; i < n; ++i) {
if (b[i] > a[i]) {
b_set.emplace(b[i], i);
}
}
ll num_ops = 0;
while (!b_set.empty()) {
int val, loc;
tie(val, loc) = *b_set.rbegin();
debug(val, loc, a, b);
b_set.erase(prev(b_set.end()));
assert(val > a[loc]);
const ll x = b[(loc + n - 1) % n];
const ll y = b[(loc + 1) % n];
const ll diff = x + y;
if (diff > b[loc] - a[loc]) {
cout << "-1\n";
return 0;
}
const int bound = max<ll>({a[loc], x - diff, y - diff});
const int max_steps = (b[loc] - bound) / diff;
num_ops += max_steps;
b[loc] -= max_steps * diff;
if (b[loc] > a[loc]) {
b_set.emplace(b[loc], loc);
}
}
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
cout << "-1\n";
return 0;
}
}
cout << num_ops << "\n";
}
| replace | 88 | 89 | 88 | 89 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, arr[202020], brr[202020];
ll ans;
priority_queue<pii> pq;
inline int pv(int x) {
if (x == 0)
return n - 1;
return --x;
}
inline int nx(int x) {
if (x == n - 1)
return 0;
return ++x;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
cin >> brr[i];
for (int i = 0; i < n; i++)
pq.push(pii(brr[i], i));
while (!pq.empty()) {
pii tmp = pq.top();
pq.pop();
// cout << tmp.va << ' ' << tmp.vb << '\n';
int v = tmp.va, i = tmp.vb;
if (v < arr[i] + brr[pv(i)] + brr[nx(i)]) {
if (brr[i] == arr[i] && brr[pv(i)] == arr[pv(i)] &&
brr[nx(i)] == arr[nx(i)])
continue;
break;
}
ans += (ll)(brr[i] - arr[i]) / (brr[pv(i)] + brr[nx(i)]);
brr[i] = (brr[i] - arr[i]) % (brr[pv(i)] + brr[nx(i)]) + arr[i];
pq.push(pii(brr[i], i));
}
for (int i = 0; i < n; i++) {
if (arr[i] == brr[i])
continue;
cout << -1;
return 0;
}
cout << ans;
} | #include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, arr[202020], brr[202020];
ll ans;
priority_queue<pii> pq;
inline int pv(int x) {
if (x == 0)
return n - 1;
return --x;
}
inline int nx(int x) {
if (x == n - 1)
return 0;
return ++x;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
cin >> brr[i];
for (int i = 0; i < n; i++)
pq.push(pii(brr[i], i));
while (!pq.empty()) {
pii tmp = pq.top();
pq.pop();
// cout << tmp.va << ' ' << tmp.vb << '\n';
int v = tmp.va, i = tmp.vb;
if (v < arr[i] + brr[pv(i)] + brr[nx(i)]) {
if (brr[i] == arr[i] && brr[pv(i)] == arr[pv(i)] &&
brr[nx(i)] == arr[nx(i)])
continue;
break;
}
ans += (ll)(brr[i] - arr[i]) / (brr[pv(i)] + brr[nx(i)]);
brr[i] = (brr[i] - arr[i]) % (brr[pv(i)] + brr[nx(i)]) + arr[i];
if (brr[i] < brr[nx(i)] || brr[i] < brr[pv(i)])
pq.push(pii(brr[i], i));
}
for (int i = 0; i < n; i++) {
if (arr[i] == brr[i])
continue;
cout << -1;
return 0;
}
cout << ans;
} | replace | 53 | 54 | 53 | 55 | TLE | |
p02941 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d) \
(RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
struct Edge {
int from, to;
ll w;
bool operator<(const Edge &rhs) const {
return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
}
};
int mod_pow(int x, int n, int mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = 1LL * x * x % mod;
n >>= 1;
}
return res;
}
typedef vector<vector<Edge>> Graph;
template <unsigned mod> struct RollingHash {
vector<unsigned> hashed, power;
unsigned base = 10007;
inline unsigned mul(unsigned a, unsigned b) const {
return (1LL * a * b) % mod;
}
RollingHash(const string &s) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
unsigned get(int l, int r) const {
unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned connect(unsigned h1, unsigned h2, ll h2len) const {
unsigned ret = mul(h1, mod_pow(base, h2len, mod)) + h2;
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned powstr(unsigned h2, int h2len, ll ts) const {
if (ts == 0)
return 0;
unsigned han = powstr(h2, h2len, ts / 2);
unsigned ret = connect(han, han, h2len * (ts / 2));
if (ts % 2)
ret = connect(ret, h2, h2len);
return ret;
}
/*unsigned powstr(unsigned h2, int h2len, ll ts) const {
unsigned ret = 0;
REP(i, ts)
ret = connect(ret, h2, h2len);
return ret;
}*/
int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
int len = min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
low = mid;
else
high = mid;
}
return (low);
}
};
using RH = RollingHash<1000000007>;
#define issel(i) \
(b[(i + n) % n] - b[(i - 1 + n) % n] - b[(i + 1 + n) % n] >= a[(i + n) % n])
signed main(void) {
int n;
cin >> n;
vector<ll> a(n), b(n);
REP(i, n)
scanf("%lld", &a[i]);
REP(i, n)
scanf("%lld", &b[i]);
set<int> cand;
REP(i, n) {
if (issel(i))
cand.insert(i);
}
ll ans = 0;
while (cand.size()) {
int cur = *cand.begin();
cand.erase(cand.begin());
ans += b[cur] / (b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n]);
b[cur] %= b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n];
if (issel(cur - 1))
cand.insert((cur - 1 + n) % n);
if (issel(cur + 1))
cand.insert((cur + 1 + n) % n);
}
REP(i, n)
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d) \
(RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
struct Edge {
int from, to;
ll w;
bool operator<(const Edge &rhs) const {
return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
}
};
int mod_pow(int x, int n, int mod) {
ll res = 1;
while (n > 0) {
if (n & 1)
res = res * x % mod;
x = 1LL * x * x % mod;
n >>= 1;
}
return res;
}
typedef vector<vector<Edge>> Graph;
template <unsigned mod> struct RollingHash {
vector<unsigned> hashed, power;
unsigned base = 10007;
inline unsigned mul(unsigned a, unsigned b) const {
return (1LL * a * b) % mod;
}
RollingHash(const string &s) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
unsigned get(int l, int r) const {
unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned connect(unsigned h1, unsigned h2, ll h2len) const {
unsigned ret = mul(h1, mod_pow(base, h2len, mod)) + h2;
if (ret >= mod)
ret -= mod;
return ret;
}
unsigned powstr(unsigned h2, int h2len, ll ts) const {
if (ts == 0)
return 0;
unsigned han = powstr(h2, h2len, ts / 2);
unsigned ret = connect(han, han, h2len * (ts / 2));
if (ts % 2)
ret = connect(ret, h2, h2len);
return ret;
}
/*unsigned powstr(unsigned h2, int h2len, ll ts) const {
unsigned ret = 0;
REP(i, ts)
ret = connect(ret, h2, h2len);
return ret;
}*/
int LCP(const RollingHash<mod> &b, int l1, int r1, int l2, int r2) {
int len = min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while (high - low > 1) {
int mid = (low + high) / 2;
if (get(l1, l1 + mid) == b.get(l2, l2 + mid))
low = mid;
else
high = mid;
}
return (low);
}
};
using RH = RollingHash<1000000007>;
#define issel(i) \
(b[(i + n) % n] - b[(i - 1 + n) % n] - b[(i + 1 + n) % n] >= a[(i + n) % n])
signed main(void) {
int n;
cin >> n;
vector<ll> a(n), b(n);
REP(i, n)
scanf("%lld", &a[i]);
REP(i, n)
scanf("%lld", &b[i]);
set<int> cand;
REP(i, n) {
if (issel(i))
cand.insert(i);
}
ll ans = 0;
while (cand.size()) {
int cur = *cand.begin();
cand.erase(cand.begin());
ll sum = b[(cur - 1 + n) % n] + b[(cur + 1 + n) % n];
ll t = (b[cur] - a[cur]) / sum;
ans += t;
b[cur] -= t * sum;
if (issel(cur - 1))
cand.insert((cur - 1 + n) % n);
if (issel(cur + 1))
cand.insert((cur + 1 + n) % n);
}
REP(i, n)
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
cout << ans << endl;
return 0;
}
| replace | 173 | 175 | 173 | 177 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
const ll MOD = 1e9 + 7;
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> pq;
rep(i, n) cin >> a[i];
rep(i, n) {
cin >> b[i];
if (a[i] != b[i])
pq.push({b[i], i});
}
ll ans = 0;
while (!pq.empty()) {
P d = pq.top();
pq.pop();
int del = b[d.se] - a[d.se];
if (del % (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]) == 0) {
ans += del / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
b[d.se] = a[d.se];
continue;
}
int cnt = b[d.se] / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
b[d.se] -= cnt * (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
if (a[d.se] < b[d.se])
pq.push({b[d.se], d.se});
if (a[d.se] > b[d.se]) {
cout << -1 << endl;
return 0;
}
ans += cnt;
}
cout << ans << endl;
}
| #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o set
const ll MOD = 1e9 + 7;
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> pq;
rep(i, n) cin >> a[i];
rep(i, n) {
cin >> b[i];
if (a[i] != b[i])
pq.push({b[i], i});
}
ll ans = 0;
while (!pq.empty()) {
P d = pq.top();
pq.pop();
int del = b[d.se] - a[d.se];
if (del % (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]) == 0) {
ans += del / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
b[d.se] = a[d.se];
continue;
}
if (b[d.se] < (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n])) {
cout << -1 << endl;
return 0;
}
int cnt = b[d.se] / (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
b[d.se] -= cnt * (b[(d.se + 1) % n] + b[(d.se - 1 + n) % n]);
if (a[d.se] < b[d.se])
pq.push({b[d.se], d.se});
if (a[d.se] > b[d.se]) {
cout << -1 << endl;
return 0;
}
ans += cnt;
}
cout << ans << endl;
}
| insert | 61 | 61 | 61 | 65 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "]";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "{" << p.first << ", " << p.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll ans = 0;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> que;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
cin >> b[i];
if (a[i] != b[i])
que.push(P(b[i], i));
}
while (!que.empty()) {
int v, m, l, r, s, k;
tie(v, m) = que.top();
que.pop();
l = (m - 1 + n) % n;
r = (m + 1) % n;
s = b[l] + b[r];
k = (b[m] - a[m]) / s;
if (b[m] <= s)
continue;
b[m] -= k * s;
if (b[m] != a[m] && a[m] < s)
que.push(P(b[m], m));
ans += k;
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "]";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "{" << p.first << ", " << p.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll ans = 0;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> que;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
cin >> b[i];
if (a[i] != b[i])
que.push(P(b[i], i));
}
while (!que.empty()) {
int v, m, l, r, s, k;
tie(v, m) = que.top();
que.pop();
l = (m - 1 + n) % n;
r = (m + 1) % n;
s = b[l] + b[r];
k = (b[m] - a[m]) / s;
if (b[m] <= s)
continue;
b[m] -= k * s;
if (b[m] > a[m] && k != 0)
que.push(P(b[m], m));
ans += k;
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
| replace | 61 | 62 | 61 | 62 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "]";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "{" << p.first << ", " << p.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll ans = 0;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> que;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
cin >> b[i];
if (a[i] != b[i])
que.push(P(b[i], i));
}
while (!que.empty()) {
int v, m, l, r, s, k;
tie(v, m) = que.top();
que.pop();
l = m == 0 ? n - 1 : m - 1;
r = m == n - 1 ? 0 : m + 1;
s = b[l] + b[r];
k = a[m] >= s ? (b[m] - a[m]) / s : b[m] / s;
if (b[m] == a[m])
continue;
if (b[m] <= s)
continue;
b[m] -= k * s;
if (b[m] != a[m])
que.push(P(b[m], m));
ans += k;
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const ll MOD = 1e9 + 7;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
o << "[";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "]";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "{" << p.first << ", " << p.second << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll ans = 0;
cin >> n;
vector<int> a(n), b(n);
priority_queue<P> que;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
cin >> b[i];
if (a[i] != b[i])
que.push(P(b[i], i));
}
while (!que.empty()) {
int v, m, l, r, s, k;
tie(v, m) = que.top();
que.pop();
l = m == 0 ? n - 1 : m - 1;
r = m == n - 1 ? 0 : m + 1;
s = b[l] + b[r];
k = a[m] >= s ? (b[m] - a[m]) / s : b[m] / s;
if (b[m] == a[m])
continue;
if (b[m] <= s)
continue;
b[m] -= k * s;
if (b[m] != a[m] && a[m] < s)
que.push(P(b[m], m));
ans += k;
}
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
| replace | 63 | 64 | 63 | 64 | TLE | |
p02941 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
int main() {
int N;
cin >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
cin >> B[i];
}
ll ans = 0;
queue<int> que;
for (int i = 0; i < N; i++) {
int v1 = B[(i + N - 1) % N];
int v2 = B[i];
int v3 = B[(i + 1) % N];
if (v1 < v2 and v2 > v3 and v2 > A[i])
que.emplace(i);
}
while (que.size()) {
int k = que.front();
que.pop();
ll ub, lb;
{
ll v1 = B[(k + N - 1) % N];
ll v3 = B[(k + 1) % N];
ub = 1LL * (B[k] + v1 + v3 - 1) / (v1 + v3) + 10;
lb = 0;
}
while (ub - lb > 1) {
ll mid = (ub + lb) / 2;
ll v1 = B[(k + N - 1) % N];
ll v2 = B[k];
ll v3 = B[(k + 1) % N];
v2 -= mid * (v1 + v3);
bool cond = (v2 >= A[k]);
if (cond)
lb = mid;
else
ub = mid;
}
ll cnt = lb;
{
int v1 = B[(k + N - 1) % N];
int v3 = B[(k + 1) % N];
ans += cnt;
B[k] -= cnt * (v1 + v3);
}
for (int d = -1; d <= 1; d++) {
int i = (k + d + N) % N;
int v1 = B[(i + N - 1) % N];
int v2 = B[i];
int v3 = B[(i + 1) % N];
// fprintf(stderr, "v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3);
if (v1 < v2 and v2 > v3 and v2 > A[i] and v1 + v3 < B[i]) {
que.emplace(i);
}
}
}
bool ok = true;
for (int i = 0; i < N; i++)
ok &= (A[i] == B[i]);
cout << (ok ? ans : -1) << endl;
return 0;
}
| // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
int main() {
int N;
cin >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
cin >> B[i];
}
ll ans = 0;
queue<int> que;
for (int i = 0; i < N; i++) {
int v1 = B[(i + N - 1) % N];
int v2 = B[i];
int v3 = B[(i + 1) % N];
if (v1 < v2 and v2 > v3 and v2 > A[i])
que.emplace(i);
}
while (que.size()) {
int k = que.front();
que.pop();
ll ub, lb;
{
ll v1 = B[(k + N - 1) % N];
ll v3 = B[(k + 1) % N];
ub = 1LL * (B[k] + v1 + v3 - 1) / (v1 + v3) + 10;
lb = 0;
}
while (ub - lb > 1) {
ll mid = (ub + lb) / 2;
ll v1 = B[(k + N - 1) % N];
ll v2 = B[k];
ll v3 = B[(k + 1) % N];
v2 -= mid * (v1 + v3);
bool cond = (v2 >= A[k]);
if (cond)
lb = mid;
else
ub = mid;
}
ll cnt = lb;
{
int v1 = B[(k + N - 1) % N];
int v3 = B[(k + 1) % N];
ans += cnt;
B[k] -= cnt * (v1 + v3);
}
for (int d = -1; d <= 1; d++) {
int i = (k + d + N) % N;
int v1 = B[(i + N - 1) % N];
int v2 = B[i];
int v3 = B[(i + 1) % N];
// fprintf(stderr, "v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3);
if (cnt > 0 and v1 < v2 and v2 > v3 and v2 > A[i] and v1 + v3 < B[i]) {
que.emplace(i);
}
}
}
bool ok = true;
for (int i = 0; i < N; i++)
ok &= (A[i] == B[i]);
cout << (ok ? ans : -1) << endl;
return 0;
}
| replace | 101 | 102 | 101 | 102 | TLE | |
p02941 | C++ | Time Limit Exceeded | /*
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 3e5;
const ll mod = 1e9 + 7;
const ld PI = acos((ld)-1);
#define int ll
#define pb push_back
#define endl '\n'
#define dokme(x) return (cout << x, 0);
#define migmig \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ms(x, y) memset(x, y, sizeof x);
#define file_init \
freopen("input.txt", "r+", stdin); \
freopen("output.txt", "w+", stdout);
ll pw(ll a, ll b, ll md = mod) {
ll res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
int n;
int a[maxn];
int b[maxn];
set<pii> st;
int32_t main() {
migmig cin >> n;
for (int i = 0; i < n; i++)
cin >> b[i];
for (int i = 0; i < n; i++)
cin >> a[i], st.insert({a[i], i});
int cnt = 0;
int ans = 0;
while (cnt != n) {
int mx = (*st.rbegin()).second;
st.erase((*st.rbegin()));
if (a[mx] == b[mx]) {
cnt++;
continue;
}
if (a[mx] < (a[(mx - 1 + n) % n] + a[(mx + 1) % n]))
dokme(-1);
if (b[mx] >= (a[(mx - 1 + n) % n] + a[(mx + 1) % n])) {
a[mx] -= b[mx];
ans += a[mx] / (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] %= (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] += b[mx];
} else {
ans += a[mx] / (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] %= (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
}
st.insert({a[mx], mx});
}
cout << ans;
return (0);
} | /*
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 3e5;
const ll mod = 1e9 + 7;
const ld PI = acos((ld)-1);
#define int ll
#define pb push_back
#define endl '\n'
#define dokme(x) return (cout << x, 0);
#define migmig \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ms(x, y) memset(x, y, sizeof x);
#define file_init \
freopen("input.txt", "r+", stdin); \
freopen("output.txt", "w+", stdout);
ll pw(ll a, ll b, ll md = mod) {
ll res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
int n;
int a[maxn];
int b[maxn];
set<pii> st;
int32_t main() {
migmig cin >> n;
for (int i = 0; i < n; i++)
cin >> b[i];
for (int i = 0; i < n; i++)
cin >> a[i], st.insert({a[i], i});
int cnt = 0;
int ans = 0;
while (cnt != n) {
int mx = (*st.rbegin()).second;
st.erase((*st.rbegin()));
if (a[mx] == b[mx]) {
cnt++;
continue;
}
if (a[mx] < (a[(mx - 1 + n) % n] + a[(mx + 1) % n]))
dokme(-1);
if (b[mx] >= (a[(mx - 1 + n) % n] + a[(mx + 1) % n])) {
a[mx] -= b[mx];
if (a[mx] < (a[(mx - 1 + n) % n] + a[(mx + 1) % n]))
dokme(-1);
ans += a[mx] / (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] %= (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] += b[mx];
} else {
ans += a[mx] / (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
a[mx] %= (a[(mx - 1 + n) % n] + a[(mx + 1) % n]);
}
st.insert({a[mx], mx});
}
cout << ans;
return (0);
} | insert | 68 | 68 | 68 | 70 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define P pair<int, int>
#define V vector<int>
#define M map<int, int>
#define itn int
#define tin int
#define tni int
bool dbg = false;
int N, A[200010], B[200010];
int ans;
bool search() {
bool flag = true;
FOR(i, 0, N) {
if (A[i] != B[i]) {
flag = false;
}
}
return flag;
}
void solve() {
priority_queue<P> que;
// set<P> S;
FOR(i, 0, N) {
if (A[i] != B[i]) {
// S.insert(make_pair(B[i], i));
que.push(make_pair(B[i], i));
}
}
// while (!S.empty()) {
while (!que.empty()) {
// auto it = S.end();
auto x = que.top();
que.pop();
// it--;
// S.erase(it);
// int t = (*it).second;
int t = x.second;
int &b = B[t];
int &a = B[(t + N - 1) % N];
int &c = B[(t + 1) % N];
if (b > a + c) {
int n = b - A[t];
n = n / (a + c);
ans += n;
b -= n * (a + c);
if (b != A[t]) {
// S.insert(make_pair(B[t], t));
que.push(make_pair(B[t], t));
}
} else {
cout << -1 << endl;
return;
}
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
FOR(i, 0, N)
cin >> A[i];
FOR(i, 0, N)
cin >> B[i];
solve();
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define P pair<int, int>
#define V vector<int>
#define M map<int, int>
#define itn int
#define tin int
#define tni int
bool dbg = false;
int N, A[200010], B[200010];
int ans;
bool search() {
bool flag = true;
FOR(i, 0, N) {
if (A[i] != B[i]) {
flag = false;
}
}
return flag;
}
void solve() {
priority_queue<P> que;
// set<P> S;
FOR(i, 0, N) {
if (A[i] != B[i]) {
// S.insert(make_pair(B[i], i));
que.push(make_pair(B[i], i));
}
}
// while (!S.empty()) {
while (!que.empty()) {
// auto it = S.end();
auto x = que.top();
que.pop();
// it--;
// S.erase(it);
// int t = (*it).second;
int t = x.second;
int &b = B[t];
int &a = B[(t + N - 1) % N];
int &c = B[(t + 1) % N];
if (b > a + c) {
int n = b - A[t];
n = n / (a + c);
if (n == 0) {
cout << -1 << endl;
return;
}
ans += n;
b -= n * (a + c);
if (b != A[t]) {
// S.insert(make_pair(B[t], t));
que.push(make_pair(B[t], t));
}
} else {
cout << -1 << endl;
return;
}
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
FOR(i, 0, N)
cin >> A[i];
FOR(i, 0, N)
cin >> B[i];
solve();
return 0;
}
| insert | 53 | 53 | 53 | 57 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n), b(n);
for
rep(i, n) cin >> b[i];
for
rep(i, n) cin >> a[i];
vector<bool> ju(n, true);
priority_queue<pair<long long, int>> da;
long long ans = 0;
for
rep(i, n) {
da.push(make_pair(a[i], i));
ju[i] = bool(a[i] == b[i]);
}
while (!da.empty()) {
int x = da.top().second;
da.pop();
if (!ju[x]) {
long long y = a[(x + 1) % n] + a[(x - 1 + n) % n];
if ((a[x] - b[x]) % y == 0) {
ans += (a[x] - b[x]) / y;
a[x] = b[x];
ju[x] = true;
} else {
ans += a[x] / y;
a[x] %= y;
if (a[x] < b[x]) {
ans = -1;
break;
}
da.push(make_pair(a[x], x));
}
}
}
cout << ans << endl;
} | #include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n), b(n);
for
rep(i, n) cin >> b[i];
for
rep(i, n) cin >> a[i];
vector<bool> ju(n, true);
priority_queue<pair<long long, int>> da;
long long ans = 0;
for
rep(i, n) {
da.push(make_pair(a[i], i));
ju[i] = bool(a[i] == b[i]);
}
while (!da.empty()) {
int x = da.top().second;
da.pop();
if (!ju[x]) {
long long y = a[(x + 1) % n] + a[(x - 1 + n) % n];
if (a[x] <= y) {
ans = -1;
break;
}
if ((a[x] - b[x]) % y == 0) {
ans += (a[x] - b[x]) / y;
a[x] = b[x];
ju[x] = true;
} else {
ans += a[x] / y;
a[x] %= y;
if (a[x] < b[x]) {
ans = -1;
break;
}
da.push(make_pair(a[x], x));
}
}
}
cout << ans << endl;
} | insert | 30 | 30 | 30 | 34 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long n;
cin >> n;
vector<long> a(n), b(n);
for (auto &x : a)
cin >> x;
for (auto &x : b)
cin >> x;
long c = 0, r = 0;
for (int i = 0; i < n; i++)
if (a[i] == b[i])
c++;
set<long> s;
for (int i = 0; i < n; i++)
if (a[i] != b[i] && b[i] > b[(i - 1 + n) % n] + b[(i + 1) % n])
s.insert(i);
while (!s.empty()) {
long i = *s.begin();
s.erase(i);
long ai = (i - 1 + n) % n, bi = i % n, ci = (i + 1) % n;
if (a[i] == b[i])
continue;
if (b[bi] <= b[ai] + b[ci])
continue;
r += (b[i] - a[i]) / (b[ai] + b[ci]);
b[i] = a[i] + ((b[i] - a[i]) % (b[ai] + b[ci]));
if (b[ai] > b[bi] + b[(ai - 1 + n) % n])
s.insert(ai);
if (b[ci] > b[bi] + b[(ci + 1 + n) % n])
s.insert(ci);
if (a[i] == b[i]) {
c++;
continue;
}
if (b[bi] > b[ai] + b[ci])
s.insert(bi);
}
cout << (c == n ? r : -1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(void) {
long n;
cin >> n;
vector<long> a(n), b(n);
for (auto &x : a)
cin >> x;
for (auto &x : b)
cin >> x;
long c = 0, r = 0;
for (int i = 0; i < n; i++)
if (a[i] == b[i])
c++;
set<long> s;
for (int i = 0; i < n; i++)
if (a[i] != b[i] && b[i] > b[(i - 1 + n) % n] + b[(i + 1) % n])
s.insert(i);
while (!s.empty()) {
long i = *s.begin();
s.erase(i);
long ai = (i - 1 + n) % n, bi = i % n, ci = (i + 1) % n;
if (a[i] == b[i])
continue;
if (b[bi] <= b[ai] + b[ci])
continue;
if ((b[i] - a[i]) / (b[ai] + b[ci]) == 0)
continue;
r += (b[i] - a[i]) / (b[ai] + b[ci]);
b[i] = a[i] + ((b[i] - a[i]) % (b[ai] + b[ci]));
if (b[ai] > b[bi] + b[(ai - 1 + n) % n])
s.insert(ai);
if (b[ci] > b[bi] + b[(ci + 1 + n) % n])
s.insert(ci);
if (a[i] == b[i]) {
c++;
continue;
}
if (b[bi] > b[ai] + b[ci])
s.insert(bi);
}
cout << (c == n ? r : -1) << endl;
}
| insert | 33 | 33 | 33 | 35 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define repn(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x) \
int x; \
cin >> (x)
#define ii2(x, y) \
int x, y; \
cin >> (x) >> (y)
#define ii3(x, y, z) \
int x, y, z; \
cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << endl
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)
inline void logger() { std::cout << " [LOGGER] " << endl; }
template <typename A, typename... B> void logger(const A &a, const B &...b) {
cout << a << " , ";
logger(b...);
}
typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 5000 * 5000 + 5;
/* ...o(^-^)o... */
int n;
int id(int i) {
if (i < 0)
return n + i;
if (i >= n)
return i - n;
return i;
}
int main() {
cin >> n;
vector<lint> a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
queue<int> que;
rep(i, n) {
if (b[i] > a[i] && b[i] > b[id(i - 1)] + b[id(i + 1)]) {
que.push(i);
}
}
lint res = 0;
while (!que.empty()) {
int i = que.front();
que.pop();
while (b[i] > a[i] && b[i] > b[id(i + 1)] + b[id(i - 1)]) {
res++;
b[i] -= b[id(i + 1)] + b[id(i - 1)];
}
if (b[id(i - 1)] > a[id(i - 1)] && b[id(i - 1)] > b[i] + b[id(i - 2)])
que.push(id(i - 1));
if (b[id(i + 1)] > a[id(i + 1)] && b[id(i + 1)] > b[i] + b[id(i + 2)])
que.push(id(i + 1));
}
bool isok = true;
rep(i, n) {
if (a[i] != b[i])
isok = false;
}
if (!isok) {
out(-1);
return 0;
}
out(res);
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for (int(i) = (n); (i) < (m); (i)++)
#define repn(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for (int(i) = (n - 1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x) \
int x; \
cin >> (x)
#define ii2(x, y) \
int x, y; \
cin >> (x) >> (y)
#define ii3(x, y, z) \
int x, y, z; \
cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << endl
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)
inline void logger() { std::cout << " [LOGGER] " << endl; }
template <typename A, typename... B> void logger(const A &a, const B &...b) {
cout << a << " , ";
logger(b...);
}
typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 5000 * 5000 + 5;
/* ...o(^-^)o... */
int n;
int id(int i) {
if (i < 0)
return n + i;
if (i >= n)
return i - n;
return i;
}
int main() {
cin >> n;
vector<lint> a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
queue<int> que;
rep(i, n) {
if (b[i] > a[i] && b[i] > b[id(i - 1)] + b[id(i + 1)]) {
que.push(i);
}
}
lint res = 0;
while (!que.empty()) {
int i = que.front();
que.pop();
lint sur = min(b[i] - a[i], b[i] - b[id(i + 1)] - b[id(i - 1)]) - 1;
lint c = sur / (b[id(i + 1)] + b[id(i - 1)]) + 1;
res += c;
b[i] -= c * (b[id(i + 1)] + b[id(i - 1)]);
if (b[id(i - 1)] > a[id(i - 1)] && b[id(i - 1)] > b[i] + b[id(i - 2)])
que.push(id(i - 1));
if (b[id(i + 1)] > a[id(i + 1)] && b[id(i + 1)] > b[i] + b[id(i + 2)])
que.push(id(i + 1));
}
bool isok = true;
rep(i, n) {
if (a[i] != b[i])
isok = false;
}
if (!isok) {
out(-1);
return 0;
}
out(res);
} | replace | 69 | 73 | 69 | 75 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a <= b) {
a = b;
return 1;
}
return 0;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first;
printf("\n");
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first;
printf(" ");
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first;
printf("\n");
putn(rest...);
}
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
REP(i, n) { cin >> a[i]; }
REP(i, n) { cin >> b[i]; }
ll res = 0;
priority_queue<int> q;
Rep(i, n) {
if (i == 0) {
if (b[i] > b[i + 1] + b[n - 1]) {
q.push(i);
}
} else if (i == n - 1) {
if (b[i] > b[0] + b[i - 1]) {
q.push(i);
}
} else {
if (b[i] > b[i + 1] + b[i - 1]) {
q.push(i);
}
}
}
while (!q.empty()) {
int i = q.top();
q.pop();
if (i == 0) {
if (b[i] > b[i + 1] + b[n - 1]) {
ll temp = b[i + 1] + b[n - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(n - 1);
q.push(i + 1);
}
}
} else if (i == n - 1) {
if (b[i] > b[0] + b[i - 1]) {
ll temp = b[0] + b[i - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(i - 1);
q.push(0);
}
}
} else {
if (b[i] > b[i + 1] + b[i - 1]) {
ll temp = b[i + 1] + b[i - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(i - 1);
q.push(i + 1);
}
}
}
}
bool flag = true;
REP(i, n) {
if (a[i] != b[i]) {
flag = false;
}
}
if (flag) {
put(res);
} else {
a.at(1000000000);
put(-1);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define RREP(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define FOR(i, n1, n2) for (ll i = (ll)(n1); i < (ll)(n2); i++)
#define RFOR(i, n1, n2) for (ll i = ((ll)(n1)-1); i >= (ll)(n2); i--)
#define all(a) (a).begin(), (a).end()
#define SORT(a) sort((a).begin(), (a).end())
#define oorret 0
#define oor(x) \
[&]() { \
try { \
x; \
} catch (const out_of_range &oor) { \
return oorret; \
} \
return x; \
}()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a <= b) {
a = b;
return 1;
}
return 0;
}
template <class Type> struct is_vector : std::false_type {};
template <class ValueType, class Alloc>
struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty())
return out;
constexpr bool is_vector_v = is_vector<T>::value;
if (is_vector_v)
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
else
for (auto itr = v.begin(); itr != v.end();)
out << (*itr), out << ((++itr != v.end()) ? " " : "");
return out;
}
inline void put() {}
template <class T> inline void put(const T &first) {
std::cout << first;
printf("\n");
}
template <class T, class... N>
inline void put(const T &first, const N &...rest) {
std::cout << first;
printf(" ");
put(rest...);
}
inline void putn() {}
template <class T, class... N>
inline void putn(const T &first, const N &...rest) {
std::cout << first;
printf("\n");
putn(rest...);
}
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
REP(i, n) { cin >> a[i]; }
REP(i, n) { cin >> b[i]; }
ll res = 0;
priority_queue<int> q;
Rep(i, n) {
if (i == 0) {
if (b[i] > b[i + 1] + b[n - 1]) {
q.push(i);
}
} else if (i == n - 1) {
if (b[i] > b[0] + b[i - 1]) {
q.push(i);
}
} else {
if (b[i] > b[i + 1] + b[i - 1]) {
q.push(i);
}
}
}
while (!q.empty()) {
int i = q.top();
q.pop();
if (i == 0) {
if (b[i] > b[i + 1] + b[n - 1]) {
ll temp = b[i + 1] + b[n - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(n - 1);
q.push(i + 1);
}
}
} else if (i == n - 1) {
if (b[i] > b[0] + b[i - 1]) {
ll temp = b[0] + b[i - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(i - 1);
q.push(0);
}
}
} else {
if (b[i] > b[i + 1] + b[i - 1]) {
ll temp = b[i + 1] + b[i - 1];
ll t = (b[i] - a[i]) / temp;
if (t > 0) {
res += t;
b[i] -= t * temp;
q.push(i - 1);
q.push(i + 1);
}
}
}
}
bool flag = true;
REP(i, n) {
if (a[i] != b[i]) {
flag = false;
}
}
if (flag) {
put(res);
} else {
// at(1000000000);
put(-1);
}
return 0;
} | replace | 142 | 143 | 142 | 143 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll a[200010], b[200010];
priority_queue<pair<ll, ll>> que;
int main() {
ll n, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
b[i] = x;
if (a[i] != b[i]) {
que.push(make_pair(x, i));
cnt++;
}
}
ll ans = 0;
while (cnt != 0) {
pair<ll, ll> kari = que.top();
que.pop();
ll nowval = kari.first;
ll nowiti = kari.second;
ll l = nowiti - 1, r = nowiti + 1;
if (l < 0) {
l = n - 1;
}
if (r == n) {
r = 0;
}
ll lval = b[l], rval = b[r];
ll herasu = (nowval - a[nowiti]) / (lval + rval);
ans += herasu;
nowval -= herasu * (lval + rval);
b[nowiti] = nowval;
if (nowval < a[nowiti]) {
cout << "-1";
return 0;
}
if (nowval == a[nowiti]) {
cnt--;
} else {
que.push(make_pair(nowval, nowiti));
}
}
cout << ans;
// your code goes here
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll a[200010], b[200010];
priority_queue<pair<ll, ll>> que;
int main() {
ll n, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
b[i] = x;
if (a[i] != b[i]) {
que.push(make_pair(x, i));
cnt++;
}
}
ll ans = 0;
while (cnt != 0) {
pair<ll, ll> kari = que.top();
que.pop();
ll nowval = kari.first;
ll nowiti = kari.second;
ll l = nowiti - 1, r = nowiti + 1;
if (l < 0) {
l = n - 1;
}
if (r == n) {
r = 0;
}
ll lval = b[l], rval = b[r];
ans++;
nowval -= (lval + rval);
ll herasu = (nowval - a[nowiti]) / (lval + rval);
ans += herasu;
nowval -= herasu * (lval + rval);
b[nowiti] = nowval;
if (nowval < a[nowiti]) {
cout << "-1";
return 0;
}
if (nowval == a[nowiti]) {
cnt--;
} else {
que.push(make_pair(nowval, nowiti));
}
}
cout << ans;
// your code goes here
return 0;
} | insert | 37 | 37 | 37 | 39 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll a[200010], b[200010];
int main() {
ll n;
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
priority_queue<pair<ll, ll>> ch;
for (ll i = 0; i < n; i++) {
cin >> b[i];
ch.push(make_pair(b[i], i));
}
ll ans = 0;
while (!ch.empty()) {
ll val = ch.top().first;
ll i = ch.top().second;
ch.pop();
ll l = i - 1;
if (l < 0) {
l = n - 1;
}
ll r = i + 1;
if (r == n) {
r = 0;
}
if (val < a[i]) {
cout << -1;
return 0;
}
if (val == a[i]) {
continue;
}
val = val - b[l] - b[r];
ans++;
ch.push(make_pair(val, i));
b[i] = val;
}
cout << ans;
// your code goes here
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
ll a[200010], b[200010];
int main() {
ll n;
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
priority_queue<pair<ll, ll>> ch;
for (ll i = 0; i < n; i++) {
cin >> b[i];
ch.push(make_pair(b[i], i));
}
ll ans = 0;
while (!ch.empty()) {
ll val = ch.top().first;
ll i = ch.top().second;
ch.pop();
ll l = i - 1;
if (l < 0) {
l = n - 1;
}
ll r = i + 1;
if (r == n) {
r = 0;
}
if (val < a[i]) {
cout << -1;
return 0;
}
if (val == a[i]) {
continue;
}
ll m = ((val)-a[i]) / (b[l] + b[r]);
if (m == 0) {
cout << -1;
return 0;
}
ans += m;
val = val - m * (b[l] + b[r]);
ch.push(make_pair(val, i));
b[i] = val;
}
cout << ans;
// your code goes here
return 0;
} | replace | 38 | 40 | 38 | 45 | TLE | |
p02941 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
bool operator<(const node s) const { return x > s.x; }
} b[100010];
int a[100010], c[100010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i].x;
c[i - 1] = b[i].x;
b[i].y = i - 1;
}
sort(b + 1, b + n + 1);
long long ans = 0;
bool fla = 1;
while (fla) {
fla = 0;
for (int i = 1; i <= n; i++) {
int pl = b[i].y;
int L = c[(pl + 1) % n], R = c[(pl + n - 1) % n];
if (c[pl] - a[pl] >= L + R)
fla = 1;
ans += (c[pl] - a[pl]) / (L + R);
c[pl] -= (c[pl] - a[pl]) / (L + R) * (L + R);
}
}
for (int i = 0; i < n; i++)
if (a[i] != c[i])
return puts("-1"), 0;
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
bool operator<(const node s) const { return x > s.x; }
} b[1000010];
int a[1000010], c[1000010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i].x;
c[i - 1] = b[i].x;
b[i].y = i - 1;
}
sort(b + 1, b + n + 1);
long long ans = 0;
bool fla = 1;
while (fla) {
fla = 0;
for (int i = 1; i <= n; i++) {
int pl = b[i].y;
int L = c[(pl + 1) % n], R = c[(pl + n - 1) % n];
if (c[pl] - a[pl] >= L + R)
fla = 1;
ans += (c[pl] - a[pl]) / (L + R);
c[pl] -= (c[pl] - a[pl]) / (L + R) * (L + R);
}
}
for (int i = 0; i < n; i++)
if (a[i] != c[i])
return puts("-1"), 0;
cout << ans;
return 0;
}
| replace | 5 | 7 | 5 | 7 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n, raod, a[300005], b[300005];
ll kk(ll x) {
if (x == 1)
return b[n] + b[2];
if (x == n)
return b[1] + b[n - 1];
return b[x - 1] + b[x + 1];
}
set<pair<ll, ll>> st;
set<pair<ll, ll>>::iterator it;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
st.insert(make_pair(b[i], (ll)i));
}
while (st.size()) {
it = st.end();
it--;
pair<ll, ll> p = (*it);
if (p.f == a[p.s]) {
st.erase(it);
continue;
}
if (p.f < a[p.s]) {
cout << "-1";
return 0;
}
st.erase(it);
b[p.s] -= kk(p.s);
raod++;
if (b[p.s] < a[p.s]) {
cout << "-1";
return 0;
}
st.insert(make_pair(b[p.s], p.s));
}
cout << raod << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n, raod, a[300005], b[300005];
ll kk(ll x) {
if (x == 1)
return b[n] + b[2];
if (x == n)
return b[1] + b[n - 1];
return b[x - 1] + b[x + 1];
}
set<pair<ll, ll>> st;
set<pair<ll, ll>>::iterator it;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
st.insert(make_pair(b[i], (ll)i));
}
while (st.size()) {
it = st.end();
it--;
pair<ll, ll> p = (*it);
if (p.f == a[p.s]) {
st.erase(it);
continue;
}
if (p.f < a[p.s]) {
cout << "-1";
return 0;
}
st.erase(it);
ll ff = 0;
if (p.s == 1)
ff = max(b[n], b[2]);
if (p.s == n)
ff = max(b[n - 1], b[1]);
ff = max(b[p.s - 1], b[p.s + 1]);
ff = max(ff, a[p.s]);
ll g = (b[p.s] - ff) / kk(p.s);
g = max(g, 1LL);
b[p.s] -= g * kk(p.s);
raod += g;
if (b[p.s] < a[p.s]) {
cout << "-1";
return 0;
}
st.insert(make_pair(b[p.s], p.s));
}
cout << raod << endl;
return 0;
}
| replace | 41 | 43 | 41 | 52 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 5;
int const MAX = 105;
int n, a[N], d[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
priority_queue<pair<int, int>> que;
for (int i = 1; i <= n; i++) {
que.push(make_pair(a[i], i));
}
a[0] = a[n];
a[n + 1] = a[1];
long long ans = 0;
while (!que.empty()) {
pair<int, int> p = que.top();
que.pop();
int val = p.first;
int id = p.second;
int pre = a[id - 1];
int nxt = a[id + 1];
if (a[id] == d[id]) {
continue;
}
if (a[id] < pre + nxt) {
continue;
}
ans += (a[id] - d[id]) / (pre + nxt);
a[id] = (a[id] - d[id]) % (pre + nxt) + d[id];
a[0] = a[n];
a[n + 1] = a[1];
que.push(make_pair(a[id], id));
}
for (int i = 1; i <= n; i++) {
if (a[i] != d[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 5;
int const MAX = 105;
int n, a[N], d[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
priority_queue<pair<int, int>> que;
for (int i = 1; i <= n; i++) {
que.push(make_pair(a[i], i));
}
a[0] = a[n];
a[n + 1] = a[1];
long long ans = 0;
while (!que.empty()) {
pair<int, int> p = que.top();
que.pop();
int val = p.first;
int id = p.second;
int pre = a[id - 1];
int nxt = a[id + 1];
if (a[id] == d[id]) {
continue;
}
if (a[id] - d[id] < pre + nxt) {
continue;
}
ans += (a[id] - d[id]) / (pre + nxt);
a[id] = (a[id] - d[id]) % (pre + nxt) + d[id];
a[0] = a[n];
a[n + 1] = a[1];
que.push(make_pair(a[id], id));
}
for (int i = 1; i <= n; i++) {
if (a[i] != d[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
LL first, second, third;
};
struct Fll {
LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
struct Edge {
LL to, cost;
};
struct edge {
LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> edges;
vector<Pll> v;
map<Pll, Pll> ma;
set<LL> st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, a[210000], b[210000];
string str, ss;
bool f;
char c;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int main() {
cin >> n;
rep(i, n) { cin >> a[i]; }
rep(i, n) { cin >> b[i]; }
for (int j = 0; j < 30; j++)
rep(i, n) {
while (a[i] + b[(i - 1 + n) % n] + b[(i + 1) % n] <= b[i]) {
b[i] -= b[(i - 1 + n) % n] + b[(i + 1) % n];
ans++;
}
}
rep(i, n) {
if (a[i] != b[i])
f = 1;
}
if (f)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
LL first, second, third;
};
struct Fll {
LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
struct Edge {
LL to, cost;
};
struct edge {
LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> edges;
vector<Pll> v;
map<Pll, Pll> ma;
set<LL> st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, a[210000], b[210000];
string str, ss;
bool f;
char c;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int main() {
cin >> n;
rep(i, n) { cin >> a[i]; }
rep(i, n) { cin >> b[i]; }
for (int j = 0; j < 30; j++)
rep(i, n) {
if (b[i] - a[i] > 0) {
cnt = (b[i] - a[i]) / (b[(i - 1 + n) % n] + b[(i + 1) % n]);
} else
cnt = 0;
b[i] -= (b[(i - 1 + n) % n] + b[(i + 1) % n]) * cnt;
ans += cnt;
}
rep(i, n) {
if (a[i] != b[i])
f = 1;
}
if (f)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| replace | 94 | 98 | 94 | 100 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall \
ios::sync_with_stdio(false); \
cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define inl(...) \
long long __VA_ARGS__; \
in(__VA_ARGS__)
#define ins(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define int long long
const long long inf = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
cin >> t;
in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
cout << t;
if (sizeof...(u))
cout << " ";
out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
#define FORR(x, arr) for (auto &x : arr)
#define ZERO(a) memset(a, 0, sizeof(a))
// 3111111111111111111111111111111
void solve() {
ini(n);
int morty[n];
int morty1[n];
int rick = 0;
FOR(i, 0, n) in(morty[i]);
priority_queue<pair<int, int>> tmp;
FOR(i, 0, n) {
in(morty1[i]);
if (morty1[i] > morty[i]) {
tmp.push(make_pair(morty1[i], i));
}
}
while (tmp.size()) {
pair<int, int> tmp1;
tmp1 = tmp.top();
tmp.pop();
if (tmp1.first > morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]) {
rick += 1;
tmp1.first -= (morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]);
morty1[tmp1.second] = tmp1.first;
if (morty1[tmp1.second] < morty[tmp1.second])
return out(-1);
if (morty1[tmp1.second] > morty[tmp1.second])
tmp.push(tmp1);
} else {
return out(-1);
}
}
out(rick);
}
int32_t main() {
oneforall oneforall oneforall oneforall oneforall oneforall oneforall
oneforall oneforall oneforall oneforall oneforall oneforall oneforall
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall \
ios::sync_with_stdio(false); \
cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define inl(...) \
long long __VA_ARGS__; \
in(__VA_ARGS__)
#define ins(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define int long long
const long long inf = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
cin >> t;
in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
cout << t;
if (sizeof...(u))
cout << " ";
out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
#define FORR(x, arr) for (auto &x : arr)
#define ZERO(a) memset(a, 0, sizeof(a))
// 3111111111111111111111111111111
void solve() {
ini(n);
int morty[n];
int morty1[n];
int rick = 0;
FOR(i, 0, n) in(morty[i]);
priority_queue<pair<int, int>> tmp;
FOR(i, 0, n) {
in(morty1[i]);
if (morty1[i] > morty[i]) {
tmp.push(make_pair(morty1[i], i));
}
}
while (tmp.size()) {
pair<int, int> tmp1;
tmp1 = tmp.top();
tmp.pop();
if (tmp1.first > morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]) {
if ((morty1[tmp1.second] - morty[tmp1.second]) %
(morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]) ==
0) {
rick += (morty1[tmp1.second] - morty[tmp1.second]) /
(morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]);
morty1[tmp1.second] = morty[tmp1.second];
} else {
morty1[tmp1.second] =
tmp1.first %
(morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]);
rick += tmp1.first /
(morty1[tmp1.second - 1 == -1 ? (n - 1) : tmp1.second - 1] +
morty1[tmp1.second + 1 == n ? 0 : tmp1.second + 1]);
if (morty1[tmp1.second] < morty[tmp1.second])
return out(-1);
tmp.push(make_pair(morty1[tmp1.second], tmp1.second));
}
} else {
return out(-1);
}
}
out(rick);
}
int32_t main() {
oneforall oneforall oneforall oneforall oneforall oneforall oneforall
oneforall oneforall oneforall oneforall oneforall oneforall oneforall
solve();
return 0;
}
| replace | 60 | 68 | 60 | 81 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.precision(12);
cout << fixed;
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
set<pair<int, int>> s;
for (int i = 0; i < n; i++) {
s.insert({b[i], i});
}
long long ans = 0;
while (!s.empty()) {
auto p = *s.rbegin();
s.erase(p);
int x = p.first;
int i = p.second;
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
if (a[i] == b[i]) {
continue;
}
int i0 = (i + n - 1) % n;
int i1 = (i + 1) % n;
int once = (b[i0] + b[i1]);
int mx = max(b[i0], b[i1]);
mx = max(mx, a[i]);
int need = b[i] - mx;
if (need < 0) {
cout << -1 << endl;
return 0;
}
int times = (need + once - 1) / once;
ans += times;
b[i] -= once * times;
s.insert({b[i], i});
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.precision(12);
cout << fixed;
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
set<pair<int, int>> s;
for (int i = 0; i < n; i++) {
s.insert({b[i], i});
}
long long ans = 0;
while (!s.empty()) {
auto p = *s.rbegin();
s.erase(p);
int x = p.first;
int i = p.second;
if (a[i] > b[i]) {
cout << -1 << endl;
return 0;
}
if (a[i] == b[i]) {
continue;
}
int i0 = (i + n - 1) % n;
int i1 = (i + 1) % n;
int once = (b[i0] + b[i1]);
int mx = max(b[i0], b[i1]);
mx = max(mx, a[i]);
int need = b[i] - mx;
if (need < 0) {
cout << -1 << endl;
return 0;
}
int times = (need + once - 1) / once;
ans += times;
b[i] -= once * times;
if (b[i] < a[i]) {
cout << -1 << endl;
return 0;
}
if (b[i] > a[i]) {
s.insert({b[i], i});
}
}
cout << ans << endl;
return 0;
}
| replace | 48 | 49 | 48 | 55 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
#define i_7 (long long)(1e9 + 7)
// #define i_7 998'244'353
long mod(long a) {
long long c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
long long po(long a, long b) {
if (b == 0) {
return 1;
}
long long z = po(a, b / 2);
z = mod(z * z);
if (b % 2 != 0) {
z = mod(a * z);
}
return z;
}
using namespace std;
// using namespace boost::multiprecision;
bool prime_(int n) {
if (n == 1) {
return false;
} else if (n == 2) {
return true;
} else {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
long long gcd_(long long a, long long b) {
if (a < b) {
swap(a, b);
}
if (a % b == 0) {
return b;
} else {
return gcd_(b, a % b);
}
}
long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }
int main() {
int n;
cin >> n;
int a[n], b[n];
REP(i, n) { cin >> a[i]; }
REP(i, n) { cin >> b[i]; }
priority_queue<pair<int, int>> q;
REP(i, n) { q.push(make_pair(b[i], i)); }
long long ans = 0;
while (!q.empty()) {
int val = q.top().first;
int i = q.top().second;
q.pop();
if (val == a[i]) {
continue;
}
if (val < a[i]) {
cout << -1 << endl;
return 0;
}
int l = i - 1, r = i + 1;
if (l < 0) {
l = n - 1;
}
if (r >= n) {
r = 0;
}
long long cnt;
cnt = (b[i] - a[i]) / (b[l] + b[r]);
ans += cnt;
b[i] -= cnt * (b[l] + b[r]);
q.push(make_pair(b[i], i));
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPP(i, n) for (int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
// const double PI = acos(-1);
// const double EPS = 1e-15;
// long long INF=(long long)1E17;
#define i_7 (long long)(1e9 + 7)
// #define i_7 998'244'353
long mod(long a) {
long long c = a % i_7;
if (c >= 0)
return c;
return c + i_7;
}
long long po(long a, long b) {
if (b == 0) {
return 1;
}
long long z = po(a, b / 2);
z = mod(z * z);
if (b % 2 != 0) {
z = mod(a * z);
}
return z;
}
using namespace std;
// using namespace boost::multiprecision;
bool prime_(int n) {
if (n == 1) {
return false;
} else if (n == 2) {
return true;
} else {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
long long gcd_(long long a, long long b) {
if (a < b) {
swap(a, b);
}
if (a % b == 0) {
return b;
} else {
return gcd_(b, a % b);
}
}
long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; }
int main() {
int n;
cin >> n;
int a[n], b[n];
REP(i, n) { cin >> a[i]; }
REP(i, n) { cin >> b[i]; }
priority_queue<pair<int, int>> q;
REP(i, n) { q.push(make_pair(b[i], i)); }
long long ans = 0;
while (!q.empty()) {
int val = q.top().first;
int i = q.top().second;
q.pop();
if (val == a[i]) {
continue;
}
if (val < a[i]) {
cout << -1 << endl;
return 0;
}
int l = i - 1, r = i + 1;
if (l < 0) {
l = n - 1;
}
if (r >= n) {
r = 0;
}
long long cnt;
cnt = (b[i] - a[i]) / (b[l] + b[r]);
if (cnt == 0) {
cout << -1 << endl;
return 0;
}
ans += cnt;
b[i] -= cnt * (b[l] + b[r]);
q.push(make_pair(b[i], i));
}
cout << ans << endl;
return 0;
}
| insert | 93 | 93 | 93 | 97 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
vector<ll> A(N), B(N);
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
priority_queue<pair<ll, ll>> task; // <B[i], i>;
rep(i, N) {
if (A[i] < B[i]) {
task.push(make_pair(B[i], i));
}
}
ll ans = 0;
while (task.size() > 0) {
ll id = task.top().second;
task.pop();
ll s = B[(id + N - 1) % N] + B[(id + 1) % N];
if (B[id] > s) {
ll op = (B[id] - A[id]) / s;
ans += op;
B[id] -= op * s;
if (A[id] < B[id]) {
task.push(make_pair(B[id], id));
}
} else {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
vector<ll> A(N), B(N);
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
priority_queue<pair<ll, ll>> task; // <B[i], i>;
rep(i, N) {
if (A[i] < B[i]) {
task.push(make_pair(B[i], i));
}
}
ll ans = 0;
while (task.size() > 0) {
ll id = task.top().second;
task.pop();
ll s = B[(id + N - 1) % N] + B[(id + 1) % N];
if (B[id] - A[id] >= s) {
ll op = (B[id] - A[id]) / s;
ans += op;
B[id] -= op * s;
if (A[id] < B[id]) {
task.push(make_pair(B[id], id));
}
} else {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | replace | 34 | 35 | 34 | 35 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <queue>
#include <stdio.h>
using namespace std;
long long n;
long long a[200000];
long long b[200000];
long long s, t, x, y, z;
long long ans;
priority_queue<long long> q;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", a + i);
s += a[i];
}
for (int i = 0; i < n; i++) {
scanf("%lld", b + i);
t += b[i];
q.push(b[i] * n + i);
}
while (t > s) {
if (q.empty())
break;
x = q.top();
x %= n;
if (b[x] == a[x]) {
q.pop();
continue;
}
if (b[x] - b[(x + n - 1) % n] - b[(x + 1) % n] <= 0)
break;
q.pop();
y = b[(x + n - 1) % n] + b[(x + 1) % n];
z = (b[x] - a[x]) / y;
b[x] -= z * y;
t -= z * y;
ans += z;
q.push(b[x] * n + x);
}
y = 1;
for (int i = 0; i < n; i++) {
if (a[i] != b[i])
y = 0;
}
if (y)
printf("%lld\n", ans);
else
printf("-1\n");
} | #include <queue>
#include <stdio.h>
using namespace std;
long long n;
long long a[200000];
long long b[200000];
long long s, t, x, y, z;
long long ans;
priority_queue<long long> q;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", a + i);
s += a[i];
}
for (int i = 0; i < n; i++) {
scanf("%lld", b + i);
t += b[i];
q.push(b[i] * n + i);
}
while (t > s) {
if (q.empty())
break;
x = q.top();
x %= n;
if (b[x] == a[x]) {
q.pop();
continue;
}
if (b[x] - b[(x + n - 1) % n] - b[(x + 1) % n] < a[x])
break;
q.pop();
y = b[(x + n - 1) % n] + b[(x + 1) % n];
z = (b[x] - a[x]) / y;
b[x] -= z * y;
t -= z * y;
ans += z;
q.push(b[x] * n + x);
}
y = 1;
for (int i = 0; i < n; i++) {
if (a[i] != b[i])
y = 0;
}
if (y)
printf("%lld\n", ans);
else
printf("-1\n");
} | replace | 29 | 30 | 29 | 30 | TLE | |
p02941 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
set<pair<int, int>> s;
for (int i = 0; i < n; ++i) {
cin >> b[i];
s.insert({b[i], i});
}
long long result = 0;
for (;;) {
if (s.empty())
break;
int i = prev(s.end())->second;
s.erase(prev(s.end()));
int i1 = (i + 1) % n;
int i2 = (i + n - 1) % n;
int sum = b[i1] + b[i2];
if (b[i] < a[i]) {
cout << -1 << endl;
return 0;
}
if (b[i] == a[i]) {
continue;
}
if (b[i] <= sum) {
cout << -1 << endl;
return 0;
}
int t = (b[i] - a[i]) / sum;
assert(t > 0);
result += t;
b[i] -= t * sum;
s.insert({b[i], i});
}
cout << result << endl;
return 0;
}
| #include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
set<pair<int, int>> s;
for (int i = 0; i < n; ++i) {
cin >> b[i];
s.insert({b[i], i});
}
long long result = 0;
for (;;) {
if (s.empty())
break;
int i = prev(s.end())->second;
s.erase(prev(s.end()));
int i1 = (i + 1) % n;
int i2 = (i + n - 1) % n;
int sum = b[i1] + b[i2];
if (b[i] < a[i]) {
cout << -1 << endl;
return 0;
}
if (b[i] == a[i]) {
continue;
}
if (b[i] <= sum) {
cout << -1 << endl;
return 0;
}
int t = (b[i] - a[i]) / sum;
if (t == 0) {
cout << -1 << endl;
return 0;
}
result += t;
b[i] -= t * sum;
s.insert({b[i], i});
}
cout << result << endl;
return 0;
}
| replace | 51 | 52 | 51 | 55 | 0 | |
p02941 | 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;
int main() {
int N;
ll ans = 0;
cin >> N;
vector<int> A(N), B(N);
rep(i, N) cin >> A[i];
priority_queue<vector<int>> q;
rep(i, N) {
cin >> B[i];
q.push({B[i], i});
}
while (!q.empty()) {
auto t = q.top();
q.pop();
if (t[0] <= A[t[1]])
continue;
int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
int r = (t[0] - A[t[1]]) / d;
t[0] -= d * r;
ans += r;
if (t[0] < A[t[1]]) {
cout << -1 << endl;
return 0;
}
if (t[0] > A[t[1]]) {
q.push({t[0], t[1]});
}
B[t[1]] = t[0];
}
rep(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
/*
9817 1108 6890 4343 8704
0005
4356
1439
0008
2896
1449
2
329
774
440
106
221
113
5
96
86
76
66
56
46
36
26
16
6
1
*/
} | #include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main() {
int N;
ll ans = 0;
cin >> N;
vector<int> A(N), B(N);
rep(i, N) cin >> A[i];
priority_queue<vector<int>> q;
rep(i, N) {
cin >> B[i];
q.push({B[i], i});
}
while (!q.empty()) {
auto t = q.top();
q.pop();
if (t[0] <= A[t[1]])
continue;
int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
int r = (t[0] - A[t[1]]) / d;
t[0] -= d * r;
ans += r;
if (t[0] < A[t[1]] || r == 0) {
cout << -1 << endl;
return 0;
}
if (t[0] > A[t[1]]) {
q.push({t[0], t[1]});
}
B[t[1]] = t[0];
}
rep(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
/*
9817 1108 6890 4343 8704
0005
4356
1439
0008
2896
1449
2
329
774
440
106
221
113
5
96
86
76
66
56
46
36
26
16
6
1
*/
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02941 | 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;
int main() {
int N;
ll ans = 0;
cin >> N;
vector<int> A(N), B(N);
rep(i, N) cin >> A[i];
priority_queue<vector<int>> q;
rep(i, N) {
cin >> B[i];
q.push({B[i], i});
}
while (!q.empty()) {
auto t = q.top();
q.pop();
if (t[0] <= A[t[1]])
continue;
int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
int r = (t[0] - max(A[t[1]], d) + d - 1) / d;
t[0] -= d * r;
ans += r;
if (t[0] < A[t[1]]) {
cout << -1 << endl;
return 0;
}
if (t[0] > A[t[1]]) {
q.push({t[0], t[1]});
}
B[t[1]] = t[0];
}
rep(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
/*
9817 1108 6890 4343 8704
0005
4356
1439
0008
2896
1449
2
329
774
440
106
221
113
5
96
86
76
66
56
46
36
26
16
6
1
*/
} | #include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
int main() {
int N;
ll ans = 0;
cin >> N;
vector<int> A(N), B(N);
rep(i, N) cin >> A[i];
priority_queue<vector<int>> q;
rep(i, N) {
cin >> B[i];
q.push({B[i], i});
}
while (!q.empty()) {
auto t = q.top();
q.pop();
if (t[0] <= A[t[1]])
continue;
int d = B[(t[1] - 1 + N) % N] + B[(t[1] + 1) % N];
int r = (t[0] - max(A[t[1]], d) + d - 1) / d;
t[0] -= d * r;
ans += r;
if (t[0] < A[t[1]] || r == 0) {
cout << -1 << endl;
return 0;
}
if (t[0] > A[t[1]]) {
q.push({t[0], t[1]});
}
B[t[1]] = t[0];
}
rep(i, N) {
if (A[i] != B[i]) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
/*
9817 1108 6890 4343 8704
0005
4356
1439
0008
2896
1449
2
329
774
440
106
221
113
5
96
86
76
66
56
46
36
26
16
6
1
*/
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02941 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <typename T> void uin(T &a, T b) {
if (b < a) {
a = b;
}
}
template <typename T> void uax(T &a, T b) {
if (b > a) {
a = b;
}
}
#define int long long
#define ghost signed
#define left left228
#define right right228
#define complex complex228
#define count count228
#define sin sin228
#define list list228
const int N = 200 * 1000 + 228;
int a[N], b[N];
ghost main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> b[i];
set<pair<int, int>> s;
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
s.insert({b[i], i});
}
}
int cnt = 0;
while (!s.empty()) {
pair<int, int> p = *s.rbegin();
s.erase(p);
int i = p.second;
b[i] -= (b[(i + n - 1) % n] + b[(i + 1) % n]);
++cnt;
if (b[i] <= 0) {
cout << -1 << '\n';
return 0;
}
if (a[i] != b[i])
s.insert({b[i], i});
}
cout << cnt << '\n';
return 0;
} // kek ;
// Ого! Кажетсья это $#@!
| #pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <typename T> void uin(T &a, T b) {
if (b < a) {
a = b;
}
}
template <typename T> void uax(T &a, T b) {
if (b > a) {
a = b;
}
}
#define int long long
#define ghost signed
#define left left228
#define right right228
#define complex complex228
#define count count228
#define sin sin228
#define list list228
const int N = 200 * 1000 + 228;
int a[N], b[N];
ghost main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i)
cin >> b[i];
set<pair<int, int>> s;
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) {
s.insert({b[i], i});
}
}
int cnt = 0;
while (!s.empty()) {
pair<int, int> p = *s.rbegin();
s.erase(p);
int i = p.second;
int bn = b[(i + 1) % n];
int bp = b[(i - 1 + n) % n];
int kek =
min((b[i] - a[i]) / (bn + bp), (b[i] - max(bn, bp)) / (bn + bp) + 1);
b[i] -= (bn + bp) * kek;
if (kek == 0) {
cout << -1 << '\n';
return 0;
}
cnt += kek;
if (b[i] <= 0) {
cout << -1 << '\n';
return 0;
}
if (a[i] != b[i])
s.insert({b[i], i});
}
cout << cnt << '\n';
return 0;
} // kek ;
// Ого! Кажетсья это $#@!
| replace | 71 | 73 | 71 | 81 | TLE | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
#define maxn 200005
using namespace std;
typedef long long ll;
typedef struct node {
int id, val;
node(int a1, int a2) {
id = a1;
val = a2;
}
};
bool operator<(const node &a1, const node &a2) {
if (a1.val <= a2.val)
return true;
return false;
}
int a[maxn], b[maxn], n;
priority_queue<node> p;
int main() {
scanf("%d", &n);
ff(i, 1, n) scanf("%d", &a[i - 1]);
ff(i, 1, n) scanf("%d", &b[i - 1]);
ff(i, 1, n) {
if (a[i - 1] < b[i - 1])
p.push(node(i - 1, b[i - 1]));
}
int flag = 1;
ll ans = 0;
while (!p.empty()) {
node temp = p.top();
p.pop();
int pos = temp.id, v = temp.val;
// printf("%d\n", v);
int mod;
if (pos == 0)
mod = b[n - 1] + b[1];
else if (pos == n - 1)
mod = b[0] + b[n - 2];
else
mod = b[pos - 1] + b[pos + 1];
if (v <= mod) {
flag = 0;
break;
}
v = (b[pos] - a[pos]) % mod;
ans += (b[pos] - a[pos]) / mod;
b[pos] = a[pos] + v;
if (v == 0)
;
else
p.push(node(pos, b[pos]));
}
while (!p.empty()) {
p.pop();
}
if (flag)
printf("%lld", ans);
else
printf("-1");
return 0;
}
| #include <bits/stdc++.h>
#define ff(i, x, y) for (int i = x; i <= y; i++)
#define fs(i, x, y) for (int i = x; i >= y; i--)
#define maxn 200005
using namespace std;
typedef long long ll;
typedef struct node {
int id, val;
node(int a1, int a2) {
id = a1;
val = a2;
}
};
bool operator<(const node &a1, const node &a2) {
if (a1.val <= a2.val)
return true;
return false;
}
int a[maxn], b[maxn], n;
priority_queue<node> p;
int main() {
scanf("%d", &n);
ff(i, 1, n) scanf("%d", &a[i - 1]);
ff(i, 1, n) scanf("%d", &b[i - 1]);
ff(i, 1, n) {
if (a[i - 1] < b[i - 1])
p.push(node(i - 1, b[i - 1]));
else if (a[i - 1] > b[i - 1]) {
printf("-1");
return 0;
}
}
int flag = 1;
ll ans = 0;
while (!p.empty()) {
node temp = p.top();
p.pop();
int pos = temp.id, v = temp.val;
// printf("%d\n", v);
int mod;
if (pos == 0)
mod = b[n - 1] + b[1];
else if (pos == n - 1)
mod = b[0] + b[n - 2];
else
mod = b[pos - 1] + b[pos + 1];
if (v <= mod) {
flag = 0;
break;
}
v = (b[pos] - a[pos]) % mod;
ans += (b[pos] - a[pos]) / mod;
b[pos] = a[pos] + v;
if (v == 0)
;
else
p.push(node(pos, b[pos]));
}
while (!p.empty()) {
p.pop();
}
if (flag)
printf("%lld", ans);
else
printf("-1");
return 0;
}
| insert | 27 | 27 | 27 | 31 | TLE | |
p02941 | C++ | Runtime Error | // minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 5e5 + 5, M = 5e7 + 5;
int a[N], b[N], las[N], nxt[N], n;
ll res;
inline bool ck(R int x) { return b[x] > a[x] && b[x] > b[las[x]] + b[nxt[x]]; }
int q[M];
int main() {
scanf("%d", &n);
fp(i, 1, n) las[i] = i - 1, nxt[i] = i + 1;
las[1] = n, nxt[n] = 1;
fp(i, 1, n) scanf("%d", &a[i]);
fp(i, 1, n) scanf("%d", &b[i]);
int h = 1, t = 0, u, k;
fp(i, 1, n) if (ck(i)) q[++t] = i;
while (h <= t) {
u = q[h++];
k = (b[u] - a[u]) / (b[las[u]] + b[nxt[u]]);
res += k, b[u] -= k * (b[las[u]] + b[nxt[u]]);
// printf("%d %d\n",u,b[u]);
if (ck(las[u]))
q[++t] = las[u];
if (ck(nxt[u]))
q[++t] = nxt[u];
if (ck(u))
q[++t] = u;
}
fp(i, 1, n) if (a[i] != b[i]) return puts("-1"), 0;
return printf("%lld\n", res), 0;
} | // minamoto
#include <bits/stdc++.h>
#define R register
#define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i)
#define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i)
#define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v)
template <class T> inline bool cmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <class T> inline bool cmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
using namespace std;
typedef long long ll;
const int N = 5e5 + 5, M = 5e7 + 5;
int a[N], b[N], las[N], nxt[N], n;
ll res;
inline bool ck(R int x) { return (b[x] - a[x]) >= b[las[x]] + b[nxt[x]]; }
int q[M];
int main() {
scanf("%d", &n);
fp(i, 1, n) las[i] = i - 1, nxt[i] = i + 1;
las[1] = n, nxt[n] = 1;
fp(i, 1, n) scanf("%d", &a[i]);
fp(i, 1, n) scanf("%d", &b[i]);
int h = 1, t = 0, u, k;
fp(i, 1, n) if (ck(i)) q[++t] = i;
while (h <= t) {
u = q[h++];
k = (b[u] - a[u]) / (b[las[u]] + b[nxt[u]]);
res += k, b[u] -= k * (b[las[u]] + b[nxt[u]]);
// printf("%d %d\n",u,b[u]);
if (ck(las[u]))
q[++t] = las[u];
if (ck(nxt[u]))
q[++t] = nxt[u];
if (ck(u))
q[++t] = u;
}
fp(i, 1, n) if (a[i] != b[i]) return puts("-1"), 0;
return printf("%lld\n", res), 0;
} | replace | 17 | 18 | 17 | 18 | -11 | |
p02941 | C++ | Runtime Error | #include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int NMAX = 1e5 + 5;
int n, a[NMAX], b[NMAX];
ll ans = 0;
pair<int, int> p;
bool possible = true;
priority_queue<pair<int, int>> valeurs;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
a[0] = a[n];
a[n + 1] = a[1];
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i] != a[i])
valeurs.push(make_pair(b[i], i));
}
b[0] = b[n];
b[n + 1] = b[1];
while (!valeurs.empty() && possible) {
p = valeurs.top();
valeurs.pop();
int id = p.second;
// cout << (int)valeurs.size() << '\n';
// cout << id << ' ' << b[id] << ' ';
if (b[id] - (b[id - 1] + b[id + 1]) < a[id])
possible = false;
else {
int rep = (b[id] - a[id]) / (b[id - 1] + b[id + 1]);
ans += rep;
b[id] -= (b[id - 1] + b[id + 1]) * rep;
if (id == n)
b[0] = b[id];
if (id == 1)
b[n + 1] = b[id];
if (b[id] != a[id])
valeurs.push(make_pair(b[id], id));
}
// cout << b[id] << ' ' << a[id] << '\n';
}
/*for(int i=1;i<=n;i++) cout << b[i] << ' ';
cout << '\n';*/
if (possible)
cout << ans << '\n';
else
cout << -1 << '\n';
return 0;
}
| #include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int NMAX = 2 * 1e5 + 5;
int n, a[NMAX], b[NMAX];
ll ans = 0;
pair<int, int> p;
bool possible = true;
priority_queue<pair<int, int>> valeurs;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
a[0] = a[n];
a[n + 1] = a[1];
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i] != a[i])
valeurs.push(make_pair(b[i], i));
}
b[0] = b[n];
b[n + 1] = b[1];
while (!valeurs.empty() && possible) {
p = valeurs.top();
valeurs.pop();
int id = p.second;
// cout << (int)valeurs.size() << '\n';
// cout << id << ' ' << b[id] << ' ';
if (b[id] - (b[id - 1] + b[id + 1]) < a[id])
possible = false;
else {
int rep = (b[id] - a[id]) / (b[id - 1] + b[id + 1]);
ans += rep;
b[id] -= (b[id - 1] + b[id + 1]) * rep;
if (id == n)
b[0] = b[id];
if (id == 1)
b[n + 1] = b[id];
if (b[id] != a[id])
valeurs.push(make_pair(b[id], id));
}
// cout << b[id] << ' ' << a[id] << '\n';
}
/*for(int i=1;i<=n;i++) cout << b[i] << ' ';
cout << '\n';*/
if (possible)
cout << ans << '\n';
else
cout << -1 << '\n';
return 0;
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02941 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
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;
}
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
cout << "-1" << endl; \
return 0
#define MM << " " <<
#define Endl endl
signed main() {
int N;
int A[214514], B[214514];
cin >> N;
REP(i, N) { cin >> A[i]; }
REP(i, N) { cin >> B[i]; }
bool end[214514] = {};
priority_queue<pair<int, int>> P;
REP(i, N) {
if (A[i] > B[i]) {
GOODBYE;
} else if (A[i] == B[i]) {
end[i] = true;
} else {
P.push({B[i], i});
}
}
int Ans = 0;
while (!P.empty()) {
pair<int, int> t = P.top();
P.pop();
int s = (P.top().first);
int j = t.second;
int K = (B[(j + 1) % N] + B[(j - 1 + N) % N]);
int kai = (int)(1 + (t.first - s) / K);
if (end[(j + 1) % N] && end[(j - 1 + N) % N]) {
kai = (int)((t.first - A[j]) / K);
}
t.first -= K * (kai);
if (t.first == B[j]) {
GOODBYE;
}
B[j] = t.first;
if (A[j] > B[j]) {
GOODBYE;
} else if (A[j] != B[j]) {
P.push({B[j], j});
} else {
end[j] = true;
}
Ans += kai;
// REP(i,N){cout << B[i];if(end[i]==true)cout << "x";cout << " ";}cout <<
// endl;
}
cout << Ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
#define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
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;
}
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE \
cout << "-1" << endl; \
return 0
#define MM << " " <<
#define Endl endl
signed main() {
int N;
int A[214514], B[214514];
cin >> N;
REP(i, N) { cin >> A[i]; }
REP(i, N) { cin >> B[i]; }
bool end[214514] = {};
priority_queue<pair<int, int>> P;
REP(i, N) {
if (A[i] > B[i]) {
GOODBYE;
} else if (A[i] == B[i]) {
end[i] = true;
} else {
P.push({B[i], i});
}
}
int Ans = 0;
while (!P.empty()) {
pair<int, int> t = P.top();
P.pop();
int s = (P.top().first);
int j = t.second;
int K = (B[(j + 1) % N] + B[(j - 1 + N) % N]);
int kai = (int)((t.first - A[j]) / K);
t.first -= K * (kai);
if (t.first == B[j]) {
GOODBYE;
}
B[j] = t.first;
if (A[j] > B[j]) {
GOODBYE;
} else if (A[j] != B[j]) {
P.push({B[j], j});
} else {
end[j] = true;
}
Ans += kai;
// REP(i,N){cout << B[i];if(end[i]==true)cout << "x";cout << " ";}cout <<
// endl;
}
cout << Ans << endl;
return 0;
}
| replace | 58 | 62 | 58 | 59 | TLE | |
p02942 | C++ | Runtime Error | // ██████╗ ██╗ ██╗██╗ ██████╗ ███████╗
// ██╔══██╗██║ ██║██║ ██╔══██╗██╔════╝
// ██████╔╝██║ ██║██║█████╗██║ ██║█████╗
// ██╔══██╗██║ ██║██║╚════╝██║ ██║██╔══╝
// ██║ ██║╚██████╔╝██║ ██████╔╝███████╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝
#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define fst first
#define snd second
#define mp make_pair
// #define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define next ajksdslk
#define prev aklsfjk
mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
return (b < a ? (a = b, true) : false);
}
const int maxn = (int)3e5 + 100;
const int maxlog = 21;
const int base = 1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 2e9;
const ll llinf = 1e18;
vector<int> f[101][101];
int a[101][101];
int b[101][101];
int c[101][101];
bool used[101];
vector<int> g[101];
int mt[101];
bool try_kuhn(int v) {
if (used[v])
return false;
used[v] = true;
for (size_t i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (mt[to] == -1 || try_kuhn(mt[to])) {
mt[to] = v;
return true;
}
}
return false;
}
int main() {
// freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
a[i][j]--;
f[i][a[i][j] / m].pb(a[i][j]);
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++)
mt[i] = -1, g[i].clear();
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
if ((int)f[k][i].size()) {
g[k].pb(i);
}
}
}
for (int i = 0; i < n; i++) {
memset(used, 0, sizeof(used));
try_kuhn(i);
}
for (int i = 0; i < n; i++) {
assert(mt[i] != -1);
assert(f[i][mt[i]].size());
b[i][j] = f[i][mt[i]].back();
f[i][mt[i]].pop_back();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << b[i][j] + 1 << ' ';
}
cout << endl;
}
// return 0;
for (int i = 0; i < n; i++) {
int fnd = 0;
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if (b[k][j] / m == i) {
c[i][j] = b[k][j];
fnd++;
}
}
}
assert(fnd);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << c[i][j] + 1 << ' ';
}
cout << endl;
}
return 0;
} | // ██████╗ ██╗ ██╗██╗ ██████╗ ███████╗
// ██╔══██╗██║ ██║██║ ██╔══██╗██╔════╝
// ██████╔╝██║ ██║██║█████╗██║ ██║█████╗
// ██╔══██╗██║ ██║██║╚════╝██║ ██║██╔══╝
// ██║ ██║╚██████╔╝██║ ██████╔╝███████╗
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═════╝ ╚══════╝
#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define fst first
#define snd second
#define mp make_pair
// #define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define next ajksdslk
#define prev aklsfjk
mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
return (b < a ? (a = b, true) : false);
}
const int maxn = (int)3e5 + 100;
const int maxlog = 21;
const int base = 1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 2e9;
const ll llinf = 1e18;
vector<int> f[101][101];
int a[101][101];
int b[101][101];
int c[101][101];
bool used[101];
vector<int> g[101];
int mt[101];
bool try_kuhn(int v) {
if (used[v])
return false;
used[v] = true;
for (size_t i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (mt[to] == -1 || try_kuhn(mt[to])) {
mt[to] = v;
return true;
}
}
return false;
}
int main() {
// freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
a[i][j]--;
f[i][a[i][j] / m].pb(a[i][j]);
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++)
mt[i] = -1, g[i].clear();
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
if ((int)f[k][i].size()) {
g[i].pb(k);
}
}
}
for (int i = 0; i < n; i++) {
memset(used, 0, sizeof(used));
try_kuhn(i);
}
for (int i = 0; i < n; i++) {
assert(mt[i] != -1);
assert(f[i][mt[i]].size());
b[i][j] = f[i][mt[i]].back();
f[i][mt[i]].pop_back();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << b[i][j] + 1 << ' ';
}
cout << endl;
}
// return 0;
for (int i = 0; i < n; i++) {
int fnd = 0;
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if (b[k][j] / m == i) {
c[i][j] = b[k][j];
fnd++;
}
}
}
assert(fnd);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << c[i][j] + 1 << ' ';
}
cout << endl;
}
return 0;
} | replace | 97 | 98 | 97 | 98 | 0 | |
p02942 | C++ | Runtime Error | /*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100, inf = (1 << 29);
int Rr, Cc, Ps, Pt, Emat[N + 3][N + 3], Edep[N * 3], Eans[N + 3][N + 3];
bool Bb[N + 3][N + 3];
vector<int> Vec[N + 3];
struct GGRAPH {
struct GNODE {
int to, cap, Vx, Vy;
GNODE *nxt, *rev;
} Gnod[N * N];
GNODE *Gtop[N * 3], *Gcnt;
void Init(int Nn) {
Gcnt = Gnod;
fill(Gtop, Gtop + 1 + Nn, Gcnt);
}
void AddEdge(int u, int v, int ca1, int ca2, int Vx, int Vy) {
GNODE *p = ++Gcnt, *q = ++Gcnt;
*p = (GNODE){v, ca1, Vx, Vy, Gtop[u], q};
Gtop[u] = p;
*q = (GNODE){u, ca2, Vx, Vy, Gtop[v], p};
Gtop[v] = q;
}
} Gflw;
GGRAPH::GNODE *Cop[N * 3];
int Aug(int u, int flwin) {
if (u == Pt)
return flwin;
int flwout = 0;
for (auto &it = Cop[u]; it; it = it->nxt) {
int v = it->to;
if (!it->cap || Edep[v] != Edep[u] + 1)
continue;
int flwv = min(flwin - flwout, it->cap);
flwv = Aug(v, flwv);
flwout += flwv;
it->cap -= flwv;
it->rev->cap += flwv;
if (flwout == flwin)
break;
}
return flwout;
}
bool BFS() {
for (int i = 1; i <= Rr * 2 + 2; i++)
Cop[i] = Gflw.Gtop[i], Edep[i] = inf;
queue<int> que;
Edep[Ps] = 0;
que.push(Ps);
while (!que.empty()) {
int u = que.front();
que.pop();
for (auto it = Cop[u]; it; it = it->nxt) {
int v = it->to;
if (!it->cap || Edep[v] != inf)
continue;
Edep[v] = Edep[u] + 1;
if (v == Pt)
return true;
que.push(v);
}
}
return false;
}
int Dinic() {
int mxflw = 0;
while (BFS())
mxflw += Aug(Ps, inf);
return mxflw;
}
int main() {
scanf("%d%d", &Rr, &Cc);
Ps = 2 * Rr + 1;
Pt = 2 * Rr + 2;
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++)
scanf("%d", &Emat[i][j]);
for (int i = 1; i <= Cc; i++) {
Gflw.Init(2 * Rr + 2);
int Vtot = 0;
for (int j = 1; j <= Rr; j++)
for (int k = 1; k <= Cc; k++)
if (!Bb[j][k]) {
Gflw.AddEdge(j, (Emat[j][k] - 1) / Cc + 1 + Rr, 1, 0, j, k);
Vtot += 2;
}
for (int j = 1; j <= Rr; j++)
Gflw.AddEdge(Ps, j, 1, 0, 0, 0);
for (int j = 1; j <= Rr; j++)
Gflw.AddEdge(j + Rr, Pt, 1, 0, 0, 0);
int Rs = Dinic();
for (int j = 1; j <= Vtot; j += 2) {
auto it = Gflw.Gnod[j];
if (it.cap == 0) {
Eans[it.Vx][i] = Emat[it.Vx][it.Vy];
Bb[it.Vx][it.Vy] = true;
}
}
}
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++) {
printf("%d", Eans[i][j]);
Vec[j].push_back(Eans[i][j]);
if (j == Cc)
printf("\n");
else
printf(" ");
}
for (int i = 1; i <= Cc; i++) {
sort(Vec[i].begin(), Vec[i].end());
for (int j = 1; j <= Rr; j++)
Eans[j][i] = Vec[i][j - 1];
}
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++) {
printf("%d", Eans[i][j]);
if (j == Cc)
printf("\n");
else
printf(" ");
}
return 0;
} | /*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100, inf = (1 << 29);
int Rr, Cc, Ps, Pt, Emat[N + 3][N + 3], Edep[N * 3], Eans[N + 3][N + 3];
bool Bb[N + 3][N + 3];
vector<int> Vec[N + 3];
struct GGRAPH {
struct GNODE {
int to, cap, Vx, Vy;
GNODE *nxt, *rev;
} Gnod[(N * N + N * 2) * 3];
GNODE *Gtop[N * 3], *Gcnt;
void Init(int Nn) {
Gcnt = Gnod;
fill(Gtop, Gtop + 1 + Nn, Gcnt);
}
void AddEdge(int u, int v, int ca1, int ca2, int Vx, int Vy) {
GNODE *p = ++Gcnt, *q = ++Gcnt;
*p = (GNODE){v, ca1, Vx, Vy, Gtop[u], q};
Gtop[u] = p;
*q = (GNODE){u, ca2, Vx, Vy, Gtop[v], p};
Gtop[v] = q;
}
} Gflw;
GGRAPH::GNODE *Cop[N * 3];
int Aug(int u, int flwin) {
if (u == Pt)
return flwin;
int flwout = 0;
for (auto &it = Cop[u]; it; it = it->nxt) {
int v = it->to;
if (!it->cap || Edep[v] != Edep[u] + 1)
continue;
int flwv = min(flwin - flwout, it->cap);
flwv = Aug(v, flwv);
flwout += flwv;
it->cap -= flwv;
it->rev->cap += flwv;
if (flwout == flwin)
break;
}
return flwout;
}
bool BFS() {
for (int i = 1; i <= Rr * 2 + 2; i++)
Cop[i] = Gflw.Gtop[i], Edep[i] = inf;
queue<int> que;
Edep[Ps] = 0;
que.push(Ps);
while (!que.empty()) {
int u = que.front();
que.pop();
for (auto it = Cop[u]; it; it = it->nxt) {
int v = it->to;
if (!it->cap || Edep[v] != inf)
continue;
Edep[v] = Edep[u] + 1;
if (v == Pt)
return true;
que.push(v);
}
}
return false;
}
int Dinic() {
int mxflw = 0;
while (BFS())
mxflw += Aug(Ps, inf);
return mxflw;
}
int main() {
scanf("%d%d", &Rr, &Cc);
Ps = 2 * Rr + 1;
Pt = 2 * Rr + 2;
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++)
scanf("%d", &Emat[i][j]);
for (int i = 1; i <= Cc; i++) {
Gflw.Init(2 * Rr + 2);
int Vtot = 0;
for (int j = 1; j <= Rr; j++)
for (int k = 1; k <= Cc; k++)
if (!Bb[j][k]) {
Gflw.AddEdge(j, (Emat[j][k] - 1) / Cc + 1 + Rr, 1, 0, j, k);
Vtot += 2;
}
for (int j = 1; j <= Rr; j++)
Gflw.AddEdge(Ps, j, 1, 0, 0, 0);
for (int j = 1; j <= Rr; j++)
Gflw.AddEdge(j + Rr, Pt, 1, 0, 0, 0);
int Rs = Dinic();
for (int j = 1; j <= Vtot; j += 2) {
auto it = Gflw.Gnod[j];
if (it.cap == 0) {
Eans[it.Vx][i] = Emat[it.Vx][it.Vy];
Bb[it.Vx][it.Vy] = true;
}
}
}
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++) {
printf("%d", Eans[i][j]);
Vec[j].push_back(Eans[i][j]);
if (j == Cc)
printf("\n");
else
printf(" ");
}
for (int i = 1; i <= Cc; i++) {
sort(Vec[i].begin(), Vec[i].end());
for (int j = 1; j <= Rr; j++)
Eans[j][i] = Vec[i][j - 1];
}
for (int i = 1; i <= Rr; i++)
for (int j = 1; j <= Cc; j++) {
printf("%d", Eans[i][j]);
if (j == Cc)
printf("\n");
else
printf(" ");
}
return 0;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02942 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
const int N = 101;
int a[N][N];
int r[N]; // r[x] = row of element x
int p[N][N]; // p[x][y] = row of element y in column x
int next(int row) {
int col = 0;
while (a[row][col] != -1)
++col;
return col;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int x;
scanf("%d", &x);
r[x - 1] = i;
a[i][j] = -1;
}
for (int i = 0; i < n * m; ++i) {
int row = r[i];
int col1 = i % m;
int col2 = next(row);
int val = i / m;
if (col1 == col2) {
a[row][col1] = val;
p[col1][val] = row;
} else
while (val != -1) {
p[col1][val] = row;
std::swap(val, a[row][col1]);
a[row][col2] = val;
if (val != -1)
std::swap(row, p[col2][val]);
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int val = a[i][j] * m;
for (int k = 0; k < m; ++k)
if (r[val + k] == i) {
a[i][j] = val + k;
r[val + k] = -1;
break;
}
}
for (int i = 0; i < n; ++i, puts(""))
for (int j = 0; j < m; ++j)
printf("%d ", a[i][j] + 1);
for (int i = 0; i < m; ++i)
for (int j = 1; j < n; ++j)
for (int k = 0; k < n - j; ++k)
if (a[k][i] > a[k + 1][i])
std::swap(a[k][i], a[k + 1][i]);
for (int i = 0; i < n; ++i, puts(""))
for (int j = 0; j < m; ++j)
printf("%d ", a[i][j] + 1);
} | #include <algorithm>
#include <cstdio>
const int N = 101;
int a[N][N];
int r[N * N]; // r[x] = row of element x
int p[N][N]; // p[x][y] = row of element y in column x
int next(int row) {
int col = 0;
while (a[row][col] != -1)
++col;
return col;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int x;
scanf("%d", &x);
r[x - 1] = i;
a[i][j] = -1;
}
for (int i = 0; i < n * m; ++i) {
int row = r[i];
int col1 = i % m;
int col2 = next(row);
int val = i / m;
if (col1 == col2) {
a[row][col1] = val;
p[col1][val] = row;
} else
while (val != -1) {
p[col1][val] = row;
std::swap(val, a[row][col1]);
a[row][col2] = val;
if (val != -1)
std::swap(row, p[col2][val]);
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int val = a[i][j] * m;
for (int k = 0; k < m; ++k)
if (r[val + k] == i) {
a[i][j] = val + k;
r[val + k] = -1;
break;
}
}
for (int i = 0; i < n; ++i, puts(""))
for (int j = 0; j < m; ++j)
printf("%d ", a[i][j] + 1);
for (int i = 0; i < m; ++i)
for (int j = 1; j < n; ++j)
for (int k = 0; k < n - j; ++k)
if (a[k][i] > a[k + 1][i])
std::swap(a[k][i], a[k + 1][i]);
for (int i = 0; i < n; ++i, puts(""))
for (int j = 0; j < m; ++j)
printf("%d ", a[i][j] + 1);
}
| replace | 4 | 6 | 4 | 6 | TLE | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
namespace maxflow {
#define inf 999999999
struct edge {
int to, nxt, f;
} e[20001];
int head[211];
int edge_cnt;
int s, t;
void init(int s1, int t1) {
s = s1, t = t1;
edge_cnt = -1;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int f) {
e[++edge_cnt].nxt = head[u];
e[edge_cnt].to = v;
e[edge_cnt].f = f;
head[u] = edge_cnt;
}
void add_edge(int u, int v, int f) {
add(u, v, f);
add(v, u, 0);
}
int dep[211];
bool bfs(int s, int t) {
memset(dep, 0x3f, sizeof(dep));
queue<int> q;
q.push(s);
dep[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = e[i].nxt)
if (e[i].f != 0) {
int to = e[i].to;
if (dep[to] > dep[u] + 1) {
dep[to] = dep[u] + 1;
q.push(to);
}
}
}
return dep[t] < inf;
}
int dfs(int u, int flow) {
if (u == t)
return flow;
int use = flow;
for (int i = head[u]; i != -1; i = e[i].nxt) {
int to = e[i].to;
if (use && dep[to] == dep[u] + 1 && e[i].f) {
int f = dfs(to, min(e[i].f, use));
use -= f;
e[i].f -= f;
e[i ^ 1].f += f;
}
}
return flow - use;
}
int dinic() {
int maxflow = 0;
while (bfs(s, t))
maxflow += dfs(s, inf);
return maxflow;
}
} // namespace maxflow
using namespace maxflow;
int c[101][101];
int a[101][101];
vector<int> rem[101][101];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
rem[i][(a[i][j] + m - 1) / m].push_back(a[i][j]);
}
for (int p = 1; p <= m; p++) {
init(0, 210);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (rem[i][j].size())
add_edge(i, j + n + 1, 1);
add_edge(s, i, 1);
add_edge(i + n + 1, t, 1);
}
dinic();
// printf("%d\n",dinic());
for (int i = 1; i <= n; i++) {
int at = 0;
for (int j = head[i]; j != -1; j = e[j].nxt)
if (e[j].f == 0 && e[j].to > n + 1)
at = e[j].to - n - 1;
c[i][p] = rem[i][at].back();
rem[i][at].pop_back();
}
// for(int t=1;t<=n;t++)
// printf("%d ",c[t][p]);
// printf("\n");
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++)
for (int k = i + 1; k <= n; k++)
if (c[k][j] <= c[i][j])
swap(c[k][j], c[i][j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
namespace maxflow {
#define inf 999999999
struct edge {
int to, nxt, f;
} e[200001];
int head[211];
int edge_cnt;
int s, t;
void init(int s1, int t1) {
s = s1, t = t1;
edge_cnt = -1;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int f) {
e[++edge_cnt].nxt = head[u];
e[edge_cnt].to = v;
e[edge_cnt].f = f;
head[u] = edge_cnt;
}
void add_edge(int u, int v, int f) {
add(u, v, f);
add(v, u, 0);
}
int dep[211];
bool bfs(int s, int t) {
memset(dep, 0x3f, sizeof(dep));
queue<int> q;
q.push(s);
dep[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = e[i].nxt)
if (e[i].f != 0) {
int to = e[i].to;
if (dep[to] > dep[u] + 1) {
dep[to] = dep[u] + 1;
q.push(to);
}
}
}
return dep[t] < inf;
}
int dfs(int u, int flow) {
if (u == t)
return flow;
int use = flow;
for (int i = head[u]; i != -1; i = e[i].nxt) {
int to = e[i].to;
if (use && dep[to] == dep[u] + 1 && e[i].f) {
int f = dfs(to, min(e[i].f, use));
use -= f;
e[i].f -= f;
e[i ^ 1].f += f;
}
}
return flow - use;
}
int dinic() {
int maxflow = 0;
while (bfs(s, t))
maxflow += dfs(s, inf);
return maxflow;
}
} // namespace maxflow
using namespace maxflow;
int c[101][101];
int a[101][101];
vector<int> rem[101][101];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
rem[i][(a[i][j] + m - 1) / m].push_back(a[i][j]);
}
for (int p = 1; p <= m; p++) {
init(0, 210);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (rem[i][j].size())
add_edge(i, j + n + 1, 1);
add_edge(s, i, 1);
add_edge(i + n + 1, t, 1);
}
dinic();
// printf("%d\n",dinic());
for (int i = 1; i <= n; i++) {
int at = 0;
for (int j = head[i]; j != -1; j = e[j].nxt)
if (e[j].f == 0 && e[j].to > n + 1)
at = e[j].to - n - 1;
c[i][p] = rem[i][at].back();
rem[i][at].pop_back();
}
// for(int t=1;t<=n;t++)
// printf("%d ",c[t][p]);
// printf("\n");
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++)
for (int k = i + 1; k <= n; k++)
if (c[k][j] <= c[i][j])
swap(c[k][j], c[i][j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
int ch[111111];
int mp[111][111];
int used[111];
bool used_gyou[111];
int n, m;
void init() {
int cou = 0;
for (int i = 1; i < 111111; i++) {
ch[i] = cou;
if (i % m == 0) {
cou++;
}
}
REP(i, n) {
REP(j, m) { cin >> mp[i][j]; }
}
}
bool check_gyou(int now_retsu, int now_gyou) {
// cout << "check_gyou " << now_retsu << " " << now_gyou << endl;
used_gyou[now_gyou] = true;
for (int i = now_retsu; i < m; i++) {
if (used[ch[mp[now_gyou][i]]] == -1) {
used[ch[mp[now_gyou][i]]] = now_gyou;
swap(mp[now_gyou][now_retsu], mp[now_gyou][i]);
return true;
}
}
for (int i = now_retsu; i < m; i++) {
if (used_gyou[used[ch[mp[now_gyou][i]]]]) {
if (check_gyou(now_retsu, used[ch[mp[now_gyou][i]]])) {
used[ch[mp[now_gyou][i]]] = now_gyou;
swap(mp[now_gyou][now_retsu], mp[now_gyou][i]);
return true;
}
}
}
return false;
}
void seica_gyou() {
REP(retsu, m) {
REP(i, 111) { used[i] = -1; }
REP(gyou, n) {
bool flag = false;
for (int i = retsu; i < m; i++) {
// cout << "# " << gyou << " " << retsu << " " << i << " " <<
// mp[gyou][i] << " " << used[ch[mp[gyou][i]]] << endl;
if (used[ch[mp[gyou][i]]] == -1) {
used[ch[mp[gyou][i]]] = gyou;
swap(mp[gyou][retsu], mp[gyou][i]);
flag = true;
break;
}
}
if (flag) {
// cout << "A " << gyou << " " << retsu << " " << mp[gyou][retsu] <<
// endl;
continue;
}
REP(i, 111) { used_gyou[i] = false; }
for (int i = retsu; i < m; i++) {
if (check_gyou(retsu, used[ch[mp[gyou][i]]])) {
used[ch[mp[gyou][i]]] = gyou;
swap(mp[gyou][retsu], mp[gyou][i]);
flag = true;
break;
}
}
if (!flag) {
cout << "!!!" << endl;
exit(1);
}
// cout << "B ";REP(i, gyou+1)cout << mp[i][retsu] << " ";cout << endl;
}
/*
REP(i, n){
REP(j, m){
cout << mp[i][j] << " ";
}
cout << endl;
}
cout << endl;
*/
}
}
void seica_retsu() {
REP(retsu, m) {
vector<int> v;
REP(gyou, n) { v.PB(mp[gyou][retsu]); }
sort(ALL(v));
REP(gyou, n) { mp[gyou][retsu] = v[gyou]; }
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
init();
seica_gyou();
REP(i, n) {
REP(j, m) { cout << mp[i][j] << " "; }
cout << endl;
}
seica_retsu();
REP(i, n) {
REP(j, m) { cout << mp[i][j] << " "; }
cout << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
int ch[111111];
int mp[111][111];
int used[111];
bool used_gyou[111];
int n, m;
void init() {
int cou = 0;
for (int i = 1; i < 111111; i++) {
ch[i] = cou;
if (i % m == 0) {
cou++;
}
}
REP(i, n) {
REP(j, m) { cin >> mp[i][j]; }
}
}
bool check_gyou(int now_retsu, int now_gyou) {
// cout << "check_gyou " << now_retsu << " " << now_gyou << endl;
used_gyou[now_gyou] = true;
for (int i = now_retsu; i < m; i++) {
if (used[ch[mp[now_gyou][i]]] == -1) {
used[ch[mp[now_gyou][i]]] = now_gyou;
swap(mp[now_gyou][now_retsu], mp[now_gyou][i]);
return true;
}
}
for (int i = now_retsu; i < m; i++) {
if (!used_gyou[used[ch[mp[now_gyou][i]]]]) {
if (check_gyou(now_retsu, used[ch[mp[now_gyou][i]]])) {
used[ch[mp[now_gyou][i]]] = now_gyou;
swap(mp[now_gyou][now_retsu], mp[now_gyou][i]);
return true;
}
}
}
return false;
}
void seica_gyou() {
REP(retsu, m) {
REP(i, 111) { used[i] = -1; }
REP(gyou, n) {
bool flag = false;
for (int i = retsu; i < m; i++) {
// cout << "# " << gyou << " " << retsu << " " << i << " " <<
// mp[gyou][i] << " " << used[ch[mp[gyou][i]]] << endl;
if (used[ch[mp[gyou][i]]] == -1) {
used[ch[mp[gyou][i]]] = gyou;
swap(mp[gyou][retsu], mp[gyou][i]);
flag = true;
break;
}
}
if (flag) {
// cout << "A " << gyou << " " << retsu << " " << mp[gyou][retsu] <<
// endl;
continue;
}
REP(i, 111) { used_gyou[i] = false; }
for (int i = retsu; i < m; i++) {
if (check_gyou(retsu, used[ch[mp[gyou][i]]])) {
used[ch[mp[gyou][i]]] = gyou;
swap(mp[gyou][retsu], mp[gyou][i]);
flag = true;
break;
}
}
if (!flag) {
cout << "!!!" << endl;
exit(1);
}
// cout << "B ";REP(i, gyou+1)cout << mp[i][retsu] << " ";cout << endl;
}
/*
REP(i, n){
REP(j, m){
cout << mp[i][j] << " ";
}
cout << endl;
}
cout << endl;
*/
}
}
void seica_retsu() {
REP(retsu, m) {
vector<int> v;
REP(gyou, n) { v.PB(mp[gyou][retsu]); }
sort(ALL(v));
REP(gyou, n) { mp[gyou][retsu] = v[gyou]; }
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
init();
seica_gyou();
REP(i, n) {
REP(j, m) { cout << mp[i][j] << " "; }
cout << endl;
}
seica_retsu();
REP(i, n) {
REP(j, m) { cout << mp[i][j] << " "; }
cout << endl;
}
return 0;
}
| replace | 65 | 66 | 65 | 66 | 0 | |
p02942 | C++ | Runtime Error | // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int i = 0; i < (n); i++) \
ni(a[i])
#define nal(a, n) \
for (int i = 0; i < (n); i++) \
nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e2 + 5;
const double eps = 1e-9;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
struct BPM {
vi adj[MAXN];
int l, r, dist[MAXN], match[MAXN];
void init(int l1, int r1) { l = l1, r = r1; }
void addEdge(int u, int v) {
adj[u].pb(v + l);
adj[v + l].pb(u);
}
void reset() {
for (int i = 0; i <= l + r; i++)
adj[i].clear();
l = 0, r = 0;
}
bool dfs(int u) {
if (u == 0)
return 1;
for (int v : adj[u]) {
if (dist[match[v]] == dist[u] + 1 && dfs(match[v])) {
match[v] = u;
match[u] = v;
return 1;
}
}
dist[u] = INF;
return 0;
}
bool bfs() {
deque<int> pq;
for (int i = 1; i <= l; i++) {
if (match[i] == 0) {
dist[i] = 0;
pq.pb(i);
} else
dist[i] = INF;
}
dist[0] = INF;
while (!pq.empty()) {
int u = pq.front();
pq.pop_front();
if (u == 0)
continue;
for (int v : adj[u]) {
if (dist[match[v]] == INF) {
dist[match[v]] = dist[u] + 1;
pq.pb(match[v]);
}
}
}
if (dist[0] == INF)
return 0;
return 1;
}
// runtime: O(sqrt(V)E)
// l nodes left, r nodes right
int matching() {
int ret = 0;
memset(match, 0, sizeof match);
while (bfs())
for (int i = 1; i <= l; i++)
if (match[i] == 0 && dfs(i))
ret++;
return ret;
}
} g;
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for (int i = 1; i <= m; i++) {
g.reset();
g.init(n, n);
for (int j = i; j <= m; j++)
for (int k = 1; k <= n; k++)
g.addEdge(k, (a[k][j] - 1) / m + 1);
g.matching();
for (int j = 1; j <= n; j++) {
for (int k = i; k <= m; k++) {
if (g.match[j] != (a[j][k] - 1) / m + 1 + n)
continue;
swap(a[j][k], a[j][i]);
break;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", a[i][j]);
enl;
}
// enl;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
b[(a[j][i] - 1) / m + 1][i] = a[j][i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
enl;
}
return 0;
}
| // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int i = 0; i < (n); i++) \
ni(a[i])
#define nal(a, n) \
for (int i = 0; i < (n); i++) \
nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e2 + 5;
const double eps = 1e-9;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
struct BPM {
vi adj[MAXN];
int l, r, dist[MAXN], match[MAXN];
void init(int l1, int r1) { l = l1, r = r1; }
void addEdge(int u, int v) {
adj[u].pb(v + l);
adj[v + l].pb(u);
}
void reset() {
for (int i = 0; i <= l + r; i++)
adj[i].clear();
l = 0, r = 0;
}
bool dfs(int u) {
if (u == 0)
return 1;
for (int v : adj[u]) {
if (dist[match[v]] == dist[u] + 1 && dfs(match[v])) {
match[v] = u;
match[u] = v;
return 1;
}
}
dist[u] = INF;
return 0;
}
bool bfs() {
deque<int> pq;
for (int i = 1; i <= l; i++) {
if (match[i] == 0) {
dist[i] = 0;
pq.pb(i);
} else
dist[i] = INF;
}
dist[0] = INF;
while (!pq.empty()) {
int u = pq.front();
pq.pop_front();
if (u == 0)
continue;
for (int v : adj[u]) {
if (dist[match[v]] == INF) {
dist[match[v]] = dist[u] + 1;
pq.pb(match[v]);
}
}
}
if (dist[0] == INF)
return 0;
return 1;
}
// runtime: O(sqrt(V)E)
// l nodes left, r nodes right
int matching() {
int ret = 0;
memset(match, 0, sizeof match);
while (bfs())
for (int i = 1; i <= l; i++)
if (match[i] == 0 && dfs(i))
ret++;
return ret;
}
} g;
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for (int i = 1; i <= m; i++) {
g.reset();
g.init(n, n);
for (int j = i; j <= m; j++)
for (int k = 1; k <= n; k++)
g.addEdge(k, (a[k][j] - 1) / m + 1);
g.matching();
for (int j = 1; j <= n; j++) {
for (int k = i; k <= m; k++) {
if (g.match[j] != (a[j][k] - 1) / m + 1 + n)
continue;
swap(a[j][k], a[j][i]);
break;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", a[i][j]);
enl;
}
// enl;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
b[(a[j][i] - 1) / m + 1][i] = a[j][i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
enl;
}
return 0;
}
| replace | 40 | 41 | 40 | 41 | 0 | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
#define maxn 128
using namespace std;
int n, m;
int a[maxn][maxn];
void read() {
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
cin >> a[i][j];
}
vector<int> v[maxn];
bool used[maxn * maxn];
bool tused[maxn * maxn];
int match[maxn * maxn];
void clearr() {
int i;
for (i = 0; i < maxn; i++)
v[i].clear();
}
/*void build()
{
int i,j;
for(i=0;i<n;i++)
{
int sz=v[i].size();
for(j=0;j<v[i].size();j++)
{
int nb=v[i][j];
if(!match[nb])
{
match[nb]=i;
break;
}
}
}
}*/
bool khun(int i) {
// cout<<i<<" ";
if (tused[i])
return false;
tused[i] = true;
int j, sz = v[i].size();
for (j = 0; j < sz; j++) {
int t = v[i][j];
if (match[t] == -1 || khun(match[t])) {
match[t] = i;
match[i] = t;
return true;
}
}
return false;
}
int ans[maxn][maxn];
void solve() {
int i, j, k;
for (i = 0; i < m; i++) {
clearr();
for (j = 0; j < n; j++)
for (k = 0; k < m; k++)
if (!used[a[j][k]]) {
v[j].push_back((a[j][k] - 1) / m + n);
v[(a[j][k] - 1) / m + n].push_back(j);
}
memset(match, -1, sizeof(match));
for (j = 0; j < n; j++) {
memset(tused, 0, sizeof(tused));
khun(j);
}
for (j = 0; j < n; j++) {
if (match[j] != -1) {
for (k = 0; k < m; k++) {
if ((a[j][k] - 1) / m + n == match[j] && !used[a[j][k]]) {
used[a[j][k]] = true;
ans[j][i] = a[j][k];
break;
}
}
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
cout << ans[i][j] << " ";
cout << endl;
}
}
// bool ans[maxn][maxn];
int main() {
int i, j, k;
read();
solve();
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
for (k = 0; k < n; k++)
if ((ans[k][j] - 1) / m == i) {
cout << ans[k][j] << " ";
break;
}
cout << endl;
}
return 0;
}
/*
3 4
1 4 7 10
2 5 8 11
3 6 9 12
*/
| #include <bits/stdc++.h>
#define maxn 128
using namespace std;
int n, m;
int a[maxn][maxn];
void read() {
cin >> n >> m;
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
cin >> a[i][j];
}
vector<int> v[maxn * maxn];
bool used[maxn * maxn];
bool tused[maxn * maxn];
int match[maxn * maxn];
void clearr() {
int i;
for (i = 0; i < maxn; i++)
v[i].clear();
}
/*void build()
{
int i,j;
for(i=0;i<n;i++)
{
int sz=v[i].size();
for(j=0;j<v[i].size();j++)
{
int nb=v[i][j];
if(!match[nb])
{
match[nb]=i;
break;
}
}
}
}*/
bool khun(int i) {
// cout<<i<<" ";
if (tused[i])
return false;
tused[i] = true;
int j, sz = v[i].size();
for (j = 0; j < sz; j++) {
int t = v[i][j];
if (match[t] == -1 || khun(match[t])) {
match[t] = i;
match[i] = t;
return true;
}
}
return false;
}
int ans[maxn][maxn];
void solve() {
int i, j, k;
for (i = 0; i < m; i++) {
clearr();
for (j = 0; j < n; j++)
for (k = 0; k < m; k++)
if (!used[a[j][k]]) {
v[j].push_back((a[j][k] - 1) / m + n);
v[(a[j][k] - 1) / m + n].push_back(j);
}
memset(match, -1, sizeof(match));
for (j = 0; j < n; j++) {
memset(tused, 0, sizeof(tused));
khun(j);
}
for (j = 0; j < n; j++) {
if (match[j] != -1) {
for (k = 0; k < m; k++) {
if ((a[j][k] - 1) / m + n == match[j] && !used[a[j][k]]) {
used[a[j][k]] = true;
ans[j][i] = a[j][k];
break;
}
}
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
cout << ans[i][j] << " ";
cout << endl;
}
}
// bool ans[maxn][maxn];
int main() {
int i, j, k;
read();
solve();
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
for (k = 0; k < n; k++)
if ((ans[k][j] - 1) / m == i) {
cout << ans[k][j] << " ";
break;
}
cout << endl;
}
return 0;
}
/*
3 4
1 4 7 10
2 5 8 11
3 6 9 12
*/
| replace | 17 | 18 | 17 | 18 | 0 | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MAXN 128
using namespace std;
int n, m;
vector<int> g[MAXN];
int a[MAXN][MAXN], b[MAXN][MAXN], us[MAXN][MAXN], used[MAXN], mt[MAXN];
void read() {
int i, j;
cin >> n >> m;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
cin >> a[i][j];
}
bool try_kuhn(int v) {
if (used[v])
return false;
used[v] = true;
for (size_t i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (mt[to] == -1 || try_kuhn(mt[to])) {
mt[to] = v;
mt[v] = to;
return true;
}
}
return false;
}
void solve() {
int i, j, l;
for (l = 0; l < m; l++) {
// cout<<"---------\n";
for (i = 0; i < n; i++)
g[i].clear();
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (!us[i][j]) {
g[i].push_back(a[i][j] / m - (a[i][j] % m == 0) + n);
g[a[i][j] / m - (a[i][j] % m == 0) + n].push_back(i);
// cout<<i<<" "<<a[i][j]/m-(a[i][j]%m==0)+n<<endl;
}
}
}
// cout<<"---------\n";
memset(mt, -1, sizeof(mt));
for (int v = 0; v < n; ++v) {
memset(used, 0, sizeof(used));
try_kuhn(v);
}
for (int i = 0; i < n; ++i) {
// cout<<mt[i]<<endl;
if (mt[i] != -1) {
// printf ("%d %d\n", mt[i], i);
for (j = 0; j < m; j++) {
if (a[i][j] / m - (a[i][j] % m == 0) + n == mt[i] && !us[i][j]) {
us[i][j] = true;
/// cout<<" - > "<<i<<" "<<j<<endl;
b[i][l] = a[i][j];
break;
}
}
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
vector<int> h;
for (j = 0; j < m; j++) {
h.clear();
for (i = 0; i < n; i++) {
h.push_back(b[i][j]);
}
sort(h.begin(), h.end());
for (i = 0; i < n; i++) {
b[i][j] = h[i];
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
int main() {
read();
solve();
return 0;
}
| #include <bits/stdc++.h>
#define MAXN 128 * 4
using namespace std;
int n, m;
vector<int> g[MAXN];
int a[MAXN][MAXN], b[MAXN][MAXN], us[MAXN][MAXN], used[MAXN], mt[MAXN];
void read() {
int i, j;
cin >> n >> m;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
cin >> a[i][j];
}
bool try_kuhn(int v) {
if (used[v])
return false;
used[v] = true;
for (size_t i = 0; i < g[v].size(); ++i) {
int to = g[v][i];
if (mt[to] == -1 || try_kuhn(mt[to])) {
mt[to] = v;
mt[v] = to;
return true;
}
}
return false;
}
void solve() {
int i, j, l;
for (l = 0; l < m; l++) {
// cout<<"---------\n";
for (i = 0; i < n; i++)
g[i].clear();
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (!us[i][j]) {
g[i].push_back(a[i][j] / m - (a[i][j] % m == 0) + n);
g[a[i][j] / m - (a[i][j] % m == 0) + n].push_back(i);
// cout<<i<<" "<<a[i][j]/m-(a[i][j]%m==0)+n<<endl;
}
}
}
// cout<<"---------\n";
memset(mt, -1, sizeof(mt));
for (int v = 0; v < n; ++v) {
memset(used, 0, sizeof(used));
try_kuhn(v);
}
for (int i = 0; i < n; ++i) {
// cout<<mt[i]<<endl;
if (mt[i] != -1) {
// printf ("%d %d\n", mt[i], i);
for (j = 0; j < m; j++) {
if (a[i][j] / m - (a[i][j] % m == 0) + n == mt[i] && !us[i][j]) {
us[i][j] = true;
/// cout<<" - > "<<i<<" "<<j<<endl;
b[i][l] = a[i][j];
break;
}
}
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
vector<int> h;
for (j = 0; j < m; j++) {
h.clear();
for (i = 0; i < n; i++) {
h.push_back(b[i][j]);
}
sort(h.begin(), h.end());
for (i = 0; i < n; i++) {
b[i][j] = h[i];
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
int main() {
read();
solve();
return 0;
}
| replace | 1 | 2 | 1 | 2 | 0 | |
p02942 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 210, M = 20010;
const int INF = 1e9;
struct edge {
int from, to, flow, cap;
edge() {}
edge(int _1, int _2, int _3, int _4) : from(_1), to(_2), flow(_3), cap(_4) {}
};
struct Dinic {
edge edges[M];
int head[N], nxt[M], tot;
inline void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
inline void add_edge(int x, int y, int z) {
edges[tot] = edge(x, y, 0, z);
nxt[tot] = head[x];
head[x] = tot++;
edges[tot] = edge(y, x, 0, 0);
nxt[tot] = head[y];
head[y] = tot++;
}
int s, t, L, R;
int cur[N];
int d[N];
bool bfs() {
memset(d + L, -1, sizeof(int) * (R - L + 1));
queue<int> q;
q.push(s);
d[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
cur[x] = head[x];
for (int i = head[x]; ~i; i = nxt[i]) {
edge &e = edges[i];
if (d[e.to] == -1 && e.flow < e.cap) {
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return d[t] != -1;
}
int dfs(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f = 0;
for (int &i = cur[x]; ~i; i = nxt[i]) {
edge &e = edges[i];
if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i ^ 1].flow -= f;
a -= f;
flow += f;
if (!a)
break;
}
}
return flow;
}
inline int maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
while (bfs())
flow += dfs(s, INF);
return flow;
}
} dinic;
int n, m;
int a[110][110], b[110][110], c[110][110];
int val[110][110];
vector<int> vec[110][110];
inline int get(int x) { return (x - 1) / m + 1; }
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
vec[i][get(a[i][j])].push_back(a[i][j]);
for (int i = 1; i <= m; i++) {
dinic.init();
dinic.L = 0, dinic.R = 2 * n + 2;
int S = 2 * n + 1, T = 2 * n + 2;
for (int j = 1; j <= n; j++)
dinic.add_edge(S, j, 1);
for (int j = 1; j <= n; j++)
dinic.add_edge(j + n, T, 1);
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (!vec[j][k].empty())
dinic.add_edge(j, k + n, 1);
dinic.maxflow(S, T);
for (int j = 1; j <= n; j++)
for (int k = dinic.head[j]; ~k; k = dinic.nxt[k]) {
edge &e = dinic.edges[k];
if (e.to != S && e.to != T && e.to > n && e.cap == e.flow) {
b[j][i] = vec[j][e.to - n].back();
vec[j][e.to - n].pop_back();
}
}
}
for (int i = 1; i <= m; i++) {
static int tmp[110];
for (int j = 1; j <= n; j++)
tmp[j] = b[j][i];
sort(tmp + 1, tmp + 1 + n, [](int a, int b) { return get(a) < get(b); });
for (int j = 1; j <= n; j++)
c[j][i] = tmp[j];
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 210, M = 40010;
const int INF = 1e9;
struct edge {
int from, to, flow, cap;
edge() {}
edge(int _1, int _2, int _3, int _4) : from(_1), to(_2), flow(_3), cap(_4) {}
};
struct Dinic {
edge edges[M];
int head[N], nxt[M], tot;
inline void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
inline void add_edge(int x, int y, int z) {
edges[tot] = edge(x, y, 0, z);
nxt[tot] = head[x];
head[x] = tot++;
edges[tot] = edge(y, x, 0, 0);
nxt[tot] = head[y];
head[y] = tot++;
}
int s, t, L, R;
int cur[N];
int d[N];
bool bfs() {
memset(d + L, -1, sizeof(int) * (R - L + 1));
queue<int> q;
q.push(s);
d[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
cur[x] = head[x];
for (int i = head[x]; ~i; i = nxt[i]) {
edge &e = edges[i];
if (d[e.to] == -1 && e.flow < e.cap) {
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return d[t] != -1;
}
int dfs(int x, int a) {
if (x == t || a == 0)
return a;
int flow = 0, f = 0;
for (int &i = cur[x]; ~i; i = nxt[i]) {
edge &e = edges[i];
if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[i ^ 1].flow -= f;
a -= f;
flow += f;
if (!a)
break;
}
}
return flow;
}
inline int maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
while (bfs())
flow += dfs(s, INF);
return flow;
}
} dinic;
int n, m;
int a[110][110], b[110][110], c[110][110];
int val[110][110];
vector<int> vec[110][110];
inline int get(int x) { return (x - 1) / m + 1; }
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
vec[i][get(a[i][j])].push_back(a[i][j]);
for (int i = 1; i <= m; i++) {
dinic.init();
dinic.L = 0, dinic.R = 2 * n + 2;
int S = 2 * n + 1, T = 2 * n + 2;
for (int j = 1; j <= n; j++)
dinic.add_edge(S, j, 1);
for (int j = 1; j <= n; j++)
dinic.add_edge(j + n, T, 1);
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (!vec[j][k].empty())
dinic.add_edge(j, k + n, 1);
dinic.maxflow(S, T);
for (int j = 1; j <= n; j++)
for (int k = dinic.head[j]; ~k; k = dinic.nxt[k]) {
edge &e = dinic.edges[k];
if (e.to != S && e.to != T && e.to > n && e.cap == e.flow) {
b[j][i] = vec[j][e.to - n].back();
vec[j][e.to - n].pop_back();
}
}
}
for (int i = 1; i <= m; i++) {
static int tmp[110];
for (int j = 1; j <= n; j++)
tmp[j] = b[j][i];
sort(tmp + 1, tmp + 1 + n, [](int a, int b) { return get(a) < get(b); });
for (int j = 1; j <= n; j++)
c[j][i] = tmp[j];
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02942 | C++ | Runtime Error | /*
_|_| _| _| _|
_| _| _| _|_| _|_|_|_| _| _| _|
_| _| _|_| _| _| _|_|
_| _| _| _| _| _| _| _|
_|_| _| _|_|_|_| _|_| _| _|
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
// #define ls (rt<<1)
// #define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn =
205; // 1~n 为颜色点 n+1~2*n为行点 所有没到过的格子给行->颜色连一条边
// 跑完后,每个颜色新增的哪个行,标记打上,然后在B里放入这个。
const int inf = 1e9;
struct Flow {
int s, t, head[maxn], cur[maxn], cnt, q[maxn], rk[maxn];
struct gg {
int u, v, w, next;
} side[maxn * 2];
Flow() { s = maxn - 3, t = maxn - 2, cnt = 1; }
void reset() {
cnt = 1;
memset(head, 0, sizeof(head));
}
void ins(int u, int v) {
side[++cnt] = (gg){u, v, 1, head[u]};
head[u] = cnt;
side[++cnt] = (gg){v, u, 0, head[v]};
head[v] = cnt;
}
bool bfs() {
memset(rk, 0, sizeof(rk));
int hd = 1, tl = 1;
q[tl] = s;
rk[s] = 1;
while (hd <= tl) {
int u = q[hd++];
for (int i = head[u]; i; i = side[i].next) {
int v = side[i].v;
if (rk[v] || !side[i].w)
continue;
rk[v] = rk[u] + 1;
q[++tl] = v;
}
}
return rk[t];
}
int dfs(int u, int flow) {
if (u == t)
return flow;
int tot = 0;
for (int &i = cur[u]; i; i = side[i].next) {
int v = side[i].v;
if (rk[u] + 1 != rk[v] || !side[i].w)
continue;
int sent = dfs(v, min(flow, side[i].w));
flow -= sent;
tot += sent;
side[i].w -= sent;
side[i ^ 1].w += sent;
}
return tot;
}
void dinic() {
int maxflow = 0;
while (bfs()) {
memcpy(cur, head, sizeof(head));
maxflow += dfs(s, inf);
}
}
} f;
int A[105][105], B[105][105], C[105][105], col[105][105]; // col=属于哪一行
bool used[105][105];
int n, m;
int get_col(int num) {
int tmp = num % m;
if (!tmp)
tmp = m;
return (num - tmp) / m + 1;
}
int main() {
scanf("%d%d", &n, &m);
rep(i, 1, n) rep(j, 1, m) scanf("%d", &A[i][j]), col[i][j] = get_col(A[i][j]);
rep(floor, 1, m) {
f.reset();
rep(i, 1, n) f.ins(f.s, i), f.ins(i + n, f.t);
rep(i, 1, n) rep(j, 1, m) if (!used[i][j]) f.ins(col[i][j], i + n);
f.dinic();
rep(i, n + 1, 2 * n) { // 枚举每一行
for (int j = f.head[i]; j; j = f.side[j].next) {
int v = f.side[j].v;
if (v <= n && f.side[j].w) { // 颜色v->行i-n的匹配被点亮
rep(k, 1, m) { // 寻找这一行中符合这一颜色的
if (col[i - n][k] == v && !used[i - n][k]) {
used[i - n][k] = 1;
B[i - n][floor] = A[i - n][k]; // 填过来
break;
}
}
break;
}
}
}
}
// 现在B中每一列的任意一种颜色只有一个
rep(i, 1, n) rep(j, 1, m) C[get_col(B[i][j])][j] = B[i][j];
rep(i, 1, n) {
rep(j, 1, m) printf("%d ", B[i][j]);
printf("\n");
}
rep(i, 1, n) {
rep(j, 1, m) printf("%d ", C[i][j]);
printf("\n");
}
return 0;
} | /*
_|_| _| _| _|
_| _| _| _|_| _|_|_|_| _| _| _|
_| _| _|_| _| _| _|_|
_| _| _| _| _| _| _| _|
_|_| _| _|_|_|_| _|_| _| _|
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
// #define ls (rt<<1)
// #define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn =
105 * 105 +
2 * 105; // 1~n 为颜色点 n+1~2*n为行点 所有没到过的格子给行->颜色连一条边
// 跑完后,每个颜色新增的哪个行,标记打上,然后在B里放入这个。
const int inf = 1e9;
struct Flow {
int s, t, head[maxn], cur[maxn], cnt, q[maxn], rk[maxn];
struct gg {
int u, v, w, next;
} side[maxn * 2];
Flow() { s = maxn - 3, t = maxn - 2, cnt = 1; }
void reset() {
cnt = 1;
memset(head, 0, sizeof(head));
}
void ins(int u, int v) {
side[++cnt] = (gg){u, v, 1, head[u]};
head[u] = cnt;
side[++cnt] = (gg){v, u, 0, head[v]};
head[v] = cnt;
}
bool bfs() {
memset(rk, 0, sizeof(rk));
int hd = 1, tl = 1;
q[tl] = s;
rk[s] = 1;
while (hd <= tl) {
int u = q[hd++];
for (int i = head[u]; i; i = side[i].next) {
int v = side[i].v;
if (rk[v] || !side[i].w)
continue;
rk[v] = rk[u] + 1;
q[++tl] = v;
}
}
return rk[t];
}
int dfs(int u, int flow) {
if (u == t)
return flow;
int tot = 0;
for (int &i = cur[u]; i; i = side[i].next) {
int v = side[i].v;
if (rk[u] + 1 != rk[v] || !side[i].w)
continue;
int sent = dfs(v, min(flow, side[i].w));
flow -= sent;
tot += sent;
side[i].w -= sent;
side[i ^ 1].w += sent;
}
return tot;
}
void dinic() {
int maxflow = 0;
while (bfs()) {
memcpy(cur, head, sizeof(head));
maxflow += dfs(s, inf);
}
}
} f;
int A[105][105], B[105][105], C[105][105], col[105][105]; // col=属于哪一行
bool used[105][105];
int n, m;
int get_col(int num) {
int tmp = num % m;
if (!tmp)
tmp = m;
return (num - tmp) / m + 1;
}
int main() {
scanf("%d%d", &n, &m);
rep(i, 1, n) rep(j, 1, m) scanf("%d", &A[i][j]), col[i][j] = get_col(A[i][j]);
rep(floor, 1, m) {
f.reset();
rep(i, 1, n) f.ins(f.s, i), f.ins(i + n, f.t);
rep(i, 1, n) rep(j, 1, m) if (!used[i][j]) f.ins(col[i][j], i + n);
f.dinic();
rep(i, n + 1, 2 * n) { // 枚举每一行
for (int j = f.head[i]; j; j = f.side[j].next) {
int v = f.side[j].v;
if (v <= n && f.side[j].w) { // 颜色v->行i-n的匹配被点亮
rep(k, 1, m) { // 寻找这一行中符合这一颜色的
if (col[i - n][k] == v && !used[i - n][k]) {
used[i - n][k] = 1;
B[i - n][floor] = A[i - n][k]; // 填过来
break;
}
}
break;
}
}
}
}
// 现在B中每一列的任意一种颜色只有一个
rep(i, 1, n) rep(j, 1, m) C[get_col(B[i][j])][j] = B[i][j];
rep(i, 1, n) {
rep(j, 1, m) printf("%d ", B[i][j]);
printf("\n");
}
rep(i, 1, n) {
rep(j, 1, m) printf("%d ", C[i][j]);
printf("\n");
}
return 0;
} | replace | 29 | 31 | 29 | 32 | 0 | |
p02942 | C++ | Runtime Error | // Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define ll long long
#define inf 20021225
#define N 210
#define M 20001
using namespace std;
int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return f * s;
}
struct edge {
int to, lt, f;
} e[M];
int in[N], cnt, s, t, n, m; // clear!
void add(int x, int y, int f) {
e[++cnt].to = y;
e[cnt].lt = in[x];
in[x] = cnt;
e[cnt].f = f;
e[++cnt].to = x;
e[cnt].lt = in[y];
in[y] = cnt;
e[cnt].f = 0;
}
queue<int> que;
int dep[N];
bool bfs() {
// printf("GG");
while (!que.empty())
que.pop();
memset(dep, 0, sizeof(dep));
que.push(s);
dep[s] = 1;
while (!que.empty()) {
int x = que.front();
que.pop();
for (int i = in[x]; i; i = e[i].lt)
if (!dep[e[i].to] && e[i].f) {
dep[e[i].to] = dep[x] + 1, que.push(e[i].to);
if (e[i].to == t)
return 1;
}
}
return 0;
}
int dfs(int x, int flow) {
if (x == t)
return flow;
int cur = flow;
for (int i = in[x]; i; i = e[i].lt) {
int y = e[i].to;
if (dep[y] == dep[x] + 1 && e[i].f) {
int tmp = dfs(y, min(cur, e[i].f)); // printf("GG");
cur -= tmp;
e[i].f -= tmp;
e[i ^ 1].f += tmp;
if (!cur)
return flow;
}
}
dep[x] = -1;
return flow - cur;
}
vector<int> val[N][N];
int sz[N][N];
void link() {
cnt = 1;
s = N - 3;
t = s + 1;
memset(in, 0, sizeof(in));
for (int i = 1; i <= n; i++) {
add(s, i, 1);
for (int j = 1; j <= n; j++)
if (sz[i][j] < val[i][j].size())
add(i, n + j, 1);
}
for (int i = 1; i <= n; i++)
add(n + i, t, 1);
}
int r[N], b[N][N], a[N][N];
void dinic() {
// printf("QAQ");
while (bfs())
dfs(s, inf);
// printf("QwQ");
for (int i = 1; i <= n; i++)
for (int j = in[i]; j; j = e[j].lt) {
if (e[j].to != s && e[j ^ 1].f) {
r[i] = e[j].to - n; // printf("%d %d\n",i,r[i]);
break;
}
}
// printf("QAQ");
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = read();
int id = (a[i][j] - 1) / m + 1;
val[i][id].push_back(a[i][j]);
// printf("%d %d\n",i,id);
}
// printf("OVO");
for (int i = 1; i <= m; i++) {
link();
dinic(); // printf("GG");
for (int j = 1; j <= n; j++)
b[j][i] = val[j][r[j]][sz[j][r[j]]++];
// printf("QAQ");
}
for (int i = 1; i <= n; i++, printf("\n"))
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]), a[j][i] = b[i][j];
for (int i = 1; i <= m; i++)
sort(a[i] + 1, a[i] + n + 1);
for (int i = 1; i <= n; i++, printf("\n"))
for (int j = 1; j <= m; j++)
printf("%d ", a[j][i]);
return 0;
} | // Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define ll long long
#define inf 20021225
#define N 410
#define M 40001
using namespace std;
int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return f * s;
}
struct edge {
int to, lt, f;
} e[M];
int in[N], cnt, s, t, n, m; // clear!
void add(int x, int y, int f) {
e[++cnt].to = y;
e[cnt].lt = in[x];
in[x] = cnt;
e[cnt].f = f;
e[++cnt].to = x;
e[cnt].lt = in[y];
in[y] = cnt;
e[cnt].f = 0;
}
queue<int> que;
int dep[N];
bool bfs() {
// printf("GG");
while (!que.empty())
que.pop();
memset(dep, 0, sizeof(dep));
que.push(s);
dep[s] = 1;
while (!que.empty()) {
int x = que.front();
que.pop();
for (int i = in[x]; i; i = e[i].lt)
if (!dep[e[i].to] && e[i].f) {
dep[e[i].to] = dep[x] + 1, que.push(e[i].to);
if (e[i].to == t)
return 1;
}
}
return 0;
}
int dfs(int x, int flow) {
if (x == t)
return flow;
int cur = flow;
for (int i = in[x]; i; i = e[i].lt) {
int y = e[i].to;
if (dep[y] == dep[x] + 1 && e[i].f) {
int tmp = dfs(y, min(cur, e[i].f)); // printf("GG");
cur -= tmp;
e[i].f -= tmp;
e[i ^ 1].f += tmp;
if (!cur)
return flow;
}
}
dep[x] = -1;
return flow - cur;
}
vector<int> val[N][N];
int sz[N][N];
void link() {
cnt = 1;
s = N - 3;
t = s + 1;
memset(in, 0, sizeof(in));
for (int i = 1; i <= n; i++) {
add(s, i, 1);
for (int j = 1; j <= n; j++)
if (sz[i][j] < val[i][j].size())
add(i, n + j, 1);
}
for (int i = 1; i <= n; i++)
add(n + i, t, 1);
}
int r[N], b[N][N], a[N][N];
void dinic() {
// printf("QAQ");
while (bfs())
dfs(s, inf);
// printf("QwQ");
for (int i = 1; i <= n; i++)
for (int j = in[i]; j; j = e[j].lt) {
if (e[j].to != s && e[j ^ 1].f) {
r[i] = e[j].to - n; // printf("%d %d\n",i,r[i]);
break;
}
}
// printf("QAQ");
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = read();
int id = (a[i][j] - 1) / m + 1;
val[i][id].push_back(a[i][j]);
// printf("%d %d\n",i,id);
}
// printf("OVO");
for (int i = 1; i <= m; i++) {
link();
dinic(); // printf("GG");
for (int j = 1; j <= n; j++)
b[j][i] = val[j][r[j]][sz[j][r[j]]++];
// printf("QAQ");
}
for (int i = 1; i <= n; i++, printf("\n"))
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]), a[j][i] = b[i][j];
for (int i = 1; i <= m; i++)
sort(a[i] + 1, a[i] + n + 1);
for (int i = 1; i <= n; i++, printf("\n"))
for (int j = 1; j <= m; j++)
printf("%d ", a[j][i]);
return 0;
} | replace | 9 | 11 | 9 | 11 | 0 | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, u) for (int i = start[u]; i; i = e[i].next)
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
#define SZ(A) ((int)A.size())
template <class T> inline void chkmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> inline void chkmax(T &a, T b) {
if (a < b)
a = b;
}
inline void myassert(bool a, string s) {
if (!a)
cerr << s << endl, exit(0);
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-')
ch = getchar();
if (ch == '-')
ch = getchar(), f = -1;
while (isdigit(ch))
s = s * 10 + ch - '0', ch = getchar();
return ~f ? s : -s;
}
const int maxn = 120;
int n, m, col[maxn][maxn];
vector<int> rest[maxn][maxn];
inline void init() {
n = read();
m = read();
REP(i, 1, n) REP(j, 1, m) {
int x = read();
col[i][j] = (x + m - 1) / m;
rest[i][col[i][j]].push_back(x);
}
}
int f[maxn][maxn];
int mch[maxn], vis[maxn];
inline bool dfs(int u) {
REP(v, 1, n) if (f[u][v] && !vis[v]) {
vis[v] = 1;
if (!mch[v] || dfs(v)) {
mch[v] = u;
return 1;
}
}
return 0;
}
int B[maxn][maxn];
int C[maxn][maxn];
inline void doing() {
REP(k, 1, m) {
REP(i, 1, n) REP(j, 1, n) f[i][j] = !rest[i][j].empty();
memset(mch, 0, sizeof(int) * (n + 1));
REP(i, 1, n) {
memset(vis, 0, sizeof(int) * (n + 1));
dfs(i);
}
REP(i, 1, n) {
int x = mch[i], y = i;
B[x][k] = rest[x][y].back();
rest[x][y].pop_back();
C[y][k] = B[x][k];
}
}
REP(i, 1, n) REP(j, 1, m) printf("%d%c", B[i][j], j == m ? '\n' : ' ');
REP(i, 1, n) REP(j, 1, m) printf("%d%c", C[i][j], j == m ? '\n' : ' ');
}
int main() {
init();
doing();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, u) for (int i = start[u]; i; i = e[i].next)
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
#define SZ(A) ((int)A.size())
template <class T> inline void chkmin(T &a, T b) {
if (a > b)
a = b;
}
template <class T> inline void chkmax(T &a, T b) {
if (a < b)
a = b;
}
inline void myassert(bool a, string s) {
if (!a)
cerr << s << endl, exit(0);
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-')
ch = getchar();
if (ch == '-')
ch = getchar(), f = -1;
while (isdigit(ch))
s = s * 10 + ch - '0', ch = getchar();
return ~f ? s : -s;
}
const int maxn = 120;
int n, m, col[maxn][maxn];
vector<int> rest[maxn][maxn];
inline void init() {
n = read();
m = read();
REP(i, 1, n) REP(j, 1, m) {
int x = read();
col[i][j] = (x + m - 1) / m;
rest[i][col[i][j]].push_back(x);
}
}
int f[maxn][maxn];
int mch[maxn], vis[maxn];
inline bool dfs(int u) {
REP(v, 1, n) if (f[u][v] && !vis[v]) {
vis[v] = 1;
if (!mch[v] || dfs(mch[v])) {
mch[v] = u;
return 1;
}
}
return 0;
}
int B[maxn][maxn];
int C[maxn][maxn];
inline void doing() {
REP(k, 1, m) {
REP(i, 1, n) REP(j, 1, n) f[i][j] = !rest[i][j].empty();
memset(mch, 0, sizeof(int) * (n + 1));
REP(i, 1, n) {
memset(vis, 0, sizeof(int) * (n + 1));
dfs(i);
}
REP(i, 1, n) {
int x = mch[i], y = i;
B[x][k] = rest[x][y].back();
rest[x][y].pop_back();
C[y][k] = B[x][k];
}
}
REP(i, 1, n) REP(j, 1, m) printf("%d%c", B[i][j], j == m ? '\n' : ' ');
REP(i, 1, n) REP(j, 1, m) printf("%d%c", C[i][j], j == m ? '\n' : ' ');
}
int main() {
init();
doing();
return 0;
}
| replace | 56 | 57 | 56 | 57 | 0 | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define N 210
#define M 200010
#define inf 2147483647
#define col(x) (((x - 1) / m) + 1)
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - '0', ch = getchar();
return x;
}
int tot = 1;
int fir[N], nex[M], got[M], tak[M];
inline void AddEdge(int x, int y, int z) {
nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z;
}
int a[N][N], b[N][N];
vector<int> v[N][N];
int dep[N], que[N];
inline int bfs(int s, int t) {
memset(dep, 0, sizeof(dep));
int l = 1, r = 1;
dep[s] = 1, que[1] = s;
while (l <= r) {
int x = que[l++];
for (int i = fir[x]; i; i = nex[i]) {
int y = got[i];
if (dep[y] || !tak[i])
continue;
dep[y] = dep[x] + 1, que[++r] = y;
}
}
return dep[t];
}
inline int dfs(int x, int t, int flow) {
int rest = flow;
if (x == t)
return flow;
for (int i = fir[x]; i && rest; i = nex[i]) {
int y = got[i];
if (!tak[i] || dep[y] != dep[x] + 1)
continue;
int k = dfs(y, t, min(rest, tak[i]));
if (!k)
dep[y] = 0;
tak[i] -= k, tak[i ^ 1] += k, rest -= k;
}
return flow - rest;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = read();
v[i][col(a[i][j])].push_back(a[i][j]);
}
for (int w = 1; w <= m; w++) {
memset(fir, 0, sizeof(fir));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (v[i][j].size()) {
AddEdge(i, j + n, 1), AddEdge(j + n, i, 0);
}
int s = 2 * n + 1, t = s + 1;
for (int i = 1; i <= n; i++)
AddEdge(s, i, 1), AddEdge(i, s, 0), AddEdge(t, i + n, 0),
AddEdge(i + n, t, 1);
while (bfs(s, t))
dfs(s, t, inf);
for (int i = 1; i <= n; i++) {
for (int j = fir[i]; j; j = nex[j]) {
if (got[j] > n && !tak[j])
b[i][w] = v[i][got[j] - n].back(), v[i][got[j] - n].pop_back();
}
}
}
for (int i = 1; i <= n; i++, putchar('\n'))
for (int j = 1; j <= m; j++)
printf("%d ", a[col(b[i][j])][j] = b[i][j]);
for (int i = 1; i <= n; i++, putchar('\n'))
for (int j = 1; j <= m; j++)
printf("%d ", a[i][j]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define N 210
#define M 200010
#define inf 2147483647
#define col(x) (((x - 1) / m) + 1)
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch))
ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - '0', ch = getchar();
return x;
}
int tot = 1;
int fir[N], nex[M], got[M], tak[M];
inline void AddEdge(int x, int y, int z) {
nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z;
}
int a[N][N], b[N][N];
vector<int> v[N][N];
int dep[N], que[N];
inline int bfs(int s, int t) {
memset(dep, 0, sizeof(dep));
int l = 1, r = 1;
dep[s] = 1, que[1] = s;
while (l <= r) {
int x = que[l++];
for (int i = fir[x]; i; i = nex[i]) {
int y = got[i];
if (dep[y] || !tak[i])
continue;
dep[y] = dep[x] + 1, que[++r] = y;
}
}
return dep[t];
}
inline int dfs(int x, int t, int flow) {
int rest = flow;
if (x == t)
return flow;
for (int i = fir[x]; i && rest; i = nex[i]) {
int y = got[i];
if (!tak[i] || dep[y] != dep[x] + 1)
continue;
int k = dfs(y, t, min(rest, tak[i]));
if (!k)
dep[y] = 0;
tak[i] -= k, tak[i ^ 1] += k, rest -= k;
}
return flow - rest;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = read();
v[i][col(a[i][j])].push_back(a[i][j]);
}
for (int w = 1; w <= m; w++) {
tot = 1;
memset(fir, 0, sizeof(fir));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (v[i][j].size()) {
AddEdge(i, j + n, 1), AddEdge(j + n, i, 0);
}
int s = 2 * n + 1, t = s + 1;
for (int i = 1; i <= n; i++)
AddEdge(s, i, 1), AddEdge(i, s, 0), AddEdge(t, i + n, 0),
AddEdge(i + n, t, 1);
while (bfs(s, t))
dfs(s, t, inf);
for (int i = 1; i <= n; i++) {
for (int j = fir[i]; j; j = nex[j]) {
if (got[j] > n && !tak[j])
b[i][w] = v[i][got[j] - n].back(), v[i][got[j] - n].pop_back();
}
}
}
for (int i = 1; i <= n; i++, putchar('\n'))
for (int j = 1; j <= m; j++)
printf("%d ", a[col(b[i][j])][j] = b[i][j]);
for (int i = 1; i <= n; i++, putchar('\n'))
for (int j = 1; j <= m; j++)
printf("%d ", a[i][j]);
return 0;
} | insert | 61 | 61 | 61 | 62 | 0 | |
p02942 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxi = 1e6 + 10;
int a[110][110], b[110][110];
string s;
int n, m;
vector<int> v[maxi];
set<pair<int, int>> st;
int c[110][110];
int cnt[110][110];
int calc(int x) {
int val = x / m;
if (x % m != 0)
val++;
return val;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
b[i][j] = a[i][j];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int val = calc(b[i][j]);
cnt[j][val]++;
}
int nula = 0;
while (true) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
for (int k = j + 1; k <= m; k++)
if ((cnt[j][calc(b[i][j])] > 1 || cnt[k][calc(b[i][k])] > 1) &&
cnt[j][calc(b[i][j])] + cnt[k][calc(b[i][k])] >=
cnt[j][calc(b[i][k])] + cnt[k][calc(b[i][j])]) {
// cout<<"USAO :"<<i<<" "<<j<<" "<<k<<endl;
cnt[j][calc(b[i][j])]--;
cnt[k][calc(b[i][k])]--;
cnt[j][calc(b[i][k])]++;
cnt[k][calc(b[i][j])]++;
swap(b[i][j], b[i][k]);
nula++;
}
}
if (!nula)
break;
nula = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int val = calc(b[i][j]);
c[val][j] = b[i][j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
printf("\n");
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int maxi = 1e6 + 10;
int a[110][110], b[110][110];
string s;
int n, m;
vector<int> v[maxi];
set<pair<int, int>> st;
int c[110][110];
int cnt[110][110];
int calc(int x) {
int val = x / m;
if (x % m != 0)
val++;
return val;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
b[i][j] = a[i][j];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int val = calc(b[i][j]);
cnt[j][val]++;
}
int nula = 0;
while (true) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
for (int k = j + 1; k <= m; k++)
if (cnt[j][calc(b[i][j])] > 1 && cnt[k][calc(b[i][j])] == 0) {
// cout<<"USAO :"<<i<<" "<<j<<" "<<k<<endl;
cnt[j][calc(b[i][j])]--;
cnt[k][calc(b[i][k])]--;
cnt[j][calc(b[i][k])]++;
cnt[k][calc(b[i][j])]++;
swap(b[i][j], b[i][k]);
nula++;
}
}
if (!nula)
break;
nula = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int val = calc(b[i][j]);
c[val][j] = b[i][j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", b[i][j]);
printf("\n");
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
| replace | 40 | 43 | 40 | 41 | TLE | |
p02942 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 998244353;
// 1000000007;
template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
i >> A.F >> A.S;
return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
for (auto &I : A) {
i >> I;
}
return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
o << A.F << " " << A.S;
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
ll i = A.size();
for (auto &I : A) {
o << I << (--i ? " " : "");
}
return o;
}
template <typename T> vector<T> &cset(vector<T> &A, T e = T()) {
for (auto &I : A) {
I = e;
}
return A;
}
using Int = long long;
// BEGIN CUT HERE
template <typename T, bool directed> struct Dinic {
struct edge {
int to;
T cap;
int rev;
edge() {}
edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
};
vector<vector<edge>> G;
vector<int> level, iter;
Dinic() {}
Dinic(int n) : G(n), level(n), iter(n) {}
int add_edge(int from, int to, T cap) {
G[from].emplace_back(to, cap, G[to].size());
G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
return G[to].back().rev;
}
void bfs(int s) {
fill(level.begin(), level.end(), -1);
queue<int> que;
level[s] = 0;
que.emplace(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.emplace(e.to);
}
}
}
}
T dfs(int v, int t, T f) {
if (v == t)
return f;
for (int &i = iter[v]; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[v] < level[e.to]) {
T d = dfs(e.to, t, min(f, e.cap));
if (d == 0)
continue;
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
return 0;
}
T flow(int s, int t, T lim) {
T fl = 0;
while (1) {
bfs(s);
if (level[t] < 0 || lim == 0)
break;
fill(iter.begin(), iter.end(), 0);
while (1) {
T f = dfs(s, t, lim);
if (f == 0)
break;
fl += f;
lim -= f;
}
}
return fl;
}
T flow(int s, int t) { return flow(s, t, numeric_limits<T>::max() / 2); }
T cut(int s, int t, int x, int a) {
static_assert(directed, "must be directed");
auto &e = G[x][a];
int y = e.to;
T ce = e.cap, cr = G[y][e.rev].cap;
if (cr == 0)
return 0;
e.cap = G[y][e.rev].cap = 0;
T cap = cr - flow(x, y, cr);
if (x != s && cap != 0)
flow(x, s, cap);
if (t != y && cap != 0)
flow(t, y, cap);
e.cap = ce + cr;
return cap;
}
};
// END CUT HERE
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> A(N, vector<int>(M));
vector<vector<int>> one(N, vector<int>(M));
vector<vector<int>> cnt(N, vector<int>(N, 0));
cin >> A;
for (auto &I : A) {
for (auto &J : I) {
J--;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cnt[i][A[i][j] / M]++;
}
}
for (int i = 0; i < M; i++) {
Dinic<int, true> fl(2 + N + N);
int s = 2 * N;
int g = s + 1;
int c = 0;
int m = N;
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
if (cnt[j][k]) {
fl.add_edge(c + j, m + k, 1);
}
}
fl.add_edge(s, c + j, 1);
fl.add_edge(m + j, g, 1);
}
fl.flow(s, g);
for (int j = 0; j < N; i++) {
for (auto &e : fl.G[c + j]) {
if (e.cap == 0 && e.to != s) {
one[j][i] = e.to - m;
cnt[j][e.to - m]--;
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < M; k++) {
if (A[i][k] != -1 && A[i][k] / M == one[i][j]) {
one[i][j] = A[i][k];
A[i][k] = -1;
break;
}
}
}
}
for (auto &I : one) {
for (auto &J : I) {
J++;
}
}
for (auto &I : one) {
cout << I << endl;
}
for (int j = 0; j < M; j++) {
vector<ll> X(N);
for (int i = 0; i < N; i++) {
X[i] = one[i][j];
}
sort(X.begin(), X.end());
for (int i = 0; i < N; i++) {
one[i][j] = X[i];
}
}
for (auto &I : one) {
cout << I << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 998244353;
// 1000000007;
template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
i >> A.F >> A.S;
return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
for (auto &I : A) {
i >> I;
}
return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
o << A.F << " " << A.S;
return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
ll i = A.size();
for (auto &I : A) {
o << I << (--i ? " " : "");
}
return o;
}
template <typename T> vector<T> &cset(vector<T> &A, T e = T()) {
for (auto &I : A) {
I = e;
}
return A;
}
using Int = long long;
// BEGIN CUT HERE
template <typename T, bool directed> struct Dinic {
struct edge {
int to;
T cap;
int rev;
edge() {}
edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
};
vector<vector<edge>> G;
vector<int> level, iter;
Dinic() {}
Dinic(int n) : G(n), level(n), iter(n) {}
int add_edge(int from, int to, T cap) {
G[from].emplace_back(to, cap, G[to].size());
G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
return G[to].back().rev;
}
void bfs(int s) {
fill(level.begin(), level.end(), -1);
queue<int> que;
level[s] = 0;
que.emplace(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = 0; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.emplace(e.to);
}
}
}
}
T dfs(int v, int t, T f) {
if (v == t)
return f;
for (int &i = iter[v]; i < (int)G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > 0 && level[v] < level[e.to]) {
T d = dfs(e.to, t, min(f, e.cap));
if (d == 0)
continue;
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
return 0;
}
T flow(int s, int t, T lim) {
T fl = 0;
while (1) {
bfs(s);
if (level[t] < 0 || lim == 0)
break;
fill(iter.begin(), iter.end(), 0);
while (1) {
T f = dfs(s, t, lim);
if (f == 0)
break;
fl += f;
lim -= f;
}
}
return fl;
}
T flow(int s, int t) { return flow(s, t, numeric_limits<T>::max() / 2); }
T cut(int s, int t, int x, int a) {
static_assert(directed, "must be directed");
auto &e = G[x][a];
int y = e.to;
T ce = e.cap, cr = G[y][e.rev].cap;
if (cr == 0)
return 0;
e.cap = G[y][e.rev].cap = 0;
T cap = cr - flow(x, y, cr);
if (x != s && cap != 0)
flow(x, s, cap);
if (t != y && cap != 0)
flow(t, y, cap);
e.cap = ce + cr;
return cap;
}
};
// END CUT HERE
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> A(N, vector<int>(M));
vector<vector<int>> one(N, vector<int>(M));
vector<vector<int>> cnt(N, vector<int>(N, 0));
cin >> A;
for (auto &I : A) {
for (auto &J : I) {
J--;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cnt[i][A[i][j] / M]++;
}
}
for (int i = 0; i < M; i++) {
Dinic<int, true> fl(2 + N + N);
int s = 2 * N;
int g = s + 1;
int c = 0;
int m = N;
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
if (cnt[j][k]) {
fl.add_edge(c + j, m + k, 1);
}
}
fl.add_edge(s, c + j, 1);
fl.add_edge(m + j, g, 1);
}
assert(fl.flow(s, g) == N);
for (int j = 0; j < N; j++) {
for (auto &e : fl.G[c + j]) {
if (e.cap == 0 && e.to != s) {
one[j][i] = e.to - m;
cnt[j][e.to - m]--;
}
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < M; k++) {
if (A[i][k] != -1 && A[i][k] / M == one[i][j]) {
one[i][j] = A[i][k];
A[i][k] = -1;
break;
}
}
}
}
for (auto &I : one) {
for (auto &J : I) {
J++;
}
}
for (auto &I : one) {
cout << I << endl;
}
for (int j = 0; j < M; j++) {
vector<ll> X(N);
for (int i = 0; i < N; i++) {
X[i] = one[i][j];
}
sort(X.begin(), X.end());
for (int i = 0; i < N; i++) {
one[i][j] = X[i];
}
}
for (auto &I : one) {
cout << I << endl;
}
return 0;
}
| replace | 173 | 175 | 173 | 175 | -11 | |
p02942 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define PP pair<int, pair<int, int>>
const int MAX = 510000;
const int MOD = 998244353;
const int INV2 = (MOD + 1) / 2;
signed main() {
int n, m;
cin >> n >> m;
int a[n][m];
pair<int, int> ichi[n * m + 1];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
ichi[a[i][j]] = make_pair(i, j);
}
int ten[n][m];
for (int i = 1; i <= n * m; i++) {
ten[(i - 1) / m][(i - 1) % m] = ichi[i].first;
int i0 = a[ichi[i].first][(i - 1) % m], j = ichi[i].second,
i1 = ichi[i].first;
if (j == (i - 1) % m)
continue;
swap(a[ichi[i].first][(i - 1) % m], a[ichi[i].first][ichi[i].second]);
while ((i0 - 1) / m < (i - 1) / m) {
swap(a[ten[(i0 - 1) / m][j]][j], a[ten[(i0 - 1) / m][j]][(i - 1) % m]);
i1 = ten[(i0 - 1) / m][j];
i0 = a[ten[(i0 - 1) / m][j]][j];
}
// cerr<<i0<<' '<<i1<<endl;
ichi[i0] = make_pair(i1, j);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << a[i][j] << ' ';
cout << endl;
}
int b[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
b[j][i] = a[i][j];
for (int i = 0; i < m; i++)
sort(b[i], b[i] + n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << b[j][i] << ' ';
cout << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define PP pair<int, pair<int, int>>
const int MAX = 510000;
const int MOD = 998244353;
const int INV2 = (MOD + 1) / 2;
signed main() {
int n, m;
cin >> n >> m;
int a[n][m];
pair<int, int> ichi[n * m + 1];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
ichi[a[i][j]] = make_pair(i, j);
}
int ten[n][m];
for (int i = 1; i <= n * m; i++) {
ten[(i - 1) / m][(i - 1) % m] = ichi[i].first;
int i0 = a[ichi[i].first][(i - 1) % m], j = ichi[i].second,
i1 = ichi[i].first;
if (j == (i - 1) % m)
continue;
swap(a[ichi[i].first][(i - 1) % m], a[ichi[i].first][ichi[i].second]);
while ((i0 - 1) / m < (i - 1) / m) {
swap(a[ten[(i0 - 1) / m][j]][j], a[ten[(i0 - 1) / m][j]][(i - 1) % m]);
i1 = ten[(i0 - 1) / m][j];
swap(ten[(i0 - 1) / m][j], ten[(i0 - 1) / m][(i - 1) % m]);
i0 = a[ten[(i0 - 1) / m][(i - 1) % m]][j];
}
// cerr<<i0<<' '<<i1<<endl;
ichi[i0] = make_pair(i1, j);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << a[i][j] << ' ';
cout << endl;
}
int b[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
b[j][i] = a[i][j];
for (int i = 0; i < m; i++)
sort(b[i], b[i] + n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << b[j][i] << ' ';
cout << endl;
}
}
| replace | 29 | 30 | 29 | 32 | TLE | |
p02942 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define f(a, b, c) for (int a = (b); a <= (c); ++a)
const int maxn = 2e4 + 1;
int he[maxn], ne[maxn], to[maxn], w[maxn], cnt = 1;
inline void addedge(int x, int y, int z) {
ne[++cnt] = he[x], he[x] = cnt, to[cnt] = y, w[cnt] = z;
ne[++cnt] = he[y], he[y] = cnt, to[cnt] = x, w[cnt] = 0;
}
int S, T;
int dis[maxn], inq[maxn];
int bfs() {
f(i, 1, T) inq[i] = dis[i] = 0;
queue<int> q;
q.push(S), dis[S] = 1;
while (q.size()) {
int x = q.front();
q.pop();
inq[x] = 0;
for (int i = he[x]; i; i = ne[i]) {
if (w[i] && !dis[to[i]]) {
dis[to[i]] = dis[x] + 1;
if (!inq[to[i]]) {
inq[to[i]] = 1;
q.push(to[i]);
}
}
}
}
return dis[T];
}
int cur[maxn];
int dfs(int x, int f) {
if (f == 0 || x == T)
return f;
int ans = 0;
for (int i = cur[x]; i && ans < f; i = ne[i]) {
cur[x] = i;
if (dis[x] + 1 == dis[to[i]] && w[i]) {
int qwq = dfs(to[i], min(f - ans, w[i]));
ans += qwq;
w[i] -= qwq;
w[i ^ 1] += qwq;
}
}
return ans;
}
int dinic() {
int ans = 0;
while (bfs()) {
f(i, 1, T) cur[i] = he[i];
ans += dfs(S, 1e9);
}
return ans;
}
vector<int> c[101][101];
int n, m;
int a[101][101];
int e[101][101];
int b[101][101];
void clear() {
cnt = 1;
f(i, 1, T) he[i] = 0;
}
int qwq[101];
int main() {
scanf("%d%d", &n, &m);
f(i, 1, n) f(j, 1, m) scanf("%d", &a[i][j]),
c[i][(a[i][j] - 1) / m + 1].push_back(a[i][j]);
S = n + n + 1, T = S + 1;
f(k, 1, m) {
// cout<<k<<":\n";
f(i, 1, n) f(j, 1, n) {
e[i][j] = -1;
if (c[i][j].size()) {
addedge(i, n + j, 1);
e[i][j] = cnt - 1;
// cout<<i<<" "<<j<<"\n";
}
}
f(i, 1, n) {
addedge(S, i, 1);
addedge(i + n, T, 1);
}
dinic();
f(i, 1, n) f(j, 1, n) {
if (e[i][j] >= 0 && w[e[i][j]] == 0) {
b[i][k] = c[i][j][c[i][j].size() - 1];
c[i][j].pop_back();
break;
}
}
clear();
}
f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
f(j, 1, m) {
f(i, 1, n) qwq[i] = b[i][j];
sort(qwq + 1, qwq + n + 1);
f(i, 1, n) b[i][j] = qwq[i];
}
f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define f(a, b, c) for (int a = (b); a <= (c); ++a)
const int maxn = 3e4 + 2;
int he[maxn], ne[maxn], to[maxn], w[maxn], cnt = 1;
inline void addedge(int x, int y, int z) {
ne[++cnt] = he[x], he[x] = cnt, to[cnt] = y, w[cnt] = z;
ne[++cnt] = he[y], he[y] = cnt, to[cnt] = x, w[cnt] = 0;
}
int S, T;
int dis[maxn], inq[maxn];
int bfs() {
f(i, 1, T) inq[i] = dis[i] = 0;
queue<int> q;
q.push(S), dis[S] = 1;
while (q.size()) {
int x = q.front();
q.pop();
inq[x] = 0;
for (int i = he[x]; i; i = ne[i]) {
if (w[i] && !dis[to[i]]) {
dis[to[i]] = dis[x] + 1;
if (!inq[to[i]]) {
inq[to[i]] = 1;
q.push(to[i]);
}
}
}
}
return dis[T];
}
int cur[maxn];
int dfs(int x, int f) {
if (f == 0 || x == T)
return f;
int ans = 0;
for (int i = cur[x]; i && ans < f; i = ne[i]) {
cur[x] = i;
if (dis[x] + 1 == dis[to[i]] && w[i]) {
int qwq = dfs(to[i], min(f - ans, w[i]));
ans += qwq;
w[i] -= qwq;
w[i ^ 1] += qwq;
}
}
return ans;
}
int dinic() {
int ans = 0;
while (bfs()) {
f(i, 1, T) cur[i] = he[i];
ans += dfs(S, 1e9);
}
return ans;
}
vector<int> c[101][101];
int n, m;
int a[101][101];
int e[101][101];
int b[101][101];
void clear() {
cnt = 1;
f(i, 1, T) he[i] = 0;
}
int qwq[101];
int main() {
scanf("%d%d", &n, &m);
f(i, 1, n) f(j, 1, m) scanf("%d", &a[i][j]),
c[i][(a[i][j] - 1) / m + 1].push_back(a[i][j]);
S = n + n + 1, T = S + 1;
f(k, 1, m) {
// cout<<k<<":\n";
f(i, 1, n) f(j, 1, n) {
e[i][j] = -1;
if (c[i][j].size()) {
addedge(i, n + j, 1);
e[i][j] = cnt - 1;
// cout<<i<<" "<<j<<"\n";
}
}
f(i, 1, n) {
addedge(S, i, 1);
addedge(i + n, T, 1);
}
dinic();
f(i, 1, n) f(j, 1, n) {
if (e[i][j] >= 0 && w[e[i][j]] == 0) {
b[i][k] = c[i][j][c[i][j].size() - 1];
c[i][j].pop_back();
break;
}
}
clear();
}
f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
f(j, 1, m) {
f(i, 1, n) qwq[i] = b[i][j];
sort(qwq + 1, qwq + n + 1);
f(i, 1, n) b[i][j] = qwq[i];
}
f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
return 0;
} | replace | 3 | 4 | 3 | 4 | TLE | |
p02942 | C++ | Time Limit Exceeded | #include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 210
#define MM 20100
int h[MN], nxt[MM], to[MM], cap[MM], K = 1;
int q[MN], hh = 0, tt = 0;
int iter[MN], d[MN];
int A[MN][MN], B[MN][MN], C[MN][MN];
std::vector<int> cnt[MN][MN];
void ins(int u, int v, int c) {
nxt[++K] = h[u];
h[u] = K;
to[K] = v;
cap[K] = c;
}
void insw(int u, int v, int c) {
ins(u, v, c);
ins(v, u, 0);
}
bool bfs(int S, int T) {
memcpy(iter, h, sizeof(h));
memset(d, 0, sizeof(d));
d[S] = 1;
hh = tt = 0;
q[tt++] = S;
while (hh < tt) {
int u = q[hh++];
for (int i = h[u]; i; i = nxt[i])
if (cap[i] && !d[to[i]]) {
d[to[i]] = d[u] + 1;
q[tt++] = to[i];
}
}
return d[T];
}
int dfs(int u, int T, int f) {
if (u == T)
return f;
int used = 0;
for (int &i = iter[u]; i; i = nxt[i]) {
if (cap[i] && d[to[i]] == d[u] + 1) {
int w = dfs(to[i], T, std::min(cap[i], f - used));
if (w) {
cap[i] -= w;
cap[i ^ 1] += w;
used += w;
if (f == used)
return f;
}
}
}
return used;
}
int dinic(int S, int T) {
int w, f = 0;
while (bfs(S, T))
while (w = dfs(S, T, 1e9))
f += w;
return f;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &A[i][j]);
cnt[i][(A[i][j] - 1) / m + 1].push_back(A[i][j]);
}
int S = 2 * n + 1, T = 2 * n + 2;
for (int i = 1; i <= m; i++) {
memset(h, 0, sizeof(h));
K = 1;
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (cnt[j][k].size())
insw(j, n + k, 1);
for (int j = 1; j <= n; j++)
insw(S, j, 1), insw(j + n, T, 1);
dinic(S, T);
for (int j = 1; j <= n; j++)
for (int x = h[j]; x; x = nxt[x])
if (!cap[x] && n + 1 <= to[x] && to[x] <= 2 * n)
B[j][i] = cnt[j][to[x] - n].back(), cnt[j][to[x] - n].pop_back();
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++) {
C[(B[i][j] - 1) / m + 1][j] = B[i][j];
printf("%d ", B[i][j]);
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", C[i][j]);
} | #include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 210
#define MM 21000
int h[MN], nxt[MM], to[MM], cap[MM], K = 1;
int q[MN], hh = 0, tt = 0;
int iter[MN], d[MN];
int A[MN][MN], B[MN][MN], C[MN][MN];
std::vector<int> cnt[MN][MN];
void ins(int u, int v, int c) {
nxt[++K] = h[u];
h[u] = K;
to[K] = v;
cap[K] = c;
}
void insw(int u, int v, int c) {
ins(u, v, c);
ins(v, u, 0);
}
bool bfs(int S, int T) {
memcpy(iter, h, sizeof(h));
memset(d, 0, sizeof(d));
d[S] = 1;
hh = tt = 0;
q[tt++] = S;
while (hh < tt) {
int u = q[hh++];
for (int i = h[u]; i; i = nxt[i])
if (cap[i] && !d[to[i]]) {
d[to[i]] = d[u] + 1;
q[tt++] = to[i];
}
}
return d[T];
}
int dfs(int u, int T, int f) {
if (u == T)
return f;
int used = 0;
for (int &i = iter[u]; i; i = nxt[i]) {
if (cap[i] && d[to[i]] == d[u] + 1) {
int w = dfs(to[i], T, std::min(cap[i], f - used));
if (w) {
cap[i] -= w;
cap[i ^ 1] += w;
used += w;
if (f == used)
return f;
}
}
}
return used;
}
int dinic(int S, int T) {
int w, f = 0;
while (bfs(S, T))
while (w = dfs(S, T, 1e9))
f += w;
return f;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &A[i][j]);
cnt[i][(A[i][j] - 1) / m + 1].push_back(A[i][j]);
}
int S = 2 * n + 1, T = 2 * n + 2;
for (int i = 1; i <= m; i++) {
memset(h, 0, sizeof(h));
K = 1;
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
if (cnt[j][k].size())
insw(j, n + k, 1);
for (int j = 1; j <= n; j++)
insw(S, j, 1), insw(j + n, T, 1);
dinic(S, T);
for (int j = 1; j <= n; j++)
for (int x = h[j]; x; x = nxt[x])
if (!cap[x] && n + 1 <= to[x] && to[x] <= 2 * n)
B[j][i] = cnt[j][to[x] - n].back(), cnt[j][to[x] - n].pop_back();
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++) {
C[(B[i][j] - 1) / m + 1][j] = B[i][j];
printf("%d ", B[i][j]);
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("%d ", C[i][j]);
} | replace | 5 | 6 | 5 | 6 | TLE | |
p02942 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 105;
int n, m, a[MAXN][MAXN];
vector<int> gr[MAXN][MAXN];
inline void addEdge(int u, int v, int c) {
gr[u][v].push_back(c), gr[v][u].push_back(c);
}
int match[MAXN << 2];
bool vis[MAXN << 2];
inline bool dfs(int u) {
for (int i = n + 1; i <= 2 * n; i++)
if (!vis[i] && !gr[u][i].empty()) {
vis[i] = true;
if (match[i] < 0 || dfs(match[i])) {
match[i] = u;
return true;
}
}
return false;
}
inline int calc(int x) { return (x - 1) / m + 1; }
int ansb[MAXN][MAXN], tmp[MAXN], ansc[MAXN][MAXN];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
addEdge(i, n + calc(a[i][j]), a[i][j]);
for (int i = 1; i <= m; i++) {
memset(match, -1, sizeof(match));
for (int j = 1; j <= n; j++) {
memset(vis, 0, sizeof(vis));
dfs(j);
}
for (int j = n + 1; j <= 2 * n; j++) {
ansb[match[j]][i] = gr[j][match[j]][gr[j][match[j]].size() - 1];
gr[j][match[j]].pop_back(), gr[match[j]][j].pop_back();
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
tmp[j] = ansb[j][i];
sort(tmp + 1, tmp + n + 1);
for (int j = 1; j <= n; j++)
ansc[j][i] = tmp[j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << ansb[i][j] << " ";
cout << endl;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << ansc[i][j] << " ";
cout << endl;
}
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXN = 505;
int n, m, a[MAXN][MAXN];
vector<int> gr[MAXN][MAXN];
inline void addEdge(int u, int v, int c) {
gr[u][v].push_back(c), gr[v][u].push_back(c);
}
int match[MAXN << 2];
bool vis[MAXN << 2];
inline bool dfs(int u) {
for (int i = n + 1; i <= 2 * n; i++)
if (!vis[i] && !gr[u][i].empty()) {
vis[i] = true;
if (match[i] < 0 || dfs(match[i])) {
match[i] = u;
return true;
}
}
return false;
}
inline int calc(int x) { return (x - 1) / m + 1; }
int ansb[MAXN][MAXN], tmp[MAXN], ansc[MAXN][MAXN];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
addEdge(i, n + calc(a[i][j]), a[i][j]);
for (int i = 1; i <= m; i++) {
memset(match, -1, sizeof(match));
for (int j = 1; j <= n; j++) {
memset(vis, 0, sizeof(vis));
dfs(j);
}
for (int j = n + 1; j <= 2 * n; j++) {
ansb[match[j]][i] = gr[j][match[j]][gr[j][match[j]].size() - 1];
gr[j][match[j]].pop_back(), gr[match[j]][j].pop_back();
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
tmp[j] = ansb[j][i];
sort(tmp + 1, tmp + n + 1);
for (int j = 1; j <= n; j++)
ansc[j][i] = tmp[j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << ansb[i][j] << " ";
cout << endl;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cout << ansc[i][j] << " ";
cout << endl;
}
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.