18 freshman season 4. Deal

Topic Description: After the two-eleven, syx found that they were about to eat the soil. But he decided to finance wit. He predicted the future n days Bitcoin market, found up and down, up and down there. His money as long as the experience in the wave of Bitcoin ups and downs, low-income, sell high, you can easily cut the leeks to others in order to get rich. To reduce risk, whenever he decides to up his hand to retain only not more than one Bitcoin, and he wanted to reduce the number of transactions that occurred in the most profitable situation as much as possible. N bits are now given day currency market, i.e., a bit value of coins per day. Please find n days, syx up to how much profit? In the case of maximum profit is the minimum number of transactions many times? We assume syx have unlimited money initially, because even if he wants to borrow money to make a wave.

Input: The first line has an integer T, T represents the total data set. 0 <T <= 5 each group of two data lines:

Each data for the first row, there is an integer n, n represents the predicted market days (1 <= n <= 100000)

For the second row of each set of data, there are n integers A i , respectively, represent the i-th day market. (0 <A = I <= 10 10 )

Export

For each set of samples, the output line of two integers separated by a space, representing the maximum profit and minimum number of transactions

Sample input

2
5
9 10 7 6 8
3
3 2 1

Sample Output

. 4. 3 
0 0 



Code
#include <stdio.h>
int main () {
 int T;
 Long Long B [. 6], C [. 6];
 Scanf ( "% D", & T);
 for (int J = 0; J <T; J ++) {
  int n-, I = 0, X = 0, S = 0, Money = 0;
  Long Long A [100005];
  Scanf ( "% D", & n-); // n-sets of data
  for (i =. 1; <= n-I; I ++) {
   Scanf ( "% D", & a [I]); // n-day market
  }
  for (I =. 1; I <n-; I ++) {
   IF (a [I + . 1]> A [I]) {
    Money Money + = (A [I +. 1] -a [I]);
    s ++; // count difference,
   } the else {
    s = 0; // variable s is introduced as a criterion, either how many times the continuous transaction, to count a minimum number, then the final number is 2
   } // if the latter is small, i.e. loss occurs, s cleared
   if (s == 1) x =         x + 2;        
  }
  b[j]=money;
  C [J] = X;
 }
 for (int I = 0; I <T; I ++) {
  the printf ( "% D% D \ n-", B [I], C [I]);
 }
 return 0;
}

most I did not expect the introduction of variable s, then you can not accurately calculate the minimum number of transactions. Best interests that go first to the lowest point, find adjacent highest point, and then make the difference, and then repeat the above, but this does not count the number of times the minimum.
After the introduction of variable s, simply determines whether the latter is large, and if large, the difference count for interests, s ++, s initially 0, + 1 after as long as 1, 2 plus the number of times, if the latter appears smaller than the former, then the continuous trading is interrupted, this time s clear, in order to prepare for the next consecutive trading.


Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11616763.html