Codeforces Round #813 (Div. 2) 1712C - Sort Zero Solution
Problem Link: https://codeforces.com/contest/1712/problem/C
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,t,i;
cin>>t;
while(t--)
{
cin>>n;
ll a[n];
for(i=0; i<n; i++)cin>>a[i];
if(is_sorted(a,a+n))
{
cout<<"0"<<endl;
continue;
}
ll ind;
for(i=n-1; i>0; i--)
{
if(a[i]<a[i-1])
{
ind=i;
break;
}
}
map<ll,ll>mp;
for(i=0; i<n; i++)
{
if(mp.find(a[i])==mp.end())
{
mp[a[i]]=i;
}
}
ll ans;
ans=mp.size();
set<ll>sv;
for(i=n-1; i>0; i--)
{
if(ind>mp[a[i]])
{
break;
}
sv.insert(a[i]);
}
ans-=sv.size();
cout<<ans<<endl;
}
}
No comments