Submission #1126407

#TimeUsernameProblemLanguageResultExecution timeMemory
1126407luvnaRestore Array (RMI19_restore)C++20
100 / 100
290 ms1032 KiB
#include<bits/stdc++.h> #define MASK(i) (1 << (i)) #define pub push_back #define all(v) v.begin(), v.end() #define compact(v) v.erase(unique(all(v)), end(v)) #define pii pair<int,int> #define fi first #define se second #define endl "\n" #define sz(v) (int)(v).size() #define dbg(x) "[" #x " = " << (x) << "]" using namespace std; template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;} template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;} typedef long long ll; typedef long double ld; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rand(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);} const int N = 1e5 + 15; const ll INF = 1e18; int n, q; int t[N], l[N], r[N], k[N]; namespace subtask1{ bool check(void){ return n <= 18 && q <= 200; } bool check(int mask){ for(int j = 1; j <= q; j++){ int cnt = 0; for(int i = l[j]; i <= r[j]; i++) cnt += ((mask >> i & 1) == t[j]); if(!t[j] && cnt < k[j]) return false; if(t[j] && cnt < (r[j] - l[j] + 1) - k[j] + 1) return false; } return true; } void main(){ for(int mask = 0; mask < (1 << n); mask++){ if(check(mask)){ for(int i = 0; i < n; i++) cout << (mask >> i & 1) << " "; return; } } return cout << -1, void(); } } namespace subtask4{ /* dp[i] = number of zeros from 1 -> i dp[0] = 0 -> prefix sum base: 0 <= diff between dp[i-1] and dp[i] <= 1 */ struct edge{ int u, v, w; edge() : u(0), v(0), w(0) {} edge(int u, int v, int w) : u(u), v(v), w(w) {} void get(int &U, int &V, int &W){ U = u; V = v; W = w; } }; ll dp[N]; vector<edge> edges; void main(){ //notes: detect negative cycle -> mul edge with -1 for(int i = 1; i <= q; i++){ l[i]++, r[i]++; // t = 0 -> at least k zeros from l -> r // dp[r] - dp[l-1] >= k // <=> dp[l-1] + k <= dp[r] -> create and edge (l-1, r, k) if(!t[i]) edges.push_back(edge(l[i]-1, r[i],-k[i])); // t = 1 -> zeros <= k-1 // dp[r] - dp[l-1] <= k-1 // dp[r] - k + 1 <= dp[l-1] else edges.push_back(edge(r[i],l[i]-1,k[i] - 1)); } //base case: 0 <= dp[i] - dp[i-1] <= 1 -> create 2*n edges for(int i = 1; i <= n; i++){ // low bound // dp[i] - dp[i-1] >= 0 // dp[i-1] <= dp[i] edges.push_back(edge(i-1,i,0)); // up bound // dp[i] - dp[i-1] <= 1 // dp[i] - 1 <= dp[i-1] edges.push_back(edge(i,i-1,1)); } fill(dp, dp + 1 + n, INF); dp[0] = INF; for(int turn = 0; turn < n; turn++){ for(int i = 0; i < sz(edges); i++){ int u, v, w; edges[i].get(u,v,w); minimize(dp[v], dp[u] + w); } } bool exist_neg_cycle = false; for(int i = 0; i < sz(edges); i++){ int u, v, w; edges[i].get(u,v,w); if(dp[u] != INF && minimize(dp[v], dp[u] + w)){ exist_neg_cycle = true; break; } } if(exist_neg_cycle) return cout << -1, void(); for(int i = 1; i <= n; i++) cout << (dp[i] - dp[i-1] < 0 ? 0 : 1) << " "; } } void solve(){ cin >> n >> q; for(int i = 1; i <= q; i++) cin >> l[i] >> r[i] >> k[i] >> t[i]; /*if(subtask1::check()) subtask1::main(); else*/ subtask4::main(); } signed main(){ ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); #define task "task" if(fopen(task".INP", "r")){ freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout); } int t; t = 1; //cin >> t; while(t--) solve(); }

Compilation message (stderr)

restore.cpp: In function 'int main()':
restore.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
restore.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...