HDOJ 1330 Deck(叠木块-物理题啊!贪心算法用到了一点)

简介: HDOJ 1330 Deck(叠木块-物理题啊!贪心算法用到了一点)

Problem Description

A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table’s edge, and half the length of the card hangs over the edge of the table. If the card hung any further out, with its center of gravity off the table, it would fall off the table and flutter to the floor. The same reasoning applies if the card were placed on another card, rather than on a table.


Two playing cards can be arranged, carefully, with short edges parallel to table edges, to extend 3/4 of a card length beyond the edge of the table. The top card hangs half a card length past the edge of the bottom card. The bottom card hangs with only 1/4 of its length past the table’s edge. The center of gravity of the two cards combined lies just over the edge of the table.


Three playing cards can be arranged, with short edges parallel to table edges, and each card touching at most one other card, to extend 11/12 of a card length beyond the edge of the table. The top two cards extend 3/4 of a card length beyond the edge of the bottom card, and the bottom card extends only 1/6 over the table’s edge; the center of gravity of the three cards lines over the edges of the table.


If you keep stacking cards so that the edges are aligned and every card has at most one card above it and one below it, how far out can 4 cards extend over the table’s edge? Or 52 cards? Or 1000 cards? Or 99999?


Input

Input contains several nonnegative integers, one to a line. No integer exceeds 99999.


Output

The standard output will contain, on successful completion of the program, a heading:


# Cards Overhang


(that’s two spaces between the words) and, following, a line for each input integer giving the length of the longest overhang achievable with the given number of cards, measured in cardlengths, and rounded to the nearest thousandth. The length must be expressed with at least one digit before the decimal point and exactly three digits after it. The number of cards is right-justified in column 5, and the decimal points for the lengths lie in column 12.


Sample Input

1

2

3

4

30


Sample Output

The line of digits is intended to guide you in proper output alignment, and is not part of the output that your solution should produce.


12345678901234567


# Cards  Overhang
    1     0.500
    2     0.750
    3     0.917
    4     1.042
   30     1.997


物理题!还是一个英语题!

题意:就是在桌面上铺一样的卡片,求最多能够超过桌面多少。


理想化条件:问题中几个理想化的用词—“水平桌面”、“完全相同”、“质量均匀”、“长方体”


物理学原理: 若物体的重心位于支持面的上方,当重力作用线在支持面内,重力矩与支持力矩平衡,物体处于平衡状态;反之,重力矩与支持力矩同方向,物体处于不平衡状态;临界的平衡条件为重力的作用线恰好经过支持面的边缘。

image.png

贪心法求解: 设每个卡片长度均为L,重力均为G。将卡片从上到下编号,依次为1、2、3、„、n。记第k(k=1,2,3,„,n)块卡片相对其下方支持物的最大允许伸出量为x.k(n号卡片的支持物为水平桌面,其余卡片的支持物为其下方的木块),第k块卡片及其上方所有卡片的公共重心到第k块卡片另一端的距离为g.k。显然,1号卡片的伸出量为及公共重心为


image.png


现加入2号卡片,1号卡片平衡的临界条件为其重力作用下恰好位于2号卡片对其支持面的边缘。公共重心的坐标是其各组成部分的重心坐标的加权平均,在图示位置下两个卡片的公共重心到2号卡片最左端的距离为


image.png


image.png


加入第k块卡片后的公共重心到第k块卡片最左端的距离为:

image.png



第t块木块的最大允许伸出量为:

image.png



总伸出量为:

image.png


贪心算法简介:

所谓贪心算法,就是指在对问题求解时,总是做出在当前看来是最好的选择。在本题中,每一步加入一个木块,1号木块的最大允许伸出量为L/2;加入2号木块后,两个木块的公共重心与1号木块的伸出量有关,那么2号木块的最大允许伸出量也就受1号木块的最大允许伸出量所限。由于贪心算法并非从整体最优上加以考虑,使得贪心算法并非对所有的问题都能求得最佳解;但在大多数情况下,贪心算法都能得出满意解。

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[] dabiao = new double[100010];
        double sum=0.0;
        dabiao[1]=0.5;
        for(int i=2;i<=100000;i++){
            dabiao[i]=dabiao[i-1]+1.0/i/2.0;
        }
        System.out.println("# Cards  Overhang");
        while(sc.hasNext()){
            int n = sc.nextInt();
            System.out.printf("%5d%10.3f\r\n",n,dabiao[n]);
        }
    }
}


目录
相关文章
|
3月前
|
算法 搜索推荐
数据结构和算法——表排序(算法概述、物理排序、复杂度分析,包含详细清晰图示过程)
数据结构和算法——表排序(算法概述、物理排序、复杂度分析,包含详细清晰图示过程)
26 0
|
4月前
|
算法 JavaScript 前端开发
游戏物理系统 - 如何在JavaScript中实现基本的碰撞检测算法?
在JavaScript中实现2D矩形碰撞检测,常用AABB方法,适合简单游戏。创建Rectangle类,包含位置和尺寸属性,并定义`collidesWith`方法检查两矩形是否相交。通过比较边界位置判断碰撞,当四条边界条件均满足时,认定发生碰撞。基础算法适用于初级需求,复杂场景可采用更高级的碰撞检测库。
39 1
|
机器学习/深度学习 算法 决策智能
方程就是二叉树森林?遗传算法从数据中直接发现未知控制方程和物理机理
方程就是二叉树森林?遗传算法从数据中直接发现未知控制方程和物理机理
|
机器学习/深度学习 传感器 算法
基于雾凇冰物理现象优化算法RIME求解单目标优化问题附matlab代码
基于雾凇冰物理现象优化算法RIME求解单目标优化问题附matlab代码
|
传感器 机器学习/深度学习 算法
【WSN】基于LGNDO算法实现传感器物理路由优化附matlab代码
【WSN】基于LGNDO算法实现传感器物理路由优化附matlab代码
|
机器学习/深度学习 编解码 算法
【物理应用】基于粒子群优化算法实现瞬变电磁法视电阻率反演附matlab代码
【物理应用】基于粒子群优化算法实现瞬变电磁法视电阻率反演附matlab代码
【物理应用】基于粒子群优化算法实现瞬变电磁法视电阻率反演附matlab代码
|
算法 Java
Java反转IC卡16进制物理卡号算法
IC卡的物理卡号一般由16进制的字符组成,日常常见的有直读卡号和反转后的卡号,本文用一个简单的算法获取反转后的卡号。
963 0
|
16天前
|
算法 BI Serverless
基于鱼群算法的散热片形状优化matlab仿真
本研究利用浴盆曲线模拟空隙外形,并通过鱼群算法(FSA)优化浴盆曲线参数,以获得最佳孔隙度值及对应的R值。FSA通过模拟鱼群的聚群、避障和觅食行为,实现高效全局搜索。具体步骤包括初始化鱼群、计算适应度值、更新位置及判断终止条件。最终确定散热片的最佳形状参数。仿真结果显示该方法能显著提高优化效率。相关代码使用MATLAB 2022a实现。
|
16天前
|
算法 数据可视化
基于SSA奇异谱分析算法的时间序列趋势线提取matlab仿真
奇异谱分析(SSA)是一种基于奇异值分解(SVD)和轨迹矩阵的非线性、非参数时间序列分析方法,适用于提取趋势、周期性和噪声成分。本项目使用MATLAB 2022a版本实现从强干扰序列中提取趋势线,并通过可视化展示了原时间序列与提取的趋势分量。代码实现了滑动窗口下的奇异值分解和分组重构,适用于非线性和非平稳时间序列分析。此方法在气候变化、金融市场和生物医学信号处理等领域有广泛应用。
|
17天前
|
资源调度 算法
基于迭代扩展卡尔曼滤波算法的倒立摆控制系统matlab仿真
本课题研究基于迭代扩展卡尔曼滤波算法的倒立摆控制系统,并对比UKF、EKF、迭代UKF和迭代EKF的控制效果。倒立摆作为典型的非线性系统,适用于评估不同滤波方法的性能。UKF采用无迹变换逼近非线性函数,避免了EKF中的截断误差;EKF则通过泰勒级数展开近似非线性函数;迭代EKF和迭代UKF通过多次迭代提高状态估计精度。系统使用MATLAB 2022a进行仿真和分析,结果显示UKF和迭代UKF在非线性强的系统中表现更佳,但计算复杂度较高;EKF和迭代EKF则更适合维数较高或计算受限的场景。