求助
查看原帖
求助
316827
Temperature_automata楼主2020/11/18 13:12

全WA,样例是正确的
代码:

#include <iostream>
#include <algorithm>

using namespace std ;

const int N = 1e6 ;
int n , m ;
struct node {
    int num , sum ;
}a[N];
int q ;

bool cmp (node a,node b) {
	return a.sum<=b.sum ;
}

int find ( int x ) {
    int lb = 1 ;
    int rb = n ;
    while(lb<rb) {
        int mid = (lb+rb)/2 ;
        if(a[mid].sum<x) {
            lb = mid + 1 ;
        }
        else {
            rb = mid- 1 ;
        }
    }
    if(a[lb].sum==x) return lb ;
    else return -1 ;
}

int main ( )  {
    cin >>n >> m ;
    for(int i = 1 ; i <= n ; i ++  ) {
        cin >> a[i].sum;
        a[i].num=i;
    }
    sort(a+1,a+n+1,cmp);
    while(m--) {
        cin >>q ;
        cout << find(q) << " " ;
    }
}
2020/11/18 13:12
加载中...