for(int i = 0; i < n; i++)
{
int Time;
int t1 = searchV(arr[i][0], V, x);
int t2 = searchW(arr[i][1], W, y);
}
Your time comp is O(n * x * y)
for(int i = 0; i < n; i++)
{
int Time;
int t1 = searchV(arr[i][0], V, x);
int t2 = searchW(arr[i][1], W, y);
}
Your time comp is O(n * x * y)
Oh yeah, got it. thanks!
still I am getting wrong answer(partially correct answer) CodeChef: Practical coding for everyone
Earlier I was using linear search, so coplexity was around O(n^2) but now I am using binary search followed by quick sort now getting O(nlogn) but still something went wrong. pls help me to find out
Use upper_bound and lower_bound. This will save your time
This is the third time I have seen the exact same mistake. Check your input loops.
https://www.codechef.com/viewsolution/32941688
@sebastian I have used upper_bound but still code is partiall submitted. Totally frustrated now.
@everule1 didn’t get ur point, plz elaborate!
int W[y];
for(i = 0; i < x; i++){
scanf("%d", &W[i]);
}
Do you see a problem now?
ohhh! got it.
std::upper_bound
returns an iterator in the range [first, last)
and hence in your code, it never checks the last element of the V[]
and W[]
. Change upper_bound(V, V + x - 1, ...)
to upper_bound(V, V + x, ...)
and similarly for W
. Hopefully it’ll work now.
I guess it won’t make your code correct, but why bother writing a sort function.
We already have sort(w, w+y);
I don’t know C++. so I am writing code in C. For upper_bound(), initially I read about that then applied in my code.
I’ll suggest that you explore some basic C++ features that are very helpful in CP. For example, I modified your 79 - liner AC code, and just modified a few stuff like unnecessary headers (If you include <bits/stdc++.h>
, you don’t need to explicitly write many headers such as <iostream>, <stdio.h>
, etc.) and changed your Quick Sort code and modified it to std::sort
, and the same code became 38 lines. Also, std::lower_bound
and std::upper_bound
can be implemented using binary search, but again, why bother to do so when you have it inbuilt? Short codes help in CP, because shorter the code, easier it is to debug (less lines where you can make an error).
PS: Even C has qsort()
, but apparently C++ std::sort()
is faster.
Thanks for ur valuable suggestion. I will surely learn C++.
Also read about STL. This will really help you.
Hi,
I started learning C++ yesterday from GFG. I covered basics, string and function. some topics are like same as in C. Currently I started STL, it’s very interesting and skipped oops concept.
Is it necessary to cover oops for competitive programming?
I think it is not needed for cp
I am also getting this error
here is my code
#include <stdio.h>
int main(void) {
// your code goes here
int n,i,j,len,s=0,o,k;
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%d",&len);
int num[len];
for (j=0;j<len;j++){
scanf("%d",&num[j]);
s=s+num[j];
}
o=0;
while (s!=0){
o++;
for (k=0;k<len;k++){
while(num[k]!=0){
if (num[k]==1){
num[k]=num[k]-2*0;
if(num[k]>0){
num[k]=num[k]*-1;
}
}
else{
num[k]=num[k]-2*1;
if(num[k]>0){
num[k]=num[k]*-1;
}
}
s=s+num[k];
}
}
}
printf("%d\n",o);
}
return 0;
}
Plz anyone help me to resolve it asap ,bcoz this camp will end in 18 hours https://www.codechef.com/MAY222D/problems/MAKEZERO