"The main topic"
Supporters of two teams are going to take a bus to see the ball, and they're in a row. We have to get them to take a number of buses, and the people on the same bus must be continuous in the queue. In order to avoid conflict in the car, it is hoped that the two teams to the same number of supporters, the difference is d. One exception is that the people in a car are all supporters of a team. Ask to send all the N people to the stadium, at least a few buses. The first line is the integer N and d,1≤n≤2500,1≤d≤n. The next n rows, in the order in which they are queued, describe the teams supported by each person, denoted by H or J.
The output must be at least a few buses.
Sample input
14 3
H
J
H
H
H
J
H
J
H
H
H
H
H
H
Sample output
2
Ideas
Record the number of people who support the two teams, and the next preprocessing [I,j] can sit in a car, and if they can sit in a car, they must meet one of the following three conditions:
(1) [I,j] All support the H team, namely h[j]-h[i-1]==j-i+1;
(2) [I,j] All support J team, namely j[j]-j[i-1]==j-i+1;
(3) [I,j] Support for H and J are less than or equal to D, i.e. | (H[j]-h[i-1])-(j[j]-j[i-1]) |≤d.
After the preprocessing is finished, the interval [i,j],car[j] is determined to indicate the minimum number of vehicles to be required by the J-person. If a person within the [i,j] interval can have a car, then Car[j]=min (car[j],car[i-1]+1).
1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 Const intmaxn=2500+ -;6 intn,d;7 intCAN[MAXN][MAXN];8 intH[MAXN],J[MAXN];9 intCAR[MAXN];Ten One intMain () A { -Freopen ("Mr354.in0","R", stdin); -Freopen ("mr354.ou0","W", stdout); thescanf"%d%d",&n,&d); -memset (H,0,sizeof(H)); -memset (J,0,sizeof(J)); -Memset (CAN,0,sizeof(Can)); + for(intI=1; i<=n;i++) - { + GetChar (); A CharC; atscanf"%c",&c); -h[i]=h[i-1]; -j[i]=j[i-1]; - if(c=='H') h[i]++;Elsej[i]++; -car[i]=i; - } in - to for(intI=1; i<=n;i++) + for(intj=i;j<=n;j++) - if(h[j]-h[i-1]==j-i+1|| j[j]-j[i-1]==j-i+1|| ABS ((j[j]-j[i-1])-(h[j]-h[i-1])) <=d) thecan[i][j]=1; * $ for(intI=1; i<=n;i++)Panax Notoginseng for(intj=1; j<=i;j++) - if(Can[j][i]) theCar[i]=min (car[i],car[j-1]+1); + Acout<<car[n]<<Endl; the return 0; +}
"Dynamic planning" mr354-to see the ball by car