Why my code is giving wrong answer on SPOJ? - c++

Problem: http://www.spoj.com/problems/TSORT/
Below code is giving correct output on my computer but it is giving wrong answer on spoj. I have tried this with several inputs and it is giving correct output. But still showing wrong answer on spoj.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
cin.tie(0);
ios::sync_with_stdio(false);
int *arr= new int[t];
for(int i=0;i<t;i++)
{
cin>>arr[i];
}
sort(arr,arr+t);
for(int i=0;i<t;i++)
{
cout<<"\n";
cout<<arr[i];
}
return 0;
}

As Tejas says, using cin and cout is not recommended in problems of optimization, as they are slower in comparison to scanf and printf. Extending his answer, you need to note that you use the STL sort that is slower than STL qsort. having this in mind, I tried your code with qsort and i got AC:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
int a[1000008];
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
qsort(a,n,sizeof(int),compare);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
The int function compare is needed for the usage of qsort.
More info:
http://www.cplusplus.com/reference/cstdlib/qsort/
http://www.cplusplus.com/reference/algorithm/sort/

If you use cout<<arr[i]<<endl; it will give TLE instead of WA. So it means it has something to do with new line.
However if you want to get your answer accepted, you have to use printf and scanf instead of cin, cout. It is always suggested in such contests to use scanf and printf for reading large inputs.
I tried the following code and it was accepted
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000008];
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}

Related

A problem of ending the implementation of the code

this code works well for all samples, but after all samples are finished, a problem occurs. I don’t know what happens and the program crashes. Is there a problem with this code?
i have this problem when i use strings arrays usualy can it be the problem?
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long long t,n;
int a[1000];
string str[1000];
int main()
{
cin>>t;
for(int r=1;r<=t;r++){
cin>>n;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxi)maxi=a[i];
};
//input first value
maxi=maxi+3;
for(int r1=0;r1<maxi;r1++){
str[1][r1]=(rand()%26)+'a';
}
for(int i=0;i<maxi;i++){
cout<<str[1][i];
}
cout<<endl;
//
for(int k=2;k<=(n+1);k++){
int w;
for(w=0 ; w<=a[k-1];w++){
str[k][w]=str[k-1][w];
};
for(int l=w-1;l<maxi;l++){
str[k][l]=(rand()%26)+'a';
};
for(int i=0;i<maxi;i++){
cout<<str[k][i];
}
cout<<endl;
}
}
return 0;
}
You are using elements of strings without allocating them.
Allocate elements by inserting
for(int i=1;i<=(n+1);i++){
str[i].resize(maxi);
}
just after
maxi=maxi+3;

Why is recursive solution faster than an iterative solution for this case?

I am trying to obtain a solution for this question https://www.spoj.com/problems/COINS/.
But oddly enough, my iterative solution:
#include <iostream>
using namespace std;
int main() {
int n;
while(cin >> n){
long long int dp[n+2];
dp[0]=0;
for(long long int i=1; i<=n; i++)
dp[i]=max(dp[i/2]+dp[i/3]+dp[i/4], i);
cout << dp[n] <<endl;
}
return 0;
}
gets a TLE, while the recursive solution for this(Not mine) gets accepted in no time:
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
map<int, long long> dp;
long long f(int n){
if(n==0) return 0;
if(dp[n]!=0) return dp[n];
long long aux=f(n/2)+f(n/3)+f(n/4);
if(aux>n) dp[n]=aux;
else dp[n]=n;
return dp[n];
}
int main(){
int n;
while(scanf("%d",&n)==1) printf("%lld\n",f(n));
return 0;
}
Shouldn't it be the opposite? I am genuinely confused.
The iterative solution is linear in n i.e it's O(n) while the recursive one is O(log_2 n ) from what I can understand.

Why is my Sliding Window algorithm not providing correct answers?

The problem is from a recent competition on Codechef.
According to the problem, for a given array of length N, find the maximum average of the numbers of any contiguous sub-array of length between A and B.
Now here is the logic that I have used. I have in a loop iterated over the possible contiguous sub-array length K from A to B. Then for every such K, I have used the sliding window mechanism to find the maximum sum of contiguous
elements of the array. Then I check if the already stored answer (the average) is smaller than the current maximum/K and update the answer.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
double ans=0;
for(int k=a;k<=b;k++)
{
long long maxi=0;
for(int i=1;i<=k;i++)
{
maxi=maxi+arr[i];
}
long long l=maxi;
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
maxi=max(maxi,l);
}
double x=(double)(((double)(maxi))/(double)(k));
ans=max(ans,x);
}
cout<<ans<<endl;
}
}
However, after implementing my logic over as shown, I am getting wrong answers even though I do not know how different they are the actual answer since I have not obtained anything wrong in my processes of debugging.
The following is an edit:
Are my previous code and the following the same?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
long double ans=0;
for(int k=a;k<=b;k++)
{
long long l=0;
for(int i=1;i<=k;i++)
{
l=l+arr[i];
}
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
}
}
cout<<ans<<endl;
}
}
Thanks for helping guys. I tried out the following and it sort of works:
cout<<fixed<<setprecision(10);
setprecision(10)

Unexpected results converting decimal number to binary

# include <iostream>
# include <cmath>
using namespace std;
int main()
{
int p;
int n;
int q;
cin>>n;
int r;
r=0;
for (int i=0,n; n>1; i=i+1,n=n/2)
{
p=n%2;
q= p*(pow(10,i));
r=r + q;
}
cout<<r;
system("pause");
return 0;
}
I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results.
For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.
your way is limited and not effient in converting to binary
you should use string it's more helpful and the range is big enough for any number
this is my code for decimal-to-binary
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
long long n;
string s,bin;
stack<string> res;
cin>>n;
while(n>0)
{
if(n%2==0)
s='0';
else
s='1';
res.push(s);
n=n/2;
}
while(!res.empty())
{
bin=bin+res.top();
res.pop();
}
cout<<bin<<endl;
return 0;
}
I hope it will help you.
int i=0,n;
should be
int i=0;
I don't know what you thought you were doing there, but what you are actually doing is declaring another variable n. Because the second n variable doesn't have a value the rest of the code doesn't work.
That's not the only problem with your code, but I'm sure you can figure out the rest.

programming g-adic expansion in c++

I want to program the g-adic expansion in the c++ language, but whatever I try, the output is still wrong. Let me first explain what the g-adic expansion is. The g-adic expansion is a way to represent numbers. For example, binary numbers, this is the 2-adic expansion of numbers. And hexadecimal is 16-adic expansion. So here is my code:
#include <iostream>
#include <cmath>
#include <complex>
#include <valarray>
using namespace std;
int main()
{
int x;
int g;
cin>>x;
cin>>g;
int k=log(x)/log(g)+1;
int e;
int b=0;
int* myArray=NULL;
myArray=new int[k];
for(int i=0;i<k;i++)
{
myArray[i]=0;
}
while(b!=k)
{
e=x/(g^(k-b-1));
myArray[b]=e;
x=x-e*g^(k-b-1);
b++;
}
b=0;
while(b!=k)
{
cout<<myArray[b]<<endl;
b++;
}
delete [] myArray;
myArray=NULL;
return 0;
}
So for example if I want to convert 105 to binary, x=105 and g=2, k is the length of the new number. In this case that is 7. int e=105/2^(7-1)=1. This is the first number. Then x=105-1*2^(7-1)=41. If you do this by hand, you will find that 105 becomes 1101001. But if I compile this piece of code, it just doesn't work. My question is what is wrong with this code?
The ^ doesn't do exponentiation. It's the exclusive-or operator. To do exponentiation, use the pow function.
e=x/std::pow(double(g),double(k-b-1));
myArray[b]=e;
x=x-e*std::pow(double(g),double(k-b-1));
You can see your program in action, with my changes, on IDE One.
here: run this program
#include <iostream.h>
#include <cmath>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int x;
int g;
cin>>x;
cin>>g;
while(x>g)
{
cout<<x%g<<endl;
x/=g;
}
cout<<x%g<<endl;
return 0;
}
works for 105 and 2 and does not need an array