树状数组求不带修改前缀最值问题 51NOD 1272 最大距离
2017-09-12 10:07
633 查看
1272 最大距离
题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
收藏
关注
给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,则这两个数可以组成一对。每个元素和自己也可以组成一对。例如:{5, 3, 6, 3, 4, 2},可以组成11对,如下(数字为下标):
(0,0), (0, 2), (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (3, 3), (3, 4), (4, 4), (5, 5)。其中(1, 4)是距离最大的一对,距离为3。
Input
Output
Input示例
Output示例
这是一道大水题。。
本来想再好好研究研究树状数组的优越使用方法
结果上一个复杂度不优,这个又很鸡肋。。
水一篇blog吧
这个题可以维护一个单调栈
栈顶到栈低递增,维护一下下表,每次更新答案时在栈里二分查找就行了
为了研究研究树状数组写了个前缀最值。。还没修改。。马上就去搞一搞。。
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=50010,inf=0X3f3f3f3f;
int n,a
,b
,bit
;
inline void modify(int x,int val)
{for(;x<=n;x+=(x&-x))bit[x]=min(bit[x],val);}
inline int query(int x)
{int res=inf;for(;x;x-=(x&-x))res=min(res,bit[x]);return res;}
inline bool cmp(int x,int y)
{return a[x]==a[y]?x<y:a[x]<a[y];}
int main()
{
n=read();int ans=0;
memset(bit,0X3f,sizeof(bit));
register int i;
for(i=1;i<=n;++i){a[i]=read();b[i]=i;}
sort(b+1,b+1+n,cmp);
for(i=1;i<=n;++i)a[b[i]]=i;
for(i=1;i<=n;++i)
{
modify(a[i],i);
ans=max(ans,i-query(a[i]));
}
print(ans);puts("");
return 0;
}
/*
6 5 3 6 3 4 2
3
*/
题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
收藏
关注
给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,则这两个数可以组成一对。每个元素和自己也可以组成一对。例如:{5, 3, 6, 3, 4, 2},可以组成11对,如下(数字为下标):
(0,0), (0, 2), (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (3, 3), (3, 4), (4, 4), (5, 5)。其中(1, 4)是距离最大的一对,距离为3。
Input
第1行:1个数N,表示数组的长度(2 <= N <= 50000)。 第2 - N + 1行:每行1个数,对应数组元素Ai(1 <= Ai <= 10^9)。
Output
输出最大距离。
Input示例
6 5 3 6 3 4 2
Output示例
3
这是一道大水题。。
本来想再好好研究研究树状数组的优越使用方法
结果上一个复杂度不优,这个又很鸡肋。。
水一篇blog吧
这个题可以维护一个单调栈
栈顶到栈低递增,维护一下下表,每次更新答案时在栈里二分查找就行了
为了研究研究树状数组写了个前缀最值。。还没修改。。马上就去搞一搞。。
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=50010,inf=0X3f3f3f3f;
int n,a
,b
,bit
;
inline void modify(int x,int val)
{for(;x<=n;x+=(x&-x))bit[x]=min(bit[x],val);}
inline int query(int x)
{int res=inf;for(;x;x-=(x&-x))res=min(res,bit[x]);return res;}
inline bool cmp(int x,int y)
{return a[x]==a[y]?x<y:a[x]<a[y];}
int main()
{
n=read();int ans=0;
memset(bit,0X3f,sizeof(bit));
register int i;
for(i=1;i<=n;++i){a[i]=read();b[i]=i;}
sort(b+1,b+1+n,cmp);
for(i=1;i<=n;++i)a[b[i]]=i;
for(i=1;i<=n;++i)
{
modify(a[i],i);
ans=max(ans,i-query(a[i]));
}
print(ans);puts("");
return 0;
}
/*
6 5 3 6 3 4 2
3
*/
相关文章推荐
- 51 nod 1272 最大距离(树状数组)
- 51NOD 1272 最大距离(线段树)
- 51nod 1294 修改数组(dp,逆向思维,最大上升子序列)
- 51nod 1270 数组的最大代价 绝对值取极值问题
- 51nod 1272 最大距离
- 51Nod 1272 最大距离 (排序,维护下标)
- 51nod 1272 最大距离(思维)
- [51Nod 1394 差和问题]树状数组
- 51nod 1272 最大距离
- 51nod 1272 最大距离(贪心)
- 51nod 1272 最大距离 (贪心或单调栈)
- 51nod 1272 最大距离 (单调栈)
- 51nod 差和问题(树状数组+离散化 (Java))
- 51nod 1272 最大距离 O(nlog(n)) , 快排 , 最大连续子串
- 【树状数组】【单点修改区间求和】【区间修改单点查询】【单点修改区间最大值查询】
- 51nod 1272 最大距离 思维题
- 51NOD 1272 最大距离
- 树状数组关于区间修改区间求和的问题
- 51Nod 1272 最大距离
- 树状数组求区间最大值一类问题