周 赛 补 题

AcWing第59场周赛

1.数组操作

简单的前缀和思想,一边录入一边计算前缀和,最后依照题意计算即可

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110;

int n;
int a[N];

int main()
{
    int sum=0;
    
    cin >> n;
    
    int s[n+1];
    s[0]=0;
    for (int i = 0; i < n; i ++ )
    {
        cin>>a[i];
        sum+=a[i];
        s[i+1]=sum;
    }
    sort(s,s+n+1);
    int x=s[0];
    cout << sum-x;
    return 0; 
}

2.减法操作

纯数学问题,找到最小质因子并用原来的数减

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>


using namespace std;

int main()
{
    int n;
    cin >> n;
    bool flag = 0;
    int x;
    for(int i = 2;i <= sqrt(n);i ++)
    {
        if(n % i == 0)
        {
            x = i;
            flag = 1;
            break;
        }
    }
    if(flag)
        cout << 1+(n-x)/2 << '\n';
    else
        cout << '1' << '\n';
    return 0;
    
}

力扣第301场周赛

1.装满杯子需要的最短总时长​​​​​

    三种水都比较均衡,每次装其中两个,瓶颈就是总数。某种水特别多,瓶颈就是数量最多的那种水(其实就是找规律)

class Solution {
public:
    int fillCups(vector<int>& amount) {
        int a = max({amount[0],amount[1],amount[2]});
        int b = amount[0]+amount[1]+amount[2]-a;
        if(a>=b) return a;
        else return ((a+b+1)/2);
    }
};

2.无限集中的最小数字

SmallestInfiniteSet:初始化需要的参数:无限集中最小整数;移除了的整数集合。
popSmallest:首先将要移除的最小整数加入移除集合,然后更新下一个最小整数。
addBack:首先判断移除集合是否含有需要添加的元素,如果有则去掉对应元素,然后重新确定最小整数。

class SmallestInfiniteSet {
    public int minNum;
    public List<Integer> list;
    public SmallestInfiniteSet() {
        minNum = 1;
        list = new ArrayList<>();
    }

    public int popSmallest() {
        list.add(minNum);

        while (list.contains(minNum)) {
            minNum++;
        }

        return list.get(list.size() - 1);
    }

    public void addBack(int num) {
        if (list.contains(num)) {
            list.remove(list.indexOf(num));
        }
        minNum = Math.min(num, minNum);
    }
}

/**
 * Your SmallestInfiniteSet object will be instantiated and called as such:
 * SmallestInfiniteSet obj = new SmallestInfiniteSet();
 * int param_1 = obj.popSmallest();
 * obj.addBack(num);
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值