• [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.
    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
    

    题意:

    对每一个位置,输出:
    ∣ x i − x j ∣ − c ⋅ ∣ i − j ∣ ,      j ≤ i |xi − xj | − c·|i − j|,\ \ \ \ j ≤ i xixjcij,    ji

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

    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]<<' ';
    }
    
    
  • 相关阅读:
    人工智能,垂直领域
    西方企业该向中国式创新学什么?
    [Leetcode 6] ZigZag问题的一种新思路
    Unknown column &#39;*&#39; in &#39;field list&#39; 异常解决
    多媒体——图像文件大小的计算
    Lucky Number
    win7 64位系统下进入debug
    Chromium网页DOM Tree创建过程分析
    做好长期奋斗的准备
    IP寻址和子网运算
  • 原文地址:https://www.cnblogs.com/PushyTao/p/16196749.html
Copyright © 2020-2023  润新知