Face The Right Way POJ - 3276 (开关问题)
2018-08-21 05:28:13来源:博客园 阅读 ()
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6707 | Accepted: 3123 |
Description
Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect.
Fortunately, FJ recently bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N) cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa.
Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K.
Input
Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.
Output
Sample Input
7 B B F B F B B
Sample Output
3 3
Hint
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<set> #include<map> #include<vector> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-10 typedef long long ll; const int maxn = 5e3+3; const int mod = 1e9 + 7; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int N,M; int dir[maxn],f[maxn]; //牛的方向 F:0 B:1 int calc(int K) { memset(f,0,sizeof(f)); int res=0,sum=0; for(int i=0;i+K<=N;i++) { if((dir[i]+sum)%2!=0) { res++; f[i]=1; } sum+=f[i]; if(i-K+1>=0) sum-=f[i-K+1]; } for(int i=N-K+1;i<N;i++) //检查剩下的牛是否有面朝后方的情况 { if((dir[i]+sum)%2!=0) return -1; if(i-K+1>=0) sum-=f[i-K+1]; } return res; } void solve() { int K=1; int M=N; for(int k=1;k<=N;k++) { int m=calc(k); if(m>=0 && M>m) { M=m; K=k; } } cout<<K<<" "<<M<<endl; } int main() { scanf("%d",&N); int num=0; for(int i=0;i<N;i++){ char ch; cin>>ch; if(ch=='B') dir[num]=1; else dir[num]=0; // cout<<dir[num]<<" "; num++; } solve(); return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- SGU 132 Another Chocolate Maniac 2020-03-06
- 协程的原理(Coroutine Theory) 2020-02-23
- 二叉堆(2)LeftistHeap 2020-02-19
- Boost C++ 库 中文教程(全) 2019-12-19
- Run-Time Check Failure #0 - The value of ESP was not pro 2019-11-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash