2020牛客寒假算法基础集训营5【无C】

题目来源:https://ac.nowcoder.com/acm/contest/3006#question
正常感受到了被模拟支配的恐惧,然后A了8题就跑路了,其实是不会写 名次是这五场以来最靠前的一次,奥利给!


A - 模板

这题差点让我心态崩了,我一开始题目看错了~~(我以为删除哪里都可以,结果只能删除末尾)~~ ,其实蛮简单的,多出来的加,等长的不一样就改

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
int f[N];
char s1[N],s2[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
LL qpow(LL x,LL p)
{
    LL res=1;
    while(p){
        if(p&1) res=res*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return res;
}
LL inv(LL x)
{
    return qpow(x,mod-2);
}
int main()
{
    r(n); r(m);
    scanf("%s",s1+1);
    scanf("%s",s2+1);
    int ans=0;
    FOR(i,1,min(n,m)){
        if(s1[i]!=s2[i]) ans++;
    }
    ans+=max(n,m)-min(n,m);
    cout<<ans<<endl;
    return 0;
}


B - 牛牛战队的比赛地

这题我想的比较复杂,就是先定一个左右的分界mm
然后左边的最大距离 与 右边最大距离 的比较分类讨论,特殊的是这个分界点的距离还大些的话 那就是这个分界点

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
int f[N];
int x[N],y[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
int main()
{
    r(n);
    int lb=100000,rb=-1000000;
    FOR(i,1,n){
        r(x[i]); r(y[i]);
        lb=min(x[i],lb);
        rb=max(x[i],rb);
    }
    int mm=(lb+rb)/2;
    double maxl=0,maxr=0,maxx=0;
    FOR(i,1,n){
        double dd=(x[i]-mm)*(x[i]-mm)+y[i]*y[i];
        if(x[i]<mm){
            if(maxl<dd) maxl=dd;
        }
        else if(x[i]>mm){
            if(maxr<dd) maxr=dd;
        }
        if(maxx<dd) maxx=dd;
    }
    if(maxl==maxr||(maxl<maxx&&maxr<maxx)){
        printf("%.7f\n",sqrt(maxx));
        return 0;
    }
    if(maxl>maxr){
        double l=-10000,r=mm;
        while(r-l>=eps){
            double mid=(l+r)/2;
            double ll=-1,rr=-1;
            FOR(i,1,n){
                double dd=(x[i]-mid)*(x[i]-mid)+y[i]*y[i];
                if(x[i]<mm){
                    if(dd>ll) ll=dd;
                }
                else{
                    if(dd>rr) rr=dd;
                }
            }
            //cout<<mid<<' '<<ll<<' '<<rr<<endl;
            if(ll>=rr){
                r=mid-eps;
            }
            else{
                l=mid+eps;
            }
        }
        double ans=-1;
        FOR(i,1,n){
            double dd=(x[i]-l)*(x[i]-l)+y[i]*y[i];
            if(ans<dd) ans=dd;
        }
        printf("%.7f\n",sqrt(ans));
    }
    else{
        double l=mm,r=10000;
        while(r-l>=eps){
            double mid=(l+r)/2;
            double ll=-1,rr=-1;
            FOR(i,1,n){
                double dd=(x[i]-mid)*(x[i]-mid)+y[i]*y[i];
                if(x[i]<=mm){
                    if(dd>ll) ll=dd;
                }
                else{
                    if(dd>rr) rr=dd;
                }
            }
            //cout<<mid<<' '<<ll<<' '<<rr<<endl;
            if(ll<=rr){
                l=mid+eps;
            }
            else{
                r=mid-eps;
            }
        }
        double ans=-1;
        FOR(i,1,n){
            double dd=(x[i]-l)*(x[i]-l)+y[i]*y[i];
            if(ans<dd) ans=dd;
        }
        printf("%.7f\n",sqrt(ans));
    }
    return 0;
}


C - C语言IDE


D - 牛牛与牛妹的约会

分了7种情况,最后一个else没写 白WA5发…

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-10;
const double pi=acos(-1);
double n,m;
int f[N];
int x[N],y[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
double ppow(double a){
    double l=1,r=a;
	while(r-l>=eps){
        double mid=(r+l)/2;
        if(mid*mid*mid>a){
            r=mid-eps;
        }
        else l=mid+eps;
	}
	return l;
}
int main()
{
    int t;
    r(t);
    while(t--){
        scanf("%lf%lf",&n,&m);
        double ans=0;
        if(n>m){
            if(n*m<0){//n>0 m<0
                double next=ppow(n);
                while(n-next>1){
                    //cout<<next<<endl;
                    ans+=1;
                    n=next;
                    next=ppow(n);
                }
                ans+=(n-m);
                printf("%.9f\n",ans);
            }
            else if(n<=0){//m<n<0
                ans=n-m;
                printf("%.9f\n",ans);
            }
            else if(n>0){
                double next=ppow(n);
                bool flag=1;
                while(n-next>1){
                    if(next>=m){
                       ans++;
                       n=next;
                       next=ppow(n);
                    }
                    else if(m-next+1<n-m){
                        ans++;
                        n=next;
                        next=ppow(n);
                        flag=0;
                        break;
                    }
                    else break;
                }
                if(flag) ans+=(n-m);
                else ans+=(m-n);
                printf("%.9f\n",ans);
            }
        }
        else if(n<m){
            if(n*m<0){//n<0 m>0
                double next=-ppow(-n);
                while(next-n>1){
                    ans++;
                    n=next;
                    next=-ppow(-n);
                }
                ans+=(m-n);
                printf("%.9f\n",ans);
            }
            else if(n>=0){//n>0 m>0
                ans=m-n;
                printf("%.9f\n",ans);
            }
            else if(n<0){//n<0 m<0
                double next=-ppow(-n);
                bool flag=1;
                while(next-n>1){
                    if(next<=m){
                       ans+=1;
                       n=next;
                       next=-ppow(-n);
                    }
                    else if(next-m+1<m-n){
                        ans+=1;
                        n=next;
                        next=-ppow(-n);
                        flag=0;
                        break;
                    }
                    else break;
                }
                //cout<<n<<' '<<m<<' '<<next<<endl;
                if(!flag) ans+=(n-m);
                else ans+=(m-n);
                printf("%.9f\n",ans);
            }
        }
        else{
            ans=0;
            printf("%.9f\n",ans);
        }
    }
    return 0;
}


E - Enjoy the game

找规律?反正我是找规律233

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
int f[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
LL qpow(LL x,LL p)
{
    LL res=1;
    while(p){
        if(p&1) res=res*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return res;
}
LL inv(LL x)
{
    return qpow(x,mod-2);
}
int main()
{
    r(n);
    LL now=1;
    FOR(i,1,62){
        now<<=1;
        if(now==n){
            cout<<"Alice\n";
            return 0;
        }
        else if(now>n) break;
    }
    cout<<"Bob\n";
    return 0;
}


F - 碎碎念

还是找规律…太菜了只会找规律

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=1000000007;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
LL dp[N];
LL sum[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
int main()
{
    r(n); r(m);
    FOR(i,0,n-1) dp[i]=1;
    dp[n]=2;
    FOR(i,n+1,200010) dp[i]=(dp[i-1]+dp[i-n-1])%mod;
    FOR(i,1,200010) sum[i]=(sum[i-1]+dp[i])%mod;
    while(m--){
        int a,b;
        r(a); r(b);
        cout<<(sum[b]-sum[a-1]+mod)%mod<<endl;
    }
    return 0;
}


G - 街机争霸

原来这题就是个bfs,我一开始死都想不到怎么处理僵尸移动的问题,没想到加一维时间维就好了
僵尸们的行走距离都是k,他们的周期就已知了。我们开一个数组记录所有僵尸整个周期所到达的点就可以判断人在某时刻会不会走到僵尸即将走到的位置 即代码中的!zom[xx][yy][tt%(2*k-2)] 2*k-2即为周期 其他的和走迷宫差不多…

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=1e9+7;
const double eps=1e-10;
const double pi=acos(-1);
int n,m,p,k;
bool zom[505][505][22];
bool vis[505][505][22];
char str[505][505];
int D[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int tox,toy,fromx,fromy;
struct node
{
    int x,y,t;
    node(){};
    node(int xx,int yy,int tt){
        x=xx;
        y=yy;
        t=tt;
    }
};
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
int bfs(int fx,int fy,int ft)
{
    node ff(fx,fy,ft);
    vis[fx][fy][ft]=1;
    queue<node> q;
    q.push(ff);
    while(q.size()){
        node now=q.front();
        q.pop();
        if(now.x==tox&&now.y==toy){
            return now.t;
        }
        //cout<<now.x<<' '<<now.y<<' '<<now.t%(2*k-2)<<endl;
        FOR(i,0,3){
            int xx=now.x+D[i][0];
            int yy=now.y+D[i][1];
            int tt=now.t+1;
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&str[xx][yy]!='&'&&!zom[xx][yy][tt%(2*k-2)]&&!vis[xx][yy][tt%(2*k-2)]){
                vis[xx][yy][tt%(2*k-2)]=1;
                node next(xx,yy,tt);
                q.push(node(xx,yy,tt));
            }
        }
    }
    return -1;
}
int main()
{
    rrr(n,m,p); r(k);
    FOR(i,1,n){
        scanf("%s",str[i]+1);
    }
    FOR(i,1,n){
        FOR(j,1,m){
            if(str[i][j]=='A'){
                tox=i; toy=j;
            }
            else if(str[i][j]=='L'){
                fromx=i; fromy=j;
            }
        }
    }
    while(p--){
        int xx,yy,dd;
        char dir[10];
        scanf("%d%d %s",&xx,&yy,dir);
        if(dir[0]=='U') dd=0;
        else if(dir[0]=='D') dd=1;
        else if(dir[0]=='L') dd=2;
        else if(dir[0]=='R') dd=3;
        zom[xx][yy][0]=1;
        FOR(i,1,k-1){
            xx+=D[dd][0];
            yy+=D[dd][1];
            zom[xx][yy][i]=1;
        }
        if(dd&1) dd--;
        else dd++;
        FOR(i,1,k-2){
            xx+=D[dd][0];
            yy+=D[dd][1];
            zom[xx][yy][i+k-1]=1;
        }
    }
    int ans=bfs(fromx,fromy,0);
    if(ans==-1) cout<<"Oh no\n";
    else cout<<ans<<endl;
    return 0;
}


H - Hash

最小的满足的 就是当前的+mod 如果加爆了 输出-1 (爆了是指超过了zzzzzz的哈希值)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
char str[M];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
int main()
{
    while(~scanf("%s",str+1)){
 
     r(m);
    LL now=0;
    LL tmp=1;
    LL maxx=0;
    for(int i=6;i>=1;i--){
        now+=tmp*(str[i]-'a');
        maxx+=tmp*25;
        tmp*=26;
    }
    now+=m;
    //cout<<now<<endl;
    if(now>maxx) cout<<-1<<endl;
    else{
        stack<char> s;
        while(now){
            //cout<<now<<endl;
            s.push(now%26+'a');
            now/=26;
        }
        if(s.size()==5) s.push('a');
        while(s.size()){
            cout<<s.top();
            s.pop();
        }
        cout<<endl;
    }
    }
    return 0;
}

I - I题是个签到题

真*签到题

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
int f[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
LL qpow(LL x,LL p)
{
    LL res=1;
    while(p){
        if(p&1) res=res*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return res;
}
LL inv(LL x)
{
    return qpow(x,mod-2);
}
int main()
{
    r(n); r(m);
    FOR(i,1,n) r(f[i]);
    int cnt=0;
    FOR(i,1,n){
        if(i!=9&&f[i]>f[9]) cnt++;
    }
    if(f[9]*1.0>=m*0.8||cnt<=2) cout<<"Yes\n";
    else cout<<"No\n";
    return 0;
}


J - 牛牛战队的秀场

简单的数学,注意精度

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
#define ls k<<1,l,mid
#define rs k<<1|1,mid+1,r
#define mp(x,y) make_pair(x,y)
#define r(x) read(x)
#define rrr(x,y,z) read(x);read(y);read(z)
#define FOR(i,l,r) for(int i=l;i<=r;i++)
using namespace std;
typedef long long LL;
typedef pair<int,int> pt;
const int N=1e6+5;
const int M=2e3+5;
const int INF=0x7fffffff;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1);
LL n,m;
int f[N];
template<class T>
inline void read(T &x)
{
    char c; x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    T res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    x*=res;
}
LL qpow(LL x,LL p)
{
    LL res=1;
    while(p){
        if(p&1) res=res*x%mod;
        x=x*x%mod;
        p>>=1;
    }
    return res;
}
LL inv(LL x)
{
    return qpow(x,mod-2);
}
int main()
{
    int a,b;
    r(n); rrr(m,a,b);
    double oo=pi*1.0/n;
    double l=2*m*sin(oo);
    double dis=abs(a-b);
    if(dis>n/2) dis=n-dis;
    printf("%.6f\n",dis*l);
    return 0;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值