题目的意思就是要把木块拿下来,但天平不能倒,天平的支点在-1.5 和 1.5的位置,如果 左边支点 左边的力矩大于右边,就有左倒, 右边支点 右边的力矩大于左边就会右倒,
但是大的量要超过木板质量。
首先要把问题看成是放木块。
先用两个数组来把左右两边的木块存起来,然后按力矩升序排列,然后按顺序一个个放,不会倒就放上去,会倒就放另一边的,知道放完。
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 20 + 5;
struct wood {
int pos;
int weight;
int torque;
}leftw[N],rightw[N],res[N];
bool ok ;
int num,l,wei;
int leftnum ,rightnum,left,right,midnum;
int cmp (wood a ,wood b) {
return a.torque < b.torque;
}
bool yes(int l ,int r) {
int suml = 0;
int sumr = 0;
for (int i = 0 ; i < l ;i++) {
suml += (-leftw[i].pos * 2 - 3) * leftw[i].weight;
}
for (int i = 0 ; i < r ;i++) {
sumr += (rightw[i].pos * 2 + 3) * rightw[i].weight;
}
sumr += wei;
if (suml > sumr)
return false;
suml = 0;
sumr = 0;
for (int i = 0; i < l; i++) {
suml += (-leftw[i].pos * 2 + 3) * leftw[i].weight;
}
for (int i = 0; i < r; i++) {
sumr += (rightw[i].pos * 2 - 3) * rightw[i].weight;
}
suml += wei;
if (sumr > suml)
return false;
return true;;
}
void dfs (int cur ,int l ,int r) {
if (cur == num) {
for (int i = cur - 1 ; i >= 0 ;i--) {
cout <<res[i].pos <<" "<<res[i].weight<<endl;
}
ok = true;
return;
}
for (int i = l ; i < leftnum ;i++) {
if(yes(i,r) && !ok) {
res[cur].pos = leftw[i].pos;
res[cur].weight = leftw[i].weight;
dfs(cur + 1, i + 1,r);
}
else
break;
}
for (int i = r ; i < rightnum ;i++) {
if(yes(l,i) && !ok) {
res[cur].pos = rightw[i].pos;
res[cur].weight = rightw[i].weight;
dfs(cur + 1, l, i + 1);
}
else
break;
}
}
int main () {
int a,b;
int T = 1;
while (cin >> l >> wei >> num && l + wei +num) {
leftnum = rightnum = 0 ;
for (int i = 0 ; i < num ; i++) {
cin >> a >> b;
if (a >= 0) {
rightw[rightnum].pos = a;
rightw[rightnum].weight = b;
rightw[rightnum++].torque = (a * 2 - 3) * b;
}
if (a < 0) {
leftw[leftnum].pos = a;
leftw[leftnum].weight = b;
leftw[leftnum++].torque = (-a * 2 - 3) * b;
}
}
sort(leftw ,leftw + leftnum ,cmp);
sort(rightw , rightw + rightnum ,cmp);
wei = wei * 3;
ok = false;
cout << "Case "<< T++ << ":\n";
dfs(0,0,0);
if(!ok)
cout << "Impossible" <<endl;
}
return 0;
}