首页 > 其他分享 >Least Prefix Sum(贪心)

Least Prefix Sum(贪心)

时间:2023-01-04 18:33:13浏览次数:74  
标签:10 int Sum number Least Prefix test Baltic sum

题目链接

题目描述:

Baltic, a famous chess player who is also a mathematician, has an array \(a_1,a_2,…,a_n\), and he can perform the following operation several (possibly \(0\)) times:

  • Choose some index \(i (1≤i≤n)\);

  • multiply \(a_i\) with \(−1\), that is, set \(a_i:=−a_i\).

Baltic's favorite number is \(m\), and he wants \(a_1+a_2+⋯+a_m\) to be the smallest of all non-empty prefix sums. More formally, for each \(k=1,2,…,n\) it should hold that

\[a_1+a_2+⋯+a_k≥a_1+a_2+⋯+a_m. \]

Please note that multiple smallest prefix sums may exist and that it is only required that \(a_1+a_2+⋯+a_m\) is one of them.

Help Baltic find the minimum number of operations required to make \(a_1+a_2+⋯+a_m\) the least of all prefix sums. It can be shown that a valid sequence of operations always exists.

输入描述:

Each test contains multiple test cases. The first line contains the number of test cases \(t (1≤t≤10000).\) The description of the test cases follows.

The first line of each test case contains two integers \(n\) and \(m (1≤m≤n≤2⋅10^5)\) — the size of Baltic's array and his favorite number.

The second line contains \(n\) integers \(a_1,a_2,…,a_n (−10^9≤ai≤10^9)\) — the array.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2⋅10^5\).

输出描述:

For each test case, print a single integer — the minimum number of required operations.

样例:

input:

6
4 3
-1 -2 -3 -4
4 3
1 2 3 4
1 1
1
5 5
-2 3 -5 1 -20
5 2
-2 3 -5 -5 -20
10 4
345875723 -48 384678321 -375635768 -35867853 -35863586 -358683842 -81725678 38576
-357865873

output:

1
1
0
0
3
4

AC代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int T = 1;
int n, m;
LL a[N];

// 要让 m 的前缀和最小,需要让m + 1 ~ n的值都大于0,让m ~ 2的值都小于0
// 要让改变次数最少,则需要找到m + 1 ~ n中小于0的最小值,m ~ 2的大于0的最大值
void solve()
{
	scanf("%d%d", &n, &m);

	for(int i = 1; i <= n; i ++)
		scanf("%lld",&a[i]);

	priority_queue<LL, vector<LL>, greater<LL>> q; // 小根堆
	priority_queue<LL, vector<LL>, less<LL>> p; // 大根堆

	LL sum = 0;
	int ans = 0; // 要用到的次数

	// 遍历m + 1 到 n
	for(int i = m + 1; i <= n; i ++)
	{	
		// m + 1 到 n 的前缀和
		sum += a[i];

		// 如果小于零则存进堆中
		if(a[i] < 0)
			q.push(a[i]);

		// 和小于零则必须要将其变为正的不然m + 1 ~ n的和加上前面的数的和必然小于m的前缀和
		while(sum < 0)
		{
			int t = q.top();
			q.pop();

			sum -= 2 * t;

			ans ++;
		}
	}

	sum = 0;

	// 从后往前遍历,要用最少的次数
	for(int i = m; i >= 2; i --)
	{
		sum += a[i];

		// 如果大于零则存入堆中
		if(a[i] > 0)
			p.push(a[i]);

		// 和大于零则必须将其变为负的不然m加上前面的和必然大于前面的某个数的前缀和
		while(sum > 0)
		{
			int t = p.top();
			p.pop();

			sum -= 2 * t;

			ans ++;
		}
	}

	printf("%d\n", ans);
}

int main()
{
	// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	scanf("%d", &T);

	while(T --)
		solve();

	return 0;
}

标签:10,int,Sum,number,Least,Prefix,test,Baltic,sum
From: https://www.cnblogs.com/KSzsh/p/17025711.html

相关文章

  • 观看入口| Pulsar Summit Asia 2022 Day 1 分论坛热播
    大会介绍ApachePulsar社区年度峰会PulsarSummitAisa2022已于2022年11月19日线上启动。大会分为主论坛和分论坛,汇聚技术大咖和行业先行者分享ApachePulsar实......
  • [ABC283Ex] Popcount Sum
    ProblemStatementFindthesumofpopcountsofallintegersbetween$1$and$N$,inclusive,suchthattheremainderwhendividedby$M$equals$R$.Here,thep......
  • [ABC283Ex] Min + Sum
    ProblemStatementYouaregiventwosequencesofintegersoflength$N$:$A=(A_1,A_2,\ldots,A_N)$and$B=(B_1,B_2,\ldots,B_N)$.Printthenumberofp......
  • java8中常用函数式接口Supplier<T>、Consumer<T>、Function<T,R>、Predicate<T>使用示
    场景函数式接口(FunctionalInterface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。而java中的函数式编程体现就是Lambda,所以函数式接口就是可以适用......
  • Codeforces 1373 D. Maximum Sum on Even Positions 做题记录(单调队列)
    因为只能转一个子数组,很显然转长度为奇数的子数组,对最大化答案是没有意义的(偶数位的数字之和不会变化)。因此只考虑转偶长度的子数组。转动偶数长度的子数组,相当于......
  • UVa10827 Maximum sum on a torus
    思路一道比较经典的题目。首先我们可以把方阵扩展一下,变成\(4n^2\)大小的方阵。问题就变为在长度为\(2\timesn\)的方阵中求一个长和宽均小于等于\(n\)的子矩阵的......
  • CodeForces 1726E Tree Sum
    洛谷传送门CF传送门不错的一道Combinatorics。结论1:\(n\)为奇数时答案为\(0\)。设\(d_i\)为与点\(i\)相连的边边权乘积。每加入一条边对两端的\(d_i\)贡献......
  • P2398 GCD SUM——欧拉函数
    此题可以拓展为\(\sum\limits^n_{i=1}\sum\limits^m_{j=1}\gcd(i,j)\)结论是\(\sum\limits^{\min(n,m)}_{d=1}\varphi(d)\lfloor\dfrac{n}{d}\rfloor\lfloor\dfrac{m}{......
  • Sumitomo Mitsui Trust Bank Programming Contest 2019 —— B
    也不知道这比赛为啥要取这么长的名称(传送门:https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e哈哈,你被骗了!但网址是真的!题意有红绿蓝三种帽子RedandBl......
  • 模拟spring-kafka实现kafka的consumer监听
    背景:因为某些原因,无法直接使用springboot提供的@KafkaListener,改为模拟springboot注解的方式搬过来实现首先创建一个业务处理的service,这个service主要用于消费下来的消息......