bzoj4576【Usaco2016 Open】262144

148 篇文章 0 订阅
78 篇文章 1 订阅

4576: [Usaco2016 Open]262144

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 97   Solved: 73
[ Submit][ Status][ Discuss]

Description

Bessie likes downloading games to play on her cell phone, even though she does find the small touch 
screen rather cumbersome to use with her large hooves.
She is particularly intrigued by the current game she is playing. The game starts with a sequence of
 N positive integers (2≤N≤262,144), each in the range 1…40. In one move, Bessie can take two adja
cent numbers with equal values and replace them a single number of value one greater (e.g., she migh
t replace two adjacent 7s with an 8). The goal is to maximize the value of the largest number presen
t in the sequence at the end of the game. Please help Bessie score as highly as possible!

Input

The first line of input contains N, and the next N lines give the sequence of N numbers at the start
 of the game.

Output

Please output the largest integer Bessie can generate.

Sample Input

4
1
1
1
2

Sample Output

3
In this example shown here, Bessie first merges the second and third 1s to obtain the sequence 1 2 2
, and then she merges the 2s into a 3. Note that it is not optimal to join the first two 1s.

HINT

Source




方法一:DP

f[i][j]表示以j为左端点的序列合成i右端点右面一个点的位置,即如果区间[j,x]可以合成i,则f[i][j]=x+1。

DP转移方程为f[i][j]=f[i-1][f[i-1][j]]。

然后再对于满足f[i][j]>0的i取最大值,即为答案。

时间复杂度O(n*logn)。


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define N 270000
using namespace std;
int n,ans;
int f[60][N];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int main()
{
	n=read();
	F(i,1,n){int x=read();f[x][i]=i+1;}
	F(i,2,58) F(j,1,n)
	{
		if (!f[i][j]) f[i][j]=f[i-1][f[i-1][j]];
		if (f[i][j]) ans=i;
	}
	printf("%d\n",ans);
	return 0;
}



方法二:单调栈

如果将序列的值想象成高度,则合并的顺序一定是从“山谷”(比两端都低的点)开始的。

然后用一个单调栈维护,每次遇到一个山谷就计算答案。

还有一个细节需要注意,在什么情况下要清空栈(详见代码)。

时间复杂度O(n)。


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define N 270000
using namespace std;
int n,ans,cnt,top,lg[N];
struct data{int val,tot;}s[N],a[N]; 
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void clear()
{
	for(;top>1;top--) s[top-1].tot+=s[top].tot/(1<<(s[top-1].val-s[top].val));
	ans=max(ans,s[top].val+lg[s[top].tot]);
	top=0;
}
void combine(int val)
{
	for(;top>1;top--)
	{
		if (s[top-1].val>val) return;
		int num=1<<(s[top-1].val-s[top].val);
		if (s[top].tot%num==0) s[top-1].tot+=s[top].tot/num;
		else
		{
			data tmp=s[top];
			clear();s[++top]=tmp;
			return;
		}
	}
}
int main()
{
	n=read();
	for(int i=2;i<=n;i<<=1) lg[i]=1;
	F(i,1,n) lg[i]+=lg[i-1];
	F(i,1,n)
	{
		int x=read();
		if (x==a[cnt].val) a[cnt].tot++;
		else a[++cnt]=(data){x,1};
	}
	F(i,1,cnt)
	{
		if (!top||a[i].val<s[top].val){s[++top]=a[i];continue;}
		combine(a[i].val);
		int num=1<<(a[i].val-s[top].val);
		if (s[top].tot%num==0)
		{
			a[i].tot+=s[top].tot/num;
			s[top]=a[i];
		}
		else
		{
			a[i].tot+=s[top].tot/num;
			clear();s[++top]=a[i];
		}
	}
	clear();
	printf("%d\n",ans);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值