C++每日刷题day2025.7.10

思路就是遇到数字就push进栈,遇到符号就取栈顶俩元素进行对应符号运算再push进栈中,直到栈中只剩一个数字。

cpp 复制代码
class Solution {
public:
    bool isNumber(string& x)
    {
        if (x == "+" || x == "-" || x == "*" || x == "/")
            return false;
        else return true;
    }
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for (int i = 0; i < tokens.size(); ++i)
        {
            string& x = tokens[i];
            if (isNumber(x)) st.push(atoi(x.c_str()));
            else{
                int num1 = st.top();
                st.pop();
                int num2 = st.top();
                st.pop();
                switch (x[0])
                {
                    case '+' :{
                        st.push(num1 + num2);
                        break;
                    }
                    case '-' :{
                        st.push(num2 - num1);
                        break;
                    }
                    case '*' :{
                        st.push(num1 * num2);
                        break;
                    }
                    case '/' :{
                        st.push(num2 / num1);
                        break;
                    }
                    default:
                        break;
                }
            }
        }
        return st.top();
    }
};

来个辅助栈,一个正常的栈,另一个是只存放最小值的栈。

cpp 复制代码
class MinStack {
public:
    stack<int> st;
    stack<int> min_st;
    MinStack() {
        min_st.push(INT_MAX);
    }
    
    void push(int val) {
        st.push(val);
        min_st.push(min(val, min_st.top()));
    }
    
    void pop() {
        st.pop();
        min_st.pop();
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return min_st.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

我是用了一个堆一个优先级队列,优先级队列用来存放偶数的,栈用来存放奇数的,每次只需要取优先级队列/2再判断是不是偶数即可。

cpp 复制代码
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

int main() {
    long long n, k;
    cin >> n >> k;
    
    // 使用 vector 动态分配内存
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    priority_queue<long long> st;
    stack<long long> other;
    for (long long i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            st.push(a[i]);
        } else {
            other.push(a[i]);
        }
    }

    while (st.size() && k--) {
        long long top = st.top();
        st.pop();
        top /= 2;
        if (top % 2 == 0) st.push(top);
        else other.push(top);
    }

    long long sum = 0;
    while (!st.empty()) {
        sum += st.top();
        st.pop();
    }
    while (!other.empty()) {
        sum += other.top();
        other.pop();
    }
    cout << sum;

    return 0;
}
相关推荐
Wang's Blog1 分钟前
Java框架 SpringCloud 快速入门: 服务拆分之服务远程调用
java·开发语言·spring cloud
努力努力再努力wz2 分钟前
【CUDA入门系列】CUDA编程实战:设备选择、显存管理、Kernel、Stream 与 Event 全流程梳理
开发语言·c++
杨丰玮41813 分钟前
C语言核心语法
java·c语言·开发语言·学习方法
ttwuai15 分钟前
Go开源后台管理系统推荐:README、源码和License应该怎么验
开发语言·golang·开源
Java后端的Ai之路18 分钟前
Python 进阶探索30 - Python中的装饰器
开发语言·人工智能·python·文件处理·装饰器模式
沫璃染墨18 分钟前
《从零入门Linux系统篇(五十八):线程篇·十一——线程安全与死锁详解:从可重入到多锁管理》
linux·服务器·开发语言·c++·驱动开发·安全·架构
郝学胜_神的一滴22 分钟前
C++ Templates 05:那些容易踩坑的技巧性基础知识
c++·visual studio
黑妹天下第一乖27 分钟前
第 05 讲:阿加犀 AidCV 图像处理加速与 OpenCV 一致开发实战
开发语言·图像处理·人工智能·嵌入式硬件·数码相机·opencv·计算机视觉
言乐628 分钟前
Python基于关键词分拣快递(适用于电商与货运代理等)
开发语言·python·django·virtualenv·pygame
一条破秋裤34 分钟前
01_字符设备基本概念_从分类到LED控制链路
开发语言·php