제출 #1201957

#제출 시각아이디문제언어결과실행 시간메모리
1201957vincentbucourt1Let's Win the Election (JOI22_ho_t3)C++20
100 / 100
785 ms503432 KiB
#include <iostream>
#include <algorithm>
using namespace std;

struct Election {
	int a, b;
};

bool operator<(const Election &a1, const Election &a2) {
	if (a1.b < a2.b) return true;
	if (a1.b > a2.b) return false;
	if (a1.a < a2.a) return true;
	return false;
}

int N, K;
Election X[509];
long long dp[509][509][509]; // (pos, total votes, cooperator)
long long CONST = 1000000000;

long long solve(int Goal) {
	// Initialize [1]
	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= min(i, K); j++) {
			if (i == j) {
				for (int k = 0; k <= min(j, Goal); k++) dp[i][j][k] = (1LL << 60);
			}
			else {
				dp[i][j][Goal] = (1LL << 60);
			}
		}
	}
	
	// Dynamic Programming
	dp[0][0][0] = 0;
	for (int i = 0; i < N; i++) {
		for (int j = max(0, K - N + i); j <= min(i, K); j++) {
			if (i == j) {
				for (int k = max(0, Goal - N + i); k <= min(j, Goal); k++) {
					dp[i + 1][j + 0][k + 0] = min(dp[i + 1][j + 0][k + 0], dp[i][j][k]);
					dp[i + 1][j + 1][k + 0] = min(dp[i + 1][j + 1][k + 0], dp[i][j][k] + CONST * X[i + 1].a / (Goal + 1));
					dp[i + 1][j + 1][k + 1] = min(dp[i + 1][j + 1][k + 1], dp[i][j][k] + CONST * X[i + 1].b / (k + 1));
				}
			}
			else {
				dp[i + 1][j + 0][Goal] = min(dp[i + 1][j + 0][Goal], dp[i][j][Goal]);
				dp[i + 1][j + 1][Goal] = min(dp[i + 1][j + 1][Goal], dp[i][j][Goal] + CONST * X[i + 1].a / (Goal + 1));
			}
		}
	}
	
	// Return Value
	return dp[N][K][Goal];
}

int main() {
	// Step #1. Input
	cin >> N;
	cin >> K;
	for (int i = 1; i <= N; i++) {
		cin >> X[i].a >> X[i].b;
		if (X[i].b == -1) X[i].b = 1000000;
	}
	
	// Step #2. Sorting
	sort(X + 1, X + N + 1);
	
	// Step #3. Brute Force
	long long Answer = (1LL << 60);
	for (int i = 0; i <= K; i++) {
		long long ret = solve(i);
		Answer = min(Answer, ret);
	}
	
	// Step #4. Output
	printf("%lld.%09lld\n", Answer / CONST, Answer % CONST);
	return 0;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...