HDU 4476 HDOJ Cut the rope

http://acm.hdu.edu.cn/showproblem.php?pid=4476

题目意思:

  给定n条边,每条边可以选择切成两条,或者不切,询问最多能得到多少长度一样的边。

首先由于长度均为正整数,我们取长度为1,则答案至少为n,所以长度的取值应该小于等于 MaxLength/2.0 ,否则,答案不会超过n。

而对于给定的长度,所能得到的边的长度是可以直接算出来的,小于这个长度的能得到0条,等于这个长度2倍的能得到2条,其余的能得到一条。

为了避免0.5小数,代码里枚举的i是 长度的2倍。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=100000,N2=50000;
 5 int x,n,h[N+10],s[N+10],ans,tmp;
 6 int main()
 7 {
 8     int T;
 9     scanf("%d",&T);
10     while(T--)
11     {
12         scanf("%d",&n);
13         memset(h,0,sizeof(h));
14         for(int i=0;i<n;i++){
15             scanf("%d",&x);
16             h[x]++;
17         }
18         for(int i=1;i<=N2;i++)s[i]=s[i-1]+h[i];
19         ans=0;
20         for(int i=1;i<=N;i++){
21             tmp=h[i]-s[(i-1)>>1];
22             if(tmp>ans)ans=tmp;
23         }
24         printf("%d\n",ans+n);
25     }
26     return 0;
27 }
原文地址:https://www.cnblogs.com/hundundm/p/2798153.html