P7994

· · 题解

P7994

在阅读本题解之前,请先思考 铺设道路。 之后,本题会更加明了。

分析 :

此题与 铺设道路 非常相似,唯一的不同在于分为升温,降温两种方式。可以证得,分开加减一定比统一升降,再单独操作步骤来的少,所以此题分成两个两个数组存放,与标准量相减,取绝对值处理。(不是我笼统,真的和铺设道路一模一样)。

其余看代码吧,还有一些处理思想,也在代码里。

Code :

1,普通 铺设道路 :

#include<iostream>
using namespace std;
long long n , a[100001] , ans;
int main()
{
    cin>>n;
    for( int i = 1 ; i <= n ; i ++ )        cin>>a[i];
    for( int i = 1 ; i <= n ;  i ++ )       if( a[i] > a[i-1])ans += a[i] - a[i-1];
    cout<<ans;
    return 0;
}

2,此题:

#include<bits/stdc++.h>
using namespace std;
long long n;
long long a[100001] , b[100001];
long long now[100001] , want[100001];
long long ans = 0;
int main()
{
    cin>>n;
    for(int i = 1 ; i<= n ; i++)    cin>>want[i];
    for(int i = 1 ; i<= n ; i++)
    {
        cin>>now[i];
        long long x = want[i] - now[i];
        if(x >= 0)a[i] = x;
        else b[i] = -x;
    }
    for(int i = 1 ; i<= n ; i++)
    {
        if(a[i] > a[i-1])ans += a[i] - a[i-1]; 
    }
    for(int i = 1 ; i<= n ; i++)
    {
        if(b[i] > b[i-1])ans += b[i] - b[i-1];
    }
    cout<<ans;
    return 0;   
} 

ps:可以做一些对比。