【codeforces 46C】Hamsters and Tigers

【题目链接】:http://codeforces.com/problemset/problem/46/C

【题意】

给你一个长度为n的01串;
让你把所有的0放在一起,把所有的1放在一起;
(即0都是连续着在一起,1也是连续着在一起)
问你最少需要进行多少次交换操作;

【题解】

先算出仓鼠有多少只ch;
然后枚举每一个点i;
最后的结果必然是仓鼠都在连续的一段;
则假设在[i,i+ch-1]这一段;
算出里面有多少只狮子;
则把这些狮子移到这个区间外面去就好(把仓鼠换进来);
在所有区间里面取最小的狮子数目;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("D:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int oo = 0x3f3f3f3f;

int n,ch,len,ans=oo;
string s;

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n;
    cin >> s;
    len = (int) s.size();
    rep1(i,0,(int) s.size()-1){
        if (s[i]=='H') ch++;
    }
    s = s+s;
    rep1(i,0,len-1){
        int ct = 0;
        rep1(j,i,i+ch-1)
            if (s[j]=='T')
                ct++;
        ans = min(ans,ct);
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626281.html