QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414878#8057. Best Carry Player 4kevinshan#WA 1ms3740kbC++142.6kb2024-05-19 23:37:342024-05-19 23:37:35

Judging History

你现在查看的是最新测评结果

  • [2024-05-19 23:37:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3740kb
  • [2024-05-19 23:37:34]
  • 提交

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;
	For(i,m) if(b[i]) {
		while(b[i]) {
			auto it = avail.lower_bound(m-1-i);
			if(it==avail.end()) {
				if(!carryable && dones && i==larg2) carryable = true;
				break;
			}
			if(!carryable) {
				auto it2 = avail.lower_bound(m-i);
				int val2 = *it2;
				if(val2 == m-i) {
					carryable = true;
					ans++;
					b[i]--;
					a[val2]--;
					if(!a[val2]) avail.erase(val2);
				}	
			}
			
			dones = true;
			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);
		}
	}
	if(carryable) {
		cout << ans << '\n';
	} else {
		if(larg1 + larg2 >=m ) { //?
			dbg("A");
			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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3740kb

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:

7
1
3
467900
29

result:

wrong answer 1st numbers differ - expected: '5', found: '7'