@OceanEye7年前
08/3
11:38
1367: [Baltic2004]sequence
Time Limit: 20 Sec Memory Limit: 64 MB
Submit: 1346 Solved: 550
[Submit][Status][Discuss]
Description
Input
Output
一个整数R
Sample Input
7
9
4
8
20
14
15
18
9
4
8
20
14
15
18
Sample Output
13
HINT
所求的Z序列为6,7,8,13,14,15,18.
R=13
Source
论文看不是很懂的可以去看看徐姥爷的题解
打错一个下划线wa N次,心痛一波ac率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <cstdlib> #include <cmath> #include <queue> #define ll long long #define INF 1000000000 #define For(i,a,b) for(int i=a;i<=b;++i) #define p(x) ('0'<=x&&x<='9') char cc; int C; template <class T> void read( T &x ) { x=0; C=1; cc=getchar(); while(!p(cc)) { if(cc=='-') C=-1; cc=getchar(); } while(p(cc)) { x=x*10+cc-48; cc=getchar(); } x*=C; } #undef p using namespace std; #define N 1000010 struct node { int c[2],val,dep; node() { c[0]=c[1]=val=0; dep=1; } int& operator [] (const int &x) { return c[x]; } }pool[N]; queue<int> q; int root[N],siz[N],_siz[N],tot,pcnt; int merge(int x,int y) { if(!x||!y) return (x+y); if(pool[x].val<pool[y].val) swap(x,y); pool[x][1]=merge(pool[x][1],y); if(pool[pool[x][0]].dep<pool[pool[x][1]].dep) swap(pool[x][1],pool[x][0]); pool[x].dep=pool[pool[x][1]].dep+1; return x; } int pop(int &x) { q.push(x); x=merge(pool[x][0],pool[x][1]); } int getpt() { static int ret; if( q.empty() ) ret=++pcnt; else { ret=q.front(); q.pop(); }; pool[ret]=node(); return ret; } int z[N],n,w[N]; ll ans; void init() { while(!q.empty()) q.pop(); read(n); For(i,1,n) read(z[i]),z[i]-=i; pool[0].dep=0; } int main() { int node=1; init(); For(i,1,n) { root[++tot]=getpt(); pool[root[tot]].val=w[tot]=z[i]; siz[tot]=_siz[tot]=1; while(w[tot]<w[tot-1]&&tot>1) { root[tot-1]=merge(root[tot-1],root[tot]); siz[tot-1]+=siz[tot]; _siz[tot-1]+=_siz[tot]; tot--; while(_siz[tot] != (siz[tot]>>1)+(siz[tot]&1)) pop(root[tot]),_siz[tot]--; w[tot]=pool[root[tot]].val; } } For(i,1,tot) { for(;siz[i];siz[i]--) ans+=abs(z[node++]-w[i]); } printf("%lld",ans); return 0; } |