[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),为序列中的元素。

输出格式:

只有一行,为一个整数,即将序列变成一个元素的最小代价。

输入输出样例

输入样例#1:

3
1
2
3

输出样例#1:

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;
}

  

原文地址:https://www.cnblogs.com/JYYHH/p/8810408.html

时间: 2024-07-30 05:52:57

[BOI2007] Sequence的相关文章

题解 P4393 【[BOI2007]Sequence 序列问题】

Solution: 对于每个很大的数,基于贪心,我们显然要让它合并次数尽可能少,以保证最终总代价最小. 如图: 考虑中间那个最大的 $maxn$,很显然,将其与其它的合并代价显然更大,所以,我们不可能先用 $maxn$ 去和其它的合并. 但是,由于最终目标长度为1,所以无论如何,$maxn$ 终将被合并,可以先考虑将比它小的全部合并,最后再与它合并,以减少它参与合并的次数. 不难想到,可以先将 $maxn$ 左边全部合并,再将其右边全部合并,最后,再让左右两部分与它合并,这样可以保证比先合并 $

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

Acdream 1427 Nice Sequence

Nice Sequence Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description Let us consider the sequence a1, a2,..., an of non-negative integer numbers. Denote as ci,j the number of occurrences of the number i a

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav