Submission #408726

# Submission time Handle Problem Language Result Execution time Memory
408726 2021-05-19T14:52:08 Z priority_queue Packing Biscuits (IOI20_biscuits) C++14
9 / 100
1000 ms 376 KB
#include <bits/stdc++.h>

#define forint(i, N) for (long long i = 0; i < (N); i++)

using namespace std;

void takeout(vector<long long>& a, long long pos, long long val) {
//	cerr << "out " << val << " from " << pos << endl;

	if (a[pos] * (1ll << pos) < val) {
		takeout(a, pos-1, val - a[pos] * (1ll << pos));
		a[pos] = 0;
		return;
	}

	long long v = val / (1ll << pos);

	a[pos] -= v;

	if (v * (1ll << pos) < val) {
		takeout(a, pos-1, val - v * (1ll << pos));	
	}
}


long long count(long long x, vector<long long>& a, long long max_y) {
/*
cerr << "max_y = " << max_y << endl;
for (auto aa : a) {
	cerr << aa << " ";	
}
cerr << endl;
*/
	if (max_y == 0) {
		return 1;
	}

	if (max_y == 1) {
		if (a[0] >= x) {
			return 2; // 0 & 1;
		}
		else {
			return 1; // 0 only 
		}
	}

	long long k = a.size();
	vector<long long> sum(61, 0);

	sum[0] = a[0];
	for (long long i = 1; i < k; i++) {
		sum[i] = sum[i - 1] + a[i] * (1ll << i);
		//cerr << "sum i=" << sum[i] << " " << endl; 
	}

	for (long long i = k; i < 61; i++) {
		sum[i] = sum[i-1];
		//cerr << "sum i=" << sum[i] << " " << endl;
	}

	// find i
	long long i = 0;
	while ((1ll << i) < max_y) {
		i++;
	}

	if (max_y == (1ll << i)) {
		if (sum[i] / x >= max_y) { 
			return count(x, a, max_y - 1) + 1;
		}
		else {
			return count(x, a, max_y - 1);
		}
	}

	long long aa = count(x, a, 1ll << (i-1));

//cerr << "omg: " << aa << endl;

	if (sum[i-1] /  (1ll << (i-1)) <  x ) { // sum[i-1] >> (i-1)
		//cerr << "a" << endl;
		return aa;
	}
	else {
		//cerr << "b" << endl;
		vector<long long> b;
		forint(j, k) {
			if ((1ll << j) <= max_y) {
				b.push_back(a[j]);
			}
			else {
				break;
			}
		}

		/*
		cerr << "take out " << (1ll << (i-1)) *  x << " from " << i-1 << endl;
		for(auto bb:b) {
			cerr  << bb << ":";
		}
		cerr << endl;
*/

		//takeout(b, b.size()-1, (1ll << (i-1)) * x);
		/*
		long long amount = x;
		int pos = i-1;
		while (pos > (b.size()-1)) {
			pos--;
			amount *= 2;
		}
		while (amount > 0) {
			if (amount <= b[pos]) {
				b[pos] -= amount;
				amount = 0;
			}
			else {
				amount = (amount - b[pos])*2;
				b[pos] = 0;
			}
		}
	   */
		long long amount = x;
		//cout << "hey " << i-1 << " " << sum[i-1] << " "  << (1ll << (i-1)) << " " << x << endl;
		for (long long j = i-1; j >=0; j--) {
			//cerr << j << ";" << amount << "*" << b[j] << endl;
			if (j < b.size() && amount <= b[j]) {
				b[j] -= amount;
				amount = 0;
				break;
			}
			else {
				if (j < b.size()) {
					amount = (amount - b[j]) * 2;
					b[j] = 0;
				} else {
					amount *= 2;
				}
			}
			//cerr << b[j] << "___";
		}

/*
		cerr << endl;
		for(auto bb:b) {
			cerr  << bb << ":";
		}
		cerr << endl;
*/
		return  aa + count(x, b, max_y - (1ll << (i-1))) - 1; // 0 has been counted
	}
 }


long long count_tastiness(long long x, vector<long long> a) {
	return count(x, a, 1e18);
	//return count(x, a, 15);
}

Compilation message

biscuits.cpp: In function 'long long int count(long long int, std::vector<long long int>&, long long int)':
biscuits.cpp:127:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |    if (j < b.size() && amount <= b[j]) {
      |        ~~^~~~~~~~~~
biscuits.cpp:133:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     if (j < b.size()) {
      |         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 34 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 52 ms 332 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 133 ms 344 KB Output is correct
7 Correct 8 ms 332 KB Output is correct
8 Correct 134 ms 348 KB Output is correct
9 Correct 19 ms 364 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 58 ms 332 KB Output is correct
13 Correct 27 ms 332 KB Output is correct
14 Correct 4 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 292 KB Output is correct
2 Execution timed out 1074 ms 332 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1088 ms 376 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 52 ms 332 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 133 ms 344 KB Output is correct
7 Correct 8 ms 332 KB Output is correct
8 Correct 134 ms 348 KB Output is correct
9 Correct 19 ms 364 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 58 ms 332 KB Output is correct
13 Correct 27 ms 332 KB Output is correct
14 Correct 4 ms 332 KB Output is correct
15 Correct 32 ms 292 KB Output is correct
16 Execution timed out 1074 ms 332 KB Time limit exceeded
17 Halted 0 ms 0 KB -