Luo Gu P1564 worship

Topic Portal

Problem-solving ideas:

The A admirers set to -1, 1 to admirers acetate, and seeking prefix, F [i] represents the minimum number required for the i-th individual room.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 int n,m,a[2501],j,y,f[2501],sum[2501];
 9 
10 int main() {
11     memset(f,0x3f3f3f,sizeof(f));
12     scanf("%d%d",&n,&m);
13     for(int i = 1;i <= n; i++) {
14         scanf("%d",&a[i]);
15         if(a[i] == 2) sum[i] = sum[i-1] - 1;
16         else sum[i] = sum[i-1] + 1; 
17     }
18     f[0] = 0;
19     for(int i = 1;i <= n; i++)
20         for(int j = 1;j <= i; j++)
21             IF (ABS (SUM [I] - SUM [J- . 1 ]) == I - J + . 1 || ABS (SUM [I] - SUM [J- . 1 ]) <= m)
 22 is              // this period all equal or both by no more than the number of m 
23 is                  F [I] = min (F [I], F [J- . 1 ] + . 1 );
 24      the printf ( " % D " , F [n-]);
 25      return  0 ;
 26 is }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/12386736.html