Submission #477516

# Submission time Handle Problem Language Result Execution time Memory
477516 2021-10-02T11:28:07 Z nicolaalexandra Xylophone (JOI18_xylophone) C++14
100 / 100
60 ms 544 KB
#include <bits/stdc++.h>
#include "xylophone.h"
#define DIM 5010
using namespace std;

int v[DIM],a[DIM],f[DIM];
int n,i,pas;
/*
void answer (int i, int val){
    //cout<<i<<" "<<val<<"\n";
    cout<<val<<"\n";
}

int query (int x, int y){
    pas++;
    int maxi = 0, mini = n+1;
    for (int i=x;i<=y;i++){
        mini = min (mini,a[i]);
        maxi = max (maxi,a[i]);
    }
    return maxi - mini;
}
*/

void solve (int n){

    /// determin pozitia lui n
    int st = 1, dr = n, sol_poz;
    while (st <= dr){
        int mid = (st+dr)>>1;
        if (query(1,mid) == n-1){
            sol_poz = mid;
            dr = mid-1;
        } else st = mid+1;
    }

    v[sol_poz] = n;

    /*divide (1,sol_poz-1,n,1,1);
    divide (sol_poz+1,n,n,1,0);
    */

    f[n] = 1;

    if (sol_poz > 1){
        v[sol_poz-1] = n - query (sol_poz-1,sol_poz);
        f[v[sol_poz-1]] = 1;
    }

    if (sol_poz < n){
        v[sol_poz+1] = n - query (sol_poz,sol_poz+1);
        f[v[sol_poz+1]] = 1;
    }

    for (i=sol_poz-2;i>=1;i--){
        int dif = query (i,i+1);

        if (v[i+1] - dif <= 0){
            v[i] = v[i+1] + dif;
            f[v[i]] = 1;
            continue;
        }

        if (v[i+1] + dif > n){
            v[i] = v[i+1] - dif;
            f[v[i]] = 1;
            continue;
        }

        if (!f[v[i+1]-dif] && !f[v[i+1]+dif]){ /// am doua variante posibile

            int dif2 = query (i,i+2);

            int val = v[i+1] + dif;
            if (max(val,max(v[i+1],v[i+2])) - min (val,min(v[i+1],v[i+2])) == dif2)
                v[i] = val;
            else v[i] = v[i+1] - dif;

            /*
            if (dif2 == max(v[i+1],v[i+2]) - min(v[i+1],v[i+2])){

                if (v[i+1] < v[i+2])
                    v[i] = v[i+1] + dif;
                else v[i] = v[i-1] - dif;

            } else {

                if (dif2 == dif){

                    if (v[i+1] < v[i+2])
                        v[i] = v[i+1] + dif;
                    else v[i] = v[i+1] - dif;

                } else {

                    if (v[i+1] < v[i+2])
                        v[i] = v[i+1] - dif;
                    else v[i] = v[i+1] + dif;

                }

            }
            */

            f[v[i]] = 1;

            continue;
        }


        if (!f[v[i+1]-dif]){
            v[i] =  v[i+1] - dif;
            f[v[i]] = 1;
        } else {
            v[i] = v[i+1] + dif;
            f[v[i]] = 1;
        }
    }

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

        int dif = query (i-1,i);
        if (v[i-1] - dif <= 0){
            v[i] = v[i-1] + dif;
            f[v[i]] = 1;
            continue;
        }

        if (v[i-1] + dif > n){
            v[i] = v[i-1] - dif;
            f[v[i]] = 1;
            continue;
        }

        if (!f[v[i-1]-dif] && !f[v[i-1]+dif]){

            int dif2 = query (i-2,i);
            if (max(v[i-1]+dif,max(v[i-1],v[i-2])) - min(v[i-1]+dif,min(v[i-1],v[i-2])) == dif2)
                v[i] = v[i-1] + dif;
            else v[i] = v[i-1] - dif;

            f[v[i]] = 1;

            continue;
        }

        if (!f[v[i-1]-dif]){
            v[i] = v[i-1] - dif;
            f[v[i]] = 1;
        } else {
            v[i] = v[i-1] + dif;
            f[v[i]] = 1;
        }


    }


    for (int i=1;i<=n;i++)
        answer (i,v[i]);

}

/*
int main (){


    ifstream cin ("date.in");
    ofstream cout ("date.out");

    cin>>n;
    for (i=1;i<=n;i++)
        cin>>a[i];

    solve (n);

    cout<<pas;

    return 0;
}
*/

Compilation message

xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:51:34: warning: 'sol_poz' may be used uninitialized in this function [-Wmaybe-uninitialized]
   51 |         v[sol_poz+1] = n - query (sol_poz,sol_poz+1);
      |                            ~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 2 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 2 ms 200 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 2 ms 200 KB Output is correct
11 Correct 2 ms 200 KB Output is correct
12 Correct 2 ms 200 KB Output is correct
13 Correct 1 ms 200 KB Output is correct
14 Correct 2 ms 300 KB Output is correct
15 Correct 1 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 2 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 2 ms 200 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 2 ms 200 KB Output is correct
11 Correct 2 ms 200 KB Output is correct
12 Correct 2 ms 200 KB Output is correct
13 Correct 1 ms 200 KB Output is correct
14 Correct 2 ms 300 KB Output is correct
15 Correct 1 ms 200 KB Output is correct
16 Correct 4 ms 292 KB Output is correct
17 Correct 5 ms 200 KB Output is correct
18 Correct 8 ms 200 KB Output is correct
19 Correct 10 ms 200 KB Output is correct
20 Correct 10 ms 308 KB Output is correct
21 Correct 11 ms 200 KB Output is correct
22 Correct 11 ms 200 KB Output is correct
23 Correct 10 ms 200 KB Output is correct
24 Correct 8 ms 200 KB Output is correct
25 Correct 8 ms 304 KB Output is correct
26 Correct 10 ms 288 KB Output is correct
27 Correct 11 ms 304 KB Output is correct
28 Correct 11 ms 200 KB Output is correct
29 Correct 10 ms 308 KB Output is correct
30 Correct 6 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 2 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 2 ms 200 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 200 KB Output is correct
10 Correct 2 ms 200 KB Output is correct
11 Correct 2 ms 200 KB Output is correct
12 Correct 2 ms 200 KB Output is correct
13 Correct 1 ms 200 KB Output is correct
14 Correct 2 ms 300 KB Output is correct
15 Correct 1 ms 200 KB Output is correct
16 Correct 4 ms 292 KB Output is correct
17 Correct 5 ms 200 KB Output is correct
18 Correct 8 ms 200 KB Output is correct
19 Correct 10 ms 200 KB Output is correct
20 Correct 10 ms 308 KB Output is correct
21 Correct 11 ms 200 KB Output is correct
22 Correct 11 ms 200 KB Output is correct
23 Correct 10 ms 200 KB Output is correct
24 Correct 8 ms 200 KB Output is correct
25 Correct 8 ms 304 KB Output is correct
26 Correct 10 ms 288 KB Output is correct
27 Correct 11 ms 304 KB Output is correct
28 Correct 11 ms 200 KB Output is correct
29 Correct 10 ms 308 KB Output is correct
30 Correct 6 ms 308 KB Output is correct
31 Correct 27 ms 304 KB Output is correct
32 Correct 38 ms 424 KB Output is correct
33 Correct 51 ms 300 KB Output is correct
34 Correct 50 ms 312 KB Output is correct
35 Correct 54 ms 304 KB Output is correct
36 Correct 45 ms 312 KB Output is correct
37 Correct 46 ms 320 KB Output is correct
38 Correct 42 ms 440 KB Output is correct
39 Correct 60 ms 308 KB Output is correct
40 Correct 45 ms 316 KB Output is correct
41 Correct 55 ms 320 KB Output is correct
42 Correct 37 ms 308 KB Output is correct
43 Correct 43 ms 544 KB Output is correct
44 Correct 42 ms 300 KB Output is correct
45 Correct 59 ms 308 KB Output is correct
46 Correct 54 ms 300 KB Output is correct
47 Correct 58 ms 304 KB Output is correct
48 Correct 30 ms 296 KB Output is correct
49 Correct 40 ms 320 KB Output is correct
50 Correct 49 ms 320 KB Output is correct
51 Correct 53 ms 304 KB Output is correct
52 Correct 47 ms 300 KB Output is correct
53 Correct 53 ms 312 KB Output is correct
54 Correct 43 ms 304 KB Output is correct
55 Correct 51 ms 320 KB Output is correct
56 Correct 35 ms 296 KB Output is correct
57 Correct 45 ms 436 KB Output is correct
58 Correct 56 ms 320 KB Output is correct
59 Correct 53 ms 316 KB Output is correct
60 Correct 48 ms 312 KB Output is correct