D Beauty of Array dp, Zhejiang 2015, China

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.