POJ

来源:互联网 发布:photoshop mac中文 编辑:程序博客网 时间:2024/06/07 15:41
BUY LOW, BUY LOWER
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 10541 Accepted: 3691

Description

The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice: 
                    "Buy low; buy lower"

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. 

Here is a list of stock prices: 
 Day   1  2  3  4  5  6  7  8  9 10 11 12Price 68 69 54 64 68 64 70 67 78 62 98 87


The best investor (by this problem, anyway) can buy at most four times if each purchase is lower then the previous purchase. One four day sequence (there might be others) of acceptable buys is: 
Day    2  5  6 10Price 69 68 64 62

Input

* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given 

* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers. 

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

1268 69 54 64 68 64 70 67 78 6298 87

Sample Output

4 2

Source

USACO 2002 February


题意:

有一个长度为n的序列,找出最长下降子序列的长度,以及有几个该长度的下降子序列(重复的不计数)


思路:


只是求最长下降子序列的长度的话直接用nlogn的算就可以得到了

然后要求个数的话,将求最长长度的过程保留建图然后记忆化搜索一下就可以得到答案了

样例建图如下:(我将整个序列倒了一下,也就是求最长上升子序列)




然后就是如何去除重复的子序列,因为相同的数字在两个序列的同一位置上出现时它们必然是连在一起的,所以去重就是将这些连在一起的数只算一个,因为如果算第一个的话会导致接下来后面的一些情况遗漏了,所以要用最后一个(这个地方找了好久才在发觉写错了),然后建完图对所有不相同的顶点进行一次DFS记忆化搜索就可以了。


#include<iostream>#include<stdio.h>#include<string.h>#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const int N = 5000 + 50;int n,a[N],ans = 0;int sz[N],h = 0;int b[N],vis[N];vector<int>num[N],G[N];int dfs(int now){    if(vis[now]) return vis[now];    for(int i=0;i<G[now].size();i++){        vis[now] += dfs(G[now][i]);    }    if(!vis[now]) vis[now] = 1;//最低点    return vis[now];}int main(){    scanf("%d",&n);    for(int i=n;i>=1;i--) scanf("%d",&a[i]);    for(int i=1;i<=n;i++){        if(a[i]>b[h]){            b[++h] = a[i];            num[h].push_back(i);            for(int j=num[h-1].size()-1;j>=0;j--){                if(a[num[h-1][j]]>=a[i]) break;                if(j!=num[h-1].size()-1&&a[num[h-1][j]]==a[num[h-1][j+1]]) continue;                G[i].push_back(num[h-1][j]);            }        }else{            int pos = (int)(lower_bound(b+1, b+h+1, a[i]) - b);//对应层数//            if(b[pos]==a[i]) continue;            b[pos] = a[i];            num[pos].push_back(i);            for(int j=num[pos-1].size()-1;j>=0;j--){                if(a[num[pos-1][j]]>=a[i]) break;                if(j!=num[pos-1].size()-1&&a[num[pos-1][j]]==a[num[pos-1][j+1]]) continue;                G[i].push_back(num[pos-1][j]);            }        }    }    for(int i=num[h].size()-1;i>=0;i--){        if(i!=num[h].size()-1&&a[num[h][i]]==a[num[h][i+1]]) continue;        ans += dfs(num[h][i]);    }    printf("%d %d\n",h,ans);    return 0;}





原创粉丝点击