Delete Prime (模拟操作)

题目链接:Delete Prime

模拟操作,利用ans数组记录最大N的弹出操作,利用upper_bound()函数查找相对应的n的答案

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
vector<int> ans[110];
bool vis[N];
int a[N], k = 0;
inline void get_prime() {
    for (int i = 2; i < N; i++) {
        if (vis[i] == 0) {
            for (int j = i + i; j < N; j += i) vis[j] = 1;
        }
    }
}
inline void get_array() {
    int r = 1e6, l, i;
    for (i = 1; i < N; i++) a[i] = i;
    while (r) {
        l = 1;
        for (i = 1; i <= r; i++) {
            if (vis[i] == 0) {
                ans[k].push_back(a[i]);
            } else
                a[l++] = a[i];
        }
        ans[k++].push_back(1e7);
        r = l - 1;
    }
}
int solve1(int n, int m) {
    int ans1 = 0;
    for (int i = 0; i < k; i++) {
        int x = upper_bound(ans[i].begin(), ans[i].end(), n) - ans[i].begin();
        int y =
            upper_bound(ans[i].begin(), ans[i].end(), m) - ans[i].begin() - 1;
        if (ans[i][y] == m) {
            ans1 += y + 1;
            break;
        } else
            ans1 += x;
    }
    return ans1;
}
int solve2(int n, int m) {
    int ans2;
    for (int i = 0; i < k; i++) {
        int x = upper_bound(ans[i].begin(), ans[i].end(), n) - ans[i].begin();
        if (m > x)
            m -= x;
        else {
            ans2 = ans[i][m - 1];
            break;
        }
    }
    return ans2;
}
int main() {
    ios::sync_with_stdio(false);
    get_prime();
    get_array();
    int T, n, m, i, j, k;
    cin >> T;
    while (T--) {
        cin >> k >> n >> m;
        if (k == 1)
            cout << solve1(n, m) << '\n';
        else
            cout << solve2(n, m) << '\n';
    }
    return 0;
}

题目:
There are n balls lie in a line. Each ball has a number on it, and the number on the i-th ball is i (1 ≤ i ≤ n).
For each round, we choose the balls whose index is 1 or a prime number and kick them out from the
line in order. After that, we start the next round with the remaining balls while their relative position
does not change and their indices are relabeled in order from 1 (but the numbers on the balls will not
change). We repeat this process until no ball is left. There is a kick-out sequence D, which is empty in
the very beginning. Every time a ball is kicked out, the number on it will be appended to D.
For example, when n = 6 we have balls [1, 2, 3, 4, 5, 6] lie in a line. In the first round, we kick out balls
[1, 2, 3, 5] (index 1, 2, 3, 5) in order and the remaining balls are [4, 6]. In the second round, we kick out
[4, 6] (index 1, 2) in order and there is no ball left. So the kicked-out sequence is D = [1, 2, 3, 5, 4, 6]. Note
that the index of D always starts from 1.
Now you need to answer two types of questions:
• Type 1 Given n and k, what is the index of k in the kick-out sequence? In other words, find x such
that D[x] = k.
• Type 2 Given n and k, what is the k-th number in the kick-out sequence? In other words, find x
such that D[k] = x.
Input
The first line of the input contains an integer T (1 ≤ T ≤ 2 · 105
), denoting the number of queries.
For the following T lines, each line contains three integers t, n, k (t ∈ {1, 2}, 1 ≤ k ≤ n ≤ 106
), representing
a query of type t mentioned above.
Output
For each query, output the answer in a single line.
Example
standard input
10
1 5 1
1 5 2
1 5 3
1 5 4
1 5 5
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
standard output
1
2
3
5
4
1
2
3
5
4

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值