Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.6 kB
3
Indexable
Never
#include<set>
//#include<iostream>
using namespace std;
struct news{
    int score;
    int section;
} news[50005];
struct Compare {
 bool operator() (int x, int y) {
     if (news[x].score != news[y].score)
        return news[x].score > news[y].score;// return bai bao co diem cao hon
     return x > y;//return bai bao co diem bang nhau va co id cao hon
 }
};

int user_fav[100001];
set<int, Compare> section[10];
void init()
{
    for (int i = 0; i < 10; i++) 
        section[i].clear();
    for(int i = 0; i < 100001; i++)
        user_fav[i] = -1;
}
void addNews(int mSection, int mNewsId)
{
    news[mNewsId].score = 0;
    news[mNewsId].section = mSection;
    section[mSection].insert(mNewsId);
}
void eraseNews(int mNewsId)
{
    section[news[mNewsId].section].erase(mNewsId);
}
void readNews(int mUserId, int mNewsId)
{// neu đổi thẳng ở trong thi nó ko tự động sắp xếp lại -> sai
    section[news[mNewsId].section].erase(mNewsId);//thu vien set, bo bai bao ra khoi section
    news[mNewsId].score++;// tang diem bai bao
    user_fav[mUserId] = news[mNewsId].section;//cap nhap section theo userID,moi userID chi quan tam section moi nhat
    section[news[mNewsId].section].insert(mNewsId);// them vao section
}
void changeSection(int mNewsId, int mSection)// bai bao doi section
{
    section[news[mNewsId].section].erase(mNewsId);
    news[mNewsId].section = mSection;
    section[mSection].insert(mNewsId);
}
int getList(int mUserId, int mList[])
{
    set<int, Compare> ::iterator it[10];
    for (int i = 0; i < 10; i++) {
        it[i] = section[i].begin();
    }
    int cnt = 0;
    while (cnt < 10) {//lay 10 gia tri
        int id = -1;
        int max_score = -1;
        int next = -1;
        for (int i = 0; i < 10; i++) {// so sanh 10 gia tri dau tien tung section
            if (it[i] == section[i].end()) continue;//section het bao
            int temp = news[*it[i]].score;//bien so sanh
            //int mid = *it[i];// con tro cua bien
            if (news[*it[i]].section == user_fav[mUserId]) 
                temp += 10;// dung section +10

            if (temp > max_score || (temp == max_score && *it[i] > id)) {
                max_score = temp;//update bien
                id = *it[i];
                next = i;
            }
        }
        if (max_score == -1) break;
        mList[cnt] = id;//them bai bao diem cao nhat
        it[next]++;// neu section co bai bao được chọn rồi thì phải nhảy qua bài của section đ
        cnt++;// tang bien den 10
    }
    return cnt;
}