蚂蚁爬杆问题总结

181 篇文章 0 订阅
112 篇文章 0 订阅

问题1:求所有蚂蚁落下杆子所需要的最短时间和最长时间。

POJ1852 Problem Link:http://poj.org/problem?id=1852

Ants

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 35180 Accepted: 12983

Description

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

Input

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.

Output

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time. 

Sample Input

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output

4 8
38 207

Source

Waterloo local 2004.09.19

 

AC code:

#include<iostream>
#include<stdio.h>
using namespace std;

int main()
{
	int T,len,x,n,ans1,ans2;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&len,&n);
		ans1=0;
		ans2=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			ans1=max(ans1,min(x,len-x));
			ans2=max(ans2,max(x,len-x));
		}
		printf("%d %d\n",ans1,ans2);
	}
	return 0;
 } 

 

问题2:蚂蚁感冒传染问题。

蓝桥杯历届试题 
蚂蚁感冒
问题描述
  长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
 
  每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
 
  当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
 
  这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
 
  请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
输入格式
  第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
 
  接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
输出格式
  要求输出1个整数,表示最后感冒蚂蚁的数目。
样例输入
3
5 -2 8
样例输出
1
样例输入
5
-10 8 -20 12 25
样例输出
3
 

AC code:

#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn=55;
int a[maxn];

int main()
{
	int n,left,right;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);	
	}	
	left=right=0;
	for(int i=2;i<=n;i++)
	{
		if(a[i]>0 && a[i]<abs(a[1]))
		{
			left++;
		}
		else if(a[i]<0 && abs(a[i])>abs(a[1]))
		{
			right++;
		}
	}
	if((left==0 && a[1]<0) || (right==0 && a[1]>0))
	{
		printf("1\n");
	}
	else
	{
		printf("%d\n",left+right+1);
	}
	return 0;
} 

 

问题3:计算T秒后蚂蚁的位置

有一个长为L(L<=10000)的木杆,上面有N(N<=1000)个蚂蚁,他们一开始的朝向随机,他们会以一定的速度一直向当前方向走,直到两只蚂蚁相遇,他们会立即掉头(即往相反方向走),计算T秒后蚂蚁的位置

      输入的第一行为数组个数,

     每组数组第一行为三个正整数,L,T,n,分别表示长度,时间,蚂蚁个数。

     L 表示向左,R表示向右

   输出格式为n行,输出每个蚂蚁的位置和朝向,如果T秒内已经掉下,输出fell off。

   样例输入:

   1

  10   1   4

   1  R

   5 R

   3 L

  10 R

 输出:

   Case:#1

  2  turing

  6 R 

  2 turing

  fell off

 

AC code:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int max=10000;
typedef struct a
{
	int id;
	int pos;
    int  status;
}ants;
ants before[max];
ants after[max];
int record[max];
const char information[][10]={{"L"},{"turning"},{"R"}};
//1 代表右转  -1代表左转  0代表正在转身
 
bool cmp(ants & a,ants & b)
{
	return a.pos<b.pos;
}
bool cmp_id(ants & a,ants & b)
{
	return a.id<b.id;
}
int main()
{
	int time;
	int L,T,n;
	int i,j;
	int len;
	char c_status;
	cin>>time;
	while(time--)
	{ 
		i=0;
		j=1;
		cin>>L>>T>>n;
		len=n;
		while(len--)
		{
            before[i].id=i;
			after[i].id=0;
			cin>>before[i].pos;
			cin>>c_status;
			if(c_status=='L')
			{
				before[i].status=-1;
				after[i].status=-1;
			}
			else if(c_status=='R')
			{
				before[i].status=1;
				after[i].status=1;
			}
			after[i].pos=before[i].pos+before[i].status*T;
			i++;
		}
       	sort(before,before+n,cmp);
		sort(after,after+n,cmp);
		for(i=0;i<n;i++)
		{
			after[i].id=before[i].id;
		}
		sort(after,after+n,cmp_id);
		for(i=0;i<n-1;i++)
		{
			if(after[i].pos==after[i+1].pos)
			{
				after[i].status=after[i+1].status=0;
			}
		}
		cout<<"Case #"<<j<<endl;
		for(i=0;i<n;i++)
		{
			if(after[i].pos<0||after[i].pos>10)
				cout<<"Fell off"<<endl;
			cout<<after[i].pos<<" "<<information[after[i].status+1]<<endl;
			
		}
		j++;
	}
	
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值