Codechef problem ISHVALA(the promised land) - c++

In My Program what is logical error,
I don't understand Please Help me,,,I tried different Test case,,in which I found all Correct....
but in Codechef it's Wrong answer.
#include<iostream>
using namespace std;
int main()
{
int T,N,M;
cin>>T;
while(T)
{
cin>>N>>M;
int X,Y,S,c=0,d=0,i1=0,j1=0;
cin>>X>>Y>>S;
int x[X],y[Y];
if(X)
{
for(int i=0;i<X;i++)
cin>>x[i];
}
if(Y)
{
for(int i=0;i<Y;i++)
cin>>y[i];
}
for(int i=1;i<=N;i=i+S)
{
for(int j=1;j<=M;j=j+S)
{
for(int k=0;k<X;k++)
{
if(i<=x[k] && i+S-1>=x[k])
{
c=1;
i1=i-S+1;
}
}
for(int k=0;k<Y;k++)
{
if(j<=y[k] && j+S-1>=y[k] )
{
c=1;
j1=j-S+1;
}
}
if(c==0 && i+S-1<=N && j+S-1<=M)
{
d++;
}
c=0;
if(j1)
j=j1;
j1=0;
}
if(i1)
i=i1;
i1=0;
}
cout<<d<<endl;
T--;
}
}
The program(Question) in Codechef in beginners.
The link is here-enter link description here

Try this:
#include<iostream>
#define gc getchar_unlocked
using namespace std;
void fs(int &x)
{
register int c=gc();
x=0;
for(;c<48||c>57;c=gc());
for(;c>47&&c<58;c=gc())
x=(x<<1)+(x<<3)+c-48;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t,n,m,s,x,y,sum1,sum2,a,b;
fs(t);
while(t--)
{
fs(n);
fs(m);
fs(x);
fs(y);
fs(s);
sum1=0;
sum2=0;
a=0;
while(x--)
{
fs(b);
sum1=sum1+(b-a-1)/s;
a=b;
}
sum1=sum1+(n-a)/s;

Related

codechef isrec wrong answer

I am facing a wrong answer on my submission. I have run all the test cases that came to my mind but unfortunately all seem to give the correct output.
I even checked out others' submissions but didn't get very far. Apparently idea seems to be fine but maybe I am missing some serious detail. I simply have no idea.
The link to the question is:
https://www.codechef.com/CCRC21C/problems/ISREC
My code is:
#include<bits/stdc++.h>
using namespace std;
int num_consec_ones(string row,int m)
{
int c=0;
for(int i=0;i<m;i++)
{
if(row[i]==1) c++;
if(c>1 && row[i-1]==0 && row[i]==1)
{
c=-1;
break;
}
}
return c;
}
int start_index(string row,int m)
{
for (int i=0;i<m;i++)
{
if(row[i]==1)
return i;
}
}
void solve()
{
int n,m,flag=0;;
cin>>n>>m;
vector<string>row;
// row.reserve(n);
for(int i=0;i<n;i++)
{
string str;
cin>>str;
row.push_back(str);
for(int j=0;j<m;j++)
row[i][j]=row[i][j]-'0';
}
set<int>s;
for(int j=0;j<n;j++)
{
if(num_consec_ones(row[j],m)==-1)
{
cout<<"No"<<endl;
return;
}
if(num_consec_ones(row[j],m) && flag==1 && num_consec_ones(row[j-1],m)==0)
{
cout<<"No"<<endl;
return;
}
if(num_consec_ones(row[j],m) && flag==0)
{
s.insert(num_consec_ones(row[j],m));
flag=1;
}
}
if(s.size()==1)
{
set<int>start;
for(int j=0;j<n;j++)
{
if(num_consec_ones(row[j],m))
{
start.insert(start_index(row[j],m));
}
}
if(start.size()==1)
{
cout<<"Yes"<<endl;
return;
}
else{
cout<<"No"<<endl;
return;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}
Please help me out.
Thank you!

Finding next palindrome of a number, getting SIGABRT error

Hi ,I am trying to solve the question PALIN on SPOJ where the idea is to find the next immediate palindrome of a number. I tested my code on the IDE with all possible test cases I found and the program is working fine but I can't figure out why I am getting SIGABRT error when I submit the solution.Please I request someone help me figure out the problem in the code For yor reference I am providing the link of the question:- https://www.spoj.com/problems/PALIN/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
bool all_nines(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]!='9')
return false;
}
return true;
}
int reverse(int a)
{
int rev=0;
while(a)
{
rev=(rev*10)+(a%10);
a/=10;
}
return rev;
}
void convert_and_compare(string &s,int low,int high)
{
int low_num,high_num;
string low_str="",high_str="";
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
if(reverse(low_num)<=high_num)
{
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
else
{
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
s="";
s+=low_str+high_str;
}
void convert_and_compare(string &s,int low,int high,int mid)
{
int low_num,high_num,mid_num;
string low_str="",high_str="";
char mid_str;
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
mid_num=(s[mid]-'0');
if(reverse(low_num)>high_num)
{
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num!=9)
{
mid_num++;
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num==9)
{
mid_num=0;
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
s="";
s=low_str+mid_str+high_str;
}
string find_next_palin(string s)
{
if(all_nines(s))
{
s[0]='1';
for(int i=1;i<s.length();i++)
s[i]='0';
s+='1';
return s;
}
else
{
if(s.length()%2==0)
{
int low,high;
low=s.length()/2-1;
high=s.length()/2;
convert_and_compare(s,low,high);
return s;
}
else
{
int low,high,mid;
mid=s.length()/2;
low=mid-1;
high=mid+1;
convert_and_compare(s,low,high,mid);
return s;
}
}
}
int main()
{
int t;
scanf("%d",&t);
vector <string> v(t);
for(int i=0;i<t;i++)
{
cin>>v[i];
}
for(int i=0;i<t;i++)
{
if(v[i].length()==1)
{
if(v[i]=="9")
{ printf("11");
printf("\n");
}
else
{
printf("%d",(v[i][0]-'0')+1);
printf("\n");
}
}
else
{
string temp;
temp=find_next_palin(v[i]);
for(int j=0;j<temp.length();j++)
cout<<temp[j];
printf("\n");
}
}
return 0;
}

SIGTSTP error in some online ide but the program works fine in my terminal

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
cin>>t;
for(int i=1;i<=t;i++)
{
int c=0;int C[t]={};
cin>>n;
int P[n]={};int G[n]={};
for(int j=0;j<n;j++)
{
cin>>P[j];
}
for(int k=0;k<n;k++)
{
int asn=1;int b=5;
if(k!=0)
{
if(k<5)
{
for(int l=0;l<k;l++)
{
if(P[l]<P[k])
{
asn*=0;
}
}
}
else
{
while(b>0)
{
if(P[k-b]<P[k])
{
asn*=0;
}
b--;
}
}
}
G[k]=asn;
}
for(int f=0;f<n;f++)
{
if(G[f]==1)
{
c++;
}
}
cout<<c<<"\n";
}
}
Can anyone please help me in finding the SIGTSTP error in this code.In my PC this program works fine but gives a run-time error while compiling in Codechef's online IDE.
The link to the problem is https://www.codechef.com/OCT19B/problems/S10E

merge sort sorting partially

for input 6,2,9,1,5,8 i'm getting 1,2,6,9,5,8.This algorithm is written based on https://gist.github.com/mycodeschool/9678029#file-mergesort_c-c-L28
please help me in getting 1,2,5,6,8,9 as the output also point out the mistake here.Thanks in advance.
#include<iostream>
using namespace std;
void merge(int a[],int l[],int lc,int r[],int rc){
int i,j,k;
i=0;j=0;k=0;
while(i<lc && j<rc){
if(l[i]<r[j]){
a[k++]=l[i++];
}
else{
a[k++]=r[j++];
}
while(i<lc){
a[k++]=l[i++];
}
while(j<rc){
a[k++]=r[j++];
}
}
}
void mergesort(int a[],int n){
int mid;
if (n<2){
return;
}
mid=n/2;
int l[mid],r[n-mid];
for(int i=0;i<mid;i++){
l[i]=a[i];
}
for(int i=mid;i<n;i++){
r[i-mid]=a[i];
}
mergesort(l,mid);
mergesort(r,n-mid);
merge(a,l,mid,r,n-mid);
}
int main(){
int a[]={6,2,9,1,5,8};
int n=6;
mergesort(a,n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
your problem is that is that these loops
while(i<lc){
a[k++]=l[i++];
}
while(j<rc){
a[k++]=r[j++];
}
are inside this loop while(i<lc && j<rc) and they should be outside it as you copy the rest of a[] if b[] is done while a[] has some elements and like wise for b[] and this is the whole merge function
void merge(int a[],int l[],int lc,int r[],int rc)
{
int i,j,k;
i=0;
j=0;
k=0;
while(i<lc && j<rc)
{
if(l[i]<r[j])
{
a[k++]=l[i++];
}
else
{
a[k++]=r[j++];
}
}
while(i<lc)
{
a[k++]=l[i++];
}
while(j<rc)
{
a[k++]=r[j++];
}
}

SIGABRT Error during runtime (c++)

This is the skyscraper floors problem from spoj. I m getting SIGABRT when I run the code.Link for the problem is http://www.spoj.com/problems/SCRAPER/. When I run this code on my PC, it runs without any error but spoj's online judge shows SIGABRT error.
#include <iostream>
#include <vector>
using namespace std;
struct floor
{
unsigned int floornum;
bool calldone;
vector <floor*> connection;
floor()
{
floornum=0;
calldone=0;
}
};
struct elevator
{
unsigned int x,y;
elevator()
{
x=y=0;
}
};
struct skyscrapper
{
unsigned int f,e,a,b;
vector <elevator> ele;
vector <floor*> flooraddress; //this is a spoj problem
void read()
{
cin>>f>>e>>a>>b;
elevator *temp;
for(unsigned int i=0;i<e;i++)
{
temp=new elevator;
cin>>(*temp).x>>(*temp).y;
ele.push_back(*temp);
}
for (unsigned int i=0;i<f;i++)
{
floor* tempp=new floor;
(*tempp).floornum=i;
flooraddress.push_back(tempp);
}
}
void allotaddress()
{
unsigned int j,k;
for(unsigned int i=0;i<ele.size();i++)
{
j=ele[i].y;
k=ele[i].x;
while(j<f)
{
if(j!=ele[i].y)
{
(*(flooraddress[j])).connection.push_back(flooraddress[j-k]);
(*(flooraddress[j-k])).connection.push_back(flooraddress[j]);
}
j=j+k;
}
}
}
};
bool flag;
bool traverse(floor* m,int destination)
{
if((*m).calldone==1)
{
return 0;
}
(*m).calldone=1;
if((*m).floornum==destination)
return 1;
if((*m).connection.empty())
return 0;
for(int i=0;i<(*m).connection.size();i++)
{
flag=traverse(((*m).connection[i]),destination);
if(flag==1)
return flag;
}
return 0;
}
int main()
{
int n;
cin>>n;
skyscrapper iit[n];
bool ans[n];
for(int i=0;i<n;i++)
{
iit[i].read();
iit[i].allotaddress();
ans[i]=traverse(iit[i].flooraddress[iit[i].a],iit[i].b);
}
for(int i=0;i<n;i++)
{
if(ans[i]==1)
cout<<"It is possible to move the furniture."<<endl;
else
cout<<"The furniture cannot be moved."<<endl;
}
return 0;
}