【BZOJ】4709: [Jsoi2011]柠檬
4709: [Jsoi2011]柠檬
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 779 Solved: 310
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2
2
5
2
3
Sample Output
//Flute 先从左端取下 4 只贝壳,它们的大小为 2, 2, 5, 2。选择 s0 = 2,那么这一段
里有 3 只大小为 s0 的贝壳,通过魔法可以得到 2×3^2 = 18 只柠檬。再从右端取下最后一
只贝壳,通过魔法可以得到 1×3^1 = 3 只柠檬。总共可以得到 18 + 3 = 21 只柠檬。没有
比这更优的方案了。
HINT
Source
还没有学会斜率优化...可以直接单调栈,参考neither_nor的博客,在代码里面写了一点注释:
#include<iostream>
#include<cstdio>
#include<vector>
#define ll long long
using namespace std; int n, s, cnt[], pre[], color[];
vector < int > stk[];
ll f[]; ll count ( int x, int num ) {
return f[x-] + (ll)color[x] * num * num;
} int find ( int x, int y ) {//x,y是需要判断的下标,find的是x超过y的位置
int l = , r = n, res = n + ;
while ( l <= r ) {
int mid = ( l + r ) >> ;
if ( count ( x, mid - pre[x] + ) >= count ( y, mid - pre[y] + ) ) {//mid查找的是当前颜色的第几个入栈时间
res = mid; r = mid - ;
} else l = mid + ;
}
return res;
} int main ( ) {
scanf ( "%d", &n );
for ( int i = ; i <= n; i ++ ) {
scanf ( "%d", &s );
cnt[s] ++;
color[i] = s;
pre[i] = cnt[s];
while ( stk[s].size ( ) >= && find ( stk[s][stk[s].size ( ) - ], stk[s][stk[s].size ( ) - ] ) <=
find ( stk[s][stk[s].size ( ) - ], i ) ) stk[s].pop_back ( );//防止第二个元素超过栈顶的时间比栈顶超过i的时间早,此时栈顶没用
stk[s].push_back ( i );//先推入防止最后从0更新
while ( stk[s].size ( ) >= && find ( stk[s][stk[s].size ( ) - ], stk[s][stk[s].size ( ) - ] ) <=
pre[i] ) stk[s].pop_back ( );//i是这种颜色第pre[i]个入栈的 //第二个元素超过栈顶的时间比第一个元素进入的时间早,栈顶就没用了
f[i] = count ( stk[s][stk[s].size ( ) - ], pre[i] - pre[stk[s][stk[s].size ( ) - ]] + );
}
ll ans = ;
for ( int i = ; i <= n; i ++ )
ans = max ( ans, f[i] );
printf ( "%lld\n", ans );
return ;
}
【BZOJ】4709: [Jsoi2011]柠檬的更多相关文章
- bzoj 4709 [Jsoi2011]柠檬——单调栈二分处理决策单调性
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 题解:https://blog.csdn.net/neither_nor/articl ...
- bzoj 4709: [Jsoi2011]柠檬
Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,000) 只,按顺序串在树枝上.为了方便,我们从 ...
- bzoj 4709 [ Jsoi2011 ] 柠檬 —— 斜率优化DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 课上讲的题,还是参考了博客...:https://www.cnblogs.com/GX ...
- 4709: [Jsoi2011]柠檬
4709: [Jsoi2011]柠檬 https://www.lydsy.com/JudgeOnline/problem.php?id=4709 分析: 决策单调性+栈+二分. 首先挖掘性质:每个段选 ...
- 【BZOJ 4709】柠檬 斜率优化dp+单调栈
题意 给$n$个贝壳,可以将贝壳分成若干段,每段选取一个贝壳$s_i$,这一段$s_i$的数目为$num$,可以得到$num^2\times s_i$个柠檬,求最多能得到几个柠檬 可以发现只有在一段中 ...
- bzoj4709: [Jsoi2011]柠檬 斜率优化
题目链接 bzoj4709: [Jsoi2011]柠檬 题解 斜率优化 设 \(f[i]\) 表示前 \(i\)个数分成若干段的最大总价值. 对于分成的每一段,左端点的数.右端点的数.选择的数一定是相 ...
- 【BZOJ4709】[Jsoi2011]柠檬 斜率优化+单调栈
[BZOJ4709][Jsoi2011]柠檬 Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N ≤ 100,0 ...
- 【LG5504】[JSOI2011]柠檬
[LG5504][JSOI2011]柠檬 题面 洛谷 题解 考虑\(dp\),令\(f_i\)表示\(dp\)到第\(i\)位且在第\(i\)位分段的最大值. 我们令题面中的\(s_i\)为\(a_i ...
- 笔记-[JSOI2011]柠檬
笔记-[JSOI2011]柠檬 [JSOI2011]柠檬 \(f_i\) 表示到第 \(i\) 只贝壳最多可以换得的柠檬数. 令 \(c_i=\sum_{h=1}^i[s_h=s_i]\). \[\b ...
随机推荐
- Vue 传递
今天刷了一遍Vue的API,做个小笔记 父子传递数据时,父组件里标记要传的数据,子组件里用props获取,子组件用$emit('func',args)发布事件,父组件用@func接收. 方法一 par ...
- nesC编程入门
1.接口 NesC程序主要由各式组件(component)构成,组件和组件之间通过特定的接口(interface)互相沟通.一个接口内声明了提供相关服务的方法(C语言函数).例如数据读取接口(Read ...
- 初窥ThinkPHP
MVC全称(Model View Controller) Model:模型(可以理解位数据库操作模型) View:视图(视图显示) Controller:(控制器) 简单的说框架就是一个类的集合.集合 ...
- 在Linux(CentOS)中安装.netcore SDK
官方链接 :https://dotnet.microsoft.com/download/linux-package-manager/centos/sdk-current 可以直接根据官方链接,选择Li ...
- PIL图片合成旋转缩放
用PIL实现图片的旋转,缩放,合成 我们需要知道合成位置的中心点坐标,用中心点坐标,不使用左顶点的坐标是由于缩放过程容易计算. 假设A是局部透明的图片,我们希望把B放在A的底部,仅从A的透明部分显示B ...
- GitBash、EGit、SourceTree三个Git管理工具对比
Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...
- centos 下单独安装mysql
https://www.cnblogs.com/running-mydream/p/4666094.html https://www.cnblogs.com/lzj0218/p/5724446.htm ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【PAT】1014. 福尔摩斯的约会 (20)
1014. 福尔摩斯的约会 (20) 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hy ...
- linq to sql: 在Entityfamework Core中使用多个DbContext
最近在学习DotNetCore并做一个自己的小项目,分为了多个数据库,AccountDbContext和BlogDbContext, 发blog的时候需要用到Account的信息,但是再Blog中只记 ...