IncDec Sequence
3043: IncDec Sequence
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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
1
1
2
2
Sample Output
1
2
HINT
对于100%的数据,n=100000,0<=ai<2147483648
Code
View 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; }
I come, I see, I conquer!