hdu 6180贪心
题意:有m个工程,一台机器在同一时间只能运行一个工程,告诉你每个工程的起始时间和结束时间,求出最少要多少个机器以及最小的机器总运行时间(机器开始了就不能停了,直到用完该台机器才停止)。
题解:由于这里可以使用多台机器,那么我们用起点排序也能够得到最小的k。(对比了下典型的区间调度问题,那个问题由于只有一台机器,所以必须用结束时间排序——尽早结束可以有更多的机会接触更多的时间区间)。然后题目要求最小的工作时间,这里我们只要保证一台机器运行的两个工程之间的时间间隔最小就可以了,也是一个贪心。一开始我用一个队列来维护机器的结束但是这是不行的,因为在判断是否需要新引入一台机器的时候,我们希望队列的首位为结束时间最早的机器;而当我们计算时间的时候,我们希望能有一个符合条件的结束时间最晚的机器来继续现在的工程。所以我们需要两个队列来维护,一个工作队列,一个空闲队列。
ac代码:
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <functional>
#define N 100010
#define LL __int64
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9 + ;
struct node {
LL st, en;
}thing[N];
bool cmp1(node a, node b) {
if (a.st == b.st) {
return a.en < b.en;
}
return a.st < b.st;
}
int main() {
cin.sync_with_stdio(false);
int T;
int n;
cin >> T;
while (T--) {
cin >> n;
priority_queue<LL>p;//空闲集合
priority_queue<LL, vector<LL>, greater<LL> >q;//工作集合
for (int i = ; i < n; i++) {
cin >> thing[i].st >> thing[i].en;
}
LL ans = , sum = ;
sort(thing, thing + n, cmp1);
for (int i = ; i < n; i++) {
node now = thing[i];
while ((!q.empty()) && q.top() <= now.st) {
p.push(q.top());
q.pop();
}
if (!p.empty()) {
int num = p.top();
p.pop();
sum += now.en - num;
q.push(now.en);
}
else {
ans++;
sum += now.en - now.st;
q.push(now.en);
}
}
cout << ans << " " << sum << endl;
}
return ;
}
hdu 6180贪心的更多相关文章
- 2017多校第10场 HDU 6180 Schedule 贪心,multiset
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6180 题意:给了一些任务的开始时间和终止时间,现在让我们安排k台及机器,让这些任务在k太机器上最小,并 ...
- HDU - 6180:Schedule(简单贪心)
There are N schedules, the i-th schedule has start time s i si and end time e i ei (1 <= i < ...
- Schedule HDU - 6180 (multiset , 贪心)
There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). Ther ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
- hdu 4982 贪心构造序列
http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...
- HDU 2307 贪心之活动安排问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- arcgis python 获得arcgis安装版本和安装位置
import arcpy print(arcpy.GetInstallInfo()['Version']) 和获得ArcGIS版本和安装位置 import arcpy # Use the dictio ...
- 基于python的人脸识别(检测人脸、眼睛、嘴巴、鼻子......)
本文链接:https://blog.csdn.net/James_Ray_Murphy/article/details/79209172 import numpy as np import cv2 # ...
- 总结解决 Android-Studio 编译耗时(好久、太长)问题
首先通过搜索有关Android-Studio 编译耗时(好久.太长)问题的博客,速度确实有所改善. 一.暂时解决 Android-Studio 编译耗时(好久.太长)问题 本文链接:https://b ...
- XListView实现上拉加载下拉刷新
package com.loaderman.androiddemo; import android.content.Context; import android.util.AttributeSet; ...
- SSD论文学习
SSD: Single Shot MultiBox Detector——目标检测 参考https://blog.csdn.net/u010167269/article/details/52563573 ...
- Qt编写自定义控件41-自定义环形图
一.前言 自定义环形图控件类似于自定义饼状图控件,也是提供一个饼图区域展示占比,其实核心都是根据自动计算到的百分比绘制饼图区域.当前环形图控件模仿的是echart中的环形图控件,提供双层环形图,有一层 ...
- LeetCode_191. Number of 1 Bits
191. Number of 1 Bits Easy Write a function that takes an unsigned integer and return the number of ...
- [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:1
带有sudo 权限执行就可以了
- MapReduce\Tez\Storm\Spark四个框架的异同
1) MapReduce:是一种离线计算框架,将一个算法抽象成Map和Reduce两个阶段进行 处理,非常适合数据密集型计算. 2) Spark:MapReduce计算框架不适合迭代计算和交互式计算, ...
- Jquery Ajax WebService
仅供参考 ws.aspx 代码 <script type="text/javascript"> $(function () { $.ajax({ type: " ...