Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
4.1 kB
2
Indexable
Never
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>
#include<climits>
  
#define MAX_SIZE 30010
  
using namespace std;
struct Product
{
    int price;
    bool status;
}ProductPool[MAX_SIZE];
 
int productcnt; //
 
  
  
unordered_map<string, vector<int>> hash1tag;
 unordered_map<string, vector<int>> hash3tag;
 
void init(int N)
{
  
    hash1tag.clear();
    hash3tag.clear();
    productcnt = 0;
     
}
  
void addProduct(int mPrice, int tagNum, char tagName[][10])
{
    ProductPool[productcnt].price = mPrice;
    ProductPool[productcnt].status = false;
    string tags[6];
     
     
    // Add vao hash1tag
    for (int i = 0; i < tagNum; i++)
    {
        tags[i]=tagName[i];
        hash1tag[tagName[i]].push_back(productcnt);
    }
    sort(tags, tags+tagNum);//Sắp xếp toàn bộ mảng s có tagNum phần tử
 
    string temp;
    // lay to hop 3 cua N tag 
    for (int i = 0; i < tagNum - 2; i++)
    {
        for (int j = i+1; j < tagNum -1; j++)
        {
            for (int k = j+1; k<tagNum; k++)
            {
                temp = tags[i] + " " + tags[j] + " " + tags[k];
                hash3tag[temp].push_back(productcnt);
            }
        }
    }
    productcnt++;
}
  
int buyProduct(char tag1[], char tag2[], char tag3[])
{
    string tags[4];
    tags[0] = tag1;
    tags[1] = tag2;
    tags[2] = tag3;
     
     
     // sap xep 3 tag
    sort(tags, tags+3);
    string tmp;
    tmp = tags[0] + " " + tags[1] + " " + tags[2];
    // tim kiem sp re nhat
    int minPrice = INT_MAX;
    int minID = -1;
    for (auto sp : hash3tag[tmp])
    {
        if (ProductPool[sp].status == true) continue;
        if (ProductPool[sp].price < minPrice&& ProductPool[sp].status==false)
        {
            minPrice = ProductPool[sp].price;
            minID = sp;
        }
    }
    if (minID == -1) return -1;
    ProductPool[minID].status = true;   //neu san pham da duoc mua thi se bi delete
 
    return minPrice;
}
  
void adjustPrice(char tag1[], int changePrice)
{
    for (auto sp : hash1tag[tag1])
    {
 
        ProductPool[sp].price += changePrice;
    }
}
//
#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;
}

//input

Leave a Comment