当前位置:网站首页>Falling ant
Falling ant
2022-06-09 20:13:00 【FW Liu】
Falling ants
Title Description :
The length of one is 1 There are some ants crawling on Mi's stick . They go at a speed of one centimeter per second or are stationary , There are only two directions , Left or right . If two ants meet , Then they immediately exchange speed and continue to crawl . Three ants meet , Then the ants on both sides exchange speed , The ant in the middle is still static . If they get to the edge of the stick (0 or 100 Cm ) And fall off the stick . At a certain moment, the position of the ant is different, and it is in an integral centimeter ( namely 1,2,3,…99 centimeter ), There is only one ant A Speed is 0, Other ants are crawling to the left or right . Give the position and initial velocity of all ants on the stick at that time , Find the ants A From this moment to the time it takes to fall .
Input description :
The first line contains an integer representing the number of ants N(2<=N<=99), After that, we have N That's ok , Each line describes the initial state of an ant . Each initial state consists of two integers , Space separates the middle , The first number is the number of centimeters in the initial position P(1<=P<=99), The second number represents the initial direction ,-1 Indicating left ,1 Indicating right ,0 Stand still .
Output description :
Ant A The time from the start to the fall . If it doesn't fall , Output “Cannot fall!”
Input :
4
10 1
90 0
95 -1
98 -1
Output :
98
Answer key : The ultimate goal of this question is to understand , An ant can only move within the range delineated by his left and right ants , That is to say, no matter how you exercise , The relative order of these ants on the stick is unchanged .
First of all, the meaning of exchange speed is not just the size of exchange speed , At the same time, the direction is also changed . That is to say, the ants will not cross each other after they meet , It's moving in the opposite direction . It can be understood as : No one can get past each other , All ants can only be found in the area composed of two ants “ prisoner 's cage ” Internal movement .
Find a speed of 0 The ants of
On its left left An ant
On the right right An ant
If it is judged that the ant finally falls to the left , Then it must be the left+1 One fell
If you fall to the right , Then it must be the second right+1 One fell
How to judge which side to fall from ?
Suppose the ants that go left have toleft individual
The ant that walks to the right toright individual
First of all, understand Last, last There will be toleft An ant fell from the left ,toright Ants fall from the right .
If the number of all ants walking to the left toleft Greater than left So the one on the left of the static ant left All of them fell down , At this time, there are ants to fall from the left , Then it's the static ant's turn
If the number of all ants walking to the right toright Greater than right So the one on the right of the static ant right All of them fell down , At this time, there are ants to fall from the right , Then it's the static ant's turn .
If an ant walks to the left toleft be equal to left Then it is impossible for a static ant to jump down
If an ant walks to the right toright be equal to right Then it is impossible for a static ant to jump down
Now it is clear that if we judge which side to jump from , So how to calculate the time
If you jump from the left , So it can be considered as one of the ants walking to the left , Subscript to be left It's the time for the ants to go from the starting position to the leftmost position
If you jump from the right , So it can be considered as one of the ants walking to the right ,(right=n-1-left) Subscript to be n-left-1 It's the time when the ants from the starting position go to the far right .
The following is the reference code :
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Ant
{
int position;
int direct; // Direction
};
bool cmp(Ant a,Ant b)
{
return a.position<b.position;
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
vector<Ant> ant(n);
for(int i = 0; i<n; i++)
scanf("%d %d",&ant[i].position,&ant[i].direct);
sort(ant.begin(),ant.end(),cmp);
// The next thing to do is to find the position of the stationary one , To do this, we have to sort
// In this way, a few ants to the left of the static ants came out
int target,toLeft = 0; // Here we choose the left as the benchmark
for(int i = 0; i<n; i++) // Traverse all the ants
{
if(ant[i].direct == 0)
target = i;
if(ant[i].direct == -1)
toLeft++;
}// current target That's the number on the left side of the static ant , That is, the subscript of the static ant
bool flag = false;
int ans;
if(toLeft == target)
flag = true;
else if (toLeft > target)// So what we're looking for is all the ants that are going left , The table below for target The ants of
{
int cnt = 0;// Counter
for(int i = 0; i<n; i++)
{
if(ant[i].direct == -1 && cnt == target)
{
ans = ant[i].position;// The location of this ant , The time it takes to fall , That is, ants A The time of the fall
break;
}
else if(ant[i].direct == -1)
cnt++;
}
}
else // Fewer ants walk to the left , So the target ant will fall to the right
{
int cnt = 0;
for(int i = n - 1; i>=0; i--)
{
if(ant[i].direct == 1 && cnt == n - target - 1)// Corresponding changes ,cnt To become a static ant, the number of ants on the right side
{
ans = 100 - ant[i].position; // Because it is to the right , Just use 100 Cut it
break;
}
else if(ant[i].direct == 1)
cnt++;
}
}
if(flag)
printf("Cannot fall!\n");
else
printf("%d\n",ans);
}//while
return 0;
}
It took a long time to figure it out , Before you forget , Write it down .
边栏推荐
- Official announcement! Broadcom will acquire VMware with us $61billion and assume US $8billion in debt
- asp. Net datatable and some notes of table
- uniapp h5单页面横屏
- 2022山东健博会,食疗养生与滋补健康展,健康管理与精准医学展
- On go language reflection
- Leetcode 1984. 學生分數的最小差值(可以,已解决)
- Unity UI scrollbar component
- Leetcode 1984. Minimum difference in student scores (yes, resolved)
- WIN7 64位旗舰版安装OFFICE2003 提示:“错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误”
- ConvNets Principles
猜你喜欢

Jvm- how the bytecode is executed by the JVM + a little thought about the thread primer

Root file system

Skills and experience of product planning

版号批下来,可公司已倒闭:现如今,做款游戏还能卖给腾讯吗?

Uniapp H5 single page horizontal screen

More than observation | Alibaba cloud observable Technology Summit officially launched

NoSQL之Redis配置与优化(你不在南京的日子我替你吹了秦淮河的晚风)

Smart PLC calls the same subroutine (FC) multiple times

Unity-UI-Scrollbar组件

Not only Lenovo, which sells computers, but also what's new?
随机推荐
VNCTF 2022 InterestingPHP
Jerry's camera frame number and layer [chapter]
杰理之有关摄像头帧数,以及图层【篇】
杰理之蓝牙配网【篇】
手把手带你使用代码迁移工具实现源码迁移到鲲鹏处理器
Exit full screen with native JS
卡尔曼滤波(KF)无迹卡尔曼滤波(UKF)
看看人家那文本识别系统,那叫一个优雅!
[effectiveness platform] test case management module - obtain use case list data, view use case details data, add and update use cases, delete use case related function development (7)
Jerry's image transmission, recording card or camera displayed by UVC - description of high frame rate [article]
杰理之图传确定工程没有开CONFIG_NO_SDRAM_ENABLE宏,工程使用sdram【篇】
Tips for writing code
驱动开发—基础
2022年GDCPC广东省大学生程序设计竞赛题解
将excel中的合并单元格拆分并填充数据
Fastjson解析JSON时乱序解决
Jvm- how the bytecode is executed by the JVM + a little thought about the thread primer
asp.net txt读写
SMART PLC多次调用同一个子程序(FC)
【opencvsharpDNN】OpenCvSharp中YoloV3和Caffe的实现示例