CodeForces 589F Gourmet and Banquet

【题目大意】:
有N份菜,分别在[ai,bi]时间段内有供应,一位美食家想吃到每样菜,并且吃每样菜的时间要相同(吃每道菜的次数不限,比如可在a1-a2时间吃A菜,a3-a4时间再吃一次A菜,这样吃A菜的总时间为a4-a3+a2-a1)。求美食家能享受菜品的最大时间。(原题及样例如下)

F. Gourmet and Banquet

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

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

Copy
3
2 4
1 5
6 9

output

Copy
6

input

Copy
3
1 2
1 2
1 2

output

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

 

【分析】:

参考求最大不相交区间的贪心法的做法,这里的做法如下:现将时间段按b排序(与求最大不相交区间相同),由于是要求总时间最大,也就是求每段区间能取的时间的最大值,且最大值满足单调性质(随每段区间能取的时间,总时间增加,即答案增加,亦即存在不满足题设条件和满足题设条件的唯一分界点),故我们可以用二分答案的方法举出每段区间能取的时间,判断是否合法的方法是从前往后地根据之前排好序了的菜品顺序去取时间,如果无法达到举出的时间就不合法,否则合法。

 

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define mem(x)	memset(x,0,sizeof(x))

struct dish
{
	int a,b;
}d[110];

int n,maxt=0;
int vis[10010];

int cmp(dish x,dish y){
	return x.b<y.b;
}

bool check(int time){
	mem(vis);
	for (int i = 0; i < n; ++i)
	{
		int q=0;
		if(time>d[i].b-d[i].a)	return false;
		for(int j=d[i].a;j<d[i].b;j++){
			if(vis[j]==0){
				q++;
				vis[j]=1;
				if(q==time)	break;
			}
		}
		if(q<time)	return false;
	}
	return true;
}

int main(int argc, char const *argv[])
{
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i].a>>d[i].b;
		maxt=max(maxt,d[i].b);
	}
	int l=0,r=maxt,ans=0;;
	sort(d,d+n,cmp);
	while(l<=r){//等于号不能少
		int mid=(l+r)/2;
		if(check(mid)){
			l=mid+1;
			ans=max(ans,mid);//只有合法的mid才能够被记录
		}
		else{
			r=mid-1;
		}
	}
	cout<<ans*n<<endl;
	return 0;
}

 

喜欢()
评论 (0)
热门搜索
37 文章
3 评论
11 喜欢
Top