답안 #121114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
121114 2019-06-26T06:32:53 Z 이온조(#2969) Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
0 / 100
4 ms 384 KB
#include <bits/stdc++.h>
using namespace std;

char S[444], A[444], B[444];

int main() {
	int N, sa = 0, sb = 0; scanf("%d",&N);
	for(int i=1; i<=N; i++) {
		scanf(" %c", &S[i]);
		A[i] = B[i] = S[i];
	}
	for(int k=1; k<=3*N; k++) {
		for(int i=2; i<=N; i++) {
			if(A[i] == A[i-1]) {
				int x = -1;
				for(int j=i+1; j<=N; j++) if(A[j] != A[i]) {x = j; break;}
				if(x != -1) for(int j=x; j>i; j--) {swap(A[j], A[j-1]); ++sa;}
			}
		}
		for(int i=N-1; i>=1; i--) {
			if(A[i] == A[i+1]) {
				int x = -1;
				for(int j=i-1; j>=1; j--) if(A[j] != A[i]) {x = j; break;}
				if(x != -1)	for(int j=x; j<i; j++) {swap(A[j], A[j+1]); ++sa;}
			}
		}
	}
	for(int i=2; i<=N; i++) if(A[i] == A[i-1]) sa = 1e9;

	for(int k=1; k<=3*N; k++) {
		for(int i=2; i<=N; i++) {
			if(B[i] == B[i-1]) {
				int x = -1;
				for(int j=i+1; j<=N; j++) if(B[j] != B[i]) {x = j; break;}
				if(x != -1) for(int j=x; j>i; j--) {swap(B[j], B[j-1]); ++sb;}
			}
		}
		for(int i=N-1; i>=1; i--) {
			if(B[i] == B[i+1]) {
				int x = -1;
				for(int j=i-1; j>=1; j--) if(B[j] != B[i]) {x = j; break;}
				if(x != -1)	for(int j=x; j<i; j++) {swap(B[j], B[j+1]); ++sb;}
			}
		}
	}
	for(int i=2; i<=N; i++) if(B[i] == B[i-1]) sb = 1e9;

	if(min(sa, sb) == 1e9) puts("-1");
	else printf("%d", min(sa, sb));
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:7:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int N, sa = 0, sb = 0; scanf("%d",&N);
                         ~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:9:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c", &S[i]);
   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Incorrect 2 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Incorrect 2 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Incorrect 3 ms 256 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Incorrect 2 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -