codeforces C. Booking System

题意:有n组客人,分别告诉每一组的个数和花费,然后给你餐厅内k个桌子,每个桌子的最大容纳人数,如何安排使得餐厅最大收益并且容纳人数尽可能大;

思路:贪心,对花费排序,然后对每一组客人找桌子就可以。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 2000
 5 using namespace std;
 6 struct node
 7 {
 8     int c,p,id;
 9     bool operator <(const node &a)const
10     {
11         return (p>a.p)||(p==a.p&&c>a.c);
12     }
13 }q[maxn];
14 struct node1
15 {
16    int x,id;
17    bool operator <(const node1 &a)const
18    {
19        return x<a.x;
20    }
21 }p[maxn],pp[maxn];
22 bool vis[maxn];
23 int num[maxn];
24 
25 int main()
26 {
27     int n,k;
28     scanf("%d",&n);
29     for(int i=0; i<n; i++)
30     {
31         scanf("%d%d",&q[i].c,&q[i].p);
32         q[i].id=i+1;
33     }
34     sort(q,q+n);
35     scanf("%d",&k);
36     for(int i=1; i<=k; i++)
37     {
38         scanf("%d",&p[i].x);
39         p[i].id=i;
40     }
41     sort(p+1,p+k+1);
42     memset(vis,false,sizeof(vis));
43     int ans=0,cnt=0;
44     for(int i=0; i<n; i++)
45     {
46         for(int j=1; j<=k; j++)
47         {
48             if(p[j].x>=q[i].c&&!vis[p[j].id])
49             {
50                 pp[cnt].x=q[i].id;
51                 pp[cnt++].id=p[j].id;
52                 ans+=q[i].p;
53                 vis[p[j].id]=true;
54                 break;
55             }
56         }
57     }
58     printf("%d %d
",cnt,ans);
59     for(int i=0; i<cnt; i++)
60     {
61         printf("%d %d
",pp[i].x,pp[i].id);
62     }
63     return 0;
64 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4276622.html