How Much Does Daytona Cost?
We define an integer to be the most common on a subsegment, if its number of occurrences on that subsegment is larger than the number of occurrences of any other integer in that subsegment. A subsegment of an array is a consecutive segment of elements in the array a.
Given an array a of size n, and an integer k, determine if there exists a non-empty subsegment of a where k is the most common element.
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n and k (1≤n≤100, 1≤k≤100) — the number of elements in array and the element which must be the most common.
The second line of each test case contains n integers a1, a2, a3, ……, an (1≤ai≤100) — elements of the array.
For each test case output "YES" if there exists a subsegment in which k is the most common element, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
YES
NO
NO
YES
YES
YES
YES
In the first test case we need to check if there is a subsegment where the most common element is 4.
On the subsegment [2,5] the elements are 4, 3, 4, 1
- 4 appears 2 times;
- 1 appears 1 time;
- 3 appears 1 time.
This means that 4 is the most common element on the subsegment [2,5], so there exists a subsegment where 4 is the most common element.
题目大意
t组数据,输入n,g,n个数据,询问是否存在数组的非空子集中的最大出现次数的元素为g
HINT
只要查询到g出现就满足条件
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int a[N];
int main(){
int t;cin>>t;
while(t--){
int n,k,flag=0;//设置标记
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==k){
flag=1;//找到就退出
}
}
if(flag==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
Aleksa and Stack
After the Serbian Informatics Olympiad, Aleksa was very sad, because he didn't win a medal (he didn't know stack), so Vasilije came to give him an easy problem, just to make his day better.
Vasilije gave Aleksa a positive integer n(n≥3) and asked him to construct a strictly increasing array of size n of positive integers, such that
- 3⋅ai+2 is not divisible by ai+ai+1 for each i(1≤i≤n−2).
Since Aleksa thinks he is a bad programmer now, he asked you to help him find such an array.
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤10^4) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (3≤n≤2⋅10^5) — the number of elements in array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.
For each test case, output n integers a1,a2,a3,…,an (1≤ai≤10^9).
It can be proved that the solution exists for any n. If there are multiple solutions, output any of them.
6 8 12
7 11 14 20 22 100
9 15 18 27 36 90 120
In the first test case, a1=6, a2=8, a3=12, so a1+a2=14 and 3⋅a3=36, so 3⋅a3 is not divisible by a1+a2.
题目大意
t组数据,每组数据由n项严格递增的数组组成,3*an不能被(an-1+an-2)整除,输出这样的数列
HINT
提前设置a0,a1,循环2~n,count持续递增,直至count不能被(ai-1+ai-2)整除,把count值赋给ai
代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
int main(){
int t;cin>>t;
while(t--){
int n;cin>>n;
a[0]=2;a[1]=3;//遵循递增的条件下任意设置值
int count=4;
for(int i=2;i<n;i++){
int add=a[i-1]+a[i-2];
while(count*3%(add)==0){
count++;
}
a[i]=count;count++;//记得每轮count++
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}
Vasilije in Cacak
Aca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills.
Vasilije is given three positive integers: n, k, and x, and he has to determine if he can choose k distinct integers between 1 and n, such that their sum is equal to x.
Since Vasilije is now in the weirdest city in Serbia where Aca and Milovan live, Cacak, the problem seems weird to him. So he needs your help with this problem.
The first line contains a single integer t (1≤t≤10^4) — the number of test cases.
The only line of each test case contains three integers n, k and x (1≤n≤2⋅10^5, 1≤k≤n, 1≤x≤4⋅10^10) — the maximum element he can choose, the number of elements he can choose and the sum he has to reach.
Note that the sum of n over all test cases may exceed 2⋅1052⋅105.
For each test case output one line: "YES", if it is possible to choose k distinct integers between 1and n, such that their sum is equal to x, and "NO", if it isn't.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
YES
NO
YES
YES
NO
NO
YES
NO
NO
NO
YES
YES
In the first test case n=5, k=3, x=10, so we can choose the numbers: 2, 3, 5, whose sum is 10, so the answer is "YES".
In the second test case n=5, k=3, x=3, there is no three numbers which satisfies the condition, so the answer is "NO". It can be shown that there are no three numbers whose sum is 3.
题目大意
t组数据,在1~n中,选择k个数字,和可能为x输出“YES”,否则输出“NO”
HINT
在1~n中,选择k个数字,和的值从min=(1+2...+k),到max=(n-k+1,n-k+2...n),所以只要x在min~max区间中,即满足条件。
·min,max求值要直接公式计算,用for循环累加会超时
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;cin>>t;
while(t--){
long long n,k,x;cin>>n>>k>>x;
long long min=((1+k)*k)/2,max=(((n-k+1)+n)*k)/2;
//cout<<max<<" "<<min<<endl;
if(x<=max&&x>=min){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}