ZOJ3068 POJ2976 Dropping tests,二分检索

本文介绍了一种使用二分查找算法解决特定类型数学问题的方法,并通过一个具体的例子进行了详细说明。该算法适用于求解一元函数的最优解,特别当函数关于变量单调递增或递减时更为有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方的解题报告可以看这里http://poj.org/showmessage?message_id=53194

我们可以看看如下推导


题目就变成了二分检索r


/*******************************************************************************
 # Author : Neo Fung
 # Email : neosfung@gmail.com
 # Last modified: 2012-06-10 10:19
 # Filename: ZOJ3068 POJ2976 Dropping tests.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
using namespace std;

const int kMAX=10010;
const double kEPS=10E-5;

int main(void)
{
#ifdef DEBUG  
	freopen("../stdin.txt","r",stdin);
	freopen("../stdout.txt","w",stdout); 
#endif  

	int n,m,ncase=1;
	double num[kMAX],den[kMAX],scores[kMAX];

	while(~scanf("%d%d",&n,&m) && (m+n))
	{
		for(int i=0;i<n;++i)
			scanf("%lf",&num[i]);
		for(int i=0;i<n;++i)
			scanf("%lf",&den[i]);


		double lhs=0,rhs=1.0;

		while(lhs+kEPS<rhs)
		{
			double x=(lhs+rhs)/2;
			for(int i=0;i<n;++i)
				scores[i]=num[i]-den[i]*x;
			sort(scores,scores+n);
			double total=0.0;
			for(int i=m;i<n;++i)
				total+=scores[i];
			if(total>=0.0)
				lhs=x;
			else
				rhs=x;
		}
		printf("%d\n",int(100*lhs+0.5));
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值