2018今日头条杯 E-Jump a Jump
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的更多相关文章
- “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road
题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...
- “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...
- 2018今日头条湖北省赛【D】
[题目链接]https://www.nowcoder.com/acm/contest/104/C 不知道这题为啥没过.队友现场推的都是对的..233333好像代码写的有问题,下来就很恼火. 题意大概就 ...
- 2018今日头条湖北省赛【H】
[题目链接]https://www.nowcoder.com/acm/contest/104/G 现场赛的H题,emmm...C++选手表示很伤心.高精度压四位板子WA四发. 题意很简单就是给你n个数 ...
- 2018今日头条湖北省赛【A】
[题目链接]https://www.nowcoder.com/acm/contest/104/A 这题就是很简单的几何题..md现场推了很久的cos sin仿佛像个zz.自己都想给自己一巴掌. 题意就 ...
- A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let be a regualr tr ...
- 2018春招-今日头条笔试题-第四题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) #-*- coding:utf-8 -*- class Magic: ''' a:用于存储数组a b:用于存储数组b num:用于 ...
- 2018春招-今日头条笔试题-第三题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...
- 2018春招-今日头条笔试题-第二题(python)
题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...
随机推荐
- AttributeError: 'dict' object has no attribute 'has_key'
运行下面的代码: if (locals().has_key('data')): del data gc.collect() 出错: if (locals().has_key('data')): Att ...
- IDEA使用笔记(十)——设置Java方法注释
如果你看到了,这篇博文,那么你是幸运的!你问什么?你百度百度同类型的网文就明白了! 一:先看效果 二:我的实验过程(肯定还有别的方式) 1:新建 Template Group,详细操作步骤见下图 ...
- java幂等性的解决方案
一.幂等性概念 在编程中.一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.幂等函数,或幂等方法,是指可以使用相同参数重复执行,并能获得相同结果的函数.这些函数不会影响系统状态,也 ...
- Troubleshooting Scheduler Autotask Issues (Doc ID 1561498.1)
In this Document Purpose Troubleshooting Steps References APPLIES TO: Oracle Database - Enterp ...
- Git操作自动触发企业微信机器人webhook
[本文出自天外归云的博客园] 背景 在git做一些merge或push的操作,我们希望可以自动在企业微信群发送自定义的通知. 服务代码 这里选用php作为网络服务的开发语言,关键的代码如下(githo ...
- Canvas入门到高级详解(上)
神奇的 canvas--AICODER 全栈培训 IT 培训专家 一.canvas 简介 1.1 什么是 canvas?(了解) 是 HTML5 提供的一种新标签 <canvas>< ...
- sqoop导入数据到hive中元数据问题
简单配置了sqoop之后开始使用,之前用的时候很好用,也不记得有没有启动hivemetastore,今天用的时候没有启动,结果导入数据时,如果使用了db.tablename,就会出现找不到数据库的错, ...
- python 操作Sqlite
Python sqlite 官方文档: https://docs.python.org/2/library/sqlite3.html 1. 连接 #!/usr/bin/python3 import s ...
- Oracle中exists替代in语句
大家都知道exists的速度要比in的速度快,也知道exists函数返回一个布尔值,也就是说exists函数里最后要是 a.id =b.id类似这种方式结束. 例如: SELECT * FROM TB ...
- php -- func_get_args
该方法必须在某个方法内部执行才有效 返回值为索引数组,一个数组元素对应一个参数