P1638 逛画展 歪解
Splay_trees · · 个人记录
P1638 逛画展 歪解
大家别走啊这篇文章不是你想的那样简单
这篇文章通过正解讲述了双指针技巧的应用,以及一种维护不可差分信息的树状数组。
题目传送门
本题解法还挺多的,但这里就先讲最简单,个人认为最正解的双指针。
0x00 前置知识
想看双指针:敏捷的思维(比本人好)即可
想看 Fenwick Tree:加上对树状数组和 lowbit 的基本了解
0x01 双指针解法
注意到,由于区间连续,我们可以通过双指针枚举闭区间
枚举时,区间的右端点显然单调不降(按从左往右的顺序,就不用回去再试),那么我们可以重复以下过程:
对于当前的右端点:
如果区间 [l,r] 包含了所有大师的画:
尽可能向右推上一步留在原处的左指针
更新当前最优区间
把右端点向右移动一格
继续判断下一个端点
对于
代码实现如下。
#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[1000005];
int vis[2005],cnt;
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int l=1,r=1,ansl=-1,ansr=1000001;
for(r=1;r<=n;r++)
{
if(!vis[a[r]]) cnt++;
vis[a[r]]++;
while(vis[a[l]]>1) vis[a[l]]--,l++;
if(cnt==m)
if(r-l<ansr-ansl)
ansl=l,ansr=r;
}
cout<<ansl<<' '<<ansr;
return 0;
}
时间复杂度
0x02 树状数组歪解
事先声明:这个解法虽然废但是可以省掉一个
这一切都要从偶然间发现的一个性质说起。
考虑以下样例:
13 5
331454235124
Match Table:
| 1 2 3 4 5 | L
—|———————————————|——
1| 0 0 1 0 0 |
2| 0 0 2 0 0 |
3| 3 0 2 0 0 |
4| 3 0 2 4 0 |
5| 3 0 2 4 5 |
6| 3 0 2 6 5 |
7| 3 7 2 6 5 | 7-2=5
8| 3 7 8 6 5 | 8-3=5
9| 3 7 8 6 9 | 9-3=6
10| 10 7 8 6 9 | 10-6=4
11| 10 11 8 6 9 | 11-6=5
12| 10 12 8 6 9 | 12-6=6
13| 10 12 8 13 9 | 13-8=5
其中 Match Table [i] [j] 表示读入到第
这其实就是双指针带来的性质的变形,可惜我没想到(
然而,我写了一个暴力程序。
拿了
记录详情 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <bits/stdc++.h>
using namespace std;
int n,m;
int arr[2005];
int Max()
{
int x=-1;
for(int j=1; j<=m; j++)
x=max(x,arr[j]);
return x;
}
int Min()
{
int x=INT_MAX;
for(int j=1; j<=m; j++)
x=min(x,arr[j]);
return x;
}
int main()
{
scanf("%d %d",&n,&m);
int x,l=9999,p;
for(int i=1; i<=n; i++)
{
scanf("%d",&x);
arr[x]=i;
int A=Max(),B=Min();
if(A-B<l && B!=0)
l=A-B,p=B;
}
cout<<p<<' '<<p+l;
return 0;
}
时间复杂度
但是空间复杂度奇迹般地变成了原来的约
目前看来最能也最该优化的,就是 Max() 和 Min() 函数了。
思索一下:单点修改,区间查询,树状数组(线段树我不会)!等等。树状数组只能维护可差分可结合律的信息。让我们求助万能的 blog 罢。于是就有了这篇题解。
0x03 维护不可差分信息树状数组
这里以
首先是建树操作,只需要将求和换成
void build()
{
for(int i=1; i<=tot; i++)
{
for(int j=i-lowbit(i)+1; j<=i; j++)
c[i]=max(c[i],a[j]);
}
}
接着是查询 (即 queryMax)。
注意到,c[i] 维护的是
那么我们就可以得到
代码中建议改写为非递归形式求解(使用尾递归优化)。这里因为查询范围固定所以没有传入范围。
int queryMax(int l=1,int r = 0)
{
r=tot;//类初始化时不可设默认值
int s=-1;
while(r>=l)
{
s=max(a[r],s);
r--;
for(; r-lowbit(r)>=l; r-=lowbit(r))
s=max(c[r],s);
}
return s;
}
最后一步是更新。
结合 c[i] 的定义可得,一个元素 c[x] 可转移到 c[i],当且仅当 x+lowbit(x)=i。
反推可得 c[i] 只可能被
代码如下。
void update(int pos,int val)
{
a[pos]=val;
int lpos;
while(pos<=tot)
{
c[pos]=a[pos];
lpos=lowbit(pos);
for(int i=1; i<lpos; i<<=1)
c[pos]=max(c[pos],c[pos-i]);
pos+=lpos;
}
}
处理最小值把
0x04 具体解法
先暴力求出第一个能求出
每一步修改就在树状数组里 update,然后求值取最优。
时间复杂度估计是
通过共用全局数组,可以进一步减小空间占用。
#include <bits/stdc++.h>
using namespace std;
int n,m;
int a[2005];
struct fenwick_tree_max
{
int c[2005],tot;
int lowbit(int x)
{
return x&-x;
}
void build()
{
for(int i=1; i<=tot; i++)
{
for(int j=i-lowbit(i)+1; j<=i; j++)
c[i]=max(c[i],a[j]);
}
}
int queryMax(int l=1,int r = 0)
{
r=tot;
int s=-1;
while(r>=l)
{
s=max(a[r],s);
r--;
for(; r-lowbit(r)>=l; r-=lowbit(r))
s=max(c[r],s);
}
return s;
}
void update(int pos,int val)
{
a[pos]=val;
int lpos;
while(pos<=tot)
{
c[pos]=a[pos];
lpos=lowbit(pos);
for(int i=1; i<lpos; i<<=1)
c[pos]=max(c[pos],c[pos-i]);
pos+=lpos;
}
}
} f1;
struct fenwick_tree_min
{
int c[2005],tot;
int lowbit(int x)
{
return x&-x;
}
void build()
{
for(int i=1; i<=tot; i++)
{
c[i]=a[i];
for(int j=i-lowbit(i)+1; j<=i; j++)
c[i]=min(c[i],a[j]);
}
}
int queryMin(int l=1,int r = 0)
{
r=tot;
int s=INT_MAX;
while(r>=l)
{
s=min(a[r],s);
r--;
for(; r-lowbit(r)>=l; r-=lowbit(r))
s=min(c[r],s);
}
return s;
}
void update(int pos,int val)
{
a[pos]=val;
int lpos;
while(pos<=tot)
{
c[pos]=a[pos];
lpos=lowbit(pos);
for(int i=1; i<lpos; i<<=1)
c[pos]=min(c[pos],c[pos-i]);
pos+=lpos;
}
}
} f2;
int main()
{
scanf("%d %d",&n,&m);
int x,l=INT_MAX,p,clk=m,i;
for(i=1; clk!=0; i++)
{
scanf("%d",&x);
if(clk!=0)
{
if(!a[x]) clk--;
a[x]=i;
}
}
f1.tot=m,f2.tot=m;
f1.build();
f2.build();
int aa=f1.queryMax(),b=f2.queryMin();
if(aa-b<l)
p=b,l=aa-b;
for(; i<=n; i++)
{
scanf("%d",&x);
f1.update(x,i);
f2.update(x,i);
aa=f1.queryMax(),b=f2.queryMin();
if(aa-b<l)
p=b,l=aa-b;
}
cout<<p<<' '<<l+p;
return 0;
}
AC 记录
空间占用约为双指针等解法的
0x05 感想
更多的考的还是思路啊。我是因为双指针没想到才找到这个树状数组,大家不要学我,一定要注意思考题目的性质,多打打草稿,一道黄题杀鸡用牛刀不怎么好,还显得毒瘤。也希望大神能教我一些拿到题目进行分析的方法。