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:用于存储最后执行次 ...
随机推荐
- PowerShell 显示气球提示框 1
#加载 Winform 程序集,使用Out-Null抑制输出 [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.For ...
- H+ 显示并激活menuTab 根据tabName
//注:在contabs.js文件中 $(function () { }); 方法外 加入//注: data-name="' + menuName + '" 这句是加入的自定义属性 ...
- pandas Series的sort_values()方法
pandas Series的 sort_values() 方法能对Series进行排序,返回一个新的Series: s = pd.Series([np.nan, 1, 3, 10, 5]) 升序排列: ...
- crawler_exa2
优化中... #! /usr/bin/env python # -*- coding:utf-8 -*- # Author: Tdcqma ''' v17.0920.1401 基本功能实现,漏洞标题与 ...
- 【转】Mac OS X 上修改主机名
修改主机名称 sudo scutil --set HostName MacBookPro 修改共享名称 sudo scutil --set ComputerName MacBookPro [转自]:h ...
- iptables转发技术
NAT 一. 什么是 NAT NAT(Network Address Translation)译为网络地址转换.通常路由器在转发我们的数据包时,仅仅会将源MAC地址换成自己的MAC地址,但是NAT技术 ...
- SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域[转]
SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域 原文地址:https://www.cnblogs.com/fanshuyao/p/716847 ...
- 挖坑:hive集成kerberos
集成hive+kerberos前,hadoop已经支持kerberos,所以基础安装略去: https://www.cnblogs.com/garfieldcgf/p/10077331.html 直接 ...
- 【css】垂直居中的几种写法
结构 <div class="vam"> <div class="vam-body">垂直居中</div> </div ...
- 通过动态SQL语句创建游标
DECLARE @sql varchar(100); DECLARE @TableName varchar(32); DECLARE @FieldName varchar(32); DECLARE @ ...