Problem L. Stock Trading Robot
题目连接:
http://www.codeforces.com/gym/100253
Description
CyberTrader is an all-in-one trading solution for investment banks, quantitative hedge funds and
proprietary trading groups. It has only one drawback it is not implemented yet.
You are working on implementing a simple algorithm to buy/sell shares. It should work as follows. Initially
a robot has d dollars and doesn't have any shares. The robot's behaviour is dened by two positive integer
numbers a and b, their role is explained below.
Starting from the second day, every day the robot analyzes a new share price comparing it with the
previous share price. If the price increases the robot buys shares it buys as many shares as it can but
not more than x. Actually, x is not a constant and depends on the number of consecutive increases: x = a
for the rst increase, x = 2a for two increases in a row, and so on, i.e. x = ka for k consecutive increases.
Surely, the robot can buy only non-negative integer number of shares and the number depends on the
money it has and on x.
If the price decreases the robot sells shares it sells as many shares as it has but not more than y.
Actually, y is not a constant and depends on the number of consecutive decreases: y = b for the rst
decrease in a row, y = 2b for two decreases in a row, and so on, i.e. y = kb for k consecutive decreases.
If the price doesn't change the robot does not buy or sell any shares.
Write a program for the robot to simulate the above algorithm.
Input
The rst line of the input contains four positive integers n, d, a and b (1 ≤ n, d ≤ 105
, 1 ≤ a, b ≤ 10),
where n is the number of days to simulate the algorithm. The following line contains sequence of positive
integers p1, p2, . . . , pn (1 ≤ pi ≤ 105
), where pi
is the share price on the i-th day.
It is guaranteed that there will be no overow of the 32-bit signed integer type, so feel free to use type
int (in C++ or Java) to store the number of dollars and shares.
Output
Print the number of dollars and the number of shares the robot will have after n days.
Sample Input
5 10 1 2
1 2 3 4 5
Sample Output
2 3
Hint
题意
自动炒股机器人,在什么价格的时候,会自动买,然后自动卖。
题面给你说买的条件,和卖的条件。
题解:
模拟就好了……
代码
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int n,a,b,d,p[N];
int main()
{
scanf("%d%d%d%d",&n,&d,&a,&b);
for(int i=0;i<n;i++)
scanf("%d",&p[i]);
int cnt1=0,cnt2=0;
long long ans1=1LL*d,ans2=0;
for(int i=1;i<n;i++)
{
if(p[i]>p[i-1])
{
cnt1++;cnt2=0;
long long tmp=min(1LL*cnt1*a,ans1/p[i]);
ans1-=tmp*p[i];
ans2+=tmp;
}
else
if(p[i]<p[i-1])
{
cnt2++;cnt1=0;
long long tmp=min(1LL*cnt2*b,ans2);
ans1+=tmp*p[i];
ans2-=tmp;
}
else cnt1=0,cnt2=0;
}
cout<<ans1<<" "<<ans2<<endl;
return 0;
}