思路
直接暴力地模拟,出界就终止循环即可。
然而,还有一种情况没考虑到:无限的跳,死循环。
这种情况也是十分好做的。这里介绍一种有趣的做法:卡时间。
计算循环所用的时间,当循环时间大于 秒,直接跳出循环输出答案。于是我们就可以轻松地切掉这道题了。
Code
#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;
}