Ants
n ants crawl at 1cm per second on poles that are long LCM. When the ant climbs to the end of the pole, it drops. Because the pole is too thin, two ants meet, they can not staggered through, only their own reverse crawl back. For each ant, we know its distance from the left side of the Pole XI, but do not know its current orientation. Please calculate the minimum and maximum time required for all ants to drop a pole.
Restrictions:
1<=l<=10^6
1<=n<=10^6
0<=xi<=l
General ideas are as follows:
#include <stdio.h>#include<stdlib.h>#defineMax (x, y) ((x>y)? x:y)#defineMin (x, y) ((x<y)? x:y)inta[1000000];intMainintargcChar*argv[]) { intmaxnum=0, minnum=0; inti,n,l; scanf ("%d",&l); scanf ("%d",&N); for(i=0; i<n;i++) {scanf ("%d",&A[i]); } for(i=0; i<n;i++) {Maxnum=max (Maxnum,max (la[i],a[i])); Minnum=max (Minnum,min (la[i],a[i])); } printf ("%d%d \ n", Minnum,maxnum); Free(a); System ("PAUSE"); return 0;}
Note The main points:
1. For a[1000000] to be set to a global variable, the program cannot run because the local variable is stored in the memory stack and its size is limited.
2. You can also use the declaration space Way Int *a= (int*) malloc (sizeof (int) *1000000), and then free (a).
3. Time complexity is O (n).
4. This problem to understand test instructions, such as in the shortest possible time to complete laundry, cooking, boiling water, knocking code.
Reading notes < programming >---Ants