Poj2976 -- Dropping tests (0-1 score Planning)
Poj2976: Question Link
N pairs are given. a maximum of k pairs can be excluded from them, and the maximum value of a/B is obtained.
0-1 score planning: x = sigma a/SIGMA B --> 0 = sigma a-x * Sigma B --> g (x) = max (sigma a-x * Sigma B ), we can find that g (x) is a monotonic decreasing function about x. When g (x) is 0, x is the maximum value. Because of this monotonicity, binary solutions can be used.
Assume that s is the required value.
G (x) = 0 --> x = s;
G (x)> 0 --> x <s;
G (x) <0 --> x> s;
For this question, g (x) = max (Σ (100 * ai-x * bi), because up to k can be discarded, so the negative values of the smallest k are discarded, you can get the maximum value.
I originally wanted to learn the largest density subgraph. I read the thesis and started from scratch.
#include
#include
#include
#include using namespace std ;#define eqs 1e-9struct node{ double a , b ;}p[1100] ;int n , k ;double c[1100] ;double solve(double s) { double ans = 0 ; for(int i = 0 ; i < n ; i++) c[i] = 100.0*p[i].a - s*p[i].b ; sort(c,c+n) ; for(int i = 0 ; i < n ; i++) { if( i < k ){ if( c[i] >= eqs ) ans += c[i] ; } else ans += c[i] ; } return ans ;}int main() { int i , j ; double low , mid , high , temp ; while( scanf("%d %d", &n, &k) && n+k > 0 ) { for(i = 0 , high = 0 ; i < n ; i++) { scanf("%lf", &p[i].a) ; high += p[i].a ; } for(i = 0 ; i < n ; i++) scanf("%lf", &p[i].b) ; low = mid = 0 ; high *= 100.0 ; while( high-low >= eqs ) { mid = (high + low) / 2.0 ; temp = solve(mid) ; if( fabs(temp) < eqs ) break ; else if( temp < 0 ) high = mid ; else low = mid ; } printf("%d\n", (int)(mid+0.5)) ; } return 0 ;}