CodeForces 799B (B) T-shirt buying
生活随笔
收集整理的這篇文章主要介紹了
CodeForces 799B (B) T-shirt buying
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
分析
本題實質上就是對數據處理一下就可以了 就是數據結構實現題 我們要求對應索引下按照某種排序規則的線性表時 取最優時 可以用堆 每次拿出一個元素 堆內元素調整順序 然后用三個堆 就可以表示三張表 每次我們得到顧客的一個喜歡顏色 就可以利用這個顏色去查堆 找到就輸出 找不到就輸出-1 本題在于把對應顏色作為下標 將元素存進來 用的時候就查表就行了減少遍歷次數 將一張大表分成不同顏色的小表code
#include<iostream> #include<cstring> #include<queue> #include<cstdio> using namespace std; struct clo{int p,id;clo(int pr,int i):p(pr),id(i){}friend bool operator<(const clo & cc,const clo & dd){return dd.p < cc.p;} }; const int maxn = 200010; priority_queue<clo>q[4]; int c[maxn],ans[maxn]; bool bok[maxn];int main() {int t,n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&c[i]);for(int i=1;i<=n;i++){scanf("%d",&t);q[t].push(clo(c[i],i));}for(int i=1;i<=n;i++){scanf("%d",&t);q[t].push(clo(c[i],i));}int m;scanf("%d",&m);for(int i=1;i<=m;i++){int t;scanf("%d",&t);if(q[t].empty())ans[i] = -1;else{while(!q[t].empty()&&bok[q[t].top().id])q[t].pop();if(q[t].empty()) ans[i]=-1;else {ans[i] = q[t].top().p;bok[q[t].top().id]=1;q[t].pop(); }} }for(int i=1;i<=m;i++)printf("%d%c",ans[i],i==m?'\n':' ');return 0; }總結
以上是生活随笔為你收集整理的CodeForces 799B (B) T-shirt buying的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP调试工具 - FirePHP安装与
- 下一篇: 2017年8个最流行的Web编程趋势