368B - Sereja and Suffixes - CodeForces Solution


data structures dp *1100

Please click on ads to support us..

Python Code:

n, m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
s = set()
dp = [0]

for i, item in zip(range(len(a)), a[::-1]):
    if item in s:
        dp.append(dp[i])
    else:
        dp.append(dp[i] + 1)
    s.add(item)

for i in range(m):
    k = int(input())
    print(dp[n + 1 - k])

C++ Code:

#include <bits/stdc++.h>
#define ANIME ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double

using namespace std;

int main()
{
    ANIME
    int n,m;
    cin>>n>>m;
    int arr[n],suffix[n]={0};
    map<int,bool>mm;
    for(int i=0;i<n;i++) cin>>arr[i];
    suffix[n-1]=1;mm[arr[n-1]]=1;
    for(int i=n-2;i>=0;i--)
    {
        if(mm[arr[i]])
        {
            suffix[i]=suffix[i+1];
        }
        else
        {
            suffix[i]=suffix[i+1]+1;
            mm[arr[i]]=1;
        }
    }
    for(int i=0;i<m;i++)
    {
        int x;
        cin>>x;
        cout<<suffix[x-1]<<"\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem