Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.5 kB
9
Indexable
Never
//Main

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>

#define CMD_INIT 1
#define CMD_HIRE 2
#define CMD_FIRE 3
#define CMD_UPDATE_SOLDIER 4
#define CMD_UPDATE_TEAM 5
#define CMD_BEST_SOLDIER 6

extern void init();
extern void hire(int mID, int mTeam, int mScore);
extern void fire(int mID);
extern void updateSoldier(int mID, int mScore);
extern void updateTeam(int mTeam, int mChangeScore);
extern int bestSoldier(int mTeam);

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

static bool run()
{
    int numQuery;

    int mID, mTeam, mScore, mChangeScore;

    int userAns, ans;

    bool isCorrect = false;

    scanf("%d", &numQuery);

    for (int i = 0; i < numQuery; ++i)
    {
        int cmd;
        scanf("%d", &cmd);
        switch (cmd)
        {
        case CMD_INIT:
            init();
            isCorrect = true;
            break;
        case CMD_HIRE:
            scanf("%d %d %d", &mID, &mTeam, &mScore);
            hire(mID, mTeam, mScore);
            break;
        case CMD_FIRE:
            scanf("%d", &mID);
            fire(mID);
            break;
        case CMD_UPDATE_SOLDIER:
            scanf("%d %d", &mID, &mScore);
            updateSoldier(mID, mScore);
            break;
        case CMD_UPDATE_TEAM:
            scanf("%d %d", &mTeam, &mChangeScore);
            updateTeam(mTeam, mChangeScore);
            break;
        case CMD_BEST_SOLDIER:
            scanf("%d", &mTeam);
            userAns = bestSoldier(mTeam);
            scanf("%d", &ans);
            if (userAns != ans)
            {
                isCorrect = false;
            }
            break;
        default:
            isCorrect = false;
            break;
        }
    }

    return isCorrect;
}

int main()
{
    setbuf(stdout, NULL);
    freopen("input.txt", "r", stdin);

    int T, MARK;
    scanf("%d %d", &T, &MARK);

    for (int tc = 1; tc <= T; tc++)
    {
        int score = run() ? MARK : 0;
        printf("#%d %d\n", tc, score);
    }

    return 0;
}

//User

#include <iostream>

using namespace std;

struct Soldier{
    int id;
    int team;
    Soldier *prev, *next;
    Soldier(){
        prev=nullptr;
        next=nullptr;
    }
};

void connect(Soldier *a, Soldier *b){
    a->next=b;
    b->prev=a;
}

struct Linklist{
    Soldier *head, *tail; //dau - duoi ao
    Linklist(){
        head=nullptr;
        tail=nullptr;
    }
};
 
Linklist ll[6][6];
Soldier soldier[100001];

void merge(Linklist a, Linklist b){
	connect(a.tail->prev, b.head->next);
	connect(b.tail->prev, a.tail);
	connect(b.head, b.tail);
}

void init()
{
	for(int i=1;i<=5;i++){
		for(int j=1;j<=5;j++){
            ll[i][j].head=new Soldier();
            ll[i][j].tail=new Soldier();
            connect(ll[i][j].head, ll[i][j].tail);
        }
	}
}

void hire(int mID, int mTeam, int mScore)
{
	Soldier* a = &soldier[mID];
	a->id = mID;
	a->team = mTeam;
	soldier[mID] = *a;

	connect(ll[mTeam][mScore].tail->prev, &soldier[mID]);
	connect(&soldier[mID], ll[mTeam][mScore].tail);
}

void fire(int mID)
{
	Soldier* a = &soldier[mID];
	connect(a->prev, a->next);
	a->prev = NULL;
	a->next = NULL;
	a = NULL;
	delete a;
}

void updateSoldier(int mID, int mScore)
{
	Soldier* a = &soldier[mID];
	connect(a->prev, a->next);

	int mTeam = a->team;

	connect(ll[mTeam][mScore].tail->prev, &soldier[mID]);
	connect(&soldier[mID], ll[mTeam][mScore].tail);
}

void updateTeam(int mTeam, int mChangeScore)
{
	 if(mChangeScore<0){
        for(int score=1;score<6;score++){
            if(score==1) continue;
            int a=score+mChangeScore;
            if(a<=1){
                merge(ll[mTeam][1],ll[mTeam][score]);
            }
            else merge(ll[mTeam][a],ll[mTeam][score]);
        }
    }
    if(mChangeScore>0){
        for(int score=5;score>0;score--){
            if(score==5) continue;
            int a=score+mChangeScore;
            if(a>=5){
                merge(ll[mTeam][5],ll[mTeam][score]);
            }
            else merge(ll[mTeam][a],ll[mTeam][score]);
        }
    }
}

int bestSoldier(int mTeam)
{
	for(int i = 5; i > 0; i--){
		if(ll[mTeam][i].head->next == ll[mTeam][i].tail)
			continue;
		int maxId=0;
		Soldier* p = ll[mTeam][i].head->next;
		while(p != ll[mTeam][i].tail){
			if(maxId < p->id)
				maxId = p->id;
			p = p->next;
		}
		return maxId;
	}
    return 0;
}