QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414906#8057. Best Carry Player 4MHo2005WA 90ms3752kbC++143.7kb2024-05-20 01:28:482024-05-20 01:28:48

Judging History

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

  • [2024-05-20 01:28:48]
  • 评测
  • 测评结果:WA
  • 用时:90ms
  • 内存:3752kb
  • [2024-05-20 01:28:48]
  • 提交

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;
	if(sum1>sum2) b[0] += sum1-sum2;
	else a[0] += sum2-sum1;

	// 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;
			dbg(i,val,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
			dbg("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: 3592kb

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: 0
Accepted
time: 60ms
memory: 3532kb

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
3
3
3
2
0
0
1
1
3
0
3
0
0
0
0
0
0
0
4
0
4
1
0
2
3
3
1
5
0
0
2
0
0
1
1
0
0
3
5
3
2
2
2
0
1
2
2
2
0
3
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:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 29ms
memory: 3524kb

input:

1000
500
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

0
1
1
2
2
2
0
0
0
3
1
1
2
0
1
0
1
1
3
0
0
4
3
1
4
4
0
0
2
1
3
3
0
0
1
0
1
1
0
0
2
1
2
0
3
1
3
1
3
4
2
1
1
2
3
0
0
1
2
0
0
1
1
1
1
2
1
2
2
0
2
2
1
1
3
0
0
1
2
0
1
3
0
0
1
1
0
0
1
0
3
1
2
0
0
5
1
1
1
3
1
4
1
0
0
0
0
0
1
3
1
2
1
0
0
0
0
0
1
1
0
4
0
1
1
3
0
1
0
0
2
3
0
1
2
1
1
1
0
3
0
0
3
2
1
1
0
1
0
1
...

result:

ok 1000 numbers

Test #4:

score: -100
Wrong Answer
time: 90ms
memory: 3752kb

input:

100000
5
119777838 337555280 806504015 458289944 321614229
979242871 431783189 423329635 193632121 7339369
5
189264782 667669243 753322761 861814678 224007583
977519325 104432095 940220826 712094722 903574615
5
977791540 278658984 601762324 633391706 36807634
689562032 997406456 118939957 891425821 ...

output:

1377698543
2884329841
1699584169
2132012702
2531472710
2754014935
2585135038
2577893498
2034726862
2303377730
1476887044
2618960687
1626312268
1068946376
600795101
927483202
2151016849
2256729729
2291627593
1751199962
2412405837
1884010446
2553701265
2014856631
848686688
1069641213
2412585811
167144...

result:

wrong answer 39th numbers differ - expected: '1572566306', found: '-2722400990'