# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
792619 | 2023-07-25T07:29:24 Z | 반딧불(#10051) | Line Town (CCO23_day1problem3) | C++17 | 2 ms | 340 KB |
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; void imp(){ puts("-1"); exit(0); } int n; int arr[500002]; void input(); void solve(); int main(){ input(); solve(); } void input(){ scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &arr[i]); vector<int> vec; for(int i=1; i<=n; i++) if(arr[i]) vec.push_back(abs(arr[i])); sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); for(int i=1; i<=n; i++){ if(!arr[i]) continue; arr[i] = (lower_bound(vec.begin(), vec.end(), abs(arr[i])) - vec.begin() + 1) * (arr[i]<0 ? -1 : 1); } int odd1 = 0, even1 = 0; for(int i=1; i<=n; i++) if(arr[i] == 1){ if(i%2) odd1++; else even1++; } if(abs(odd1 - even1) > 1) imp(); } ll ans; ll prf[500002], suf[500002]; int c[500002]; void solve(){ for(int i=0; i<=n+1; i++) prf[i] = suf[i] = INF; prf[0] = 0; suf[n+1] = 0; ll cnt = 0; int MX = 0; /// prf�� ���Ѵ� for(int i=1; i<=n; i++) c[i] = arr[i]; cnt = 0, MX = 0; for(int i=1; i<=n; i++){ if(c[i] == -1){ prf[i] = cnt; continue; } int j = i+1; while(j<=n && c[j] != c[j-1]) j++; if(j>n) break; MX = max(MX, j); for(int t=j; t>=i+1; t--){ cnt++; c[t] = c[t-1] = -c[t]; } if(MX<=i+1) prf[++i] = cnt; } /// suf�� ���Ѵ� for(int i=1; i<=n; i++) c[i] = arr[i]; cnt = 0, MX = n+1; for(int i=n; i>=1; i--){ if(c[i] == 1){ suf[i] = cnt; continue; } int j = i-1; while(j>=1 && c[j] != c[j+1]) j--; if(j<1) break; MX = min(MX, j); for(int t=j; t<=i-1; t++){ cnt++; c[t] = c[t+1] = -c[t]; } if(MX>=i-1) suf[--i] = cnt; } ans = INF; for(int i=0; i<=n; i++) ans = min(ans, prf[i] + suf[i+1]); printf("%lld", ans == INF ? -1 : ans); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Incorrect | 0 ms | 212 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Incorrect | 0 ms | 212 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 212 KB | Output is correct |
2 | Correct | 1 ms | 212 KB | Output is correct |
3 | Correct | 1 ms | 212 KB | Output is correct |
4 | Correct | 1 ms | 340 KB | Output is correct |
5 | Correct | 1 ms | 340 KB | Output is correct |
6 | Correct | 1 ms | 340 KB | Output is correct |
7 | Correct | 1 ms | 340 KB | Output is correct |
8 | Correct | 1 ms | 340 KB | Output is correct |
9 | Correct | 2 ms | 340 KB | Output is correct |
10 | Correct | 2 ms | 340 KB | Output is correct |
11 | Correct | 2 ms | 340 KB | Output is correct |
12 | Correct | 2 ms | 340 KB | Output is correct |
13 | Incorrect | 2 ms | 340 KB | Output isn't correct |
14 | Halted | 0 ms | 0 KB | - |