Untitled
unknown
plain_text
2 years ago
48 kB
41
Indexable
[Nguyễn Văn Vang / Nguyen Van Vang]
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class student{
public:
string name;
int id;
student(string name1, int id1){
name=name1;
id=id1;
}
};
int main(){
unordered_map<string,student *> mymap;
student s1("abc",1);
student s2("bcd",2);
//them
mymap["abc"]=&s1;
mymap["bcd"]=&s2;
//tim
//unordered_map<string,student *>::iterator it;
auto it = mymap.find("abc");
if(it==mymap.end()){
//ko thay
cout<<"ko thay"<<endl;
}
else{
student *s=it->second;
cout<<"id: "<<s->id<<endl;
}
//xoa
mymap.erase(s2.name);
auto it1 = mymap.find(s2.name);
if(it1==mymap.end()){
//ko thay
cout<<"ko thay"<<endl;
}
else{
student *s=it1->second;
cout<<"id: "<<s->id<<endl;
}
return 0;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:25
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
extern void init(int N);
extern void addProduct(int mPrice, int tagNum, char tagName[][10]);
extern int buyProduct(char tag1[], char tag2[], char tag3[]);
extern void adjustPrice(char tag1[], int changePrice);
/////////////////////////////////////////////////////////////////////////
#define INIT 0
#define ADD 1
#define BUY 2
#define ADJ 3
static void mstrcpy(char dst[], const char src[]) {
int c = 0;
while ((dst[c] = src[c]) != '\0') ++c;
}
static int mstrcmp(const char str1[], const char str2[]) {
int c = 0;
while (str1[c] != '\0' && str1[c] == str2[c]) ++c;
return str1[c] - str2[c];
}
static bool run()
{
int N, cmd, ans, ret, tnum, price;
char tag[5][10];
int Q = 0;
bool okay = false;
ret = ans = 0;
okay = false;
scanf("%d", &Q);
for (int i = 0; i < Q; ++i)
{
scanf("%d", &cmd);
switch (cmd)
{
case INIT:
scanf("%d", &N);
init(N);
okay = true;
break;
case ADD:
scanf("%d %d", &price, &tnum);
for (int m = 0; m < tnum; m++) {
scanf("%s", tag[m]);
}
addProduct(price, tnum, tag);
break;
case BUY:
scanf("%d", &ans);
for (int m = 0; m < 3; m++) {
scanf("%s", tag[m]);
}
ret = buyProduct(tag[0], tag[1], tag[2]);
if (ans != ret) {
okay = false;
}
break;
case ADJ:
scanf("%s %d", tag[0], &price);
adjustPrice(tag[0], price);
break;
default:
okay = false;
}
}
return okay;
}
int main()
{
setbuf(stdout, NULL);
//freopen("sample_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;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:25
note pad
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:26
#include<iostream>
using namespace std;
class Node {
public:
char data;
Node *pre;
Node *next;
};
typedef class Node *node;
node creatNewnode(char c) {
node temp = new Node();
temp->data = c;
temp->next = NULL;
temp->pre = NULL;
return temp;
}
void Linknode(node front, node end) {
front->next = end;
end->pre = front;
}
void insertNode(node front, node p, node end) {
Linknode(front, p);
Linknode(p, end);
}
void removeNode(node p) {
Linknode(p->pre, p->next);
}
class cur {
public:
int row;
int col;
}mouse;
int h, w;
int line;
class List {
public:
Node *phead;
Node *ptail;
int size;
int cnt[29];
}list[301];
void init(int H, int W, char mStr[]) {
h = H;
w = W;
mouse.col = mouse.row = 1;
for (int i = 1; i <= h; i++) {
list[i].size = 0;
list[i].phead = new Node();
list[i].ptail = new Node();
Linknode(list[i].phead, list[i].ptail);
}
for (int i = 1; i <= h; i++) {
for (int j = 0; j < 29; j++) {
list[i].cnt[j] = 0;
}
}
line = 1;
for (int i = 0; mStr[i] != 0; i++) {
node temp = creatNewnode(mStr[i]);
insertNode(list[line].ptail->pre, temp, list[line].ptail);
list[line].cnt[mStr[i] - 'a']++;
list[line].size++;
if (list[line].size >= w) {
line++;
}
}
}
void insert(char mChar) {
node temp = creatNewnode(mChar);
int ha = mouse.row;
int cot = mouse.col;
node cur = list[ha].phead->next;
for (int i = 1; i < cot; i++) {
cur = cur->next;
}
insertNode(cur->pre, temp, cur);
mouse.col++;
list[ha].size++;
list[ha].cnt[mChar - 'a']++;
while (list[ha].size > w) {
node temp = list[ha].ptail->pre;
Linknode(temp->pre, temp->next);
list[ha].size--;
list[ha].cnt[temp->data - 'a']--;
ha++;
node cur = list[ha].phead;
insertNode(cur, temp, cur->next);
list[ha].size++;
list[ha].cnt[temp->data - 'a']++;
}
if (ha > line) {
line = ha;
}
if (mouse.col > w) {
mouse.row++;
mouse.col = 1;
}
}
char moveCursor(int mRow, int mCol) {
if (mRow > line || (mRow == line && mCol > list[line].size)) {
mouse.row = line;
mouse.col = list[line].size + 1;
return '$';
}
mouse.col = mCol;
mouse.row = mRow;
node cur = list[mRow].phead->next;
for (int i = 1; i < mCol; i++) {
cur = cur->next;
}
return cur->data;
}
int countCharacter(char mChar) {
int res = 0;
for (int i = mouse.row; i <= line; i++) {
res += list[i].cnt[mChar - 'a'];
}
node temp = list[mouse.row].phead->next;
for (int i = 1; i < mouse.col; i++) {
if (temp->data == mChar) {
res--;
}
temp = temp->next;
}
return res;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:26
number baseball
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:26
#define N 4
typedef class
{
public:
int strike;
int ball;
} Result;
int A[6000][4];
int Count;
extern Result query(int guess[]);
Result myquery(int guess[], int dest[]){
Result result;
result.strike=0;
result.ball=0;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if (i == j && guess[i] == dest[j])
result.strike++;
else if(i != j && guess[i] == dest[j])
result.ball++;
}
}
return result;
}
void mycopy(int src[], int dest[]){
for(int i = 0; i < N; i++){
dest[i] = src[i];
}
}
void init(){
int a,b,c,d;
Count = 0;
for (a=0;a<=9;a++){
for (b=0;b<=9;b++){
for (c=0;c<=9;c++){
for (d=0;d<=9;d++){
if ((a!=b)&&(a!=c)&&(a!=d)&&(b!=c)&&(b!=d)&&(c!=d)){
A[Count][0]=a;
A[Count][1]=b;
A[Count][2]=c;
A[Count][3]=d;
Count ++;
}
}
}
}
}
}
void doUserImplementation(int inputD[])
{
Result res, res1;
init();
while(Count>0){
int countg = 0;
mycopy(A[0], inputD);
res = query(inputD);
if(res.strike==4){
return;
}
for(int i=1;i<Count;i++){
res1 = myquery(inputD,A[i]);
if(res.strike==res1.strike&&res.ball==res1.ball){
mycopy(A[i],A[countg]);
countg++;
}
}
Count = countg;
}
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:26
2hand market
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:26
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct product
{
int price;
bool isDel;
};
product prd[30005];
unordered_map<string,vector<int>> umap;
int tagid;
void init(int N)
{
umap.clear();
tagid = 0;
}
void addProduct(int mPrice, int tagNum, char tagName[][10])
{
prd[tagid].price = mPrice;
prd[tagid].isDel = false;
vector<string> TohopTag;
for (int i = 0; i < tagNum; i++)
{
TohopTag.push_back(tagName[i]);
umap[tagName[i]].push_back(tagid);
}
sort(TohopTag.begin(), TohopTag.end());
for (int i = 0; i < tagNum; i++){
for (int j = i+1; j < tagNum; j++){
for (int k = j+1; k < tagNum; k++)
{
string longtag = TohopTag[i] + " " + TohopTag[j] + " " + TohopTag[k];
umap[longtag].push_back(tagid);
}
}
}
tagid++;
}
int buyProduct(char tag1[], char tag2[], char tag3[])
{
vector<string> TohopTag;
TohopTag.push_back(tag1);
TohopTag.push_back(tag2);
TohopTag.push_back(tag3);
sort(TohopTag.begin(), TohopTag.end());
string longTag = TohopTag[0] + " " + TohopTag[1] + " " + TohopTag[2];
int prdSelected = -1;
for (auto it:umap[longTag])
{
if (prd[it].isDel == false)
{
if (prdSelected == -1 || prd[it].price < prd[prdSelected].price)
{
prdSelected = it;
}
}
}
if (prdSelected == -1) return -1;
prd[prdSelected].isDel = true;
return prd[prdSelected].price;
}
void adjustPrice(char tag1[], int changePrice)
{
for (auto it:umap[tag1])
{
prd[it].price += changePrice;
}
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:27
soldier
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:27
#define MAXSIZE 100001
#define NULL 0
class Soldier
{
public:
int ID, teamID, Score;
Soldier *next, *prev;
Soldier(int id, int team, int score) {
ID = id;
teamID = team;
Score = score;
next = prev = nullptr;
}
};
class DoubleLL
{
public:
Soldier *head, *tail;
DoubleLL()
{
head = new Soldier(-1, -1, -1);
tail = new Soldier(-1, -1, -1);
connect(head, tail);
}
void connect(Soldier *s1, Soldier *s2)
{
s1->next = s2;
s2->prev = s1;
}
void insSoldier(Soldier *newSol)
{
connect(newSol, head->next);
connect(head, newSol);
}
};
class Team
{
public:
DoubleLL *arrScore[6];
Team()
{
for(int i=1; i<=5; i++) {
arrScore[i] = new DoubleLL();
}
}
void hideSol(Soldier *newSol, int score)
{
arrScore[score]->insSoldier(newSol);
}
void connectSol(Soldier *s1, Soldier *s2)
{
s1->next = s2;
s2->prev = s1;
}
void updateTeam(int score)
{
int deltaScore = 0;
if(score > 0) {
for(int i = 4; i >= 1; i--) {
if(arrScore[i]->head->next != arrScore[i]->tail) {
deltaScore = (i + score) > 5 ? 5 : (i + score);
connectSol(arrScore[deltaScore]->tail->prev, arrScore[i]->head->next);
connectSol(arrScore[i]->tail->prev, arrScore[deltaScore]->tail);
connectSol(arrScore[i]->head, arrScore[i]->tail);
}
}
} else if(score < 0) {
for(int i = 2; i <= 5; i++) {
if(arrScore[i]->head->next != arrScore[i]->tail) {
deltaScore = (i + score) < 1 ? 1 : (i + score);
connectSol(arrScore[deltaScore]->tail->prev, arrScore[i]->head->next);
connectSol(arrScore[i]->tail->prev, arrScore[deltaScore]->tail);
connectSol(arrScore[i]->head, arrScore[i]->tail);
}
}
}
}
};
Team team[6];
Soldier *soldier[MAXSIZE];
void init()
{
for(int i = 1; i <= 5; i++)
team[i] = Team();
}
void hire(int mID, int mTeam, int mScore)
{
Soldier *sol = new Soldier(mID, mTeam, mScore);
team[mTeam].hideSol(sol, mScore);
soldier[mID] = sol;
}
void fire(int mID)
{
soldier[mID]->prev->next = soldier[mID]->next;
soldier[mID]->next->prev = soldier[mID]->prev;
}
void updateSoldier(int mID, int mScore)
{
fire(mID);
soldier[mID]->Score = mScore;
team[soldier[mID]->teamID].hideSol(soldier[mID], mScore);
}
void updateTeam(int mTeam, int mChangeScore)
{
team[mTeam].updateTeam(mChangeScore);
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 7/28/2023 17:27
int bestSoldier(int mTeam)
{
int res = -1;
for(int i = 5; i >= 1; i--) {
if(team[mTeam].arrScore[i]->head->next != team[mTeam].arrScore[i]->tail) {
Soldier *temp = team[mTeam].arrScore[i]->head->next;
res = temp->ID;
while(temp != team[mTeam].arrScore[i]->tail) {
if(temp->ID > res)
res = temp->ID;
temp = temp->next;
}
break;
}
}
return res;
}
>> Monday, July 31, 2023
[Nguyễn Văn Vang / Nguyen Van Vang] 7/31/2023 17:39
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <cstring>
using namespace std;
#define MAX_N 100002
#define MAX_GRP 11
#define MAX_JOB 1001
struct PASAGE {
int id;
int point;
int groupid;
};
struct COM_UP {
bool operator () (const PASAGE a, const PASAGE b) {
if (a.point == b.point) {
return a.id < b.id;
}
return a.point > b.point;
}
};
struct COM_DOWN {
bool operator () (const PASAGE a, const PASAGE b) {
if (a.point == b.point) {
return a.id > b.id;
}
return a.point < b.point;
}
};
priority_queue<PASAGE, vector<PASAGE>, COM_UP> heap_small[MAX_GRP];
priority_queue<PASAGE, vector<PASAGE>, COM_DOWN> heap_big[MAX_GRP];
PASAGE org_vec[MAX_N];
vector<int> job_psgid[MAX_JOB];
int m_grougs;
void updataQueue(const int id) {
heap_big[org_vec[id].groupid].emplace(org_vec[id]);
heap_small[org_vec[id].groupid].emplace(org_vec[id]);
}
void init(int N, int M, int J, int mPoint[], int mJobID[])
{
for (int i = 0; i < J; i++) {
job_psgid[i].clear();
}
for (int i = 0; i < N / M; i++) {
heap_big[i] = priority_queue<PASAGE, vector<PASAGE>, COM_DOWN>();
heap_small[i] = priority_queue<PASAGE, vector<PASAGE>, COM_UP>();
}
[Nguyễn Văn Vang / Nguyen Van Vang] 7/31/2023 17:39
m_grougs = N / M;
for (int i = 0; i < N; i++) {
org_vec[i] = { i, mPoint[i], i / M };
updataQueue(i);
job_psgid[mJobID[i]].push_back(i);
}
}
void destroy()
{
}
int update(int mID, int mPoint)
{
org_vec[mID].point += mPoint;
updataQueue(mID);
return org_vec[mID].point;
}
int updateByJob(int mJobID, int mPoint)
{
int ret_val = 0;
for (const auto id : job_psgid[mJobID]) {
org_vec[id].point += mPoint;
updataQueue(id);
ret_val += org_vec[id].point;
}
return ret_val;
}
[Nguyễn Văn Vang / Nguyen Van Vang] 7/31/2023 17:39
int move(int mNum)
{
vector<int> up_vecs[MAX_GRP], down_vecs[MAX_GRP];
// take num biggest value
bool flag = false;
for (int g = 1; g < m_grougs; g++) {
int num = 0;
while (num < mNum) {
const auto heap_top = heap_big[g].top();
heap_big[g].pop();
if (heap_top.point != org_vec[heap_top.id].point || heap_top.groupid != org_vec[heap_top.id].groupid) {
continue;
}
flag = false;
for (auto t : up_vecs[g]) {
if (t == heap_top.id) {
flag = true;
break;
}
}
if (flag) continue;
up_vecs[g].push_back(heap_top.id);
num++;
}
}
//take num less value
for (int g = 0; g < m_grougs - 1; g++) {
int num = 0;
while (num < mNum) {
const auto heap_top = heap_small[g].top();
heap_small[g].pop();
if (heap_top.point != org_vec[heap_top.id].point || heap_top.groupid != org_vec[heap_top.id].groupid) {
continue;
}
flag = false;
for (auto t : down_vecs[g]) {
if (t == heap_top.id) {
flag = true;
break;
}
}
if (flag) continue;
down_vecs[g].push_back(heap_top.id);
num++;
}
}
// move big(up_vecs) to less
int ret_val = 0;
for (int g = 1; g < m_grougs; g++) {
for (auto up : up_vecs[g]) {
org_vec[up].groupid = g - 1;
updataQueue(up);
ret_val += org_vec[up].point;
}
}
for (int g = 0; g < m_grougs - 1; g++) {
for (auto down : down_vecs[g]) {
org_vec[down].groupid = g + 1;
updataQueue(down);
ret_val += org_vec[down].point;
}
}
return ret_val;
}
>> Tuesday, August 1, 2023
[Nguyễn Văn Vang / Nguyen Van Vang] 8/1/2023 10:51
tree set
[Nguyễn Văn Vang / Nguyen Van Vang] 8/1/2023 10:51
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <set>
using namespace std;
class Node {
public:
int id;
int score;
Node(int i, int s) {
id = i;
score = s;
}
};
class CmpNode {
public:
bool operator() (const Node *n1, const Node *n2) {
if (n2->score != n1->score) return n1->score < n2->score;
return n1->id < n2->id;
}
};
int main() {
set<Node *, CmpNode> p;
Node n1(1, 22);
Node n2(2, 33);
Node n3(3, 77);
Node n4(4, 77);
p.insert(&n1);
p.insert(&n2);
p.insert(&n3);
p.insert(&n4);
//p.erase(&n4);
n4.score = 11;
//p.insert(&n4);
for (Node *tp : p) {
printf("%d %d\n", tp->id, tp->score);
}
printf("\n\n");
/*
p.erase();
p.find();
p.upper_bound();
p.lower_bound();
p.begin();
p.end();
p.size();
*/
Node n(2, 33);
auto it = p.upper_bound(&n);
it--;
printf("%d_%d\n", (*it)->id, (*it)->score);
return 0;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 8/1/2023 16:45
#include <iostream>
#include <unordered_map>
#include <set>
using namespace std;
#define maxN 200000000
class node
{
public:
int st;
int value;
bool is_empty;
};
class cmp
{
public:
bool operator()(const node *n1,const node *n2)
{
if(n1->value != n2->value)return n1->value < n2->value;
return n1->st < n2->st;
}
};
unordered_map < int , node *> mymap;
set <node *, cmp> myset;
void init(int N) {
mymap.clear();
myset.clear();
node *newNode = new node();
newNode->st = 0;
newNode->value = N;
newNode->is_empty = true;
myset.insert(newNode);
return;
}
int allocate(int mSize) {
/*for (node *tp : myset) {
printf("%d %d\n", tp->st, tp->value);
}
printf("\n\n");*/
//cout<<"them "<<mSize<<" vao vi tri ";
node *newNode = new node();
newNode->value = mSize;
newNode->st= -1;
//newNode->is_empty = false;
auto it = myset.upper_bound(newNode);
if(it!= myset.end()){
int start = (*it)->st;
int kc = (*it)->value;
//cout<<start<<"-\n";
newNode->st = start;
newNode->value = mSize;
mymap[start]= newNode;
myset.erase(*it);
if(mSize < kc ){
node *newNod = new node();
newNod->st = start + mSize;
newNod->value = kc -mSize;
myset.insert(newNod);
}
return start;
}
return -1;
}
int release(int mAddr) {
auto it = mymap.find(mAddr);
if(it == mymap.end())return -1;
int ans = (it->second)->value;
//cout<<mAddr<<" "<<(it->second)->st<<" "<<ans<<"+\n";
node *newN = new node();
newN->st = (it->second)->st;
newN->value = ans;
mymap.erase(it);
int dau = newN->st,cuoi = dau + ans - 1;
node *bef = new node();
node *aft = new node();
for (node *tp : myset) {
if(tp->st< dau && tp->st + tp->value == dau)
{
dau = tp->st;
bef = tp;
}
if(tp->st == cuoi+1)
{
cuoi = tp->st + tp->value -1;
aft = tp;
}
//printf("%d %d\n", tp->st, tp->value);
}
newN->st = dau;
newN->value = cuoi-dau+1;
myset.erase(bef);
myset.erase(aft);
myset.insert(newN);
return ans;
}
>> Wednesday, August 2, 2023
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:44
#include<set>
#include<unordered_map>
using namespace std;
struct loc
{
int start;
int end;
};
struct comperator
{
bool operator()(const struct loc &a, const struct loc &b)
{
if ((a.end - a.start) > (b.end - b.start))
{
return true;
}
else if ((a.end - a.start) == (b.end - b.start) && (a.start < b.start))
{
return true;
}
else
{
return false;
}
}
};
set<struct loc, struct comperator> alllockers;
set<int> alreadyAssigned;
unordered_map<int, int> locker_to_mid;
int numOfLockers;
int alreadyallocated;
int getLockerId(int start, int end)
{
if (start == 1)
{
return start;
}
else if (end == numOfLockers)
{
return end;
}
else
{
return (start + (end - start) / 2);
}
}
void init(int N) {
alreadyallocated = 0;
numOfLockers = N;
alllockers.clear();
alreadyAssigned.clear();
locker_to_mid.clear();
alllockers.insert({ 1,numOfLockers });
alreadyAssigned.insert(0);
alreadyAssigned.insert(numOfLockers + 1);
return;
}
bool isValidInsertion(int leftIdx, int rightIdx)
{
if (leftIdx > rightIdx)
{
return false;
}
return true;
}
int arrive(int mId) {
auto itr = alllockers.begin();
int lockerId = getLockerId(itr->start,itr->end);
if (isValidInsertion(itr->start, lockerId - 1))
{
alllockers.insert({ itr->start, lockerId - 1 });
}
if (isValidInsertion(lockerId + 1, itr->end ))
{
alllockers.insert({ lockerId + 1, itr->end });
}
alllockers.erase(itr);
alreadyAssigned.insert(lockerId);
locker_to_mid[mId] = lockerId;
return lockerId;
}
int leave(int mId) {
int lockerId = locker_to_mid[mId];
auto itr = alreadyAssigned.find(lockerId);
int priviousLoc = *(--itr);
itr++;
int nextLoc = *(++itr);
alllockers.erase({priviousLoc + 1, lockerId - 1});
alllockers.erase({lockerId + 1, nextLoc - 1});
if(isValidInsertion(priviousLoc + 1,nextLoc - 1)) {
alllockers.insert({ priviousLoc + 1,nextLoc - 1 });
}
alreadyAssigned.erase(lockerId);
locker_to_mid.erase(mId);
return (numOfLockers - alreadyAssigned.size() + 2);
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:49
locker
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:52
#include <iostream>
#include <unordered_map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
int N;
int emp;
class node
{
public:
int st;
int fi;
int value;
bool del;
node()
{
del = false;
}
};
class cmp{
public:
bool operator()(const node *n1, const node *n2)
{
if(n1->value != n2->value) return n1->value < n2->value;
return n1->st > n2->st;
}
};
class cmp2{
public:
bool operator()(const node *n1, const node *n2)
{
if(n1->st !=n2->st)
return n1->st < n2->st;
return n1->fi < n2->fi;
}
};
unordered_map <int , int> mymap;
priority_queue < node*,vector<node *>, cmp> myset;
set <node *, cmp2> myset2;
void init(int N) {
::N = N;
emp = N;
while(!myset.empty())myset.pop();
mymap.clear();
myset2.clear();
node *newNode = new node();
newNode->st = 0;
newNode->fi = N+1;
newNode->value = N;
myset.push(newNode);
myset2.insert(newNode);
return;
}
node *x = new node();
int arrive(int mId) {
emp--;
node *tp = myset.top();
myset.pop();
while(tp->del)
{
tp = myset.top();
myset.pop();
}
int u = tp->st;
int v = tp->fi;
myset2.erase(tp);
if(u==0)
{
mymap[mId]= 1;
if(tp->fi != 2){
tp->st++;
tp->value--;
myset.push(tp);
myset2.insert(tp);
}
return 1;
}else if(v== N+1)
{
mymap[mId]= N;
if(tp->st != N-1){
tp->fi--;
tp->value--;
myset.push(tp);
myset2.insert(tp);
}
return N;
}else
{
int mid = (u+v)/2;
mymap[mId]= mid;
if(mid - u -1 >0){
node *newNode = new node();
newNode->st = tp->st;
newNode->fi = mid;
newNode->value = mid - u -1;
myset.push(newNode);
myset2.insert(newNode);
}
if(v - mid - 1 > 0){
node *newNode = new node();
newNode->st = mid;
newNode->fi = v;
newNode->value = v- mid -1 ;
myset.push(newNode);
myset2.insert(newNode);
}
return mid;
}
}
int leave(int mId) {
emp++;
int u = mymap[mId];
mymap.erase(mId);
x->st = u;
x->fi = u;
auto it = myset2.upper_bound(x);
node *cur = new node();
cur->st = u-1;
cur->fi = u+1;
if(it != myset2.end() && (*it)->st == u)
{
cur->fi = (*it)->fi;
(*it)->del = true;
myset2.erase(it);
}
it = myset2.upper_bound(x);
if(it != myset2.begin() )
{
it--;
if((*it)->fi == u)
{
cur->st = (*it)->st;
(*it)->del = true;
myset2.erase(it);
}
}
cur->value = cur->fi - cur->st -1;
myset.push(cur);
myset2.insert(cur);
return emp;
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:52
#include <set>
#include <unordered_map>
using namespace std;
struct compare_section
{
bool operator()(pair<int, int> p1, pair<int, int> p2){
if(p1.second-p1.first == p2.second-p2.first){
return p1.first < p2.first;
}
return p1.second-p1.first > p2.second-p2.first;
}
};
set <pair<int, int>, compare_section> section;
set <int> locker;
unordered_map<int, int> map_id;
int n_total;
int cnt_user;
void init(int N) {
n_total = N;
section.clear();
locker.clear();
map_id.clear();
section.insert(make_pair(1, N)); //trong tu 1 -> N;
locker.insert(0);
locker.insert(N+1);
return;
}
int arrive(int mId) {
auto it = section.begin();
int st = it->first;
int end = it->second;
if(it->first == 1){
map_id[mId] = 1;
section.erase(make_pair(1,end));
section.insert(make_pair(2, end));
locker.insert(1);
return 1;
}
else if(it->second == n_total){
map_id[mId] = n_total;
section.erase(make_pair(st,n_total));
section.insert(make_pair(st, n_total - 1));
locker.insert(n_total);
return n_total;
}
int post_lock = (it->second + it->first) >> 1;
map_id[mId] = post_lock;
section.insert(make_pair(it->first, post_lock-1));
section.insert(make_pair(post_lock+1, it->second));
section.erase(it);
locker.insert(post_lock);
return post_lock;
}
int leave(int mId) {
int lock_id = map_id[mId];
auto it = locker.find(lock_id);
int next_post = *(++it);
int pre_post = *(--(--it));
locker.erase(++it);
section.erase(make_pair(pre_post + 1, lock_id - 1));
section.erase(make_pair(lock_id + 1, next_post - 1));
section.insert(make_pair(pre_post + 1, next_post - 1));
return n_total - locker.size() + 2;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 8/2/2023 14:55
#include <iostream>
#include <set>
#include <unordered_map>
using namespace std;
const int MAXNODE = 40005;
class Node {
public:
int start, size;
void init(int st, int sz) {
start = st; size = sz;
}
};
Node pool[MAXNODE];
int cnt;
Node *getNode(int st, int sz) {
pool[cnt].init(st, sz);
return (pool + cnt++);
}
class CmpSize {
public:
bool operator() (const Node *n1, const Node *n2) const {
if (n1->size != n2->size) return n1->size > n2->size;
return n1->start < n2->start;
}
};
class CmpStart {
public:
bool operator() (const Node *n1, const Node *n2) const {
return n1->start < n2->start;
}
};
int N;
unordered_map<int, int> hashLocker;
set<Node *, CmpSize> treeSize;
set<Node *, CmpStart> treeStart;
void init(int N) {
cnt = 0;
::N = N;
hashLocker.clear();
treeSize.clear();
treeStart.clear();
Node *n = getNode(1, N);
treeSize.insert(n);
treeStart.insert(n);
return;
}
int arrive(int mId) {
Node *maxSpace = *(treeSize.begin());
treeSize.erase(maxSpace);
treeStart.erase(maxSpace);
if (maxSpace->start == 1) {
// pick locker at first
hashLocker[mId] = 1;
maxSpace->start = 2;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return 1;
}
if (maxSpace->start + maxSpace->size - 1 == N) {
// pick locker at last
hashLocker[mId] = N;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return N;
}
{
// pick locker at the middle of space
int indexLocker = maxSpace->start + (maxSpace->size - 1) / 2;
hashLocker[mId] = indexLocker;
Node *rightSpace = getNode(indexLocker + 1, maxSpace->start + maxSpace->size - (indexLocker + 1));
maxSpace->size = indexLocker - maxSpace->start;
if (maxSpace->size > 0) {
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
}
if (rightSpace->size > 0) {
treeSize.insert(rightSpace);
treeStart.insert(rightSpace);
}
return indexLocker;
}
return 0;
}
int leave(int mId) {
int indexLocker = hashLocker[mId];
hashLocker.erase(mId);
Node *delNode;
Node *newNode = getNode(indexLocker, 1);
Node tp;
tp.init(indexLocker, 0);
auto it = treeStart.lower_bound(&tp);
if (it != treeStart.end()) {
delNode = (*it);
if (newNode->start + newNode->size == delNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->size += delNode->size;
}
}
auto it2 = treeStart.lower_bound(&tp);
if (it2 != treeStart.begin()) {
it2--;
delNode = (*it2);
if (delNode->start + delNode->size == newNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->start = delNode->start;
newNode->size += delNode->size;
}
}
treeSize.insert(newNode);
treeStart.insert(newNode);
return N - hashLocker.size();
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:56
#include <iostream>
#include <set>
#include <unordered_map>
using namespace std;
const int MAXNODE = 40005;
class Node {
public:
int start, size;
void init(int st, int sz) {
start = st; size = sz;
}
};
Node pool[MAXNODE];
int cnt;
Node *getNode(int st, int sz) {
pool[cnt].init(st, sz);
return (pool + cnt++);
}
class CmpSize {
public:
bool operator() (const Node *n1, const Node *n2) const {
if (n1->size != n2->size) return n1->size > n2->size;
return n1->start < n2->start;
}
};
class CmpStart {
public:
bool operator() (const Node *n1, const Node *n2) const {
return n1->start < n2->start;
}
};
int N;
unordered_map<int, int> hashLocker;
set<Node *, CmpSize> treeSize;
set<Node *, CmpStart> treeStart;
void init(int N) {
cnt = 0;
::N = N;
hashLocker.clear();
treeSize.clear();
treeStart.clear();
Node *n = getNode(1, N);
treeSize.insert(n);
treeStart.insert(n);
return;
}
int arrive(int mId) {
Node *maxSpace = *(treeSize.begin());
treeSize.erase(maxSpace);
treeStart.erase(maxSpace);
if (maxSpace->start == 1) {
// pick locker at first
hashLocker[mId] = 1;
maxSpace->start = 2;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return 1;
}
if (maxSpace->start + maxSpace->size - 1 == N) {
hashLocker[mId] = N;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return N;
}
{
int indexLocker = maxSpace->start + (maxSpace->size - 1) / 2;
hashLocker[mId] = indexLocker;
Node *rightSpace = getNode(indexLocker + 1, maxSpace->start + maxSpace->size - (indexLocker + 1));
maxSpace->size = indexLocker - maxSpace->start;
if (maxSpace->size > 0) {
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
}
if (rightSpace->size > 0) {
treeSize.insert(rightSpace);
treeStart.insert(rightSpace);
}
return indexLocker;
}
return 0;
}
int leave(int mId) {
int indexLocker = hashLocker[mId];
hashLocker.erase(mId);
Node *delNode;
Node *newNode = getNode(indexLocker, 1);
Node tp;
tp.init(indexLocker, 0);
auto it = treeStart.lower_bound(&tp);
if (it != treeStart.end()) {
delNode = (*it);
if (newNode->start + newNode->size == delNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->size += delNode->size;
}
}
auto it2 = treeStart.lower_bound(&tp);
if (it2 != treeStart.begin()) {
it2--;
delNode = (*it2);
if (delNode->start + delNode->size == newNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->start = delNode->start;
newNode->size += delNode->size;
}
}
treeSize.insert(newNode);
treeStart.insert(newNode);
return N - hashLocker.size();
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 14:59
#include <iostream>
#include <set>
#include <unordered_map>
using namespace std;
const int MAXNODE = 40005;
struct Node {
int start, size;
void init(int st, int sz) {
start = st; size = sz;
}
};
Node pool[MAXNODE];
int cnt;
Node *getNode(int st, int sz) {
pool[cnt].init(st, sz);
return (pool + cnt++);
}
struct CmpSize {
bool operator() (const Node *n1, const Node *n2) const {
if (n1->size != n2->size) return n1->size > n2->size;
return n1->start < n2->start;
}
};
struct CmpStart {
bool operator() (const Node *n1, const Node *n2) const {
return n1->start < n2->start;
}
};
int N;
unordered_map<int, int> hashLocker;
set<Node *, CmpSize> treeSize;
set<Node *, CmpStart> treeStart;
void init(int N) {
cnt = 0;
::N = N;
hashLocker.clear();
treeSize.clear();
treeStart.clear();
Node *n = getNode(1, N);
treeSize.insert(n);
treeStart.insert(n);
return;
}
int arrive(int mId) {
Node *maxSpace = *(treeSize.begin());
treeSize.erase(maxSpace);
treeStart.erase(maxSpace);
if (maxSpace->start == 1) {
// pick locker at first
hashLocker[mId] = 1;
maxSpace->start = 2;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return 1;
}
if (maxSpace->start + maxSpace->size - 1 == N) {
// pick locker at last
hashLocker[mId] = N;
maxSpace->size--;
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
return N;
}
{
// pick locker at the middle of space
int indexLocker = maxSpace->start + (maxSpace->size - 1) / 2;
hashLocker[mId] = indexLocker;
Node *rightSpace = getNode(indexLocker + 1, maxSpace->start + maxSpace->size - (indexLocker + 1));
maxSpace->size = indexLocker - maxSpace->start;
if (maxSpace->size > 0) {
treeSize.insert(maxSpace);
treeStart.insert(maxSpace);
}
if (rightSpace->size > 0) {
treeSize.insert(rightSpace);
treeStart.insert(rightSpace);
}
return indexLocker;
}
return 0;
}
int leave(int mId) {
int indexLocker = hashLocker[mId];
hashLocker.erase(mId);
Node *delNode;
Node *newNode = getNode(indexLocker, 1);
Node tp;
tp.init(indexLocker, 0);
auto it = treeStart.lower_bound(&tp);
if (it != treeStart.end()) {
delNode = (*it);
if (newNode->start + newNode->size == delNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->size += delNode->size;
}
}
auto it2 = treeStart.lower_bound(&tp);
if (it2 != treeStart.begin()) {
it2--;
delNode = (*it2);
if (delNode->start + delNode->size == newNode->start) {
treeSize.erase(delNode);
treeStart.erase(delNode);
newNode->start = delNode->start;
newNode->size += delNode->size;
}
}
treeSize.insert(newNode);
treeStart.insert(newNode);
return N - hashLocker.size();
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 15:03
servival train
[Nguyễn Văn Vang / Nguyen Van Vang] 8/2/2023 15:03
#include<iostream>
#include<vector>
#include<set>
using namespace std;
struct passenger
{
int gId;
int points;
};
passenger PS[100005];
vector<int> JOBID[1000];
struct cmp
{
bool operator()(int a, int b) const
{
if (PS[a].points == PS[b].points)
return a < b;
return PS[a].points > PS[b].points;
}
};
set<int, cmp> GROUPSET[10]; // decreasing order
int groupcnt;
int job;
void init(int N, int M, int J, int mPoint[], int mJobID[])
{
groupcnt = N/M;
job = J;
for (int i = 0; i < N; i++)
{
PS[i].gId = i / M;
PS[i].points = mPoint[i];
JOBID[mJobID[i]].push_back(i);
GROUPSET[i / M].insert(i);
}
}
void destroy()
{
for (int i = 0; i < groupcnt; i++)
GROUPSET[i].clear();
for (int i = 0; i < job; i++)
JOBID[i].clear();
}
int update(int mID, int mPoint)
{
int groupNumber = PS[mID].gId;
GROUPSET[groupNumber].erase(mID);
PS[mID].points += mPoint;
GROUPSET[groupNumber].insert(mID);
return PS[mID].points;
}
int updateByJob(int mJobID, int mPoint)
{
int sum = 0;
int size = JOBID[mJobID].size();
// update point for each passenger in that jobid
for (int i = 0; i < size; i++)
{
int id = JOBID[mJobID][i];
sum += update(id, mPoint);
}
return sum;
}
int move(int mNum)
{
int sum = 0;
// store temp value
vector<int> TMP[10];
for (int i = 1; i < groupcnt; i++)
{
for (int j = 0; j < mNum; j++)
{
// heigh element
auto itr1 = GROUPSET[i].begin();
// low element
auto itr2 = --GROUPSET[i - 1].end();
TMP[i - 1].push_back(*itr1);
TMP[i].push_back(*itr2);
// erase from set
GROUPSET[i].erase(itr1);
GROUPSET[i - 1].erase(itr2);
}
}
for (int i = 0; i < groupcnt; i++)
{
for (int j = 0; j < TMP[i].size(); j++)
{
sum += PS[TMP[i][j]].points;
PS[TMP[i][j]].gId = i;
GROUPSET[i].insert(TMP[i][j]);
}
}
return sum;
}
[Phạm Quang Trà (Tra_광차_Tra) / Pham Quang Tra] 8/2/2023 16:41
//soldier
const int MIN_ID = 1;
const int MAX_ID = 100000;
const int MIN_TEAM = 1;
const int MAX_TEAM = 5;
const int MIN_SCORE = 1;
const int MAX_SCORE = 5;
struct Node
{
int id;
int team;
Node *prev;
Node *next;
} soldier[MAX_ID + 1];
struct List
{
Node head;
Node tail;
static void link(Node *front, Node *back)
{
front->next = back;
back->prev = front;
}
static void erase(Node *node)
{
link(node->prev, node->next);
}
void initialize()
{
link(&head, &tail);
}
void insert(Node *node)
{
link(tail.prev, node);
link(node, &tail);
}
bool isEmpty()
{
return (head.next == &tail);
}
void splice(List *list)
{
if (list->isEmpty())
return;
link(tail.prev, list->head.next);
link(list->tail.prev, &tail);
list->initialize();
}
} soldierGroup[MAX_TEAM + 1][MAX_SCORE + 1];
void init()
{
for (int i = MIN_TEAM; i <= MAX_TEAM; i++)
for (int j = MIN_SCORE; j <= MAX_SCORE; j++)
soldierGroup[i][j].initialize();
}
void hire(int mID, int mTeam, int mScore)
{
soldier[mID].id = mID;
soldier[mID].team = mTeam;
soldierGroup[mTeam][mScore].insert(soldier + mID);
}
void fire(int mID)
{
List::erase(soldier + mID);
}
void updateSoldier(int mID, int mScore)
{
List::erase(soldier + mID);
soldierGroup[soldier[mID].team][mScore].insert(soldier + mID);
}
void updateTeam(int mTeam, int mChangeScore)
{
if (mChangeScore > 0)
{
for (int i = MAX_SCORE - 1; i >= MIN_SCORE; i--)
{
int newScore = i + mChangeScore;
if (newScore > MAX_SCORE)
newScore = MAX_SCORE;
soldierGroup[mTeam][newScore].splice(&soldierGroup[mTeam][i]);
}
}
else if (mChangeScore < 0)
{
for (int i = MIN_SCORE + 1; i <= MAX_SCORE; i++)
{
int newScore = i + mChangeScore;
if (newScore < MIN_SCORE)
newScore = MIN_SCORE;
soldierGroup[mTeam][newScore].splice(&soldierGroup[mTeam][i]);
}
}
}
int bestSoldier(int mTeam)
{
List *maxScoreGroup;
for (int i = MAX_SCORE; i >= MIN_SCORE; i--)
{
if (!soldierGroup[mTeam][i].isEmpty())
{
maxScoreGroup = &soldierGroup[mTeam][i];
break;
}
}
int maxId = MIN_ID - 1;
Node *maxScoreSoldier = maxScoreGroup->head.next;
while (maxScoreSoldier != &(maxScoreGroup->tail))
{
if (maxId < maxScoreSoldier->id)
maxId = maxScoreSoldier->id;
maxScoreSoldier = maxScoreSoldier->next;
}
return maxId;
}
>> Thursday, August 3, 2023
[Nguyễn Văn Vang / Nguyen Van Vang] 8/3/2023 14:23
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
struct node{
int id;
int score;
int grade;
int gen;
};
struct cmp{
bool operator()(const node *n1, const node *n2){
if(n1->score!=n2->score) return n1->score<n2->score;
else return n1->id<n2->id;
}
};
set <node *,cmp> s[4][3];
unordered_map<int,node*> mymap;
void init() {
mymap.clear();
for(int i=1;i<4;i++){
s[i][1].clear();
s[i][2].clear();
}
return;
}
int add(int mId, int mGrade, char mGender[7], int mScore) {
node *newNode=new node();
int gender=1;
if(mGender[0]=='f') gender=2;
newNode->id=mId;
newNode->gen=gender;
newNode->grade=mGrade;
newNode->score=mScore;
s[mGrade][gender].insert(newNode);
auto it=s[mGrade][gender].rbegin();
mymap[mId]=newNode;
return (*it)->id;
}
int remove(int mId) {
auto it=mymap.find(mId);
if(it==mymap.end()) return 0;
node *cur=it->second;
int mGrade = cur->grade;
int mGender = cur->gen;
s[mGrade][mGender].erase(cur);
if(s[mGrade][mGender].empty())return 0;
auto i = s[mGrade][mGender].begin();
return (*i)->id;
}
int query(int mGradeCnt, int mGrade[], int mGenderCnt, char mGender[][7], int mScore) {
int gen[2];
for(int i=0;i< mGenderCnt;i++)
{
gen[i] = 1;
if(mGender[i][0]=='f')gen[i]=2;
}
int ansId =0 , ansScore = 300003;
node *newNode = new node();
newNode->score = mScore;
newNode->id = 0;
for(int i=0;i<mGradeCnt;i++)
for(int j=0;j<mGenderCnt;j++)
{
int lop = mGrade[i];
int gt = gen[j];
auto it = s[lop][gt].upper_bound(newNode);
if(it != s[lop][gt].end())
{
if((*it)->score < ansScore)ansScore = (*it)->score, ansId = (*it)->id;else
if((*it)->score == ansScore)if(ansId > (*it)->id)ansId = (*it)->id;
}
}
return ansId;
}
[Nguyễn Văn Vang / Nguyen Van Vang] 8/3/2023 14:24
school record
Editor is loading...