#include<cstdio>
#include<vector>
using namespace std;
void check(vector<long long>& toys) {
int first = 0;
while (toys[first] == 0) {
first++;
}
int last = toys.size() - 1;
while (toys[last] == 0) {
last--;
}
for (int j = first; j <= last; j++) {
if (toys[j] == 0) {
printf("NIE\n");
return;
}
}
// printf("first: %d, last %d\n", first, last);
int ends = 0;
bool inEnd = false;
int j = first;
while (j < last) {
if (toys[j] > toys[j + 1] + 1) {
printf("NIE\n");
return;
}
if (toys[j] == toys[j + 1] + 1) {
if (j == last - 1 && (ends == 0 || (ends == 1 && inEnd))) {
printf("TAK\n");
} else {
printf("NIE\n");
}
return;
}
if (toys[j] == toys[j + 1]) {
toys[j + 1] -= toys[j];
j += 2;
if (!inEnd) {
ends++;
inEnd = true;
}
if (ends > 2) {
printf("NIE\n");
}
} else {
toys[j + 1] -= toys[j];
j += 1;
inEnd = false;
}
}
if (toys[last] == 0 || (toys[last] == 1 && (ends == 0 || inEnd))) {
printf("TAK\n");
} else {
printf("NIE\n");
}
}
int main() {
int days;
scanf("%d\n", &days);
vector<vector<long long> > toysDays;
for (int i = 0; i < days; i++) {
int toysNum;
scanf("%d\n", &toysNum);
vector<long long> toys;
long long visited;
scanf("%lld", &visited);
toys.push_back(visited);
for (int j = 1; j < toysNum; j++) {
scanf(" %lld", &visited);
toys.push_back(visited);
}
scanf("\n");
toysDays.push_back(toys);
}
for (int i = 0; i < days; i++) {
// printf("checking %d\n", i);
vector<long long> toys = toysDays[i];
check(toys);
}
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 | #include<cstdio> #include<vector> using namespace std; void check(vector<long long>& toys) { int first = 0; while (toys[first] == 0) { first++; } int last = toys.size() - 1; while (toys[last] == 0) { last--; } for (int j = first; j <= last; j++) { if (toys[j] == 0) { printf("NIE\n"); return; } } // printf("first: %d, last %d\n", first, last); int ends = 0; bool inEnd = false; int j = first; while (j < last) { if (toys[j] > toys[j + 1] + 1) { printf("NIE\n"); return; } if (toys[j] == toys[j + 1] + 1) { if (j == last - 1 && (ends == 0 || (ends == 1 && inEnd))) { printf("TAK\n"); } else { printf("NIE\n"); } return; } if (toys[j] == toys[j + 1]) { toys[j + 1] -= toys[j]; j += 2; if (!inEnd) { ends++; inEnd = true; } if (ends > 2) { printf("NIE\n"); } } else { toys[j + 1] -= toys[j]; j += 1; inEnd = false; } } if (toys[last] == 0 || (toys[last] == 1 && (ends == 0 || inEnd))) { printf("TAK\n"); } else { printf("NIE\n"); } } int main() { int days; scanf("%d\n", &days); vector<vector<long long> > toysDays; for (int i = 0; i < days; i++) { int toysNum; scanf("%d\n", &toysNum); vector<long long> toys; long long visited; scanf("%lld", &visited); toys.push_back(visited); for (int j = 1; j < toysNum; j++) { scanf(" %lld", &visited); toys.push_back(visited); } scanf("\n"); toysDays.push_back(toys); } for (int i = 0; i < days; i++) { // printf("checking %d\n", i); vector<long long> toys = toysDays[i]; check(toys); } return 0; } |
English