Advertisement
ec1117

Untitled

Nov 14th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.33 KB | None | 0 0
  1. // #include <iostream>
  2. // #include <string>
  3. // #include <vector>
  4. // #include <algorithm>
  5. // #include <sstream>
  6. // #include <queue>
  7. // #include <deque>
  8. // #include <bitset>
  9. // #include <iterator>
  10. // #include <list>
  11. // #include <stack>
  12. // #include <map>
  13. // #include <set>
  14. // #include <functional>
  15. // #include <numeric>
  16. // #include <utility>
  17. // #include <limits>
  18. // #include <time.h>
  19. // #include <math.h>
  20. // #include <stdio.h>
  21. // #include <string.h>
  22. // #include <stdlib.h>
  23. // #include <assert.h>
  24. // #include <complex.h>
  25. // #include <random>
  26. // #include <chrono>
  27. #include "bits/stdc++.h"
  28.  
  29. using namespace std;
  30.  
  31. typedef long long ll;
  32. typedef long double ld;
  33. typedef double db;
  34. typedef string str;
  35.  
  36. typedef pair<int,int> pi;
  37. typedef pair<pi, int> pii;
  38. typedef pair<ll,ll> pl;
  39. typedef pair<db,db> pd;
  40.  
  41. typedef vector<int> vi;
  42. typedef vector<bool> vb;
  43. typedef vector<ll> vl;
  44. typedef vector<db> vd;
  45. typedef vector<str> vs;
  46. typedef vector<pi> vpi;
  47. typedef vector<pl> vpl;
  48. typedef vector<pd> vpd;
  49. template<class T> using V = vector<T>;
  50. template<class T, size_t SZ> using AR = array<T,SZ>;
  51.  
  52. #define mp make_pair
  53. #define f first
  54. #define s second
  55. #define sz(x) (int)(x).size()
  56. #define all(x) begin(x), end(x)
  57. #define rall(x) (x).rbegin(), (x).rend()
  58. #define sor(x) sort(all(x))
  59. #define rsz resize
  60. #define ins insert
  61. #define ft front()
  62. #define bk back()
  63. #define pf push_front
  64. #define pb push_back
  65. #define eb emplace_back
  66. #define lb lower_bound
  67. #define ub upper_bound
  68.  
  69. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  70. #define F0R(i,a) FOR(i,0,a)
  71. #define For(i,a) FOR(i,0,a)
  72. #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
  73. #define R0F(i,a) ROF(i,0,a)
  74. #define Rof(i,a) ROF(i,0,a)
  75. #define trav(a,x) for (auto& a: x)
  76.  
  77. template<class T> bool ckmin(T& a, const T& b) {
  78.         return b < a ? a = b, 1 : 0; }
  79. template<class T> bool ckmax(T& a, const T& b) {
  80.         return a < b ? a = b, 1 : 0; }
  81. constexpr int pct(int x) { return __builtin_popcount(x); }
  82. constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
  83. ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
  84. ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
  85. ll half(ll x) { return fdiv(x,2); }
  86.  
  87. template<class T, class U> T fstTrue(T lo, T hi, U f) {
  88.         // note: if (lo+hi)/2 is used instead of half(lo+hi) then this will loop infinitely when lo=hi
  89.         hi ++; assert(lo <= hi); // assuming f is increasing
  90.         while (lo < hi) { // find first index such that f is true
  91.                 T mid = half(lo+hi);
  92.                 f(mid) ? hi = mid : lo = mid+1;
  93.         }
  94.         return lo;
  95. }
  96. template<class T, class U> T lstTrue(T lo, T hi, U f) {
  97.         lo --; assert(lo <= hi); // assuming f is decreasing
  98.         while (lo < hi) { // find first index such that f is true
  99.                 T mid = half(lo+hi+1);
  100.                 f(mid) ? lo = mid : hi = mid-1;
  101.         }
  102.         return lo;
  103. }
  104. template<class T> void remDup(vector<T>& v) {
  105.         sort(all(v)); v.erase(unique(all(v)),end(v)); }
  106.  
  107. // INPUT
  108. template<class A> void re(complex<A>& c);
  109. template<class A, class B> void re(pair<A,B>& p);
  110. template<class A> void re(vector<A>& v);
  111. template<class A, size_t SZ> void re(array<A,SZ>& a);
  112.  
  113. template<class T> void re(T& x) { cin >> x; }
  114. void re(db& d) { str t; re(t); d = stod(t); }
  115. void re(ld& d) { str t; re(t); d = stold(t); }
  116. template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
  117.  
  118. template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; }
  119. template<class A, class B> void re(pair<A,B>& p) { re(p.f,p.s); }
  120. template<class A> void re(vector<A>& x) { trav(a,x) re(a); }
  121. template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); }
  122.  
  123. // TO_STRING
  124. #define ts to_string
  125. str ts(char c) { return str(1,c); }
  126. str ts(const char* s) { return (str)s; }
  127. str ts(str s) { return s; }
  128. str ts(bool b) {
  129.         #ifdef LOCAL
  130.                 return b ? "true" : "false";
  131.         #else
  132.                 return ts((int)b);
  133.         #endif
  134. }
  135. template<class A> str ts(complex<A> c) {
  136.         stringstream ss; ss << c; return ss.str(); }
  137. str ts(vector<bool> v) {
  138.         str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
  139.         res += "}"; return res; }
  140. template<size_t SZ> str ts(bitset<SZ> b) {
  141.         str res = ""; F0R(i,SZ) res += char('0'+b[i]);
  142.         return res; }
  143. template<class A, class B> str ts(pair<A,B> p);
  144. template<class T> str ts(T v) { // containers with begin(), end()
  145.         #ifdef LOCAL
  146.                 bool fst = 1; str res = "{";
  147.                 for (const auto& x: v) {
  148.                         if (!fst) res += ", ";
  149.                         fst = 0; res += ts(x);
  150.                 }
  151.                 res += "}"; return res;
  152.         #else
  153.                 bool fst = 1; str res = "";
  154.                 for (const auto& x: v) {
  155.                         if (!fst) res += " ";
  156.                         fst = 0; res += ts(x);
  157.                 }
  158.                 return res;
  159.  
  160.         #endif
  161. }
  162. template<class A, class B> str ts(pair<A,B> p) {
  163.         #ifdef LOCAL
  164.                 return "("+ts(p.f)+", "+ts(p.s)+")";
  165.         #else
  166.                 return ts(p.f)+" "+ts(p.s);
  167.         #endif
  168. }
  169.  
  170. // OUTPUT
  171. template<class A> void pr(A x) { cout << ts(x); }
  172. template<class H, class... T> void pr(const H& h, const T&... t) {
  173.         pr(h); pr(t...); }
  174. void ps() { pr("\n"); } // print w/ spaces
  175. template<class H, class... T> void ps(const H& h, const T&... t) {
  176.         pr(h); if (sizeof...(t)) pr(" "); ps(t...); }
  177.  
  178. // DEBUG
  179. void DBG() { cerr << "]" << endl; }
  180. template<class H, class... T> void DBG(H h, T... t) {
  181.         cerr << ts(h); if (sizeof...(t)) cerr << ", ";
  182.         DBG(t...); }
  183. #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
  184.         #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  185.         #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
  186.                  << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
  187. #else
  188.         #define dbg(...) 0
  189.         #define chk(...) 0
  190. #endif
  191.  
  192. int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}
  193. int max(int a,int b, int c){return max(a,max(b,c));}
  194. int min(int a,int b, int c){return min(a,min(b,c));}
  195. ll max(ll a,ll b, ll c){return max(a,max(b,c));}
  196. ll min(ll a,ll b, ll c){return min(a,min(b,c));}
  197. void dbga(int arr[], int n){vi v;For(i,n)v.pb(arr[i]);dbg(v);}
  198. void dbga(ll arr[], int n){vi v;For(i,n)v.pb(arr[i]);dbg(v);}
  199.  
  200. // FILE I/O
  201. void setIn(str s) { freopen(s.c_str(),"r",stdin); }
  202. void setOut(str s) { freopen(s.c_str(),"w",stdout); }
  203. void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
  204. void setIO(str s = "") {
  205.         unsyncIO();
  206.         // cin.exceptions(cin.failbit);
  207.         // throws exception when do smth illegal
  208.         // ex. try to read letter into int
  209.         if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
  210. }
  211. // 576743 29995400689069LL
  212. const int MOD = 1e9+7; // 998244353;
  213. const int MX = 1e3+5;
  214. const ll INF = 1e18;
  215. const ld PI = acos((ld)-1);
  216. const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
  217. mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
  218.  
  219.  
  220. ll pre[MX][MX],pre2[MX][MX];
  221.  
  222. void solve(){
  223.     int n,k;re(n,k);
  224.     int a[n], b[n];
  225.     For(i,n){
  226.         re(a[i]);
  227.         b[i]=k-a[i];
  228.     }
  229.     dbga(a,n);
  230.     For(i,n){
  231.         pre[i+1][i+1]=a[i];
  232.         pre2[i+1][i+1]=b[i];
  233.         FOR(j,i+1,n){
  234.             dbg(i+1,j+1,i,j,a[j],a[j-1]);
  235.             pre[i+1][j+1]=pre[i+1][j]+max(0,a[j]-a[j-1]);
  236.             pre2[i+1][j+1]=pre2[i+1][j]+max(0,b[j]-b[j-1]);
  237.         }
  238.     }
  239.  
  240.     ll dp[n+1];
  241.     For(i,n+1)dp[i]=INF;
  242.     dp[0]=0;
  243.     For(i,n){
  244.         For(j,i+1){
  245.             ckmin(dp[i+1],dp[j]+min(pre[j+1][i+1],pre2[j+1][i+1]));
  246.         }
  247.     }
  248.     ps(dp[n]);
  249. }
  250.  
  251. int main() {
  252.         setIO();
  253.         int t=1;
  254.         // re(t);
  255.         while(t--)solve();
  256.         // you should actually read the stuff at the bottom
  257. }
  258.  
  259. //psums, bsearch, two pointers
  260. /* stuff you should look for
  261.         * int overflow, array bounds
  262.         * special cases (n=1?)
  263.         * think of simple solution first
  264.         * do smth instead of nothing and stay organized
  265.         * WRITE STUFF DOWN
  266.         * DON'T GET STUCK ON ONE APPROACH
  267. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement