Acwing-100-IncDec搴忓垪(宸垎)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Acwing-100-IncDec搴忓垪(宸垎)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%b8%8b%e6%a0%87' title='涓嬫爣'>涓嬫爣
code typedef its 閫夋嫨 names space abs print閾炬帴:
https://www.acwing.com/problem/content/102/
棰樻剰:
缁欏畾涓€涓暱搴︿负 n 鐨勬暟鍒?a1,a2,鈥?an锛屾瘡娆″彲浠ラ€夋嫨涓€涓尯闂?[l,r]锛屼娇涓嬫爣鍦ㄨ繖涓尯闂村唴鐨勬暟閮藉姞涓€鎴栬€呴兘鍑忎竴銆?/p>
姹傝嚦灏戦渶瑕佸灏戞鎿嶄綔鎵嶈兘浣挎暟鍒椾腑鐨勬墍鏈夋暟閮戒竴鏍凤紝骞舵眰鍑哄湪淇濊瘉鏈€灏戞鏁扮殑鍓嶆彁涓嬶紝鏈€缁堝緱鍒扮殑鏁板垪鍙兘鏈夊灏戠銆?/p>
鎬濊矾:
灏辨槸灏嗘煡鍒嗘暟缁勯櫎浜嗙涓€浣嶉兘缃负0, 鐨勬搷浣滄鏁?鍥犱负姣忔鍙兘鍔?鎴栧噺涓€.鍙互浼樺厛閰嶅涓€姝d互鍋惰礋,鍐嶅鍓╀綑鐨勫€艰繘琛屽鐞?
浠g爜:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
int a[MAXN], Sub[MAXN];
int n;
int main()
{
LL ne = 0, po = 0;
scanf("%d", &n);
for (int i = 1;i <= n;i++)
{
scanf("%d", &a[i]);
Sub[i] = a[i]-a[i-1];
if (i <= 1)
continue;
if (Sub[i] > 0)
po += Sub[i];
else
ne += abs(Sub[i]);
}
LL oth = abs(po-ne);
printf("%lld
%lld
", max(ne, po), oth+1);
return 0;
}
以上是关于Acwing-100-IncDec搴忓垪(宸垎)的主要内容,如果未能解决你的问题,请参考以下文章