Untitled
unknown
plain_text
2 years ago
2.5 kB
19
Indexable
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <string>
using namespace std;
struct PRODUCT {
int active;
int price;
} Product[30000];
int pcnt;
struct NODE {
PRODUCT *prod;
NODE *next;
} Node[500000];
int ncnt;
NODE TagHead[27000];
unordered_map<string, int> Tag;
int tcnt;
void init(int N)
{
Tag.clear();
tcnt = 0;
pcnt = 0;
ncnt = 0;
for (int i = 0; i < 27000; i++) TagHead[i].next = 0;
}
int get_tagid(char name[])
{
auto tag = Tag.find(name);
if (tag == Tag.end()) {
int tid = tcnt++;
Tag[name] = tid;
return tid;
}
return tag->second;
}
void add_node(int tid, PRODUCT *prod)
{
NODE *node = &Node[ncnt++];
node->prod = prod;
node->next = TagHead[tid].next;
TagHead[tid].next = node;
}
void addProduct(int mPrice, int tagNum, char tagName[5][10])
{
vector <int> tid(tagNum);
for (int i = 0; i < tagNum; i++)
tid[i] = get_tagid(tagName[i]);
sort(tid.begin(), tid.end());
PRODUCT *prod = &Product[pcnt++];
prod->active = 1;
prod->price = mPrice;
for (int i = 0; i < tagNum; i++) {
add_node(tid[i], prod);
for (int k = i + 1; k < tagNum; k++){
for (int m = k + 1; m < tagNum; m++){
add_node(tid[i] + tid[k] * 30 + tid[m] * 900, prod);
}
}
}
}
int buyProduct(char tag1[10], char tag2[10], char tag3[10])
{
vector <int> tid(3);
tid[0] = get_tagid(tag1);
tid[1] = get_tagid(tag2);
tid[2] = get_tagid(tag3);
sort(tid.begin(), tid.end());
int minprice = 987654321;
NODE *minnode;
NODE* node = TagHead[tid[0] + tid[1] * 30 + tid[2] * 900].next;
while (node) {
PRODUCT *prod = node->prod;
if (prod->active != 0) {
if (minprice > prod->price) {
minprice = prod->price;
minnode = node;
}
}
node = node->next;
}
if (minprice != 987654321) {
minnode->prod->active = 0;
}
else return -1;
return minprice;
}
void adjustPrice(char tag[10], int changePrice)
{
int tid = get_tagid(tag);
NODE* node = TagHead[tid].next;
while (node) {
node->prod->price += changePrice;
node = node->next;
}
}Editor is loading...
Leave a Comment