uva_10123_No Tipping

来源:互联网 发布:gif制作软件在线 编辑:程序博客网 时间:2024/05/06 11:23

很有技巧性,问题等价于逐个放到天平上去,先放中间的。将左右的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;}               


 

原创粉丝点击