No Tipping +回溯+物理+贪心

来源:互联网 发布:实时监测网速软件 编辑:程序博客网 时间:2024/05/27 02:33

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 102 45 78 820 3 151 10 8 5-6 85 9-8 48 10-3 10-4 52 9-2 23 3-3 25 1-6 12 530 10 2-8 1009 910 0 0 
 

Possible Output for sample input

Case 1:-4 108 8-8 45 7-3 102 4Case 2:1 10 8 5-6 85 9-8 48 10-3 10-4 52 9-2 23 3-3 25 1-6 12 5Case 3:Impossible

解决方案:做这道题,要用到物理的知识+回溯+剪枝。题目意思是,在一块长板上放着很多块物体,在1.5和-1.5处有支点,从板上抽出物块,使长板不倾斜,求拿出物品的顺序。如果直接暴力回溯,那肯定超时了。所以,要制定一个策略。首先确定,放在-1.5和1.5之间的只会使木板更稳定;然后,若支点为1.5为情况1,-1.5为情况2 。则当wl1>wr1,或wr2>wl2时,都不会倾斜。所以可制定以下策略:

1)把拿出的操作换成放回;

2)在-1.5和1.5之间的肯定先放;

3)可把力矩分成左和右两类,然后分别对这两类的力矩的绝对值重小到大排序;

4)放置的规则为,小力矩的先放,如果左边放不了,放右边,都放不了则可肯定结果是“Impossible”

5) 最后按照放回的顺序逆序输出就可以了。



#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int len,n;int weight;struct a_banlance{   int position;   int weight;}B[3][100],rec[100];int cnt[3],ok,pace[100];double wr1,wl1,wr2,wl2;bool cmp(a_banlance a,a_banlance b){   return abs(a.position*a.weight)<abs(b.position*b.weight);}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){        if(wr2<(wl2+(-3-B[dir][i].position)*B[dir][i].weight)){            pace[dir]=i;            break;        }        wl2+=(-3-B[dir][i].position)*B[dir][i].weight;        wl1+=(3-B[dir][i].position)*B[dir][i].weight;    }    else {        if((wr1+(-3+B[dir][i].position)*B[dir][i].weight)>wl1)        {            pace[dir]=i;            break;        }        wr1+=(-3+B[dir][i].position)*B[dir][i].weight;        wr2+=(3+B[dir][i].position)*B[dir][i].weight;    }    yes=1;    pace[dir]=i+1;    rec[cur++]=B[dir][i];  }  if(!yes&&!bo){    ok=-1;    return ;  }  dfs(cur,2-dir,yes);  if(ok)    return ;}int main(){    int p,w,k=0;    while(~scanf("%d%d%d",&len,&weight,&n)&&(len+weight+n)){       memset(cnt,0,sizeof(cnt));       memset(pace,0,sizeof(pace));       memset(rec,0,sizeof(rec));       memset(B,0,sizeof(B));       wl1=wr2=(len+3)*(len+3)*weight/(4.0*len);       wl2=wr1=(len-3)*(len-3)*weight/(4.0*len);        for(int i=0;i<n;i++){            scanf("%d%d",&p,&w);           p*=2;        if(p>3){            B[2][cnt[2]].position=p;            B[2][cnt[2]].weight=w;            cnt[2]++;        }        else if(p<-3){            B[0][cnt[0]].position=p;            B[0][cnt[0]].weight=w;            cnt[0]++;        }        else {            B[1][cnt[1]].position=p;            B[1][cnt[1]].weight=w;            cnt[1]++;        }        }    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].position)*B[1][i].weight;        wr2+=(3+B[1][i].position)*B[1][i].weight;    }    ok=0;    dfs(cnt[1],0,1);    cout<<"Case "<<++k<<":\n";    if(ok==1){            for(int i=n-1;i>=0;i--){                printf("%d %d\n",rec[i].position/2,rec[i].weight);            }    }    else printf("Impossible\n");    }    return 0;    }


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 按扣皮箱扣不上怎么办 拉链一边掉了怎么办 拉链一半掉了怎么办 去北戴河怎么办安检证 电脑空格键打字失灵怎么办 国外玩游戏延迟怎么办 韩服lol要求输韩文怎么办 智齿碰到牙神经怎么办 合影中有人去世怎么办 在家中遇到蛇怎么办 去澳门手机充电怎么办 商标撕下来胶怎么办 商标被提出异议怎么办 商标被别人用怎么办 商标申请不下来怎么办 注册商标有近似商标怎么办 电话话打不通怎么办 公司卖了商标怎么办 商标撕不下来怎么办 商标撕不下来时怎么办 商标还没下来怎么办 商标揭不下来怎么办 手机程序出现异常怎么办 公众号企业名称是*怎么办 家人生命受到威胁怎么办 海淘看不懂英文怎么办 对英语不感兴趣怎么办 装修无合同起诉怎么办 上海离职后档案怎么办 公司注销了银行帐户怎么办 360借条注销了怎么办 注销营业执照公章丢失怎么办 工厂招聘信息有假怎么办 个体餐饮怎么办核名 见父母后接下来怎么办 工商核名重名怎么办 核名通知书过期怎么办 包头鼎太风华怎么办 用人单位不续签劳动合同怎么办 全是英文看不懂怎么办 孩子智力发育晚怎么办