题面戳这→P2672 推销员
【解题思路】:
用sort函数对他走到点能积累的最大疲劳值进行排序,然后枚举每个x,判断他需要走的路是否大于现在的路,是就更新mx并重新对结构体排序,这样不断贪心返回最大能积累的疲劳值。
【AC代码】:
#include<bits/stdc++.h>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define Mod 100003
using namespace std;
inline void read(int &x){
char ch=getchar(),c=ch;
x=0;
while(ch<'0' || ch>'9'){
c=ch;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
if(c=='-')x=-x;
}
struct node{
int s,a;
}map1[100000];
int n,m,i,mx,j,k,ans;
bool b;
bool cmp(const node &x,const node &y){
int va=x.a,vb=y.a;
if(x.s>mx)va+=(x.s-mx)*2;
if(y.s>mx)vb+=(y.s-mx)*2;
return (va>vb?1:0);
}
int main(){
read(n);
for(i=0;i<n;i++)read(map1[i].s);
for(i=0;i<n;i++)read(map1[i].a);
sort(map1,map1+n,cmp);
for(i=0;i<n;i++){
if(map1[i].s>mx){b=true;mx=map1[i].s;}
else b=false;
ans+=map1[i].a;
cout<<(mx*2+ans)<<endl;
if(b==true)sort(map1+i+1,map1+n,cmp);
}
return 0;
}