标签:eof string back clu ring spl lap += ble
参考资料:
[1]:挑战程序设计竞赛
先献上AC代码,题解晚上再补
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define mem(a,b) (memset(a,b,sizeof(a))) 6 const int maxn=5e3+30; 7 8 int N; 9 int dir[maxn];//0:forward; 1:backward 10 int f[maxn]; 11 12 int Calculate(int k) 13 { 14 mem(f,0); 15 int res=0; 16 int sum=0; 17 for(int i=1;i+k-1 <= N;++i) 18 { 19 if(i-k > 0)//如果在 i-k 处使用机器,作用的范围为 [i-k,i-1] ,共 k 头牛,所以需要在第 i 处减去在 i-k 处的反转次数 20 sum -= f[i-k]; 21 if((dir[i]+sum)%2 != 0) 22 res++,f[i]=1; 23 sum += f[i]; 24 } 25 for(int i=N-k+2;i <= N;++i) 26 { 27 if(i-k > 0)//同上解释 28 sum -= f[i-k]; 29 if((dir[i]+sum)%2 != 0) 30 return -1; 31 } 32 return res; 33 } 34 void Solve() 35 { 36 int K=1,M=N; 37 for(int k=1;k <= N;++k)//每次反转 k 头牛 38 { 39 int m=Calculate(k); 40 if(m != -1 && m < N) 41 K=k,M=m; 42 } 43 printf("%d %d\n",K,M); 44 } 45 46 int main() 47 { 48 scanf("%d",&N); 49 for(int i=1;i <= N;++i) 50 { 51 getchar(); 52 char c; 53 c=getchar(); 54 dir[i]=(c == ‘F‘ ? 0:1); 55 } 56 Solve(); 57 }
标签:eof string back clu ring spl lap += ble
原文地址:https://www.cnblogs.com/violet-acmer/p/9802218.html