HDU-4476 cut the rope tree Array

Source: Internet
Author: User

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;    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.