Problem E: One Stroke----暴力
来源:互联网 发布:骰子开奖网站源码 编辑:程序博客网 时间:2024/06/02 06:16
Problem E: One Stroke
Time Limit: 2 Sec Memory Limit: 1280 MBSubmit: 238 Solved: 44
[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
15 6 2 3 4 1 7
Sample Output
3
题目链接:http://acm.hzau.edu.cn/problem.php?cid=1029&pid=4题目的意思就是说有一棵二叉树,问你点权之和不超过k的最长路径长度是多少。
暴力就好,从下往上
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n,k;int a[1000010];int solve(int x){ int x1=x; int len=0; while(x1>-1){ if(a[x]-a[x1]>k){ break; } len++; if(x1==0) break; x1>>=1; } return len-1;}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); a[i]+=a[i/2]; } int ans=0; for(int i=n;i>=1;i--){ ans=max(ans,solve(i)); } if(ans) printf("%d\n",ans); else printf("-1\n"); }}
- Problem E: One Stroke----暴力
- one problem
- Codeforces699B One Bomb 暴力
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- Problem E
- 2017腾讯移动端开发暑期实习生面试总结(倒在二面)
- SQL基本语句
- 基于 Django1.10 文档的深入学习(3)—— models.py 之 FileField
- 我对linux驱动 输入子系统的理解
- C++容器
- Problem E: One Stroke----暴力
- Android ListView与SQLite综合使用(水果商品展示案例)
- STFT短时傅里叶变换的实现
- inno setup软件--教你如何制作一个漂亮的安装程序
- Spring 注解:@Autowired、@Controller、@Service 原理层面分析
- java运算符优先级
- Hibernate事务管理
- ios命令行自助打包-导入p12证书
- Android studio Day02-1