Codeforces Round 927 (Div. 3)

A. Thorns and Coins

考点:模拟

思路:

当遇到两个**的时候就结束循环,其余时候当字符是"@"的时候就+1。

复杂度:O(1)

python 复制代码
import heapq
import sys
input = sys.stdin.readline
t=int(input())
for _ in range(t):
    n=int(input())
    a=input().strip()
    cnt=0
    i=0
    for i in range(n):
        if i+1<n and a[i]=='*' and a[i+1]=='*':
            break
        if a[i]=='@':
            cnt+=1
    print(cnt)

B. Chaya Calendar

考点:模拟

思路:

找当前数,第一个大于他的倍数是多少即可。

复杂度:O(n)

python 复制代码
import heapq
import sys
input = sys.stdin.readline
t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    cur=0
    for x in a:
        cur=(cur//x+1)*x
    print(cur)

C. LR-remainders

考点:后缀积

思路:

这题肯定是不能真的按题意模拟的,真的去删会超时。

我们将题目转换一下,先用双指针把要删除的顺序用b存一下,然后因为:

第1总的时候是:

第2总的时候是:

第i+1总的时候是:

可以发现这是一个典型的后缀和加双指针问题。

复杂度:O(n)

python 复制代码
import sys
input=sys.stdin.readline
from collections import deque
t=int(input())
for _ in range(t):
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    s=input().strip()
    b=[]
    z=1
    r=n-1
    l=0
    for v in s:
        if v=='R':
            b.append(a[r])
            r-=1
        if v=='L':
            b.append(a[l])
            l+=1
    z=1
    c=[]
    for i in range(n-1,-1,-1):
        z=(z*b[i])%m
        c.append(z)
    print(*(c[::-1]))

D. Card Game

考点:模拟,贪心

思路:

一个非常屎的模拟题。思路就是:

  • 先将b按花色存牌。
  • 非主牌花色内部先两两配对。
  • 如果某个非主牌花色是奇数张,就拿一张出来,给一张主牌去压它。
  • 最后剩下的主牌再内部两两配对。
  • 如果主牌数量不够这些落单牌,直接输出IMPOSSIBLE。

复杂度:O(n)

python 复制代码
import sys
input=sys.stdin.readline
from collections import deque
t=int(input())
c=['C','D','H','S']
r="23456789"
for _ in range(t):
    n=int(input())
    wp=input().strip()
    a=list(input().split())
    b={ch:[] for ch in c}
    #按花色分组
    for v in a:
        b[v[1]].append(v[0])
    #每个花色按点数从小到大排序
    for ch in c:
        b[ch].sort(key=lambda x:r.index(x))
    ans=[]
    left=[]#非主牌里落单,需要主牌去压的牌
    #先处理非主牌花色
    for ch in c:
        if ch==wp:
            continue
        m=len(b[ch])
        #两两配对,同花色后一个压前一个
        for i in range(0,m-1,2):
            ans.append((b[ch][i]+ch,b[ch][i+1]+ch))
        #如果是奇数张,最后一张留给主牌去压
        if m%2==1:
            left.append(b[ch][-1]+ch)
    #主牌数量不够接这些落单牌
    if len(b[wp])<len(left):
        print("IMPOSSIBLE")
        continue
    #用主牌去压非主牌落单
    p=0
    for card in left:
        ans.append((card,b[wp][p]+wp))
        p+=1
    #剩下的主牌内部两两配对
    rem=b[wp][p:]
    for i in range(0,len(rem),2):
        ans.append((rem[i]+wp,rem[i+1]+wp))
    for x,y in ans:
        print(x,y)
相关推荐
无限进步_2 小时前
【C++】字符串中的字母反转算法详解
开发语言·c++·ide·git·算法·github·visual studio
2401_857865232 小时前
用Python破解简单的替换密码
jvm·数据库·python
2401_891482172 小时前
C++中的状态模式实战
开发语言·c++·算法
Frostnova丶2 小时前
LeetCode 1727.重新排列后的最大子矩阵
算法·leetcode
自信150413057592 小时前
数据结构之二叉树算法题
c语言·数据结构·算法
Looooking2 小时前
Python 之自动下载更新 selenium 驱动 chromedriver
python·selenium·chromedriver
qianbo_insist2 小时前
鱼眼图像的三维投影逆变换和AI计算
人工智能·opencv·算法
我怎么又饿了呀2 小时前
DataWhale—大模型的算法基础(文本表示与词向量)
算法
yzx9910132 小时前
使用Python构建交易回撤分析器:Trad与Claw模块实战
开发语言·python