E - Little Elephant and Shifts

思路:

  一次函数线段树(疯狂debug);

  b不断循环左移,判断每次最小的|i-j|,a[i]=b[j];

  仔细观察发现,每个bi移动时,|i-j|呈现多个一次函数图像;

  所以用线段树来维护这些一次函数图像;

  线段树维护一次函数,当两个函数在区间没有交点时;

  判断哪个在图像较下的位置,然后覆盖;

  当有交点时,保留最优,将另一条传下去;

  时间复杂度O(nlog^2n);

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005
#define INF 0x7fffffff struct TreeNodeType {
int l,r,k,b,mid; bool if_;
};
struct TreeNodeType tree[maxn<<]; int n,ai[maxn],p[maxn],X; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void tree_build(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r,tree[now].mid=l+r>>;
if(l==r) return ;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
} double com(int k1,int b1,int k2,int b2)
{
if(k1==k2) return ;
return (double)(b2-b1)/(double)(k1-k2);
} void tree_down(int now,int k,int b)
{
if(!tree[now].if_)
{
tree[now].if_=true,tree[now].k=k,tree[now].b=b;
return ;
}
double x=com(k,b,tree[now].k,tree[now].b);
if(x<=tree[now].l||x>=tree[now].r)
{
double mid=(double)(tree[now].l+tree[now].r)/;
if(mid*k+b<mid*tree[now].k+tree[now].b) tree[now].k=k,tree[now].b=b;
return ;
}
if(x<=tree[now].mid)
{
if(k>tree[now].k) tree_down(now<<,k,b);
else
{
tree_down(now<<,tree[now].k,tree[now].b);
tree[now].k=k,tree[now].b=b;
}
}
else
{
if(k<tree[now].k) tree_down(now<<|,k,b);
else
{
tree_down(now<<|,tree[now].k,tree[now].b);
tree[now].k=k,tree[now].b=b;
}
}
} void tree_add(int now,int l,int r,int k,int b)
{
if(tree[now].l==l&&tree[now].r==r)
{
if(tree[now].if_)
{
if(k==tree[now].k&&b==tree[now].b);
else tree_down(now,k,b);
}
else tree[now].if_=true,tree[now].k=k,tree[now].b=b;
return ;
}
if(l>tree[now].mid) tree_add(now<<|,l,r,k,b);
else if(r<=tree[now].mid) tree_add(now<<,l,r,k,b);
else
{
tree_add(now<<,l,tree[now].mid,k,b);
tree_add(now<<|,tree[now].mid+,r,k,b);
}
} void tree_query(int now,int to)
{
if(tree[now].if_) X=min(X,tree[now].k*to+tree[now].b);
if(tree[now].l==tree[now].r) return ;
if(to<=tree[now].mid) tree_query(now<<,to);
else tree_query(now<<|,to);
} int main()
{
in(n);int pos,debug;
for(int i=;i<=n;i++) in(ai[i]);
for(int i=;i<=n;i++) in(pos),p[pos]=i;
tree_build(,,n);
for(int i=;i<=n;i++)
{
if(i<p[ai[i]])
{
tree_add(,,p[ai[i]]-i+,-,p[ai[i]]+-i);
if(i!=) tree_add(,p[ai[i]]-i+,p[ai[i]],,i-p[ai[i]]-);
if(p[ai[i]]!=n) tree_add(,p[ai[i]]+,n,-,n-i+p[ai[i]]+);
}
if(i>p[ai[i]])
{
tree_add(,,p[ai[i]],,i-p[ai[i]]-);
tree_add(,p[ai[i]]+,p[ai[i]]+n-i+,-,n-i+p[ai[i]]+);
if(i-p[ai[i]]>) tree_add(,p[ai[i]]+n-i+,n,,i-n-p[ai[i]]-);
}
if(i==p[ai[i]])
{
tree_add(,,i,,-);
if(i!=n) tree_add(,i+,n,-,n+);
}
}
for(int i=;i<=n;i++) X=INF,tree_query(,i),printf("%d\n",X);
return ;
}

AC日记——Little Elephant and Shifts codeforces 221e的更多相关文章

  1. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  2. AC日记——Little Elephant and Numbers codeforces 221b

    221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...

  3. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  4. AC日记——Little Elephant and Problem codeforces 221c

    221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  5. AC日记——Sagheer and Nubian Market codeforces 812c

    C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...

  6. AC日记——Red and Blue Balls codeforces 399b

    399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...

  7. AC日记——Andryusha and Colored Balloons codeforces 780c

    C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...

  8. AC日记——The Child and Sequence codeforces 250D

    D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...

  9. Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

    C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/pro ...

随机推荐

  1. build dynamic libraries for iOS and load them at runtime

    编译了libmt.dylib, 和 test 程序调用,均正常.在xcode中显示调用正常,隐式调用则出现问题. 提示 dyld: Library not loaded. 即使存在在/usr/lib/ ...

  2. 查询数据库里当前用户下的所有表的总共数据sql

    select t.table_name,t.num_rows from user_tables t select sum(num_rows) from user_tables t

  3. Active Directory 域服务 (AD DS) 虚拟化

    TechNet 库 Windows Server Windows Server 2012 R2 和 Windows Server 2012 服务器角色和技术 Active Directory Acti ...

  4. Pascal小游戏 贪吃蛇

    一段未完成的Pascal贪吃蛇 说这段代码未完成其实是没有源代码格式化,FP中一行最多只有255字符宽. uses crt; const screenwidth=50; screenheight=24 ...

  5. vue.js中created方法作用

    这是它的一个生命周期钩子函数,就是一个vue实例被生成后调用这个函数.一个vue实例被生成后还要绑定到某个html元素上,之后还要进行编译,然后再插入到document中.每一个阶段都会有一个钩子函数 ...

  6. 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'

    学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...

  7. 【Linux】wc :字数统计命令

    wc :(Word Count) 统计每个传入文件中行数.词数与字节数 $ wc py_this # 三个数字分别对应行数.词数和字节数 21 144 857 py_this $ wc py_this ...

  8. Ubuntu下禁用笔记本自带键盘

    想要禁用笔记本自带键盘(Ubuntu)只要2条命令. 1. 打开终端,输入: xinput list ⎡ Virtual core pointer id=2 [master pointer (3)] ...

  9. ZOJ 3606 Lazy Salesgirl ( 线段树 + 思路 )

    卖切糕的小女孩 http://www.cnblogs.com/wuyiqi/archive/2012/04/28/2474672.html #include <cstdio> #inclu ...

  10. jQuery基础知识点(下)

    在实际开发中,jQuery的实践性非常强大.上一篇本人已经整理出了一部分基础知识点,该文即是对以上的补充和扩展. 1.表单值的操作 //获取文本框的值 //txt.value var val = $( ...