试题编号: | 201909-4 |
试题名称: | 推荐系统 |
时间限制: | 5.0s |
内存限制: | 512.0MB |
问题描述: | |
#include<iostream>
#include<vector>
#include<set>//set与结构体的结合使用
using namespace std;
typedef struct Good{//商品结构体
int type;
int id;
int score;
}Good;
typedef struct Del{//被删除的商品信息
int type;
int id;
}Del;
bool operator<(const Good & x,const Good & y){//把商品结构体装入set
//其中涉及到了score,type,id三个信息来装入set,所以set在erase,find等操作时,必须三个信息一起查找才有结果
if(x.score!=y.score)return x.score>y.score;
else{
if(x.type==y.type)return x.id<y.id;
else return x.type<y.type;
}
}
bool operator<(const Del & x,const Del & y){//把删除商品结构体装入set
//其中涉及到了type,id两个信息来装入set,所以set在erase,find等操作时,只要有两个信息一起查找有结果
if(x.type==y.type)return x.id<y.id;
else return x.type<y.type;
}
set<Good> good; //存放所有商品信息
set<Del> del; //存放所有已删除的商品信息
int main(){
int m,n,num=0;
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);
}
}
cin>>n;
while(n--){
int op;
cin>>op;
if(op==1){
Good temp;
cin>>temp.type>>temp.id>>temp.score;
good.insert(temp);
}else if(op==2){
Del temp;
cin>>temp.type>>temp.id;
del.insert(temp);
}else{
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();k>0 && it!=good.end();){
if(type_k[(*it).type]>0){
Del has_del;
has_del.type=(*it).type;has_del.id=(*it).id;
if(del.find(has_del)!=del.end()){ //若存在于删除表中
good.erase(it++); //删除该元素,迭代器自增
//重点!!erase之后,it所在的那个位置迭代器为NULL,所以不能拿出来执行it++
//所以要在执行erase之前,先让it++
}
//未删除,就装入输出列表
else{
type_k[(*it).type]--;
k--;
vec[(*it).type].push_back((*it).id);
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]<<" ";
}cout<<endl;
}else cout<<"-1\n";
}
}
}
}
更多相关CCF的试题解答,请点击>>CCF历年认证考试解答