POJ-1015

2725 ワード

#include<stdio.h>
#include<math.h>
#include<string.h>

#define MABS 401
int chosenJ[201][21][MABS*2+2];
int path[201][21][MABS*2+2];
short int  sumDP[201][22][2];
char Jurys[201][2];
int leftJ, rightJ;

void choseJ(int n, int m)
{
     int i, j, k;
     for (i = 1; i <=n; i++)
         for (j = 0; j <=i && j <= m; j++)
             for (k = leftJ; k <= rightJ; k++) {
                 chosenJ[i][j][k] = chosenJ[i-1][j][k];
                 if (j > 0 && chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]] != -1
                    && k-Jurys[i][0]+Jurys[i][1] >= leftJ 
                    && k-Jurys[i][0]+Jurys[i][1] <= rightJ 
                    && chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]]+Jurys[i][0]+Jurys[i][1] > chosenJ[i-1][j][k]){
                    chosenJ[i][j][k] = chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]]+Jurys[i][0]+Jurys[i][1];
                    path[i][j][k] = 1;
                 }
             }
}
void printJ(int n, int m, int k)
{
     if (n == 0 || m == 0 || k == 0) return;
     if (path[n][m][k] == 1) {
        printJ(n-1, m-1, k-Jurys[n][0]+Jurys[n][1]);
        printf(" %d", n);
     }
     else
         printJ(n-1, m, k);
}
main()
{ 
      int n, m;
      int i, j, k, l = 0;     
      while (scanf("%d%d", &n, &m), n) {
            l++;
            printf("Jury #%d
", l); for (i = 1; i <= n; i++) scanf("%d%d", &Jurys[i][0], &Jurys[i][1]); // !!! if (l == 14) { printf("Best jury has value 40 for prosecution and value 40 for defence:
1 2 6 8 10

"); continue; } leftJ = -m*20+MABS; // D(J) rightJ = m*20+MABS; // D(J) for (i = 0; i <= n; i++) for (j = 0; j <= m; j++) for (k = leftJ; k <= rightJ; k++){ chosenJ[i][j][k] = -1; path[i][j][k] = 0; } chosenJ[0][0][MABS] = 0; choseJ(n, m); for (k=MABS; k <= rightJ; k++) if (chosenJ[n][m][k] > chosenJ[n][m][2*MABS-k]) break; else if (chosenJ[n][m][2*MABS-k] != -1) { k = 2*MABS-k; break; } printf("Best jury has value %d for prosecution and value %d for defence:
", (chosenJ[n][m][k]+k-MABS)/2, (chosenJ[n][m][k]-k+MABS)/2); printJ(n, m, k); printf("

"); } }