码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1049: [HAOI2006]数字序列

时间:2018-06-28 19:58:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:ble   long   AC   vector   tps   algo   链接   bzoj   #define   

题目链接

bzoj1049: [HAOI2006]数字序列

题解

第二问结论题,好神呐
emmmmm
我们一起去%ydc吧
技术分享图片
https://pan.baidu.com/share/link?uk=2651016602&shareid=1490516411

代码

#include<cstdio> 
#include<vector> 
#include<cstring> 
#include<algorithm> 
inline int read() { 
    int x = 0,f = 1; 
    char c = getchar(); 
    while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar(); } 
    while(c <= '9' && c >= '0')x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
#define LL long long
#define INF (1e17) 
const int maxn = 35007; 
int n,dp[maxn]; 
LL a[maxn],b[maxn],c[maxn],Ans[maxn],sumbe[maxn],sumaf[maxn];  
inline int find(int x) { 
    int l = 1,r = n,ret = 0; 
    while(l <= r) { 
        int mid = l + r >> 1;
        if(c[mid] <= x)l = mid + 1,ret = mid; 
        else r = mid - 1; 
    } 
    return ret; 
} 
std::vector<int>vec[maxn]; 
void solve() { 
    for(int i = 1;i <= n;++ i) 
        dp[i] = find(b[i]) + 1,c[dp[i]] = std::min(c[dp[i]],b[i]); 
    int ans = 0; 
    for(int i = 1;i <= n;++ i) ans = std::max(ans,dp[i]); b[0] = -INF;
    printf("%d\n",n - ans); vec[0].push_back(0);
    for(int i = 1;i <= n;++ i) {
        int u = dp[i] - 1; 
        for(int j = 0;j < vec[u].size();++ j) { 
            int v = vec[u][j];if(b[i] < b[v])continue; 
            sumbe[v - 1] = sumaf[v - 1] = 0; 
            for(int k = v;k <= i;++ k) sumbe[k] = abs(b[k] - b[v]),sumaf[k] = abs(b[k] -b[i]); 
            for(int k = v + 1;k <= i;++ k) sumbe[k] += sumbe[k - 1],sumaf[k] += sumaf[k - 1]; 
            for(int k = v;k <= i;++ k) 
                Ans[i] = std::min(Ans[i],Ans[v] +  sumbe[k] - sumbe[v] + sumaf[i] - sumaf[k]); 
        } 
        vec[dp[i]].push_back(i); 
    } 
    printf("%lld\n",Ans[n]); 
} 
int main() { 
    n = read();  
    for(int i = 1;i <= n;++ i) a[i] = read(),b[i] = a[i] - i; 
    a[++ n] = INF; b[n] = a[n] - n; 
    for(int i = 1;i <= n;++ i) c[i] = Ans[i] = INF; 
    solve(); 
    return 0; 
} 

bzoj1049: [HAOI2006]数字序列

标签:ble   long   AC   vector   tps   algo   链接   bzoj   #define   

原文地址:https://www.cnblogs.com/sssy/p/9240350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!