跳到正文

题解

题解:P2672 [NOIP2015 普及组] 推销员

Charles_with_wkc约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年1月23日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟
题目
P2672

版本

  1. v1当前

题意:

已知,每走 11 米增加 11 点疲劳值。给出每户人家到入口的距离,和向这户人家推销所需的疲劳值。求出对于不同的 XX,在不走多余的路的前提下,阿明最多可以积累多少点疲劳值。

思路:

这是一道比较明显的贪心+前缀和题,贪心策略就是找到 2×Si+Ai2 \times S_i + A_i (往返距离+推销疲劳值)最大的那户人家。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N=100005;
struct node{
	int s,a;
}a[N];
int n;
int last[N],pre[N],sum[N];
bool cmp(node x,node y){
	return x.a>y.a;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].s;
	for(int i=1;i<=n;i++) cin>>a[i].a;
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++) last[i]=last[i-1]+a[i].a;
	for(int i=1;i<=n;i++) pre[i]=max(pre[i-1],a[i].s);
	for(int i=n;i>=1;i--) sum[i]=max(sum[i+1],a[i].s*2+a[i].a);
	for(int i=1;i<=n;i++) cout<<max(last[i-1]+sum[i],last[i]+2*pre[i])<<endl;
	return 0;
}
原文
存档评论1 条抓取于