Problem E. Jump A Jump
Input file: standard input
Output file: standard output
Time limit: 1 seconds
Memory limit: 512 mebibytes

There’s a very popular game called jump a jump recently.In order to get good marks, many people spend a lot of
time in this game.Wei is the master of jump a jump,he can easily get a very high score every time.
Recently he discovered that because of his muscle memory,he could not fully control his game score.
Now he wants you to help him figure out the nearest score that he can get from his desired score.
Every time he jumps, he chooses one from m distances,he can jump until he wants to end,and he can also use every
distance any times.
Input
The first line has a number n(0 < n ≤ 1015) to indicate the score he desired.
The next line has a number m(0 < m ≤ 2000), which indicates how many choices each step has.
The last line has m integers bi(0 < bi ≤ 5000) representing the ith distances.
Output
Output is the absolute value of the difference between the nearest score he can get and his desired score.
Examples
standard input standard output
11
3
5 7 8

1

Note
In the example,
He can jump 5 + 7 = 12, 12 − 11 = 1.
He can also jump distance 5 for two times, and 2 × 5 = 10, 11 − 10 = 1.

题意:给你m个数字,任意用组成离n最近的数与n相差多少

估计我一辈子都想不到用图论来做吧,只能说一句膜拜队友。

题解:从m个数字中任意选一个数字作为mod(选最小b[i]时最优),dis[i]表示从 取模余数为0的点 到 取模余数为i的点 的最短路

很明显,m个数字任意取得到的数字可以写成 dis[i] + k*mod (k为常数, 0 < i < m)

那么我们就是比较 n 与 dis[i] + k*mod 了

当 dis[i] <= n 的时候

很明显,看起来我们只需要(n - dis[i])%mod 就行了,然而非也,因为n是可以大于dis[i] + k*mod的,所以必须在求一个mod-(n - dis[i])%mod的值,二者取min

dis[i] > n 的时候

很明显,你只能跑那么远再回来了 dis[i] - n (这里不写能水过去!!!惊了)

OK,上代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const long long maxn = 5010;
const long long INF = 0x3f3f3f3f3f3f3f3f;
long long vis[maxn];
long long dis[maxn];
long long n,m,mod;
long long b[maxn]; struct node
{
long long x,d;
node();
node(long long xx,long long dd){
x = xx;
d = dd;
}
}; vector<node> edge[maxn]; void dijkstra(long long x)
{
long long i,j;
for(i=0;i<mod;i++) vis[i] = 0,dis[i] = INF;
dis[x] = 0; for(i=0;i<mod;i++){
long long minn = INF;
long long u = 0;
for(j=0;j<mod;j++){
if(!vis[j] && minn > dis[j]){
minn = dis[j];
u = j;
}
}
vis[u] = 1;
for(j=0;j<edge[u].size();j++){
long long v = edge[u][j].x;
if(!vis[v] && (minn + edge[u][j].d) < dis[v])
dis[v] = minn + edge[u][j].d;
}
}
} int main()
{
// freopen("in.txt","r",stdin);
cin>>n>>m;
mod = maxn;
for(long long i=1; i<=m; i++){
scanf("%d",&b[i]);
mod = min(b[i],mod);
}
for(long long i=0; i<mod; i++){
for(long long j=1; j<=m; j++){
edge[i].push_back(node((b[j]+i)%mod,b[j]));
}
}
dijkstra(0); long long ans = maxn; for(long long i=0; i<mod; i++){
if(dis[i] < n){
long long x = (n-dis[i])%mod;
long long y = min(x,mod-x);
ans = min(ans,y);
}
else{
ans = min(ans,dis[i]-n);
}
}
cout<<ans<<endl;
return 0;
}

2018今日头条杯 E-Jump a Jump的更多相关文章

  1. “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road

    题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...

  2. “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...

  3. 2018今日头条湖北省赛【D】

    [题目链接]https://www.nowcoder.com/acm/contest/104/C 不知道这题为啥没过.队友现场推的都是对的..233333好像代码写的有问题,下来就很恼火. 题意大概就 ...

  4. 2018今日头条湖北省赛【H】

    [题目链接]https://www.nowcoder.com/acm/contest/104/G 现场赛的H题,emmm...C++选手表示很伤心.高精度压四位板子WA四发. 题意很简单就是给你n个数 ...

  5. 2018今日头条湖北省赛【A】

    [题目链接]https://www.nowcoder.com/acm/contest/104/A 这题就是很简单的几何题..md现场推了很久的cos sin仿佛像个zz.自己都想给自己一巴掌. 题意就 ...

  6. A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let  be a regualr tr ...

  7. 2018春招-今日头条笔试题-第四题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) #-*- coding:utf-8 -*- class Magic: ''' a:用于存储数组a b:用于存储数组b num:用于 ...

  8. 2018春招-今日头条笔试题-第三题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...

  9. 2018春招-今日头条笔试题-第二题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...

随机推荐

  1. 终端下将 man 命令的结果输出到文件保存

    终端下将 man 命令的结果输出到文件保存 在linux或mac下,当我们使用man命令查看某一个命令的详细帮助说明信息时: 可能终端的显示效果不是那么方便: 那么我们可以将man命令的结果输出到tx ...

  2. Nginx Web服务应用

    Nginx 指令目录 Nginx 介绍 Nginx 编译安装 Nginx 功能模块 Nginx 目录结构 Nginx 配置文件 Nginx 虚拟主机配置 Nginx 状态信息功能配置 Nginx 错误 ...

  3. Android开发导出apk报错:Unable to build: the file dx.jar was not loaded from the SDK folder

    问题背景 此问题一般出现在,同时使用了Eclipse和Android Studio,eclipse是不会去下载最新的Android的相关tools,但是studio有时候会自动更新最新的build-t ...

  4. hadoop HA (no zkfc to stop) DFSZKFailoverController进程没有启动

    这个bug确实恶心的不要不要的.我一开始以为是我自己打开方式(毕竟不熟悉搭建流程,别人怎么做,我照着做) 我照着视频或者博客编写hdfs-site.xml(dfs.ha.fencing.methods ...

  5. k8s yaml说明

    k8s yaml # yaml格式的pod定义文件完整内容: apiVersion: v1       #必选,版本号,例如v1 kind: Pod       #必选,Pod metadata:   ...

  6. Object type TYPE failed to create with error

    ORA-39083: Object type TYPE failed to create with error: ORA-02304: invalid object identifier litera ...

  7. mybatis-plus忽略映射字段

    mybatis-plus使用对象属性进行SQL操作,经常会出现对象属性非表字段的情况,忽略映射字段使用以下注解: @TableField(exist = false):表示该属性不为数据库表字段,但又 ...

  8. 03Hadoop的TopN的问题

    TopN的问题分为两种:一种是建是唯一的,还有是建非唯一.我们这边做的就是建是唯一的. 这里的建指得是:下面数据的第一列. 有一堆数据,想根据第一列找出里面的Top10. 如下: 关键:在map和re ...

  9. Linux 环境变量_006

    ***Linux 环境变量指系统运行程序或命令的能快速找到其位置等其它功能,不用输入复杂命令.以$PATH环境变量为例子, $PATH决定了shell指定寻找命令或程序的路径,比较执行ls命令,如果没 ...

  10. featureCounts 软件说明

    featuresCounts 软件用于定量,不仅可以支持gene的定量,也支持exon, gene bodies, genomic bins, chromsomal locations的定量: 官网 ...