Submission #1001077

# Submission time Handle Problem Language Result Execution time Memory
1001077 2024-06-18T14:11:32 Z Otalp Rope (JOI17_rope) C++14
80 / 100
2349 ms 241332 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
 
map<ll, int> dp[2];
int cnt[5001];
int a[1000001];
unordered_map<int, vector<int>> d;
 

bool cmp(int a, int b){
    if(cnt[a] == cnt[b]) return a > b;
    return cnt[a] > cnt[b];
}

ll h(ll a, ll b){ //hynya
    return a * 1e7 + b;
}
 
void solve(){
    int n, m;
    cin>>n>>m;
    for(int i=1; i<=n; i++){
        cin>>a[i];
        cnt[a[i]] ++;
    }
    vector<pii> g;
    for(int i=1; i<=m; i++){
        g.pb({cnt[i], i});
    }
    sort(g.begin(), g.end(), greater<pii>());
 
    for(int i=1; i+1<=n; i+=2){
        int l=a[i], r=a[i + 1];
        dp[1][h(l, r)] ++;
        dp[1][h(r, l)] ++;
        d[l].pb(r);
        d[r].pb(l);
    }
    for(int i=2; i+1<=n; i+=2){
        int l=a[i], r=a[i + 1];
        dp[0][h(l, r)] ++;
        dp[0][h(r, l)] ++;
        d[l].pb(r);
        d[r].pb(l);
    }
    ll dd = 0;
    for(int i=1; i<=m; i++) dd += d[i].size();
    if(dd > 2e6) return;
    if(g.size() == 0 or g.size() > 1e6) return;
    if(dp[0].size() + dp[1].size() > 2e6) return;
    for(int i=1; i<=m; i++){
        d[i].pb(i);
        sort(d[i].begin(), d[i].end(), cmp);
    }
    for(int i=1; i<=m; i++){
        int ans = n - cnt[i];
        int ls = 0;
        for(int j: d[i]){
            while(ls < g.size() and g[ls].ss == j) ls++;
            if(j == i) continue;
            for(int k=0; k<2; k++){ 
                ans = min(ans, n-cnt[i]-cnt[j]+dp[k][h(i, j)]);
            }
        }
        if(ls < g.size()) ans = min(ans, n-cnt[i]-g[ls].ff);
        cout<<ans<<'\n';
    }
}


signed main(){
    ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
    solve();
}






















Compilation message

rope.cpp: In function 'void solve()':
rope.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |             while(ls < g.size() and g[ls].ss == j) ls++;
      |                   ~~~^~~~~~~~~~
rope.cpp:70:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         if(ls < g.size()) ans = min(ans, n-cnt[i]-g[ls].ff);
      |            ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 22 ms 4052 KB Output is correct
28 Correct 18 ms 3668 KB Output is correct
29 Correct 21 ms 3676 KB Output is correct
30 Correct 19 ms 3772 KB Output is correct
31 Correct 22 ms 3932 KB Output is correct
32 Correct 19 ms 3608 KB Output is correct
33 Correct 21 ms 3784 KB Output is correct
34 Correct 20 ms 3800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 22 ms 4052 KB Output is correct
28 Correct 18 ms 3668 KB Output is correct
29 Correct 21 ms 3676 KB Output is correct
30 Correct 19 ms 3772 KB Output is correct
31 Correct 22 ms 3932 KB Output is correct
32 Correct 19 ms 3608 KB Output is correct
33 Correct 21 ms 3784 KB Output is correct
34 Correct 20 ms 3800 KB Output is correct
35 Correct 114 ms 20820 KB Output is correct
36 Correct 116 ms 20764 KB Output is correct
37 Correct 122 ms 20252 KB Output is correct
38 Correct 112 ms 20560 KB Output is correct
39 Correct 119 ms 20820 KB Output is correct
40 Correct 73 ms 12460 KB Output is correct
41 Correct 71 ms 12276 KB Output is correct
42 Correct 55 ms 10068 KB Output is correct
43 Correct 60 ms 8936 KB Output is correct
44 Correct 67 ms 12880 KB Output is correct
45 Correct 71 ms 12640 KB Output is correct
46 Correct 57 ms 10316 KB Output is correct
47 Correct 57 ms 9124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 22 ms 4052 KB Output is correct
28 Correct 18 ms 3668 KB Output is correct
29 Correct 21 ms 3676 KB Output is correct
30 Correct 19 ms 3772 KB Output is correct
31 Correct 22 ms 3932 KB Output is correct
32 Correct 19 ms 3608 KB Output is correct
33 Correct 21 ms 3784 KB Output is correct
34 Correct 20 ms 3800 KB Output is correct
35 Correct 114 ms 20820 KB Output is correct
36 Correct 116 ms 20764 KB Output is correct
37 Correct 122 ms 20252 KB Output is correct
38 Correct 112 ms 20560 KB Output is correct
39 Correct 119 ms 20820 KB Output is correct
40 Correct 73 ms 12460 KB Output is correct
41 Correct 71 ms 12276 KB Output is correct
42 Correct 55 ms 10068 KB Output is correct
43 Correct 60 ms 8936 KB Output is correct
44 Correct 67 ms 12880 KB Output is correct
45 Correct 71 ms 12640 KB Output is correct
46 Correct 57 ms 10316 KB Output is correct
47 Correct 57 ms 9124 KB Output is correct
48 Correct 2346 ms 241272 KB Output is correct
49 Correct 2349 ms 241236 KB Output is correct
50 Correct 2318 ms 241332 KB Output is correct
51 Correct 2063 ms 182608 KB Output is correct
52 Correct 1706 ms 121580 KB Output is correct
53 Correct 1146 ms 119944 KB Output is correct
54 Correct 854 ms 80016 KB Output is correct
55 Correct 834 ms 76844 KB Output is correct
56 Correct 729 ms 59408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 22 ms 4052 KB Output is correct
28 Correct 18 ms 3668 KB Output is correct
29 Correct 21 ms 3676 KB Output is correct
30 Correct 19 ms 3772 KB Output is correct
31 Correct 22 ms 3932 KB Output is correct
32 Correct 19 ms 3608 KB Output is correct
33 Correct 21 ms 3784 KB Output is correct
34 Correct 20 ms 3800 KB Output is correct
35 Correct 114 ms 20820 KB Output is correct
36 Correct 116 ms 20764 KB Output is correct
37 Correct 122 ms 20252 KB Output is correct
38 Correct 112 ms 20560 KB Output is correct
39 Correct 119 ms 20820 KB Output is correct
40 Correct 73 ms 12460 KB Output is correct
41 Correct 71 ms 12276 KB Output is correct
42 Correct 55 ms 10068 KB Output is correct
43 Correct 60 ms 8936 KB Output is correct
44 Correct 67 ms 12880 KB Output is correct
45 Correct 71 ms 12640 KB Output is correct
46 Correct 57 ms 10316 KB Output is correct
47 Correct 57 ms 9124 KB Output is correct
48 Correct 2346 ms 241272 KB Output is correct
49 Correct 2349 ms 241236 KB Output is correct
50 Correct 2318 ms 241332 KB Output is correct
51 Correct 2063 ms 182608 KB Output is correct
52 Correct 1706 ms 121580 KB Output is correct
53 Correct 1146 ms 119944 KB Output is correct
54 Correct 854 ms 80016 KB Output is correct
55 Correct 834 ms 76844 KB Output is correct
56 Correct 729 ms 59408 KB Output is correct
57 Runtime error 1 ms 600 KB Execution killed with signal 11
58 Halted 0 ms 0 KB -