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的大小关系。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<sstream>
  6. #include<vector>
  7. #include<stack>
  8. #include<queue>
  9. #include<cmath>
  10. #include<map>
  11. #include<set>
  12. using namespace std;
  13. typedef long long ll;
  14. typedef pair<int,ll> pll;
  15. const int INF = 0x3f3f3f3f;
  16. const int maxn=+;
  17. const double eps=1e-;
  18.  
  19. int n, k;
  20. int a[maxn];
  21. int b[maxn];
  22. double tmp[maxn];
  23.  
  24. bool check(double x)
  25. {
  26. for(int i=;i<n;i++)
  27. tmp[i]=((double)a[i]-x*b[i]);
  28. sort(tmp,tmp+n);
  29. double sum=;
  30. for(int i=k;i<n;i++) sum+=tmp[i];
  31. if(sum>=0.0) return true;
  32. else return false;
  33. }
  34.  
  35. int main()
  36. {
  37. //freopen("in.txt","r",stdin);
  38. while(~scanf("%d%d",&n,&k))
  39. {
  40. if(n== && k==) break;
  41. for(int i=;i<n;i++) scanf("%d",&a[i]);
  42. for(int i=;i<n;i++) scanf("%d",&b[i]);
  43.  
  44. double ans=; //刚开始没给ans赋0,wa了很久。。可能会出现k=n的情况...
  45. double l=,r=;
  46. while(r-l>=eps)
  47. {
  48. double mid=(l+r)/2.0;
  49. if(check(mid)) {ans=mid;l=mid;}
  50. else r=mid;
  51. }
  52. printf("%d\n",(int)(ans*+0.5));
  53. }
  54. return ;
  55. }

POJ 2976 Dropping tests(分数规划)的更多相关文章

  1. POJ - 2976 Dropping tests && 0/1 分数规划

    POJ - 2976 Dropping tests 你有 \(n\) 次考试成绩, 定义考试平均成绩为 \[\frac{\sum_{i = 1}^{n} a_{i}}{\sum_{i = 1}^{n} ...

  2. 二分算法的应用——最大化平均值 POJ 2976 Dropping tests

    最大化平均值 有n个物品的重量和价值分别wi 和 vi.从中选出 k 个物品使得 单位重量 的价值最大. 限制条件: <= k <= n <= ^ <= w_i <= v ...

  3. POJ 2976 Dropping tests 【01分数规划+二分】

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

  4. POJ 2976 Dropping tests(01分数规划入门)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11367   Accepted: 3962 D ...

  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分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:17069   Accepted: 5925 De ...

  7. Poj 2976 Dropping tests(01分数规划 牛顿迭代)

    Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, you take n t ...

  8. POJ 2976 Dropping tests【0/1分数规划模板】

    传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...

  9. poj 2976 Dropping tests 0/1分数规划

    0/1分数规划问题,用二分解决!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> # ...

随机推荐

  1. go-001-环境部署,IDEA插件

    一.下载安装 https://golang.org/dl/ 下载之后安装即可 官网地址:https://golang.org/ 1.1.mac上安装go 1.安装Homebrew 安装命令: ruby ...

  2. Spark-2.0原理分析-shuffle过程

    shuffle概览 shuffle过程概览 shuffle数据流概览 shuffle数据流 shuffle工作流程 在运行job时,spark是一个stage一个stage执行的.先把任务分成stag ...

  3. hdu1671Phone List(字典树)

    #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h&g ...

  4. 因子分析(Factor Analysis)

    原文地址:http://www.cnblogs.com/jerrylead/archive/2011/05/11/2043317.html 1 问题 之前我们考虑的训练数据中样例的个数m都远远大于其特 ...

  5. CentOS6.5安装RHadoop

    1.首先安装依赖包(各个节点都要安装) [root@Hadoop-NN-01 ~]$ yum install gcc-gfortran #否则报”configure: error: No F77 co ...

  6. 4.keras实现-->生成式深度学习之用GAN生成图像

    生成式对抗网络(GAN,generative adversarial network)由Goodfellow等人于2014年提出,它可以替代VAE来学习图像的潜在空间.它能够迫使生成图像与真实图像在统 ...

  7. linux du df ls

    du可以查看文件和文件夹的大小,df主要查看数据库大小,查看分区的的磁盘使用情况,ls可以添加-h参数,方便我们查看文件大小 df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为 ...

  8. PKU2418_树种统计(map应用||Trie树)

    Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nu ...

  9. 《User Modeling with Neural Network for Review Rating Prediction》评论打分预测

    摘要: 传统的评分预测只考虑到了文本信息,没有考虑到用户的信息,因为同一个词 在不同的用户表达中是不一样的.同样good 一词, 有人觉得5分是good 有人觉得4分是good.但是传统的文本向量表达 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON HomMat2dRotate1

    zw版[转发·台湾nvp系列Delphi例程]HALCON HomMat2dRotate1 procedure TForm1.Button1Click(Sender: TObject);var img ...