牛客练习赛79

题目链接

A.炼金术师

思路:

单调栈,
题意求布有多少颜色.如果输入3 3 1 2 ,答案是2 因为第3次2会把1覆盖掉;所以每次输入1个数就看前面有没有比他小的数,有就去掉,显然是个单调栈问题.

代码

STL写法
#include<bits/stdc++.h>
using namespace std;
stack<int>stk;
int main()
{
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        while(stk.size()&&stk.top()<=x)stk.pop();//注意:如果输入是4 3 1 2 2  答案还是2  所以stk.top<=x
        stk.push(x);
    }
    cout<<stk.size();
    return 0;
}
数组模拟单调栈写法
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int stk[N],tt;
int main()
{
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        while(tt&&stk[tt]<=x)tt--;
        stk[++tt]=x;
    }
    cout<<tt;
    return 0;
}

B.刀工对决

思路

…太菜了没有思路 先进行*3/5的操作,然后就剩下3的因子了

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int  T;
    cin>>T;
    int real_ans=0;
    while(T--)
    {
        int a,b;
        cin>>a>>b;
        if(a==b)continue;
        int ans1=0,ans2=0;
        while(a%5==0)
        {
            ans1++;
            a=(a/5)*3;
        }
        while(b%5==0)
        {
            ans2++;
            b=(b/5)*3;
        }
        if(a<b)swap(a,b);
        int ans=abs(ans1-ans2);
        int cnt=0;
        while(a%3==0&&a!=b)
        {
            a/=3;
            cnt++;
        }
        if(a!=b)
        {
            cout<<-1<<endl;
            return 0;
        }
        if(a==b)real_ans+=ans+cnt;
    }
    cout<<real_ans<<endl;
    return 0;
}

C.小G的GCD

思路

根据题目给的代码打表就可以发现规律;

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    int n;
    cin>>n;
    int a=0,b=1;
    int c=a+b;
    
    int ans=2;
    while(c<=n)
    {
        a=b;
        b=c;
        c=a+b;
        ans++;
    }
    if(n==1)cout<<2<<endl;
    else 
    cout<<ans-1<<endl;
}

D.回文字D

思路

字符串哈希:先正序求哈希值再逆序求哈希值,然后每次循环判断len为D的字符串是否回文,如果回文,则len++,判断[右端点-len+1,右端点]长度为len是否回文.不回文就是Si.

代码

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int P=131,N=1e7+10;
ull h[N],p[N],rh[N];
int n,D;
char s[N];
bool check(int l,int r)
{
//     if(r-l+1<D)return true;
    return h[r]-h[l-1]*p[r-l+1]==rh[l]-rh[r+1]*p[r-l+1];
}
int main()
{
    cin>>n>>D;
    cin>>s+1;
    p[0]=1;
    for(int i=1;i<=n;i++)
    {
        p[i]=P*p[i-1];
        h[i]=P*h[i-1]+s[i];
    }
    for(int i=n;i>=1;i--)
    {
        rh[i]=rh[i+1]*P+s[i];
    }
    int l=1;
    int ans=0;
    while(l<=n)
    {
        int r=l+D-1;
        while(r<=n&&check(r-D+1,r))r++;
        l=r;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值