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 (贪心)的更多相关文章

  1. Codeforces #366 Div. 2 C. Thor (模拟

    http://codeforces.com/contest/705/problem/C 题目 模拟题 : 设的方法采用一个 r 数组(第几个app已经阅读过的消息的数量),和app数组(第几个app发 ...

  2. codeforces 352 div 2 C.Recycling Bottles 贪心

    C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. 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 ...

  4. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  5. Codeforces #344 Div.2

    Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...

  6. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

  7. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  8. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

  9. codeforces #592(Div.2)

    codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...

随机推荐

  1. chrome devTool

    在console中访问节点 使用document.querySelectAll()访问元素 使用$0快速访问选中的元素,光标选中的元素,早console中输入$0获取选中元素的dom信息 拷贝 > ...

  2. ELK之es常用查询语句

    参考:https://www.cnblogs.com/kyleinjava/p/10497042.html           https://blog.csdn.net/luanpeng825485 ...

  3. AI - H2O - 第一个示例

    1 - Iris数据集 Iris数据集是常用的机器学习分类实验数据集,特点是数据量很小,可以快速学习. 数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性. Sepal.Leng ...

  4. Centos7.0操作系统加固常见方法

    1. 账号和口令 1.1 禁用或删除无用账号 减少系统无用账号,降低安全风险. 操作步骤 使用命令 userdel <用户名> 删除不必要的账号. 使用命令 passwd -l <用 ...

  5. 通过git命令“六步”提交新项目到码云

    通过git命令“六步”提交新项目到码云 一.初始化本地仓库 git init 二.添加文件 git add . 三.添加远程数据仓库链接 git remote add origin https://g ...

  6. Tuner工作原理详解

      1.TV自动搜台原理:https://wenku.baidu.com/view/3b771f8b84868762caaed514 2.彩电自动搜台的原理与维修:http://tv.baoxiu.c ...

  7. BOOT目录磁盘占用满处理

    背景:Ubuntu:16.04 查看已安装启动镜像 dpkg --get-selections |grep linux-image 这里会列出目前已经安装的启动镜像,一般分两种,一种状态为“insta ...

  8. ROS中的通信机制

    http://www.ros.org/core-components/ Communications Infrastructure At the lowest level, ROS offers a ...

  9. SpringBoot项目集成PageHelper使用

    SpringBoot项目集成PageHelper使用 一.开始 ​ 地址:https://github.com/pagehelper/Mybatis-PageHelper ​ 在spring boot ...

  10. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) (1208F,1208G,1208H)

    1208 F 大意:  给定序列$a$, 求$\text{$a_i$|$a_j$&$a_k$}(i<j<k)$的最大值 枚举$i$, 从高位到低位贪心, 那么问题就转化为给定$x$ ...