Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
4
Indexable
Never
#define N 1000001
//#include <iostream>
 
//using namespace std;
class Node {
public:
    int id;
    int team;
     
    Node *next,*prev;
 
};
Node a[N];
 
void connect(Node *a,Node *b)
{
    a->next = b;
    b->prev = a;
}
 
 
class LL{
     
  
 
public:
    static void erase(Node *node)
    {
        connect(node->prev, node->next);
    }
    Node *head,*tail;
    LL() {
        head= new Node();
        tail = new Node();
        connect(head, tail);
    }
    void add(Node *cur)
    {
        Node *p =tail->prev;
        connect(p,cur);
        connect(cur, tail);
    }
     
    bool isEmpty(){
        return head->next == tail;
    }
 
    void merge2(LL *link2) {
        if(link2->isEmpty())return;
        Node *n2 = link2->head->next;
        Node *p2 = link2->tail->prev;
        Node *p1 = tail->prev;
        connect(p1,n2);
        connect(p2,tail);
        connect(link2->head, link2->tail);
 
        // if (link2->isEmpty())return;
        //connect(tail->prev, link2->head->next);
        //connect(link2->tail->prev, tail);
        //list->initialize();
    }
 
    void printAll(){
        Node  *cur = head->next;
        while(cur !=tail)
        {
            //cout<<cur->id <<" ";
            cur = cur->next;
        }
        //cout<<"\n";
    }
};
 
//khoi tao 
 
LL ll[6][6];
void init()
{
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=5;j++){
        ll[i][j].head= new Node();
        ll[i][j].tail = new Node();
        connect(ll[i][j].head, ll[i][j].tail);
        }
    }
 
}
// them linh mID , thuoc team mTeam, danh tieng = mScore
 
void hire(int mID, int mTeam, int mScore)
{
     
    a[mID].id = mID;
    a[mID].team = mTeam;
    ll[mTeam][mScore].add(a+mID);
    //ll[mTeam][mScore].printAll();
}
// loai linh mID
void fire(int mID)
{
    LL::erase(a+mID);
    //cout<<"xoa "<<mID<<"\n";
}
// sua danh tieng cua linh mID thanh mScore
void updateSoldier(int mID, int mScore)
{
    LL::erase(a+mID);
    //a[mID].data = mScore;
    ll[a[mID].team][mScore].add(a+mID);
    //cout<<"sua diem cua "<<mID<<" thanh "<<mScore<<"\n";
}
// sua danh tieng cua team mTeam them mChangeScore ( tu 1=>5)
void updateTeam(int mTeam, int mChangeScore)
{
    //cout<<"update team\n";
    if(mChangeScore<0){
    for(int i=1;i<=5;i++)if(!ll[mTeam][i].isEmpty()){
        int u= i + mChangeScore;
        if(u>5)u=5;else if(u<1)u=1;
        if(u!=i)ll[mTeam][u].merge2(&ll[mTeam][i]);
        //cout<<"diem "<<u<<": ";
        //ll[mTeam][u].printAll();
    }
    }else if(mChangeScore>0)
    {
        for(int i=5;i>=1;i--)if(!ll[mTeam][i].isEmpty()){
        int u= i + mChangeScore;
        if(u>5)u=5;else if(u<1)u=1;
        if(u!=i)ll[mTeam][u].merge2(&ll[mTeam][i]);
        //cout<<"diem "<<u<<": ";
        //ll[mTeam][u].printAll();
 
    }
    }
     
}
// dua ra id cua linh co danh tieng cao nhat
int bestSoldier(int mTeam)
{
    int maxScore=0;
     
    for (int i = 5; i >= 1; i--)
    {
        if (!ll[mTeam][i].isEmpty())
        {
            maxScore = i;
            break;
        }
    }
  
    int mID = 0;
    if(maxScore){
    Node *cur = ll[mTeam][maxScore].head->next;
    while (cur != ll[mTeam][maxScore].tail)
    {
        if (mID < (cur->id))
            mID = cur->id;
        cur = cur->next;
    }
    }
    //cout<<"best team"<<mTeam<<" la "<<mID<<"\n";
    return mID;
}