• 7.10测试


    第一题
    众数

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int maxx;
    int main()
    {
        int n,s[30005],t;
        memset(s,0,sizeof(s));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&t);
            s[t]++;
        }
        for(int i=1;i<=30004;i++)
            if(maxx<s[i]) maxx=s[i];
        for(int i=1;i<=30004;i++)
            if(s[i]==maxx) printf("%d  %d
    ",i,s[i]);
        return 0;
    } 
    

    桶排解决,但是因为格式没拿到分,输出格式一定要看清楚了。

    第二题 楼

    一道数学题(感觉)

    考试的时候一是没开ll而是方法不是最简只拿了50分

    50分代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxx=2001;
    int u[maxx],d[maxx],s[maxx];
    int main()
    {
        //freopen("build.in","r",stdin);
        //freopen("build.out","w",stdout);
        int n,m,t,i;
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++)
            scanf("%d%d",&u[i],&d[i]);
        for(i=1;i<=m;i++)
        {
            s[i]=u[i]*n;
            do
            {
                s[i]-=u[i]; s[i]-=d[i];	
            }
            while(s[i]>=0);
            s[i]+=u[i];s[i]+=d[i];
        }
        sort(s+1,s+m+1);
        printf("%d",s[1]);
        return 0;
    } 
    

    100分代码:

    #include <iostream>
    using namespace std;
    typedef long long ll;
    ll n,m,ans=1e15;
    //方程组{(u+d)x-dn>=0,x<n}的最小整数解,其中x是按向上键的次数 
    int main() {
        cin >> n >> m;
        for(ll i=1;i<=m;i++) 
        {
            ll u,d;
            cin >> u >> d;
            ll x=d*n/(u+d)+1; 
            ll fx=(u+d)*x-d*n;
            ans=min(fx,ans);
        }
        cout << ans << endl;
        return 0;
    }
    

    第三题
    面积

    一道比较基础的bfs题目。

    #include <cstdio>
    #include <queue>
    #include <iostream>
    using namespace std;
    int n=10,sum=100,tcl[11][11];
    int dx[5]={0,1,0,-1,0};
    int dy[5]={0,0,1,0,-1};
    int bfs(int x,int y)
    {
        queue<int> tclx;
        queue<int> tcly;
        tclx.push(x); tcly.push(y);
        sum--;
        tcl[x][y]=1;
        do
        {
            int xx=tclx.front(),yy=tcly.front();
            tclx.pop(),tcly.pop();
            for (int i=1;i<=4;i++)
                if (dx[i]+xx>=1&&dx[i]+xx<=n&&dy[i]+yy>=1&&dy[i]+yy<=n&&tcl[dx[i]+xx][dy[i]+yy]==0)
                {
                    tcl[dx[i]+xx][dy[i]+yy]=1;
                    sum--;
                    tclx.push(dx[i]+xx);
                    tcly.push(dy[i]+yy);
                }
        }while (!tclx.empty());
    }
    int main()
    {
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
            {
                scanf("%d",&tcl[i][j]);
                if (tcl[i][j]==1) sum--;
            }
        if (tcl[1][1]==0)
            {
                bfs(1,1);
            }
        if (tcl[1][n]==0)
            {
            	bfs(1,n);
            }
        if (tcl[n][1]==0)
            {
            	bfs(n,1);
            }
        if (tcl[n][n]==0)
            {
            	bfs(n,n);
            }
        printf("%d
    ",sum);
    }
    

    第四题

    没想到就直接骗了分233,其实暴力也是可以得50分的。
    裸DFS暴力:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 1010
    #define ll long long
    using namespace std;
    ll n,m,a[maxn],b[maxn],c[maxn][maxn],ans=1e15;
    ll init(){
        ll x=0,f=1;char s=getchar();
        while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
        while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
        return x*f;
    }
    ll Abs(ll x){
        return x<0?-x:x;
    }
    ll Cal(){
        ll r=0,L,R,p;
        for(int i=1;i<=n;i++){
            if(c[i][0]==0)continue;
            L=b[c[i][1]];R=b[c[i][c[i][0]]];
            p=a[i];r=max(r,min(Abs(R-p),Abs(p-L))+R-L);
        }
        return r;
    }
    void Dfs(int now){
        if(now==m+1){
            ans=min(ans,Cal());
            return;
        }
        for(int i=1;i<=n;i++){
            c[i][++c[i][0]]=now;
            Dfs(now+1);c[i][0]--;
        }
    }
    int main()
    {
        freopen("read.in","r",stdin);
        freopen("read.out","w",stdout);
        n=init();m=init();
        for(int i=1;i<=n;i++)a[i]=init();
        for(int i=1;i<=m;i++)b[i]=init();
        Dfs(1);cout<<ans<<endl;
        return 0;
    }
    

    如果想得全分本题还要加上类似二分的优化

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define M 100009
    #define LL long long
    using namespace std;
    LL a[M],b[M],ans;
    int n,m;
    bool check(LL x)
    {
        int i=1,j;
        for(int j=1;j<=n;j++)
        {
            LL t=0,pos=a[j],k=i;
            LL tmp=0;
            if(b[i]<a[j])
            {
                if(a[j]-b[i]>x) return 0;
                tmp=max((x-(a[j]-b[i]))/2+a[j],x-(a[j]-b[i])+b[i]);
            }
            else tmp=a[j]+x;
            i=upper_bound(b+k,b+m+1,tmp)-b;
            if(i>m) return 1;
        }
        if(i>m) return 1;
        return 0;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]); 
        for(int i=1;i<=m;i++) scanf("%lld",&b[i]);
    
        LL L=0,R=1e15,mid;
        while(L<=R)
        {
            mid=(L+R)>>1;
            if(check(mid)) R=mid-1;
            else L=mid+1;
        } 
        printf("%lld",L);
        return 0;
    }
    

    以后像第一题还是要看仔细,而且像忘开ll这种问题就不要再出现了....

  • 相关阅读:
    Ubuntu编译gdb-ARM调试环境
    12小时制字符串转24小时制字符串
    Qt QByteArray或者Char转十六进制 QString
    STM32 串口通信使用奇偶校验
    127.*.*.* 为本地回环地址,均返回127.0.0.1
    winform解析json
    qString转char*
    下载vc++运行库
    CentOS 7 通过 yum 安装 nodejs 和 npm
    go语言 工程目录
  • 原文地址:https://www.cnblogs.com/LSWorld/p/710test.html
Copyright © 2020-2023  润新知