CF580-B. Make Product Equal One

https://codeforces.com/contest/1206/problem/B

  • 평범한 이지선다 DP. 코드가 깔끔하게 나와서 위안거리가 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n, odd = 0xfffff, even = 0, a;
  cin >> n;
  for(int i =0 ;i < n; i++) {
    cin >> a;
    int one = abs(a-1);
    int minus = abs(a+1);
    int nodd = min(odd+one, even+minus);
    int neven = min(even+one, odd+minus);
    odd = nodd;
    even = neven;
  }
  cout << even;
}