【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 ...
随机推荐
- 天梯赛 L2-009 抢红包
题目链接 没有人没抢过红包吧-- 这里给出N个人之间互相发红包.抢红包的记录,请你统计一下他们抢红包的收获. 输入格式: 输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数 ...
- Linux增加swap文件
起因 在阿里云搞了台ECS,但是内存就1个G,操作总是悲剧的卡卡卡,于是就想着增加一点交换文件来缓解一下. 快速添加交换文件 step 1. 生成文件 先填充一个大文件,等会儿当做交换文件用: dd ...
- 使用JSON语法创建JS对象(重要)
JS对象的键值可以加单引号或者不加或者加双引号 JSON语法提供了一种更简单的方式来创建对象,可以避免书写函数,也可避免用new关键字,可以直接创建一个JS对象,使用一个花括号,然后将每个属性写成&q ...
- arch优化开机
查看开机时间 systemd-analyze 具体开机时间 systemd-analyze blame 你可以systemctl --all | grep not-found 查看有哪些服务挂掉了.然 ...
- [006] largest_common_substring
[Description] Given two different strings, find the largest successive common substring. e.g. str1[] ...
- STL hashtable阅读记录
unordered_map,unordered_set等相关内容总结: unordered_map和unordered_set是在开发过程中常见的stl数据结构.其本质是hashtable.在SGI_ ...
- React 16 源码瞎几把解读 【三 点 二】 react中的fiberRoot
〇.先来看看常用的常量 NoWork = 0 noTimeout = undefined HostRoot = 3 NoContext = 0b000; AsyncMode = 0b001; Stri ...
- Android 开发笔记(二)菜单设计
菜单设计一 // 创建菜单 public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "关于"); men ...
- vsftpd.log内容的意义
vsftpd日志(xferlog格式)的含义 引用: Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a ...
- HTTP协议头注射漏洞实例
HTTP 响应头文件中包含未经验证的数据会引发 cache-poisoning.cross-site scripting.cross-user defacement.page hijacking.co ...