成环求连续m个最大序列

题意:如题...

主要还是数据太弱了...直接暴力过了...

There are N little kids sitting in a circle, each of them are carrying some java beans in their hand. Their teacher want to select M kids who seated in M consecutive seats and collect java beans from them.

The teacher knows the number of java beans each kids has, now she wants to know the maximum number of java beans she can get from M consecutively seated kids. Can you help her?

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases.

For each test case, the first line contains two integers N (1 ≤ N ≤ 200) and M (1 ≤ MN). Here N and M are defined in above description. The second line of each test case contains N integers Ci (1 ≤ Ci ≤ 1000) indicating number of java beans the ith kid have.

Output

For each test case, output the corresponding maximum java beans the teacher can collect.

Sample Input

2
5 2
7 3 1 3 9
6 6
13 28 12 10 20 75

Sample Output

16
158
#include <stdio.h>
#include <string.h>
int a[410]; 
int s[410];
int main()
{
	int n,m,i,t,T;
	scanf("%d",&T); 
	while (T--)
	{
		scanf("%d%d",&n,&m);
		t=1;
		int max=0;
		memset(s,0,sizeof(s));
		for (i=1;i<=n;i++)
			scanf("%d",&a[i]);
		for (i=n+1;i<=2*n;i++)
			a[i]=a[i-n];
		for (i=1;i<=m;i++)
			s[1]+=a[i];
		for (i=m+1;i<=2*n;i++)
			s[++t]=s[t-1]+a[i]-a[i-m];
		for (i=1;i<=t;i++)
			if (max<s[i])
			max=s[i];
		printf("%d\n",max);
	}
	return 0;
} 


 

``` import numpy as np from stl import mesh import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import splprep, splev from collections import defaultdict def extract_boundary_edges(stl_file): # 原提取边界边函数保持不变 stl_mesh = mesh.Mesh.from_file(stl_file) triangles = stl_mesh.vectors edge_count = {} for triangle in triangles: for i in range(3): edge = tuple(sorted([tuple(triangle[i]), tuple(triangle[(i + 1) % 3])])) edge_count[edge] = edge_count.get(edge, 0) + 1 return [edge for edge, count in edge_count.items() if count == 1] def connect_contours(boundary_edges, tolerance=1e-6, min_segment_length=0.1): adjacency = defaultdict(list) for edge in boundary_edges: p1, p2 = edge # 过滤短边(新增) if np.linalg.norm(np.array(p1) - np.array(p2)) > min_segment_length: adjacency[p1].append(p2) adjacency[p2].append(p1) # 构建邻接表 adjacency = defaultdict(list) for edge in boundary_edges: p1, p2 = edge adjacency[p1].append(p2) adjacency[p2].append(p1) # 拓扑排序连接轮廓 contours = [] visited = set() for start_point in adjacency: if start_point in visited: continue current = start_point contour = [np.array(current)] visited.add(current) while True: neighbors = [n for n in adjacency[current] if tuple(n) not in visited] if not neighbors: break next_point = neighbors[0] contour.append(np.array(next_point)) visited.add(tuple(next_point)) current = tuple(next_point) # 闭合检测 if np.linalg.norm(contour[0] - contour[-1]) < tolerance: contour.append(contour[0]) contours.append(np.array(contour)) return contours def fit_contours(contours, smooth=0.05, num_points=3000): fitted_curves = [] for contour in contours: # 添加输入数据验证 m = len(contour) if m < 4: print(f"跳过点数不足的轮廓({m}点),至少需要4个点进行三次样条拟合") fitted_curves.append(contour) # 保留原始数据 continue try: # 显式设置最大允许阶数 k = min(3, m-1) tck, u = splprep(contour.T, s=smooth, k=k) u_new = np.linspace(u.min(), u.max(), num_points) x_new, y_new, z_new = splev(u_new, tck) fitted_curves.append(np.column_stack((x_new, y_new, z_new))) except Exception as e: print(f"轮廓拟合失败: {str(e)}") fitted_curves.append(contour) # 保留原始数据作为后备 return fitted_curves def visualize_results(contours, fitted_curves): plt.rcParams['font.family'] = ['Microsoft YaHei'] plt.rcParams['axes.unicode_minus'] = False fig = plt.figure(figsize=(12, 6)) # 原始边界可视化 ax1 = fig.add_subplot(121, projection='3d') for contour in contours: ax1.plot(contour[:, 0], contour[:, 1], contour[:, 2], 'b-', alpha=0.5) ax1.set_title('原始边界边') # 拟合结果可视化 ax2 = fig.add_subplot(122, projection='3d') for curve in fitted_curves: ax2.plot(curve[:, 0], curve[:, 1], curve[:, 2], 'r-', lw=2) ax2.set_title('拟合曲线') for ax in [ax1, ax2]: ax.set_box_aspect([1, 1, 0.1]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.tight_layout() plt.show() # 主程序流程 stl_file = '片1.stl' # 步骤1:提取边界边 boundary_edges = extract_boundary_edges(stl_file) print(f"提取边界边数量: {len(boundary_edges)}") # 步骤2:连接连续轮廓 contours = connect_contours(boundary_edges,min_segment_length=0.5) print(f"发现轮廓数量: {len(contours)}") # 步骤3:进行样条拟合 fitted_curves = fit_contours(contours) # 步骤4:可视化对比 visualize_results(contours, fitted_curves)```帮我在拟合之前在顶点处打断,比如长方形在四个角点处打断,注意为非水密模型,我需要完整全部修改后的代码
03-21
### 数学期望计算 在概率论中,当有 \( n \) 条绳子并随机打结时,形数可以通过数学期望来描述。对于初始状态下的 \( 2n \) 个端点,每次打结会减少两个自由端点,并可能增加一个。如果进行了 \( k \) 次打结操作,则剩余的自由端点数量为 \( 2n - 2k \)[^1]。 #### 初始条件与递推关系 假设每一步都均匀地从剩下的自由端点对中选取一对进行连接。那么第 \( i \) 步形的概率等于当前未配对端点总数除以所有可能对的方式数目减去已经存在的的数量。具体来说,在第一步之后,形第一个的概率是 \( \frac{1}{2n-1} \),因为总共有 \( 2n(2n−1)/2 \) 种方式选择两头打结,而其中只有一种情况能构[^2]。 随着过程继续下去,到第 \( j \) 轮为止所累积起来的新生圈数平均值可以表示如下: \[ E[X_j]= E[X_{j-1}] + P(\text{"new loop at step } j") \] 这里的关键在于理解每一次尝试创建一个新的循的可能性如何依赖于之前的状态以及总的可用末端数量的变化规律[^3]。 #### 计算特定实例的结果 针对题目中的具体情况——即拥有200根独立线段(意味着最初存在400个开放终端),经过一百轮这样的随机绑定动作后最终得到的预期闭合路径计数值可通过上述理论框架逐步解得出结论。然而实际运算较为复杂,通常借助计算机模拟或者深入解析组合方法完精确评估[^4]。 ```python import math def expected_loops(n, k): result = 0 for _ in range(k): result += 1 / (2 * n - 1) n -= 1 return round(result, 6) print(expected_loops(200, 100)) ``` 此代码片段定义了一个函数 `expected_loops` ,它接受参数\( n \)(代表原始绳索数的一半) 和 \( k \)(执行绑扎次数), 并返回基于前述公式的近似期望值.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值