uva 104 Arbitrage (DP + floyd)

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 12:14:34

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

UVA 1543 - Telescope(dp+几何)

题目链接:1543 - Telescope 题意:按顺序给定圆周上一些点,问用选一些点组成m边形面积的最大值. 思路:dp,dp[i][j][k] 表示第一个点为i,最后一个点为j,当前选择k的最大值,因为多选一个点,会多的面积为他和第一个点和最后一个点构成的三角形面积,然后利用海伦公式求面积,状态转移为:dp[i][j][x] = max(dp[i][j][x], dp[i - 1][j][k] + s); 代码: #include <stdio.h> #include <string

UVA - 1498 Activation (DP+概率)

Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey. But before starting the game, he must first activate

Uva 111-History Grading(DP/LCS)

题目链接:点击打开链接 题意坑.. 本来一看就是就裸LCS ,但题目中给的输入并不是原序列,而是原序列,而是原序列的位置..比如 3 1 2 并不是 s[1]=3 而是1在序列中位置是3 即 s[3]=1; (s[x]=i;) 然后输入处理一下就裸LCS. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string>

UVA 10497 - Sweet Child Makes Trouble(DP+高精度)

题目链接:10497 - Sweet Child Makes Trouble 题意:n个物品,原来物品属于一个地方,现在要把物品重新放回去,问能放几种使得每个物品都与原来位置不同 思路:递推,一开始随便搞了个二维状态,dp[i][j]表示i个物品,有j个位置不同,那么dp[n][n]就是答案,递推式为: dp[i][j] = 1 (j == 0) dp[i][j] = (j - 1) * dp[i - 1][j - 1] + dp[i - 1][j] + (i - j + 1) * dp[i -

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

UVA 1371 - Period(DP)

6.4 一些说明 数据属性可以重写同名的方法属性.这是为了避免在大型系统中产生问题的意外名称冲突.所以用一些减少冲突的常用方法是很有效果的.常用的方法包括:大写字母方法名称,用唯一的字符串来做为数据属性的名称(可以是个下划线_)或者用动词命名方法和用名字命名数据属性. 数据属性就像和对象的普通用户一样可以被方法引用.换句话说,类不能用来实现纯净的数据类型.事实上,在python中不能强制数据隐藏,一切基于约定.(另一方面,如C中写的,python的实现可以做到完全隐藏实现细节并且在必要是可以控制

UVA - 620Cellular Structure(递推)

题目:UVA - 620Cellular Structure(递推) 题目大意:只能给出三种细胞的增殖方式,然后给出最后细胞的增殖结果,最后问你这是由哪一种增殖方式得到的.如果可以由多种增殖方式得到,就输出题目中列出来的增殖方式靠前的那种. 解题思路:也是递推,细胞长度长的可以由细胞长度短的推得,并且这里第一种只能是长度为1的细胞才有可能,所以判断的时候可以3个判断,看能否与上面的增殖结果匹配,可以的话就记录下来,以后的长串就是由这样的短串再加上两个细胞继续往后推. 例如: BAABA 将A变为

UVA - 10003Cutting Sticks(递推)

题目:UVA - 10003Cutting Sticks(递推) 题目大意:给根木棍长度l,现在要锯这根木棍,给出n个锯点,求怎样锯才能使得开销最小.例如 长度为10的木棍, 锯点2 4 7,那么如果按照这个顺序 , 首先显示由长度位10的木头先锯了2 ,开销就加10,然后锯完现在有[0,2]和[2,10]长度分别为2 ,8的木棍,现在要在4这个位置锯木头,就是在长度为8的木头上锯4这个位置,这样就加上8,然后又有长度为[2,4][4,10]的木头,最后要锯7的话,就需要开销加上6.所以开销就是

UVA - 10163Storage Keepers(01背包)

题目大意:UVA - 10163Storage Keepers(01背包) 题目大意:现在有m个守店人,和n家店,每个守店人有个能力值,然后一个守护店的人可以守K家店,那么这些店能到的安全度就是Pi / K.店的安全度取决于守护它的人给的安全度中间最低的那个.这些店的最高安全度取决于最低安全度的那家店.现在问如何雇佣这些人使得店的安全度最高的情况下,费用最少. 解题思路: 安全度要求最高,那么就是要判断每个守店的人要不要雇佣,并且雇佣来之后让它守几家店(安全度).dp[i][j]表示:前面的i家