hzau 1209 Deadline(贪心)
K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And an engineer can repair a bug per day. Each bug has a deadline A[i].
Question: How many engineers can repair all bugs before those deadlines at least? 1<=n<= 1e6. 1<=a[i] <=1e9
Input Description There are multiply test cases. In each case, the first line is an integer N , indicates the number of bugs. The next line is n integers indicates the deadlines of those bugs.
Output Description There are one number indicates the answer to the question in a line for each case.
Input 4 1 2 3 4
Output 1
排序,大于N的不用考虑,否则超时
贪心
#include <bits/stdc++.h>
using namespace std; const int MAXN = 1e6 + ; int a[MAXN];
int b[MAXN];
int tot; int main()
{
int n;
int i;
int j;
int maxB;
int lMaxB; while (~scanf("%d", &n)) {
int n2 = n;
for (i = ; i < n; ++i) {
scanf("%d", &a[i]);
if (a[i] >= n2) {
--i;
--n;
}
}
//cout << n << endl;
sort(a, a + n); tot = ;
b[tot++] = ;
for (i = ; i < n; ++i) {
maxB = -;
for (j = ; j < tot; ++j) {
if (b[j] < a[i] && b[j] > maxB) {
maxB = b[j];
lMaxB = j;
break;
}
} if (maxB == -) {
b[tot++] = ;
} else {
++b[lMaxB];
} } printf("%d\n", tot);
} return ;
}
但是为什么用set写超时呢,不是应该比数组遍历查找要快吗?
超时代码:
#include <bits/stdc++.h>
using namespace std; const int MAXN = 1e6 + ; int a[MAXN]; struct cmp {
bool operator()(int a, int b)
{
return a > b;
}
};
multiset<int, cmp> st; int main()
{
int n;
int i;
multiset<int, cmp>::iterator it;
int tmp; while (~scanf("%d", &n)) {
st.clear();
int n2 = n;
for (i = ; i < n; ++i) {
scanf("%d", &a[i]);
if (a[i] >= n2) {
--i;
--n;
}
}
//cout << n << endl;
sort(a, a + n); st.insert();
for (i = ; i < n; ++i) {
it = st.upper_bound(a[i]);
if (it == st.end()) {
st.insert();
} else {
tmp = *it + ;
st.erase(it);
st.insert(tmp);
}
} printf("%d\n", st.size()); } return ;
}
其实这题可以用 任务数 / 天数,向上取整得到最少需要的人数,取最大值
#include <bits/stdc++.h>
using namespace std; const int MAXN = 1e6 + ; int maxA;
int b[MAXN];//b[i]保存当天要完成的任务数
int sum[MAXN];//sum[i]代表之前一共要完成任务数 int main()
{
int n;
int i;
int a;
int ans; while (~scanf("%d", &n)) { memset(b, , sizeof(b));
for (i = ; i < n; ++i) {
scanf("%d", &a);
if (a > n) {
continue;
}
++b[a];
if (a > maxA) {
maxA = a;
}
}
//cout << n << endl; sum[] = ;
ans = ;//最小为1个工人...//初始化为0不对。因为有可能所有日期都大于n,那么结果应该为1,而不是0
for (i = ; i <= maxA; ++i) {
sum[i] = sum[i - ] + b[i];
ans = max(ans, (sum[i] + i - ) / i);
} printf("%d\n", ans);
} return ;
}
hzau 1209 Deadline(贪心)的更多相关文章
- HZAU 18——Array C——————【贪心】
18: Array C Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 586 Solved: 104[Submit][Status][Web Boar ...
- POJ 1456(贪心)
#include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- BZOJ_1620_[Usaco2008_Nov]_Time_Management_时间管理_(二分+贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1620 N个工作,每个工作其所需时间,及完成的Deadline,问要完成所有工作,最迟要什么时候 ...
- [BZOJ 1029] [JSOI2007] 建筑抢修 【贪心】
题目链接:BZOJ - 1029 题目分析 使用一种贪心策略. 现将任务按照deadline从小到大排序. 然后枚举每一个任务,如果当前消耗的时间加上完成这个任务的时间不会超过这个任务的deadlin ...
- HDU 1789 Doing Homework again(贪心)
在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...
- hdu--1798--Doing Homework again(贪心)
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- POJ-1456 Supermarket 销售商品【贪心】+【并查集】
题目链接:http://poj.org/problem?id=1456 题目大意: 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售处,就能得到相应的利润,并且销售该商品需要1天 ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
随机推荐
- Linux介绍和基本命令
Linux是什么? 就是运行在硬件之上的一组软件,主要控制内核和系统调用这2个层面为上层应用软件提供各种接口,并高效的控制硬件资源,与window一样是一种操作系统 Linux的创始人是林纳斯-托瓦兹 ...
- 测试开发面试的Linux面试题:常用命令
Hello,大家好上次给大家介绍了vim使用方法,今天来给大家讲一讲linux系统文件命令 (1)Linux的文件系统目录配置要遵循FHS规范,规范定义的两级目录规范如下: /home ...
- 李治军老师操作系统课程资源分享(视频+pdf)
最近别人推荐,看看了哈工大的李治军老师主讲的操作系统,李治军老师通过linux0.11内核源码的讲解,学习了很多,更加形象了解了理论知识. 分享给大家,有pdf 链接:https://pan.baid ...
- C#类和结构(1)
1.结构功能特性? 实现代码? 结构用struct关键字定义的,与类类似,但有本质区别.结构实质是一个值类型,它不需要对分配的. 结构的特性: (1).结构作为参数传递时,是值传递. (2).结构的构 ...
- requirejs源码分析: define 方法
define = function (name, deps, callback) { var node, context; //Allow for anonymous modules ...
- 每天一个Linux命令(48)ping命令
ping命令用来测试主机之间网络的连通性. (1)用法: 用法: ping [参数] [主机名或IP地址] (2)功能: 功能: 确定网络和各外部主机的状态 ...
- $SublimeText2常用快捷键
1.删除一行:ctrl + shift + K2.替换:ctrl + H3.设置书签:Ctrl+F2设置书签F2 下一个书签Shift+F2上一个书签4.查找:ctrl + F 查找F3 查找下一个s ...
- hostname -f 失败解决办法
$ hostname fzk $ uname -n fzk 当 hostname -f 时报错:未搜索到主机名 产生这个原因时因为 /etc/hosts和/etc/sysconfig/network ...
- opencv manager package was not found 解决办法
http://blog.csdn.net/zjck1995/article/details/50358817 从网上好不容易找到的一个方法 1 解压OpenCV sdk 压缩包 2 eclipse 导 ...
- 【FAQ系列】Relay log 导致复制启动失败
今天在使用冷备份文件重做从库时遇到一个报错,值得研究一下. 版本:MySQL5.6.27 一.报错现象 dba:(none)> start slave; ERROR (HY000): Slave ...