The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXLyou are given the number of t-shirts of this size.
During the registration, the organizers asked each of the n participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizes — this means that any of these two sizes suits him.
Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size:
- the size he wanted, if he specified one size;
- any of the two neibouring sizes, if he specified two sizes.
If it is possible, the program should find any valid distribution of the t-shirts.
The first line of the input contains six non-negative integers — the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100 000.
The second line contains positive integer n (1 ≤ n ≤ 100 000) — the number of participants.
The following n lines contain the sizes specified by the participants, one line per participant. The i-th line contains information provided by the i-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring.
If it is not possible to present a t-shirt to each participant, print «NO» (without quotes).
Otherwise, print n + 1 lines. In the first line print «YES» (without quotes). In the following n lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input.
If there are multiple solutions, print any of them.
0 1 0 1 1 0 3 XL S,M XL,XXL
YES XL M XXL
1 1 2 0 1 1 5 S M S,M XXL,XXXL XL,XXL
NO
思路:将二选一的人挑出来,从小到大处理,优先买小码。
# include <bits/stdc++.h>
using namespace std;
string s[8] = {"S","M","L","XL","XXL","XXXL"};
int a[8], b[8], c[1<<17][2];
int buf[8][2];
int cal(string st)
{
for(int i=0; i<6; ++i)
if(s[i] == st) return i;
}
int main()
{
int n;
string t;
memset(c, -1, sizeof(c));
for(int i=0; i<6; ++i) scanf("%d",&a[i]);
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
cin >> t;
int pos = t.find(",",0);
if(pos == string::npos)
{
int id = cal(t);
if(--a[id] < 0) return 0*puts("NO");
else c[i][0] = id;
}
else
{
int id = cal(t.substr(0, pos));
++b[id];
c[i][1] = id;
}
}
for(int i=0; i<5; ++i)
{
if(b[i])
{
int imin = min(b[i], a[i]);
a[i] -= imin, b[i] -= imin;
buf[i][0] = imin;
imin = min(b[i], a[i+1]);
a[i+1] -= imin;
b[i] -= imin;
buf[i][1] = imin;
if(b[i]) return 0*puts("NO");
}
}
puts("YES");
for(int i=0; i<n; ++i)
{
if(c[i][0] != -1)
cout << s[c[i][0]] << endl;
else
{
if(buf[c[i][1]][0] > 0)
{
--buf[c[i][1]][0];
cout << s[c[i][1]] << endl;
}
else
{
--buf[c[i][1]][1];
cout << s[c[i][1]+1] << endl;
}
}
}
return 0;
}
本文介绍了一种竞赛场景下的T恤分配算法,旨在解决不同尺码T恤如何有效分配给参与者的问题,确保每位参与者都能获得合适的T恤。
1万+

被折叠的 条评论
为什么被折叠?



