P1108 Low Price purchase

Source: Internet
Author: User
Title Description

The proposal for "low-cost purchases" is half the rule of success in the dairy market. To be considered a great investor, you must follow the following questions to suggest: "Buy at a low price, then buy at a lower price". Every time you buy a stock, you must buy it at a price lower than the one you bought it last. The more times you buy, the better! Your goal is to ask for the maximum number of shares you can buy if you follow the above suggestions. You will be given a daily sale of a stock for a period of time (a positive integer within the 2^16 range), and you can choose which days to buy the stock. Each purchase must be guided by the principle of "low-cost purchase, then low-cost purchase". Write a program to calculate the maximum number of purchases.

Here is a list of prices for a stock:

Date 1 2 3 4 5 6 7 8 9 10 11 12

Price 68 69 54 64 68 64 70 67 78 62 98 87

The best investors can buy up to 4 shares, one of the options is:

Date 2 5 6 10

The price of the input output format input format:

Line 1th: N (1 <= n <= 5000), Stock issue days

Line 2nd: N number, is the daily stock price.

Output format:

The output file contains only two numbers on a single line: Maximum purchase count and number of scenarios with maximum number of purchases (<=2^31) These 2 scenarios are considered identical when two scenarios "look the same" (that is, they constitute the same price queue).

input and Output sample input Example # #:

12
62 98 87
Sample # # of output:
4 2






The first question is the longest descending sub-sequence, needless to say;

The second question is to ask for the number of programs, but also to remove duplication,

We observe that the state transition equation of the first question, if f[i]==1, indicates that the length of the longest descent subsequence ending with I is 1, then the scheme number is obviously 1//initialization.

Then if (A[j]>a[i]) F[i]=max (f[i],f[j]+1), if F[i]==f[j]+1 then the state of F[i] is transferred from the state of f[j] then c[i]+=c[j]

Next is the weight: if there is a[i]==a[j]&&f[i]==f[j] then in this case I and J are equivalent, so c[j]=0;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
const int maxn=1000000;
using namespace std;
int n,a[maxn],f[maxn],c[maxn],ans,total;
int main ()
{
	scanf ("%d", &n);
	for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
	for (int i=1;i<=n;i++)//The longest descent sub-sequence
	{
		f[i]=1;//initialization, for each digit the longest non-descending subsequence ending with this number is 1, which is itself for
		(int j=1;j <=i-1;j++)
		{
			if (A[j]>a[i]) F[i]=max (f[i],f[j]+1);//Transfer Equation
		}
	} for
	(int i=1;i<=n;i + +) Ans=max (Ans,f[i]);//Find the maximum for
	(int i=1;i<=n;i++)//Find scheme number
	{
		if (f[i]==1) c[i]=1;//Initialize for
		( int j=1;j<=i-1;j++)
		{
			if (a[j]>a[i]&&f[i]==f[j]+1) c[i]+=c[j]; 	
			if (A[i]==a[j]&&f[i]==f[j]) c[j]=0;//the Weight
		}	
	} for
	(int i=1;i<=n;i++)
	{
		if (f[i]== ANS) total+=c[i];//cumulative scheme number
	}	
	printf ("%d%d", ans,total);
	return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.