1367: [Baltic2004]sequence

Time Limit: 20 Sec   Memory Limit: 64 MB

Submit: 521  
Solved: 159

[
Submit][
Status]

Description

Input

Output

一个整数R

Sample Input

7
9
4
8
20
14
15
18

Sample Output

13

HINT

所求的Z序列为6,7,8,13,14,15,18.
R=13

左偏树+1

这题裸裸的左偏树,我却各种WA。。。。

结果发现 左偏树合并 a*b==0 会乘爆掉。。。掉节操。。。。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1300000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int father[MAXN];
int getfather(int x)
{
if (father[x]==x) return x;
father[x]=getfather(father[x]);
return father[x];
}
struct node
{
ll v;
int dis,ch[2];
node():v(0),dis(0){ch[0]=ch[1]=0;}
node(ll _v):v(_v),dis(0){ch[0]=ch[1]=0;}
}q[MAXN];
int merge(int a,int b)
{
if (a==0||b==0) return a+b;
if (q[a].v<q[b].v) swap(a,b);
q[a].ch[1]=merge(q[a].ch[1],b);
if (q[q[a].ch[0]].dis<q[q[a].ch[1]].dis) swap(q[a].ch[0],q[a].ch[1]);
if (q[a].ch[1]) q[a].dis=q[q[a].ch[1]].dis+1;else q[a].dis=0;
return a;
}
int root[MAXN],tot=0;
int pop(int a)
{
int p=merge(q[a].ch[0],q[a].ch[1]);
return p;
}
int n,siz[MAXN],l[MAXN],r[MAXN];
ll a[MAXN];
ll abs2(ll a,ll b){return a-b>0?a-b:b-a;}
int main()
{
// freopen("bzoj1367.in","r",stdin);
scanf("%d",&n);
For(i,n) scanf("%lld",&a[i]),a[i]-=(long long)i,father[i]=i,q[i].v=a[i];
For(i,n)
{
siz[++tot]=1;root[tot]=i;l[tot]=r[tot]=i;
while (tot>1&&q[root[tot]].v<=q[root[tot-1]].v)
{
int ru=root[tot-1],rv=root[tot];
Fork(j,l[tot],r[tot])
{
q[j]=node(a[j]);
ru=merge(ru,j);
}
siz[tot-1]+=r[tot]-l[tot]+1;r[tot-1]=r[tot];tot--;
while (siz[tot]>(r[tot]-l[tot]+2)/2) ru=pop(ru),siz[tot]--;
root[tot]=ru;
}
// For(i,tot) cout<<l[i]<<' '<<r[i]<<' '<<q[root[i]].v<<endl; cout<<endl;
}
//For(i,n) cout<<a[i]<<' ';cout<<endl;
//For(i,tot) cout<<siz[i]<<' ';cout<<endl; //For(i,tot) cout<<l[i]<<' '<<r[i]<<' '<<q[root[i]].v<<endl; ll ans=0;
For(i,tot)
{
Fork(j,l[i],r[i]) ans+=abs2(q[root[i]].v,a[j]);
}
printf("%lld\n",ans);
return 0;
}

第二Sol-贪心:

假想有2数列A,B

A的中位数<B的中位数

A的中位数>(B+C)的中位数

则(A+B+C)的中位数≤A的中位数<B的中位数

所以A中位数,B中位数往上的部分去掉,不影响结果

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define MAXN (1000000+10)
typedef long long ll;
struct node
{
int v,dis,ch[2];
node():v(0),dis(0){ch[0]=ch[1]=0;}
node(int _v):v(_v),dis(0){ch[0]=ch[1]=0;}
}q[MAXN];
int merge(int a,int b)
{
if (a==0||b==0) return a+b;
if (q[a].v<q[b].v) swap(a,b);
q[a].ch[1]=merge(q[a].ch[1],b);
if (q[q[a].ch[0]].dis<q[q[a].ch[1]].dis) swap(q[a].ch[0],q[a].ch[1]);
q[a].dis=q[q[a].ch[1]].dis+1;
return a;
}
int root[MAXN],tot=0;
int pop(int a)
{
int p=merge(q[a].ch[0],q[a].ch[1]);
return p;
}
int n,siz[MAXN]={0},l[MAXN]={0},r[MAXN]={0};
int a[MAXN]={0};
ll abs2(ll a,ll b){return a-b>0?a-b:b-a;}
int main()
{
freopen("bzoj1367.in","r",stdin);
scanf("%d",&n);
For(i,n)
{
scanf("%d",&a[i]),a[i]-=i;q[i].v=a[i];
}
For(i,n)
{
siz[++tot]=1;root[tot]=i;l[tot]=r[tot]=i;
while (tot>1&&q[root[tot-1]].v>q[root[tot]].v)
{
int ru=root[tot-1],rv=root[tot];
/*
Fork(j,l[tot],r[tot])
{
q[j]=node(a[j]);
ru=merge(ru,j);
}*/
ru=merge(ru,rv);int b=((r[tot-1]-l[tot-1]+1)%2)+((r[tot]-l[tot]+1)%2);
siz[tot-1]+=siz[tot];r[tot-1]=r[tot];tot--;
if (b==2) ru=pop(ru),siz[tot]--;
root[tot]=ru;
// For(i,tot) cout<<l[i]<<' '<<r[i]<<' '<<q[root[i]].v<<endl; cout<<endl;
}
}
// For(i,n) cout<<a[i]<<' ';cout<<endl;
// For(i,tot) cout<<siz[i]<<' ';cout<<endl; // For(i,tot) cout<<l[i]<<' '<<r[i]<<' '<<q[root[i]].v<<endl; ll ans=0;
For(i,tot)
{
Fork(j,l[i],r[i]) ans+=abs2(q[root[i]].v,a[j]);
}
cout<<ans<<endl;
return 0;
}

BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)的更多相关文章

  1. BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1111  Solved: 439[Submit][ ...

  2. bzoj1367 [Baltic2004]sequence 左偏树+贪心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1367 题解 先考虑条件为要求不下降序列(不是递增)的情况. 那么考虑一段数值相同的子段,这一段 ...

  3. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  4. BZOJ1367: [Baltic2004]sequence(左偏树)

    Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...

  5. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

  6. BZOJ 1367 [Baltic2004]sequence (可并堆)

    题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...

  7. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  8. BZOJ 1455 罗马游戏 ——左偏树

    [题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...

  9. bzoj 1455: 罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 668  Solved: 247[Submit][Status] Descriptio ...

随机推荐

  1. c++, class的大小

    不为类.对象的函数分配空间: 在类中如果有virtual声明的虚函数,则会隐藏一个指针,该指针指向虚函数表,这对于纯虚函数也是一样: 对于虚继承,还有一个指向父类的指针,该指针为指向虚基类的指针(Po ...

  2. Eclipse用法和技巧二十八:Eclipse插件Easy Explore的今世

    先说明一下easyexplore插件的功能,easyexplore是一个类似于 Windows Explorer的Eclipse插件,它可以帮助你在不退出Eclipse的环境下迅速浏览本地文件系统. ...

  3. 基于visual Studio2013解决面试题之1003字符串逆序

     题目

  4. 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)

    Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...

  5. 【linux】内核+文件系统下载到开发板

    K开发 欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...

  6. 486E - LIS of Sequence(LIS)

    题意:给一个长度为n的序列.问每一个数关于序列的LIS(longest increasing subsequence)是什么角色. 这里分了三种: 1.此数没有出如今随意一条LIS中 2.此数出如今至 ...

  7. 基于visual Studio2013解决面试题之0207单词翻转

     题目

  8. Boost下载安装编译配置使用指南(含Windows和Linux

    理论上,本文适用于boost的各个版本,尤其是最新版本1.45.0:适用于各种C++编译器,如VC6.0(部分库不支持),VS2003,VS2005,VS2008,gcc,C++ Builder等.先 ...

  9. hadoop学习之ZooKeeper

    1. 什么是ZooKeeper? ZooKeeper是一组工具,用来配置和支持分布式调度. 它能处理分布式应用的“部分失败”问题. 什么是部分失败? 部分失败是分布式处理系统的固有特征,即发送者无法知 ...

  10. MMA7455加速度传感器測量角度

    使用加速度传感器应该注意几点: 第一:确保你的IIC是正确的: 第二,首先必须校准系统,校准方法,例如以下:将7455平放,保证z轴向下,这是假设系统是Ok的,那么x轴输出为0,y轴输出为0,z轴输出 ...