Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)
给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作。
例如:

所以执行两次删除操作。
思路:
区间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)的更多相关文章
- 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 ...
- Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)
题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...
- Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)
题目: To encourage visitors active movement among the attractions, a circular path with ice cream stan ...
- Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)
题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...
- 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 ...
- Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)
题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下) Bishop(斜 ...
- 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 ...
- Gym - 101670J Punching Power(CTU Open Contest 2017 最大独立集)
题目: The park management finally decided to install some popular boxing machines at various strategic ...
- CTU Open Contest 2017
这场题很水.水题我就懒得贴了. B - Pond Cascade 优先队列维护这个水池需要多少时间 或者 直接扫一遍. #include <cstdio> #include <cst ...
随机推荐
- Robot Framework快捷键图标制作 去掉cmd命令窗口
安装好Robot Framework之后,通过 C:\Python27\Scripts\ride.py 启动时会带上一个命令行窗口: 怎样让启动的界面后面不带这个命令行窗口,且图片以机器人显示? 方法 ...
- data-toggle data-target
data-toggle https://stackoverflow.com/questions/30629974/how-does-the-data-toggle-attribute-work-wha ...
- 怎么样关掉红米note开发者选项
进 系统设置\应用 ,找到“设置”点进去,清一下数据,再打开“设置”查看,就没有“开发者选项”了
- UVA - 12345 带修改的莫队
题意显然:给出初始序列,单点修改,区间查询元素的种类. 由于时限过宽,暴力可过. 比较优秀的解法应该是莫队. 带修改的莫队题解可以看https://www.luogu.org/blog/user126 ...
- uva1560
In an extended version of the game Lights Out®, is a puzzle with 5 rows of 6 buttons each (the actua ...
- 如何给mysql用户分配权限+增、删、改、查mysql用户
在mysql中用户权限是一个很重析 参数,因为台mysql服务器中会有大量的用户,每个用户的权限需要不一样的,下面我来介绍如何给mysql用户分配权限吧,有需要了解的朋友可参考. 1,Mysql下创建 ...
- E20180113-hm
round robin algorithm 轮询调度算法 circular adj. 圆形的; 环行的; 迂回的,绕行的; 供传阅的,流通的;
- 统一微信公众号、小程序、APP的用户信息
上次接手一个项目需要整合公众号.小程序以及APP的用户,查阅了微信文档以及一些作者的文章,中间踩了不少坑,在此记录一下解决的流程. 要点 实现统一信息的有以下几点: 1. 在微信开放平台绑定需要 ...
- org.apache.poi.hssf.util.Region
从POI 3.18开始被Deprecated,在3.20版本中被移除了,所以3.20以前的都有 为了避免这个问题,用CellRangeAddress代替Region,其用法相同
- linux C编程 gdb的使用
linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...