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) ...
随机推荐
- Java判断整数溢出
开题报告第一版写完发给老师了,熬了两周终于搞出来了,等着被怼了之后再改吧.晚上选了Leetcode一道简单的题,整数反转,就是将一个int类型的数反转.原本确实很简单,最后出现个问题有意思--整数溢出 ...
- arcgis python 不知道一个工具怎么用
完整的工具帮助信息 import arcpy print(arcpy.Usage("Buffer_analysis")) print(arcpy.Usage("MakeF ...
- Restful API 架构与设计参考原则
1. 什么是RESTREST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的博 ...
- 使用python显示当前系统中的所有进程并关闭某一进程
环境: Windows 10操作系统 python idle 原理: 调用windows系统自带的命令task,该命令使用方式: 第一步.调用cmd命令行,显示当前系统中所有进程: Win+R-> ...
- Celery如何修复Python的GIL问题
小结: 1. Celery如何修复Python的GIL问题https://python.freelycode.com/contribution/detail/346 最近,我重读了Glyph写的Uny ...
- eclipse手动添加本地jar包到本地maven仓库
在使用maven进行构建项目时,有时候中央仓库不包含所需的jar包,就需要下载到本地后手动添加到本地仓库中.这里介绍下利用eclipse进行本地jar安装到maven本地仓库. 在Eclipse项目中 ...
- Java同步数据结构之ConcurrentHashMap
前言 这是Java并发包最后一个集合框架的数据结构,其复杂程度也较以往任何数据结构复杂的多,顾名思义ConcurrentHashMap是线程安全版本的HashMap,总所周知HashMap是非线程安全 ...
- LC 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- python下multiprocessing和gevent的组合使用
python下multiprocessing和gevent的组合使用 对于有些人来说Gevent和multiprocessing组合在一起使用算是个又高大上又奇葩的工作模式. Python的多线程受制 ...
- MySQL数据库之慢查询日志
一.开启慢查询日志 通过show global variables like '%slow%' #查看MySQL慢查询日志是否开启 [root@mysqlmaster01 ~]# mysql --lo ...