链接:https://ac.nowcoder.com/acm/contest/888/B
来源:牛客网

题目描述

Gromah and LZR have entered the second level. There is a sequence a1,a2,⋯ ,an on the wall.

There is also a note board saying "the beauty value of a sequence is the number of different elements in the sequence".

LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.

Please help them determine the password!

输入描述:

The first line contains one positive integer n denoting the length of the sequence.

The second line contains n positive integers a1,a2,⋯ ,an, denoting the sequence.

1≤ai≤n≤10^5

输出描述:

Print a non-negative integer in a single line, denoting the answer.

示例1

输入

4
1 2 1 3

输出

18

说明

The beauty values of subintervals [1,1],[2,2],[3,3],[4,4] are all 1.
The beauty values of subintervals [1,2],[1,3],[2,3],[3,4]are all 2​.
The beauty values of subintervals [1,4],[2,4]are all 3​.
As a result, the sum of all beauty values are 1×4+2×4+3×2=18.

题解:

题目让求所有的子区间不同数字个数的和。

用dp[j]表示以j为右区间的所有的子区间的不同数字的个数的和。

那么我们考虑如何状态转移:

考虑如何由dp[j]推dp[j+1]. 以j为右区间的所有子区间加上a[j+1]就都变成了以j+1为右区间,如果不考虑第j+1个数字的贡献,那么

dp[j+1]=dp[j],然后考虑a[j+1]对多少子区间产生了贡献,应该是j+1-pos[a[j+1]],   pos记录每个数字最后出现的位置。

所以dp[j]=dp[j-1]+j-pos[a[j]].

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
ll dp[maxn];
int pos[maxn];
int main(){int n,x;ll res=0;scanf("%d",&n);for(int i=1;i<=n;++i){scanf("%d",&x);dp[i]=dp[i-1]+(i-pos[x]);pos[x]=i;res+=dp[i];}printf("%lld\n",res);return 0;
}

我队友刚开始给我翻译题意的时候给我翻译成了 :求所有子区间不同数字的和,把个数两个字没翻译出来,如果是这个题意,状态转移方程应该为: dp[i]=dp[i-1]+(i-pos[x])*a[i];

2019牛客多校第八场 BBeauty Values dp相关推荐

  1. 2019牛客多校第八场

    A.All-one Matrices 按行枚举(即枚举下边界) e.g.e.g.e.g. 000000000000000 001100011000110 001000010000100 0111101 ...

  2. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  3. 2019牛客多校训练营第一场 H题 HOR 题解

    题目描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

  4. 2019牛客多校 第七场 B Irreducible Polynomial 多项式因式分解判断

    链接:https://ac.nowcoder.com/acm/contest/887/B 来源:牛客网 Irreducible Polynomial 时间限制:C/C++ 1秒,其他语言2秒 空间限制 ...

  5. 2019牛客多校训练营第一场 E题 ABBA 题解

    问题描述: 输入描述: 输出描述: 示例1: 题解: 更多问题可关注牛客竞赛区,一个刷题.比赛.分享的社区. 传送门:https://ac.nowcoder.com/acm/contest/discu ...

  6. 2021牛客多校第八场补题 D-OR

    链接:https://ac.nowcoder.com/acm/contest/11259/D 来源:牛客网 题目描述 There are two sequences of length n−1n-1n ...

  7. 2019牛客多校第七场 C Governing sand

    因为当时时间不怎么够就没写... 其实就是一个模拟题而已下面注释很清楚 链接:https://ac.nowcoder.com/acm/contest/887/C 来源:牛客网 时间限制:C/C++ 3 ...

  8. 2019牛客多校第四场 B xor (线性基求交)

    xor 思路 题目是要求[l,r][l, r][l,r]的所有集合是否都可以得到xxx,那么显然我们可以对这[l,r][l, r][l,r]个线性基求交,然后再特判能否xxx能否插入,如果能插入,显然 ...

  9. 2019牛客多校第三场 F.Planting Trees

    题目链接 题目链接 题解 题面上面很明显的提示了需要严格\(O(n^3)\)的算法. 先考虑一个过不了的做法,枚举右下角的\((x,y)\),然后二分矩形面积,枚举其中一边,则复杂度是\(O(n^3 ...

  10. 2019牛客多校第七场E Find the median 权值线段树+离散化

    Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...

最新文章

  1. 四轴飞行器实践教程(内部资料)
  2. [置顶] OAuth工作原理随想——让你的系统提供的服务更加安全
  3. 基于MATLAB的面向对象编程(4)——类文件
  4. 在创业之路上不断创新
  5. [转]linux awk命令详解
  6. NEERC2017 Archery Tournament 线段树 新套路
  7. 四个关键步骤,精进算法和数据结构 ​
  8. c语言字符数组不写,C语言数组
  9. php sql好处,关于php:使用Doctrine与SQL相比,使用DQL有什么好处?
  10. Dell Latitude 3490 使用 UEFI+GPT 安装 Win7 x64
  11. 基于双麦克风声源定位的视频跟踪
  12. 再次思考 classpath 环境变量 等
  13. 企业Web应用中的敏捷测试和瀑布测试
  14. 基于LM2596开关稳压电源控制设计(毕业设计整套资料)
  15. in use 大学英语4word_(完整word版)全新版大学英语第四册综合教程课后翻译答案及课文译文...
  16. 计算机系统具有运行可靠性,计算机系统的可靠性技术
  17. port isolate enable命令
  18. 二级分销系统平台软件开发
  19. 微信小程序实现微信APP上的扫一扫扫码跳到小程序对应的结果页面和签字等功能
  20. SharpDevelop出现未找到resgen.exe问题的解决办法

热门文章

  1. 最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
  2. Kafka的入门级API应用
  3. 艾肯声卡安装调试方法【必看】
  4. moectf chall 数独题 z3约束器求解
  5. PC微信多开,超简单
  6. mysql 一周签到表_最实用的考勤签到表(上下班签到表);
  7. shiro 调用 subject.login(token)方法后
  8. 2020-01-22
  9. “报告星”自动报告生成系统介绍
  10. win系统自带校验命令