Submission #1047313

#TimeUsernameProblemLanguageResultExecution timeMemory
1047313gagik_2007Sprinklers (CEOI24_sprinklers)C++17
3 / 100
183 ms2372 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ff first #define ss second ll ttt; const ll INF=1e18; const ll MOD=1e9+7; const ll N=100007; ll n,m,k; int f[N],s[N]; int dp[N]; int par[N]; bool pardir[N]; int getFlowerAmount(int l, int r){ auto it1=lower_bound(f+1,f+m+1,l); auto it2=upper_bound(f+1,f+m+1,r); int cnt=(it2-it1); //cout<<"GetFlowerAmount: "<<l<<" "<<r<<" - "<<cnt<<endl; return cnt; } bool check(int vl){ //cout<<vl<<endl; for(int i=0;i<=n;i++){ dp[i]=0; par[i]=0; } for(int i=0;i<n;i++){ int ind=dp[i]+1; if(ind==m+1){ dp[i+1]=dp[i]; par[i+1]=i; pardir[i+1]=true; continue; } if(f[ind]>=s[i+1]&&f[ind]<=s[i+1]+vl){ //cout<<1<<endl; int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]+vl); if(pos>dp[i+1]){ par[i+1]=i; pardir[i+1]=true; } dp[i+1]=max(dp[i+1],pos); //cout<<i+1<<" -> "<<dp[i+1]<<endl; } else if(f[ind]>=s[i+1]-vl&&f[ind]<=s[i+1]){ //cout<<2<<endl; int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]); if(pos>dp[i+1]){ par[i+1]=i; pardir[i+1]=false; } dp[i+1]=max(dp[i+1],pos); //cout<<i+1<<" -> "<<dp[i+1]<<endl; } if(i<n-1&&s[i+2]-vl<=s[i+1]&&s[i+1]<=s[i+2]&&s[i+2]<=s[i+1]+vl&&f[ind]>=s[i+2]-vl){ //cout<<3<<endl; int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]+vl); if(pos>dp[i+2]){ par[i+2]=i; } dp[i+2]=max(dp[i+2],pos); //cout<<i+2<<" -> "<<dp[i+2]<<endl; } //cout<<i<<": "<<dp[i]<<endl; } //cout<<n<<": "<<dp[n]<<endl; //cout<<endl; if(dp[n]==m){ return true; } return false; } string getAnswer(){ string ans=""; for(int i=0;i<n;i++){ ans+='L'; } int cur=n; while(cur>0){ if(dp[cur]==0){ break; } if(cur-par[cur]==2){ ans[cur-1]='R'; } else if(pardir[cur]){ ans[cur-1]='R'; } cur=par[cur]; } return ans; } int binsearch(){ int l=0,r=MOD; while(l<r){ int mid=(l+r)/2; if(check(mid)){ r=mid; } else{ l=mid+1; } } return l; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("Finput.txt","r",stdin); // freopen("Foutput.txt","w",stdout); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>s[i]; } for(int i=1;i<=m;i++){ cin>>f[i]; } int res=binsearch(); if(res==MOD){ cout<<-1<<endl; return 0; } check(res); string ans=getAnswer(); cout<<res<<endl<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...