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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值