CodeForces 631C Print Check

本文深入探讨了排序算法、构造优化及预处理技术在数据处理中的关键作用,通过实例展示了如何有效利用这些算法提升数据处理效率与准确性。文章详细介绍了算法原理、实现步骤,并结合具体案例分析了其在实际场景中的应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

排序+构造+预处理

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 200000 + 10;
int n, m;
int a[maxn], ans[maxn];
int op[maxn], e[maxn];
int pos[maxn];
int first, last;
int flag;
int p;
int tot;

bool cmp(const int &a, const int &b)
{
    return a>b;
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for (int i = 1; i <= m; i++) scanf("%d%d", &op[i], &e[i]);
        memset(ans, 0, sizeof ans);
        tot = 0; memset(pos, -1, sizeof pos);
        pos[m] = m;
        for (int i = m - 1; i >= 1; i--)
        {
            pos[i] = pos[i + 1];
            if (e[i] > e[pos[i + 1]]) pos[i] = i;
        }

        p = pos[1];
        if (op[p] == 1){sort(a + 1, a + 1 + e[p]); flag = 1;}
        else{sort(a + 1, a + 1 + e[p], cmp);flag = 2;}
        first = 1; last = e[p];
        for (int i = e[p] + 1; i <= n; i++) ans[i] = a[i], tot = tot + 1;
        while (tot<n)
        {
            p = pos[p + 1]; if (p == -1) break;
            if (first>last)
            {
                for (int i = last; i <= first - e[p]; i++) ans[n - tot] = a[i], tot = tot + 1;
                last = first - e[p]+1;
            }
            else
            {
                for (int i = last; i >= first + e[p]; i--) ans[n - tot] = a[i], tot = tot + 1;
                last = first + e[p] - 1; 
            }
            if (op[p] != flag)
            {
                swap(first, last);
                flag = op[p];
            }
        }
        if (tot < n)
        {
            if (first <= last) for (int i = last; i >= first; i--) ans[n - tot] = a[i], tot = tot + 1;
            else for (int i = last; i <= first; i++) ans[n - tot] = a[i], tot = tot + 1;
        }

        for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zufezzt/p/5244283.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值