uva 10123 No Tipping(逆向思维+力矩)

uva 10123 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重量M的木板上放n个木块,下n行表示木块放的位置和木块的重量,开始木板是处于平衡的,每次拿下一个木块,直到木块全部被取下,期间要求木板不发生偏移,如果可以完全取下,就输出去下木块的顺序(情况不唯一),不能完全取下就输出Impossible。

解题思路:题目要求是在一块一块拿走的前提下保持平衡,这里采用逆向思维会使问题变得更简单----放木块,在保持平衡的基础上一块一块放上木块,先放中间,然后放左边,当左边出现不平衡,就放右边,右边出现不平衡就再回来放左边,这样不断循环,直到两边都无法再放,就判断木块是否放完。再放之前要对木块进行分类(左边和右边)和排序(力矩乘重量)。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct block {
	int p, w;
};
block L[20], R[20], M[20];
int fL, fR, cL, cR, move;
int cmp1(block a, block b) {
	return (a.p + 3) * a.w > (b.p + 3) * b.w; //排序坐标-1.5左边的木块
}
int cmp2(block a, block b) {
	return (a.p - 3) * a.w < (b.p - 3) * b.w; //排序坐标1.5右边的木块
}
int check(block *b, int &cnt, int C) { //检查放上b(L/R)边的编号为cnt的木块后,b(L/R)边是否平衡
	if (cnt >= C) return 0;
	int temp1, temp2;
	temp1 = (b[cnt].p + 3) * b[cnt].w; //放上该木块对左右两边的影响
	temp2 = (b[cnt].p - 3) * b[cnt].w;
	if (fL + temp1 >= 0 && fR + temp2 <= 0) { //判断放上该木块是否会导致不平衡
		fL += temp1;
		fR += temp2;
		M[move++] = b[cnt];
		cnt++;
		return 1;
	}
	return 0;
}
int put(int c1, int c2) { //逆向思维:题目要求一个一个拿,保持平衡;这里采用逆向思维,在保持平衡的基础上,从零个开始一个一个放,最后逆序输出
	do {
		while (check(L, c1, cL));
		while (check(R, c2, cR));
	} while (check(L, c1, cL));
	if (c1 == cL && c2 == cR) {
		return 1;
	}
	return 0;
}
int main() {
	int l, w, n, Case = 1;
	while (scanf("%d %d %d", &l, &w, &n) == 3, (l || w || n)) {
		l *= 2;                     //乘以2,避免小数点
		fL = 3 * w;                 //左侧木板的重量是相对于右侧支点的,将右侧支点左侧的所有木板看成质点,坐标为0,所以力臂为3,F = (0 + 3) * w
		fR = -fL;                   //右边重量记为负,便于计算
		cL = cR = 0;
		move = 0;
		for (int i = 0; i < n; i++) {
			int pos, we;
			scanf("%d %d", &pos, &we);
			pos *= 2;
			if (pos >= -3 && pos <= 3) { //存在于中部的木块使得木板更稳固
				fL += (pos + 3) * we; // 左边的重量是相对右边的支点的,所以要加3
				fR += (pos - 3) * we; // 右边的重量是相对左边的支点的,所以要减3
				M[move].p = pos;
				M[move++].w = we;
			}
			else if (pos > 3) {
				R[cR].p = pos;
				R[cR++].w = we;
			}
			else {
				L[cL].p = pos;
				L[cL++].w = we;
			}
		}
		sort(L, L + cL, cmp1);
		sort(R, R + cR, cmp2);

		int flag = put(0, 0);
		printf("Case %d:\n", Case++);
		if (!flag) printf("Impossible\n");
		else {
			while (--move >= 0) {
				printf("%d %d\n", M[move].p / 2, M[move].w);
			}
		}
	}
	return 0;
}
时间: 2024-10-25 04:32:38

uva 10123 No Tipping(逆向思维+力矩)的相关文章

uva :10123 - No Tipping(dfs + 几何力距 )

题目:uva :10123 - No Tipping 题目大意:给出l, m, n 分别表示 长度为l 的杠杆, 重量为 m, 有n个物体放在上方.问每次从上面挑选一个物品移除,是否能使杠杆继续平衡.这个过程中都可以的话,就输出移除顺序(不唯一) 否则就输出 impossible ,一开始,这个杠杆就不平衡的情况也会是有的.因为杠杆也是有重量的. 解题思路; 1.这题先前我就不明白什么怎么样的情况下,双支撑点的杠杆不平横,后面看了别人的报告才明白. 首先  我这里有两个支撑点 (1, 2) 左边

uva 10123 - No Tipping dp 记忆化搜索

这题的题意是 在双脚天平上有N块东西,依次从上面取走一些,最后使得这个天平保持平衡! 解题: 逆着来依次放入,如果可行那就可以,记得得有木板自身的重量. /************************************************************************* > File Name: 10123.cpp > Author: opas > Mail: [email protected] > Created Time: 2016年10月22日

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

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 th

UVA 10391 Compound Words(哈希,逆向思维)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 /** 5 学习: 6 逆向思维:将复合词拆分成两个词,看是否在set中出现 7 string类 的 substr 8 9 */ 10 11 int main(){ 12 set<string> myset; 13 set<string>::iterator it; 14 string str; 15 int cnt =

Uva 10328 逆向思维

传送门: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1269 这个自己想的思路不知道哪里WA了,测试的都对,找不到数据.然后看了别人的思路感觉很棒棒啊. 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 原题中问出现连续至少k个H的情况,很难下手.我们可以试着将问题转化一下. 设dp[i][j]表示抛掷i个硬币

uva 10003 Cutting Sticks (DP)

uva 10003 Cutting Sticks Description 你的任务是替一家叫Analog Cutting Machinery (ACM)的公司切割木棍.切割木棍的成本是根据木棍的长度而定.而且切割木棍的时候每次只切一段.很显然的,不同切割的顺序会有不同的成本.例如:有一根长10公尺的木棍必须在第2.4.7公尺的地方切割.这个时候就有几种选择了.你可以选择先切2公尺的地方,然后切4公尺的地方,最后切7公尺的地方.这样的选择其成本为:10+8+6=24.因为第一次切时木棍长10公尺,

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te