ICPC North Central NA Contest 2017 H.Zebras and Ocelots

H. Zebras and Ocelots

计蒜客重现赛题目链接-H. Zebras and Ocelots
The famous City Central Zoo houses just two creatures, Zebras and Ocelots. These magical creatures spend their time in a single, tall column, each standing atop the back of the creature below it. Whenever the zoo bell rings, the ocelot lowest in the pile turns into a zebra, and all zebras (if there are any) below that creature simultaneously turn into ocelots. If there is no ocelot in the pile when the bell tolls, then nothing happens. The zookeeper has been watching this interesting process for years. Today, suddenly, he begins to wonder how much longer this can go on. That is, given a pile of zebras and/or ocelots, how many times may the bell toll before there are no more ocelots?
Input
Input consists of a number N in the range 1 to 60, followed by N lines, each of which is a single character, either Z (for zebra) or O(for ocelot). These give the order of the creatures from top (first) to bottom (last).

Output
Output should be a single integer giving the number of times the bell must toll in order for there to be no more ocelots.
输出时每行末尾的多余空格,不影响答案正确性

样例输入1

3
Z
O
Z

样例输出1

2

样例输入2

4
O
Z
Z
O

样例输出2

9
题目大意

给出一个Z和O的序列,每次变化序列最后的O变成Z,后面的Z变成O,问需要多少次变化才能使该序列没有O全是Z

解题思路

找规律即可
倒数第i位的O把它变为Z所需的次数是2n-i
所以用快速幂就行

附上代码
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
string s;
ll quick_pow(ll x,ll n){
    ll ans=1;
    while(n){
        if(n&1) 
			ans*=x;
        x*=x;
        n>>=1;
    }
    return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int n;
	cin>>n;
	ll ans=0;
	for(int i=n-1;i>=0;i--)
		cin>>s[i];
	for(int i=0;i<n;i++){
		if(s[i]=='O')
			ans+=quick_pow(2,i);
	}
	cout<<ans<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值