304 North Cardinal St.
Dorchester Center, MA 02124

# Traffic jam CodeChef Solution

## Traffic jam CodeChef Solution in C++17

``````#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int M=1e5+5;
inline void rd(int &a){
a=0;char c;
while(c=getchar(),!isdigit(c));
do a=a*10+(c^48);
while(c=getchar(),isdigit(c));
}
struct node{
int pos,val;
inline bool operator<(const node &tmp)const{
return val<tmp.val||val==tmp.val&&pos<tmp.pos;
}
}th[M];
int dp[M],A[M],B[M];
#define bug(x) cout<<#x<<"="<<x<<" "
#define debug(x) cout<<#x<<"="<<x<<endl
inline void gao(){
int n;cin>>n;
for(int i=1;i<=n;++i)rd(A[i]),B[i-1]=A[i];
sort(B,B+n);
int s=unique(B,B+n)-B;
for(int i=1;i<=n;++i)
dp[i]=lower_bound(B,B+s,A[i])-B+1,th[i]=(node){i,dp[i]};
int allc=0;
sort(th+1,th+n+1);
th[0].val=-1;
memset(A,0,sizeof(A));
th[n+1]=(node){0,0};
for(int i=1,pos=0,l=1;i<=n+1;++i){
if(th[i].val!=th[i-1].val){
pos=th[i-1].pos;
for(int j=l;j<i;++j)
if(!A[th[j].pos])A[pos=th[j].pos]=++allc;
l=i;
}
if(th[i].pos>pos)A[th[i].pos]=++allc;
}
memset(dp,0,sizeof(dp));
int res=0,ans=0;
for(int i=1;i<=n;++i)
dp[A[i]]=1,res+=!dp[A[i]-1];
for(--res;res;res>>=1,++ans);
cout<<ans<<endl;
}
int main(){
int _;
for(cin>>_;_--;)gao();
return 0;
}``````

## Traffic jam CodeChef Solution in C++14

``````#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int M=1e5+5;
inline void rd(int &a){
a=0;char c;
while(c=getchar(),!isdigit(c));
do a=a*10+(c^48);
while(c=getchar(),isdigit(c));
}
struct node{
int pos,val;
inline bool operator<(const node &tmp)const{
return val<tmp.val||val==tmp.val&&pos<tmp.pos;
}
}th[M];
int dp[M],A[M],B[M];
#define bug(x) cout<<#x<<"="<<x<<" "
#define debug(x) cout<<#x<<"="<<x<<endl
inline void gao(){
int n;cin>>n;
for(int i=1;i<=n;++i)rd(A[i]),B[i-1]=A[i];
sort(B,B+n);
int s=unique(B,B+n)-B;
for(int i=1;i<=n;++i)
dp[i]=lower_bound(B,B+s,A[i])-B+1,th[i]=(node){i,dp[i]};
int allc=0;
sort(th+1,th+n+1);
th[0].val=-1;
memset(A,0,sizeof(A));
th[n+1]=(node){0,0};
for(int i=1,pos=0,l=1;i<=n+1;++i){
if(th[i].val!=th[i-1].val){
pos=th[i-1].pos;
for(int j=l;j<i;++j)
if(!A[th[j].pos])A[pos=th[j].pos]=++allc;
l=i;
}
if(th[i].pos>pos)A[th[i].pos]=++allc;
}
memset(dp,0,sizeof(dp));
int res=0,ans=0;
for(int i=1;i<=n;++i)
dp[A[i]]=1,res+=!dp[A[i]-1];
for(--res;res;res>>=1,++ans);
cout<<ans<<endl;
}
int main(){
int _;
for(cin>>_;_--;)gao();
return 0;
}``````

## Traffic jam CodeChef Solution in C

``````#include <stdio.h>
#include <stdlib.h>
#define siz 100000
#define S(a,b) if(a)return(b)
#define C(a) if(a)continue

typedef struct {
int x, n;
}st;

int o[siz];
st e[siz], *w[siz];

int A(const void *a, const void *b)
{
st *p=*(st**)a;
st *q=*(st**)b;
S(p->x<q->x,-1);
S(p->x>q->x,1);
S(p->n<q->n,-1);
S(p->n>q->n,1);
S(1,0);
}

main()
{
int fall, i, j, k, n, b, tmp;
for(scanf("%d",&fall); fall--; printf("%d\n",tmp))
{
for(i=!scanf("%d",&n); i<n; scanf("%d",&e[i].x),e[i].n=i,w[i]=e+i,o[i++]=0);
qsort(w,n,sizeof(st*),A);
for(i=!(b=1); i<n; i++)
{
C(o[i]);
for(j=i+1; j<n&&o[j]; j++);
C(j>=n);
if(w[i]->n>w[j]->n)
b++;
else
continue;
for(k=j+1; k<n&&w[k]->x==w[j]->x&&w[k]->n<w[i]->n; k++);
C(k>=n||w[k]->x!=w[j]->x);
for(;k<n&&w[k]->x==w[j]->x; o[k++]=1);
}
for(tmp=0; b>1; b=(b+1)/2,tmp++);
}
return 0;
}
``````
##### Traffic jam CodeChef Solution Review:

In our experience, we suggest you solve this Traffic jam CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Traffic jam CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Traffic jam CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!