답안 #225422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
225422 2020-04-20T14:34:12 Z emil_physmath Nice sequence (IZhO18_sequence) C++17
76 / 100
2000 ms 55080 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 - 3), r = len + 3;
        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 10 ms 9728 KB Ok
3 Correct 10 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 10 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 10 ms 9728 KB Ok
8 Correct 10 ms 9728 KB Ok
9 Correct 10 ms 9728 KB Ok
10 Correct 10 ms 9728 KB Ok
11 Correct 10 ms 9728 KB Ok
12 Correct 10 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 10 ms 9728 KB Ok
6 Correct 14 ms 9984 KB Ok
7 Correct 30 ms 11036 KB Ok
8 Correct 19 ms 10368 KB Ok
9 Correct 35 ms 11268 KB Ok
10 Correct 25 ms 10644 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 9 ms 9728 KB Ok
3 Correct 11 ms 9856 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 10 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 10 ms 9728 KB Ok
8 Correct 9 ms 9728 KB Ok
9 Correct 9 ms 9728 KB Ok
10 Correct 10 ms 9728 KB Ok
11 Correct 10 ms 9728 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 10 ms 9728 KB Ok
6 Correct 422 ms 28632 KB Ok
7 Correct 324 ms 30648 KB Ok
8 Correct 703 ms 33828 KB Ok
9 Correct 503 ms 32368 KB Ok
10 Correct 250 ms 22532 KB Ok
11 Correct 499 ms 34988 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 10 ms 9728 KB Ok
3 Correct 10 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 10 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 10 ms 9728 KB Ok
8 Correct 10 ms 9728 KB Ok
9 Correct 10 ms 9728 KB Ok
10 Correct 10 ms 9728 KB Ok
11 Correct 10 ms 9728 KB Ok
12 Correct 10 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 9 ms 9728 KB Ok
15 Correct 11 ms 9856 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 10 ms 9728 KB Ok
18 Correct 10 ms 9728 KB Ok
19 Correct 10 ms 9728 KB Ok
20 Correct 9 ms 9728 KB Ok
21 Correct 9 ms 9728 KB Ok
22 Correct 10 ms 9728 KB Ok
23 Correct 10 ms 9728 KB Ok
24 Correct 14 ms 9984 KB Ok
25 Correct 13 ms 9984 KB Ok
26 Correct 14 ms 9984 KB Ok
27 Correct 14 ms 9984 KB Ok
28 Correct 13 ms 9984 KB Ok
29 Correct 15 ms 9984 KB Ok
30 Correct 12 ms 9984 KB Ok
31 Correct 16 ms 9984 KB Ok
32 Correct 14 ms 9984 KB Ok
33 Correct 14 ms 9984 KB Ok
34 Correct 19 ms 10240 KB Ok
35 Correct 19 ms 10240 KB Ok
36 Correct 19 ms 10368 KB Ok
37 Correct 19 ms 10368 KB Ok
38 Correct 19 ms 10368 KB Ok
39 Correct 21 ms 10240 KB Ok
40 Correct 20 ms 10368 KB Ok
41 Correct 18 ms 10368 KB Ok
42 Correct 19 ms 10240 KB Ok
43 Correct 19 ms 10368 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 10 ms 9728 KB Ok
3 Correct 10 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 10 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 10 ms 9728 KB Ok
8 Correct 10 ms 9728 KB Ok
9 Correct 10 ms 9728 KB Ok
10 Correct 10 ms 9728 KB Ok
11 Correct 10 ms 9728 KB Ok
12 Correct 10 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 9 ms 9728 KB Ok
15 Correct 9 ms 9728 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 10 ms 9728 KB Ok
18 Correct 14 ms 9984 KB Ok
19 Correct 30 ms 11036 KB Ok
20 Correct 19 ms 10368 KB Ok
21 Correct 35 ms 11268 KB Ok
22 Correct 25 ms 10644 KB Ok
23 Correct 9 ms 9728 KB Ok
24 Correct 9 ms 9728 KB Ok
25 Correct 11 ms 9856 KB Ok
26 Correct 10 ms 9728 KB Ok
27 Correct 10 ms 9728 KB Ok
28 Correct 10 ms 9728 KB Ok
29 Correct 10 ms 9728 KB Ok
30 Correct 9 ms 9728 KB Ok
31 Correct 9 ms 9728 KB Ok
32 Correct 10 ms 9728 KB Ok
33 Correct 10 ms 9728 KB Ok
34 Correct 14 ms 9984 KB Ok
35 Correct 13 ms 9984 KB Ok
36 Correct 14 ms 9984 KB Ok
37 Correct 14 ms 9984 KB Ok
38 Correct 13 ms 9984 KB Ok
39 Correct 15 ms 9984 KB Ok
40 Correct 12 ms 9984 KB Ok
41 Correct 16 ms 9984 KB Ok
42 Correct 14 ms 9984 KB Ok
43 Correct 14 ms 9984 KB Ok
44 Correct 19 ms 10240 KB Ok
45 Correct 19 ms 10240 KB Ok
46 Correct 19 ms 10368 KB Ok
47 Correct 19 ms 10368 KB Ok
48 Correct 19 ms 10368 KB Ok
49 Correct 21 ms 10240 KB Ok
50 Correct 20 ms 10368 KB Ok
51 Correct 18 ms 10368 KB Ok
52 Correct 19 ms 10240 KB Ok
53 Correct 19 ms 10368 KB Ok
54 Correct 216 ms 16780 KB Ok
55 Correct 267 ms 17252 KB Ok
56 Correct 255 ms 17264 KB Ok
57 Correct 159 ms 15720 KB Ok
58 Correct 236 ms 16956 KB Ok
59 Correct 204 ms 16348 KB Ok
60 Correct 169 ms 16084 KB Ok
61 Correct 165 ms 16436 KB Ok
62 Correct 267 ms 17332 KB Ok
63 Correct 189 ms 16156 KB Ok
64 Correct 249 ms 16916 KB Ok
65 Correct 242 ms 16984 KB Ok
66 Correct 192 ms 16752 KB Ok
67 Correct 155 ms 16280 KB Ok
68 Correct 218 ms 16608 KB Ok
69 Correct 551 ms 26248 KB Ok
70 Correct 537 ms 26840 KB Ok
71 Correct 539 ms 26468 KB Ok
72 Correct 541 ms 26388 KB Ok
73 Correct 522 ms 26144 KB Ok
74 Correct 534 ms 26016 KB Ok
75 Correct 526 ms 26272 KB Ok
76 Correct 570 ms 26588 KB Ok
77 Correct 515 ms 25676 KB Ok
78 Correct 542 ms 26112 KB Ok
79 Correct 509 ms 26620 KB Ok
80 Correct 520 ms 25836 KB Ok
81 Correct 538 ms 26628 KB Ok
82 Correct 573 ms 26304 KB Ok
83 Correct 525 ms 25996 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9728 KB Ok
2 Correct 10 ms 9728 KB Ok
3 Correct 10 ms 9728 KB Ok
4 Correct 10 ms 9728 KB Ok
5 Correct 10 ms 9728 KB Ok
6 Correct 10 ms 9728 KB Ok
7 Correct 10 ms 9728 KB Ok
8 Correct 10 ms 9728 KB Ok
9 Correct 10 ms 9728 KB Ok
10 Correct 10 ms 9728 KB Ok
11 Correct 10 ms 9728 KB Ok
12 Correct 10 ms 9728 KB Ok
13 Correct 9 ms 9728 KB Ok
14 Correct 9 ms 9728 KB Ok
15 Correct 9 ms 9728 KB Ok
16 Correct 10 ms 9728 KB Ok
17 Correct 10 ms 9728 KB Ok
18 Correct 14 ms 9984 KB Ok
19 Correct 30 ms 11036 KB Ok
20 Correct 19 ms 10368 KB Ok
21 Correct 35 ms 11268 KB Ok
22 Correct 25 ms 10644 KB Ok
23 Correct 9 ms 9728 KB Ok
24 Correct 9 ms 9728 KB Ok
25 Correct 11 ms 9856 KB Ok
26 Correct 10 ms 9728 KB Ok
27 Correct 10 ms 9728 KB Ok
28 Correct 10 ms 9728 KB Ok
29 Correct 10 ms 9728 KB Ok
30 Correct 9 ms 9728 KB Ok
31 Correct 9 ms 9728 KB Ok
32 Correct 10 ms 9728 KB Ok
33 Correct 10 ms 9728 KB Ok
34 Correct 9 ms 9728 KB Ok
35 Correct 10 ms 9728 KB Ok
36 Correct 9 ms 9728 KB Ok
37 Correct 10 ms 9728 KB Ok
38 Correct 10 ms 9728 KB Ok
39 Correct 422 ms 28632 KB Ok
40 Correct 324 ms 30648 KB Ok
41 Correct 703 ms 33828 KB Ok
42 Correct 503 ms 32368 KB Ok
43 Correct 250 ms 22532 KB Ok
44 Correct 499 ms 34988 KB Ok
45 Correct 14 ms 9984 KB Ok
46 Correct 13 ms 9984 KB Ok
47 Correct 14 ms 9984 KB Ok
48 Correct 14 ms 9984 KB Ok
49 Correct 13 ms 9984 KB Ok
50 Correct 15 ms 9984 KB Ok
51 Correct 12 ms 9984 KB Ok
52 Correct 16 ms 9984 KB Ok
53 Correct 14 ms 9984 KB Ok
54 Correct 14 ms 9984 KB Ok
55 Correct 19 ms 10240 KB Ok
56 Correct 19 ms 10240 KB Ok
57 Correct 19 ms 10368 KB Ok
58 Correct 19 ms 10368 KB Ok
59 Correct 19 ms 10368 KB Ok
60 Correct 21 ms 10240 KB Ok
61 Correct 20 ms 10368 KB Ok
62 Correct 18 ms 10368 KB Ok
63 Correct 19 ms 10240 KB Ok
64 Correct 19 ms 10368 KB Ok
65 Correct 216 ms 16780 KB Ok
66 Correct 267 ms 17252 KB Ok
67 Correct 255 ms 17264 KB Ok
68 Correct 159 ms 15720 KB Ok
69 Correct 236 ms 16956 KB Ok
70 Correct 204 ms 16348 KB Ok
71 Correct 169 ms 16084 KB Ok
72 Correct 165 ms 16436 KB Ok
73 Correct 267 ms 17332 KB Ok
74 Correct 189 ms 16156 KB Ok
75 Correct 249 ms 16916 KB Ok
76 Correct 242 ms 16984 KB Ok
77 Correct 192 ms 16752 KB Ok
78 Correct 155 ms 16280 KB Ok
79 Correct 218 ms 16608 KB Ok
80 Correct 551 ms 26248 KB Ok
81 Correct 537 ms 26840 KB Ok
82 Correct 539 ms 26468 KB Ok
83 Correct 541 ms 26388 KB Ok
84 Correct 522 ms 26144 KB Ok
85 Correct 534 ms 26016 KB Ok
86 Correct 526 ms 26272 KB Ok
87 Correct 570 ms 26588 KB Ok
88 Correct 515 ms 25676 KB Ok
89 Correct 542 ms 26112 KB Ok
90 Correct 509 ms 26620 KB Ok
91 Correct 520 ms 25836 KB Ok
92 Correct 538 ms 26628 KB Ok
93 Correct 573 ms 26304 KB Ok
94 Correct 525 ms 25996 KB Ok
95 Correct 578 ms 27540 KB Ok
96 Correct 1221 ms 36216 KB Ok
97 Correct 1313 ms 31604 KB Ok
98 Correct 584 ms 29728 KB Ok
99 Correct 958 ms 30460 KB Ok
100 Correct 1084 ms 30748 KB Ok
101 Correct 1055 ms 34028 KB Ok
102 Correct 918 ms 32896 KB Ok
103 Correct 1006 ms 32348 KB Ok
104 Correct 1451 ms 34424 KB Ok
105 Correct 1268 ms 36708 KB Ok
106 Correct 736 ms 33848 KB Ok
107 Correct 1008 ms 33696 KB Ok
108 Correct 1492 ms 36636 KB Ok
109 Correct 1127 ms 35060 KB Ok
110 Execution timed out 2092 ms 55080 KB Time limit exceeded
111 Halted 0 ms 0 KB -