C - Free Food

Do you know what attracts almost any college student to participate in an event? Yes, free food. It doesn’t matter whether the event involves a long (sometimes boring) seminar. As long as free food is served for the event, then students will surely come.

Suppose there are N
events to be held this year. The ith event is scheduled from day si to day ti, and free food is served for that event every day from day si to day ti

(inclusive). Your task in this problem is to find out how many days there are in which free food is served by at least one event.

For example, let there be N=3
events. The first event is held from day 10 to 14, the second event is held from day 13 to 17, and the third event is held from day 25 to 26. The days in which free food is served by at least one event are 10,11,12,13,14,15,16,17,25,26, for a total of 10 days. Note that both events serve free food on days 13 and 14

.
Input

The first line contains an integer N
(1≤N≤100) denoting the number of events. Each of the next N lines contains two integers si and ti (1≤si≤ti≤365) denoting that the ith event will be held from si to ti

(inclusive), and free food is served for all of those days.
Output

The output contains an integer denoting the number of days in which free food is served by at least one event.
Sample Input 1 Sample Output 1

3
10 14
13 17
25 26

10

Sample Input 2 Sample Output 2

2
1 365
20 28

365

Sample Input 3 Sample Output 3

4
29 29
48 48
102 102
94 94

4

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    int t, n, a[370], x, y, i;
    int c;
    scanf("%d", &t);
    memset(a, 0, sizeof(a));
    c = 0;
    while(t--)
    {
        scanf("%d %d", &x, &y);
        for(i=x;i<=y;i++)
        {
            if(a[i]==0)
            {
                c++;
                a[i] = 1;
            }
        }
    }
    printf("%d\n", c);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值