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> # ...
随机推荐
- Kafka介绍及安装部署
本节内容: 消息中间件 消息中间件特点 消息中间件的传递模型 Kafka介绍 安装部署Kafka集群 安装Yahoo kafka manager kafka-manager添加kafka cluste ...
- Spark-2.0原理分析-shuffle过程
shuffle概览 shuffle过程概览 shuffle数据流概览 shuffle数据流 shuffle工作流程 在运行job时,spark是一个stage一个stage执行的.先把任务分成stag ...
- A + B Problem II(大数加法)
一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...
- PAT Sign In and Sign Out[非常简单]
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- Java-单向链表算法
/** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main ...
- iOS开发需要学习哪些内容?
看图:
- PIMPL(一)
1 参考 <effective C++> 条款31:将文件间的编译关系降至最低 PIMPL Idiom: http://c2.com/cgi/wiki?PimplIdiom 2 什么是PI ...
- HDU 4500 小Q系列故事——屌丝的逆袭(简单题)
http://acm.hdu.edu.cn/showproblem.php?pid=4500 AC代码: #include<math.h> #include<stdio.h> ...
- redis windows版本下载
https://github.com/dmajkic/redis/downloads http://windows.php.net/downloads/pecl/snaps/redis/3.1.4rc ...
- 深入理解 Java 内存模型(一)- 内存模型介绍
深入理解 Java 内存模型(一)- 内存模型介绍 深入理解 Java 内存模型(二)- happens-before 规则 深入理解 Java 内存模型(三)- volatile 语义 深入理解 J ...