Submission #226547

# Submission time Handle Problem Language Result Execution time Memory
226547 2020-04-24T08:17:09 Z Ruxandra985 Rope (JOI17_rope) C++14
100 / 100
1297 ms 96444 KB
#include <bits/stdc++.h>

using namespace std;
int v[1000010] , fii[1000010] , sol[1000010];
int n , m;
vector <int> w[1000010] , now;
pair <int,int> f[1000010] , fi[1000010];
void solve (int caz){
    int i , first , curr , idx , x , fmax , maxi , j;

    for (i = 1 ; i <= m ; i++)
        w[i].clear();

    for (i = caz ; i < n ; i += 2 ){

        if (v[i] == v[i + 1]){
            w[v[i]].push_back(i); /// unde incepe perechea
        }
        else {
            w[v[i]].push_back(i); /// unde incepe perechea
            w[v[i + 1]].push_back(i); /// unde incepe perechea
        }


    }

    if (i == n)
        w[v[n]].push_back(n);

    /// in w, tin pt fiecare culoare, in ce perechi e

    for (i = 1 ; i <= m ; i++){

        //if (caz == 2 && i == 1)
          //  printf ("a");

        curr = 0;
        first = 2 * w[i].size();
        maxi = 0;
        fmax = 0;
        now.clear();

        /// as vrea sa tin cea mai mare val a unui elem modificat

        for (j = 0 ; j < w[i].size() ; j++){

            idx = w[i][j];

            x = v[idx];
            if (f[x].second != i){
                f[x] = make_pair(1 , i);
                now.push_back(x);
            }
            else f[x].first++;



            if (x != i)
                curr++;

            x = v[idx + 1];
            if (f[x].second != i){
                f[x] = make_pair(1 , i);
                now.push_back(x);
            }
            else f[x].first++;

            if (x != i)
                curr++;

        }

        /// in now ai valorile schimbate !!!!

        if (caz == 2){
            x = v[1];
            if (f[x].second != i){
                f[x] = make_pair(0 , i);
                now.push_back(x);
            }



            if (x == i){
                first++;
                f[x].first++;
            }
        }

        /// in first tii cate elem iau valoarea i
        /// curr = cate elemente ai schimbat in i


        for (j = 0 ; j < now.size() ; j++){

            x = now[j];

            if (maxi < fii[x] - f[x].first){
                maxi = fii[x] - f[x].first;
                fmax = x;
            }

        }

        j = m;
        while (j && f[ fi[j].second ].second == i)
            j--;

        if (j){

            if (maxi < fi[j].first){
                maxi = fi[j].first;
                fmax = fi[j].second;
            }
        }




        sol[i] = min(sol[i] , curr + n - first - maxi);
        sol[fmax] = min(sol[fmax] , curr + n - first - maxi);

    }



}


int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int i;
    fscanf (fin,"%d%d",&n,&m);
    for (i = 1 ; i <= n ; i++){
        fscanf (fin,"%d",&v[i]);
        fi[v[i]].first++;
        fii[v[i]]++;
    }

    for (i = 1 ; i <= m ; i++)
        fi[i].second = i;

    sort (fi + 1 , fi + m + 1);

    if (m == 1){ /// sa scap
        fprintf (fout,"0");
        return 0;
    }

    for (i = 1 ; i <= m ; i++)
        sol[i] = 2000000000;

    solve (1);

    for (i = 1 ; i <= m ; i++){
        f[i].second = 0;
    }


    solve (2);

    for (i = 1 ; i <= m ; i++)
        fprintf (fout,"%d\n",sol[i]);

    return 0;
}

Compilation message

rope.cpp: In function 'void solve(int)':
rope.cpp:45:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0 ; j < w[i].size() ; j++){
                      ~~^~~~~~~~~~~~~
rope.cpp:94:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0 ; j < now.size() ; j++){
                      ~~^~~~~~~~~~~~
rope.cpp: In function 'int main()':
rope.cpp:135:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d",&n,&m);
     ~~~~~~~^~~~~~~~~~~~~~~~~~
rope.cpp:137:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d",&v[i]);
         ~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 35 ms 25088 KB Output is correct
28 Correct 32 ms 24960 KB Output is correct
29 Correct 34 ms 24960 KB Output is correct
30 Correct 33 ms 25088 KB Output is correct
31 Correct 32 ms 25088 KB Output is correct
32 Correct 32 ms 24952 KB Output is correct
33 Correct 32 ms 24960 KB Output is correct
34 Correct 31 ms 25080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 35 ms 25088 KB Output is correct
28 Correct 32 ms 24960 KB Output is correct
29 Correct 34 ms 24960 KB Output is correct
30 Correct 33 ms 25088 KB Output is correct
31 Correct 32 ms 25088 KB Output is correct
32 Correct 32 ms 24952 KB Output is correct
33 Correct 32 ms 24960 KB Output is correct
34 Correct 31 ms 25080 KB Output is correct
35 Correct 39 ms 25216 KB Output is correct
36 Correct 38 ms 25208 KB Output is correct
37 Correct 47 ms 25292 KB Output is correct
38 Correct 42 ms 25208 KB Output is correct
39 Correct 39 ms 25208 KB Output is correct
40 Correct 37 ms 25208 KB Output is correct
41 Correct 36 ms 25216 KB Output is correct
42 Correct 36 ms 25216 KB Output is correct
43 Correct 36 ms 25216 KB Output is correct
44 Correct 37 ms 25212 KB Output is correct
45 Correct 39 ms 25248 KB Output is correct
46 Correct 36 ms 25208 KB Output is correct
47 Correct 36 ms 25208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 35 ms 25088 KB Output is correct
28 Correct 32 ms 24960 KB Output is correct
29 Correct 34 ms 24960 KB Output is correct
30 Correct 33 ms 25088 KB Output is correct
31 Correct 32 ms 25088 KB Output is correct
32 Correct 32 ms 24952 KB Output is correct
33 Correct 32 ms 24960 KB Output is correct
34 Correct 31 ms 25080 KB Output is correct
35 Correct 39 ms 25216 KB Output is correct
36 Correct 38 ms 25208 KB Output is correct
37 Correct 47 ms 25292 KB Output is correct
38 Correct 42 ms 25208 KB Output is correct
39 Correct 39 ms 25208 KB Output is correct
40 Correct 37 ms 25208 KB Output is correct
41 Correct 36 ms 25216 KB Output is correct
42 Correct 36 ms 25216 KB Output is correct
43 Correct 36 ms 25216 KB Output is correct
44 Correct 37 ms 25212 KB Output is correct
45 Correct 39 ms 25248 KB Output is correct
46 Correct 36 ms 25208 KB Output is correct
47 Correct 36 ms 25208 KB Output is correct
48 Correct 271 ms 38776 KB Output is correct
49 Correct 287 ms 39164 KB Output is correct
50 Correct 274 ms 39032 KB Output is correct
51 Correct 263 ms 38520 KB Output is correct
52 Correct 252 ms 37240 KB Output is correct
53 Correct 231 ms 38136 KB Output is correct
54 Correct 217 ms 37240 KB Output is correct
55 Correct 218 ms 37016 KB Output is correct
56 Correct 215 ms 36728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 17 ms 23808 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 17 ms 23808 KB Output is correct
9 Correct 18 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 17 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 19 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 18 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 19 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 17 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 18 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 35 ms 25088 KB Output is correct
28 Correct 32 ms 24960 KB Output is correct
29 Correct 34 ms 24960 KB Output is correct
30 Correct 33 ms 25088 KB Output is correct
31 Correct 32 ms 25088 KB Output is correct
32 Correct 32 ms 24952 KB Output is correct
33 Correct 32 ms 24960 KB Output is correct
34 Correct 31 ms 25080 KB Output is correct
35 Correct 39 ms 25216 KB Output is correct
36 Correct 38 ms 25208 KB Output is correct
37 Correct 47 ms 25292 KB Output is correct
38 Correct 42 ms 25208 KB Output is correct
39 Correct 39 ms 25208 KB Output is correct
40 Correct 37 ms 25208 KB Output is correct
41 Correct 36 ms 25216 KB Output is correct
42 Correct 36 ms 25216 KB Output is correct
43 Correct 36 ms 25216 KB Output is correct
44 Correct 37 ms 25212 KB Output is correct
45 Correct 39 ms 25248 KB Output is correct
46 Correct 36 ms 25208 KB Output is correct
47 Correct 36 ms 25208 KB Output is correct
48 Correct 271 ms 38776 KB Output is correct
49 Correct 287 ms 39164 KB Output is correct
50 Correct 274 ms 39032 KB Output is correct
51 Correct 263 ms 38520 KB Output is correct
52 Correct 252 ms 37240 KB Output is correct
53 Correct 231 ms 38136 KB Output is correct
54 Correct 217 ms 37240 KB Output is correct
55 Correct 218 ms 37016 KB Output is correct
56 Correct 215 ms 36728 KB Output is correct
57 Correct 1297 ms 96444 KB Output is correct
58 Correct 1073 ms 71676 KB Output is correct
59 Correct 1067 ms 71544 KB Output is correct
60 Correct 1139 ms 77688 KB Output is correct
61 Correct 1131 ms 77816 KB Output is correct
62 Correct 485 ms 55800 KB Output is correct
63 Correct 729 ms 63096 KB Output is correct
64 Correct 608 ms 58648 KB Output is correct
65 Correct 350 ms 47224 KB Output is correct