CCF 2019-9-4 推荐系统 100分

试题编号: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历年认证考试解答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值