蒟蒻的第24次CCF计算机软件能力认证

100+70+40+25+0,没有技巧全是感情www!

202112-1 序列查询

这题一开始没有照题目的暗示,拿到50,后来根据题目的优化方法优化就可以了

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <limits.h>
using namespace std;
const int maxn = 210;
int A[maxn];
int main()
{
    
    
    int n, N;
    int t = 0;
    long long ans = 0;
    int count = -1;
    scanf("%d %d",&n,&N);
    int i,j;
    j = 0;
    A[0] = 0;
    for (i = 1; i <= n; i++) {
    
    
        scanf("%d",&A[i]);
        
    }
    for (i = 0; i <= N-1; i++)
    {
    
    
        for (; j <= n; j++)
        {
    
    
            if (A[j] > i)
                break;
        }
        ans += j - 1;
    }
    printf("%lld\n",ans);
    return 0;
}




202112-2 序列查询新解

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

#define M 500000+2
#define LL long long
inline LL read(){
    
    
    LL x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
    
    
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
LL n,m,nn;
LL a[M];
typedef struct NODE{
    
    
    LL s,e;
    LL num;
    NODE(){
    
    };
    NODE(LL ss,LL ee,LL numm):s(ss),e(ee),num(numm){
    
    };
}node;
node g[M],f[M];
int main(){
    
    
    scanf("%lld%lld",&n,&nn);
    LL gi=0,fi=0;
    a[1]=read();
    f[fi++]=NODE(0,a[1]-1,0);
    for (register LL i=2;i<=n;i++){
    
    
        a[i]=read();
        f[fi++]=NODE(a[i-1],a[i]-1,i-1);
    }
    f[fi]=NODE(a[n],nn-1,n);
    LL r=floor(nn/(n+1));
    LL j=1,tmp;
    for (LL i=r,k=1;i<=nn-1;i+=r,k++){
    
    
        g[gi++]=NODE(i-r,i-1,k-1);
        j=i;tmp=k;
    }
    g[gi]=NODE(j,nn-1,tmp);
    LL ans=0,i=0,last=0;
    j=0;
    while (i<=fi&&j<=gi){
    
    
        LL right=min(f[i].e,g[j].e);
    //    cout<<right<<' ';
        ans+=abs(f[i].num-g[j].num)*(right-last);
        last=right;
    //    cout<<last<<endl;
        if (right==f[i].e) i++;
        if (right==g[j].e) j++;
    }
    printf("%lld\n",ans);
    return 0;
}

202112-3 登机牌条码

只考虑了最简单的-1的情况,用vector存放数字。

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <map>
#include <string>
#include<cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <limits.h>
using namespace std;
const int maxn = 1010;
vector<int> mazi;
vector<int> ans;
char a[maxn];
int main()
{
    
    
    int w, s;
    scanf("%d %d", &w, &s);
    scanf("%s", a);
    int i, j;
    int len = strlen(a);
    int flag;//flag表示当前的模式,flag=0表示大写,=1表示小写,=2表示数字
    flag = 0;

    for (i = 0; i < len; i++)
    {
    
    
        if (a[i] >= 'A' && a[i] <= 'Z')
        {
    
    
            if (flag == 1)//表示之前是小写模式
            {
    
    
                mazi.push_back(28);
                mazi.push_back(28);
                flag = 0;
            }
            else if (flag == 2)//表示之前是数字模式
            {
    
    
                mazi.push_back(28);
                flag = 0;
            }
            mazi.push_back(a[i] - 'A');
        }

        else if (a[i] >= 'a' && a[i] <= 'z')
        {
    
    
            if (flag == 0)//大写模式
            {
    
    
                mazi.push_back(27);
                flag = 1;
            }
            else if (flag = 2)//数字模式
            {
    
    
                mazi.push_back(28);
                flag = 1;
            }
            mazi.push_back(a[i] - 'a');
        }
        else if (a[i] >= '0' && a[i] <= '9')
        {
    
    
            if (flag == 0)
            {
    
    
                mazi.push_back(28);
                flag = 2;
            }
            else if (flag == 1)//小写
            {
    
    
                mazi.push_back(28);
                flag = 2;
            }
            mazi.push_back(a[i] - '0');

        }

    }
    if (mazi.size() % 2 == 1)//表示是偶数
    {
    
    
        mazi.push_back(29);
    }
    ans.push_back(0);
    for (i = 0; i < mazi.size(); i = i + 2)
    {
    
    
        ans.push_back(mazi[i] * 30 + mazi[i + 1]);
    }
    if (ans.size() % w == 0)
    {
    
    
        ans[0] = ans.size();
    }
    else
    {
    
    
        int temp = w - ans.size() % w;
        for (j = 1; j <= temp; j++)
        {
    
    
            ans.push_back(900);
        }

        ans[0] = ans.size();
    }


    for (i = 0; i < ans.size(); i++)
    {
    
    
        printf("%d\n", ans[i]);
    }
    return 0;
}

202112-4 磁盘文件操作

本来想用结构体,但是结构体的内存超限了,所以用了map,都用位置sta(tion)作为索引即可。

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <limits.h>
using namespace std;
map<int, int> sta2id;//sta位置的id号
map<int, int> sta2num;//sta位置的数字
map<int, int> sta2preid;//sta位置的前id号
int main()
{
    
    
    int n, m, k;
    scanf("%d %d %d",&n,&m,&k);//读取n,m,k
    int i, j;//循环变量
    int caozuo;//操作
    int id,l, r, x,p;
    int flag;
    //首先初始化
    for (i = 1; i <= m; i++)
    {
    
    
        sta2id[i] = 0;//位置对应的id号为0
    }
    for (i = 0; i < k; i++)
    {
    
    
        scanf("%d",&caozuo);
        if (caozuo == 0)
        {
    
    
            scanf("%d %d %d %d",&id,&l,&r,&x);
            for (j = l; j <= r; j++)
            {
    
    
                if (sta2id[j] != id && sta2id[j] != 0)
                    break;
                else
                {
    
    
                    sta2id[j] = id;
                    sta2num[j] = x;
                }                   
            }
            if (j == l)
                printf("-1\n");
            else
                printf("%d\n",j-1);
        }
        else if (caozuo == 1)//删除
        {
    
    
            scanf("%d %d %d", &id, &l, &r);
            flag = 0;
            for (j = l; j <= r; j++)
            {
    
    
                if (sta2id[j] != id)
                {
    
    
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
            {
    
    
                printf("FAIL\n");
            }
            else
            {
    
    
                for (j = l; j <= r; j++)
                {
    
    
                    sta2id[j] = 0;//占有的id变成0;
                    sta2preid[j] = id;//preid变为id
                }
                printf("OK\n");
            }

        }
        else if (caozuo == 2)
        {
    
    
            scanf("%d %d %d", &id, &l, &r);
            flag = 0;
            for (j = l; j <= r; j++)
            {
    
    
                if (sta2id[j] != 0 || sta2preid[j] != id)
                {
    
    
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
            {
    
    
                printf("FAIL\n");
            }                
            else
            {
    
    
                for (j = l; j <= r; j++)
                {
    
    
                    sta2id[j] = id;
                }
                printf("OK\n");
            }
        }
        else if (caozuo == 3)
        {
    
    
            scanf("%d",&p);
            if (sta2id[p] == 0)
            {
    
    
                printf("0 0\n");
            }
            else
            {
    
    
                printf("%d %d\n",sta2id[p],sta2num[p]);
            }
        }
    }
    return 0;
}




202112-5 极差路径

猜你喜欢

转载自blog.csdn.net/susuate/article/details/121982199