Given several ropes with the length of Li, each rope can only be cut into two parts or not. How many ropes with the same length can be generated at most?
Solution: first, it must be clear that each rope can contribute 2 to the result at most, because a rope can be cut into two parts at most, and the final result is valid only when the length is half, the other ropes are either 0 (the length is smaller than the enumeration length) or 1 (the length is greater than or equal to 2 times the enumeration length ). Therefore, we can enumerate the cut length. It is easy to say that the length must be half of the length of a rope, because the length of the rope may be odd, therefore, multiply all the lengths by 2 and then enumerate half of each rope. Use a tree array to initialize the prefix and, if the enumerated length is lx, the final result is the number from Lx to maxl plus the number of ropes with a length of 2 * lx.
The Code is as follows:
#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <cstdio>#include <set>using namespace std;const int MaxN = 200000;int N;int bit[MaxN+5];inline int lwb(int x) { return x & -x;}void add(int x, int val) { for (int i = x; i <= MaxN; i += lwb(i)) { bit[i] += val; }}int sum(int x) { int ret = 0; for (int i = x; i > 0; i -= lwb(i)) { ret += bit[i]; } return ret;}int cal(int x) { if (x <= MaxN/2) { int k = x << 1; return sum(k) - sum(k-1) + sum(MaxN) - sum(x - 1); } else { return sum(MaxN) - sum(x - 1); }}char vis[100005];int que[100005];int tail;int main() { int T, x; scanf("%d", &T); while (T--) { scanf("%d", &N); tail = 0; memset(bit, 0, sizeof (bit)); memset(vis, 0, sizeof (vis)); for (int i = 0; i < N; ++i) { scanf("%d", &x); add(x<<1, 1); if (!vis[x]) { vis[x] = 1; que[tail++] = x; } } int Max = 0; for (int i = 0; i < tail; ++i) { Max = max(Max, cal(que[i])); } printf("%d\n", Max); } return 0; }