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 ;
}