浙江省 2015 省赛 D Beauty of Array dp

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;
long long int dp[100005];
int a[100005];
int main() {
	int t;
	scanf("%d",&t);
	while(t--){
		int n;

		scanf("%d",&n);
		map<int,int> num; //num记录前一个a[i]的位置 

		for(int i = 0;i < n;i++){
			scanf("%d",&a[i]);
			num[a[i]] = 0;
		}
		dp[0] = 0;
		dp[1] = a[0];
		num[a[0]] = 1;

		for(int i = 1;i < n;i++){
			/*
				dp[i] 为前i个美丽的数组的和
				前i个和包括前i-1个的和 dp[i-1] 还有由第i个数组成的和 dp[i] - dp[i-1] + a[i] * (i+1 - num[a[i]]);
				其中的i+1 - num[a[i]] 是用于去除重复的a[i],重复的只计算一次
			*/
			dp[i+1] = dp[i] + dp[i] - dp[i-1] + a[i] * (i+1 - num[a[i]]);
			num[a[i]] = i+1; //将新的位置记录下来
		}
		cout << dp[n] << endl;
	}
	return 0;
}

时间: 2024-07-28 15:30:44

浙江省 2015 省赛 D Beauty of Array dp的相关文章

浙江省 2015 省赛 B Team Formation

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque

zoj3872 Beauty of Array (dp)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 题意: 给你n个数,求这n个数的连续子序列中不算重复的数的和,比如第二个样例他的子序列就是{2},{2,3},{2,3,3},{3},{3,3},{3}:但每个子序列中重复的元素不被算入,所以他们的总和就是2+5+5+3+3+3=21: 思路: 考虑到当前第i个数,对答案的贡献是多少,dp[i]表示第i个数所做的贡献 比如 1, 2, 3 -------

12th浙江省省赛 B题 Beauty of Array

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

2015省赛总结

2015省赛总结 AC-team  周凯——————2015年5月15日----23:35 2015年的省赛已经过去一个星期,今天我就做一下总结. 上周五,我们接到消息,可以作为旅游对去参加今年的省赛,其实听到这个消息我很高兴,因为终于可以出去见识一下什么是真正的ACM竞赛. 就在周四,舍友突发阑尾炎,我和另一基友几乎一晚没睡,舍友更不用说,整整疼了一天一夜.第二天(星期五)中午打的回来时,居然在出租车上睡着了.回来之后,下午接到消息,我们队可以以旅游队的名义去参加省赛,队友得知消息后,都很高兴

ZOJ 3872 Beauty of Array(数学)

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

DP ZOJ 3872 Beauty of Array

题目传送门 1 /* 2 DP:dp 表示当前输入的x前的包含x的子序列的和, 3 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: 4 sum 表示当前输入x前的所有和: 5 a[x] 表示id: 6 详细解释:http://blog.csdn.net/u013050857/article/details/45285515 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cmath>

zoj3827--Beauty of Array (dp)

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

[hdu5136]Yue Fei&#39;s Battle 2014 亚洲区域赛广州赛区J题(dp)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下午有点事情,无法打重现,所以下午只是花了十分钟做了一道J题,抢了个FB,2333333333 Yue Fei's Battle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)T

CUGBACM_Summer_Tranning3 2013长沙现场赛(二分+bfs模拟+DP+几何)

A题:二分 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 用lower_bound可以轻松解决,不过比赛的时候逗逼了. 刚开始没有预处理,所以队友给出一组数据的时候没通过,然后一时紧张又想不出什么好的解决办法,所以就没再继续敲代码.实在有点可惜了. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #includ