More than 2019 cattle off summer school camp (Session 8) Beauty Values

Time limit: C / C ++ 1 second, 2 seconds other languages

Space limitations: C / C ++ 524288K, other languages 1048576K
64bit IO the Format:% LLD

Title Description

Gromah and LZR have entered the second level. There is a sequence a1,a2,⋯ ,an​ on the wall.
There is also a note board saying "the beauty value of a sequence is the number of different elements in the sequence".
LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.
Please help them determine the password!

Enter a description:

The first line contains one positive integer nn_{}n​, denoting the length of the sequence.
The second line contains n​ positive integers a1,a2,⋯ ,an​, denoting the sequence.

1≤ai≤n≤10^5

Output Description:

Print a non-negative integer in a single line, denoting the answer.

Entry

4
1 2 1 3

Export

18

Meaning of the questions: all sub-sections on the number of different elements and.

 

answer:

 

 

 

 

 

Stars dp [i] = dp [i-1] + (w [i] when the only contribution to the answer) - (w [i] does not affect the number of unique time interval), the dp [i] = dp [ i-1] + i-flag [w [i]]. The number of position w [i] of the last occurrence, the position and influence of the interval flag [w [i]] is equivalent. Finally, the answer is

 

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
long long dp[maxn];
int w[maxn],flag[maxn];
int main()
{
  int i,n;
  long long ans=0;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&w[i]);
  dp[1]=1;flag[w[1]]=1;
  for(i=2;i<=n;i++)
  {
    dp[i]=dp[i-1]+i-flag[w[i]];
    flag[w[i]]=i;
  }
  for(i=1;i<=n;i++) ans+=dp[i];
  printf("%lld\n",ans);
  system("pause");
  return 0;
}

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11334440.html