TOWIN - Editorial

1
11
2 1 1 5 1 5 1 5 1 5 1 
2 Likes

see i got one mistake that i was doing cnt1++ instead of cnt1=cnt1+v1[i] now i changed it but then also wrong

#include<bits/stdc++.h>
#define ll long long
#define PI 3.14
#define pb push_back
#define pop pop_back
#define MOD 1000000007
#define fi first
#define se second
#define mp make_pair
using namespace std;

int main() 
{ 
    ll t,n,v;
    cin>>t;
     
      
   
    while(t--)
    {
        ll  cnt1=0,cnt2=0;
        cin>>n;
       vector<ll>v1;
       
     for(int  i=0;i<n;i++)
       {
           cin>>v;
           v1.pb(v);
       }
       if(n==1)
       {
           cout<<"first"<<endl;
       }
       else  if(n==2)
       {
           if(v1[0]>v1[1])
           {
               cout<<"first"<<endl;
           }
           else if(v1[1]>v1[0])
           cout<<"second"<<endl;
           else
          {
              cout<<"draw"<<endl;
          }
       }
       else
       {
           cnt1=v1[0];
           cnt2=v1[1]+v1[2];
       for(int  i=3;i<v1.size();i++)
       {
           if(i%2==1)
           {
              cnt1=cnt1+v1[i]; 
           }
           else
           cnt2=cnt2+v1[i];
           
       }
      
       if(cnt1==cnt2)
      {
          cout<<"draw"<<endl;
       }
       else  if(cnt1>cnt2)
        {
          cout<<"first"<<endl;
       }
       else
        {
          cout<<"second"<<endl;
       }
       }
    }
	
	return 0; 
} 

The answer for the test input:

1
11
2 1 1 5 1 5 1 5 1 5 1 

should be second.

3 Likes

how second …here sum of second type=6 first type=22 so first will be answer ?

Read the Editorial :slight_smile:

Hint:

First player gets 13 points; second player gets 15.

4 Likes

i misunderstood the question now got my mistake @ssjgz :grinning:

1 Like

#include <bits/stdc++.h>

using namespace std;

int main()
{
int t,n;
cin>>t;

while(t–)
{ cin>>n;

   int arr[n], even =0,odd=0;
   for(int i=0;i<n;i++)
   {
       cin>>arr[i];
   }
   sort(arr,arr+n,greater<int>());
   odd += arr[0];
   even  +=arr[1]+arr[2];
   if(n>=3)
   {
   for(int i =3;i<n;i++)
   {
       if (i % 2 == 0) 
        even += arr[i]; 
    else
        odd += arr[i]; 
   }
   if(even == odd)
   cout<<"draw"<<endl;
   else if (odd > even)
   cout<<"first"<<endl;
   else
   cout<<"second"<<endl;
   }

}
}

please correct me where am I wrong!

//Remeber choose the turns wisely so that P1 wins(we r on P1s side)

string whoWillWin(int n,int a[]){

int P1=0,P2=0;

if(n==1)

    P1+=a[0];

if(n==2){

    P1+=a[0];

    P2+=a[1];

}

 if(n>=3){

     P1+=a[0];

     P2+=a[1];

     P2+=a[2];

     for(int i=3;i<n;i+=2){

         P1+=a[i];

         if(i>3)

         P2+=a[i-1];

     }

     

 }

if(P1>P2)

    return "first";

else if(P1<P2)

    return "second";

else 

    return "draw";

}

int main(){

int T;

cin>>T;

for(int i=0;i<T;i++){

    int n;

    cin>>n;

    int a[n];

    sort(a,a+n,greater<int>());

for(int j=0;j<n;j++){

    cin>>a[j];

}

  string st=whoWillWin(n,a);

    cout<<st<<endl;

}

return 0;

}

Please Let me know where i am wrong???

//Remeber choose the turns wisely so that P1 wins(we r on P1s side)

string whoWillWin(int n,int a[]){

int P1=0,P2=0;

if(n==1)

    P1+=a[0];

if(n==2){

    P1+=a[0];

    P2+=a[1];

}

 if(n>=3){

     P1+=a[0];

     P2+=a[1];

     P2+=a[2];

     for(int i=3;i<n;i+=2){

         P1+=a[i];

         if(i>3)

         P2+=a[i-1];

     }

     

 }

if(P1>P2)

    return "first";

else if(P1<P2)

    return "second";

else 

    return "draw";

}

int main(){

int T;

cin>>T;

for(int i=0;i<T;i++){

    int n;

    cin>>n;

    int a[n];

    sort(a,a+n,greater<int>());

for(int j=0;j<n;j++){

    cin>>a[j];

}

  string st=whoWillWin(n,a);

    cout<<st<<endl;

}

return 0;

}

Please let me know where i am wrong???

/* package codechef; // don’t place package name! */

import java.util.Scanner;
import java.lang.;
import java.io.
;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(); //testcase input
int i;
int c1,c2;
while(t>0){
c1=0; c2=0; //chance 1, chance 2
int n=sc.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
c1=c1+a[0];
for(i=3;i<n;i=i+2){
c1=c1+a[i];
}
c2=c2+a[1];
for(i=2;i<n;i=i+2){
c2=c2+a[i];
}
if(c1>c2){
System.out.println(“first”);
}
else if(c1<c2){
System.out.println(“second”);
}
else if(c1==c2){
System.out.println(“draw”);
}
t=t-1;
}
}
}

I am getting a NZEC runtime error, can someone pls tell me what is wrong?

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

Here is the link to my submission
https://www.codechef.com/viewsolution/37374791

And thanks a lot for taking the initiative to check my code.

Consider the test input:

1
1
1
1 Like

Thanks a lot for your help, I could succesfully debug my code.
:smile:

1 Like

In the for loop starting with i=3 you have incremented cnt1 and cnt2 with 1 … you have to increment both these variables with the vector values like cnt1+=v[i] and cnt2+=v[i]

Thanks …

1 Like

#include
#include <bits/stdc++.h>

using namespace std;

int main() {
int test,n,p1=0,p2=0;
cin>>test;

while(test--){
cin>>n;
int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a, a + n, greater<int>());


        for(int i=0;i<n;i++){
           if(i==1||i==2)
               p2+=a[i];
           else if(i==0||i%2!=0)
               p1+=a[i];
           else
           p2+=a[i];

    }
    if(p1>p2)
    cout<<"first"<<endl;
    else if(p2==p1)
    cout<<"draw"<<endl;
    else if(p2>p1)
    cout<<"second"<<endl;

}


// your code goes here
return 0;

}

help please…

#include <stdio.h>

void sort(long a[],int n){
	int i,j;
	long temp;
	for(i=0;i<n-1;i++){
		for(j=0;j<n-i-1;j++){
			if(a[j]<a[j+1]){
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
}

int main(void) {
	int t,i;
	int n;
	 long p1,p2;
	scanf("%d",&t);
	while(t--){
	    scanf("%d",&n);
	    long a[10000];
	    for(i=0;i<n;i++){
	        scanf("%ld",&a[i]);
	    }
	    sort(a,n);
	    p1=0;
	    p2=0;
	    for(i=0;i<n;i++){
	    	if(i%2!=0){
	    		if(i==1)
	    			p2+=a[i];
	    		else
	    	 		p1+=a[i];
	    	}
	    	else{
	    		if(i==0)
	    			p1+=a[i];
	    		else
					p2+=a[i];
	    }
	    }
	    if(p1>p2)
	        printf("first\n");
	    else if(p1==p2)
	        printf("draw\n");
	    else
	    	printf("second\n");
	}
}

Dont know whats wrong , please help
edit- i got it i made silly mistake in the sorting :fp:

hey you didnt checked the n <3
like for input

1
2
1 2

the answer should be 2 but you code says 1

https://www.codechef.com/viewsolution/37842826
kindly help I cant understand which test case my code is failing.