第十五届蓝桥杯最后一题 拔河问题

做法就是先枚举右区间的区间和,循环从下标1开始,然后从2开始 全部插入到set里面,

然后枚举左区间,先把从下标1开始的组合删掉,再把下标2的值的组合删掉,然后枚举左区间的组合,用二分来查找差距最小的值

cpp 复制代码
#include <iostream>
#include <set>
using namespace std;
typedef long long LL;
const int N = 1e3+10; 
LL s[N],a[N];
int n,sum;
LL retleft,retright;
multiset <LL> mp;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	LL ret = 1e9;
	mp.insert(-1e9);
	mp.insert(1e9); 
	for(int i = 1;i<=n;i++)
	{
		cin >> a[i];
		s[i] = s[i-1]+a[i];
	}
	for(int i = 1;i<=n;i++)
	{
		for(int j = i;j<=n;j++)
		{
			mp.insert(s[j]-s[i-1]);
		}
		
	}
   for(int i = 1;i<=n;i++)
   {
   	  for(int j = i;j<=n;j++)
   	  {
   	  	auto p = mp.find(s[j]-s[i-1]);
			 mp.erase(p); 
	  }
	  for(int j = i;j>=1;j--)
	  {
	  	LL k = s[i]-s[j-1];//s[j] 
	  	auto it = mp.lower_bound(k);
	  	LL t1 = *it;
	  	it--;
	  	LL t2 = *it;
	  	LL dis = min(abs(t1-k),abs(t2-k)); 
		ret = min(ret,dis);
	  }
   }
	cout << ret << endl;
	
	
	
	

}
相关推荐
YuTaoShao4 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
天真小巫8 小时前
2025.7.6总结
职场和发展
算法_小学生12 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展
alphaTao12 小时前
LeetCode 每日一题 2025/6/30-2025/7/6
算法·leetcode·职场和发展
Owen_Q13 小时前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
Kaltistss1 天前
98.验证二叉搜索树
算法·leetcode·职场和发展
牛客企业服务2 天前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
爱coding的橙子2 天前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
YuTaoShao2 天前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
desssq2 天前
力扣:70. 爬楼梯
算法·leetcode·职场和发展