Loj #149. 01 Score Planning (01 Score Planning Template Question)

link

Title:

Insert picture description here

answer:

For the detailed solution, see here.
Here is a point. The eps must be small enough. When I started with 1e-5, it passed 90% of the data, and it was enough to open 1e-7.

Code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
    
    
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
   return s*w;
}
double eps=1e-7;
const int maxn=1e5+9;
double a[maxn],b[maxn];
double dis[maxn];
int n,k;
bool check(double mid)
{
    
    
	double ans=0;
	for(int i=1;i<=n;i++)dis[i]=a[i]-mid*b[i];
	sort(dis+1,dis+1+n,greater<double>());
	for(int i=1;i<=k;i++)ans+=dis[i];
	//for(int i=n;i>n-k;i--)ans+=dis[i];
	if(ans>eps)return 0;
	return 1;
}
int main()
{
    
    
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];
	double l=0,r=1000;
	while(r-l>eps)
	{
    
    
		double mid=(l+r)/2;
		if(check(mid))r=mid;
		else l=mid;
	}
	printf("%.4f",l);
	return 0;
}

[POJ2976]Dropping tests

If you change the code slightly for this question, it will be OK. This question is nk

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/115142251