[BOI2007] Sequence
题目描述
对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列。这一操作的代价是max(ai,ai+1)。进行n-1次该操作后,可以得到一个长度为1的序列。
我们的任务是计算代价最小的reduce操作步骤,将给定的序列变成长度为1的序列。
输入输出格式
输入格式:
第一行为一个整数n( 1 <= n <= 1,000,000 ),表示给定序列的长度。
接下来的n行,每行一个整数ai(0 <=ai<= 1, 000, 000, 000),为序列中的元素。
输出格式:
只有一行,为一个整数,即将序列变成一个元素的最小代价。
输入输出样例
3
1
2
3
5
说明
提示 30%的测试数据 n<=500; 50%的测试数据 n <= 20,000。
发现只能是相邻的合并,并且一个数会在第一次与比它大的数合并时候消失,并且产生那个数的贡献。
所以我们就找一下一个数两边第一个大于它的数,加一下两者中的较小值即可。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000005;
ll sum=0;
int L[maxn],R[maxn];
int n,a[maxn],s[maxn],tp=0; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
} int main(){
scanf("%d",&n),fill(L+1,L+n+1,-1),fill(R+1,R+n+1,-1);
for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++){
while(tp&&a[s[tp]]<=a[i]) tp--;
if(tp) L[i]=a[s[tp]];
s[++tp]=i;
}
tp=0;
for(int i=n;i;i--){
while(tp&&a[s[tp]]<=a[i]) tp--;
if(tp) R[i]=a[s[tp]];
s[++tp]=i;
} for(int i=1;i<=n;i++) if(L[i]>=0||R[i]>=0){
if(L[i]<0) sum+=(ll)R[i];
else if(R[i]<0) sum+=(ll)L[i];
else sum+=(ll)min(L[i],R[i]);
} printf("%lld\n",sum);
return 0;
}
[BOI2007] Sequence的更多相关文章
- [BOI2007]Sequence 序列问题 BZOJ1345
题目描述 对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一 ...
- P4393 [BOI2007]Sequence 序列问题[贪心]
题目描述 对于一个给定的序列a1, -, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一 ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- xampp中php手动升级
http://windows.php.net/download/ //要下载的 里面有dll文件 http://www.php.net/downloads.php VC9 x86 ...
- HTML5拖放(drag和drog)
拖放(drag和drog)是HTML5的标准的组成部分,也是种常见的特性,意义为抓起一个元素放入到另外的一个位置,在HTML5中任何元素都可以被拖放,前题是要相关进行设置. 1.设置元素为可拖放,也就 ...
- shell脚本,配置文件加载顺序,以及什么时候加载。
在linux系统中,有/etc/profile,/etc/bashrc ,~/.bash_profile,~/bashrc这四个配置文件,这些文件,会自动的在某些时候加载,也就是点一下,一般都是些别名 ...
- 51nod 1264 线段相交——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1264 检查点的位置就行了,具体见注释. /* (a-c)×(d-c)*(d ...
- tkinter学习-事件绑定与窗口
阅读目录: 事件绑定 Toplevel组件 标准对话框 事件绑定: 说明:对于每个组件来说,可以通过bind()方法将函数或方法绑定到具体的事件上. 事件序列: 说明:用户需要使用bind()方法将具 ...
- C++简单年月日的功能实现
// C++年月日判断初步代码 #include <iostream> using namespace std; class Data { int year; int month; int ...
- python logging with yaml
Recently, I was made a service which can provide a simple way to get best model. so, i spent lot of ...
- vfs_caches_init函数解析
vfs_caches_init函数初始化VFS,下面梳理函数调用流程 start_kernel() -->vfs_caches_init_early(); -->dcache_init_e ...
- 使用Lucene的java api 写入和读取索引库
import org.apache.commons.io.FileUtils;import org.apache.lucene.analysis.standard.StandardAnalyzer;i ...
- Knockout v3.4.0 中文版教程-3-监控-通过监控创建视图模型(下)
6. 显式订阅监控 你通常不需要手动设置订阅,所以初学者应该跳过这一节. 对于高级用户,如果你想注册自己的订阅来监控通知变化,你可以使用 subscribe函数,比如: myViewModel.per ...