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. 斯坦福大学Andrew Ng - 机器学习笔记(1) -- 单变量&多变量线性回归

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  2. Selenium获取input输入框中值的几种方法

    1.selenium 自带的方法 get_attribute('value') driver.find_element_by_name("f[url]").get_attribut ...

  3. End to End 端到端

    在DeepLearning的文章中有看到end2end一次,作者们似乎都比较喜欢这个end2end learning的方式.那么到底啥是end2end? 找了一下相关论文,没找到专门讲这个概念的,看来 ...

  4. Java基础—运算符(转载)

    转载自:Java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运 ...

  5. mysql分组查询报错

    执行sql group by查询时报错 SELECT id from userz GROUP BY username Expression #1 of SELECT list is not in GR ...

  6. 详尽全面的matlab绘图教程

    Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...

  7. python cookboo 文件与IO 函数

    写出文本数据 g = open('test.txt', 'rt', newline='',encoding = 'utf-8', errors='replace') t是windows平台特有的所谓t ...

  8. php一些常用功能封装

    //二分查找 function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval(($low + $hi ...

  9. springboot-FilterRegistrationBean

    主要用来对servlet filter进行自定义,比如设置order. 先写一个普通的filter: public class FilterDemo implements Filter { priva ...

  10. $.queue() 与 $.dequeue() -- 队列

    JQuery 运用队列为动画模块服务,但好像它应该有更多用处,我觉得的,那试试就知道咯. 简单的来讲,它就是形成队列和出列, 也就因此可以进行很有规律的回调和延时了呀(暂停感觉有难度),当然这就是后面 ...