CCF Computer Professional Qualification Examination 201809-2

Keywords: C++

The following is too naive, please consciously bypass.

Title Description:

Time limit:
1.0s
Memory limitation:
256.0MB
Problem Description:
Problem description
Xiao H and Xiao W came to a street. They bought vegetables separately. The process of buying vegetables can be described as going to the store to buy some vegetables and then going to a nearby square to load the vegetables into the car. Both of them had to buy n kinds of vegetables, so they also had to load n cars. Specifically, for small H, there are n disjoint periods [a1, b1], [a2, b2], [an, bn] in loading, for small W, there are n disjoint periods [c1, d1], [c2, d2], [cn, dn] in loading. Among them, a time period [s, t] denotes the period from time s to time t, and the time length is t-s.
Because they are good friends, they all talk when loading cars in the square. They want to know how long they can talk.
Input format
The first line of input contains a positive integer n, representing the number of time periods.
Next, two numbers ai and bi in line n describe each loading period of Xiao H.
Next, line n has two ci and di numbers per line, describing each loading period of Xiao W.
Output format
Output a line, a positive integer, indicating how long they can talk.
sample input
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
sample output
3
Data size and conventions
For all evaluation cases, 1 < n < 2000, AI < Bi < AI + 1, CI < Di < CI + 1, and for all i(1 < i < n), 1 < ai, bi, ci, di < 1000000.

 

1. Naive solution, applicable to all cases

 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 problems

 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 }

 

3. The easiest to understand and the most 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 give me your advice.

Posted by Toonster on Sat, 26 Jan 2019 02:51:15 -0800