Untitled

 avatar
Darin
plain_text
a year ago
1.6 kB
2
Indexable
Never
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct product{
    int price;
    bool isbuy;
};
product prd[30010];
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].isbuy=false;
    vector<string> tags;
    for(int i=0;i<tagNum;i++){
        tags.push_back(tagName[i]);
        umap[tagName[i]].push_back(tagID);
    }
    sort(tags.begin(),tags.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 tag3=tags[i]+" "+tags[j]+" "+tags[k]; 
                umap[tag3].push_back(tagID);
            }
        }
    }
    tagID++;
}
 
int buyProduct(char tag1[], char tag2[], char tag3[])
{
    vector <string> tags;
    tags.push_back(tag1);
    tags.push_back(tag2);
    tags.push_back(tag3);
    sort(tags.begin(),tags.end());
    string longtag=tags[0]+" "+tags[1]+" "+tags[2];
    int isFind=-1;
    for(auto it:umap[longtag]){
        if(prd[it].isbuy==false){
            if(isFind==-1||prd[it].price<prd[isFind].price){
                isFind=it;
            }
        }
    }
    if(isFind==-1) return -1;
    else {
        prd[isFind].isbuy=true;
        return prd[isFind].price;
    }
}
 
void adjustPrice(char tag1[], int changePrice)
{
    for(auto it:umap[tag1]){
        prd[it].price+=changePrice;
    }
}