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(贪心)的更多相关文章

  1. HZAU 18——Array C——————【贪心】

    18: Array C Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 586  Solved: 104[Submit][Status][Web Boar ...

  2. POJ 1456(贪心)

    #include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...

  3. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. BZOJ_1620_[Usaco2008_Nov]_Time_Management_时间管理_(二分+贪心)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1620 N个工作,每个工作其所需时间,及完成的Deadline,问要完成所有工作,最迟要什么时候 ...

  5. [BZOJ 1029] [JSOI2007] 建筑抢修 【贪心】

    题目链接:BZOJ - 1029 题目分析 使用一种贪心策略. 现将任务按照deadline从小到大排序. 然后枚举每一个任务,如果当前消耗的时间加上完成这个任务的时间不会超过这个任务的deadlin ...

  6. HDU 1789 Doing Homework again(贪心)

    在我上一篇说到的,就是这个,贪心的做法,对比一下就能发现,另一个的扣分会累加而且最后一定是把所有的作业都做了,而这个扣分是一次性的,所以应该是舍弃扣分小的,所以结构体排序后,往前选择一个损失最小的方案 ...

  7. hdu--1798--Doing Homework again(贪心)

    Doing Homework again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. POJ-1456 Supermarket 销售商品【贪心】+【并查集】

    题目链接:http://poj.org/problem?id=1456 题目大意: 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售处,就能得到相应的利润,并且销售该商品需要1天 ...

  9. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

随机推荐

  1. python函数回顾:next()

    描述 next() 返回迭代器的下一个项目. 语法 next(iterator[, default]) 参数说明: iterator -- 可迭代对象 default -- 可选,用于设置在没有下一个 ...

  2. Python中的魔术(双下划线'__xxx__')方法详解

    介绍 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中 ...

  3. OC、C#与JAVA语法特点一些异同(差集&交集)

    C#对JAVA: 1.扩展方法 2.部分类 3.动态对象 4.匿名返回类型 5.表达式树 6.Linq 7.没有函数指针,委托,事件的直接提供方式 8.JAVA接口不规定以I开头,这个很烂的思想! J ...

  4. 007-sql整体概述

    一.概述 sql基础:数据库.数据表.行.列.关系 查询: Select 字段1,字段2,* from 表 where 条件 去除重复:Distinct 必须放在所有列前面 区间语句:BETWEEN ...

  5. 20170421 F110 常见问题

    F110常見問題以及處理方式 1. Vendor中沒有與F110中相同的Payment method 解決辦法: 在Vendor主檔中維護Payment method 2. 結報被Block 解決辦法 ...

  6. Summaries On Java

    @1:== 和 equals(): ==用于比较引用和比较基本数据类型时具有不同的功能: 比较基本数据类型:如果两个值相同,则结果为true. 比较引用:如果引用指向内存中的同一对象,结果为true( ...

  7. 每天一个Linux命令(56)yum命令

          用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级.     (1)用法:     用法:  yum  [参数] [软件名]     (2)功能:     功能:  yum ...

  8. 每天一个Linux命令(38)top命令

     top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.       (1)用法:       用法:  top  [参数] top是 ...

  9. $git学习总结系列(2)——远程仓库

    本文主要介绍git本地仓库和GitHub远程仓库之间的交互和数据传输. 注:首先需要到github.com上注册一个账号. 1. 添加本地SSH Key到GitHub 要向GitHub远程仓库推送代码 ...

  10. qt的登录设置(转)

    1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...