Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #define task "CWALKWAY"
- using namespace std;
- using ll = long long;
- using ld = long double;
- template<class T>
- void read(T &x){
- register int c;
- x = 0;
- while((c = getchar()) && c < '0' || c > '9');
- for(; c >= '0' && c <= '9'; c = getchar())
- x = x * 10 + c - '0';
- }
- const int N = 1e6 + 2;
- int n;
- ll c, a[N];
- void Read(){
- read(n); read(c);
- for(int i = 1; i <= n; ++i)
- read(a[i]);
- }
- vector<int> line;
- vector<ld> point;
- ll dp[N];
- const ll Inf = 1e15 + 7;
- int last = 0;
- ll bp(ll v){
- return v * v;
- }
- ld f(int i, int j){
- return (ld) 1.0 * (dp[i - 1] + bp(a[i]) - dp[j - 1] - bp(a[j])) / (-2ll * a[j] + 2ll * a[i]);
- }
- void Solve(){
- line.push_back(1);
- point.push_back(-Inf);
- dp[1] = c;
- dp[0] = 0;
- for(int i = 2; i <= n; ++i){
- while(last < point.size() && point[last] < a[i])
- ++last;
- --last;
- dp[i] = c + dp[i - 1];
- dp[i] = min(dp[i], c + bp(a[i]) + bp(a[line[last]]) + dp[line[last] - 1] - 2ll * (a[line[last]]) * a[i]);
- while(line.size() > 1){
- if( (a[i] == a[line.back()] && dp[i - 1] < dp[line.back() - 1]) ||
- (a[i] != a[line.back()] && f(i, line[line.size() - 1]) <= f(i, line[line.size() - 2])))
- {
- line.pop_back();
- point.pop_back();
- continue;
- }
- break;
- }
- last = min(last, (int)point.size() - 1);
- if(a[i] != a[line.back()] || dp[i - 1] < dp[line.back() - 1]){
- point.push_back(f(i, line.back()));
- line.push_back(i);
- }
- }
- cout << dp[n];
- }
- main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".INP", "r", stdin),
- freopen(task".OUT", "w", stdout);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment