Girls Love 233
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 720 Accepted Submission(s): 250Problem DescriptionBesides skipping class, it is also important to meet other girls for luras in the new term.
As you see, luras sneaked into another girl's QQgroup to meet her indescribable aim.
However, luras can only speak like a cat. To hide her real identity, luras is very careful to each of her words.
She knows that many girls love saying "233",however she has already made her own word at first, so she needs to fix it.
Her words is a string of length n,and each character of the string is either '2' or '3'.
Luras has a very limited IQ which is only m.
She could swap two adjacent characters in each operation, which makes her losing 2 IQ.
Now the question is, how many substring "233"s can she make in the string while her IQ will not be lower than 0 after her operations?
for example, there is 1 "233" in "2333", there are 2 "233"s in "2332233", and there is no "233" in "232323".InputThe first line is an integer T which indicates the case number.
and as for each case,
the first line are two integers n and m,which are the length of the string and the IQ of luras correspondingly.
the second line is a string which is the words luras wants to say.
It is guaranteed that——
1 <= T <= 1000
for 99% cases, 1 <= n <= 10, 0 <= m <= 20
for 100% cases, 1 <= n <= 100, 0<= m <= 100OutputAs for each case, you need to output a single line.
there should be one integer in the line which represents the largest possible number of "233" of the string after her swap.Sample Input3 6 2 233323 6 1 233323 7 4 2223333Sample Output2 1 2Source
【分析】
考虑交换完之后的序列的样子:
假设是2333233->2333332
只考虑2的移动就好了,肯定是第一个2对应末状态第一个2,以此类推。。。
所以DP考虑2填在什么位置就好了。
f[i][j][k][p]表示填了i个数,有j个2,花费了k,p是最后几个数的状态。
0表示没有,1表示有‘2’,2表示有’23‘。
然后直接状态转移就好了【T那么大理论上不是过不了的吗??
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define INF 0xfffffff 8 9 int mymax(int x,int y) {return x>y?x:y;} 10 11 int f[160][160][160][3]; 12 int pos[160]; 13 char s[210]; 14 15 int main() 16 { 17 int T; 18 scanf("%d",&T); 19 while(T--) 20 { 21 int n,m,sm=0; 22 scanf("%d%d",&n,&m); 23 scanf("%s",s+1); 24 for(int i=1;i<=n;i++) if(s[i]=='2') pos[++sm]=i; 25 // memset(f,0,sizeof(f)); 26 for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) for(int k=0;k<=m;k++) f[i][j][k][0]=f[i][j][k][1]=f[i][j][k][2]=-INF; 27 f[0][0][0][0]=0; 28 int ans=0; 29 for(int i=1;i<=n;i++) 30 for(int j=0;j<=sm;j++) 31 for(int k=0;k<=m;k++) 32 { 33 int ad=2*abs(i-pos[j+1]); 34 f[i][j+1][k+ad][1]=mymax(f[i][j+1][k+ad][1],f[i-1][j][k][0]); 35 f[i][j+1][k+ad][1]=mymax(f[i][j+1][k+ad][1],f[i-1][j][k][1]); 36 f[i][j+1][k+ad][1]=mymax(f[i][j+1][k+ad][1],f[i-1][j][k][2]); 37 38 f[i][j][k][0]=mymax(f[i][j][k][0],f[i-1][j][k][0]); 39 f[i][j][k][2]=mymax(f[i][j][k][2],f[i-1][j][k][1]); 40 f[i][j][k][0]=mymax(f[i][j][k][0],f[i-1][j][k][2]+1); 41 if(j==sm) 42 { 43 ans=mymax(ans,f[i][j][k][0]); 44 ans=mymax(ans,f[i][j][k][1]); 45 ans=mymax(ans,f[i][j][k][2]); 46 } 47 } 48 printf("%d ",ans); 49 } 50 return 0; 51 }
2017-04-19 10:38:30