[SOJ]15. 守衛

題目URL: https://pc2.tfcis.org/dev/index.php/problem/view/15/
下圖是一份WA的code,不管是賽中or賽後我都de不到bug,但是剛剛發現了一件事,我要算距離的點相減沒有加abs(我是垃圾),所以附上了AC code做比較。(提醒,賽中我忘了lower_bound的寫法,然後我也是垃圾不知道有開放網路,所以當場手刻了二分搜結果吃了TLE,好孩子千萬別學我這個垃圾)


#pragma GCC optimizer("O2")
#include<bits/stdc++.h>
#define int long long int 
#define IOS ios_base::sync_with_stdio(false)
#define TI cin.tie(NULL)
using namespace std;
signed main()
{
    int a,b,c,d,e;
    vector<int> v;
    cin >> a >> b  >> c;
    for(int i=0;i<a;i++)
    {
        cin >> d;
        v.emplace_back(d);
    }
    sort(v.begin(),v.end());
    while(b--)
    {
        cin >> e;
        int low=(lower_bound(v.begin(),v.end(),e)-v.begin());
        if(low==0)
        {
            if(abs(v[low]-e)<= c)
                cout << "Yes" <<endl;
            else
                cout << "No" <<endl;
        }
        else if(min(abs(v[low]-e),abs(e-v[low-1]))<=c)
            cout << "Yes" <<endl;
        else
            cout << "No" <<endl;
    }
    return 0;
}

留言

這個網誌中的熱門文章

[ZJ] b229: TOI 2009 第一題:路徑問題

交大資工(APCS組)(面試&心得)

滿是挫傷的ION CAMP