跳到正文

题解

P10132 [USACO24JAN] Cannonball B

_colin1112_约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
获赞
1
题目
P10132

版本

  1. v1当前

思路

直接暴力地模拟,出界就终止循环即可。

然而,还有一种情况没考虑到:无限的跳,死循环。

这种情况也是十分好做的。这里介绍一种有趣的做法:卡时间。

计算循环所用的时间,当循环时间大于 0.70.7 秒,直接跳出循环输出答案。于是我们就可以轻松地切掉这道题了。

Code

C++
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'

using namespace std;
const int E = 1e6 + 5;
ll n, s, v, x = 1;
bool flag = 1;
bitset<E> flag1;
int main()
{
    cin >> n >> s;
    vector<pair<ll, ll>> a(n);
    for (auto &v : a)
        cin >> v.first >> v.second;
    ll ans = 0;
    double start = clock();
    while (1)
    {
        // cout << s << " " << x << endl;
        if (a[s - 1].first == 0)
        {
            x += a[s - 1].second;
            if (flag == 1)
                flag = 0;
            else
                flag = 1;
            if (flag == 0)
                s -= x;
            else
                s += x;
        }
        else
        {
            // cout << s << " " << x << endl;
            if (x >= a[s - 1].second && !flag1[s - 1])
                ans++, flag1[s - 1] = true;
            if (flag == 0)
                s -= x;
            else
                s += x;
        }

        double end = clock();
        double tim = (end - start) / CLOCKS_PER_SEC;//计算时间
        if (s < 1 || s > n || tim >= 0.7)//出界或时间来到0.7秒
            break;
    }
    cout << ans;
    return 0;
}
原文