题意:有n场考试,给出每场答对的题数a和这场一共有几道题b,求去掉k场考试后,公式.的最大值

解题关键:01分数规划,double类型二分的写法(poj崩溃,未提交)

或者r-l<=1e-3(右边是精度)

为什么v-xw>=0?(v/x>=x?)

ans要求的是最大值,我们定义:c(x)可以选择使得单位重量的价值>=x,最大值一定满足此函数,此函数关于x单调递减,可以求得一个最大值。求得的x的最大值即是ans。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#define maxn 1006
#define eps 1e-8
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,k;
double ans;
struct node{
double a,b;
}num[maxn];
bool cmp(const node &x,const node &y){
return x.a-x.b*ans>=y.a-y.b*ans;
}
bool check(double x){
ans=x;
sort(num+,num+n+,cmp);
double sum=;
for(int i=;i<=n-k;i++) sum+=num[i].a-x*num[i].b;
return sum>=-eps;
} double erfen(double l,double r){
while(r-l>eps){//for(int i=1;i<=100;i++)
double mid=(l+r)/;
if(check(mid)) l=mid;
else r=mid;
}
return r;
} int main(){
while(scanf("%d%d",&n,&k)!=EOF&&(n||k)){
for(int i=;i<=n;i++) scanf("%lf",&num[i].a);
for(int i=;i<=n;i++) scanf("%lf",&num[i].b);
double ans=erfen(,1.0*inf);
printf("%.0lf\n",*ans);//必须四舍五入
//printf("%d\n",(int)(100*ans+0.5));
}
return ;
}

2、Dinkelbach算法(原理上比二分更快,因为利用了当前直线所对应的解)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#define eps 1e-8
using namespace std;
typedef long long ll;
double a[],b[]; struct node{
double num;
int ord;
}d[]; bool cmp(node a,node b){
return a.num>b.num;
} int main(){
int n,k;
while(scanf("%d%d",&n,&k)&&(n||k)){
for(int i=;i<n;i++)scanf("%lf",&a[i]);
for(int i=;i<n;i++)scanf("%lf",&b[i]);
double l=0.0,ans;
while(){
ans=l;
for(int i=;i<n;i++){
d[i].num=a[i]-ans*b[i];
d[i].ord=i;
}
sort(d,d+n,cmp);
double p=0.0,q=0.0;
for(int i=;i<n-k;i++){
p+=a[d[i].ord];
q+=b[d[i].ord];
}
l=p/q;
if(fabs(ans-l)<eps)
break;
}
if(ans>)ans=;
if(ans<)ans=;
printf("%.0f\n",*ans);
}
return ;
}

[poj2976]Dropping tests(01分数规划,转化为二分解决或Dinkelbach算法)的更多相关文章

  1. POJ2976 Dropping tests —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. POJ2976 Dropping tests(01分数规划)

    题意 给你n次测试的得分情况b[i]代表第i次测试的总分,a[i]代表实际得分. 你可以取消k次测试,得剩下的测试中的分数为 问分数的最大值为多少. 题解 裸的01规划. 然后ans没有清0坑我半天. ...

  3. POJ2976 Dropping tests 01分数规划

    裸题 看分析请戳这里:http://blog.csdn.net/hhaile/article/details/8883652 #include<stdio.h> #include<a ...

  4. Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8176   Accepted: 2862 De ...

  5. POJ 2976 Dropping tests 01分数规划 模板

    Dropping tests   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6373   Accepted: 2198 ...

  6. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  7. $POJ$2976 $Dropping\ tests$ 01分数规划+贪心

    正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...

  8. POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))

    题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...

  9. 【POJ2976】Dropping tests - 01分数规划

    Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...

随机推荐

  1. 51nod 1732 LCS变形

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1732 1732 51nod婚姻介绍所 题目来源: 原创 基准时间限制:1 ...

  2. 51nod 1257 01分数规划/二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 1257 背包问题 V3 基准时间限制:3 秒 空间限制:1310 ...

  3. hive中order by,sort by, distribute by, cluster by的用法

    1.order by hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...

  4. ps-基础知识

    一.常用名词及几个常见控制面板. 二.工具栏中各工具的名称及使用方法. 三.新建文件的流程和注意事项. 四.光与色的基础知识

  5. syq小姐姐的分享的历年考试经验

    一>读题 10~20min浏览题目,把握题目方向和做题大致顺序 不要轻敌,最好先看完题目,大概掌握整套题的难度顺序再动手 仔细读题步骤: (1)文件名(也是检查的重点,绝对重要,注意区分l和1和 ...

  6. 14_ Component 游戏开发组件模式

    # component 不同作用的程序需要保持互相隔离 我们不想ai 物理 渲染 声音 等等功能 耦合在一起,像下面这样 ``` //bad if (collidingWithFloor() & ...

  7. uva11991(二分查找或map的应用)

    11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Easy Problem from Rujia Liu? Though R ...

  8. Spring转账业务_注解配置事物控制

    1.beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  9. LeetCode 251. Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...

  10. RabbitMQ用户角色及权限控制(不错)

    ########################用户角色####################### RabbitMQ的用户角色分类:none.management.policymaker.moni ...