zoukankan      html  css  js  c++  java
  • 2019牛客暑期多校训练营(第八场) Beauty Values

    时间限制:C/C++ 1秒,其他语言2秒

    空间限制:C/C++ 524288K,其他语言1048576K
    64bit IO Format: %lld

    题目描述

    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!

    输入描述:

    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

    输出描述:

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

    输入

    4
    1 2 1 3

    输出

    18

    题意:所有子区间上不同元素的个数之和。

    题解:

     

    得出 dp[i]=dp[i-1]+(w[i]唯一时对答案的贡献)-(w[i]不唯一时影响区间的个数), 则dp[i]=dp[i-1]+i-flag[w[i]]。flag[w[i]]为w[i]最后一次出现的位置,位置和影响区间的个数是等价的。最后答案为

    代码:

    #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
    ",ans);
      system("pause");
      return 0;
    }
    本博客仅为本人学习,总结,归纳,交流所用,若文章中存在错误或有不当之处,十分抱歉,劳烦指出,不胜感激!!!
  • 相关阅读:
    Nand flash uboot 命令详解【转】
    uboot命令分析+实现【转】
    UBoot常用命令手册
    第一个Linux驱动-流水灯【转】
    lcd ram/半反穿技术解析【转】
    使用 .gitignore来忽略某些文件【转】
    git常见操作--忽略文件以及常用命令【转】
    Git忽略文件方法【转】
    Git查看、删除、重命名远程分支和tag【转】
    SPA与DPA 攻击【转】
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/11334440.html
Copyright © 2011-2022 走看看