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 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

题意:定义一个序列的美丽值为序列中不同数的和,求一个序列的所有子序列的美丽值的和

思路:在代码中,关键是理解我C(x,2)代表取得区间是左开右闭的

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

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef long long ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 1000005

int ha[N],hato[N];
int a[N],b[N];
vector<int>g[N];
int n;
int k;

void solve()
{
	int i,j;
	ll ans=0;
	ll temp;
	fre(i,0,k)
	{

		ans+=(long long)(n)*(n+1)/2*hato[i]; //根据德保大神点拨,
		temp=0;                           //例如一个区间有3的位置为(这里用真实位置,不是从0开始)
										  //    3 ..  5... 8    假设在0位置有一个3
										  //那么有3的位置  0    3    5     8
										  //因为加了0,所以序列一共n+1个数,假设n+1个数都是3
										  //,现在任取两个数s,e作为
										  //有效区间为[s+1,e]时,那么就会加一个3,此时是上面那一步
										  //现在因为两个3之间并不都是3
										  //所以要去重,假设两个3之间的数的数目,设为x,那么应该x++,
										  //因为下面的C(x,2)代表取得区间的有效值是做开右闭的
										  //去掉的个数应该是C(x,2),当然减去的东西应该 再  * 3(这里的3代表原序列相同的数)
		fre(j,0,g[i].size())
		{
			ll tt=g[i][j]+1-temp;
			ans-=(tt)*(tt-1)/2*hato[i];
			temp=g[i][j]+1;
		}
		ll tt=n+1-temp;
		ans-=(tt)*(tt-1)/2*hato[i];
	}

  pf("%lld\n",ans);
}

int main()
{
   int i,j,t;
   sf(t);
   while(t--)
   {
   	  sf(n);
   	  fre(i,0,n)
   	  {
   	  	sf(a[i]);
   	  	b[i]=a[i];
   	  }

      sort(b,b+n);
      k=unique(b,b+n)-b; //担心数据太大,哈希预处理

      for(i=0;i<k;i++)
		{
			ha[b[i]]=i;   //哈希
		    hato[i]=b[i];
		}

	  fre(i,0,k+1)
	    g[i].clear();

	  fre(i,0,n)
	    g[ha[a[i]]].push_back(i); //保存相同数的下标,注意这里比实际的小1
	  solve();
   }
   	  return 0;
}

/*

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

*/
时间: 2024-10-13 12:43:45

ZOJ 3872 Beauty of Array(数学)的相关文章

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>

ZOJ 3872 Beauty of Array(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 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

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 subarray of the array A. Input There are multiple

ZOJ 3872 Beauty of Array&amp;&amp;ZOJ 3870 Team Formation

3872链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 题目大意:给你n个数,问所有的连续的子序列中的所有元素的和(子序列中有相同元素只计算一次)(n<100000). 即若序列为1  2  3,则组成1,2,3,1 2,2 3,1 2 3,和为20: 若序列为1 2 2,则组成1,2,2,1 2,2 2,1 2 2,和为13: 解题思路:求不重复的序列和很简单,关键是去重. 现在看一个序列: 3  4 

ZOJ - 3872 Beauty of Array

题意:给定一个含有N个数的序列S,定义序列的魅力值为序列中不同数字之和,求出该序列所有子序列的魅力值之和. 分析:每个数乘以它出现的次数,求和即可. 如何求每个数出现的次数? 1.对于一个各数字完全不同的序列, eg:3 5 2 6 8 对于5来说,确定其存在于的子序列 (1)其右面,可选0个数字---5  可选1个数字---3 5 (2)其右面,可选0个数字---5  可选1个数字---5 2  可选2个数字---5 2 6        可选3个数字---5 2 6 8 因此,2 * 4 =

Zoj 3842 Beauty of Array

Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the beauty的总和. 那么要求这个这个总和,刚开始最容易想到的就是这样: for( int i=1; i<=N; i++ ) { for( int j = 1; j<=i; j++ ) { ... //排除重复的数计算总和 } } 这样子的结果实际上是 Time Limit Exceeded 因此采取

zoj 3872

D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3872 Appoint description:  System Crawler  (2015-04-30) Description Edward has an array A with N integers. He defines the beauty

ZOJ 3872 计算对答案的贡献

                                               D - Beauty of Array Description 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

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