CLEANUP - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

No real tricks here. An easy approach is to create a boolean array b[] of size n where item b[i] is true if and only if i does not appear in the input list. Make two passes through the array, each time keeping track of the number of “true” items seen so far. When an entry i is hit such that b[i] is true, output i if and only if the number of true items seen so far is odd/even depending on whether it is the first/second pass.

4 Likes

why this code is giving runtim error ???

#include<stdio.h>
int main(){
short t,n,m,a[1000]={0},x,k,l,count,j;
char d[1000];
scanf("%d",&t);
int i;
for(i=0;i<t;i++){
scanf("%d%d",&n,&m);
for(j=0;j<1000;j++)
a[j]=0;
for(j=0;j<1000;j++)
d[j]=’\0’;
for(j=0;j<m;j++)
{
scanf("%d",&x);
a[x-1]=1;

    }
    count=0,k=0,l=0;
    for(j=0;j<n;j++){
       if(a[j]==0){
       count++;
       if(count%2==0&&count!=0)
        d[j]='s';
       if(count%2==1) d[j]='c';
       }
    }
    for(j=0;j<n;j++)
    if(d[j]=='c')
    printf("%d ",j+1);
    printf("\n");
    for(j=0;j<n;j++)
    if(d[j]=='s')
    printf("%d ",j+1);
    printf("\n");

}

}

1 Like

I’m getting an NZEC on my Python code: http://discuss.codechef.com/questions/42958/cleaning-up-runtime-error

Help please!

This is giving Runtime error… dnt knw why… plz help

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class CleaningUp {
public static void main(String args[]) throws Exception{

	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
	int n=Integer.parseInt(br.readLine());
	for(int i=0;i<n;i++)
	{

		String[] s=br.readLine().split(" ");
		int totalJobs=Integer.parseInt(s[0]);
		int JobsCompleted=Integer.parseInt(s[1]);
		
		String JobsCompletedList=br.readLine();
		String s1="";
		for(int j=1;j<=totalJobs;j++)
		{
			if(!(JobsCompletedList.contains(String.valueOf(j)))){
				s1+=j+" ";
			}
		}
		s1=s1.trim();
		s=s1.split(" ");
		String chefJobs="",assJobs="";
		for(int j=0;j<totalJobs-JobsCompleted;j++){
			if(j%2==0){
				chefJobs+=s[j]+" ";
			}
			else
				assJobs+=s[j]+" ";
		}
		
		out.println(chefJobs.trim());
		out.println(assJobs.trim());
	}
	
	out.close();
}

}

#include<stdio.h>
int main()
{
int i,t,j,k,n,m,a[1001],c[1001],d[1001];
int p,q,temp;
scanf("%d",&t);
while(t–)
{
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
for(j=i+1;j<m;j++)
{
if(a[j]<a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
j=0;
k=0;
p=0;
q=0;
for(i=1;i<=n;i++)
{
if(i==a[j])
j=j+1;
else
{
if(k==0)
{
c[p]=i;
p=p+1;
k=1;
}
else
{
d[q]=i;
q=q+1;
k=0;
}
}
}
for(i=0;i<p;i++)
printf("%d “,c[i]);
printf(”\n");
for(i=0;i<q;i++)
printf("%d “,d[i]);
printf(”\n");
}
return 0;
}

can someone please help me out with my


[1] , its giving wrong ans. cant figure out why,, thanks!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
 
#define gc getchar_unlocked
#define pc putchar_unlocked
#define ull	    long long
#define forloop(i,a,b)		for(ull i=a;i<=b;i++)


using namespace std;


inline void fs(ull &x)
{
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}

int main(){

	ull t,n,m,jobs[2][511],turn,l1,l2;
	fs(t);
	while(t--){
		turn = l1 = l2 =0;
		fs(n);
		fs(m);
		ull completed[m];
		forloop(i,0,m-1)
			fs(completed[i]);
		sort(completed,completed+m);
    
		for(ull i=0,j=0;i<n;i++){
			if((i+1)!=completed[j]){
				if(turn==0){
					jobs[0][l1] = i+1;
					l1++;
					turn = 1;
					// cout<<"chef\n";
				}
				else{
					jobs[1][l2] = i+1;
					l2++;
					turn = 0;
					// cout<<"assistant\n";
				}
			}
			else{
				// cout<<"job done already\n";
				j++;
			}
		}
		if(l1>0){
			printf("%lld", jobs[0][0]);
		forloop(i,1,l1-1)
			printf(" %lld", jobs[0][i]);
		}
		printf("\n");
	
		if(l2>0){
			printf("%lld", jobs[1][0]);
		forloop(i,1,l2-1)
			printf(" %lld", jobs[1][i]);
		}
		printf("\n");
	
	}
	return 0;
}


  [1]: https://www.codechef.com/viewsolution/8684365

What is Wrong with this Code ? I am Getting a wrong Answer !

for _ in range(int(input())):
n,m = map(int,input().split())
completed = set(sorted(map(int,input().split())))
ful = set(sorted(range(1,n+1)))
r = ful - completed
chef = []
assi = []
for i,ind in enumerate(r):
    if i%2 == 0:
        chef.append(str(ind))
    else:
        assi.append(str(ind))
print(' '.join(chef))
print(' '.join(assi))

is there any hidden test case with this question? please help me because i am getting wrong answer but public test cases are running fine

1 Like

can someone tell me why i am getting run time error [SEGSIEV] in my code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t–){
int m,n,i,indices,count=0;
cin>>n>>m;
int a[n]={0};
while(m–){
cin>>indices;
a[indices-1]=1;
}

	  for(i=0;i<n;i++){
	  	if(a[i]!=1){
		  
	  	count++;
	  	if(count%2==0&&count!=0)
	  	a[i]=3;
	  	if(count%2==1)
	  	a[i]=4;		     
	  }
}
	  
	for(i=0;i<n;i++){
		if(a[i]==4)
		cout<<i+1<<" ";

}
cout<<endl;
for(i=0;i<n;i++){

		if(a[i]==3)
		cout<<i+1<<" ";
	}
	cout<<endl;
}
return 0;

}

1 Like

iam getting wrong code despite that i have checked my code for several cases on my machine
here’s my approach
#include
using namespace std;
int main()
{int tc;char j[1000];
cin>>tc;
while(tc–)
{int to=0,done=0,c=0,i=0,ch=0;
cin>>to>>done;

for(i=0;i<done;i++)
{cin>>c;
j[c-1]=‘d’;
}
for(i=0,ch=0;i<to;i++)
{if(j[i]!=‘d’)
{if (ch==0)
{j[i]=‘c’;
ch++;}
else {j[i]=‘a’;
ch–;}

}
}
for(i=0;i<to;i++)
{if(j[i]==‘c’)
cout<<(i+1)<<’ ‘;
}cout<<endl;
for(i=0;i<to;i++)
{if(j[i]==‘a’)
cout<<(i+1)<<’ ‘;
}cout<<endl;
for(i=0,ch=0;i<to;i++)
j[i]=’ ';
}return 0;}

Can’t we use Linked List here? By using arrays we are actually wasting memory from what I can understand. Let’s say in Array there are 1000 Elements and 900 Elements are input. So, in this case, we are actually wasting a lot of space here of 900 elements, and on top of that, we have to traverse through them each time. With Linked list, we can 1st Create a Linked List of 6 elements then delete 3 elements from it. I know to delete it actually have to traverse but when printing the answer all we would have are jobs that are need to be done. In this case 3 elements. Now traverse alternatively and print them. Any suggestion or correction will be helpful if u find this method wrong.

def main():
t = int(input())

for _ in range(t):
    n, m = map(int, input().split())
    finished_job = set(map(int, input().split()))
    unfinished_job = list(map(str, {j for j in range(1, n + 1)} - finished_job))
    unfinished_job.sort()
    print(' '.join(unfinished_job[::2]))
    print(' '.join(unfinished_job[1::2]))

main()

Any idea why this doesn’t work? Been brainstorming for like 1 hour+ for the reason.

Why am I getting WRONG ANSWER here:

tests=int(input())
for i in range(0,tests):
    j=input().split()
    total=int(j[0])
    done=int(j[1])
    jdone=input().split()
    jdone=[int(i) for i in jdone]
    jobs=[]
    for i in range(0,total):
        jobs.append(i+1)
    jobs=set(jobs)
    jdone=set(jdone)
    jleft=(jobs-jdone)
    jleft=list(jleft)
    chef=[]
    assist=[]
    jloop=0
    for i in jleft:
        if jloop%2==0:
            chef.append(str(i) )
        else:
            assist.append(str(i) )
        jloop+=1
    print(' '.join(chef) )
    print(' '.join(assist) )

I am got all the public test cases CORRECT!

Why does this c++ code give WA? I have run it on several test cases, works correctly in all of them

    #include 

    using namespace std;

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

	for(int i = 0; i < t ; i++)
    {
        int n , m, doer = 0;
        cin >> n >> m;

        char jobs[n+1];

        int jdone = 0;

  //to store which jobs have been done    
        for(int j = 0; j < m ; j++ )
        {
            cin >> jdone;
            jobs[jdone] = 'd';
        }

   //to store which jobs the chef is doing, and which the assistant is doing
        for(int k = 1; k < n+1 ; k++)
        {
            if(jobs[k] == 'd')
                continue;
            else
            {
                if(doer % 2 == 0)
                {
                    jobs[k] = 'c';
                }
                else
                    jobs[k] = 'a';

                doer++;
            }
        }

   //to print out chef's jobs
        for(int l = 1; l < n+1; l++)
        {
            if(jobs[l] == 'c')
            {
                cout << l << " " ;
            }
        }

        cout << endl;

   //to print assistant's jobs
        for(int l2 = 0; l2 < n+1; l2++)
        {
            if(jobs[l2] == 'a')
            {
                cout << l2 << " " ;
            }
        }

        cout << endl;
    }
	return 0;
}

import java.util.Scanner;
class Solution
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int cases= sc.nextInt();
int i,j;
for(i=0;i<cases;i++)
{
int n = sc.nextInt();
int m = sc.nextInt();
String chef="";
String assi="";
boolean arr[] = new boolean[n+1];
for(j=1;j<=m;j++)
{
arr[sc.nextInt()]=true;
}
int count=0;
for(j=1;j<=n;j++)
{
if(arr[j]==false)
{
if(count%2==0)
{
chef=chef+String.valueOf(j);
chef=chef+" ";
}
else
{

                                            assi=assi+String.valueOf(j);
                                            assi=assi+" ";
                                    }
                                    count++;
                            }
                    }
                    if(chef.length()!=0)
                    {
                            System.out.println(chef);
                    }
                    if(chef.length()==0)
                    {
                            System.out.println();
                    }
                    if(assi.length()!=0)
                    {
                            System.out.println(assi);
                    }
                    if(assi.length()==0)
                    {
                            System.out.println();
                    }

            }

    }

}
Comments

#include<stdio.h>
#include<stdlib.h>
#define max 1000
void swap(int *a,int *b);
void sort(int *a,int n);
int main()
{
int n,m,t,a[max],a1[max/2],a2[max/2],i,j,w,c,k;
scanf("%d",&t);

for(k=0;k<t;k++)
{
//printf(“test=%d\n”,k);
j=1,w=1,c=1;
scanf("%d %d",&n,&m);

for(i=1;i<=m;i++)
scanf("%d",&a[i]);
sort(a,m);
//printf(“after sort\n”);
for(i=1;i<=n;i++)
{
if(i==a[j])
{
//printf(“i=%d j=%d\n”,i,a[j]);
j++;
}
else
{
if(w<c)

a2[w++]=i;

//printf(“i=%d w=%d %d\n”,i,a[w],w);
else

a1[c++]=i;

//printf(“i=%d c=%d %d\n”,i,a[c],c);
}}

if(c!=1)
{
//printf(“chief\n”);
for(i=1;i<c;i++)
printf("%d “,a1[i]);
printf(”\n");
}
else
printf("\n");
if(w!=1)
{
//printf(“ass\n”);
for(i=1;i<w;i++)
printf("%d “,a2[i]);
printf(”\n");
}
else
printf("\n");

}
return 0;}

void sort(int *a,int n)
{
//printf(“sort start\n”);
int i,j;
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);

}}
//for(i=1;i<=n;i++)
// printf("%d ",a[i]);
}

void swap(int *a,int *b)
{

int temp;
temp=*a;
*a=*b;
*b=temp;
}

why code is giving wron answer??

i have tried all test cases and all possible cases for my solution still getting wrong answer fir this.dont know why…my solution link is:-https://www.codechef.com/viewsolution/14310780

If anyone finds where the error is then please help me…

hello friends
my answer is for solve bug of other participants:

import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter;

public class CleaningUp { public static void main(String args[]) throws Exception{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{

    String[] s=br.readLine().split(" ");
    int totalJobs=Integer.parseInt(s[0]);
    int JobsCompleted=Integer.parseInt(s[1]);

    String JobsCompletedList=br.readLine();
    String s1="";
    for(int j=1;j<=totalJobs;j++)
    {
        if(!(JobsCompletedList.contains(String.valueOf(j)))){
            s1+=j+" ";
        }
    }
    s1=s1.trim();
    s=s1.split(" ");
    String chefJobs="",assJobs="";
    for(int j=0;j<totalJobs-JobsCompleted;j++){
        if(j%2==0){
            chefJobs+=s[j]+" ";
        }
        else
            assJobs+=s[j]+" ";
    }

    out.println(chefJobs.trim());
    out.println(assJobs.trim());
}

out.close();

}
}
input:
1
13 1
12

your problem is that: when say 12 solved then 1 2 and 12 imagined solved and not run for this situation

1 Like

t=int(raw_input())
def print_values(s):
if len(s)==0:
print " "
else:
for i in s:
print i,

for i in xrange(t):
n,m=map(int,(raw_input().split(’ ‘)))
finished=map(int,raw_input().strip().split(’ '))
unfinished=[i+1 for i in xrange(n) if (i+1) not in finished]
chef_job=[unfinished[i] for i in xrange(len(unfinished)) if i%2==0]
assis_job=[unfinished[i] for i in xrange(len(unfinished)) if i%2==1]
print_values(chef_job)
print “\n”
print_values(assis_job)

why this code has runtime error ?

Hi. Want to know where I went wrong with my code. It is giving wrong answer when submitted. Please help.
#include <stdio.h>

int main(void) {
int t, m, n, i, temp;
scanf("%d", &t);
while(t){
scanf("%d %d", &n, &m);
int jobs[n];
for(i=0;i<m;i++){
scanf("%d", &temp);
jobs[temp-1] = 1;
}
int assist[n/2 + 1];
int temp = 1, k=0;
for(i=0;i<n;i++){
if(jobs[i] != 1){
if(temp%2 != 0){
printf("%d “, i+1);
}
else{
assist[k] = i+1;
k++;
}
temp++;
}
}
printf(”\n");
if(k != 0){
for(i=0;i<(n/2)+1;i++){
if(assist[i] <= n && assist[i] > 0)
printf("%d ", assist[i]);
else
break;
}
}

    printf("\n");
    t--;
}
return 0;

}