时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

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 nn_{}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.

输入

4
1 2 1 3

输出

18

题意:所有子区间上不同元素的个数之和。

题解:

得出 dp[i]=dp[i-1]+(w[i]唯一时对答案的贡献)-(w[i]不唯一时影响区间的个数), 则dp[i]=dp[i-1]+i-flag[w[i]]。flag[w[i]]为w[i]最后一次出现的位置,位置和影响区间的个数是等价的。最后答案为

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
long long dp[maxn];
int w[maxn],flag[maxn];
int main()
{int i,n;long long ans=0;scanf("%d",&n);for(i=1;i<=n;i++) scanf("%d",&w[i]);dp[1]=1;flag[w[1]]=1;for(i=2;i<=n;i++){dp[i]=dp[i-1]+i-flag[w[i]];flag[w[i]]=i;}for(i=1;i<=n;i++) ans+=dp[i];printf("%lld\n",ans);system("pause");return 0;
}

转载于:https://www.cnblogs.com/VividBinGo/p/11334440.html

2019牛客暑期多校训练营(第八场) Beauty Values相关推荐

  1. 2019牛客暑期多校训练营(第八场) CDMA

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言1048576K Special Judge, 64bit IO Format: %lld 题目描述   Gro ...

  2. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

  3. 2019牛客暑期多校训练营(第一场)E-ABBA(dp)

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  4. 2019牛客暑期多校训练营(第一场)

    传送门 参考资料: [1]:官方题解(提取码:t050 ) [2]:标程(提取码:rvxr ) [3]:牛客题解汇总 A.Equivalent Prefixes(单调栈) •题意 定义两个数组 u,v ...

  5. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  6. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  7. 【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of ...

  8. 【2019牛客暑期多校训练营(第二场) - D】Kth Minimum Clique(bfs,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/D 来源:牛客网 Given a vertex-weighted graph with N vertice ...

  9. 【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Two arrays u and v each with m distinct elem ...

  10. 【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/H 来源:牛客网 Bobo has a set A of n integers a1,a2,-,ana1, ...

最新文章

  1. java读取doc文档
  2. 37.操作系统的信号量
  3. PyCharm安装和配置教程
  4. 艾伟:基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写
  5. 【概念集锦】之 shim和polyfill
  6. 2017年度中国专利代理十强
  7. 【转】sql server 订阅发布、快照发布(一)
  8. [转]如何正确查看Linux机器内存使用情况
  9. 试题8 算法训练 P0103(从键盘输入一个大写字母,要求改用小写字母输出。)
  10. php 连接芒果数据库,芒果数据库mongoDB
  11. 一文解决十大排序算法(动画图解)
  12. 多多计算机分屏版本怎么使用,电脑双屏幕怎么设置_电脑分屏怎么设置方法
  13. win10禁用全角_Win10微软拼音无法切换输入法全角和半角怎么办?
  14. 世界坐标系、相机坐标系、图像平面坐标系
  15. 双色球历史数据下载最新2003年2021年
  16. 今天是星期一,两天后是星期三,5天后是星期六;今天是星期六,3天后是星期二。
  17. 2019年MongoDB中文社区 长沙大会
  18. windows 下查看防火墙状态命令
  19. 【DCT】基于simulink的dual clutch Transmission双离合器变速器系统仿真系统详细解析
  20. 计算机启动后桌面图标都没有了,我的电脑开机后桌面上的图标都没了怎么办?...

热门文章

  1. 安卓手机安装magisk
  2. Spring Boot Actuator自定义健康检查
  3. PPT如何导出高质量图片
  4. Linux下文件压缩、打包,看这一篇就够了
  5. linux怎样安装xz工具,linux xz解压工具
  6. PC微信多开,超简单
  7. python论坛签到_python简单实现网站打卡签到
  8. mac QQ 语音或视频时其他声音变小的解决办法
  9. Java游戏编程不完全详解-2
  10. 纯互联网项目“失宠”乐博资本杨宁称今后只投O2O