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

----------------------------------

1   dp[1] = 1;

----------------------------------

2

1,2   dp[i] = dp[i-1]+a[i]*i = 1 + 2*2

----------------------------------

3

2,3

1,2,3  dp[i] = dp[i-1]+a[i]*i = 5 + 3*3   红色部分是dp[i-1],  对于第i个数的贡献,只是把第i-1个数的贡献,加上当前第i个数出现了几次(i)

-----------------------------------

例如有序列1,2,3,4,5

若在末尾加入一个序列中没有的数N,则新产生的子序列为:

N;

5,N;

4,5,N;

3,4,5,N;

2,3,4,5,N;

1,2,3,4,5,N;

则增加的输出值为:

5;

4,5;

3,4,5;

2,3,4,5;

1,2,3,4,5;

的输出值(dp[5])+6*N;

若在末尾加入一个序列中出现过的数字3,则新产生的子序列为:

3;

5,3;

4,5,3;

3,4,5,3;

2,3,4,5,3;

1,2,3,4,5,3;

0,1,2,3,4,5,3;

其中,最后四个子序列,因为末尾的3是第二次出现,故输出值里不将其计入,因此有效的子序列只有前面3个; dp[7] = dp[6]+7*a[7]-vis[a[7]]*a[7];  vis[a[i]]表示a[i]这个数最后出现的位置。

最终答案就是所有dp值加起来

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;

int T;
int vis[maxn*10];
ll dp[maxn],a[maxn];

int main(){
    cin >> T;
    while(T--){
        MS(vis); MS(dp);
        int n = read();
        for(int i=1; i<=n; i++)
            a[i] = read();
        ll ans = 0;
        for(int i=1; i<=n; i++){
            dp[i] = dp[i-1] + (i-vis[a[i]])*a[i];
            vis[a[i]] = i;
            ans += dp[i];
        }
        cout << ans << endl;
    }

    return 0;
}
时间: 2024-11-08 04:25:53

zoj3872 Beauty of Array (dp)的相关文章

AndyQsmart ACM学习历程——ZOJ3872 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 the beauty of all contiguous subarray of the array A. Input There a

浙江省 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

ZOJ3872 Beauty of Array---规律 | DP| 数学能力

传送门ZOJ 3872 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 t

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

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

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

Codeforces 57C Array dp暴力找规律

题目链接:点击打开链接 先是计算非递增的方案, 若非递增的方案数为x, 则非递减的方案数也是x 答案就是 2*x - n 只需求得x即可. 可以先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元即可. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #def

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