CSP第17次 201909-4 推荐系统 C/C++满分答案
题目描述有大问题,“同类商品的编号从小到大输出” 这句话
并不是【同类商品中,编号小的先输出】
真正AC的答案是【同类商品中,分数高的编号先输出;分数相同时,编号小的先输出】
本题对C++选手主要是考察对STL容器的熟练度,接着就是时间问题了
对第四题来说还是算比较简单的,只要选对容器就不需要手码任何算法,题目思路清晰,模块划分好之后主要考验码力
也是非常好的STL容器练手题,重点在于:对有排序功能的STL容器要懂得如何自定义排序。这种东西到了赛场上会就是会,不会可能就要耗大量的时间手码出来了(还不一定码得出来哈哈哈)
把测试案例放在这里,方便各位复制测试
2 3
1 3
2 2
3 1
8
3 100 1 1
1 0 4 3
1 0 5 1
3 10 2 2
3 10 1 1
2 0 1
3 2 1 1
3 1 1 1
下面这段是满分代码,不懂可以问
#include <stdio.h>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
int m,n;
string str="";//把输出的内容存起来,最后一次性输出
struct goods
{
int id;
int score;
int type;
};
class set_cmp//重载,使set自定义排序(本题重中之重)
{
public:
bool operator()(const goods &a,const goods &b)
{
if(a.score!=b.score)
return a.score>b.score;
else if(a.type!=b.type)
return a.type<b.type;
else
return a.id<b.id;
}
};
set<goods,set_cmp> see;//存储所有商品
map<int,int> set_find_hash[50];//通过 类型和编号 映射 分数,仅为了删除时可以快速找到set see中的目标
void zengjia()//1增加操作
{
int type,comm,score;
scanf("%d %d %d",&type,&comm,&score);
goods g;
g.id=comm,g.score=score,g.type=type;
see.insert(g);
set_find_hash[g.type][g.id]=g.score;
}
void shanchu()//2删除操作
{
int i,type,comm,score;
scanf("%d %d",&type,&comm);
score=set_find_hash[type][comm];
goods g;
g.id=comm,g.score=score,g.type=type;
see.erase(see.find(g));
set_find_hash[g.type].erase(g.id);
}
void chaxun()//3查询操作
{
int i,j,k,k_arr[60]={
0};
vector<int> print_arr[60];//将查询结果暂时存储
scanf("%d",&k);
for(i=0;i<m;i++)
scanf("%d",&k_arr[i]);
for(set<goods>::iterator it=see.begin(); k>0&&it!=see.end(); k--,it++)//set see已经按规则排序好了,直接遍历得到查询结果
if(k_arr[(*it).type]-- > 0)
print_arr[(*it).type].push_back((*it).id);
for(i=0;i<m;i++)//把得到的print_arr整理存入str
{
if(print_arr[i].size()==0)
str+="-1 ";
else
for(vector<int>::iterator it=print_arr[i].begin();it!=print_arr[i].end();it++)
{
char add_str[10];
sprintf(add_str,"%d ",*it);
str+=add_str;
}
str.erase(str.end()-1);//每行去掉最后多余的空格
str+='\n';
}
}
int main()
{
int i,j,op_num;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++)//将set see按题意初始化,所有类的n个编号都相同
{
int id,score;
scanf("%d %d",&id,&score);
for(j=0;j<m;j++)
{
goods g;
g.id=id,g.score=score,g.type=j;
see.insert(g);
set_find_hash[g.type][g.id]=g.score;
}
}
scanf("%d",&op_num);
for(i=0;i<op_num;i++)//万事俱备 处理OP
{
int op_kind;
scanf("%d",&op_kind);
if(op_kind==1)
zengjia();
else if(op_kind==2)
shanchu();
else if(op_kind==3)
chaxun();
}
cout<<str;
return 0;
}
下面这段是理解错题目意思(倒不如说出题人的表达有问题),得到的60分(仅与上面满分代码的chaxun()函数有些不同,多了排序的功能)
#include <stdio.h>
#include <map>
#include <string>
#include <string.h>
#include <iostream>
#include <set>
using namespace std;
int m,n;
string str="";//把输出的内容存起来,最后一次性输出
struct goods
{
int id;
int score;
int type;
};
class set_cmp//重载,使set自定义排序
{
public:
bool operator()(const goods &a,const goods &b)
{
if(a.score!=b.score)
return a.score>b.score;
else if(a.type!=b.type)
return a.type<b.type;
else
return a.id<b.id;
}
};
set<goods,set_cmp> see;//存储所有商品
map<int,int> set_find_hash[50];//通过 类型和编号 映射 分数
void zengjia()
{
int type,comm,score;
scanf("%d %d %d",&type,&comm,&score);
goods g;
g.id=comm,g.score=score,g.type=type;
see.insert(g);
set_find_hash[g.type][g.id]=g.score;
}
void shanchu()
{
int i,type,comm,score;
scanf("%d %d",&type,&comm);
score=set_find_hash[type][comm];
goods g;
g.id=comm,g.score=score,g.type=type;
see.erase(see.find(g));
set_find_hash[g.type].erase(g.id);
}
void chaxun()
{
int i,j,k,k_arr[60]={
0};
set<int> print_arr[60];//为了同一类编号由小到大输出
scanf("%d",&k);
for(i=0;i<m;i++)
scanf("%d",&k_arr[i]);
for(set<goods>::iterator it=see.begin(); k>0&&it!=see.end(); k--,it++)
if(k_arr[(*it).type]-- > 0)
print_arr[(*it).type].insert((*it).id);
for(i=0;i<m;i++)//把得到的print_arr整理存入str
{
if(print_arr[i].size()==0)
str+="-1 ";
else
for(set<int>::iterator it=print_arr[i].begin();it!=print_arr[i].end();it++)
{
char add_str[10];
sprintf(add_str,"%d ",*it);
str+=add_str;
}
str.erase(str.end()-1);
str+='\n';
}
}
int main()
{
int i,j,op_num;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++)
{
int id,score;
scanf("%d %d",&id,&score);
for(j=0;j<m;j++)
{
goods g;
g.id=id,g.score=score,g.type=j;
see.insert(g);
set_find_hash[g.type][g.id]=g.score;
}
}
scanf("%d",&op_num);
for(i=0;i<op_num;i++)
{
int kind;
scanf("%d",&kind);
if(kind==1)
zengjia();
else if(kind==2)
shanchu();
else if(kind==3)
chaxun();
}
cout<<str;
return 0;
}
把做的第一版本也放下面吧,十分运行超时
其实做之前就想到必超时,只是想先看下题目理解对没
思路是把所有类的所有编号物品都放到一个vector里,查询前排序一下就行
#include <stdio.h>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int m,n,op_num,need_sort=1;
string str="";
struct goods
{
int id;
int score;
int type;
};
vector<goods> vec;
bool cmp(goods a,goods b)
{
if(a.score!=b.score)
return a.score>b.score;
else if(a.type!=b.type)
return a.type<b.type;
else
return a.id<b.id;
}
void zengjia()
{
int type,comm,score;
need_sort=1;
scanf("%d %d %d",&type,&comm,&score);
goods g;
g.id=comm,g.score=score,g.type=type;
vec.push_back(g);
}
void shanchu()
{
int i,type,comm;
scanf("%d %d",&type,&comm);
for(vector<goods>::iterator it = vec.begin();it!=vec.end();it++)
if((*it).type==type&&(*it).id==comm)
{
vec.erase(it);
break;
}
}
void chaxun()
{
int i,j,get_index=0,k,k_arr[60]={
0};
vector<int> print_arr[60];
scanf("%d",&k);
for(i=0;i<m;i++)
scanf("%d",&k_arr[i]);
if(need_sort)
{
sort(vec.begin(),vec.end(),cmp);
need_sort=0;
}
while(1)
{
if(k_arr[vec[get_index].type]>0)
{
print_arr[vec[get_index].type].push_back(vec[get_index].id);
k_arr[vec[get_index].type]--;
k--;
}
get_index++;
if(k==0||get_index==vec.size())
break;
}
for(i=0;i<m;i++)
{
if(print_arr[i].size()==0)
str+="-1";
else
for(j=0;j<print_arr[i].size();j++)
{
char add_str[10];
sprintf(add_str,"%d",print_arr[i][j]);
str+=add_str;
if(j!=print_arr[i].size()-1)
str+=' ';
}
str+='\n';
}
}
int main()
{
int i,j;
scanf("%d %d",&m,&n);
for(i=0;i<n;i++)
{
int id,score;
scanf("%d %d",&id,&score);
for(j=0;j<m;j++)
{
goods g;
g.id=id,g.score=score,g.type=j;
vec.push_back(g);
}
}
scanf("%d",&op_num);
for(i=0;i<op_num;i++)
{
int kind;
scanf("%d",&kind);
if(kind==1)
zengjia();
else if(kind==2)
shanchu();
else if(kind==3)
chaxun();
}
cout<<str;
return 0;
}