CodeForces 337B Preparing for the Contest(二分+贪心+优先队列)

2024-06-05 04:18

本文主要是介绍CodeForces 337B Preparing for the Contest(二分+贪心+优先队列),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:CodeForces 337B Preparing for the Contest


题目大意:有n个人,m个病毒,s张通行证,然后给出m个病毒的等级,n个人的等级,以及n个人去杀病毒所需要的通行证数量,问所最少花费几天可以杀光病毒,并输出每个病毒被那一个人所清理。PS:人要杀病毒必须等级大于等于病毒,一个人只需支付一次通行证。


解题思路:二分+贪心+优先队列。二分天数,贪心判断,每次用可以杀除当前最高等级病毒中通行证所需最少的。杀mid个大的病毒,优先队列进行优化。


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>using namespace std;const int N = 100005;struct state {int id, val, pass;friend bool operator <(const state& a, const state& b) {return a.pass > b.pass;}
}p[N], bug[N];
int n, m, s;bool cmp(const state& a, const state& b) {return a.val > b.val;
}void init() {scanf("%d%d%d", &n, &m, &s);for (int i = 0; i < m; i++) {scanf("%d", &bug[i].val); bug[i].id = i;}for (int i = 0; i < n; i++) scanf("%d", &p[i].val);for (int i = 0; i < n; i++) {scanf("%d", &p[i].pass);p[i].id = i;}sort(p, p + n, cmp);sort(bug, bug + m, cmp);
}bool judge(int k) {int a = 0, b = 0, sum = s;priority_queue<state> que;while (b < m) {while (a < n) {if (p[a].val >= bug[b].val) que.push(p[a]);else break;a++;}if (que.empty()) return false;state c = que.top(); que.pop();if (sum < c.pass) return false;sum -= c.pass;b += k;}return true;	
}void put(int k) {int a = 0, b = 0;int ans[N];priority_queue<state> que;while (b < m) {while (a < n) {if (p[a].val >= bug[b].val) que.push(p[a]);else break;a++;}state c = que.top(); que.pop();int t = min(m, b + k);for (int i = b; i < t; i++)ans[bug[i].id] = c.id + 1;b += k;}printf("%d", ans[0]);for (int i = 1; i < m; i++) printf(" %d", ans[i]);printf("\n");
}void solve() {if (!judge(m))  {printf("NO\n"); return;}int l = 0, r = m;while (l < r) {int mid = (l + r) / 2;if (judge(mid)) r = mid;else l = mid + 1;}printf("YES\n");put(r);
}int main() {init();solve();return 0;
}


这篇关于CodeForces 337B Preparing for the Contest(二分+贪心+优先队列)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1032048

相关文章

hdu1180(广搜+优先队列)

此题要求最少到达目标点T的最短时间,所以我选择了广度优先搜索,并且要用到优先队列。 另外此题注意点较多,比如说可以在某个点停留,我wa了好多两次,就是因为忽略了这一点,然后参考了大神的思想,然后经过反复修改才AC的 这是我的代码 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2431 poj 3253 优先队列的运用

poj 2431: 题意: 一条路起点为0, 终点为l。 卡车初始时在0点,并且有p升油,假设油箱无限大。 给n个加油站,每个加油站距离终点 l 距离为 x[i],可以加的油量为fuel[i]。 问最少加几次油可以到达终点,若不能到达,输出-1。 解析: 《挑战程序设计竞赛》: “在卡车开往终点的途中,只有在加油站才可以加油。但是,如果认为“在到达加油站i时,就获得了一

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta