VJ 补题
J. Candy
Bahosain went back for teaching but now in a primary school. His classes, which contain a total of Nstudents,
went to a school trip. Bahosain has Mpackets of candies; each packet contains one or more candies.
Students think that a distribution of packets is fair if all students from the same age get the same number of
candies, and older students get more candies than the younger students.
Given the age of each student, and the number of candies in each packet, determine if it is possible to
distribute the packets so that every student gets exactly one packet and the distribution is fair according to
the students.
Input
The first line of input contains T (1 ≤ T ≤ 128), the number of test cases.
The first line of each test case contains two integers: N (1 ≤ N ≤ 100)and M (N ≤ M ≤ 200), which represent
the number of students and the number of candy packets respectively.
The next line contains Nspace-separated integers between 5and 15, each representing the age of one
student.
The next line contains Mspace-separated integers between 1 and 50, each representing the amount of
candies in one of the packets.
Output
For each test case print a single line with “YES” if it is possible to distribute the packets according to the
problem statement, otherwise print “NO”.
Sample Input Sample Output
3
36
51510
222334
44
5567
5466
56
86755
121312
YES
NO
NO
一开始做的时候把糖按照数量由少到多和小盆友按照年龄从小到大一个一个比较了,没有注意到有不同数量糖的糖包可能比年龄不同的小盆友数量多,用a,b数组存储年龄为i的小朋友的数量和糖数为i的糖包的数量,然后从后面到前面一次比较是否当前年龄的小朋友的数量比当前数量的糖包少,如果多,那么就去和下一级糖包的数量比较,如果最后一级小盆友有相应的糖包匹配就ok,没有就不ok
代码如下,直接奉上:
#include
#include <bits/stdc++.h>
using namespace std;
#define maxn 111
int main()
{
int t, n, m, a[maxn], b[maxn];//a[i]表示年龄为i的小盆友的数量,b[i]表示数量为i的糖包数量
cin>>t;
while(t–)
{
cin>>n>>m;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int temp;
for(int i=1;i<=n;i++)
{
cin>>temp;
a[temp]++;
}
for(int i=1;i<=m;i++)
{
cin>>temp;
b[temp]++;
}
int i=20, j=50,flag=0;
while(i)
{
if(!a[i])
{
i–;
continue;
}
while(j&&b[j]<a[i])
j–;
if(!j)
{
flag = 1;
break;
}
i–;
j–;
}
if(flag)
cout<<“NO”<<endl;
else
cout<<“YES”<<endl;
}
return 0;
}