1. 程式人生 > >【CodeForces - 589F】Gourmet and Banquet (貪心,思維,二分)

【CodeForces - 589F】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.

Examples

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).

題目大意:

來自谷歌翻譯:

一位美食家進入宴會廳,廚師為客人提供了n道菜。美食家知道時間表:每個菜餚都將供應。
對於第i道菜餚,他知道時間ai和bi的兩個整數時刻(從宴會開始的幾秒鐘內) - ai為該菜端出來的時間,bi為該菜端走的時間(ai <BI)。例如,如果ai = 10且bi = 11,那麼第i個菜餚可在一秒鐘內進食。
菜餚數量非常大,所以只要菜餚可以吃(即,在大廳裡),它就無法用完。
美食家想要嘗試每道菜,不要冒犯任何廚師。因此,美食家想要在相同的時間內吃每道菜。在吃飯期間,美食可以在菜餚之間立即切換。僅在整數時刻允許在菜餚之間切換。
美食家希望在宴會上儘可能長時間吃飯而不違反上述任何條件。你能幫助他,並找出他在宴會上吃的最長時間嗎?
輸入
第一行輸入包含一個整數n(1≤n≤100) - 宴會上的菜餚數量。
以下n行包含有關菜餚可用性的資訊。第i行包含兩個整數ai和bi(0≤ai<bi≤10000) - 第i個菜餚可用於進食以及第i個菜餚從大廳被帶走時的時刻。
產量
輸出應該包含唯一的整數 - 美食家可以在宴會上吃的最大總時間。
美食可以在菜餚之間即時切換,但只能在整個時間點切換。在吃完任何其他菜餚後,它可以返回菜餚。此外,在每個時刻,他都可以吃不超過一道菜。

解題報告:

    區間排程問題學好了這題就會做了。。我們要貪心出結束時間最早的,因為這是對後面的影響最小的。

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
struct Node {
	int l,r;
} node[105];
int n;
bool vis[1000005];
bool ok(int x) {
	memset(vis,0,sizeof vis);
	for(int i = 1; i<=n; i++) {
		int cnt = 0;
		for(int j = node[i].l+1;j<=node[i].r; j++) {
			if(vis[j] == 0) {
				vis[j]=1;
				cnt++;
			}
			if(cnt == x) break;
		}
		if(cnt < x) return 0 ;
	}
	return 1;
}
bool cmp(const Node &a, const Node &b) {
	if(a.r!=b.r) return a.r<b.r;
	return a.l<b.l;
}
int main()
{
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%d%d",&node[i].l,&node[i].r);
	sort(node+1,node+n+1,cmp);
	int l = 0,r = 100000 + 5;
	int mid = (l+r)>>1,ans = -1;
	while(l<=r) {
		mid=(l+r)>>1;
		if(ok(mid)) {
			l=mid+1;ans=mid;
		}
		else r=mid-1;
	}
	if(ans!=-1) {
		printf("%d\n",ans*n);
	}
	else puts("0");
	return 0 ;
 }

總結:這題不對l排序也可以AC,,也就是那裡的符號反過來也可以AC。。。