POJ 2976 Dropping tests (01 score planning), poj2976

Source: Internet
Author: User
Tags acos

POJ 2976 Dropping tests (01 score planning), poj2976

Address: POJ 2976
The details of the 01 score plan are all here, portal.
First, the time of the split method was written as 110 ms.
The binary code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-8;const int MAXN=40000+10;int n, m;double d[2000], a[2000], b[2000];bool check(double L){        for(int i=0;i<n;i++){                d[i]=a[i]-L*b[i];        }        sort(d,d+n);        double tmp=0;        for(int i=m;i<n;i++){                tmp+=d[i];        }        return tmp>=0;}int main(){        int i;        while(scanf("%d%d",&n,&m)!=EOF&&n+m){                for(i=0;i<n;i++){                        scanf("%lf",&a[i]);                }                for(i=0;i<n;i++){                        scanf("%lf",&b[i]);                }                double low=0.0, high=1.0, mid;                while(high-low>eqs){                        mid=(low+high)/2;                        if(check(mid)){                                low=mid;                        }                        else high=mid;                }                printf("%.0f\n",low*100);        }        return 0;}

Next is the iteration method. The difference between the iteration method and the binary method is that the iteration method makes full use of the value of R. The time is greatly reduced. My iteration time is 47 ms.
The Code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-8;const int MAXN=40000+10;int n, m;struct node{        double a, b, d;}fei[2000];bool cmp(node f1, node f2){        return f1.d<f2.d;}int main(){        int i;        while(scanf("%d%d",&n,&m)!=EOF&&n+m){                for(i=0;i<n;i++){                        scanf("%lf",&fei[i].a);                }                for(i=0;i<n;i++){                        scanf("%lf",&fei[i].b);                }                double ans=0, tmp, p, q;                while(1){                        tmp=ans;                        for(i=0;i<n;i++){                                fei[i].d=fei[i].a-tmp*fei[i].b;                        }                        sort(fei,fei+n,cmp);                        p=q=0;                        for(i=m;i<n;i++){                                p+=fei[i].a;                                q+=fei[i].b;                        }                        ans=p/q;                        if(fabs(ans-tmp)<=eqs) break;                }                printf("%.0f\n",ans*100);        }        return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.