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 0

Possible 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的更多相关文章

  1. uva :10123 - No Tipping(dfs + 几何力矩 )

    option=com_onlinejudge&Itemid=8&page=show_problem&category=109&problem=1064&mosm ...

  2. UVA 10123 No Tipping (物理+贪心+DFS剪枝)

    Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm, it will ...

  3. uva 10123 - No Tipping dp 记忆化搜索

    这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /********************** ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. 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 ...

随机推荐

  1. TopCoder中插件的用法

    今天弄了一下TopCoder的插件,发现真的很好很强大,插件的下载地址为 : http://community.topcoder.com/tc?module=Static&d1=applet& ...

  2. 使用Visual Studio 寻找App性能瓶颈

    使用Visual Studio 寻找App性能瓶颈 最后更新日期:2014-05-05 阅读前提: 环境:Windows 8.1 64bit英文版,Visual Studio 2013 专业版Upda ...

  3. [IOS]UIWebView实现保存页面和读取服务器端json数据

    如何通过viewView保存访问过的页面?和如何获取并解析服务器端发送过来的json数据?通过一个简单的Demo来学习一下吧! 操作步骤: 1.创建SingleViewApplication应用,新建 ...

  4. Eureka 的 Application Client client的执行演示样例

            上篇以一个 demo 演示样例介绍了 Eureka 的 Application Service 客户端角色.今天我们继续了解 Eureka 的 Application Client 客 ...

  5. HTML5----响应式(自适应)网页设计

    第一步:在网页代码的头部,加入一行viewport元标签 <meta name="viewport" content="width=device-width, in ...

  6. JS 在 HTML 中做加减乘除

    <script type="text/javascript"> function fun(t){ var fa = parseInt(document.getEleme ...

  7. Oracle SQL Developer使用

    原文 Oracle SQL Developer使用 比较:   Plsqldev:第三方的,启动软件时快,执行sql查询时很慢 Sqldeveloper:oracle公司的,启动软件时慢,执行sql查 ...

  8. 游戏碰撞OBB算法(java代码)

    业务需求      游戏2D型号有圆形和矩形,推断说白了就是碰撞检测 :      1.圆形跟圆形是否有相交      2.圆形跟矩形是否相交       3.矩形和矩形是否相交           ...

  9. win10无法新建文件夹怎么办 win10右键新建菜单设置方法

    有朋友安装了win10系统后发现右键菜单中没有新建项,而平时使用新建 - 文件夹项的机率很高.如何才能恢复桌面右键菜单中的新建项呢? 右键点击桌面空白处,在右键菜单中发现没有新建项: 桌面右键菜单没有 ...

  10. Java 并发专题 : Semaphore 实现 互斥 与 连接池

    继续并发方面的知识.今天介绍Semaphore,同样在java.util.concurrent包下. 本来准备通过例子,从自己实现到最后使用并发工具实现,但是貌似效果并不是很好,有点太啰嗦的感觉,所有 ...