C2. Increasing Subsequence (hard version) (思维+贪心+前后缀预处理+细节模拟)

104 篇文章 0 订阅
52 篇文章 1 订阅

https://codeforces.com/problemset/problem/1157/C2


思路:

比较明显的是优先拿两边中能拿的小的那一个。什么叫能拿的?就是不能小于等于上一个拿出去的数。

一个特殊的情况就是两者相等。这个情况下其实只会跑一边跑到不能再拿为止。可以考虑此时两边分别跑到底,最后取个最大的。开始没这么想。开始想的就是预处理前后缀处理每个点能到的最远长度。然后O1判断。

suf:前往后跑递减

pre:后往前跑递减

模拟的一个细节就是所有都不符合的情况要else {break;}

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],pre[maxn],suf[maxn],tot=0;
char ans[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i];
  a[0]=0x3f3f3f3f;a[n+1]=-0x3f3f3f3f;
  for(LL i=1;i<=n;i++){
    if(a[i-1]>a[i]) suf[i]=suf[i-1]+1;
    else suf[i]=1;
  }
  for(LL i=n;i>=1;i--){
    if(a[i]<a[i+1]) pre[i]=pre[i+1]+1;
    else pre[i]=1;
  }
  LL l=1,r=n;
  LL last=-1;
  while(l<=r){
      if(l==r){
        if(a[r]>last) ans[++tot]='R';
        break;
      }
      else if(last>a[l]&&last>a[r]){
        break;
      }
      else if(last<a[l]&&last>=a[r]){
        ans[++tot]='L';
        last=a[l];
        l++;
      }
      else if(last>=a[l]&&last<a[r]){
        ans[++tot]='R';
        last=a[r];
        r--;
      }
      else if(last<a[l]&&a[l]<a[r]){
        ans[++tot]='L';
        last=a[l];
        l++;
      }
      else if(last<a[r]&&a[l]>a[r]){
        ans[++tot]='R';
        last=a[r];
        r--;
      }
      else if(a[l]==a[r]){
         if(pre[l]>suf[r]){
            while(a[l]<a[l+1]&&l<r){
                ans[++tot]='L';
                l++;
            }
            ans[++tot]='L';
            break;
         }
         else if(pre[l]<=suf[r]){
            while(a[r-1]>a[r]&&r>l){
                ans[++tot]='R';
                r--;
            }
            ans[++tot]='R';
            break;
         }
      }
      else break;
  }
  cout<<tot<<"\n";
  for(LL i=1;i<=tot;i++){
    cout<<ans[i];
  }
  cout<<"\n";
return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值