CodeForces 377B 優先隊列 + 二分

`int n,m,s;typedef struct Node {int id;int nnum;bool operator<(const Node &aa)const {return nnum > aa.nnum;}};Node bug[100000 + 55];typedef struct NODE {int id;int ablity;int cost;bool operator<(const NODE & aa)const {return cost > aa.cost;}};NODE stu[100000 + 55];int maxn;int ans[100000 + 55],tmp[100000 + 55];void init() {memset(bug,0,sizeof(bug));memset(stu,0,sizeof(stu));}bool input() {while(cin>>n>>m>>s) {maxn = -1;for(int i=0;i<m;i++) {cin>>bug[i].nnum;bug[i].id = i + 1;maxn = max(maxn,bug[i].nnum);}for(int i=0;i<n;i++) {cin>>stu[i].ablity;stu[i].id = i + 1;}for(int i=0;i<n;i++)cin>>stu[i].cost;return false;}return true;}bool cmp(NODE x,NODE y) {return x.ablity > y.ablity;}bool isok(int t) {int ret = 0;priority_queue<NODE> q;for(int i=0,j=0;i<m;i+=t) {for(;j<n;j++) {if(stu[j].ablity >= bug[i].nnum)q.push(stu[j]);else break;}if(q.size() == 0)return false;NODE qq = q.top();q.pop();ret += qq.cost;if(ret > s)return false;int mark = 0;for(int j=i;j<m;j++) {tmp[bug[j].id] = qq.id;mark++;if(mark == t)break;}}for(int i=1;i<=m;i++)ans[i] = tmp[i];return true;}void cal() {bool flag = false;for(int i=0;i<n;i++) {if(stu[i].ablity >= maxn && stu[i].cost <= s)flag = true;}if(!flag) {puts("NO");return;}sort(bug,bug + m);sort(stu,stu + n,cmp);int cc = 0;if(!isok(m)){puts("NO");return ;}int l = 1,r = m;while(l <= r) {int mid = (l + r)>>1;if(isok(mid))r = mid - 1;else l = mid + 1;}puts("YES");for(int i=1;i<=m;i++)printf("%d%c",ans[i],i == m?'\n':' ');}void output() {}int main() {while(true) {init();if(input())return 0;cal();output();}return 0;}`

CodeForces 377B 優先隊列 + 二分

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD

Learn more >