3043: IncDec Sequence
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 520 Solved: 299
[ 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
Source
嗯,,没有想到正解。。是不是太弱了
先对原数列进行差分处理
这样区间加法/减法只影响头尾的值
统计差分后,除了最后一位,正数的和s1,负数的和的绝对值s2
第一问就是max(s1,s2),因为只要有一个正数和一个负数,两数就可以相消,而剩下的只能和边界消了
第二问是abs(s1 - s2) + 1,也就是你和边界消了几次,因为差分数列的最终情况是只有a[n] > 0
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<bitset>
using namespace std;
const int maxn = 1E5 + 10;
typedef long long LL;
int n,a[maxn];
LL s1,s2;
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
for (int i = 1; i < n; i++)
if (a[i] - a[i+1] > 0) s1 += 1LL*(a[i] - a[i+1]);
else s2 += 1LL*(a[i+1] - a[i]);
cout << max(s1,s2) << endl << abs(s1 - s2) + 1LL;
return 0;
}