牛客小白月赛3 【题解完成】

72 阅读7分钟
题目难度知识点
A 音标模拟
B 躲藏★★组合数学,前后缀和
C 博弈★★前缀和,数学
D 妹纸★★找规律
E 幻方★★模拟
F 异或签到
G 旅游★★树形DP
H模拟
I 排名排序
J 零点★★零点

音标

image.png

#include<bits/stdc++.h>
using namespace std;
string ss="aeiouy";
int main(void)
{
    string s; 
    while(cin>>s)
    {
        for(int i=0;i<s.size();i++)
        {
            for(int j=ss.size()-1;j>=0;j--)
            {
                if(ss[j]<=s[i])
                {
                    cout<<ss[j];
                    break;
                }
            }
        }
        puts("");
    }
    return 0;
}

躲藏

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long int LL;
const long long int mod=2000120420010122;
LL cnt1[N],cnt2[N],cnt3[N],n;
//cnt1[N],第一个c的前缀和
//cnt2[N],bc的排列组合
//cnt3[N],c的后缀和
int main(void)
{
    string s;
    while(cin>>s)
    {
        n=s.size();
        s="0"+s;
        for(int i=0;i<=n+5;i++) cnt1[i]=0,cnt2[i]=0,cnt3[i]=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]+32;
        }
        for(int i=1;i<=n;i++)
        {
            cnt1[i]+=cnt1[i-1];
            if(s[i]=='c') cnt1[i]++;
        }
        for(int i=n;i>=1;i--)
        {
            cnt3[i]=cnt3[i+1];
            if(s[i]=='c') cnt3[i]++;
            if(s[i]=='b')
            {
                cnt2[i]+=cnt2[i+1];
                cnt2[i]+=cnt3[i+1];
            }
        }
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='w')
            {
                ans=(ans+(cnt1[i-1]*cnt2[i+1])%mod)%mod;
            }
        }
        cout<<ans%mod<<endl;
    }
    return 0;
}

博弈

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long int LL;
LL f[105][N],l,r,k;//f[i][j] 是以i为k从1-j中的操作次数。
void init()//打表
{
    for(int i=2;i<=100;i++)
    {
        for(int j=1;j<=1e5;j++)
        {
            LL temp=j/i,ans=1,cnt=i;
            while(temp)
            {
                temp/=i;
                ans=ans+cnt;
                cnt=cnt*i;
            }
            f[i][j]=f[i][j-1];
            f[i][j]+=ans;
        }
    }
}
int main(void)
{
    init();
    while(cin>>l>>r>>k)
    {
        if(k==1)
        {
            puts("Draw");
            continue;
        }
        LL temp=f[k][r]-f[k][l-1];
        if(temp&1) puts("XHRlyb");
        else puts("Cwbc");
    }
    return 0;
}

妹纸

image.png

1 20 6 11   这里都是闭区间好记得,实际只要维护对应的边界-1就行了,可以看出是一个长度为r的周期循环。
1 2 3 4 5 0 0 0 0 0 0 1 2 3 4 5 0 0 0 0 
1 30 6 11
1 2 3 4 5 0 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 0 1 2 3 4 5 0 0 0 


1 20 4 7
1 2 3 0 0 0 0 1 2 3 0 0 0 0 1 2 3 0 0 0 
#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL a,b,l,r;
LL sum(LL n)
{
    return n*(n+1)/2;
}
LL calc(LL n,LL l,LL r)
{
    LL temp1=sum(l-1)*(n/r);
    LL temp2=min(sum(n%r),sum(l-1));
    return temp1+temp2;
}
int main(void)
{
    while(cin>>a>>b>>l>>r) 
    {
        cout<<calc(b,l,r-1)-calc(a,l,r-1)<<endl;
    }
    return 0;
}

幻方

image.png

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=202;
char s[10][N][N];
bool st[N][N][N][3];
int n;
int main(void)
{
    while(cin>>n)
    {
        for(int i=0;i<6;i++)
            for(int j=0;j<n;j++)
                for(int z=0;z<n;z++) cin>>s[i][j][z];
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[0][i][j]);
            reverse(ve.begin(),ve.end());
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[i][z][j][0]=1;
        }
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[1][i][j]);
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[i][z][j][0]=1;
        }
        
        
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[2][i][j]);
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[i][j][z][1]=1;
        }
        
        
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[3][i][j]);
            reverse(ve.begin(),ve.end());
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[i][j][z][1]=1;
        }
        
        
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[4][i][j]);
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[z][i][j][2]=1;
        }
        
        
        for(int i=0;i<n;i++)
        {
            vector<char>ve;
            for(int j=0;j<n;j++) ve.push_back(s[5][i][j]);
            for(int j=0;j<ve.size();j++)
                if(ve[j]=='#')
                    for(int z=0;z<n;z++) st[z][n-1-i][j][2]=1;
        }
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int z=0;z<n;z++)
                {
                    if(st[i][j][z][0]&&st[i][j][z][1]&&st[i][j][z][2]) cnt++;
                    st[i][j][z][0]=st[i][j][z][1]=st[i][j][z][2]=0;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

异或

image.png

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL gcd(LL a,LL b)
{return b?gcd(b,a%b):a;}
LL l1,r1,l2,r2;
int main(void)
{
    while(cin>>l1>>r1>>l2>>r2)
    {
        if(l1>l2) swap(l1,l2),swap(r1,r2);
        LL len1=r1-l1+1,len2=r2-l2+1;
        LL temp=0;
        if(r1>=r2) temp=len2;
        if(r2>r1) temp=r1-l2+1;
        if(l2>r1)  puts("0/1");
        else
        {
            LL temp1=gcd(temp,len1*len2);
            cout<<temp/temp1<<"/"<<len1*len2/temp1<<endl;
        }
    }
    return 0;
}

旅游

image.png

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int h[N],e[N],ne[N],idx;
int f[N][2],n,r;
void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int fa)
{
    f[u][1]=1;
    for(int i=h[u];i!=-1;i=ne[i])
    {
        int j=e[i];
        if(j==fa) continue;
        dfs(j,u);
        f[u][1]+=f[j][0];
        f[u][0]+=max(f[j][1],f[j][0]);
    }
}
int main(void)
{
    cin>>n>>r;
    memset(h,-1,sizeof h);
    for(int i=1;i<=n-1;i++)
    {
        int a,b; cin>>a>>b;
        add(a,b),add(b,a);
    }
    dfs(r,-1);
    cout<<f[r][1];
    return 0;
}

纪年

image.png

image.png

60一循环,现将它移动到0,再移动到n模拟即可。
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    int temp=2018%60;
    int cnt1=5,cnt2=11;
    for(int i=1;i<=temp;i++)
    {
        cnt1--,cnt2--;
        if(cnt1==0) cnt1=10;
        if(cnt2==0) cnt2=12;
    }
    int n; 
    while(cin>>n)
    {
        n=n%60;
        int cnt11=cnt1,cnt22=cnt2;
        for(int i=1;i<=n;i++)
        {
            cnt11++,cnt22++;
            if(cnt11==11) cnt11=1;
            if(cnt22==13) cnt22=1;
        }
        cout<<cnt11<<" "<<cnt22<<endl;
    }
    return 0;
}

排名

image.png

#include<bits/stdc++.h>
using namespace std;
const int N=1e5*2+10;
struct node
{
    string name;
    double x1,x2,x3,x4,x5,sum;
}Node[N];
int n;
bool cmp(struct node a,struct node b)
{
    if(a.sum==b.sum) return a.name<b.name;
    return a.sum>b.sum;
}
int main(void)
{
    while(cin>>n)
    {
        double max1,max2,max3,max4,max5;
        max1=max2=max3=max4=max5=0;
        for(int i=0;i<n;i++)
        {
            string name;
            double x1,x2,x3,x4,x5; cin>>name>>x1>>x2>>x3>>x4>>x5;
            Node[i]={name,x1,x2,x3,x4,x5,0};
            max1=max(max1,x1);
            max2=max(max2,x2);
            max3=max(max3,x3);
            max4=max(max4,x4);
            max5=max(max5,x5);
        }
        for(int i=0;i<n;i++)
        {
            double temp1=Node[i].x1*600/max1*0.25;
            double temp2=(Node[i].x2*300/max2+Node[i].x3*300/max3)*0.25;
            double temp3=(Node[i].x4*300/max4+Node[i].x5*300/max5)*0.5;
            Node[i].sum=temp1+temp2+temp3;
        }
        sort(Node,Node+n,cmp);
        for(int i=0;i<n;i++)
        {
            cout<<Node[i].name<<" ";
            printf("%.5lf\n",Node[i].sum);
        }
    }
    return 0;
}

零点

image.png

image.png

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
set<LL>st;
LL flag=0;
void f1(LL x1,LL y1,LL x2,LL y2)
{
    if(y1==y2)
    {
        if(y1==0) flag=1;
    }else
    {
        if(y1==0) st.insert(x1);
        if(y2==0) st.insert(x2);
        if( (y1<y2&&y2>0) || (y1>y2&&y2<0) ) 
        {
            LL temp1=y1*(x2-x1);
            LL temp2=y1-y2;
            if(temp1%temp2==0) st.insert(temp1/temp2+x1);
        }
    }
}
void f2(LL x1,LL y1,LL x2,LL y2)
{
     if(y1==y2)
    {
        for(int i=x1;i<=x2;i++) st.insert(i);
    }else
    {
        if(y1==0) st.insert(x1);
        if(y2==0) st.insert(x2);
        if(y1*y2<0) 
        {
            LL temp1=y1*(x2-x1);
            LL temp2=y1-y2;
            if(temp1%temp2==0) st.insert(temp1/temp2+x1);
        }
    }
}
void f3(LL x1,LL y1,LL x2,LL y2)
{
    if(y1==y2)
    {
        if(y1==0) flag=1;
    }else
    {
        if(y1==0) st.insert(x1);
        if(y2==0) st.insert(x2);
        if( (y1<y2&&y1<0) || (y1>y2&&y1>0) ) 
        {
            LL temp1=y1*(x2-x1);
            LL temp2=y1-y2;
            if(temp1%temp2==0) st.insert(temp1/temp2+x1);
        }
    }
}
int main(void)
{
    LL n,x1,y1,x2,y2; 
    cin>>n>>x1>>y1;
    for(int i=2;i<=n;i++)
    {
        cin>>x2>>y2;
        if(i==2) f1(x1,y1,x2,y2);
        if(i>=3&&i<=n-1) f2(x1,y1,x2,y2);
        if(i==n) f3(x1,y1,x2,y2);
        x1=x2,y1=y2;
    }
    if(flag || st.size()>300000) puts("-1");
    else
    {
        cout<<st.size()<<endl;
        for(auto i=st.begin();i!=st.end();i++) cout<<*i<<" ";
    }
    return 0;
}