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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441347.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 调用动态链接库_JAVA技巧:JNative调用动态链接库问题(SOS)

动态链接库的方法如下&#xff1a;__declspec(dllexport) ret __stdcall rLachTran(const char *pc_trancode,const char *pc_clicode,const char *pc_orgcode,const char *pc_ttycode,const int i_brandid,const char *pc_reqstamp,const int i_reqseqno,const char *pc_svrip…

SPFA算法模板

简单的一个模板吧&#xff0c;留个底。如果要判负环的话&#xff0c;需要加一个cnt数组记录入队次数就可以了。 void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]0;vis[st]1;while(!q.empty()) {int cur q.front();q.pop();vis[cur]0;i…

js和php能生成一样的随机数_JavaScript_JS生成某个范围的随机数【四种情况详解】,前言: JS没有现成的函数,能 - phpStudy...

JS生成某个范围的随机数【四种情况详解】前言&#xff1a;JS没有现成的函数&#xff0c;能够直接生成指定范围的随机数。但是它有个函数&#xff1a;Math.random() 这个函数可以生成 [0,1) 的一个随机数。利用它&#xff0c;我们就可以生成指定范围内的随机数。而涉及范围的话…

【POJ - 3347 】Kadj Squares (计算几何,思维 或 扫描线)

题干&#xff1a; In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees w…

按钮开关java代码,Android自定义实现开关按钮代码

我们在应用中经常看到一些选择开关状态的配置文件&#xff0c;做项目的时候用的是android的Switch控件&#xff0c;但是感觉好丑的样子子个人认为还是自定义的比较好&#xff0c;先上个效果图&#xff1a;实现过程&#xff1a;1.准备开关不同状态的两张图片放入drawable中。2.x…

java创建的zip没写入权限,java中的zip创建错误

我正在使用ZipOutputStream,FileOutputStream和FileInputStream.首先我创建了一个包含一个文件的文件夹它成功创建了.然后我尝试创建zip文件.动态地,它首次正确创建文件,但第二次,第三次在打开文件时出错.Error: zip [path/././file.zip] Cannot open The process cannot acces…

【qduoj - 夏季学期创新题】最长公共子串(水题暴力枚举,不是LCS啊)

题干&#xff1a; 描述 编写一个程序&#xff0c;求两个字符串的最长公共子串。输出两个字符串的长度&#xff0c;输出他们的最长公共子串及子串长度。如果有多个最长公共子串请输出在第一个字符串中先出现的那一个。 特别注意公共子串中可能包含有空格&#xff0c;但不计回车…

向量合并 matlab,MATLAB追加向量

如果有两个行向量 r1 和 r2 这两个行向量中各有 n 和 m 个元素&#xff0c;现在创建行向量 r 并将n和m个元素都放在行向量 r 中&#xff0c;通过附加这些载体&#xff0c;编写&#xff1a;r [r1,r2]通过追加这两个向量&#xff0c;向量r2的&#xff0c;也可以建立一个矩阵R&am…

*【CodeForces - 202C 】Clear Symmetry (思维,找规律,特判)

题干&#xff1a; Consider some square matrix A with side n consisting of zeros and ones. There are nrows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. Well denote the element of the matrix wh…

matlab将模型解封装,模型保护 - MATLAB Simulink - MathWorks 中国

当您要与第三方共享模型而又不能泄露知识产权时&#xff0c;请对模型进行保护。Test your protected model by comparing it to the original model.Attach a digital signature to your protected model.Files to include in the protected model package.Specify a post-proc…

*【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)

题干&#xff1a; Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the decease…

matlab 自适应噪声对消,基于Matlab的RLS自适应语音噪声对消系统的设计与实现

基于Matlab 的R LS 自适应语音噪声对消系统的设计与实现①肖 哲(湖南工业大学科技学院, 湖南株洲 412008)摘 要:自适应信号处理的理论和技术经过40多年的发展和完善,已逐渐成为人们常用的语音去噪技术.而Matlab 的出现又为其提供了更为方便快捷的方法来对语音信号进行消噪处…

【qduoj - 142】 多重背包(0-1背包的另类处理,dp)

题干&#xff1a; ycb的ACM进阶之路 Description ycb是个天资聪颖的孩子&#xff0c;他的梦想是成为世界上最伟大的ACMer。为此&#xff0c;他想拜附近最有威望的dalao为师。dalao为了判断他的资质&#xff0c;给他出了一个难题。dalao把他带到一个到处都是题的oj里对他说&am…

python数字类型怎么学,python的数字类型学习之数据类型

1、在python中&#xff0c;数字并不是一个真正的对象类型&#xff0c;而是一组类似类型的分类。它支持通常的数字类型&#xff0c;还能够可以通过常量直接创建数字&#xff0c;还可以处理数字表达式。2、数字常量&#xff1a;(1)整数和浮点数常量(2)16进制、8进制、2进制常量(3…

贪心算法 -- 最小延迟调度

转自&#xff1a;https://blog.csdn.net/bqw18744018044/article/details/80285414 总结&#xff1a; 首先&#xff0c;证明贪心的时候交换论证是万能的&#xff01;其次&#xff0c;这一点如果要满足&#xff0c;也就是&#xff0c;如果你要用交换论证法&#xff0c;那么首先…

php成行排列,一个php实现的生成排列的算法

function perm($s, $n, $index){if($n 0){return ;}else{$nIndex count($index); //可用的字符串下标$res array();foreach($index as $i > $v){$tmp $index;unset($tmp[$i]); //去掉当前的前缀/* 调试信息&#xff0c;便于理解echo "len $n , cur $i , index:\n&q…

【CodeForces - 1051C 】Vasya and Multisets (模拟)

题干&#xff1a; Vasya has a multiset ss consisting of nn integer numbers. Vasya calls some number xxnice if it appears in the multiset exactly once. For example, multiset {1,1,2,3,3,3,4}{1,1,2,3,3,3,4} contains nice numbers 22 and 44. Vasya wants to spl…

apache2+支持php7,Ubuntu14.04下配置PHP7.0+Apache2+Mysql5.7

Apache步骤一&#xff1a;安装apacheronyaoubuntu:~$ sudo apt install apache2安装好后&#xff0c;在浏览器上输入localhost(服务器端&#xff0c;请输入你的IP地址)&#xff0c;回车就会看到&#xff1a;PHP7.0步骤二&#xff1a;Ubuntu14.04下的默认源是PHP5.0&#xff0c;…

php怎么添加验证码,PHP添加验证码以及使用

现在很多页面在使用表单提交时&#xff0c;都会使用到验证码的使用、如何制做一个验证码呢&#xff1f;这里有一个用PHP的方法 以及代码1、首先在php.ini 配置文件里面把GD库打开 // 在php.ini里面找到 extensionphp_gd2.dll 把前面的分号删去。2、代码&#xff1a;<?php …

【CodeForces - 1051D】Bicolorings (dp,类似状压dp)

题干&#xff1a; You are given a grid, consisting of 22 rows and nn columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells AA and BB be…