Riverside Curio
Riverside Curio
time limit per test1 second
memory limit per test256 megabytes
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi.
Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of days.
The second line contains n space-separated integers m1, m2, ..., mn (0 ≤ mi < i) — the number of marks strictly above the water on each day.
Output
Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.
Examples
input
6
0 1 0 3 0 2
output
6
input
5
0 1 2 1 2
output
1
input
5
0 1 1 2 2
output
0
Note
In the first example, the following figure shows an optimal case.

Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case.

这道题很妙啊~~~
我们可以假设\(t[i]\)表示第\(i\)天所有的水位线条数,显然\(t[i] = d[i] + 1 + m[i]\)
由于题目要求的是\(min(\sum d[i])\),我们就可以转化为求\(min(\sum t[i])\)
接下来我们来看看\(t\)这个数列应该满足什么条件:
\(t_i ≥ max(t_{i-1}, m_i + 1)\)
\(t_i ≥ t_{i+1}-1\)
由第二个条件可以推出\(t_i ≥ t_j-(j-i)\ \ (j>i)\)
那么我们来想一件事,是不是有一个合法的数列\(t\),就一定有一种对应的合法方案吗?
条件一看出,任意两个相邻的数的差小于等于1.(当然,后面的大于等于前面的)
显然,如果\(t[i]\)是递增的,那么没有任何问题。
如果有两个相邻的数相等时,说明新画的水位线和以前的一条重合了。
那么我们来思考一个问题,什么时候会产生矛盾。
当你发现你后面需要\(x\)根水位线,但是你当前的方案一共都没有这么多条的时候就矛盾了。
那么会不会出现这种情况呢?
这种情况是\(m[i] > t[i-1]\)的时候,由于\(t_i ≥ max(t_{i-1}, m_i + 1)\),所以\(t_i ≥ m_i+1\)
又因为相邻两个数的差不超过1,所以\(t_i ≥ t_{i-1}\),所以这种情况不存在。
简单的证明完毕。
接下来考虑代码实现问题:
我们要构造这个数列,也就是要满足上述条件。
那么我们可以这样构造
先让所有的数都等于\(m_i + 1\),然后遍历一遍
发现\(t_{i-1}>t_i, t_i = t_{i-1}\)
这样就满足条件一了。
紧接着去满足条件二,我们可以倒着遍历一遍,如果发现不满足条件的,就强行变成满足条件的最优解。
#include<bits/stdc++.h>
long long m[200000],t[200000],ans,n;
int main()
{
scanf("%I64d",&n);
for (int i=1;i<=n;++i) scanf("%I64d",&m[i]),t[i]=m[i]+1;
for (int i=2;i<=n;++i) if (t[i]<t[i-1]) t[i]=t[i-1];
for (int i=n;i>=2;--i) if (t[i-1]<t[i]-1) t[i-1]=t[i]-1;
for (int i=1;i<=n;++i) ans+=t[i]-1-m[i];
printf("%I64d",ans);
return 0;
}
Riverside Curio的更多相关文章
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
- [Codeforces947D]Riverside Curio(思维)
Description 题目链接 Solution 设S[i]表示到第i天总共S[i]几个标记, 那么满足S[i]=m[i]+d[i]+1 m[i]表示水位上的标记数,d[i]表示水位下的标记数 那么 ...
- codeforces round 472(DIV2)D Riverside Curio题解(思维题)
题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...
- 【Codeforces 924C】Riverside Curio
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设第i天总共的线数为t[i] 水平线上线数为m[i]是固定的 水平线下的线数设为d[i] 则d[i]+m[i]+1=t[i] 也就是说问题可以 ...
- CF957D Riverside Curio
dp+预处理 dp[i]表示第i天时的水位线有多少条, 然后你会发现这个dp是有后效性的,当第i天的m[i]>dp[i-1]时就要修改之前的dp值 因此我们预处理出每一天的至少要多少条水位线,记 ...
- {ICIP2014}{收录论文列表}
This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...
- Python标准模块--asyncio
1 模块简介 asyncio模块作为一个临时的库,在Python 3.4版本中加入.这意味着,asyncio模块可能做不到向后兼容甚至在后续的Python版本中被删除.根据Python官方文档,asy ...
- 读书笔记--SQL必知必会09--汇总数据
9.1 聚集函数 聚集函数(aggregate function),对某些行运行的函数,计算并返回一个值. 使用聚集函数可以汇总数据而不必将涉及的数据实际检索出来. 可利用标准的算术操作符,实现更高级 ...
- 读书笔记--SQL必知必会11--使用子查询
11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...
随机推荐
- C语言文件读写操作
C语言实现文件读写,注意区分几个方法: 写入: fwrite() //个人认为这个最好,可是实现写入任何数据类型,任何长度 fputs() //写入一个字符串,字符串长度不能太长,具体的长度未知,但估 ...
- EAN13条码的校验位的Excel算法
校验位原公式: 单元格=10-RIGHT(SUM(MID($B3,{1;2;3;4;5;6;7;8;9;10;11;12},1)*{1;3;1;3;1;3;1;3;1;3;1;3})) 简化公式: 单 ...
- openstack组件之nova
Nova常用命令 1.查看vm列表 nova listnova list --all 2.查看镜像列表 nova image-list 3.查看卷列表 nova voluma-list 4.查 ...
- MyCat(1.2)Mycat的安装
[0]基本环境 OS:CentOS7.5 Software envireonment:JDK1.7.0 Master Software:Mycat1.6.5 Linux Client:CRT 8.0 ...
- 前端每日实战:49# 视频演示如何用纯 CSS 创作一支诱人的冰棍
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vrxzMw 可交互视频教程 此视频 ...
- Java字节缓冲流和字符缓冲流学习
1.字节缓冲流 首先要明确一个概念:对文件或其他目标频繁的读写操作,效率低,性能差. 使用缓冲流的好处是,能够高效的读写信息,原理是将数据先缓冲起来,然后一起写入或者读取出来. BufferedInp ...
- hive之调优
1.简单的查询,就是只是select,不带count,sum,group by这样的,都不走map/reduce,直接读取hdfs文件进行filter过滤,即尽量让fetch task(当开启一个Fe ...
- pycharm html 注释
修改方式:如图修改成值None以后,command+/快捷键,html注释的符号就是<!-- 注释内容 -->:为Jinja2的时候,注释符号就是{# 注释内容 #} 修改成None时,H ...
- NET Core+win10+Jenkins+Gogs+open ssh持续集成
背景 阿里云测试环境一台,带宽1M跟不上,Jenkins安装一个插件耗时很长,于是想在本地搭建Jenkins服务,将生成的安装文件同步到目标服务器上. 技术点有: win10:本地环境是win10,测 ...
- 【leetcode】1017. Convert to Base -2
题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...