Codeforces 846 A Curriculum Vitae 思维 暴力

本文解析了CodeForces上的一道A级题目,通过枚举分割点的方法找到最优解,确保0不会出现在1的右侧,同时给出了详细的解题思路及代码实现。

  题目链接: http://codeforces.com/contest/846/problem/A

  题目描述: 给你一个串, 你可以做删除操作, 要求结果串0不能在1的右边, 问最多可以剩几个数字

  解题思路: 我们可以看最后的结果是什么, 结果一定是全0或者全1, 或左0右1, 这样我们暴力枚举分割点就可以了

  代码: 

#include <iostream>
#include <cstdio>
#include <map>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long ll;
const int maxn = 1e2+10;
int a[maxn];

int main() {
    int n;
    cin >> n;
    int ans1, ans0;
    ans1 = ans0 = 0;
    for( int i = 1; i <= n; i++ ) {
        cin >> a[i];
        if( a[i] == 0 ) ans0++;
        else ans1++;
    }
    int ans = 0;
    for( int i = 1; i <= n; i++ ) {
        int cnt0,cnt1;
        cnt0 = cnt1 = 0;
        for( int j = 1; j <= i; j++ ) {
            if( a[j] == 0 ) cnt0++;
        }
        for( int j = i+1; j <= n; j++ ) {
            if( a[j] == 1 ) cnt1++;
        }
        ans = max(cnt1+cnt0, ans);
        
        cnt0 = cnt1 = 0;
        for( int j = 1; j < i; j++ ) {
            if( a[j] == 0 ) cnt0++;
        }
        for( int j = i; j <= n; j++ ) {
            if( a[j] == 1 ) cnt1++;
        }
        ans = max(cnt1+cnt0, ans);
        
    }

    cout << max(ans, max(ans1, ans0)) << endl;
    return 0;
}
View Code

  思考: 一开始忘了初始化了, WA了一发, MDZZ!!!

http://codeforces.com/contest/846/problem/A

 

转载于:https://www.cnblogs.com/FriskyPuppy/p/7616412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值