Luo Gu P4158 [SCOI2009] painters solution to a problem

Daily questions day59 punch

Analysis

It is easy to see that a dp,

dp [i] [j [k] [0/1] to indicate to the (i, j), the k-th brush, 0 indicates that no brush, 1 denotes a brush.

Then there is the transfer:

1. Be sure to re-brush line feed

2. If the grid with a grid before the same color, the best way is to put an intact state before a transfer, when the 0 state also followed a greedy count intact:

dp[i][j][k][1]=dp[i][j-1][k][1]+1;

dp[i][j][k][0]=dp[i][j-1][k][0];

1 3. Otherwise, there are two options: (Do not forget we set up the state 1 is mandatory that contribute to grid) is a k Change Color sacrifice a brush, and the other is to continue on a grid of colors

dp[i][j][k][1]=max(dp[i][j-1][k-1][1]+1,dp[i][j-1][k][0]+1);

4.0 also greedy, because this grid to keep up with a different, so if you want to continue to brush the wrong, probably come from a 1 on intact, then a brush might be making the wrong brush.

dp[i][j][k][0]=max(dp[i][j-1][k][1],dp[i][j-1][k-1][0]);

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define int long long
 7 #define rep(i,s,e) for(register int i=s;i<=e;++i)
 8 #define dwn(i,s,e) for(register int i=s;i>=e;--i)
 9 using namespace std;
10 inline int read()
11 {
12     int x=0,f=1;
13     char c=getchar();
14     while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
15     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
16     return f*x;
17 }
18 inline void write(int x)
19 {
20     if(x<0) {putchar('-'); x=-x;}
21     if(x>9) write(x/10);
22     putchar(x%10+'0');
23 }
24 int n,m,t,ans;
25 int map[51][51];
26 int dp[51][51][2501][2];
27 signed main()
28 {
29     n=read();m=read();t=read();
30     rep(i,1,n)
31         rep(j,1,m)
32         {
33             char c=getchar();
34             while(c!='0'&&c!='1') c=getchar();
35             map[i][j]=c-'0';
36         }
37     rep(i,1,n)
38         rep(j,1,m)
39             rep(k,1,t)
40             {
41                 if(j==1)
42                 {
43                     dp[i][j][k][0]=max(dp[i-1][m][k-1][0],dp[i-1][m][k-1][1]);
44                     dp[i][j][k][1]=max(dp[i-1][m][k-1][0],dp[i-1][m][k-1][1])+1;
45                 }
46                 else 
47                 {
48                     if(map[i][j]==map[i][j-1])
49                     {
50                         dp[i][j][k][1]=dp[i][j-1][k][1]+1;
51                         dp[i][j][k][0]=dp[i][j-1][k][0];
52                     }
53                     else
54                     {
55                         dp[i][j][k][0]=max(dp[i][j-1][k-1][0],dp[i][j-1][k][1]);
56                         dp[i][j][k][1]=max(dp[i][j-1][k-1][1],dp[i][j-1][k][0])+1;
57                     }
58                 }
59                 ans=max(ans,max(dp[i][j][k][0],dp[i][j][k][1]));
60             }
61     write(ans);        
62     return 0;
63 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/12039910.html