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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

POJ3111 K Best —— 01分数规划 二分法

發(fā)布時(shí)間:2025/7/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ3111 K Best —— 01分数规划 二分法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:http://poj.org/problem?id=3111

?

K Best
Time Limit:?8000MS?Memory Limit:?65536K
Total Submissions:?11380?Accepted:?2935
Case Time Limit:?2000MS?Special Judge

Description

Demy has?n?jewels. Each of her jewels has some value?vi?and weight?wi.

Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep?k?best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels?S?= {i1,?i2, …,?ik} as

.

Demy would like to select such?k?jewels that their specific value is maximal possible. Help her to do so.

Input

The first line of the input file contains?n?— the number of jewels Demy got, and?k?— the number of jewels she would like to keep (1 ≤?k?≤?n?≤ 100 000).

The following?n?lines contain two integer numbers each —?vi?and?wi?(0 ≤?vi?≤ 106, 1 ≤?wi?≤ 106, both the sum of all?vi?and the sum of all?wi?do not exceed 107).

Output

Output?k?numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.

Sample Input

3 2 1 1 1 2 1 3

Sample Output

1 2

Source

Northeastern Europe 2005, Northern Subregion 題解: http://www.cnblogs.com/DOLFAMINGO/p/7563213.html 代碼一: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 struct node 21 { 22 double d; 23 int a, b, id; 24 bool operator<(const node x)const{ 25 return d>x.d; 26 } 27 }q[MAXN]; 28 int n, k; 29 30 bool test(double L) 31 { 32 for(int i = 1; i<=n; i++) 33 q[i].d = 1.0*q[i].a - L*q[i].b; 34 35 sort(q+1, q+1+n); 36 double sum = 0; 37 for(int i = 1; i<=k; i++) //取前k大的數(shù) 38 sum += q[i].d; 39 return sum>=0; 40 } 41 42 int main() 43 { 44 while(scanf("%d%d", &n, &k)!=EOF) 45 { 46 //一次性把所有信息都錄入結(jié)構(gòu)體中,當(dāng)排序時(shí),即使打亂了順序,仍然還記得初始下標(biāo)。 47 for(int i = 1; i<=n; i++) 48 { 49 scanf("%d%d", &q[i].a, &q[i].b); 50 q[i].id = i; 51 } 52 53 double l = 0, r = 1e7; 54 while(l+EPS<=r) 55 { 56 double mid = (l+r)/2; 57 if(test(mid)) 58 l = mid + EPS; 59 else 60 r = mid - EPS; 61 } 62 63 for(int i = 1; i<=k; i++) 64 printf("%d ", q[i].id); 65 printf("\n"); 66 } 67 } View Code 代碼二: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 struct node 21 { 22 double d; 23 int id; 24 bool operator<(const node x)const{ 25 return d>x.d; 26 } 27 }q[MAXN]; 28 29 int n, k; 30 int a[MAXN], b[MAXN]; 31 32 bool test(double L) 33 { 34 for(int i = 1; i<=n; i++) //每一次q[i]都重新更新,與a[i],b[i]獨(dú)立開來(lái) 35 { 36 q[i].id = i; 37 q[i].d = 1.0*a[i] - L*b[i]; 38 } 39 sort(q+1, q+1+n); 40 double sum = 0; 41 for(int i = 1; i<=k; i++) 42 sum += q[i].d; 43 return sum>=0; 44 } 45 46 int main() 47 { 48 while(scanf("%d%d", &n, &k)!=EOF) 49 { 50 for(int i = 1; i<=n; i++) 51 scanf("%d%d", &a[i], &b[i]); 52 53 double l = 0, r = 1e7; 54 while(l+EPS<=r) 55 { 56 double mid = (l+r)/2; 57 if(test(mid)) 58 l = mid + EPS; 59 else 60 r = mid - EPS; 61 } 62 63 for(int i = 1; i<=k; i++) 64 printf("%d ", q[i].id); 65 printf("\n"); 66 } 67 } View Code

?

錯(cuò)誤代碼:

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 struct node 21 { 22 double d; 23 int id; 24 bool operator<(const node x)const{ 25 return d>x.d; 26 } 27 }q[MAXN]; 28 29 int n, k; 30 int a[MAXN], b[MAXN], ans[MAXN]; 31 32 bool test(double L) 33 { 34 //經(jīng)過(guò)一次排序后,q[i].id不再等于i,所以出錯(cuò)。應(yīng)該同時(shí)更新q[i].id 35 for(int i = 1; i<=n; i++) 36 q[i].d = 1.0*a[i] - L*b[i]; 37 38 sort(q+1, q+1+n); 39 double sum = 0; 40 for(int i = 1; i<=k; i++) 41 sum += q[i].d; 42 return sum>0; 43 } 44 45 int main() 46 { 47 while(scanf("%d%d", &n, &k)!=EOF) 48 { 49 for(int i = 1; i<=n; i++) 50 { 51 scanf("%d%d", &a[i], &b[i]); 52 q[i].id = i; 53 } 54 55 double l = 0, r = 1e7; 56 while(l+EPS<=r) 57 { 58 double mid = (l+r)/2; 59 if(test(mid)) 60 l = mid + EPS; 61 else 62 r = mid - EPS; 63 } 64 65 for(int i = 1; i<=k; i++) 66 printf("%d ", q[i].id); 67 printf("\n"); 68 } 69 } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/DOLFAMINGO/p/7571434.html

總結(jié)

以上是生活随笔為你收集整理的POJ3111 K Best —— 01分数规划 二分法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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