首页 > 专题系列 > Java解POJ > POJ 1952 BUY LOW, BUY LOWER [解题报告] Java
2013
11-10

POJ 1952 BUY LOW, BUY LOWER [解题报告] Java

BUY LOW, BUY LOWER

问题描述 :

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 12

Price 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 10

Price 69 68 64 62

输入:

* 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.

输出:

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.

样例输入:

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

样例输出:

4 2

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
  public static void main(String[] args) throws IOException
  {
   InputStreamReader is=new InputStreamReader(System.in);
   BufferedReader in=new BufferedReader(is);
   int a=Integer.parseInt(in.readLine());
   int n=0;
   my[] p=new my[a];
   String[] ss;
   while(a-n>10)
    {
	ss=in.readLine().split(" ");
	for(int i=0;i< 10;i++)
	{
		p[n+i]=new my();
		p[n+i].p=Integer.parseInt(ss[i]);
	}
	n+=10;
    }
   ss=in.readLine().split(" ");
   for(int i=0;i< ss.length;i++)
   {
	p[n+i]=new my();
	p[n+i].p=Integer.parseInt(ss[i]);
   }
   for(int i=0;i< a;i++)
   {
	p[i].q=1;
	for(int j=i-1;j>=0;j--)
	{
		if(p[j].p>p[i].p&&p[j].q>=p[i].q)
			p[i].q=p[j].q+1;
	}
   }

  int max=p[a-1].q;
  for(int i=0;i< a;i++)
	if(p[i].q>max) max=p[i].q;
  System.out.print(max);
  int cnt=0;
  for(int i=0;i< a;i++)
	if(p[i].q==1)p[i].n=1;
  for(int i=0;i< a;i++)
  {
	for(int j=0;j< i;j++)
	{
		if(p[i].q==p[j].q+1&&p[i].p< p[j].p)
			 p[i].n+=p[j].n;
		else if(p[i].p==p[j].p&&p[i].q==p[j].q)
		{
			p[i].n-=p[j].n;
		}
	}
   }
   for(int i=0;i< a;i++)
	if(p[i].q==max) cnt+=p[i].n;
   System.out.println(" "+cnt);
 }
}

  class my implements Comparable< my>
  {
	int p;
	int q;
	int n=0;
	@Override
	public int compareTo(my arg0) {
		if(q==arg0.q)
			return p-arg0.p;
		else return q-arg0.q;
	}
}

  1. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  2. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }