CodeForces 589F-Gourmet and Banquet-二分答案

有m盘菜,每盘有一个开始时间和结束时间,必须每盘都吃同样的时间。问最多能吃多久。

二分答案,然后用一个优先队列维护当前时间内的菜,然后每次都吃结束时间最小的那盘。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <queue>
 5
 6 using namespace std;
 7
 8 const int maxn = 1e4+10;
 9 const int INF = 0x3f3f3f3f;
10 int N,len,a[maxn],b[maxn],save[200];;
11
12 struct node
13 {
14     int a,b,id;
15     node(){}
16     node(int x,int y,int z):a(x),b(y),id(z){}
17     bool operator < (const node &b) const
18     {
19         return a<b.a;
20     }
21 }Dish[maxn];
22
23 struct node2
24 {
25     int ed,id;
26     node2(int x,int y):ed(x),id(y){}
27     bool operator < (const node2 &b) const
28     {
29         return ed > b.ed;
30     }
31 };
32
33 int solve(int ll,int rr)
34 {
35     int mid = (ll+rr)>>1,cnt=0;
36     if(ll+1 >= rr) return mid;
37     priority_queue<node2> q;
38
39     memset(save,0,sizeof save);
40     //printf("ll:%d rr:%d\n",ll,rr);
41     for(int cur=0;cur<=len;cur++)
42     {
43         //printf("cur:%d cnt:%d num:%d\n",cur,cnt,q.size());
44         while(cnt < N && Dish[cnt].a == cur)
45         {
46              q.push(node2(Dish[cnt].b,Dish[cnt].id));
47              cnt++;
48         }
49         //printf("save:%d mid:%d\n",save[q.top().id],mid);
50         while(!q.empty() && save[q.top().id]>=mid && save[q.top().id])
51         {
52             q.pop();
53         }
54
55         if(!q.empty())   {/*printf("id:%d++\n",q.top().id);*/save[q.top().id]++;}
56
57         while(!q.empty() && q.top().ed <= cur+1)
58         {
59             if(save[q.top().id] < mid || save[q.top().id]==0) {/*printf("id:%d save:%d\n",q.top().id,save[q.top().id]);*/return solve(ll,mid==0?0:mid);}
60             q.pop();
61         }
62
63         if(q.empty()&&cnt==N) return solve(mid,rr);
64     }
65 }
66
67 int main()
68 {
69     scanf("%d",&N);
70     int l = INF,r = -INF;
71     len = -INF;
72     for(int i=0;i<N;i++)
73     {
74         scanf("%d%d",&a[i],&b[i]);
75         Dish[i] = node(a[i],b[i],i);
76         r = max(b[i]-a[i],r);
77         len = max(len,b[i]);
78     }
79     sort(Dish,Dish+N);
80     printf("%d\n",solve(0,r+1)*N);
81 }
时间: 2024-10-19 03:37:24

CodeForces 589F-Gourmet and Banquet-二分答案的相关文章

Codeforces 670D2 Magic Powder - 2 二分答案

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai - how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinar

Codeforces 551C GukiZ hates Boxes 二分答案

题目链接 题意: 一共有n个空地(是一个数轴,从x=1 到 x=n),每个空地上有a[i]块石头 有m个学生 目标是删除所有石头 一开始所有学生都站在 x=0的地方 每秒钟每个学生都可以在原地删除一块石头,或者向 → 移动一格距离 问:删除所有石头的最短时间 案例解析: 3 2 1 0 2 第一个学生第一秒向→走,第二秒删a[1]的一块石头 第二个学生一直走到头,删掉a[3] ,所以第二个学生花费是 1+1+1+2 = 5 两个学生可以同时运动. 思路: 二分答案,设答案为x,即需要x秒来搬完石

CodeForces 343C Read Time 【二分答案】+【贪心】

<题目链接> 题目大意: 一条水平的磁道上有n个磁头和m个待扫描的点,磁头可以左右互不干扰的移动去扫描点,每秒移动一个单位(也可以停留在原地),求这些磁头扫描完这些所有的点最少需要要花多少时间. 解题分析: 本题用二分答案和贪心求解,先二分出这些磁头扫描完所有的点所需的时间,然后用贪心策略去模拟每个磁头扫描这些点.对这些磁头从左向右分析,假设二分出的总时间为mid,在该条件下,每个磁头扫描点的最优情况毫无疑问是,在保证扫描到当前 最左边未被扫描到的点的情况下,向右扫描尽可能远的距离(对于这些磁

Codeforces 825D Suitable Replacement - 贪心 - 二分答案

You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Codeforces 700A As Fast As Possible(二分答案)

[题目链接] http://codeforces.com/problemset/problem/700/A [题目大意] 有一辆限载k人速度为v2的车,n个步行速度均为v1的人要通过一段长度为l的距离,每个人只能上车一次,车可以来回走,问所有人到达目的地所需要的最短时间是多少 [题解] 因为车可以载k个人,所以,我们把人k个为一组分成(n+k-1)/k组,记为p吗,设需要的最短时间为t,每个人在车上待的时间为t2,那么可以列方程v1*(t-t2)+v2*t2=l,我们可以发现t2可以用t来表示,

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序 题意 有 a[ i ] 个数 要求选最多的数 使其和不超过 S ,且在此情况下,和最小选最多数情况下 和最小 且 每个数有加成 如果选了 k个数 那么加成后 就是 a[ i ] + k*i ; 题解 二分mid 表示选了个数 加成一下,将加成以后结果排序一下 , 若前 mid数 和大于 s 则此方案不可行 PS 要用 long long ..... 还有 co

Codeforces 460C prsent(二分答案)

//题意:给定N朵花的原先的高度,从左到右排列, //最多浇水m天,每天只能浇一次,每次使得连续的w朵花的高度增长1,问最后最矮的花的高度最高是多少. # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int main() { __int64 n,m,w,l,r,i,m1,sum; __int64 a[200010],b[200010]; while(~

Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

Digital collectible card games have become very popular recently. So Vova decided to try one of these. Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck