royalsflush

Help-or-else O(n^3) - Live Archive 5688 AC

Sep 7th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. const int inf =0x3f3f3f3f;
  7.  
  8. struct assist {
  9.     int d,e;
  10. };
  11.  
  12. int n,k;
  13. int dp[210][210];
  14. assist p[210];
  15. int np;
  16. int _42=1;
  17.  
  18. bool cmp(assist a, assist b) {
  19.     if (a.d==b.d)
  20.         return a.e<b.e;
  21.     return a.d<b.d;
  22. }
  23.  
  24. int main() {
  25.     while (1) {
  26.         scanf("%d %d", &n,&k);
  27.         if (!n && !k) break;
  28.  
  29.         for (int i=0; i<n; i++)
  30.             scanf("%d %d", &p[i].e, &p[i].d);
  31.         sort(p,p+n,cmp);
  32.  
  33.         for (np=n; np>=0; np--) {
  34.             memset(dp,0x3f,sizeof(dp));
  35.  
  36.             dp[0][np]=p[0].e;
  37.             dp[0][np-1]=np*p[0].d;
  38.  
  39.             for (int i=1; i<n; i++)
  40.                 for (int j=np; j>=0; j--)
  41.                     dp[i][j]=min(dp[i-1][j]+p[i].e,
  42.                         dp[i-1][j+1]+p[i].d*(j+1));
  43.  
  44.             if (dp[n-1][0]<=k)
  45.                 break;
  46.         }
  47.        
  48.         if (np!=-1) printf("%d: %d\n" ,_42++, np);
  49.         else printf("%d: Mission Impossible\n", _42++);
  50.     }
  51.  
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment