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)
相关推荐
第九序言T2 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
m0_639906942 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
Java后端的Ai之路2 小时前
CAS并发控制-从CPU指令到企业级乐观锁
vue.js·人工智能·python·cas·乐观锁
qq_426003962 小时前
UI自动化高频次登录login限流解决方案
python·ui·自动化
Sakura_j_h_2 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
ZhengEnCi3 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
quantdash_cc3 小时前
如何验证历史行情的交易日期是否完整覆盖研究区间?
python·数据分析·pandas·量化交易·股票数据·quantdash
I Am a robert girl3 小时前
当智能体学会“串通”:从长程交互中读懂涌现式合谋
python·多智能体系统·多智能体协作·llm agent·涌现式合谋·对齐问题·激励机制设计
秋天的怀念_David3 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi4 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展