2021牛客多校训练2

这篇博客探讨了两种不同的算法实现,一种是基于栈的模拟算法,用于处理数组元素的特殊排列,另一种是图的遍历算法,解决二维地图中特定路径的搜索问题。文章详细介绍了每种算法的逻辑和实现细节,并通过示例代码进行了解析。

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

k题(模拟)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, b[maxn];
int ans[maxn]={0};
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int pos,x;
        scanf("%d %d",&pos,&x);
        b[pos] = x;
    }
    stack<int> k;
    int maxx=n;
    for(int i=1;i<=n;i++)
    {
        if(b[i])
        {
            if(k.size()+1<b[i])///把第i个的a压进去也不满足要求,所以不能
            {
                printf("-1\n");
                return 0;
            }
            while((int)k.size()>=b[i])
            ///前面有(int)k.size()-b[i]个都比当前的ans[i]大
            {
                ans[ k.top() ] = maxx;
                maxx--;
                k.pop();
            }
            k.push(i);
        }
        else
        {
            k.push(i);
        }
    }
    while(!k.empty())
    {
        ans[k.top()]=maxx;
        maxx--;
        k.pop();
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d ",ans[i]);
    }
    return 0;
}

I无他 可可爱爱的板子 QwQ早知道比赛的时候敲一下了 不知道自己在想什么 居然以为要分层什么的 就在那里干坐着啊啊啊啊啊 抱憾终生

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " == " << x << endl;
#define INF 0x3f3f3f
#define ll long long
const ll int MAX_N = 5e5 + 10;
char map1[40][40];
char map2[40][40];
struct node
{
    int x1, y1;
    int x2, y2;
    string way;
    node(int a, int b, int c, int d, string e) : x1(a), y1(b), x2(c), y2(d) { way = e; }
};

////
int dx1[] = {1, 0, 0, -1}; ///左边的🐧
int dy1[] = {0, -1, 1, 0};
////
int dx2[] = {1, 0, 0, -1}; ///右边的🐧
int dy2[] = {0, 1, -1, 0};
////
char fx[] = {'D', 'L', 'R', 'U'};
char ff[] = {'U', 'R', 'L', 'D'};
////
bool vis[22][22][22][22] = {0};

bool judge(int x, int y)
{
    return x && y && x <= 20 && y <= 20;
}

node point(0, 0, 0, 0, "");
void bfs()
{
    queue<node> q;
    q.push(node(20, 20, 20, 1, ""));
    vis[20][20][20][1] = 1;
    while (!q.empty())
    {
        node qd = q.front();
        q.pop();

        int x1 = qd.x1;
        int x2 = qd.x2;
        int y1 = qd.y1;
        int y2 = qd.y2;

        string way = qd.way;

        if (x1 == 1 && y1 == 20 && x2 == 1 && y2 == 1)
        {
            point = qd;
            break;
        }
        for (int i = 0; i < 4; i++)
        {

            int xx1 = x1 + dx1[i];
            int yy1 = y1 + dy1[i];
            int xx2 = x2 + dx2[i];
            int yy2 = y2 + dy2[i];

            if (xx1 < 1 || xx1 > 20 || yy1 < 1 || yy1 > 20 || map1[xx1][yy1] == '#')
            {
                xx1 = x1;
                yy1 = y1;
            }
            if (xx2 < 1 || xx2 > 20 || yy2 < 1 || yy2 > 20 || map2[xx2][yy2] == '#')
            {
                xx2 = x2;
                yy2 = y2;
            }
            if (vis[xx1][yy1][xx2][yy2])
                continue;
            vis[xx1][yy1][xx2][yy2] = 1;
            q.push(node(xx1, yy1, xx2, yy2, way + fx[i]));
        }
    }
    printf("%d\n", point.way.length());
    cout << point.way << endl;
}
int main()
{
    for (int i = 1; i <= 20; i++)
    {
        scanf("%s", map1[i] + 1);
        scanf("%s", map2[i] + 1);
    }
    bfs();

    int x1 = 20, y1 = 20;
    int x2 = 20, y2 = 1;
    map1[20][20] = 'A';
    map2[20][1] = 'A';
    string a = point.way;
    for (int i = 0; i < a.length(); i++)
    {

        int pos;
        if (a[i] == 'D')
            pos = 0;
        else if (a[i] == 'L')
            pos = 1;
        else if (a[i] == 'R')
            pos = 2;
        else
            pos = 3;

        int xx1 = x1 + dx1[pos];
        int yy1 = y1 + dy1[pos];
        int xx2 = x2 + dx2[pos];
        int yy2 = y2 + dy2[pos];
        if (xx1 < 1 || xx1 > 20 || yy1 < 1 || yy1 > 20 || map1[xx1][yy1] == '#')
        {
            xx1 = x1;
            yy1 = y1;
        }
        if (xx2 < 1 || xx2 > 20 || yy2 < 1 || yy2 > 20 || map2[xx2][yy2] == '#')
        {
            xx2 = x2;
            yy2 = y2;
        }
        x1 = xx1;
        y1 = yy1;
        x2 = xx2;
        y2 = yy2;
        map1[x1][y1] = 'A';
        map2[x2][y2] = 'A';
    }
    for (int i = 1; i <= 20; i++)
    {
        printf("%s %s\n", map1[i] + 1, map2[i] + 1);
    }
    system("pause");
}

G下面的思路还是有问题 不太明白 放着吧

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " == " << x << endl;
#define ll long long
const ll int MAX_N = 1e5 + 10;
struct node
{
    int l, r; ///左区间端点和右区间端点
    int len;  ///区间长度
    bool operator<(node b)
    {
        if(b.l==l)
            return r > b.r;
        return l < b.l;////这样子区间尽量地长,而且包含的数组多的在前面
    }
} a[MAX_N],s2[MAX_N];///被其他组包含的组
int tot2 = 0;

int s1[MAX_N] = {0};///包含着其他组的组
int tot1 = 0;


int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        a[i].l=x;
        a[i].r=y;
        a[i].len = y - x;
    }
    sort(a + 1, a + n + 1);
    ll maxxr = INT_MAX;
    for (int i = 1;i<=n;i++)
    {
        if((ll)a[i].r>maxxr)
        {
            tot1++;
            s1[tot1] = a[i].len;///当前左端点最短的时候,右区间达到最大距离
        }
        else
        {
            tot2++;
            maxxr = a[i].r;
            s2[tot2] = a[i];
        }
        sort(s1 + 1, s1 + 1 + tot1, greater<int>());
        ///从大到小排序,这样子可以获得最大的可能前缀和
        for(int i=1;i<=tot1;i++)
        {
            s1[i] = s1[i - 1] + s1[i];
        }
        
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值