CCP-CSP认证考试 201909-4 推荐系统 c/c++题解

题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

题解:

先说一下,我这道题暂时只有10分。。。
我的思路:

  1. vector <product> productClass[50+5];用来表示商品的种类,每一种商品都是一个带有product {id,score}元素的vector,做好初始化即可。
  2. 每次输入一个操作时,先判断第一个输入是1or2or3,然后分别做不同的处理:
    1:在对应的vector(productClass[type])中插入一个商品(commodity,score)
    2:在对应的vector(productClass[type])中找到商品的编号为commodity的迭代器,然后删除即可
    3:由于可能有商品的插入和删除,所以查询之前要提前排个序(每一类商品 都应该满足 商品的score从大到小,score相等,id从小到大),
    然后再执行查询操作,首先输入一个K,表示可以输出的商品个数,然后输入每类商品可以输出的个数(放到k数组中),然后遍历k数组,对于每一类商品,从前往后输出指定数量的商品id,如果输出个数达到了K,则提前退出,以后的商品都是输出-1了。
    我也不知道哪里错了,如果有人愿意帮我找出原因,感激不尽!
    在这里插入图片描述
    我感觉是上面的第二点,我还没看懂题目的意思,暂时不去看了(复习12月的考试的时候再来尝试调试出来)。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;
int m,n;// m类商品,每类商品初始有n个编号各不相同的商品
int opnum;// opnum次操作

typedef struct Product
{
    int id,score;
}product;
vector <product> productClass[50+5];// 每一类商品(productClass)都有若干个商品product

int cmp(product a,product b)
{
    if(a.score != b.score)
    {
        return a.score > b.score;
    }
    else
    {
        return a.id < b.id;
    }
}
void SortProductClass(int i)
{
    sort(productClass[i].begin(), productClass[i].end(), cmp);
}

int main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */

    while(cin >> m >> n)
    {
        int id,score;
        for(int i = 1; i <= n; i++)
        {
            cin >> id >> score;
            product t; t.id = id; t.score = score;
            for(int j = 0; j < m; j++)
            {
                productClass[j].push_back(t);
            }
        }
        /*
        printf("初始时商品:\n");
        for(int i = 0; i < m; i++)
        {
            printf("第%d类:\n",i);
            for(int j = 0; j < (int)productClass[i].size(); j++)
            {
                printf("(%d,%d)\n",productClass[i][j].id,productClass[i][j].score);
            }
        }*/

        cin >> opnum;
        int tag;
        for(int i = 1; i <= opnum; i++)
        {
            cin >> tag;
            if(tag == 1)
            {
                // 1 type commodity score
                int type,commodity,score;
                cin >> type >> commodity >> score;
                product newProduct;
                newProduct.id = commodity; newProduct.score = score;
                productClass[type].push_back(newProduct);
            }
            else if(tag == 2)
            {
                // 2 type commodity
                int type,commodity;
                cin >> type >> commodity;
                for(auto it = productClass[type].begin(); it != productClass[type].end(); it++)
                {
                    if((*it).id == commodity)
                    {
                        productClass[type].erase(it);
                        break;
                    }
                }
            }
            else if(tag == 3)
            {
                // 3 K k_0 k_1 ... k_{m-1}
                // 每次查询之前:
                // 都要保证每种商品productClass[j]中的具体商品都是:score从大到小,id从小到大
                for(int j = 0; j < m; j++)
                {
                    SortProductClass(j);
                }
                /*
                printf("开始查询:\n");
                printf("商品的状态:\n");
                for(int j = 0; j < m; j++)
                {
                    printf("第%d类:\n",j);
                    for(int k = 0; k < (int)productClass[j].size(); k++)
                    {
                        printf("(%d,%d)\n",productClass[j][k].id,productClass[j][k].score);
                    }
                }*/

                int inputSum = 0;// 输出个数不能超过K
                int K;
                int k[MAX];
                cin >> K;
                for(int j = 0; j < m; j++)
                {
                    cin >> k[j];
                }
                for(int j = 0; j < m; j++)
                {
                    // 对于每种商品(m种),选出 score 值较大的k[j]个
                    if(inputSum >= K)
                    {
                        cout << "-1" << endl;
                        continue;
                    }
                    for(int r = 0; r < k[j]; r++)
                    {
                        if(inputSum >= K)
                        {
                            break;
                        }
                        cout << productClass[j][r].id << " ";
                        inputSum++;
                    }
                    cout << endl;
                }
            }
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值