codeforces 46C Hamsters and Tigers(枚举)
来源:互联网 发布:大学生it就业培训中心 编辑:程序博客网 时间:2024/06/08 03:36
题目链接
Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
3HTH
0
9HTHTHTHHT
2
题意:输入一个字符串,H代表仓鼠的位置,T代表老虎的位置。第一个字符和最后一个字符是相邻的,也就是一个环。每次可以交换两个动物的位置,求最少交换多少次使得所有仓鼠站在一起,所有的老虎站在一起?
题解:统计出老虎的个数和仓鼠的个数。枚举老虎站在一起所在的区间,假设为[l,r] ,那么最少交换的次数就是 [l,r] 中仓鼠的个数。由于是一个环,所以要用同样的方法枚举仓鼠站在一起所在的区间。我们可以先处理出所有前缀的仓鼠的个数和老虎的个数,就可以O(1)查询一个区间中老虎或者仓鼠的个数。
代码如下:
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<string>#include<queue>#include<stack>#include<map>#include<set>#include<stdlib.h>#include<vector>#define inff 0x3fffffff#define nn 110000#define mod 1000000007typedef long long LL;const LL inf64=inff*(LL)inff;using namespace std;int n;char s[nn];int h[nn],t[nn];int main(){ int i; int ans; while(scanf("%d",&n)!=EOF) { scanf("%s",s+1); ans=inff; h[0]=t[0]=0; for(i=1;i<=n;i++) { h[i]=h[i-1]; t[i]=t[i-1]; if(s[i]=='H') h[i]++; else t[i]++; } for(i=1;i+h[n]-1<=n;i++) { ans=min(ans,t[i+h[n]-1]-t[i-1]); } for(i=1;i+t[n]-1<=n;i++) { ans=min(ans,h[i+t[n]-1]-h[i-1]); } printf("%d\n",ans); } return 0;}
- codeforces 46C Hamsters and Tigers(枚举)
- Codeforces-734C-Anton and Making Potions(枚举+二分)
- codeforces 493C Vasya and Basketball(枚举)
- Codeforces 558C - Amr and Chemistry (枚举)
- Pasha and Hamsters
- codeforces #311 C 557C. Arthur and Table(枚举+贪心)
- codeforces 354C C. Vasya and Beautiful Arrays( 数论+枚举)
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- Codeforces Round #372 (Div. 2) -- C. Plus and Square Root(找规律枚举)
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions(枚举+二分)
- Codeforces 679C Bear and Square Grid(枚举 标记)
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
- Codeforces 673C Bear and Colors【暴力枚举】
- Codeforces 355C Vasya and Robot 暴力枚举
- Codeforces 892 C. Pride (枚举)
- codeforces 519C C. A and B and Team Training(枚举)
- Codeforces 425A Sereja and Swaps(暴力枚举)
- codeforces-#476B. Dreamoon and WiFi(dfs、二进制枚举)
- UVa 1601 - The Morning after Halloween(双向BFS版)
- 【1197】约瑟夫问题
- 自动监控进程并启动的shell脚本
- thinkphp left join 一定要写上left
- linux ftp和lftp 命令小结
- codeforces 46C Hamsters and Tigers(枚举)
- 黑马程序员——Java基础---深入理解面向对象(封装继承和多态)
- #ifdef __cplusplus extern "C" { #endif”的定义
- IOS的app图标去掉玻璃效果
- Xcode6 使用MJRefresh
- 解决JBoss不能远程访问只能使用localhost或127.0.0.1在本机访问
- 集训小记 3
- 【 随笔 】 为什么要写技术文章
- Java中的对List中元素进行自定义排序