• codeforces ~ 1004 C Sonya and Robots (dp)


    C. Sonya and Robots
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.

    Sonya has drawn nn numbers in a row, aiai is located in the ii-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.

    Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.

    For example, if the numbers [1,5,4,1,3][1,5,4,1,3] are written, and Sonya gives the number 11 to the first robot and the number 44 to the second one, the first robot will stop in the 11-st position while the second one in the 33-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number 44 to the first robot and the number 55 to the second one, they will meet since the first robot will stop in the 33-rd position while the second one is in the 22-nd position.

    Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.

    Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs (pp, qq), where she will give pp to the first robot and qq to the second one. Pairs (pipi, qiqi) and (pjpj, qjqj) are different if pipjpi≠pj or qiqjqi≠qj.

    Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.

    Input

    The first line contains a single integer nn (1n1051≤n≤105) — the number of numbers in a row.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1051≤ai≤105) — the numbers in a row.

    Output

    Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.

    Examples
    input
    Copy
    5
    1 5 4 1 3
    output
    Copy
    9
    input
    Copy
    7
    1 2 1 1 1 3 2
    output
    Copy
    7
    Note

    In the first example, Sonya can give pairs (11, 11), (11, 33), (11, 44), (11, 55), (44, 11), (44, 33), (55, 11), (55, 33), and (55, 44).

    In the second example, Sonya can give pairs (11, 11), (11, 22), (11, 33), (22, 11), (22, 22), (22, 33), and (33, 22).

    题意:

    给你n个数字,有两个不同的机器人,一个在数列的最左端,另一个在数列的最右端,

    如果机器人1标号为x,那么他从左往右会在第一个x这里停下来

    第二个机器人标号为y,他会从右往左在第一个y这里停下来

    求(x,y)这样的一共有多少对,可以使得机器人不相撞

    题解:

    左边的机器人遇见重复的就不能往后走了

    右边的机器人遇见重复的就不能往前走了

    那么我们先将前n个数中不重复的表给打出来

    那么从右往左走的时候,不同的个数就是我们要求的对数

    注意开longlong

    代码如下

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+5;
    int a[maxn];
    int vis[maxn];
    int dp[maxn];
    int main(){
      int n;
      while(cin>>n){
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        dp[0]=0;
        for(int i=1;i<=n;i++){
          cin>>a[i];
          if(!vis[a[i]]){
            vis[a[i]]=1;
            dp[i]=dp[i-1]+1;
          }else{
            dp[i]=dp[i-1];
          }
        }
        long long ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=n;i>=1;i--){
          if(!vis[a[i]]){
            vis[a[i]]=1;
            ans+=dp[i-1];
          }
        }
        printf("%I64d
    ",ans);
    
      }
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    解决“google快照无法打开”的简单而有效的方法~
    在Struts2里面嵌入Spring
    HDU
    设计模式大总结(二)
    Node.js入门笔记
    草图检索和识别[开源]
    2019-10-31-VisualStudio-断点调试详解
    2019-10-31-VisualStudio-断点调试详解
    2019-9-2-C#-设计模式-责任链
    2019-9-2-C#-设计模式-责任链
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9285940.html
Copyright © 2020-2023  润新知