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. nodejs安装&bower 安装

    1.进入官网下载:https://nodejs.org/en/ 2.直接进行安装,可以将安装路径设置为:D:\nodejs 3.进入node.js command prompt 命令窗口 4.检测是否 ...

  2. Linux命令学习总结(一)

    命令 -选项 参数 如果选项是一个单词时,选项前面要加2个- modprobe -r pcspkr   在终端中输入的时候有声音,可以用这个命令屏蔽声音 ,需要root权限 useradd userd ...

  3. wget下载https文件,服务器可以虚拟机中不行的问题

    用wget下载一个图片资源(https协议),在服务器上可以,但在本机的虚拟机中卡在下面这里了: [root@localhost ~]# wget 'https://gp1.wac.edgecastc ...

  4. SpringMVC 整合 kaptcha(验证码功能)

    一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...

  5. python学习总结----时间模块 and 虚拟环境(了解)

    time - sleep:休眠指定的秒数(可以是小数) - time:获取时间戳 # 获取时间戳(从1970-01-01 00:00:00到此刻的秒数) t = time.time() print(t ...

  6. Oracle 插入数据时获取系统时间

    insert into table_xx (xx,dateTime) values( 'xx',sysdate) 这个sysdate 相当于sql server中的GETDATE()函数 另to_ch ...

  7. UPX压缩

    什么是UPX UPX (the Ultimate Packer for eXecutables)是一款先进的可执行程序文件压缩器,压缩过的可执行文件体积缩小50%-70% ,这样减少了磁盘占用空间.网 ...

  8. linux的screen常用命令使用记录

    新建screen screen 或者 screen -S name - name用于指定一个screen的名字,默认系统随机 暂时退出当前screen ctrl + a + d - 同时按住这三个键即 ...

  9. 软工实践 - 第十六次作业 Alpha 冲刺 (7/10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10013959.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...

  10. PHP路径相关 dirname,realpath,__FILE__

    ​比如:程序根目录在:E:\wamp\www 中 1.    __FILE__   当前文件的绝对路径 如果在index.php中调用 则返回  E:\wamp\www\index.php 下面再看一 ...