湖南大学第十四届ACM程序设计大赛 A-AFei Loves Magic

链接:https://ac.nowcoder.com/acm/contest/338/A
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

AFei is a trainee magician who likes to study various magical materials. Today, he came to the forest to find rare materials. He was so lucky that he found a piece of high-level magic stone. He knew that this stone always appeared in the pile, so there must be more nearby. Then he went deeper.

As expected, he found many magic stones. These stones were arranged in a row. Just as he was ready to pick up one, a magical circle was triggered. He was petrified and the stones began to move. As mentioned above, the stones were lined up. Now, some stones moved to one end of the line and the other stones moved to the other end. Stones would not change direction of movement unless they collided with other stones. Collision meant that two stones moved to the same position, and then the directions of the two stones would both change. Whether or not a collision occured, the speed was always 1 m/s. Stone would disappear when it reached one of the ends.
AFei knew that the magical circle would disappear after t seconds. It meant that after t seconds, he could move and the stones would return to immobility. This also meant that AFei would get those stones. He wondered how many magic stones he could get in the end, including the first one he got when he came to the forest.

 

输入描述:

The first line contains three integers n, L, t (0≤n≤1000000, 2≤L≤1000000000, 0<=t<=1000000) − the number of stones on the line is n,the length of the line is L meter, and the magic circle will disappear after t seconds.
The following n lines contain description of magic stones on the line. Each i-th of these lines contains two space-separated integers x[i] and d[i] (0<x[i]<L, d[i]∈{1,2} for i<=n), which stand for initial position and direction of motion(1 means from 0 to L, 2 means from L to 0.).

输出描述:

Output a number indicating the amount of the magic stones that AFei will eventually be able to obtain.

示例1

输入

复制

0 10000 3

输出

复制

1

说明

There is no magic stone on the line, but AFei has got one  when he came to the forest.

示例2

输入

复制

4 10 6
1 1
5 2
6 1
9 2

输出

复制

3

说明

The stones are A(1,1), B(5,2), C(6,1), D(9,2).

After 1s, they become A(2,1), B(4,2), C(7,1), D(8,2);

After 2s, they become A(3,2), B(3,1), C(7,2), D(8,1);

After 3s, they become A(2,2), B(4,1), C(6,2), D(9,1);

After 4s, they become A(1,2), B(5,2), C(5,1), D reach L and disappears;

After 5s, they become A reach 0 and disappears, B(4, 2), C(6,1), D disappeared;

After 6s, they become A disappeared, B(3, 2), C(7, 1), D disappeared.

AFei finially gets the first one, B and C.

备注:

1,Input guarantees that there will not be two magic stones in one location.

2,If stone A and stone B are located at 4 and 5, respectively, and A's direction is 1, B's direction is 2. Then next second, the position of the two stones have not changed, but they have gone in the opposite direction.

题目大意:

AFei是一名见习魔术师,他喜欢学习各种魔法材料。今天,他来到森林寻找稀有材料。他很幸运,找到了一块高级魔石。他知道这石头总是出现在那堆石头里,所以附近一定有更多的石头。然后他走得更深了。
正如所料,他发现了许多魔法石。这些石头排列成一排。就在他准备接电话的时候,一个神奇的圆圈被触发了。他被石化了,石头开始移动。如上所述,这些石头排列整齐。现在,一些石头移到了线的一端,另一些石头移到了线的另一端。除非石头与其他石头相撞,否则它们不会改变运动方向。碰撞意味着两块石头移动到相同的位置,然后两块石头的方向都会改变。无论是否发生碰撞,速度始终为1 m/s。当石头到达其中一端时,它将消失。
阿菲知道魔法圈会在T秒后消失。这意味着T秒后,他可以移动,石头将恢复静止。这也意味着阿菲会得到那些石头。他想知道到底能得到多少块魔法石,包括他来到森林时得到的第一块。

第一行包含三个整数n,l,t(0≤n≤1000000,2≤l≤100000000,0<=t<=1000000)-行上的石头数为n,行的长度为l米,T秒后魔法圆消失。
下面的n行包含对线上魔法石的描述。这些行中的每一个i-th都包含两个空格分隔的整数x[i]和d[i](0<x[i]<l,d[i]1,2表示i<=n),表示初始位置和运动方向(1表示从0到l,2表示从l到0)。

输出一个数字,指示AFEI最终能够获得的魔法石数量。

分析:本题的难点主要在于碰撞后的情况,因为碰撞后两块石头的方向都改变了,但有一点是不变的,即所有的石头都是相同的,我们可以把碰撞看作是交换,或者可以说是两块石头“擦肩而过”,因为两块相同的石头碰撞后改变方向与两块相同的石头迎面路过没什么区别。可以等效看待。这样问题就简化了。每次输入石头的位置和运动方向后只需考虑这一个石头在规定时间内是否会运动到起点或终点,如果成立那么这块石头就作废了,作废数量+1。最后用总数量减去作废数量再+1借有效石头数量

#include<iostream>
using namespace std;
long long a[1000005],b[1000005];//注意一定要把数组开为全局变量开到1e6,否则判题机会发生运行错误
int main()
{
	long long n,l,t,sum=0;
	cin>>n>>l>>t;//输入石头数,行的长度,规定时间
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];//输入单个石头的位置以及运动方向
		if(b[i]==1&&a[i]+t>=l)//判断单个石头是否在规定时间内会运动到终点,如果成立,则该石头作废
		sum++;
		else if(b[i]==2&&a[i]-t<=0)
		sum++;          //或者判断单个石头是否在规定时间内会运动到起点,如果成立,则该石头作废
		
	}
    cout<<n-sum+1<<endl;//输出总数减去作废数+1即为有效石头数
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值