Codeforces1131G Most Dangerous Shark
Description
Original Problem

Chinese Translation
大概就是给你一个间隔为1的多米诺序列,推倒一个多米诺骨牌有个花费,求推倒所有多米诺骨牌的最小花费
Solution
这道题先处理出每一个点最左及最右可推倒的位置,这可以用栈维护
设以上位置为\(l_{i}\),\(r_{i}\)
接下来设\(f_{i}\)为第1~i个点全部倒下,且第i个点往左倒的最小花费
\(g_{i}\)为第1~i个点全部倒下,且第i个点往右倒的最小花费
先考虑\(f_{i}\)
显然,\(f_{i}=min(f_{l_{i}-1},g_{l_{i}-1})+cost_{i}\)
即第\(l_{i}-1\)之前的点都倒下再加上\(l_{i}\)到i倒下的花费
再考虑\(g_{i}\)
对于\(g_{i}\),初始肯定是手动放倒该点即$$g_{i}=min(g_{i-1},f_{i-1})+cost_{i}$$
用一个栈维护最小的能推倒i的\(g_{j}\)来更新\(g_{i}\)
因为j能影响i,那么i能影响的j都能影响,所以只有\(g_{i}<g_{j}\)时才需要将该点压入栈中
Code
#include <cstdio>
#include <algorithm>
#define M 10000001
#define N 250010
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
int n,m,i,q,id,mul,t,j,cnt,zhan[M],left[M],right[M],k[N],a[N],b[N],h[M],pl[N];
long long f[M],g[M],c[M];
int main()
{
open("shark");
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
{
scanf("%d",&k[i]);
pl[i]=cnt+1;
for (j=1;j<=k[i];j++)
scanf("%d",&a[++cnt]);
cnt=pl[i]-1;
for (j=1;j<=k[i];j++)
scanf("%d",&b[++cnt]);
}
pl[n+1]=cnt+1;
scanf("%d",&q);
for (i=1;i<=q;i++)
{
scanf("%d %d",&id,&mul);
for (j=pl[id];j<=pl[id+1]-1;j++)
{
h[++t]=a[j];
c[t]=(long long)b[j]*mul;
}
}zhan[1]=zhan[0]=1;
for (i=1;i<=m;i++)
{
left[i]=max(1,i-h[i]+1);
t=left[i];
while (left[i]<=zhan[zhan[0]])
{
if (!zhan[0]) break;
t=min(t,left[zhan[zhan[0]]]);
zhan[0]--;
}
left[i]=t;
zhan[++zhan[0]]=i;
}
zhan[1]=m;zhan[0]=1;
for (i=m;i>=1;i--)
{
right[i]=min(m,i+h[i]-1);
t=right[i];
while (right[i]>=zhan[zhan[0]])
{
if (!zhan[0]) break;
t=max(t,right[zhan[zhan[0]]]);
zhan[0]--;
}
right[i]=t;
zhan[++zhan[0]]=i;
}
f[1]=g[1]=c[1];
zhan[0]=zhan[1]=1;
for (i=2;i<=m;i++)
{
f[i]=min(f[left[i]-1],g[left[i]-1])+c[i];
while (right[zhan[zhan[0]]]<i && zhan[0]) zhan[0]--;
g[i]=min(g[i-1],f[i-1])+c[i];
if (!zhan[0]) zhan[++zhan[0]]=i;else
{
g[i]=min(g[i],g[zhan[zhan[0]]]);
if (g[i]<g[zhan[zhan[0]]]) zhan[++zhan[0]]=i;
}
}
printf("%lld",min(g[m],f[m]));
return 0;
}
Codeforces1131G Most Dangerous Shark的更多相关文章
- CodeForces 1131G. Most Dangerous Shark
题目简述:从左到右依次有$n \leq 10^7$个Domino骨牌,高度为$h_i$,手动推倒他的花费为$c_i$.每个骨牌之间的距离为$1$.一个骨牌可以被向左或者向右推倒.当第$i$个骨牌被推倒 ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces-541div2
https://www.cnblogs.com/31415926535x/p/10427505.html codeforces-1131A~G 这场很多题都很简单,,应该是要能至少做出4道的,,但是我 ...
- Codeforces Round #541
因为这次难得不在十点半(或是更晚),大家都在打,然后我又双叒叕垫底了=.= 自己对时间的分配,做题的方法和心态还是太蒻了,写的时候经常写一半推倒重来.还有也许不是自己写不出来,而是在开始写之前就觉得自 ...
- Codeforces 1131 (div 2)
链接:http://codeforces.com/contest/1131 A Sea Battle 利用良心出题人给出的图,不难看出答案为\(2*(h1+h2)+2*max(w1,w2)+4\)由于 ...
- 与"shark"相关的表达
The word shark can be used to describe someone who is tricky and uses other people. Shark这个单词可以用来形容一 ...
- System.Web.HttpRequestValidationException: A potentially dangerous Request.F
ASP.NET .0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F System.W ...
- 机器学习库shark安装
经过两天的折腾,一个对c++和机器学习库的安装都一知半解的人终于在反复安装中,成功的将shark库安装好了,小小纪念一下,多亏了卡门的热心帮忙. shark的安装主要分为以下几个部分: (1)下载 s ...
- ASP.NET 4.0 potentially dangerous Request.Form value was detected
A few days ago, while working on an ASP.NET 4.0 Web project, I got an issue. The issue was, when use ...
随机推荐
- OpenCV Error - Core.hpp header must be compiled as C++
在XCode 里编译OpenCV的时候,经常报如题类似的错误. 简单解决办法: 把 *.m 文件重命名为 *.mm 即可
- 每日一道 LeetCode (19):合并两个有序数组
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- JavaSwing关于GridBagLayout(网格袋布局)的使用
下面的链接有初步的介绍: https://blog.csdn.net/xietansheng/article/details/72814552 关于GridBagConstraints: GridBa ...
- nautilus pg autoscaler PG自动伸缩
链接地址:https://ceph.io/rados/new-in-nautilus-pg-merging-and-autotuning/ [root@controller ~]# ceph osd ...
- github 加速方法
登录网址:https://github.com.ipaddress.com/codeload.github.com#ipinfo 更改hosts:
- OpenStack虚拟机virtaulinterfance 网络设备在libvirt的代码梳理
nova创建虚机网卡实际设备的代码调用流程为 _create_domain_and_network---->plug_vifs-->LibvirtGenericVIFDriver.plug ...
- python的各种包安装地址
http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy-stack 这个网页里有python的所有包,whl的后缀是python压缩包的意思.在windows ...
- 兄弟,别再爬妹子图了整点JS逆向吧--陆金所密码加密破解
好久没有写爬虫文章了,今晚上得空看了一下陆金所登录密码加密,这个网站js加密代码不难,适合练手,篇幅有限,完整js代码我放在了这里从今天开始种树,不废话,直接开整. 前戏热身 打开陆金所网站,点击到登 ...
- 牛客网PAT练兵场-跟奥巴马一起编程
题目地址: 题意:无 /** * *作者:Ycute *时间:2019-11-14-21.29.07 *题目题意简单描述:模拟题输出 */ #include<iostream> #incl ...
- ARM开发板实现双系统引导的一种方法——基于迅为iTOP-4412开发板
前言 本文所用的uboot代码为迅为官方提供,开发板是迅为iTOP-4412开发板.本文如有错误,欢迎指正. 首先,我们确定一下系统启动的流程:首先启动uboot,uboot启动内核并挂载rootfs ...