royalsflush

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

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. struct assist {
  7.     int d,e;
  8.  
  9.     bool operator<(const assist& a) const {
  10.         return this->d<a.d;
  11.     }
  12. };
  13.  
  14. const int inf=0x3f3f3f3f;
  15.  
  16. int n,k;
  17. int dp[210][210];
  18. assist p[210];
  19. int _42=1;
  20.  
  21. int main() {
  22.     while (1) {
  23.         scanf("%d %d", &n,&k);
  24.         if (!n && !k) break;
  25.  
  26.         for (int i=0; i<n; i++)
  27.             scanf("%d %d", &p[i].e, &p[i].d);
  28.         sort(p,p+n);
  29.  
  30.         memset(dp,0x3f,sizeof(dp));
  31.        
  32.         for (int i=0; i<=n; i++)
  33.             dp[i][0]=0;
  34.  
  35.         for (int i=n-1; i>=0; i--)
  36.             for (int j=0; j<=n; j++) { 
  37.                 dp[i][j]=dp[i+1][j]+p[i].e;
  38.                 if (j) dp[i][j]=min(dp[i][j],
  39.                     dp[i+1][j-1]+p[i].d*j);
  40.             }
  41.  
  42.         int np;
  43.         for (np=n; np>=0; np--)
  44.             if (dp[0][np]<=k) break;
  45.  
  46.         if (np==-1)
  47.             printf("%d: Mission Impossible\n", _42++);
  48.         else
  49.             printf("%d: %d\n", _42++, np);
  50.     }
  51.     return 0;
  52. }
Add Comment
Please, Sign In to add comment