QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#414881#8057. Best Carry Player 4kevinshan#WA 57ms3764kbC++142.6kb2024-05-19 23:49:592024-05-19 23:50:01

Judging History

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

  • [2024-05-19 23:50:01]
  • 评测
  • 测评结果:WA
  • 用时:57ms
  • 内存:3764kb
  • [2024-05-19 23:49:59]
  • 提交

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]) {
			if(!carryable) {
				auto it2 = avail.lower_bound(m-i);
				if(it2 != avail.end()) {
					int val2 = *it2;
					if(val2 == m-i) {
						carryable = true;
						ans++;
						b[i]--;
						a[val2]--;
						if(!a[val2]) avail.erase(val2);
					}		
				}
			}

			auto it = avail.lower_bound(m-1-i);
			if(it==avail.end()) {
				if(!carryable && dones && i==larg2) carryable = true;
				break;
			}
			
			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
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

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: 57ms
memory: 3764kb

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

result:

wrong answer 3rd numbers differ - expected: '4', found: '3'