CCF201909-4 推荐系统(100分)模拟
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CCF201909-4 推荐系统(100分)模拟相关的知识,希望对你有一定的参考价值。
试题编号: 201909-4
试题名称: 推荐系统
时间限制: 5.0s
内存限制: 512.0MB
问题链接:CCF201909-4 推荐系统
问题简述:(略)
问题分析:模拟题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
100分的C++语言程序如下:
/* CCF201909-4 推荐系统 */
#include <bits/stdc++.h>
using namespace std;
const int M = 50;
int a[M], cnt[M + 1];
vector<int> choose[M + 1];
struct Goods {
int type;
int id;
int score;
bool operator < (const Goods &a) const {
return score == a.score ? (type == a.type ? id < a.id : type < a.type) : score > a.score;
}
};
struct DelGoods {
int type;
int id;
bool operator < (const DelGoods &a) const {
return type == a.type ? id < a.id : type < a.type;
}
};
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
set<Goods> g;
set<DelGoods> d;
int m, n, type, id, score;
cin >> m >> n;
for (int i = 1; i <= n; i++) {
cin >> id >> score;
for (int j = 0; j < m; j++)
g.insert(Goods{j, id, score});
}
int num, op, k;
cin >> num;
for (int i = 1; i <= num; i++) {
cin >> op;
if (op == 1) { // 增加
cin >> type >> id >> score;
g.insert(Goods{type, id, score});
} else if (op == 2) { // 删除
cin >> type >> id;
d.insert(DelGoods{type, id});
} else if (op == 3) { // 查询
memset(cnt, 0, sizeof cnt);
for (int j = 0; j < m; j++) choose[j].clear();
cin >> k;
for (int j = 0; j < m; j++) cin >> a[j];
for (set<Goods>::iterator iter = g.begin(); cnt[M] < k && iter != g.end();) {
if (cnt[iter->type] < a[iter->type]) {
if (d.find(DelGoods{iter->type, iter->id}) != d.end())
g.erase(iter++);
else {
cnt[iter->type]++;
cnt[M]++;
choose[iter->type].push_back(iter->id);
iter++;
}
} else
iter++;
}
// 输出结果
for (int j = 0; j < m; j++) {
if (cnt[j] == 0) cout << -1;
else {
for(vector<int>::iterator iter = choose[j].begin(); cnt[M] <= k && iter != choose[j].end(); iter++)
cout << *iter << " ";
}
cout << "\\n";
}
}
}
return 0;
}
100分的C++语言程序如下:
/* CCF201909-4 推荐系统 */
#include <bits/stdc++.h>
using namespace std;
const int M = 50;
int a[M], cnt[M];
struct Goods {
int type;
int id;
int score;
bool operator < (const Goods &a) const {
return score == a.score ? (type == a.type ? id < a.id : type < a.type) : score > a.score;
}
};
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
set<Goods> g;
map<pair<int, int>, int> mp;
int m, n, type, id, score;
cin >> m >> n;
for (int i = 1; i <= n; i++) {
cin >> id >> score;
for (int j = 0; j < m; j++) {
g.insert(Goods{j, id, score});
mp[make_pair(j, id)] = score;
}
}
int num, op, k;
cin >> num;
for (int i = 1; i <= num; i++) {
cin >> op;
if (op == 1) { // 增加
cin >> type >> id >> score;
mp[make_pair(type, id)] = score;
g.insert(Goods{type, id, score});
} else if (op == 2) { // 删除
cin >> type >> id;
score = mp[make_pair(type, id)];
g.erase(Goods{type, id, score});
} else if (op == 3) { // 查询
memset(cnt, 0, sizeof cnt);
cin >> k;
for (int j = 0; j < m; j++) cin >> a[j];
vector<int> v[m];
int sum = 0;
for (set<Goods>::iterator iter = g.begin(); iter != g.end(); iter++) {
type = iter->type;
if (sum < k) {
if (cnt[type] < a[type]) {
sum++;
cnt[type]++;
v[type].push_back(iter->id);
}
} else
break;
bool flag = true;
for (int j = 0; j < m; j++)
if (cnt[j] != a[j]) {
flag = false;
break;
}
if (flag) break;
}
for (int j = 0; j < m; j++) {
if (v[j].size() == 0) cout << -1;
else {
for(int l = 0; l < (int)v[j].size(); l++)
cout << v[j][l] << " ";
}
cout << "\\n";
}
}
}
return 0;
}
以上是关于CCF201909-4 推荐系统(100分)模拟的主要内容,如果未能解决你的问题,请参考以下文章