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.
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 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
题意:
给定一块木板长度l,重量w,和上面放了n个木块。
下面n行为n个木块的信息,每个木块有放置的位置,和重量。
现在已知木板两个支点为-1.5和1.5位置。
要求出一个把木块拿下来的顺序。
保证木板一直是平衡的。输出这个顺序,如果做不到就输出Impossible。
解析:题目中有两个支点根据物理学
<--------------L---------------->
<----ll----><--3---><----rr---->
|-----------|---------|------------|
<--------rm-------->
<----------lm------->
ll,rr,rm,lm,都表示该长度下的力矩。
ll > lm 或者 rr > rm 时,会失去平衡。
如果木块是放在-1.5 到 1.5之间,那么木块只会使木板更平衡。
所以我们可以用贪心的思想。把中间的木块最后拿掉。接着我们把左边的木块和右边的木块分成两堆。进行力距从小到大的排序。然后把思路反过来想,可以转换成,把木块一个个放上去,仍然保持平衡。这时候。我们从力距小的开始放,可以使得木板最不可能失去平衡。
木块一般是交替放置的,这样左边往上不符合,就放右边,反之,当右边不符合,就放左边。这样贪心可极大减少时间。直到木块全部放置完毕就结束。
AC代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 35;
struct M{
int w,l;
double rr,rm,lm,ll;
}mr[N],ml[N];
double rr,rm,lm,ll;
int nm,nr,nl;
int out[N][2];
int l,w,n;
bool ok;
bool cmpr(M a,M b) {
return a.rr < b.rr;
}
bool cmpl(M a,M b) {
return a.ll < b.ll;
}
void dfs(double ll ,double lm ,double rm ,double rr ,int num,int numl,int numr) {
if(ok || ll > lm || rr > rm) {
return ;
}
if(num == n) {
ok = true;
return ;
}
out[num][0] = ml[numl].l;
out[num][1] = ml[numl].w;
if(numl < nl) {
dfs( ll + ml[numl].ll, lm, rm + ml[numl].rm, rr, num+1,numl+1,numr);
}
if(ok) {
return;
}
out[num][0] = mr[numr].l;
out[num][1] = mr[numr].w;
if(numr < nr) {
dfs( ll, lm + mr[numr].lm, rm, rr + mr[numr].rr, num+1,numl,numr+1);
}
}
int main() {
int lt,wt;
int cas = 1;
while( scanf("%d%d%d",&l,&w,&n) != EOF && (l || w || n)) {
nm = nr = nl = 0;
rr = ll = (l - 3)*(l - 3)*w / (4 * l);
rm = lm = (l + 3)*(l + 3)*w / (4 * l);
for(int i = 0; i < n; i++) {
scanf("%d%d",<,&wt);
lt *= 2;
if( abs(lt) <= 3) {
out[nm][0] = lt / 2;
out[nm][1] = wt;
nm++;
if(lt < 0) {
lm += (3 - abs(lt)) * wt;
rm += (3 + abs(lt)) * wt;
}else {
lm += (3 + abs(lt)) * wt;
rm += (3 - abs(lt)) * wt;
}
}else {
if(lt < 0) {
ml[nl].l = lt / 2;
ml[nl].w = wt;
ml[nl].ll = (abs(lt) - 3) * wt;
ml[nl].rm = (abs(lt) + 3) * wt;
nl++;
}else{
mr[nr].l = lt / 2;
mr[nr].w = wt;
mr[nr].rr = (abs(lt) - 3) * wt;
mr[nr].lm = (abs(lt) + 3) * wt;
nr++;
}
}
}
sort(ml,ml+nl,cmpl);
sort(mr,mr+nr,cmpr);
ok = false;
dfs(ll,lm,rm,rr,nm,0,0);
printf("Case %d:\n",cas++);
if(ok) {
for(int i = n - 1; i >= 0; i--) {
printf("%d %d\n",out[i][0],out[i][1]);
}
}else{
printf("Impossible\n");
}
}
return 0;
}