Usaco 2014 Jan skiing video

Source: Internet
Author: User

2.Ski video{SilverQuestion3}

[Problem description]

The TV timetable for the Winter Olympics contains N (1 <= n <= 150) programs, each of which has a start time and end time. Farmer John has two video recorders. Please calculate the maximum number of programs he can record.

[File input]

The first line is an integer n.

The next n rows have two integers in each row, indicating the start time and end time of a program. The value range is 0 .. 1,000,000,000.

[File output]

An integer that indicates the maximum number of programs that can be recorded.

[Input example]

6

0 3

6 7

3 10

1 5

2 8

1 9

[Output example]

4

[Example]

1st recorded programs 1 and 3, and 2nd recorded programs 2 and 4.

 

 

Thank you for sharing your thoughts ~

 

Ideas:

Greedy, because it is two cameras, so if you use them, there will be repeated use, Fang Dashen only belongs to 70, because the special sentence is more complicated, this scum will give up.

I started to write brute-force search, but I was greedy because I only scored 10 points due to overlap.

 

The greedy idea is to sort by end time B [I] First (why not use the start time, because if you sort by start time, the start time is very small, however, if the time period is long, this will affect the optimum ). Set two variables to record the end time of the current program of the two cameras, and then compare the programs from 1 to n. You can add the variables to the program.

PS: When both cameras can be added, select the camera whose end time is later. Cause: because it is sorted by the end time, it may appear that the program start time is earlier than the program start time. In this case, you can add it to the camera before the end time.

Example:

| ------ | End Time 4

| ---------- | End Time 6

For example, if two cameras have been added to the time range, and now add another start time of 9, add it to the second time, because if the start time of the next program is 5, you can add 9 to the first one. If 9 is added to the first one, 5 cannot be added, and thus it cannot be optimized.

 

 

program recording;var i,j,k,n,m,x,y:longint;a,b:array[0..100001] of longint;procedure data1;var i,j:longint; begin  assign(input,‘recording.in‘);  assign(output,‘recording.out‘);  reset(input);  rewrite(output); end;procedure data2; begin  close(input);  close(output); end;    procedure sort(l,r: longint);      var         i,j,x,y: longint;      begin         i:=l;         j:=r;         x:=b[(l+r) div 2];         repeat           while b[i]            inc(i);           while x            dec(j);           if not(i>j) then             begin                y:=b[i];                b[i]:=b[j];                b[j]:=y;                y:=a[i];                a[i]:=a[j];                a[j]:=y;                inc(i);                j:=j-1;             end;         until i>j;         if l           sort(l,j);         if i           sort(i,r);      end;procedure datain;var i,j,x,y,ans:longint; begin  readln(n);  for i:=1 to n do   readln(a[i],b[i]);  sort(1,n);  x:=0;y:=0;ans:=0;  for i:=1 to n do   begin    if (a[i]>=x) and (a[i]>=y) then      if x>y then begin inc(ans);x:=b[i]; continue; end      else begin inc(ans);y:=b[i]; continue; end;    if a[i]>=x then begin inc(ans);x:=b[i]; end;    if a[i]>=y then begin inc(ans);y:=b[i]; end;   end;  writeln(ans); end;begin data1; datain; data2;end.

 

Usaco 2014 Jan skiing video

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.