11.2清北test4T1優先佇列模擬
阿新 • • 發佈:2018-11-08
思路
- 每次要讓pos最小,且val最小的貓吃魚
- 用小根堆維護
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> #include<cstdlib> #include<ctime> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; inline int read(){ char ch=' ';int f=1;int x=0; while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*f; } struct node { int val,pos; }; priority_queue <node> q; bool operator < (node a,node b) { if(a.pos==b.pos) { return a.val>b.val; } else { return a.pos>b.pos; } } int main() { int m,n,x; m=read();n=read();x=read(); int i,j; int mm=m;int cnt1=0; for(i=1;i<=n;i++) { node a; a.val=read(); a.pos=a.val; q.push(a); if(x>=0&&a.pos>x) cnt1++; mm--; if(mm==0) break; } if(mm==0) { cout<<'0'<<' '<<cnt1<<endl; return 0; } while(true) { node a=q.top();q.pop(); if(a.pos>=x) break; if(a.pos<=x&&a.pos+a.val>x) cnt1++; mm--; if(mm==0) break; a.pos+=a.val; q.push(a); } cout<<mm<<' '<<cnt1<<endl; return 0; }