1. 程式人生 > >中古世界的惡龍[The Drangon of Loowater,UVa 11292]

中古世界的惡龍[The Drangon of Loowater,UVa 11292]

a.out 一行 spa open 時間 最小 scan 增強 sin

時間限制:1 s 內存限制:256 MB

【題目描述】

King(CH)的王國裏有一條n個頭的惡龍,他希望雇傭一些騎士把它殺死(即砍掉所有的頭)。小酒館裏有m個騎士可以雇傭,一個能力值為x的騎士可以砍掉一個直徑不超過x的頭,且需要支付x個金幣。如何雇用騎士才能砍掉所有惡龍的頭,且需要支付的金幣最少?註意:一個騎士只能砍一個頭(且不能被雇傭2次)。

【輸入格式】

輸入包含多組數據。數據的第一行為正整數n和m(1≤n,m≤20 000);以下n行每行為一個整數,即惡龍每個頭的直徑;以下m行每行為一個整數,即每個騎士的能力。

【輸出格式】

輸出最小花費。如果無解,輸出"Loowater is doomed"。(無解的情況為龍頭砍不完)

【樣例輸入】

2 3
5
4
7
8
4

【樣例輸出】

11

【提示】

小酒館裏向來是可以尋得大量雇傭軍的。

雇傭兵戰鬥屬性較強,但屬性固定,不能增強或升級。

雇主是不需要為雇傭兵的傷亡負責的。

【來源】

The Dragon of Loowater,UVa 11292,數據來自[Chen Hao,Algorithms Consult]

解法:

貪心。

龍頭的大小從大到小排個序,騎士的能力從大到小排個序。

對於當前龍頭,在騎士中選一個還未選過的並且能砍掉當前龍頭的去砍。

註意:一個騎士只能被選一次。

數組寫法:

技術分享圖片
 1 #include<iostream>
 2 #include<cstdio>
 3
#include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<map> 7 #include<queue> 8 #define mod 10000 9 #define inf 336860180 10 #define PI 3.1415926 11 #define ll long long 12 using namespace std; 13 const int N=1e6; 14 int ans,n,m,l,x,p; 15 int a[N],b[N]; 16
int main() 17 { 18 freopen("DragonUVa.in","r",stdin); 19 freopen("DragonUVa.out","w",stdout); 20 scanf("%d%d",&n,&m); 21 for(int i=1;i<=n;++i) scanf("%d",&a[i]); 22 sort(a+1,a+n+1); 23 for(int i=1;i<=m;++i) scanf("%d",&b[i]); 24 sort(b+1,b+m+1); 25 l=1; 26 for(int i=1;i<=n;++i) 27 { 28 x=lower_bound(b+l,b+m+1,a[i])-b; 29 if(x>m) break; 30 l=x+1; 31 ans+=b[x];p++; 32 } 33 if(p<n) cout<<"Loowater is doomed"; 34 else cout<<ans; 35 return 0; 36 }
View Code

multiset寫法:

技術分享圖片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<set>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 20005
 7  
 8 int n,m;
 9 int dra[N];
10 multiset<int>se;
11  
12 inline int read() 
13 {
14     int tmp=0,w=1;
15     char ch=0;
16     while(ch<0||ch>9) {if(ch==-) w=-1;ch=getchar();}
17     while(ch>=0&&ch<=9) tmp=(tmp<<1)+(tmp<<3)+ch-0,ch=getchar();
18     return tmp*w;
19 }
20  
21 int main()
22 {
23     freopen("DragonUVa.in","r",stdin);
24     freopen("DragonUVa.out","w",stdout);
25     n=read(),m=read();
26     if(n>m) {printf("Loowater is doomed\n");return 0;}
27     for(int i=1;i<=n;++i) dra[i]=read();
28     int x;
29     se.insert(1e9);
30     for(int i=1;i<=m;++i) x=read(),se.insert(x);
31     int ans=0;
32     sort(dra+1,dra+1+n);
33     for(int i=1;i<=n;++i) 
34        {
35         set<int>::iterator x=se.lower_bound(dra[i]);
36         if(*x==1e9) {printf("Loowater is doomed\n");return 0;}
37         ans=ans+*x;
38         se.erase(x);
39     }
40     printf("%d\n",ans);
41     return 0;
42 }/*
43 2 1
44 5
45 5
46 10
47 */
View Code

(?′?‵?)I L???????

中古世界的惡龍[The Drangon of Loowater,UVa 11292]