留下蛋蛋的忧伤:这道题目第一次做,想了半天,突然想到了一种思路,在能杀死兔子的箭的集合中,选中价格最小的贪心,用了两个for()循环查找,悲剧了.......果断没想到优先队列,看了一下别的思路,利用优先队列,顿时自己就蒙了,o(︶︿︶)o 唉! 弱的不行.....继续刷题吧~!
#include<iostream> #include<algorithm> #include<cstdio> #include<queue> #include<string.h> #include<vector> #define maxn 100100 using namespace std; struct node { int d; long long price; /* bool friend operator <(const node &a,const node &b) { return a.price>b.price; }*/ bool operator < (const node& a) const { return price > a.price; } } Node[maxn]; bool cmp1(int a,int b) { return a>b; } bool cmp(node a,node b) { return a.d>b.d; } priority_queue<node>q; int num[maxn]; int n,m; long long get_sum() { int sum=0; long long t=0; while(!q.empty()) q.pop(); for(int i=0; i<m; i++) if(Node[i].d>=num[sum]) q.push(Node[i]); else { if(q.empty()) return -1; //为了避免Node[i].d<num[sum]当前值,跳出判断,必须在找到它 t+=q.top().price; q.pop(); i--; sum++; if(sum==n) return t; } for(int i=sum; i<n; i++) { if(q.empty()) return -1; t+=q.top().price; q.pop(); } return t; } int main() { long long ans; while(scanf("%d%d",&n,&m)!=EOF) { memset(num,0,sizeof(num)); memset(Node,0,sizeof(Node)); for(int i=0; i<n; i++) scanf("%d",&num[i]); for(int j=0; j<m; j++) scanf("%d",&Node[j].d); for(int j=0; j<m; j++) scanf("%lld",&Node[j].price); if(n>m) { printf("No\n"); continue; } sort(num,num+n,cmp1); sort(Node,Node+m,cmp); ans=get_sum(); if(ans==-1) printf("No\n"); else printf("%I64d\n",ans); } return 0; }