Submission #199606

# Submission time Handle Problem Language Result Execution time Memory
199606 2020-02-02T08:15:26 Z Ruxandra985 Restore Array (RMI19_restore) C++14
100 / 100
111 ms 116448 KB
#include <bits/stdc++.h>
#define DIMN 5010
#define DIMM 10010
using namespace std;
vector <pair <int , pair <int,int> > > v[DIMN];
int inq[DIMN], sir[DIMN];
deque <int> q;
pair <int,int> sol[DIMN];
pair <int,int> pos[DIMN][DIMN];
int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , m , i , l , r , k , val , nod , vecin , mini , maxi , j , poz;
    fscanf (fin,"%d%d",&n,&m);
    for ( i = 1 ; i <= m ; i++ ){
        fscanf (fin,"%d%d%d%d",&l,&r,&k,&val);
        l++;
        r++;
        if (val == 1){
            mini = (r - (l + k - 1) + 1);
            maxi = (r - l + 1);
        }
        else {
            mini = 0;
            maxi = (r - l + 1 - k);
        }
        ///mini si maxi = cate 1 uri ar putea fi in intervalul i
        v[l-1].push_back(make_pair (r , make_pair(mini , maxi)));
        v[r].push_back(make_pair (l-1 , make_pair(mini , maxi)));
    }
    for (i=1;i<=n;i++){
        v[i].push_back(make_pair(i-1 , make_pair(0 , 1)));
        v[i-1].push_back(make_pair(i , make_pair(0 , 1)));
    }
    for (i=0;i<=n;i++){
        sol[i] = make_pair(0 , i); /// intre 0 si i 1 - uri
        q.push_back(i);
        inq[i] = 1;
    }
    while (!q.empty()){
        nod = q.front();
        q.pop_front();
        inq[nod] = 0;
        for (i = 0 ; i < v[nod].size() ; i++){
            vecin = v[nod][i].first;
            mini = v[nod][i].second.first;
            maxi = v[nod][i].second.second;
            if (vecin >= nod){
                if (sol[nod].first + mini > sol[vecin].first || sol[nod].second + maxi < sol[vecin].second){
                    sol[vecin].first = max(sol[vecin].first , min(vecin , sol[nod].first + mini));
                    sol[vecin].second = min(sol[vecin].second , min(vecin , sol[nod].second + maxi));
                    //printf ("%d %d %d %d\n",nod , vecin , sol[vecin].first , sol[vecin].second);
                    if (sol[vecin].first > sol[vecin].second){
                        fprintf (fout,"-1");
                        return 0;
                    }
                    if (!inq[vecin]){
                        inq[vecin] = 1;
                        q.push_back(vecin);
                    }
                }
            }
            else {
                if (sol[nod].first - maxi > sol[vecin].first || sol[nod].second - mini < sol[vecin].second){
                    sol[vecin].first = max(sol[vecin].first , max (0 , sol[nod].first - maxi));
                    sol[vecin].second = min(sol[vecin].second , max(0 , sol[nod].second - mini));
                    //printf ("%d %d %d %d\n",nod , vecin , sol[vecin-1].first , sol[vecin-1].second);
                    if (sol[vecin].first > sol[vecin].second){
                        fprintf (fout,"-1");
                        return 0;
                    }
                    if (!inq[vecin]){
                        inq[vecin] = 1;
                        q.push_back(vecin);
                    }
                }
            }
        }
    }
    if (0>=sol[1].first)
        pos[1][0] = make_pair(1 , 0);
    if (1<=sol[1].second)
        pos[1][1] = make_pair(1 , 0);
    for (i = 2 ; i <= n ; i++){
        for (j = sol[i].first ; j <= sol[i].second ; j++){

            if (j-1>=0 && pos[i-1][j-1].first){ /// add 1
                pos[i][j] = make_pair(1 , j-1);
            }
            else {
                if (pos[i-1][j].first){ /// add 0
                    pos[i][j] = make_pair(1 , j);
                }
            }
        }
    }
    int stot = 0;
    for (j = sol[n].first ; j <= sol[n].second ; j++){
        if (pos[n][j].first){
            stot = j;
            break;
        }
    }
    poz = n;
    while (poz){
        sir[poz] = stot;
        stot = pos[poz][stot].second;
        poz--;
    }
    for (i=1;i<=n;i++){
        fprintf (fout,"%d ",sir[i] - sir[i-1]);
    }
    return 0;
}

Compilation message

restore.cpp: In function 'int main()':
restore.cpp:45:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0 ; i < v[nod].size() ; i++){
                      ~~^~~~~~~~~~~~~~~
restore.cpp:15: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);
     ~~~~~~~^~~~~~~~~~~~~~~~~~
restore.cpp:17:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d%d%d",&l,&r,&k,&val);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 6 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 116220 KB Output is correct
2 Correct 82 ms 114424 KB Output is correct
3 Correct 82 ms 116448 KB Output is correct
4 Correct 81 ms 109816 KB Output is correct
5 Correct 11 ms 1272 KB Output is correct
6 Correct 11 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 116220 KB Output is correct
2 Correct 82 ms 114424 KB Output is correct
3 Correct 82 ms 116448 KB Output is correct
4 Correct 81 ms 109816 KB Output is correct
5 Correct 11 ms 1272 KB Output is correct
6 Correct 11 ms 1272 KB Output is correct
7 Correct 90 ms 113400 KB Output is correct
8 Correct 81 ms 113912 KB Output is correct
9 Correct 79 ms 111352 KB Output is correct
10 Correct 80 ms 111996 KB Output is correct
11 Correct 12 ms 1272 KB Output is correct
12 Correct 11 ms 1272 KB Output is correct
13 Correct 80 ms 114080 KB Output is correct
14 Correct 39 ms 21748 KB Output is correct
15 Correct 89 ms 21624 KB Output is correct
16 Correct 111 ms 21752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 6 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 6 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 81 ms 116220 KB Output is correct
12 Correct 82 ms 114424 KB Output is correct
13 Correct 82 ms 116448 KB Output is correct
14 Correct 81 ms 109816 KB Output is correct
15 Correct 11 ms 1272 KB Output is correct
16 Correct 11 ms 1272 KB Output is correct
17 Correct 90 ms 113400 KB Output is correct
18 Correct 81 ms 113912 KB Output is correct
19 Correct 79 ms 111352 KB Output is correct
20 Correct 80 ms 111996 KB Output is correct
21 Correct 12 ms 1272 KB Output is correct
22 Correct 11 ms 1272 KB Output is correct
23 Correct 80 ms 114080 KB Output is correct
24 Correct 39 ms 21748 KB Output is correct
25 Correct 89 ms 21624 KB Output is correct
26 Correct 111 ms 21752 KB Output is correct
27 Correct 27 ms 25208 KB Output is correct
28 Correct 27 ms 25336 KB Output is correct
29 Correct 27 ms 24696 KB Output is correct
30 Correct 26 ms 24312 KB Output is correct
31 Correct 26 ms 25080 KB Output is correct
32 Correct 28 ms 24952 KB Output is correct
33 Correct 12 ms 1272 KB Output is correct
34 Correct 11 ms 1272 KB Output is correct
35 Correct 28 ms 24568 KB Output is correct
36 Correct 27 ms 24952 KB Output is correct