
Link: http://zerojudge.tw/ShowProblem?problemid=b542
每次詢問使用爬行法O(N)求是否有相差K的兩數,總複雜度O(NQ)
AC code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
using namespace std; int a[100000]; int n,k; bool () { int i=0,j=1; while(j<n) { if(a[j]-a[i]>k) i++; else if(a[j]-a[i]<k) j++; else if(i==j) j++; else return true; } return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); int q; cin>>n>>q; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); while(q--) { cin>>k; if(solve()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }
|
近期评论