题目&题意:(有点难读...)

给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作。

例如:

所以执行两次删除操作。

思路:

区间dp,关键在于确定大的区间是由哪些小的区间转化来的。

当a[l] == a[r]的时候,dp[l][r] = dp[l+1][r-1]+1(因为要得到最多的删除次数,大的区间的次数在相等的情况下肯定是由内部小的区间加一得来的);

当a[l] != a[r]的时候,dp[l][r] = max(dp[l+1][r],dp[l][r-1])(这个自己模拟的出的......)

代码:

内层循环枚举长度:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
const int maxn = +;
int dp[maxn][maxn];
int a[maxn]; int main(){
//FRE();
int n;
while(scanf("%d",&n)!=EOF){
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
}
for(int i = ; i<=n; i++){
for(int j = ; j<=n; j++){
dp[i][j] = ;
}
}
//memset(dp,0,sizeof(dp));
for(int k=; k<n; k++){//枚举区间长度
for(int l=; l+k<=n; l++){//枚举区间的起点
if(a[l] == a[l+k]){
dp[l][l+k] =dp[l+][l+k-]+;
}
else{
dp[l][l+k] = max(dp[l+][l+k],dp[l][l+k-]);
}
}
}
printf("%d\n",dp[][n]);
}
return ;
}
/*
PutIn:
3
6 6 6
12
3 14 15 92 65 35 89 79 32 38 46 26
12
3 1 4 1 5 9 2 6 5 3 5 9
7
2 7 1 8 2 8 1
4
1 6 1 8
11
1 2 4 8 16 32 16 8 4 2 1
6
1 2 3 1 2 3
PutOut:
1
0
2
2
1
5
1
*/

内层循环枚举区间右端点

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
const int maxn = 5e3+;
int dp[maxn][maxn];
int a[maxn]; int main(){
//FRE();
int n;
while(scanf("%d",&n)!=EOF){
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
}
for(int i = ; i<=n; i++){
for(int j =; j<=n; j++){
dp[i][j] = ;
}
}
for(int l=n; l>=; l--){//只能逆序来枚举起点,正序大区间的值无法更新
for(int r=l+; r<=n; r++){
if(a[l] == a[r]){
dp[l][r] = dp[l+][r-]+;
}else {
dp[l][r] = max(dp[l+][r],dp[l][r-]);
//dp[l][r] = max(dp[l][r-1],dp[l][r]);
}
}
}
printf("%d\n",dp[][n]);
}
return ;
}

区间dp过程大致相同:

第一层循环枚举区间的长度,第二层循环枚举区间的起点。

第二层又有两种情况:

第一种:需要在[st,en]中找一个分割点k使得将[st,en]分成[st,k]和[k+1,en]这样两个区间能够得到最优解。

第二种:[i,j]可以由[i,j-1]或者[i,j+1]转移过来。这种转移关系肯定是有具体的情况推出的,不是一成不变的。

Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)的更多相关文章

  1. Gym - 101670H Go Northwest!(CTU Open Contest 2017 思维题+map)

    题目: Go Northwest! is a game usually played in the park main hall when occasional rainy weather disco ...

  2. Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)

    题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...

  3. Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

    题目: To encourage visitors active movement among the attractions, a circular path with ice cream stan ...

  4. Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)

    题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...

  5. Gym - 101670H Dark Ride with Monsters(CTU Open Contest 2017 贪心)

    题目: A narrow gauge train drives the visitors through the sequence of chambers in the Dark Ride attra ...

  6. Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)

    题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下)  Bishop(斜 ...

  7. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)

    题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...

  8. Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)

    题目: The park management finally decided to install some popular boxing machines at various strategic ...

  9. CTU Open Contest 2017

    这场题很水.水题我就懒得贴了. B - Pond Cascade 优先队列维护这个水池需要多少时间 或者 直接扫一遍. #include <cstdio> #include <cst ...

随机推荐

  1. Linux 系统内核空间与用户空间通信的实现与分析

    本文转载自:https://www.ibm.com/developerworks/cn/linux/l-netlink/index.html 多数的 Linux 内核态程序都需要和用户空间的进程交换数 ...

  2. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  3. Web框架 - 开源软件库 - 开源中国社区

    网址:http://www.oschina.net/project/tag/127?lang=194

  4. 【转载pku】三十分钟掌握STL

    三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有 ...

  5. js实现IOS上删除app时颤抖动画j函数

    欢迎提供更好的方法! <!--http://www.cnblogs.com/webzhangnan/p/3244920.html --> <html> <head> ...

  6. .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097A) (80131506)

    VS2010调试IIS发布的web工程提示:无法连接到 Visual Studio 开发服务器 .NET Runtime version 2.0.50727.8762 - 执行引擎错误(7969097 ...

  7. E20171225-hm

    abstract  adj. 抽象的,理论上的; 难解的; 抽象派的; 茫然的;

  8. bzoj 1594: [Usaco2008 Jan]猜数游戏【二分+线段树】

    写错一个符号多调一小时系列-- 二分答案,然后判断这个二分区间是否合法: 先按值从大到小排序,然后对于值相同的一些区间,如果没有交集则不合法:否则把并集在线段树上打上标记,然后值小于这个值的区间们,如 ...

  9. git分支的理解

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  10. Node“getTextContent() is undefined for the type Node”处理办法

    最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...