1. 程式人生 > >【51nod 1191】消滅兔子

【51nod 1191】消滅兔子

open put b+ return ont con 所有 opera 輸出

Description

有N只兔子,每只有一個血量B[i],需要用箭殺死免子。有M種不同類型的箭可以選擇,每種箭對兔子的傷害值分別為D[i],價格為P[i](1 <= i <= M)。假設每種箭只能使用一次,每只免子也只能被射一次,計算要消滅地圖上的所有兔子最少需要多少Q幣。如不能殺死所有兔子,請輸出No Solution。 特別說明:1、當箭的傷害值大於等於兔子的血量時,能將兔子殺死;2、血量B[i],箭的傷害值D[i],箭的價格P[i],均小於等於100000。

Input

第1行:兩個整數N,M,中間用空格分隔(1 <= N, M <= 50000),分別表示兔子的個數和箭的種類。
第2 - N + 1行:每行1個正整數(共N行),表示兔子的血量B[i](1 <= B[i] <= 100000)。
第N + 2 - N + M + 1行:每行2個正整數(共M行),中間用空格分隔,表示箭所能造成的傷害值D[i],和需要花費的Q幣P[i](1 <= D[i], P[i] <= 100000)。

Output

輸出最少需要多少Q幣才能消滅所有的兔子。如果不能殺死所有兔子,請輸出"No Solution"。

Input示例

3 3
1
2
3
2 1
3 2
4 3

Output示例

6

技術分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 int n,m,ans,b[50050];
 7 struct node
 8 {
 9     int d,p;
10     bool operator
<(const node& a)const{return p>a.p;} 11 }; 12 struct kick{int d,p;}a[50050]; 13 priority_queue<node>q; 14 int read() 15 { 16 int x=0,f=1;char c=getchar(); 17 while(c<0||c>9){if(c==-)f=-1;c=getchar();} 18 while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}
19 return x*f; 20 } 21 bool cmp(kick a,kick b){return a.d<b.d;} 22 int main() 23 { 24 n=read();m=read(); 25 int tail=m; 26 for(int i=1;i<=n;i++)b[i]=read(); 27 sort(b+1,b+n+1); 28 for(int i=1;i<=m;i++)a[i].d=read(),a[i].p=read(); 29 sort(a+1,a+m+1,cmp); 30 for(int i=n;i>=1;i--) 31 { 32 while(tail&&a[tail].d>=b[i]){q.push((node){a[tail].d,a[tail].p});tail--;} 33 if(q.empty()){printf("No Solution");return 0;} 34 node t=q.top();ans+=t.p;q.pop(); 35 } 36 printf("%d",ans); 37 return 0; 38 }
View Code

 

【51nod 1191】消滅兔子