B: 排数字(签到题)

2020牛客寒假算法基础集训营2

传送门

题意:

在这里插入图片描述

分析:

要想最多应该这样排列6161616…然后讨论6和1字符的个数即可

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#define Pll make_pair
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int phi=1e9+6;
char ch[maxn];
int main()
{
    int n;
    scanf("%d ",&n);
    int p1=0,p2=0;
    for(int i=1;i<=n;i++){
        char k;
        scanf("%c",&k);
        if(k=='1')p1++;
        if(k=='6')p2++;
    }
    //cout<<p1<<" "<<p2<<endl;
    if(p1<p2){
        printf("%d\n",p1);
        return 0;
    }
    else if(p1==p2){
        printf("%d\n",max(0,p1-1));
        return 0;
    }
    else {
        printf("%d\n",max(0,p2-1));
        return 0;
    }
    return 0;
}
发布了142 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44091178/article/details/104202601