POJ3111 K Best —— 01分数规划 二分法
題目鏈接: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 3Sample Output
1 2Source
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大的數 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 //一次性把所有信息都錄入結構體中,當排序時,即使打亂了順序,仍然還記得初始下標。 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]獨立開來 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?
錯誤代碼:
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 //經過一次排序后,q[i].id不再等于i,所以出錯。應該同時更新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?
轉載于:https://www.cnblogs.com/DOLFAMINGO/p/7571434.html
總結
以上是生活随笔為你收集整理的POJ3111 K Best —— 01分数规划 二分法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 将文件夹下的所有文件复制到另
- 下一篇: 英文词频统计预备,组合数据类型练习