Poj2976 -- Dropping tests (0-1 score Planning)

Source: Internet
Author: User

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 ;}
   
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.