codeforces贪心 B. Students in Railway Carriage

B. Students in Railway Carriage
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are
n
consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.

The university team for the Olympiad consists of
a
student-programmers and
b
student-athletes. Determine the largest number of students from all
a
+
b
students, which you can put in the railway carriage so that:

no student-programmer is sitting next to the student-programmer;
and no student-athlete is sitting next to the student-athlete.
In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.

Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

Input
The first line contain three integers
n
,
a
and
b
(
1

n

2

10
5
,
0

a
,
b

2

10
5
,
a
+
b
>
0
) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.

The second line contains a string with length
n
, consisting of characters “.” and “*”. The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

Output
Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

Examples
inputCopy
5 1 1

outputCopy
2
inputCopy
6 2 3
.
outputCopy
4
inputCopy
11 3 10
.….*.*.
outputCopy
7
inputCopy
3 2 3


outputCopy
0
Note
In the first example you can put all student, for example, in the following way: .AB

In the second example you can put four students, for example, in the following way: *BAB*B

In the third example you can put seven students, for example, in the following way: B*ABAB**A*B

The letter A means a student-programmer, and the letter B — student-athlete.

这道题贪心,主要原则是当前面一个是‘*’,那就放a,b两个中最多的一个
我傻逼的为甚么超时到奔溃,因为strlen()函数是O(n)的复杂度,如果这样写
for(int i = 0;i < strlen(ans);++i)这样写是O(n * n)的复杂度,所以会超时
应该这样写,int len = strlen(ans); for(int i = 0;i < len;++i)
总结:这属于习惯问题,为甚么别人一次就AC,自己写代码的习惯不好,还有遇到精度问题,不能做到很好的处理

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
const int N = 200005;
char ans[N];
int main()
{
    LL n,a,b;
    scanf("%lld %lld %lld",&n,&a,&b);
        scanf("%s",ans);
        //printf("%d\n",sum);
        LL cnt = 0;
        int len = strlen(ans);
        for(int i = 0;i < len;++i)
        {
            if(a == 0 && b == 0)
                break;
            if(ans[i] == '.'){
                if(i == 0 || ans[i - 1] == '*' || ans[i - 1] == '.'){
                    if(a >= b){
                        a--;
                        ans[i] = 'a';
                        cnt++;
                    }
                    else{
                        b--;
                        ans[i] = 'b';
                        cnt++;
                    }
                }
                else{
                    if(a > 0 && ans[i - 1] == 'b'){
                        a--;
                        ans[i] = 'a';
                        cnt++;
                    }
                    if(b > 0 && ans[i - 1] == 'a'){
                        b--;
                        ans[i] = 'b';
                        cnt++;
                    }
                }
            }
        }
        //printf("%s\n",ans);
        printf("%lld\n",cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值