#include <bits/stdc++.h> using namespace std; vector<int> wine, cost; int n, k; int min_val, ind; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; wine.resize(n*(n+1)/2); cost.resize(n*(n+1)/2); for (int i = 0; i < n*(n+1)/2; i++) cin >> wine[i]; min_val = 0; ind = 0; int max_iter = min(n, k); for (int h = 1; h <= max_iter; h++) { for (int i = 0; i < h; i++) { if (i == 0 || i == h - 1) cost[ind + i] = h; else cost[ind + i] = 1 + cost[ind - h + i] + cost[ind - h + i + 1] - cost[ind - 2*h + 2 + i]; if (cost[ind + i] <= k && (min_val == 0 || wine[ind + i] < min_val)) min_val = wine[ind + i]; } ind += h; } cout << min_val << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <bits/stdc++.h> using namespace std; vector<int> wine, cost; int n, k; int min_val, ind; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; wine.resize(n*(n+1)/2); cost.resize(n*(n+1)/2); for (int i = 0; i < n*(n+1)/2; i++) cin >> wine[i]; min_val = 0; ind = 0; int max_iter = min(n, k); for (int h = 1; h <= max_iter; h++) { for (int i = 0; i < h; i++) { if (i == 0 || i == h - 1) cost[ind + i] = h; else cost[ind + i] = 1 + cost[ind - h + i] + cost[ind - h + i + 1] - cost[ind - 2*h + 2 + i]; if (cost[ind + i] <= k && (min_val == 0 || wine[ind + i] < min_val)) min_val = wine[ind + i]; } ind += h; } cout << min_val << endl; } |