华中农业大学第五届程序设计大赛 E One Stroke [枚举]【思维】

题目链接:http://acm.hzau.edu.cn/problem.php?id=1203
——————————————————————————————————————
1203: One Stroke
Time Limit: 2 Sec Memory Limit: 1280 MB
Submit: 274 Solved: 58
[Submit][Status][Web Board]
Description
There is a complete binary tree which includes n nodes. Each node on the tree has a weight w, each edge on the tree is directed from the parent node to the child node. Give you a pen, draw from the any node along the directed edge at one stroke. It is required that the sum of those drawn nodes’ s weight is no more than k. How many node can be drawn at most in one stroke?

Input
The first line input an positive integer T(1<=T<=10)indicates the number of test cases. Next, each case occupies two lines. The first line input two positive integers n(1<=n<=10^6) and k,(1<=k<=10^9)

The second line input n integers w(1<=w <=10^3),, indicate the weight of nodes from the first level of the tree and from left to right.

Output
For each test cases, output one line with the most number of nodes can be drawn in one stroke. If any node likes this doesn’t exists, output -1.

Sample Input
1
5 6
2 3 4 1 7
Sample Output
3

——————————————————————————————————————————

题目大意:
就是给你一棵树,每个树上有一个权值,让你找一条最长的从上向下的链,使其权值和不大于k,问你最长的长度是多少。

解题思路:

限制了链式从上到下的,
所以我们可以枚举每个节点所在的链,然后找最远的权值和不到k的点,就是到这个节点最长的长度,

找的时候用二分就好了,当时脑抽写了个BIT,其实发现数组就好了啊,。。

枚举节点在dfs遍历树的过程创建链就好了,

总复杂度 O(nlogn)

注:代码是 O(nlog2n) 把BIT换成普通数组就好了.

附本题代码
———————————————————————————————————————————

#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N   = 2e6+7;
//const int INF = (~(1<<31));

int read(){
    int x=0,f=1;char ch = getchar();
    while(ch<'0'||ch>'9') ch = getchar();
    while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch = getchar();}
    return x;
}
/*******************************************/
int n,k,mx;

int w[N];

LL sum[N];
#define lowbit(x) (x&-x)
void update(int i,int v){for(;i<=n;i+=lowbit(i))sum[i]+=v;}
LL getSum(int i){LL ans=0;for(;i;i-=lowbit(i))ans+=sum[i];return ans;}

void display(int m){
    for(int i=1;i<=m;i++)  printf("%d ",getSum(i)-getSum(i-1)); puts("");
}

void solve(int m){
//    display(m);
    int l=1,r=m,ans=-1,mid;
    while(l<=r){
        mid = r+l >> 1;
        if(getSum(m)-getSum(mid-1)<=k) ans = mid,r=mid-1;
        else l=mid+1;
    }
    if(ans!=-1)   mx = (mx>(m-ans+1))?mx:(m-ans+1);
//    printf("---- %d %d  %d \n",ans,m,mx);

}

void dfs(int rt,int dep){
    if(rt>n) return;
    update(dep,w[rt]);
    if(w[rt]<=k)
    solve(dep);
    dfs(rt<<1  ,dep+1);
    dfs(rt<<1|1,dep+1);
    update(dep,-w[rt]);
}

int main(){
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)      w[i]=read();
        mx = -1;dfs(1,1);
        printf("%d\n",mx);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值