結果
問題 | No.1508 Avoid being hit |
ユーザー |
|
提出日時 | 2021-05-14 22:57:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 205 ms / 3,000 ms |
コード長 | 2,936 bytes |
コンパイル時間 | 2,374 ms |
コンパイル使用メモリ | 209,948 KB |
最終ジャッジ日時 | 2025-01-21 11:58:20 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 |
ソースコード
#include<bits/stdc++.h> #define int long long using namespace std; signed main(){ int N,Q; cin>>N>>Q; vector<int>A(Q),B(Q); for(int i=0;i<Q;i++) cin>>A[i],A[i]--; for(int i=0;i<Q;i++) cin>>B[i],B[i]--; vector<set<pair<int,int>>>dp(Q+1); for(int i=Q;i--;){ set<pair<int,int>>&st=dp[i+1]; auto it=st.upper_bound({A[i],N+2}); if(it==st.begin() || prev(it)->second<=A[i]){ auto itr=st.insert({A[i],A[i]+1}).first; if(itr!=st.begin() && prev(itr)->second==itr->first){ itr=st.insert({prev(itr)->first,itr->second}).first; st.erase(prev(itr)); st.erase(next(itr)); } if(next(itr)!=st.end() && itr->second==next(itr)->first){ itr=st.insert({itr->first,next(itr)->second}).first; st.erase(prev(itr)); st.erase(next(itr)); } } it=st.upper_bound({B[i],N+2}); if(it==st.begin() || prev(it)->second<=B[i]){ auto itr=st.insert({B[i],B[i]+1}).first; if(itr!=st.begin() && prev(itr)->second==itr->first){ itr=st.insert({prev(itr)->first,itr->second}).first; st.erase(prev(itr)); st.erase(next(itr)); } if(next(itr)!=st.end() && itr->second==next(itr)->first){ itr=st.insert({itr->first,next(itr)->second}).first; st.erase(prev(itr)); st.erase(next(itr)); } } if(st.size()){ if(st.begin()->first==0) st.erase(next(st.insert({-1,st.begin()->second}).first)); if(prev(st.end())->second==N) st.erase(prev(st.insert({prev(st.end())->first,N+1}).first)); for(auto[a,b]:st){ if(a+1<b-1)dp[i].insert({a+1,b-1}); } if(st.begin()->first==-1) st.erase(prev(st.insert({0,st.begin()->second}).first)); if(prev(st.end())->second==N+1) st.erase(next(st.insert({prev(st.end())->first,N}).first)); } } if(dp[0].count({0,N})) puts("NO"); else{ puts("YES"); int now=0; for(int i=0;i<N;i++){ auto it=dp[0].upper_bound({i,N+2}); if(it==dp[0].begin() || prev(it)->second<=i){ now=i; break; } } cout<<now+1<<endl; for(int i=0;i<Q;i++){ set<pair<int,int>> st=dp[i+1]; set<pair<int,int>>::iterator it; if((it=st.upper_bound({now,N+2}))==st.begin() || prev(it)->second<=now){ now=now; }else if(now!=0 && (it=st.upper_bound({now-1,N+2}))==st.begin() || prev(it)->second<=now-1){ now=now-1; }else if((it=st.upper_bound({now+1,N+2}))==st.begin() || prev(it)->second<=now+1){ now=now+1; } cout<<now+1<<endl; } } }