1. 程式人生 > >HDU 4803 Poor Warehouse Keeper(貪心+精度)

HDU 4803 Poor Warehouse Keeper(貪心+精度)

題解:

先求出最後的單價,我們每一步都於逼近單價。

因為只有總價部分可以優化成加一個整數,所以每一步去就加上目標總價與當前總價差多少即可。

#include <algorithm>
#include  <iostream>
#include   <cstdlib>
#include   <cstring>
#include    <cstdio>
#include    <string>
#include    <vector>
#include    <bitset>
#include     <stack>
#include     <cmath>
#include     <deque>
#include     <queue>
#include      <list>
#include       <set>
#include       <map>
#define line printf("---------------------------\n") 
#define mem(a, b) memset(a, b, sizeof(a))
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn = 2000+10;
 
int main(){
	int x, y;
	while(~scanf("%d %d", &x, &y)){
		if(y < x){
			printf("-1\n");
			continue;
		}
		double sum = 1.0, univalence = (y+1.0-0.01)/x;
		int ans = x-1;
		for(int i = 1; i <= x; i++){
			int k = (int)(univalence*i-sum);
			ans += k;
			sum += k;
			sum = sum*(i+1.0)/i;
		}
		printf("%d\n", ans); 
	}
}