CSES - Datatähti 2024 loppu - Results
Submission details
Task:Retkeily
Sender:Kemm1706
Submission time:2024-01-20 16:57:01 +0200
Language:C++11
Status:READY
Result:25
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED15
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3, 4details
#2ACCEPTED0.00 s1, 2, 3, 4details
#3ACCEPTED0.00 s1, 2, 3, 4details
#4ACCEPTED0.00 s1, 2, 3, 4details
#5ACCEPTED0.00 s1, 2, 3, 4details
#6ACCEPTED0.00 s1, 2, 4details
#7ACCEPTED0.00 s1, 2, 4details
#8ACCEPTED0.00 s1, 2, 3, 4details
#9ACCEPTED0.00 s1, 2, 4details
#10ACCEPTED0.01 s1, 2, 3, 4details
#11ACCEPTED0.00 s1, 2, 4details
#12ACCEPTED0.00 s1, 2, 4details
#13ACCEPTED0.00 s1, 2, 4details
#14ACCEPTED0.00 s1, 2, 4details
#15ACCEPTED0.01 s1, 2, 4details
#16ACCEPTED0.26 s2, 4details
#17ACCEPTED0.25 s2, 4details
#18ACCEPTED0.26 s2, 4details
#19ACCEPTED0.29 s2, 3, 4details
#20ACCEPTED0.28 s2, 4details
#21ACCEPTED0.32 s2, 3, 4details
#22ACCEPTED0.00 s1, 3, 4details
#23ACCEPTED0.01 s1, 3, 4details
#240.01 s3, 4details
#250.07 s3, 4details
#260.07 s3, 4details
#270.07 s3, 4details
#280.07 s3, 4details
#29ACCEPTED0.00 s1, 4details
#30ACCEPTED0.00 s1, 4details
#31ACCEPTED0.00 s1, 4details
#32ACCEPTED0.00 s1, 4details
#33ACCEPTED0.01 s1, 4details
#34ACCEPTED0.01 s1, 4details
#35ACCEPTED0.00 s1, 4details
#36ACCEPTED0.01 s1, 4details
#370.01 s4details
#380.07 s4details
#390.07 s4details
#400.07 s4details
#410.05 s4details

Compiler report

input/code.cpp: In function 'void solve2(ll, ll)':
input/code.cpp:100:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  100 |         auto [w, u] = q.top();
      |              ^
input/code.cpp:65:11: warning: unused variable 'j' [-Wunused-variable]
   65 |     ll i, j, minn, x = -1, y = -1, ans = -1, a, b, k = 1;
      |           ^
input/code.cpp:65:14: warning: unused variable 'minn' [-Wunused-variable]
   65 |     ll i, j, minn, x = -1, y = -1, ans = -1, a, b, k = 1;
      |              ^~~~
input/code.cpp: In function 'int main()':
input/code.cpp:131:14: warning: unused variable 'i' [-Wunused-variable]
  131 |     ll n, m, i, a, b, j, ans = -1, minn;
      |              ^
input/code.cpp:131:17: warning: unused variable 'a' [-Wunused-variable]
  131 |     ll n, m, i, a, b, j, ans = -1, minn;
      |                 ^
input/code.cpp:131:20: warning: unused variable 'b' [-Wunused-variable]
  131 |     ll n, m, i, a, b, j, ans = -1, minn;
      |...

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef pair <ll, ll> pl;
typedef vector <pl> vpl;
typedef vector <bool> vb;

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll nmax = 1e6;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

ll cal(ll n, ll m, ll x, ll y)
{
    return n * (x - 1) + m;
}

pl d[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool valid(ll n, ll m, ll x, ll y)
{
    return n > 0 && m > 0 && n <= x && m <= y;
}

void solve1(ll n, ll m)
{
    vpl r(n), f(m);
    ll i, j, minn, ans = -1, a, b;
    for(i = 0; i < n; i++)
    {
        cin >> a >> b;
        r[i] = {a, b};
    }
    for(i = 0; i < m; i++)
    {
        cin >> a >> b;
        f[i] = {a, b};
    }

    for(j = 0; j < m; j++)
    {
        minn = inf;
        for(i = 0; i < n; i++)
            minn = min(minn, abs(r[i].fi - f[j].fi) + abs(r[i].se - f[j].se));
        ans = max(ans, minn);
    }

    cout << ans;
}

pl operator + (pl a, pl b)
{
    return {a.fi + b.fi, a.se + b.se};
}

void solve2(ll n, ll m)
{
    vpl r(n), f(m);
    ll i, j, minn, x = -1, y = -1, ans = -1, a, b, k = 1;
    for(i = 0; i < n; i++)
    {
        cin >> a >> b;
        r[i] = {a, b};
        x = max(x, a);
        y = max(y, b);
    }

    for(i = 0; i < m; i++)
    {
        cin >> a >> b;
        f[i] = {a, b};
        x = max(x, a);
        y = max(y, b);
    }

    sort(r.begin(), r.end());
    r.resize(unique(r.begin(), r.end()) - r.begin());
    sort(f.begin(), f.end());
    f.resize(unique(f.begin(), f.end()) - f.begin());


    vl val(x * y + 5, inf);
    priority_queue <pair <ll, pl>, vector <pair <ll, pl>>, greater<pair <ll, pl>>> q;
    for(i = 0; i < n; i++)
    {
        val[cal(r[i].fi, r[i].se, x, y)] = 0;
        q.push({0, r[i]});
    }

    //cerr << x << " " << y << "\n";

    while(!q.empty())
    {
        auto [w, u] = q.top();
        q.pop();
        for(i = 0; i < 4; i++)
        {
            pl v = u + d[i];
            k = cal(v.fi, v.se, x, y);
            //cerr << v.fi << " " << v.se << "\n";
            //cerr << valid(v.fi, v.se, x, y) << " " << val[k] <<  "\n";
            if(valid(v.fi, v.se, x, y) && val[k] > w + 1)
            {
                //cerr << v.fi << " " << v.se << "\n";
                val[k] = w + 1;
                q.push({val[k], v});
            }
        }
    }

    /*for(i = 0; i <= 49; i++)
        cout << val[i] << "\n";*/

    for(i = 0; i < m; i++)
        ans = max(ans, val[cal(f[i].fi, f[i].se, x, y)]);
    cout << ans;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m, i, a, b, j, ans = -1, minn;
    cin >> n >> m;

    if(max(n, m) <= 1000)
        solve1(n, m);
    else
        solve2(n, m);


    return 0;
}

/*
4 2
1 1
5 2
2 6
4 7
1 3
7 5
*/

Test details

Test 1

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
1 1
6 6
8 9

correct output
5

user output
5

Test 2

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
5 5
5 10
8 10
1 2
4 10
...

correct output
4

user output
4

Test 3

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10 10
5 2
1 10
6 10
5 5
...

correct output
5

user output
5

Test 4

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
10 5
6 1
8 9
3 2
6 6
...

correct output
3

user output
3

Test 5

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
5 10
10 10
6 2
10 9
8 7
...

correct output
7

user output
7

Test 6

Group: 1, 2, 4

Verdict: ACCEPTED

input
1 1
55 68
28 42

correct output
53

user output
53

Test 7

Group: 1, 2, 4

Verdict: ACCEPTED

input
100 100
52 56
58 96
3 99
18 1
...

correct output
20

user output
20

Test 8

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
1000 100
60 40
68 47
30 32
74 52
...

correct output
5

user output
5

Test 9

Group: 1, 2, 4

Verdict: ACCEPTED

input
100 1000
44 26
18 81
18 6
83 90
...

correct output
20

user output
20

Test 10

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
1000 1000
15 13
23 79
81 43
30 40
...

correct output
7

user output
7

Test 11

Group: 1, 2, 4

Verdict: ACCEPTED

input
1 1
948 495
227 149

correct output
1067

user output
1067

Test 12

Group: 1, 2, 4

Verdict: ACCEPTED

input
100 100
114 530
748 841
612 709
810 232
...

correct output
203

user output
203

Test 13

Group: 1, 2, 4

Verdict: ACCEPTED

input
1000 100
225 82
265 691
978 367
993 396
...

correct output
50

user output
50

Test 14

Group: 1, 2, 4

Verdict: ACCEPTED

input
100 1000
848 668
189 716
451 80
626 973
...

correct output
192

user output
192

Test 15

Group: 1, 2, 4

Verdict: ACCEPTED

input
1000 1000
931 656
382 809
666 609
1000 923
...

correct output
66

user output
66

Test 16

Group: 2, 4

Verdict: ACCEPTED

input
10000 1000
961 158
561 313
991 125
821 964
...

correct output
18

user output
18

Test 17

Group: 2, 4

Verdict: ACCEPTED

input
1000 10000
428 1000
485 958
46 915
582 127
...

correct output
67

user output
67

Test 18

Group: 2, 4

Verdict: ACCEPTED

input
10000 10000
503 849
367 829
448 926
362 512
...

correct output
22

user output
22

Test 19

Group: 2, 3, 4

Verdict: ACCEPTED

input
100000 10000
111 705
808 24
69 858
961 122
...

correct output
7

user output
7

Test 20

Group: 2, 4

Verdict: ACCEPTED

input
10000 100000
844 874
339 315
819 918
627 936
...

correct output
27

user output
27

Test 21

Group: 2, 3, 4

Verdict: ACCEPTED

input
100000 100000
100 468
303 899
784 458
505 54
...

correct output
8

user output
8

Test 22

Group: 1, 3, 4

Verdict: ACCEPTED

input
10 10
451219 220496
369161 161920
241139 700531
811276 993633
...

correct output
10

user output
10

Test 23

Group: 1, 3, 4

Verdict: ACCEPTED

input
1000 1000
337358 113599
674585 852084
717817 983395
895431 391144
...

correct output
10

user output
10

Test 24

Group: 3, 4

Verdict:

input
10000 10000
636432 664736
341727 37864
469264 360610
545785 879043
...

correct output
10

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 25

Group: 3, 4

Verdict:

input
100000 100000
750315 55215
550921 465416
435380 742666
288479 495099
...

correct output
7

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 26

Group: 3, 4

Verdict:

input
100000 100000
416825 677767
347155 162523
602643 936386
181956 517732
...

correct output
8

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 27

Group: 3, 4

Verdict:

input
100000 100000
155202 457283
421100 55104
604625 136495
841749 569346
...

correct output
9

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 28

Group: 3, 4

Verdict:

input
100000 100000
290146 808578
489810 268578
956361 635214
939844 534435
...

correct output
10

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 29

Group: 1, 4

Verdict: ACCEPTED

input
10 10
668880 791821
226050 188133
859493 736633
290460 838926
...

correct output
342068

user output
342068

Test 30

Group: 1, 4

Verdict: ACCEPTED

input
100 100
729367 697755
338457 742774
535371 701216
93743 555995
...

correct output
285526

user output
285526

Test 31

Group: 1, 4

Verdict: ACCEPTED

input
100 1000
50870 539657
476809 462765
311713 355546
901838 829393
...

correct output
255789

user output
255789

Test 32

Group: 1, 4

Verdict: ACCEPTED

input
1000 100
182415 659794
581623 510256
594984 525993
367726 938015
...

correct output
50501

user output
50501

Test 33

Group: 1, 4

Verdict: ACCEPTED

input
1000 1000
668976 422918
350791 933024
893307 88057
278098 13847
...

correct output
57897

user output
57897

Test 34

Group: 1, 4

Verdict: ACCEPTED

input
1000 1000
701742 116334
278337 368995
187245 133297
273083 369187
...

correct output
60537

user output
60537

Test 35

Group: 1, 4

Verdict: ACCEPTED

input
10 10
693836 74759
61731 520849
666762 45364
559335 979511
...

correct output
417182

user output
417182

Test 36

Group: 1, 4

Verdict: ACCEPTED

input
1000 1000
207572 43521
513003 683552
58800 253576
16541 558695
...

correct output
69609

user output
69609

Test 37

Group: 4

Verdict:

input
10000 10000
89066 605574
504488 66068
959128 348414
68004 849599
...

correct output
22936

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 38

Group: 4

Verdict:

input
100000 100000
20824 214259
463783 904059
603615 769692
789080 399093
...

correct output
8070

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 39

Group: 4

Verdict:

input
100000 100000
811424 464350
336948 946495
204883 914446
171888 431769
...

correct output
8240

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 40

Group: 4

Verdict:

input
100000 100000
850810 902354
292608 63461
223139 188900
197760 995048
...

correct output
7769

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 41

Group: 4

Verdict:

input
100000 100000
1 1
1 2
1 3
1 4
...

correct output
1899999

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc