Codeforces GYM 100960B: ForceField 题解

这道题有一种很简单的做法,就是用c++STL中的set来查询离当前子弹位置最近的且与子弹相对的木板的位置,这样的做法是O(nlogn)的

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<int,int>
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD=1e9+7;
const int INF=0x7ffffff;
const int magic=348;

Pair a[100048];
int n,x;

set<int> s1,s2;

int main ()
{
	int i;
	scanf("%d%d",&n,&x);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		if (!a[i].y) s1.insert(a[i].x); else s2.insert(a[i].x);
	}
	int dir=0,cur=x,ans=0;
	while (true)
	{
		if (!dir)
		{
			set<int>::iterator iter=s1.lower_bound(cur);
			if (iter==s1.begin())
			{
				ans++;
				cur=0;
			}
			else
			{
				iter--;
				cur=*iter;
				s1.erase(iter);
			}
		}
		else
		{
			set<int>::iterator iter=s2.lower_bound(cur);
			if (iter==s2.end()) break;
			cur=*iter;
			s2.erase(iter);
		}
		dir^=1;
	}
	if (s1.size() || s2.size()) printf("-1\n"); else printf("%d\n",ans);
	return 0;
}

事实上,这道题有数学做法,可以做到O(n),(当然,利用单调性进行模拟好像也可以做到O(n))

将这些墙想象成左括号和右括号,那个人相当于无限多个左括号

子弹的弹射一定是轮流消除左括号和右括号

对这个序列求以任意位置为起点,左括号最多比右括号多几个,记其为cnt

如果cnt=0,那么一定可以将所有板子打掉,答案就是所有右括号比左括号多的个数+1

如果cnt>1,那么一定不可能打掉所有板子,直接输出-1

如果cnt==1,就要分情况了

第一点,例如 ( ( ) ( ) 可以,但 ( ) ( ( )不可以,即如果删掉最左边的括号,右边的所有括号不能匹配,那么不可能打掉所有的板子,输出-1

第二点,只有在这种情况,子弹的发射位置会对答案造成影响,设子弹发射位置为*

如果是*( ( ) ( ),是不可以的,但是 ( * ( ) ( ) 是可以的,即如果子弹发射位置的左边没有左括号,那么不可能打掉所有板子,输出-1

最后的话就一定能打掉了,而且答案是0

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<int,int>
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD=1e9+7;
const int INF=0x7ffffff;
const int magic=348;

Pair a[100048];
int n,x;

int main ()
{
	int i;
	scanf("%d%d",&n,&x);
	for (i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1);
	int cnt=0;
	for (i=1;i<=n;i++)
		if (!a[i].y)
			cnt++;
		else
		{
			cnt--;
			if (cnt<0) cnt=0;
		}
	if (cnt>1)
	{
		printf("-1\n");
		return 0;
	}
	if (cnt==0)
	{
		int cnt_left=0,cnt_right=0;
		for (i=1;i<=n;i++) if (a[i].y) cnt_right++; else cnt_left++;
		printf("%d\n",cnt_right-cnt_left+1);
		return 0;
	}
	bool f=false;
	for (i=1;i<=n && a[i].x<x;i++)
		if (!a[i].y)
		{
			f=true;
			break;
		}
	if (!f)
	{
		printf("-1\n");
		return 0;
	}
	cnt=0;
	for (i=2;i<=n;i++)
		if (!a[i].y)
			cnt++;
		else
		{
			cnt--;
			if (cnt<0) cnt=0;
		}
	if (cnt>0)
	{
		printf("-1\n");
		return 0;
	}
	printf("0\n");
	return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值