Codeforce-- 思维题
2-11 Eat the chip
思路:题意可知,a从下往上走,b从上往下走,我们可以思考一个问题,在某一个回合,如果a和b在同一列中(a是Alice,b是Bob),一方怎么走,另外一方就跟着这一方走,所以同一列的时候是该回合后手的必胜态,先手的必败态。于是可以递推:如果在走到边界的时候有机会仍然保持b在a的上方,那么有机会分出胜负,因为一方不可以走出边界,所以ya-yb一定会发生变化,直到xa-xb = 0
import sys
#file I/O
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
#faster I/O
input = sys.stdin.readline
sys.setrecursionlimit(30)
def print(ans='',end = '\n'):
sys.stdout.write(str(ans) + end)
def solve() -> None:
n,m,xa,ya,xb,yb = map(int,input().split())
flag = bool((xb - xa) % 2)
if not flag:
winner = "Bob"
if xa >= xb:
win = False
elif ya == yb:
win = True
else:
if yb > ya:
n_turn = yb - 1
else:
n_turn = m - yb
win = xb - 2 * n_turn >= xa
else:
winner = "Alice"
xa += 1
ya += 0 if yb - ya == 0 else 1 if yb - ya > 0 else -1
if xa > xb:
win = False
elif ya == yb:
win = True
else:
if ya < yb :
n_turn = m - ya
else:
n_turn = ya - 1
win = xb - 2 * n_turn >= xa
print(winner if win else "Draw")
T = int(input())
for _ in range(T):
solve()
CF1920C-Partitioning the Array
import sys
import math
#file I/O
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
#faster I/O
input = sys.stdin.readline
sys.setrecursionlimit(30)
def write(ans='',ends = '\n'):
sys.stdout.write(str(ans) + ends)
def solve() -> None:
n = int(input())
a = [int(x) for x in input().split()]
a.insert(0,0)
ans = 0
for k in range(1,n+1):
if n % k == 0:
res = 0
for i in range(1,n-k+1):
res = math.gcd(res,abs(a[i]-a[i+k]))
ans += res != 1
write(ans)
T = int(input())
for _ in range(T):
solve()
CF1917C-Watering an Array
import sys
import math
from collections import Counter
#file I/O
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
#faster I/O
input = sys.stdin.readline
sys.setrecursionlimit(30)
def write(ans='',ends = '\n'):
sys.stdout.write(str(ans) + ends)
def solve() -> None:
n,k,d = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(min(d,2*n+1)):
cur = (d-i-1) // 2
for j in range(n):
cur += a[j] == j+1
ans = max(ans,cur)
for j in range(b[i%k]):
a[j] += 1
write(ans)
T = int(input())
for _ in range(T):
solve()
B. AND Sequences

因此可得重排后的序列只需要满足:$$a_1 = a_n$$就可以了,中间数字的选择就是(n-2)!种;假设整个序列的与运算后的答案是k,那么整个序列中的数字等于k的有cnt种,因此前后两种的选择是cnt * (cnt-1)
import sys
input = lambda : sys.stdin.readline().strip()
mod = 10**9 + 7
def solve():
n = int(input())
a = [int(x) for x in input().split()]
k,cnt = a[0],0
for i in range(1,n):
k &= a[i]
for i in a:
if i == k:
cnt += 1
cnt = cnt * (cnt-1) % mod
for i in range(1,n-1):
cnt = cnt * i % mod
print(cnt)
for _ in range(int(input())):
solve()
E. Making Anti-Palindromes
首先,我们可以确定反回文串一定满足:
1、反回文串的长度一定为偶数(如果为奇数中间位置自己等于自己不符合要求)
2、某个字符出现的次数<=n/2
我们记same[k]为某位置与对称位置相同且为字符k的数量,很简单可以想到我们可以自己和其他不合法字符c(c!=k and same[c] > 0)的交换,我们会交换sum(same) // 2上取整次(每交换一对数量减2,如果sum(same)是奇数我们最后一对必定可以与其他合法的对交换)
我们记cnt为same的总和(sum(same)),若某一个same[k]<=n/2,可以自己和其他不合法字符c(c!=k and same[c] > 0)内部消耗,如果>n/2次,我们的想法是先用一部分用自己将其余不合法字符抵消干净,后面再和合法对交换,总共same[k]次,因此答案为max(cnt // 2,max(same))
import sys
import math
from collections import Counter
import heapq
# file I/O
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
# faster I/O
input = lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(30)
def solve() -> None:
n = int(input())
s = input()
cnt = Counter(s)
if n%2 or max(cnt.items(),key = lambda x : x[1])[1] > n//2 :
print(-1)
return
same = [0] * 27
for i in range(len(s)//2):
if s[i] == s[n-i-1]:
same[ord(s[i]) - ord('a')] += 1
m,tot = max(same),sum(same)
print(max(m,(tot+1)//2))
if __name__ == '__main__':
#T = 1
T = int(input())
for i in range(T):
solve()

浙公网安备 33010602011771号