zl程序教程

您现在的位置是:首页 >  其它

当前栏目

POJ 2976 Dropping tests (二分+贪心)

poj 二分 贪心 tests
2023-09-11 14:17:18 时间

题意:给定 n 个分数,然后让你去年 m 个分数,使得把剩下的所有的分子和分母都相加的分数最大。

析:这个题并不是分子越大最后结果就越大,也不是整个分数越大,最后结果就越大的,我们可以反过来理解,要去掉 m 个分数,那么就是要选 n-m个分数,

那么就是 sigma(分子) / sigma(分母) 尽量大,那么最大是多大啊?这个我们可以通过二分来解决,也就是sigma(分子) / sigma(分母) >= x,

因为分子和分母都是正数,所以可以得到 sigma(分子) - sigma(分母)* x  >= 0,也就是 sigma(分子 - x * 分母) >= 0(前n-m项),我们就可以按这个进行排序,

看看前 n-m 项成不成立。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
double c;

struct Node{
  double a, b;
  bool operator < (const Node &p) const{
    return a - b * c > p.a - p.b * c;
  }
};
Node a[maxn];

bool judge(){
  sort(a, a + n);
  double ans = 0;
  for(int i = 0; i < n-m; ++i)  ans += a[i].a- a[i].b * c;
  return ans >= 0.0;
}

double solve(){
  double l = 0, r = INF;
  for(int i = 0; i < 100; ++i){
    c = (l+r) / 2;
    if(judge())  l = c;
    else r = c;
  }
  return l;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2 && n+m){
    for(int i = 0; i < n ; ++i)  scanf("%lf", &a[i].a);
    for(int i = 0; i < n ; ++i)  scanf("%lf", &a[i].b);
    printf("%.f\n", solve()*100);
  }
  return 0;
}