二分+01分数规划+最大化平均值 Dropping tests POJ - 2976
題意:
給你若n個(gè)分?jǐn)?shù),分子a[i]a[i]a[i],分母b[i]b[i]b[i],使?jié)M足公式100?∑i=1nai∑i=1nbi100\cdot\tfrac{\sum_{i=1}^{n} a_{i}}{\sum_{i=1}^{n} b_{i}}100?∑i=1n?bi?∑i=1n?ai??,求任意去掉k個(gè)分?jǐn)?shù)后,公式結(jié)果最大值。
題目:
In a certain course, you take n tests. If you get aia_{i}ai? out of bib_{i}bi? questions correct on test i, your cumulative average is defined to be
100?∑i=1nai∑i=1nbi100\cdot\tfrac{\sum_{i=1}^{n} a_{i}}{\sum_{i=1}^{n} b_{i}}100?∑i=1n?bi?∑i=1n?ai??
Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.
Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is 100?5+0+25+1+6100\cdot\tfrac{5+0+2}{5+1+6}100?5+1+65+0+2?. However, if you drop the third test, your cumulative average becomes 100?5+05+1≈83.33≈83100\cdot\tfrac{5+0}{5+1}\approx83.33\approx83100?5+15+0?≈83.33≈83.
Input
The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ ai ≤ bi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed.
Output
For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer.
Sample Input
3 1
5 0 2
5 1 6
4 2
1 2 7 9
5 6 7 9
0 0
Sample Output
83
100
Hint
To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997).
分析:
- 01分?jǐn)?shù)規(guī)劃:
簡單的來說,就是有一些二元組(ai,bi),從中選取一些二元組,使得∑ai / ∑bi最大(最小)。
這種題一類通用的解法就是,我們假設(shè)x = ∑ai / ∑bi的最大(小)值,那么就有x * ∑ai = ∑bi ,即∑ai - x * ∑bi= 0。也就是說,當(dāng)某一個(gè)值x滿足上述式子的時(shí)候,它就是要求的值。我們可以想到枚舉……不過再想想,這個(gè)可以二分答案。
所以我們直接二分答案,當(dāng)上述式子>0,說明答案小了,<0則說明答案大了,這樣計(jì)算即可。
01分?jǐn)?shù)規(guī)劃問題相關(guān)算法詳解
AC代碼:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int M=1e3+10; const double eps=1e-4; int n,m; double a[M],b[M],c[M]; bool cmp(double u,double v){return u>v; } bool dfs(double x){for(int i=0;i<n;i++){c[i]=a[i]-x*b[i];}sort(c,c+n,cmp);double ans=0.0;for(int i=0;i<n-m;i++)ans+=c[i];if(ans>=0) return true;return false; } int main(){while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;for(int i=0;i<n;i++)scanf("%lf",&a[i]);for(int i=0;i<n;i++)scanf("%lf",&b[i]);double r=0.0,l=1.0;while(l-r>eps){double mid=(r+l)/2;if(dfs(mid)) r=mid;else l=mid;}printf("%.0f\n",r*100);}return 0; }基本01分?jǐn)?shù)規(guī)劃問題
給定 n 個(gè)二元組 (ai,bia_{i},b_{i}ai?,bi? ) aia_{i}ai?是選擇此二元組獲得的價(jià)值(非負(fù)),bib_{i}bi?是選擇此二元組付出的代價(jià)(非負(fù)),設(shè) xix_{i}xi? (xix_{i}xi? ∈ { 0 , 1 } ) 代表第 i個(gè)二元組的選與不選,最大(小)化下式
max(ormax(ormax(or min)min)min) r=∑i=1nai?xi∑i=1nbi?xir=\tfrac{\sum_{i=1}^{n} a_{i}\cdot x_{i}}{\sum_{i=1}^{n} b_{i}\cdot x_{i}}r=∑i=1n?bi??xi?∑i=1n?ai??xi??
下面先說最大化
解決方法:二分法
設(shè) r最大值為r?r^{?}r?
r?r^{?}r?=∑i=1nai?xi∑i=1nbi?xi=\tfrac{\sum_{i=1}^{n} a_{i}\cdot x_{i}}{\sum_{i=1}^{n} b_{i}\cdot x_{i}}=∑i=1n?bi??xi?∑i=1n?ai??xi??
?\Leftrightarrow? ∑ai?xi?r??∑bi?xi=0\sum a_{i}\cdot x_{i}-r^{*}\cdot\sum b_{i}\cdot x_{i}=0∑ai??xi??r??∑bi??xi?=0
設(shè)一個(gè)函數(shù),自變量為 r值,
f(r)f ( r )f(r) = ∑ai?xi?r??∑bi?xi\sum a_{i}\cdot x_{i}-r^{*}\cdot\sum b_{i}\cdot x_{i}∑ai??xi??r??∑bi??xi?
觀察這個(gè)函數(shù),假如xi{x_{i}}xi?固定,則這個(gè)函數(shù)就是坐標(biāo)系中一條直線( y = B ? A ? x),每一組xix_{i}xi?對應(yīng)著一條直線,這些直線斜率非正(因?yàn)?? A = ? ∑bi?xib_{i} ? x_{i}bi??xi? ≤ 0),縱截距非負(fù)(因?yàn)?B = ∑ai?xi≥0)a_{i}? x_{i} ≥ 0)ai??xi?≥0),如圖1。
對于每一條直線,當(dāng)f(r)=0f ( r ) = 0f(r)=0時(shí),橫截距就是這一組的 r,那么r?r^{*}r? 就是每條直線橫截距的最大值(每組xix_{i}xi?對應(yīng)r的最大值)如圖2。
在圖中上任取一條垂直x軸的豎線,
如果存在直線與這條豎線的交點(diǎn)縱坐標(biāo)為正,那么最優(yōu)值一定在當(dāng)前豎線的右側(cè);
如果所有直線與這條豎線交點(diǎn)縱坐標(biāo)為負(fù),那么最優(yōu)值一定在當(dāng)前豎線的左側(cè);
如果所有直線與這條豎線交點(diǎn)縱坐標(biāo)非正且存在直線與這條豎線交點(diǎn)縱坐標(biāo)為0,那么當(dāng)前豎線橫坐標(biāo)即為最優(yōu)值r?r^{*}r? 。
按照這個(gè)思想,可以二分答案r,那么二分時(shí)如何進(jìn)行判斷呢?
選擇一個(gè) r時(shí)需要判斷所有 f(r)f ( r )f(r)的最大值是否為0,如果 maxm a xmax {f(r)f ( r )f(r) } > 0則 r < r?r^{?}r?;
怎樣求 maxf(r)m a x { f ( r ) }maxf(r)?
f(r)f ( r )f(r) = ∑ai?xi?r?∑bi?xi\sum a_{i}\cdot x_{i}-r\cdot\sum b_{i}\cdot x_{i}∑ai??xi??r?∑bi??xi?
?\Leftrightarrow?∑(ai?r?bi)?xi\sum( a_{i}-r\cdot b_{i})\cdot x_{i}∑(ai??r?bi?)?xi?
二分一個(gè) r時(shí),每個(gè)二元組的(ai?r?bi)?xi( a_{i}-r\cdot b_{i})\cdot x_{i}(ai??r?bi?)?xi?都可以求出,設(shè)其為weightiweight_{i}weighti?,現(xiàn)在的目標(biāo)就是找到一組 xix_{i}xi?使得 ∑ wighti?xiw i g h t _{i} ? x_{ i}wighti??xi? 最大(即求maxf(r)m a x { f ( r ) }maxf(r))。怎么找到這一組xix_{i}xi?,或者直接求得 maxf(r)m a x { f ( r ) }maxf(r) 呢?具體問題具體分析,經(jīng)常借助最短路算法判斷是否存在負(fù)環(huán)。
總結(jié)
以上是生活随笔為你收集整理的二分+01分数规划+最大化平均值 Dropping tests POJ - 2976的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 桂花果泡酒的功效与作用、禁忌和食用方法
- 下一篇: 莫比乌斯反演/容斥 +2020ICPC