QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#850785#8355. T3rqoi031#0 0ms0kbC++204.4kb2025-01-10 11:50:522025-01-10 11:50:52

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 11:50:52]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2025-01-10 11:50:52]
  • 提交

answer

#include<stdio.h>
#include<algorithm>
#include<numeric>
#include<vector>
#include<tuple>
#include<cassert>
constexpr int N{300};
enum direction {
    U,D,L,R
};
char ch[4]{'U','D','L','R'};
int dx[4]{-1,1,0,0},dy[4]{0,0,-1,1};
int cnt[4][N+5];
int perm[N+5],aux[N+5],tag[N+5][N+5];
int vis[N+5][N+5];
std::vector<std::tuple<int,int>> opt;
int main() {
    freopen("0.in","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=0;i!=4;i++) {
        for(int j=1;j<=n;j++) {
            scanf("%d",cnt[i]+j);
        }
    }
    for(int i=1;i<=n;i++) {
        if(cnt[U][i]+cnt[D][i]>n||cnt[L][i]+cnt[R][i]>n) {
            return puts("NO"),0;
        }
    }
    for(int i=1;i<=n;i++) {
        aux[i]=cnt[U][i]+cnt[D][i];
    }
    for(int i=1;i<=n;i++) {
        std::iota(perm+1,perm+n+1,1);
        std::sort(perm+1,perm+n+1,[&](const int &x,const int &y)->bool {
            return aux[x]<aux[y];
        });
        for(int j=1;j<=cnt[L][i]+cnt[R][i];j++) {
            ++aux[perm[j]];
            tag[i][perm[j]]=2;
        }
    }
    if(*std::max_element(aux+1,aux+n+1)!=n) {
        return puts("NO"),0;
    }
    for(int i=1;i<=n;i++) {
        int p{0};
        for(int c=0;c!=cnt[L][i];c+=(tag[i][++p]==2));
        for(int j=1;j<=p;j++) {
            if(tag[i][j]==2) {
                tag[i][j]=L;
            }
        }
        for(int j=p+1;j<=n;j++) {
            if(tag[i][j]==2) {
                tag[i][j]=R;
            }
        }
    }
    for(int i=1;i<=n;i++) {
        int p{0};
        for(int c=0;c!=cnt[U][i];c+=(tag[++p][i]==0));
        for(int j=1;j<=p;j++) {
            if(tag[j][i]==0) {
                tag[j][i]=U;
            }
        }
        for(int j=p+1;j<=n;j++) {
            if(tag[j][i]==0) {
                tag[j][i]=D;
            }
        }
    }
    for(int i=1;i<=n;i++) {
        assert(cnt[U][i]==std::count_if(tag+1,tag+n+1,[&](const auto &x)->bool {return x[i]==U;}));
        assert(cnt[D][i]==std::count_if(tag+1,tag+n+1,[&](const auto &x)->bool {return x[i]==D;}));
        assert(cnt[L][i]==std::count(tag[i]+1,tag[i]+n+1,L));
        assert(cnt[R][i]==std::count(tag[i]+1,tag[i]+n+1,R));
    }
    // for(int i=1;i<=n;i++) {
    //     for(int j=1;j<=n;j++) {
    //         putchar(ch[tag[i][j]]);
    //     }
    //     putchar('\n');
    // }
    const auto check([&]()->bool {
        for(int i=1;i<=n;i++) {
            std::fill(vis[i]+1,vis[i]+n+1,0);
        }
        int clk{0};
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(!vis[i][j]) {
                    ++clk;
                    std::vector<std::tuple<int,int>> tmp;
                    int x{i},y{j};
                    while(1<=x&&x<=n&&1<=y&&y<=n&&!vis[x][y]) {
                        tmp.emplace_back(x,y);
                        vis[x][y]=clk,std::tie(x,y)=std::make_tuple(x+dx[tag[x][y]],y+dy[tag[x][y]]);
                    }
                    if(vis[x][y]==clk) {
                        for(int p=tmp.size()-1;p!=-1;p--) {
                            auto &[_x,_y](tmp[p]);
                            if(_x==x&&_y==y) {
                                break;
                            }
                            int _x2{_x+dx[tag[_x][_y]]},_y2{_y+dy[tag[_x][_y]]};
                            std::swap(tag[_x][_y],tag[_x2][_y2]);
                        }
                        return true;
                    }
                }
            }
        }
        return false;
    });
    // while(check());
    // for(int i=1;i<=n;i++) {
    //     for(int j=1;j<=n;j++) {
    //         putchar(ch[tag[i][j]]);
    //     }
    //     putchar('\n');
    // }
    opt.clear();
    for(int i=1;i<=n;i++) {
        std::fill(vis[i]+1,vis[i]+n+1,0);
    }
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(!vis[i][j]) {
                std::vector<std::tuple<int,int>> tmp;
                int x{i},y{j};
                while(1<=x&&x<=n&&1<=y&&y<=n&&!vis[x][y]) {
                    tmp.emplace_back(x,y);
                    vis[x][y]=1,std::tie(x,y)=std::make_tuple(x+dx[tag[x][y]],y+dy[tag[x][y]]);
                }
                opt.insert(opt.end(),tmp.rbegin(),tmp.rend());
            }
        }
    }
    for(auto &[x,y]:opt) {
        printf("%c%d\n",ch[tag[x][y]],tag[x][y]&2?x:y);
    }
    return 0;
}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

1
0
1
0
0

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #11:

score: 0
Runtime Error

input:

290
28 35 25 29 26 23 36 36 24 39 27 36 24 26 31 28 30 27 25 32 37 26 38 20 31 30 30 35 33 24 25 27 20 26 32 26 33 38 25 29 27 34 25 31 21 22 33 33 24 24 31 31 26 31 25 28 33 27 30 27 24 30 29 26 32 36 20 31 28 23 22 23 37 32 32 27 33 30 27 42 25 31 25 25 26 32 25 35 28 27 33 26 35 39 23 22 26 29 35...

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

289
30 29 31 35 25 34 26 28 25 25 44 26 33 30 27 30 33 37 26 27 43 28 28 40 31 36 21 26 35 28 31 29 41 25 30 25 35 28 38 24 26 26 24 24 25 27 18 44 31 24 37 28 26 31 27 32 29 24 24 32 20 35 26 39 30 28 33 30 27 28 37 35 29 22 27 27 31 30 25 31 31 22 30 34 33 31 30 29 41 26 38 36 28 28 21 22 31 34 32...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #31:

score: 0
Runtime Error

input:

1
1
0
0
0

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #77:

score: 0
Runtime Error

input:

299
72 66 62 73 80 85 70 93 79 88 77 72 67 70 73 84 77 62 80 77 88 63 69 76 73 91 64 76 75 65 74 71 71 68 81 80 74 77 69 75 73 87 90 82 86 79 76 83 69 72 73 73 75 78 76 80 66 76 67 75 72 71 77 63 80 68 82 63 74 67 74 72 73 76 71 72 66 78 74 65 69 80 76 71 72 74 77 70 85 60 65 89 66 64 77 63 78 82 80...

output:


result: