I do not know why my code is wrong, its just not right!

136 views
Skip to first unread message

fe ding

unread,
May 9, 2021, 9:51:05 AM5/9/21
to C++ GCU Forum
Here is the question I am trying to solve:
There are a number of N students in open judge, and each of them has passed some questions on the open judge website. The Boss gave M questions, each time he asks a question, he will give a number K, you have to quickly answer the number of students with the number of questions K
Input Format:
In the first line, the integers N and m=M separated by two spaces indicating the number of people and the number of questions. In the second line, there are a number of N integers separated by spaces, indicating the number of questions each student has solved. Next, there are M rows, each with an integer, which represents a question from the Boss.
Output Format:
M lines, each line an integer, indicating the answer to the corresponding question.
Example Input:
7 4
6 2 1 2 6 2 5
6
5
8
2
Example Output:
2
1
0
3

Here is My code;
#include<bits/stdc++.h> 
using namespace std; 
const int maxn=100000+5; 
int n,m,a[maxn],b[maxn],tp[maxn];
int cnt; 
inline void merge(int l,int r) {
if(l>=r)return ; 
int mid=(l+r)/2;
int i=l,j=mid+1,k=l; 
  merge(l,mid); 
  merge(mid+1,r); 
  while(i<=mid&&j<=r) 
  { 
  if(a[i]<a[j])tp[k++]=a[i++]; 
  else tp[k++]=a[j++]; 
  }
while(i<=mid)tp[k++]=a[i++]; 
  while(j<=r)tp[k++]=a[j++];
for(int i=l;i<=r;i++)a[i]=tp[i]; } 
//bool apk; 
inline bool cmp(int x,int n) { 
     int l=1,r=n; 
  while(l<=r) {
int mid=(l+r)/2; 
  if(x==a[mid])
  return true; 
// apk=true; 
  break;
  else if(x>a[mid]) { 
 l=mid+1;
  else if(x<a[mid])
{
r=mid-1; 
  } 
  }
/*if(apk!=true)*/ 
  return false;
signed main() {
ios::sync_with_stdio(0);
std::cin.tie(0); 
 cin>>n>>m;
for(int j=1;j<=n;j++)cin>>a[j]; 
  merge(1,n); 
  for(int i=1;i<=m;i++) { cnt=0; 
 cin>>b[i]; 
  for(int j=1;j<=n;j++)if(cmp(b[i],a[j]))cnt++;
cout<<cnt<<endl; 
  } 
  return 0; 
}
please help!

Reply all
Reply to author
Forward
0 new messages