Codeforces Round #318 D. Bear and Blocks DP

dp[i]表示第i列被消除需要的操作次数, dp[i] = min(i, n-i, h[i], dp[i-1]+1, dp[i+1]+1),,i、n-i表示每次消除边缘一列直到第i列的情况,h[i]表示每次消除顶端直到为0的情况,dp[i-1]+1和dp[i+1]+1表示左右两侧消除完再操作一次消除i列的情况,前三个读入的时候

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair
#define mk make_pair
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repr(i, n) for(int (i)=(int)(n);(i)>=0;(i)--)
#define repab(i,a,b) for(int (i)=(int)(a);(i)<=(int)(b);(i)++)
#define reprab(i,a,b) for(int (i)=(int)(a);(i)>=(int)(b);(i)--)
#define sc(x) scanf("%d", &x)
#define pr(x) printf("x:%d\n", x)
#define fastio ios::sync_with_stdio(0), cin.tie(0)
#define frein freopen("in.txt", "r", stdin)
#define freout freopen("out.txt", "w", stdout)
#define freout1 freopen("out1.txt", "w", stdout)
//#define lb puts("")
#define lson ((rt<<1)+1)
#define rson ((rt<<1)+2)
#define mid ((l+r)/2)
#define lmid (l+(r-l)/3)
#define rmid (r-(r-l)/3)
#define debug cout<<"???"<

const double PI = 3.1415926535897932384626433;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-12;
template T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}

const int maxn = 2e5+10;

int n, m, g, h[maxn], dp[maxn];
int main()
{
    //frein;
    //freout;
    cin >> n;
    for(int i = 1; i <= n; i++){
        sc(h[i]);
        dp[i] = min(i, n-i+1);
        dp[i] = min(dp[i], h[i]);
    }
    dp[0] = dp[n+1] = INF;
    for(int i = 1; i <= n; i++){
        dp[i] = min(dp[i], dp[i-1]+1);
    }
    int ans = -1;
    for(int i = n; i >= 1; i--){
        dp[i] = min(dp[i], dp[i+1]+1);
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
    return 0;
}

设为初值,dp[i-1]+1从左到右跑一次,dp[i+1]+1从右到左跑一次


你可能感兴趣的:(打牌)