【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)
題干:
In a certain course, you take?n?tests. If you get?ai?out of?bi?questions correct on test?i, your cumulative average is defined to be
.
Given your test scores and a positive integer?k, determine how high you can make your cumulative average if you are allowed to drop any?k?of your test scores.
Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is?. However, if you drop the third test, your cumulative average becomes?.
題目大意:
給出n個物品,每個物品有兩個屬性a和b,選擇n-k個元素,詢問的最大值。
1<=n<=1000,0<=k<n,0<=ai<=bi<=1000000000。
解題報告:
? ?典型的分?jǐn)?shù)規(guī)劃。
? 首先答案是符合單調(diào)性的,而就等價于。
所以我們發(fā)現(xiàn)二分完把ai-x*bi排序后把最大的n-k個選出來就行了。
AC代碼:(80ms)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int n,k; const double eps = 1e-5; double qq[MAX]; struct Node {double a,b; } node[MAX]; bool ok(double x) {double res = 0;for(int i = 1; i<=n; i++) qq[i] = node[i].a - x * node[i].b;sort(qq+1,qq+n+1); for(int i = n; i>=k+1; i--) res += qq[i];return res >= 0; } int main() {while(~scanf("%d%d",&n,&k) && n+k) {for(int i = 1; i<=n; i++) scanf("%lf",&node[i].a);for(int i = 1; i<=n; i++) scanf("%lf",&node[i].b);double l = 0,r = 1e9;double mid = (l+r)/2;while(l+eps < r) {mid = (l+r)/2;if(ok(mid)) l = mid;else r = mid;}printf("%d\n",(int)(round(l*100)));//mid-eps?}return 0 ;}迭代法可以快很多:(30ms)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int n,k; const double eps = 1e-5; double qq[MAX],aa,bb; struct Node {double a,b; } node[MAX]; struct NN {double a,b,qq; } q[MAX]; bool cmp(NN a,NN b) {return a.qq < b.qq; } double ok(double x) {double res = 0;aa=bb=0;for(int i = 1; i<=n; i++) q[i].qq = node[i].a - x * node[i].b,q[i].a = node[i].a,q[i].b = node[i].b;sort(q+1,q+n+1,cmp); for(int i = n; i>=k+1; i--) aa += q[i].a,bb += q[i].b;return aa/bb; } int main() {while(~scanf("%d%d",&n,&k) && n+k) {for(int i = 1; i<=n; i++) scanf("%lf",&node[i].a);for(int i = 1; i<=n; i++) scanf("%lf",&node[i].b);double l = 0,r = 1e9;double mid = (l+r)/2,ans = -1;while(fabs(ans-mid) >= eps) {ans = mid;mid = ok(mid);}printf("%d\n",(int)(round(ans*100)));//mid-eps?}return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 号称“飞行的硫酸”!你被隐翅虫爬过吗 网
- 下一篇: 【POJ - 3694】Network(