답안 #225423

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
225423 2020-04-20T14:36:02 Z emil_physmath Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 66796 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef double ldouble;
typedef long long llong;
typedef unsigned int uint;
const int maxN = 100001;

vector<int> nei[400001];
int a[400001];
bool used[400001];

void DFS(int v, vector<int>& order)
{
    used[v] = true;
    for (int to: nei[v])
        if (!used[to])
            DFS(to, order);
    order.push_back(v);
}
bool Check(int n, int m, int len)
{
    if (len >= n)
    {
        llong sum = 0;
        for (int i = 1; i <= n; ++i)
            sum += a[i];
        if (sum >= 0) return false;
        for (int i = 2; i + n - 1 <= len; ++i)
        {
            sum -= a[i - 1];
            sum += a[i + n - 1];
            if (sum >= 0) return false;
        }
    }
    if (len >= m)
    {
        llong sum = 0;
        for (int i = 1; i <= m; ++i)
            sum += a[i];
        if (sum <= 0) return false;
        for (int i = 2; i + m - 1 <= len; ++i)
        {
            sum -= a[i - 1];
            sum += a[i + m - 1];
            if (sum <= 0) return false;
        }
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        int ans = 0;
        vector<int> ansArr;
        int len = 0;
        int i = 0;
        do {
            len = max(len, i += n);
            while (i - m >= 0) i -= m;
        } while (i);
        int l = max(1, len - 1), r = len + 1;
        bool up = false;
        int prevLen = 0;
        while (l <= r)
        {
            int len = (l + r) / 2;
            for (int i = 0; i <= len; ++i)
            {
                used[i] = false;
                if (!up) vector<int>().swap(nei[i]);
            }
            for (int i = (up ? prevLen + 1 : 1); i <= len; ++i)
            {
                if (i - n >= 0)
                    nei[i].push_back(i - n);
                if (i - m >= 0)
                    nei[i - m].push_back(i);
            }
            vector<int> order;
            for (int i = 0; i <= len; ++i)
                if (!used[i])
                    DFS(i, order);
            reverse(order.begin(), order.end());
            vector<int> pref(len + 1);
            for (int i = 0; i < order.size(); ++i)
                pref[order[i]] = i;
            for (int i = 1; i < pref.size(); ++i)
                pref[i] -= pref[0];
            pref[0] = 0;
            for (int i = 1; i <= len; ++i)
                a[i] = pref[i] - pref[i - 1];
            if (Check(n, m, len))
            {
                ans = len;
                ansArr.resize(len);
                for (int i = 0; i < len; ++i)
                    ansArr[i] = a[i + 1];
                l = len + 1;
                // up = true;
                prevLen = len;
            }
            else
            {
                r = len - 1;
                up = false;
            }
        }
        cout << ans << endl;
        for (int i = 0; i < ans; ++i)
            cout << ansArr[i] << ' ';
        cout << '\n';
    }
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:97:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int i = 0; i < order.size(); ++i)
                             ~~^~~~~~~~~~~~~~
sequence.cpp:99:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int i = 1; i < pref.size(); ++i)
                             ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 9 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 9 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 9 ms 9728 KB Ok
11 Correct 9 ms 9728 KB Ok
12 Correct 9 ms 9728 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 10 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 13 ms 9984 KB Ok
7 Correct 27 ms 11048 KB Ok
8 Correct 17 ms 10368 KB Ok
9 Correct 29 ms 11240 KB Ok
10 Correct 20 ms 10624 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 10 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 9 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 9 ms 9728 KB Ok
11 Correct 9 ms 9728 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 265 ms 27836 KB Ok
7 Correct 220 ms 29816 KB Ok
8 Correct 531 ms 32752 KB Ok
9 Correct 351 ms 31728 KB Ok
10 Correct 183 ms 22260 KB Ok
11 Correct 348 ms 33572 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 9 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 9 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 9 ms 9728 KB Ok
11 Correct 9 ms 9728 KB Ok
12 Correct 9 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 10 ms 9728 KB Ok
15 Correct 9 ms 9728 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 9 ms 9728 KB Ok
18 Correct 10 ms 9728 KB Ok
19 Correct 9 ms 9728 KB Ok
20 Correct 9 ms 9728 KB Ok
21 Correct 9 ms 9728 KB Ok
22 Correct 9 ms 9728 KB Ok
23 Correct 9 ms 9728 KB Ok
24 Correct 12 ms 9984 KB Ok
25 Correct 12 ms 9984 KB Ok
26 Correct 13 ms 9984 KB Ok
27 Correct 14 ms 9984 KB Ok
28 Correct 12 ms 9856 KB Ok
29 Correct 11 ms 9856 KB Ok
30 Correct 12 ms 9856 KB Ok
31 Correct 13 ms 9984 KB Ok
32 Correct 12 ms 9984 KB Ok
33 Correct 12 ms 9984 KB Ok
34 Correct 17 ms 10240 KB Ok
35 Correct 16 ms 10240 KB Ok
36 Correct 17 ms 10240 KB Ok
37 Correct 18 ms 10368 KB Ok
38 Correct 16 ms 10240 KB Ok
39 Correct 20 ms 10240 KB Ok
40 Correct 17 ms 10240 KB Ok
41 Correct 17 ms 10240 KB Ok
42 Correct 17 ms 10240 KB Ok
43 Correct 17 ms 10240 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 9 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 9 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 9 ms 9728 KB Ok
11 Correct 9 ms 9728 KB Ok
12 Correct 9 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 9 ms 9728 KB Ok
15 Correct 10 ms 9728 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 9 ms 9728 KB Ok
18 Correct 13 ms 9984 KB Ok
19 Correct 27 ms 11048 KB Ok
20 Correct 17 ms 10368 KB Ok
21 Correct 29 ms 11240 KB Ok
22 Correct 20 ms 10624 KB Ok
23 Correct 9 ms 9728 KB Ok
24 Correct 10 ms 9728 KB Ok
25 Correct 9 ms 9728 KB Ok
26 Correct 10 ms 9728 KB Ok
27 Correct 9 ms 9728 KB Ok
28 Correct 10 ms 9728 KB Ok
29 Correct 9 ms 9728 KB Ok
30 Correct 9 ms 9728 KB Ok
31 Correct 9 ms 9728 KB Ok
32 Correct 9 ms 9728 KB Ok
33 Correct 9 ms 9728 KB Ok
34 Correct 12 ms 9984 KB Ok
35 Correct 12 ms 9984 KB Ok
36 Correct 13 ms 9984 KB Ok
37 Correct 14 ms 9984 KB Ok
38 Correct 12 ms 9856 KB Ok
39 Correct 11 ms 9856 KB Ok
40 Correct 12 ms 9856 KB Ok
41 Correct 13 ms 9984 KB Ok
42 Correct 12 ms 9984 KB Ok
43 Correct 12 ms 9984 KB Ok
44 Correct 17 ms 10240 KB Ok
45 Correct 16 ms 10240 KB Ok
46 Correct 17 ms 10240 KB Ok
47 Correct 18 ms 10368 KB Ok
48 Correct 16 ms 10240 KB Ok
49 Correct 20 ms 10240 KB Ok
50 Correct 17 ms 10240 KB Ok
51 Correct 17 ms 10240 KB Ok
52 Correct 17 ms 10240 KB Ok
53 Correct 17 ms 10240 KB Ok
54 Correct 153 ms 16432 KB Ok
55 Correct 193 ms 16724 KB Ok
56 Correct 178 ms 16672 KB Ok
57 Correct 123 ms 15384 KB Ok
58 Correct 166 ms 16488 KB Ok
59 Correct 168 ms 16228 KB Ok
60 Correct 135 ms 15764 KB Ok
61 Correct 123 ms 15948 KB Ok
62 Correct 196 ms 16840 KB Ok
63 Correct 140 ms 15736 KB Ok
64 Correct 185 ms 16708 KB Ok
65 Correct 173 ms 16568 KB Ok
66 Correct 161 ms 16324 KB Ok
67 Correct 120 ms 15924 KB Ok
68 Correct 164 ms 16332 KB Ok
69 Correct 396 ms 25540 KB Ok
70 Correct 421 ms 25964 KB Ok
71 Correct 377 ms 25652 KB Ok
72 Correct 365 ms 25352 KB Ok
73 Correct 401 ms 25748 KB Ok
74 Correct 458 ms 25332 KB Ok
75 Correct 387 ms 25432 KB Ok
76 Correct 425 ms 25708 KB Ok
77 Correct 370 ms 25424 KB Ok
78 Correct 397 ms 25316 KB Ok
79 Correct 408 ms 25788 KB Ok
80 Correct 386 ms 25392 KB Ok
81 Correct 417 ms 25888 KB Ok
82 Correct 417 ms 25544 KB Ok
83 Correct 373 ms 25560 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 9 ms 9728 KB Ok
4 Correct 9 ms 9728 KB Ok
5 Correct 9 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 9 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 9 ms 9728 KB Ok
11 Correct 9 ms 9728 KB Ok
12 Correct 9 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 9 ms 9728 KB Ok
15 Correct 10 ms 9728 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 9 ms 9728 KB Ok
18 Correct 13 ms 9984 KB Ok
19 Correct 27 ms 11048 KB Ok
20 Correct 17 ms 10368 KB Ok
21 Correct 29 ms 11240 KB Ok
22 Correct 20 ms 10624 KB Ok
23 Correct 9 ms 9728 KB Ok
24 Correct 10 ms 9728 KB Ok
25 Correct 9 ms 9728 KB Ok
26 Correct 10 ms 9728 KB Ok
27 Correct 9 ms 9728 KB Ok
28 Correct 10 ms 9728 KB Ok
29 Correct 9 ms 9728 KB Ok
30 Correct 9 ms 9728 KB Ok
31 Correct 9 ms 9728 KB Ok
32 Correct 9 ms 9728 KB Ok
33 Correct 9 ms 9728 KB Ok
34 Correct 9 ms 9728 KB Ok
35 Correct 9 ms 9728 KB Ok
36 Correct 9 ms 9728 KB Ok
37 Correct 10 ms 9728 KB Ok
38 Correct 9 ms 9728 KB Ok
39 Correct 265 ms 27836 KB Ok
40 Correct 220 ms 29816 KB Ok
41 Correct 531 ms 32752 KB Ok
42 Correct 351 ms 31728 KB Ok
43 Correct 183 ms 22260 KB Ok
44 Correct 348 ms 33572 KB Ok
45 Correct 12 ms 9984 KB Ok
46 Correct 12 ms 9984 KB Ok
47 Correct 13 ms 9984 KB Ok
48 Correct 14 ms 9984 KB Ok
49 Correct 12 ms 9856 KB Ok
50 Correct 11 ms 9856 KB Ok
51 Correct 12 ms 9856 KB Ok
52 Correct 13 ms 9984 KB Ok
53 Correct 12 ms 9984 KB Ok
54 Correct 12 ms 9984 KB Ok
55 Correct 17 ms 10240 KB Ok
56 Correct 16 ms 10240 KB Ok
57 Correct 17 ms 10240 KB Ok
58 Correct 18 ms 10368 KB Ok
59 Correct 16 ms 10240 KB Ok
60 Correct 20 ms 10240 KB Ok
61 Correct 17 ms 10240 KB Ok
62 Correct 17 ms 10240 KB Ok
63 Correct 17 ms 10240 KB Ok
64 Correct 17 ms 10240 KB Ok
65 Correct 153 ms 16432 KB Ok
66 Correct 193 ms 16724 KB Ok
67 Correct 178 ms 16672 KB Ok
68 Correct 123 ms 15384 KB Ok
69 Correct 166 ms 16488 KB Ok
70 Correct 168 ms 16228 KB Ok
71 Correct 135 ms 15764 KB Ok
72 Correct 123 ms 15948 KB Ok
73 Correct 196 ms 16840 KB Ok
74 Correct 140 ms 15736 KB Ok
75 Correct 185 ms 16708 KB Ok
76 Correct 173 ms 16568 KB Ok
77 Correct 161 ms 16324 KB Ok
78 Correct 120 ms 15924 KB Ok
79 Correct 164 ms 16332 KB Ok
80 Correct 396 ms 25540 KB Ok
81 Correct 421 ms 25964 KB Ok
82 Correct 377 ms 25652 KB Ok
83 Correct 365 ms 25352 KB Ok
84 Correct 401 ms 25748 KB Ok
85 Correct 458 ms 25332 KB Ok
86 Correct 387 ms 25432 KB Ok
87 Correct 425 ms 25708 KB Ok
88 Correct 370 ms 25424 KB Ok
89 Correct 397 ms 25316 KB Ok
90 Correct 408 ms 25788 KB Ok
91 Correct 386 ms 25392 KB Ok
92 Correct 417 ms 25888 KB Ok
93 Correct 417 ms 25544 KB Ok
94 Correct 373 ms 25560 KB Ok
95 Correct 404 ms 26484 KB Ok
96 Correct 800 ms 34668 KB Ok
97 Correct 853 ms 30280 KB Ok
98 Correct 412 ms 29288 KB Ok
99 Correct 677 ms 29624 KB Ok
100 Correct 728 ms 30072 KB Ok
101 Correct 665 ms 33116 KB Ok
102 Correct 598 ms 31824 KB Ok
103 Correct 654 ms 31700 KB Ok
104 Correct 880 ms 33328 KB Ok
105 Correct 802 ms 35332 KB Ok
106 Correct 524 ms 32852 KB Ok
107 Correct 668 ms 33432 KB Ok
108 Correct 880 ms 35360 KB Ok
109 Correct 681 ms 34124 KB Ok
110 Execution timed out 2071 ms 66796 KB Time limit exceeded
111 Halted 0 ms 0 KB -