POJ 2976 Dropping tests(分数规划)
http://poj.org/problem?id=2976
题意:
给出ai和bi,ai和bi是一一配对的,现在可以删除k对,使得的值最大。
思路:
分数规划题,可以参考《挑战程序竞赛》第144页。
枚举答案x,然后去判断是否存在$\frac{\sum a[i]}{\sum b[i]}>=x$,现在把这个式子转换一下,变成$\sum a[i]-x*\sum b[i]>=0$,这样每次贪心选择前面最大的n-k个即可,判断和x的大小关系。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+;
const double eps=1e-; int n, k;
int a[maxn];
int b[maxn];
double tmp[maxn]; bool check(double x)
{
for(int i=;i<n;i++)
tmp[i]=((double)a[i]-x*b[i]);
sort(tmp,tmp+n);
double sum=;
for(int i=k;i<n;i++) sum+=tmp[i];
if(sum>=0.0) return true;
else return false;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
if(n== && k==) break;
for(int i=;i<n;i++) scanf("%d",&a[i]);
for(int i=;i<n;i++) scanf("%d",&b[i]); double ans=; //刚开始没给ans赋0,wa了很久。。可能会出现k=n的情况...
double l=,r=;
while(r-l>=eps)
{
double mid=(l+r)/2.0;
if(check(mid)) {ans=mid;l=mid;}
else r=mid;
}
printf("%d\n",(int)(ans*+0.5));
}
return ;
}
POJ 2976 Dropping tests(分数规划)的更多相关文章
- POJ - 2976 Dropping tests && 0/1 分数规划
POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- POJ 2976 Dropping tests 【01分数规划+二分】
题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2976 Dropping tests(01分数规划入门)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11367 Accepted: 3962 D ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- POJ 2976 Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions:17069 Accepted: 5925 De ...
- Poj 2976 Dropping tests(01分数规划 牛顿迭代)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, you take n t ...
- POJ 2976 Dropping tests【0/1分数规划模板】
传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...
- poj 2976 Dropping tests 0/1分数规划
0/1分数规划问题,用二分解决!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> # ...
随机推荐
- golang 的精髓--pipeline流水线,对现实世界的完美模拟
https://blog.golang.org/pipelines https://www.cnblogs.com/junneyang/p/6215785.html 简介 Go语言的并发原语允许开发者 ...
- resume 简历
1:uestc社会实践平台,sql,mapper.xml,,图片验证码,读写excel,excel和list,数据库的转化. 2:购物网站,全代码,平台搭建,服务发布,远程数据库连接,前端,搜索,支付 ...
- 最简单的css实现页面宽度自适应
<div class="rxs"> <div class="rxleft"> 第一段内容,可以是任何html标签 </div> ...
- testng入门教程15数据驱动
testng在类 里面的数据驱动 package driver_test; import org.testng.annotations.DataProvider; import org.testng. ...
- 用python与文件进行交互
一.文件处理 1.介绍 计算机系统:计算机硬件,操作系统,应用程序 应用程序无法直接操作硬件,通过操作系统来操作文件,进而读/写硬件中的文件. python打开文件过程: #打开 f=open('a. ...
- 摘要JSR168 PORLET标准手册汉化整理
本规范汉化资源搜集整理于网上并由我作了些修改和添加,主要为适应大陆的语辞.用语及其他未译之处. 由于本人于水平有限,如有错误,请各位高手指正:若有高见,希望不吝言辞,同为中国开源作项献. 特此严重感谢 ...
- kendo datetimepicker
@(Html.Kendo().DatePicker() .Name("yearCalendar") .Value(DateTime.Now) .Start(CalendarView ...
- Redis缓存穿透问题及解决方案
上周在工作中遇到了一个问题场景,即查询商品的配件信息时(商品:配件为1:N的关系),如若商品并未配置配件信息,则查数据库为空,且不会加入缓存,这就会导致,下次在查询同样商品的配件时,由于缓存未命中,则 ...
- 倒计时60s
- Eclipse 启动项目错误:class not found
其中,很可能的原因:项目存在编译错误,根本没有编译成功,没有生成class文件:可查看problems标签页查看具体错误.