HDU 6058 Kanade's sum [链表]
来源:互联网 发布:淘宝店办理营业执照 编辑:程序博客网 时间:2024/06/07 09:21
题意:给你n个数求所有区间第k的的值得总和
题解:求出对于一个数x左边最近的k个比他大的和右边最近k个比他大的,然后从左到右统计答案。我们考虑从大到小枚举x,每次维护一个链表,加入数据用set处理,时间复杂度O(n(k+logn))。
AC代码:
#include<set>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<time.h>struct node{long long x,y;}a[500010];bool cmp(const node&a,const node&b){return a.x>b.x; }long long ans,t,n,k,c,d,xc,xx,yy,l[500010],r[500010],cc[100],dd[100];using namespace std;set<int> st;int main(){ scanf("%lld",&t); while(t--){ ans=0; st.clear(); scanf("%lld%lld",&n,&k); for(int i=1;i<=n;i++){ scanf("%lld",&a[i].x); a[i].y=i; } sort(a+1,a+n+1,cmp); st.insert(0); st.insert(n+1); for (int i = 1; i <= n; i++){ pair<set<int>::iterator, bool> it = st.insert(a[i].y); set<int>::iterator pre = it.first, suc = it.first; --pre; ++suc; int ll = *pre; int rr = *suc; r[ll] = a[i].y;l[rr] = a[i].y; r[a[i].y] = rr;l[a[i].y] = ll; c=0;d=0; cc[0]=a[i].y;dd[0]=a[i].y; xc=a[i].y;while(xc!=0 && c<=80){c++;xc=l[xc];cc[c]=xc;} xc=a[i].y;while(xc!=n+1 && d<=80){d++;xc=r[xc];dd[d]=xc;} c--;d--; int lx=min(k-1,c);int ry=min(k-1,d); for(int xx=0;xx<=lx;xx++){ yy=k-1-xx; if(yy>ry)continue; ans+=a[i].x*(cc[xx]-cc[xx+1])*(dd[yy+1]-dd[yy]); } } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- HDU 6058 Kanade's sum [链表]
- 【HDU 6058 Kanade's sum】+ 链表
- HDU 6058 Kanade's sum(链表)
- HDU 6058 Kanade's sum (链表)
- HDU 6058 Kanade's sum 链表+枚举
- HDU 6058 Kanade's sum(链表)
- HDU 6058 Kanade's sum(链表)
- HDU-6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- [HDU]6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu--6058Kanade's sum
- Kanade's sum HDU 6058
- HDU 6058 Kanade's sum
- 【洛谷】3796 【模板】AC自动机(加强版)
- Log4j和Log4j2的区别
- 8-1
- 2017 多校训练第三场 HDU 6060 RXD and dividing
- C
- HDU 6058 Kanade's sum [链表]
- java代码从初始化到具象执行
- 手写选择题识别-生成编译opencv项目.so文件-失败
- ASP.NET网站怎么发布 Web项目程序怎么发布部署
- 《Android开发艺术探索》读书笔记-第三章 View的事件体系
- Uva
- 杭电acm 1302 蜗牛爬井
- Add to List 67. Add Binary
- python: extend (扩展) 与 append (追加) 的区别