POJ 3104&&POJ 2976 继续二分搜索。。。

来源:互联网 发布:windows 定时重启任务 编辑:程序博客网 时间:2024/05/19 13:56
Drying
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8573 Accepted: 2170

Description

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases byk this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line containsai separated by spaces (1 ≤ ai ≤ 109). The third line containsk (1 ≤ k ≤ 109).

Output

Output a single integer — the minimal possible number of minutes required to dry all clothes.

Sample Input

sample input #132 3 95sample input #232 3 65

Sample Output

sample output #13sample output #22题意是说N件衣服上各自有AI滴水,如果用烘干机来烘烤,则每分钟可以烘干K滴水,自然晾干是每分钟烘干1滴水,问最少要多少时间可以让衣服上的水全部烘干。思路:枚举一个时间mid,如果衣服中的水量大于mid那么就去烘干它,否则自然晾干。。接着我们继续假设对于每一件要用烘干机来烘干的衣服,自然烘干的时间为T1,烘干机烘干的时间为T2,有T1+T2=mid, T1*1+T2*K>=AI,联立解得用烘干机烘干的时间应该满足T2=ceil((a[i]-t)/(k-1)),剩下就是注意细节了,比如如果K=1的话,直接输出含水量的最大值,然后这个时间神马的最好全都用LL表示。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;long long n,k;long long a[100005];bool C(long long  t){   long long  ht=0;    for(int i=0;i<n;i++)    {        if(a[i]>t)        {            ht+=ceil(1.0*(a[i]-t)/(k-1));        }    }    if(ht<=t)        return true;    else        return false;}int main(){    while(scanf("%I64d",&n)!=EOF)    {        long long  maxn=-999;        for(int i=0;i<n;i++)        {            scanf("%I64d",&a[i]);            if(a[i]>maxn)            {                maxn=a[i];            }        }        scanf("%I64d",&k);        if(k==1)        {           printf("%I64d\n",maxn);            continue;        }        long long l=0;        long long r=maxn;        long long ans=0;        while(l<=r)        {            long long mid=(l+r)/2;            long long sum=0;            if(C(mid))            {                ans=mid;                r=mid-1;            }            else            {                l=mid+1;            }        }        printf("%I64d\n",ans);    }    return 0;}

Dropping tests
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5894 Accepted: 2048

Description

In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be

.

Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.

Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes .

Input

The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ aibi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed.

Output

For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer.

Sample Input

3 15 0 25 1 64 21 2 7 95 6 7 90 0

Sample Output

83100

Hint

To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997).

题意是 给你N组 a[i]和b[i],并允许你删除N组中的K组,要让剩余的100*sigema(a[i])/b[i]最大。
思路:假定一个比率最大,则有100*sigema(a[i])/b[i]=r----->得到sigema(100*a[i]-r*b[i])=0时,r最大,通过这个式子,然后对sigema(100*a[i]-r*b[i])进行降序排序,
选取前N-K个,若最后的SUM>=0则符合条件,对r继续进行二分即可,按照题给的精度貌似1e-3就可以,我开了1e-4保险。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;struct node{double a;double b;double c;}t[10005];int n,k;bool cmp(node a,node b){    return a.c>b.c;}bool C(double x){    for(int i=1;i<=n;i++)    {        t[i].c=100.0*t[i].a-x*t[i].b;    }    sort(t+1,t+n+1,cmp);    double sum=0.0;    for(int i=1;i<=n-k;i++)    {        sum+=t[i].c;    }    return sum>=0.0;}int main(){    while(cin>>n>>k)    {        if(n==0&&k==0)            return 0;        for(int i=1;i<=n;i++)        {            cin>>t[i].a;        }        for(int i=1;i<=n;i++)        {            cin>>t[i].b;        }        double l=0,r=100.0,mid,ans;        while(r-l>0.00001)        {            mid=(l+r)/2.0;           if(C(mid))           {               l=mid;           }           else           {               r=mid;           }        }        printf("%.0lf\n",mid);    }    return 0;}


0 0
原创粉丝点击