洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way

Posted third2333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way相关的知识,希望对你有一定的参考价值。

 

 

 1 #include <bits/stdc++.h>
 2 #define For(i, j, k) for(int i=j; i<=k; i++)
 3 #define Dow(i, j, k) for(int i=j; i>=k; i--)
 4 #define LL long long
 5 using namespace std;
 6 inline int read() {
 7     int x = 0, f = 1;
 8     char ch = getchar();
 9     while(ch<0||ch>9) { if(ch==-) f = -1; ch = getchar(); }
10     while(ch>=0&&ch<=9) { x = x*10+ch-48; ch = getchar(); }
11     return x * f;
12 }
13 void write(int x) {
14     if(x<0) putchar(-), x = -x;
15     if(x>9) write(x/10);
16     putchar(x%10+48);
17 }
18 inline void writeln(int x) { write(x); putchar(\n); }
19 
20 const int N = 5011;
21 int n, ans, KK; 
22 int a[N], f[N]; 
23 
24 int main() {
25     n = read(); 
26     For(i, 1, n) {
27         char ch[5]; 
28         scanf("%s", ch+1); 
29         if(ch[1]==B) a[i] = 1; 
30     }
31     
32     ans = 1e9; 
33     For(K, 1, n) {
34         int t = 0, ok = 1, sum = 0; 
35         For(i, 1, n) f[i] = 0;  
36         For(i, 1, n-K+1) {
37             if( (a[i]+t)%2==1 ) {
38                 ++f[i+K-1]; 
39                 ++t; 
40                 ++sum; 
41             }
42             t -= f[i]; 
43             f[i] = 0; 
44         }
45         For(i, n-K+2, n) {
46             if( (a[i]+t)%2==1 ) {
47                 ok = 0; 
48                 break; 
49             }
50             t -= f[i]; 
51             f[i] = 0; 
52         }
53         if(ok && sum<ans) {
54             KK = K; 
55             ans = sum; 
56         }
57     }
58     printf("%d %d\n", KK, ans); 
59 }

 

以上是关于洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way的主要内容,如果未能解决你的问题,请参考以下文章

[USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)

洛谷 P2904 [USACO08MAR]跨河River Crossing

洛谷 P2945 [USACO09MAR]沙堡Sand Castle

洛谷P2947 [USACO09MAR]仰望Look Up

洛谷 P3079 [USACO13MAR]农场的画Farm Painting

洛谷2115 [USACO14MAR]破坏Sabotage