CF 1114 D. Flood Fill

本文介绍了一种使用区间动态规划解决颜色序列问题的方法,旨在找到将颜色序列统一成单一颜色所需的最少操作次数。通过详细解析算法思路和分享C++实现代码,帮助读者理解区间DP的应用。

D. Flood Fill

链接

题意:

  一个颜色序列,每个位置有一个颜色,选择一个起始位置,每次可以改变包含这个位置的颜色段,将这个颜色段修改为任意一个颜色, 问最少操作多少次。n<=5000

分析:

  区间dp。

  dp[i][j][0/1]表示当前的区间是l~r,把这一段变成一个颜色的最少次数,最后一个改变的颜色是最左边/最右边的一段。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int read() {
    int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
}

const int N = 5005;
int f[N][N][2], a[N], b[N];

int dp(int l,int r,int p) {
    if (l > r) return 0;
    if (l == r) return 0;
    if (f[l][r][p]) return f[l][r][p];
    int &res = f[l][r][p];
    if (p == 0) {
        res = dp(l + 1, r, 0) + (a[l + 1] != a[l]);
        res = min(res, dp(l + 1, r, 1) + (a[r] != a[l]));
    }
    else {
        res = dp(l, r - 1, 0) + (a[l] != a[r]);
        res = min(res, dp(l, r - 1, 1) + (a[r - 1] != a[r]));
    }
    return res;
}
int main() {
    int n = read();
    for (int i = 1; i <= n; ++i) a[i] = read();
    int cnt = 0;
    for (int i = 1; i <= n; ++i) if (a[i] != a[i - 1]) b[++cnt] = a[i];
    n = cnt;
    for (int i = 1; i <= n; ++i) a[i] = b[i];
    cout << min(dp(1, n, 1), dp(1, n, 0));
    return 0;
}

 

转载于:https://www.cnblogs.com/mjtcn/p/10361269.html

import xarray as xr import numpy as np import pandas as pd # 1. 读取NetCDF文件 ds = xr.open_dataset(r'D:\Python\pythonProject1\pre_1960_2014_fixed.nc', chunks={'time': 100}) # 分块读取处理大数据 # 2. 时间维度处理(假设时间单位是天数) # 转换为datetime格式(需根据实际时间单位调整) time_units = ds.time.attrs.get('units', 'days since 1900-01-01') times = xr.decode_cf(ds).time # 提取年份和月份 year = times.dt.year month = times.dt.month # 选择5-8月数据 summer_months = ds.sel(time=ds.time.dt.month.isin([5, 6, 7, 8])) # 3. 标准化降水(按月计算Z-score) def standardize_by_month(data): """按月计算标准化降水""" # 按月分组计算均值和标准差 monthly_mean = data.groupby('time.month').mean('time', skipna=True) monthly_std = data.groupby('time.month').std('time', skipna=True) # 标准化处理 standardized = (data.groupby('time.month') - monthly_mean) standardized = standardized.groupby('time.month') / np.maximum(monthly_std, 1e-6) # 避免除零 return standardized # 应用标准化 pre_std = standardize_by_month(summer_months['pre']) # 4. SDFAI计算函数 def calculate_sdfai(pi, pj): """计算单个月对的SDFAI指数""" diff = pj - pi abs_sum = np.abs(pi) + np.abs(pj) exponent = -np.abs(pi + pj) return diff * abs_sum * np.power(3.2, exponent) # 5. 计算每年的指数对(5-6,6-7,7-8) def compute_yearly_sdfai(dataset): """计算每年的SDFAI指数""" years = np.unique(dataset.time.dt.year) results = [] for year in years: year_data = dataset.sel(time=dataset.time.dt.year == year) # 提取各月数据 may = year_data.sel(time=year_data.time.dt.month == 5).pre june = year_data.sel(time=year_data.time.dt.month == 6).pre july = year_data.sel(time=year_data.time.dt.month == 7).pre august = year_data.sel(time=year_data.time.dt.month == 8).pre # 计算三个指数对 may_june = calculate_sdfai(may, june) june_july = calculate_sdfai(june, july) july_august = calculate_sdfai(july, august) # 组合为年度数据 year_sdfai = xr.concat([may_june, june_july, july_august], dim=xr.DataArray(['May-Jun', 'Jun-Jul', 'Jul-Aug'], name='period')) results.append(year_sdfai.expand_dims(dim={'year': [year]})) return xr.concat(results, dim='year') # 6. 执行计算并保存结果 sdfai_result = compute_yearly_sdfai(pre_std.to_dataset(name='pre')) # 添加描述性属性 sdfai_result.attrs = { 'long_name': 'Short-term Drought-Flood Abrupt Alternation Index', 'units': 'dimensionless', 'formula': '(Pj-Pi)*(|Pi|+|Pj|)*3.2^(-|Pi+Pj|)', 'calculation_period': 'May to August' } # 保存结果 sdfai_result.to_netcdf('sdfai_results.nc') print("SDFAI计算完成,结果已保存至sdfai_results.nc") 这是最开始进行SDFAI计算的程序,能不能直接在这个程序的基础上直接生成分析图,不用生成sdfai_results.nc
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值