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