【Gym - 101196F】Removal Game (环形区间dp,环状,细节优化)

题干:

健健开发了一个游戏叫做<<者护守形隐>>,里面有一个情节是这样的,女主子纯藤武被坏人关在了密室里,作为男主的肖健当然要英雄救美。但是要打开密室的门,必须解开一道谜题。
门上有几个数字围成的一个圈,每次消除一个数字的代价是这个数字旁边的两个数字的gcd,当最后消的只剩两个数时,消除这两个数的代价就是这两个数字的gcd,密室的密码就是消除所有数字的最小代价。
请你帮助肖健解决这个问题
例如数字2,3,4,5,他可以以2的代价(gcd(2,4)==2)消除3,或者以1的代价(gcd(3,5)==1)消除4,或者以1的代价(gcd(3,5)==1)消除2

Input

Input contains multiple test cases. Each test case consists of a single line starting with an integer n which indicates the number of values in the sequence (2 ≤ n ≤ 100). This is followed 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

题目大意:

一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价。

解题报告:

这题如果不提前把gcd打出来在codeforce上不会T(900ms左右),但是比赛的机子好像就T了。但是这题其实是可以变成环来做的。但是区别就是最后的更新ans不能直接就是让他长度是n的来更新,因为最后剩下的两个点可能在任意的位置,所以我们需要n^2枚举这两个位置,然后分别更新答案,这样得到的才是正解。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int a[MAX];
int g[1005][1005];
int dp[205][205];
int n;
int main()
{for(int i = 0; i<=1000; i++) {for(int j = 0; j<=1000; j++) {g[i][j] = __gcd(i,j);}}while(~scanf("%d",&n)) {if(n == 0) break;memset(dp,0x3f,sizeof dp);for(int i = 1; i<=n; i++) scanf("%d",a+i),a[i+n] = a[i];for(int i = 1; i<=2*n - 1; i++) dp[i][i+1] = 0;//g[a[i]][a[i+1]];for(int i = 1; i<=2*n; i++) dp[i][i] = 0;for(int len = 3; len<=n; len++) {for(int l = 1; l+len-1<=2*n; l++) {int r = l+len-1;for(int k = l+1; k<r; k++) {dp[l][r] = min(dp[l][r] , dp[l][k] + dp[k][r] + g[a[l]][a[r]]);}}}int ans = 0x3f3f3f3f;for(int i = 1; i<=n; i++) {for(int j = i+1; j<=n; j++) {ans = min(ans,dp[i][j] + dp[j][i+n] + g[a[i]][a[j]]);}}printf("%d\n",ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440505.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java day_Java_Day7(上)

Java learning_Day7(上)内容常用类枚举类型常用类String 类java.lang.String 类代表不可变的字符序列。String 类的常见构造方法&#xff1a;String(String original)创建一个 String 对象为 original 的拷贝。String(char[] value)用一个字符数组创建一个 String 对象。String…

【FZU - 2202】犯罪嫌疑人(思维,假装建图,分类讨论)

题干&#xff1a; 福尔摩斯是个大侦探&#xff0c;他总是在解决疑难案件。这一次的案件也不例外&#xff0c;案件是这样的&#xff1a;有编号为1到N的N位嫌疑犯&#xff0c;他们其中有一个犯了罪&#xff0c;然后每个嫌疑犯都被询问,“哪一个人犯了罪&#xff1f;”犯罪嫌疑人…

java关键字 valotile_Java内存模型-jsr133规范介绍,java中volatile关键字的含义

最近在看《深入理解Java虚拟机&#xff1a;JVM高级特性与最佳实践》讲到了线程相关的细节知识&#xff0c;里面讲述了关于java内存模型&#xff0c;也就是jsr 133定义的规范。系统的看了jsr 133规范的前面几个章节的内容&#xff0c;觉得受益匪浅。废话不说&#xff0c;简要的介…

【HDU - 5050 】Divided Land (Java大数,大数进制转换,大数gcd)

题干&#xff1a; It’s time to fight the local despots and redistribute the land. There is a rectangular piece of land granted from the government, whose length and width are both in binary form. As the mayor, you must segment the land into multiple squar…

Java重载和重写6_深入理解Java中的重写和重载

深入理解Java中的重写和重载重载(Overloading)和重写(Overriding)是Java中两个比较重要的概念。但是对于新手来说也比较容易混淆。本文通过两个简单的例子说明了他们之间的区别。定义重载简单说&#xff0c;就是函数或者方法有同样的名称&#xff0c;但是参数列表不相同的情形&…

【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

题干&#xff1a; Just days before the JCPC, your internet service went down. You decided to continue your training at the ACM club at your university. Sadly, you discovered that they have changed the WiFi password. On the router, the following question wa…

java框架讲解ppt_经典框架spring介绍课件.ppt

Wepull Information Service Spring-javaEE的春天 预习检查 根据你的理解&#xff0c;“依赖注入”是什么&#xff1f; 根据你的理解&#xff0c;谈谈“依赖注入”适于在项目中应用吗&#xff1f; 你理解的“面向方面编程(AOP)”是什么样的&#xff1f; Spring框架的内容和作用…

python 协程池gevent.pool_进程池\线程池,协程,gevent

目录1. 进程池与线程池2. 协程3. gevent4. 单线程下实现并发的套接字通信首先写一个基于多线程的套接字服务端:from socket import *from threading import Threaddef comunicate(conn):while True: # 通信循环try:data conn.recv(1024)if len(data) 0: breakconn.send(data.…

【ZOJ - 3963】Heap Partition (STLset,二叉树的性质,构造,贪心,思维)

题干&#xff1a; A sequence S {s1, s2, ..., sn} is called heapable if there exists a binary tree Twith n nodes such that every node is labelled with exactly one element from the sequence S, and for every non-root node si and its parent sj, sj ≤ si and j…

java poi 3.13_Java 读取Excl文件 (poi-3.13)

最近做项目遇到了读取Excel数据到数据库做数据的初始化。于是找一个。发现(poi-3.13)可以解决问题。可以解析两种格式(xlsx和xls)以下是实现的步骤1.下载poi3.13包&#xff0c;地址(http://poi.apache.org/download.html#POI-3.13)2.学习APi。接下来是还是demo来说明问题吧&…

【CodeChef - CLIQUED 】Bear and Clique Distances(建图,缩点技巧,思维)

题干&#xff1a; 解题报告&#xff1a; 主要就是在于怎么处理那个前K个点&#xff1a;组成一个团。换句话说&#xff0c;缩成一个点。先直接当成每个点多了k条边来处理&#xff0c;T了。想想也是啊&#xff0c;要是K1e5&#xff0c;那就是1e10条边了。。刚开始尝试了半天缩点&…

【HDU - 5649】DZY Loves Sorting(线段树,区间更新区间查询,思维,01缩数变换,线段树分割)

题干&#xff1a; DZY has a sequence a[1..n]a[1..n]. It is a permutation of integers 1∼n1∼n. Now he wants to perform two types of operations: 0lr0lr: Sort a[l..r]a[l..r] in increasing order. 1lr1lr: Sort a[l..r]a[l..r] in decreasing order. After doin…

php错误403_phpstudy访问文件报错403/Forbidden解决办法

使用phpstudy访问WWW目录下的文件时&#xff0c;浏览器提示Forbidden错误&#xff0c;没有访问权限。我在网上搜索了喝多资料以及本人亲自尝试过后&#xff0c;总结了一下两种方法。方法一&#xff1a;打开phpStudy&#xff0c;点击按键“其他选项菜单”>找到phpStudy配置&g…

【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

题干&#xff1a; Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelfs dimensions to be as small as possible. The thickness of the i-th book is ti and its pages width is equal to wi. The thickness of each book is eith…

mac php开发集成环境,MAC OS X下php集成开发环境mamp

之前苦于mac上搭建本地服务器之艰辛&#xff0c;找寻好久都没找到一款类似windows上集成的本地服务器环境&#xff0c;诸如phpstudy&#xff0c;xampp,appserv,虽说xampp也有mac版&#xff0c;但不知为何不是Apache启动不了&#xff0c;这里小编为大家分享了MAC OS X 下php集成…

知识点总结vector创建二维数组

vector构造函数通常含有两个参数 原型如下&#xff1a; vector( size_type num, const TYPE &val ); 数量(num)和值(val) - 构造一个初始放入num个值为val的元素的Vector方法1&#xff1a; #include <iostream> #include<vector> #include<algorithm&…

php获取手机目录,php如何获取手机型号

手机App中判断平台&#xff0c;可以根据$_SERVER[HTTP_USER_AGENT]中的内容来判断浏览器类型或手机平台。(推荐学习&#xff1a;PHP编程从入门到精通)iPhone UA&#xff1a;Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, l…

【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)

题干&#xff1a; You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices…

php获取post全部数据,PHP获取POST数据的几种方法汇总_PHP教程

PHP获取POST数据的几种方法汇总本文给大家汇总介绍了PHP获取POST数据的几种常用方法&#xff0c;这里分享给大家&#xff0c;有需要的小伙伴来参考下吧。一、PHP获取POST数据的几种方法方法1、最常见的方法是&#xff1a;$_POST[‘fieldname’];说明&#xff1a;只能接收Conten…

yiilite.php,缓存 - yii在哪些情况下可以加载yiilite.php?

yii权威指南上说&#xff0c;在开启apc缓存的情况下&#xff0c;可以加载yiilite.php提升性能。我有以下几点疑问&#xff1a;1.开启apc缓存的情况下&#xff0c;引入yiilite.php能提升性能的原因是因为缓存了opcode的关系么&#xff1f;2.使用其他缓存服务缓存opcode的情况下&…