[NCPC2021] Antenna Analysis | 思维递推

简介: 题目描述Åke has heard that there may be some suspicious 5G radiation in his city. To test this, he uses the antenna on his roof to measure the 5G level each day. However, he does not know how he should analyze the data.

题目描述


Åke has heard that there may be some suspicious 5G radiation in his city. To test this, he uses the antenna on his roof to measure the 5G level each day. However, he does not know how he should analyze the data.

We are given the measurements for n consecutive days as a list of numbers x1 , . . . , xn (where xi denotes the measurement for day i) and a constant c that measures how much Åke expects the radiation to vary from day to day. We want to find, for each day i, the most significant difference between the measurement on day i and any earlier day, after the expected variations are taken into account. More precisely, the goal is to find the maximum value of |xi − xj | − c·|i − j| where j ≤ i. I.e., we want to find a large difference in 5G level that has happened recently.


输入


The first line of input contains the two integers n and c (1 ≤ n ≤ 4 · 105 , 1 ≤ c ≤ 106 ), the number of measurements and expected day-to-day variation. The second input line contains the n integers x1 , x2 , . . . , xn (1 ≤ xi ≤ 106 for i = 1, 2, . . . , n), giving the measurements of the n days.


输出


Output n integers y1 , . . . , yn , where yi is the most significant difference on day i.


样例输入 Copy


5 1
2 7 1 5 4


样例输出 Copy


0 4 5 3 1


题意:


对每一个位置,输出:

∣ xi − xj ∣ − c⋅∣i − j∣ ,  j ≤ i

只需要维护最大值最小值即可


int a[maxn];
int main() {
  ll n = read,c = read;
  ll mini = 0, maxx = 0;
  ll x = read;
  maxx = -x;
  mini = x;
  a[1] = 0;
  for(int i=2; i<=n; i++) {
    ll x = read;
    maxx -= c,mini -= c;
    a[i] = max(mini-x,max(0LL,maxx+x));
    maxx = max(maxx,-1 * x);
    mini = max(mini, x);
  }
  for(int i=1; i<=n; i++) {
    printf("%d ",a[i]);
  }
  return 0;
}


队友代码:

const int maxn=4e5+7;
ll n,c,a[maxn],b[maxn],mx,mi;
int main(){
  n=read(); c=read();
  for(int i=0;i<n;i++)  a[i]=read();
  mx=-a[0];
  mi=a[0];
  for(int i=1;i<n;i++){
    mx-=c;  mi-=c;
    mx=max(mx,-a[i-1]-c);
    mi=max(mi,a[i-1]-c);
    b[i]=max(a[i]+mx,-a[i]+mi);
    if(b[i]<0)  b[i]=0;
  }
  for(int i=0;i<n;i++)  cout<<b[i]<<' ';
}



目录
相关文章
|
3月前
|
算法
第七章 回溯算法理论基础
第七章 回溯算法理论基础
32 0
|
7月前
|
存储 缓存 算法
动归和递归算法讲解
动归和递归算法讲解
|
7月前
|
存储 算法 数据挖掘
螺旋矩阵 II:从理论到实践的五种算法解析
螺旋矩阵 II:从理论到实践的五种算法解析
|
8月前
|
算法 Java C语言
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-913 二元函数
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-913 二元函数
44 0
|
算法 IDE Java
【洛谷算法题】P1001-A+B Problem【入门1顺序结构】
【洛谷算法题】P1001-A+B Problem【入门1顺序结构】
|
人工智能
P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心思想加一点特别的循环)
P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心思想加一点特别的循环)
81 0
2022小美赛B题The Genetic Process of Sequences序列的遗传过程思路分享
2022小美赛B题The Genetic Process of Sequences序列的遗传过程思路分享
2022小美赛B题The Genetic Process of Sequences序列的遗传过程思路分享
CF979B Treasure Hunt(贪心 思维)
CF979B Treasure Hunt(贪心 思维)
98 0
CF979B Treasure Hunt(贪心 思维)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
137 0
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
135 0