[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]<<' ';
}



目录
相关文章
|
2月前
|
存储 算法 人工智能
【算法设计与分析】——动态规划算法
【算法设计与分析】——动态规划算法
53 1
【算法设计与分析】——动态规划算法
|
11月前
剑指offer_发散思维---求1+2+3+...+n
剑指offer_发散思维---求1+2+3+...+n
37 0
|
机器学习/深度学习 开发框架 编解码
动手学强化学习(三):动态规划算法 (Dynamic Programming)
动态规划(dynamic programming)是程序设计算法中非常重要的内容,能够高效解决一些经典问题,例如背包问题和最短路径规划。动态规划的基本思想是将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到目标问题的解。动态规划会保存已解决的子问题的答案,在求解目标问题的过程中,需要这些子问题答案时就可以直接利用,避免重复计算。本章介绍如何用动态规划的思想来求解在马尔可夫决策过程中的最优策略。
173 0
动手学强化学习(三):动态规划算法 (Dynamic Programming)
|
人工智能
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
86 0
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
99 0
Codeforces1153——D. Serval and Rooted Tree(思维好题+dfs+贪心)
|
机器学习/深度学习 人工智能 决策智能
LDU2019第一次质检——CoolGuang‘s Good Game(思维+区间DP)
LDU2019第一次质检——CoolGuang‘s Good Game(思维+区间DP)
85 0
|
人工智能 算法
每天一道 CodeForces 构造/思维题 (day1)
每天一道 CodeForces 构造/思维题 (day1)