Aizu 2305 Beautiful Currency DP
2015-10-01 21:18
375 查看
Beautiful Currency
Time Limit: 1 SecMemory 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
然后我们直接类似筛法一样转移就好了
![](file:///D:/record_of_qq/475517977/Image/C2C/Y%7D)@)1%KBO0HM418$J94$1R.jpg)
代码:
#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; }
相关文章推荐
- Aizu 2304 Reverse Roads 费用流
- Aizu 2302 On or Off dfs/贪心
- hdu 4300 Clairewd’s message (kmp)
- RAID的原理和应用
- Linux守护进程设计规范及python实现http://blog.csdn.net/dysj4099/article/details/18219411
- 【解决方法】Setup script exited with error: command 'cc' failed with exit status 1
- Raid控制器
- Aizu 2309 Sleeping Time DFS
- POJ 3216 Repairing Company (floyd + 最小路径覆盖)
- Aizu 2304 Reverse Roads(无向流)
- system函数源码 http://blog.csdn.net/ericming200409/article/details/5416234
- RAID
- LightOJ 1236 Pairs Forming LCM
- Aizu 2300 Calender Colors(暴力)
- Aizu 2301 Sleeping Time(概率,剪枝)
- Aizu 2303 Marathon Match (概率)
- org.hibernate.LazyInitializationException: failed to lazily initialize no session or session was clo
- raid各级别特性
- LightOJ 1282 Leading and Trailing
- root@kali:~# insmod /usb/serial/cp210x.ko :Unknown symbol in module