Codeforces #366 (Div. 2) D. Ant Man (贪心)
https://blog.csdn.net/liangzhaoyang1/article/details/52215276 原博客
原来好像是个dp题,不过我看了别人的博客使用贪心做的 复杂度(n^2)
题意:在一个数轴上有n个点,每个点有5个值x,a,b,c,d,你每次可以从一个点i跳跃到另外一个点j。
如果j在i的右边,则需要花费abs(x[i]-x[j])+c[i]+b[j]。
如果j在i的左边,则需要花费abs(x[i]-x[j])+d[i]+a[j],
一开始你位于s点,你的目的是遍历所有的点1遍并且最后停在e点,求最小花费.(注意:每个点最多只能遍历1遍)
题解:
初始化链:next[s] = e表示直接从起点s跳到终点e。
然后枚举其他所有的点,将它们一个一个插入到链中(寻找加入到链中的什么地方花费最低)
例如样例:
①:初始化:4->3
②:插入编号为1的点,显然1只能插入一个地方,插入后:4->1->3
③:插入编号为2的点,2可以插入两个地方(4和1中间或1和3中间),可以轻松算出插入4和1中间成本更低,所以
插入后:4->2->1->3
④:编号为3和4的点是起点和终点,跳过
⑤:插入编号为5的点,5可以插入三个地方,其中插入1和3中间成本更低,插入后:4->2->1->5->3
⑥:插入编号为6的点,……,插入后:4->2->1->6->5->3。
代码中的实现:
用 dis函数处理距离。
对 i=1~n按顺序 插,碰到s和e就跳过。 对每一个i,都找到一个最恰当的插的位置k,处理 k和前后的关系。
花费 用t表示,在计算t的时候 要减去原来dis(u,next[u]),这样后面直接就可以 ans+=minn。
#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const int INF= 0x3f3f3f3f;
const int N=3e5+; int n,s,e;
ll x[],a[],b[],c[],d[];
ll net[]; ll dis(int l,int r)
{
if(l<r) return abs(x[l]-x[r])+d[l]+a[r];
if(l>r) return abs(x[l]-x[r])+c[l]+b[r];
} int main()
{
cin>>n>>s>>e;
for(int i=;i<=n;i++) scanf("%lld",&x[i]);
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
for(int i=;i<=n;i++) scanf("%lld",&b[i]);
for(int i=;i<=n;i++) scanf("%lld",&c[i]);
for(int i=;i<=n;i++) scanf("%lld",&d[i]); net[s]=e;
ll ans=dis(s,e);
for(int i=;i<=n;i++)
{
if(i==s||i==e) continue; int u=s;
ll minn=1e18;
int k;
while(u!=e)
{
ll t=dis(u,i)+ dis(i,net[u])- dis(u,net[u]);
if(t<minn)
{
minn=t;
k=u;
}
u=net[u];
}
ans+=minn;
//接下来把i插入这个线性结构中
net[i]=net[k]; //把i插在k的后面,k的next 就变成了 i的next
net[k]=i; //k的next变成 i
}
cout<<ans;
}
Codeforces #366 (Div. 2) D. Ant Man (贪心)的更多相关文章
- Codeforces #366 Div. 2 C. Thor (模拟
http://codeforces.com/contest/705/problem/C 题目 模拟题 : 设的方法采用一个 r 数组(第几个app已经阅读过的消息的数量),和app数组(第几个app发 ...
- codeforces 352 div 2 C.Recycling Bottles 贪心
C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- Codeforces #344 Div.2
Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...
- Codeforces #345 Div.1
Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- codeforces #592(Div.2)
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...
随机推荐
- 详解consul的安装和配置
Consul 简化了分布式环境中的服务的注册和发现流程,通过 HTTP 或者 DNS 接口发现.支持外部 SaaS 提供者等. consul提供的一些关键特性: service discovery:c ...
- top命令常用
top 使用时的命令: Ctrl+L擦除并且重写屏幕. h或者?显示帮助画面,给出一些简短的命令总结说明. k 终止一个进程.系统将提示用户输入需要终止的进程PID,以及需要发送给该进程什么样的信号. ...
- Django时区导致的datetime时间比较报错
我们使用python 的datetime模块比较Django数据库Datetime字段的时候,可能会出现报错: TypeError: can't compare offset-naive and of ...
- xadmin自定义菜单、增加功能、富文本编辑器
xadmin功能:https://www.cnblogs.com/derek1184405959/p/8682250.html#blogTitle7
- 二、部署DHCP
*本文转自https://blog.51cto.com/lumay0526/2046957 简述 DHCP是Dynamic Host Configuration Protocol的缩写,中文称动态主机 ...
- Beginning Linux Programming 学习--chapter 11 Processes and Signals
What's process--什么是进程? The UNIX standards, specifically IEEE Std 1003.1, 2004 Edition, defines a pr ...
- mac upgrade node and npm
一直以来, 我们都可以很轻松的更新npm: npm install npm -g 而Node我却是很久没有更新了, 记得当时好像是使用安装包安装的, 实际上有更加简单的安装方法. 实际上Mac上有一个 ...
- SVN增加访问用户
1.在Linux中进入SVN配置文件目录. 2.authz是设置权限的,只读还是可读可写,passwd是增加访问用户的. vim passwd; vim authz;
- Model 的使用
1. 设计数据结构 问题表Question:作用存放问题 id 主键 自增 question_text 题目 varchar120 created 创建时间 datetime 选项表Choice: ...
- golang日志库之glog使用问题总结
1. 日志默认输出路径为临时路径,可通过执行命令时带上 -log_dir="路径",指定输出,但路径必须已存在,源码如下,日志文件会生成两个 .INFO等后缀是符号链接文件,另一个 ...