Submission #779005

# Submission time Handle Problem Language Result Execution time Memory
779005 2023-07-11T06:25:19 Z 박상훈(#10000) Bodyguards (CEOI10_bodyguards) C++17
100 / 100
109 ms 9984 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr ll INF = 4e18;
pair<ll, ll> a[200200], b[200200];
ll bS[200200];

void NO(){
	printf("0\n");
	exit(0);
}

struct Frac{
	ll a, b; // = a/b
	Frac(){}
	Frac(ll _a, ll _b): a(_a), b(_b) {}

	bool operator < (const Frac &F) const{
		return (__int128)a * F.b < (__int128)F.a * b;
	}
};

struct Line{
	ll a, b;
	Line(){}
	Line(ll _a, ll _b): a(_a), b(_b) {}

	ll operator () (ll x) const{
		return a*x + b;
	}
};

Frac cross(const Line &f, const Line &g){
	return Frac(g.b - f.b, f.a - g.a);
}

int solve(int n, int m){
	sort(a+1, a+n+1, greater<pair<ll, ll>>());
	sort(b+1, b+m+1, greater<pair<ll, ll>>());
	
	b[0].first = INF;
	bS[0] = 0;
	for (int i=1;i<=m;i++) bS[i] = bS[i-1] + b[i].second;

	int i = 1, j = m, ret = 1;
	ll c = 0, val = 0, sum = 0;

	while(i<=n){
		ll nc = min(c + a[i].second, b[j].first);
		assert(c<=nc);

		if (c==nc){
			assert(nc < c + a[i].second);
			val += (ll)b[j].first * b[j].second;
			--j;
			continue;
		}

		Line f(a[i].first, sum - (ll)a[i].first * c);
		Line g(bS[j], val);

		// printf("check %lld to %lld\n", c+1, nc);
		// for (int i=c+1;i<=nc;i++){
			// printf("i = %d -> cur = %lld / val = %lld\n", i, f(i), g(i));
		// }

		if (f(c+1) > g(c+1)){
			ret = 0;
			break;
		}
		// if (f.a > g.a && cross(f, g) < Frac(nc, 1)) NO();
		if (f(nc) > g(nc)){
			ret = 0;
			break;
		}

		sum = f(nc);
		a[i].second -= nc-c;
		if (a[i].second==0) i++;
		if (b[j].first==nc){
			val += (ll)b[j].first * b[j].second;
			--j;
		}

		c = nc;
		
	}

	return ret;
}

int naive(int n, int m){
	vector<int> X, Y;

	for (int i=1;i<=n;i++){
		auto [x, y] = a[i];
		for (int j=1;j<=y;j++) X.push_back(x);
	}

	for (int i=1;i<=m;i++){
		auto [x, y] = b[i];
		for (int j=1;j<=y;j++) Y.push_back(x);
	}

	sort(X.begin(), X.end(), greater<int>());
	sort(Y.begin(), Y.end(), greater<int>());
	swap(X, Y);

	ll cur = 0;
	int ret = 1;

	for (int i=0;i<(int)Y.size();i++){
		cur += Y[i];
		ll val = 0;
		for (int j=0;j<(int)X.size();j++) val += min(X[j], i+1);

		// printf("i = %d -> cur = %lld / val = %lld\n", i, cur, val);
		if (cur > val){
			ret = 0;
			break;
		}
	}

	return ret;
}

mt19937 seed(1557);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed)%(r-l+1)+l;}

void stress(int tc){
	printf("---------------------------------\n");
	printf("Stress #%d\n", tc);
	printf("Input:\n");

	int n = getrand(1, 4), m = getrand(1, 4);
	ll sum = 0;

	for (int i=1;i<=n;i++){
		a[i].first = getrand(1, 5);
		a[i].second = getrand(1, 5);
		sum += a[i].first * a[i].second;
	}

	for (int j=1;j<=m;j++){
		b[j].first = getrand(1, 5);
		b[j].second = getrand(1, 5);

		sum -= b[j].first * b[j].second;
	}

	if (sum < 0){
		n++;
		a[n].first = 1;
		a[n].second = -sum;
	}

	else if (sum > 0){
		m++;
		b[m].first = 1;
		b[m].second = sum;
	}

	printf("%d\n", n);
	for (int i=1;i<=n;i++) printf("%lld %lld\n", a[i].first, a[i].second);
	printf("%d\n", m);
	for (int i=1;i<=m;i++) printf("%lld %lld\n", b[i].first, b[i].second);

	auto ans = naive(n, m);
	auto out = solve(n, m);

	printf("Answer: %d\n", ans);
	printf("Output: %d\n", out);

	assert(ans==out);
}


int main(){
	// for (int i=1;;i++) stress(i);

	int n, m;
	scanf("%d", &n);

	vector<int> X, Y;

	for (int i=1;i<=n;i++){
		ll x, y;
		scanf("%lld %lld", &x, &y);
		a[i] = {x, y};
		// for (int j=1;j<=y;j++) X.push_back(x);
	}

	scanf("%d", &m);
	for (int i=1;i<=m;i++){
		ll x, y;
		scanf("%lld %lld", &x, &y);
		b[i] = {x, y};
		// for (int j=1;j<=y;j++) Y.push_back(x);
	}

	printf("%d\n", solve(n, m));
	return 0;

	// sort(X.begin(), X.end(), greater<int>());
	// sort(Y.begin(), Y.end(), greater<int>());

	// ll cur = 0;
	// for (int i=0;i<(int)Y.size();i++){
	// 	cur += Y[i];
	// 	ll val = 0;
	// 	for (int j=0;j<(int)X.size();j++) val += min(X[j], i+1);
	// 	if (cur > val) NO();
	// }


	// printf("1\n");
}

Compilation message

bodyguards.cpp: In function 'int main()':
bodyguards.cpp:184:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
bodyguards.cpp:190:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  190 |   scanf("%lld %lld", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
bodyguards.cpp:195:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |  scanf("%d", &m);
      |  ~~~~~^~~~~~~~~~
bodyguards.cpp:198:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  198 |   scanf("%lld %lld", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 284 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 520 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 584 KB Output is correct
9 Correct 4 ms 580 KB Output is correct
10 Correct 3 ms 580 KB Output is correct
11 Correct 3 ms 596 KB Output is correct
12 Correct 3 ms 580 KB Output is correct
13 Correct 3 ms 580 KB Output is correct
14 Correct 3 ms 596 KB Output is correct
15 Correct 3 ms 568 KB Output is correct
16 Correct 3 ms 572 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 1616 KB Output is correct
2 Correct 12 ms 1336 KB Output is correct
3 Correct 23 ms 3040 KB Output is correct
4 Correct 24 ms 2960 KB Output is correct
5 Correct 20 ms 3028 KB Output is correct
6 Correct 23 ms 3648 KB Output is correct
7 Correct 16 ms 2556 KB Output is correct
8 Correct 23 ms 3708 KB Output is correct
9 Correct 24 ms 3388 KB Output is correct
10 Correct 21 ms 3404 KB Output is correct
11 Correct 22 ms 3404 KB Output is correct
12 Correct 25 ms 3620 KB Output is correct
13 Correct 21 ms 3512 KB Output is correct
14 Correct 22 ms 3532 KB Output is correct
15 Correct 23 ms 3540 KB Output is correct
16 Correct 23 ms 3528 KB Output is correct
17 Correct 26 ms 3512 KB Output is correct
18 Correct 22 ms 3504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 3220 KB Output is correct
2 Correct 30 ms 2712 KB Output is correct
3 Correct 46 ms 5688 KB Output is correct
4 Correct 7 ms 1188 KB Output is correct
5 Correct 48 ms 6092 KB Output is correct
6 Correct 37 ms 5184 KB Output is correct
7 Correct 45 ms 5752 KB Output is correct
8 Correct 6 ms 1096 KB Output is correct
9 Correct 47 ms 6032 KB Output is correct
10 Correct 48 ms 5852 KB Output is correct
11 Correct 48 ms 5752 KB Output is correct
12 Correct 53 ms 5804 KB Output is correct
13 Correct 52 ms 6148 KB Output is correct
14 Correct 49 ms 6088 KB Output is correct
15 Correct 46 ms 6092 KB Output is correct
16 Correct 47 ms 6060 KB Output is correct
17 Correct 47 ms 6116 KB Output is correct
18 Correct 48 ms 6092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 5720 KB Output is correct
2 Correct 70 ms 5680 KB Output is correct
3 Correct 58 ms 5048 KB Output is correct
4 Correct 29 ms 2568 KB Output is correct
5 Correct 75 ms 7960 KB Output is correct
6 Correct 74 ms 9984 KB Output is correct
7 Correct 59 ms 6544 KB Output is correct
8 Correct 69 ms 8108 KB Output is correct
9 Correct 109 ms 9456 KB Output is correct
10 Correct 84 ms 9408 KB Output is correct
11 Correct 84 ms 9408 KB Output is correct
12 Correct 88 ms 9388 KB Output is correct
13 Correct 92 ms 9576 KB Output is correct
14 Correct 9 ms 1620 KB Output is correct
15 Correct 103 ms 9956 KB Output is correct
16 Correct 103 ms 9952 KB Output is correct
17 Correct 108 ms 9928 KB Output is correct
18 Correct 87 ms 9496 KB Output is correct