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;
    }
    
  • 相关阅读:
    JS---案例:tab切换效果
    .net core 使用MD5加密解密字符串
    c#实战开发:用.net core开发一个简单的Web以太坊钱包 (六)
    c#实战开发:以太坊Geth 命令发布智能合约 (五)
    c#实战开发:以太坊Geth 常用命令 (四)
    c#实战开发:以太坊钱包快速同步区块和钱包卡死解决方案 (三)
    c#实战开发:以太坊钱包对接私链 (二)
    c# API接受图片文件以文件格式上传图片
    c# API接受图片文件以Base64格式上传图片
    命令查看当前电脑安装所有版本.NET Core SKD
  • 原文地址:https://www.cnblogs.com/graytido/p/11714022.html
Copyright © 2011-2022 走看看