Untitled
unknown
plain_text
2 years ago
3.9 kB
18
Indexable
struct USER
{
int follow_cnt;
int follow_user[1000];
struct POST *post_head;
} user[1001];
struct POST
{
int id;
int like;
int timestamp;
struct POST *next;
} post[100001];
void init(int N)
{
int i;
for (i = 1; i <= N; i++)
{
user[i].follow_cnt = 1;
user[i].follow_user[0] = i;
user[i].post_head = 0;
}
}
void follow(int uID1, int uID2, int timestamp)
{
user[uID1].follow_user[user[uID1].follow_cnt] = uID2;
user[uID1].follow_cnt++;
}
void makePost(int uID, int pID, int timestamp)
{
post[pID].id = pID;
post[pID].like = 0;
post[pID].timestamp = timestamp;
post[pID].next = user[uID].post_head;
user[uID].post_head = &post[pID];
}
void like(int pID, int timestamp)
{
post[pID].like++;
}
/*若id1优先,则返回正值,若id2优先,则返回负值*/
int compare(int id1, int id2)
{
if (post[id1].like != post[id2].like)
{
return post[id1].like - post[id2].like;
}
return post[id1].timestamp - post[id2].timestamp;
}
void getFeed(int uID, int timestamp, int pIDList[])
{
int i, j, idx, pos;
int select_cnt_1 = 0;
int select_cnt_2 = 0;
int post_array[10];
struct POST *post_ptr[1000];
struct POST *ptr;
for (i = 0; i < user[uID].follow_cnt; i++)
{
idx = user[uID].follow_user[i];
ptr = user[idx].post_head;
post_ptr[i] = 0;
while (ptr != 0)
{
if (timestamp - ptr->timestamp <= 1000)
{
if (select_cnt_1 < 10)
{
select_cnt_1++;
}
else
{
if (compare(post_array[select_cnt_1 - 1], ptr->id) > 0)
{
ptr = ptr->next;
continue;
}
}
for (j = select_cnt_1 - 2; j >= 0; j--)
{
if (compare(post_array[j], ptr->id) > 0)
{
break;
}
}
pos = j + 1;
for (j = select_cnt_1 - 2; j >= pos; j--)
{
post_array[j + 1] = post_array[j];
}
post_array[pos] = ptr->id;
}
else
{
post_ptr[i] = ptr;
break;
}
ptr = ptr->next;
}
}
for (i = 0; i < select_cnt_1; i++)
{
pIDList[i] = post_array[i];
}
if (select_cnt_1 < 10)
{
int post_array_2_need_cnt = 10 - select_cnt_1;
for (i = 0; i < user[uID].follow_cnt; i++)
{
ptr = post_ptr[i];
while (ptr != 0)
{
if (select_cnt_2 < post_array_2_need_cnt)
{
select_cnt_2++;
}
else
{
if (ptr->id < post_array[select_cnt_2 - 1])
{
break;
}
}
for (j = select_cnt_2 - 2; j >= 0; j--)
{
if (post_array[j] > ptr->id)
{
break;
}
}
pos = j + 1;
for (j = select_cnt_2 - 2; j >= pos; j--)
{
post_array[j + 1] = post_array[j];
}
post_array[pos] = ptr->id;
ptr = ptr->next;
}
}
for (i = 0; i < select_cnt_2; i++)
{
pIDList[i + select_cnt_1] = post_array[i];
}
}
}
Editor is loading...