#include <cstdio> #include <algorithm> int year[2000][2000]; int cost[2000][2000]; int main() { int N, K; // change to "while" for multiple inputs if (scanf("%d%d", &N, &K) == 2) { for (int n = 0; n<N; ++n) { for (int i = 0; i<=n; ++i) { if (scanf("%d", &year[n][i])!=1) return 1; } } cost[0][0] = 1; for (int n = 1; n<N; ++n) { cost[n][0] = cost[n][n] = 1+n; for (int i = 1; i<n; ++i) { cost[n][i] = cost[n-1][i-1]+cost[n-1][i]-cost[n-2][i-1]+1; } } int best_year = year[0][0]; for (int n = 1; n<N; ++n) { for (int i = 0; i<=n; ++i) { if (cost[n][i]<=K) { best_year = std::min(best_year, year[n][i]); } } } printf("%d\n", best_year); } }
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 <cstdio> #include <algorithm> int year[2000][2000]; int cost[2000][2000]; int main() { int N, K; // change to "while" for multiple inputs if (scanf("%d%d", &N, &K) == 2) { for (int n = 0; n<N; ++n) { for (int i = 0; i<=n; ++i) { if (scanf("%d", &year[n][i])!=1) return 1; } } cost[0][0] = 1; for (int n = 1; n<N; ++n) { cost[n][0] = cost[n][n] = 1+n; for (int i = 1; i<n; ++i) { cost[n][i] = cost[n-1][i-1]+cost[n-1][i]-cost[n-2][i-1]+1; } } int best_year = year[0][0]; for (int n = 1; n<N; ++n) { for (int i = 0; i<=n; ++i) { if (cost[n][i]<=K) { best_year = std::min(best_year, year[n][i]); } } } printf("%d\n", best_year); } } |