Submission #853510

# Submission time Handle Problem Language Result Execution time Memory
853510 2023-09-24T13:17:28 Z lolismek Restore Array (RMI19_restore) C++14
100 / 100
557 ms 41260 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>

#define pii pair <int, int>

using namespace std;

const int NMAX = 5001;
const int INF = 1e9 + 1;

vector <pii> adj[NMAX + 1];

int d[NMAX + 1];

bool inq[NMAX + 1];
int f[NMAX + 1];

int ans[NMAX + 1];

int main(){

    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= m; i++){
        int l, r, k, v;
        cin >> l >> r >> k >> v;
        ++l, ++r;

        int a, b, c;

        if(v == 1){
            a = r, b = l - 1, c = -((r - l + 1) - k + 1);
        }else{
            a = l - 1, b = r, c = (r - l + 1) - k;
        }
        adj[a].push_back({b, c});
    }

    for(int i = 0; i <= n; i++){
        d[i] = INF;
    }

    int source = 0;

    for(int i = n; i >= 1; i--){
        adj[i].push_back({i - 1, 0});
        adj[i - 1].push_back({i, 1});
    }

    queue <int> Q;
    Q.push(source);
    d[source] = 0;
    inq[source] = true;

    while(!Q.empty()){
        int node = Q.front();
        Q.pop();

        inq[node] = false;

        if(++f[node] > n){
            cout << -1 << '\n';
            exit(0);
        }

        for(pii vec : adj[node]){
            if(d[node] + vec.second < d[vec.first]){
                
                d[vec.first] = d[node] + vec.second;
                if(!inq[vec.first]){
                    Q.push(vec.first);
                }
            }
        }
    }

    vector <pii> aux;
    for(int i = 1; i <= n; i++){
        if(d[i] != INF){
            aux.push_back({i, d[i]});
        }
    }

    sort(aux.begin(), aux.end());

    int mini = INF;
    for(int i = 0; i <= n; i++){
        mini = min(mini, d[i]);
    }

    for(int i = (int)aux.size() - 1; i >= 0; i--){
        int r = aux[i].second;
        int sz = aux[i].first;
        if(i > 0){
            r -= aux[i - 1].second;
            sz -= aux[i - 1].first;
        }

        if(r > sz){
            cout << -1 << '\n';
            exit(0);
        }

        for(int j = aux[i].first; r > 0; j--, r--){
            ans[j] = 1;
        }
    }

    for(int i = 1; i <= n; i++){
        cout << ans[i] << ' ';
    }
    cout << '\n';
   

    return 0;
}

/*
5 1
0 4 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 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 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 856 KB Output is correct
2 Correct 13 ms 1112 KB Output is correct
3 Correct 14 ms 1116 KB Output is correct
4 Correct 12 ms 1116 KB Output is correct
5 Correct 521 ms 38356 KB Output is correct
6 Correct 557 ms 36488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 856 KB Output is correct
2 Correct 13 ms 1112 KB Output is correct
3 Correct 14 ms 1116 KB Output is correct
4 Correct 12 ms 1116 KB Output is correct
5 Correct 521 ms 38356 KB Output is correct
6 Correct 557 ms 36488 KB Output is correct
7 Correct 13 ms 1116 KB Output is correct
8 Correct 13 ms 1108 KB Output is correct
9 Correct 20 ms 1084 KB Output is correct
10 Correct 12 ms 1036 KB Output is correct
11 Correct 547 ms 39656 KB Output is correct
12 Correct 513 ms 38340 KB Output is correct
13 Correct 12 ms 1112 KB Output is correct
14 Correct 18 ms 1104 KB Output is correct
15 Correct 27 ms 1076 KB Output is correct
16 Correct 83 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 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 1 ms 348 KB Output is correct
11 Correct 12 ms 856 KB Output is correct
12 Correct 13 ms 1112 KB Output is correct
13 Correct 14 ms 1116 KB Output is correct
14 Correct 12 ms 1116 KB Output is correct
15 Correct 521 ms 38356 KB Output is correct
16 Correct 557 ms 36488 KB Output is correct
17 Correct 13 ms 1116 KB Output is correct
18 Correct 13 ms 1108 KB Output is correct
19 Correct 20 ms 1084 KB Output is correct
20 Correct 12 ms 1036 KB Output is correct
21 Correct 547 ms 39656 KB Output is correct
22 Correct 513 ms 38340 KB Output is correct
23 Correct 12 ms 1112 KB Output is correct
24 Correct 18 ms 1104 KB Output is correct
25 Correct 27 ms 1076 KB Output is correct
26 Correct 83 ms 1104 KB Output is correct
27 Correct 9 ms 1112 KB Output is correct
28 Correct 14 ms 1116 KB Output is correct
29 Correct 11 ms 1116 KB Output is correct
30 Correct 9 ms 1112 KB Output is correct
31 Correct 9 ms 1116 KB Output is correct
32 Correct 9 ms 1116 KB Output is correct
33 Correct 550 ms 41060 KB Output is correct
34 Correct 551 ms 41260 KB Output is correct
35 Correct 10 ms 1116 KB Output is correct
36 Correct 9 ms 1112 KB Output is correct