입력받는 숫자에대해서, 증가하고 있는지 감소하고 있는지 카운트를 해준다.
감소하다가 다시 증가하는 상태가 되면
증가 개수 * 감소 개수 만큼의 조합이 나오게 되므로, 이 값을 더해준다.
즉
1 3 5 4 1 의 경우 증가 카운트 =2, 감소 카운트 = 2로 1 3 5 4, 1 3 5 4 1, 3 5 4, 3 5 4 1 총 4가지의 경우가 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <stdio.h> #include <string.h> int main(void) { int T; scanf("%d\n", &T); for (int test_case = 1; test_case <= T; test_case++) { int n; scanf("%d", &n); int cnt = 0; int check = 0;//0이면 증가 상태, 1이면 감소 int prev, next; int cnt1 = 0, cnt2 = 0; scanf("%d", &prev); scanf("%d", &next); if (prev > next) { check = 1; } else cnt1++; prev = next; for (int i = 2; i < n; i++) { scanf("%d", &next); if (check == 0 && next < prev) { cnt2++;// 증가 상태에서 감소가 되면 카운트 check = 1; } else if (check == 0 && next > prev) //계속 증가상태 { cnt1++; } else if (check == 1 && next < prev) { cnt2++; //계속 감소 상태 } else if (check == 1 && next > prev) { cnt += (cnt1 * cnt2); check = 0; cnt1 = 1; cnt2 = 0; } prev = next; } if(cnt1 !=0 && cnt2!=0) cnt += (cnt1 * cnt2); printf("#%d %d\n", test_case, cnt); } return 0; } | cs |