小言_互联网的博客

B. Light bulbs(The Preliminary Contest for ICPC Asia Shanghai 2019)

308人阅读  评论(0)

B. Light bulbs(The Preliminary Contest for ICPC Asia Shanghai 2019)


测试数据

输入:
2
10 2
2 6
4 8
6 3
1 1
2 3
3 4
输出:
Case #1: 4
Case #2: 3

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
    int t, n, m;
    scanf("%d", &t);
    for (int i = 1; i <= t; i++){
        int a[10100];
        int pos = 0;
        scanf("%d%d", &n, &m);
        for (int j = 0; j < m; j++){
            int l, r;
            scanf("%d%d", &l, &r);
            a[pos++] = l;
            a[pos++] = r+1;
        }
        sort(a, a + m*2);
        int sum = 0;
        for (int j = 0; j < pos; j += 2) sum += a[j+1]-a[j];
        printf("Case #%d: %d\n", i, sum);
    }
    return 0;
}

转载:https://blog.csdn.net/weixin_43359312/article/details/100860118
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场