骑士砍龙问题

n条龙直径为x,m个勇士,能量值为x能砍直径<=x的龙,需要支付x个金币.

将n条龙按从小到大排列,m个勇士按能量值从小到大排列,勇士不够砍返回无解.

 1 #include<IOSTREAM>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int MAXM=2000;
 6 int B[MAXM], A[MAXM];
 7 int main()
 8 {
 9     int n,m;
10 //    cout <<"n,m \n";
11     cin >>n >>m;
12 //    cout <<"A[i] \n";
13     for (int i=0; i<n; i++)  cin >>A[i];
14 //    cout <<"B[j] \n";
15     for (int j=0; j<m; j++)  cin >>B[j];
16     sort(A,A+n);
17     sort(B,B+m);
18     int cost=0;                          //当前总费用
19     int cur=0;                           //龙的编号
20     for (int k=0; k<n; k++)
21     {
22         if (A[cur]<=B[k])
23         {
24             cost +=B[k];                 //雇佣该勇士
25             if(++cur==n)  break;         //砍完提前结束
26         }
27     }
28     if(cur<n)  cout <<"无解 \n";
29     else cout <<cost <<endl;
30 
31     return 0;
32 }
View Code

 

 

posted on 2013-10-31 22:35  缠绕寂寞  阅读(162)  评论(0)    收藏  举报