Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
5
Indexable
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
   
#define MAX_PRODUCT 30000
   
struct myProduct
{
    int price;
    bool isSold;
};
   
myProduct pool[MAX_PRODUCT];
int cnt;
   
unordered_map<string, vector<int>> hashTag;   //hash voi key la tag, gia tri la mang index
string tempTag[5], temp;    //mang luu tru cac gia tri tag cua product, temp luu tru tag
   
void init(int N)
{
    hashTag.clear();
    cnt = 0;
}
   
void addProduct(int mPrice, int tagNum, char tagName[][10])
{
    for(int i=0; i<tagNum; i++)
    {
        tempTag[i] = tagName[i];
    }
    sort(tempTag, tempTag + tagNum);
   
    pool[cnt].price = mPrice;
    pool[cnt].isSold = false;
   
    for(int i=0; i<tagNum; i++)
    {
        hashTag[tempTag[i]].push_back(cnt);
        for(int j=i+1; j<tagNum; j++)
        {
            for(int k=j+1; k<tagNum; k++)
            {
                temp = tempTag[i] + tempTag[j] + tempTag[k];
                hashTag[temp].push_back(cnt);
            }
        }
    }
    cnt++;
}
   
int buyProduct(char tag1[], char tag2[], char tag3[])
{
    tempTag[0] = tag1;
    tempTag[1] = tag2;
    tempTag[2] = tag3;
    sort(tempTag, tempTag + 3);
    temp = tempTag[0] + tempTag[1] + tempTag[2];
   
    int ans = -1;
    for(int idx: hashTag[temp])
    {
        if(pool[idx].isSold == false && (pool[ans].price > pool[idx].price || ans == -1))
        {
            ans = idx;
        }
    }
   
    if(ans == -1)return -1;
   
    pool[ans].isSold = true;
    return pool[ans].price;
}
   
void adjustPrice(char tag1[], int changePrice)
{
    for(int idx: hashTag[tag1])
    {
        pool[idx].price += changePrice;
    }
}
Editor is loading...
Leave a Comment