Codeforces Round #443 (Div. 2), problem: (D) Teams Formation Solution In C/C++

#include <stdio.h>
struct node
{
__int64 ans,num;
}s[101000];
int main()
{
int n,k,m;
while(~scanf(“%d%d%d”,&n,&k,&m))
{
scanf(“%I64d”,&s[0].num);
s[0].ans=1;
int len=1;
for(int i=1;i<n;i++)
{
int x;
scanf(“%d”,&x);
if(x==s[len-1].num)
{
s[len-1].ans++;
if(s[len-1].ans==k)
{
s[len-1].ans=0;
len–;
}
}
else
{
s[len].num=x;
s[len].ans=1;
len++;
}
}
int l=0,r=len-1;
__int64 sum=0;
while(s[l].num==s[r].num&&r>l)
{
if((s[l].ans+s[r].ans)>=k)sum+=k;
s[l].ans=(s[l].ans+s[r].ans)%k;
s[r].ans=0;
if(s[l].ans!=0)
{
break;
}
l++,r–;
}
if(l==r)
{
if((s[l].ans*m)%k==0)sum=0;
else sum+=((s[l].ans*m)%k);
}
else
{
for(int i=l;i<=r;i++)
{
sum+=(s[i].ans*m);
}
}

printf(“%I64d\n”,sum);
}
return 0;
}

#include <stdio.h>
struct node
{
__int64 ans,num;
}s[101000];
int main()
{
int n,k,m;
while(~scanf(“%d%d%d”,&n,&k,&m))
{
scanf(“%I64d”,&s[0].num);
s[0].ans=1;
int len=1;
for(int i=1;i<n;i++)
{
int x;
scanf(“%d”,&x);
if(x==s[len-1].num)
{
s[len-1].ans++;
if(s[len-1].ans==k)
{
s[len-1].ans=0;
len–;
}
}
else
{
s[len].num=x;
s[len].ans=1;
len++;
}
}
int l=0,r=len-1;
__int64 sum=0;
while(s[l].num==s[r].num&&r>l)
{
if((s[l].ans+s[r].ans)>=k)sum+=k;
s[l].ans=(s[l].ans+s[r].ans)%k;
s[r].ans=0;
if(s[l].ans!=0)
{
break;
}
l++,r–;
}
if(l==r)
{
if((s[l].ans*m)%k==0)sum=0;
else sum+=((s[l].ans*m)%k);
}
else
{
for(int i=l;i<=r;i++)
{
sum+=(s[i].ans*m);
}
}

printf(“%I64d\n”,sum);
}
return 0;
}

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 Portable Blender Bottle Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Famed mountaineer Nirmal Purja, 43, among 10 climbers killed after avalanche in Pakistan

Tragedy in the Karakoram: Nirmal Purja Among 10 Lost in Avalanche In a devastating turn of events for the global mountaineering community, renowned high-altitude climber...

How Under Desk Storage Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

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!