牛客小白月赛57 ABCDE(dfs)

很好(bushi,该有的坑我都踩了,麻

A-最大面积
https://ac.nowcoder.com/acm/contest/40229/A

题目大意:
给定两个矩形的长和宽,让我们求出可重合的最大面积。(题目说了不能改变长宽)
输入 
2 2 3 2
输出 
4

不能改变长宽那就是不可以翻转哇!

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL a,b,c,d;
        cin>>a>>b>>c>>d;
        LL sum1=min(a,c)*min(b,d);
        cout<<sum1<<endl;
    }
    return 0;
}

B-种树
https://ac.nowcoder.com/acm/contest/40229/B

题目大意:
每天选择一个已经种过了树的地方开始往一个方向种,种过的地方可以重复种
问我们种满长度为n的字符串s要多久?(字符串中1表示已经种了树,0表示没有)
输入 
7
0111110
输出 
2

记得特判0

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        string s;
        cin>>s;
        LL sum0=0;
        for(LL i=0;i<s.size();i++)
            if(s[i]=='0') sum0++;
        if(sum0==0) cout<<"0"<<endl;
        else{
        if(s[0]=='1'||s[s.size()-1]=='1') cout<<"1"<<endl;
        else cout<<"2"<<endl;
        }
    }
    return 0;
}

C-奇怪的电梯
https://ac.nowcoder.com/acm/contest/40229/C

确实是够奇怪的

题目大意:
n层的办公楼(最底层是 1层),进入电梯后相邻 k 层的按钮是不能按的。换句话说,假设你在 x 层进入了电梯,那么 x - k到 x + k层之间的所有楼层都去不了。

[x-k,x+k]哒咩

问我们总楼层n,禁止k,从a到b,可不可以?
5
10 3 2 7
10 7 1 4
10 4 2 9
10 11 1 10
9 3 7 2
输出 
YES
NO
YES
NO
YES

**智慧数据 4 1 2 3 **
else if((b-1)>k&&(n-a)>k) cout<<"YES"<<endl;

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n,k,a,b;
        cin>>n>>k>>a>>b;
        if(a>b) swap(a,b);
        if(a==b) cout<<"YES"<<endl;
        else if((b-a)>k) cout<<"YES"<<endl;
        else if((a-1)>k||(n-b)>k) cout<<"YES"<<endl;
        else if((b-1)>k&&(n-a)>k) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

D-最大gcd
https://ac.nowcoder.com/acm/contest/40229/D

题目大意:
给一个长度为n的序列a
求出这个序列中的最大gcd。
输入 
3
1 2 2
输出 
2

注意写法不要超时
还有这题卡map,改为数组比较稳

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2002000,M=2002;
LL x,a[N];
LL mp[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL maxn=0;
        for(LL i=1;i<=n;i++)
        {
            cin>>x;
            mp[x]++;
            maxn=max(maxn,x);
        }
        for(LL i=maxn;i>=0;i--)
        {
            LL sum=0;
            for(LL j=i;j<=maxn;j+=i)
            {
                sum+=mp[j];
                if(sum>=2)
                {
                    cout<<i<<endl;
                    return 0;
                }
            }
        }
    }
    return 0;
}

E-一道难题
https://ac.nowcoder.com/acm/contest/40229/E

给定一个数字n,求出1−n 中 只由 0或 1组成的,且至少有三个 1相连的数(不含前导零)有多少个。
输入
2001
输出
3

用dfs对字符串进行计数
//有一个关键的点在于,一旦出现过3个连续的1之后,这整个字符串都是正确的,别的地方就算少于1也对

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2002000,M=2002;
string s;
LL ans=0;
void dfs(string c,LL len,LL flag)
{
    if(c.size()>s.size()||(c.size()==s.size()&&c>=s))
    {
        return ;
    }
    if(len>=3) flag=1;
    if(flag) ans++;
    dfs(c+"0",0,flag);
    dfs(c+"1",len+1,flag);
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>s;
        dfs("1",1,0);
        //初始化的字符串c
        //最接近末尾里有长度为1的串有多长
        //c中是否含有至少连续3个1
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2022-09-19 21:41  高尔赛凡尔娟  阅读(41)  评论(0编辑  收藏  举报