近些年來,我國防沙治沙取得顯著成果。某沙漠新種植N棵胡楊(編號1-N),排成一排。一個月后,有M棵胡楊未能成活。現可補種胡楊K棵,請問如何補種(只能補種,不能新種),可以得到最多的連續胡楊樹?
輸入描述
N 總種植數量
M 未成活胡楊數量
M 個空格分隔的數,按編號從小到大排列
K 最多可以補種的數量
其中:
1 <= N <= 100000
1 <= M <= N
0 <= K <= M
輸出描述
最多的連續胡楊棵樹
示例1 輸入輸出 示例僅供調試,后臺判題數據一般不包含示例
輸入
5
2
2 4
1
輸出
3
說明
補種到2或4結果一樣,最多的連續胡楊棵樹都是3。
示例2
輸入
10
3
2 4 7
1
輸出
6
說明
補種第7棵樹,最多的連續胡楊棵樹為6(5,6,7,8,9,10)
# 補種未成活胡楊 : 數組中元素的索引隊列 + 滑動窗口# 根據窗口中 0 的個數是否大于 k 個滑動 l 的位置
n =int(input())
m =int(input())
death =sorted(list(map(int,input().split(" "))))
k =int(input())trees =[-1]+[1]* n
for i in death:trees[i]=0l, r, zero_pos, zero_cnt, one_cnt, ans =1,1,[],0,0,0while r < n +1:if trees[r]==0:zero_pos.append(r)zero_cnt +=1else:one_cnt +=1if zero_cnt > k:first_zero = zero_pos.pop(0)one_cnt -=sum(trees[l:first_zero])l = first_zero +1zero_cnt -=1# 窗口中存在的滿足 k 條件的 zero_cnt 個數是可以變為 1 的ans =max(ans, one_cnt + zero_cnt)r +=1print(ans)
# 最少交換次數 : 滑動窗口# 1. 將數組中所有滿足小于 K 條件的數字都看做 1, 不滿足的看做 0# 2. 要將窗口大小維持為數組中的所有 1 的個數 m, 利益最大化# 3. 窗口中 0 的個數即為當前答案(所要交換的次數)# 當窗口長度為整個數組中數字滿足小于 K 的個數 m 時, 滑動 l 的位置# 更新答案為 min 當前 m 長度的窗口 - 窗口中所有小于 K 的數字的個數
nums =list(map(int,input().split()))
k =int(input())arr =[1if i < k else0for i in nums]
m =sum(arr)if m ==0:print(0)exit()n =len(arr)
l, r, ans =0,0,float("inf")while r < n:if r - l +1== m:ans =min(ans, m -sum(arr[l:r+1]))l +=1r +=1print(ans)
# 類似題目# 交換定義為選中一個數組中的兩個互不相同的位置并交換二者的值。# 環形數組是一個數組, 可以認為第一個元素和最后一個元素相鄰。# 給你一個二進制環形數組 nums, 返回在任意位置將數組中的所有 1 聚集在一起需要的最少交換次數。# https://leetcode.cn/problems/minimum-swaps-to-group-all-1s-together-ii/description/classSolution:defminSwaps(self, nums: List[int])->int:n =len(nums)m =sum(nums)if m ==0:return0arr =[*nums]for i in nums:arr.append(i)l, r, one_cnts, ans =0,0,0,float("inf")while l < n:if arr[r]:one_cnts +=1if r - l +1== m:ans =min(ans, m - one_cnts)if arr[l]:one_cnts -=1l +=1r +=1return ans if ans !=float("inf")else0
# 寬度最小的子矩陣 : 負債模型 + 滑動窗口
n, m =list(map(int,input().split(" ")))
nums =[]for i inrange(n):nums.append(list(map(int,input().split(" "))))
k =int(input())
target =list(map(int,input().split(" ")))MAXNUM =1001
cnt =[0]* MAXNUM
for i in target:cnt[i]-=1l, r, debt, width =0,0, k,float("inf")while r < m:for i inrange(n):if cnt[nums[i][r]]<0:debt -=1cnt[nums[i][r]]+=1if debt ==0:count =sum([1if cnt[nums[i][l]]>0else0for i inrange(n)])while count == n:count =sum([1if cnt[nums[i][l]]>0else0for i inrange(n)])if count == n:judge_all_duplicate =Falsefor i inrange(n):cnt[nums[i][l]]-=1for i in cnt:if i <0:judge_all_duplicate =Truebreakif judge_all_duplicate:for i inrange(n):cnt[nums[i][l]]+=1breakelse:l +=1width =min(width, r - l +1)r +=1print(width if width !=float("inf")else-1)
一個 DNA 序列由 A/C/G/T 四個字母的 排列組合 組成。 G 和 C 的比例(定義為 GC-Ratio )是序列中 G 和 C 兩個字母的總的出現次數除以總的字母數目(也就是序列長度)。在基因工程中,這個比例非常重要。因為高的 GC-Ratio 可能是基因的起始點。給定一個很長的 DNA 序列,以及限定的子串長度 N ,請幫助研究人員在給出的 DNA 序列中從左往右找出 GC-Ratio 最高且長度為 N 的第一個子串。
DNA序列為 ACGT 的子串有: ACG , CG , CGT 等等,但是沒有 AGT , CT 等等(這行內容毫無關系?)
數據范圍: 字符串長度 滿足 1≤n≤1000,輸入的字符串只包含 A/C/G/T 字母
輸入描述:
輸入一個string型基因序列,和int型子串的長度
輸出描述:
找出GC比例最高的子串,如果有多個則輸出第一個的子串
示例1:
輸入
ACGT
2
輸出
CG
說明
ACGT長度為2的子串有AC,CG,GT3個,其中AC和GT2個的GC-Ratio都為0.5,CG為1,故輸出CG
示例2:
輸入
AACTGTGCACGACCTGA
5
輸出
GCACG
說明
雖然CGACC的GC-Ratio也是最高,但它是從左往右找到的GC-Ratio最高的第2個子串,所以只能輸出GCACG。
# DNA序列 : 滑動窗口
s =input().strip()
k =int(input().strip())n =len(s)
l, r, max_ratio, start =0,0,float("-inf"),0while r < n:if r - l +1== k:cur = s[l:r+1]ratio =(cur.count("C")+ cur.count("G"))/len(cur)if ratio > max_ratio:max_ratio = ratiostart = ll +=1r +=1print(s[start:start+k]if max_ratio !=float("-inf")else"")