D Beauty of Array dp, Zhejiang 2015, China
# 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 # define INF 0x7fffffffusing namespace std; 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 records the position of the previous 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] for the front I beautiful array and the front I and including the Front I-1 and dp [I-1] and also composed of the number of I and dp [-dp [I-1] + a [I] * (I + 1-num [a [I]); where I + 1-num [a [I] is used to remove duplicate a [I], duplicate calculation only once */dp [I + 1] = dp [I] + dp [I]-dp [I-1] + a [I] * (I + 1-num [a [I]); num [a [I] = I + 1; // record the new location} cout <dp [n] <endl;} return 0 ;}