이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pll pair<int, int>
#define pb push_back
#define fi first
#define se second
using namespace std;
const int MX=450;
int n, pref[MX], res;
vector<char>cc(MX), tmp;
int check1(vector<char>c){
int ans=0;
for(int i=1; i<n; i++){
if(c[i]!=c[i+1])continue;
int idx=i;
while(idx<=n && c[idx]==c[i]){
idx++;
}
if(idx>n) break;
ans+=idx-(i+1);
swap(c[idx], c[i+1]);
}
for(int i=n; i>=2; i--){
if(c[i]!=c[i-1])continue;
int idx=i;
while(idx>=1 && c[idx]==c[i]){
idx--;
}
if(idx<1) break;
ans+=(i-1)-idx;
swap(c[idx], c[i-1]);
}
for(int i=2; i<=n; i++){
if(c[i]==c[i-1]){
// cout << -1 << endl;
return -1;
}
}
return ans;
}
int check2(vector<char>c){
int ans=0;
for(int i=n; i>=2; i--){
if(c[i]!=c[i-1])continue;
int idx=i;
while(idx>=1 && c[idx]==c[i]){
idx--;
}
if(idx<1) break;
ans+=(i-1)-idx;
swap(c[idx], c[i-1]);
}
for(int i=1; i<n; i++){
if(c[i]!=c[i+1])continue;
int idx=i;
while(idx<=n && c[idx]==c[i]){
idx++;
}
if(idx>n) break;
ans+=idx-(i+1);
swap(c[idx], c[i+1]);
}
for(int i=2; i<=n; i++){
if(c[i]==c[i-1]){
// cout << -1 << endl;
return -1;
}
}
return ans;
}
int main(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> cc[i];
}
tmp=cc;
int ans=1e9;
// cout << check1(cc) << " " << check2(cc)<< endl;
res=min(check1(cc), check2(cc));
cout << res << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:80:6: warning: unused variable 'ans' [-Wunused-variable]
80 | int ans=1e9;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |