类似题目
主要思路
有错误劳烦指出
看了好长时间的题解终于明白了 呜呜呜
代码
package day;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class 增减序列 {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader(System.in));
int n=Integer.parseInt(bufferedReader.readLine());
int a[]=new int [n+1];
long b[]=new long [(int) (1e5+10)];
for(int i=1;i<=n;i++){
a[i]=Integer.parseInt(bufferedReader.readLine());
}
b[1]=a[1];
long s1=0;
long s2=0;
for (int i = 2; i <=n; i++) {
b[i]=a[i]-a[i-1];
if(b[i]>0){
s1=s1+b[i];
}
if(b[i]<0){
s2=s2+b[i];
}
}
long s=s2*-1;
System.out.println(Math.max(s1, s));
long k =Math.abs(s1-s)+1;
System.out.println(k);
}
}