HDU6058 2017杭电多校联赛第三场-Kanade's sum

来源:互联网 发布:领英是什么软件 编辑:程序博客网 时间:2024/05/22 16:48

题目大意:有n个数,询问在任意区间内第k大的数的和值

解题思想:这题在处理时我们用到的是set和链表,纵观网上的一些博客,在使用链表和set处理时都有点复杂化,且步骤繁琐,而我只是在处理它左移以及右移寻找最大的区间时进行了优化,使得代码减少了一部分,看起来没那么繁琐,想用set和链表的同学可以看下,思想的和网上其他的也都差不多,这里就不再累述了。

#include<cstdio>#include<set>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define ll long longconst int N=500005;int a[N],f[N],p[N],l[100],r[100];set<int>s;int read(){int s=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')s=s*10+c-'0',c=getchar();return s;}int main(){ll ans;int i,j,k,n,m,x,y,T; T=read();while(T--){n=read(),m=read();for(i=1;i<=n;i++){a[read()]=i;}ans=0;a[0]=0,a[n+1]=n+1;f[0]=0,p[0]=n+1;f[n+1]=0,p[n+1]=n+1;s.clear();s.insert(0),s.insert(n+1);for(i=n;i>=1;i--){x=a[i];y=*s.lower_bound(x);//cout<<y<<endl; s.insert(x);f[x]=f[y];p[x]=y;p[f[x]]=x;f[p[x]]=x;for(j=0,y=x;j<=m;j++,y=f[y])l[j]=y;for(j=0,y=x;j<=m;j++,y=p[y])r[j]=y;for(j=0,k=m;j<m;j++,k--)ans+=(ll)i*(l[j]-l[j+1])*(r[k]-r[k-1]);}printf("%lld\n",ans);}return 0;} /*1 7 45 4 6 7 3 2 1*/
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6058

原创粉丝点击