2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp
Problem F Removal Game
Bobby Roberts is totally bored in his algorithms class, so he’s developed a little solitaire game. He writes down a sequence of positive integers and then begins removing them one at a time. The cost of each removal is equal to the greatest common divisor (gcd) of the two surrounding numbers (wrapping around either end if necessary). For example, if the sequence of numbers was 2, 3, 4, 5 he could remove the 3 at a cost of 2 (= gcd(2,4)) or he could remove the 4 at a cost of 1 (= gcd(3,5)). The cost of removing 2 would be 1 and the removal of 5 would cost 2. Note that if the 4 is removed first, the removal of the 3 afterwards now has a cost of only 1. Bobby keeps a running total of each removal cost. When he ends up with just two numbers remaining he takes their gcd, adds that cost to the running total, and ends the game by removing them both. The object of the game is to remove all of the numbers at the minimum total cost. Unfortunately, he spent so much time in class on this game, he didn’t pay attention to several important lectures which would lead him to an algorithm to solve this problem. Since none of you have ever wasted time in your algorithm classes, I’m sure you’ll have no problem finding the minimum cost given any sequence of numbers.
Input
Input contains multiple test cases. Each test case consists of a single line starting with an integer n whichindicates thenumber ofvaluesin thesequence (2 ≤ n ≤ 100). This isfollowed by n positive integers which make up the sequence of values in the game. All of these integers will be≤ 1000. Input terminates with a line containing a single 0. There are at most 1000 test cases.
Output
For each test case, display the minimum cost of removing all of the numbers.
Sample Input 1
4 2 3 4 5
5 14 2 4 6 8
0
Sample Output 1
3
8
题意:给你一个长度为n的序列 每删除一个数的代价为与他相邻的的两个数的gcd 注意是一个循环的序列 把这个序列首尾相接考虑
题解:参看tyvj1056 写法稍微不同,思想类似。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
const int N=1e3+,M=1e6+,inf=1e9+,mod=;
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
ll a[N];
ll dp[N][N];
ll ff[][];
int main()
{
ll x,i,t;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
ff[i][j]=gcd(i,j);
}
}
while(scanf("%I64d",&x)!=EOF)
{
if(x==)
break;
for(i=; i<=x; i++)
scanf("%I64d",&a[i]),a[i+x]=a[i];
for(int i=; i<=*x; i++)
{
dp[i][i]=;
for(int j=i+; j<=*x; j++)
{
dp[i][j]=;
}
}
for(t=; t<=x; t++)
{
for(i=; i+t<*x; i++)
{
for(ll k=i; k<t+i; k++)
{
if((i+x)==(t+i+))//终态只剩下两个 直接求gcd 更新
dp[i][i+t]=min(dp[i][i+t],dp[i][k]+dp[k+][t+i]+ff[a[i]][a[k+]]);
else
dp[i][i+t]=min(dp[i][i+t],dp[i][k]+dp[k+][t+i]+ff[a[i]][a[t+i+]]);
}
}
}
ll ans=;
for(i=; i<=x; i++)
ans=min(ans,dp[i][i+x-]);
printf("%I64d\n",ans);
}
return ;
}
2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp的更多相关文章
- Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)
A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...
- 2017-2018 ACM-ICPC East Central North America Regional Contest (ECNA 2017) Solution
A:Abstract Art 题意:给出n个多边形,求n个多边形分别的面积和,以及面积并 思路:模板 #include <bits/stdc++.h> using namespace st ...
- 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】
任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...
- [bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation
Problem D Lost in Translation The word is out that you’ve just finished writing a book entitled How ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010
ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...
- poj 2732 Countdown(East Central North America 2005)
题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...
- East Central North America Region 2015
E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
随机推荐
- HDU-6315:Naive Operations(线段树+思维)
链接:HDU-6315:Naive Operations 题意: In a galaxy far, far away, there are two integer sequence a and b o ...
- 基于preteus的1602液晶显示器的学习(LM016L)
(证明学过,以示纪念) 所谓1602就是每行可以显示16个字符,可以显示两行.1602液晶在工业中使用比较广泛,其基本都采用的是HD44780控制器,或者兼容该指令集,因此基于HD44780写的控制程 ...
- 基于C#的机器学习--模糊逻辑-穿越障碍
模糊逻辑-穿越障碍 模糊逻辑.另一个我们经常听到的术语.但它的真正含义是什么?它是否意味着不止一件事?我们马上就会知道答案. 我们将使用模糊逻辑来帮助引导一辆自动驾驶汽车绕过障碍,如果我们做得正确,我 ...
- 利用Tensorflow进行自然语言处理(NLP)系列之二高级Word2Vec
本篇也同步笔者另一博客上(https://blog.csdn.net/qq_37608890/article/details/81530542) 一.概述 在上一篇中,我们介绍了Word2Vec即词向 ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- Scrum立会报告+燃尽图(十一月二十日总第二十八次):功能开发与纪录版本控制报告
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...
- 20181023-10 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 07
作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2290 Scrum master:范靖旋 一.小组介绍 组长:王一可 组员: ...
- 【探路者】Alpha发布用户使用报告
预期统计用户使用数量:13人. 博文内容:1用户列表.2评论列表.3统计与总结 1用户列表: 二.评论内容 用户1:1不够好看.2不应该是中国地图为背景,蛇头是人物头像的么?(那是宣传片,不是预览图) ...
- 使用RStudio学习一个简单神经网络
数据准备 1.收集数据 UC Irvine Machine Learning Repository-Concrete Compressive Strength Data Set 把下载到的Concre ...
- oracle 行转列和列转行
WITH L AS ( ), m AS ( SELECT A.LV AS LV_A, B.LV AS LV_B, TO_CHAR(B.LV) || 'x' || TO_CHAR(A.LV) || '= ...