Codeforces Round 918 (Div. 4) (前缀和,权值树状数组,二维偏序, python + golang)

发布时间 2023-12-30 22:12:02作者: 深渊之巅

Dashboard - Codeforces Round 918 (Div. 4) - Codeforces

 

 

from collections import *


def solve():
    a, b, c = list(map(int, input().split()))
    hs = defaultdict(int)
    hs[a] += 1
    hs[b] += 1
    hs[c] += 1

    for i in hs:
        if hs[i] == 1:
            print(i)
            break


T = int(input())

for i in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
)

var in = bufio.NewReader(os.Stdin)

func main() {
    var T int
    fmt.Fscan(in, &T)

    for tt := 0; tt < T; tt++ {
        var a, b, c int
        fmt.Fscan(in, &a, &b, &c)
        mp := make(map[int]int)
        mp[a] += 1
        mp[b] += 1
        mp[c] += 1
        for k, v := range mp {
            if v == 1 {
                fmt.Println(k)
                break
            }
        }
    }
}

 

 

 

from collections import *


def solve():
    g = []
    for i in range(3):
        g.append(input())

    x = 0
    for i in range(3):
        for j in range(3):
            if g[i][j] == '?':
                x = i

    st = set(g[x])
    for i in range(ord('A'), ord('C') + 1):
        if chr(i) not in st:
            print(chr(i))
            break


T = int(input())

for i in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

var in = bufio.NewReader(os.Stdin)

func readLine() []string {
    inp, _ := in.ReadString('\n')
    return strings.Split(strings.Trim(inp, " \r\n"), " ")
}

func stoi(s string) int64 { v, _ := strconv.ParseInt(s, 10, 64); return v }

func main() {
    T := stoi(readLine()[0])

    for tt := 0; tt < int(T); tt++ {
        cnt := make([]int, 3)
        for i := 0; i < 3; i++ {
            str := readLine()[0]
            for _, v := range str {
                if v != '?' {
                    cnt[v-'A']++
                }
            }
        }

        for i, v := range cnt {
            if v == 2 {
                fmt.Println(string(rune('A' + i)))
                break
            }
        }
    }
}

 

 

 这道题就是看所给数组的和能不能分解为平方和

import math
from collections import *


def solve():
    n = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    ss = int(math.sqrt(s))
    if ss * ss == s:
        print("YES")
    else:
        print("NO")


T = int(input())

for i in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
    "strconv"
    "strings"
)

var in = bufio.NewReader(os.Stdin)

func readLine() []string {
    ipt, _ := in.ReadString('\n')
    return strings.Split(strings.Trim(ipt, "\r\n"), " ")
}

func stoi(s string) int64 { v, _ := strconv.ParseInt(s, 10, 64); return v }

func main() {
    T := int(stoi(readLine()[0]))

    for tt := 0; tt < T; tt++ {
        readLine()
        var s int64 = 0
        for _, v := range readLine() {
            s += stoi(v)
        }
        sq := sort.Search(1e9, func(i int) bool {
            return int64(i)*int64(i) > s
        }) - 1

        if int64(sq)*int64(sq) == s {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
    
}

 

 一个小贪心,当我们遇到辅音字母时,它可以作为开头,就看当前位置往后三个位置是不是辅音字母,如果是,则s[i:i + 3]加到答案中(cvc),否则s[i:i+2] (cv)。

import math
from collections import *

ll = ['b', 'c', 'd']


def is_c(ss: str) -> bool:
    return ss in ll


def solve():
    n = int(input())
    s = input()
    res = []
    i = 0
    while i < n - 3:
        if is_c(s[i + 3]):
            res.append(s[i: i + 3])
            res.append('.')
            i += 3
        else:
            res.append(s[i: i + 2])
            res.append('.')
            i += 2
    res.append(s[i:])
    print(''.join(res))


T = int(input())

for i in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

var in = bufio.NewReader(os.Stdin)

func readLine() []string {
    ipt, _ := in.ReadString('\n')
    return strings.Split(strings.Trim(ipt, "\r\n"), " ")
}

func stoi(s string) int64 { v, _ := strconv.ParseInt(s, 10, 64); return v }

func Isc(c rune) bool {
    return c == 'b' || c == 'c' || c == 'd'
}

func main() {
    T := int(stoi(readLine()[0])) // 读一个整数

    for tt := 0; tt < T; tt++ {
        n := int(stoi(readLine()[0]))
        s := readLine()[0]
        ans := []rune{}
        ss := []rune(s)
        i := 0
        for i < n-3 {
            if Isc(rune(s[i+3])) {
                ans = append(ans, ss[i:i+3]...)
                ans = append(ans, '.')
                i += 3
            } else {
                ans = append(ans, ss[i:i+2]...)
                ans = append(ans, '.')
                i += 2
            }
        }

        ans = append(ans, ss[i:]...)
        fmt.Println(string(ans))
    }

}

 

 本题就是看有没有一个连续子数组中,奇数和等于偶数和,经典前缀和+哈希

from collections import *
from itertools import *


def solve():
    n = int(input())
    a = list(map(int, input().split()))
    c = set()
    c.add(0)
    for i in range(len(a)):
        if i % 2 == 0:
            a[i] = -a[i]

    s = 0
    for i in range(n):
        s += a[i]
        if s in c:
            print("YES")
            return
        c.add(s)

    print("NO")


T = int(input())

for i in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

var in = bufio.NewReader(os.Stdin)

func readLine() []string {
    ipt, _ := in.ReadString('\n')
    return strings.Split(strings.Trim(ipt, "\r\n"), " ")
}

func stoi(s string) int64 { v, _ := strconv.ParseInt(s, 10, 64); return v }

func Isc(c rune) bool {
    return c == 'b' || c == 'c' || c == 'd'
}

func solve() {
    var n int
    fmt.Fscan(in, &n)
    a := make([]int64, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
        if i%2 == 0 {
            a[i] = -a[i]
        }
    }

    var s int64 = 0
    mp := make(map[int64]bool)
    mp[0] = true
    for _, x := range a {
        s += x
        if mp[s] {
            fmt.Println("YES")
            return
        }
        mp[s] = true
    }

    fmt.Println("NO")
}

func main() {
    var T int
    fmt.Fscan(in, &T)

    for tt := 0; tt < T; tt++ {
        solve()
    }

}

 

 可将问题抽象为一个二维偏序问题,可以使用排序+权值树状数组解决。

由于数据范围太大,要使用离散化减小数据范围

from itertools import *
from collections import *
import bisect


def lowbit(x: int) -> int:
    return x & -x


def update(nums, x, n):
    while x <= n:
        nums[x] += 1
        x += lowbit(x)


def query(nums, x) -> int:
    res = 0
    while x > 0:
        res += nums[x]
        x -= lowbit(x)
    return res


def solve():
    n = int(input())
    se = []
    b = []
    for _ in range(n):
        x, y = map(int, input().split())
        se.append((x, y))
        b.append(y)

    se.sort(key=lambda x: (x[0], x[1]))
    b.sort()
    num = [0] * (n + 1)
    ans = 0
    for i in range(n - 1, -1, -1):
        idx = bisect.bisect_left(b, se[i][1]) + 1
        ans += query(num, idx)
        update(num, idx, n)

    print(ans)


T = int(input())
for _ in range(T):
    solve()
package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
    "strconv"
    "strings"
)

var in = bufio.NewReader(os.Stdin)

func readLine() []string {
    ipt, _ := in.ReadString('\n')
    return strings.Split(strings.Trim(ipt, "\r\n"), " ")
}

func stoi(s string) int64 { v, _ := strconv.ParseInt(s, 10, 64); return v }

func lowbit(x int64) int64 {
    return x & -x
}

func add(nums []int64, x int64, n int64) {
    for x <= n {
        nums[x] += 1
        x += lowbit(x)
    }
}

func query(nums []int64, x int64) int64 {
    var res int64 = 0
    for x > 0 {
        res += nums[x]
        x -= lowbit(x)
    }
    return res
}

func solve() {
    var n int
    fmt.Fscan(in, &n)
    a := make([][2]int64, n)
    alls := []int64{}
    for i := 0; i < n; i++ {
        fmt.Fscan(in, &a[i][0], &a[i][1])
        alls = append(alls, a[i][1])
    }

    sort.Slice(alls, func(i, j int) bool {
        return alls[i] < alls[j]
    })

    sort.Slice(a, func(i, j int) bool {
        if a[i][0] == a[j][0] {
            return a[i][1] < a[j][1]
        }
        return a[i][0] < a[j][0]
    })

    num := make([]int64, n+10)
    var ans int64

    for i := n - 1; i >= 0; i-- {
        idx := int64(sort.Search(len(alls), func(x int) bool { return alls[x] >= a[i][1] })) + 1
        ans += query(num, idx)
        add(num, idx, int64(n))
    }

    fmt.Println(ans)
}

func main() {
    var T int
    fmt.Fscan(in, &T)

    for tt := 0; tt < T; tt++ {
        solve()
    }

}