GDUT_排位賽題解報告_第2場_B. Snakes

題目:

B. Snakes
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
According to legend, St. Patrick banished all of the snakes in Mooland over a thousand years ago. However, snakes have since made their way back to Mooland! St. Patrick’s day was on March 17, so Bessie is going to commemorate St. Patrick by banishing all of the snakes from Mooland once and for all. Bessie is equipped with a net to capture snakes distributed in N groups on a line (1≤N≤400). Bessie must capture every snake in every group in the order that the groups appear on the line. Each time Bessie captures a group, she can put the snakes in a cage and start with an empty net for the next group.

A net with size s means that Bessie can capture any group that contains g snakes, where g≤s. However, every time Bessie captures a group of snakes of size g with a net of size s, she wastes s−g space. Bessie’s net can start at any size and she can change the size of her net K times (1≤K<N).

Please tell Bessie the minimum amount of total wasted space she can accumulate after capturing all the groups.

Input
The first line contains N and K. The second line contains N integers, a1,…,aN, where ai (0≤ai≤106) is the number of snakes in the ith group.

Output
Output one integer giving the minimum amount of wasted space after Bessie captures all the snakes.

Example
inputCopy
6 2
7 9 8 2 3 2
outputCopy
3
Note
Bessie’s net starts at a size of 7. After she captures the first group of snakes, she changes her net to a size of 9 and keeps that size until the 4th group of snakes, when she changes her net to size 3. The total wasted space is (7−7)+(9−9)+(9−8)+(3−2)+(3−3)+(3−2)=3.

這個題目題意就是,給出一個序列,然後讓你畫k條水平線,要求水平線數字大於它蓋住的數字,求水平線面積 - 數字和 的最小值。

做法是一個dp,用dp[i][j]表示前i個數,變了j次時候求得的最小差值。
我們用前綴和qzh[i]來表示前i個數字和,可以容易求得dp[i][0]等於多少:
定義一個maxn意義爲最大值,邊更新邊賦值dp[i][0]=maxn*i - qzh[i]。

然後就可以開始轉移方程:dp[i][j]等於多少呢?

我們假設有dp[s][j-1],這個東西就是dp[i][j]的前身,也就是第s+1個數到第i個數的答案加上dp[s][j-1]就是dp[i][j]了,那麼這樣的話,我們就枚舉s,s到底是誰?
我們從j開始枚舉,因爲對於一個dp[i][j],j肯定是<=i的,i個數,j個水平線嘛,然後就是一個n^3的二維dp,只有400個數,那麼問題就解決了。
代碼:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜頭文件
using namespace std;
const int INF = 0x3f3f3f3f;//1.06e9大小
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define
int n,k;
int qzh[401];
int all[401];
int dp[401][401];
 
int main()
{
	scanf("%d %d",&n,&k);
	qzh[0]=0;
	for(int time=1;time<=n;time++)
	{
		scanf("%d",&all[time]);
		qzh[time]=qzh[time-1]+all[time];
	}
	for(int time=1;time<=n;time++)
	{
		for(int time1=1;time1<=n;time1++)
		{
			dp[time][time1]=INF;
		}
	}
	int maxn=all[1];
	for(int time=1;time<=n;time++)
	{
        maxn=max(maxn,all[time]);
        dp[time][0]=time*maxn-(qzh[time]-qzh[0]);
    }
    for(int j=1;j<=k;j++)
	{
        for(int i=j+1;i<=n;i++)
	{
            maxn=all[i];
            for(int s=i-1;s>=j;s--)
            {
                maxn=max(maxn,all[s+1]);
                dp[i][j]=min(dp[i][j],dp[s][j-1]+maxn*(i-s)-(qzh[i]-qzh[s]));
            }
        }
    }
 
    printf("%d\n",dp[n][k]);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章