Hsue66 / Algo

0 stars 0 forks source link

socialmedia #8

Open Hsue66 opened 5 years ago

Hsue66 commented 5 years ago

include

using namespace std;

define U_MAX 1001

define T_MAX 100001

struct Record { int timestamp, like, uId; }; Record DB[T_MAX]; int dbSize, sTime; int buf[T_MAX]; bool UserTB[U_MAX][U_MAX]; int Pivot;

inline bool compare(int i, int j) { if (sTime - DB[i].timestamp <= 1000 && sTime - DB[j].timestamp <= 1000) { if (DB[i].like > DB[j].like) return 1; else if (DB[i].like == DB[j].like) { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } else return 0; } else if (sTime - DB[i].timestamp > 1000 && sTime - DB[j].timestamp > 1000) { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } else { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } }

void mergesort(int *p, int len) { if (len < 2) return; int i, j, k, mid; i = k = 0; j = mid = len / 2;

mergesort(p, mid);
mergesort(p + mid, len - mid);

while (i < mid && j < len) {
    if (compare(p[i], p[j]))
        buf[k++] = p[i++];
    else
        buf[k++] = p[j++];
}
while (i < mid) 
    buf[k++] = p[i++];
while (j < len)
    buf[k++] = p[j++];

for (register int i = 0; i < len; i++)
    p[i] = buf[i];

}

void qsort(int* p, int left, int right) { if (left >= right) return; int l = left - 1; int r = right + 1; int mid = p[(l + r) / 2]; while (1) { while (compare(p[++l], mid)); while (compare(mid, p[--r])); if (l >= r) break; int temp = p[l]; p[l] = p[r]; p[r] = temp; }

qsort(p, left, l - 1);
if (r + 1 <= Pivot)
    qsort(p, r + 1, right);

}

void init(int N) { dbSize = 0; Pivot = 10; for (register int i = 1; i <= N; i++) { for (register int j = 1; j <= N; j++) { if (i == j) UserTB[i][j] = true; else UserTB[i][j] = false; } }

}

void follow(int uID1, int uID2, int timestamp) { UserTB[uID1][uID2] = true; }

void makePost(int uID, int pID, int timestamp) { DB[pID].like = 0; DB[pID].uId = uID; DB[pID].timestamp = timestamp; dbSize++; }

void like(int pID, int timestamp) { DB[pID].like++; }

void getFeed(int uID, int timestamp, int pIDList[]) { int s = dbSize; sTime = timestamp; int out[T_MAX]; int oidx = 0;

for (register int s = dbSize; s > 0; s--) {
    if (oidx >= 10 && timestamp - DB[s].timestamp > 1000)
        break;
    if (UserTB[uID][DB[s].uId])
        out[oidx++] = s;
}
if (oidx != 0)
    qsort(out, 0, oidx - 1);

for (int i = 0; i < 10 && i<oidx; i++) 
    pIDList[i] = out[i];

}

void show() { for (int i = 0; i <= dbSize; i++) cout << DB[i].uId << " " << DB[i].like << " " << DB[i].timestamp << endl; }

void done(){ int tmp[10]; follow(1, 2, 1); follow(2, 1, 1); getFeed(2, 534,tmp); getFeed(2, 766, tmp); getFeed(1, 1088, tmp); makePost(1, 1, 1752); like(1, 1861); makePost(1, 2, 2027); makePost(2, 3, 2117); makePost(1, 4, 2163);

getFeed(2, 2476, tmp);
like(2, 2494);
like(1, 2542);
makePost(1, 5, 2666);
getFeed(2, 2853, tmp);
like(3, 2944);
getFeed(2, 3033, tmp);

}

void sample() {

follow(1, 2, 1);
follow(2, 1, 1);
makePost(1, 1, 0);
makePost(1, 2, 1020);
like(2, 1021);
like(2, 1023);
makePost(1, 3, 1030);
makePost(1, 4, 1050);
makePost(1, 5, 1060);
like(5, 1063);
makePost(1, 6, 1070);
makePost(1, 7, 1080);
makePost(1, 8, 1090);
makePost(1, 9, 1100);
like(9, 1103);
//makePost(1, 10, 1110);
//makePost(1, 11, 1200);

}

void Test() { init(4);

//done();

int tmp[10];
sample();
getFeed(2, 2000, tmp);

}

Hsue66 commented 5 years ago

/*#define U_MAX 1001

define T_MAX 100001

struct Record { int timestamp, like, uId; }; Record DB[T_MAX]; int dbSize, sTime; int buf[T_MAX]; bool UserTB[U_MAX][U_MAX];

inline bool compare(int i, int j) { if (sTime - DB[i].timestamp <= 1000 && sTime - DB[j].timestamp <= 1000) { if (DB[i].like > DB[j].like) return 1; else if (DB[i].like == DB[j].like) { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } else return 0; } else if (sTime - DB[i].timestamp > 1000 && sTime - DB[j].timestamp > 1000) { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } else { if (DB[i].timestamp > DB[j].timestamp) return 1; else return 0; } }

void mergesort(int *p, int len) { if (len < 2) return; int i, j, k, mid; i = k = 0; j = mid = len / 2;

mergesort(p, mid);
mergesort(p + mid, len - mid);

while (i < mid && j < len) {
    if (compare(p[i], p[j]))
        buf[k++] = p[i++];
    else
        buf[k++] = p[j++];
}
while (i < mid)
    buf[k++] = p[i++];
while (j < len)
    buf[k++] = p[j++];

for (register int i = 0; i < len; i++)
    p[i] = buf[i];

}

void init(int N) { dbSize = 0; for (register int i = 1; i <= N; i++) { for (register int j = 1; j <= N; j++) { if (i == j) UserTB[i][j] = true; else UserTB[i][j] = false; } }

}

void follow(int uID1, int uID2, int timestamp) { UserTB[uID1][uID2] = true; }

void makePost(int uID, int pID, int timestamp) { DB[pID].like = 0; DB[pID].uId = uID; DB[pID].timestamp = timestamp; dbSize++; }

void like(int pID, int timestamp) { DB[pID].like++; }

void getFeed(int uID, int timestamp, int pIDList[]) { int s = dbSize; sTime = timestamp; int out[T_MAX] = { 0 }; int oidx = 0; while (s > 0) { //cout << oidx << " " << timestamp - DB[s].timestamp << endl; if (oidx >= 10 && timestamp - DB[s].timestamp > 1000) break; if (UserTB[uID][DB[s].uId]) out[oidx++] = s; s--; } if (oidx != 0) mergesort(out, oidx); for (int i = 0; i < 10; i++) { pIDList[i] = out[i]; //cout << out[i] << " "; } //cout << endl; } */