赞
踩
好难的场, 从第二题开始,就感觉画风不对.
签到
特判<=2的情况
z = int(input())
if z <= 2:
print("NO")
else:
print ("YES")
print (1, z - 1)
思路: 贪心
把握了这两点,基本就能AC
def check(a, b, n):
z = n // 3
if b % 2 == 0:
return a * 2 + min(z * 2, b) * 3 >= 2 * n
else:
return a * 2 + min(z * 2, (b - 1)) * 3 >= 2 * n
t = int(input())
for _ in range(t):
a, b, n = list(map(int, input().split()))
print ("YES" if check(a, b, n) else "NO")
思路: 贪心
从贪心的角度,交换法则,
非负数先对消,在用负数对消,结果只会更好,至少不变差 非负数先对消,在用负数对消,结果只会更好,至少不变差 非负数先对消,在用负数对消,结果只会更好,至少不变差
因此
n = int(input()) arr = list(map(int, input().split())) from collections import Counter cnt = Counter() neg = 0 for v in arr: if v < 0: neg += 1 else: cnt[v] += 1 non = 0 for (k, v) in cnt.items(): non += v % 2 s = min(neg, non) neg -= s non -= s neg %= 2 print (non + neg)
思路: 多路归并
先考虑两个字符串S和T的合并
这个合并方式,其实挺难写的。
如果按照多路归并的方式
引入优先队列,则其时间复杂度为
O ( ∣ C ∣ ∗ l o g n ∗ ∑ i = 0 i = n − 1 ∣ C i ∣ ) O(|C| * logn * \sum_{i=0}^{i=n-1}|C_i|) O(∣C∣∗logn∗i=0∑i=n−1∣Ci∣)
∑ i = 0 i = n − 1 ∣ C i ∣ ≤ 2 ∗ 1 0 5 \sum_{i=0}^{i=n-1}|C_i| \le 2*10^5 i=0∑i=n−1∣Ci∣≤2∗105
∣ C ∣ |C| ∣C∣越大,时间复杂度越高
因为 a i ≤ 1 0 9 ai \le 10^9 ai≤109,因此 ∣ C ∣ ≤ 10 |C| \le 10 ∣C∣≤10, 此时多路归并 O ( 3.2 ∗ 1 0 7 ) O(3.2*10^7) O(3.2∗107)
注:python的heapq没法自定义比较函数,所以字符串按字母序逆序排,挺麻烦的
n = int(input()) arr = input().split() import heapq hp = [] def change(c): return chr(ord('9') - ord(c) + ord('0')) for s in arr: # 转换后,尾巴还要加个?,可以想下为什么? heapq.heappush(hp, ''.join([change(c) for c in s]) + '?') r = [] while len(hp) > 0: s = heapq.heappop(hp) r.append(change(s[0])) if len(s) > 2: heapq.heappush(hp, s[1:]) print (''.join(r))
思路: 连通图 + 贪心
贪心合并,感觉结论为
∑ w i − min w i \sum w_i - \min w_i ∑wi−minwi
w i 为连通块中的最大值 w_i为连通块中的最大值 wi为连通块中的最大值
n, m = list(map(int, input().split())) arr = list(map(int, input().split())) g = [[] for _ in range(n)] for _ in range(m): u, v = list(map(int, input().split())) u, v = u - 1, v - 1 g[u].append(v) g[v].append(u) vis = [False] * n from collections import deque def bfs(s): r = arr[s] deq = deque() deq.append(s) vis[s] = True while len(deq) > 0: u = deq.popleft() for v in g[u]: if not vis[v]: vis[v] = True r = max(r, arr[v]) deq.append(v) return r w = [] for i in range(n): if not vis[i]: c = bfs(i) w.append(c) print (sum(w) - min(w))
猜结论: 满足组合解,必然存在一个循环转移使得括号匹配
那最后就是单纯的组合解
后期再补证明吧
mod = 10 ** 9 + 7 n = int(input()) s = input() mx = 10 ** 5 fac = [0] * (mx + 1) inv = [0] * (mx + 1) fac[0] = 1 for i in range(1, mx + 1): fac[i] = fac[i - 1] * i % mod inv[mx] = pow(fac[mx], mod - 2, mod) for i in range(mx - 1, -1, -1): inv[i] = inv[i + 1] * (i + 1) % mod l1, l2 = 0, 0 for c in s: if c == '(': l1 += 1 elif c == ')': l2 += 1 if n % 2 == 1 or l1 > n // 2 or l2 > n // 2: print (0) else: x = n - l1 - l2 x1 = n // 2 - l1 r = fac[x] * inv[x1] % mod * inv[x - x1] % mod print (r)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。