#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, k, elem;
cin >> n >> k;
vector<int> dp((n + 1) * (n + 2) / 2, k + 1);
dp[0] = 0;
int cnt = 0;
int res = 2019;
for (int row = 1; row <= n; ++row)
{
for (int col = 0; col < row; ++col)
{
cin >> elem;
++dp[cnt + col];
if (dp[cnt + col] <= k)
res = min(res, elem);
dp[cnt + row + col] = min(dp[cnt + row + col], dp[cnt + col] + col);
dp[cnt + row + col + 1] = min(dp[cnt + row + col + 1], dp[cnt + col] + row - 1 - col);
}
cnt += row;
}
cout << res;
return 0;
}
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 38 39 40 | #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, k, elem; cin >> n >> k; vector<int> dp((n + 1) * (n + 2) / 2, k + 1); dp[0] = 0; int cnt = 0; int res = 2019; for (int row = 1; row <= n; ++row) { for (int col = 0; col < row; ++col) { cin >> elem; ++dp[cnt + col]; if (dp[cnt + col] <= k) res = min(res, elem); dp[cnt + row + col] = min(dp[cnt + row + col], dp[cnt + col] + col); dp[cnt + row + col + 1] = min(dp[cnt + row + col + 1], dp[cnt + col] + row - 1 - col); } cnt += row; } cout << res; return 0; } |
English