1125 Chain the Ropes (25 point(s))

【信息学奥赛】1125:矩阵乘法 阅读详情

1125 Chain the Ropes (25 point(s))

Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulting chain will be treated as another segment of rope and can be folded again. After each chaining, the lengths of the original two segments will be halved.

rope.jpg

Your job is to make the longest possible rope out of N given segments.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (2≤N≤10​4​​). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 10​4​​.

Output Specification:

For each case, print in a line the length of the longest possible rope that can be made by the given segments. The result must be rounded to the nearest integer that is no greater than the maximum length.

Sample Input:

8
10 15 12 3 4 13 1 15

Sample Output:

14

贪心策略、Huffman树

要使得绳子的长度尽可能长,那么要使得每一次消耗掉的尽可能短(每次选出的两根绳子的一半尽可能短),因此每次贪心地选择最短的两根绳子即可。可以采用优先队列,建立小顶堆即可。

数学证明

实际上,对于这道题,由于刚好是一半,也就是选出的最短的两根的一半再放回去以后,仍然是最短的。

假设c \geq b\geq a,必然有c \geq \frac{a+b}{2},因此实际上排序以后遍历即可。

Huffman

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int N;
priority_queue<int,vector<int> , greater<int> > Q;
int main(void){
	cin>>N;double x;
	for(int i=0;i<N;i++){
		cin>>x;
		Q.push(x);
	}
	while(Q.size()>=2){
		double a = Q.top();
		Q.pop();
		double b = Q.top();
		Q.pop();
		Q.push(0.5*(a+b));
	}
	cout<<(int)(Q.top()+0.5);
	return 0; 
} 

数学

#include<bits/stdc++.h>
using namespace std;
int main(){
    int N;
    scanf("%d",&N);
    double A[N];
    for(int i=0;i<N;++i)
        scanf("%lf",&A[i]);
    sort(A,A+N);//排序
    double sum=A[0];
    for(int i=1;i<N;++i)
        sum=sum/2+A[i]/2;//对折两根绳子
    printf("%d",(int)floor(sum));//向下取整
    return 0;
}

参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79795133

算法-矩阵乘法(信息学奥赛一本通-T1125)(包含源程序).rar 立即下载

相关推荐

在运行opencv时出现的两个问题:AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘

解决了opencv的两个问题:由问题一引出 AttributeError: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' cv2.error:OpenCV(4.1.2)C:\projects\opencvpython\opencv\modules\highgui\src\window.cpp:717:error: (-2:Unspecified error) The function is not implemented...

hu_yinghui的博客 3277

1125:矩阵乘法(C C++)

【题目描述】 计算两个矩阵的乘法。n×m阶的矩阵A乘以m×k阶的矩阵B得到的矩阵C 是n×k阶的,且C[i][j] = A[i][0]×B[0][j] + A[i][1]×B[1][j] + …… +A[i][m-1]×B[m-1][j](C[i][j]表示C矩阵中第i行第j列元素)。 【输入】 第一行为n, m, k,表示A矩阵是n行m列,B矩阵是m行k列,n, m, k均小于100。 然后先后输入A和B两个矩阵,A矩阵n行m列,B矩阵m行k列,矩阵中每个元素的绝对值不会大于1000。 【输出】 输出矩阵

ネロ・クラウディウス 1972

Seedance2.5本地部署指南:从环境配置到生产优化的完整实践

在 AI 内容生成领域,本地部署方案正逐渐成为开发者和技术团队关注的焦点。Seedance2.5 作为一款支持图像和视频生成的 AI 工具,其完全离线运行的能力解决了云端服务在数据隐私、使用成本和访问稳定性方面的痛点。对于需要处理敏感内容、频繁生成素材或希望完全掌控生成流程的技术人员来说,掌握本地部署方法具有实际价值。 本文将以 Seedance2.5 的本地部署为例,详细介绍从环境准备、依赖配置到运行验证的完整流程,同时深入分析关键参数配置、常见问题排查方法以及生产环境下的优化建议。通过实际部署过程,理解

weixin_33139275的博客 732

一本通-1125:矩阵乘法

矩阵乘法

m0_61990249的博客 1009

差分逻辑电平——LVDS、CML、LVPECL、HCSL互连

首先了解差分逻辑电平结构,也包括单端逻辑电平常见的逻辑电平_常用的逻辑电平-优快云博客注:ECL >> PECL >> LVPECL演变而来;QDR(Quad Data Rate):四倍数据倍率本文章只涉及差分逻辑电平:LVDS 、LVPECL、CML (高速)(最常用)在任何给定的系统设计中,必须设计驱动器侧和接收器侧之间的时钟逻辑转换。通过在它们之间增加衰减电阻和偏置电路来将一个差分时钟转换为其他类型的差分逻辑,来衰减摆幅电平并重新偏置共模输入接收器。

weixin_45365488的博客 4万+

Jetson系列目标检测及推流

使用硬件编码,gstreamer推rtsp流至平台

xiao__run的博客 2710

c++ 二维数组初始化_【题解二维数组】1125:矩阵乘法

1125:矩阵乘法时间限制: 1000 ms内存限制: 65536KB【题目描述】计算两个矩阵的乘法。n×m阶的矩阵A乘以m×k阶的矩阵B得到的矩阵C 是n×k阶的,且C[i][j] = A[i][0]×B[0][j] + A[i][1]×B[1][j] + …… +A[i][m-1]×B[m-1][j](C[i][j]表示C矩阵中第i行第j列元素)。【输入】第一行为n, m,...

weixin_39814088的博客 823

利用AWVS进行反制

awvs 10.5下载 http://www.32r.com/soft/12986.html

渗透之路,攻防之间皆学问 6654

1125:矩阵乘法

【题目描述】 计算两个矩阵的乘法。n×m阶的矩阵A乘以m×k阶的矩阵B得到的矩阵C 是n×k阶的,且C[i][j] = A[i][0]×B[0][j] + A[i][1]×B[1][j] + …… +A[i][m-1]×B[m-1][j](C[i][j]表示C矩阵中第i行第j列元素)。 【输入】 第一行为n, m, k,表示A矩阵是n行m列,B矩阵是m行k列,n, m, k均小于100。 ...

C_Dreamy的博客 938

mapbox进阶,dem数据转为等高面,加载转换后的等高面到地图

本文详细介绍如何基于mapbox-gl v3.*.* 版本,使用dem数据转为等高面,加载转换后的等高面到地图,亲测可用。希望能帮助到您。一起学习,加油!加油!

我哎GIS博客 407

使用 Python 和蒙特卡罗计算未来股价走势以及历史波动率和隐含波动率

蒙特卡罗模拟等工具使投资者能够洞察一系列潜在的未来。这远离了单纯的猜测,迈向了明智的、数据驱动的决策。

探索人工智能领域 707

1125 Chain the Ropes (25 point(s))

#include&lt;cstdio&gt; #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; using namespace std; vector&lt;int&gt; t; int n, res; int main() { scanf("%d", &amp;n); t.resize(n)..

徐伯莱 269

SAP-RPT-1 深度解读:面向企业表格数据的 rapid one

摘要: SAP在TechEd 2025大会上推出面向企业结构化数据的SAP-RPT-1(Relational Pretrained Transformer),旨在通过in-context learning直接预测表格数据中的缺失值,无需传统机器学习流程。与LLM不同,RPT专为业务表格设计,支持实时预测(如交货延迟、回款风险等)。提供Small(高吞吐)、Large(高精度)和开源版本,用户可通过无代码Playground(上传CSV并用[PREDICT]标记目标列)或Python API快速验证。典型场景

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 238

【PAT】A1125 Chain the Ropes (25point(s))

文章目录A1125 Chain the Ropes (25point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis Author: CHEN, Yue Organization: 浙江大学 Time Limit: 200 ms Memory Limit: 64 MB Code ...

Duke 227

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

1125 Chain the Ropes (25point(s)) - C语言 PAT 甲级

1. 用数组下标表示绳子长度 2. 用数组记录每种长度绳子的个数 3. 由最小值开始折叠绳子,完毕即为最大值 * 感谢 OliverLew (LuXu) 大佬: https://oliverlew.github.io/PAT/Basic/1070.html

有三只土豆 590

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PTA甲1125 Chain the Ropes (25 point(s))

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的优快云博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全名是 算法笔记 上级训练实战指南 //这本都是PTA的题解算法笔记 PS 今天也要加油鸭 题目原文 Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold

Huangpengyu123的博客 257

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

1125 Chain the Ropes (25point(s)) 贪心

题目 题目链接 思路 给很多段绳子,你要做的是将这些绳子串成一条绳子: 每次从绳子中取两段,每段绳子对折后套在一起,所以绳子的长度会减半,求最终能串成的绳子最大长度; 为了使最终绳子的长度最大,我们要尽可能使较长的绳子尽量少对折; 从小到大排序后,依次串联起来即可; 代码 #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack

hjyp的博客 168

PAT(A)1125 Chain the Ropes (25point(s))

Sample Input 8 10 15 12 3 4 13 1 15 Sample Output 14 思路: 贪心,然后求不大于的最小值。 代码 #include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstrin...

Sqssq的博客 159

PAT(A) 1125 Chain the Ropes (25 point(s))

贪心 注意:The result must be rounded to the nearest integer that is no greater than the maximum length. 即向下取整 见注释 #include<bits/stdc++.h> using namespace std; int main(void){ int N; scanf("%d", &N); vector<float> v(N); f.

Anpotan的博客 99

1125 Chain the Ropes(25 分)

1125 Chain the Ropes(25 分) Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by th...

galesaur_wcy 546

1125 Chain the Ropes

1125 Chain the Ropes Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figur...

ssf_cxdm的博客 140

PAT 1125 Chain the Ropes (25 分)贪心算法 结果向下取整(题目有歧义) 燚

Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulting chai...

永无止境 342

pat甲级 1125 Chain the Ropes (25分)

pat甲级 1125 Chain the Ropes (25分) note:哈夫曼树的变形,隐含条件在代码注释处 代码如下:(题目最后一句描述模糊而且后台测试用例结果不对,试了几次 才知道根本不用四舍五入,理解思想就好,复习一下哈夫曼树) #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n; cin>&

qq_44992782的博客 197

PAT 甲级 1125  Chain the Ropes

1125 Chain the Ropes (25 point(s)) Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as sho...

漫浸天空的雨色的备忘录~ 528

PAT_甲级_1125 Chain the Ropes (25point(s))(C++)【贪心算法】

目录 1,题目描述 题目大意 2,思路 3,AC代码 4,解题过程 第一搏 第二搏 第三搏 1,题目描述 Sample Input: 8 10 15 12 3 4 13 1 15 Sample Output: 14 题目大意 将给定的几段绳子进行合并,每次将两段和为一段,并且总长度减半。求最终的最大程度。 2,思路 贪心算法 给定的节点个数...

&再见萤火虫&的博客 301
上一篇: 1122 Hamiltonian Cycle (25 point(s))
下一篇: 1126 Eulerian Path (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值