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;
}

uva 104 Arbitrage (DP + floyd)的更多相关文章

  1. UVa 104 - Arbitrage(Floyd动态规划)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. POJ 2240 - Arbitrage(bellman_ford & floyd)

    题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之 ...

  3. 洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速$dp\&Floyd$)

    洛谷P3502 [POI2010]CHO-Hamsters感想及题解(图论+字符串+矩阵加速\(dp\&Floyd\)) 标签:题解 阅读体验:https://zybuluo.com/Junl ...

  4. uva 509 RAID!(磁盘数据)

    来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...

  5. 取数字(dp优化)

    取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...

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

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

  7. [Codeforces722E] Research Rover (dp+组合数学)

    [Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...

  8. 【wikioi】2800 送外卖(状压dp+floyd)

    http://www.wikioi.com/problem/2800/ 本题状压莫名其妙的tle了,(按照hzwer大神打的喂,他1000多ms,我就2000ms了?) (14.8.7更,将getnu ...

  9. UVA - 1347 Tour(DP + 双调旅行商问题)

    题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...

随机推荐

  1. Dapper丶DapperExtention,以及AbpDapper之间的关系,

    在一些开发上,由于系统的迭代上面,不能立即使用ABP框架一些框架,框架是死的,框架是死的,框架中的一些东西很值得我们学习.例如DDD架构设计,Dto数据传输对象,以及AutoMapper.仓储,依赖注 ...

  2. [agc004c]and grid

    别问我为什么咕了两天 题意: 给出一个$H\times W$的网格图A,仅由'.'和'#'构成,边界上没有'#'且至少有一个'#'.构造两个网格图B和C,大小均为$H\times W$,要求A中为'# ...

  3. json对象与其字符串之间的转换

    在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如: JSON字符串:var str1 = '{ &qu ...

  4. HYSBZ-1040 骑士 基环树上的树状dp

    题目链接:https://cn.vjudge.net/problem/HYSBZ-1040 题意 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英. 他们劫富济贫,惩恶扬善,受到社会各界的 ...

  5. Django综合基础知识

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...

  6. 【jQuery02】点击标题面板显示内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Vue组件开发 -- Markdown

    利用marked 和 highlight.js开发markdown组件 实现效果图如下: markdown组件已这种形式<Markdown v-model="markdown" ...

  8. SVN学习总结(2)——SVN冲突解决

    在我们用VS进行项目合作开发的过程中,SVN的提交控制是至关重要的,大家不可避免的都遇到过SVN冲突的问题,开发的时候,应该认真学习SVN的知识,减少冲突,集中时间放在开发上. 解决冲突有三种方式: ...

  9. C++容器(三):pair类型

    pair类型 在开始介绍关联容器之前,我们有必要了解一种与之相关的标准库类型–pair类型. 操作 含义 pair<T1, T2> p1 创建一个空的pair对象,它的两个元素分别为T1和 ...

  10. xp秘钥

    TDCXC-M9FW9-3HQ28-CPXYR-YXQ3QCCBDF-9W9T8-K8B7M-83HJM-X2MCWP3MF6-BTDKT-KR7YF-X4BM9-4HD9TMCCWF-42JGF-W ...