Guest User

hack101 contest strategy, fleimgruber

a guest
Apr 30th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. #define all(a) (a).begin(),(a).end()
  9. #define pb push_back
  10. #define sz(a) ((int)(a).size())
  11. #define mp make_pair
  12. #define fi first
  13. #define se second
  14.  
  15. typedef pair<int,int> pint;
  16. typedef long long ll;
  17. typedef vector<int> vi;
  18.  
  19.  
  20. #define MAX_N 3005
  21. #define INF 123456789876543LL
  22.  
  23. int n,d,t[MAX_N];
  24. ll dp[MAX_N][MAX_N];
  25.  
  26. //persons [0..i-1] know the problems
  27. //persons [0..j-1] is not explaining anymore
  28. ll f(int i, int j)
  29. {
  30.     //everyone knows the problems and nobody is explaining
  31.     //anymore -> solved all the problems
  32.     if (i==n&&j==n)
  33.         return 0;
  34.     //more (or all) people that know the problems
  35.     //don't explaining anymore -> that means there's
  36.     //no way to tell the remaining people about them
  37.     if (j>=i)
  38.         return INF;
  39.     if (dp[i][j]!=-1)
  40.         return dp[i][j];
  41.     ll a=INF;
  42.     //all of the (i-1)-(j-1) = i-j people teach
  43.     //so now 2*i-j people know the problems (and at most all)
  44.     int explaining=min(n,2*i-j);
  45.     if (explaining>i)
  46.         a=d+f(explaining,j);
  47.     //one person stops explaining and solves his problem
  48.     return dp[i][j]=min(a,max(1LL*t[j],f(i,j+1)));
  49. }
  50.  
  51. int main()
  52. {
  53.     int tc;
  54.     scanf("%d",&tc);
  55.     while (tc--)
  56.     {
  57.         scanf("%d %d",&n,&d);
  58.         for (int i=0; i<n; i++)
  59.             scanf("%d",t+i);
  60.         sort(t,t+n,greater<int>());
  61.         for (int i=0; i<=n; i++)
  62.             for (int j=0; j<=n; j++)
  63.                 dp[i][j]=-1;
  64.         printf("%lld\n",f(1,0));
  65.     }
  66.     return 0;
  67. }
Add Comment
Please, Sign In to add comment