一种新的 (dp) 优化方法
设 (f[i][j]) 表示涂到第 (i) 个球,涂了 (i) 和 (j) 的最小代价
首先能够想出一种朴素的转移方程: (f[i][j]=min(f[j][k])+c[j]~(k>i+1-m))
这样是 (O(nm^2)) 的,我们尝试去优化
对于 (j,i~in~{j+1,j+m-1},k~in~{i+1-m+1,j-1})
然后你会发现,如果倒叙枚举 (i),(f[j][k]) 范围依次变大,所以 (k) 就是 (i-m)
然后就搞定了
时间复杂度 (O(nm))
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 110 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 101 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf
", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
void upd(int &a, int b) { (a += b) %= MOD ; }
void mul(int &a, int b) { a = 1ll * a * b % MOD ; }
int n, m ;
int c[10010] ;
int f[N][N] ; // f[i][j] 表示涂到第i个球,涂了第i,第j个球的最小代价
// f[i][j] = min(f[j][k])+c[j] (k > i + 1 - m)
// 对于 j, i in {j+1,j+m-1},k in {i+1-m+1,j-1}
// 倒着枚举i,f[j][k] 范围依次变大,k 就是 i-m
signed main() {
scanf("%d%d", &n, &m) ;
rep(i, 1, n) scanf("%d", &c[i]) ;
ass(f, 0x3f) ;
f[1][0] = c[1] ;
rep(i, 1, m)
rep(j, 1, i - 1)
f[i][j] = c[i] + c[j] ;
rep(j, 2, n - 1) {
int tmp = 0x3f3f3f3f ;
per(i, j + m - 1, j + 1) {
if (i <= m) break ;
tmp = min(tmp, f[j % MOD][(i - m) % MOD]) ;
f[i % MOD][j % MOD] = tmp + c[i] ;
}
}
int ans = 0x7fffffff ;
rep(i, n - m + 1, n)
for (int j = i - 1; i - j < m && n - j < m; j--)
ans = min(ans, f[i % MOD][j % MOD]) ;
printf("%d
", ans) ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/