답안 #47039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47039 2018-04-26T16:03:40 Z SpaimaCarpatilor Cultivation (JOI17_cultivation) C++17
60 / 100
2000 ms 1212 KB
#include<bits/stdc++.h>

using namespace std;

int N, R, C, x[309], y[309];
const int INF = 1e9 + 100;
long long ans = 1LL << 60;
set < int > sa, sb, sab;

namespace ds {
    multiset < int > xs, dx;

    void init ()
    {
        xs.clear (), dx.clear ();
    }

    void delDif (int x)
    {
        dx.erase (dx.find (x));
    }

    void add (int x)
    {
        if (xs.find (x) != xs.end ()) xs.insert (x);
        else
        {
            xs.insert (x);
            auto it1 = xs.lower_bound (x), it2 = xs.upper_bound (x);
            if (it1 != xs.begin () && it2 != xs.end ())
                it1 --,
                delDif ((*it2) - (*it1)),
                it1 ++;
            if (it1 != xs.begin ())
                it1 --,
                dx.insert (x - (*it1));
            if (it2 != xs.end ())
                dx.insert ((*it2) - x);
        }
    }

    void del (int x)
    {
        if (xs.count (x) >= 2) xs.erase (xs.find (x));
        else
        {
            auto it1 = xs.lower_bound (x), it2 = xs.upper_bound (x);
            if (it1 != xs.begin () && it2 != xs.end ())
                it1 --,
                dx.insert ((*it2) - (*it1)),
                it1 ++;
            if (it1 != xs.begin ())
                it1 --,
                delDif (x - (*it1));
            if (it2 != xs.end ())
                delDif ((*it2) - x);
            xs.erase (x);
        }
    }

    int maxDif ()
    {
        if (dx.empty ()) return 0;
        return *(dx.rbegin ());
    }

    int getA ()
    {
        if (xs.empty ()) return INF;
        return (*xs.begin ()) - 1;
    }

    int getB ()
    {
        if (xs.empty ()) return INF;
        return C - (*xs.rbegin ());
    }
}

vector < pair < int, int > > events;
void solve (int up, int down)
{
    int minA = 0, minB = 0, minAB = 0;
    ds::init ();
    events.clear ();
    for (int i=1; i<=N; i++)
    {
        int lx = max (1, x[i] - up), dx = min (R, x[i] + down);
        events.push_back ({lx, -y[i]});
        if (dx + 1 <= R)
            events.push_back ({dx + 1, y[i]});
    }
    sort (events.begin (), events.end ());
    for (int ii=0; ii<events.size (); ii++)
    {
        int jj = ii;
        while (jj < events.size () && events[jj].first == events[ii].first)
        {
            if (events[jj].second < 0) ds::add (-events[jj].second);
            else ds::del (events[jj].second);
            jj ++;
        }
        jj --;
        ///[ii, jj]
        minA = max (minA, ds::getA ());
        minB = max (minB, ds::getB ());
        minAB = max (minAB, ds::maxDif () - 1);
        ///
        ii = jj;
    }
//    printf ("(%d, %d) -> (%d, %d, %d)\n", up, down, minA, minB, minAB);
    long long curr = (long long) max (minA + minB, minAB) + up + down;
    if (curr < ans)
        ans = curr;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d\n", &R, &C);
scanf ("%d", &N);
for (int i=1; i<=N; i++)
    scanf ("%d %d", &x[i], &y[i]);
for (int i=1; i<=N; i++)
    sa.insert (x[i] - 1), sb.insert (R - x[i]);
for (int i=1; i<=N; i++)
    for (int j=i + 1; j<=N; j++)
    if (x[i] != x[j])
    {
        int val = x[i] - x[j];
        if (val < 0) val = -val;
        sab.insert (val - 1);
    }
for (auto a : sa)
{
    set < int > psb;
    for (auto b : sb)
        psb.insert (b);
    for (auto ab : sab)
        if (ab >= a)
            psb.insert (ab - a);
    for (auto b : psb)
        solve (a, b);
}
printf ("%lld\n", ans);
return 0;
}

Compilation message

cultivation.cpp: In function 'void solve(int, int)':
cultivation.cpp:94:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int ii=0; ii<events.size (); ii++)
                    ~~^~~~~~~~~~~~~~~
cultivation.cpp:97:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (jj < events.size () && events[jj].first == events[ii].first)
                ~~~^~~~~~~~~~~~~~~~
cultivation.cpp: In function 'int main()':
cultivation.cpp:122:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d\n", &R, &C);
 ~~~~~~^~~~~~~~~~~~~~~~~~~
cultivation.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d", &N);
 ~~~~~~^~~~~~~~~~
cultivation.cpp:125:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &x[i], &y[i]);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 484 KB Output is correct
4 Correct 2 ms 484 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Correct 2 ms 496 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 612 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 2 ms 708 KB Output is correct
11 Correct 2 ms 708 KB Output is correct
12 Correct 2 ms 708 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 2 ms 708 KB Output is correct
15 Correct 2 ms 708 KB Output is correct
16 Correct 2 ms 708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 484 KB Output is correct
4 Correct 2 ms 484 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Correct 2 ms 496 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 612 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 2 ms 708 KB Output is correct
11 Correct 2 ms 708 KB Output is correct
12 Correct 2 ms 708 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 2 ms 708 KB Output is correct
15 Correct 2 ms 708 KB Output is correct
16 Correct 2 ms 708 KB Output is correct
17 Correct 3 ms 744 KB Output is correct
18 Correct 15 ms 792 KB Output is correct
19 Correct 8 ms 796 KB Output is correct
20 Correct 2 ms 796 KB Output is correct
21 Correct 20 ms 804 KB Output is correct
22 Correct 104 ms 936 KB Output is correct
23 Correct 4 ms 936 KB Output is correct
24 Correct 116 ms 936 KB Output is correct
25 Correct 89 ms 936 KB Output is correct
26 Correct 200 ms 952 KB Output is correct
27 Correct 226 ms 956 KB Output is correct
28 Correct 124 ms 956 KB Output is correct
29 Correct 183 ms 956 KB Output is correct
30 Correct 180 ms 956 KB Output is correct
31 Correct 196 ms 956 KB Output is correct
32 Correct 198 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 484 KB Output is correct
4 Correct 2 ms 484 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Correct 2 ms 496 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 612 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 2 ms 708 KB Output is correct
11 Correct 2 ms 708 KB Output is correct
12 Correct 2 ms 708 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 2 ms 708 KB Output is correct
15 Correct 2 ms 708 KB Output is correct
16 Correct 2 ms 708 KB Output is correct
17 Correct 3 ms 744 KB Output is correct
18 Correct 15 ms 792 KB Output is correct
19 Correct 8 ms 796 KB Output is correct
20 Correct 2 ms 796 KB Output is correct
21 Correct 20 ms 804 KB Output is correct
22 Correct 104 ms 936 KB Output is correct
23 Correct 4 ms 936 KB Output is correct
24 Correct 116 ms 936 KB Output is correct
25 Correct 89 ms 936 KB Output is correct
26 Correct 200 ms 952 KB Output is correct
27 Correct 226 ms 956 KB Output is correct
28 Correct 124 ms 956 KB Output is correct
29 Correct 183 ms 956 KB Output is correct
30 Correct 180 ms 956 KB Output is correct
31 Correct 196 ms 956 KB Output is correct
32 Correct 198 ms 980 KB Output is correct
33 Correct 3 ms 980 KB Output is correct
34 Correct 289 ms 1020 KB Output is correct
35 Correct 357 ms 1020 KB Output is correct
36 Correct 453 ms 1020 KB Output is correct
37 Correct 367 ms 1020 KB Output is correct
38 Correct 458 ms 1020 KB Output is correct
39 Correct 459 ms 1020 KB Output is correct
40 Correct 467 ms 1020 KB Output is correct
41 Correct 170 ms 1020 KB Output is correct
42 Correct 303 ms 1020 KB Output is correct
43 Correct 615 ms 1020 KB Output is correct
44 Correct 324 ms 1020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1020 KB Output is correct
2 Correct 31 ms 1020 KB Output is correct
3 Correct 35 ms 1020 KB Output is correct
4 Correct 27 ms 1020 KB Output is correct
5 Correct 34 ms 1020 KB Output is correct
6 Correct 18 ms 1020 KB Output is correct
7 Correct 4 ms 1020 KB Output is correct
8 Correct 17 ms 1020 KB Output is correct
9 Correct 22 ms 1020 KB Output is correct
10 Correct 33 ms 1020 KB Output is correct
11 Correct 40 ms 1020 KB Output is correct
12 Correct 53 ms 1020 KB Output is correct
13 Correct 4 ms 1020 KB Output is correct
14 Correct 15 ms 1020 KB Output is correct
15 Correct 8 ms 1020 KB Output is correct
16 Correct 65 ms 1020 KB Output is correct
17 Correct 39 ms 1152 KB Output is correct
18 Correct 7 ms 1152 KB Output is correct
19 Correct 45 ms 1152 KB Output is correct
20 Correct 20 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1020 KB Output is correct
2 Correct 31 ms 1020 KB Output is correct
3 Correct 35 ms 1020 KB Output is correct
4 Correct 27 ms 1020 KB Output is correct
5 Correct 34 ms 1020 KB Output is correct
6 Correct 18 ms 1020 KB Output is correct
7 Correct 4 ms 1020 KB Output is correct
8 Correct 17 ms 1020 KB Output is correct
9 Correct 22 ms 1020 KB Output is correct
10 Correct 33 ms 1020 KB Output is correct
11 Correct 40 ms 1020 KB Output is correct
12 Correct 53 ms 1020 KB Output is correct
13 Correct 4 ms 1020 KB Output is correct
14 Correct 15 ms 1020 KB Output is correct
15 Correct 8 ms 1020 KB Output is correct
16 Correct 65 ms 1020 KB Output is correct
17 Correct 39 ms 1152 KB Output is correct
18 Correct 7 ms 1152 KB Output is correct
19 Correct 45 ms 1152 KB Output is correct
20 Correct 20 ms 1152 KB Output is correct
21 Execution timed out 2056 ms 1212 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 2 ms 484 KB Output is correct
4 Correct 2 ms 484 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Correct 2 ms 496 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 612 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 2 ms 708 KB Output is correct
11 Correct 2 ms 708 KB Output is correct
12 Correct 2 ms 708 KB Output is correct
13 Correct 2 ms 708 KB Output is correct
14 Correct 2 ms 708 KB Output is correct
15 Correct 2 ms 708 KB Output is correct
16 Correct 2 ms 708 KB Output is correct
17 Correct 3 ms 744 KB Output is correct
18 Correct 15 ms 792 KB Output is correct
19 Correct 8 ms 796 KB Output is correct
20 Correct 2 ms 796 KB Output is correct
21 Correct 20 ms 804 KB Output is correct
22 Correct 104 ms 936 KB Output is correct
23 Correct 4 ms 936 KB Output is correct
24 Correct 116 ms 936 KB Output is correct
25 Correct 89 ms 936 KB Output is correct
26 Correct 200 ms 952 KB Output is correct
27 Correct 226 ms 956 KB Output is correct
28 Correct 124 ms 956 KB Output is correct
29 Correct 183 ms 956 KB Output is correct
30 Correct 180 ms 956 KB Output is correct
31 Correct 196 ms 956 KB Output is correct
32 Correct 198 ms 980 KB Output is correct
33 Correct 3 ms 980 KB Output is correct
34 Correct 289 ms 1020 KB Output is correct
35 Correct 357 ms 1020 KB Output is correct
36 Correct 453 ms 1020 KB Output is correct
37 Correct 367 ms 1020 KB Output is correct
38 Correct 458 ms 1020 KB Output is correct
39 Correct 459 ms 1020 KB Output is correct
40 Correct 467 ms 1020 KB Output is correct
41 Correct 170 ms 1020 KB Output is correct
42 Correct 303 ms 1020 KB Output is correct
43 Correct 615 ms 1020 KB Output is correct
44 Correct 324 ms 1020 KB Output is correct
45 Correct 8 ms 1020 KB Output is correct
46 Correct 31 ms 1020 KB Output is correct
47 Correct 35 ms 1020 KB Output is correct
48 Correct 27 ms 1020 KB Output is correct
49 Correct 34 ms 1020 KB Output is correct
50 Correct 18 ms 1020 KB Output is correct
51 Correct 4 ms 1020 KB Output is correct
52 Correct 17 ms 1020 KB Output is correct
53 Correct 22 ms 1020 KB Output is correct
54 Correct 33 ms 1020 KB Output is correct
55 Correct 40 ms 1020 KB Output is correct
56 Correct 53 ms 1020 KB Output is correct
57 Correct 4 ms 1020 KB Output is correct
58 Correct 15 ms 1020 KB Output is correct
59 Correct 8 ms 1020 KB Output is correct
60 Correct 65 ms 1020 KB Output is correct
61 Correct 39 ms 1152 KB Output is correct
62 Correct 7 ms 1152 KB Output is correct
63 Correct 45 ms 1152 KB Output is correct
64 Correct 20 ms 1152 KB Output is correct
65 Execution timed out 2056 ms 1212 KB Time limit exceeded
66 Halted 0 ms 0 KB -