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 ...
随机推荐
- 如何成为一名hacker?
很多人问我如何成为黑客,一般来说我总是会扯一大堆废话,告诉他们要时刻保持好奇心,要学会打破规则挑战权威之类的. 但这类话肯定不是提问者想听的.我揣摩了一下他们的心情,他们想问的应该是这个问题:如何学习 ...
- 【C#】Get the html code of a webpage
As for the title,the console program will show you a sample which can get the webpage-code.Enjoy it: ...
- udp接收
char receive_buffer[500] = {0}; std::vector<std::string> mysplit(std::string str,std::string p ...
- asp.net mvc + mysql + ef6
1.通过NuGet包管理器安装:EntityFramework6.1.3.MySql.Data.Entity6.9.9 2.添加新建项→ADO.NET实体对象模型(命名MyContext)→空Code ...
- fileupload实现控制大小进行图片上传
if ($(".img-upload").length > 0) { $('.img-upload').fileupload({ type: 'POST', url: &qu ...
- log4j基本使用方法
通过配置文件可知,我们需要配置3个方面的内容: 1.根目录(级别和目的地) 2.目的地(控制台和文件等) 3.输出样式 Log4j由三个重要的组件构成: 1.日志信息的优先级 日志信息的优先级从高到低 ...
- pyhon的数据类型
1.数字 整型和浮点型 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2** ...
- find中的-print0和xargs中-0的区别
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...
- [HMLY]3.如何使用Xcode Targets管理开发和生产版本?
本文原地址:http://www.appcoda.com/using-xcode-targets/ 在开始此教程之前,我们假设你已经完成了应用程序的开发和测试,现在准备提交生产发布.问题是,某些web ...
- JAVA-反射学习
原文:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html 个人总结: 1. 反射是什么:由字节码文件(.class)获取 ...