大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间.
每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天dp即可.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 510; #else const int N = 111; #endif int n, m, k, tot; char s[N][N]; int f[N][N], g[N][N], dp[N][N]; int main() { scanf("%d%d%d", &n, &m, &k); REP(i,1,n) scanf("%s",s[i]+1); REP(i,1,n) { vector<int> v; REP(j,1,m) if (s[i][j]=='1') v.pb(j); int sz=v.size(); if (sz) { memset(dp,0,sizeof dp); v.insert(v.begin(),0); PER(len,1,sz-1) { REP(L,1,sz+1-len) { int R = L+len-1; if (L!=1) dp[L][R]=max(dp[L][R],dp[L-1][R]+v[L]-v[L-1]); if (R!=sz) dp[L][R]=max(dp[L][R],dp[L][R+1]+v[R+1]-v[R]); f[i][sz-len]=max(f[i][sz-len],dp[L][R]); } } f[i][sz] = f[i][sz-1]+1; tot += v[sz]-v[1]+1; } REP(j,0,k) REP(jj,0,min(j,sz)) { g[i][j]=max(g[i][j],g[i-1][j-jj]+f[i][jj]); } } printf("%d ", tot-g[n][k]); }