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. 已知直线上的两点 A(x1, y1), B(x2, y2) 和另外一点 C(x0, y0),求C点到直线的距离。

    数学知识太差,一点点积累,高手勿喷. 1. 先求出AB向量 a = ( x2-x1, y2-y1 ) 2. 求AB向量的单位方向向量 b = √((x2-x1)^2 + (y2-y1)^2)) a1 ...

  2. C++基础知识---static const初始化成员变量

    为了限制常数的范围class中.你必须要做出成为class成员:而要确保这是丝毫不亚于有一个恒定的实体.你必须要做出成为static员: Class Gameplayer { Private: Sta ...

  3. Linux 单用户模式的使用

    在进入系统启动菜单时,选择kernel,输入"e"后进入,在kernel开头的启动项后输入(空格) /single或者 / 1,然后输入"b"重新启动机器,此时 ...

  4. Backbone.js 为复杂Javascript应用程序提供模型(models)、集合(collections)、视图(views)的结构

    Backbone.js 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和 自定义事件:集合附有可枚举函数 ...

  5. httl开源JAVA模板引擎,动态HTML页面输出

    HTTL(Hyper-Text Template Language)是一个适用于HTML输出的开源JAVA模板引擎,适用于动态HTML页面输出,可用于替代JSP页面,它的指令类似于Velocity. ...

  6. 谈Web应用系统的可维护性

           每一个软件开发人员都十分清楚, 当软件构建得越来越复杂时, 可维护性就成了一个很突出的问题. 如何在构造软件系统的过程中始终保持可控制的可维护性呢?          一. 整体组织   ...

  7. hdu 3790 (最短路径问题dijkstra)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  8. VMware workstation 安装错误提示1021解决方法

    Failed to create the requested registry key Key: Installer Error: 1021 解决方法:删除注册表--HKEY_LOCAL_MACHIN ...

  9. 游戏开发实验室的内部讲座总结----c++

     第三节  动态内存分配new和delete 经过调试设置断点,发现new 函数事实上还是调用的malloc函数. 第四节  引用 一个变量是能够有多个引用的,引用也是能够传递的.  常量是不能有 ...

  10. android 自己定义开关(SwitchButton)

    近期心血来潮,写了一个自己定义仿iPhone的开关. 有须要的同学能够来下载啦.支持点击自己主动滚动,速率能够自己依据须要改动.触摸滚动,大小自己定义,支持改动样式.就不录制动画,就上传了两张图给大家 ...