QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#414903 | #8057. Best Carry Player 4 | MHo2005 | WA | 60ms | 3720kb | C++14 | 3.6kb | 2024-05-20 01:19:50 | 2024-05-20 01:19:50 |
Judging History
answer
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define bk back()
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define For(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define Rof(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7;
const ld PI = acos((ld)-1);
mt19937 rng; // or mt19937_64
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
void solve() {
int m;cin>>m;
vi a(m),b(m);
ll sum1=0,sum2=0;
int larg1=0, larg2=0;
For(i,m)cin>>a[i], sum1+=a[i];
For(i,m)cin>>b[i], sum2+=b[i];
// if(sum2>sum1) swap(a,b);
For(i,m) if(a[i]) larg1 = max(larg1,i);
For(i,m) if(b[i]) larg2 = max(larg2,i);
a[0] = MOD, b[0]=MOD;
// greedy pairs that sum to m-1
set<int> avail;
For(i,m)if(a[i])avail.insert(i);
bool carryable = false;
bool dones = false;
ll ans = 0;
int mnUse = MOD;
For(i,m) if(b[i]) {
bool flag = false;
while(b[i]) {
auto it = avail.lower_bound(m-1-i);
if(it==avail.end()) {
// can just use this b to carry if used before elsewhere
if(!carryable && dones) carryable = true;
break;
}
flag = true;
int val = *it;
ll tmp = min(a[val], b[i]);
b[i] -= tmp;
assert(a[val]);
a[val] -= tmp;
ans += tmp;
if(tmp) mnUse = min(mnUse, val);
// this (i,val) pair can be my carry
if(tmp && i+val>m-1) carryable = true;
if(!a[val]) avail.erase(val);
}
if(flag) dones = true;
}
// have more a's left
if(sz(avail) && *avail.rbegin() > mnUse) carryable = true;
if(carryable) {
cout << ans << '\n';
} else {
if(larg1 + larg2 >=m ) {
// have to sack 1
cout << ans-1 << '\n';
} else {
cout << 0 << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t;cin>>t;
For(i,t) solve();
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
avail.clear();
For(i,m) a[i]=c[i], b[i] = d[i];
For(i,m)if(a[i])avail.insert(i);
ans = 0;
bool flag = false;
FOR(zz,m,2*m) {
For(i,m) if(b[i]) {
auto it2 = avail.lower_bound(zz-i);
if(it2 != avail.end()) {
int val2 = *it2;
if(b[i] && a[val2] && i+val2==zz) {
ans++;
b[i]--;
a[val2]--;
if(!a[val2]) avail.erase(val2);
flag = true;
goto done;
}
}
}
}
done:;
if(!flag) {
cout << 0 << '\n';
return;
}
For(i,m) if(b[i]) {
while(b[i]) {
auto it = avail.lower_bound(m-1-i);
if(it==avail.end()) {
break;
}
int val = *it;
ll tmp = min(a[val], b[i]);
dbg(i,val,a[val], b[i], tmp);
b[i] -= tmp;
a[val] -= tmp;
ans += tmp;
if(i+val>m-1) carryable = true;
if(!a[val]) avail.erase(val);
}
}
dbg("B");
cout << ans << '\n';
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3720kb
input:
5 2 1 2 3 4 3 1 0 1 0 1 0 4 1 0 0 1 1 1 1 1 5 123456 114514 1919810 233333 234567 20050815 998244353 0 0 0 10 5 3 5 3 2 4 2 4 1 5 9 9 8 2 4 4 3 5 3 0
output:
5 1 2 467900 29
result:
ok 5 number(s): "5 1 2 467900 29"
Test #2:
score: -100
Wrong Answer
time: 60ms
memory: 3600kb
input:
100000 5 0 1 1 1 1 0 0 1 0 0 5 0 0 0 0 0 1 1 1 0 0 5 0 0 2 1 1 0 2 1 0 1 5 0 0 0 0 0 1 2 1 0 0 5 0 1 0 1 1 0 0 1 1 1 5 2 0 0 0 1 1 0 0 0 3 5 2 0 0 1 1 0 2 1 1 1 5 0 0 0 0 2 0 0 0 0 1 5 0 0 0 0 0 0 1 1 0 0 5 4 0 0 0 0 0 0 0 1 0 5 0 0 0 0 1 2 1 1 0 0 5 0 2 3 0 0 0 0 0 1 0 5 1 1 1 0 1 1 0 1 0 1 5 0 0 0...
output:
2 0 4 0 4 3 3 2 0 0 1 1 3 0 3 0 0 0 0 0 0 0 4 0 4 1 0 2 4 3 1 5 0 0 2 0 0 1 1 0 0 3 5 3 2 2 2 0 1 2 2 2 0 4 0 2 1 1 0 1 0 4 0 0 2 2 0 3 3 0 2 0 1 0 0 1 1 2 0 3 4 0 2 5 0 2 1 0 0 0 3 2 3 0 2 0 4 3 3 0 2 2 0 1 3 1 1 0 0 0 1 0 3 2 2 0 2 1 1 0 1 0 0 2 4 1 3 3 2 2 2 0 2 0 0 2 3 1 3 1 0 2 2 3 0 1 2 0 1 1 ...
result:
wrong answer 5th numbers differ - expected: '3', found: '4'