제출 #894722

#제출 시각아이디문제언어결과실행 시간메모리
894722beaboss건물 4 (JOI20_building4)C++14
컴파일 에러
0 ms0 KiB
// Source: https://oj.uz/problem/view/JOI20_building4 // #include "bits/stdc++.h" using namespace std; #define s second #define f first #define pb push_back typedef long long ll; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef vector<int> vi; #define FOR(i, a, b) for (int i = (a); i<b; i++) bool ckmin(int& a, int b){ return b < a ? a = b, true : false; } bool ckmax(int& a, int b){ return b > a ? a = b, true : false; } const int N = (5e5 + 10) * 2; int a[N]; int b[N]; int c[N]; int oc[N]; int swaps[N]; // -1 if not possible, 1 if dependent, 2 if independent int cnt = 0; bool flipped=false; int n; char rev(char ff) { if (ff == 'A') { cnt--; return 'B'; } else { cnt++; return 'A'; } } string chk(string s) { if (flipped) { FOR(i, 0, n) s[i] = rev(s[i]); } int prv = -1; FOR(i, 0, n) { if (s[i] == 'A') { assert(a[i] >= prv); prv = a[i]; } else { assert(b[i] >= prv); prv = b[i]; } } return s; } int count(int s, char c) { int res = 0; FOR(i, 0, n) if (c == s[i]) res++; return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; n *= 2; FOR(i, 0, n) { cin >> a[i]; } FOR(i, 0, n) cin >> b[i]; string ans; c[0] = min(a[0], b[0]); oc[0] = max(a[0], b[0]); if (c[0] == a[0]) { cnt++; ans += 'A'; } else ans += 'B'; for (int i = 1; i < n; i++) { if (min(a[i], b[i]) >= c[i-1]) { c[i] = min(a[i], b[i]); oc[i] = max(a[i], b[i]); } else if (max(a[i], b[i]) >= c[i-1]) { swaps[i]=-1; c[i] = max(a[i], b[i]); oc[i] = min(a[i], b[i]); } else { cout << -1 << endl; return 0; } if (c[i] == a[i]) { cnt++; ans += 'A'; } else ans += 'B'; } if (cnt == n/2) { cout << ans << endl; return 0; } if (cnt > n/2) { flipped=true; FOR(i, 0, n) ans[i] = rev(ans[i]); } c[n] = 1e9 + 10; for (int i = n-1; i >= 0; i--) { assert(c[i] <= c[i+1]); if (swaps[i] == -1) continue; if (oc[i] <= c[i + 1]) swaps[i]=2; // independent else if (swaps[i + 1] != -1 && oc[i] <= oc[i + 1]) swaps[i]=1; // dependent else swaps[i]=-1; // cout << i << swaps[i] << endl; } int st = n-1; int mx = -1; int pos = -1; int res = 0; // assumes that A has less frequency for (int i = n-1; i >= 0; i--) { // cout << i << st << swaps[i] << endl; assert(cnt == count(ans, 'A')); if (swaps[i] == -1 || swaps[i] == 2) { // cout << i << endl; if (i != st) { if (mx != -1) { // cout << st << pos << endl; for (int j = st; j >= pos; j--) { if (cnt != n/2) ans[j] = rev(ans[j]); else { cout << chk(ans) << endl; return 0; } if (cnt == n/2) { cout << chk(ans) << endl; return 0; } } } } st = i-1; // cout << i << st << endl; } if (swaps[i] == 2) { st = i; } if (swaps[i] != -1) { res -= (ans[i] == 'A'); res += (ans[i] == 'B'); if (res > mx) { pos = i; mx = res; } } // cout << i << st << mx << res << ans[i] << endl; } if (0 <= st) { for (int j = st; j >= pos; j--) { if (cnt != n/2) ans[j] = rev(ans[j]); else { cout << chk(ans) << endl; return 0; } } } if (cnt == n/2) cout << chk(ans) << endl; else cout << -1 << endl; }

컴파일 시 표준 에러 (stderr) 메시지

building4.cpp: In function 'int count(int, char)':
building4.cpp:72:25: error: invalid types 'int[int]' for array subscript
   72 |  FOR(i, 0, n) if (c == s[i]) res++;
      |                         ^
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from building4.cpp:4:
building4.cpp: In function 'int main()':
building4.cpp:161:31: error: no matching function for call to 'count(std::string&, char)'
  161 |   assert(cnt == count(ans, 'A'));
      |                               ^
building4.cpp:70:5: note: candidate: 'int count(int, char)'
   70 | int count(int s, char c) {
      |     ^~~~~
building4.cpp:8:11: note:   no known conversion for argument 1 from 'std::string' {aka 'std::__cxx11::basic_string<char>'} to 'int'
    8 | #define s second
      |           ^
building4.cpp:70:15: note: in expansion of macro 's'
   70 | int count(int s, char c) {
      |               ^
In file included from /usr/include/c++/10/algorithm:62,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from building4.cpp:4:
/usr/include/c++/10/bits/stl_algo.h:4077:5: note: candidate: 'template<class _IIter, class _Tp> typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::count(_IIter, _IIter, const _Tp&)'
 4077 |     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
      |     ^~~~~
/usr/include/c++/10/bits/stl_algo.h:4077:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from building4.cpp:4:
building4.cpp:161:31: note:   deduced conflicting types for parameter '_IIter' ('std::__cxx11::basic_string<char>' and 'char')
  161 |   assert(cnt == count(ans, 'A'));
      |                               ^