K
K
krikyn2016-02-22 23:11:12
C++ / C#
krikyn, 2016-02-22 23:11:12

What did I not take into account in my decision?

I solve the following problem:
Problem No. 1253. Stock trading
Currently, stock exchanges actively use computer systems to trade stocks, which simplify and automate the process of buying and selling stocks. Some of them even allow trading without human intervention at all.
Of course, the main criterion by which such systems are evaluated is the profit that trading with their use brings. In order to increase it in the construction of these systems, various mathematical methods and models are used, such as, for example, quadratic programming, neural networks, genetic algorithms, etc.
The main difficulty in creating such systems is that they must somehow take into account the change in the value of shares in the future, as well as predict it. Your task is somewhat simpler - the selling and buying rates of shares for the entire period of n days are already known, you just need to develop an optimal strategy for selling and buying. At the same time, for simplicity, we will assume that during these n days you can buy shares no more than once and you can also sell shares no more than once.
In addition, we will assume that the sale and purchase will be carried out only with shares of the same type. At the beginning of this period, you have an amount of x rubles. For each day, we know the price ai (from ask - the offer price) at which one share can be bought, and the price bi (from bid - the ask price) at which one share can be sold. At the same time, in accordance with the current trading rules on the stock exchange, it is allowed to sell and buy only an integer number of shares (for example, if you have 5 rubles, and a share costs 2 rubles, then you can buy no more than two shares).
It is required to write a program that, based on the available data on the value of shares on each day, will find the optimal strategy for buying and selling shares.
Input data
The first line of the input file contains two integers n and x (1 ≤ n ≤ 100000, 1 ≤ x ≤ 106).
The second line of the input file contains n integers a1, . . . , at. The third line of the input file contains n integers b1, . . . , bt. For each index i (1 ≤ i ≤ n) the inequalities 1 ≤ bi ≤ ai ≤ 1000 hold true.
Output
In the first line of the output file print the maximum amount you can have at the end of the period under consideration. In the second line print two numbers — the number of the day d1 on which the shares should be bought, and the number of the day d2 on which these shares should be sold (the inequality d2 > d1 must hold). This implies that as many shares are bought as they can be bought for x rubles, and then they are all sold. If the strategy you have found does not require selling and buying shares, then print "-1 -1" in the second line.
If there are several variants of the optimal strategy, then print any of them
Examples
input
5 1000
2 3 1 4 3
1 2 1 2 3
output
3000
3 5
input
5 1000
10 9 8 7 6
9 8 7 6 5
output
1000
-1 -1
Tried different solutions but many tests still fail (status: wrong answer)
Source code:

#include<iostream>
#include<stdio.h>
#include<queue>
#include<set>
#include<functional>
#include<stack>
#include<iterator>
#include<algorithm>
#include<math.h>
#include<limits>

using namespace std;

int main()
{
    int n,x,a[100009],b[100009],mina,mini,a2=-2,b2=-2;
    cin>>n>>x;
    int maxs = x;

    for (int i=0; i<n; i++)
        cin>>a[i];
    for (int i=0; i<n; i++)
        cin>>b[i];

    mina = a[0];
    mini = 0;

    for (int i=1; i<n; i++)
    {
        if ((x/mina)*b[i]>maxs)
        {
            maxs = (x/mina)*b[i];
            a2 = mini;
            b2 = i;
        }

        if (a[i]<mina)
        {
            mina = a[i];
            mini=i;
        }
    }

    cout<<maxs<<endl;
    cout<<a2+1<<" "<<b2+1;
    return 0;
}

Please help, maybe I did not take into account some things from the condition. I would be happy with counterexamples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
krikyn, 2016-02-22
@krikyn

Problem solved. Maybe someone will need a solution

#include<iostream>
#include<stdio.h>
#include<queue>
#include<set>
#include<functional>
#include<stack>
#include<iterator>
#include<algorithm>
#include<math.h>
#include<limits>

using namespace std;

int main()
{
    int n,x,a[100009],b[100009],mina,mini,a2=-2,b2=-2;
    cin>>n>>x;
    int maxs = x;

    for (int i=0; i<n; i++)
        cin>>a[i];
    for (int i=0; i<n; i++)
        cin>>b[i];

    mina = a[0];
    mini = 0;

    for (int i=1; i<n; i++)
    {
        if (((x/mina)*b[i]+x%mina)>maxs)
        {
            maxs = (x/mina)*b[i]+x%mina;
            a2 = mini;
            b2 = i;
        }

        if (a[i]<mina)
        {
            mina = a[i];
            mini=i;
        }
    }

    cout<<maxs<<endl;
    cout<<a2+1<<" "<<b2+1;
    return 0;
}

My mistake was that I did not take into account the change from the purchase of shares

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question