洛谷P1367 蚂蚁

题目描述 https://www.luogu.org/problemnew/show/P1367
看似模拟,却不是模拟。
两个蚂蚁相碰,实际上就是互换名字的蚂蚁互相穿过去,而且相对位置没有变化,比如 2与3相碰(2在前),碰完后,2还在3前面。、
所以只要序号的位置就行了,第一次排序后,记录第i个点以前的位置,因为相对位置不会改变,所以就需要记录一次就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
using namespace std;
const int N=1e5+5;
struct node
{
	int x,d,num;
}ant[N];
int n,t,p[N];
bool cmp(const node &a,const node &b)
{
	return a.x<b.x;
}
int main() 
{ 
   scanf("%d%d",&n,&t);
   for(int i=1;i<=n;i++)
   {
   	  scanf("%d%d",&ant[i].x,&ant[i].d);
   	  ant[i].num=i;
   }
   sort(ant+1,ant+n+1,cmp);
   for(int i=1;i<=n;i++)
   {
   	  p[ant[i].num]=i;
   	  ant[i].x+=ant[i].d*t;
   }
   sort(ant+1,ant+n+1,cmp);
   for(int i=1;i<n;i++)
     if(ant[i].x==ant[i+1].x) ant[i].d=ant[i+1].d=0;
   for(int i=1;i<=n;i++)
     printf("%d %d\n",ant[p[i]].x,ant[p[i]].d); 
 	
  return 0;	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值