LA 3971 Assemble

题目:Assemble


思路:

贪心+二分。

二分最大的最小品质值x。

每次选品质值超过x的价格最低的配件。


注意:

1、多组数据,初始化时不能忘记minw和maxw。

2、二分边界不能写错。

3、如果在同一种类中无法找到可以买的配件,也作不可能处理。


代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cstring>
#include <map>
using namespace std;

#define maxn 1000
#define maxb 1000000000

struct ob {
	int w,c;
	ob() {}
	ob(int w_,int c_) {
		w=w_,c=c_;
	}
};

int n,b;

map<string,int> mp;
vector<ob> vec[maxn+5];
int cnt;

int minw=(1<<30),maxw=0;

void init() {
	for(int i=1; i<=maxn; i++) {
		vec[i].clear();
	}
	mp.clear();
	cnt=0;
	minw=(1<<30),maxw=0;
}

void readin() {
	scanf("%d%d",&n,&b);
	for(int i=1; i<=n; i++) {
		string x,y;
		cin>>x>>y;
		if(!mp.count(x)) mp[x]=++cnt;
		int c,w;
		scanf("%d%d",&c,&w);
		minw=min(minw,w);
		maxw=max(maxw,w);
		vec[mp[x]].push_back(ob(w,c));
	}
}

bool judge(int x) {
	int s=0;
	bool flag;
	for(int i=1; i<=cnt; i++) {
		flag=false;
		int minc=(1<<30);
		for(int j=0; j<vec[i].size(); j++) {
			if(vec[i][j].w>=x&&minc>vec[i][j].c) {
				minc=vec[i][j].c;
				flag=true;
			}
		}
		s+=minc;
		if(s>b||!flag) return false;
	}
	return true;
}

int main() {
	int T;
	scanf("%d",&T);

	while(T--) {
		init();
		readin();
		int l=minw,r=maxw;
		while(l+1<r) {
			int mid=(l+r)/2;
			if(judge(mid)) l=mid;
			else r=mid;
		}
		if(judge(l+1)) l++;
		printf("%d\n",l);
	}
	return 0;
}

# -*- coding: utf-8 -*- """ 装配器类 @author: Konstantinos Karapiperis """ import numpy as np from utilities import * class Assembler(): def __init__(self, mat_points, n_nodes, data_set_idxs): self.mat_points = mat_points # 所有材料点列表 self.n_mat_points = len(mat_points) # 材料点总数 self.n_nodes = n_nodes # 总节点数 self.nodal_support = mat_points[0].nodal_support # 每个单元的节点支撑数 self.n_dof = mat_points[0].n_dof # 自由度数(平面应变假设为2) # 初始化所有材料点的局部状态 self.initialize_local_states(data_set_idxs) def initialize_local_states(self, data_set_idxs): """ 初始化每个材料点的局部状态 参数: data_set_idxs: 各材料点对应的数据集索引数组 """ for mp_idx in range(self.n_mat_points): self.mat_points[mp_idx].initialize_local_state(data_set_idxs[mp_idx]) def assign_local_states(self, displacements, lagrange_multipliers): """ 为每个材料点分配局部状态 参数: displacements: 全局节点位移数组 (n_nodes*n_dof,) lagrange_multipliers: 拉格朗日乘子数组 (n_nodes*n_dof,) """ for mp_idx in range(self.n_mat_points): # 获取当前材料点关联的节点索引 node_idxs = self.mat_points[mp_idx].connectivity # 生成局部自由度索引(考虑多自由度) dof_idxs = np.vstack(( self.n_dof*node_idxs, self.n_dof*node_idxs + 1 )).reshape((-1,), order='F') # 按列优先展开 # 提取当前材料点的局部位移和拉格朗日乘子 disp_mp = displacements[dof_idxs].reshape(-1, self.n_dof) lagr_multipl_mp = lagrange_multipliers[dof_idxs].reshape(-1, self.n_dof) # 更新材料点的局部状态 self.mat_points[mp_idx].assign_local_states(disp_mp, lagr_multipl_mp) def compute_global_distance(self): """ 计算全局相变距离总和 返回值: 所有材料点距离的总和 """ global_dist = 0 for mp_idx in range(self.n_mat_points): global_dist += self.mat_points[mp_idx].compute_current_distance() return global_dist def assemble_force_vectors(self): """ 组装全局应力和应变力向量 返回值: (stress_based_forces, strain_based_forces) 两个长度为n_nodes*n_dof的全局力向量 """ stress_based_forces = np.zeros(self.n_nodes*self.n_dof) strain_based_forces = np.zeros(self.n_nodes*self.n_dof) for mp_idx in range(self.n_mat_points): # 获取材料点的应力和应变力贡献 stress_forces_mp = self.mat_points[mp_idx].compute_stress_based_forces() strain_forces_mp = self.mat_points[mp_idx].compute_strain_based_forces() # 获取当前材料点关联的节点索引 node_idxs = self.mat_points[mp_idx].connectivity # 生成局部自由度索引(考虑多自由度) dof_idxs = np.vstack(( self.n_dof*node_idxs, self.n_dof*node_idxs + 1 )).reshape((-1,), order='F') # 将局部力贡献累加到全局向量 stress_based_forces[dof_idxs] += np.array(stress_forces_mp).ravel() strain_based_forces[dof_idxs] += np.array(strain_forces_mp).ravel() return stress_based_forces, strain_based_forces def assemble_stiffness_matrix(self): """ 组装全局刚度矩阵 返回值: 完整的刚度矩阵 (n_nodes*n_dof x n_nodes*n_dof) """ stiffness = np.zeros((self.n_nodes*self.n_dof, self.n_nodes*self.n_dof)) for mp_idx in range(self.n_mat_points): stiffness_mp = self.mat_points[mp_idx].compute_stiffness_matrix() # 遍历材料点内部的所有节点对 for node_idx_A in range(self.nodal_support): node_A = self.mat_points[mp_idx].connectivity[node_idx_A] for node_idx_B in range(self.nodal_support): node_B = self.mat_points[mp_idx].connectivity[node_idx_B] # 遍历所有自由度组合 for i in range(self.n_dof): voigt_idx_Ai = convert_to_voigt_idx(node_A, i) voigt_idx_Ai_loc = convert_to_voigt_idx(node_idx_A, i) for j in range(self.n_dof): voigt_idx_Bj = convert_to_voigt_idx(node_B, j) voigt_idx_Bj_loc = convert_to_voigt_idx(node_idx_B, j) # 累加局部刚度到全局矩阵 stiffness[voigt_idx_Ai, voigt_idx_Bj] += \ stiffness_mp[voigt_idx_Ai_loc, voigt_idx_Bj_loc] return stiffness def get_convergence_status(self): """ 获取全局收敛状态 返回值: 布尔值:所有材料点是否收敛 """ global_convergence = True self.number_of_unconverged_mps = 0 for mp_idx in range(self.n_mat_points): if self.mat_points[mp_idx].convergence_status: continue self.number_of_unconverged_mps += 1 return (self.number_of_unconverged_mps == 0) def compute_node_strains(self): """ 计算节点的平均应变(基于邻近材料点) 返回值: node_mat_strains: (n_nodes, n_dof, n_dof) 应变张量数组 """ node_mat_strains = np.zeros((self.n_nodes, self.n_dof, self.n_dof)) node_weights = np.zeros(self.n_nodes) for mp_idx in range(self.n_mat_points): eps_mp = self.mat_points[mp_idx].mat_strain weight_mp = self.mat_points[mp_idx].nodal_weights for node_idx, node in enumerate(self.mat_points[mp_idx].connectivity): node_mat_strains[node] += eps_mp * weight_mp[node_idx] node_weights[node] += weight_mp[node_idx] # 归一化处理 for node_idx in range(self.n_nodes): if node_weights[node_idx] != 0: node_mat_strains[node_idx] /= node_weights[node_idx] return node_mat_strains def compute_node_stresses(self): """ 计算节点的平均应力(基于邻近材料点) 返回值: node_mat_stresses: (n_nodes, n_dof, n_dof) 应力张量数组 """ node_mat_stresses = np.zeros((self.n_nodes, self.n_dof, self.n_dof)) node_weights = np.zeros(self.n_nodes) for mp_idx in range(self.n_mat_points): sigma_mp = self.mat_points[mp_idx].mat_stress weight_mp = self.mat_points[mp_idx].nodal_weights for node_idx, node in enumerate(self.mat_points[mp_idx].connectivity): node_mat_stresses[node] += sigma_mp * weight_mp[node_idx] node_weights[node] += weight_mp[node_idx] # 归一化处理 for node_idx in range(self.n_nodes): if node_weights[node_idx] != 0: node_mat_stresses[node_idx] /= node_weights[node_idx] return node_mat_stresses assembler = Assembler(mat_points, n_nod, data_set_idxs) 不考虑代码跑通的情况下,分析assembler变量的含义
08-28
复杂几何的多球近似MATLAB类及多球模型的比较 MATLAB类Approxi提供了一个框架,用于使用具有迭代缩放的聚集球体模型来近似解剖体积模型,以适应目标体积和模型比较。专为骨科、生物力学和计算几何应用而开发。 MATLAB class for multi-sphere approximation of complex geometries and comparison of multi-sphere models 主要特点: 球体模型生成 1.多球体模型生成:与Sihaeri的聚集球体算法的接口 2.音量缩放 基于体素的球体模型和参考几何体的交集。 迭代缩放球体模型以匹配目标体积。 3.模型比较:不同模型体素占用率的频率分析(多个评分指标) 4.几何分析:原始曲面模型和球体模型之间的顶点到最近邻距离映射(带颜色编码结果)。 如何使用: 1.代码结构:Approxi类可以集成到相应的主脚本中。代码的关键部分被提取到单独的函数中以供重用。 2.导入:将STL(或网格)导入MATLAB,并确保所需的函数,如DEM clusteredSphere(populateSpheres)和inpolyhedron,已添加到MATLAB路径中 3.生成多球体模型:使用DEM clusteredSphere方法从输入网格创建多球体模型 4.运行体积交点:计算多球体模型和参考几何体之间的基于体素的交点,并调整多球体模型以匹配目标体积 5.比较和可视化模型:比较多个多球体模型的体素频率,并计算多球体模型与原始表面模型之间的距离,以进行2D/3D可视化 使用案例: 骨科和生物力学体积建模 复杂结构的多球模型形状近似 基于体素拟合度量的模型选择 基于距离的患者特定几何形状和近似值分析 优点: 复杂几何的多球体模型 可扩展模型(基于体素)-自动调整到目标体积 可视化就绪输出(距离图)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值