Header Ad

HackerEarth Scoreboard queries problem solution

In this HackerEarth Scoreboard queries problem solution In a tournament, there are (N + 1) players, N players have already played and their scores are denoted by an array A. Here, A1 is the score of the first player, A2 of the second, ..., An of the Nth player.

Now, the organizers decide the ranks according to the following rules:

The player x scored more than player y, player x gets a better rank.

In the case of a tie, the player with lower indices gets a better rank.

Now, it is the turn of the (N + 1)th player to play. Before playing, he wants to know the number of ranks that he can secure so that he can decide his strategy.

Now, the jury has some scoreboard updates. Therefore, your task is to tell the jury after every update the number of distinct possible ranks that he can get.


HackerEarth Scoreboard queries problem solution


HackerEarth Scoreboard queries problem solutions.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "\n"
#define test ll txtc; cin>>txtc; while(txtc--)
typedef long long int ll;
typedef long double ld;
int main() {
FIO;
test
{
ll n,q,x,l,r; cin>>n>>q;
set<ll>s;
map<ll,ll>mp;
vector<ll>a(n);
for(int i=0;i<n;i++){
cin>>a[i];
s.insert(a[i]);
mp[a[i]]++;
}
while(q--){
cin>>l>>r;
l--;
mp[a[l]]--;
if(mp[a[l]]==0){
s.erase(s.find(a[l]));
}
mp[r]++;
s.insert(r);
a[l]=r;
cout<<(ll)s.size()+1ll<<endl;
}

}
return 0;
}

Second solution

t = int(input())
while t > 0:
t -= 1
n, q = map(int, input().split())
have = dict()
scores = list(map(int, input().split()))
for score in scores:
have[score] = have.get(score, 0) + 1
while q > 0:
q -= 1
i, nw = map(int, input().split())
i -= 1
have[scores[i]] -= 1
if have[scores[i]] == 0:
have.pop(scores[i])
scores[i] = nw
have[scores[i]] = have.get(scores[i], 0) + 1
print(len(have) + 1)


Post a Comment

0 Comments