首页 > 编程语言 >挑战程序设计竞赛---Ants

挑战程序设计竞赛---Ants

时间:2023-04-17 19:45:21浏览次数:38  
标签:杆子 蚂蚁 long --- pole Ants ans 程序设计 ants

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

Inputcopy Outputcopy1
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
4 8
38 207

一群蚂蚁在一根长为l厘米的水平杆上行走,每根杆子的恒定速度为1厘米/秒。当一只行走的蚂蚁到达杆子的尽头时,它会立即从杆子上掉下来。当两只蚂蚁相遇时,它们会回头并开始向相反的方向行走。我们知道蚂蚁在杆子上的原始位置,不幸的是,我们不知道蚂蚁行走的方向。你的任务是计算所有蚂蚁从杆子上掉下来所需的最早和最晚时间。

输入

输入的第一行包含一个整数,给出后面的事例数。每种情况的数据都以两个整数开头:杆子的长度(以厘米为单位)和n,即居住在杆子上的蚂蚁数量。这两个数字后面跟着n个整数,给出每只蚂蚁在杆子上的位置,作为从杆子左端测量的距离,没有特定的顺序。所有输入整数都不大于 1000000,并且用空格分隔。

输出

对于每种输入情况,输出两个由单个空格分隔的数字。第一个数字是所有蚂蚁从杆子上掉下来的最早时间(如果它们的行走方向选择得当),第二个数字是最晚的时间。

样本

输入复制 输出复制
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
4 8
38 207

解答:

实际上,这题的话,一开始可以很好的看出来,最短的时间怎么求,因为蚂蚁的速度都是一样的,所以当每只蚂蚁都向着距离自己端点更近的那个方向走,那么最后所消耗的时间是最短的,但是它消耗时间最长的有点迷惑性,它说蚂蚁碰头之后就会掉头走,实际上很迷惑,但是可以理解成蚂蚁碰头之后就擦肩而过(因为不是求走的路径,而是求时间),这样想了之后是不是解答起来就简单许多了呢?

下面是答案哦(本人ac的代码)

 

#include<iostream>
using namespace std;
const int N = 10 + 1e7;
int ans[N];
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		int l, ant;
		long long ans1=0;
		long long a = 0;
		long long b = 0;
		long long ans2=0;
		cin >> l >> ant;
		for (int i = 0; i < ant; i++)
		{
			cin >> ans[i];
			ans1 = min(ans[i], l - ans[i]);
			a = max(ans1, a);
			ans2 = max(ans[i], l - ans[i]);
			b = max(ans2, b);
		}
		cout << a << " " << b << endl;
	}
	return 0;
}

congratulations!

 

标签:杆子,蚂蚁,long,---,pole,Ants,ans,程序设计,ants
From: https://www.cnblogs.com/haggard/p/17327224.html

相关文章

  • pta程序设计辅助平台-练习
    现在要开发一个系统,管理对多种汽车的收费工作。给出下面的一个基类框架classVehicle{protected:stringNO;public:Vehiclvirtualintfee()=0;//计算应收费用};以Vehicle为基类,构建出Car、Truck和Bus三个类。Car的收费公式为:载客数*8+重量*2Truck的收费公式为:重量*5Bus的收费......
  • ABC297F AtCoder Beginner Contest 297 F - Minimum Bounding Box 2
    https://atcoder.jp/contests/abc297/tasks/abc297_f在\(n\timesm\)的棋盘上放置\(k\)个棋子,记矩形A为能覆盖所有\(k\)个棋子的最小的矩形,求A的面积的期望将问题反过来考虑,枚举每种矩形有多少种放置棋子的方案,对于一个\(n\timesm\)的矩形,我们可以用容斥的方法......
  • cocos-js 播放cocos studio创建的时间轴场景动画
    ctor:1、加载场景动画json2、把场景动画的node添加到层3、找到运动的node4、关联动画和nodeload.node.runAction(load.action)播放:this._load.action.gotoFrameAndPlay(0,100,false);如果需要改变运动的node下面的子节点,可以把子节点removeFromParent然后创建一个新的Sp......
  • cocos2dx-js 帧动画的播放方法
    ctor:varload=ccs.load(res.Ani_json);varmainNode=load.node;this.addChild(mainNode);//对应帧动画的节点,使用seekWidgetByName无效,需要用getChildByNamethis._spriteAni=mainNode.getChildByName("spriteAni");this._spriteAni.setVisible(false);this._lo......
  • zynq7010,petalinux, USB-wifi测试
    zynq7010,基于linux验证USB-wifi功能1.相关电路图,这里貌似复位键默认上电开启的,引脚并没有印出来需要注意的地方注意芯片型号"USB3320",这个在linux内核中如果USB配置正确的话是会被打印出来的usbcore:registerednewinterfacedriverusb-storagechipidea-us......
  • 1.OS-Introduction|Begin
    VonNeumannmodelofcomputingManymillions(andthesedays,evenbillions)oftimeseverysecond,**theprocessorfetchesaninstructionfrommemory,decodesit(i.e.,figuresoutwhichinstructionthisis),andexecutesit**operatingsystemInfact,......
  • GDOU-CTF-2023新生赛Pwn题解与反思
    第一次参加CTF新生赛总结与反思因为昨天学校那边要进行天梯模拟赛,所以被拉过去了。16点30分结束,就跑回来宿舍开始写。第一题和第二题一下子getshell,不用30分钟,可能我没想那么多,对比网上的WP,自己和他们有点不太一样,比较暴力。大概17点10的时候,写第三题,可能自己第一次遇到随机数问......
  • 每日打卡-6
    一.问题描述一年一度“跳石头”比赛又要开始了!这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石。组委会已经选择好了两块岩石作为比赛起点和终点。在起点和终点之间,有N块岩石(不含起点和终点的岩石)。在比赛过程中,选手们将从起点出发,每一步跳向相邻的岩石,直至到达终......
  • :)深度学习模型如何统计params量-|
    :)深度学习模型如何统计params量-|1大概统计已知模型大小,如312M计算为312000000Bytes,浮点数据一个参数占4个字节,importtransformersimporttorchimportosfromtransformersimportGPT2TokenizerFast,GPT2LMHeadModel,GPT2ConfigfromtransformersimportBertT......
  • 1、创业:开篇 - 开公司创业系列文章
            开公司创业是笔者这些年来有在准备的内容。从2008年开始,笔者赚到了第一桶金,然后有规划开公司创业,但是当时的想法相对简单,通过算了账目,发现这桶金只够开公司维持3个月,所以就暂时搁置了。        对于创业,笔者这些年有在学习和准备,比如开公司的流程,注册,......