跳到正文

题解

题解:P13674 [GCPC 2023] Investigating Frog Behaviour on Lily Pad Patterns

#SUSPENDED#约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年8月8日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
题目
P13674

版本

  1. v2当前
  2. v1

思路

使用 set 进行存储。

先初始化青蛙位置的同时记录被占用的荷叶,接着查找空荷叶位置,当前荷叶没被占用就暂时存进空荷叶的集合。

每一次询问,找到青蛙当前位置,查找最近空荷叶(也就是找第一个大于当前青蛙坐标的空荷叶),新坐标被占用,旧坐标就加入空荷叶集合。

记得每一次扩展池塘荷叶,防止后面的青蛙无路可走。

代码

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MAXX = 1e6 + 5;
int frog_pos[MAXN];
set<int> occupied;
set<int> empty_pads;
int n, q;
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> frog_pos[i];
        occupied.insert(frog_pos[i]);
    }
    int max_pos = frog_pos[n];
    for (int i = 1; i <= max_pos + 1; ++i) {
        if (occupied.find(i) == occupied.end()) {
            empty_pads.insert(i);
        }
    }
    cin >> q;
    while (q--) {
        int i;
        cin >> i;
        int current = frog_pos[i];
        auto it = empty_pads.upper_bound(current);
        int next_pos = *it;
        occupied.erase(current);
        occupied.insert(next_pos);
        empty_pads.erase(next_pos);
        empty_pads.insert(current);
        frog_pos[i] = next_pos;
        if (empty_pads.upper_bound(next_pos) == empty_pads.end()) {
            empty_pads.insert(next_pos + 1);
        }
        cout << next_pos << '\n';
    }
    return 0;
}

时间复杂度为 O((n+q)log⁡n)O((n+q)\log n)。可以通过此题。

原文