Submission #51685

# Submission time Handle Problem Language Result Execution time Memory
51685 2018-06-19T23:43:42 Z gs13105 None (JOI16_ho_t4) C++17
100 / 100
111 ms 9852 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <iterator>

using namespace std;

struct pos
{
    int x, y;
    bool operator <(const pos &p) const
    {
        return x != p.x ? x < p.x : y < p.y;
    }
    bool operator ==(const pos &p) const
    {
        return x == p.x && y == p.y;
    }
};

struct eve
{
    int x, e;
    bool operator <(const eve &a) const
    {
        return x != a.x ? x < a.x : e < a.e;
    }
};

inline int irem(int x, int y)
{
    assert(y != 0);
    y = abs(y);
    return (x % y + y) % y;
}

int kx, ky;
pair<pos, int> decom(pos p)
{
    if(kx != 0)
    {
        pos q;
        q.x = irem(p.x, kx);

        int t = (q.x - p.x) / kx;
        q.y = p.y + t * ky;

        return { q, t };
    }
    if(ky != 0)
    {
        pos q;
        q.y = irem(p.y, ky);

        int t = (q.y - p.y) / ky;
        q.x = p.x + t * kx;

        return { q, t };
    }
    return { p, 0 };
}

vector<pos> com;
inline int idx(pos p)
{
    return lower_bound(com.begin(), com.end(), p) - com.begin();
}

char str[100010];
pos arr[100010];

pos base[100010];
int off[100010];

vector<int> lis[100010];
const int dx[4] = { 0, 0, 1, 1 };
const int dy[4] = { 0, 1, 0, 1 };

int main()
{
    //freopen("in", "r", stdin);
    //freopen("out", "w", stdout);

    int n, k, i;
    scanf("%d%d%s", &n, &k, str);

    kx = ky = 0;
    for(i = 0; i <= n; i++)
    {
        arr[i] = { kx, ky };
        if(i == n)
            break;

        if(str[i] == 'E')
            kx++;
        else if(str[i] == 'N')
            ky++;
        else if(str[i] == 'W')
            kx--;
        else
            ky--;
    }

    if(kx == 0 && ky == 0)
        k = 1;

    for(i = 0; i <= n; i++)
    {
        tie(base[i], off[i]) = decom(arr[i]);
        com.push_back(base[i]);
    }
    sort(com.begin(), com.end());
    com.erase(unique(com.begin(), com.end()), com.end());
    int sz = com.size();

    for(i = 0; i <= n; i++)
        lis[idx(base[i])].push_back(off[i]);

    long long res = 0;
    for(i = 0; i < sz; i++)
    {
        int a[4];
        int b[4];

        bool ok = 1;
        for(int d = 0; d < 4; d++)
        {
            pos p;
            tie(p, b[d]) = decom({ com[i].x + dx[d], com[i].y + dy[d] });
            a[d] = idx(p);
            if(!(a[d] < com.size() && com[a[d]] == p))
            {
                ok = 0;
                break;
            }
        }
        if(!ok)
            continue;

        vector<eve> v;
        for(int d = 0; d < 4; d++)
        {
            for(int l : lis[a[d]])
            {
                v.push_back({ l - b[d], d + 1 });
                v.push_back({ l - b[d] + k, -(d + 1) });
            }
        }
        sort(v.begin(), v.end());

        int s[4];
        memset(s, 0, sizeof s);

        int cnt = 0;
        bool inr = 0;
        int sta;
        for(int j = 0; j < v.size(); j++)
        {
            eve t = v[j];
            if(t.e > 0)
            {
                t.e--;
                s[t.e]++;
                if(s[t.e] == 1)
                    cnt++;
            }
            else
            {
                t.e = -t.e - 1;
                s[t.e]--;
                if(s[t.e] == 0)
                    cnt--;
            }

            if(j == (int)v.size() - 1 || t.x != v[j + 1].x)
            {
                if(!inr && cnt == 4)
                {
                    inr = 1;
                    sta = t.x;
                }
                else if(inr && cnt != 4)
                {
                    inr = 0;
                    res += t.x - sta;
                }
            }
        }
    }

    printf("%lld\n", res);
    return 0;
}

Compilation message

2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:143:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(!(a[d] < com.size() && com[a[d]] == p))
                  ~~~~~^~~~~~~~~~~~
2016_ho_t4.cpp:169:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < v.size(); j++)
                        ~~^~~~~~~~~~
2016_ho_t4.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%s", &n, &k, str);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
2016_ho_t4.cpp:197:32: warning: 'sta' may be used uninitialized in this function [-Wmaybe-uninitialized]
                     res += t.x - sta;
                            ~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 4 ms 2864 KB Output is correct
7 Correct 5 ms 2864 KB Output is correct
8 Correct 4 ms 2864 KB Output is correct
9 Correct 4 ms 2876 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 5 ms 2952 KB Output is correct
13 Correct 4 ms 2952 KB Output is correct
14 Correct 4 ms 2952 KB Output is correct
15 Correct 4 ms 2952 KB Output is correct
16 Correct 5 ms 2952 KB Output is correct
17 Correct 5 ms 2952 KB Output is correct
18 Correct 4 ms 3136 KB Output is correct
19 Correct 5 ms 3136 KB Output is correct
20 Correct 4 ms 3136 KB Output is correct
21 Correct 4 ms 3136 KB Output is correct
22 Correct 5 ms 3136 KB Output is correct
23 Correct 6 ms 3136 KB Output is correct
24 Correct 5 ms 3136 KB Output is correct
25 Correct 4 ms 3136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 4 ms 2864 KB Output is correct
7 Correct 5 ms 2864 KB Output is correct
8 Correct 4 ms 2864 KB Output is correct
9 Correct 4 ms 2876 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 5 ms 2952 KB Output is correct
13 Correct 4 ms 2952 KB Output is correct
14 Correct 4 ms 2952 KB Output is correct
15 Correct 4 ms 2952 KB Output is correct
16 Correct 5 ms 2952 KB Output is correct
17 Correct 5 ms 2952 KB Output is correct
18 Correct 4 ms 3136 KB Output is correct
19 Correct 5 ms 3136 KB Output is correct
20 Correct 4 ms 3136 KB Output is correct
21 Correct 4 ms 3136 KB Output is correct
22 Correct 5 ms 3136 KB Output is correct
23 Correct 6 ms 3136 KB Output is correct
24 Correct 5 ms 3136 KB Output is correct
25 Correct 4 ms 3136 KB Output is correct
26 Correct 4 ms 3136 KB Output is correct
27 Correct 9 ms 3196 KB Output is correct
28 Correct 18 ms 3452 KB Output is correct
29 Correct 36 ms 4852 KB Output is correct
30 Correct 62 ms 5488 KB Output is correct
31 Correct 61 ms 8432 KB Output is correct
32 Correct 71 ms 8432 KB Output is correct
33 Correct 70 ms 8432 KB Output is correct
34 Correct 74 ms 8432 KB Output is correct
35 Correct 81 ms 8432 KB Output is correct
36 Correct 84 ms 8432 KB Output is correct
37 Correct 73 ms 8432 KB Output is correct
38 Correct 97 ms 8432 KB Output is correct
39 Correct 74 ms 8432 KB Output is correct
40 Correct 65 ms 8432 KB Output is correct
41 Correct 78 ms 8432 KB Output is correct
42 Correct 63 ms 8432 KB Output is correct
43 Correct 86 ms 8432 KB Output is correct
44 Correct 70 ms 8432 KB Output is correct
45 Correct 66 ms 8432 KB Output is correct
46 Correct 63 ms 8432 KB Output is correct
47 Correct 82 ms 8432 KB Output is correct
48 Correct 76 ms 9748 KB Output is correct
49 Correct 50 ms 9748 KB Output is correct
50 Correct 64 ms 9748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 4 ms 2864 KB Output is correct
7 Correct 5 ms 2864 KB Output is correct
8 Correct 4 ms 2864 KB Output is correct
9 Correct 4 ms 2876 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 5 ms 2952 KB Output is correct
13 Correct 4 ms 2952 KB Output is correct
14 Correct 4 ms 2952 KB Output is correct
15 Correct 4 ms 2952 KB Output is correct
16 Correct 5 ms 2952 KB Output is correct
17 Correct 5 ms 2952 KB Output is correct
18 Correct 4 ms 3136 KB Output is correct
19 Correct 5 ms 3136 KB Output is correct
20 Correct 4 ms 3136 KB Output is correct
21 Correct 4 ms 3136 KB Output is correct
22 Correct 5 ms 3136 KB Output is correct
23 Correct 6 ms 3136 KB Output is correct
24 Correct 5 ms 3136 KB Output is correct
25 Correct 4 ms 3136 KB Output is correct
26 Correct 4 ms 9748 KB Output is correct
27 Correct 5 ms 9748 KB Output is correct
28 Correct 4 ms 9748 KB Output is correct
29 Correct 4 ms 9748 KB Output is correct
30 Correct 4 ms 9748 KB Output is correct
31 Correct 4 ms 9748 KB Output is correct
32 Correct 4 ms 9748 KB Output is correct
33 Correct 4 ms 9748 KB Output is correct
34 Correct 5 ms 9748 KB Output is correct
35 Correct 5 ms 9748 KB Output is correct
36 Correct 6 ms 9748 KB Output is correct
37 Correct 5 ms 9748 KB Output is correct
38 Correct 4 ms 9748 KB Output is correct
39 Correct 4 ms 9748 KB Output is correct
40 Correct 4 ms 9748 KB Output is correct
41 Correct 4 ms 9748 KB Output is correct
42 Correct 4 ms 9748 KB Output is correct
43 Correct 3 ms 9748 KB Output is correct
44 Correct 4 ms 9748 KB Output is correct
45 Correct 4 ms 9748 KB Output is correct
46 Correct 4 ms 9748 KB Output is correct
47 Correct 4 ms 9748 KB Output is correct
48 Correct 4 ms 9748 KB Output is correct
49 Correct 4 ms 9748 KB Output is correct
50 Correct 4 ms 9748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Correct 4 ms 2864 KB Output is correct
5 Correct 4 ms 2864 KB Output is correct
6 Correct 4 ms 2864 KB Output is correct
7 Correct 5 ms 2864 KB Output is correct
8 Correct 4 ms 2864 KB Output is correct
9 Correct 4 ms 2876 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 4 ms 2952 KB Output is correct
12 Correct 5 ms 2952 KB Output is correct
13 Correct 4 ms 2952 KB Output is correct
14 Correct 4 ms 2952 KB Output is correct
15 Correct 4 ms 2952 KB Output is correct
16 Correct 5 ms 2952 KB Output is correct
17 Correct 5 ms 2952 KB Output is correct
18 Correct 4 ms 3136 KB Output is correct
19 Correct 5 ms 3136 KB Output is correct
20 Correct 4 ms 3136 KB Output is correct
21 Correct 4 ms 3136 KB Output is correct
22 Correct 5 ms 3136 KB Output is correct
23 Correct 6 ms 3136 KB Output is correct
24 Correct 5 ms 3136 KB Output is correct
25 Correct 4 ms 3136 KB Output is correct
26 Correct 4 ms 3136 KB Output is correct
27 Correct 9 ms 3196 KB Output is correct
28 Correct 18 ms 3452 KB Output is correct
29 Correct 36 ms 4852 KB Output is correct
30 Correct 62 ms 5488 KB Output is correct
31 Correct 61 ms 8432 KB Output is correct
32 Correct 71 ms 8432 KB Output is correct
33 Correct 70 ms 8432 KB Output is correct
34 Correct 74 ms 8432 KB Output is correct
35 Correct 81 ms 8432 KB Output is correct
36 Correct 84 ms 8432 KB Output is correct
37 Correct 73 ms 8432 KB Output is correct
38 Correct 97 ms 8432 KB Output is correct
39 Correct 74 ms 8432 KB Output is correct
40 Correct 65 ms 8432 KB Output is correct
41 Correct 78 ms 8432 KB Output is correct
42 Correct 63 ms 8432 KB Output is correct
43 Correct 86 ms 8432 KB Output is correct
44 Correct 70 ms 8432 KB Output is correct
45 Correct 66 ms 8432 KB Output is correct
46 Correct 63 ms 8432 KB Output is correct
47 Correct 82 ms 8432 KB Output is correct
48 Correct 76 ms 9748 KB Output is correct
49 Correct 50 ms 9748 KB Output is correct
50 Correct 64 ms 9748 KB Output is correct
51 Correct 4 ms 9748 KB Output is correct
52 Correct 5 ms 9748 KB Output is correct
53 Correct 4 ms 9748 KB Output is correct
54 Correct 4 ms 9748 KB Output is correct
55 Correct 4 ms 9748 KB Output is correct
56 Correct 4 ms 9748 KB Output is correct
57 Correct 4 ms 9748 KB Output is correct
58 Correct 4 ms 9748 KB Output is correct
59 Correct 5 ms 9748 KB Output is correct
60 Correct 5 ms 9748 KB Output is correct
61 Correct 6 ms 9748 KB Output is correct
62 Correct 5 ms 9748 KB Output is correct
63 Correct 4 ms 9748 KB Output is correct
64 Correct 4 ms 9748 KB Output is correct
65 Correct 4 ms 9748 KB Output is correct
66 Correct 4 ms 9748 KB Output is correct
67 Correct 4 ms 9748 KB Output is correct
68 Correct 3 ms 9748 KB Output is correct
69 Correct 4 ms 9748 KB Output is correct
70 Correct 4 ms 9748 KB Output is correct
71 Correct 4 ms 9748 KB Output is correct
72 Correct 4 ms 9748 KB Output is correct
73 Correct 4 ms 9748 KB Output is correct
74 Correct 4 ms 9748 KB Output is correct
75 Correct 4 ms 9748 KB Output is correct
76 Correct 10 ms 9748 KB Output is correct
77 Correct 9 ms 9748 KB Output is correct
78 Correct 16 ms 9748 KB Output is correct
79 Correct 47 ms 9748 KB Output is correct
80 Correct 59 ms 9748 KB Output is correct
81 Correct 68 ms 9748 KB Output is correct
82 Correct 67 ms 9748 KB Output is correct
83 Correct 72 ms 9748 KB Output is correct
84 Correct 86 ms 9748 KB Output is correct
85 Correct 80 ms 9748 KB Output is correct
86 Correct 61 ms 9748 KB Output is correct
87 Correct 94 ms 9748 KB Output is correct
88 Correct 78 ms 9748 KB Output is correct
89 Correct 102 ms 9748 KB Output is correct
90 Correct 68 ms 9748 KB Output is correct
91 Correct 86 ms 9748 KB Output is correct
92 Correct 67 ms 9748 KB Output is correct
93 Correct 73 ms 9748 KB Output is correct
94 Correct 71 ms 9748 KB Output is correct
95 Correct 95 ms 9748 KB Output is correct
96 Correct 59 ms 9748 KB Output is correct
97 Correct 75 ms 9748 KB Output is correct
98 Correct 45 ms 9852 KB Output is correct
99 Correct 51 ms 9852 KB Output is correct
100 Correct 111 ms 9852 KB Output is correct