POJ 3111 K Best 贪心 二分
Posted FriskyPuppy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3111 K Best 贪心 二分相关的知识,希望对你有一定的参考价值。
题目链接: http://poj.org/problem?id=3111
题目描述:
解题思路:
代码: 这是我错的代码......一会儿回来改啊......一会儿回来可能会很沮丧
#include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #include <cmath> #include <algorithm> #include <stack> #include <deque> #include <map> #include <set> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define sca(x) scanf("%d",&x) #define de printf("=======\n") typedef long long ll; using namespace std; const double INF = 20000000.0; const int maxn = 1e5+10; int n, k; struct node { double v; int index; }y[maxn]; double v[maxn], w[maxn]; stack<int> ans; int cmp( node n1, node n2 ) { return n1.v > n2.v; } bool ok( double x ) { for( int i = 0; i < n; i++ ) { y[i].v = v[i] - x*w[i]; y[i].index = i+1; } double sum = 0; sort(y, y+k, cmp); for( int i = 0; i < k; i++ ) { sum += y[i].v; } if( sum >= 0 ) { while( !ans.empty() ) ans.pop(); for( int i = 0; i < k; i++ ) { ans.push(y[i].index); } } return sum >= 0; } void solve() { double low = 0; double high = INF; for( int i = 0; i < 100; i++ ) { double m = (low + high) * 0.5; if( ok(m) ) { low = m; } else { high = m; } } stack<int> ans1; while( !ans.empty() ) { ans1.push(ans.top()); ans.pop(); } while( !ans1.empty() ) { int t = (int)ans1.size(); if( t > 1 ) printf( "%d ", ans1.top() ); else printf( "%d\n", ans1.top() ); ans1.pop(); } } int main() { while( scanf( "%d%d", &n, &k ) == 2 ) { for( int i = 0; i < n; i++ ) { scanf( "%lf%lf", v+i, w+i ); } solve(); } return 0; }
思考:
以上是关于POJ 3111 K Best 贪心 二分的主要内容,如果未能解决你的问题,请参考以下文章