绿皮书


私信TA

用户名:dotcpp0639496

访问量:566

签 名:

持之以恒

等  级
排  名 9440
经  验 1093
参赛次数 0
文章发表 1
年  龄 21
在职情况 学生
学  校 广东第二师范学院
专  业 软件工程

  自我简介:

低调

TA的其他文章

解题思路:优先队列有自动排序功能,这里用最小优先队列(即卡牌数越少,在队列中排名越靠前),每次都从优先队列中拿出队头,使用一张万能牌,每次都判断该类牌的万能牌可使用量是否已经为零,如果为零,循环提前结束,一直到万能牌归零,循环自然结束,最终答案就是返回队列头元素的卡牌数量

注意事项:注意变量的更新

参考代码:

#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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

为什么我这个不对?????只得了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;
}
2023-06-03 23:49:30
  • «
  • 1
  • »