bzoj4576Usaco2016 Open262144

Posted AaronPolaris

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj4576Usaco2016 Open262144相关的知识,希望对你有一定的参考价值。

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;
}


以上是关于bzoj4576Usaco2016 Open262144的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 4576: [Usaco2016 Open]262144

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

BZOJ 4580: [Usaco2016 Open]248

bzoj1710Usaco2007 OpenCheappal 便宜回文

BZOJ 4579: [Usaco2016 Open]Closing the Farm

BZOJ4580: [Usaco2016 Open]248