bzoj4576【Usaco2016 Open】262144
2016-06-19 16:07
501 查看
4576: [Usaco2016 Open]262144
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 97 Solved: 73
[Submit][Status][Discuss]
Description
Bessie likes downloading games to play on her cell phone, even though she does find the small touchscreen 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 startof the game.
Output
Please output the largest integer Bessie can generate.Sample Input
41
1
1
2
Sample Output
3In 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
Platinum方法一: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] ; 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 ; struct data{int val,tot;}s ,a ; 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; }
相关文章推荐
- HDU-1521 Entropy(哈夫曼树)
- Nginx高可用使用Keepalived+nginx实现
- Centos安装gcc及g++
- linux网卡
- properties文件装载读取Util
- Linux常见基础命令总结(三)
- linux调用.so动态库
- Centos创建和修改用户及密码命令
- 第3章 配置自定义嵌入式Linux
- Inside this Linux boot process
- Linux 系列(七)——Zookeeper集群搭建
- 万亿级调用系统:微信序列号生成器架构设计及演变
- Linux中fork()函数详解
- Shell的test
- ssh证书登录(实例详解)
- 自动车牌识别(ANPR)练习项目学习笔记3(基于opencv)
- Docker实战(十三):Docker安装MySQL数据库
- centos 7安装完后出现please make your choice from '1' to e
- 第2章 测试Petalinux BSP镜像
- Linux setuid setgid stickybit 权限