Time Limit: 1000MSMemory Limit: 30000KB

Each time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices.

You will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be strictly lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy.

Output

Two integers on a single line:
* The length of the longest sequence of decreasing prices
* The number of sequences that have this length (guaranteed to fit in 31 bits)

In counting the number of solutions, two potential solutions are considered the same (and would only count as one solution) if they repeat the same string of decreasing prices, that is, if they "look the same" when the successive prices are compared. Thus, two different sequence of "buy" days could produce the same string of decreasing prices and be counted as only a single solution.

Sample Input

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


Sample Output

4 2

792KB  94MS
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=5050;

int a[inf];
int len[inf];			//len[i]表示从i开始的最大长度，
long long tim[inf];		//tim[i]表示从i开始有几种方法
int n,maxn=-1,tot=0;

int main(){
cin>>n;
for (int i=1;i<=n;i++) scanf("%d",&a[i]);

for (int i=1;i<=n;i++) len[i]=1,tim[i]=1;	//初始化

for (int i=n;i;i--){						//从右向左依次搜索

for (int j=i+1;j<=n;j++){				//从当前点向右逐个讨论

if (a[i] > a[j]){					//满足递减

if (len[i] < len[j]+1){			//接上j的话可以变长
len[i]=len[j]+1;
tim[i]=tim[j];				//这时i只接了j，故这一步成立
}

else if (len[i] == len[j]+1)	//i接上j和i已有的接法一样长
tim[i]+=tim[j];				//这一步一定成立。有重复的情况在下文会排除
}

else if (a[i]==a[j]){		//出现了重复
if (len[i]==1)			/*重要：由于j的坐标是从i开始向右，所以len[i]=1意味着i在遇到与它重复的j之前没有能接上任何点；
那么在i左边的点z接上i时，z一定会与稍后要接上的j重复。
综上，要将i忽略不计。这里照应了上文的“tim[i]+=tim[j];”*/

tim[i]=0;			//避免之后重复讨论
break;			//不再讨论i点
}
}
}

for(int i=1;i<=n;i++) maxn=max(len[i],maxn);

for(int i=1;i<=n;i++)
if(len[i]==maxn)
tot+=tim[i];

cout<<maxn<<' '<<tot<<endl;
return 0;
}

08-25 3293

07-28 331

01-31 171

08-19 3446

08-06 826

11-26 320

05-03 839

07-22 366

04-29 1298

05-13 980