Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
2
Indexable
Never
#include<iostream>
#include<unordered_map>
#include<set>
#include<vector>
 
 
using namespace std;
 
unordered_map<int, bool> newsExpectancy;
unordered_map<int, vector<int>>userMap;
unordered_map<int, vector<int>>usersNewsId;
 
struct newsArticle
{
    int time;
    int newsid;
    int chanid;
};
 
struct Comp {
    bool operator()(newsArticle n1, newsArticle n2) const {
        if (n1.time == n2.time)
            return n1.newsid < n2.newsid;
        return n1.time < n2.time;
    }
};
 
set<newsArticle, Comp> newsPool;
 
 
void ProcessOn(int timeS)
{
    while (newsPool.size() > 0)
    {
        auto it = newsPool.begin();
        newsArticle c = *it;
        if (c.time > timeS)
            break;
        newsPool.erase(it);
        if (newsExpectancy[c.newsid]) {
            for (auto& itr : userMap[c.chanid])
                usersNewsId[itr].push_back(c.newsid);
        }
    }
}
 
void init(int N, int K)
{
    newsExpectancy.clear();
    userMap.clear();
    usersNewsId.clear();
    newsPool.clear();
}
 
void registerUser(int mTime, int mUID, int mNum, int mChannelIDs[])
{
    ProcessOn(mTime);
 
    for (int i = 0; i < mNum; i++)
    {
        userMap[mChannelIDs[i]].push_back(mUID);
    }
}
 
int offerNews(int mTime, int mNewsID, int mDelay, int mChannelID)
{
    ProcessOn(mTime);
    newsExpectancy[mNewsID] = true;
 
    newsArticle c1;
    c1.time = mTime + mDelay;
    c1.newsid = mNewsID;
    c1.chanid = mChannelID;
    newsPool.insert(c1);
 
    return userMap[mChannelID].size();
}
 
void cancelNews(int mTime, int mNewsID)
{
    ProcessOn(mTime);
    newsExpectancy[mNewsID] = false;
}
 
int checkUser(int mTime, int mUID, int mRetIDs[])
{
    ProcessOn(mTime);
 
    int countt = 0;
    int i = 0;
    for (auto it = usersNewsId[mUID].end(); it != usersNewsId[mUID].begin();it-- )
    {
        //need to check this api
        if (!newsExpectancy[*it])
            continue;
        if (i < 3)
            mRetIDs[i++] = *it;
        countt++;
    }
 
    usersNewsId[mUID].clear();
 
    return countt;
}