PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642


题目描述

给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度就会减半。

给定N段绳子的长度,你需要找出它们能串成的绳子的最大长度。


输入格式:

每个输入包含 1 个测试用例。每个测试用例第 1 行给出正整数 N (2≤N≤10^4);第 2 行给出 N 个正整数,即原始绳段的长度,数字间以空格分隔。所有整数都不超过10^4


输出格式:

在一行中输出能够串成的绳子的最大长度。结果向下取整,即取为不超过最大长度的最近整数。


输入样例:

8
10 15 12 3 4 13 1 15

输出样例:

14

解题思路:

刚看到这一题的时候,就想到了数学里面的一个不等式的性质,两个数列的乘积问题正序和 > 乱序和 > 逆序和

这里我们首先分析一下,题目意思就是两根身子连成一根绳子时,两根身子的长度均减半。

有 n 根绳子,意味着我们需要打 n - 1 次结,第1次打结,第一第二根绳子减半成新绳子1,第二次打结,新绳子1和第三根减半形成新绳子2 , 其实就是 第一根绳子和第二根绳子减半两次,第三根绳子减半一次 , 依次类推 ......

最后的绳子其实就是,第一根和第二根减半 n - 1 次,第二根绳子减半 n - 2 次,第三根绳子 减半 n - 3 次 ...... 第

n 根绳子减半 1 次!其实就变成了 一个这样的问题,给你两个数列,数列 A 是输入的每段绳子的长度,数列 B 是 {1/2n-1 , 1/2n-1 ,1/2n-2 , ...... , 1/2 },就变成了我一开始所说的问题,求两个数列的乘积问题!

为了在实际操作中更好的实现,我们不如将 数列 B 的第一项变为 2/2n-1 . 然后就能用如下的代码解决求和问题了。(这可能是 PAT 甲级里最简单的 25 分题啦)

ans = num[0] ; // 对第一项特殊处理一下
for(int i = 0 ; i < n ; i++){
ans = (ans + num[i]) / 2 ;
}

完整代码:

#include<bits/stdc++.h>
using namespace std ;
double num[10010] ;
int main(){
int n ;
double ans = 0.0 ;
cin>> n ;
for(int i = 0 ; i < n ; i ++) cin >> num[i] ;
sort(num,num+n) ;
ans = num[0] ;
for(int i = 0 ; i < n ; i++) ans = (ans + num[i]) / 2 ;
cout<<(int)ans<<endl ;// 向下取整,可以直接用 double 转 int
return 0 ;
}

PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  2. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  3. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

  4. PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  5. PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

  6. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  7. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  8. PAT乙级:1070 结绳 (25分)

    PAT乙级:1070 结绳 (25分) 题干 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟 ...

  9. PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...

随机推荐

  1. deep copy & deep merge

    deep copy & deep merge JSON.parse(JSON.stringify(obj)); lodash https://lodash.com/docs/ https:// ...

  2. Service Worker in Action

    Service Worker in Action https://caniuse.com/#feat=serviceworkers Service Workers 1 W3C Candidate Re ...

  3. ES6 Arrow Function return Object

    ES6 Arrow Function return Object https://github.com/lydiahallie/javascript-questions/issues/220#issu ...

  4. React In Depth

    React In Depth React Component Lifecycle https://reactjs.org/docs/react-component.html https://react ...

  5. 蓝牙鼠标 & 罗技 M337

    蓝牙鼠标 & 罗技 M337 蓝牙鼠标,有哪些不需要适配器的 https://www.logitech.com.cn/zh-cn/product/bluetooth-mouse-m337 ht ...

  6. GitHub & JavaScript & Trending

    GitHub & JavaScript & Trending Trending JavaScript repositories on GitHub this week https:// ...

  7. js滚轮事件兼容写法

    /** * 简易的事件添加方法 */ define(function(require, exports, module) { exports.addEvent = (function(window, ...

  8. 「NGK每日快讯」12.21日NGK第48期官方快讯!

  9. NGK DeFi Baccarat怎么玩能赚钱?

    市面上大多数DeFi项目都是基于以太坊来开发的,除了吞吐量低.存储量小以及交易速度慢等问题以外,高额的Gas手续费将不少终端用户拒之门外. 基于此NGK.IO推出了低门槛的DeFi项目-- Bacca ...

  10. 翻译:《实用的Python编程》01_06_Files

    目录| 上一节(1.5 列表) | 下一节 (1.7 函数) 1.6 文件管理 大多数的程序需要从某处读取输入.本节讨论文件访问. 文件输入和输出 打开一个文件: f = open('foo.txt' ...