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)
相关推荐
To_OC4 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
Scott9999HH4 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
2401_841495645 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
AI云海5 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰6 小时前
[爬虫]-Urllib
爬虫·python
玉鸯8 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260858 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao8 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest