F. Eating Candies

双指针求最多相同糖果数量
文章描述了一个使用双指针算法解决的编程问题,给定一个糖果数组,找到两个指针同时指向的糖果数量之和相同的最大数量。通过模拟操作,找到满足条件的指针位置,计算出吃掉相同数量糖果的最大次数。

题目:

 样例:

输入
4
3
10 20 10
6
2 1 4 2 4 1
5
1 2 4 8 16
9
7 3 20 5 15 1 11 8 10

输出
2
6
0
7

思路:

        思维题,模拟操作,在这里我们只要找到符合前面某个下标之前的总和等于后面开始往前遍历的总和,即可得出答案,有点像双指针算法,这里我们利用相减的特性,当它们吃糖果,吃到相等总和的时候,相差是 0 ,所以我们记录好相差是 0 的两个下标,然后取个 答案 ans max 就可以得到吃的最多糖果总数量

代码详解如下:

 

#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;
int n;
umap<int, int>a;	// 糖果数组

inline void solve()
{
	int ans = 0;
	int sum = 0;	// 记录吃糖果过程
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	// 开始操作模拟
	// 这里用 i <= j 是因为 当我们数量为 2 的时候 比如  3  3  也是总共吃的数量相同
	for (int i = 0, j = n - 1; i <= j;)
	{
		// 左边先吃一颗,所以 >= 0    sum -=
		if (sum >= 0)
			sum -= a[i++];
		else
			sum += a[j--];

		// 当我们sum == 0 说明有可以吃的糖果相同数字量
		// 记录我们吃的总数量  n - j - 1  是我们倒数第几个   i 是我们当前左边吃的多少个
		if (!sum)
			ans = max(ans, i + n - j - 1);
	}
	cout << ans << endl;
}


int main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

Problem Statement You have an unlimited supply of two types of candies: small candies and large candies. The weight of a small candy is X grams, and the weight of a large candy is Y grams. Large candies are heavier than small candies (that is, X<Y). There are N children, numbered 1 to N. You have decided to distribute candies so that the following conditions are satisfied: For i=1,…,N, child i receives exactly A i ​ candies in total of the two types. The total weights of candies distributed to the N children are all equal. Determine whether there exists a distribution method that satisfies the conditions. If it exists, find the maximum possible value for the number of large candies distributed. DeepL 翻译    问题陈述 你有无限量的两种糖果:小糖果和大糖果。小糖果的重量是 X 克,大糖果的重量是 Y 克。大糖果比小糖果重(即 X<Y )。 有 N 个孩子,编号为 1 至 N 。 你决定分发糖果,以满足以下条件: 对于 i=1,…,N 来说, i 个孩子收到的两种糖果的总重量正好是 A i ​ 。 分给 N 个孩子的糖果的总重量都相等。 确定是否存在满足条件的分配方法。如果存在,求大号糖果分配数量的最大可能值。    Constraints 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 All input values are integers. DeepL 翻译    限制因素 2≤N≤2×10 5 1≤A i ​ ≤10 9 1≤X<Y≤10 9 所有输入值均为整数。    Input The input is given from Standard Input in the following format: N X Y A 1 ​ … A N ​ DeepL 翻译    输入 输入内容由标准输入法提供,格式如下 N X Y A 1 ​ … A N ​    Output If there is no distribution method that satisfies the conditions, output -1. If there exists a distribution method that satisfies the conditions, output the maximum possible value for the number of large candies distributed in such a distribution method. DeepL 翻译    输出 如果没有满足条件的分配方法,则输出 -1。 如果存在满足条件的分配方法,则输出这种分配方法所分配的大颗糖果数量的最大可能值。
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值