题目:![](https://img-blog.csdnimg.cn/852e0f7af79040e986c7b396bfc6266a.png)
样例:
|
0 1 3 2 2 7 -1 |
思路:
双指针,思维可模拟的题,这题目的意思是 只删除前面和后面,我们应该联想到 子序列
这里需要特判两个情况,最后难点就是在维护子序列的情况;
代码详解如下:
#include <iostream>
#include <unordered_map>
#define endl '\n'
#define umap unordered_map
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve()
{
umap<int, int>v;
int ans = -1;
int n, needSum, sum = 0;
cin >> n >> needSum;
for (int i = 0, x; i < n; ++i)
{
cin >> x;
v[i] = x;
sum += x;
}
// 特判 如果数组总和小于,那么无论删减都无法完成
if (sum < needSum)
{
puts("-1");
return ;
}
// 特判 如果数组一开始就等于了,那么不用操作
if (sum == needSum)
{
putchar('0');
putchar('\n');
return ;
}
// 由于是只删减前面和后面,所以答案数组就是该数字的子序列
// 重新开始一步一步的求和
sum = 0;
for (int i = 0, j = 0; i < n; ++i)
{
sum += v[i];
// 当我们总和大于之后,从后面开始减起,得到子序列
while (sum > needSum)
{
sum -= v[j++];
}
// 当我们等于的时候
if (sum == needSum)
{
// 这里的 tem 是维护子序列的长度
int tem = i - j + 1;
// 选取子序列的最大长度
ans = (tem > ans ? tem : ans);
}
}
// 输出 总长度减去符合条件的子序列最大长度,就是最小操作数
cout << n - ans << endl;
}
int main()
{
// freopen("a.txt", "r", stdin);
___G;
int _t = 1;
cin >> _t;
while (_t--)
{
solve();
}
return 0;
}