Gourmet and Banquet -CodeForces - 589F
2017-07-27 08:07
495 查看
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
Output
Input
Output
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).
题意:
这道题就是贪心加上二分,先对时间段按照结束时间排序,做贪心选择,之后对答案进行二分
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int n;
int vis[10005];
struct node
{
int left;
int right;
bool operator < (const node&a) const
{
return right<a.right||(right==a.right&&left<a.left);
}
};
node a[105];
bool ok(int x)
{
memset(vis,0,sizeof(vis));
for(int i = 0;i<n;i++)
{
if((a[i].right-a[i].left)<x)
return false;
int help = 0;
for(int j = a[i].left;j<a[i].right;j++)
{
if(!vis[j])
{
h
4000elp++;
vis[j] = 1;
if(help==x)
break;
}
}
if(help<x)
return false;
}
return true;
}
int main()
{
while(cin>>n)
{
int big = 10000;
for(int i = 0;i<n;i++)
{
cin>>a[i].left;
cin>>a[i].right;
}
sort(a,a+n);
int small = 0;
int ans = 0;
while(big>=small)
{
int mid = (small+big)/2;
if(ok(mid))
{
small = mid+1;
ans = max(ans,mid);
}
else big = mid-1;
}
cout<<ans*n<<endl;
}
}
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).
题意:
这道题就是贪心加上二分,先对时间段按照结束时间排序,做贪心选择,之后对答案进行二分
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int n;
int vis[10005];
struct node
{
int left;
int right;
bool operator < (const node&a) const
{
return right<a.right||(right==a.right&&left<a.left);
}
};
node a[105];
bool ok(int x)
{
memset(vis,0,sizeof(vis));
for(int i = 0;i<n;i++)
{
if((a[i].right-a[i].left)<x)
return false;
int help = 0;
for(int j = a[i].left;j<a[i].right;j++)
{
if(!vis[j])
{
h
4000elp++;
vis[j] = 1;
if(help==x)
break;
}
}
if(help<x)
return false;
}
return true;
}
int main()
{
while(cin>>n)
{
int big = 10000;
for(int i = 0;i<n;i++)
{
cin>>a[i].left;
cin>>a[i].right;
}
sort(a,a+n);
int small = 0;
int ans = 0;
while(big>=small)
{
int mid = (small+big)/2;
if(ok(mid))
{
small = mid+1;
ans = max(ans,mid);
}
else big = mid-1;
}
cout<<ans*n<<endl;
}
}
相关文章推荐
- CodeForces 589F -- F. Gourmet and Banquet (二分 + 贪心)
- CodeForces 589F-Gourmet and Banquet-二分答案
- CodeForces 589F:Gourmet and Banquet(二分+贪心)
- 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)
- 网络流CodeForces. Original 589F:Gourmet and Banquet
- 文章标题 codeforces 598F :Gourmet and Banquet(贪心+二分)
- Gourmet and Banquet CodeForces - 589F
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet CodeForces - 589F
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet
- 2015 NEERC F. Gourmet and Banquet
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F. Gourmet and Banquet(贪心+二分)
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F】【贪心+ STL-优先队列】 Gourmet and Banquet 最大活动时间
- Codeforces 400A Inna and Choose Options(暴力)
- codeforces 442 A. Alex and broken contest(水题)
- Codeforces 595 B. Pasha and Phone【数学】
- Codeforces Div. 2 #257-C. Jzzhu and Chocolate
- codeforces - 877A - Alex and broken contest【string的一些技巧】
- 【打CF,学算法——四星级】CodeForces 514D R2D2 and Droid Army (RMQ+二分)
- Codeforces 832D Misha, Grisha and Underground【LCA】