CCF-CSP certification exam to buy food

Problem Description

Question number: 201809-2
Questions Name: Grocery shopping
time limit: 1.0s
Memory Limit: 256.0MB
Problem Description:
Problem Description
  Small H and W came to a small street, the two separate buy food, they can be described as the process of grocery shopping, go to the store to buy some food and then go to a square next to the Cook loaded on the train, they have to buy seed n vegetables, so it should be installed n the second car. Specifically, H is an n for small disjoint period [A . 1 , B . 1 ], [A 2 , B 2 ] ... [A n , B n ] in loading, for small W is there are n disjoint periods [C . 1 , D . 1 ], [C 2 , D 2 ] ... [C n , D n ] in loading. Wherein a time period [s, t] represents the time t s to the time from this time, when the length of ts.
  Because they are good friends, they were loading time will chat in the square, they want to know how long they can talk.
Input Format
  The first line input comprises a positive integer n, the number of time periods.
  The next two lines each number n A I , B I period, each describe a small loading of H.
  The next two lines each number n C I , D I , described loading period each W is small.
Output Format
  Output line, a positive integer, said the two can talk long.
Sample input
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
Sample Output
3
Scale data and conventions
  For all evaluation use cases,. 1 ≤ n-≤ 2000, A <B <A I +. 1 , C <D <C I +. 1 , for all i (1 ≤ i ≤ n) with a, 1 ≤ a I , B I , C I , D I  ≤ 1000000.

#include <bits/stdc++.h>

using namespace std;

const int N=2001;
int H[N][2];
int W[N][2];
int times=0;
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++){
cin>>H[i][0]>>H[i][1];
}
for(int i=0; i<n; i++){
cin>>W[i][0]>>W[i][1];
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(H[i][1]>W[j][0] && H[i][0]<=W[j][0] && H[i][1]<=W[j][1]){//等于号千万不能漏了
times+=H[i][1]-W[j][0];
}
else if(H[i][0]<W[j][1] && H[i][0]>=W[j][0] && H[i][1]>=W[j][1]){
times+=W[j][1]-H[i][0];
}
else if(H[i][0]<W[j][0] && H[i][1]>W[j][1]){
times+=W[j][1]-W[j][0];
}
else if(H[i][0]>W[j][0] && H[i][1]<W[j][1]){
times+=H[i][1]-H[i][0];
}
}
}
cout<<times;
return 0;
}

//只能靠水题维持生活了

Guess you like

Origin www.cnblogs.com/yu-xia-zheng-ye/p/11311690.html