Fork me on GitHub

POJ 2976 Dropping tests(最大化平均值 or 01整数规划)

题目链接

  忽略运算符逻辑导致奇怪的错误(代码中指明位置了)

  输出没加0.5,WA。

  还有,注意特殊情况k=0,所以scanf("%d%d", &n, &k)&n就够了, scanf("%d%d", &n, &k)&n&k应该会WA。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 int a[1005];
 6 int    b[1005];
 7 double c[1005];
 8 int n, k;
 9 double l, m, u; 
10 bool C(double m) {
11     double sum = 0;
12     for (int i = 0; i < n; i++) {
13         c[i] = a[i] - b[i]*m;
14     }
15     sort(c, c+n);
16     for (int i = k; i < n; i++) sum += c[i];
17     //for (int i = k; i < n; sum += c[i] && i++); priority
18     return sum >= 0.0;
19 }
20 int main(void) {
21     freopen("in.txt", "r", stdin);
22     freopen("out.txt", "w", stdout);
23     for (;scanf("%d%d", &n, &k) && n; printf("%d\n", (int)(l*100+0.5))) {//n = ?, k=0
24         for (int i = 0; i < n; scanf("%d", a+i)&&i++);
25         for (int i = 0; i < n; scanf("%d", b+i)&&i++);
26         l = 0.0, u = 1.0;
27         for ( ;fabs(u-l) > 0.0001; ) {
28             m = (u+l)/2.0;
29             if (C(m)) l = m;
30             else u = m;
31         }
32     }
33     return 0;
34 }
View Code

  听说还是典型的01整数规划,有时间试试!

posted @ 2016-08-15 17:33  赵裕(vimerzhao)  阅读(224)  评论(0编辑  收藏  举报