CodeForces - 512B Fox And Jumping[map优化dp]
B. Fox And Jumping
2 seconds
256 megabytes
standard input
standard output
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.
The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
3
100 99 9900
1 1 1
2
5
10 20 30 40 50
1 1 1 1 1
-1
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
6
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
7237
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.

#include<map>
#include<cstdio>
using namespace std;
const int N=1e5+;
int n,c[N],l[N];
map<int,int>f;
map<int,int>::iterator it;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&l[i]);
for(int i=;i<=n;i++) scanf("%d",&c[i]);
for(int i=;i<=n;i++) if(!f.count(l[i])) f[l[i]]=c[i];else f[l[i]]=min(f[l[i]],c[i]);
for(int i=;i<=n;i++){
for(it=f.begin();it!=f.end();it++){
if(it->first==l[i]) continue;
int t=gcd(l[i],it->first);
if(!f.count(t)) f[t]=it->second+c[i];
else f[t]=min(f[t],it->second+c[i]);
}
}
printf("%d\n",f[]?f[]:-);
return ;
}
CodeForces - 512B Fox And Jumping[map优化dp]的更多相关文章
- Codeforces 512B: Fox And Jumping
题目链接 题意说的是,有n种卡片,使用第i种卡片可以使当前自己在数轴上的位置移动 l[i],要获得使用第i种卡片的代价是 c[i],求能使自己移动到数轴上任意位置的最小代价,如果不可能则输出-1 当前 ...
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- CodeForces 311 B Cats Transport 斜率优化DP
题目传送门 题意:现在有n座山峰,现在 i-1 与 i 座山峰有 di长的路,现在有m个宠物, 分别在hi座山峰,第ti秒之后可以被带走,现在有p个人,每个人会从1号山峰走到n号山峰,速度1m/s.现 ...
- Consecutive Subsequence CodeForces - 977F (map优化DP)·
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...
- Codeforces 570E - Pig and Palindromes - [滚动优化DP]
题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- CodeForces 834D The Bakery(线段树优化DP)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- BZOJ 3357 [Usaco2004]等差数列:map优化dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3357 题意: 给你n个数a[i],让你找出一个最长的是等差数列的子序列. 题解: 表示状态 ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- zend opcache的最佳设置
2016-01-21 在网上无意中看到的一篇文章,这哥们非常简洁地谈论了zend opcache的最佳设置,他说他为此花了大量的时间探索zend opcache的每个设置选项的细节,甚至是阅读它的源代 ...
- ggplot2——简介
ggplot2是R语言最为强大的作图软件包,强于其自成一派的数据可视化理念.当熟悉了ggplot2的基本套路后,数据可视化工作将变得非常轻松而有条理. 本文主要对ggplot2的可视化理念及开发套路做 ...
- AOP——引言
转自:http://wayfarer.cnblogs.com/articles/241012.html 1.引言 2.AOP技术基础3.Java平台AOP技术研究 4..Net平台AOP技术研究 软件 ...
- 关于Unity中的删除、显示和隐藏节点
一.删除节点和组件 1.删除一个节点,以及节点上面所有的组件全部删除了//删除一个节点的时候,节点上面所有的组件也会被删除:MonoBehaviour.Destroy(this.gameObject) ...
- 关于Cocos2d-x中背景音乐和音效的添加
1.首先引入头文件和命名空间 #include "SimpleAudioEngine.h" using namespace CocosDenshion; 2.在GameScene. ...
- 关于Cocos2d-x中让主角运动的方法
比如要让角色跳起来 1.如果是用到物理引擎,那么在物理世界中,可以用 hero->getPhysicsBody()->setVelocity(Vec2(0, 400)); //给主角一个 ...
- struts 标签 牛逼之处
<s:hidden>标签的value属性的类型是String类型,所以把<s:property value="#session.LOGIN_USER"/>当 ...
- Android startActivityForResult 回传数据
一个activity打开新的activity,新的activity关闭之后,返回数据.原来的activity要接收返回的数据,在开启新的activity时,就需要调用startActivityForR ...
- page指令属性简要介绍:
page指令属性简要介绍: language=”java” 声明脚本语言的种类,暂时只能用”java” extends=”package.class” 标明JSP编译时需要加入的Java Class的 ...
- 蔡勒(Zeller)公式
来源好搜百科:http://baike.haosou.com/doc/1048888-1109421.html 蔡勒(Zeller)公式,是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星 ...