问题 C: Team Match
时间限制: 1 Sec 内存限制: 128 MB
提交: 104 解决: 37
[提交] [状态] [讨论版] [命题人:admin]
题目描述
The programming competition not only depends on the programmers, but also directed by the coaches. Mr Z is a coach who direct n players to take part in the Guangxi Province Collegiate Programming Contest. We assume that a team is consisted of 3 players whose ability is x, y, z respectively and x >= y >= z. Then the team’s total ability is 3 * x + 2 * y + 1 * z; And for a team, if its ability is not lower than the gold medal level m, the team will certainly win the gold medal. Mr Z would like to match teams to gain as many gold medals as possible, could you tell him how many gold medals it is?
输入
The first line is an integer T which indicates the case number.
And as for each case, there will be 2 lines.
In the first line, there are 2 integers n m, which indicate the number of players, the gold medal level respectively. Please remember n is always the multiple of 3.
In the second line, there are n integers which represents everyone’s ability.
It is guaranteed that——
T is about 100.
for 100% cases, 1 <= n <= 15, 1 <= m <= 30, 1 <= a[i] <= 20.
输出
As for each case, you need to output a single line.
There should be an integer in the line which means the gold medal teams Mr Z could match.
样例输入
2 6 18 3 3 3 4 2 2 6 7 1 1 1 1 1 1
样例输出
2 0
解题思路:暴力求解
#include<bits/stdc++.h>
using namespace std;
#define maxn 20
#define M(a) memset(a,0,sizeof(a))
struct node{
int val;
int vis;
} a[maxn];
bool cmp(node a,node b){
return a.val>b.val;
}
int main()
{
int t,n,m;
cin>>t;
while(cin>>n>>m)
{
int ans=0;
for(int i=0; i<n; i++){
cin>>a[i].val;
a[i].vis=0;
}
sort(a,a+n,cmp);
for(int i=0; i<n; i++){
int b=-1,c=-1,d=-1;
if(a[i].vis) continue;
for(int j=i+1; j<n; j++)
{
if(a[j].vis) continue;
for(int k=j+1; k<n; k++){
if(a[k].vis) continue;
else{
if(3*a[i].val+2*a[j].val+a[k].val>=m){
b=i,c=j,d=k;
}
}
}
}
if(b==-1) break;
else{
a[b].vis=a[c].vis=a[d].vis=1;
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}