uva_10123_No Tipping

很有技巧性,问题等价于逐个放到天平上去,先放中间的。将左右的Pack分开来并排序,先放小的后放大的,如果一边小的放不下,那么以后的就不用放了,接着放另一边,如此来回。

搜索3:

 

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef struct Pack{
        int pos, w;
}Pack;
Pack L[20], R[20], PATH[20];
int tL, tR;
int nL, nR;
int plen;
int nCase = 1;

bool cmp1 (Pack a, Pack b)
{
       return (a.pos + 3) * a.w > (b.pos + 3) * b.w;
}
bool cmp2 (Pack a, Pack b)
{
       return (a.pos - 3) * a.w < (b.pos - 3) * b.w;
}

int check(Pack *LoR, int &cnt, int lmt)
{
       if(cnt >= lmt) return 0;
       int tmp1, tmp2;
       tmp1 = (LoR[cnt].pos + 3) * LoR[cnt].w;
       tmp2 = (LoR[cnt].pos - 3) * LoR[cnt].w;
       if(tL + tmp1 >= 0 && tR + tmp2 <= 0)
       {
             tL += tmp1, tR += tmp2;
             PATH[plen++] = LoR[cnt];
             cnt++;
             return 1;
       }
       return 0;
}
       
int solve (int cntL, int cntR)
{
       int f1 = 1, f2 = 1;
       do
       {
             while (check (L, cntL, nL));
             while (check(R, cntR, nR));
       }while (check (L, cntL, nL));
       if (cntL == nL && cntR == nR)return 1;
       return 0;
}
    
          
main()
{
       int lb,  wb, n;
       int w, pos;
        //FILE *fp = fopen("in.txt", "r");
       while (scanf ("%d %d %d", &lb, &wb, &n ) == 3 && (lb + wb + n) )
       {
               lb *= 2;
               tL = 3 * wb;
               tR = -tL;
               nL = nR = 0;
               plen = 0;
               for (int i = 0; i < n ; i++)
               {
                     scanf ("%d %d", &pos, &w);
                     pos *= 2;
                     if (pos < 3 && pos > -3)
                     {
                              tL += (pos + 3) * w, tR += (pos - 3) *w;
                              PATH[plen].pos = pos;
                              PATH[plen].w = w;
                              plen ++;
                     }
                     else if(pos < -3)L[nL].pos = pos, L[nL].w = w, nL++;
                     else R[nR].pos = pos, R[nR].w = w, nR++;
               }
               sort (L, L+nL, cmp1);
               sort (R, R+nR, cmp2);
               printf("Case %d:\n", nCase++);
               
               int flag =solve (0, 0);
               if (!flag) puts("Impossible");
               else while (--plen >= 0) printf("%d %d\n", PATH[plen].pos/2, PATH[plen].w);
             
       }
      //  getchar();getchar();getchar();
       return 0;
}
               


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值