csp 2019 - 4 추천 시스템

18925 단어 수필
#include 
using namespace std;
const int maxn = 3e4 + 7;
const int maxm = 55;
int n, m;
int op, typ, com, val;
int q;
int cnt;
int k[maxm];

struct goods{
     
    int type, com, val;
    bool operator < (const goods &b) const
    {
     
        if(val == b.val)
        {
     
            if(type == b.type)
            {
     
                return com < b.com;
            }
            return type < b.type;
        }
        else
        {
     
            return val > b.val;
        }
    }
    goods(int a, int b, int c) : type(a), com(b), val(c){
     };
};
set <goods> select;
set <goods> :: iterator it;
unordered_map <int, int> sc[maxm];
void add(int t, int c, int v)
{
     
    select.insert(goods(t, c, v));
    sc[t][c] = v;
}
void del(int t, int c)
{
     
    select.erase(goods(t, c, sc[t][c]));
}
void work()
{
     
    it = select.begin();
    vector <int> ans[maxm];
    while(cnt && it != select.end())
    {
     
        typ = it->type;
        com = it->com;
        if(k[typ])
        {
     
            k[typ] --;
            ans[typ].push_back(com);
            cnt -- ;
        }
        it ++;
    }
    for(int i = 0; i < m; i ++)
    {
     
        if(ans[i].size())
        {
     
            for(auto j : ans[i])
            {
     
                printf("%d ", j);
            }
            printf("
"
); } else printf("-1
"
); } } int main() { scanf("%d%d", &m, &n); for(int i = 0; i < n; i ++) { scanf("%d%d", &com, &val); for(int j = 0; j < m; j ++) { add(j, com, val); } } scanf("%d", &q); for(int i = 0; i < q; i ++) { scanf("%d", &op); if(op == 1) { scanf("%d%d%d", &typ, &com, &val); add(typ, com, val); } if(op == 2) { scanf("%d%d", &typ, &com); del(typ, com); } if(op == 3) { scanf("%d", &cnt); for(int j = 0; j < m; j ++) { scanf("%d", k + j); } work(); } } }

좋은 웹페이지 즐겨찾기