AC日记——Little Elephant and Shifts codeforces 221e
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的更多相关文章
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- AC日记——Little Elephant and Numbers codeforces 221b
221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- AC日记——Little Elephant and Problem codeforces 221c
221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——Sagheer and Nubian Market codeforces 812c
C - Sagheer and Nubian Market 思路: 二分: 代码: #include <bits/stdc++.h> using namespace std; #defin ...
- AC日记——Red and Blue Balls codeforces 399b
399B - Red and Blue Balls 思路: 惊讶的发现,所有的蓝球的消除都是独立的: 对于在栈中深度为i的蓝球消除需要2^i次操作: 代码: #include <cstdio&g ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- AC日记——The Child and Sequence codeforces 250D
D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- 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.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...
- Jforum环境搭建
前提:搭建好JDK.JRE.Tomcat.数据库 1.之前安装了Navicat Premium,所以直接用这个创建名为jforum的MySQL数据库,默认密码为空,记得设置密码,因为Jforum要用到 ...
- Python全栈(一)编程语言介绍
一.编程语言介绍 程序是计算机能读懂的语言,是人和计算机沟通的方式. 计算机无法理解符号,只能理解0,1的二进制. 计算机内的运行状态就像灯泡的开关一样来表示各庄状态,两个灯泡能表示4种状态,无数的灯 ...
- Python之日志处理 logging模块
Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四 ...
- 普通用户操作tomcat项目时报:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program
在使用普通用户更新tomcat项目适合出现这个信息,Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At ...
- ftrace 简介
ftrace 简介 ftrace 的作用是帮助开发人员了解 Linux 内核的运行时行为,以便进行故障调试或性能分析. 最早 ftrace 是一个 function tracer,仅能够记录内核的函数 ...
- 【转】通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...
- 使用“\n\t”将多行字符串拼接起来
以前js拼接字符串有好多 \n \t 不使用ES6 使用"\n\t"将多行字符串拼接起来: var roadPoem = 'Then took the other, as just ...
- REST Web 服务(二)----JAX-RS 介绍
1. 什么是JAX-RS? JAX-RS——Java API for RESTful Web Services,是为 Java 程序员提供的一套固定的接口(Java API),用于开发表述性状态转移( ...
- BZOJ - 2728 与非
题意: 给定N个数,一个数k和一个范围[L,R].每个数可以使用任意次,k表示与非不超过k位.求出范围内有多少个数可以由他们的与非和表示. 题解: m个数进行NAND,最终的数二进制下某一位如果为1, ...