Reading notes < programming >---Ants

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.