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 = 5000+10;
int dp[maxn][maxn];
int a[maxn];

int main(){
    //FRE();
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
        }
        for(int i = 1; i<=n; i++){
            for(int j = 1; j<=n; j++){
                dp[i][j] = 0;
            }
        }
        //memset(dp,0,sizeof(dp));
        for(int k=1; k<n; k++){//枚举区间长度
            for(int l=1; l+k<=n; l++){//枚举区间的起点
                if(a[l] == a[l+k]){
                    dp[l][l+k] =dp[l+1][l+k-1]+1;
                }
                else{
                    dp[l][l+k] = max(dp[l+1][l+k],dp[l][l+k-1]);
                }
            }
        }
        printf("%d\n",dp[1][n]);
    }
    return 0;
}
/*
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+10;
int dp[maxn][maxn];
int a[maxn];

int main(){
    //FRE();
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
        }
        for(int i = 1; i<=n; i++){
            for(int j =1; j<=n; j++){
                dp[i][j] = 0;
            }
        }
        for(int l=n; l>=0; l--){//只能逆序来枚举起点,正序大区间的值无法更新
            for(int r=l+1; r<=n; r++){
                if(a[l] == a[r]){
                    dp[l][r] = dp[l+1][r-1]+1;
                }else {
                    dp[l][r] = max(dp[l+1][r],dp[l][r-1]);
                    //dp[l][r] = max(dp[l][r-1],dp[l][r]);
                }
            }
        }
        printf("%d\n",dp[1][n]);
    }
    return 0;
}

区间dp过程大致相同:

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

第二层又有两种情况:

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

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

原文地址:https://www.cnblogs.com/sykline/p/9898398.html

时间: 2024-09-29 10:26:48

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

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

题目: To encourage visitors active movement among the attractions, a circular path with ice cream stands was built in the park some time ago. A discount system common for all stands was also introduced. When a customer buys ice cream at some stand, he

CTU Open Contest 2017 Go Northwest!(思维题+map)

题目: Go Northwest! is a game usually played in the park main hall when occasional rainy weather discourages the visitors from enjoying outdoor attractions. The game is played by a pair of players and it is based entirely on luck, the players can hardl

(寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)

layout: post title: (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! 许老师! B.Buildings (polya定理) 题意 B:给你m面墙,每面墙是n*n的格子,你有c种颜色,问你有多少种涂色方案.用po

CodeForces 30C Shooting Gallery 简单dp

题目链接:点击打开链接 给定n个气球 下面n行 x y t val 表示气球出现的坐标(x,y) 出现的时刻t,气球的价值val 枪每秒移动1个单位的距离 问: 射击的最大价值,开始时枪瞄准的位置任意. 思路: dp一下.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set

The North American Invitational Programming Contest 2017 题目

NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain. Ming has two opera

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! 许老师! Hello SCPC 2018! (签到) #include<bits/stdc++.h> using namespace std; typedef

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces - DP - 状态压缩 - LCA 传送门 付队! C - Greetings! (状态压缩) 题意 给N种信件,你可以任意选择K种信封装信件,问你最少的浪费是多少 不能大的信件装

[ICPC训练联盟周赛1] CTU Open Contest 2019

昨天ICPC训练联盟进行了第一场比赛,题目是CTU Open Contest 2019,烟台大学给出了解析. 题目和题解在此(提取码zre3) 我和索队还有yz组队,喊着队友nb就AK了,抱大腿真爽. A题是简单的组合数学,显然有公式 \(1\times C_n^1+2\times C_n^2+ \dots + n \times C_n^n=n \times 2^{n-1}\) 但忘了考虑\(n=0\)的情况,我wa了一发. B题题目我不太读得懂,yz A的. C题是算圆和矩形相交的面积,我抄了

ICPC North Central NA Contest 2017 部分题解

ICPC North Central NA Contest 2017 部分题解 B. Pokemon Go Go 大意:用最短路径来抓住所有的稀有精灵,DFS求最短路 #include <bits/stdc++.h> #define mem(a) memset(a,0,sizeof(a)) #define forn(i,n) for(int i=0;i<n;++i) #define for1(i,n) for(int i=1;i<=n;++i) #define IO std::io