royalsflush

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

Sep 7th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 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 e,d;
  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. assist p[210];
  18. int dp[210][210];
  19. int _42=1;
  20.  
  21. int doit(int i, int j) {
  22.     if (i==n && j==0) return 0;
  23.     if (i==n) return inf;
  24.  
  25.     if (dp[i][j]!=-1) return dp[i][j];
  26.  
  27.     int nhelp=doit(i+1,j)+p[i].e;
  28.     int help=doit(i+1,j-1)+j*p[i].d;
  29.  
  30.     return dp[i][j]=min(nhelp,help);
  31. }
  32.  
  33. int main() {
  34.     while (1) {
  35.         scanf("%d %d", &n,&k);
  36.         if (!n && !k) break;
  37.  
  38.         for (int i=0; i<n; i++)
  39.             scanf("%d %d", &p[i].e, &p[i].d);
  40.         sort(p,p+n);
  41.  
  42.         memset(dp,-1,sizeof(dp));
  43.  
  44.         int np=-1;
  45.         for (np=n; np>=0; np--)
  46.             if (doit(0,np)<=k) break;
  47.  
  48.         if (np==-1)
  49.             printf("%d: Mission Impossible\n", _42++);
  50.         else
  51.             printf("%d: %d\n", _42++, np);
  52.     }
  53.     return 0;
  54. }
Add Comment
Please, Sign In to add comment