Algoogle

Algorithm for Programming Contest

PKU 2976 Dropping tests

Category: PKU Tag: binary-search

Dropping tests

問題概要


解法


平均の最大化. 二分探索. 四捨五入で値を丸めるので100回ぐらい回してからlbを四捨五入する.

コード


(2976.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#define rep(i,a) for(int i = 0;i < (a); i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define pb push_back
using namespace std;
typedef long long ll;

int n, k;
vector<ll> a, b;

bool can(double val){
    vector<double> v;
    rep(i,n) v.pb(100.0 * a[i] - val * b[i]);
    sort(rall(v));
    double sum = 0;
    rep(i,n-k) sum += v[i];
    return sum >= 0;
}

int main(){
    while(cin >> n >> k, n || k){
	a.resize(n); b.resize(n);
	rep(i,n) scanf("%lld", &a[i]);
	rep(i,n) scanf("%lld", &b[i]);
	double lb = 0, ub = 128;
	rep(_,100){
	    double mid = (lb + ub) / 2;
	    if(can(mid)) lb = mid;
	    else ub = mid;
	}
	int ans = (int)(lb + 0.5);
	cout << ans << endl;
    }
    return 0;
}

Comments