题解 | 小猫排队

小猫排队

https://www.nowcoder.com/practice/a88a85759a9b4c9c8dfd687c366ad5bf

#include <bits/stdc++.h>
using namespace std;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)cin >> a[i];
    int cute;
    cin >> cute;
    int ans = 0;
    int l = 1, r = n + 1;
    for (int i = n; i >= l; i--) if (a[i] > cute)l++, r = i, ans++;
    ans += r - l + 1;
    cout << ans;
}

双指针模拟

已知交换一定不劣,于是能交换就交换,用l记录队伍头部的位置,用r记录小猫的位置。所有能交换的都交换完之后剩下的步数就是r-l+1

全部评论

相关推荐

2025-11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务