UVA No Tipping
Problem A - No Tipping
As Archimedes famously observed, if you put an object on a lever arm,it will exert a twisting force around the lever's fulcrum. Thistwisting is called torque and is equal to the object's weightmultiplied by its distance from the fulcrum (the angle of the leveralso
comes in, but that does not concern us here). If the object is tothe left of the fulcrum, the direction of the torque iscounterclockwise; if the object is to the right, the direction isclockwise. To compute the torque around a support, simply sum all thetorques
of the individual objects on the lever.
The challenge is to keep the lever balanced while adjusting theobjects on it. Assume you have a straight, evenly weighted board, 20meters long and weighing three kilograms. The middle of the board isthe center of mass, and we will call that position 0. So
the possiblepositions on the board range from -10 (the left end) to +10 (the rightend). The board is supported at positions -1.5 and +1.5 by two equalfulcrums, both two meters tall and standing on a flat floor. On theboard are six packages, at positions -8,
-4, -3, 2, 5 and 8, havingweights of 4, 10, 10, 4, 7 and 8 kilograms, respectively as in thepicture below.

Your job is to remove the packages one at a time in such a way thatthe board rests on both supports without tipping. The board would tipif the net torque around the left fulcrum (resulting from the weightsof the packages and the board itself) were counterclockwise
or if thenet torque around the right fulcrum were clockwise. A possiblesolution to this problem is: first remove the package at position -4,then the package at 8, then -8, then 5, then -3 and finally 2.
You are to write a program which solves problems like the onedescribed above. The input contains multiple cases. Each case startswith three integers: the length of the board (in meters, at least3), the weight of the board (in kilograms) and
n thenumber of packages on the board (n <= 20).The board is supported at positions -1.5 and +1.5 by two equalfulcrums, both two meters tall and standing on a flat floor.The following
n lines contain two integers each: the position of apackage on board (in meters measured from the center,negative means to the left) and the weight of the package (inkilograms). A line containing three 0's ends the input. For eachcase you are to output
the number of the case in the format shownbelow and then n lines each containing 2 integers, theposition of a package and its weight, in an order in which the packagescan be removed without causing the board to tip. If there is nosolution for a case,
output a single line Impossible. Thereis no solution if in the initial configuration the board is notbalanced.
Sample input
20 3 6
-8 4
-4 10
-3 10
2 4
5 7
8 8
20 3 15
1 10
8 5
-6 8
5 9
-8 4
8 10
-3 10
-4 5
2 9
-2 2
3 3
-3 2
5 1
-6 1
2 5
30 10 2
-8 100
9 91
0 0 0Possible Output for sample input
Case 1:
-4 10
8 8
-8 4
5 7
-3 10
2 4
Case 2:
1 10
8 5
-6 8
5 9
-8 4
8 10
-3 10
-4 5
2 9
-2 2
3 3
-3 2
5 1
-6 1
2 5
Case 3:
Impossible 我刚開始是直接DFS,果断超时了。然后參考了别人的算法,例如以下:(抄自hackerwin7的专栏) 题解:两种方法。 一、DFS剪枝+物理力矩+贪心二、状态压缩+记忆话搜索 ... 一:将放上去的砝码做一个逆变换,即初始化为杠杆上没有砝码。我们一个一个往上放。使其过程中不发生側翻的情况。 側翻的情况:当左边点的左端的力矩大于左边点的右端的力矩,会往左側翻。当右边点的右端的力矩大于右边点的左端的力矩。会往右側翻。 注意点: 1、处理时将坐标放大两倍。使1.5->3省略小数的处理。 2、处于-1.5~+1.5之间的先放,能够证明其不会影响杠杆的側翻(剪枝)。 3、在此区间以外的分为两组。-L~-1.5为左组 +1.5~+L为右组,将两组按相应点力矩升序排列,放砝码时,若放最大的,非常可能会引起側翻, 所以尽可能维持平衡,从影响度最小的開始放,再放大的,这样到后面这些若干小的能够缓和还有一側大的砝码的側翻影响效应,从而尽可能维持平衡 (若实在无法维持平衡 那就是impossible)。所以整个DFS过程能够贪心地去一边的砝码,比方左边的按升序先放砝码,一直到左边再放一个就 会引起側翻后,再到右边的按升序放,用来抑制左边会側翻的情况。若左边还是側翻继续往右边放。直到左边能够放为止,注意左右两组砝码都是有序 的,这样省去了无序砝码的遍历查找时间(剪枝)。一直维持这个过程。直到全部砝码都放上去,也没有出现側翻的条件就ok;假设出现这种情况,无 论放左边还是放右边的都有側翻情况发生,那么这一种情况就是impossible的。(感觉这种模式与快排的算法模式有点相似。 AC的代码例如以下:
UVA No Tipping的更多相关文章
- uva :10123 - No Tipping(dfs + 几何力矩 )
option=com_onlinejudge&Itemid=8&page=show_problem&category=109&problem=1064&mosm ...
- UVA 10123 No Tipping (物理+贪心+DFS剪枝)
Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm, it will ...
- uva 10123 - No Tipping dp 记忆化搜索
这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /********************** ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
随机推荐
- MySql 安装及0基础使用具体解释
1. sudo apt-get install mysql-server, input administrator password , '123' 2. enter mysql promot in ...
- 用xml建立仓库的逻辑层的操作
package com.repositoryclient.xml; import java.io.FileNotFoundException; import java.io.FileOutputStr ...
- Spark技术内幕:Stage划分及提交源代码分析
当触发一个RDD的action后.以count为例,调用关系例如以下: org.apache.spark.rdd.RDD#count org.apache.spark.SparkContext#run ...
- WPF界面设计技巧(5)—自定义列表项呈现内容
原文:WPF界面设计技巧(5)-自定义列表项呈现内容 接续上次的程序,稍微改动一下原有样式,并添加一个数据模板,我们就可以达成下面这样的显示功能: 鼠标悬停于文件列表项上,会在工具提示中显示图像缩略图 ...
- 十年linux命令总结
十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令 ...
- hdu 5187 高速幂高速乘法
http://acm.hdu.edu.cn/showproblem.php?pid=5187 Problem Description As one of the most powerful brush ...
- 【C/C++】BinarySearch
/* * BinarySearch.c * * Created on: 2014年6月25日 * Author: wenbo */ #include <stdio.h> int binar ...
- tarjan算法(割点/割边/点连通分量/边连通分量/强连通分量)
tarjan算法是在dfs生成一颗dfs树的时候按照访问顺序的先后,为每个结点分配一个时间戳,然后再用low[u]表示结点能访问到的最小时间戳 以上的各种应用都是在此拓展而来的. 割点:如果一个图去掉 ...
- Shiro学习笔记(5)——web集成
Web集成 shiro配置文件shiroini 界面 webxml最关键 Servlet 測试 基于 Basic 的拦截器身份验证 Web集成 大多数情况.web项目都会集成spring.shiro在 ...
- Cocos性能优化工具的开发介绍Visual Studio内存泄漏检测工具——Visual Leak Detector
然后,Windows下有什么好的内存泄漏检測工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检測功能.我们能够使用第三方工具Visual Leak Detector(下面简 ...