Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
4
Indexable
Never
#define MAXU 1005
#define MAXP 100005
struct Post{
    int like, time, next;
} posts[MAXP];
struct User{
    int cntF, userF[MAXU];
    int postL;
} users[MAXU];
void init(int N){
    for (int i = 0; i < MAXU; i++){
        users[i].cntF = 1;
        users[i].userF[0] = i;
        users[i].postL = 0;
    }
}
void follow(int uID1, int uID2, int timestamp){
    users[uID1].userF[users[uID1].cntF++] = uID2;
}
void makePost(int uID, int pID, int timestamp){
    posts[pID].time = timestamp;
    posts[pID].like = 0;
    posts[pID].next = users[uID].postL;
    users[uID].postL = pID;
}
void like(int pID, int timestamp){
    posts[pID].like++;
}
bool cmp(int p1, int p2, bool isOut){
    if (isOut) return posts[p1].time > posts[p2].time;
    if (posts[p1].like != posts[p2].like)
        return posts[p1].like > posts[p2].like;
    return posts[p1].time > posts[p2].time;
}
void swap(int &a, int &b){
    int t = a; a = b; b = t;
}
void sort(int *arr, int n, bool isOut){
    for (int i = n; i >= 1; i--){
        if (cmp(arr[i], arr[i-1], isOut)) swap(arr[i], arr[i-1]);
        else break;
    }
}
void getFeed(int uID, int timestamp, int pIDList[]){
    int postRank[15], cnt = 0;
    int postRankOut[15], cntOut = 0;
    int postOut[MAXU];
 
    for (int i = 0; i < users[uID].cntF; i++){
        postOut[i] = 0;
        int u = users[uID].userF[i];
        for (int p = users[u].postL; p; p = posts[p].next){
            if (timestamp - posts[p].time > 1000){
                postOut[i] = p;
                break;
            }
            postRank[cnt] = p;
            sort(postRank, cnt, 0);
            if (cnt < 10) cnt++;
        }
    }
    if (cnt < 10){
        for (int i = 0; i < users[uID].cntF; i++){
            for (int p = postOut[i]; p; p = posts[p].next){
                if (cntOut == 10 - cnt && !cmp(p, postRankOut[cntOut-1], 1))
                    break;
                postRankOut[cntOut] = p;
                sort(postRankOut, cntOut, 1);
                if (cntOut < 10 - cnt) cntOut++;
            }
        }
    }
    for (int i = 0; i < cnt; i++) pIDList[i] = postRank[i];
    for (int i = 0; i < cntOut; i++) pIDList[cnt+i] = postRankOut[i];
}