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 ...
随机推荐
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 串的两种模式匹配方式(BF/KMP算法)
前言 串,又称作字符串,它是由0个或者多个字符所组成的有限序列,串同样可以采用顺序存储和链式存储两种方式进行存储,在主串中查找定位子串问题(模式匹配)是串中最重要的操作之一,而不同的算法实现有着不同的 ...
- Spring Boot制作启动图案
SpringBoot在启动时会有一个默认图案的,如果不喜欢可以自己制作一个. 在resources的目录下新建banner.txt文件. 制作图案地址:springboot启动图案定制 通过输入字符串 ...
- [转帖]什么是UWB?UWB有什么用?
什么是UWB?UWB有什么用? https://www.sohu.com/a/224891573_531173 小米碰传 就是 UWB吧? 2018-03-05 17:02 UWB在早期被用来应用在近 ...
- [转帖]EPOLL和IOCP比较
EPOLL和IOCP比较 https://blog.csdn.net/educast/article/details/15503179 IOCP 异步非阻塞EPOLL 异步阻塞 EPOLL是半成品 ...
- [转帖]TPC-C解析系列01_TPC-C benchmark测试介绍
TPC-C解析系列01_TPC-C benchmark测试介绍 http://www.itpub.net/2019/10/08/3334/ 学习一下. 自从蚂蚁金服自研数据库OceanBase获得TP ...
- java当中JDBC当中请给出一个sql server的stored procedure例子
3.sql server的stored procedure例子: import java.sql.*;public class StoredProc0 {public static void main ...
- Zuul【自定义Filter】
实际业务中,如果要自定义filter过滤器,只需集成ZuulFIlter类即可,该类是个抽象类,它实现了IZuulFIlter接口,我们需要实现几个方法,如下示例: import static org ...
- C++程序的多文件组成
C++程序的多文件组成 [例3.32] 一个源程序按照结构划分为3个文件 // 文件1 student.h (类的声明部分) #include<iostream.h> #include&l ...
- youku项目总结(粗略总结)
一.ORM 之前我们都是以文件保存的形式存储数据,这次我们用的是数据库结合python使用,用到 ORM:关系型映射 类>>数据库的一张表 对象>>表一条记录 对象.属性> ...