CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet

C - Gourmet and Banquet

A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of the dishes will be served.

For i-th of the dishes he knows two integer moments in time ai and bi (in seconds from the beginning of the banquet) — when the cooks will bring the i-th dish into the hall and when they will carry it out (ai < bi). For example, if ai = 10 and bi = 11, then the i-th dish is available for eating during one second.

The dishes come in very large quantities, so it is guaranteed that as long as the dish is available for eating (i. e. while it is in the hall) it cannot run out.

The gourmet wants to try each of the n dishes and not to offend any of the cooks. Because of that the gourmet wants to eat each of the dishes for the same amount of time. During eating the gourmet can instantly switch between the dishes. Switching between dishes is allowed for him only at integer moments in time. The gourmet can eat no more than one dish simultaneously. It is allowed to return to a dish after eating any other dishes.

The gourmet wants to eat as long as possible on the banquet without violating any conditions described above. Can you help him and find out the maximum total time he can eat the dishes on the banquet?

Input

The first line of input contains an integer n (1 ≤ n ≤ 100) — the number of dishes on the banquet.

The following n lines contain information about availability of the dishes. The i-th line contains two integers ai and bi (0 ≤ ai < bi ≤ 10000) — the moments in time when the i-th dish becomes available for eating and when the i-th dish is taken away from the hall.

Output

Output should contain the only integer — the maximum total time the gourmet can eat the dishes on the banquet.

The gourmet can instantly switch between the dishes but only at integer moments in time. It is allowed to return to a dish after eating any other dishes. Also in every moment in time he can eat no more than one dish.

Example

Input

3
2 4
1 5
6 9

Output

6



Input

3
1 2
1 2
1 2

Output

0

Note

In the first example the gourmet eats the second dish for one second (from the moment in time 1 to the moment in time 2), then he eats the first dish for two seconds (from 2 to 4), then he returns to the second dish for one second (from 4 to 5). After that he eats the third dish for two seconds (from 6 to 8).

In the second example the gourmet cannot eat each dish for at least one second because there are three dishes but they are available for only one second (from 1 to 2).

这题主要靠二分来快速尝试答案,同时辅以贪心来检验答案。思路为首先按撤走菜的时间排序,先撤走的靠前,然后通过二分获得一个时间长度,代入每道菜的可吃时间,那个时段张了口,就将这些时段标记,下次再遇到即忽略这些已被占用的时段。
由于每确定一次吃菜的时刻,必须自此时刻起吃一分钟,且一分钟结束,可立马切换至另一道菜,可以只标记每次吃菜的开始时刻,如吃菜时段 3 .. 4,可以只记录 3 这个数值。
接着就是在排好序的基础上一道菜一道菜地套用二分得到的时段,若有不满足者,说明时段过长;反之说明过短或正好。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
//#define TEST
using namespace std;
struct node{
    int beg, end;
} dish[110];
int n;
bool isAccessed[10009];
bool cmp(node a, node b){
    return a.end < b.end;
}
bool check(int len){ //对当前时段长度的可行性进行检验。
    memset(isAccessed, false, sizeof(isAccessed));
    for(int i = 0; i < n; i++){
        int cnt = 0;
        for(int j = dish[i].beg; j < dish[i].end; j++){
            if(!isAccessed[j]){
                isAccessed[j] = true;
                cnt++;
                if(cnt == len)
                    break;
            }
        }
        if(cnt < len){
            return false;
        }
    }
    return true;
}
int main(){
#ifdef TEST
freopen("test.txt","r",stdin);
#endif // TEST

    while(cin >> n){
        int left = 0, right = 10000, mid, ans;
        for(int i = 0; i < n; i++){
            cin >> dish[i].beg >> dish[i].end;
            right = dish[i].end - dish[i].beg < right ? dish[i].end - dish[i].beg : right; //right的最大值是可供品尝时间最短的那道菜对应的时段长度。
        }
        sort(dish, dish + n, cmp);
        while(left <= right){
            mid = (right + left) / 2;
            if(check(mid)){
                ans = mid;
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }
        }
        cout << ans * n << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值