Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) B - T-shirt buying

1.题目链接

http://codeforces.com/contest/799/problem/B

2.题目描述

B. T-shirt buying
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A new pack of n t-shirts came to a shop. Each of the t-shirts is characterized by three integers piai and bi, where pi is the price of the i-th t-shirt, ai is front color of the i-th t-shirt and bi is back color of the i-th t-shirt. All values pi are distinct, and values ai and bi are integers from 1 to 3.

m buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the j-th buyer we know his favorite color cj.

A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.

You are to compute the prices each buyer will pay for t-shirts.

Input

The first line contains single integer n (1 ≤ n ≤ 200 000) — the number of t-shirts.

The following line contains sequence of integers p1, p2, ..., pn (1 ≤ pi ≤ 1 000 000 000), where pi equals to the price of the i-th t-shirt.

The following line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3), where ai equals to the front color of the i-th t-shirt.

The following line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 3), where bi equals to the back color of the i-th t-shirt.

The next line contains single integer m (1 ≤ m ≤ 200 000) — the number of buyers.

The following line contains sequence c1, c2, ..., cm (1 ≤ cj ≤ 3), where cj equals to the favorite color of the j-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served.

Output

Print to the first line m integers — the j-th integer should be equal to the price of the t-shirt which the j-th buyer will buy. If the j-th buyer won't buy anything, print -1.

Examples
input
Copy
5
300 200 400 500 911
1 2 1 2 3
2 1 3 2 1
6
2 3 1 2 1 1
output
Copy
200 400 300 500 911 -1 
input
Copy
2
1000000000 1
1 1
1 2
2
2 1
output
Copy
1 1000000000 

3.题意解释

有n件衬衫,对于第i件衬衫,其价格为pi,正面的颜色为ai,背面的颜色为bi。有m个顾客,对于第i个顾客,其喜欢的颜色为ci。

一个顾客至多买一件衬衫,顾客买衬衫当且仅当这个衬衫没有被卖出去并且衬衫有一面的颜色是他喜欢的颜色,如果有多个可以买的衬衫,就买最便宜的。

输出每个顾客买的衬衫的价格,如果没有可以买的衬衫,就输出-1。

4.思路

每当输入一个顾客喜欢的颜色之后,在所有的还未卖出去的衬衫中去寻找有这个颜色的价格最小的衬衫,如果找到了就输出,找不到最后就输出-1。关键在于怎么去找,如果采用二重循环,那么时间复杂度是O(n * m),结合n和m的取值范围就知道很容易超时。可以先把每种颜色的衬衫的价格从低到高排序,这样,拿到顾客想要的颜色后可以直接输出价格。因为有三种颜色,所以三种颜色所对应的价格要分开存放,拿到顾客想要的颜色后直接输出该颜色的衣服中价格最低的即可。而每输出一个解,就要把这个衬衫给删除掉。使用set可以很方便地按照价格进行排序以及删除元素。

5.代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

const int maxn = 200005;

set<int> s[4];
int price[maxn];

int main()
{
	s[1].clear();  s[2].clear();  s[3].clear();
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", &price[i]);
	int col;
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &col);
		s[col].insert(price[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &col);
		s[col].insert(price[i]);
	}

	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; ++i)
	{
		int want;
		scanf("%d", &want);
		if (s[want].empty())
			printf("-1");
		else
		{
			set<int>::iterator it = s[want].begin();
			printf("%d", *it);
			int pri = *it;
			for (int j = 1; j <= 3; ++j)
			{
				if (s[j].empty())
					continue;
				set<int>::iterator temp = s[j].find(pri);
				if (temp != s[j].end())
					s[j].erase(temp);
			}
		}
		if (i == m)
			printf("\n");
		else
			printf(" ");
	}

	return 0;
}

6.错误分析

代码TLE了一次,就是因为在思路中提到的O(n * m)的时间复杂度。思考之后认识到超时的原因一定是在查找最低价格的衬衫上,所以想到可以先把价格从低到高排序,这样可以直接输出最低价格的衬衫。而用set容器可以很方便地实现排序和对元素的删除。

猜你喜欢

转载自blog.csdn.net/void_wq/article/details/80003002
今日推荐