(ZOJ) 3334 二分+贪心(二分double的写法)
2017-08-11 23:54
375 查看
这题的关键是想到二分医生们共同工作的时间,因为病人的check时间是可以被无限分割的只要保证同一时刻每一个病人只被一个医生check,同时每一个医生在一个时刻只check一个病人,因为医生工作的条件是要么所有医生一起工作,要么只有一个医生工作,所以要最大化医生共同工作的时间就可以最小化完成check的总时间,还有一点就是因为病人check时间可以被分割和组合,假设医生的共同工作时间为X,这每一个病人所占有的医生共同检查的时间是min(X, pi),
如果所有病人占有的共同检查的时间的和小于m * X,那就使得每位医生工作的时间达不到X,否则就是可以达到的,然后就这样二分即可。
还有这个题目使我第一次写二分double,还是有很多double的精度误差要好好考虑,还有二分的停止条件,以及如何缩小范围,这些还得继续学习。
Body Check
Time Limit: 1 Second Memory Limit: 32768 KB Special Judge
Due to the spread of H1N1 all over the world, the king of Kingdom atouSK is so afraid that he decides to ask all his people to do a body check, no matter how much money it will take.
Thus, the king calls up m of the best doctors across the kingdom to finish the task. The docotrs of Kingdom atouSK are selfish. If some of them are asked to work while others of them can take a rest, the docotrs who need to work will get angry and
make mistakes at their work time. However, there is one exception, when just one of them needs to work and the others can have a rest, the other doctors will be united and report to the king if the working doctor makes mistakes, so the working doctor needs
to do the job alone carefully (How poor!). Therefore, all the m docotrs need to work together or only one of them does the work at certain time.
There are n people in Kingdom atouSK, and it takes different people differnt time to finish the body check. A person can do parts of the check by one doctor and later move to
another doctor to continue the check. In other words, a person can do any part of the body check by any docotr at any time, all depending on the doctors' arrangement. But of course, a personCANNOT do the check by two or more doctors at the
same time! Suppose the chosen m docotrs are so excellent that they all work at the same efficiency, but they should be focused and each CANNOT check more than one people at the same time.
Though the king doesn't care about the money, he is so concerned about the time to finish all the checks, because the later all the checks finish, the more likely the disease will spread.
The king is too busy, so he asks you to calculate the shortest time needed to finish all the body checks.
Input
There may be multiple cases. In each case, there are two integers in the first line, m (0 < m <= 1000) and n (0 < n <= 1000). In the second line, there
are n positive real numbers, the ithone indicating the time needed for the ith person to finish the check, not exceeding 1000000.
Output
Output the shortest time needed to finish all the body checks in one line for each case.
Answer having a relative error less than 1e-9 will be accepted.
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#in
4000
clude <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const double eps = 1e-8;//改成1e-9之后会TLE,在遇到double是精度太高容易TLE
//因此处理double一定要注意
double num[1005];
int cmp(double x)
{
if(abs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
int main()
{
int m, n;
while(scanf("%d%d", &m, &n) != EOF)
{
double sum = 0.0;
for(int i = 0; i < n; ++i)
{
scanf("%lf", &num[i]);
sum += num[i];
}
if(m > n)
{
printf("%.4f\n", sum);
continue;
}
double l = 0.0, r = sum;
while(cmp(r - l))
{
double mid = (l + r) / 2;
double tmp = 0.0;
for(int i = 0; i < n; ++i)
tmp += min(mid, num[i]);
if(cmp(tmp - m * mid) < 0) r = mid;
else l = mid;
}
printf("%.4f\n", sum - (m - 1) * r);
}
return 0;
}
如果所有病人占有的共同检查的时间的和小于m * X,那就使得每位医生工作的时间达不到X,否则就是可以达到的,然后就这样二分即可。
还有这个题目使我第一次写二分double,还是有很多double的精度误差要好好考虑,还有二分的停止条件,以及如何缩小范围,这些还得继续学习。
Body Check
Time Limit: 1 Second Memory Limit: 32768 KB Special Judge
Due to the spread of H1N1 all over the world, the king of Kingdom atouSK is so afraid that he decides to ask all his people to do a body check, no matter how much money it will take.
Thus, the king calls up m of the best doctors across the kingdom to finish the task. The docotrs of Kingdom atouSK are selfish. If some of them are asked to work while others of them can take a rest, the docotrs who need to work will get angry and
make mistakes at their work time. However, there is one exception, when just one of them needs to work and the others can have a rest, the other doctors will be united and report to the king if the working doctor makes mistakes, so the working doctor needs
to do the job alone carefully (How poor!). Therefore, all the m docotrs need to work together or only one of them does the work at certain time.
There are n people in Kingdom atouSK, and it takes different people differnt time to finish the body check. A person can do parts of the check by one doctor and later move to
another doctor to continue the check. In other words, a person can do any part of the body check by any docotr at any time, all depending on the doctors' arrangement. But of course, a personCANNOT do the check by two or more doctors at the
same time! Suppose the chosen m docotrs are so excellent that they all work at the same efficiency, but they should be focused and each CANNOT check more than one people at the same time.
Though the king doesn't care about the money, he is so concerned about the time to finish all the checks, because the later all the checks finish, the more likely the disease will spread.
The king is too busy, so he asks you to calculate the shortest time needed to finish all the body checks.
Input
There may be multiple cases. In each case, there are two integers in the first line, m (0 < m <= 1000) and n (0 < n <= 1000). In the second line, there
are n positive real numbers, the ithone indicating the time needed for the ith person to finish the check, not exceeding 1000000.
Output
Output the shortest time needed to finish all the body checks in one line for each case.
Answer having a relative error less than 1e-9 will be accepted.
Sample Input
3 2 1.0 2.0 2 2 1.0 2.0 2 3 1.00 2.0 3.0
Sample Output
3.0000 2.0000 3.0000
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#in
4000
clude <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const double eps = 1e-8;//改成1e-9之后会TLE,在遇到double是精度太高容易TLE
//因此处理double一定要注意
double num[1005];
int cmp(double x)
{
if(abs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
int main()
{
int m, n;
while(scanf("%d%d", &m, &n) != EOF)
{
double sum = 0.0;
for(int i = 0; i < n; ++i)
{
scanf("%lf", &num[i]);
sum += num[i];
}
if(m > n)
{
printf("%.4f\n", sum);
continue;
}
double l = 0.0, r = sum;
while(cmp(r - l))
{
double mid = (l + r) / 2;
double tmp = 0.0;
for(int i = 0; i < n; ++i)
tmp += min(mid, num[i]);
if(cmp(tmp - m * mid) < 0) r = mid;
else l = mid;
}
printf("%.4f\n", sum - (m - 1) * r);
}
return 0;
}
相关文章推荐
- zoj-3963 Heap Partition(贪心+二分+树状数组)
- ZOJ 3820 Building Fire Stations (二分+贪心) 2014 牡丹江现场赛B
- 浙江14届省赛 F.Heap Partition(ZOJ 3963)[贪心][二分]
- ZOJ 3963 Heap Partition( 并查集 + 贪心 +二分 )
- zoj 2109 FatMouse' Trade简单的贪心 (注意double数组的排序问题)
- ZOJ 2002 Copying Books 二分 贪心
- ZOJ 3090 Assemble(二分+贪心)
- zoj 2968 Difference Game [贪心+二分]
- ZOJ 3908 Number Game (贪心+二分+multiset)
- zoj 3963 Heap Partition(并查集,贪心,二分)
- ZOJ 3334 Body Check (二分)
- zoj 3820 Building Fire Stations(贪心 + 二分)
- ZOJ 2002 Copying Books 二分 贪心
- ZOJ 3778 Talented Chef (贪心+二分)
- ZOJ 2968 Difference Game 【贪心 + 二分】
- HDU 4004 The Frog's Games 二分+贪心
- ZOJ-3987 Numbers 2017CCPC秦皇岛站G题 (二进制、贪心)
- UVaLive 3971 Assemble (水题二分+贪心)
- ZOJ2283 Challenge of Wisdom(DP+二分求最长非上升子序列)
- 【贪心+二分】codeforces C. Sagheer and Nubian Market