Untitled

 avatar
unknown
plain_text
2 years ago
4.2 kB
5
Indexable
#define MAXU 100002
#define MAXS 50002
#define MAXM 10
 
struct News
{
    int _section;
    int _cntRead;
    bool _isActive;
};
 
News lNews[MAXS];
int lUser[MAXU];
int RES[MAXM + 1][MAXM]; //RES[10] do not depends on sections
int GN;
 
void init()
{
    for (int i = 0; i < MAXU; i++)
        lUser[i] = 10; // do not depends on sections
    for (int i = 0; i <= MAXM; i++)
        for (int j = 0; j < MAXM; j++)
            RES[i][j] = -1;
}
 
int CheckPriority(int section, int nId1, int nId2)
{
    if (nId2 == -1)
        return 1;
    int score1 = lNews[nId1]._section == section ? 10 + lNews[nId1]._cntRead : lNews[nId1]._cntRead;
    int score2 = lNews[nId2]._section == section ? 10 + lNews[nId2]._cntRead : lNews[nId2]._cntRead;
    if (score1 > score2 || (score1 == score2 && nId1 > nId2))
        return 1;
    return 0;
}
int Update(int section, int nId)
{
    int nIdE = RES[section][MAXM - 1];
    if (CheckPriority(section, nId, nIdE))
    {
        for (int i = 0; i < MAXM; i++)
        {
            if (CheckPriority(section, nId, RES[section][i]))
            {
                for (int j = MAXM - 1; j > i; j--)
                    RES[section][j] = RES[section][j - 1];
                RES[section][i] = nId;
                break;
            }
        }
        return 1;
    }
    return 0;
}
int Remove(int section, int nId)
{
    for (int i = 0; i < MAXM; i++)
    {
        if (RES[section][i] == nId)
        {
            for (int j = i; j < MAXM - 1; j++)
                RES[section][j] = RES[section][j + 1];
            RES[section][MAXM - 1] = -1;
            return 1;
        }
    }
    return 0;
}
void FindLastest(int section)
{
    int maxScore = -1;
    int nId = -1;
    int score;
    bool flag;
    for (int i = 1; i <= GN; i++) //nId
    {
        if (!lNews[i]._isActive)
            continue;
        score = lNews[i]._section == section ? 10 + lNews[i]._cntRead : lNews[i]._cntRead;
        if (score > maxScore || (score == maxScore && i > nId))
        {
            flag = true;
            for (int j = 0; j < MAXM; j++)
            {
                if (RES[section][j] == -1)
                    break;
                if (i == RES[section][j])
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                maxScore = score;
                nId = i;
            }
        }
    }
    //
    if (nId != -1)
    {
        for (int i = 0; i < MAXM; i++)
            if (RES[section][i] == -1)
            {
                RES[section][i] = nId;
                break;
            }
    }
}
void addNews(int mSection, int mNewsId)
{
    GN = mNewsId;
    lNews[mNewsId]._section = mSection;
    lNews[mNewsId]._cntRead = 0;
    lNews[mNewsId]._isActive = true;
    for (int i = 0; i <= MAXM; i++)
        Update(i, mNewsId);
}
 
void eraseNews(int mNewsId)
{
    lNews[mNewsId]._isActive = false;
    for (int i = 0; i <= MAXM; i++)
        if (Remove(i, mNewsId))
            FindLastest(i);
}
 
void Check(int section, int nId)
{
    int i;
    bool flag;
    if (Remove(section, nId))
    {
        for (i = 0; i < MAXM; i++)
            if (RES[section][i] == -1)
                break;
        flag = CheckPriority(section, nId, RES[section][i - 1]);
        if (flag) Update(section, nId);
        else FindLastest(section);
        return;
    }
    Update(section, nId);
}
 
void readNews(int mUserId, int mNewsId)
{
    lUser[mUserId] = lNews[mNewsId]._section;
    lNews[mNewsId]._cntRead++;
    for (int i = 0; i <= MAXM; i++)
        Check(i, mNewsId);
}
 
void changeSection(int mNewsId, int mSection)
{
    int oldSection = lNews[mNewsId]._section;
    lNews[mNewsId]._section = mSection;
 
    Check(oldSection, mNewsId);
    Check(mSection, mNewsId);
}
 
int getList(int mUserId, int mList[])
{
    int i;
    int section = lUser[mUserId];
    for (i = 0; i < MAXM; i++)
    {
        if (RES[section][i] == -1)
            break;
        mList[i] = RES[section][i];
    }
    return i;
}
Editor is loading...