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. ...
随机推荐
- ovs源码阅读--流表查询原理
背景 在ovs交换机中,报文的处理流程可以划分为一下三个步骤:协议解析,表项查找和动作执行,其中最耗时的步骤在于表项查找,往往一个流表中有数目巨大的表项,如何根据数据报文的信息快速的查找到对应的流表项 ...
- java读取excel或者csv时日期格式数据处理
背景:最近写一个通过excel批量导入数据的功能,里面含有时间,但是java读取之后把时间转为了距离1990年1月1号的天数,比如excel中时间为2018/9/16 18:30,java读取之后变成 ...
- nginx upstream 名称下划线问题
原始配置: user nobody;worker_processes 1; #pid logs/nginx.pid; worker_connections 1024;} http ...
- javascript提高篇
本章简介 本章内容比较少,有三个分享的知识.你可能都看过了,因为网上也有很多提问和解答,如果没看过或者没搞懂,你可以再看看这篇文章. 1. 数组去重方法的演变 -- 走向代码缩短化 2. [] ...
- Spring演示及总结
一.目标 二.分工 三.回顾 发现问题: 第一个冲刺的任务以基本完成,但队友的状态相对有些疲软,主要原因可能是这两周有好几个课程大作业要赶, 有的队友还要为比赛做准备,及做一些其他是项目,时间较紧,有 ...
- Alpha发布——美工+文案展示博客
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283 文案: 学海无涯苦作舟,深海的远帆扬起成长的新程. 我将一滴水滴注 ...
- Java JDK安装及环境配置
转载:https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html 环境变量配置: 系统变量→新建 JAVA_HOME 变量 . 变量 ...
- ASP.NET MVC5 学习系列之模型绑定
一.理解 Model Binding Model Binding(模型绑定) 是 HTTP 请求和 Action 方法之间的桥梁,它根据 Action 方法中的 Model 类型创建 .NET 对象, ...
- HDU 5167 Fibonacci 筛法+乱搞
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5167 题意: 给你一个x,判断x能不能由斐波那契数列中的数相乘得到(一个数可以重复使用) ...
- 未能加载文件或程序集“log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821”或它的某一个依赖项。系统找不到指定的文件。
在网上找了很久,很多个地方让修改配置文件,也有重装log4net的. 如文章:使用Common.Logging与log4net的组件版本兼容问题 我检查下发现项目中的package包中的Log4net ...