본문 바로가기
알고리즘 & 자료구조/백준

백준 13144

by 신재권 2022. 9. 12.
package baekjoon.투포인터;

import java.util.*;
import java.io.*;

class Main13144 {

   static int N;
   static int[] A, C;

   public static void main(String[] args) throws Exception {
      input();
      go();

   }

   private static void go() {
      long ans = 0;
      for (int L = 1, R = 0; L <= N; L++) { 
         while (R + 1 <= N && C[A[R + 1]] == 0) {
            R++;
            C[A[R]]++;
         }
         ans += R - L + 1;
         
         C[A[L]]--;
      }
      System.out.println(ans);
   }

   private static void input() throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
      N = Integer.parseInt(br.readLine());
      A = new int[N + 1];
      st = new StringTokenizer(br.readLine());
      for (int i = 1; i <= N; i++) {
         A[i] = Integer.parseInt(st.nextToken());
      }
      C = new int[100_000 + 1];
   }
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 16472  (0) 2022.09.13
백준 1253  (0) 2022.09.12
백준 2470 V2  (0) 2022.09.12
백준 1806  (0) 2022.09.12
백준 2110  (0) 2022.09.11