1203: One Stroke
Time Limit: 2 Sec Memory Limit: 1280 MBSubmit: 247 Solved: 50
[ 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
题意:给你一棵完全二叉树,每个节点都有一个权值,然后要你从上往下找一条链(不一定包括根),使得链上权值的和<m,且节点数最大,求最大节点数。
先求一下前缀和,当到i节点时前缀和<m,更新cnt,当前缀和>m,倍增向上找
使得前缀和<m的节点,更新cnt。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <string> #include <stack> using namespace std; typedef long long ll; #define PI 3.1415926535897932 #define E 2.718281828459045 #define INF 0xffffffff//0x3f3f3f3f #define mod 997 const int M=1005; int n,m; int cnt; int sx,sy,sz; int mp[1000][1000]; int pa[M*10],rankk[M]; int head[M*6],vis[M*100]; int dis[M*100]; ll prime[M*1000]; bool isprime[M*1000]; int lowcost[M],closet[M]; char st1[5050],st2[5050]; int len[M*6]; typedef pair<int ,int> ac; //vector<int> g[M*10]; ll dp[50][1<<10][50]; int has[10500]; int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0}; int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; void getpri() { ll i; int j; cnt=0; memset(isprime,false,sizeof(isprime)); for(i=2; i<1000000LL; i++) { if(!isprime[i])prime[cnt++]=i; for(j=0; j<cnt&&prime[j]*i<1000000LL; j++) { isprime[i*prime[j]]=1; if(i%prime[j]==0)break; } } } struct node { int v,w; node(int vv,int ww) { v=vv; w=ww; } }; vector<int> g[100005]; char str[10005]; int ind[2550]; int bit[50]; int sum[1000005],fa[1000005][22],dep[1000005]; int a[1000005]; void dfs(int x,int fx)//基于二叉树前序遍历的求解 { if(x>n)return ; sum[x]=sum[fx]+a[x]; dep[x]=dep[fx]+1; //fa[i][j]表示i结点的第2^j祖先,当然其中 2^j (j =0...log(该点深度))倍祖先,1倍祖先就是父亲,2倍祖先是父亲的父亲......。 fa[x][0]=fx; //预处理当前节点的各级祖先 for(int i=1;i<=22;i++)fa[x][i]=fa[fa[x][i-1]][i-1];//i-1.。i-1==父亲的父亲 if(sum[x]<=m)cnt=max(cnt,dep[x]);//包含了从根往下的最大小于M的值,但是不确定不包含根的情况是否最大 else { int y=x; for(int i=21;i>=0;i--){ if(sum[x]-sum[fa[y][i]]<=m) { cnt=max(cnt,dep[x]-dep[fa[y][i]]);//求不包含根情况的最大解,并求出总体最大解 y=fa[y][i];//逐级向上找父亲 } } } dfs(x*2,x); dfs(x*2+1,x); } int main() { int i,j,k,t; int u,v; scanf("%d",&t); while(t--) { cnt=0; memset(dep,0,sizeof(dep)); memset(sum,0,sizeof(sum)); scanf("%d%d",&n,&m); for(i=1; i<=n; i++) scanf("%d",&a[i]); dfs(1,0); printf("%d\n",cnt==0?-1:cnt); } return 0; }