Submission #388500

#TimeUsernameProblemLanguageResultExecution timeMemory
388500JimmyZJXPacking Biscuits (IOI20_biscuits)C++14
100 / 100
27 ms1340 KiB
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>

using namespace std;

typedef long long LL;
typedef vector<int> Vi;
typedef vector<LL> VL;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;

#define MAXP 61

#define forR(i, n) for (int i = 0; i < (n); i++)

VL preSum(MAXP);

LL count(LL x, VL a, int p) {
	// [ 0, 2^p ) ++ [ 2^p , 2^(p+1) )

	if (p == 0) {
		return a[0] >= x ? 2 : 1;
	}

	LL count_before_p = count(x, a, p - 1);

	if (a[p] >= x) {
		return 2 * count_before_p;
	} else if (p > 0 && a[p] + (preSum[p - 1] >> p) >= x) {
		LL rest = (x - a[p]) * (1LL << p);

		int c; for (c = p - 1; c >= 0; c--) {
			LL cnt = min(a[c], rest / (1LL << c));
			rest -= cnt * (1LL << c);
			a[c] -= cnt;
			if (rest == 0) break;
		}
		assert(rest == 0);

		LL chooseP = count(x, a, c);
		return count_before_p + chooseP;
	} else {
		return count_before_p;
	}
}

LL count_tastiness(LL x, VL a) {
	int k = a.size();
	a.resize(MAXP);

	LL c = 0;
	forR(i, MAXP) {
		a[i] += c; c = 0;
		if (a[i] > x) {
			c = (a[i] - x) / 2;
			a[i] -= c * 2;
		}
	}

	forR(i, MAXP) {
		preSum[i] = i > 0 ? preSum[i - 1] : 0;
		preSum[i] += ((LL)1 << i) * a[i];
	}

	VL counts(MAXP + 1); // counts[p]: [ 0 , 2^p )
	counts[0] = 1;
	for (int p = 0; p < MAXP; p++) {
		LL CountLeP = counts[p];
		if (a[p] >= x) {
			counts[p + 1] = 2 * CountLeP;
		} else {
			LL countP = CountLeP;
			int r = p;

			LL ar = a[r];
			while (r > 0 && ar + (preSum[r - 1] >> r) >= x) {
				LL rest = (x - ar) * (1LL << r);

				for (r = r - 1; r >= 0; r--) {
					LL cnt = min(a[r], rest / (1LL << r));
					rest -= cnt * (1LL << r);
					if (rest == 0) {
						ar = a[r] - cnt;
						break;
					}
				}
				assert(rest == 0);

				countP += counts[r];
			}

			counts[p + 1] = countP;
		}
	}

	return counts[MAXP];
	// return count(x, a, MAXP - 1);
}


#ifdef TEST_LOCAL
int main() {
	LL res;
	res = count_tastiness(3, VL{ 5,2,1 });
	res = count_tastiness(2, VL{ 2,1,2 });

	return 0;
}
#endif

Compilation message (stderr)

biscuits.cpp: In function 'LL count_tastiness(LL, VL)':
biscuits.cpp:59:6: warning: unused variable 'k' [-Wunused-variable]
   59 |  int k = a.size();
      |      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...