Codeforces Round #390 (Div. 2)D Fedor and coupons
来源:互联网 发布:行知职业技术学校 编辑:程序博客网 时间:2024/06/06 09:31
题目大意:
给你n个区间,求k个区间的最大并区间长度。
题目解法:
贪心,不断想优先队列中保存内容,保证每次队列顶端的右值最大,不断修正左区间。最后按照最优的左端点和右端点,寻找答案就行。
代码:
#include "iostream"#include "cstdio"#include "math.h"#include "algorithm"#include "string"#include "string.h"#include "vector"#include "map"#include "queue"using namespace std;int n, k;struct Node {int left, right, id;friend bool operator <(Node a, Node b) {return a.right > b.right;}}node[300010];bool cmp(Node a, Node b) {if (a.left == b.left)return a.right > b.right;return a.left < b.left;}int main() {scanf("%d %d", &n, &k);for (int i = 1;i <= n;i++) {scanf("%d %d", &node[i].left, &node[i].right);node[i].id = i;}int maxnum = 0, ansleft, ansright;sort(node + 1, node + n + 1, cmp);priority_queue<Node>que;for (int i = 1;i <= n;i++) {int left = node[i].left;que.push(node[i]);if (que.size() > k)que.pop();int right = que.top().right;if (que.size() == k&&right - left + 1 > maxnum) {maxnum = right - left + 1;ansleft = left;ansright = right;}}printf("%d\n", maxnum);if (maxnum == 0) {for (int i = 1;i <= k;i++) {printf("%d ", i);}}else {for (int i = 1, j = 0;i <= n&&j < k;i++) {if (node[i].left <= ansleft&&node[i].right >= ansright) {printf("%d ", node[i].id);j++;}}}return 0;}
0 0
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2//754D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D - Fedor and coupons (贪心)
- Codeforces Round #390 (Div. 2)D Fedor and coupons
- Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- D. Fedor and coupons Codeforces Round #390 (Div. 2)(优先队列)
- Codeforces Round #267 (Div. 2) D. Fedor and Essay
- Codeforces Round #267 (Div. 2) D. Fedor and Essay
- Codeforces 754 D Fedor and coupons
- 【codeforces 754D】Fedor and coupons
- codeforces 754D. Fedor and coupons
- CodeForces 754D Fedor and coupons
- Codeforces 754 D Fedor and coupons
- Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
- codeforces 754D Fedor and coupons(优先队列)
- codeforces 754D Fedor and coupons (优先队列)
- 和最大连续子数组
- TPS和事务响应时间的关系、计算公式
- maven install时报:Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME
- JDK1.8环境下导入项目到Eclipse中报错,提示要移除“@Override” 注解,报错原因与解决方法
- python精句收集
- Codeforces Round #390 (Div. 2)D Fedor and coupons
- Spring的《XML显式装配bean》- XML自动装配
- android下载新版本并更新(DownLoadManager、HttpURLConnection)
- CentOS Oracle数据库每天自动备份
- Android:真机调试遇到的问题(INSTALL_FAILED_CANCELLED_BY_USER和INSTALL_FAILED_INSUFFICIENT_STORAGE)
- 求字符串的最小正周期
- Storm和Redis native的集成
- sonar增量问题报告
- 【最火的微信开发开源框架】推荐给开发者应该学会的有关微信开发的十个开源项目