Title Description
The Andy \ (N \) white balls in a row, he wants to transfer some white Brush black, and any continuous \ (m \) balls must have at least \ (2 \) black balls.
Andy knew he needed \ (C_i \) dye brush section \ (i \) balls. Andy help you calculate how much dye his minimum needs.
Input
The first line of two integers \ (n-\) and \ (m \) .
The second row \ (n-\) integers, indicates \ (C_i \) .
For \ (30 \% \) data \ (n-\) data range \ ([1,20] \)
For \ (60 \% \) data \ (n-\) data range \ ([1,500] \) , \ (m \) range \ ([2,100] \) ;
For \ (80 \% \) data \ (n-\) data range \ ([1,10000] \) , \ (m \) range \ ([2,1000] \) ;
For \ (100 \% \) data \ (n-\) data range \ ([1,20000] \) , \ (m \) range \ ([2,2000] \) , \ (m≤n \) , \ (C_i \) range \ ([1,20000] \) ;
Output
Output required minimum number of dye.
Sample Input
6 3
1 5 6 2 1 3
Sample Output
9
When found to be a state transition to the next state, only the last two positions concerned stained, ( note that not a )
So that we can very easily play time complexity \ (O (n * n * m) \) code.
\ (dp [i] [j ] \) representing the forward \ (J \) balls to meet the conditions, the final ball \ (J \) and \ (I \) minimum cost on.
Then \ (dp [i] [k ] \) can be made \ (dp [j] [i ] \) transferred from, wherein due to continuous \ (m \) balls must have \ (2 \) black balls so \ (J \) must be in the ([km, i-1] \) \ job within the interval.
即\ (dp [i] [j] = min (dp [k] [i] + A [j]), jm \ k \ i-1 \)
Thereby had lost \ (60 \% \) data.
code show as below
#include <bits/stdc++.h>
using namespace std;
#define u64 unsigned long long
#define u32 unsigned int
#define reg register
#define Raed Read
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(reg int i=(G).Head[x]; i; i=(G).Nxt[i])
inline int Read() {
int res = 0, f = 1;
char c;
while (c = getchar(), c < 48 || c > 57)if (c == '-')f = 0;
do res = (res << 3) + (res << 1) + (c ^ 48);
while (c = getchar(), c >= 48 && c <= 57);
return f ? res : -res;
}
template<class T>inline bool Min(T &a, T const&b) {
return a > b ? a = b, 1 : 0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a < b ? a = b, 1 : 0;
}
const int N=2e4+5,M=1e5+5,mod=205;
bool MOP1;
int n,m,A[N];
struct T260 {
int dp[505][505];
inline void solve(void) {
memset(dp,63,sizeof dp);
rep(i,1,m)rep(j,i+1,m)dp[i][j]=A[i]+A[j];
rep(i,m+1,n) {
int L=i-m,R=i-1;
rep(j,L,R)rep(k,j+1,R)Min(dp[k][i],dp[j][k]+A[i]);
}
int L=n-m+1,R=n,Ans=1e9;
rep(i,L,R)rep(j,i+1,R)Min(Ans,dp[i][j]);
printf("%d",Ans);
}
} P60;
bool MOP2;
inline void _main(void) {
n=Raed(),m=Read();
rep(i,1,n)A[i]=Read();
P60.solve();
}
signed main() {
#define offline1
#ifdef offline
freopen("paint.in", "r", stdin);
freopen("paint.out", "w", stdout);
_main();
fclose(stdin);
fclose(stdout);
#else
_main();
#endif
return 0;
}
For the same \ (I \) , if \ (J \) descending enumerated, have overlapping portions can be directly (O (1) \) \ complexity obtain \ (dp [k] [I] \) , so that the complexity down \ (O (M * N) \) .
But we found, but we follow the above \ (dp \) when you define the space we had had enough.
The countdown has certain second goal in the countdown before the first ball \ (m \) within months.
Therefore, we only use the \ (DP \) status to \ (dp [i] [j ] \) represents one of the last \ (I \) , the penultimate bit from \ (I \) of \ (J \) minimum cost.
However, the most troublesome thing here, space is not enough! ! !
Thus, we can optimize use the scroll array, due to the current \ (DP [I] [J] \) , useful only \ ([i-m + 1 , i-1] \) in the range \ (DP \) value.
Thus, we can pressed into a first dimension \ (m \) , for \ (I \) modulo save space.
code show as below
#include <bits/stdc++.h>
using namespace std;
#define u64 unsigned long long
#define u32 unsigned int
#define reg register
#define Raed Read
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(reg int i=(G).Head[x]; i; i=(G).Nxt[i])
inline int Read() {
int res = 0, f = 1;
char c;
while (c = getchar(), c < 48 || c > 57)if (c == '-')f = 0;
do res = (res << 3) + (res << 1) + (c ^ 48);
while (c = getchar(), c >= 48 && c <= 57);
return f ? res : -res;
}
template<class T>inline bool Min(T &a, T const&b) {
return a > b ? a = b, 1 : 0;
}
template<class T>inline bool Max(T &a, T const&b) {
return a < b ? a = b, 1 : 0;
}
const int N=2e4+5,M=1e5+5,mod=205;
bool MOP1;
int n,m,A[N];
struct T2100 {
int dp[2005][2005],Mod[N];
inline void solve(void) {
int Ans=1e9;
rep(i,1,n)Mod[i]=i%m;
rep(i,1,n) {
memset(dp[Mod[i]],63,sizeof dp[Mod[i]]);
rep(j,max(0,i-m+1),i-1)Min(dp[Mod[i]][j+m-i],dp[Mod[j]][i-j]+A[i]);
drep(j,m,0)Min(dp[Mod[i]][j],dp[Mod[i]][j+1]);
if(i>=n-m+1)Min(Ans,dp[Mod[i]][n-i]);
}
printf("%d",Ans);
}
} P100;
bool MOP2;
inline void _main(void) {
n=Raed(),m=Read();
rep(i,1,n)A[i]=Read();
P100.solve();
}
signed main() {
#define offline1
#ifdef offline
freopen("paint.in", "r", stdin);
freopen("paint.out", "w", stdout);
_main();
fclose(stdin);
fclose(stdout);
#else
_main();
#endif
return 0;
}