PAT A1125 Chain the Ropes
来源:互联网 发布:sql 双竖线 用法 编辑:程序博客网 时间:2024/06/05 14:35
/*---------------------------------------------------------------PAT A1125 Chain the Ropes题意:绳子环环相扣,各段(halved)减半,求可能的最大长度;题解:权为 1/2的赫尔曼树,取最小的两段配对成新的节点;练习一下快速排序。---------------------------------------------------------------*/#include<iostream>#include<fstream>#include<math.h>using namespace std;//快速排序复杂度O(nlog(n))void Quick_Sort(int (&Sequence)[10001], const int& Left, const int& Right){if (Left >= Right) return;int Low = Left, High = Right; Sequence[0] = Sequence[Low]; //取0位为枢轴while (Low < High){while (Low < High && Sequence[High] >= Sequence[0])//左边 --High;Sequence[Low] = Sequence[High];while (Low < High && Sequence[Low] <= Sequence[0])//右边++Low;Sequence[High] = Sequence[Low];}Sequence[Low] = Sequence[0]; //枢轴归位int Center = Low;Quick_Sort(Sequence, Left, Center - 1); //递归快速排序Quick_Sort(Sequence, Center + 1, Right); //类似于递归建树,二分法递归查询}int main(){#ifdef _DEBUGifstream cin("Input.txt");ofstream cout("Output.txt");#endifint Number;cin >> Number;int Segments[10001];for (int i = 1; i <= Number; ++i)cin >> Segments[i];Quick_Sort(Segments, 1, Number);//for (int i = 1; i <= Number; ++i) //查看快速排序是否正确//cout << Segments[i] << ' ';double Sum = Segments[1];for (int i = 2; i <= Number; ++i)Sum = 0.5 * (Sum + Segments[i]);cout << floor(Sum); //取整输出#ifdef _DEBUGcin.close();cout.close();#endifreturn 0;}
为什么没有(preview)预览?
0 0
- PAT A1125 Chain the Ropes
- PAT A1125. Chain the Ropes (25)(哈夫曼树)
- PAT--1125. Chain the Ropes
- PAT 1125. Chain the Ropes (25)-甲级
- PAT 1125. Chain the Ropes (25)
- pat 甲1125. Chain the Ropes (贪心)
- PAT甲级.1125. Chain the Ropes (25)
- PAT甲级 1125. Chain the Ropes (25)
- PAT 甲级 1125. Chain the Ropes (25)
- Chain the Ropes
- PAT (Advanced Level) Practise 1125 Chain the Ropes (25)
- PAT (Advanced Level) 1125. Chain the Ropes (25)
- PAT (Advanced Level) 1125. Chain the Ropes (25) 解题报告
- 2017.3.4 pat甲级B题Chain the Ropes
- PAT (Advanced Level) Practise 1125 Chain the Ropes (25)
- 【PAT】【Advanced Level】1125. Chain the Ropes (25)
- 1125. Chain the Ropes (25)
- 1125. Chain the Ropes 解析
- MySQL--忘记密码后跳过检查修改密码
- C++ 基础(六) —— 函数(1)
- [POJ2823]Sliding Window(单调队列)
- Android 属性动画(Property Animation) 完全解析 (上)
- 怎样解决Myeclipse中运行jsp乱码问题,亲测有效(虽然是个小问题但是为了大家不被网络上的一些乱七八糟的回答坑)不是改什么windows-propories-...............
- PAT A1125 Chain the Ropes
- java 编译运行
- Activiti工作流-入门
- windows下安装git教程,github注册教程,github版本控制,分支管理,
- Binder驱动(二)
- 关于怎样解决eclipse打开时出现的Failed to load the JNIshared library亲测有效
- C语言结构体字节对齐
- tomcat闪退问题解决方案
- Activity的缓存方法