CCF 201909-4 推荐系统

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

#include <iostream>
#include <set>
#include <vector>
using namespace std;

struct Good{//商品结构体
    int type;
    int id;
    int score;
};

struct Del{//删除的物品
    int type;
    int id;
};

int m,n;

bool operator <(const Good& x,const Good& y){
    if(x.score!=y.score)
        return x.score>y.score;
    else{
        if(x.type!=y.type)
            return x.type<y.type;
        else
            return x.id<y.id;
    }
}

bool operator <(const Del& x,const Del& y){
    if(x.type!=y.type)
        return x.type<y.type;
    else
        return x.id<y.id;
}


set<Good> good;
set<Del> del;

int main()
{
    cin>>m>>n;
    for(int i=0;i<n;i++){
        Good temp;
        cin>>temp.id>>temp.score;
        for(int j=0;j<m;j++){
            temp.type=j;
            good.insert(temp);
        }
    }

    int oper;

    cin>>n;
    while(n--){
        cin>>oper;
        if(oper==1){
            Good temp;
            cin>>temp.type>>temp.id>>temp.score;
            good.insert(temp);
        }
        else if(oper==2){
            Del tmp;
            cin>>tmp.type>>tmp.id;
            del.insert(tmp);
        }
        else if(oper==3){
            int k,type_k[55]={0};
            vector<int> vec[55];
            cin>>k;
            for(int i=0;i<m;i++)
                cin>>type_k[i];

            for(set<Good>::iterator it=good.begin();it!=good.end()&&k>0;){
                if(type_k[(*it).type]>0){//该类商品还能再接着拿
                    Del tmp;
                    tmp.id=(*it).id,tmp.type=(*it).type;
                    if(del.find(tmp)!=del.end()){//这个商品被删除了
                        good.erase(it++);//it++不能单独写出来,erase之后原来的it指向NULL再++就会指向未知的地方
                    }
                    else{
                        vec[(*it).type].push_back((*it).id);
                        k--;
                        type_k[(*it).type]--;
                        it++;
                    }
                }
                else
                    it++;
            }
            //输出
            for(int i=0;i<m;i++){
                if(vec[i].size()>0){
                    for(int j=0;j<vec[i].size();j++){
                        cout<<vec[i][j];
                        if(j!=vec[i].size()-1)
                            cout<<" ";
                    }
                    cout<<endl;
                }
                else
                    cout<<"-1\n";
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值