Acwing 第 82 场周赛题解

AcWing 4782. 第k个数

原题连接

签到题

排序求第k大(逆序)

这里直接一个快排厚葬了

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 1e5 + 10;
int q[N];

int quick_sort(int l,int r,int k){
    if(l >= r) return q[l];
    int i = l - 1,j = r + 1,x = q[l + r >> 1];
    while(i < j) {
        do i++;while(q[i] < x);
        do j--;while(q[j] > x);
        if(i < j) swap(q[i],q[j]);
    }
    if(j - l + 1 >= k) return quick_sort(l,j,k);
    else return quick_sort(j+1,r,k - (j - l + 1));
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin >> n >> k;
    for(int i = 0; i < n; i ++ ){
        cin >> q[i];
    }
    cout << quick_sort(0,n-1,n - k + 1);
    return 0;
}

AcWing 4783. 多米诺骨牌

原题连接

题解

模拟,向左边倒的,我们给这个方向上的骨牌减去一个偏移量,同理,右边倒的加上一个偏移量,如果第i块的偏移量为0,则它最后一定是直立着的

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); 
    int n;
    std::cin >> n;

    string s;
    std::cin >> s;

    std::vector<int> a(n);

    for (int i = 0; i < n; i ++) {
        if (s[i] == 'L') {
            a[i] = -1;
            int idx = 1;
            int j = i - 1;
            while(j >= 0 and s[j] == '.') {
                a[j] -= idx;
                idx ++;
                j --;
            }
        }
        if (s[i] == 'R') {
            a[i] = -1;
            int idx = 1;
            int j = i + 1;
            while(j < n and s[j] == '.') {
                a[j] += idx;
                idx ++;
                j ++;
            }   
        }
    }
    int res = 0;
    for (int i = 0; i < n; i ++) {
        res += (!a[i]);
    }
    
    std::cout << res << "\n";
    return 0;
}



AcWing 4784. 构造序列

原题连接

题解

1的数量符合题目的下阶的数量为m >= n - 1,上阶为m <= 2 * (n + 1).对于n个0,需要n-1个1来填补.所以多余的1我们把他放在一起,比如110110110···这样的方式排,少的则以010101···这样排

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); 
    int n,m;
    std::cin >> n >> m;

    if (m < n - 1 or m > 2 * (n + 1)) std::cout << "-1\n";
    else {
        for (int i = 0; i < 2; i ++) {
            if (m > n - 1) {
                std::cout << "1";
                m --;
            }
        }
        while (n) {
            std::cout << "0";
            n --;
            if (n) {
                std::cout << "1";
                m --;
                if (m > n - 1) {
                    std::cout << "1";
                    m --;
                }
            }
        }
        while (m--) std::cout << "1"; 
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值