Students in Railway Carriage,

原创 2018年04月16日 18:55:31

    题目大致意思,一个由'*'和'.'组成的长度为N的字符串,现在有a个A和b个B去顶替其中的'.',不能有两个A相邻或B相邻的情况,问最后一共可以放几个A和B。

    Example:

input
Copy
5 1 1
*...*
output
Copy
2

    上手开始暴力求解......(这一段没有算法思想而且冗长,觉得没有意义就跳过吧)

#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,best=0;
void solve(int a,int b,int index,int sum,string s)
{
    if(index!=0&&index+1!=n)
    {
        if(s[index]=='.')
        {
            if(!a&&!b) best=max(best,sum);
            if(s[index-1]!='A'&&a)
            {
                s[index]='A';
                //cout<<s<<endl;
                solve(a-1,b,index+1,sum+1,s);
                s[index]='.';
            }
            if(s[index-1]!='B'&&b)
            {
                s[index]='B';
                //cout<<s<<endl;
                solve(a,b-1,index+1,sum+1,s);
                s[index]='.';
            }
        }
        else
            solve(a,b,index+1,sum,s);
    }
    else if(n==1)
    {
        best=1;
    }
    else if(index==0)
    {
        if(s[index]=='.')
        {
            if(a)
            {
                s[index]='A';
                //cout<<s<<endl;
                solve(a-1,b,index+1,sum+1,s);
                s[index]='.';
            }
            if(b)
            {
                s[index]='B';
                //cout<<s<<endl;
                solve(a,b-1,index+1,sum+1,s);
                s[index]='.';
            }
        }
        else
            solve(a,b,index+1,sum,s);
    }
    else if(index==n-1)
    {
        if(s[index]=='.')
        {
            if(a==0&&b==0)
            {
                best=max(sum,best);
            }
            if(s[index-1]!='A'&&a)
                best=max(sum+1,best);
            if(s[index-1]!='B'&&b)
                best=max(sum+1,best);
        }
        else
            best=max(sum,best);
    }
}

int main()
{
    int a,b;
    while(scanf("%d%d%d",&n,&a,&b)!=EOF)
    {
        char s[500];
        scanf("%s",s);
        best=0;
        solve(a,b,0,0,s);
        cout<<best<<endl;
    }
    return 0;
}

结果自然是超时。于是开始更换思想。

后来发现,其实每次去比较a和b是可行的,在一串连续的'*'中能坐下的最多的方式就是ABAB这样轮流坐,如果'*'数量是奇数,则在两端坐a和b中数量多的那个;如果是偶数,就只管往下安排就好了,a和b的差不会变。算是一种贪心策略吧。

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
const int maxn = 2e5+5;
int main()
{
    char seat[maxn];
    int n,a,b,sum;
    while(scanf("%d%d%d",&n,&a,&b)!=EOF)
    {
        getchar();

        sum=a+b;
        int flag;
        for(int i=0;i<n;i++)
        {
            scanf("%c",&seat[i]);
            if(seat[i]=='.'&&(a||b))
            {
                if(i==0||seat[i-1]=='*')        //new start
                {
                    if(a>b)
                    {
                        a--;
                        flag=1;
                    }
                    else
                    {
                        b--;
                        flag=0;
                    }
                }
                else
                {
                    if(flag&&b)
                    {
                        b--;
                        flag=0;
                    }
                    else if(!flag&&a)
                    {
                        a--;
                        flag=1;
                    }
                }
            }
        }
            cout<<sum-a-b<<endl;
    }
    return 0;
}

CodeForces 962B Students in Railway Carriage

          题意是输入n,a,b,表示有n个字符,a个A,b个B,字符串中'.'表示空位,A B不能相连,问最多能放多少个A和B。AC代码:#include &amp;lt;iostream&...
  • Charles_Zaqdt
  • Charles_Zaqdt
  • 2018-04-11 11:06:44
  • 140

codeforces 962B. Students in Railway Carriage

题目链接:http://codeforces.com/problemset/problem/962/BB. Students in Railway Carriagetime limit per tes...
  • qq_38185591
  • qq_38185591
  • 2018-04-11 17:14:29
  • 31

Codeforces Educational Codeforces Round 42 (Rated for Div. 2) B. Students in Railway Carriage 填放人数

点击打开链接#include&amp;lt;iostream&amp;gt; #include&amp;lt;cstdio&amp;gt; #include&amp;lt;cstring&amp;gt...
  • m0_37428263
  • m0_37428263
  • 2018-04-11 12:58:13
  • 63

Educational Codeforces Round 42 (Rated for Div. 2)-B. Students in Railway Carriage

B. Students in Railway Carriagetime limit per test2 secondsmemory limit per test256 megabytesinputst...
  • Eric_chen_song_lin
  • Eric_chen_song_lin
  • 2018-04-12 15:09:43
  • 35

CF811C:Vladik and Memorable Trip(dp)

C. Vladik and Memorable Trip time limit per test 2 seconds memory limit per test 256 meg...
  • junior19
  • junior19
  • 2017-05-31 13:39:29
  • 200

Educational Codeforces Round 42 (Rated for Div. 2) B. Students in Railway Carriage题解

B. Students in Railway Carriage consecutive seat places in a railway carriage. Each place is either ...
  • Hacker_Wind
  • Hacker_Wind
  • 2018-04-11 23:01:43
  • 17

962B Students in Railway Carriage

B. Students in Railway Carriage time limit per test 2 seconds memory limit per test 256 ...
  • memory_qianxiao
  • memory_qianxiao
  • 2018-04-11 09:29:38
  • 5

Students in Railway Carriage

这个题有点坑; 刚开始想的就是先先放人数多的,然后见了*后再接着放人数多的,但一直wa在第十组数据,无语,不过关键在于逻辑思维能力还是不是很强,并且所见题型也不是特别多,导致代码写得非常繁琐...
  • qq_40679299
  • qq_40679299
  • 2018-04-11 20:45:51
  • 3

Educational Codeforces Round 42 (Rated for Div. 2) A. Equator

A. Equatortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputst...
  • Eric_chen_song_lin
  • Eric_chen_song_lin
  • 2018-04-12 14:59:26
  • 53

codeforces贪心 B. Students in Railway Carriage

B. Students in Railway Carriage time limit per test2 seconds memory limit per test256 megabytes i...
  • qq_36386435
  • qq_36386435
  • 2018-04-12 20:20:59
  • 1
收藏助手
不良信息举报
您举报文章:Students in Railway Carriage,
举报原因:
原因补充:

(最多只允许输入30个字)