题目链接: 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; }
思考:
时间: 2024-12-28 00:36:46