0pts,大佬救救孩子吧(二分)

P1678 烦恼的高考志愿

simon_620827 @ 2024-01-30 11:33:32

#include<bits/stdc++.h>
#define int long long
const int N = 1e5 + 5;
int n,m;
int a[N],b[N];
bool tot[N];
using namespace std;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);

    int n,m;
    cin >> n >> m;
    for(int i = 1;i <= n;++i){
        cin >> a[i];
    }
    for(int i = 1;i <= m;++i){
        cin >> b[i];
    } 

    sort(b + 1,b + m + 1);
    for(int i = 1;i <= n;++i){
        int ans = -1;
        int l = 1, r = m;
        while(l <= r) {
            int mid = l + (r - l) / 2;
            if(b[mid] < a[i]){
                l = mid + 1;
            } else if(b[mid] > a[i]) {
                r = mid - 1;
            } else if(b[mid] == a[i]) {
                ans = mid;
                break;
            }
        }
        if(ans != -1) cout << a[i] << " ";
    }
    return 0;
}

by LionBlaze @ 2024-01-30 11:56:56

cout << a[i] << " ";

?


by hqzx_610fuziyu @ 2024-02-16 17:21:32

需要开longlong


|