呜呜呜dalao路过给蒟蒻看一下(90pts,第二个点居然MLE了)

P1434 [SHOI2002] 滑雪

aldol_reaction @ 2020-11-22 21:25:17

我开的数组也不大啊QAQ真的要疯了求救求救


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define maxn 105
typedef pair<pair<int, int>, int> ppii;

const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int r, c, ans, a[maxn][maxn], z;
queue<ppii> q;

struct node {
    int x, y, val;
} w[maxn*maxn];

bool cmp(node a, node b) {
    return a.val > b.val;
}

int bfs(int x, int y) {
    int ret = 1;
    q.push(ppii(make_pair(x, y), 1));
    while(!q.empty()) {
        ppii p = q.front();
        q.pop();
        for(int i = 0; i < 4; ++i) {
            int x = p.first.first, y = p.first.second, cnt = p.second;
            int nx = x+dx[i], ny = y+dy[i];
            if(0 <= nx && nx < r && 0 <= ny && ny < c && a[x][y] > a[nx][ny]) {
                ++cnt;
                ret = max(cnt, ret);
                q.push(ppii(make_pair(nx, ny), cnt));
            }
        }
    }
    return ret;
}

int main() {
    cin >> r >> c;
    for(int i = 0; i < r; ++i) {
        for(int j = 0; j < c; ++j, ++z) {
            scanf("%d", &a[i][j]);
            w[z].val = a[i][j];
            w[z].x = i;
            w[z].y = j;
        }
    }
    sort(w, w+r*c, cmp);
    ans = bfs(w[0].x, w[0].y);
    for(int i = 1; i < r*z; ++i) {
        if(ans >= w[i].val) {
            cout << ans;
            return 0;
        }
        ans = max(bfs(w[i].x, w[i].y), ans);
    }
    cout << ans;
    return 0;
}

    return 0;
}

by LoverBoyInMacau @ 2020-11-22 21:56:44

终于有码风跟我像的人了!!!感动


by aldol_reaction @ 2020-11-22 22:46:09

@LSGJ—大水怪 那不知您是否看看蒟蒻到底哪里的问题啊QAQ


by aldol_reaction @ 2020-11-22 22:54:14

@LSGJ—大水怪 抱歉多打了一个return 0; 如果您有空能看下吗蒟蒻不胜感激,要疯了


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define maxn 105
typedef pair<pair<int, int>, int> ppii;

const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int r, c, ans, a[maxn][maxn], z;
queue<ppii> q;

struct node {
    int x, y, val;
} w[maxn*maxn];

bool cmp(node a, node b) {
    return a.val > b.val;
}

int bfs(int x, int y) {
    int ret = 1;
    q.push(ppii(make_pair(x, y), 1));
    while(!q.empty()) {
        ppii p = q.front();
        q.pop();
        for(int i = 0; i < 4; ++i) {
            int x = p.first.first, y = p.first.second, cnt = p.second;
            int nx = x+dx[i], ny = y+dy[i];
            if(0 <= nx && nx < r && 0 <= ny && ny < c && a[x][y] > a[nx][ny]) {
                ++cnt;
                ret = max(cnt, ret);
                q.push(ppii(make_pair(nx, ny), cnt));
            }
        }
    }
    return ret;
}

int main() {
    cin >> r >> c;
    for(int i = 0; i < r; ++i) {
        for(int j = 0; j < c; ++j, ++z) {
            scanf("%d", &a[i][j]);
            w[z].val = a[i][j];
            w[z].x = i;
            w[z].y = j;
        }
    }
    sort(w, w+r*c, cmp);
    ans = bfs(w[0].x, w[0].y);
    for(int i = 1; i < r*z; ++i) {
        if(ans >= w[i].val) {
            cout << ans;
            return 0;
        }
        ans = max(bfs(w[i].x, w[i].y), ans);
    }
    cout << ans;
    return 0;
}

by LoverBoyInMacau @ 2020-11-23 07:53:13

我把我的代码给您参考一下??
我觉得您是爆栈或者是pair的问题

#include<bits/stdc++.h>
#define re register
#define il inline
#define ll long long
#define MAXR 105
#define MAXC 105
#define rep(i,a,b) for (re int i = a;i <= b;++ i)
#define fin(a)  freopen(#a".in","r",stdin)
#define fout(a) freopen(#a".out","w",stdout)

using namespace std;

int a[MAXR][MAXC],f[MAXR][MAXC];
int R,C,sx,sy,flag = -1983,ans = -1983;

il int Max (int a,int b) {
    return a > b ? a : b;
}

il int dfs (int x,int y) {
    if (x < 1 || x > R || y < 1 || y > C)
        return 0;
    if (f[x][y])
        return f[x][y];

    f[x][y] = 1;
    if (a[x + 1][y] < a[x][y])
        f[x][y] = Max(f[x][y],dfs(x + 1,y) + 1);
    if (a[x][y + 1] < a[x][y])
        f[x][y] = Max(f[x][y],dfs(x,y + 1) + 1);
    if (a[x - 1][y] < a[x][y])
        f[x][y] = Max(f[x][y],dfs(x - 1,y) + 1);
    if (a[x][y - 1] < a[x][y])
        f[x][y] = Max(f[x][y],dfs(x,y - 1) + 1);

    return f[x][y];
}

int main () {
    scanf ("%d%d",&R,&C);
    rep(i,1,R)
        rep(j,1,C) {
            scanf ("%d",&a[i][j]);
        }

    rep(i,1,R)
        rep(j,1,C) {
            dfs(i,j);
            ans = Max(ans,f[i][j]);
        }
    printf ("%d",ans);
    return 0;
}

by LoverBoyInMacau @ 2020-11-23 08:03:56

@aldol_reaction 要用深搜+记忆化


by aldol_reaction @ 2020-11-23 08:18:04

@LSGJ—大水怪 谢谢dalao!鞠躬


by LoverBoyInMacau @ 2020-11-23 12:12:45

@aldol_reaction 加油加油!


|