牛客小白月赛12

https://ac.nowcoder.com/acm/contest/392#question

A 华华听月月唱歌

排序,然后贪心处理。
设一个y
每次 当第一个l <= y+1 的时候 那么这行 r 的这个值,就是可能替换y 的值,所以我们就找满足这个条件的最大的r

直到l大于y+1的时候,将y替换成之间查找的最大值

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
const int maxn=1e5+6;
pair<int ,int > d[maxn];
bool cmp(pair <int ,int >a ,pair<int ,int > b)
{
   if(a.first!=b.first) return a.first<b.first;
   else return a.second>b.second;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int x,y;
        for(int i=0;i<m;i++)
        {
            cin>>x>>y;
            if(x>y) swap(x,y);
            d[i]=mp(x,y);
        }
        sort(d,d+m,cmp);
        if(d[0].first!=1)
        {
            cout<<-1<<endl;
            break;
        }
        x=d[0].first,y=d[0].second;
        int tempx=0,tempy=0,ans=1,p=0;
        d[m].first=1e9;
        for(int i=1;i<=m;i++)
        {
            if(d[i].first<= y+1)
            {
                tempy=max(tempy,d[i].second);
            }
            else if(tempy>y&&d[i-1].first<=y+1)
            {
                y=tempy;
                tempy=d[i].second;
                ans++;
            }
            //cout<<y<<' '<<tempy<<' '<<ans<<endl;
        }
 
       if(y>=n) cout<<ans<<endl;
       else cout<<-1<<endl;
    }
}

B 华华教月月做数学

快速幂加快速乘
所谓快速幂 就是快速的乘
快速乘 就是快速的加

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
unsigned long long mod;
unsigned long long add(unsigned long long x,unsigned long long y)
{
    unsigned long long ans=0;
    while(y)
    {
        if(y%2) ans=(ans+x)%mod;
        y/=2;
        x=(x+x)%mod;
    }
    return ans;
}

unsigned long long pow( unsigned long long  x, unsigned long long  y, unsigned long long mod)
{
   unsigned long long  ans=1;
    while(y)
    {
        if(y%2) ans=add(ans,x);
        y/=2;
        x=add(x,x);
    }
    return ans;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        unsigned long long a,b,p;

        cin>>a>>b>>p;
         mod=p;
        cout<<pow(a,b,p)<<endl;
    }
}


C 华华给月月出题

f(x*y)=f(x)*f(y);
积函数的性质 ;
加上线性筛直接过

#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long pow(long long x,long long y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans* x %mod;
        y>>=1;
        x=x*x%mod;
    }
    return ans;
}

const long long maxn=1e7+3e6+5;
long long num[maxn];

void run(long long n)
{
    vector<long long > prime;
    memset(num,-1,sizeof(num));
    long long ans=1;

    for(long long i=2;i<=n;i++)
    {
        if( num[i]==-1 )
        {
            num[i]=pow(i,n);
            prime.push_back(i);
        }

        for(long long j=0;j<prime.size()&&prime[j]*i<=n;j++)
        {
            num[prime[j]*i] = num[prime[j]] *num[i]%mod;
            if(i%prime[j]==0) break;
        }
        ans^=num[i];
    }
    cout<<ans<<endl;
}




int  main()
{
    long long n;
    while(cin>>n)
    {
        run(n);
    }



}

E 华华给月月准备礼物

二分枚举答案

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,k;
int a[maxn];
int check(int mid)
{
    int ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=a[i]/mid;
    }
    return ans>=k;
}
int main()
{

    while(cin>>n>>k)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }

        int l=1,r=1e9;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if( check(mid) ) l=mid+1;
            else r=mid-1;
        }
        cout<<l-1<<endl;
    }
}

I 华华和月月逛公园

tarjan数桥

#include <bits/stdc++.h>
using namespace std;
vector<int > d[500005];
stack<int > s;
int dfn[500005];
int low[500005];
int vtag[500005];
int b[500005];
int brige[500005];
int times;
int ans=0;
void dfs(int t,int u)
{
    dfn[t]=low[t]=times++;
    s.push(t);
    vtag[t]=1;
    for(int i=0;i<d[t].size();i++)
    {
        if(!dfn[d[t][i]])
        {
            dfs(d[t][i],t);
            low[t]=min(low[t],low[d[t][i]]);
            if(dfn[t]<low[d[t][i]])
            {
                ans+=!brige[d[t][i]];
                brige[d[t][i]]=1;
            }
        }
        if(vtag[d[t][i]]&&d[t][i]!=u)
            low[t]=min(low[t],dfn[d[t][i]]);
 
    }
     if(low[t]==dfn[t])
     {
            while(s.top()!=t)
            {
                //cout<<s.top()<<' ';
                vtag[s.top()]=0;
                b[s.top()]=t;
                s.pop();
            }
            //cout<<s.top()<<endl;
            b[s.top()]=t;
            s.pop();
     }
}
int main()
{
    int n,m,q;
    while(cin>>n>>m)
    {
        memset(b,0,sizeof(b));
        memset(vtag,0,sizeof(vtag));
        while(!s.empty()) s.pop();
        times=1;
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            d[x].push_back(y);
            d[y].push_back(x);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])dfs(i,0);
 
        cout<<m-ans<<endl;
 
    }
}

J 月月查华华的手机

建立26个数组,按照顺序存放每个字母的位置。每个字母使用一个数组
之后使用二分查询数组,只要比上次的二分结果大并且有解,就代表这个字符后面有字符。
比如 abcd字符串
查询acd
a在数组第一个位置
然后查询c数组,c在第三个位置,大于第一个位置 就继续下去。

#include  <bits/stdc++.h>
using namespace std;

int lower(int x,vector<int > &d)
{
    int l=0,r=d.size()-1;
    while( l<=r)
    {
        int mid=(l+r)/2;
        if(d[mid]<x) l=mid+1;
        else r=mid-1;
    }
    return d[l];
}


int main()
{
    string str;
    while(cin>>str)
    {
        vector<int > tag[28];
        for(int i=0;str[i];i++)
        {
            tag[ str[i]-'a'] .push_back(i);
        }
        int ll=str.size();
        for(int i=0;i<26;i++)
            tag[i].push_back(ll);
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            string temp;
            cin>>temp;
            int x=-1;
            for(int j=0;temp[j];j++)
            {
                int y=lower(x,tag[temp[j]-'a']) ;
                // cout<<x<< ' '<<y<<endl;
                if(y<ll)
                    x=y+1;
                else
                {
                    cout<<"No"<<endl;
                    break;
                }
                if(!temp[j+1])
                {
                    cout<<"Yes"<<endl;
                }

            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值