[usaco]4.3.1 最长递减子序列 和超级整型数

简介: <p>Buy Low, Buy Lower</p> <p>The advice to "buy low" is half the formula to success in the stock market. But to be considered a great investor you must also follow this problems' advice: </p> <

Buy Low, Buy Lower

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

"Buy low, buy lower"
That is, each time you buy a stock, you must purchase more 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 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 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.

By way of example, suppose on successive days stock is selling like this:

 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

In the example above, the best investor (by this problem, anyway) can buy at most four times if they purchase at a lower price each time. One four day sequence (there might be others) of acceptable buys is:

Day    2  5  6 10
Price 69 68 64 62

PROGRAM NAME: buylow
INPUT FORMAT
Line 1:  N (1 <= N <= 5000), the number of days for which stock prices are available.
Line 2..etc:  A series of N positive space-separated integers (which may require more than one line of data) that tell the price for that day. The integers will fit into 32 bits quite nicely.

SAMPLE INPUT (file buylow.in)
12
68 69 54 64 68 64 70 67
78 62 98 87

OUTPUT FORMAT
Two integers on a single line:

the length of the longest sequence of decreasing prices
the number of sequences that have this length
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 OUTPUT (file buylow.out)
4 2

 

-----------------------------------------------------------
求最长递减子序列。
参考算法:http://blog.pfan.cn/rickone/13086.html
上述算法是求最长递增子序列,而且只需要求长度,并不需要求数量。
因此需要添加一个数组记录每一个长度对应的数目。
此外,某个长度可能是一个非常大的整数。因此需要定义一个整数类:

class big{
public :
 int s[bit];
 big  operator+ (big &b){
 }
 big & operator =(const big &a)
 big & operator =(const int a)
 friend ostream & operator <<(ostream & input,big & b)
};
我所遇到的testcase8就有这么长:1606938044258990275541962092341162602522202993782792835301376
USER: Ma yunlei [yunleis2]
TASK: buylow
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3328 KB]
   Test 2: TEST OK [0.000 secs, 3328 KB]
   Test 3: TEST OK [0.000 secs, 3328 KB]
   Test 4: TEST OK [0.000 secs, 3328 KB]
   Test 5: TEST OK [0.000 secs, 3328 KB]
   Test 6: TEST OK [0.000 secs, 3328 KB]
   Test 7: TEST OK [0.000 secs, 3328 KB]
   Test 8: TEST OK [0.000 secs, 3328 KB]
   Test 9: TEST OK [0.054 secs, 3328 KB]
   Test 10: TEST OK [0.189 secs, 3328 KB]


------------------------------------------------------------------

 

/*
ID:yunleis2
PROG:buylow
LANG:C++
*/

#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;
const int maxn=5001;
const int base=1000000;
			   
const int bit=13;
class big{
public :
	int s[bit];
	big  operator+ (big &b){
		big bt;
		int carry=0;
		for(int i=bit-1;i>=0;--i){
			bt.s[i]=(s[i]+b.s[i]+carry)%base;
			carry=(s[i]+b.s[i]+carry)/base;
		}
		return bt;
	}
	big & operator =(const big &a){
		for(int i=0;i<bit;++i)
			this->s[i]=a.s[i];
		return *this;
	}
	big & operator =(const int a){
		for(int i=0;i<bit;++i)
			this->s[i]=0;
		this->s[bit-1]=a;
		return *this;
	}
	friend ostream & operator <<(ostream & input,big & b){
		bool flag=false;
		bool flag1=false;
		for(int i=0;i<bit;++i)
		{	
			if(b.s[i]!=0)
				flag=true;
			
			if(flag){
				if(!flag1)
				{
					input<<b.s[i];
					flag1=true;
				}
				else
					input<<setw(6)<<setfill('0')<<b.s[i];
				
			}
		}
		return input;
	}
};
//seq seqs[maxn];
int ptr=0;
int n;
int stock[maxn];
int size[maxn];
big num[maxn];
bool flag[maxn];
int main()
{
	fstream fin("buylow.in",ios::in );
	fin>>n;
	for(int i=0;i<n;++i)
		fin>>stock[i];
	//seqs[ptr++]
	size[0]=1;
	num[0]=1;
	flag[0]=true;
	for(int i=1;i<n;i++){
		size[i]=1;
		num[i]=1;
		flag[i]=true;
		for(int j=0;j<i;j++){
			if(!flag[j])
				continue;
			if(stock[i]<stock[j])
			{
				if(size[j]>size[i]-1)
				{
					size[i]=size[j]+1;
					num[i]=num[j];
				}
				else if(size[j]==(size[i]-1))
					num[i]=num[i]+num[j];
			}
			else if(stock[i]==stock[j]){
#if 0
				if(size[j]==1){
					size[i]=0;
					num[i]=0;
				}
				else{
					size[i]=1;
					num[i]=1;
				}
#endif
				flag[j]=false;
				if(size[j]>size[i])
				{
					size[i]=size[j];
					num[i]=num[j];
				}
				else if(size[j]==(size[i]))
					num[i]=num[j];
 
			}
		}
	}
	int maxlength=0;
	big num1;
	num1=0;
	for(int i=n-1;i>=0;--i){
		//cout<<"("<<size[i]<<"~"<<num[i]<<") ";
		if(!flag[i])
			continue;
		if(maxlength<size[i])
		{
			maxlength=size[i];
			num1=num[i];
		}
		else if(maxlength==size[i])
			num1=num1+num[i];
	}
 
	//int num2=num1.s[bit-1];
	fstream fout("buylow.out",ios::out);
	fout<<maxlength<<" ";
	fout<<num1<<endl;
	//system("pause");
}


目录
相关文章
|
SQL 关系型数据库 MySQL
MySQL 数据库备份与还原
MySQL 是一款常用的关系型数据库管理系统,用于存储和管理数据。在数据库应用中,数据备份和还原是非常重要的操作,用于保护数据免受意外删除、损坏或数据丢失的影响。本文将详细介绍如何在 MySQL 中进行数据库备份和还原操作,包括常用的备份和还原方法以及相关注意事项。
1917 3
|
1天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
11天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
449 192
|
3天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
9天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
5天前
|
消息中间件 安全 NoSQL
阿里云通过中国信通院首批安全可信中间件评估
近日,由中国信通院主办的 2025(第五届)数字化转型发展大会在京举行。会上,“阿里云应用服务器软件 AliEE”、“消息队列软件 RocketMQ”、“云数据库 Tair”三款产品成功通过中国信通院“安全可信中间件”系列评估,成为首批获此认证的中间件产品。此次评估覆盖安全可信要求、功能完备性、安全防护能力、性能表现、可靠性与可维护性等核心指标,标志着阿里云中间件产品在多架构适配与安全能力上达到行业领先水平。
315 195

热门文章

最新文章