Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.      
  3. using namespace std;
  4.  
  5. #define x first
  6. #define y second
  7. #define pb push_back
  8. #define mp make_pair
  9. #define sqr(a) ((a) * (a))
  10. #define sz(a) int((a).size())
  11. #define all(a) (a).begin(), (a).end()
  12. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  13. #define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
  14.  
  15. template<class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
  16.     return out <<  "(" << a.x << ", " << a.y << ")";
  17. }
  18.  
  19. template<class A> ostream& operator << (ostream& out, const vector<A> &a) {
  20.     out << "[";
  21.     for (auto it = a.begin(); it != a.end(); ++it) {
  22.         if (it != a.begin())
  23.             out << ", ";
  24.         out << *it;
  25.     }
  26.     return out << "]";
  27. }
  28.  
  29. typedef long long li;
  30. typedef long double ld;
  31. typedef pair<int, int> pt;
  32.  
  33. mt19937 rnd(time(NULL));
  34. mt19937_64 rnd64(time(NULL));
  35.  
  36. const int INF = 1e9;
  37. const li INF64 = 1e18;
  38. const int MOD = 1e9 + 7;
  39. const ld PI = acosl(-1.0);
  40. const ld EPS = 1e-9;
  41.  
  42. const int N = 5010;
  43.  
  44. int n, X, Y, Z;
  45. int a[N];
  46.  
  47. bool read() {
  48.     if (scanf("%d%d%d%d", &n, &X, &Y, &Z) != 4)
  49.         return false;
  50.     forn(i, n) scanf("%d", &a[i]);
  51.     return true;
  52. }
  53.  
  54. li dp[N][N];
  55. int nxt[N][N], prv[N][N];
  56.  
  57. vector<int> z_function(const vector<int>& s) {
  58.     int n = sz(s);
  59.     vector<int> z(n);
  60.     for (int i = 1, l = 0, r = 0; i < n; ++i) {
  61.         if (i <= r)
  62.             z[i] = min (r - i + 1, z[i - l]);
  63.         while (i + z[i] < n && s[z[i]] == s[i + z[i]])
  64.             ++z[i];
  65.         if (i + z[i] - 1 > r)
  66.             l = i, r = i + z[i] - 1;
  67.     }
  68.     return z;
  69. }
  70.  
  71. void solve() {
  72.     forn(i, n + 1) fore(j, 0, n + 1) {
  73.         prv[i][j] = 0;
  74.         nxt[i][j] = -1;
  75.     }
  76.        
  77.     forn(i, n) {
  78.         vector<int> s(n - i);
  79.         fore(j, i, n) s[j - i] = a[j];
  80.         auto z = z_function(s);
  81.         int pos = 1;
  82.         fore(j, 1, sz(s)) {
  83.             pos = max(pos, j);
  84.             while (pos < sz(s) && z[pos] < j)
  85.                 pos++;
  86.             if (pos < sz(s)) {
  87.                 nxt[i + j][j] = pos + i + j;
  88.             }
  89.         }
  90.     }
  91.    
  92.     reverse(a, a + n);
  93.     forn(i, n) {
  94.         vector<int> s(n - i);
  95.         fore(j, i, n) s[j - i] = a[j];
  96.         auto z = z_function(s);
  97.         int pos = 1;
  98.         fore(j, 1, sz(s)) {
  99.             pos = max(pos, j);
  100.             while (pos < sz(s) && z[pos] < j)
  101.                 pos++;
  102.             if (pos < sz(s)) {
  103.                 int pp = n - (i + j);
  104.                 prv[pp][j] = 1;
  105.                 //cout << pp << " " << j << endl;
  106.             }
  107.         }
  108.     }
  109.    
  110.     reverse(a, a + n);
  111.    
  112.     //forn(i, n) forn(j, n) {
  113.     //  cout << i << " " << j << " " << nxt[i][j] << endl;
  114.     //}
  115.    
  116.     forn(i, n + 1) forn(j, n + 1)
  117.         dp[i][j] = INF64;
  118.    
  119.     dp[0][0] = 0;
  120.     forn(i, n) {
  121.         li mn = INF64;
  122.         forn(j, i + 1) if (dp[i][j] != INF64) {
  123.             //cout << i << " " << j << " " << dp[i][j] << endl;
  124.             mn = min(mn, dp[i][j]);
  125.             dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + X);
  126.             if (j != 0 && nxt[i][j] != -1) {
  127.                 dp[nxt[i][j]][j] = min(dp[nxt[i][j]][j], dp[i][j] + Z + li(nxt[i][j] - i - j) * X);
  128.             }
  129.         }
  130.         fore(k, 1, i + 1) {
  131.             if (prv[i][k]) {
  132.                 dp[i + k][k] = min(dp[i + k][k], mn + Y + Z);
  133.             }
  134.         }
  135.     }
  136.    
  137.     li ans = INF64;
  138.     forn(i, n + 1) ans = min(ans, dp[n][i]);
  139.    
  140.     printf("%lld\n", ans);
  141. }
  142.  
  143. int main() {
  144. #ifdef _DEBUG
  145.     freopen("input.txt", "r", stdin);
  146.     //freopen("output.txt", "w", stdout);
  147.    
  148.     int tt = clock();
  149. #endif
  150.  
  151.     cout << fixed << setprecision(10);
  152.     cerr << fixed << setprecision(10);
  153.  
  154.     int tc;
  155.     scanf("%d", &tc);
  156.     forn(i, tc) {
  157.         read();
  158.         printf("Case #%d: ", i + 1);
  159.         solve();
  160.        
  161. #ifdef _DEBUG
  162.         cerr << "TIME = " << clock() - tt << endl;
  163.         tt = clock();  
  164. #endif
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement