QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#132494#6563. Four SquareSorting#AC ✓3ms4040kbC++233.1kb2023-07-30 00:41:462023-07-30 00:41:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-30 00:41:49]
  • 评测
  • 测评结果:AC
  • 用时:3ms
  • 内存:4040kb
  • [2023-07-30 00:41:46]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
template<class T> void check_min(T &a, const T &b){ a = (a < b) ? a : b; }
template<class T> void check_max(T &a, const T &b){ a = (a > b) ? a : b; }
#define all(x) (x).begin(), (x).end()

const int N = 2000 + 3;

pair<int, int> s[4];
vector<pair<int, int>> v[N];
int n;

void fix(int x){
    for(int i = 0; i < (int)v[x].size() - 1; ++i){
        if(v[x][i].second == v[x][i + 1].first - 1){
            v[x][i].second = v[x][i + 1].second;
            v[x].erase(v[x].begin() + i + 1);
            break;
        }
    }
}

bool try_insert(int x, int y1, int y2){
    bool inserted = false;
    for(int i = 0; i < v[x].size(); ++i){
        if(y1 <= v[x][i].first && v[x][i].first <= y2)
            return false;
         if(y1 <= v[x][i].second && v[x][i].second <= y2)
            return false;
        if(v[x][i].first <= y1 && y1 <= v[x][i].second)
            return false;
        if(v[x][i].first <= y2 && y2 <= v[x][i].second)
            return false;

        if(y2 < v[x][i].first){
            v[x].insert(v[x].begin() + i, {y1, y2});
            inserted = true;
            break;
        }
    }
    if(!inserted)
        v[x].push_back({y1, y2});

    fix(x);
    fix(x);
    return true;
}

bool solve(int idx){
    if(idx == 4) return true;
    
    vector<pair<int, int>> v2[N];
    for(int i = 1; i <= n; ++i)
        v2[i] = v[i];

    int x, y;
    for(int i = 1; i <= n; ++i){
        if(v[i].empty() || v[i][0].first != 1 || v[i][0].second != n){
            if(v[i].empty()){
                x = i;
                y = 1;
                break;
            }
            if(v[i][0].first != 1){
                x = i;
                y = 1;
                break;
            }
            x = i;
            y = v[i][0].second + 1;
            break;
        }
    }

    for(int i = 0; i < 2; ++i, swap(s[idx].first, s[idx].second)){
        if(x + s[idx].first - 1 > n || y + s[idx].second - 1 > n)
            continue;
        for(int j = 1; j <= n; ++j)
            v[j] = v2[j];

        bool ok = true;
        for(int j = x; j < x + s[idx].first; ++j){
            if(!try_insert(j, y, y + s[idx].second - 1)){
                ok = false;
                break;
            }
        }

        if(!ok) continue;
        if(solve(idx + 1)) return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int sum = 0;
    for(int i = 0; i < 4; ++i){
        cin >> s[i].first >> s[i].second;
        sum += s[i].first * s[i].second;
    }

    for(int j = 0; j * j <= sum; ++j){
        if(j * j == sum){
            n = j;
        }
    }

    if(n * n != sum){
        cout << "0\n";
        return 0;
    }

    sort(s, s + 4);
    do{
        for(int i = 1; i <= n; ++i)
            v[i].clear();
        if(solve(0)){
            cout << 1 << "\n";
            return 0;
        }
    }
    while(next_permutation(s, s + 4));
    cout << 0 << "\n";
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3756kb

input:

1 1
1 1
1 1
1 1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

3 1
3 3
2 2
3 3

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

2 8
2 8
2 8
2 8

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

5 3
5 5
3 3
3 5

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3548kb

input:

1 2
4 8
16 32
64 128

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3732kb

input:

4 4
2 1
4 4
2 1

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3720kb

input:

995 51
559 565
154 536
56 780

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Accepted
time: 3ms
memory: 3772kb

input:

391 694
540 42
240 937
691 246

output:

0

result:

ok single line: '0'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3796kb

input:

519 411
782 710
299 45
21 397

output:

0

result:

ok single line: '0'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3780kb

input:

96 960
948 18
108 82
371 576

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

3 2
4 3
3 1
1 4

output:

0

result:

ok single line: '0'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

4 3
1 2
4 4
3 2

output:

0

result:

ok single line: '0'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3804kb

input:

4 4
1 3
5 4
2 5

output:

0

result:

ok single line: '0'

Test #14:

score: 0
Accepted
time: 1ms
memory: 4040kb

input:

1000 1000
1000 1000
1000 1000
1000 1000

output:

1

result:

ok single line: '1'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3920kb

input:

1000 999
998 1000
997 1000
997 997

output:

1

result:

ok single line: '1'

Test #16:

score: 0
Accepted
time: 1ms
memory: 3748kb

input:

1 3
3 3
3 3
4 7

output:

1

result:

ok single line: '1'

Test #17:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

2 5
5 4
7 1
6 2

output:

1

result:

ok single line: '1'

Test #18:

score: 0
Accepted
time: 1ms
memory: 3800kb

input:

12 2
12 7
7 12
16 4

output:

1

result:

ok single line: '1'

Test #19:

score: 0
Accepted
time: 1ms
memory: 3740kb

input:

7 2
2 14
5 14
7 12

output:

1

result:

ok single line: '1'

Test #20:

score: 0
Accepted
time: 1ms
memory: 3780kb

input:

32 36
5 1
1 37
35 5

output:

1

result:

ok single line: '1'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3780kb

input:

28 30
30 1
31 1
2 30

output:

1

result:

ok single line: '1'

Test #22:

score: 0
Accepted
time: 1ms
memory: 3756kb

input:

66 68
9 11
7 66
9 64

output:

1

result:

ok single line: '1'

Test #23:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

59 44
25 44
40 32
40 52

output:

1

result:

ok single line: '1'

Test #24:

score: 0
Accepted
time: 1ms
memory: 3736kb

input:

4 4
2 3
4 2
3 2

output:

1

result:

ok single line: '1'