ACWING 2816. 判断子序列 (入门) (双指针算法)

给定一个长度为 n 的整数序列 a1,a2,…,an 以及一个长度为 m 的整数序列 b1,b2,…,bm。

请你判断 a 序列是否为 b 序列的子序列。

子序列指序列的一部分项按原有次序排列而得的序列,例如序列 {a1,a3,a5} 是序列 {a1,a2,a3,a4,a5} 的一个子序列。

输入格式
第一行包含两个整数 n,m。

第二行包含 n 个整数,表示 a1,a2,…,an。

第三行包含 m 个整数,表示 b1,b2,…,bm。

输出格式
如果 a 序列是 b 序列的子序列,输出一行 Yes。

否则,输出 No。

数据范围
1≤n≤m≤105,
−109≤ai,bi≤109
输入样例:
3 5
1 3 5
1 2 3 4 5
输出样例:
Yes

https://www.acwing.com/problem/content/2818/

#include<bits/stdc++.h>
using namespace std;

int main() {
    
    int n,m;
    cin>>n>>m;
    int a[n+1],b[m+1];
    for(int i=0;i<n;i++) cin>>a[i];
    for(int j=0;j<m;j++) cin>>b[j];
    
    int i=0, j=0;
    while(i<n&&j<m) {
        if(a[i]==b[j]) i++;
        j++;
    }
    if(i==n) cout<<"Yes\n";
    else cout<<"No\n";    
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

struct A {
    int l;
    int r;
};

struct f {
    bool operator() (const A& a, const A& b) {
        if(a.l<b.l) return true;
        else return false;
    }
};

int main() {

    int n;
    cin>>n;

    A a[n+2];
    for(int i=0; i<n; i++) cin>>a[i].l>>a[i].r;

    sort(a,a+n,f());

//    cout<<"a:\n";
//    for(int i=0; i<n; i++) {
//        printf("%d %d\n",a[i].l,a[i].r);
//    }

//    int index=0;
//    int ans=1;

//    int st=a[index].l;
//    int ed=a[index].r;
//
//    while(index<n-1) {
//
        printf("\n\na[index+1] = a[%d] l = %d, r = %d\n\n",index+1,a[index+1].l,a[index+1].r);
//
//        if(ed>=a[index+1].l) {
//            if(ed<a[index+1].r)
//                ed=a[index+1].r;
            printf("\n\ntrue:\ned = %d\n",ed);
//        } else {
//            ans++;
//            st=a[index+1].l;
//            ed=a[index+1].r;
            printf("\n\nflase:\nans = %d\nst = %d\ned = %d\n",ans,st,ed);
//        }
//        index++;
//    }
//    cout<<ans<<endl;
    int cnt=1;
    int nowl=a[0].l,nowr=a[0].r;
    for(int i=1;i<n;i++) {
        if(a[i].l<=nowr){
            nowr=max(nowr,a[i].r);
        }else{
            nowr=a[i].r;
            cnt++;
        }
        nowl=a[i].l;
    }
    cout<<cnt<<endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值