CCF-201809-2-买菜

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AivenZhong/article/details/83343579

题目:
自己去官网看

思路:
计算两个人装车重叠的时间段。具体方法:建立一个时间段数组,计每个时间段人出现的次数,比如小H在时刻3-时刻6装车,就在时间段3(3-4),4(4-5),5(5-6) 加1,说明时间段3,4,5有人装车,如果小W在4-8装车,同理在时间段4,5,6,7 加1。最后算出时间段4,5出现了2次人,说明两个人有相遇。(注意,要计算两个人重合的时间,是算时间段的,不是时刻。时刻3-时刻6,有3个时间段(3-4,4-5,5-6))同理,处理剩下的时间段,最后统计时间点次数为2的有多少个。

样例输入
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
样例输出
3

图解:
在这里插入图片描述

Java代码:

import java.util.Scanner;

public class 买菜 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] t = new int[1000000];
        int count = 0;

        for (int i = 0; i < n * 2; i++) {
            int a = input.nextInt();
            int b = input.nextInt();
            for (int j = a; j < b; j++)
                t[j]++;
        }

        for (int i : t)
            if (i > 1)
                count++;

        System.out.println(count);
    }
}


python代码:

bk = [0 for i in range(1000000)]
n = int(input())
count = 0

for i in range(n * 2):
    a, b = [int(val) for val in input().split()]
    for j in range(a, b):
        bk[j] += 1

for val in bk:
    if val == 2:
        count += 1

print(count)

这次考证的第1,2题用不了多少时间,水题。。。。。。广东的同学亏了。(因为台风山竹,没能去参加考证。)

猜你喜欢

转载自blog.csdn.net/AivenZhong/article/details/83343579