比赛时因为写的时间太晚了,有点着急,结果也没写出来。
分析:贪心。按照团队的花费从大到小排序,如果花费相同则按照人数从小到大排序。然后从k张桌子里面挑选可以让这个团队坐下的桌子,然后把这个团队的id和桌子的id记录下来输出即可。
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, k;
struct table //桌子
{
int id;
int limit;
int flag;
}t[1005];
struct group //团队
{
int num;
int id;
int cost;
}g[1005];
struct Ans //答案
{
int g_id;
int t_id;
}ans[1005];
bool comp1(group g1, group g2)
{
if(g1.cost != g2.cost)
return g1.cost > g2.cost;
return g1.num < g2.num;
}
bool comp2(table t1, table t2)
{
return t1.limit < t2.limit;
}
int Search(int x)
{
for(int i = 0; i < k; i++)
{
if(!t[i].flag && t[i].limit >= x)
{
t[i].flag = 1;
return t[i].id;
}
}
return -1;
}
int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 0; i < n; i++)
{
scanf("%d%d",&g[i].num, &g[i].cost);
g[i].id = i + 1;
}
sort(g, g+n, comp1);
scanf("%d",&k);
for(i = 0; i < k; i++)
{
scanf("%d",&t[i].limit);
t[i].flag = 0;
t[i].id = i + 1;
}
sort(t, t+k, comp2);
int s = 0, sum = 0;
for(i = 0; i < n; i++)
{
int pos = Search(g[i].num);
if(pos == -1) continue;
ans[s].g_id = g[i].id;
ans[s++].t_id = pos;
sum += g[i].cost;
}
printf("%d %d\n",s, sum);
for(i = 0; i < s; i++)
printf("%d %d\n",ans[i].g_id, ans[i].t_id);
}
return 0;
}