Advertisement
RaFiN_

cf-264C

Feb 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. /*   dp[c] := the maximal value of a sequence whose last ball's color is c
  2.  
  3. For each ball i, we want to update the array. Let the i-th ball's color be col[i], the i-th ball's value be val[i], and the maximal value of dp array other than dp[col[i]] be otherMAX. We can update the value of dp[col[i]] to dp[col[i]] + val[i] × a or otherMAX + val[i] × b. Here, we only need to know dp[col[i]] and otherMAX. If we remember the biggest two values of dp array in that time and their indexes in the array, otherMAX can be calculated using the biggest two values, which always include maximal values of dp array other than any particular color.
  4.  
  5. Since the values of dp array don't decrease, we can update the biggest two values in O(1). Finally, the answer for the query is the maximal value of dp array.
  6.  
  7. The complexity of the described algorithm is O(QN).*/
  8.  
  9.  
  10. ll dp[MAX],v[MAX];ll a,b;int len;int pk[MAX],arr[MAX],col[MAX],vis[MAX][2],last[MAX];
  11.  
  12.  
  13. ll giveres(int n)
  14. {
  15.     ///cout<<pos<<" "<<pk[pos]<<endl;
  16.  
  17.     for(int i = 1;i<MAX;i++)dp[i] = -3e17;
  18.     pll x1 = pll(-3e17,-1),x2 = pll(-3e17,-1);
  19.     for(int i = 1;i<=n;i++)
  20.     {
  21.         if(dp[col[i]]==-3e17)
  22.         {
  23.             dp[col[i]] = b * v[i];
  24.         }
  25.         else
  26.         {
  27.             dp[col[i]] = max(dp[col[i]],dp[col[i]] + a*v[i]);
  28.             dp[col[i]] = max(dp[col[i]],b*v[i]);
  29.         }
  30.         if(x1.ss!=col[i]&&x1.ff!=-3e17)
  31.         {
  32.             if(x1.ff + b*v[i]>dp[col[i]])
  33.             {
  34.                 dp[col[i]] = x1.ff + b*v[i];
  35.             }
  36.         }
  37.         if(x2.ss!=col[i]&&x2.ff!=-3e17)
  38.         {
  39.             if(x2.ff + b*v[i]>dp[col[i]])
  40.             {
  41.                 dp[col[i]] = x2.ff + b*v[i];
  42.             }
  43.         }
  44.         if(dp[col[i]]>x1.ff)
  45.         {
  46.             if(x1.ss!=col[i])
  47.             {
  48.                 swap(x1,x2);
  49.             }
  50.             x1.ff = dp[col[i]];
  51.             x1.ss = col[i];
  52.         }
  53.         else if(dp[col[i]]>x2.ff&&x1.ss!=col[i])
  54.         {
  55.             x2.ff = dp[col[i]];
  56.             x2.ss = col[i];
  57.         }
  58.  
  59.         ///cout<<x1.ff<<" "<<x1.ss<<" "<<x2.ff<<" "<<x2.ss<<" "<<a*v[i]<<" "<<b*v[i]<<" "<<dp[col[i]]<<" "<<col[i]<<endl;
  60.  
  61.     }
  62.  
  63.     return *max_element(dp,dp+MAX);
  64.  
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70.     ///booster()
  71.     ///read("input.txt");
  72.     int n,q;
  73.     scani2(n,q);
  74.     len = n;
  75.     for(int i = 1;i<=n;i++)scanl(v[i]);
  76.     for(int i = 1;i<=n;i++)
  77.     {
  78.         scani(col[i]);
  79.     }
  80.  
  81. ///    for(int i = 1;i<=n;i++)cout<<pk[i]<<" "<<last[i]<<" "<<i<<endl;
  82.  
  83.     while(q--)
  84.     {
  85.         scanf("%I64d%I64d",&a,&b);
  86.         printf("%I64d\n",giveres(n));
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement