【BZOJ-1367】sequence 可并堆+中位数
2016-05-07 21:17
489 查看
1367: [Baltic2004]sequence
Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 932 Solved: 348
[Submit][Status][Discuss]
Description
![](http://www.lydsy.com/JudgeOnline/images/1367_1.jpg)
Input
![](http://www.lydsy.com/JudgeOnline/images/1367_2.jpg)
Output
一个整数RSample Input
79
4
8
20
14
15
18
Sample Output
13HINT
所求的Z序列为6,7,8,13,14,15,18.R=13
Source
Solution
论文中的例题,非常吼啊....思想巧妙Code
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> using namespace std; 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; } #define maxn 1000100 long long ans; int n,data[maxn],root[maxn],L[maxn],R[maxn],cnt[maxn],tot; struct LeftTreeNode { int sz,son[maxn][2],d[maxn],size[maxn],a[maxn]; // LeftTreeNode () // { // sz=0;memset(son,0,sizeof(son));memset(size,0,sizeof(size)); // memset(cnt,0,sizeof(cnt));memset(a,0,sizeof(a));memset(d,0,sizeof(d)); // } int Merge(int x,int y) { if (!x) return y; if (!y) return x; if (a[x]<a[y]) swap(x,y); son[x][1]=Merge(son[x][1],y); size[x]=size[son[x][0]]+size[son[x][1]]+1; if (d[son[x][1]]>d[son[x][0]]) swap(son[x][0],son[x][1]); d[x]=d[son[x][1]]+1; return x; } int Push(int x) { a[++sz]=x; size[sz]=1; son[sz][0]=son[sz][1]=d[sz]=0; return sz; } int Pop(int x) {return Merge(son[x][0],son[x][1]);} int Top(int x) {return a[x];} int Size(int x) {return size[x];} }LTHeap; int main() { n=read(); for (int i=1; i<=n; i++) data[i]=read()-i; for (int i=1; i<=n; i++) { tot++; root[tot]=LTHeap.Push(data[i]); cnt[tot]=1; L[tot]=R[tot]=i; while (tot>1 && LTHeap.Top(root[tot])<LTHeap.Top(root[tot-1])) { tot--; root[tot]=LTHeap.Merge(root[tot],root[tot+1]); cnt[tot]+=cnt[tot+1]; R[tot]=R[tot+1]; while (LTHeap.Size(root[tot])*2>cnt[tot]+1) root[tot]=LTHeap.Pop(root[tot]); } } for (int i=1; i<=tot; i++) for (int j=L[i],top=LTHeap.Top(root[i]); j<=R[i]; j++) ans+=abs(data[j]-top); printf("%lld\n",ans); return 0; }
被YveH发现了...
相关文章推荐
- 7.UI补充/03静态库和内存分析、硬件获取
- CodeForces 592B The Monster and the Squirrel
- CodeForces 598B Queries on a String
- 347. Top K Frequent Elements
- 关于Arduino Mega2560的最基本介绍
- 06 GUI(图形用户界面)
- codeforces 447C DZY Loves Sequences(最长上升子序列变体)
- Androidstuido快捷键
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)
- HDU-3750-Guess Game
- 最长公共子序列LCS(The longest common subsequence)
- 【UNET自学日志】Part20 UI
- 关键字提取工具(面向英文)
- 最长上升子序列LIS(Longest Increasing Subsequence)
- mysql5中大数据错误:Packet for query is too large (***>***). You can change this value on the ser
- mysql 5.5 开启慢日志slow log的方法(log_slow_queries)
- MySQL开启慢查询日志log-slow-queries的方法
- 蓝牙(Bluetooth)
- Servlet中使用RequestDispatcher调派请求--forware
- Android Volley完全解析(三),定制自己的Request