POJ2976 Dropping tests 二分搜索
来源:互联网 发布:nginx lua waf 安装 编辑:程序博客网 时间:2024/05/21 02:32
题目大意是,两对数列ai,bi 其中ai <= bi,现在可以从中取走K对,使得剩下的所有ai / bi 的和最大。
这题一开始拿到会以为直接对ai / bi的值进行排序后选取最大的那几个,但是实际上不是,可以自己举出反例。设C(x):=剩下所有ai / bi的和不小于x。此时,需要对ai - bi * x进行排序,从大到小依次选择求和,大于0的话就说明C(x)成立。
#include <stdio.h>#include <vector>#include <math.h>#include <string.h>#include <string>#include <iostream>#include <queue>#include <list>#include <algorithm>#include <stack>#include <map>using namespace std;struct SCORE{int ivalue;int bvalue;};SCORE values[1001];double X = 0;int compp(const void* a1, const void* a2){SCORE* score1 = (SCORE*)a1;SCORE* score2 = (SCORE*)a2;double dis = score2->ivalue - score2->bvalue * X - (score1->ivalue - score1->bvalue * X);if (dis > 0){return 1;}else if (dis < 0){return -1;}elsereturn 0;}bool CC(double x, int n, int k){int select = n - k;X = x;qsort(values, n, sizeof(SCORE), compp);long double sum = 0;for (int i = 0; i < select; i++){sum += (values[i].ivalue - values[i].bvalue * x);}if (sum - 0.0 >= 0){return true;}elsereturn false;}int main(){#ifdef _DEBUGfreopen("d:\\in.txt", "r", stdin);#endifint n, k;while (scanf("%d %d", &n, &k) != EOF){if (n == 0 && k ==0){break;}for (int i = 0; i < n; i++){scanf("%d", &values[i].ivalue);}for (int i = 0; i < n; i++){scanf("%d", &values[i].bvalue);}double l = 0;double r = 1.0;for (int i = 0; i < 100; i++){double mid = (l + r) / 2;if (CC(mid, n, k)){l = mid;}elser = mid;}printf("%d\n", (int)(l * 100 + 0.5));}return 1;}
0 0
- POJ2976 Dropping tests 二分搜索
- [二分]poj2976 Dropping tests
- poj2976 Dropping tests 二分
- ZOJ3068 POJ2976 Dropping tests,二分检索
- POJ2976 Dropping tests 最大化平均值(二分)
- POJ2976 Dropping tests (最大化平均值/二分)
- POJ2976-01分数规划&二分-Dropping tests
- poj2976 Dropping tests(二分答案)
- POJ2976--Dropping tests
- POJ2976-Dropping tests
- [POJ2976] Dropping tests
- POJ2976-Dropping tests
- poj2976(二分,最大值平均化模板)Dropping tests
- POJ2976:Dropping tests(二分法)
- POJ2976 Dropping tests (分数规划)
- POJ2976-Dropping tests-最大化平均值
- POJ 2976 - Dropping tests(二分搜索)
- (最大化平均值)poj2976,Dropping tests
- 渗透测试方法与流程
- ZOJ 3621 Factorial Problem in Base K 分解质因数
- Android Handler的使用略
- DSS 搭建手机流媒体服务器(部分转载)
- TCP 中的EAGAIN错误
- POJ2976 Dropping tests 二分搜索
- Nexus 7 (2013) and Win 7 64 - cannot install USB driver
- GTK 笔记本构件(The NoteBook Widget)
- 7-0. 写出这个数 (20)
- Stanford ML - Logistic Regression 逻辑回归
- 比较全的xcode真机调试教程
- 为什么时间很紧还想玩游戏?(李松蔚版)
- 面试总结
- com关于IUnknown接口