#include <cstdio> #include <stdio.h> using namespace std; int removeNumber(short int row, short int col, int k) { short int col_left = col; short int col_right = col; int cnt = 1; for (short int i = row - 1; i >= 1; i--) { // if (cnt > k) { // return cnt; // } col_left -= 1; if (col_right > i) col_right = i; if (col_left == 0) col_left = 1; cnt += col_right - col_left + 1; } //printf("\n%hi % hi %d", row, col, cnt); return cnt; } struct coordinates { short int row; short int col; }; coordinates getCoordinates(short int position) { int cnt = 0; struct coordinates coords; coords.col = 1, coords.row = 1; short int i = 1, j = 1; while (coords.row * (coords.row + 1) / 2 < position) { coords.row++; } while (coords.row * (coords.row + 1) / 2 - coords.row + coords.col < position) { coords.col++; } //printf("coords: %d, %d\n", coords.row, coords.col); return coords; } int main() { short int n = 0; int k = 0; scanf("%hi", &n); scanf("%d", &k); int repeats = n * (n + 1) / 2; int firstNumber; scanf("%d", &firstNumber); if (k == 1) { printf("%d", firstNumber); return 0; } int cnt = 1; short int lowest; lowest = firstNumber; short int year; while (repeats-- > 1) { scanf("%hi", &year); cnt++; if (year < lowest) { coordinates crd = getCoordinates(cnt); int result = removeNumber(crd.row, crd.col, k); if (result <= k) { lowest = year; } } } printf("%hi", lowest); 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <cstdio> #include <stdio.h> using namespace std; int removeNumber(short int row, short int col, int k) { short int col_left = col; short int col_right = col; int cnt = 1; for (short int i = row - 1; i >= 1; i--) { // if (cnt > k) { // return cnt; // } col_left -= 1; if (col_right > i) col_right = i; if (col_left == 0) col_left = 1; cnt += col_right - col_left + 1; } //printf("\n%hi % hi %d", row, col, cnt); return cnt; } struct coordinates { short int row; short int col; }; coordinates getCoordinates(short int position) { int cnt = 0; struct coordinates coords; coords.col = 1, coords.row = 1; short int i = 1, j = 1; while (coords.row * (coords.row + 1) / 2 < position) { coords.row++; } while (coords.row * (coords.row + 1) / 2 - coords.row + coords.col < position) { coords.col++; } //printf("coords: %d, %d\n", coords.row, coords.col); return coords; } int main() { short int n = 0; int k = 0; scanf("%hi", &n); scanf("%d", &k); int repeats = n * (n + 1) / 2; int firstNumber; scanf("%d", &firstNumber); if (k == 1) { printf("%d", firstNumber); return 0; } int cnt = 1; short int lowest; lowest = firstNumber; short int year; while (repeats-- > 1) { scanf("%hi", &year); cnt++; if (year < lowest) { coordinates crd = getCoordinates(cnt); int result = removeNumber(crd.row, crd.col, k); if (result <= k) { lowest = year; } } } printf("%hi", lowest); return 0; } |