【CodeForces - 589F】Gourmet and Banquet (贪心,思维,二分)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83549650

题干:

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.

扫描二维码关注公众号,回复: 3869733 查看本文章

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。。。

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83549650