#include <cstdio>
#include <algorithm>
std::pair<int, int> weight_position[500100];
long long int sum[500100];
bool result[500100];
int N;
int find(long long int w) {
if (w <= sum[0]) {
return -1;
}
int lhs = 0;
int rhs = N-1;
while (lhs < rhs) {
int mid = (rhs+lhs+1) / 2;
if (weight_position[mid].first < w) {
lhs = mid;
} else {
rhs = mid-1;
}
}
return lhs;
}
int main() {
scanf("%d", &N);
for (int i=0; i<N; ++i) {
int x;
scanf("%d", &x);
weight_position[i] = std::make_pair(x, i);
}
std::sort(weight_position, weight_position+N);
sum[0] = weight_position[0].first;
for (int i=1; i<N; ++i) {
sum[i] = sum[i-1] + weight_position[i].first;
}
int pos = -1;
for (int i=0; i<N; ++i) {
while (1) {
long long int weight = ((pos<i)?weight_position[i].first:0) + ((pos>=0)?sum[pos]:0);
int npos = find(weight);
if (pos == npos) {
break;
} else {
pos = npos;
}
}
if ((i==N-1 && pos>=N-2) || (pos>=N-1)) {
result[weight_position[i].second] = true;
}
}
for (int i=0; i<N; ++i) {
printf("%c", result[i]?'T':'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 | #include <cstdio> #include <algorithm> std::pair<int, int> weight_position[500100]; long long int sum[500100]; bool result[500100]; int N; int find(long long int w) { if (w <= sum[0]) { return -1; } int lhs = 0; int rhs = N-1; while (lhs < rhs) { int mid = (rhs+lhs+1) / 2; if (weight_position[mid].first < w) { lhs = mid; } else { rhs = mid-1; } } return lhs; } int main() { scanf("%d", &N); for (int i=0; i<N; ++i) { int x; scanf("%d", &x); weight_position[i] = std::make_pair(x, i); } std::sort(weight_position, weight_position+N); sum[0] = weight_position[0].first; for (int i=1; i<N; ++i) { sum[i] = sum[i-1] + weight_position[i].first; } int pos = -1; for (int i=0; i<N; ++i) { while (1) { long long int weight = ((pos<i)?weight_position[i].first:0) + ((pos>=0)?sum[pos]:0); int npos = find(weight); if (pos == npos) { break; } else { pos = npos; } } if ((i==N-1 && pos>=N-2) || (pos>=N-1)) { result[weight_position[i].second] = true; } } for (int i=0; i<N; ++i) { printf("%c", result[i]?'T':'N'); } return 0; } |
English