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;
}

发布了123 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104653371