HDU-4476 Cut the rope (枚举、前缀和)

来源:互联网 发布:java工程师学什么专业 编辑:程序博客网 时间:2024/05/17 22:43

Cut the rope

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 761    Accepted Submission(s): 355


Problem Description
  Now we have some ropes. For each rope, you can cut it only once at any place, or you can also do nothing on it.
  If we cut these ropes optimally, how many ropes we can get at most with the same length?
 

Input
  There is an integer T (1 <= T <= 100) in the first line, indicates there are T test cases in total.
  For each test case, the first integer N (1 <= N <= 100000) indicates there are N ropes. And then there are N integers whose value are all in [1, 100000], indicate the lengths of these ropes.
 

Output
  For each test case, you should output one integer in one line, indicates that the number of ropes we can get at most with the same length if we cut these ropes optimally.
 

Sample Input
31 12 1 23 2 3 3
 

Sample Output
235
Hint
  For Sample 1, we can get two ropes with 0.5 unit length.  For Sample 2, we can get three ropes with 1 unit length.  For Sample 3, we can get five ropes with 1.5 unit length.

 

#include <bits/stdc++.h>using namespace std;#define maxn 400005int cnt[maxn], pre[maxn];int main(){    int T, n, x, ans, mx;    scanf("%d", &T);    while(T--){        ans = mx = 0;        memset(cnt, 0, sizeof(cnt));        memset(pre, 0, sizeof(pre));        scanf("%d", &n);        for(int i = 1; i <= n; ++i){            scanf("%d", &x);            x *= 2;            cnt[x]++;            mx = max(mx, x);        }        for(int i = mx; i >= 0; --i){            ans = max(ans, cnt[i] + cnt[i * 2] + pre[i + 1]);            pre[i] = pre[i + 1] + cnt[i];        }        printf("%d\n", ans);    }}/*题意:1e5根长度范围为1e5的绳子,每根绳子最多可以切一次,求最多可以得到的长度相同的绳子。思路:要发现一个点就是,最终数量最多的绳子的长度一定可以是0.5的倍数(虽然可能不是,但是不是的情况下是因为有多种切的方案,但满足0.5的倍数的方案一定存在且最优)。统计答案就是枚举最优方案得到的绳子的长度,它等于所有大于它的绳子的数量求和,其中2倍于它的计算两次,因为对半切会得到2根,其他的可以切出1根。*/