题目链接:

Beauty of Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 813    Accepted Submission(s): 379

Problem Description
Sequence is beautiful and the beauty of an integer sequence is defined as follows: removes all but the first element from every consecutive group of equivalent elements of the sequence (i.e. unique function in C++ STL) and the summation of rest integers is the beauty of the sequence.

Now you are given a sequence A of n integers {a1,a2,...,an}. You need find the summation of the beauty of all the sub-sequence of A. As the answer may be very large, print it modulo 109+7.

Note: In mathematics, a sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example {1,3,2} is a sub-sequence of {1,4,3,5,2,1}.

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤105), indicating the size of the sequence. The following line contains n integers a1,a2,...,an, denoting the sequence (1≤ai≤109).

The sum of values n for all the test cases does not exceed 2000000.

 
Output
For each test case, print the answer modulo 109+7 in a single line.
 
Sample Input
3
5
1 2 3 4 5
4
1 2 1 3
5
3 3 2 1 2
 
Sample Output
240
54
144
 
题意:
 
一个数列的美丽值为这个数列合并相邻的且相同的数后这个序列的和,现在给一个序列,求所有子序列的美丽值得和;
 
思路:
 
套路题,dp[i]表示以第i个为结尾的所有的序列的美丽值得和,那么就可以递推了
if a[j]==a[i],dp[i]+=dp[j];
else dp[i]+=dp[j]+num[j]*a[i];其中j属于[1,i-1],num[j]表示以j结尾的序列的个数;
这样会超时啊,用前缀和优化,然后开个map记录每个值结尾的序列的个数;
 
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
const LL mod=1e9+7;
LL dp[maxn],num,sum[maxn];
map<int,LL>mp;
int n,a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
mp.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sum[1]=a[1];num=1;mp[a[1]]=1;
for(int i=2;i<=n;i++)
{
dp[i]=(a[i]+sum[i-1])%mod;
dp[i]=(dp[i]+(num-mp[a[i]]+mod)%mod*a[i])%mod;
mp[a[i]]=(mp[a[i]]+num+1)%mod;
num=(num*2+1)%mod;
sum[i]=(sum[i-1]+dp[i])%mod;
}
printf("%lld\n",sum[n]);
}
return 0;
}

  

 

hdu-5496 Beauty of Sequence(递推)的更多相关文章

  1. Hdu 5496 Beauty of Sequence (组合数)

    题目链接: Hdu 5496 Beauty of Sequence 题目描述: 一个整数序列,除去连续的相同数字(保留一个)后,序列的和成为完美序列和.问:一个整数序列的所有子序列的完美序列和? 解题 ...

  2. HDU 5496 Beauty of Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence ...

  3. HDU 5496——Beauty of Sequence——————【考虑局部】

    Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  5. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  7. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  8. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  9. HDU 5965 三维dp 或 递推

    题意:= =中文题 思路一:比赛时队友想的...然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍. 定义dp[i][j][k],表示第i列, ...

随机推荐

  1. nginx服务器是怎么执行php脚本的?

    简单的说: fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解 析php脚本,即php-fpm相 当于一个动态应用服务器,从而实现nginx动态解析 ...

  2. mysql ALL_O_DIRECT引发的unaligned AIO/DIO导致hang

    公司内部有一套mysql环境,使用的是percona server分支(和其他几十套环境的版本.参数完全相同),就这套环境每隔两三天就会hang一次,关键hang的时候服务器cpu也就是百分之三四十, ...

  3. 个人总结 HTML+CSS

    从大一下学期接触,一直到今年,接触的时间也挺长的了,最近一些认识的盆友和同学说是想学习前端,自己也开始慢慢停下脚步,不再拼命地去学很多框架的东西,回归到基础,慢慢把基础打牢 很多知识碎片一直来不及整理 ...

  4. HTML 5 中的标准属性

    HTML 全局属性 HTML 属性赋予元素意义和语境. 下面的全局属性可用于任何 HTML 元素. (5)= HTML5 中添加的属性. 属性 描述 accesskey 规定激活元素的快捷键. cla ...

  5. 转发离线安装 Android Studio 更新

    1.在线更新 随着 Android Studio 的越来越完善与流行,无论从功能性,还是性能上,它正在成为广大 Android 开发者的首选.但是因为总所周知墙的原因,我们在 Android Stud ...

  6. 【读书笔记】iOS-开发技巧-三种收起键盘的方法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  7. UISegmentedControl的使用

    #import "SegmentedControlTestViewController.h" @implementation SegmentedControlTestViewCon ...

  8. CSS 类选择器(四)

    一.类选择器 类选择用使用"."(英文点号)进行标识,后面紧跟类名 如: .red{color:red;} 类样式可以应用于文档中的多个元素,这体现了CSS代码的可重用性,帮助用户 ...

  9. 【转】IOS高级教程1:处理1000张图片的内存优化

    转载请保留以下原文链接: http://my.oschina.net/taptale/blog/91894 一.项目需求 在实际项目中,用户在上传图片时,有时会一次性上传大量的图片.在上传图片前,我们 ...

  10. OpenGL ES学习笔记(三)——纹理

    首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <OpenGL ES学习笔记( ...