cf简单思维题

68 阅读2分钟

1.A. Maxmina

You have an array aa of size nn consisting only of zeroes and ones and an integer kk. In one operation you can do one of the following:

  • Select 22 consecutive elements of aa and replace them with their minimum (that is, let a:=[a1,a2,…,ai−1,min(ai,ai+1),ai+2,…,an]a:=[a1,a2,…,ai−1,min(ai,ai+1),ai+2,…,an] for some 1≤i≤n−11≤i≤n−1). This operation decreases the size of aa by 11.
  • Select kk consecutive elements of aa and replace them with their maximum (that is, let a:=[a1,a2,…,ai−1,max(ai,ai+1,…,ai+k−1),ai+k,…,an]a:=[a1,a2,…,ai−1,max(ai,ai+1,…,ai+k−1),ai+k,…,an] for some 1≤i≤n−k+11≤i≤n−k+1). This operation decreases the size of aa by k−1k−1.

Determine if it's possible to turn aa into [1][1] after several (possibly zero) operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤k≤n≤502≤k≤n≤50), the size of array aa and the length of segments that you can perform second type operation on.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (aiai is 00 or 11), elements of array aa.

Output

For each test case, if it is possible to turn aa into [1][1], print "YES", otherwise print "NO".

题意:大致题意就是给你一个长度为n只有0,1组成的序列,你可以进行以下两种操作:选中k个连续元素,只保留一个最大的或者最小的,其余的全都删去。最后输出是否能得到一个全部为1的序列,是则输出yes,反之输出no。

解题思路:因为是A题,所以逻辑也显得特别简单,判断当前给出的序列中是否有1,即可得到答案。Python代码如下:

1.  n,m=map(int,input().split())
1.  p=list(map(int,input().split()))
1.  if 1 in p:
1.  print("YES")
1.  else:
1.  print("NO")

B. Rebellion

同样是一套题,这是第二题,题目就不在贴了。 题意:给你一个长度为n,只有0,1组成的序列,可以一个操作包括以下步骤:任意选择序列中的两个数ai,aj,将aj+ai,然后删去ai,最后询问最少几次操作能把序列a变成一个非递减的序列。

解题思路:其实这题交换和添加删除的效果是一样的,只是我一开始没想到应该怎么处理, 但是后来经过同学提醒,才想清楚,因为只有0和1,可以先统计01的个数,为m,n,最后排好序的结果一定是前m个是0,所以只需要计算原始的前m个有多少为1即为答案。python代码如下:

1.  n=int(input())
1.  a=list(map(int,input().split()))
1.  x=0
1.  for k in a:
1.  if k==0:
1.  x+=1
1.  pp=sum(a[:x])
1.  print(pp)