luogu P4756 Added Sequence(凸包+思维)
一眼望去不会。
考虑问题中的\(f(i,j)=|\sum_{p=i}^{j}a_p |\)的实际意义。
其实就是前缀和相减的绝对值。
\(f(i,j)=|\ sum[j]-sum[i-1]\ |\)
\(f(i,j)=max(sum[j]-sum[i-1],sum[i-1]-sum[j])\)
那加上x呢。
\(f(i,j)=max[(sum[j]+xj)-(sum[i-1]+x(i-1)),(sum[i-1]+x(i-1))-(sum[j]+xj)]\)
\(sum[i]+xi\)联想到直线。
我们把这些直线放到坐标系里。
(这个图是从别人那里蒯过来的)

这是把样例转为坐标系中直线的效果图。
对于一个 \(x\) ,我们询问的实际上就是横坐标为x的两条直线纵坐标的差的最大值。
所以我们用半平面交的方法维护出上下两个凸包。
因为题目中的强制在线是假的(实际上处理出的\(x\in [-2n,2n]\))。
又因为凸包上的线段斜率是单调的,我们可以直接处理出所有 \(x\) 时的答案。
然后这题就解决了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int N=201000;
const double eps=1e-8;
double sum[N];
struct node{
double x,y;
node(double xx=0,double yy=0){
x=xx;y=yy;
}
};
int top1,top2;
struct Line{
node u,v;
double w;
}a[N],stack1[N],stack2[N];
node operator +(node a,node b){
return node(a.x+b.x,a.y+b.y);
}
node operator -(node a,node b){
return node(a.x-b.x,a.y-b.y);
}
node operator *(node a,double b){
return node(a.x*b,a.y*b);
}
double chaji(node a,node b){
return a.x*b.y-a.y*b.x;
}
node jiao(Line a,Line b){
double A=chaji(b.u-a.u,b.v-a.u);
double B=chaji(b.v-a.v,b.u-a.v);
return a.u+(a.v-a.u)*(A/(A+B));
}
bool judge1(Line a,Line b,Line c){
node x=jiao(b,c);
if(chaji(x-a.u,a.v-a.u)+eps>=0)return true;
else return false;
}
bool judge2(Line a,Line b,Line c){
node x=jiao(b,c);
if(chaji(x-a.u,a.v-a.u)+eps<0)return true;
else return false;
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
int n,m;
double w(Line a,double x){
// cout<<a.w<<endl;
return a.u.y+a.w*(x-(-2.0*n));
}
map<int,double>ans;
long long lastans;
int main(){
n=read();m=read();
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+read();
for(int i=1;i<=n;i++){
a[i].u=node(-2.0*n,sum[i]+i*-2.0*n);
a[i].v=node(2.0*n,sum[i]+i*2.0*n);
a[i].w=i;
}
a[0].u=node(-2.0*n,0);a[0].v=node(2.0*n,0);
a[0].w=0;
for(int i=0;i<=n;i++){
while(top1>1&&judge1(a[i],stack1[top1-1],stack1[top1]))top1--;
stack1[++top1]=a[i];
}
for(int i=n;i>=0;i--){
while(top2>1&&judge2(a[i],stack2[top2-1],stack2[top2]))top2--;
stack2[++top2]=a[i];
}
int now1=1;int now2=1;
for(int i=-2*n;i<=n*2;i++){
while(now1<top1&&w(stack1[now1+1],i)>=w(stack1[now1],i))now1++;
while(now2<top2&&w(stack2[now2+1],i)<=w(stack2[now2],i))now2++;
double A=w(stack1[now1],i);
double B=w(stack2[now2],i);
ans[i]=A-B;
}
lastans=0;
while(m--){
int x=read();
x=(x+lastans)%(4*n+1)-2*n;
lastans=ans[x];
printf("%.0lf\n",ans[x]);
}
return 0;
}
luogu P4756 Added Sequence(凸包+思维)的更多相关文章
- BZOJ1345 Baltic2007 序列问题Sequence 【思维题】*
BZOJ1345 Baltic2007 序列问题Sequence Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用 ...
- luogu 2742 二维凸包
链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...
- BZOJ 1069 Luogu P4166 最大土地面积 (凸包)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=1069 (luogu)https://www.luogu.org/probl ...
- HDU 2062:Subset sequence(思维)
Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- Serval and Parenthesis Sequence【思维】
Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...
- luogu P4724 模板 三维凸包
LINK:三维凸包 一个非常古老的知识点.估计也没啥用. 大体上了解了过程 能背下来就背下来吧. 一个bf:暴力枚举三个点 此时只需要判断所有的点都在这个面的另外一侧就可以说明这个面是三维凸包上的面了 ...
- luogu P3829 [SHOI2012]信用卡凸包 凸包 点的旋转
LINK:信用卡凸包 当 R==0的时候显然是一个点的旋转 之后再求凸包即可. 这里先说点如何旋转 如果是根据原点旋转的话 经过一个繁杂的推导可以得到一个矩阵. [cosw,-sinw] [sinw, ...
- Funny Positive Sequence (思维+前缀)
There are n integers a 1,a 2,…,a n-1,a n in the sequence A, the sum of these n integers is larger th ...
随机推荐
- Python - 字符串模板的安全替换(safe_substitute) 具体解释
字符串模板的安全替换(safe_substitute) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 ...
- PostgreSQL sum typecasting as a bigint
https://stackoverflow.com/questions/20203081/postgresql-sum-typecasting-as-a-bigint Question: I am d ...
- RubyMine中自动完成只输入部分字母
RubyMine中自动完成只输入部分字母 1,有下划线情况(其实看第二点跟下划线就关系不大了) 对于attr_reader之类的输入,输入attr之后,下划线可以不输入,然后输入r或者e都可以出来, ...
- CSS3 timing-function: steps()介绍
在应用 CSS3 渐变/动画时.有个控制时间的属性 <timing-function>.它的取值中除了经常使用到的三次贝塞尔曲线以外,还有个steps() 函数. steps 函数指定了一 ...
- hadoop权威指南(第四版)要点翻译(5)——Chapter 3. The HDFS(5)
5) The Java Interface a) Reading Data from a Hadoop URL. 使用hadoop URL来读取数据 b) Although we focus main ...
- SQL SERVER的浮点数类型及与C#的对应关系
SQL SERVER: float 与 real 7位数或15位数.这里说的位数,不是指小数位,而是包括整数和小数在内的位数. float的位数是多少,要看float[(n)]里的n数值是多少. n ...
- 循环神经网络(RNN, Recurrent Neural Networks)——无非引入了环,解决时间序列问题
摘自:http://blog.csdn.net/heyongluoyao8/article/details/48636251 不同于传统的FNNs(Feed-forward Neural Networ ...
- Spark中常用的算法
Spark中常用的算法: 3.2.1 分类算法 分类算法属于监督式学习,使用类标签已知的样本建立一个分类函数或分类模型,应用分类模型,能把数据库中的类标签未知的数据进行归类.分类在数据挖掘中是一项重要 ...
- [BZOJ 2100] Apple Delivery
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2100 [算法] Answer = min{ dist(PB,PA1) + dist( ...
- JS——BOM操作(基本用法与实现:open()、close()、scrollTop等了解)
(1)window.open() 定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口 语法 window.open(URL,name,specs,replace) [默认填 ...