Submission #199606

#TimeUsernameProblemLanguageResultExecution timeMemory
199606Ruxandra985Restore Array (RMI19_restore)C++14
100 / 100
111 ms116448 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...