답안 #673657

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673657 2022-12-21T14:56:30 Z abysmal Copy and Paste 3 (JOI22_copypaste3) C++14
100 / 100
778 ms 98760 KB
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>

using namespace std;

const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = 2500 + 5;
const int64_t MOD = 1e9 + 9;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};

struct Thing
{
    int h1;
    int h2;
    int id;

    Thing(int h1_, int h2_, int id_) : h1(h1_), h2(h2_) ,id(id_) {}

    bool operator < (const Thing& o) const
    {
        if(h1 != o.h1) return h1 > o.h1;
        else if(h2 != o.h2) return h2 > o.h2;
        return id < o.id;
    }
};

struct Solution
{
    int n;
    string s;
    int64_t a;
    int64_t b;
    int64_t c;
    Solution() {}

    void solve()
    {
        cin >> n >> s >> a >> b >> c;
        vector<vector<int> > h1(n, vector<int>(n, -1));
        vector<vector<int> > h2(n, vector<int>(n, -1));
        for(int i = 0; i < n; i++)
        {
            int t1 = 0;
            int t2 = 0;
            int pow1 = 1;
            int pow2 = 1;
            for(int j = i; j < n; j++)
            {
                int ch = s[j] - 'a' + 1;

                t1 = modadd(t1, modmul(ch, pow1));
                t2 = modadd(t2, modmul(ch, pow2));
                pow1 = modmul(pow1, p1);
                pow2 = modmul(pow1, p2);

                h1[i][j] = t1;
                h2[i][j] = t2;
            }
        }

        vector<vector<int64_t> > dp(n, vector<int64_t>(n, INF));
        for(int i = 0; i < n; i++)
        {
            dp[i][i] = a;
        }

        for(int len = 0; len < n; len++)
        {
            set<Thing> st;

            for(int i = 0; i < n; i++)
            {
                int j = i + len;
                if(j >= n) break;

                st.insert(Thing(h1[i][j], h2[i][j], i));
            }

            for(int i = 0; i < n; i++)
            {
                int j = i + len;
                if(len == 0 || j >= n) break;

                dp[i][j] = min(dp[i][j], dp[i][j - 1] + a);
                if(i != n - 1) dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
            }

            vector<Thing> tt;
            for(set<Thing>::iterator it = st.begin(); it != st.end(); it++)
            {
                tt.push_back(*(it));
            }

            int k = tt.size();
            int tid = 0;
            vector<int> nx(n, -1);
            while(tid < k)
            {
                pair<int, int> last = make_pair(tt[tid].h1, tt[tid].h2);

                vector<int> pos;
                while(tid < k && make_pair(tt[tid].h1, tt[tid].h2) == last)
                {
                    pos.push_back(tt[tid].id);
                    tid++;
                }

                int ps = pos.size();
                int r = ps - 1;
                for(int i = ps - 2; i >= 0; i--)
                {
                    while(r > 0 && pos[r - 1] > pos[i] + len) r--;

                    if(pos[r] > pos[i] + len) nx[pos[i]] = pos[r];
                }
            }

            for(int64_t i = 0; i < n; i++)
            {
                int64_t l = i;
                int64_t j = i + len;

                int64_t cnt = 2;
                while(nx[l] != -1)
                {
                    int64_t nl = nx[l];
                    int64_t nr = nl + len;

                    int64_t x = nr - i + 1 - cnt * (len + 1);
                    dp[i][nr] = min(dp[i][nr], dp[i][j] + b + c * cnt + a * x);

                    cnt++;
                    l = nl;
                }
            }
        }

//        for(int i = 0; i < n; i++)
//        {
//            for(int j = 0; j < n; j++)
//            {
//                if(dp[i][j] == INF) cout << "I ";
//                else cout << dp[i][j] << " ";
//            }
//            cout << "\n";
//        }

        cout << dp[0][n - 1];
    }

    int modsub(int t1, int t2)
    {
        int res = t1 - t2;
        if(res < 0) res += MOD;

        return res;
    }

    int modadd(int t1, int t2)
    {
        int res = t1 + t2;
        if(res >= MOD) res -= MOD;

        return res;
    }

    int modmul(int t1, int t2)
    {
        int64_t res = 1LL * t1 * t2;
        return (res % MOD);
    }

    bool BIT(int& mask, int& j)
    {
        return (mask & MASK(j));
    }

    int MASK(int j)
    {
        return (1 << j);
    }
};

void __startup()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
}

int main()
{
    __startup();
    int t = 1;
//    cin >> t;

    for(int i = 1; i <= t; i++)
    {
        Solution().solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 334 ms 53000 KB Output is correct
4 Correct 396 ms 61864 KB Output is correct
5 Correct 474 ms 72292 KB Output is correct
6 Correct 560 ms 84416 KB Output is correct
7 Correct 647 ms 98692 KB Output is correct
8 Correct 654 ms 98696 KB Output is correct
9 Correct 692 ms 98692 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 988 KB Output is correct
42 Correct 4 ms 980 KB Output is correct
43 Correct 4 ms 980 KB Output is correct
44 Correct 4 ms 984 KB Output is correct
45 Correct 4 ms 980 KB Output is correct
46 Correct 5 ms 980 KB Output is correct
47 Correct 3 ms 988 KB Output is correct
48 Correct 4 ms 980 KB Output is correct
49 Correct 5 ms 980 KB Output is correct
50 Correct 4 ms 988 KB Output is correct
51 Correct 4 ms 980 KB Output is correct
52 Correct 4 ms 980 KB Output is correct
53 Correct 4 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 988 KB Output is correct
42 Correct 4 ms 980 KB Output is correct
43 Correct 4 ms 980 KB Output is correct
44 Correct 4 ms 984 KB Output is correct
45 Correct 4 ms 980 KB Output is correct
46 Correct 5 ms 980 KB Output is correct
47 Correct 3 ms 988 KB Output is correct
48 Correct 4 ms 980 KB Output is correct
49 Correct 5 ms 980 KB Output is correct
50 Correct 4 ms 988 KB Output is correct
51 Correct 4 ms 980 KB Output is correct
52 Correct 4 ms 980 KB Output is correct
53 Correct 4 ms 980 KB Output is correct
54 Correct 17 ms 3536 KB Output is correct
55 Correct 79 ms 16084 KB Output is correct
56 Correct 90 ms 16160 KB Output is correct
57 Correct 89 ms 16084 KB Output is correct
58 Correct 91 ms 16168 KB Output is correct
59 Correct 93 ms 16160 KB Output is correct
60 Correct 93 ms 16168 KB Output is correct
61 Correct 91 ms 16224 KB Output is correct
62 Correct 82 ms 16084 KB Output is correct
63 Correct 90 ms 16084 KB Output is correct
64 Correct 90 ms 16160 KB Output is correct
65 Correct 89 ms 16164 KB Output is correct
66 Correct 94 ms 16164 KB Output is correct
67 Correct 86 ms 16076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 334 ms 53000 KB Output is correct
14 Correct 396 ms 61864 KB Output is correct
15 Correct 474 ms 72292 KB Output is correct
16 Correct 560 ms 84416 KB Output is correct
17 Correct 647 ms 98692 KB Output is correct
18 Correct 654 ms 98696 KB Output is correct
19 Correct 692 ms 98692 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 1 ms 256 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 0 ms 212 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 3 ms 988 KB Output is correct
59 Correct 4 ms 980 KB Output is correct
60 Correct 4 ms 980 KB Output is correct
61 Correct 4 ms 984 KB Output is correct
62 Correct 4 ms 980 KB Output is correct
63 Correct 5 ms 980 KB Output is correct
64 Correct 3 ms 988 KB Output is correct
65 Correct 4 ms 980 KB Output is correct
66 Correct 5 ms 980 KB Output is correct
67 Correct 4 ms 988 KB Output is correct
68 Correct 4 ms 980 KB Output is correct
69 Correct 4 ms 980 KB Output is correct
70 Correct 4 ms 980 KB Output is correct
71 Correct 17 ms 3536 KB Output is correct
72 Correct 79 ms 16084 KB Output is correct
73 Correct 90 ms 16160 KB Output is correct
74 Correct 89 ms 16084 KB Output is correct
75 Correct 91 ms 16168 KB Output is correct
76 Correct 93 ms 16160 KB Output is correct
77 Correct 93 ms 16168 KB Output is correct
78 Correct 91 ms 16224 KB Output is correct
79 Correct 82 ms 16084 KB Output is correct
80 Correct 90 ms 16084 KB Output is correct
81 Correct 90 ms 16160 KB Output is correct
82 Correct 89 ms 16164 KB Output is correct
83 Correct 94 ms 16164 KB Output is correct
84 Correct 86 ms 16076 KB Output is correct
85 Correct 227 ms 39736 KB Output is correct
86 Correct 698 ms 98688 KB Output is correct
87 Correct 672 ms 98692 KB Output is correct
88 Correct 660 ms 98692 KB Output is correct
89 Correct 656 ms 98760 KB Output is correct
90 Correct 680 ms 98692 KB Output is correct
91 Correct 730 ms 98688 KB Output is correct
92 Correct 706 ms 98688 KB Output is correct
93 Correct 680 ms 98696 KB Output is correct
94 Correct 704 ms 98620 KB Output is correct
95 Correct 776 ms 98692 KB Output is correct
96 Correct 778 ms 98688 KB Output is correct
97 Correct 653 ms 98592 KB Output is correct