lelouche29

oil_mines_dp_Samsung

Sep 25th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n,c;
  5. int a[10000];
  6. int b[10000];
  7. int pre[1000];
  8. int dp[1000][1000][2];
  9.  
  10. void solve1(){
  11.     pre[0] = 0;
  12.     for(int i=1; i<=n; i++){
  13.         pre[i] = pre[i-1] + a[i];
  14.     }
  15. }
  16.  
  17. int solve2(){
  18.     for(int j=1; j<=n; j++){  //mines
  19.         for(int k=1; k<=j && k<=c; k++){ //companies
  20.             if(k==1 || j<=1){
  21.                 dp[j][k][0] = pre[j];
  22.                 dp[j][k][1] = pre[j];
  23.             }
  24.             else{
  25.                 int temp=9999999;
  26.                 for(int l=j; l>k-1; l--){
  27.                     int mn = min( (pre[j] - pre[l-1]), min( dp[l-1][k-1][0], dp[l-1][k-1][1]));
  28.                     int mx = max( (pre[j] - pre[l-1]), max( dp[l-1][k-1][0], dp[l-1][k-1][1]));
  29.                    
  30.                     // cout<<" hellp ";
  31.                     if(temp > mx - mn){
  32.                         temp = mx - mn;
  33.                         dp[j][k][0] = mn;
  34.                         dp[j][k][1] = mx;
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     return dp[n][c][1] - dp[n][c][0];
  41. }
  42.  
  43. int main() {
  44.     int t;
  45.     cin>>t;
  46.     while(t--){
  47.         cin>>c>>n;
  48.  
  49.         for(int i=1; i<=n; i++) cin>>b[i];
  50.  
  51.         for(int i=1; i<=n; i++) b[i+n] = b[i];
  52.  
  53.         int ans=99999;
  54.         for(int i=1; i<=n+1; i++){
  55.             int temp=1;
  56.             for(int k=i; k<i+n; k++){
  57.                 a[temp] = b[k];
  58.                 temp++;
  59.             }
  60.             solve1();
  61.             ans = min(ans,solve2());
  62.         }
  63.         if(c>n) ans=-1;
  64.         cout<<ans<<endl;
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment