解题思路:优先队列有自动排序功能,这里用最小优先队列(即卡牌数越少,在队列中排名越靠前),每次都从优先队列中拿出队头,使用一张万能牌,每次都判断该类牌的万能牌可使用量是否已经为零,如果为零,循环提前结束,一直到万能牌归零,循环自然结束,最终答案就是返回队列头元素的卡牌数量
注意事项:注意变量的更新
参考代码:
#include<iostream>
#include<queue>
#include<utility>
#include<vector>
using namespace std;
class cmp{
public:
bool operator()(pair<int,int> a,pair<int,int> b){
//pair<index,number>
return a.second>b.second;
}
};
int main(){
//存储结构定义
int n,m;
cin>>n>>m;
//优先队列结构
priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> q;
int time[n+1];
for(int i=1;i<=n;i++){
int value;
scanf("%d",&value);
q.push({i,value});
}
//记录每种卡牌的万能使用最大数量
for(int i=1;i<=n;i++){
int value;
scanf("%d",&value);
time[i]=value;
}
//算法开始
while(m--){
pair<int,int> temp=q.top();
if(time[temp.first]==0)break;
time[temp.first]--;
q.pop();
temp.second++;
q.push(temp);
}
cout<<q.top().second<<endl;
}
0.0分
3 人评分
为什么我这个不对?????只得了92分,错哪了? int n, m, a[200000], b[200000]; struct cmp { bool operator()(int x, int y) { return a[x] > a[y]; } }; std::priority_queue<int, vector<int>, cmp> q; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i], q.push(i); for (int i = 0; i < n; i++) cin >> b[i]; while (m-- && b[q.top()]) { int x = q.top(); b[x]--; a[x]++; q.pop(), q.push(x); } q.push(q.top()); q.pop(); cout << a[q.top()]; return 0; }
成绩转换 (C语言代码)浏览:1005 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:532 |
淘淘的名单 (C语言代码)答案错误???浏览:593 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:538 |
WU-蓝桥杯算法提高VIP-Quadratic Equation (C++代码)浏览:1742 |
【计算球体积】 (C语言代码)浏览:1101 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:609 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:551 |
Tom数 (C语言代码)浏览:492 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:534 |
SharpKon 2023-06-08 20:54:32 |
我也是,最后发现读取输入用scanf就对了
duan 2024-03-16 15:20:05 |
@SharpKon 我靠,为什么啊