Beauty of Array————思维(或者DP)

Beauty of Array(贡献度思想) 传送门 题意:给你n个数,求所有连续子序列的和,并且每个连续子序列中相同的数只保留一个。 思路:这是贡献度的经典简单题。 贡献度思想是什么,贡献度思想就是把对答案有贡献的区间都列出来,把对答案没贡献的区间都舍去。为什么会有对答案没贡献的区间呢,因为在这个区间里,这个数字前面有和它相同的数字,我们只需要保留前面那个区间就行。 对没有重复数字的样例1来说 序号 1 2 3 4 5 ai 1 2 3 4 5 ①a1=1对答案的贡献度.. 阅读详情

Beauty of Array

Time Limit: 2 Seconds Memory Limit: 65536 KB
Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.

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 <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output
For each case, print the answer in one line.

Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
Sample Output
105
21
38
Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest


题目大意:自己翻译

这道题看一下数据范围就知道不能用O(n2)O(n^2)O(n2)的写了
于是想到找每一个数出现的次数,然后让这个数乘以它出现的次数,然后求和就OK

我们先来观察:
对于一个没有重复数字数组,比如说

a[] = {1,2,3,4,5}
含1的有:1,12,123,1234,12345共5次
含2的有:12,123,1234,12345,2,23,234,2345共8次
含3的有:123,1234,12345,23,234,2345,3,34,345共9次
含4的有:1234,12345,234,2345,34,345,4,45共8次
含5的有:12345,2345,345,45,5共5次

设i从0开始
我们可以发现,对于任意一个数字,它的出现次数等于&ThickSpace;i∗(n−i+1)&ThickSpace;\; i*(n-i+1) \;i∗(n−i+1)比如3,它出现的次数是它的位置3*(5-3+1)=9次,其它的数字也类似
答案就是 1∗5+2∗8+3∗9+4∗8+5∗5=1051*5+2*8+3*9+4*8+5*5=1051∗5+2∗8+3∗9+4∗8+5∗5=105
对于没有出现重复数字的数组来说,我们这样的做法已经可以解决问题了,不过对于有重复数字的数组怎么办呢?

我们来继续观察

a[] = {2,3,3,2}
含第一个2的有: 2,23,233,2332共4次
含第一个3的有: 23,233,2332,3,33,332共6次
含第二个3的有:233,2332,33,332,3,32共6次
含第二个2的有: 2332,332,32,2共4次

可是我们对于重复出现的数字只统计一次就好了,请注意我加粗的部分,它们在之前已经出现过,当统计到第二个2或者3的时候,我们可以认为它们不需要被统计,因为当2或者3重复出现出现的时候,我们认为它已经被统计过了
记pos[x]为x上一次出现的位置,pos[x]默认为0
观察可以知道对于任意一个数字x,它的出现次数等于&ThickSpace;(i−pos[x])∗(n−i+1)&ThickSpace;\; (i-pos[x])*(n-i+1) \;(i−pos[x])∗(n−i+1)比如第二个3,它出现的次数是它的位置(3-2)*(4-3+1)=2次,其它的数字也类似
答案就是 2∗4+3∗6+3∗2+2∗3=382*4+3*6+3*2+2*3=382∗4+3∗6+3∗2+2∗3=38

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+7;
int pos[MAXN];
int main()
{
        int t,n,x;
        cin>>t;
        while(t--)
        {
                cin>>n;
                memset(pos,0,sizeof(pos));
                ll ans = 0;
                for(int i=1;i<=n;++i)
                {
                        cin>>x;
                        ans += 1ll*(i-pos[x])*(n-i+1)*x;
                        pos[x] = i;
                }
                cout<<ans<<endl;
        }
        return 0;
}


我菜的一批

pdb文件删除杂原子 HETATM;保留或去除水分子 这样操作水分子也会跟着删除,如果需要保留水分子。 阅读详情

相关推荐

从零开始:手把手教你用Verilog搭建一个可配置的Cache模块(以Vortex GPGPU为例)

本文详细介绍了如何使用Verilog从零开始搭建一个可配置的Cache模块,以Vortex GPGPU为例。内容涵盖Cache设计基础、模块接口与参数化设计、核心子模块实现、仲裁与一致性机制、功能验证与性能调优等关键环节,适合RTL设计初学者和希望深入理解Cache工作原理的工程师。通过实战指南,读者可以掌握硬件代码分析技巧,提升Cache模块的设计能力。

weixin_42552315的博客 441

D - Beauty of Array (好题)

Edward has an arrayAwithNintegers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous ...

cherish_lin的博客 373

SVPWM原理与MCU/FPGA实现:从数学推导到工程实战

空间矢量调制(SVPWM)是实现高性能电机驱动控制的核心算法之一,其通过六个基本电压矢量与零矢量的伏秒平衡,在一个PWM周期内合成任意方向的旋转电压圆,相比传统SPWM可提升约15%的直流电压利用率。该技术不仅涉及扇区判断、作用时间计算、过调制限幅等数学原理,还在MCU与FPGA两种硬件平台上有不同的工程实现路径,包括中心对齐PWM配置、死区插入、定点数流水线设计等。在电动工具、无人机、机器人及车载执行器等电池供电场景中,SVPWM直接决定续航与扭矩表现。本文从数学原理出发,详细拆解MCU和FPGA实现细节

weixin_34265814的博客 301

Beauty of Array(思维)

Beauty of Array ZOJ - 3872 Edward has an arrayAwithNintegers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation o...

weixin_30814329的博客 196

Prompt Learning 简介

• Prompt Learning 可以将所有的任务归一化预训练语言模型的任务• 避免了预训练和fine-tuning 之间的gap,几乎所有 NLP 任务都可以直接使用,不需要训练数据。• 在少样本的数据集上,能取得超过fine-tuning的效果。• 使得所有的任务在方法上变得一致。

jxsdq的博客 1万+

【AUTOSAR-COM】Transmission Mode 和 Transfer Property

1.COM模块的基本概念 首先在描述Transmission Mode 和 Transfer Property之前,有必要对COM模块的基本处理有一个直观的理解,我所描述的和对概念理解的也不一定完全正确。 Signal:指的是通信协议中的,一条报文中所保存的若干信号。 I-PDU:至少在COM模块中,可以理解为一条报文。 上图是在CANdb软件中编辑一个报文的Layout的界面,可以很形象的将I-PDU对等为这里的一条Message,而Signal就是需要在Message中需要摆放的Signal

tim_hoven的博客 4635

Beauty of Array

Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of...

永夜莫明的博客 447

华为非AI方向0603笔试真题-爆破小游戏(详细思路+多语言题解)

华为非AI方向0603笔试第二题【爆破小游戏】,提供详细思路讲解以及代码实现。

qq_45776114的博客 358

Beauty of Array ZOJ - 3872

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous ...

小帅哥快来看啊的博客 242

安装anaconda3时出现错误:Failed to link extracted packages to D:\anaconda!...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1482

Uniapp返回上一页并刷新:亲测有效的 goBack 实现技巧!

这个技巧在实际开发中非常常用,特别适合表单提交后返回列表刷新这类场景。比起依赖onShow或复杂的vuex状态同步,这种方式更直接、好维护,也不容易出 bug。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1843

ZOJ 3872 Beauty of Array (思维 dp思想)

D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3872 Description Edward has an array A with N integers. He defin

K键盘里的青春K 525

【HomeAssistant接入的设备实现天猫精灵】

Home Assistant的HassLife组件: 此Home Assistant的组件可以让天猫精灵、小爱同学对接HomeAssistant平台上的设备,控制设备、查询上报设备状态。 注意:本插件只负责上报HomeAssistant的设备状态信息到服务器,控制功能需要在HassLife 平台上配置。.........

vor234的博客 1万+

浙江省第十二届省赛 Beauty of Array(思维题)

Description Edward has an arrayAwithNintegers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the bea...

125

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

ZOJ-3872-Beauty of Array-思维

ZOJ-3872-Beauty of Array 传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 参考:https://blog.youkuaiyun.com/u013050857/article/details/45285515 题意: 定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有子...

weixin_30764137的博客 139

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

2015 浙江省赛 Beauty of Array (思维题)

Beauty of Array   Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the...

蔡军帅 117

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

js获取服务器响应头信息,js获取HTTP的请求头信息

&lbrack;IIS&rsqb;IIS扫盲&lpar;二&rpar;iis-IIS之Web服务器建立 第一篇IIS之Web服务器 一.建立第一个Web站点 比如本机的IP地址为192.168.0.1,自己的网页放在D:\Wy目录下,网页的首页文件名为Inde ...history and its relevant variables in Linux&a...

weixin_39999730的博客 894

zoj-3872——Beauty of Array

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous ...

watestill的博客 347

Beauty of Array(找规律)

Edward has an arrayAwithNintegers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous ...

WhileTrue 363

ZOJ3872:Beauty of Array(浙江省赛2015)

The 12th Zhejiang Provincial Collegiate Programming Contest  Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now

ACM!荣耀之路! 3647

F - Beauty of Array

F - Beauty of Array https://vjudge.net/contest/225989#problem/FEdward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in ...

liu344439833的博客 315

ZOJ-3872-Beauty of Array【数学】【12th浙江省赛】

ZOJ-3872-Beauty of Array

Linoy 1120

ZOJ 3872 Beauty of Array(DP)

ZOJ 3872 Beauty of Array( DP思想 )

1033
上一篇: 两个矩阵面积并——————简单几何
下一篇: ZOJ3870 Team Formation————位运算&&思维
陶鸿杰
博客等级 码龄9年 93粉丝 298原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值