uva 104 Arbitrage

Description
Download as PDF

Background

The use of computers in the finance industry has been marked with controversy lately as programmed trading – designed to take advantage of extremely small fluctuations in prices – has been outlawed at many Wall Street firms. The ethics of computer programming is a fledgling field with many thorny issues.

The Problem

Arbitrage is the trading of one currency for another with the hopes of taking advantage of small differences in conversion rates among several currencies in order to achieve a profit. For example, if 1.00inU.S.currencybuys0.7Britishpoundscurrency,£1inBritishcurrencybuys9.5Frenchfrancs,and1Frenchfrancbuys0.16inU.S.dollars,thenanarbitragetradercanstartwith1.00 and earn tex2html_wrap_inline29 dollars thus earning a profit of 6.4 percent.

You will write a program that determines whether a sequence of currency exchanges can yield a profit as described above.

To result in successful arbitrage, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered.

The Input

The input file consists of one or more conversion tables. You must solve the arbitrage problem for each of the tables in the input file.

Each table is preceded by an integer n on a line by itself giving the dimensions of the table. The maximum dimension is 20; the minimum dimension is 2.

The table then follows in row major order but with the diagonal elements of the table missing (these are assumed to have value 1.0). Thus the first row of the table represents the conversion rates between country 1 and n-1 other countries, i.e., the amount of currency of country i ( tex2html_wrap_inline37 ) that can be purchased with one unit of the currency of country 1.

Thus each table consists of n+1 lines in the input file: 1 line containing n and n lines representing the conversion table.

The Output

For each table in the input file you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length, i.e., one of the sequences that uses the fewest exchanges of currencies to yield a profit.

Because the IRS (United States Internal Revenue Service) notices lengthy transaction sequences, all profiting sequences must consist of n or fewer transactions where n is the dimension of the table giving conversion rates. The sequence 1 2 1 represents two conversions.

If a profiting sequence exists you must print the sequence of exchanges that results in a profit. The sequence is printed as a sequence of integers with the integer i representing the tex2html_wrap_inline51 line of the conversion table (country i). The first integer in the sequence is the country from which the profiting sequence starts. This integer also ends the sequence.

If no profiting sequence of n or fewer transactions exists, then the line

no arbitrage sequence exists

should be printed.

Sample Input

3
1.2 .89
.88 5.1
1.1 0.15
4
3.1 0.0023 0.35
0.21 0.00353 8.13
200 180.559 10.339
2.11 0.089 0.06111
2
2.0
0.45

Sample Output

1 2 1
1 2 4 1
no arbitrage sequence exists

题目大意:套汇。给你n种货币相互之间的汇率。问你如何转换货币能在最少次数内获得利润。(利润 > 0.01)。

解题思路:.89就是0.89。他所求的不是最大利润,而是最少的获利交换次数。所以遍历交换次数,没个交换次数都算一次获利,当获利超过0.01,递归输出当前路径。用一个三维的dp数据dp[i][j][s],i, j代表由货币i转换到货币j。s代表转换次数,记录的是该情况下的汇率。然后用相似floyd的方式来更新这个数组。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 25;
int n;
double rec[N][N][N], dp[N][N][N];
int temp[N];
void printfPath(int i, int j, int s) {if (s == 0) {printf("%d", i);    return;}printfPath(i, rec[i][j][s], s - 1);printf(" %d", j);return;
}
void DP() {int cnt, flag = 0;for (int s = 2; s <= n; s++) {cnt = 0;for (int k = 1; k <= n; k++) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (dp[i][j][s] < dp[i][k][s - 1] * dp[k][j][1]) {dp[i][j][s] = dp[i][k][s - 1] * dp[k][j][1];rec[i][j][s] = k;}}   }}for (int i = 1; i <= n; i++) {if (dp[i][i][s] - 1.0 > 0.01) {flag = 1;printfPath(i, i, s);printf("\n");break;}}   if (flag) break;}if (!flag) printf("no arbitrage sequence exists\n");}
void input() {memset(dp, 0, sizeof(dp));memset(rec, 0, sizeof(rec));for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (i == j) dp[i][j][1] = 1;else scanf("%lf", &dp[i][j][1]);}   }
}
int main() {while (scanf("%d", &n) != EOF) {input();    DP();}return 0;
}

转载于:https://www.cnblogs.com/gavanwanggw/p/7047784.html

uva 104 Arbitrage (DP + floyd)相关推荐

  1. 牛牛的宝可梦Go(dp+floyd)

    题目链接:传送门 来源:牛客网 题目描述 牛牛所在的W市是一个不太大的城市,城市有n个路口以及m条公路,这些双向连通的公路长度均为1,保证你可以从一个城市直接或者间接移动到所有的城市.牛牛在玩宝可梦G ...

  2. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  3. CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数)

    CodeForces - 1000D:Yet Another Problem On a Subsequence (DP+组合数) 题目大意:这题目啊,贼难理解- 定义一个数列是"好的&quo ...

  4. 蓝桥杯备考-刷题之路-动态规划算法(DP算法)Part1

    之前在刷力扣的时候就是浑浑噩噩的,照着评论区的答案写了一遍就万事大吉了,没有深度思考过.这次备考蓝桥杯看到DP算法的第一道题就不会,更难受的是看答案了依然完全不理解,所以决心把DP算法一次弄懂. 开始 ...

  5. Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维)

    Ivan the Fool and the Probability Theory-Codeforces Round #594 (Div. 2)-C题(dp+思维) time limit per tes ...

  6. H - 拦截导弹 OpenJ_Bailian - 2945(dp动态规划)

    某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹来袭,并观 ...

  7. leetcode 1024. 视频拼接(dp/贪心)

    你将会获得一系列视频片段,这些片段来自于一项持续时长为 T 秒的体育赛事.这些片段可能有所重叠,也可能长度不一. 视频片段 clips[i] 都用区间进行表示:开始于 clips[i][0] 并于 c ...

  8. LeetCode 161. 相隔为 1 的编辑距离(DP/遍历)

    文章目录 1. 题目 2. 解题 2.1 DP超时 2.2 一次遍历 1. 题目 给定两个字符串 s 和 t,判断他们的编辑距离是否为 1. 注意: 满足编辑距离等于 1 有三种可能的情形: 往 s ...

  9. LeetCode 1293. 网格中的最短路径(DP/BFS)

    1. 题目 给你一个 m * n 的网格,其中每个单元格不是 0(空)就是 1(障碍物). 每一步,您都可以在空白单元格中上.下.左.右移动. 如果您 最多 可以消除 k 个障碍物,请找出从左上角 ( ...

  10. LeetCode 140. 单词拆分 II(DP+回溯)

    1. 题目 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使 ...

最新文章

  1. redis的导入导出需要特别注意的地方
  2. .mmp怎么打开查看?
  3. pagefile.sys巨型文件在windows10下的尺寸调整
  4. 菜鸟学习Spring——60s配置XML方法实现简单AOP
  5. iOS开发文档(中文)
  6. java IO流经典编程题(5题)
  7. C++ 实现数字黑洞
  8. Davinci BI报表工具~
  9. 劝学篇翻译软件测试,汪洙《神童诗劝学篇》原文与译文
  10. kali中清除历史命令
  11. 【Django】 终端打印出错信息
  12. Ajax发送的put请求过程
  13. 数据分析Qgis-城市餐饮店铺选址
  14. 关于 git rebase 的踩坑记录
  15. 成人脑力训练 3.951
  16. Echarts 柱状图,带背景色且数值显示在最顶部
  17. ArcMap 通过瓦片导出地图
  18. Airflow调度工具简介和使用
  19. svga插件_SVGA实例讲解
  20. FFmpeg被声明为已否决情况整理

热门文章

  1. 何为启发式算法——退火算法,蚁群算法,遗传算法
  2. windows系统文件和dll文件
  3. 力扣-205 同构字符串
  4. Flutter高级第1篇:ListView嵌套GridView、不同终端屏幕适配方案
  5. 7-15 求组合数 (15 分)
  6. L1-043 阅览室 (20 分)—团体程序设计天梯赛
  7. HTML 标签学习总结第一天
  8. ROS 日志消息(C++)
  9. noip2017颓废记
  10. Chapter eight Data Structure(数据结构)