P2672 推销员

下面讲正确的贪心

题解

考虑当推销员要推销 i 家客户时,他可以有两种选择:

(1)选择前 i 家疲劳值 a 最大的客户,加上这些客户里最远的距离

(2)选择前 i-1 家疲劳值 a 最大的客户,然后从后边找一个距离最远的客户

所以贪心思路就出来了

考虑维护什么?

反正怎样都是与疲劳值息息相关,那不如先按照疲劳值从大到小sort一下好了

then

sum[ i ]  前 i 个疲劳值最大的客户,疲劳值之和

x[ i ]  前 i 个疲劳值最大的客户中(也就是sum[ i ]中),距离起点最远的那个客户的距离

hou[ i ]  (sort之后)后 i 个客户中单独推销最大的那个客户的 单推值(=距离*2+疲劳值)

对于每一个 i ,ans就是:

max(sum[ i-1 ]+hou[ i ] , sum[ i ]+q[ i ])

代码

#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+;
int n;
int sum[maxn],hou[maxn],x[maxn]; struct node
{
int s,a;
}peo[maxn]; bool cmp(node x,node y)
{
return x.a >y.a ;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&peo[i].s );
for(int i=;i<=n;i++)
scanf("%d",&peo[i].a );
sort(peo+,peo+n+,cmp);
for(int i=;i<=n;i++)
{
sum[i]=sum[i-]+peo[i].a ;
x[i]=max(x[i-],peo[i].s* );
}
for(int i=n;i>=;i--)
hou[i]=max(hou[i+],peo[i].s*+peo[i].a ); for(int i=;i<=n;i++)
{
printf("%d\n",max(sum[i-]+hou[i],sum[i]+x[i]));
} return ;
}

后记

带坏萌新

这题数据好水啊,一个错误的贪心井然可以AC

一个nice数据可以卡死这个错误代码

有Bug:测试数据为:5 1 2 4 2 5 5 4 4 3 2

正确输出:12 17 21 25 28

下面这个程序的输出:12 17 21 24 28

代码

#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+10;
int n,k,rode; long long ans=0; struct node
{
int s,a,dt;
}peo[maxn]; bool cmp(node x,node y)
{
return x.a >y.a ;
} int main()
{
scanf("%d\n",&n);
if(n==1)
{
int x,y;
scanf("%d\n",x*2+y);
return 0;
}
for(int i=1;i<=n;i++)
scanf("%d",&peo[i].s );
for(int i=1;i<=n;i++)
{
scanf("%d",&peo[i].a );
peo[i].dt =peo[i].s *2+peo[i].a ;
if(peo[i].dt>ans)
{
ans=peo[i].dt ;
k=i;
}
}
printf("%ld\n",ans);
rode=peo[k].s;
peo[k].a =-1;
sort(peo+1,peo+n+1,cmp); for(int i=1;i<n;i++)
{
if(peo[i].s <=rode )
{
ans+=peo[i].a ;
printf("%ld\n",ans);
}
else
{
ans+=peo[i].a ;
ans-=rode*2;
ans+=peo[i].s *2;
rode=peo[i].s ;
printf("%ld\n",ans);
}
} return 0;
}

最新文章

  1. C#数组,List,Dictionary的相互转换
  2. ARM汇编程序结构
  3. 【转】提高VR渲染速度的最好方法(经典转载)
  4. JQuery Mobile 页面参数传递(转)
  5. Android View绘制过程
  6. spring,hibernate,struts的面试笔试题
  7. JavaWEB入门
  8. ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题
  9. check running processes in Ubuntu
  10. Java I/O继承图
  11. IPX/SPX
  12. ****微信小程序架构解析
  13. Docker:容器间互联的应用zabbix监控项目 [十]
  14. 转载及总结:cron表达式详解,cron表达式写法,cron表达式例子
  15. SQL Server 怎么在分页获取数据的同时获取到总记录数
  16. 性能测试—JMeter 常用元件(四)
  17. C++中的类模板
  18. 我的vim配置脚本
  19. drop user 报错ora-00604
  20. Make a printer-port EEPROM programmer and dongle

热门文章

  1. electron builder 打包多个第三方依赖的软件
  2. docker私有仓库registry的使用
  3. 使用TensorFlow玩GTA5
  4. 关于windows下无法删除文件,需要TrueInstaller权限的问题
  5. 08Response
  6. java8学习之Stream实例剖析
  7. 清北学堂提高组突破营游记day3
  8. poj1830 开关问题[高斯消元]
  9. C#中无边框窗体移动或拖控件移动窗体
  10. Python 操作 MySQL 数据库Ⅱ