1548: 【例】【入门】前缀最大值

内存限制:16 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:8 解决:6

题目描述

求一个数列的所有前缀最大值之和。

即:给出长度为 lns="http://www.w3.org/1998/Math/MathML">n 的数列 lns="http://www.w3.org/1998/Math/MathML">ai,求出对于所有 lns="http://www.w3.org/1998/Math/MathML">1≤i≤n,lns="http://www.w3.org/1998/Math/MathML">max(a1,a2,...,ai) 的和。

比如,有数列:lns="http://www.w3.org/1998/Math/MathML">666 lns="http://www.w3.org/1998/Math/MathML">304 lns="http://www.w3.org/1998/Math/MathML">692 lns="http://www.w3.org/1998/Math/MathML">188 lns="http://www.w3.org/1998/Math/MathML">596,前缀最大值为:lns="http://www.w3.org/1998/Math/MathML">666 lns="http://www.w3.org/1998/Math/MathML">666 lns="http://www.w3.org/1998/Math/MathML">692 lns="http://www.w3.org/1998/Math/MathML">692 lns="http://www.w3.org/1998/Math/MathML">692,和为 lns="http://www.w3.org/1998/Math/MathML">3408。

对于每个位置的前缀最大值解释如下:对于第 lns="http://www.w3.org/1998/Math/MathML">1 个数 lns="http://www.w3.org/1998/Math/MathML">666 ,只有一个数,一定最大;对于第 lns="http://www.w3.org/1998/Math/MathML">2 个数,求出前两个数的最大数,还是 lns="http://www.w3.org/1998/Math/MathML">666 ;对于第 lns="http://www.w3.org/1998/Math/MathML">3 个数,求出前 lns="http://www.w3.org/1998/Math/MathML">3 个数的最大数是 lns="http://www.w3.org/1998/Math/MathML">692… 其余位置依次类推,最后求前缀最大值得和。

由于读入较大,数列由随机种子生成。

其中 lns="http://www.w3.org/1998/Math/MathML">a[1]=x,lns="http://www.w3.org/1998/Math/MathML">a[i]=(379×a[i−1]+131)mod997。(lns="http://www.w3.org/1998/Math/MathML">mod 代表求余数)

输入

一行两个正整数 lns="http://www.w3.org/1998/Math/MathML">n, lns="http://www.w3.org/1998/Math/MathML">x ,分别表示数列的长度和随机种子。(lns="http://www.w3.org/1998/Math/MathML">n≤100000,lns="http://www.w3.org/1998/Math/MathML">x<997)

输出

一行一个正整数表示该数列的前缀最大值之和。

样例输入 复制

5 666

样例输出 复制

3408

提示

样例解释

数列为 lns="http://www.w3.org/1998/Math/MathML">666,304,692,188,596,前缀最大值为lns="http://www.w3.org/1998/Math/MathML">666,666,692,692,692,和为 lns="http://www.w3.org/1998/Math/MathML">3408。


#include<bits/stdc++.h>
using namespace std;
int a[100100],dp[100100],n,i,x,s=0;
int main(){
    cin>>n>>x;
	a[1]=x;
	dp[1]=a[1];
	s=dp[1];
	for (i = 2;i <=n ;i++) {
		a[i]=(379*a[i-1]+131)%997;
		dp[i]=max(dp[i-1],a[i]);
		s=s+dp[i];
	}
	cout<<s; 
	return 0;
}