日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划)

發(fā)布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2976】【ZOJ - 3068】【SCU - 2992】Dropping tests (01分数规划) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。