Advertisement
Huxulm

CF1712C

Aug 13th, 2022 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     . "fmt"
  6.     "io"
  7.     "os"
  8. )
  9.  
  10. func Solve(_in io.Reader, _out io.Writer) {
  11.     in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
  12.     defer out.Flush()
  13.     var t, n int
  14.     for Fscan(in, &t); t > 0; t-- {
  15.         Fscan(in, &n)
  16.         if n == 1 {
  17.             Fprintln(out, 0)
  18.             continue
  19.         }
  20.         a := make([]int, n)
  21.         // 统计 a[i] 最早出现位置
  22.         mp := map[int]int{}
  23.         cnt := make([]int, n)
  24.         for i := range a {
  25.             Fscan(in, &a[i])
  26.             cnt[i] = len(mp)
  27.             if _, ok := mp[a[i]]; !ok {
  28.                 mp[a[i]] = i
  29.             }
  30.         }
  31.         j := n-1
  32.         for ; j >= 0; j-- {
  33.             if j >= 1 && a[j] == a[j-1] {
  34.                 continue
  35.             }
  36.             if i, ok := mp[a[j]]; ok && i < j {
  37.                 break
  38.             }
  39.             if j >= 1 && a[j] < a[j-1] {
  40.                 break
  41.             }
  42.         }
  43.         if j >= 0 {
  44.             Fprintln(out, cnt[j])
  45.         } else {
  46.             Fprintln(out, 0)
  47.         }
  48.     }
  49. }
  50.  
  51. func main() { Solve(os.Stdin, os.Stdout) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement