Question: A dragon has n heads and M brave men. The power of a brave man is greater than the leader's size to defeat him. Each brave man needs an equivalent commission,
Ask how much it costs to kill a dragon.
Analysis: greedy. First, sort the dragons and brave warriors in ascending order. Then, each time you hire the brave ones with the smallest current capacity value, you can kill the dragons.
(If the current brave leads to an unsuitable optimal solution, he will be hired earlier if he has a higher commission than he has previously)
Note: Tianji Horse Racing (⊙ _ ⊙ ).
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int d[20001];int k[20001];int main(){int n,m;while ( scanf("%d%d",&n,&m) && n+m ) {for ( int i = 0 ; i < n ; ++ i )scanf("%d",&d[i]);for ( int i = 0 ; i < m ; ++ i )scanf("%d",&k[i]);sort( d, d+n );sort( k, k+m );int now = 0,sum = 0,flag = 0;for ( int i = 0 ; i < n ; ++ i ) {while ( now < m && d[i] > k[now] )now ++;if ( now >= m ) {flag = 1; break;}else sum += k[now ++];}if ( flag ) printf("Loowater is doomed!\n");else printf("%d\n",sum);}return 0;}