【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

题干:

In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be

.

Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.

Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes .

题目大意:

给出n个物品,每个物品有两个属性a和b,选择n-k个元素,询问\frac{\sum ai }{\sum bi }的最大值。

1<=n<=1000,0<=k<n,0<=ai<=bi<=1000000000。

解题报告:

   典型的分数规划。

  首先答案是符合单调性的,而\frac{\sum ai }{\sum bi } \geq x就等价于\sum ai - x * \sum bi \geq 0

所以我们发现二分完把ai-x*bi排序后把最大的n-k个选出来就行了。

AC代码:(80ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,k;
const double eps = 1e-5;
double qq[MAX];
struct Node {double a,b;
} node[MAX];
bool ok(double x) {double res = 0;for(int i = 1; i<=n; i++) qq[i] = node[i].a - x * node[i].b;sort(qq+1,qq+n+1); for(int i = n; i>=k+1; i--) res += qq[i];return res >= 0;
}
int main()
{while(~scanf("%d%d",&n,&k) && n+k) {for(int i = 1; i<=n; i++) scanf("%lf",&node[i].a);for(int i = 1; i<=n; i++) scanf("%lf",&node[i].b);double l = 0,r = 1e9;double mid = (l+r)/2;while(l+eps < r) {mid = (l+r)/2;if(ok(mid)) l = mid;else r = mid;}printf("%d\n",(int)(round(l*100)));//mid-eps?}return 0 ;}

迭代法可以快很多:(30ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,k;
const double eps = 1e-5;
double qq[MAX],aa,bb;
struct Node {double a,b;
} node[MAX];
struct NN {double a,b,qq;
} q[MAX];
bool cmp(NN a,NN b) {return a.qq < b.qq;
}
double ok(double x) {double res = 0;aa=bb=0;for(int i = 1; i<=n; i++) q[i].qq = node[i].a - x * node[i].b,q[i].a = node[i].a,q[i].b = node[i].b;sort(q+1,q+n+1,cmp); for(int i = n; i>=k+1; i--) aa += q[i].a,bb += q[i].b;return aa/bb;
}
int main()
{while(~scanf("%d%d",&n,&k) && n+k) {for(int i = 1; i<=n; i++) scanf("%lf",&node[i].a);for(int i = 1; i<=n; i++) scanf("%lf",&node[i].b);double l = 0,r = 1e9;double mid = (l+r)/2,ans = -1;while(fabs(ans-mid) >= eps) {ans = mid;mid = ok(mid);}printf("%d\n",(int)(round(ans*100)));//mid-eps?}return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440781.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

重写过的url 怎么获取当前页面url java_网站URL重写(Java UrlRewrite 的使用)

现在大部分的网站和商城都会使用到URL重写&#xff0c;接触到这个&#xff0c;也是因为正在做的电子商务商城。URL重写&#xff0c;是将原有的URL采用另一种规则来显示&#xff0c;使得用户方便访问同时也屏蔽一些信息。在此说下它的好处&#xff0c;在开发过程中&#xff0c;经…

Java行业情景分析_Java 设计模式情景分析——单例模式

单例模式可以说是应用最广的模式之一&#xff0c;在应用单例模式时&#xff0c;单例对象的类必须保证只有一个实例存在&#xff0c;而且可以自行实例化并向整个系统提供这个实例。一般在不能自由构造对象的情况下&#xff0c;就会使用单例设计模式&#xff0c;例如创建一个对象…

php实现播放直播_PHP直播技术分享(一:实现直播)

推流服务器采用的是自搭的推流服务器 , 自己玩玩 做外包项目还是去搞七牛云/阿里这样的服务器吧,开始bb-----1:技术栈image.jpeg2:开发中业务(1)主播申请时创建个秘钥 , 这个秘钥随时字符串即可到时候根据字符串找到拉流的直播位置存数据库包括推流地址image.png3:配置nginx-rt…

php获取location,php获取header[‘location’]信息常见问题

15/01/31本文关键字: 302, header, location//初始化url信息$host “#8221;;$url$host.”l/rzTf7ap2viw/&iid222004556&resourceId0_04_05_99/v.swf”;//按照字段获取header响应信息$headers get_headers($url, TRUE);//获取这个土豆的302跳转地址$u302 $headers[“Lo…

php对联广告,html左右对联代码 cms网站对联广告html代码

实现网页左右两侧居中的对联广告代码你只记得自己有好多朋友却从没想过小编只有你一人。无标题文档 #left{ width:200px;height:450px; position:absolute; left:0px; background:red; border:1px #000 solid} #right{width:200px;height:450px; position:absolute; right:0px;…

php 分页 url重写 分页问题,解决千古难题,wordpress分页URL问题,wordpress给分页加链接...

原本的wordpress文章如果分页会成如下的结构&#xff1a;http://www.xyqzmt.cn/1.html/2通常固定链接都是这样的结构&#xff0c;设为/%postname%.html或者/%post_id%.html 以前我一直无法解决如上的问题&#xff0c;最后放弃挣扎&#xff0c;如果遇到很长的文章全都放在一个页…

【牛客 - 181F】子序列(容斥,组合数,费马小定理)

题干&#xff1a; 题目描述 给出一个长度为n的序列&#xff0c;你需要计算出所有长度为k的子序列中&#xff0c;除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T&#xff0c;表示数据组数。 对于每组数据&#xff0c;第一行两个整数N&#xff0c;k&#…

【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干&#xff1a; Lets denote as the number of bits set (1 bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, an…

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…

【CodeForces - 1020A】New Building for SIS(模拟)

题干&#xff1a; You are looking at the floor plan of the Summer Informatics Schools new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get …

【CodeForces - 1020B】Badge(模拟,图,环)

题干&#xff1a; In Summer Informatics School, if a student doesnt behave well, teachers make a hole in his badge. And today one of the teachers caught a group of nn students doing yet another trick. Lets assume that all these students are numbered from …

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…

【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/I 来源&#xff1a;牛客网 现在处女座顺利的完成了测验&#xff0c;处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。 更具体的&#xff0c…

linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

原标题&#xff1a;你可能不知道的 即时监控 Linux 使用者执行指令的三种方法这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令。Linux 的 root 管理者可对系统进行任何的管理与操作&#xff0c;如果想要即时监控特定使用者在主机上所执行的指令&am…

【POJ - 2728】Desert King (最有比率生成树,分数规划)

题干&#xff1a; David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be…