Practice Round

Problem A GBus count (9pt/15pt) (2019年1月14日,kickstart群每日一题)

题意:有一条笔直的大路,上面有城市编号从 1 开始从左到右一次排列。上面有 N 个 GBuses, 每一个 bus[i] 连接 A[i]  到 B[i] 两个地点(包含这两个地方)。我们想要求 P 个城市,每个城市经过的公交车数量。

输入输出 和 数据规模 如下:

There exist some cities that are built along a straight road. The cities are numbered 1, 2, 3... from left to right.
There are N GBuses that operate along this road. For each GBus, we know the range of cities that it serves: the i-th gBus serves the cities with numbers between Ai and Bi, inclusive.
We are interested in a particular subset of P cities. For each of those cities, we need to find out how many GBuses serve that particular city.
Input
The first line of the input gives the number of test cases, T. Then, T test cases follow; each case is separated from the next by one blank line. (Notice that this is unusual for Kickstart data sets.)
In each test case: The first line contains one integer N: the number of GBuses. The second line contains 2N integers representing the ranges of cities that the buses serve, in the form A1 B1 A2 B2 A3 B3 ... AN BN. That is, the first GBus serves the cities numbered from A1 to B1 (inclusive), and so on. The third line contains one integer P: the number of cities we are interested in, as described above. (Note that this is not necessarily the same as the total number of cities in the problem, which is not given.) Finally, there are P more lines; the i-th of these contains the number Ci of a city we are interested in.
Output For each test case, output one line containing Case #x: y, where x is the number of the test case (starting from 1), and y is a list of P integers, in which the i-th integer is the number of GBuses that serve city Ci. Limits 1 ≤ T ≤ 10. Small dataset 1 ≤ N ≤ 50 1 ≤ Ai ≤ 500, for all i. 1 ≤ Bi ≤ 500, for all i. 1 ≤ Ci ≤ 500, for all i. 1 ≤ P ≤ 50. Large dataset 1 ≤ N ≤ 500. 1 ≤ Ai ≤ 5000, for all i. 1 ≤ Bi ≤ 5000, for all i. 1 ≤ Ci ≤ 5000, for all i. 1 ≤ P ≤ 500.

题解:我们只需要把每个bus的起点和终点读进来,然后对于每一个城市,都去每个bus的区间里面查这个城市是不是在这个bus区间里面,是的话就加一。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <map>
 6 #include <set>
 7 
 8 using namespace std;
 9 
10 void print(vector<pair<int, int>>& bus) {
11   for (int i = 0; i < bus.size(); ++i) {
12     printf("%d -> %d \n", bus[i].first, bus[i].second);
13   }
14 }
15 void solve(const int id) {
16   int n, p;
17   cin >> n;
18   vector<pair<int, int>> bus(n);
19   for (int i = 0; i < n; ++i) {
20     int s, e;
21     cin >> s >> e;
22     if (s > e) { 
23       swap(s, e); 
24     }
25     bus[i] = make_pair(s, e);
26   }
27 // print(bus);
28   cin >> p;
29   vector<int> ans(p, 0);
30   for (int i = 0; i < p; ++i) {
31     int city;
32     cin >> city;
33     for (auto b : bus) {
34       if (city >= b.first && city <= b.second) {
35         ans[i]++;
36       }
37     }
38   }
39   printf("Case #%d:", id);
40   for (auto e : ans) {
41     printf(" %d", e);
42   }
43   printf("\n");
44   return;
45 }
46 int main () {
47   int t;
48   cin >> t;
49   for (int idx = 1; idx <= t; ++idx) {
50     solve(idx);
51   }
52   return 0;
53 }
View Code

相关文章: