absi2011's Blog & Daily Life.

全新的开始       我要省选翻盘       I wanna AK in 高考\化学       自此,生无可恋
[破碎的状态] [-9] Hdu 2087
[破碎的状态] [-5] BZOJ 2333

[破碎的状态] [-7] BZOJ 1367

absi2011 posted @ Jul 15, 2016 06:35:31 PM in 刷题记录 with tags bzoj 小高考 配对堆 , 587 阅读

这是一道权限题

所以要感谢@wnjxyk 的号

还要抱歉..拉低了他的正确率呢

解法:

因为一些奇怪的情况..

这一题是一个中位数的题..

嗯..

感觉自己不太会表达..贴个网址好了http://www.cnblogs.com/rausen/p/4033724.html

代码如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#include<math.h>
#include<memory>
#include<vector>
#include<bitset>
#include<fstream>
#include<stdio.h>
#include<utility>
#include<sstream>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int b[1000005];
struct node
{
    node * right;
    int val;
    node * ch;
};
node * null;
node * join(node * x,node * y)
{
    if (x->val>y->val) swap(x,y);
    x->right=y->ch;
    y->ch=x;
    return y;
}
node * u(node * x)
{
    if (x==null) return null;
    node * y=x->right;
    x->right=null;
    if (y==null) return x;
    node * z=y->right;
    y->right=null;
    if (z==null) return join(x,y);
    return join(u(z),join(x,y));
}
void delete_max(node * &x)
{
    x=u(x->ch);
}
node * new_node(int x)
{
    static node a[2000005];
    static int top=0;
    node * t=&a[top++];
    t->val=x;
    t->ch=null;
    t->right=null;
    return t;
}
int main()
{
    #ifdef absi2011
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    null=new_node(0);
    null->ch=null;
    null->right=null;
    int n;
    scanf("%d",&n);
    int i;
    static int size[1000005];
    static node * a[1000005];
    int rail=0;
    for (i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        x-=i;
        b[i]=x;
        size[rail]=1;
        a[rail]=new_node(x);
        rail++;
        for (;;)
        {
            if (rail==1) break;
            if (a[rail-1]->val<a[rail-2]->val)
            {
                rail--;
                size[rail-1]+=size[rail];
                a[rail-1]=join(a[rail],a[rail-1]);
                if ((size[rail-1]%2==0)&&(size[rail]%2==1))
                {
                    delete_max(a[rail-1]);
                }
            }
            else
            {
                break;
            }
        }
    }
    long long ans=0;
    int now=0;
    for (i=0;i<n;i++)
    {
        ans+=abs(a[now]->val-b[i]);
        size[now]--;
        if (size[now]==0) now++;
    }
    cout<<ans<<endl;
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter