제출 #1334322

#제출 시각아이디문제언어결과실행 시간메모리
1334322gohchingjaykIMO (EGOI25_imo)C++20
100 / 100
136 ms17948 KiB
#pragma GCC optimize("O3,inline")
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
#define int ll

using ii = pair<int, int>;
using iii = pair<int, ii>;
using pvii = pair<vector<int>, ii>;
 
constexpr int MAXN = 20000 + 5;
constexpr int INF = 1e18 + 5;
constexpr int LOG = 20;

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

	int N, M, K; cin >> N >> M >> K;
	int ans = N * M;
	
	vector<pvii> inp(N);
	
	for (int i = 0; i < N; ++i) {
		vector<int> lol(M); for (int j = 0; j < M; ++j) cin >> lol[j];
		sort(lol.begin(), lol.end());
		int s = 0; for (int j = 0; j < M; ++j) s += lol[j];
		inp[i] = pvii{lol, ii{s, i}};
	}
	
	sort(inp.begin(), inp.end(), [&](const pvii &a, const pvii &b) {
		return a.second.first < b.second.first || a.second.first == b.second.first && a.second.second > b.second.second;
	});
	
	// buffer element
	inp.emplace_back(vector<int>(), ii{M * K, -INF});

	vector<int> max_with_lb(M*K+2);
	for (int i = N-1; i >= 0; --i) {
		int maxRem = (!i ? inp[i].second.first : inp[i].second.first - inp[i-1].second.first);
		if (i && inp[i].second.second > inp[i-1].second.second) maxRem--;
		bool off = inp[i+1].second.second > inp[i].second.second;
		int nxt = inp[i+1].second.first;
		
		vector<int> &v = inp[i].first;
		int s = inp[i].second.first;
		
		vector<bitset<105>> dp_curr(maxRem + 1);

		dp_curr[0].set(0);
		
		for (int j = 0; j < M; ++j) {
			for (int k = maxRem; k >= v[j]; --k) {
				dp_curr[k] |= dp_curr[k - v[j]] << 1;
			}
		}
		
		for (int r = 0; r <= maxRem; ++r) {
			max_with_lb[s - r] = max(max_with_lb[s - r], max_with_lb[s - r + 1]);
			for (int j = 0; j <= M; ++j) {
				int lb = s - r + j * K + off;
				if (dp_curr[r][j] && lb <= nxt) {
					max_with_lb[s - r] = max(max_with_lb[s - r], max_with_lb[lb] + j);
				}
			}
		}
	}
	
	cout << ans - max_with_lb[0];
}

/*
2 3 5
3 0 4
4 4 4

ans should be 4

*/
#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...