#include <iostream> struct dyn { int left = 0; int up = 0; }; int ceil(int x, int y) { return x/y + (x % y != 0); } using namespace std; int main() { int n, k, probe; cin>>n>>k; int curr_min = 2147483647; int size = k < n ? k : n; dyn heap[2][size + 1]; int dyn_prev = 0; for(int i = 1; i <= n; i++) { int mid = ceil(i, 2); for(int j = 1; j <= mid; j++) { if(i <= size) { if (j == 1) { heap[!dyn_prev][j].left = i; heap[!dyn_prev][j].up = i; } else { heap[!dyn_prev][j].up = heap[dyn_prev][j].up + 1; heap[!dyn_prev][j].left = heap[dyn_prev][j-1].left + heap[!dyn_prev][j].up; } } cin>>probe; if(i <= size) { if (heap[!dyn_prev][j].left <= k) { if (probe < curr_min) { curr_min = probe; } } } } for(int j = mid + 1; j <= i; j++) { if(i <= size) { heap[!dyn_prev][j] = heap[!dyn_prev][i - j + 1]; } cin>>probe; if(i <= size) { if (heap[!dyn_prev][j].left <= k) { if (probe < curr_min) { curr_min = probe; } } } } dyn_prev = ! dyn_prev; } cout<<curr_min<<endl; 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> struct dyn { int left = 0; int up = 0; }; int ceil(int x, int y) { return x/y + (x % y != 0); } using namespace std; int main() { int n, k, probe; cin>>n>>k; int curr_min = 2147483647; int size = k < n ? k : n; dyn heap[2][size + 1]; int dyn_prev = 0; for(int i = 1; i <= n; i++) { int mid = ceil(i, 2); for(int j = 1; j <= mid; j++) { if(i <= size) { if (j == 1) { heap[!dyn_prev][j].left = i; heap[!dyn_prev][j].up = i; } else { heap[!dyn_prev][j].up = heap[dyn_prev][j].up + 1; heap[!dyn_prev][j].left = heap[dyn_prev][j-1].left + heap[!dyn_prev][j].up; } } cin>>probe; if(i <= size) { if (heap[!dyn_prev][j].left <= k) { if (probe < curr_min) { curr_min = probe; } } } } for(int j = mid + 1; j <= i; j++) { if(i <= size) { heap[!dyn_prev][j] = heap[!dyn_prev][i - j + 1]; } cin>>probe; if(i <= size) { if (heap[!dyn_prev][j].left <= k) { if (probe < curr_min) { curr_min = probe; } } } } dyn_prev = ! dyn_prev; } cout<<curr_min<<endl; return 0; } |