#include <iostream> #include <algorithm> #define ll long long #define INF (ll)10e13 using namespace std; ll sumOfNumbersUpToN(ll n) { return n * (n + 1) / 2; } void printNTimes(int number, int n) { for(int i = 0; i < n; i++) { printf("%i ", number); } } void optimallyPrintTwoNumbers(int num1, int count1, int num2, int count2) { if(count1 < count2) { optimallyPrintTwoNumbers(num2, count2, num1, count1); return; } if(count2 == 0) { printNTimes(num1, count1); return; } int proportion = count1 / count2; for(int i = 0; i < count1 / proportion; i++) { printNTimes(num1, proportion); printNTimes(num2, 1); count1 -= proportion; count2--; } printNTimes(num2, count2); } int abs(int n) { return n < 0 ? -1 * n : n; } void printSolutionForNumber(int sum, int num) { int first = sum / num; int second; if(sum > 0) { second = first + 1; } else { second = first - 1; } int nSecond = abs(sum) % num; int nFirst = num - nSecond; optimallyPrintTwoNumbers(first, nFirst, second, nSecond); } void printSolution(int table[300], int size) { ll solutionSize = sumOfNumbersUpToN(size) + size; printf("%lli\n", solutionSize); for(int i = 0; i < size; i++) { printSolutionForNumber(table[i], i + 1); printf("%lli ", -INF); } printf("\n"); } bool isCorrect(int table[300], int size) { int last = table[0]; for(int i = 1; i < size; i++) { int current = table[i] / (i + 1); if(current > last) { return false; } last = current; } return true; } int main() { int n; scanf("%i", &n); int table[300]; for(int i = 0; i < n; i++) { scanf("%i", &table[i]); } if(isCorrect(table, n)) { printf("TAK\n"); printSolution(table, n); } else { printf("NIE\n"); } 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <algorithm> #define ll long long #define INF (ll)10e13 using namespace std; ll sumOfNumbersUpToN(ll n) { return n * (n + 1) / 2; } void printNTimes(int number, int n) { for(int i = 0; i < n; i++) { printf("%i ", number); } } void optimallyPrintTwoNumbers(int num1, int count1, int num2, int count2) { if(count1 < count2) { optimallyPrintTwoNumbers(num2, count2, num1, count1); return; } if(count2 == 0) { printNTimes(num1, count1); return; } int proportion = count1 / count2; for(int i = 0; i < count1 / proportion; i++) { printNTimes(num1, proportion); printNTimes(num2, 1); count1 -= proportion; count2--; } printNTimes(num2, count2); } int abs(int n) { return n < 0 ? -1 * n : n; } void printSolutionForNumber(int sum, int num) { int first = sum / num; int second; if(sum > 0) { second = first + 1; } else { second = first - 1; } int nSecond = abs(sum) % num; int nFirst = num - nSecond; optimallyPrintTwoNumbers(first, nFirst, second, nSecond); } void printSolution(int table[300], int size) { ll solutionSize = sumOfNumbersUpToN(size) + size; printf("%lli\n", solutionSize); for(int i = 0; i < size; i++) { printSolutionForNumber(table[i], i + 1); printf("%lli ", -INF); } printf("\n"); } bool isCorrect(int table[300], int size) { int last = table[0]; for(int i = 1; i < size; i++) { int current = table[i] / (i + 1); if(current > last) { return false; } last = current; } return true; } int main() { int n; scanf("%i", &n); int table[300]; for(int i = 0; i < n; i++) { scanf("%i", &table[i]); } if(isCorrect(table, n)) { printf("TAK\n"); printSolution(table, n); } else { printf("NIE\n"); } return 0; } |