ZOJ 3872 - Team Formation(DP)

来源:互联网 发布:linux编辑hosts 编辑:程序博客网 时间:2024/06/06 03:36

题目:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872

题意:

求出序列中元素不相同的子序列的总和。

思路:

记录上一个出现x的位置 b[a].

res += (i-b[a])*a;

AC.

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;int b[1000005];int main(){    //freopen("in", "r", stdin);    int T;    scanf("%d", &T);    while(T--) {        int n, a;        ll ans = 0, res = 0;        scanf("%d", &n);        memset(b, 0, sizeof(b));        for(int i = 1; i <= n; ++i) {            scanf("%d", &a);            res = res + (i-b[a])*a;            b[a]= i;            ans += res;        }        printf("%lld\n", ans);    }    return 0;}


0 0
原创粉丝点击