uva 10123 No Tipping(DFS+几何力矩+贪心)

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。

解题思路:这道提的难点不是得到结果,而是在时间的问题上,如果单纯用收索去做的话是可以得出结果的,但是会超时。

这题写了我一天,有点小纠结。

<1>可以将木块取下的问题看成是将木块放上木板。

<2>要分清楚什么时候木板会倾斜,因为它有两个支点,所以要考虑两种情况,(wl1:-L ~ 1.5 ,wr1:1.5 ~ L 和  wl2:-L ~ -1.5 , wr2:-1.5 ~ L),要注意的是wl1 和 wr2 下面还有一个支点,所以  wl1 > wr1  和   wr2 > wl2 的时候木板是不会发生偏移的。(这里提一下,F = L * W ,   对于质量均匀的木盘呢,L取长度的中间值)  

<3>接下来要讲点关键的优化了,在计算完原先木板的力矩之后呢, 可以将处于(-1.5~1.5)这个区域的木块放到木板上,不难发现,这个地方的木块只会加大木板的稳定度(对应实际问题就是最后拿下来)。

<4>第二个优化就是该解决掉那部分会导致木块发生移动的木块了,首先将木块按位置分成放左边和放右边的两份,为什么要分开来考虑呢,因为同堆的木块对木板的偏移趋势是相同的,这对有助于后面用到的贪心的方法,只需将所有木块遍历一遍就可以的到结果。

<5>将两堆木块中的木块分别按照力矩的大小从小到大排列,然后从任意一堆中每次取小的放入木板,直到下一个木块放入后会发生移动,然后就换放另一堆的木块,同样的终止条件,直到木块全部放上去为止,这中间有个终止的特殊条件,就是不能全部放上去的情况,对应的就是两堆木块均无法放入的情况。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define N 30

struct block{
    int x;
    int w;
}b[3][N], rec[N];

int L, M, n, cnt[3], vis[3][N];
int ok, pace[3];
double wr1, wr2, wl1, wl2;

bool cmp(const block &a, const block &b){
    return abs(a.x * a.w) < abs(b.x * b.w);
}

void DFS(int cur,int dir, int bo){
    if (cur >= n){
	ok = 1; 
	return;
    }
    int yes = 0;
    for (int i = pace[dir]; i < cnt[dir]; i++){
	if(dir == 0){
//		printf("L%.3lf %.3lf\n", wr2, wl2 + (-3 - b[dir][i].x) * b[dir][i].w);
	    if(wr2 < (wl2 + (-3 - b[dir][i].x) * b[dir][i].w)){
		pace[dir] = i;
		break;
	    }
	    wl1 = wl1 + (3 - b[dir][i].x) * b[dir][i].w;
	    wl2 = wl2 + (-3 - b[dir][i].x) * b[dir][i].w;
	}
	else{
//		printf("R%.3lf %.3lf\n", wr1 + (b[dir][i].x - 3) * b[dir][i].w, wl1);
	    if((wr1 + (b[dir][i].x - 3) * b[dir][i].w) > wl1){
		pace[dir] = i;
		break;
	    }
	    wr1 = wr1 + (b[dir][i].x - 3) * b[dir][i].w;
	    wr2 = wr2 + (b[dir][i].x + 3) * b[dir][i].w;
	}
	yes = 1;
	pace[dir] = i + 1;
	rec[cur++] = b[dir][i];
    }

    if (!bo && !yes){
	ok = -1;
	return;
    }
    DFS(cur, 2 - dir, yes);
    if (ok)	return ;
}

int main(){
    int t = 1;
    while (scanf("%d%d%d", &L, &M, &n), L + M + n){
	// Init;
	memset(b, 0, sizeof(b));
	memset(cnt, 0, sizeof(cnt));
	memset(rec, 0, sizeof(rec));
	memset(pace, 0, sizeof(pace));
	wr1 = wl2 = (L - 3) * (L - 3) * M / (4.0 * L);
	wl1 = wr2 = (L + 3) * (L + 3) * M / (4.0 * L);
	ok = 0;

	// Read;
	int xi, wi;
	for (int i = 0; i < n; i++){
	    scanf("%d%d", &xi, &wi);
	    xi *= 2;
	    if (xi > 3){
		b[2][cnt[2]].x = xi;
		b[2][cnt[2]].w = wi;
		cnt[2]++;
	    }
	    else if (xi < -3){
		b[0][cnt[0]].x = xi;
		b[0][cnt[0]].w = wi;
		cnt[0]++;
	    }
	    else{
		b[1][cnt[1]].x = xi;
		b[1][cnt[1]].w = wi;
		cnt[1]++;
	    }
	}

	// Handle;
	for (int i = 0; i < 3; i++)
	    sort(b[i], b[i] + cnt[i], cmp);
	memcpy(rec, b[1], sizeof(b[1]));
	for (int i = 0; i < cnt[1]; i++){
	    wl1 += (3 - b[1][i].x) * b[1][i].w;
	    wr2 += (b[1][i].x + 3) * b[1][i].w;
	}

	DFS(cnt[1], 0, 1);

	printf("Case %d:\n", t++);
	if(ok == 1){
	    for (int i = n - 1; i >= 0; i--)
		printf("%d %d\n", rec[i].x / 2, rec[i].w);
	}
	else
	    printf("Impossible\n");
    }
    return 0;}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值