ccf-csp 2019秋季真题题解


  1. 小明种苹果
    ccf-csp 2019秋季真题题解_第1张图片
    ccf-csp 2019秋季真题题解_第2张图片

代码:

#include 

using namespace std;

const int MAXN = 1010;
int arr[MAXN];

int main(){
    int n, m;
    int T = 0, k = -1, P = 0;

    cin >> n >> m;
    for(int i = 1; i <= n; i ++){
        int tmp = 0, a, b;
        cin >> b;
        for(int j = 0; j < m; j ++){
            cin >> a;
            //记录操作的数量
            tmp -= a;
        }
        //更新答案
        if(tmp > P)
            P = tmp, k = i;
        T += b - tmp;
    }
    cout << T << " " << k << " " << P << endl;
    return 0;
}

  1. 小明种苹果(续)
    ccf-csp 2019秋季真题题解_第3张图片
    ccf-csp 2019秋季真题题解_第4张图片
    ccf-csp 2019秋季真题题解_第5张图片

代码:

#include 

using namespace std;

const int MAXN = 1010;
bool isD[MAXN];

int main(){
    int n, T = 0, D = 0, E = 0;

    cin >> n;
    for(int i = 0; i < n; i ++){
        int m, num;
        cin >> m;
        m -= 1;
        //记录初始苹果数量
        cin >> num;

        while(m --){
            int a;
            cin >> a;
            if(a > 0){
            	//不等说明有掉落的情况
                if(a != num)
                    isD[i] = true;
                num = a;
            }else	//否则减去疏去的果子数量
                num += a;
        }
        T += num;
    }

    for(int i = 0; i < n; i ++){
    	//计算掉落果树的数量
        if(isD[i])
            ++ D;
        //计算三次果树连续掉落的数量
        if(isD[i] && isD[(i + 1) % n] && isD[(i + 2) % n])
            ++ E;
    }

    cout << T << " " << D << " " << E << endl;
    return 0;
}

  1. 字符画
    ccf-csp 2019秋季真题题解_第6张图片
    ccf-csp 2019秋季真题题解_第7张图片
    ccf-csp 2019秋季真题题解_第8张图片
    ccf-csp 2019秋季真题题解_第9张图片

代码:

#include 
#include 
#include 

using namespace std;

typedef unsigned char UC;
const int N = 1080, M = 1920;

int m, n, p, q;
UC g[N][M][3];

inline int get(char c)
{
    if (c <= '9') return c - '0';
    return c - 'a' + 10;
}

inline char get(int x)
{
    if (x <= 9) return x + '0';
    return x - 10 + 'A';
}

inline void print(char* str)
{
    for (int i = 0; str[i]; i ++ )
        printf("\\x%c%c", get(str[i] / 16), get(str[i] % 16));
}

int main()
{
    scanf("%d%d%d%d", &m, &n, &p, &q);
    char str[100];
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < m; j ++ )
        {
            scanf("%s", str);
            int len = strlen(str);
            if (len == 2)
            {
                int t = get(str[1]);
                for (int k = 0; k < 3; k ++ )
                    g[i][j][k] = t * 16 + t;
            }
            else if (len == 4)
            {
                for (int k = 0; k < 3; k ++ )
                {
                    int t = get(str[1 + k]);
                    g[i][j][k] = t * 16 + t;
                }
            }
            else
            {
                for (int k = 0; k < 3; k ++ )
                    g[i][j][k] = get(str[1 + k * 2]) * 16 + get(str[2 + k * 2]);
            }
        }

    int bg[3] = {0};
    for (int i = 0; i < n / q; i ++ )
    {
        for (int j = 0; j < m / p; j ++ )
        {
            int cur[3] = {0};
            for (int x = 0; x < q; x ++ )
                for (int y = 0; y < p; y ++ )
                    for (int z = 0; z < 3; z ++ )
                        cur[z] += g[i * q + x][j * p + y][z];
            for (int k = 0; k < 3; k ++ ) cur[k] /= p * q;
            if (cur[0] == bg[0] && cur[1] == bg[1] && cur[2] == bg[2]) ;  // pass
            else if (!cur[0] && !cur[1] && !cur[2]) print("\033[0m");
            else
            {
                sprintf(str, "\033[48;2;%d;%d;%dm", cur[0], cur[1], cur[2]);
                print(str);
            }
            for (int k = 0; k < 3; k ++ ) bg[k] = cur[k];
            print(" ");
        }
        if (bg[0] || bg[1] || bg[2])
        {
            print("\033[0m");
            for (int k = 0; k < 3; k ++ ) bg[k] = 0;
        }
        print("\n");
    }
    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/908738/

  1. 推荐系统
    ccf-csp 2019秋季真题题解_第10张图片
    ccf-csp 2019秋季真题题解_第11张图片
    ccf-csp 2019秋季真题题解_第12张图片

代码:

#include 
#include 
#include 
#include 
#include 
#include 

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
const int N = 55;

int m, n;
set<PII> g[N];
unordered_map<int, int> f[N];
int sum[N], cnt[N];
set<PII>::reverse_iterator it[N];
vector<int> ans[N];

int main()
{
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i ++ )
    {
        int id, score;
        scanf("%d%d", &id, &score);
        for (int j = 0; j < m; j ++ )
        {
            f[j][id] = score;
            g[j].insert({score, -id});
        }
    }

    int Q;
    scanf("%d", &Q);
    while (Q -- )
    {
        int t;
        scanf("%d", &t);
        if (t == 1)
        {
            int type, id, score;
            scanf("%d%d%d", &type, &id, &score);
            f[type][id] = score;
            g[type].insert({score, -id});
        }
        else if (t == 2)
        {
            int type, id;
            scanf("%d%d", &type, &id);
            g[type].erase({f[type][id], -id});
            f[type].erase(id);
        }
        else
        {
            int tot;
            scanf("%d", &tot);
            for (int i = 0; i < m; i ++ )
            {
                scanf("%d", &sum[i]);
                it[i] = g[i].rbegin();
                cnt[i] = 0;
                ans[i].clear();
            }
            while (tot -- )
            {
                int k = -1;
                for (int i = 0; i < m; i ++ )
                    if (it[i] != g[i].rend() && cnt[i] < sum[i])
                        if (k == -1 || it[i]->x > it[k]->x)
                            k = i;
                if (k == -1) break;
                ans[k].push_back(-it[k]->y);
                cnt[k] ++ ;
                it[k] ++ ;
            }
            for (int i = 0; i < m; i ++ )
                if (ans[i].empty()) puts("-1");
                else
                {
                    for (auto x: ans[i])
                        printf("%d ", x);
                    puts("");
                }
        }
    }
    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/911199/

  1. 城市规划
    ccf-csp 2019秋季真题题解_第13张图片
    ccf-csp 2019秋季真题题解_第14张图片

代码:

#include 
#include 
#include 

using namespace std;

typedef long long LL;
const int N = 50010, M = N * 2;

int n, m, K;
int h[N], e[M], w[M], ne[M], idx;
LL f[N][110];
bool st[N];
int sz[N];
LL ans = 1e18;

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u, int fa)
{
    f[u][0] = 0;
    if (st[u]) f[u][1] = 0;
    sz[u] = 1;
    for (int i = h[u]; ~i; i = ne[i])  // 枚举物品组
    {
        int ver = e[i];
        if (ver == fa) continue;
        dfs(ver, u);
        sz[u] += sz[ver];
        for (int j = min(sz[u], K); j >= 0; j -- )  // 枚举体积
            for (int k = 0; k <= min(j, sz[ver]); k ++ )  // 枚举决策
                f[u][j] = min(f[u][j], f[u][j - k] + f[ver][k] + (LL)w[i] * k * (K - k));
    }

    ans = min(ans, f[u][K]);
}

int main()
{
    scanf("%d%d%d", &n, &m, &K);
    memset(h, -1, sizeof h);
    while (m -- )
    {
        int x;
        scanf("%d", &x);
        st[x] = true;
    }
    for (int i = 0; i < n - 1; i ++ )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }

    memset(f, 0x3f, sizeof f);
    dfs(1, -1);
    printf("%lld\n", ans);
    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/911370/

更多历年题解戳这里:ccf-csp 历年真题题解

你可能感兴趣的:(备战2021春,ccf-csp,C++,算法,数据结构,ccf,csp)