UVA 10881 Piotr's Ants

本文提供了一道VJudge竞赛题目的详细解答代码,该题目涉及结构体定义与排序、数组应用等C++知识点。通过本解析可以了解如何利用C++进行高效的数据处理与算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vjudge地址:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25979


代码:

#include "stdio.h"
#include "iostream"
#include "algorithm"
#include "string.h"
using namespace std;

int l,t,n,nl=0,nr=0,nf=0,q[10010];

struct ant
{
	int m;
	int w;
	int o;
}a[10010];

bool cmp1(struct ant a1,struct ant a2)
{
	return (a1.m)<(a2.m);
}

bool cmp2(struct ant a1,struct ant a2)
{
	return (a1.o)<(a2.o);
}

int main()
{
	int T;
	scanf("%d",&T);
	for(int j=1;j<=T;j++)
	{
		a[0].m=-1;
		scanf("%d%d%d",&l,&t,&n);
		char s;
		int x;
		for(int i=1;i<=n;i++)
		{
			scanf("%d %c",&x,&s);
			if(s=='L')
			{
				a[i].m=x;
				a[i].w=-1;
				a[i].o=i;
			}
			else
			{
				a[i].m=x;
				a[i].w=1;
				a[i].o=i;
			}
		}
		sort(a+1,a+n+1,cmp1);
		for(int i=1;i<=n;i++)
		{
			q[i]=a[i].o;
			if(a[i].w>0)
				a[i].m+=t;
			else 
				a[i].m-=t;
		}
		sort(a+1,a+n+1,cmp1);
		for(int i=1;i<=n;i++)
		{
			a[i].o=q[i];
			if(a[i].m==a[i-1].m)
				a[i-1].w=a[i].w=0;
		}
		sort(a+1,a+n+1,cmp2);
		printf("Case #%d:\n",j);
		for(int i=1;i<=n;i++)
		{
			if(a[i].m<0||a[i].m>l)
				printf("Fell off\n");
			else
			{
				printf("%d ",a[i].m);
				if(a[i].w==-1)
					printf("L\n");
				else if(a[i].w)
					printf("R\n");
				else
					printf("Turning\n");
			}
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值