zoukankan      html  css  js  c++  java
  • 【题解】SDOI2016征途

      就放个代码吧……实在是太套路了。不过据说有复杂度还要低很多的算法,不知道是怎么做呀……

    #include <bits/stdc++.h>
    using namespace std;
    #define maxn 10000
    #define int long long
    #define db double
    int n, m, head, tail, sum[maxn];
    int dp[2][maxn], q[maxn], X[2][maxn], Y[2][maxn];
    
    int read()
    {
        int x = 0, k = 1;
        char c;
        c = getchar();
        while(c < '0' || c > '9') { if(c == '-') k = -1; c = getchar(); }
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * k;
    }
    
    db Get_S(int a, int b, int opt)
    {
        return (db) (Y[opt][a] - Y[opt][b]) / (db) (X[opt][a] - X[opt][b]);
    }
    
    signed main()
    {
        n = read(); m = read();
        for(int i = 1; i <= n; i ++) sum[i] = read() + sum[i - 1];
        int now = 1, pre = 0;
        for(int i = 1; i <= m; i ++)
        {
            memset(dp[now], -1, sizeof(dp[now]));
            if(i != 1) head = 1, tail = 0;
            for(int j = 1; j <= n; j ++)
            {        
                while(head + 1 <= tail && Get_S(q[head], q[head + 1], pre) < (db) sum[j]) head ++;
                if(head <= tail) dp[now][j] = dp[pre][q[head]] + (sum[j] - sum[q[head]]) * (sum[j] - sum[q[head]]);
                X[now][j] = 2 * sum[j], Y[now][j] = dp[now][j] + sum[j] * sum[j];
                if(dp[pre][j] == -1) continue;
                while(tail - 1 >= head && Get_S(q[tail], q[tail - 1], pre) > Get_S(q[tail], j, pre)) tail --;
                q[++ tail] = j;
            }
            swap(now, pre);
        }
        int ans = dp[pre][n] * m - sum[n] * sum[n];
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    最长公共子序列
    最长重复子串—后缀数组
    最长递增子序列
    最长重复子串(转)
    最长递增子序列(转)
    最长不重复子串(转)
    连续子数组最大和(转)
    alert 与 console.log
    一个null,让浏览器SB
    javascript实现简单的动画功能
  • 原文地址:https://www.cnblogs.com/twilight-sx/p/9041002.html
Copyright © 2011-2022 走看看