CCF computer vocational qualification certification exam 201809-2 Food Buyers

Source: Internet
Author: User

The following content is too naive ..

Description:

Time limit:
1.0 s
Memory limit:
256.0 MB
Problem description:
Problem description
H and W came to a street, And they bought food separately. They could describe the process of buying food. They went to the store to buy some food and then loaded the food into a square next to them, both of them need to buy n kinds of food, so they also need to install n cars. Specifically, for small h, there are N non-Intersecting time periods [A1, B1], [A2, B2]... [An, BN] when loading a car, for small W, there are n different time periods [C1, D1], [C2, D2]... [CN, DN] loading. [S, T] indicates the period from time s to time t, and the length of time is t-s.
Because they are good friends, they will chat when they are loading cars in the square. They want to know how long they can chat.
Input Format
The first line of the input contains a positive integer N, indicating the number of time periods.
In the next n rows, there are two pieces of AI and Bi in each row, which describe the loading time periods of small h.
The next n rows have two CI, Di, and describe the loading time periods of W.
Output Format
Output a row, a positive integer, indicating how long the two can chat.
Sample Input
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
Sample output
3
Data scale and conventions
For all evaluation cases, 1 ≤ n ≤ 2000, AI <Bi <AI + 1, CI <di <Ci + 1, for all I (1 ≤ I ≤ n) yes, 1 ≤ AI, Bi, CI, di ≤ 1000000.

 

1. Simple Solution, applicable to all situations

 1 #include<iostream> 2 using namespace std; 3 int main(){ 4     int n; 5     cin>>n; 6     int a[n],b[n],c[n],d[n]; 7     for(int i=0;i<n;i++){ 8         cin>>a[i]>>b[i]; 9     }10     for(int i=0;i<n;i++){11         cin>>c[i]>>d[i];12     }13     int count=0;14     int i=0,j=0;15     while(i<n&&j<n){16         if(a[i]<=c[j]){17             if(b[i]<=d[j]&&b[i]>c[j]) {18                 count+=b[i]-c[j];i++;19             }20             else {21                 if(b[i]<=c[j]) i++;22                 else{23                     count+=d[j]-c[j];j++;24                 }    25             }26                 27         }28         else{29             if(b[i]>=d[j]&&d[j]>a[i]){30                 count+=d[j]-a[i];j++;31             }32             else{33                 if(d[j]<=a[i]) j++;34                 else{35                     count+=b[i]-a[i];i++;36                 }37                 38             }39         }40     41     }        42     cout<<count;43     return 0;44 } 
View code

 

2. Summarize the problem

 1 #include<iostream> 2 using namespace std; 3 int main(){ 4     int n; 5     cin>>n; 6     int a[n],b[n],c[n],d[n]; 7     for(int i=0;i<n;i++){ 8         cin>>a[i]>>b[i]; 9     }10     for(int i=0;i<n;i++){11         cin>>c[i]>>d[i];12     }13     int count=0;int x,y;14     for(int i=0;i<n;i++){15         for(int j=0;j<n;j++){16             count+=max(0,min(b[i],d[j])-max(a[i],c[j]));17         }18     }19     cout<<count;20     return 0;21 }
View code

 

3. The most understandable and ingenious

 1 #include<iostream> 2 using namespace std; 3 int ojbk[1000000];     4 int main(){ 5     int n; 6     cin>>n; 7     int a[n],b[n],c[n],d[n]; 8     for(int i=0;i<1000000;i++){ 9         ojbk[i]=0;10     }11     for(int i=0;i<n;i++){12         cin>>a[i]>>b[i];13         for(int j=a[i];j<b[i];j++){14             ojbk[j]++;15         }16     }17     for(int i=0;i<n;i++){18         cin>>c[i]>>d[i];19         for(int j=c[i];j<d[i];j++){20             ojbk[j]++;21         }22     }23     int count=0;24     for(int i=0;i<1000000;i++){25         if(ojbk[i]>1) count++;26     }27     cout<<count;28     return 0;29 }
View code

 

Please advise

 

CCF computer vocational qualification certification exam 201809-2 Food Buyers

Related Article

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.