【BZOJ3043】IncDec Sequence

3043: IncDec Sequence

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 13   Solved: 6
[ Submit][ Status][ Discuss]

Description

给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一。
问至少需要多少次操作才能使数列中的所有数都一样,并求出在保证最少次数的前提下,最终得到的数列有多少种。

Input

第一行一个正整数n 
接下来n行,每行一个整数,第i+1行的整数表示ai。

Output

第一行输出最少操作次数
第二行输出最终能得到多少种结果

Sample Input

4
1
1
2
2

Sample Output


1
2

HINT

对于100%的数据,n=100000,0<=ai<2147483648

Code

#include <cstdio>
#define maxn 1000000
int a[maxn], b[maxn], n, i;
long long x = 0, y = 0, ans;
using namespace std;
int main()
{
	scanf("%d", &n); b[1] = 0;
	for (i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (i = 2; i <= n; i++)
	{
		b[i] = a[i] - a[i - 1];
		if (b[i] > 0) x += b[i]; else y -= b[i];
	}
	if (x > y) {printf("%lld\n", x); ans = x - y;} else {printf("%lld\n", y); ans = y - x;}
	printf("%lld\n", ans + 1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值