Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals), problem: (E) Cards Sorting Solution in C/C++

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#include <bits/stdc++.h>
using namespace std;
set<int> s[100005];
int main()
{
int n,i,x;
cin>>n;
int m=0;
for(i=0;i<n;i++)
{
cin>>x;
s[x].insert(i);
m=max(m,x);
}
i=0;
long long int c=0,val=1,ans=0;
while(i<=m)
{
if(s[i].size()==0)
i++;
else
{
set<int>::iterator itr=s[i].lower_bound(c);
if(itr!=s[i].end())
{
c=*itr;
ans+=val;
s[i].erase(itr);
}
else
{
c=0;
val++;
}
}
}
cout<<ans<<endl;
return 0;
}

📦 Looking for Codeforces Round 424 Div Based Vk? Check the best deals on Amazon.

🛒 Shop Codeforces Round 424 Div Based Vk on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#include <bits/stdc++.h>
using namespace std;
set<int> s[100005];
int main()
{
int n,i,x;
cin>>n;
int m=0;
for(i=0;i<n;i++)
{
cin>>x;
s[x].insert(i);
m=max(m,x);
}
i=0;
long long int c=0,val=1,ans=0;
while(i<=m)
{
if(s[i].size()==0)
i++;
else
{
set<int>::iterator itr=s[i].lower_bound(c);
if(itr!=s[i].end())
{
c=*itr;
ans+=val;
s[i].erase(itr);
}
else
{
c=0;
val++;
}
}
}
cout<<ans<<endl;
return 0;
}

📦 Looking for Codeforces Round 424 Div Based Vk? Check the best deals on Amazon.

🛒 Shop Codeforces Round 424 Div Based Vk on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Paul Walker’s brother dishes on late actor’s future involvement in ‘Fast & Furious’ franchise — potentially using AI

The enduring legacy of Paul Walker continues to resonate deeply with fans across the globe, especially as the high-octane Fast & Furious saga speeds...

The Real Truth About Viral Kitchen Gadgets Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!