3369 Worship
time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingTitle Description
Description
There are many gods and cows ... Of course... Every classmate has his own heartfelt worship of the God Ox.
A school has two gods, God New Gae and God Newyi. The newly enrolled N students have already heard their myth. So, I have worshipped one of them heartily.
Now, the teacher will give them a room to divide.
However, either guarantee that the entire room is the same God-ox cult, or two gods cattle worship the number of the poor not more than M.
In addition, now n students lined up in a row, the teacher will only be a continuous section of the students into a room. The teacher wants to know how many rooms are needed at least.
Enter a description
Input Description
The first line of the input file includes N and M.
Then n rows, an integer per line, 1 represents the New Gae of God, and 2 represents the Newyi of God.
Output description
Output Description
Outputs an integer that represents the minimum number of required rooms.
Sample input
Sample Input
5 1
2
2
1
2
2
Sample output
Sample Output
2
Data range and Tips
Data Size & Hint
For 30% of the data, there are 1≤n,m≤50;
For 100% of data, there are 1≤n,m≤2500
/*F[i] Indicates the minimum number of rooms required by the first I students in a period of j+1~i, if the students in this interval meet the conditions, you can get the state transfer equation f[i]=max{f[j]+1}*/#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#defineM 2510using namespacestd;intF[m],a[m];intMain () {Freopen ("jh.in","R", stdin); Memset (F,0x7f,sizeof(f)); intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) { intx; scanf ("%d",&A[i]); } f[0]=0; for(intI=1; i<=n;i++) { intt1=0, t2=0; if(a[i]==1) t1++; Elset2++; for(intj=i-1; j>=0; j--) { if(!t1| |! t2| | ABS (T1-T2) <=m) F[i]=min (f[i],f[j]+1); if(a[j]==1) t1++; Elset2++; }} printf ("%d", F[n]); return 0;}
View Code
Worship (Codevs 3369)