結果
問題 | No.1405 ジグザグロボット |
ユーザー | register |
提出日時 | 2024-06-07 22:36:17 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 10,623 bytes |
コンパイル時間 | 2,197 ms |
コンパイル使用メモリ | 198,456 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-07 22:36:28 |
合計ジャッジ時間 | 8,550 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 6 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 8 ms
5,376 KB |
testcase_12 | AC | 8 ms
5,376 KB |
testcase_13 | AC | 8 ms
5,376 KB |
testcase_14 | AC | 8 ms
5,376 KB |
testcase_15 | AC | 10 ms
5,376 KB |
testcase_16 | WA | - |
testcase_17 | AC | 7 ms
5,376 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 8 ms
5,376 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 8 ms
5,376 KB |
testcase_24 | WA | - |
testcase_25 | RE | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 16 ms
5,376 KB |
testcase_32 | AC | 17 ms
5,376 KB |
testcase_33 | WA | - |
testcase_34 | AC | 3 ms
5,376 KB |
testcase_35 | AC | 21 ms
5,376 KB |
testcase_36 | AC | 3 ms
5,376 KB |
testcase_37 | AC | 2 ms
5,376 KB |
コンパイルメッセージ
main.cpp:119:49: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 119 | void upd(int l,int r,int pos,int x,sb v,auto f){ | ^~~~ main.cpp:132:50: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 132 | void add(int l,int r,int pos,int x,int v,auto f){ | ^~~~
ソースコード
#include<bits/stdc++.h> using namespace std; struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__; typedef long long lint; #define INF (1LL<<60) #define IINF (1<<30) #define endl ('\n') typedef vector<lint> vec; #define _overload4(_1,_2,_3,_4,name,...) name #define __rep(i,a) repi(i,0,a,1) #define _rep(i,a,b) repi(i,a,b,1) #define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c) #define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__) #define all(n) begin(n),end(n) template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;} template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;} template<typename T> struct maybe{ bool _is_none; T val; maybe():_is_none(true){} maybe(T val):_is_none(false),val(val){} T unwrap()const{ assert(!_is_none); return val; } T unwrap_or(T e)const{ return _is_none?e:val; } bool is_none()const{return _is_none;} bool is_some()const{return !_is_none;} }; template<typename T,typename F> auto expand(F op){ return [&op](const maybe<T>& s,const maybe<T>& t){ if(s.is_none())return t; if(t.is_none())return s; return maybe<T>(op(s.unwrap(),t.unwrap())); }; } template<typename T,typename F> class segment_tree{ maybe<T>* node; F op; int n=1; public: segment_tree(){} segment_tree(int sz,F op=F()):op(op){ while(n<=sz)n<<=1; node=new maybe<T>[n*2]; for(int i=0;i<n*2;i++)node[i]=maybe<T>(); } segment_tree(const std::vector<T>&v,F op=F()):op(op){ auto f=expand<T,F>(op); const int sz=v.size(); while(n<=sz)n<<=1; node=new maybe<T>[n*2](); for(int i=0;i<sz;i++)node[i+n]=maybe<T>(v[i]); for(int i=n-1;i>=1;i--)node[i]=f(node[i*2],node[i*2+1]); } maybe<T> get(int l,int r){ auto f=expand<T,F>(op); l+=n;r+=n; maybe<T> s,t; while(l<r){ if(l&1)s=f(s,node[l++]); if(r&1)t=f(node[--r],t); l>>=1;r>>=1; } return f(s,t); } void apply(int t,T _val){ auto f=expand<T,F>(op); t+=n; maybe<T> val=maybe<T>(_val); while(t){ node[t]=f(node[t],val); t=t>>1; } } void apply_left(int t,T _val){ auto f=expand<T,F>(op); t+=n; maybe<T> val=maybe<T>(_val); while(t){ node[t]=f(val,node[t]); t=t>>1; } } void change(int t,T val){ auto f=expand<T,F>(op); t+=n; node[t]=maybe<T>(val); while(t>1){ t=t>>1; node[t]=f(node[t*2],node[t*2+1]); } } }; using sb=array<lint,2>; #define lc pos<<1 #define rc pos<<1|1 #define mid ((l+r)>>1) class weishashi4logde{ vector<sb> tr; vector<int> tg; public: weishashi4logde(int sz){ int n=sz+1; tr.resize(n*4+1,{-INF,0}); tg.resize(n*4+1); } void cov(int x,int v) {tr[x][0]+=v;tg[x]+=v;} void pushdown(int pos){ int&t=tg[pos]; if(t) cov(lc,t),cov(rc,t),t=0; } void upd(int l,int r,int pos,int x,sb v,auto f){ if(l==r) {tr[pos]=v;return;} pushdown(pos); if(x<=mid) upd(l,mid,lc,x,v,f); else upd(mid+1,r,rc,x,v,f); tr[pos]=f(tr[lc],tr[rc]); } int getp(int l,int r,int pos){ if(l==r) return tr[pos][0]?l+1:l; pushdown(pos); if(tr[rc][0]) return getp(mid+1,r,rc); return getp(l,mid,lc); } void add(int l,int r,int pos,int x,int v,auto f){ if(x<0) return; if(r<=x) return cov(pos,v); pushdown(pos); add(l,mid,lc,x,v,f); if(x>mid) add(mid+1,r,rc,x,v,f); tr[pos]=f(tr[lc],tr[rc]); } sb ask() {return tr[1];} }; template<typename F,typename T=long long> T bs(T mn,T mx,F func){ mn--; mx++; while(mx-mn>1){ T md=(mn+mx)/2; if(!func(md))mx=md; else mn=md; } return mn; } template<typename T,typename F> std::vector<int> monotone_minima(int h,int w,T inf,F f){ std::vector<int>ret(h); auto g=[&](auto g,int a,int b,int c,int d,T inf,auto f)->void{ int e=(a+b)/2,idx=0; T mn=inf; for(int i=c;i<d;++i){ if(mn>f(e,i))mn=f(e,i),idx=i; } ret[e]=idx; if(b>a+1){ g(g,a,e,c,idx+1,inf,f); g(g,e,b,idx,d,inf,f); } }; g(g,0,h,0,w,inf,f); return ret; } int main(){ //freopen("robot.in","r",stdin); //freopen("robot.out","w",stdout); lint n,x,y; cin>>n>>x>>y; string s; cin>>s; vector<tuple<lint,lint,lint,lint,lint>>v; lint p=0,q=0,cnt=0,cnt_mx=0; vec edge; vector<lint>spos; vector<pair<lint,lint>> vv; if(count(all(s),'S')<y){ cerr<<"ok\n"; cout<<-1<<endl; return 0; } rep(i,n){ if(s[i]=='S'){ spos.push_back(i); edge.push_back(cnt); vv.push_back(make_pair(p,q)); cnt++;cnt_mx++; p=q=0; } if(s[i]=='R'){ q++; chmin(p,q); } if(s[i]=='L'){ q--; chmin(p,q); } } edge.push_back(cnt); vv.push_back(make_pair(p,q)); cnt++;cnt_mx++; auto get=[&](pair<lint,lint>x){ return x.second-x.first; }; auto merge=[&](pair<lint,lint>s,pair<lint,lint>t){ return make_pair(min(s.first,s.second+t.first),s.second+t.second); }; segment_tree<pair<lint,lint>,decltype(merge)>seg(vv,merge); lint pena=bs(-n,n,[&](lint x){ vector<lint>dp(cnt+1,-INF); vector<lint>d(cnt+1,-INF); dp[0]=d[0]=0; int now=0; auto mer=[&](sb s,sb t){ auto _s=s,_t=t; _s[1]=d[s[1]];_t[1]=d[t[1]]; return _s>_t?s:t; }; weishashi4logde shabi(cnt); shabi.upd(0,cnt,1,0,sb{0,0},mer); rep(i,n){ if(s[i]=='S'){ now++; auto t=shabi.ask(); dp[now]=t[0]-x;d[now]=d[t[1]]+1; shabi.upd(0,cnt,1,now,sb{dp[now],now},mer); } if(s[i]=='L') shabi.add(0,cnt,1,shabi.getp(0,cnt,1)-1,-1,mer); if(s[i]=='R') shabi.add(0,cnt,1,now,1,mer); } auto t=shabi.ask(); assert(now+1==cnt); dp[cnt]=t[0]-x;d[cnt]=d[t[1]]+1; return d[cnt]>y+1; })+1; vector<lint>a,b; { vector<lint>dp(cnt+1,-INF); vector<lint>d(cnt+1,-INF); vector<lint>from(cnt+1,-1); dp[0]=0; d[0]=0; auto dfs=[&](auto dfs,lint l,lint r)->void{ if(r-l==1)return; lint m=(l+r)/2; dfs(dfs,l,m); auto res=monotone_minima(r-m,m-l,make_pair(INF,INF),[&](lint p,lint q)->pair<lint,lint>{ return make_pair(-dp[q+l]-get(seg.get(q+l,p+m).unwrap()),d[q+l]); }); rep(i,r-m){ if(make_pair(dp[i+m],-d[i+m])<make_pair(dp[res[i]+l]+get(seg.get(res[i]+l,i+m).unwrap())-pena,-d[res[i]+l]-1)){ dp[i+m]=dp[res[i]+l]+get(seg.get(res[i]+l,i+m).unwrap())-pena; d[i+m]=d[res[i]+l]+1; from[i+m]=res[i]+l; } } dfs(dfs,m,r); }; dfs(dfs,0,cnt+1); lint now=cnt; while(now!=-1){ a.push_back(now); now=from[now]; } reverse(all(a)); } { vector<lint>dp(cnt+1,-INF); vector<lint>d(cnt+1,-INF); vector<lint>from(cnt+1,-1); dp[0]=0; d[0]=0; auto dfs=[&](auto dfs,lint l,lint r)->void{ if(r-l==1)return; lint m=(l+r)/2; dfs(dfs,l,m); auto res=monotone_minima(r-m,m-l,make_pair(INF,INF),[&](lint p,lint q)->pair<lint,lint>{ return make_pair(-dp[q+l]-get(seg.get(q+l,p+m).unwrap()),d[q+l]); }); rep(i,r-m){ if(make_pair(dp[i+m],-d[i+m])<make_pair(dp[res[i]+l]+get(seg.get(res[i]+l,i+m).unwrap())-pena,-d[res[i]+l]+1)){ dp[i+m]=dp[res[i]+l]+get(seg.get(res[i]+l,i+m).unwrap())-pena; d[i+m]=d[res[i]+l]-1; from[i+m]=res[i]+l; } } dfs(dfs,m,r); }; dfs(dfs,0,cnt+1); lint now=cnt; while(now!=-1){ b.push_back(now); now=from[now]; } reverse(all(b)); } vec c,d; lint ai=0,bi=0; while(ai<(int)a.size()||bi<(int)b.size()){ if((int)a.size()-ai+(int)c.size()==y+2||(int)b.size()-bi+(int)d.size()==y+2){ if(ai<(int)a.size())c.push_back(a[ai++]); if(bi<(int)b.size())d.push_back(b[bi++]); }else if(ai<(int)a.size()&&bi+1<(int)b.size()&&a[ai]==b[bi+1]){ c.push_back(a[ai++]); d.push_back(b[bi++]); d.push_back(b[bi++]); c.swap(d); }else if(ai<(int)a.size()&&bi+1<(int)b.size()&&a[ai]>b[bi+1]){ d.push_back(b[bi++]); c.swap(d); }else{ if(ai<(int)a.size())c.push_back(a[ai++]); if(bi<(int)b.size())d.push_back(b[bi++]); } } vec vvv(y+1),ll(y+1),rr(y+1); { if((int)c.size()!=y+2){ c.swap(d); } rep(i,c.size()-1){ vvv[i]=get(seg.get(c[i],c[i+1]).unwrap()); ll[i]=c[i]; rr[i]=c[i+1]; } } vector<pair<lint,lint>> ans; ans.emplace_back(-1,0); lint nx=0; for(int ny=0;ny<=y;++ny){ lint px=nx; if(nx==x){ ans.emplace_back(x-1,ny); ans.emplace_back(x+1,ny); }else if(nx+vvv[ny]>x){ lint p=0,q=0,r=0; rep(i,ll[ny],rr[ny]){ if(s[i]=='R'){ q++; chmax(r,q); if(chmin(p,q)){ r=0; } } if(s[i]=='L'){ q--; chmax(r,q); if(chmin(p,q)){ r=0; } } } nx=x; rep(i,px-1,nx){ ans.emplace_back(i,ny+1); } rep(i,0,ny+1)ans.emplace_back(x+r-q+1,i); rep(i,x+1,x+r-q+1)ans.emplace_back(i,ny+1); }else{ nx+=vvv[ny]; rep(i,px-1,nx){ ans.emplace_back(i,ny+1); } } } rep(i,n)ans.emplace_back(i,y+1); if(x!=nx){ cout<<-1<<endl; return 0; } cout<<ans.size()<<endl; for(auto&o:ans){ lint s,t;tie(s,t)=o; cout<<s<<" "<<t<<endl; } }