Submission #1112880

#TimeUsernameProblemLanguageResultExecution timeMemory
1112880ntdaccodeLine Town (CCO23_day1problem3)C++17
0 / 25
1 ms336 KiB
#include<bits/stdc++.h> #define fori(i,a,b) for(int i=a;i<=b;i++) #define int long long #define pb push_back using namespace std; typedef pair<int,int> ii; typedef tuple<int,int,int> tp; const int M=1e6+10; const int N=5e5+10; const int mod=1e9+7; int n,f[N],a[N],b[N],g[N]; void sub1() { int kq = 1e9; for(int pos = 0;pos <= n; pos++) { for(int i = 1;i <= n; i++) b[i] = a[i]; bool ok = 0; int res = 0; // for(int i = 1;i <= pos ; i++) { if(b[i] == 1) { if(b[i + 1] == 1 && i != pos) i++,res++; else ok = 1; } } for(int i = pos + 1;i <= n;i++) { if(b[i] == -1) { if(b[i + 1] == -1 && i != n) i++,res++; else ok = 1; } } if(ok == 0) kq = min(kq,res); } cout << (kq == 1e9 ? -1 : kq); } void sub2() { int kq = 1e9; f[0] = g[n+1] = 0; f[1] = (a[1] == -1 ? 0 : 1e9); g[n] = (a[n] == 1 ? 0 : 1e9); for(int i = 2;i <= n; i++) { if(a[i] == 1) { if(a[i-1] == 1) f[i] = f[i - 2] + 1; else f[i] = 1e9; } else f[i] = f[i - 1]; } for(int i = n - 1;i != 0;i--) { if(a[i] == -1) { if(a[i+1] == -1) g[i] = g[i + 2] + 1; else g[i] = 1e9; } else g[i] = g[i + 1]; } for(int i = 0;i <= n;i++) kq = min(kq,f[i] + g[i+1]); cout << (kq == 1e9 ? -1 : kq); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen("1.inp","r")) { freopen("1.inp","r",stdin); freopen("1.out","w",stdout); } #define task "" if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin >> n; for(int i = 1;i <= n; i++) cin >> a[i]; if(n <= 2000) sub1(); else sub2(); //sub2(); }

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     freopen("1.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("1.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:76:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...