炮弹

题目链接:https://www.acwing.com/problem/content/description/5528/

题意:

初始位置s,能量为1,方向为右,遇到跳板方向改变,能量增加,遇到炮击目标,如果能量足够大,cnt++,求最后的cnt

思路:

模拟,注意当重复经过跳板时,如果大小方向都和上次经过该跳板时一样,说明进入死循环,跳出即可

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define int long long
#define endl "\n"
#define fi first
#define se second
//#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
const int mod=1e9+7;
int n,s;
pii a[maxn];
int power=1;
int cnt;
int dir=1;
bool vis[maxn];
bool ok(int a,int b,int index){
	if(a>=b&&!vis[index])return true;
	return false;
}
vector<set<int>>ap(maxn);
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	cin>>n>>s;
	
	rep(i,1,n){
		int opt,v;cin>>opt>>v;
		
		a[i]={opt,v};
	}
	while(s>0&&s<=n){
		if(a[s].fi==0){
			power+=a[s].se;dir=-dir;
			if(ap[s].count(power*dir))break;
			ap[s].insert(power*dir);
		}else{
			if(ok(power,a[s].se,s)){
				vis[s]=true;
				cnt++;
			}
		}
		s+=(power*dir);
	}
	
	cout<<cnt;
	return 0;
}


posted @ 2025-03-01 10:27  Marinaco  阅读(34)  评论(0)    收藏  举报
//雪花飘落效果