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 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.
<h4< dd="">Output
For each case, print the answer in one line.
<h4< dd="">Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
<h4< dd="">Sample Output
105
21
38
题意:
给定一串数字,求所有连续的子序列的和,和为其中所有元素和(出现多次只算一次)。、
PS:
我们可以这样想:
因为要区别于不同的数
,可以看成序列里的数是一个一个加进去的,每次加入一个数,
统计前面序列里第一次出现新加入的这个数的位置;
这样每次加入的数的贡献度只会是这个数字之前出现的位置后面的数字!
//输入 1 2 3
//dp 1 5 14
//sum 1 6 20
//a[i] 1 2 3
如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;
我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;
,如果重复的话,假设1 2 3 4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(i,i-2),(i,i-3)。。。。(i,j+1)这些连续子序列的值可以加上a的值;
(i,j),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的i,j位置上的值,因为只需要算一次,所以不需要给这些以i
结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;
如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。
#include<cstdio>
#include<string.h>
using namespace std;
#define ll long long
const int maxn=;
ll dp[maxn],d[maxn],sum[maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n;scanf("%d",&n);
memset(d,,sizeof(d));
ll x;scanf("%lld",&x);
sum[]=dp[]=x;d[x]=;
for(ll i=;i<=n;i++)
{
scanf("%lld",&x);
if(d[x])
{
ll id=d[x];d[x]=i;
sum[i]=sum[i-]+(i-id)*x;
dp[i]=dp[i-]+sum[i];
}
else
{
d[x]=i;
sum[i]=sum[i-]+i*x;
dp[i]=dp[i-]+sum[i];
}
}
printf("%lld\n",dp[n]);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll pre[],a[];
int main()
{
ll t,i,j,n,ans,temp;
cin>>t;
while(t--) {
cin>>n;
ans=;
memset(pre,,sizeof(pre));
for(i=;i<=n;i++) {
cin>>a[i];
temp=(n+-i)*a[i];
ans+=temp;
ans+=(i-pre[a[i]]-)*temp;
pre[a[i]]=i;
}
cout<<ans<<endl;
}
return ;
}
#include <iostream>
using namespace std;
const int maxn=;
int n[maxn]; int main()
{
int t;
cin>>t;
while(t--)
{
fill(n,n+maxn,);
int m;
cin>>m;
long long ans=,dp=;
int t;
for(int i=;i<=m;i++)
{
cin>>t;
dp=(i-n[t])*t+dp;
ans+=dp;
n[t]=i;
}
cout<<ans<<endl;
}
retu
2015 浙江省赛 Beauty of Array (思维题)的更多相关文章
- Beauty of Array(思维)
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integ ...
- 浙江省第十二届省赛 Beauty of Array(思维题)
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
- 2019浙大校赛--G--Postman(简单思维题)
一个思维水题 题目大意为,一个邮递员要投递N封信,一次从邮局来回只能投递K封.求最短的投递总距离.需注意,最后一次投递后无需返回邮局. 本题思路要点: 1.最后一次投递无需返回邮局,故最后一次投递所行 ...
- 2015 浙江省赛 H - May Day Holiday
H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...
- 2015 浙江省赛B Team Formation (技巧,动归)
Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...
- CF949B A Leapfrog in the Array 思维题,推理
题意: Dima是一名初级程序员. 在他的工作中,他经常不断地重复以下操作:从数组中删除每个第二个元素. 有一天,他对这个问题的解决方案感到厌倦,他提出了以下华丽的算法. 假设有一长度为2n的数组,最 ...
- 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...
- ZOJ 3879 Capture the Flag 15年浙江省赛K题
每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)
Ace of Aces Time Limit: 2 Seconds Memory Limit: 65536 KB There is a mysterious organization c ...
随机推荐
- nodejs 中module.exports 和 exports 区别详细介绍
你肯定非常熟悉nodejs模块中的exports对象,你可以用它创建你的模块接下来介绍创建过程,感兴趣的朋友可以参考下 你肯定非常熟悉nodejs模块中的exports对象,你可以用它创建你的模块.例 ...
- hdoj1006--Tick and Tick
Problem Description The three hands of the clock are rotating every second and meeting each other ma ...
- Python快速学习-高级特性
1.切片 取一个list或tuple的部分元素是非常常见的操作 L = ['hello','the','world','and','my','love'] 取前三个元素 L[0:3],L[:3] 取倒 ...
- SQL索引工作原理
SQL 当一个新表被创建之时,系统将在磁盘中分配一段以8K为单位的连续空间,当字段的值从内存写入磁盘时,就在这一既定空间随机保存,当一个8K用完的时候, SQLS指针会自动分配一个8K的空间.这里,每 ...
- java大数字操作:BigInteger,BigDecimal(浮点型)
java大数字操作: BigInteger:大数字整型的 BigDecimal(浮点型):大数字小数的,也适用大的整数 BigInteger: String num1 = "10038182 ...
- 配置web项目出的各种error (安装sql2008错误,网站连接数据库error错误等等)
一个破error:40 错误搞出了很多莫名其妙的为问题,搞了5天,最后重装系统加上重新配置终于好了. 1. 关于SQL 2008 安装错误 安装之前必须安装VS2008 SP1 安装到最后提示 试 ...
- Mac的搜狗输入法和QQ输入法加入⌘⌥⌃⇧自定义短语
搜狗输入法(Mac):http://pinyin.sogou.com/mac/ 创建名为『搜狗输入法自定义短语.ini』的文本文件(建议用Sublime Text),内容如下,然后偏好设置的自定义短语 ...
- iOS 检查指定日期是否在当前日期之前
iOS检查指定日期是否在当前日期之前, 直接上代码: - (BOOL)checkProductDate: (NSString *)tempDate { NSDateFormatter *dateFor ...
- mysql分组取topn
本文来自 http://www.jb51.net/article/31590.htm 有些语句sql top n 是sqlserver语法 --按某一字段分组取最大(小)值所在行的数据 代码如下: ...
- LINUX下SYN FLOOD攻击及LINUX下SYN攻防简述
LINUX下SYN攻防战如下 (一)SYN攻击原理 SYN攻击属于DOS攻击的一种,它利用TCP协议缺陷,通过发送大量的半连接请求,耗费服务器CPU和内存资源.SYN攻击聊了能影响主机外,还可以危害路 ...