XENTASK - Editorial

basic-prog
cakewalk
editorial
march17
xentask

#1

PROBLEM LINK:

Practice
Contest

Author: Vaibhav Tulsyan
Testers: Sergey Kulik, Kamil Dębowski
Editorialist: Pawel Kacprzak

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Most basic programming

PROBLEM:

There are N tasks numbered from 1 to N to perform. Two people, Xenny and Yana, have to complete all these tasks alternatingly. It means that either Xenny performs tasks with odd numbers and Yana with even numbers, or Xenny performs tasks with even numbers and Yana with odd numbers. These two ways are the only possibilities to fulfill the task. Moreover, we know that Xenny takes X_i seconds to complete the i-th task, while Yana takes Y_i to do that. The goal of the problem is to compute the minimum possible time in which Xenny and Yana can complete all the tasks.

QUICK EXPLANATION:

Calculate the time needed to complete the tasks for each of two distinct ways of doing that and return the minimum of these times.

EXPLANATION:

The main observation is that since all the tasks have to performed alternatingly by Xenny and Yana, then there are only two distinct ways of completing all the tasks:

  1. Xenny starts first, which means that Xenny performs tasks with odd numbers: 1, 3, \ldots, while Yana performs tasks with even numbers: 2, 4, \ldots
  2. Yana starts first, which means that Yana performs tasks with odd numbers: 1, 3, \ldots, while Xenny performs tasks with even numbers: 2, 4, \ldots

If we assume that for i = 1, 2, \ldots, N X* is the time for performing the i-th task by Xenny and Y* is the time for performing the i-th task by Yana, then we can solve the problem by the following pseudocode:

xenny_starts_first = 0 // time to perform all the tasks using the first method,
                       // i.e. when Xenny starts first
for i = 1 to N:
    if i is odd:
       xenny_starts_first = xenny_starts_first + X* // add time of completing 
                                                      // the i-th task by Xenny
   else:
      xenny_starts_first = xenny_starts_first + Y* // add time of completing 
                                                     // the i-th task by Yana

yana_starts_first = 0 // time to perform all the tasks using the second method, 
                             // i.e. when Yana starts first 
for i = 1 to N:
    if i is odd:
       yana_starts_first = yana_starts_first + Y* // add time of completing 
                                                    // the i-th task by Yana
   else:
      yana_starts_first = yana_starts_first + X* // add time of completing 
                                                   // the i-th task by Xenny

print minimum(xenny_starts_first, yana_starts_first)

Since in order to compute the time of completing the tasks for each of the two methods we need only to iterate once over all tasks, the overall time complexity of this solution is O(N).

AUTHOR’S AND TESTER’S SOLUTIONS:


Setter's solution can be found [here][333]. First tester's solution can be found [here][444]. Second tester's solution can be found [here][555]. Editorialist's solution can be found [here][666].

#2

#include<stdio.h>
int main()
{
int t,n,i,z,w;
scanf("%d",&t);
while(t–)
{
scanf("%d",&n);
int a[n],z=0;
int b[n],w=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a*);
}
for(i=1;i<=n;i++)
{
scanf("
%d",&b*);
}
for(i=1;i<=n;i++)
{
if(i%2==0)
{
z+=a*;
}
else
{
z+=b*;
}
}
for(I=1;i<=n;i++)
{
if(i%2==0)
{
w+=b*;
}
else
{
w+=a*;
}
}
int sum=z<w?z:w;
printf("%d",sum);
}
return 0;
}
/* help me it’s shoing rong answer */


#3

#include<stdio.h>
int main()
{
int t,n,i,z,w;
scanf("%d",&t);
while(t–)
{
scanf("%d",&n);
int a[n],z=0;
int b[n],w=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a*);
}
for(i=1;i<=n;i++)
{
scanf("
%d",&b*);
}
for(i=1;i<=n;i++)
{
if(i%2==0)
{
z+=a*;
}
else
{
z+=b*;
}
}
for(i=1;i<=n;i++)
{
if(i%2==0)
{
w+=b*;
}
else
{
w+=a*;
}
}
int sum=z<w?z:w;
printf("%d",sum);
}
return 0;
}


#4

@vayuhu Change the indexing in your code or make array of size n+1 this will get you can AC.In your code you have made array of n which will be from 0 to n-1 and you are accessing 1 upto n elements.You are not able to access very first element of the array.Hope it helps :slight_smile:
Edit:Your AC solution Click here


#5

hi
I need a little help
codechef is not considering my code as correct answer but I have run is on ideone . com and its running perfectly there.
Can you please tell me where am I wrong.
#include
using namespace std;
int main()
{
int t,n,i;
cin>>t;
int s[2]={0,0};
if(t>=0)
{
cin>>n;
int a[n],b[n];
for(int j=0;j<t;j++)
{

        for(i=0; i<n; i++)
        {
            cin>>a*;
        }
        for(i=0; i<n; i++)
        {
            cin>>b*;
        }
        for(i=0;i<n;i++)
        {
            if(i%2==0)
            {
                s[0]+=a*;
                s[1]+=b*;
            }
            else
            {
                s[0]+=b*;
                s[1]+=a*;
            }
        }
        if(s[0]<=s[1])
            cout<<s[0]<<"

“;
else
cout<<s[1]<<”
";

        s[0]=s[1]=0;
    }
}

return 0;

}
thanks


#6

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

import java.util.;
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();
long n,s1=0,s2=0;
long xi[]=new long[20000];
int i;
while(t!=0)
{
t–;
n=sc.nextLong();
for(i=0;i<n;i++)
{
xi
=sc.nextLong();
if(i%2==1)s1+=xi*;
if(i%2==0)s2+=xi*;
}
for(i=0;i<n;i++)
{
xi*=sc.nextLong();
if(i%2==1)s2+=xi*;
if(i%2==0)s1+=xi*;
}
if(s1>s2)System.out.println(s2);
else System.out.println(s1);
}
}
}
What is wrong with my code?


#7

Can you edit your answer, select the code and use the “Insert code” button. That would make your code readable. Then I can help you dear :slight_smile:


#8

Nice of you to help him!


#9

happy coding :slight_smile: