zoukankan      html  css  js  c++  java
  • HDU -3507 Print Article(斜率优化)

    Print Article

    Problem Description

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
    One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
    此处输入图片的描述
    M is a const number.
    Now Zero want to know the minimum cost in order to arrange the article perfectly.

    Input

    There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.

    Output

    A single number, meaning the mininum cost to print the article.

    Sample Input

    5 5
    5
    9
    5
    7
    5

    Sample Output

    230
    第一道斜率优化题,对于边界的处理还要注意,看了很多dalao的代码才写完..
    主要就是利用单调队列维护一个下凸包,具体推导过程网上的题解也都很清晰...我的都在纸上而且字太丑了

    代码

    #include <bits/stdc++.h>
    using namespace std;
    /*    freopen("k.in", "r", stdin);
        freopen("k.out", "w", stdout); */
    //clock_t c1 = clock();
    //std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #define de(a) cout << #a << " = " << a << endl
    #define rep(i, a, n) for (int i = a; i <= n; i++)
    #define per(i, a, n) for (int i = n; i >= a; i--)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> PII;
    typedef pair<double, double> PLL;
    typedef vector<int, int> VII;
    #define inf 0x3f3f3f3f
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const ll MAXN = 500010;
    const ll MAXM = 5e6 + 7;
    const ll MOD = 1e9 + 7;
    const double eps = 1e-6;
    const double pi = acos(-1.0);
    int dp[MAXN];
    int q[MAXN];
    int sum[MAXN];
    int head, tail, n, m;
    //dp[i]=min(dp[j]+(sum[i]-sum[j])^2)
    int Getdp(int i, int j)
    {
    	return dp[j] + m + (sum[i] - sum[j]) * (sum[i] - sum[j]);
    }
    int Getup(int j, int k)
    {
    	return dp[j] + sum[j] * sum[j] - (dp[k] + sum[k] * sum[k]);
    }
    int Getdown(int j, int k)
    {
    	return sum[j] - sum[k];
    }
    int main()
    {
    	while (~scanf("%d%d", &n, &m))
    	{
    		dp[0] = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			int x;
    			scanf("%d", &x);
    			sum[i] = sum[i - 1] + x;
    		}
    		head = tail = 0;
    		q[tail++] = 0;
    		//单调队列维护下凸包
    		for (int i = 1; i <= n; i++)
    		{
    			while (head + 1 < tail && Getup(q[head + 1], q[head]) <= 2 * sum[i] * Getdown(q[head + 1], q[head]))
    				head++;
    			dp[i] = Getdp(i, q[head]);
    			while (head + 1 < tail && Getup(i, q[tail - 1]) * Getdown(q[tail - 1], q[tail - 2]) <= Getup(q[tail - 1], q[tail - 2]) * Getdown(i, q[tail - 1]))
    				tail--;
    			q[tail++] = i;
    		}
    		printf("%d
    ", dp[n]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    【HCIE-RS_TAC诊断5-2】
    【HCIE-RS_TAC诊断5-1】
    【HCIE-RS_TAC诊断4】
    【HCIE-RS_TAC诊断3】
    【HCIE-RS_TAC诊断2】
    华为ICT大赛辅导——双AC主备双链路备份
    shell 函数与内置变量
    CF505E Mr. Kitayuta vs. Bamboos
    CF559E Gerald and Path
    CF538H Summer Dichotomy
  • 原文地址:https://www.cnblogs.com/graytido/p/11714022.html
Copyright © 2011-2022 走看看