01分数规划
OI分数规划
首先,单纯的贪心是错误的(废话)
除了常见的二分之外还有一种调整法,不会.....
板题:POJ2976 Dropping tests
1 #include <cstdio> 2 #include <algorithm> 3 4 typedef long long LL; 5 const int N = 1010; 6 const double INF = 1e11; 7 8 struct Node { 9 double a, b, c; 10 inline bool operator <(const Node &w) const { 11 return c < w.c; 12 } 13 }node[N]; 14 15 int n, k; 16 17 inline bool check(double D) { 18 for(int i = 1; i <= n; i++) { 19 node[i].c = node[i].a - node[i].b * D; 20 } 21 std::sort(node + 1, node + n + 1); 22 //printf("k = %d \n", k); 23 double ans = 0; 24 for(int i = 1; i <= k; i++) { 25 ans += node[n + 1 - i].c; 26 } 27 return ans >= 0; 28 } 29 30 inline void solve() { 31 k = n - k; 32 double l = INF, r = -INF; 33 for(int i = 1; i <= n; i++) { 34 scanf("%lf", &node[i].a); 35 } 36 for(int i = 1; i <= n; i++) { 37 scanf("%lf", &node[i].b); 38 double temp = node[i].a / node[i].b; 39 l = std::min(l, temp); 40 r = std::max(r, temp); 41 } 42 43 for(int i = 1; i <= 50; i++) { 44 double mid = (l + r) / 2; 45 if(check(mid)) { 46 l = mid; 47 } 48 else { 49 r = mid; 50 } 51 } 52 printf("%lld\n", (LL)(r * 100 + 0.5)); 53 return; 54 } 55 56 int main() { 57 while(scanf("%d%d", &n, &k)) { 58 if(!n && !k) break; 59 solve(); 60 } 61 62 return 0; 63 }