POJ 2976 Dropping tests (二分+贪心)
题意:给定 n 个分数,然后让你去年 m 个分数,使得把剩下的所有的分子和分母都相加的分数最大。
析:这个题并不是分子越大最后结果就越大,也不是整个分数越大,最后结果就越大的,我们可以反过来理解,要去掉 m 个分数,那么就是要选 n-m个分数,
那么就是 sigma(分子) / sigma(分母) 尽量大,那么最大是多大啊?这个我们可以通过二分来解决,也就是sigma(分子) / sigma(分母) >= x,
因为分子和分母都是正数,所以可以得到 sigma(分子) - sigma(分母)* x >= 0,也就是 sigma(分子 - x * 分母) >= 0(前n-m项),我们就可以按这个进行排序,
看看前 n-m 项成不成立。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
double c; struct Node{
double a, b;
bool operator < (const Node &p) const{
return a - b * c > p.a - p.b * c;
}
};
Node a[maxn]; bool judge(){
sort(a, a + n);
double ans = 0;
for(int i = 0; i < n-m; ++i) ans += a[i].a- a[i].b * c;
return ans >= 0.0;
} double solve(){
double l = 0, r = INF;
for(int i = 0; i < 100; ++i){
c = (l+r) / 2;
if(judge()) l = c;
else r = c;
}
return l;
} int main(){
while(scanf("%d %d", &n, &m) == 2 && n+m){
for(int i = 0; i < n ; ++i) scanf("%lf", &a[i].a);
for(int i = 0; i < n ; ++i) scanf("%lf", &a[i].b);
printf("%.f\n", solve()*100);
}
return 0;
}
POJ 2976 Dropping tests (二分+贪心)的更多相关文章
- POJ 2976 Dropping tests [二分]
1.题意:同poj3111,给出一组N个有价值a,重量b的物品,问去除K个之后,剩下的物品的平均值最大能取到多少? 2.分析:二分平均值,注意是去除K个,也就是选取N-K个 3.代码: # inclu ...
- 二分算法的应用——最大化平均值 POJ 2976 Dropping tests
最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...
- 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 【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: 6373 Accepted: 2198 ...
- 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:17069 Accepted: 5925 De ...
- POJ 2976 Dropping tests (0/1分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4654 Accepted: 1587 De ...
- Poj 2976 Dropping tests(01分数规划 牛顿迭代)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, you take n t ...
随机推荐
- async & await (转载)
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们 编程埋下了一些 ...
- .Net反射机制
现在谈.Net反射机制本不在计划中,因为本打算研究完设计模式后再去学习别的技术:但迫于设计模式系列一创建型之(抽象工厂模式)一章中遗留问题,才不得已在设计模式之游中插入本篇文章!签于本人对反射理解也不 ...
- linux rz sz命令
rz是receive zmodem的缩写,sz是send zmodem的缩写. 传输文件使用的是zmodem协议,所以叫zmodem. r和s是以服务器为主体的,服务器接收就是r,服务器发送就是s.
- git config --system --unset credential.helper 重新输入账号密码
检查本地配置$ git config --local -lcore.repositoryformatversion=0core.filemode=falsecore.bare=falsecore.lo ...
- Kubernetes TensorFlow 默认 特定 集群管理器
Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...
- thinkphp5 (最棒的php开源框架)
tp5的唯一可访问目录是public,即项目根目录: http://localhost/tp5/public/ 开发规范: 类库.函数文件统一以.php为后缀 类(命名和路径)和命名空间保持一致 类文 ...
- 常用BAPI list
2017-03-25 MD 主数据 1.创建物料主数据 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' 2.创建供应商 CALL METHOD VMD_EI_API=&g ...
- Java for LeetCode 093 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- Allegro PCB中添加汉字
注明出处:http://www.cnblogs.com/einstein-2014731/p/5650943.html Cadence用起来比AltiumDesigner要麻烦些,但是也更开放,更灵活 ...
- Listfragment 列表标题 显示内容
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...