题目链接:

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. java LinkedBlockingQueue和ConcurrentLinkedQueue的区别

    实现上看,两者都继承于AbstractQueue,但是ConcurrentLinkedQueue实现了Queue,而LinkedBlockingQueue实现了BlockingQueue,Blocki ...

  2. winform(无边框窗体与timer)

    一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要放置的控件可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入(pictureBox1_MouseE ...

  3. SQLserver2008如何把表格变量传递到存储过程中

    在Microsoft SQL Server 2008中,你可以实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就可以被传递.下面我们来具体介绍如何把表格变量(包括内含的数据)传递到存储过程和 ...

  4. ArcGIS10.2下调试10.1的程序

    听说:10.2比10.1好,诚然,安装了10.2打开一看是这样的,以为是下载的版本问题,后来才以现是显示设置的问题. 因为,我使用的两个显示器,屏幕有点大,所以,就改成中等了,不然怎么可截取下面的截图 ...

  5. 关于oracle-12514错误的修改方法

    原因1: 打开文件"<OracleHome>/network/admin/listener.ora",添加 (SID_DESC =         (GLOBAL_DB ...

  6. Powershell Remove "Limited Access" - 金大昊(jindahao)

    对于有多级web获取getlist会报错:Exception calling “GetList” with “1” argument $SPWeb = Get-SPWeb -Identity http ...

  7. Python: 关于nose

    1. 使用rednose增强输出 pip install rednose nosetests --rednose tests 2. 使用coverage pip install coverage no ...

  8. Eclipse环境下配置spket中ExtJS5.0提示

    使用eclipse编写extjs时,一定会用到spket这个插件,spket可以单独当作ide使用,也可以当作eclipse插件使用,我这里是当作eclipse的插件使用的,下面来一步步图解说明如何配 ...

  9. iOS 清理缓存功能实现第一种方法

    添加一个提示框效果导入第三方MBProgressHUD #import "MBProgressHUD+MJ.h" /** * 清理缓存第一种方法 */ -(void)clearCa ...

  10. iOS 开发技巧-制作环形进度条

    有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...