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的更多相关文章

  1. Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

    A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

  6. 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 ...

  7. poj 2732 Countdown(East Central North America 2005)

    题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...

  8. East Central North America Region 2015

    E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...

  9. 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. ...

随机推荐

  1. Git + Gerrit 操作备忘

    Git review 作用 可以用来提交代码审核到Gerrit 安装 使用pip 安装 git-review 插件,执行 sudo -H pip install git-review 使用示例 可以参 ...

  2. 排查GCC 4.4.X版本优化switch-enum的BUG

    起因 一次偶然碰到一个诡异的bug,现象是同一份C++代码使用GCC4.4.x版本在开启优化前和优化后的结果不一样,优化后的代码逻辑不正确. 示例代码如下: //main.cpp #include & ...

  3. scikit-learn使用PCA降维小结

    本文在主成分分析(PCA)原理总结和用scikit-learn学习主成分分析(PCA)的内容基础上做了一些笔记和补充,强调了我认为重要的部分,其中一些细节不再赘述. Jupiter notebook版 ...

  4. 基础系列(5)—— C#控制语句

    语句是程序中最小程序指令.C#语言中可以使用多种类型的语句,每一种类型的语句又可以通过多个关键字实现.以下是C# 语言中使用的主要控制语句 类别 关键字 选择语句  if.else.switch.ca ...

  5. 结对编程--fault,error,failure的程序设计

    一.结对编程内容: 1.不能触发Fault. 2.触发Fault,但是不触发Error. 3.触发Error,但不触发Failure. 二.结对编程人员 1.周浩,周宗耀 2.结对截图: 三.结对项目 ...

  6. 【Leetcode】771. Jewels and Stones

    (找了leetcode上最简单的一个题来找一下存在感) You're given strings J representing the types of stones that are jewels, ...

  7. 把握曝光三要素(上):快门、光圈、ISO

    概要: 如果你还没有掌握快门.光圈和ISO,那这篇文章或许对你有所帮助! 把照片比作水池.把进光量比作水.把快门比作关闭水龙头的速度.把光圈比作水龙头的大小.把感光度ISO比作水龙头的滤网,这就变得好 ...

  8. C语言 aabbcc、abc、fabc、aabc

    输入一个字符串,匹配字符串中连续出现的字符串.并且连续个数相等 如输入 aabbcc.abc.fabc.aabc.aabbc 分别输出yes还是no #include<stdio.h>#i ...

  9. 【bzoj4897】[Thu Summer Camp2016]成绩单 区间dp

    题目描述 给你一个数列,每次你可以选择连续的一段,付出 $a+b\times 极差^2$ 的代价将其删去,剩余部分拼到一起成为新的数列继续进行此操作.求将原序列全部删去需要的最小总代价是多少. 输入 ...

  10. varnish启动报错

    错误1.Starting Varnish Cache: Error: Cannot open socket: :80: Address family not supported by protocol ...