Fox And Jumping
Fox And Jumping
题目链接:http://codeforces.com/problemset/problem/512/B
dp
若所选卡片能到达区间内任意点,那么所选卡片的最大公约数为1(a*x+b*y=gcd(a,b)=1)。
定义状态dp[i]:获得i需要的最小的代价。
代码如下:
#include<cstdio>
#include<map>
#include<iostream>
#define LL long long
using namespace std;
int n;
int a[];
int b[];
map<int,int> dp;
map<int,int>::iterator it;
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int main(void){
scanf("%d",&n);
for(int i=;i<n;++i)scanf("%d",a+i);
for(int i=;i<n;++i)scanf("%d",b+i);
for(int i=;i<n;++i){
if(dp.count(a[i]))dp[a[i]]=min(dp[a[i]],b[i]);
else dp[a[i]]=b[i];
for(it=dp.begin();it!=dp.end();++it){
int temp=gcd(a[i],it->first);
if(dp.count(temp))dp[temp]=min(dp[temp],dp[a[i]]+it->second);
else dp[temp]=dp[a[i]]+it->second;
}
}
if(dp.count())printf("%d\n",dp[]);
else printf("-1\n");
}
Fox And Jumping的更多相关文章
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- CodeForces - 512B Fox And Jumping[map优化dp]
B. Fox And Jumping time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces510 D. Fox And Jumping
Codeforces题号:#510D 出处: Codeforces 主要算法:map+DP 难度:4.6 思路分析: 题意:给出n张卡片,分别有l[i]和c[i].在一条无限长的纸带上,你可以选择花c ...
- 【Cf #290 B】Fox And Jumping(dp,扩展gcd)
根据裴蜀定理,当且仅当选出来的集合的L[i]的gcd等于1时,才能表示任何数. 考虑普通的dp,dp[i][j]表示前i个数gcd为j的最少花费,j比较大,但状态数不多,拿个map转移就好了. $ \ ...
- 【codeforces 510D】Fox And Jumping
[题目链接]:http://codeforces.com/contest/510/problem/D [题意] 你可以买n种卡片; 每种卡片的花费对应c[i]; 当你拥有了第i种卡片之后; 你可以在任 ...
- Codeforces 512B: Fox And Jumping
题目链接 题意说的是,有n种卡片,使用第i种卡片可以使当前自己在数轴上的位置移动 l[i],要获得使用第i种卡片的代价是 c[i],求能使自己移动到数轴上任意位置的最小代价,如果不可能则输出-1 当前 ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
- CodeForces 512B(区间dp)
D - Fox And Jumping Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- codeforces510D
Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...
随机推荐
- MyBatis:打印SQL 日志
配置Log4J比较简单, 比如需要记录这个mapper接口的日志: package org.mybatis.example; public interface BlogMapper { @Select ...
- NodeJS 实现 客户端 js 加密
NodeJS 实现 客户端 js 加密 思路: 服务端渲染业务代码js => 前后端约定加密算法 => 业务代码进行签名 => 客户端解密业务代码 => eval 执行 Nod ...
- Docker集群实验环境布署--swarm【3 注册服务监控与自动发现组件--consul】
参考官网集群配置方式 https://hub.docker.com/r/progrium/consul/ 集群中需要manager与node能通信consul的发现服务,不然,管理节点选举不了,无 ...
- shell 分支/循环
==)); then patern="Update" else patern="Read" fi in "-h") ] then helpI ...
- 数据结构之Heap (Java)
Heap简介 Heap译为“堆”,是一种特殊的树形数据结构,它满足所有堆的特性:父节点的值大于等于子节点的值(max heap),或者小于等于子节点的值(min heap).对于max heap 根节 ...
- 移动平台下的Socket几个问题
在页游时代,使用Flash ActionScript 3.0进行开发,as3提供比较简单和健全的socket API.到了手游时代,基于tcp的socket编程遇到了一些棘手的问题.通常情况下手游都要 ...
- etcd 集群搭建
现有三台机器 CentOS7 node1 10.2.0.10 node2 10.2.0.11 node3 10.2.0.12 1 源码解压命令行方式 node1 ./etcd --name infr ...
- table表头thead固定
<html> <head> <meta charset="utf-8"/> <script type="text/javascr ...
- js数组中的注意
1.数组删除 2.数组合并 3.原数组会被修改的数组方法有: 1)排序 .sotr() 2)逆序 .reverse() 3)数组拼接 .splice()
- 运行CUDA实例时候出现的问题
问题一:>LINK : fatal error LNK1123: 转换到 COFF 期间失败:文件无效或损坏 将 项目——项目属性——配置属性——连接器——清单文件——嵌入清单 “是”改为“否” ...