Aizu 2305 Beautiful Currency DP
Beautiful Currency
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93265#problem/FDescription
KM country has N kinds of coins and each coin has its value a_i.
The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beautiful by changing the values of some (possibly no) coins.
A currency system is called beautiful if each coin has an integer value and the (i+1)-th smallest value is divisible by the i-th smallest value for all i (1 \leq i \leq N-1).
For example, the set {1, 5, 10, 50, 100, 500} is considered as a beautiful system, while the set {1, 5, 10, 25, 50, 100} is NOT, because 25is not divisible by 10.
Since changing the currency system may confuse citizens, the king, Kita_masa, wants to minimize the maximum value of the confusion ratios. Here, the confusion ratio for the change in the i-th coin is defined as |a_i - b_i| / a_i, where a_i and b_i is the value of i-th coin before and after the structure changes, respectively.
Note that Kita_masa can change the value of each existing coin, but he cannot introduce new coins nor eliminate existing coins. After the modification, the values of two or more coins may coincide.
Input
Each dataset contains two lines. The first line contains a single integer, N, and the second line contains N integers, {a_i}.
You may assume the following constraints:
1 \leq N \leq 20
1 \leq a_1 \lt a_2 \lt... \lt a_N \lt 10^5
Output
Output one number that represents the minimum of the maximum value of the confusion ratios. The value may be printed with an arbitrary number of decimal digits, but may not contain an absolute error greater than or equal to 10^{-8}.
Sample Input
3 6 11 12
Sample Output
0.090909090909
HINT
题意
有一个国家,要创造完美的货币制度,货币制度完美,就是要第i面值的钱,能够整除i-1面值的钱
然后代价是|bi-ai|/ai,问你能够更待的策略中,最小的的最大代价是多少
题解:
DP转移,枚举金钱,首先,纸币的面值不可能超过2e5,因为这样,还不如全部变成1
然后我们直接类似筛法一样转移就好了
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 20+ 15; double dp[2][200050]; int p[maxn],n,cur=0; double cal(int x,int y) { int tx = abs(y-x); return (double)tx/(double)x; } void init_() { for(int i = 0 ; i <= 2e5 ; ++ i) dp[cur][i] = 1e233; } inline void updata(double & x,double v) { x = min(x,v); } int main(int argc,char * argv[]) { scanf("%d",&n); for(int i = 0 ; i < n ; ++ i) scanf("%d",p+i); init_();dp[cur][1] = 0; for(int i = 0 ; i < n ; ++ i) { int pre = cur;cur ^= 1;init_(); for(int j = 0 ; j <= 2e5 ; ++ j) if(dp[pre][j] < 1e23) { int fs = j; while(fs <= 2e5) { updata(dp[cur][fs],max(dp[pre][j],cal(p[i],fs))); fs += j; } } } double ans = 1e233; for(int j = 0 ; j <= 2e5 ; ++ j) ans = min(ans , dp[cur][j]); printf("%.12lf\n",ans); return 0; }