IncDec Sequence(差分)

探讨如何通过差分操作使得长度为 n 的数列在不超过10^5的限制下,所有元素变得相同,最少需要的操作次数以及可能的最终结果数量。分析涉及正数和负数的和,以及不同情况下的操作策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个长度为 n(n≤105)(n \leq 10^5 )(n≤105) 的数列a1,a2,…,an{a_1,a_2,…,a_n}a1​,a2​,…,an​,每次可以选择一个区间 [l,r],使下标在这个区间内的数都加一或者都减一。
求至少需要多少次操作才能使数列中的所有数都一样,并求出在保证最少次数的前提下,最终得到的数列可能有多少种。
输入描述:

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

输出描述:

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

输入
4
1
1
2
2

输出
1
2

#求原序列的差分数组,题意等价于将差分数组里在[2, n]区间内的数都变为零所需要的操作次数。
令正数的和为 sumPOS, 负数的绝对值之和为 sumPos。
接下来分类讨论:

1.若差分数组里有正数和负数,那么我们尽量选择绝对值相等的一对数进行操作。
2.若只有正数或负数,那么对此项与第一项或者此项与第 n + 1项进行操作。

由此得出最少操作 min(sumPos, sumNeg) + abs(sumPos - sumNeg)次, 即max(sumPos, sumNeg)。

根据类型2,可得结果的种类数为 abs(sumPos - sumNeg) + 1。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;
const int maxn = 1e6 + 10;
int db[maxn];
int a[maxn];

int main()
{
	int n;
	cin >> n;
	ll sumPos = 0, sumNeg = 0;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	db[1] = a[1];
	db[n + 1] = -a[n];
	for(int i = 2; i <= n; i++)
		db[i] = a[i] - a[i - 1];
	for(int i = 2; i <= n; i++)
		if(db[i] > 0)
			sumPos += db[i];
		else if(db[i] < 0)
			sumNeg -= db[i];
	
//	cout << max(sumPos, sumNeg) << endl;
	cout << min(sumPos, sumNeg) + abs(sumPos - sumNeg) << endl;
	cout << abs(sumPos - sumNeg) + 1 << endl;
	return 0;
}
### Increment Decrement Sequence Implementation In programming, an increment-decrement sequence typically refers to operations that involve increasing or decreasing values by a fixed amount iteratively. This concept can be implemented using loops and arithmetic operators. A common way to implement such sequences involves initializing variables with start values and then applying increments or decrements within loop structures: ```cpp // C++ example demonstrating inc/dec sequence #include <iostream> int main() { int value = 0; // Initial value while (value < 5) { // Loop condition std::cout << "Incrementing: " << ++value << '\n'; // Pre-increment operation } while (value > 0) { std::cout << "Decrementing: " << --value << '\n'; // Pre-decrement operation } return 0; } ``` For more complex scenarios involving collections like arrays or vectors, iterator-based approaches provide flexibility when traversing elements forward or backward[^2]: ```cpp std::vector<int> numbers{1, 2, 3, 4}; for(auto it = numbers.begin(); it != numbers.end(); ++it){ *it += 1; // Increment each element via dereferenced iterator } for(auto rit = numbers.rbegin(); rit != numbers.rend(); ++rit){ *rit -= 1; // Decrement from reverse direction similarly } ``` Iterators allow for efficient traversal over container types without exposing underlying data structure details. When working with standard template library containers, iterators offer powerful ways to manipulate sequences through generic algorithms provided by the C++ Standard Library. Regarding specific implementations of `incdec` as mentioned in some contexts, these might refer to specialized functions designed around particular requirements not covered here directly but following similar principles outlined above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值