G - Supermarket

该博客主要讨论了一个关于商品销售的最大利润问题。给定n件商品,每件商品有不同的利润和销售截止日期,每天只能销售一件。通过动态规划策略,博主提供了代码实现,按利润从高到低排序商品,并在不超过截止日期的情况下选择能获得最大利润的商品。最终,算法遍历所有商品,更新当前最大利润,并返回总和。

传送门:Supermarket
在这里插入图片描述
在这里插入图片描述
题意:有n件商品,每件商品的利润为p_i,销售日期的截止时间为d_i(即只能在d_i时间前销售该物品)。一天只能销售一件物品。问这n件商品的最大利润为多少
思路:具体见代码和注释。
代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10010;

int n, ans;
int p[N];
struct node{
	int p,d;
}s[N];

bool comp(node x,node y)
{
	return x.p>y.p;//按利润从高到低排序
}
int find(int x) {
	if(x != p[x]) p[x] = find(p[x]);
	return p[x];
}

int main() {
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++) scanf("%d%d",&s[i].p,&s[i].d);
		for(int i=0;i<N;i++) p[i]=i;
		sort(s+1,s+1+n,comp);
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			int day=find(s[i].d);
			if(day)//day为0说明这个物品已经过保质期无法再选
			{
				ans+=s[i].p;
				//父节点直接指向下一个有空的位置(为0就说明没空位了)
				p[day]=find(day-1);
			}
		}
		printf("%d\n",ans);
	}
}
import numpy as np import matplotlib.pyplot as plt # 随机生成超市和小区位置及需求 def generate_locations_and_demands(num_supermarkets, num_communities): supermarket_locations = np.random.rand(num_supermarkets, 2) * 100 # 随机生成超市位置 supermarket_demands = np.random.randint(10, 100, num_supermarkets) # 随机生成超市需求 community_locations = np.random.rand(num_communities, 2) * 100 # 随机生成小区位置 community_demands = np.random.randint(10, 100, num_communities) # 随机生成小区需求 return supermarket_locations, supermarket_demands, community_locations, community_demands # 极坐标转换分群 def polar_coordinate_clustering(locations): angles = np.arctan2(locations[:, 1], locations[:, 0]) sorted_indices = np.argsort(angles) return sorted_indices # 最近插入法规划路线 def nearest_insertion(locations): route = [0] unvisited = list(range(1, len(locations))) while unvisited: min_distance = float('inf') best_insertion = None best_node = None for node in unvisited: for i in range(len(route)): current_distance = np.linalg.norm(locations[route[i]] - locations[node]) if current_distance < min_distance: min_distance = current_distance best_insertion = i best_node = node route.insert(best_insertion + 1, best_node) unvisited.remove(best_node) return route # 可视化展示 def visualize_route(locations, route): plt.figure() plt.scatter(locations[:, 0], locations[:, 1], c='b') for i in range(len(route) - 1): start = locations[route[i]] end = locations[route[i + 1]] plt.plot([start[0], end[0]], [start[1], end[1]], 'r-') plt.plot([locations[route[-1]][0], locations[route[0]][0]], [locations[route[-1]][1], locations[route[0]][1]], 'r-') plt.show() # 主函数 def main(): num_supermarkets = 5 num_communities = 10 supermarket_locations, supermarket_demands, community_locations, community_demands = generate_locations_and_demands(num_supermarkets, num_communities) all_locations = np.vstack((supermarket_locations, community_locations)) all_demands = np.hstack((supermarket_demands, community_demands)) # 极坐标转换分群 sorted_indices = polar_coordinate_clustering(all_locations) sorted_locations = all_locations[sorted_indices] # 最近插入法规划路线 route = nearest_insertion(sorted_locations) # 可视化展示 visualize_route(sorted_locations, route) # 信息输出 print("路线规划:", route) print("需求信息:", all_demands) if __name__ == "__main__": main() 根据上述代码进行更改修正,代码所生成的图像用PyCharm能生成出来
10-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值