There is an n-headed dragon in the kingdom of , and you want to hire some knights to kill it (i.e. chop off all the heads). There are m knights in the village that can be hired, and a knight with an ability value of x can cut off a head of an evil dragon whose diameter does not exceed x, and needs to pay x gold coins. How can a hired knight cut off all the heads of a dragon with the least amount of gold? Note that a knight can only chop off one head (and cannot be hired twice). enter The input contains multiple sets of data. The first row of each set of data contains positive integers n and m ( 1 <=n,m<= 20 000 ); each row of the following n rows is an integer, that is, the diameter of each head of the dragon; the following m rows are each row of an integer, that is The abilities of each knight. The input end flag is n = m = 0 . output For each set of data, output the least cost. If there is no solution, output "Loowater is doomed! ". sample input 2 3 5 4 7 8 4 2 1 5 5 10 0 0 Sample output 11 Loowater is doomed!
#include<stdio.h> int a[ 20000 ],b[ 20000 ]; //Generally speaking, greed must be sorted int main() { int n,m,s,j,i; while(scanf("%d%d",&n,&m)&&n&&m) { s=0; j=0; for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<m;i++) scanf("%d",&b[i]); for(int k=0;k<n;k++) //排序 for(i=0;i<n-1-k;i++) if(a[i]>a[i+1]) { int t=a[i]; a[i]=a[i+1]; a[i+1]=t; } for(int k=0;k<m;k++) for(i=0;i<m-1-k;i++) if(b[i]>b[i+1]) { int t=b[i]; b[i]=b[i+1]; b[i+1]=t; } for ( int i= 0 ;i<m;i++ ) //Compare m times, how many times there are warriors { if (j== n) break ; if (a[j]> b[i]) //If the diameter of the dragon head is greater than the knight's ability value, continue the loop continue ; else //Otherwise, add the knight's ability value Get up, the head array of the dragon is going to add one to the next one { s=b[i]+s; j++; } } if (j== n) //The dragon's head is cut off printf("%d\n",s); else printf("Loowater is doomed!\n"); } return 0; }