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. This twisting is called torque and is equal to the object's weight multiplied by its distance from the fulcrum (the angle of the lever also comes in, but that does not concern us here). If the object is to the left of the fulcrum, the direction of the torque is counterclockwise; if the object is to the right, the direction is clockwise. To compute the torque around a support, simply sum all the torques of the individual objects on the lever.

The challenge is to keep the lever balanced while adjusting the objects on it. Assume you have a straight, evenly weighted board, 20 meters long and weighing three kilograms. The middle of the board is the center of mass, and we will call that position 0. So the possible positions on the board range from -10 (the left end) to +10 (the right end). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. On the board are six packages, at positions -8, -4, -3, 2, 5 and 8, having weights of 4, 10, 10, 4, 7 and 8 kilograms, respectively as in the picture below.

Your job is to remove the packages one at a time in such a way that the board rests on both supports without tipping. The board would tip if the net torque around the left fulcrum (resulting from the weights of the packages and the board itself) were counterclockwise or if the net torque around the right fulcrum were clockwise. A possible solution 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 one described above. The input contains multiple cases. Each case starts with three integers: the length of the board (in meters, at least 3), the weight of the board (in kilograms) and n the number of packages on the board (n <= 20). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. The following n lines contain two integers each: the position of a package on board (in meters measured from the center, negative means to the left) and the weight of the package (in kilograms). A line containing three 0's ends the input. For each case you are to output the number of the case in the format shown below and then n lines each containing 2 integers, the position of a package and its weight, in an order in which the packages can be removed without causing the board to tip. If there is no solution for a case, output a single line Impossible. There is no solution if in the initial configuration the board is not balanced.

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

题意:给定一块木板长度l,重量w,和上面放了n个木块,下面n行为n个木块的信息,每个木块有放置的位置,和重量。现在已知木板两个支点为-1.5和1.5位置。要求出一个把木块拿下来的顺序。保证木板一直是平衡的。输出这个顺序,如果做不到就输出Impossible。。

思路:题目中有两个支点根据物理学,可以证明,当左支点左边的力距大于左支点右边的力距时,和右支点右边的力距大于右支点左边的力距时,会失去平衡。还有如果木块是放在-1.5 到 1.5之间,那么木块只会使木板更平衡。所以我们可以用贪心的思想。把中间的木块最后拿掉。

接着我们把左边的木块和右边的木块分成两堆。进行力距从小到大的排序。然后把思路反过来想,可以转换成,把木块一个个放上去,仍然保持平衡。这时候。我们从力距小的开始放,可以使得木板最不可能失去平衡。然后之前分成两堆是因为。木块一般是交替放置的。这样左边往上不符合,就放右边,反之,当右边不符合,就放左边。这样贪心可极大减少时间。

直到木块全部放置完毕就结束。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std; struct M {
int l;
int w;
double ll;
double lz;
double rr;
double rz;
} ml[35], mr[35]; bool cmpl(M a, M b) {
return a.ll < b.ll;
}
bool cmpr(M a, M b) {
return a.rr < b.rr;
}
int numm;
int numl, numr;
int l, w, n;
double ll, lz, rr, rz;
int a, b;
int judge;
int out[35][2]; void dfs(double ll, double lz, double rr, double rz, int num, int numll, int numrr) {
if (judge)
return;
if (ll > lz || rr > rz)
return;
if (num == n) {
judge = 1;
return;
}
out[num][0] = ml[numll].l;
out[num][1] = ml[numll].w;
if (numll != numl)
dfs(ll + ml[numll].ll, lz, rr, rz + ml[numll].rz, num + 1, numll + 1, numrr);
if (judge)
return;
out[num][0] = mr[numrr].l;
out[num][1] = mr[numrr].w;
if (numrr != numr)
dfs(ll, lz + mr[numrr].lz, rr + mr[numrr].rr, rz, num + 1, numll, numrr + 1);
}
int main()
{
int t = 1;
while (scanf("%d%d%d", &l, &w, &n) != EOF && l && w && n) {
memset(ml, 0, sizeof(ml));
memset(mr, 0, sizeof(mr));
memset(out, 0, sizeof(out));
numl = numr = numm = 0;
judge = 0;
ll = rr = (l - 3) * (l - 3) * w / (4.0 * l);
lz = rz = (l + 3) * (l + 3) * w / (4.0 * l);
l *= 2;
for (int i = 0; i < n; i ++) {
scanf("%d%d", &a, &b);
a *= 2;
if (abs(a) <= 3) {
out[numm][0] = a / 2;
out[numm++][1] = b;
if (a < 0) {
lz += ((3 - abs(a)) * b) * 1.0;
rz += ((3 + abs(a)) * b) * 1.0;
}
else {
lz += ((3 + abs(a)) * b) * 1.0;
rz += ((3 - abs(a)) * b) * 1.0;
}
}
else {
if (a < 0) {
ml[numl].l = a / 2;
ml[numl].w = b;
ml[numl].ll = ((abs(a) - 3) * b) * 1.0;
ml[numl ++].rz = ((abs(a) + 3) * b) * 1.0;
}
if (a > 0) {
mr[numr].l = a / 2;
mr[numr].w = b;
mr[numr].rr = ((abs(a) - 3) * b) * 1.0;
mr[numr ++].lz = ((abs(a) + 3) * b) * 1.0;
}
}
}
sort(ml, ml + numl, cmpl);
sort(mr, mr + numr, cmpr);
dfs(ll, lz, rr, rz, numm, 0, 0);
printf("Case %d:\n", t ++);
if (judge) {
for (int i = n - 1; i >= 0; i --)
printf("%d %d\n", out[i][0], out[i][1]);
}
else
printf("Impossible\n");
}
return 0;
}

UVA 10123 No Tipping (物理+贪心+DFS剪枝)的更多相关文章

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

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

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

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

  3. 【UVa】11882 Biggest Number(dfs+剪枝)

    题目 题目     分析 典型搜索,考虑剪枝. 统计一下联通分量. 1.本位置能够达到所有的点的数量加上本已有的点,还没有之前的结果长,直接返回. 2.当本位置能够达到所有的点的数量加上本已有的点与之 ...

  4. Sticks(UVA - 307)【DFS+剪枝】

    Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...

  5. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  6. AcWing:165. 小猫爬山(dfs + 剪枝)

    翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). 翰翰和达达只好花钱让它们坐索道下山. 索道上的缆 ...

  7. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  9. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

随机推荐

  1. [实战]MVC5+EF6+MySql企业网盘实战(14)——逻辑重构

    写在前面 上篇文章关于修改文件夹和文件名称导致的找不到物理文件的问题,这篇文章将对其进行逻辑的修改. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6 ...

  2. Centos7 ocsp功能验证

    转载:https://blog.csdn.net/tsh185/article/details/8107248 先按照Centos7创建CA和申请证书创建PKI所需要的文件 运行服务器端: opens ...

  3. 【WPF】RenderTransform和LayoutTransform

    布局系统 在WPF中,许多绘图任务通过使用变换(transform)可以变得更加简单——变换是通过不加通告地切换形状或元素使用的坐标系统来改变形状或元素绘制方式的对象.在WPF中,变换的一些类大多继承 ...

  4. 【C#】数据类型(sbyte,byte,short,ushort,int,uint,long,ulong和char。、、、)

    C#的数据类型可以分为3类:数值类型,引用类型,指针类型.指针类型仅在不安全代码中使用. 值类型包括简单类型(如字符型,浮点型和整数型等),集合类型和结构型.引用类型包括类类型,接口类型,代表类型和数 ...

  5. linux下php pcntl_fork模拟多线程

    开始用php写后台服务一段时间了.也是在这样的驱动下,不断的学习php语法,体验这一原来一直以为神秘且敬而远之的神奇语言的魅力.最初看php多线程的资料是为了提高程序的处理能力,充分发挥linux多任 ...

  6. libevent的作用或者说是有哪些功能

    1. 介绍 libevent是一个用来开发可扩展的网络服务器的事件通知函数库.当一个文件描述符上的特定事件发生或是一个超时时间到达后,libevent API提供一种执行回调函数的机制.而且,libe ...

  7. Can you find it? HDU - 2141 (二分查找)

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

  8. FastReport.Net使用:[26]数字格式

    1.数据包含固定格式的小数,自由格式的小数,以及字符串格式等四列数据.包含3行数据(1.2,1.23,1.234). 以下为Access数据视图和FastReport.Net报表设计器中的数据视图. ...

  9. apk安装 卸载 原理

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 复制 apk到 /data/app目录下,解压并扫描 安装包, 把 dex文件,保存到 ...

  10. [BZOJ4555][TJOI2016&HEOI2016]求和(分治FFT)

    4555: [Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 525  Solved: 418[Sub ...