题目链接
思维 + 差分 + 线段树
分析
把这个式子和数组形象化一下;
如果把问题放到一个二维坐标系,数组下标是横坐标,那么原数组对应的值是纵坐标;
那么这个式子就是求两点之间最大斜率。
直接上结论: 最大的斜率一定是两个相邻的点.
证明: 反证法, 考虑这样三个相邻的数, a1 a2 a3(a1 < a2 < a3);
k1 = a2 - a1;
k2 = a3 - a2;
k3 = (a3 - a1) / 2;
假设最大斜率不是相邻两个点,即 k3 同时 > k1 k2;
k3 > k1 <=> -a2 > -(a3+a1)/2; (式子1)
k3 > k2 <=> (a3 - a1) / 2 > a3 - a2 然后用(式子1)放缩一下
=> a2 < a1 (矛盾)
证明完毕.
因此我们可以维护原数组的差分数组的最大值;
修改的话注意第一个点和最后一个点的特判! ! !
注意有多组数据输入
代码
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
// #include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second
#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double> > PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;
const int maxn = 1e6 + 7;
const int N = 2010 + 7;
const int M = 1e6 + 7;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
// const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}
int a[maxn];
int d[maxn];
struct node
{
int l,r;
int v;
}tr[maxn*4];
int n,m;
void pushup(int u)
{
tr[u].v = max(tr[u<<1].v, tr[u<<1|1].v);
}
void build(int u,int l,int r)
{
tr[u] = {l,r};
if(l == r)
{
tr[u].v = d[l];
return;
}
int mid = l + r >> 1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
pushup(u);
}
void update(int u,int idx,int x) //单点修改
{
if(tr[u].l == idx && tr[u].r == idx)
{
tr[u].v = x;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if(idx <= mid) update(u<<1, idx, x);
else update(u<<1|1, idx, x);
pushup(u);
}
int query(int u,int l,int r)
{
if(l <= tr[u].l && r >= tr[u].r) return tr[u].v;
int mid = tr[u].l + tr[u].r >> 1;
int ans = -inf;
if(l <= mid) ans = max(ans,query(u<<1, l, r));
if(r > mid) ans = max(ans,query(u<<1|1, l, r));
return ans;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i=1;i<=n;i++) ire(a[i]);
for(int i=1;i<=n;i++) d[i] = a[i] - a[i-1];
d[0] = d[1] = -inf;
build(1,1,n);
ire(m);
while(m--)
{
int x,y;
iire(x,y);
a[x] = y;
int num1 = a[x] - a[x-1];
int num2 = a[x+1] - a[x];
if(x != 1) update(1,x,num1);
if(x != n) update(1,x+1,num2);
printf("%.2lf\n",(double)query(1,1,n));
}
}
return 0;
}