Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lowbit(x) (x & -x)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0), eps = 1e-6;
const int N = 2e5 + 10;
int pre[N], suf[N];
bool vis[N];
bool judge(int a, int b, int k) {
if (!a || !b) return false;
// if (a % k == 0 && b % k == 0) return true;
// if (a % k == k / 2 && b % k == k / 2) return true;
if (a >= k / 2 && b >= k / 2) return true;
return false;
}
int main(int argc, char const *argv[]) {
int n, k, m, t, x;
cin >> t;
while (t--) {
scanf("%d %d %d", &n, &k, &m);
k = k - 1;
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
vis[x] = true;
}
bool flag = false;
if ((n - m) % k == 0) {
suf[n + 1] = pre[0] = 0;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + !vis[i];
for (int i = n; i; i--) suf[i] = suf[i + 1] + !vis[i];
for (int i = 1; i <= n; i++) {
if (!vis[i]) continue;
if (judge(pre[i], suf[i], k)) {
flag = true;
break;
}
}
}
puts(flag ? "YES" : "NO");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<ll,ll>
#define ct(x) cout<<x<<' ';
#define nl cout<<'\n';
#define lb lower_bound
#define all(v) v.begin(), v.end()
#define pb push_back
ll MOD = 1e9+7;
int main(){
int tc;
cin>>tc;
while(tc--){
int n,k,m;
cin>>n>>k>>m;
int b = 1, tot = n-m;
vector<int> v;
for(int i=0;i<m;++i) {
int x;
cin>>x;
if(b<x) {
v.pb(x-b);
}
b=x+1;
}
if(b<=n){
v.pb(n-b+1);
}
if(tot % (k-1) > 0) {cout<<"NO\n";continue;}
int side = (k-1)/2;
int s1=0,s2=tot;
bool ok = 0;
for(int i=0;i<v.size();++i){
if(s1 >= side && s2 >= side){
ok = true;
break;
}
s1+=v[i];
s2-=v[i];
}
if(ok) cout<<"YES\n";
else cout<<"NO\n";
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import sys
input=sys.stdin.readline
t=int(input())
for you in range(t):
l=input().split()
n=int(l[0])
k=int(l[1])
m=int(l[2])
l=input().split()
li=[int(i) for i in l]
if((n-m)%(k-1)):
print("NO")
continue
poss=0
for i in range(m):
z=n-li[i]
z-=(m-i-1)
y=li[i]-1
y-=i
if(z>=(k-1)//2 and y>=(k-1)//2):
poss=1
break
if(poss):
print("YES")
else:
print("NO")
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
for(int q=ni();q>0;q--){
work();
}
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
void work() {
int n=ni(),k=ni(),m=ni();
int c=k/2;
int sum=n-m;
int[] A=nia(m);
if((n-m)%(k-1)!=0){
out.println("NO");
return;
}
for(int i=0,pre=1,cur=0;i<m;i++){
cur+=A[i]-pre;
int r=sum-cur;
int v=cur%(k-1);
if(v<c){
v+=c;
if(cur>=v&&r>=k-v-1){
out.println("YES");
return;
}
}else{
v-=c;
if(r>=k-v||(v==0&&r>0&&cur>0)){
out.println("YES");
return;
}
}
pre=A[i]+1;
}
out.println("NO");
}
//input
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
} | JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
using namespace std;
typedef __int128 LL;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int mod = 1e9+7;
const int inf = 1<<30;
int main() {
#ifdef ernestico
freopen ("a.in", "r", stdin );
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while ( t-- ) {
int n, k, m; cin >> n >> k >> m;
vector<int> b(m);
for ( auto &i : b) cin >> i;
if ( (n-m)%(k-1) != 0 ) {
cout << "NO" << endl;
continue;
}
int x = b[0] - 1;
int y = (n - m - x);
bool ok = false;
if ( x >= (k-1)/2 && y >= (k-1)/2 )
ok = true;
for ( int i = 1; i < m; i ++ ) {
x += ( b[i] - b[i-1] - 1 );
y -= ( b[i] - b[i-1] - 1 );
if ( x >= (k-1)/2 && y >= (k-1)/2 )
ok = true;
}
if ( ok ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll , ll> pii;
typedef long double ld;
typedef map<pii,set<pii> >::iterator mapit;
typedef multiset<ll>::iterator setit;
const int maxn = 1e6 + 43;
const int maxm = 3003;
const ll maxii = 1e11 ;
const int maxlog = 30;
const ll mod = 1e9 + 7;
const int sq = 340;
const int inf = 1e9 + 43 ;
const ld PI =3.141592653589793238463;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t ;
cin >> t;
while(t -- ){
int n , m , k ;
cin >> n >> k >> m;
int r = k / 2;
int khali_kol = n - m;
int pre_khali = 0;
int pre = 0;
bool ans = false;
for(int i = 0 ; i < m ; i ++ ){
int x ;
cin >> x;
pre_khali += x - pre - 1;
int post_khali = khali_kol - pre_khali ;
// cout << x << " " << pre_khali << " " << post_khali << endl;
pre = x ;
if(pre_khali >= r && post_khali >= r){
ans = true ;
}
}
if(khali_kol % (k - 1) != 0){
cout << "NO" << endl;
}
else if(ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 50;
int n , k , m;
int b[maxn];
vector<int>d;
int l[maxn];
int r[maxn];
int cnt;
int flag;
set<int>s;
void init()
{
cnt = 0;
memset(l , 0 , sizeof(l));
memset(r , 0 , sizeof(r));
flag = 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t --)
{
init();
scanf("%d %d %d",&n,&k,&m);
int ci = k - 1;
for (int i = 1 ; i <= m ; i ++)
{
scanf("%d",&b[i]);
s.insert(b[i]);
if (i == 1)
{
l[i] = b[i] - 1;
}
else
{
l[i] = l[i - 1] + (b[i] - b[i - 1] - 1);
}
}
for (int i = m ; i >= 1 ; i --)
{
if (i == m)
{
r[i] = n - b[i];
}
else
{
r[i] = r[i + 1] + (b[i + 1] - b[i] - 1);
}
}
if ((n - m) % ci != 0)
{
printf("NO\n");
continue;
}
else
{
int qwq = (k - 1) / 2;
for (int i = 1 ; i <= n ; i ++)
{
if (r[i] >= qwq && l[i] >= qwq)
{
flag = 1;
break;
}
}
if (flag)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <iostream>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;
int a[N], st[N];
int main()
{
int t;
cin >> t;
int n, m, k;
while(t --)
{
int success = 0;
cin >> n >> k >> m;
// cout << n << k << m <<endl;
for (int i = 1; i <= n; i ++) st[i] = 0;
if ((n - m) % (k - 1) != 0)
{
puts("NO");
for (int i = 1; i <= m; i ++)
{
int c;
cin >> c;
}
continue;
}
else
{
for (int i = 1; i <= m; i ++)
{
cin >> a[i];
st[a[i]] = 1;
}
int len = k / 2;
if(m == n)
{
success = 1;
}
int success = 0, cnt = 0, num = n - m;
for (int i = 1; i <= n; i ++)
{
if (st[i])
{
if (cnt >= len && num - cnt >= len)
success = 1;
// cout << cnt << endl;
}
else cnt ++;
}
if (success)
puts("YES");
else
puts("NO");
}
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace :: std;
#define ll long long
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
const int maxn=2e5+500;
const int inf=1e9+900;
const int mod=1e9+7;
bool a[maxn];
int pr[maxn];
int saf[maxn];
int main(){/*
ios_base::sync_with_stdio(false);
cin.tie(0);*/
int t;
cin>>t;
while(t--){
int n,k,m;
cin>>n>>k>>m;
fill(a,a+n,1);
for(int i=0;i<m;i++){
int b;
cin>>b;
b--;
a[b]=0;
}
pr[0]=a[0];
for(int i=1;i<n;i++){
pr[i]=pr[i-1]+(a[i]);
}
for(int i=0;i<n;i++){
saf[i]=(n-m)-pr[i];
}
bool coutt=0;
if((n-m)%(k-1)!=0){
cout<<"NO\n";
continue;
}
for(int i=0;i<n;i++){
if(a[i]==0){
if(pr[i]>=k/2 && saf[i]>=k/2){
cout<<"Yes\n";
coutt=1;
break;
}
}
}
if(coutt)continue;
cout<<"No\n";
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | /*
* Author: Lanly
* Time: 2020-12-26 09:28:00
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T>
void read(T &x) {
int s = 0, c = getchar();
x = 0;
while (isspace(c)) c = getchar();
if (c == 45) s = 1, c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (s) x = -x;
}
template <typename T>
void write(T x, char c = ' ') {
int b[40], l = 0;
if (x < 0) putchar(45), x = -x;
while (x > 0) b[l++] = x % 10, x /= 10;
if (!l) putchar(48);
while (l) putchar(b[--l] | 48);
putchar(c);
}
bool check(vector<bool> &b, int n, int m, int k){
int rm = 0;
for(int i = 0; i < n; ++ i){
if (!b[i]) ++ rm;
else if (rm >= k / 2 && (n - m - rm) >= k / 2) return true;
}
return false;
}
int main(void) {
int kase; read(kase);
for (int ii = 1; ii <= kase; ii++) {
int n, m, k;
read(n);
read(k);
read(m);
vector<bool> b(n, false);
for(int a, i = 0; i < m; ++i){
read(a);
-- a;
b[a] = true;
}
if ((n - m) % (k - 1)){
puts("NO");
continue;
}
if (check(b, n, m, k)) puts("YES");
else puts("NO");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define R return
#define B break
#define C continue
#define sf scanf
#define pf printf
#define pb push_back
#define F first
#define S second
using namespace std;
const ll N=500500,K=22,Inf=4e18,Mx=3e5;
const ld eps=1e-10;
ll a[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--){
ll n,k,m;
bool ok=0;
cin>>n>>k>>m;
for(int i=1;i<=m;i++){
cin>>a[i];
}
if((n-m)%(k-1)){
cout<<"NO";
}
else{
ll b=k/2,c=0,d=(n-m);
for(int i=1,j=1;i<=n&&j<=m;i++){
if(a[j]==i){
if(c>=b&&c<=d-b){
ok=1;
}
j++;
}
else{
c++;
}
}
cout<<(ok?"YES":"NO");
}
if(t)cout<<'\n';
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
// Prioridade
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define all(x) x.begin(),x.end()
#define MOD 1000000007ll
#define endl '\n'
#define mdc(a, b) (__gcd((a), (b)))
#define mmc(a, b) (((a)/__gcd(a, b)) * (b))
#define W(x) cerr << "\033[31m"<< #x << " = " << x << "\033[0m" << endl;
// fim da Prioridade
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t;
cin >> t;
while(t--){
int n, k, m;
cin >> n >> k >> m;
int sz = n-m;
vi b(m);
for (int i = 0; i < m; i++){
cin >> b[i];
}
if(sz % (k - 1)){
cout << "NO" << endl;
continue;
}
int ans = false;
vi a(n + 1);
for (int i = 1; i <= n; i++){
a[i] = 1;
}
for (int i = 0; i < m; i++){
a[b[i]] = 0;
}
vi prefix(n+1);
vi suffix(n+2);
for (int i = 1; i <= n; i++){
prefix[i] = prefix[i-1] + a[i];
}
for (int i = n; i >= 1; i--){
suffix[i] = suffix[i+1] + a[i];
}
for (int i = 0; i < m; i++){
if(prefix[b[i]] >= k/2 and suffix[b[i]] >= k/2) ans = true;
}
if(ans){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, k, m, arr[N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &k, &m);
for(int i = 0 ; i < m ; i++) scanf("%d", arr + i);
bool valid = false;
for(int i = 1, c = 0 ; i <= n ; i++)
{
if(c < m and i == arr[c])
{
int lf = (i - 1 - c), rt = (n - i + 1 - (m - c));
if(lf >= (k / 2) and rt >= (k / 2))
{
valid = true;
break;
}
c++;
}
}
valid &= (n - m) % (k - 1) == 0;
puts(valid ? "YES" : "NO");
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | for _ in range(int(input())):
n,k,m=tuple(map(int,input().split(" ")));ml=set(map(int,input().split(" ")));havitada=[i for i in range(1,n+1) if i not in ml];saab=False
if len(havitada)%(k-1)!=0:print("no");continue
for i in range(k//2-1,len(havitada)-k//2):
if havitada[i+1]-havitada[i]!=1:print("yes");break
else:print("no") | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n, k, m, a, x, y, cnt1, cnt2;
cin >> n >> k >> m;
vector<int> v;
v.push_back(0);
for (int i = 1; i <= m; i++) {
cin >> a;
v.push_back(a);
}
v.push_back(n + 1);
string sol = "YES";
if ((n - m) % (k - 1) > 0) {
sol = "NO";
cout << sol << endl;
continue;
}
int left = 1, right = m;
while(v[left] == left) {left++;}
while(v[right] == right + n - m) {right--;}
if (right < left) {
sol = "NO";
cout << sol << endl;
continue;
}
bool check = false;
cnt1 = 0;
for (int i = 1; i <= m; i++) {
cnt1 += v[i] - v[i - 1] - 1;
if (cnt1 >= k / 2 && n - m - cnt1 >= k / 2) {check = true;}
}
if (!check) {sol = "NO";}
cout << sol << endl;
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int T,a[N],n,m,k,mark;
int main() {
scanf("%d",&T);
while(T--){
memset(a,0,sizeof a);
scanf("%d%d%d",&n,&k,&m);
for(int i=0;i<m;++i){
int x;
scanf("%d",&x);
a[x]=1;
}
//cout << "\nn = " << n << " k = " << k << " m = " << m << endl;for(int i=1;i<=n;++i)cout << a[i] << ' ';cout << endl;
if((n-m)%(k-1)){
puts("NO");
continue;
}
int j=1,i=1;
int h=k/2;
while(h--){
while(a[i])++i;
while(j<=i||!a[j]&&j<=n)++j;
++i;
}
mark=j;
j=n;i=n;
h=k/2;
while(h--){
while(a[i])--i;
while(j>=i||!a[j]&&j)--j;
--i;
}
if(j<mark){
puts("NO");
continue;
}
puts("YES");
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=3e5+5;
void solve(){
int n,k,m;
cin>>n>>k>>m;
vector<int>v,vis(n);
while(m--){
int x;
cin>>x;
vis[x-1]=1;
}
for(int i=0;i<n;i++)
if(!vis[i])
v.push_back(i);
for(int i=k/2;i<v.size();i++){
if(v.size()%(k-1)==0&&v[i]!=v[i-1]+1&&v.size()-i>=k/2){
cout<<"YES\n";
return;
}
}
cout<<"NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _=1;cin>>_;
while(_--){solve();}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int t , k , n , m , x , totl , totr , l , r , flag;
int a[N];
int main()
{
cin >> t;
while(t --)
{
for(int i = 1 ; i <= n ; i ++)
a[i] = 0;
flag = 0;
totl = 0;
totr = 0;
cin >> n >> k >> m;
for(int i = 0 ; i < m ; i ++)
{
cin >> x;
a[x] = 1;
}
// for(int i = 1 ; i <= n ; i ++)
// cout << a[i] << " ";
// cout << endl;
if((n-m) % (k-1) != 0)
{
cout << "NO" << endl;
}
else
{
for(int i = 1 ; i <= n && totl < (k-1)/2 ; i ++)
{
if(a[i] == 0)
{
totl ++;
l = i;
}
}
// cout << "&" << totl << " " << (k-1)/2 << " ";
for(int i = n ; i > 0 && totr < (k-1)/2 ; i --)
{
if(a[i] == 0)
{
totr ++;
r = i;
}
}
// cout << totr << endl;
for(int i = l+1 ; i < r ; i ++)
{
if(a[i])
{
flag = 1;
break;
}
}
if(flag)
cout << "YES" << endl;
else
{
cout << "NO" << endl;
}
}
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, k;
cin >> n >> k >> m;
int x;
vector<int> a(n + 1, 0);
for (int i = 0; i < m; ++i) {
cin >> x;
a[x] = 1;
}
int c0 = n - m, c = 0;
bool can = false;
can |= c0 == 0;
for (int i = 1; i <= n; ++i) {
if (a[i] == 0) ++c;
else can |= (c >= k / 2) && ((c0 - c) >= k / 2);
}
if (c0 % (k - 1) != 0) can = false;
puts(can ? "YES" : "NO");
}
int main () {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) solve();
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
bool good[200005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin>>T;
while(T--){
int n, k, m;
cin>>n>>k>>m;
for(int i=1;i<=n;i++){
good[i]=false;
}
for(int i=0;i<m;i++){
int x;
cin>>x;
good[x]=true;
}
//cout<<n<<" "<<k<<" "<<m<<endl;
if((n-m)%(k-1)!=0){
cout<<"NO\n";
continue;
}
int cur=0;
for(int i=1;i<=n;i++){
if(!good[i]){
cur++;
}
else{
if(cur>=(k-1)/2 && n-m-cur>=(k-1)/2){
cout<<"YES\n";
goto hell;
}
}
}
cout<<"NO\n";
hell:;
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
using ii = pair<int, int>;
using pii = pair<int, int>;
using vi = vector<int>;
#define sz(x) ((int)((x).size()))
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
bool done_something = false;
stringstream res;
res << "[";
for (; begin_iter != end_iter and counter; ++begin_iter) {
done_something = true;
counter--;
res << *begin_iter << ", ";
}
string str = res.str();
if (done_something) {
str.pop_back();
str.pop_back();
}
str += "]";
return str;
}
vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
vector<int> ord(size);
for (int i = 0; i < size; i++) ord[i] = i;
sort(ord.begin(), ord.end(), compare);
return ord;
}
template <typename T>
bool MinPlace(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool MaxPlace(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename S, typename T>
ostream& operator <<(ostream& out, const pair<S, T>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& v) {
out << "[";
for (int i = 0; i < (int)v.size(); i++) {
out << v[i];
if (i != (int)v.size()-1) out << ", ";
}
out << "]";
return out;
}
template<class TH>
void _dbg(const char* name, TH val){
clog << name << ": " << val << endl;
}
template<class TH, class... TA>
void _dbg(const char* names, TH curr_val, TA... vals) {
while(*names != ',') clog << *names++;
clog << ": " << curr_val << ", ";
_dbg(names+1, vals...);
}
#if TPOPPO
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define dbg_arr(x, len) clog << #x << ": " << print_iterable(x, x+len, -1) << endl;
#else
#define dbg(...)
#define dbg_arr(x, len)
#endif
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
const int MAXM = 2e5 + 100;
int v[MAXM];
int main() {
ios::sync_with_stdio(false);
cin.tie(0); // Remove in problems with online queries!
cin.exceptions(cin.failbit);
int t;
cin >> t;
while(t--){
int n, k, m;
cin >> n >> k >> m;
for(int i=0; i<m; i++){
cin >> v[i];
}
bool fine = true;
if((n-m)%(k-1) == 0){
for(int i=0; i<m; i++){
int a = v[i] - i -1;
int b = n - v[i] - m + 1 + i;
if(a < b) swap(a, b);
if(b >= k/2){
cout << "YES\n";
fine = false;
break;
}
}
}
if(fine) cout << "NO\n";
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n,k,m = map(int,stdin.readline().split())
b = list(map(int,stdin.readline().split()))
if (n-m)%(k-1) != 0:
print ("NO")
continue
lis = [1] * n
for i in b:
lis[i-1] = 0
ns = 0
while ns != k//2:
ns += lis[-1]
del lis[-1]
ns = 0
lis.reverse()
while ns != k//2:
ns += lis[-1]
del lis[-1]
if len(lis) == sum(lis):
print ("NO")
else:
print ("YES") | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
#define rep(i,n,z) for(int i=z;i<n;i++)
#define rrep(i,z) for(int i=z;i>=0;i--)
#define nl cout<<endl
#define vi vector<int>
#define vlli vector<long long int>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair <int,int>
#define llipair pair <lli,lli>
#define pq priority_queue
#define displaymatrix(a,m,n) for(int i=0;i<m;i++){for(int j=0;j<n;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define printarray(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}nl;
#define vinput(a,n) vlli a(n);rep(i,n,0)cin>>a[i]
#define ainput(a,n) rep(i,n,0)cin>>a[i]
#define SO(a) sort(a.begin(),a.end())
#define all(x) (x).begin(),(x).end()
#define SOP(a,comp) sort(a.begin(),a.end(),comp)
#define inf INT_MAX
#define endl '\n'
int main()
{
fastio;
int t;
cin>>t;
while(t--){
lli n,k,m;
cin >> n >> k >> m;
vinput(a,m);
set <int> s;
rep(i,n,0)s.insert(i + 1);
rep(i,m,0)s.erase(a[i]);
stack <int> pivot;
rep(i,m,0)pivot.push(a[m - i - 1]);
if((n - m)%(k - 1) != 0){
cout<<"NO"<<endl;
continue;
}
while(!pivot.empty()){
set <int> newe;
int mx = 0;
for(auto x: s){
if(newe.size() != (k - 1)/2)newe.insert(x);
else break;
mx = x;
}
if(newe.size() != (k - 1)/2)break;
while(!pivot.empty() and pivot.top() < mx)pivot.pop();
if(pivot.empty())break;
// cout<<"new pivot = "<<pivot.top()<<" mx = "<<mx<<endl;
for(auto itr = s.lower_bound(pivot.top());itr != s.end();itr++){
if(newe.size() != (k - 1))newe.insert(*itr);
else break;
}
if(newe.size() != (k - 1))break;
s.clear();
}
if(s.empty())cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
// 4
// 4 5 7
// 4 1 3
// 1 2 5
// 2 3 8
// 2 4 1
// 3 4 4
// 4 6 5
// 1 2 1
// 1 3 1
// 1 4 2
// 2 4 1
// 4 3 1
// 3 2 1
// 3 2 10
// 1 2 8
// 1 3 10
// 5 5 15
// 1 2 17
// 3 1 15
// 2 3 10
// 1 4 14
// 2 5 8 | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<cstdio>
#include<vector>
using namespace std;
int T,N,K,M;
bool ex[2<<17];
int main()
{
scanf("%d",&T);
for(;T--;)
{
scanf("%d%d%d",&N,&K,&M);
for(int i=0;i<N;i++)ex[i]=false;
for(int i=0;i<M;i++)
{
int b;scanf("%d",&b);ex[b-1]=true;
}
if((N-M)%(K-1)!=0)
{
puts("NO");
continue;
}
int t=(N-M)/(K-1);
vector<int>del;del.reserve(N-M);
for(int i=0;i<N;i++)if(!ex[i])del.push_back(i);
int L=del[K/2-1],R=del[N-M-K/2];
bool fn=false;
for(int i=L;i<=R;i++)if(ex[i])fn=true;
puts(fn?"YES":"NO");
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | //November
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=5e5+1;
int a[maxn];
bool visit[maxn];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,k,m;
cin>>n>>k>>m;
for(int i=1;i<=m;i++)
cin>>a[i];
memset(visit,0,sizeof(visit));
for(int i=1;i<=m;i++)
visit[a[i]]=true;
bool can=0;
int now=0;
int all=n-m;
if(all%(k-1)!=0)
{
cout<<"NO"<<endl;
continue;
}
for(int i=1;i<=n;i++)
{
if(!visit[i])
now++;
else
{
if(now>=(k-1)/2&&all-now>=(k-1)/2)
can=1;
}
}
if(can)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
cout.setf(ios::fixed); cout.precision(20);
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
int t; cin >> t;
while (t--) {
int n, k, m; cin >> n >> k >> m;
vector<int> b(m);
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
if ((n - m) % (k - 1) != 0) {
cout << "NO" << endl;
continue;
}
vector<pii> v(m);
for (int i = 0; i < m; ++i) {
v[i].first = b[i] - 1 - i;
v[i].second = n - b[i] - m + i + 1;
}
int r = (k - 1) / 2;
bool alpha = false;
for (int i = 0; i < m; ++i) {
if (v[i].first >= r && v[i].second >= r) {
alpha = true;
break;
}
}
if (alpha) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | // Retired?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long unsigned long ull;
typedef double long ld;
int main() {
ios::sync_with_stdio(!cin.tie(0));
int t;
cin >> t;
while (t--) {
int n, k, m;
cin >> n >> k >> m;
k--;
vector<int> u(n);
for (int i=0; i<m; i++) {
int x;
cin >> x;
x--;
u[x] = 1;
}
if ((n-m) % k) {
cout << "NO\n";
continue;
}
vector<int> e;
for (int i=0; i<n; i++) {
if (u[i] == 0) {
e.push_back(i);
}
}
int l = e[k/2-1];
int r = e[e.size() - k/2];
if (count(u.begin()+l+1, u.begin()+r, 1) == 0) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
const int N = 2e5 + 9;
using namespace std;
int n,k,m,x;
int a[N];
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> k>>m;
for (int i=1;i<=n;i++)
a[i]=0;
for (int i=0;i<m;i++)
{
scanf("%d",&x);
a[x]++;
}
if ((n-m)%(k-1)!=0)
{
cout<<"No"<<endl;
continue;
}
for (int i=1;i<=n;i++)
{
a[i]^=1;
//cout<<i<<' '<<a[i]<<' '<<a[i-1]<<endl;
if (a[i]==0 && a[i-1]>=(k-1)/2 && a[i-1]<=(n-m)-(k-1)/2)
{
cout<<"Yes"<<endl;
break;
}
a[i]+=a[i-1];
if (i==n)
cout<<"No"<<endl;
}
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second
#define ii pair<int,int>
template<typename G1, typename G2 = G1, typename G3 = G1>
struct triple{ G1 F; G2 S; G3 T; };
typedef triple<int> iii;
int main() {
#ifdef Adrian
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
//ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int t;
cin>>t;
while(t--)
{
int n, k, m;
cin>>n>>k>>m;
vector<bool> g(n);
for(int i=0; i<m; i++)
{
int x;
cin>>x;
g[x - 1] = true;
}
if((n - m) % (k - 1) != 0)
{
cout<<"NO\n";
continue;
}
k /= 2;
bool ok = false;
int a = 0, b = n - m;
for(int i=0; i<n; i++)
{
if(g[i] && a > 0 && b > 0)
{
if(a % k == 0 && b % k == 0)
ok = true;
else if(a > k && b > k)
ok = true;
}
a += !g[i];
b -= !g[i];
}
cout<<(ok ? "YES\n" : "NO\n");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import sys
def load_sys():
return sys.stdin.readlines()
def load_local():
with open('input.txt','r') as f:
input = f.readlines()
return input
def km(n,k,m,B):
if (n-m)%(k-1) != 0:
return 'NO'
R = [0]*m
L = [0]*m
for i in range(m):
R[i] = n-B[i]-(m-i-1)
L[i] = n-m-R[i]
if R[i] >= k//2 and L[i] >= k//2:
return 'YES'
return 'NO'
#input = load_local()
input = load_sys()
idx = 1
N = len(input)
while idx < len(input):
n,k,m = [int(x) for x in input[idx].split()]
B = [int(x) for x in input[idx+1].split()]
idx += 2
print(km(n,k,m,B)) | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | /// You just can't beat the person who never gives up
/// ICPC next year
#include<bits/stdc++.h>
using namespace std ;
const int N = 2e5+5 ;
int t ,n ,k ,m ,s[N] ,l[N] ,r[N] ;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&k,&m);
for(int i=1;i<=m;++i){
scanf("%d",s+i);
l[i] = r[i] = 0 ;
}
sort(s+1,s+m+1);
int need = (k-1)/2 ;
int rmv = k-1 ;
bool ok = 0 ;
for(int i=1;i<=m;++i){
l[i] = s[i]-i ;
r[i] = (n-s[i]+1) - (m-i+1) ;
if((l[i]+r[i])%rmv) continue ;
if(l[i]>=need && r[i]>=need){
ok = 1 ;
break ;
}
}
puts(ok?"YES":"NO");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | # -*- coding: utf-8 -*-
"""
Created on Sat Jan 16 12:33:14 2021
@author: ludoj
"""
t=int(input())
res=[]
for x1 in range(t):
n,k,m=[int(i) for i in input().split()]
eind=[int(i) for i in input().split()]
x2=len(eind)
if (n-m)%(k-1)!=0:
res.append("NO")
else:
h=(k-1)//2
i1=0
a1=1
tel1=0
while(tel1<h and i1<x2):
if eind[i1]==a1:
a1+=1
i1+=1
else:
tel1+=1
a1+=1
#nu gevonden met h dingen ervoor
if i1>=x2:
res.append("NO")
else:
a2=eind[i1]
tel2=0
while(tel2<h and i1<x2):
if eind[i1]==a2:
a2+=1
i1+=1
else:
tel2+=1
a2+=1
if i1==x2:
tel2+=n-eind[i1-1]
if tel2>=h:
res.append("YES")
else:
res.append("NO")
for i in res:
print(i)
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
#define int long long
#define zmrl(x) signed((x).size())
#define ahen(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using pii = pair<int,int>;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int TC; cin>>TC;
for (int tc=1; tc<=TC; tc++) {
int n,k,m; cin>>n>>k>>m;
vector<bool> v(n+5, true);
for (int i=1; i<=m; i++) {
int a; cin>>a; v[a] = false;
}
if ((n-m)%(k-1)) {
cout<<"no\n";
continue;
}
int cnt = 0;
bool flag = false;
for (int i=1; i<=n; i++) {
if (v[i]) cnt++;
else {
if (cnt >= (k-1)/2 && n-m-cnt >= (k-1)/2) {
flag = true;
break;
}
}
}
cout<<(flag ? "yes" : "no")<<'\n';
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | from __future__ import division, print_function
from itertools import permutations
import threading,bisect,math,heapq,sys
from collections import deque
# threading.stack_size(2**27)
# sys.setrecursionlimit(10**4)
from sys import stdin, stdout
i_m=9223372036854775807
def cin():
return map(int,sin().split())
def ain(): #takes array as input
return list(map(int,sin().split()))
def sin():
return input()
def inin():
return int(input())
prime=[]
def dfs(n,d,v):
v[n]=1
x=d[n]
for i in x:
if i not in v:
dfs(i,d,v)
return p
def block(x):
v = []
while (x > 0):
v.append(int(x % 2))
x = int(x / 2)
ans=[]
for i in range(0, len(v)):
if (v[i] == 1):
ans.append(2**i)
return ans
"""**************************MAIN*****************************"""
def main():
t=inin()
for _ in range(t):
n,k,m=cin()
a=ain()
if (n-m)%(k-1)!=0:
print("NO")
continue
if n==m:
ans="YES"
continue
h=n-m
x=0
ans="NO"
for i in range(0,m):
if i==0:
x=a[i]-1
else:
x+=a[i]-a[i-1]-1
p=h-x
# print(x,p)
if x>=k//2 and p>=k//2:
ans="YES"
print(ans)
"""***********************************************"""
def intersection(l,r,ll,rr):
# print(l,r,ll,rr)
if (ll > r or rr < l):
return 0
else:
l = max(l, ll)
r = min(r, rr)
return max(0,r-l+1)
######## Python 2 and 3 footer by Pajenegod and c1729
fac=[]
def fact(n,mod):
global fac
fac.append(1)
for i in range(1,n+1):
fac.append((fac[i-1]*i)%mod)
f=fac[:]
return f
def nCr(n,r,mod):
global fac
x=fac[n]
y=fac[n-r]
z=fac[r]
x=moddiv(x,y,mod)
return moddiv(x,z,mod)
def moddiv(m,n,p):
x=pow(n,p-2,p)
return (m*x)%p
def GCD(x, y):
x=abs(x)
y=abs(y)
if(min(x,y)==0):
return max(x,y)
while(y):
x, y = y, x % y
return x
def Divisors(n) :
l = []
ll=[]
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
ll.append(n//i)
l.extend(ll[::-1])
return l
def SieveOfEratosthenes(n):
global prime
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
f=[]
for p in range(2, n):
if prime[p]:
f.append(p)
return f
def primeFactors(n):
a=[]
while n % 2 == 0:
a.append(2)
n = n // 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
a.append(i)
n = n // i
if n > 2:
a.append(n)
return a
"""*******************************************************"""
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'R' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'R' [0]:
A.append(sign*numb)
return A
# threading.Thread(target=main).start()
if __name__== "__main__":
main() | PYTHON |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0)
int main() {
IO; //freopen("in.txt", "r", stdin);
int T; cin >> T; while(T--) {
int n, k, m; cin >> n >> k >> m;
vector<int> a(m);
for(int i = 0; i < m; ++i) {
cin >> a[i];
}
if((n - m) % (k - 1) != 0) {
cout << "NO\n";
continue;
}
bool ok = 0;
for(int i = 0; i < m; ++i) {
if(a[i] - i - 1 >= (k - 1) / 2 &&
(n - a[i] - (m - i - 1)) >= (k - 1) / 2) {
ok = 1;
}
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(0);
int t;
cin >> t;
while(t--){
int n,k,m;
cin >> n >> k >> m;
vector<int> b(m,0);
for(auto &it:b) cin >> it;
for(auto &it:b) it--;
if(m==n){
cout << "YES\n";
continue;
}
int o = n-m;
if(o%(k-1)){
cout << "NO\n";
continue;
}
vector<int> bmap(n,0);
for(auto it:b)
bmap[it]=1;
int presum = 0;
bool failed = true;
for(int i = 0; i<n; i++){
presum+=1-bmap[i];
if(bmap[i]==1&&presum>=(k-1)/2&&o-presum>=(k-1)/2)
failed = false;
}
if(failed)
cout << "NO\n";
else
cout << "YES\n";
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t; cin >> t;
while (t--)
{
ll n, m, k; cin >> n >> k >> m;
vector<ll>arr(m);
for (ll i = 0; i < m; i++)
cin >> arr[i];
ll len = k / 2LL;
if ((n - m) % (k - 1))
cout << "NO\n";
else if (n == m)
cout << "YES\n";
else
{
vector<ll>diff;
ll prev = 0;
for (ll i = 0; i < m; i++)
{
diff.push_back(arr[i] - prev - 1LL);
prev = arr[i];
}
vector<ll>pref(m);
pref[0] = diff[0];
for (ll i = 1; i < m; i++)
pref[i] = pref[i - 1] + diff[i];
bool check = false;
for (ll i = 0; i < m; i++)
{
ll l = pref[i] , r = n - m - l;
if (l >= len && r >= len)
check = true;
}
if (check)
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define endl '\n'
#define lli long long int
#define ld long double
#define forn(i,n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define fastIO(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define SZ(s) int(s.size())
using namespace std;
typedef vector<lli> VLL;
typedef vector<int> VI;
// 0-indexed
template <typename T, typename S>
struct SegmentTreeIt{
T neutro = 0;
int size;
vector<T> st;
SegmentTreeIt(int n, T val = 0, const vector<S> &v = vector<S>()){
st.resize(2*n);
if(v.empty())
fill(all(st),val);
size = n;
if(!v.empty())
forn(i,n) st[i+size] = set(v[i]);
build();
}
T merge(T a, T b){
return a + b;
}
void build(){
for(int i = size - 1; i; i--)
st[i] = merge(st[i << 1], st[i << 1 | 1]);
}
void update(int i, T val) {
for(st[i += size] = val; i > 1; i >>= 1)
st[i >> 1] = merge(st[i], st[i ^ 1]);
}
T query(int l, int r){
T ans = neutro;
for(l += size, r += size; l <= r; l >>= 1, r >>= 1) {
if (l & 1) ans = merge(ans, st[l++]);
if (~r & 1) ans = merge(ans, st[r--]);
}
return ans;
}
T set(S x){
return x;
}
T& operator[](int i) { return st[i + size]; }
};
string solve()
{
lli n, k, m; cin>>n>>k>>m;
vector<lli> B(m);
for(auto &x: B) cin>>x;
vector<lli> spaces(m+1);
lli last = 0;
for(int i = 0; i<m; i++)
{
spaces[i] = B[i] - last - 1;
last = B[i];
}
spaces[m] = n - last;
SegmentTreeIt<lli, lli> ST(SZ(spaces));
lli sum_spaces = 0;
for(int i = 0; i<SZ(spaces); i++)
{
//cout << spaces[i] << " ";
sum_spaces += spaces[i];
ST.update(i, spaces[i]);
}
if(sum_spaces % (k-1) != 0) return "NO";
for(int i = 0; i<SZ(spaces)-1; i++)
{
lli l = i, r = i+1; //queries = (0, l), (r, SZ(spaces)-1);
lli izq = ST.query(0, l);
lli der = ST.query(r, m);
if(izq >= (k/2) && der >= (k/2))
{
return "YES";
}
//cout << B[i] << " " << izq << " " << der << endl;
}
return "NO";
}
int main () {
fastIO();
lli t; cin>>t;
while(t--) cout << solve() << endl;
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define INF 2000000000000000000LL
#define EPS 1e-9
#define debug(a) cerr<<#a<<"="<<(a)<<"\n"
#define debug2(a, b) cerr<<#a<<"="<<(a)<<" ";debug(b)
#define debug3(a, b, c) cerr<<#a<<"="<<(a)<<" ";debug2(b, c)
#define debug4(a, b, c, d) cerr<<#a<<"="<<(a)<<" ";debug3(b, c, d)
#define debug5(a, b, c, d, e) cerr<<#a<<"="<<(a)<<" ";debug4(b, c, d, e)
#define FastSlowInput ios_base::sync_with_stdio(false); cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T>
void printArray(const T * a, int n, ostream& out = cerr);
template<class T>
void printArray(const vector<T> &arr, ostream& out = cerr);
const ll mod = 1e9+7;
const double PI = acos(-1);
int n,i,j,k,t,m;
int a[200003];
int l[200003];
int r[200003];
int main(){
scanf("%d", &t);
while(t--) {
scanf("%d %d %d", &n, &k, &m);
a[0] = a[n+1] = 0;
for(int i=1;i<=n;++i){
a[i] = 1;
}
for(int i=0;i<m;++i){
int x = 0;
scanf("%d", &x);
a[x] = 0;
}
l[0] = 0;
for(int i=1;i<=n;++i) {
l[i] = l[i-1] + a[i];
}
r[n+1] = 0;
for(int i=n;i>0;--i) {
r[i] = r[i+1] + a[i];
}
if((n-m)%(k-1) != 0) {
puts("NO");
continue;
}
bool bisa = false;
for(int i=1;i<=n;++i) {
if(a[i] == 0){
if ((l[i-1] >= k/2 && r[i+1] >= k/2)) {
bisa = true;
break;
}
}
}
puts(bisa? "YES":"NO");
}
return 0;
}
/* Template Function */
template<class T>
void printArray(const T * a, int n, ostream& out){
for(int i=0;i<n;++i){
out<<a[i]<<" ";
}
out<<endl;
}
template<class T>
void printArray(const vector<T> &arr, ostream& out){
for(const T& x : arr){
out<<x<<" ";
}
out<<endl;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import heapq
t = int(input())
for _ in range(t):
n, k, m = map(int, input().split())
l = [0] + list(map(int, input().split())) + [n+1]
diffs = [l[i+1]-l[i]-1 for i in range(m+1)]
if sum(diffs) % (k-1) > 0:
print('NO')
continue
ls = 0
ind = -1
while ls < k//2:
ind += 1
ls += diffs[ind]
rs = 0
ind2 = m+1
while rs < k//2:
ind2 -= 1
rs += diffs[ind2]
if ind2 <= ind:
print('NO')
else:
print('YES')
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | //#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define MOD 998244353
#define pdd pair<double,double>
#define mem(a,x) memset(a,x,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const long long INF = 1e18+5;
const int inf = 0x3f3f3f3f;
const double eps=1e-6;
const int maxn=200005;
const double pi=acos(-1);
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(int &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int vis[200005];
int a[200005];
void solve()
{
int n,k,m;
mem(vis,0);
scanf("%d%d%d",&n,&k,&m);
for(int i=1;i<=m;i++){
int num;
scanf("%d",&num);
vis[num]=1;
}
int pos=0;
for(int i=1;i<=n;i++){
if(!vis[i]){
a[++pos]=i;
}
}
for(int i=k/2+1;i<=pos;i++){
//cout<<pos-i<<endl;
if(pos%(k-1)==0&&pos-i+1>=k/2&&a[i]!=a[i-1]+1){
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
return ;
}
int main()
{
int T;
cin>>T;
while(T--){
solve();
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<iostream>
using namespace std;
int x[200001];
int t,n,k,m;
int l,r,last,d,mk;
void solve(){
cin>>n>>k>>m;
l=0;
r=n-m;
last=0;
d=0;
mk=k/2;
for(auto i=x;i!=x+m;i++){
cin>>*i;
}
if((n-m) % (k-1) != 0){
cout<<"no\n";
return;
}
for(int i=0;r>=mk && i<m;i++){
d = x[i] - last - 1;
l += d;
r -= d;
last = x[i];
if(l>=mk && r>=mk){
cout<<"yes\n";
return;
}
}
cout<<"no\n";
}
int main(){
std::ios::sync_with_stdio(false);
cin>>t;
while(t--){
solve();
}
cout.flush();
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | // In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x));
#define debug(x) cerr << #x << " : " << x << "\n"
#define use_file freopen("input.txt", "r", stdin); frepen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef string str;
const ll maxn = 5e5 + 6, inf = 1e18, mod = 1e9 + 7;
ll t, n, m, k, b[maxn];
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t --){
cin >> n >> k >> m;
bool bl = 0;
ll d = k / 2;
for(ll i = 1; i <= m; i ++){
cin >> b[i];
if((b[i] - i >= d) && ((n - b[i]) - (m - i) >= d))bl = 1;
}
if(!bl || ((n - m) % (k - 1)))cout << "NO\n";
else cout << "YES\n";
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | /**
* Author: Daniel
* Created Time: 2021-01-28 17:54:32
**/
// time-limit: 2000
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ER erase
#define IS insert
#define PI acos(-1)
#define PB pop_back
#define EB emplace_back
#define lowbit(x) (x & -x)
#define SZ(x) ((int)x.size())
#define MP(x, y) make_pair(x, y)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define SOS; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<fixed<<setprecision(10);
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
template <typename A> using VE = vector<A>;
template <typename A> using USET = unordered_set<A>;
template <typename A> using HEAP = priority_queue<A>;
template <typename A, typename B> using PA = pair<A, B>;
template <typename A, typename B> using UMAP = unordered_map<A, B>;
template <typename A> using RHEAP = priority_queue<A, vector<A>, greater<A> >;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(const char c) { return to_string((string)"" + c); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v)
{
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++)
{
if (!first) res += ", ";
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v)
{
string res = "";
for (size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
return res;
}
template <typename A>
string to_string(A v)
{
bool first = true;
string res = "{";
for (const auto &x : v)
{
if (!first) res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cout << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); }
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
// check the limitation!!!
const int N = 200010, M = 1010;
int T;
int n, m, k;
int b[N];
bool st[N];
// read the question carefully!!!
int main()
{
SOS;
cin >> T;
while (T -- )
{
cin >> n >> k >> m;
for (int i = 1; i <= n; i ++ ) st[i] = false;
for (int i = 1; i <= m; i ++ )
{
cin >> b[i];
st[b[i]] = true;
}
if ((n - m) % (k - 1))
{
cout << "NO\n";
continue;
}
bool flag = false;
int cnt = 0;
for (int i = 1; i <= n; i ++ )
if (!st[i]) cnt ++ ;
else
{
if (cnt >= (k - 1) / 2 && (n - m - cnt) >= (k - 1) / 2)
flag = true;
}
cout << (flag ? "YES\n" : "NO\n");
}
return 0;
}
// GOOD LUCK!!!
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | """
Author - Satwik Tiwari .
15th Dec , 2020 - Tuesday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
# from itertools import *
from heapq import *
from math import gcd, factorial,floor,ceil,sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def testcase(t):
for pp in range(t):
solve(pp)
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
y%=(p-1) #not so sure about this. used when y>p-1. if p is prime.
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
inf = pow(10,20)
mod = 10**9+7
#===============================================================================================
# code here ;))
def solve(case):
n,k,m = sep()
a = lis()
have = set(a)
if(len(have) != m):
print("NO")
return
notvis = []
for i in range(1,n+1):
if(i not in have):
notvis.append(i)
if(len(notvis)%(k-1)):
# print(len(notvis))
print('NO')
return
left = 0
right = 0
diff = []
cnt = 1
for i in range(1,len(notvis)):
if(notvis[i] == notvis[i-1]+1):
cnt+=1
else:
diff.append(cnt)
cnt = 1
diff.append(cnt)
ind = 0
while(left + diff[ind] < k//2):
left += diff[ind]
ind+=1
rnd = len(diff)-1
while(right + diff[rnd] < k//2):
right += diff[rnd]
rnd-=1
print('YES' if ind < rnd else 'NO')
# testcase(1)
testcase(int(inp()))
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | T=int(input())
for _ in range(T):
n,k,m=map(int, input().split())
b=[0]+list(map(int, input().split()))+[0]*10 #1~m (len=m+1)
cnt=0
l=[0]*(m+10)
r=[0]*(m+10)
for i in range(1,m+1): #i:1~m
cnt+=b[i]-b[i-1]-1
l[i]=l[i-1]+b[i]-b[i-1]-1
cnt+=n-b[m]
r[m]=n-b[m]
for i in range(m-1,0,-1): #i:m-1~1
r[i]=r[i+1]+b[i+1]-b[i]-1
flag=False
for i in range(1,m+1): #i:1~m
if l[i]>=k//2 and r[i]>=k//2:
flag=True
if cnt%(k-1)!=0:
flag=False
if flag:
print("YES")
else:
print("NO")
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | //#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#define ll long long
#define skip cin>>ws;
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define vpll vector<pair<ll,ll>>
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define pll pair<ll,ll>
#define vs vector<string>
#define vvpll vector<vector<pair<ll, ll>>>
#define pb push_back
#define pob pop_back()
#define MOD (ll)(1e9 + 7)
#define MOD2 (ll)(998244353)
#define INF (ll)(1e18 + 5)
#define count1(n) __builtin_popcountll(n)
#define test ll t; cin>>t; while(t--)
#define enter(a) for(ll i=0;i<a.size();i++) cin>>a[i];
#define show(a) for(ll e: a) cout<<e<<" "; cout<<"\n";
using namespace std;
ll mo(ll a){ return a%MOD;}
ll po(ll x, ll y, ll p)
{
ll res = 1; x = x % p;
while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; }
return res%p;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
test
{
ll n, k, m;
cin>>n>>k>>m;
vll a(m);
enter(a);
vll rem(n, 1);
for(ll i=0;i<m;i++)
{
a[i]--;
rem[a[i]] = 0;
}
if(((n - m)%(k-1)))
{
cout<<"NO\n";
continue;
}
vll left(n), right(n);
for(ll i=1;i<n;i++)
{
left[i] = left[i-1] + rem[i-1];
}
for(ll i=n-2;i>=0;i--)
{
right[i] = right[i+1] + rem[i+1];
}
// cout<<"left: "; show(left)
// cout<<"right: "; show(right)
ll pos = 0;
for(ll i=0;i<n;i++)
{
if(((left[i]*2)>=(k-1)) && ((right[i]*2)>=(k-1)) && (!rem[i]))
{
pos = 1;
break;
}
}
if(pos)
{
cout<<"YES\n";
}
else
cout<<"NO\n";
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, m, b[N];
void solve()
{
scanf("%d%d%d", &n, &k, &m);
for(int i=1; i<=m; i++) scanf("%d", b+i);
if((n-m)%(k-1)) puts("NO");
else
{
for(int i=1; i<=m; )
{
int j = i + 1;
while(j<=m && b[j]==b[i]+1) ++j;
if((b[j-1]-j+1)>=k/2 && (n-b[j-1]-m+j-1)>=k/2)
{
puts("YES");
return;
}
i = j;
}
puts("NO");
}
}
int main()
{
int _; scanf("%d", &_);
while(_--) solve();
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define int long long
#define PI pair<int,int>
using namespace std;
const int maxm=2e6+5;
int b[maxm];
int n,k,m;
void solve(){
cin>>n>>k>>m;
for(int i=1;i<=m;i++){
cin>>b[i];
}
if((n-m)%(k-1)){
cout<<"NO"<<endl;
return ;
}
for(int i=1;i<=m;i++){
if(b[i]-i>=(k-1)/2&&(n-b[i])-(m-i)>=(k-1)/2){
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
}
signed main(){
ios::sync_with_stdio(0);
int T;cin>>T;
while(T--){
solve();
}
return 0;
}
/*
每次操作减少k-1个数,那么当(n-m)%(k-1)!=0时一定无解.
*/
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from itertools import accumulate
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(i) for i in st()]
for _ in range(val()):
n, k, m = li()
b = set(li())
ans = 0
poss = []
temp = n
while temp > 0:
poss.append(temp)
temp -= k - 1
if m not in poss:
print('NO')
continue
left = 0
right = n - m
for i in range(1, n + 1, 1):
if i in b:
if left >= k // 2 and right >= k // 2:
ans = 1
break
else:
left += 1
right -= 1
print('YES' if ans else 'NO') | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <iostream>
#include <vector>
#define int long long
signed main() {
int t;
std::cin >> t;
while(t--) {
int n, k, m;
std::cin >> n >> k >> m;
int c = (k-1)/2;
std::vector<int> stay(n, 0);
for(int i = 0; i < m; i++) {
int x;
std::cin >> x;
x--;
stay[x] = 1;
}
if((n-m)%(2*c) != 0) {
std::cout << "NO" << std::endl;
continue;
}
bool poss = false;
int dell = 0, delr = n-m;
for(int i = 0; i < n; i++) {
if(stay[i]) {
if(std::min(dell, delr) == 0)
continue;
if(dell%c == 0 && delr%c == 0) {
poss = true;
}
if((dell%c)+c+1 < dell && delr >= c)
poss = true;
if((delr%c)+c+1 < delr && dell >= c)
poss = true;
}
else {
dell++;
delr--;
}
}
if(poss)
std::cout << "YES";
else
std::cout << "NO";
std::cout << std::endl;
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #define _USE_MATH_DEFINES
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <math.h>
#include <ctime>
#include <cctype>
#include <vector>
#include <string>
#include <utility>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
int main()
{
//srand(time(NULL));
cin.tie(NULL);
cout.tie(NULL);
ios_base :: sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t;
cin >> t;
while (t--)
{
int n, k, m;
cin >> n >> k >> m;
vector <int> b(m + 1);
vector <int> nd(n + 1);
for (int i = 1; i <= m; i++)
{
cin >> b[i];
nd[b[i]] = 1;
}
int del = n - m;
if ( (n - m) % (k - 1) != 0)
{
cout << "No\n";
continue;
}
bool flg1 = true, flg2 = true;
for (int i = 1; i <= del; i++)
if (nd[i]) flg1 = false;
for (int i = n - del + 1; i <= n; i++)
if (nd[i]) flg2 = false;
if (flg1 || flg2)
{
cout << "No\n";
continue;
}
vector <int> pr(n + 1);
for (int i = 1; i <= n; i++)
pr[i] = pr[i - 1] + (int)(!nd[i]);
bool flag = false;
for (int i = 1; i <= n; i++)
{
if (nd[i] && pr[i - 1] - pr[0] >= (k - 1) / 2 && pr[n] - pr[i] >= (k - 1) / 2)
flag = true;
}
if (flag)
cout << "Yes\n";
else
cout << "No\n";
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | // Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(System.out);
int T=Int();
for(int t=0;t<T;t++){
int n=Int();int k=Int();int m=Int();
int A[]=new int[m];
for(int i=0;i<m;i++){
A[i]=Int();
}
Solution sol=new Solution();
sol.solution(out,A,n,k);
}
out.flush();
}
public static long Long(){ return in.nextLong();}
public static int Int(){
return in.nextInt();
}
public static String Str(){
return in.next();
}
}
class Solution{
public void solution(PrintWriter out,int A[],int n,int k){
if((n-A.length)%(k-1)!=0){
System.out.println("NO");
return;
}
//add (k-1) digits back
int cnt=(k-1)/2;
for(int i=0;i<A.length;i++){
int left=A[i]-(i+1);
int right=n-A[i]-(A.length-(i+1));
if(left>=cnt&&right>=cnt){
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
}
| JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author kamel
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskH solver = new TaskH();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskH {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int m = in.nextInt();
int[] data = new int[m];
for (int i = 0; i < m; i++) {
data[i] = in.nextInt();
}
int s = (k - 1) / 2;
int toDelete = n - m;
if ((toDelete % (2 * s)) != 0) {
out.println("NO");
} else {
boolean valid = false;
for (int i = 0; i < m; i++) {
int left = data[i] - 1 - i;
int right = n - data[i] - (m - 1 - i);
// System.out.println(i + " " + left + " " + right);
if (left >= s && right >= s) {
valid = true;
out.println("YES");
break;
}
}
if (!valid) {
out.println("NO");
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<vvpi> vvvpi;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef complex<double> cd;
typedef vector<cd> vcd;
typedef unsigned int uint;
vector<int, int> &
getVector(const unordered_map<ll, int> &trad, vector<map<int, int>> &g, int dst, int &prox, ll res, int v, int i);
template<class C> void mini(C&a, C b){ a=min(a, b);}
template<class C> void maxi(C&a, C b){a=max(a,b);}
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define forall(it,s) for(auto it = s.begin(); it != s.end(); ++it)
#define F0(i,n) for(int i = 0; i < n; i++)
#define F1(i,n) for(int (i) = 1; i <= n; i++)
#define F0R(i,n) for(int (i) = n-1; i >= 0; i--)
#define F1R(i,n) for(int (i) = n; i >= 1; i--)
#define REP(i,a,b) for(int i = a; i <= b; i++)
#define REPR(i,a,b) for(int i = a; i >= b; i--)
#define INF 1e9
#define todo(v) v.begin(),v.end()
#define eps 0.000000001
#define mod 1000000007
#define PI acos(-1.0)
void h(){
int n,k,m;
cin>>n>>k>>m;
vb borrar(n,true);
int bi;
F0(i,m) {
cin>>bi;
bi--;
borrar[bi] = false;
}
if((n-m) % (k-1) != 0) {
cout<<"NO\n";
return;
}
int izq = 0;
F0(i,n){
if(borrar[i]) izq++;
else {
if(izq >= k/2 and (n-m)-izq >= k/2){
cout<<"YES\n";
return;
}
}
}
cout<<"NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("../input.txt","r",stdin);
//freopen("../output.txt","w",stdout);
int t;
cin>>t;
while(t--) h();
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
int n, k, m;
signed main() {
int T;
cin >> T;
while (T--) {
cin >> n >> k >> m;
bool flag = false;
for (int i = 1, b; i <= m; ++i) {
cin >> b;
if (b - i >= k / 2 && n - b - (m - i) >= k / 2) flag = true;
}
cout << (flag && ((n - m) % (k - 1) == 0)? "YES" : "NO") << "\n";
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include "bits/stdc++.h"
using namespace std;
typedef long double ld;
typedef long long ll;
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
template<typename T, typename U> bool ckmin(T &a, const U &b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool ckmax(T &a, const U &b){ return b > a ? a = b, true : false; }
int tt;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> tt;
while(tt--){
int n, m, k;
cin >> n >> k >> m;
vector<int> a(n, 0), b(m, 0), used(n, -1);
iota(a.begin(), a.end(), 0);
for(int i = 0; i < m; ++i){
cin >> b[i]; --b[i];
used[b[i]] = 0;
}
if((n - m)%(k - 1)){
cout << "NO\n";
continue;
}
if(n == m){
cout << "YES\n";
continue;
}
// cerr << "hi";
int cnt = 0, mx = ((n - m)/(k - 1)) * (k/2), md = 0;
for(int i = 0; i < n; ++i){
if(!used[i]) continue;
if(cnt < mx){
++cnt, used[i] = -1;
if(cnt == mx) md = i; // all L are to the <= md, all R > md
}
else used[i] = 1;
}
// cout << mx << "\n";
// for(auto i : used) cout << i << " ";
// cout << "\n";
bool wks = false;
// already a center
for(int i = md + 1; i < n; ++i){
if(!used[i]){
wks = true;
}
else break;
}
//try removing from right-middle
{
int rcen = md + 1;
if(rcen == -1) goto nxt;
int to_right = 0, side = k/2;
for(int i = rcen + 1; i < n; ++i){
if(used[i] == 1) ++to_right;
}
to_right = (to_right/side) * side;
for(int i = md; i >= 0; --i){
if(!used[i]) wks = true;
else{
--to_right;
if(to_right < 0) break;
}
}
}
nxt:;
//try removing from right-middle
{
int lcen = md;
if(lcen == -1) goto nxt2;
int to_left = 0, side = k/2;
for(int i = lcen - 1; i >= 0; --i){
if(used[i] == -1) ++to_left;
}
to_left = (to_left/side) * side;
for(int i = md + 1; i < n; ++i){
if(!used[i]) wks = true;
else{
--to_left;
if(to_left < 0) break;
}
}
}
nxt2:;
cout << (wks ? "YES" : "NO") << "\n";
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.util.*;
import java.util.Map.Entry;
import javax.xml.transform.OutputKeys;
import java.math.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
InputReader in = new InputReader(System.in);
// Scanner in = new Scanner(System.in);
// Scanner in = new Scanner(new BufferedReader(new
// InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(System.out);
// InputReader in = new InputReader(new
// File("ethan_traverses_a_tree.txt"));
// PrintWriter out = new PrintWriter(new
// File("ethan_traverses_a_tree-output.txt"));
int t = in.nextInt();
for (int qi = 0; qi < t; qi++) {
int n = in.nextInt();
int k = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
int[] b = new int[m];
int[] c = new int[n - m];
for (int i = 0; i < m; i++) {
int p = in.nextInt() - 1;
a[p] = 1;
b[i] = p;
}
int index = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
c[index] = i;
index++;
}
}
int kk = (k - 1);
if ((n - m) % kk != 0) {
out.printf("NO\n");
} else {
boolean ans = false;
for (int i = c[kk / 2 - 1] + 1; i < c[n - m - kk / 2]; i++) {
// System.out.println(i);
if (a[i] == 1) {
ans = true;
}
}
if (ans == true) {
out.printf("YES\n");
} else {
out.printf("NO\n");
}
}
}
out.close();
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public InputReader(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public boolean hasNext() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | for _ in range(int(input())):
n,k,m=tuple(map(int,input().split(" ")))
ml=set(map(int,input().split(" ")))
havitada=[]
for i in range(1,n+1):
if i not in ml:
havitada.append(i)
saab=False
if len(havitada)%(k-1)!=0:
print("no")
continue
for i in range(k//2-1,len(havitada)-k//2):
if havitada[i+1]-havitada[i]!=1:
print("yes")
break
else:
print("no") | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | for _ in range(int(input())):
n, k, m = list(map(int, input().split()))
arr = list(map(int, input().split()))
remain = [0] * (n+1)
for x in arr:
remain[x] = 1
use = n - m
if use % (k-1) != 0:
print('NO')
else:
flg = False
cnt = 0
for x in range(1, n+1):
if remain[x]==1:
if cnt >= k //2 and use-cnt>=k//2:
flg=True
else:
cnt+=1
if flg==True:
print('YES')
else:
print('NO') | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
long long N,M,K,X,ok,cur,mark[300000];
int main()
{
int tc;
cin>>tc;
while(tc--)
{
ok=0;
cur=0;
cin>>N>>K>>M;
for(int A=1;A<=N;A++)
mark[A]=1;
for(int A=1;A<=M;A++)
{
cin>>X;
mark[X]=0;
}
if((N-M)%(K-1)==0)
{
for(int A=1;A<=N;A++)
{
cur+=mark[A];
if(mark[A]==0 and cur>=(K-1)/2 and N-M-cur>=(K-1)/2)
{
ok=1;
break;
}
}
}
else
ok=0;
if(ok)
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t;
cin >> t;
while(t--){
int n, k, m;
cin >> n >> k >> m;
int a[m];
set<int> aa;
for(int i = 0; i < m; i++) cin >> a[i], aa.insert(a[i]);
if((n - m) % (k - 1) != 0){
cout << "NO\n";
continue;
}
int bad = (k - 1) / 2;
vector<int> b;
for(int i = 1; i <= n; i++) if(!aa.count(i)) b.push_back(i);
bool ok = false;
for(int i = bad; i <= b.size() - bad; i++) if(b[i] - b[i - 1] != 1) ok = true;
cout << (!ok ? "NO\n" : "YES\n");
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
#ifdef ALBE_PC
#include"debugger.h"
#else
#define db(...) false;
#define dbg(...) false;
#define dbl(...) false;
#endif //ALBE_PC
#define endl '\n'
#define int long long
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("z.txt", "r", stdin);
int TC;
cin >> TC;
while(TC--)
{
int n, k, m;
cin >> n >> k >> m ;
vector<int> ar(m);
for(auto &i: ar)
cin >> i ;
vector<int> car = ar;
sort(car.begin(), car.end());
car.resize(unique(car.begin(), car.end()) - car.begin());
if(car != ar)
{
cout << "NO" ;
continue;
}
bool sol = false;
for(int i = 0 ; i < m and !sol; i++)
{
if(ar[i] - i - 1 >= k / 2 and n - ar[i] - (m-i-1) >= k / 2)
sol = true ;
if(n % 2 == 0 and ar[i] - i - 1 >= (k+1) / 2 and n - ar[i] - (m-i-1) >= (k+1) / 2)
sol = true;
}
cout << (sol and (m - n) % (k - 1) == 0 ? "YES" : "NO") << endl ;
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll n,k,m;
cin>>n>>k>>m;
map<ll,ll>mp;
for(int i=0;i<m;i++){
ll x;
cin>>x;
mp[x]=1;
}
if((n-m)%(k-1)){
cout<<"NO"<<endl;
return;
}
ll cnt=0;
ll have=k/2;
ll xd=0,flag=0;
for(int i=1;i<=n;i++){
if(mp[i]!=1)cnt++;
else{
xd++;
if(cnt>=have && (n-i-(m-xd))>=have){
flag=1;
break;
}
}
}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
ll t=1;
cin>>t;
while(t--){
solve();
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
int vis[200005],b[200005],r[200005];
int n,k,m;
bool check(){
if((n-m)%(k-1)!=0)return false;
for(int i=1;i<=m;i++)if(vis[b[i]]>=k/2 && vis[n]-vis[b[i]]>=k/2)return true;
return false;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&k,&m);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++)vis[i]=1;
for(int i=1;i<=m;i++)vis[b[i]]=0;
for(int i=1;i<=n;i++)vis[i]+=vis[i-1];
if(check())printf("YES\n");
else printf("NO\n");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | /*
Washief Hossain Mugdho
04 March 2021
1468 1468H
*/
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define fastio ios_base::sync_with_stdio(0)
#define untie cin.tie(0)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepe(i, n) for (int i = n; i > 0; i--)
#define ms(a, b) memset(a, b, sizeof a)
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vvb = vector<vector<bool>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vpii = vector<pair<int, int>>;
using mii = map<int, int>;
/***********IO Utility**************/
template <typename... ArgTypes>
void print(ArgTypes... args);
template <typename... ArgTypes>
void input(ArgTypes &...args);
template <>
void print() {}
template <>
void input() {}
template <typename T, typename... ArgTypes>
void print(T t, ArgTypes... args)
{
cout << t;
print(args...);
}
template <typename T, typename... ArgTypes>
void input(T &t, ArgTypes &...args)
{
cin >> t;
input(args...);
}
inline void _()
{
int n, k, m;
cin >> n >> k >> m;
vi b(m);
int p = k >> 1;
rep(i, m) cin >> b[i];
rep(i, m)
{
int cur = b[i];
int l = cur - 1 - i;
int r = n - cur - m + i + 1;
int gap = abs(r - l);
if (l >= p && r >= p && (l + r) % (2 * p) == 0 && gap % 2 == 0)
{
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main()
{
fastio;
#ifdef LOCAL_OUTPUT
freopen(LOCAL_OUTPUT, "w", stdout);
#endif
#ifdef LOCAL_INPUT
freopen(LOCAL_INPUT, "r", stdin);
#endif
int __;
cin >> __;
while (__--)
_();
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}
const int N=200050;
int b[N];
int main(){
for(int t=ri();t--;){
int n=ri(),k=ri(),m=ri();
ra(b,m);b[m+1]=n+1;
int sum=0;
k/=2;
bool ok=0;
for(int i=1;i<=m+1;i++){
int sz=b[i]-b[i-1]-1;
sum+=sz;
}
int now=0;
for(int i=1;i<=m+1;i++){
int sz=b[i]-b[i-1]-1;
now+=sz;
if(now>=k&&sum-now>=k)ok=1;
}
//printf("mx:%i sum:%i\n",mx,sum);
if(sum%(2*k)!=0||!ok)printf("NO\n");
else printf("YES\n");
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define LD long double
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
int const MAXN = 2e6 + 9;
int a[MAXN];
int main(){
ios_base::sync_with_stdio (0),cin.tie(0);
int T;
cin >> T;
while(T--){
int n, k, m;
cin >> n >> k >> m;
for(int i = 1; i <= n; i++) a[i] = 0;
for(int i = 0; i < m; i++){
int x;
cin >> x;
a[x]++;
}
if((n - m) % (k - 1)) {
cout << "NO" << endl;
continue;
}
int sum = 0;
bool cond = false;
for(int i = 1; i <= n; i++){
sum += a[i] == 0;
if(sum >= k / 2 && (n - m - sum) >= k/2 && a[i]){
cond = true;
}
}
if(cond) cout << "YES" << endl;
else cout << "NO" << endl;
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define mk make_pair
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const ll linf = 0x3f3f3f3f3f3f3f3f;
#define deb(k) cerr << #k << ": " << k << "\n";
#define fastcin cin.tie(0)->sync_with_stdio(0);
#define st first
#define nd second
#define MAXN 1000100
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int v[200100];
int pref[200100];
int suf[200100];
int b[200100];
void solve(){
int n, m, k;
cin>>n>>k>>m;
for(int i=1;i<=n;i++) v[i] = 0;
for(int i=0;i<m;i++){
cin>>b[i];
v[b[i]]++;
}
if( (n-m)%(k-1) ){
cout<<"NO\n";
return;
}
pref[0] = 0;
suf[n+1] = 0;
for(int i=n;i>=1;i--){
if(!v[i]) suf[i] = suf[i+1]+1;
else suf[i] = suf[i+1];
}
for(int i=1;i<=n;i++){
if(!v[i]) pref[i] = pref[i-1]+1;
else pref[i] = pref[i-1];
}
int aux = k/2;
for(int i=0;i<m;i++){
if(pref[b[i]] >= aux && suf[b[i]] >= aux){
cout<<"YES\n";
return;
}
}
cout<<"NO\n";
}
int main(){
fastcin;
int t_;
cin>>t_;
while(t_--) solve();
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define prec fixed<<setprecision(9)
using namespace std;
int main()
{
fastIO
//lulz
int t; cin >> t;
while (t--) {
int n,k,m;
cin>>n>>k>>m;
vector<int> arr(m); for(int i=0;i<m;i++) {cin>>arr[i]; arr[i]--;}
vector<int> in(n,0); for(int &i : arr) in[i]=1;
int ct = n - m; int half = k/2;
bool works = false;
if (ct%(k-1)==0){
int f = 0,l = n-1;
ct = 0;
while(true){
if(in[f]==0) ct++;
if(ct==half) break;
f++;
}
ct = 0;
while(true){
if(in[l]==0) ct++;
if(ct==half) break;
l--;
}
for(int i=f+1;i<l;i++){
if(in[i]==1) works = true;
}
}
cout<<(works ? "YES" : "NO")<<endl;
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import sys
for _ in range(int(input())):
n,k,m=tuple(map(int,input().split(" ")))
ml=set(map(int,input().split(" ")))
havitada=[]
for i in range(1,n+1):
if i not in ml:
havitada.append(i)
saab=False
if len(havitada)%(k-1)!=0:
print("no")
continue
for i in range(k//2-1,len(havitada)-k//2):
if havitada[i+1]-havitada[i]!=1:
print("yes")
break
else:
print("no") | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
int b[200100];
int l[200100], r[200100];
int main(){
int T; scanf("%d", &T);
int tot = T;
int num = 0;
while (T--){
num++;
int N, K, M; scanf("%d%d%d", &N, &K, &M);
for (int i = 1; i <= M; i++) scanf("%d", &b[i]);
int ind = 1;
int cnt = 0;
for (int i = 1; i <= N; i++){
l[i] = 0;
if (ind == M+1 || b[ind] != i){
cnt++;
} else {
ind++;
l[i] = cnt;
}
}
if (cnt%(K-1) != 0){
printf("NO\n");
continue;
}
bool work = false;
for (int i = 1; i <= N; i++){
if (l[i] >= K/2 && cnt-l[i] >= K/2){
work = true;
break;
}
}
if (work) printf("YES\n");
else printf("NO\n");
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1
using LL = long long;
using ULL = unsigned long long;
using pii = pair<int,int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int N = 2e5 + 10;
int T, n, k, m, a[N];
bool vis[N];
bool ok(){
if((n - m) % (k - 1) != 0){
return 0;
}
if(m == n) return 1;
rep(i, 1, n+1) vis[i] = 0;
rep(i, 0, m){
vis[a[i]] = 1;
}
int d = (k - 1) / 2;
int lft, rgt;
int c = 0;
rep(i, 1, n+1){
if(!vis[i]){
lft = i;
++c;
if(c == d) break;
}
}
c = 0;
per(i, n, 0){
if(!vis[i]){
rgt = i;
++c;
if(c == d) break;
}
}
rep(i, lft+1, rgt){
if(vis[i]) return 1;
}
return 0;
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%d %d %d", &n, &k, &m);
rep(i, 0, m){
scanf("%d", a+i);
}
puts(ok()?"YES":"NO");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
int t,n,m,k;
bool d[200500];
int main() {
cin>>t;
while (t--) {
cin>>n>>k>>m; k=(k-1)/2;
for (int i=1; i<=n; ++i) d[i]=1;
for (int i=0; i<m; ++i) {
int t; scanf("%d",&t);
d[t]=0;
}
if ((n-m)%(2*k)) {
cout<<"NO\n"; continue;
}
for (int i=1,t=0; i<=n; ++i)
if (d[i]) ++t;
else if (t>=k&&n-m-t>=k) {
cout<<"YES\n";
goto lend;
}
cout<<"NO\n";
lend:;
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;
void array_show(int *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void init(){
}
bool solve(){
int n,m,p;
int i,j,k;
int a,b,c;
vector<int> v1;
cin>>n>>p>>m;
for(i=0;i<m;i++){
cin>>a;
v1.push_back(a);
}
if((n-m)%(p-1))return false;
for(i=0;i<m;i++){
a=v1[i]-1-i;
b=n-v1[i]-(m-1-i);
if(a>=p/2 && b>=p/2)return true;
}
return false;
}
int main(){
int n,i;
init();
cin>>n;
for(i=0;i<n;i++){
if(solve())cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=2e9;//infi??,????inf????int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
inline int qi(){
int f=0,fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline ll ql(){
ll f=0;int fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline db qd(){
char c=getchar();int flag=1;double ans=0;
while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
if(c=='-') flag=-1,c=getchar();
while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
return ans*flag;
}
}
namespace Read{
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define rai(x,a,b) repi(i,a,b) x[i]=qi()
#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
#define pi(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)
#define pc(x) printf("%c",x)
#define pe() puts("")
}
namespace DeBug{
#define MARK false
#define DB if(MARK)
#define pr(x) cout<<#x<<": "<<x<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
const int MAX_N=2e5+5;
int b[MAX_N];
bool vis[MAX_N];
int main()
{
int T=qi();
while(T--)
{
int n=qi(),k=qi(),m=qi(); rai(b,1,m);
if(n==m){ puts("YES"); continue; }
if((n-m)%(k-1)){ puts("NO"); continue; }
repi(i,1,n) vis[i]=false;
repi(i,1,m) vis[b[i]]=true;
int half=k/2,pre=0,suf=n-m;
bool flag=false;
repi(i,1,n){
if(!vis[i]) pre++,suf--;
else if(pre>=half&&suf>=half){ flag=true; break; }
}
puts(flag?"YES":"NO");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
int T;
std::cin>>T;
for(; T; -- T) {
int N, K, M;
std::cin >> N >> K >> M;
std::vector<bool> is(N + 1, false);
for (int i = 1; i <= M; ++ i) {
int x;
std::cin >> x;
is[x] = true;
}
if (N == M) {
std::cout << "YES" << '\n';
continue;
}
if ((N - M) % (K - 1)) {
std::cout << "NO" << '\n';
continue;
}
bool f = false;
int cnt = 0;
for (int i = 1; i <= N; ++ i) {
if (!is[i]) {
++ cnt;
}
else {
if (cnt >= K / 2 && (N - M) - cnt >= K / 2) {
f = true;
}
}
}
std::cout << (f ? "YES" : "NO") << '\n';
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
double pi = acos(-1);
#define tezi ios_base::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORR(i,b,a) for(int i=b;i>=a;i--)
#define fill(a,b) memset(a,b,sizeof(a))
#define _time_ 1.0 * clock() / CLOCKS_PER_SEC
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(),a.end()
#define rev(a) reverse(all(a))
#define reva(a,n) reverse(a,a+n)
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll powerm(ll a,ll b,ll m){
if(b==0) return 1;
if(b&1) return a*powerm(a*a%m,b/2,m)%m;
return powerm(a*a%m,b/2,m);
}
ll power(ll a,ll b){
if(b==0) return 1;
if(b&1) return a*power(a*a,b/2);
return power(a*a,b/2);
}
ll sqroot(ll number){
ll x = sqrt(number);
while(x*x<number)x++;
while(x*x>number)x--;
return x;
}
ll cbroot(ll number){
ll x = cbrt(number);
while(x*x*x<number)x++;
while(x*x*x>number)x--;
return x;
}
const int maxn=2e5+5;
//bool prime[maxn];
ll b[maxn];
int main(){
tezi
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//sieve
//memset(prime,true,sizeof(prime));
//for(ll p=2;p*p<maxn;p++)if(prime[p]==true)for(ll i=p*p;i<maxn;i+=p)prime[i]=false;
int t;
cin >> t;
while(t--){
int n,m,k;
cin >> n >> k >> m;
int curr=0;
for(int i=1;i<=m;i++){
cin >> b[i];
}
if((n-m)%(k-1)){
cout << "NO\n";
continue;
}
if(n==m){
cout << "YES\n";
continue;
}
bool ok=0;
for(int i=1;i<=m;i++){
int a = b[i]-i;
int c = n - m - a;
if(a/(k-1)>0 && c/(k-1)>0){
ok=1;
break;
}else{
if(a==0 || c==0)
continue;
if(a/(k-1)==0 && c/(k-1)>0){
a%=(k-1);
c%=(k-1);
if(a>=c){
ok=1;
break;
}
}else if(a/(k-1)>0 && c/(k-1)==0){
a%=(k-1);
c%=(k-1);
if(c>=a){
ok=1;
break;
}
}else{
if(a==c){
ok=1;
break;
}
}
}
}
if(ok){
cout << "YES\n";
}else{
cout << "NO\n";
}
}
return 0;
}
/*
###### ####### # # ### # # # ####### ####### ######
# # ###### # # # # ## ## # ## # # # # # # # #
# # # ## # # # # # # # # # # # # # # # # # #
# # ##### # # # # # # # # # # # # # # # # # ######
# # # # # # # # # # # # # # ####### # # # # #
# # # # ## # # # # # # ## # # # # # # #
###### ###### # # ####### # # ### # # # # # ####### # #
*/ | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
/*<DEBUG>*/
#define tem template <typename
#define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i)
#define _op debug& operator<<
tem C > auto test(C *x) -> decltype(cerr << *x, 0LL);
tem C > char test(...);
tem C > struct itr{C begin, end; };
tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; }
struct debug{
#ifdef _LOCAL
~debug(){ cerr << endl; }
tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; }
tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); }
tem T, typename U > _op (pair<T, U> i){
return *this << "< " << i.first << " , " << i.second << " >"; }
tem T> _op (itr<T> i){
*this << "{ ";
for(auto it = i.begin; it != i.end; it++){
*this << " , " + (it==i.begin?2:0) << *it;
}
return *this << " }";
}
#else
tem T> _op (const T&) { return *this; }
#endif
};
string _ARR_(int* arr, int sz){
string ret = "{ " + to_string(arr[0]);
for(int i = 1; i < sz; i++) ret += " , " + to_string(arr[i]);
ret += " }"; return ret;
}
#define exp(...) " [ " << #__VA_ARGS__ << " : " << (__VA_ARGS__) << " ]"
/*</DEBUG>*/
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array
const int INF = 1e9 + 7, N = 2e5;
int n, k, m;
bool a[N];
int l[N], r[N];
int solve(){
cin >> n >> k >> m;
for(int i = 0; i < n; ++i) a[i] = 0;
for(int i = 0; i < m; ++i){
int x; cin >> x;
--x;
a[x] = 1;
}
if((n-m) % (k-1) != 0) return 0;
l[0] = !a[0];
for(int i = 1; i < n; ++i) l[i] = l[i-1] + !a[i];
r[n-1] = !a[n-1];
for(int i = n-2; i >= 0; --i) r[i] = r[i+1] + !a[i];
for(int i = 0; i < n; ++i){
int r1 = (l[i] - (k/2))%(k-1);
int r2 = (r[i] - (k/2))%(k-1);
if(a[i] && l[i] >= (k/2) && r[i] >= (k/2) && (r1+r2 == 0 || r1+r2 == k-1)) return 1;
}
return 0;
}
int main(void)
{
FAST;
TC{
cout << (solve() ? "YES\n" : "NO\n");
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
def __init__(self, file):
self.newlines = 0
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l): print(' '.join(map(str, l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
from math import ceil
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
for _ in range(N()):
n, k, m = RL()
a = RLL()
k >>= 1
if k == 0 or (n - m) % (2 * k):
print('NO')
else:
t = n - m
now = a[0] - 1
flag = False
a.append(n + 1)
for i in range(1, m + 1):
if k <= now <= t - k:
flag = True
break
now += a[i] - a[i - 1] - 1
print('YES' if flag else 'NO') | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, k, m, b[N];
int main() {
int t;
scanf("%d", &t);
while (t){
t--;
scanf("%d%d%d", &n, &k, &m);
for (int i=0; i<m; i++) scanf("%d", b+i);
if ((n-m)%(k-1)) printf("NO\n");
else {
bool ok = 0;
for (int i=0; i<m; i++){
int x=b[i]-i-1 , y=(n-b[i])-(m-i-1) ;
if (x>=k/2 && y>=k/2){
ok=1;
break;
}
}
if (ok) printf("YES\n");
else printf("NO\n");
}
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.util.*;
import java.io.*;
public class tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n;
static ArrayList<int[]>[] ad, ad2;
static long[][] memo;
static boolean vis[];
static long[] f, inv, ncr[];
static HashMap<Integer, Integer> hm;
static char[][] g;
static int[] a, pre, suf, Smax[], Smin[];
static int idmax, idmin;
static TreeSet<Long> ts;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int m = sc.nextInt();
int[] b = sc.nextArrInt(m);
int len = n - m;
if (len % (k - 1) != 0) {
out.println("NO");
continue;
}
boolean f = false;
TreeSet<Integer> hs = new TreeSet<>();
for (int i = 1; i <= n; i++)
hs.add(i);
for (int i = 0; i < m; i++)
hs.remove(b[i]);
int[] arr = new int[hs.size()];
for (int i = 0; i < arr.length; i++)
arr[i] = hs.pollFirst();
for (int i = 0; i < m; i++) {
int mk = (k - 1) / 2;
int lo = 0;
int hi = arr.length - 1;
int last = 0;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (b[i] > arr[mid]) {
last = mid + 1;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
int end = arr.length - last;
if (last >= mk && end >= mk)
f = true;
}
if (f)
out.println("YES");
else
out.println("NO");
}
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | from sys import stdin, stdout
# if erased left elements >= (k-1)/2
# and erased right elements >= (k-1)/2
# and (n-m)%(k-1) == 0
# then YES
# Prove:
# set d = (k-1)/2
# left elements: d + x
# right elements: d + y
# ------------------------------------------
# if x + y >= k, set x + y = x + y - n*(k-1)
# then x + y < k
# ------------------------------------------
# because (2d + x + y) % (k-1) == 0,
# so x + y = k - 1
# ------------------------------------------
# d x b d y
# => d d-z b d d+z
# => d (d-z) b (z) d [d]
#
def k_and_medians(n, k, m, b_a):
if (n-m) % (k-1) != 0:
return False
b_s = set(b_a)
l_a = [0] * (n+1)
for i in range(1, n+1):
l_a[i] = l_a[i - 1]
if i not in b_s:
l_a[i] = l_a[i-1] + 1
r = 0
for i in range(n, 0, -1):
if i not in b_s:
r += 1
elif r >= (k-1) // 2 and l_a[i] >= (k-1) // 2:
return True
return False
t = int(stdin.readline())
for _ in range(t):
n, k, m = map(int, stdin.readline().split())
b_a = list(map(int, stdin.readline().split()))
r = k_and_medians(n, k, m, b_a)
if r:
stdout.write('YES\n')
else:
stdout.write('NO\n')
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sparsh Sanchorawala
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
HKAndMedians solver = new HKAndMedians();
solver.solve(1, in, out);
out.close();
}
static class HKAndMedians {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt(), k = s.nextInt(), m = s.nextInt();
int[] a = new int[n];
for (int i = 0; i < m; i++)
a[s.nextInt() - 1] = 1;
if ((n - m) % (k - 1) != 0) {
w.println("NO");
continue;
}
boolean res = false;
int v = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
if (v >= (k - 1) / 2 && n - m - v >= (k - 1) / 2)
res = true;
} else
v++;
}
if (res)
w.println("YES");
else
w.println("NO");
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| JAVA |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define x1 x1228
#define y1 y1228
#define left left228
#define right right228
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << endl;
#define TIME (ld)clock()/CLOCKS_PER_SEC
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 3e5 + 7, mod = 1e9 + 7, MAXN = 2e6 + 7;
const double eps = 1e-9;
const ll INF = 1e18, inf = 1e15;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n, k, m;
int b[maxn];
bool check(vector<int> &ptr) {
for (int i = 0; i < m; ++i) {
int x = lower_bound(all(ptr), b[i]) - ptr.begin();
if (x == k / 2) return true;
}
return false;
}
void solve() {
cin >> n >> k >> m;
for (int i = 0; i < m; ++i) cin >> b[i];
if ((n - m) % (k - 1)) {
cout << "No\n";
return;
}
vector<int> ptr;
int lst = 1;
for (int i = 0; i < m; ++i) {
for (int j = lst; j < b[i]; ++j) ptr.pb(j);
lst = b[i] + 1;
}
for (int i = lst; i <= n; ++i) ptr.pb(i);
int mid = k / 2;
vector<int> tet;
if (ptr.size() == k - 1) {
tet = ptr;
if (check(tet)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
return;
}
for (int i = 0; i < mid; ++i) {
tet.pb(ptr[i]);
}
for (int i = mid; i >= 0; --i) {
if (i == mid - 1) continue;
tet.pb(ptr[(int)ptr.size() - i - 1]);
}
if (check(tet)) {
cout << "Yes\n";
return;
}
tet.clear();
for (int i = 0; i <= mid; ++i) {
if (i == mid - 1) continue;
tet.pb(ptr[i]);
}
for (int i = mid - 1; i >= 0; --i) {
tet.pb(ptr[(int)ptr.size() - i - 1]);
}
if (check(tet)) {
cout << "Yes\n";
return;
}
cout << "No\n";
}
signed main() {
#ifdef LOCAL
freopen("TASK.in", "r", stdin);
freopen("TASK.out", "w", stdout);
#else
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
cout << fixed;
int t = 1; cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
allAns=[]
t=int(input())
for _ in range(t):
n,k,m=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
b.append(n+1)
ans='NO'
if (n-m)%(k-1)==0: #valid number of missing numbers
leftMissing=0
rightMissing=n-m
prev=0
for x in b:
leftMissing+=x-prev-1
rightMissing-=(x-prev-1)
prev=x
if leftMissing>=(k-1)//2 and rightMissing>=(k-1)//2:
ans='YES'
break
# print('leftMissing:{} rightMissing:{}'.format(leftMissing,rightMissing))
allAns.append(ans)
multiLineArrayPrint(allAns) | PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #ifdef Prateek
#include "Prateek.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif
#define F first
#define S second
#define pb push_back
#define int ll
#define f(i,x,n) for(int i=x;i<n;i++)
#define all(c) c.begin(),c.end()
using ll = long long;
const int MOD = 1e9+7, N = 1e5 + 10;
vector<int> a;
int k;
void test(){
int n, m;
cin >> n >> k >> m;
vector<bool> chk(n + 1);
vector<int> b(m + 1);
for (int i = 1; i <= m; ++i) {
cin >> b[i];
chk[b[i]] = true;
}
a.clear();
for (int i = 1; i <= n; ++i) {
if (!chk[i]) {
a.push_back(i);
}
}
int r = n - m;
if (r % (k - 1) != 0) {
cout << "NO\n";
return;
}
vector<int> A;
int M = (k - 1) / 2;
int lst;
for (int i = 0; i < M; ++i) {
A.push_back(a[i]);
lst = a[i];
}
int fst = 1e9;
for (int i = r - M; i < r; ++i) {
fst = min(fst, a[i]);
A.push_back(a[i]);
}
for (int i = lst; i <= fst; ++i) {
if (chk[i]) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
return;
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
cin >> tt;
f(i,0,tt) test();
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const int N = 5e5 + 7;
const long long mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve()
{
int n,k,m;
cin >> n >> k >> m;
vector < int > a(m + 1 , 0);
for( int i = 1; i <= m; i++ ){
cin >> a[i];
}
if( (n - m) % (k - 1) ){
cout << "NO\n";
return;
}
int x = n - m;
int y = 0;
for( int i = 1 , j = 1; i <= n; i++ ){
if( j <= m && a[j] == i ){
if( x >= k / 2 && y >= k / 2 ){
cout << "YES\n";
return;
}
j++;
}else{
y++,x--;
}
}
cout << "NO\n";
}
int main()
{
ios_base::sync_with_stdio(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int T;
cin >> T;
while( T-- ){
solve();
}
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include<cstdio>
#include<algorithm>
using namespace std;
int b[200005];
void slove(){
int n,k,m;
scanf("%d%d%d",&n,&k,&m);
for(int i=1;i<=m;i++){
scanf("%d",&b[i]);
}
if((n-m)%(k-1)!=0){
printf("NO\n");
return ;
}else{
for(int i=1;i<=m;i++){
if(b[i]-i>=k/2&&n-b[i]-m+i>=k/2){
printf("YES\n");
return ;
}
}
printf("NO\n");
return ;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
slove();
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #include <bits/stdc++.h>
using namespace std;
int a[200010];
int main()
{
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--)
{
int n,k,m;
cin>>n>>k>>m;
for(int i=1;i<=n;i++) a[i]=1;
for(int x,i=1;i<=m;i++) cin>>x,a[x]=0;
if((n-m)%(k-1)!=0)
{
cout<<"NO"<<endl;
continue;
}
int cnt1=0,cnt2=n-m,flag=0;
for(int i=1;i<=n;i++)
{
if(a[i]==0)
{
if(cnt1>=(k>>1) && cnt2>=(k>>1))
{
flag=1;
break;
}
}
if(a[i]==1)
{
cnt1++;
cnt2--;
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
| CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | from sys import stdin
t = int(stdin.readline())
for _ in range(t):
L = [int(x) for x in stdin.readline().split(" ")]
n, k, m = L[0], L[1], L[2]
A = [int(x) for x in stdin.readline().split(" ")]
if (n - m) % (k - 1) != 0:
print("NO")
continue
works = False
k = (k - 1) // 2
count = 0
for i in range(m):
a = A[i]
if a - i - 1 >= k and n - a - (m - 1 - i) >= k:
works = True
break
if works:
print("YES")
else:
print("NO")
| PYTHON3 |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | #define f first
#define s second
#define loop(a,b) for(int i=a;i<b;i++)
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define ll long long
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define loop2(a,b) for(int j=a;j<b;j++)
#define str_in cin.ignore(numeric_limits<streamsize>::max(),'\n')
#define sz(a) int(a.size())
#define displaymatrix(a,m,n) for(int i=0;i<m;i++) {for(int j=0;j<n;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define printarray(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}nl;
#define vinput(a,n) vl a(n);loop(0,n)cin>>a[i]
#define ainput(a,n) loop(0,n)cin>>a[i]
#define nl cout << "\n";
//#define all(c) c.begin(),c.end()
#define tr(container,it)\
for(__typeof(container.begin()) it=container.begin();it!=container.end();it++)
#include<bits/stdc++.h>
using namespace std;
const int mod=1e+9+7;
void solve(){
int n,k,m;cin>>n>>k>>m;
vi b(m);
loop(0,m) cin>>b[i];
int less, more;
if ((n-m) % (k-1) != 0){
cout << "no\n";
return;
}
loop(0,m){
less = b[i] - i - 1;
more = n - m - less;
if (less >= (k-1)/2 && more >= (k-1)/2){
cout << "Yes\n";
return;
}
}
cout << "No\n";
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
cin>>t;
while(t--) solve();
return 0;
} | CPP |
1468_H. K and Medians | Let's denote the median of a sequence s with odd length as the value in the middle of s if we sort s in non-decreasing order. For example, let s = [1, 2, 5, 7, 2, 3, 12]. After sorting, we get sequence [1, 2, 2, \underline{3}, 5, 7, 12], and the median is equal to 3.
You have a sequence of n integers [1, 2, ..., n] and an odd integer k.
In one step, you choose any k elements from the sequence and erase all chosen elements except their median. These elements do not have to go continuously (gaps are allowed between them).
For example, if you have a sequence [1, 2, 3, 4, 5, 6, 7] (i.e. n=7) and k = 3, then the following options for the first step are possible:
* choose [1, \underline{2}, 3]; 2 is their median, so it is not erased, and the resulting sequence is [2, 4, 5, 6, 7];
* choose [2, \underline{4}, 6]; 4 is their median, so it is not erased, and the resulting sequence is [1, 3, 4, 5, 7];
* choose [1, \underline{6}, 7]; 6 is their median, so it is not erased, and the resulting sequence is [2, 3, 4, 5, 6];
* and several others.
You can do zero or more steps. Can you get a sequence b_1, b_2, ..., b_m after several steps?
You'll be given t test cases. Solve each test case independently.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains three integers n, k, and m (3 ≤ n ≤ 2 ⋅ 10^5; 3 ≤ k ≤ n; k is odd; 1 ≤ m < n) — the length of the sequence you have, the number of elements you choose in each step and the length of the sequence you'd like to get.
The second line of each test case contains m integers b_1, b_2, ..., b_m (1 ≤ b_1 < b_2 < ... < b_m ≤ n) — the sequence you'd like to get, given in the ascending order.
It's guaranteed that the total sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
Output
For each test case, print YES if you can obtain the sequence b or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
Input
4
3 3 1
1
7 3 3
1 5 7
10 5 3
4 5 6
13 7 7
1 3 5 7 9 11 12
Output
NO
YES
NO
YES
Note
In the first test case, you have sequence [1, 2, 3]. Since k = 3 you have only one way to choose k elements — it's to choose all elements [1, \underline{2}, 3] with median 2. That's why after erasing all chosen elements except its median you'll get sequence [2]. In other words, there is no way to get sequence b = [1] as the result.
In the second test case, you have sequence [1, 2, 3, 4, 5, 6, 7] and one of the optimal strategies is following:
1. choose k = 3 elements [2, \underline{3}, 4] and erase them except its median; you'll get sequence [1, 3, 5, 6, 7];
2. choose 3 elements [3, \underline{5}, 6] and erase them except its median; you'll get desired sequence [1, 5, 7];
In the fourth test case, you have sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]. You can choose k=7 elements [2, 4, 6, \underline{7}, 8, 10, 13] and erase them except its median to get sequence b. | 2 | 14 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.nio.CharBuffer;
import java.io.IOException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.io.UncheckedIOException;
import java.util.Objects;
import java.nio.charset.Charset;
import java.io.Writer;
import java.util.NoSuchElementException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author mikit
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
LightScanner2 in = new LightScanner2(inputStream);
LightWriter2 out = new LightWriter2(outputStream);
HKAndMedians solver = new HKAndMedians();
solver.solve(1, in, out);
out.close();
}
static class HKAndMedians {
public void solve(int testNumber, LightScanner2 in, LightWriter2 out) {
out.setBoolLabel(LightWriter2.BoolLabel.YES_NO_ALL_UP);
int testCases = in.ints();
outer:
for (int testCase = 1; testCase <= testCases; testCase++) {
int n = in.ints(), k = in.ints(), m = in.ints();
int[] b = in.ints(m);
for (int i = 0; i < m; i++) b[i]--;
if ((n - m) % (k - 1) != 0) {
out.noln();
continue outer;
}
int bsize = (k - 1) / 2;
int ops = (n - m) / (k - 1);
int[] l = new int[ops * 2], r = new int[ops * 2];
int[] a = new int[n];
for (int i = 0; i < m; i++) a[b[i]] = -1;
int p = 0;
for (int i = 0; i < n; i++) {
if (a[i] == -1) continue;
a[i] = p++;
if (a[i] % bsize == 0) l[a[i] / bsize] = i;
if ((a[i] + 1) % bsize == 0) r[a[i] / bsize] = i;
}
int leftmostRight = r[0], rightmostLeft = l[ops * 2 - 1];
boolean ok = false;
for (int i = 0; i < m; i++) {
if (leftmostRight < b[i] && b[i] < rightmostLeft) ok = true;
}
out.ans(ok).ln();
}
}
}
static class LightScanner2 extends LightScannerAdapter {
private static final int BUF_SIZE = 16 * 1024;
private final InputStream stream;
private final StringBuilder builder = new StringBuilder();
private final byte[] buf = new byte[BUF_SIZE];
private int ptr;
private int len;
public LightScanner2(InputStream stream) {
this.stream = stream;
}
private int read() {
if (ptr < len) return buf[ptr++];
reload();
if (len == -1) return -1;
return buf[ptr++];
}
private void reload() {
try {
ptr = 0;
len = stream.read(buf);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
private void load(int n) {
if (ptr + n <= len) return;
System.arraycopy(buf, ptr, buf, 0, len - ptr);
len -= ptr;
ptr = 0;
try {
int r = stream.read(buf, len, BUF_SIZE - len);
if (r == -1) return;
len += r;
if (len != BUF_SIZE) buf[len] = '\n';
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
private void skip() {
while (len != -1) {
while (ptr < len && isTokenSeparator(buf[ptr])) ptr++;
if (ptr < len) return;
reload();
}
throw new NoSuchElementException("EOF");
}
private void loadToken() {
builder.setLength(0);
skip();
for (int b = read(); !isTokenSeparator(b); b = read()) {
builder.appendCodePoint(b);
}
}
public String string() {
loadToken();
return builder.toString();
}
public int ints() {
skip();
load(12);
int b = buf[ptr++];
boolean negate;
if (b == '-') {
negate = true;
b = buf[ptr++];
} else negate = false;
int x = 0;
for (; !isTokenSeparator(b); b = buf[ptr++]) {
if ('0' <= b && b <= '9') x = x * 10 + b - '0';
else throw new NumberFormatException("Unexpected character '" + ((char) b) + "'");
}
return negate ? -x : x;
}
public void close() {
try {
stream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static boolean isTokenSeparator(int b) {
return b < 33 || 126 < b;
}
}
static class LightWriter2 implements AutoCloseable {
private static final int BUF_SIZE = 16 * 1024;
private static final int BUF_THRESHOLD = 1024;
private final OutputStream out;
private final byte[] buf = new byte[BUF_SIZE];
private int ptr;
private boolean autoflush = false;
private boolean breaked = true;
private LightWriter2.BoolLabel boolLabel = LightWriter2.BoolLabel.YES_NO_FIRST_UP;
public LightWriter2(OutputStream out) {
this.out = out;
}
public LightWriter2(Writer out) {
this.out = new LightWriter2.WriterOutputStream(out);
}
public void setBoolLabel(LightWriter2.BoolLabel label) {
this.boolLabel = Objects.requireNonNull(label);
}
private void allocate(int n) {
if (ptr + n <= BUF_SIZE) return;
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
if (BUF_SIZE < n) throw new IllegalArgumentException("Internal buffer exceeded");
}
public void close() {
try {
out.write(buf, 0, ptr);
ptr = 0;
out.flush();
out.close();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
public LightWriter2 print(char c) {
allocate(1);
buf[ptr++] = (byte) c;
breaked = false;
return this;
}
public LightWriter2 print(String s) {
byte[] bytes = s.getBytes();
int n = bytes.length;
if (n <= BUF_THRESHOLD) {
allocate(n);
System.arraycopy(bytes, 0, buf, ptr, n);
ptr += n;
return this;
}
try {
out.write(buf, 0, ptr);
ptr = 0;
out.write(bytes);
out.flush();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return this;
}
public LightWriter2 ans(String s) {
if (!breaked) print(' ');
breaked = false;
return print(s);
}
public LightWriter2 ans(boolean b) {
return ans(boolLabel.transfer(b));
}
public LightWriter2 noln() {
return ans(false).ln();
}
public LightWriter2 ln() {
allocate(1);
buf[ptr++] = '\n';
breaked = true;
if (autoflush) {
try {
out.flush();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
return this;
}
public enum BoolLabel {
YES_NO_FIRST_UP("Yes", "No"),
YES_NO_ALL_UP("YES", "NO"),
YES_NO_ALL_DOWN("yes", "no"),
Y_N_ALL_UP("Y", "N"),
POSSIBLE_IMPOSSIBLE_FIRST_UP("Possible", "Impossible"),
POSSIBLE_IMPOSSIBLE_ALL_UP("POSSIBLE", "IMPOSSIBLE"),
POSSIBLE_IMPOSSIBLE_ALL_DOWN("possible", "impossible"),
FIRST_SECOND_FIRST_UP("First", "Second"),
FIRST_SECOND_ALL_UP("FIRST", "SECOND"),
FIRST_SECOND_ALL_DOWN("first", "second"),
ALICE_BOB_FIRST_UP("Alice", "Bob"),
ALICE_BOB_ALL_UP("ALICE", "BOB"),
ALICE_BOB_ALL_DOWN("alice", "bob"),
;
private final String positive;
private final String negative;
BoolLabel(String positive, String negative) {
this.positive = positive;
this.negative = negative;
}
private String transfer(boolean f) {
return f ? positive : negative;
}
}
private static class WriterOutputStream extends OutputStream {
final Writer writer;
final CharsetDecoder decoder;
WriterOutputStream(Writer writer) {
this.writer = writer;
this.decoder = StandardCharsets.UTF_8.newDecoder();
}
public void write(int b) throws IOException {
writer.write(b);
}
public void write(byte[] b) throws IOException {
writer.write(decoder.decode(ByteBuffer.wrap(b)).array());
}
public void write(byte[] b, int off, int len) throws IOException {
writer.write(decoder.decode(ByteBuffer.wrap(b, off, len)).array());
}
public void flush() throws IOException {
writer.flush();
}
public void close() throws IOException {
writer.close();
}
}
}
static abstract class LightScannerAdapter implements AutoCloseable {
public abstract String string();
public int ints() {
return Integer.parseInt(string());
}
public final int[] ints(int length) {
int[] res = new int[length];
Arrays.setAll(res, ignored -> ints());
return res;
}
public abstract void close();
}
}
| JAVA |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 | #include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define lb printf("\n");
#define INF 1000000000000000000
#define LES -1000000000000000000
using namespace std;
//---------------All Solution Functions Start Here--------------//
int t, h, m, hh, mm;
int a[10] = {0, 1, 5, -1, -1, 2, -1, -1, 8, -1};
bool check(int nh, int nm)
{
if (a[nh / 10] == -1 || a[nh % 10] == -1 || a[nm / 10] == -1 || a[nm % 10] == -1){
return false;
}
int ih = a[nm % 10] * 10 + a[nm / 10], im = a[nh % 10] * 10 + a[nh / 10];
return ih < h && im < m;
}
void solve()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &h, &m);
scanf("%d:%d", &hh, &mm);
int nh = hh, nm = mm;
while (nh != 0 || nm != 0)
{
if (check(nh, nm)){
break;
}
if (nm == m - 1){
nh = (nh + 1) % h;
}
nm = (nm + 1) % m;
}
printf("%d%d:%d%d\n", nh / 10, nh % 10, nm / 10, nm % 10);
}
}
//---------------All Solution Functions End Here---------------//
void file()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main()
{
file();
solve();
return 0;
}
| CPP |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 | import sys,functools,collections,bisect,math,heapq
input = sys.stdin.readline
#print = sys.stdout.write
mirror = {0:0,1:1,2:5,5:2,8:8}
@functools.lru_cache(None)
def fun(i):
ones = i%10
i //= 10
tens = i%10
if ones in mirror:
newten = mirror[ones]
else:
return None
if tens in mirror:
newone = mirror[tens]
else:
return None
return str(newten)+str(newone)
t = int(input())
for _ in range(t):
h,m = map(int,input().strip().split())
hour,minute = map(int,input().strip().split(':'))
while True:
#print(hour,minute)
currhour = fun(minute)
currminute = fun(hour)
#print(currhour,currminute)
if currhour and currminute and int(currhour) < h and int(currminute) < m:
print(str(hour).zfill(2)+':'+str(minute).zfill(2))
break
minute += 1
if minute >= m:
minute = 0
hour += 1
if hour >= h:
hour = 0
minute = 0 | PYTHON3 |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 |
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Round705 {
static boolean check(int h,int min,int ch,int cm,HashMap<Integer, Integer>map) {
String hr=ch+"";
String mn=cm+"";
if(hr.length()==1) {
hr="0"+hr;
}
if(mn.length()==1) {
mn="0"+mn;
}
StringBuffer chr=new StringBuffer(hr+mn);
StringBuffer ans=new StringBuffer("");
for(int i=0;i<chr.length();i++) {
ans=ans.append(map.get(Character.getNumericValue(chr.charAt(i))));
}
ans.reverse();
int first=Integer.parseInt(ans.substring(0, 2));
int second=Integer.parseInt(ans.substring(2,4));
if(first<h&&second<min) {
return true;
}
return false;
}
public static void main(String[] args) {
Scanner o=new Scanner(System.in);
int t=o.nextInt();
while(t>0) {
int h=o.nextInt();
int m=o.nextInt();
String st=o.next();
int ch=Integer.parseInt(st.substring(0, 2));
int cm=Integer.parseInt(st.substring(3,5));
HashMap<Integer,Integer>map=new HashMap<>();
map.put(0, 0);
map.put(1, 1);
map.put(2, 5);
map.put(5, 2);
map.put(8, 8);
boolean ans=false;
int hours=0;
int minutes=0;
for(int i=ch;i<h;i++) {
for(int j=cm;j<m;j++) {
String ckm=j+"";
String chh=i+"";
if(ckm.length()==2) {
if(chh.length()==2) {
if(map.containsKey(Character.getNumericValue(ckm.charAt(0)))&&map.containsKey(Character.getNumericValue(ckm.charAt(1)))&&map.containsKey(Character.getNumericValue(chh.charAt(0)))&&map.containsKey(Character.getNumericValue(chh.charAt(1)))) {
if(check(h,m,i,j,map)) {
ans=true;
hours=i;
minutes=j;
break;
}
}
}
else {
if(map.containsKey(Character.getNumericValue(ckm.charAt(0)))&&map.containsKey(Character.getNumericValue(ckm.charAt(1)))&&map.containsKey(Character.getNumericValue(chh.charAt(0)))) {
if(check(h,m,i,j,map)) {
ans=true;
hours=i;
minutes=j;
break;
}
}
}
}
else {
if(chh.length()==2) {
if(map.containsKey(Character.getNumericValue(ckm.charAt(0)))&&map.containsKey(Character.getNumericValue(chh.charAt(0)))&&map.containsKey(Character.getNumericValue(chh.charAt(1)))) {
if(check(h,m,i,j,map)) {
ans=true;
hours=i;
minutes=j;
break;
}
}
}
else {
if(map.containsKey(Character.getNumericValue(ckm.charAt(0)))&&map.containsKey(Character.getNumericValue(chh.charAt(0)))) {
if(check(h,m, i,j,map)) {
ans=true;
hours=i;
minutes=j;
break;
}
}
}
}
}
if(ans) {
break;
}
cm=0;
}
if(!ans) {
System.out.println("00:00");
}
else {
String hrs=hours+"";
if(hrs.length()==1)
hrs=0+hrs;
String mns=minutes+"";
if(mns.length()==1)
mns=0+mns;
System.out.println(hrs+":"+mns);
}
t--;
}
}
}
| JAVA |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 |
import java.util.HashMap;
import java.util.Scanner;
public class B {
static HashMap<Character, Character> mirrorMap;
static {
mirrorMap = new HashMap<>();
mirrorMap.put('0', '0');
mirrorMap.put('1', '1');
mirrorMap.put('2', '5');
mirrorMap.put('5', '2');
mirrorMap.put('8', '8');
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while (T-- > 0) {
int h = in.nextInt(), m = in.nextInt();
String[] time = in.next().split(":");
int hours = Integer.parseInt(time[0]), minutes = Integer.parseInt(time[1]);
Time currentTime = new Time(hours, minutes, h, m);
while (!isValidReflectedTime(currentTime)) { // Would always terminate at 00:00 next day
currentTime.tick();
}
System.out.println(currentTime);
}
}
public static boolean isValidReflectedTime(Time time) {
return isValidMirrored(time.minute, time.maxHour) && isValidMirrored(time.hour, time.maxMinute);
}
private static boolean isValidMirrored(int num, int maxMirroredTime) {
String str = toTime(num);
if (!mirrorMap.containsKey(str.charAt(0)) || !mirrorMap.containsKey(str.charAt(1))) {
return false;
}
String mirrored = "" + mirrorMap.get(str.charAt(1)) + mirrorMap.get(str.charAt(0));
int mirroredNumber = Integer.parseInt(mirrored);
return mirroredNumber < maxMirroredTime;
}
private static String toTime(int num) {
return (num < 10) ? "0"+num : ""+num;
}
static class Time {
private int hour;
private int minute;
private int maxHour;
private int maxMinute;
public Time(int hour, int minute, int maxHour, int maxMinute) {
this.hour = hour;
this.minute = minute;
this.maxHour = maxHour;
this.maxMinute = maxMinute;
}
public void tick() {
minute = (minute + 1) % maxMinute;
if (minute == 0) {
hour = (hour + 1) % maxHour;
}
}
@Override
public String toString() {
return makeTwoDigits(hour) + ":" + makeTwoDigits(minute);
}
private String makeTwoDigits(int num) {
return (num < 10) ? "0"+num : ""+num;
}
}
}
| JAVA |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 | #include <bits/stdc++.h>
#define MAX_N 300001
#define MAX_M 10001
#define MAX_C 10001
#define MAX 500
#define pii pair<int, int>
#define pdi pair<double, int>
#define pid pair<int, double>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define INF 987654321
#define vi vector<int>
#define sq(x) ((x) * (x))
#define FOR(i, n) for(int i=0; i < (n) ; ++i)
typedef long long ll;
using namespace std;
int T;
int h, m;
int reflect(int num) {
int ret = 0;
switch (num / 10) {
case 0:
ret += 0;
break;
case 1:
ret += 1;
break;
case 2:
ret += 5;
break;
case 5:
ret += 2;
break;
case 8:
ret += 8;
break;
default:
return -1;
}
switch (num % 10) {
case 0:
ret += 0;
break;
case 1:
ret += 10;
break;
case 2:
ret += 50;
break;
case 5:
ret += 20;
break;
case 8:
ret += 80;
break;
default:
return -1;
}
return ret;
}
int main() {
ios::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> h >> m;
string s;
cin >> s;
int org_h = (s[0] - '0') * 10 + (s[1] - '0');
int org_m = (s[3] - '0') * 10 + (s[4] - '0');
int min = 0;
int hour = 0;
if (reflect(org_h % h) != -1 && reflect(org_h % h) < m) {
hour = org_h % h;
while (org_m <= m) {
if (org_m == m) {
org_h = (org_h + 1) % h;
while (org_h <= h) {
int ref = reflect((org_h) % h);
if (ref != -1 && ref < m) {
hour = (org_h) % h;
break;
}
++org_h;
}
min = 0;
break;
}
int ref = reflect((org_m) % m);
if (ref != -1 && ref < h) {
min = (org_m) % m;
break;
}
++org_m;
}
}
else {
while (org_h <= h) {
int ref = reflect((org_h) % h);
if (ref != -1 && ref < m) {
hour = (org_h) % h;
break;
}
++org_h;
}
min = 0;
}
string s1;
s1 += (char)(hour / 10 + '0');
s1 += (char)(hour % 10 + '0');
string s2;
s2 += (char)(min / 10 + '0');
s2 += (char)(min % 10 + '0');
cout << s1 << ":" << s2 << "\n";
}
} | CPP |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 | #include<bits/stdc++.h>
using namespace std;
#define rep(i,k,n) for(i=k;i<n;i++)
#define repr(i,k,n) for(i=k;i>=n;i--)
#define inf(i,s) for(i=s;;i++)
#define ll long long
#define pb(i) push_back(i)
#define pop pop_back();
#define all(s) s.begin(),s.end()
#define maxl 9223372036854775807
#define up upper_bound
#define lb lower_bound
#define mod 1000000007
#define vll vector<ll>
#define mkp make_pair
template <typename T>
inline void debug(vector<T> v){cout << "\ndebug(vector)\n"; ll i; rep(i, 0, v.size())cout << v[i] << " "; cout << "\n\n";} // debug<ll>(v);
inline bool prime(ll n){
ll i;
for(i=2;i*i<=n;i++)
if(n%i==0)
return false;
return true;
}
int unitPlace(int n)
{return n%10;}
int tensPlace(int n)
{return n/10;}
int h,m;
vll v={0,1,5,-1,-1,2,-1,-1,8,-1};
bool check(int hour,int mins)
{
if (v[tensPlace(hour)] < 0)
return false;
if (v[unitPlace(hour)] < 0)
return false;
if (v[tensPlace(mins)] < 0)
return false;
if (v[unitPlace(mins)] < 0)
return false;
int hr = v[unitPlace(mins)] * 10 + v[tensPlace(mins)];
int ms = v[unitPlace(hour)] * 10 + v[tensPlace(hour)];
return hr < h && ms < m;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t; cin>>t;
while(t--)
{
string s;
cin>>h>>m>>s;
int hour = stoi(s.substr(0,2));
int mins = stoi(s.substr(3,5));
while(true)
{
if(check(hour,mins))
{
cout<<tensPlace(hour)<<unitPlace(hour)<<":"<<tensPlace(mins)<<unitPlace(mins)<<endl;
break;
}
else
{
mins++;
if(mins==m)
{
mins=0;
hour=((hour+1)%h);
}
}
}
}
return 0;
} | CPP |
1493_B. Planet Lapituletti | The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts h hours and each hour lasts m minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) follows the number of minutes in decimal; the number of minutes and hours is written with leading zeros if needed to form a two-digit number). Hours are numbered from 0 to h-1 and minutes are numbered from 0 to m-1.
<image>
That's how the digits are displayed on the clock. Please note that digit 1 is placed in the middle of its position.
A standard mirror is in use on the planet Lapituletti. Inhabitants often look at the reflection of the digital clocks in the mirror and feel happy when what you see on the reflected clocks is a valid time (that means that you see valid digits in the reflection and this time can be seen on the normal clocks at some moment of a day).
The image of the clocks in the mirror is reflected against a vertical axis.
<image>
The reflection is not a valid time.
<image>
The reflection is a valid time with h=24, m = 60. However, for example, if h=10, m=60, then the reflection is not a valid time.
An inhabitant of the planet Lapituletti begins to look at a mirrored image of the clocks at some time moment s and wants to know the nearest future time moment (which can possibly happen on the next day), when the reflected clock time is valid.
It can be shown that with any h, m, s such a moment exists. If the reflected time is correct at the moment the inhabitant began to look at the clock, that moment is considered the nearest.
You are asked to solve the problem for several test cases.
Input
The first line contains a single integer T (1 ≤ T ≤ 100) — the number of test cases.
The next 2 ⋅ T lines contain the description of test cases. The description of each test case consists of two lines.
The first line of a test case contains two integers h, m (1 ≤ h, m ≤ 100).
The second line contains the start time s in the described format HH:MM.
Output
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
Example
Input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
Output
12:21
00:00
52:28
00:00
00:00
Note
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
<image> | 2 | 8 | #pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3", "omit-frame-pointer","inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
/***********************************************/
/* Dear online judge:
* I've read the problem, and tried to solve it.
* Even if you don't accept my solution, you should respect my effort.
* I hope my code compiles and gets accepted.
* ___ __ _______ _______
* |\ \|\ \ |\ ___ \ |\ ___ \
* \ \ \/ /|_\ \ __/| \ \ __/|
* \ \ ___ \\ \ \_|/__\ \ \_|/__
* \ \ \\ \ \\ \ \_|\ \\ \ \_|\ \
* \ \__\\ \__\\ \_______\\ \_______\
* \|__| \|__| \|_______| \|_______|
*/
const long long mod = 1000000007;
//const long long mod = 998244353;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int mxN = 1000010;
int main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cout << fixed << setprecision(9);
int t = 1;
// int Case = 1;
cin >> t;
// t = 100;
int rev[] = { 0, 1, 5, -1, -1, 2, -1, -1, 8, -1 };
auto ref = [rev](int x) {
int res = 0;
for(int f = 0;f < 2 && res >= 0;f++) {
int d = x%10;
x/=10;
res *= 10;
if(rev[d] == -1) res = -1;
else res += rev[d];
}
return res;
};
while (t--) {
int h, m, sh, sm;
char c;
cin >> h >> m;
cin >> sh >> c >> sm;
int oh = INT_MAX, om = INT_MAX;
for (int a = sh; a < h && oh == INT_MAX; a++) {
for (int b = 0; b < m && oh == INT_MAX; b++) {
if (a == sh && b < sm)
continue;
int ra = ref(a), rb = ref(b);
if (ra == -1 || ra >= m)
continue;
if (rb == -1 || rb >= h)
continue;
oh = a, om = b;
}
}
if (oh == INT_MAX)
oh = 0, om = 0;
cout << setfill('0') << setw(2) << oh << ":" << setfill('0') << setw(2) << om << '\n';
}
return 0;
}
/* stuff you should look for:
* overflow, array bounds
* special cases (n=1?)
* DON'T STICK TO ONE APPROACH
* solve simpler or different variation
* Solve DP using graph ( \_(-_-)_/ )
*/
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.