当前位置:网站首页>Hdu3507 (slope DP entry)
Hdu3507 (slope DP entry)
2022-07-03 00:39:00 【ccsu_ deer】
Reference from : Blog 1 Blog 2
Look at two monotonous queue maintenance dp Introductory questions :
Blog
The characteristics of this type of question : Divide into consecutive segments The shortest segment length is k, Transmitted by beacon fire dp:f[i]=min{f[k]}(i-m<=k<=i-1)+w[i]
Maintain incremental with monotonic queues f[i], Every time you take the first place in the team .
Look again. HDU3507 The meaning of the question :
The question :
Here you are. n Number , Ask you how to output in sequence , Minimize the total value .
The value calculation formula of each line is shown in the figure above , You can enter any character in each line here , You can put n Any character ( But in order ) Output to different lines ,
In fact, it is similar to cutting cake , Here you are. n A string , Ask you how to cut in the middle , You can cut any knife , Minimize the total value
It is also a continuous segment , But Duan Chang didn't ask .
The weight of each segment is :( Suppose this paragraph is k)
set up dp[i] For the former i The minimum value of the number .
So the transfer equation :
Obviously, we can't simply use the monotonic queue optimization above .
Consider slope optimization , The optimization process is a little complicated and difficult to write , I don't have to write it myself , Here are the screenshots of the blog source : Blog
#include < cstdio >
#include < cstring >
#include < cstdlib >
#include < algorithm >
#include < queue >
#include < vector >
#include < string >
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 5e5 + 10;
int que[maxn], dp[maxn];
int sum[maxn];
int n, m;
int up(int i,int j)
{
return dp[i]+sum[i]*sum[i] - (dp[j]+sum[j]*sum[j]);
}
int down(int i,int j)
{
return 2*(sum[i]-sum[j]);
}
int DP(int i,int j)
{
return dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+m;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
sum[0] = dp[0] = 0;
for (int i = 1; i < = n; i++)
scanf( "%d" , &sum[i]), sum[i] += sum[i - 1];
int tail = 0, head = 0;
que[tail++] = 0;// Because it may be the front i All of them as a segment is the minimum
for (int i = 1; i <= n; i++) {//head+1 < tail Ensure that there are at least two values in the queue
while(head+1 <tail & &up(que[head+1],que[head]) < = sum[i]*down(que[head+1],que[head])) head++;
//head+1 Than head better
dp[i] = DP(i, que[head]);
while (head+1 <tail & &up(i,que[tail-1])*down(que[tail-1],que[tail-2]) < = up(que[tail-1],que[tail-2])*down(i,que[tail-1])) tail--;
// Maintenance pocket , Convex hull has been proved to be inconsistent in the middle
que[tail++] = i;
}
printf( "%d\n" , dp[n]);
}
return 0;
}
————————————————
Copyright notice : This paper is about CSDN Blogger 「ccsu_deer」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_41286356/article/details/106267614
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
边栏推荐
- Bloom filter
- Graduation summary
- [shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
- 【雅思阅读】王希伟阅读P1(阅读判断题)
- Nc17059 queue Q
- Helm basic learning
- Is there a specific format for English papers?
- Pytorch 20 realizes corrosion expansion based on pytorch
- Rust字符串切片、结构体和枚举类
- Use Jenkins II job
猜你喜欢
Redis21 classic interview questions, extreme pull interviewer
Shell 实现文件基本操作(sed-编辑、awk-匹配)
Install docker and use docker to install MySQL
How do educators find foreign language references?
Which websites can I search for references when writing a thesis?
Introduction and use of ftrace tool
Detailed explanation of pod life cycle
MySQL 23 classic interview hanging interviewer
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a
随机推荐
DotNet圈里一个优秀的ORM——FreeSql
机器学习:numpy版本线性回归预测波士顿房价
logback配置文件
Nc20806 District interval
【雅思阅读】王希伟阅读P1(阅读判断题)
【luogu P4320】道路相遇(圆方树)
MySQL 23 classic interview hanging interviewer
Graduation summary
Markdown tutorial
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
线程的启动与优先级
文件操作IO-Part2
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
[shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
Extension of flutter
腾讯云免费SSL证书扩展文件含义
How SQLSEVER removes data with duplicate IDS
ftrace工具的介绍及使用
Multiprocess programming (V): semaphores