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?
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.
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
235HintFor 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根。*/
阅读全文
0 0
- HDU-4476 Cut the rope (枚举、前缀和)
- hdu 4476 Cut the rope
- hdu 4476 Cut the rope
- HDU 4476 Cut the rope
- hdu 4476 Cut the rope
- cocos2d cut the rope
- NYOJ Cut the rope
- hdu 5452 Minimum Cut(树链剖分+差分前缀和)
- hdu 5163 前缀和+枚举
- Cut The Rope正式登录pc
- 前缀和,枚举+递推(Average,HDU 5353)
- hdu 4344 Mark the Rope
- hdu 4344 Mark the Rope
- HDU 4344 Mark the Rope
- 带你一起分析cut the rope(切绳子游戏)中绳子的制作方法
- hdu 4344 Mark the Rope (Miller Rabbin + Pollard rho)
- html5在线网页游戏-Cut The Rope割绳子免费版
- cut the rope HTML 5版本背后的开发故事
- 模糊控制简介及数学基础 (模糊集合、隶属函数)
- Ural 1003 Parity(并查集)
- leetcode.array--42. Trapping Rain Water
- 算法设计与分析(屈婉玲)网络课学习笔记(一)
- JavaScript是如何实现继承的(六种方式)
- HDU-4476 Cut the rope (枚举、前缀和)
- 前端考试知识点归纳总结
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
- IP地址与Long型整数的相互转换
- HDU
- Accelerated C++之计算学生成绩3
- JAVA学习笔记
- mysql 更改数据库字符编码的方法
- 开源分享-桌面版坦克大战