Hzau 1203 One Stroke (dfs+ binary Or double pointer)

Source: Internet
Author: User




Test instructions: Give you a binary tree, point a bit of power, every time to the left or go to the right, to find the longest way to go, and point right and less than K;

Idea: The official solution, ruler take, my writing, two points on the tree,

For a chain, enumerate each point as the end point, vector the point to the root node prefix and, two minutes can be;

See Code;

  

Learn from the user code.

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cmath> #include <string> #include <queue> #include <algorithm> #include <stack> #include <cstring> #include <vector> #include <list> #include <set> #include <map> using namespace
Std
#define LL Long Long #define PI (4*atan (1.0)) #define EPS 1e-4 #define BUG (x) cout<< "Bug" <<x<<endl;
const int n=1e6+10,m=1e6+10,inf=2147483647;
 
const LL inf=1e18+10,mod=2147493647;
Array size int n,ans,k,a[n];
vector<int>v;
    void Dfs (int x) {int s=0,t=v.size ()-1;
    int e=v.size () -1,ansq=-1;
        while (s<=e) {int mid= (s+e) >>1;
            if (v[t]-v[mid]<=k) {ansq=mid;
        E=mid-1;
    } else s=mid+1;
    } if (v[t]<=k) Ans=max (ans,t+1);
    else Ans=max (ANS,T-ANSQ);
    int Z=v[v.size ()-1]; if (x*2<=n) {v.push_back (z+a[x<<1]);
        DFS (X&LT;&LT;1);
    V.pop_back ();
        } if (x*2+1<=n) {v.push_back (z+a[x<<1|1]);
        DFS (X&LT;&LT;1|1);
    V.pop_back ();
    }} int main () {int T;
    scanf ("%d", &t);
        while (t--) {ans=0;
        V.clear ();
        scanf ("%d%d", &n,&k);
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
        V.push_back (a[1]);
        DFS (1);
        if (ans) printf ("%d\n", ans);
    else printf (" -1\n");
} 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.