题目描述 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;
}