A. Plus One on the Subset
题意:给一个数组,每次选任意个数,依次加一,问最少几次可以使数组大小一致
思路:选最大和最小相减即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll t,n,m;
int main()
{
scanf("%lld",&t);
while(t--)
{
int n;
cin>>n;
ll ans = 0,mx = 0,mi = 2e9;
for(int i = 0 ; i < n ; i ++ )
{
cin>>m;
mx = max(m,mx);
mi = min(m,mi);
}
cout<<mx - mi<<"\n";
}
return 0;
}
B - Make AP
题意:给定三个数,你可以任意选择一个,乘以m(m > 0),如果操作之后的序列满足等差数列,输出 YES,否则输出 NO。
思路:对每个数依次枚举即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll t;
bool sol1(int a,int b,int c)//变a
{
if(b > c)
{
int x = b + (b - c);
if(x % a == 0 && x >= a) return true;
}
else if(b < c)
{
int x = b - (c - b);
if(x % a == 0 && x >= a) return true;
}
else if(b == c)
{
if(b % a == 0 && b >= a) return true;
}
return false;
}
bool sol2(int a,int b,int c)//变b
{
if(a>c && (a-c)%2 == 0)
{
int x = a - (a - c)/2;
if(x % b == 0 && x >= b) return true;
}
else if(a < c && (c-a) % 2 == 0)
{
int x = c - (c - a)/2;
if(x % b == 0 && x >= b) return true;
}
else if(a == c)
{
if(a % b == 0 && a >= b) return true;
}
return false;
}
bool sol3(int a,int b,int c)//变c
{
if(a > b)
{
int x = b - (a - b);
// cout<<"x1";
if(x % c == 0 && x >= c) return true;
}
else if(a < b)
{
int x = b + (b - a);
// cout<<"x2";
if(x % c == 0 && x >= c) return true;
}
else if(b == a)
{
// cout<<"x3";
if(b % c == 0 && b >= c) return true;
}
return false;
}
int main()
{
scanf("%lld",&t);
while(t--)
{
int a,b,c;
cin>>a>>b>>c;
if(a-b == b - c)
{
puts("YES");
// cout<<"x1";
continue;
}
else if(b-a == c-b)
{
puts("YES");
// cout<<"x2";
continue;
}
else
{
if(sol1(a,b,c))
{
puts("YES");
// cout<<"x3";
continue;
}
else if(sol2(a,b,c))
{
puts("YES");
// cout<<"x4";
continue;
}
else if(sol3(a,b,c))
{
puts("YES");
// cout<<"x5";
continue;
}
}
puts("NO");
}
return 0;
}
C - Division by Two and Permutation
题意:给定一个长度为 n的数组,你可以任意次将数组中的数除以2(向下取整),如果数组元素可以变成 1 2 3 … n,输出YES,否则输出NO。
思路:先处理使每个数都小于等于n,接着排序,从小到大,对每个数不断向下除,直到遇见未标记的1~n中的数。
也可用map,从大到小,对于多余的数依次往下分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll t;
int n;
ll a[100];
bool st[100];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
memset(a,0,sizeof a);
memset(st,0,sizeof st);
for(int i = 1 ; i <= n ; i ++ )
{
cin>>a[i];
while(a[i] > n)
{
a[i] /= 2;
}
}
sort(a+1,a+n+1);
// for(int i = 1 ; i <= n ; i ++ )
// cout<<a[i]<<" ";
if(a[n] < n)
{
puts("NO");
continue;
}
else
{
for(int i = 1 ; i <= n ; i ++ )
{
// cout<<a[i]<<" ";
while(a[i])
{
// cout<<a[i]<<" ";
if(!st[a[i]])
{
// cout<<a[i]<<" ";
st[a[i]] = true;
break;
}
a[i] /= 2;
}
}
bool flag = true;
for(int i = 1 ; i <= n ; i ++ )
{
if(!st[i])
{
flag = false;
break;
}
}
if(!flag) puts("NO");
else puts("YES");
}
}
return 0;
}
map写法
D. Palindromes Coloring
题意:给定一个字符串,k种颜色,然后给字符串染色。每种颜色至少用 1次,但可以不把所有字符都染色。染色之后把相同颜色的字符当作一组,可以组内任意交换位置,保证每组都为回文串。求长度最短的一组的最大长度。
思路:使得所有颜色至少有一个数要染,且目标值为每个颜色中染的数量中最少颜色最大。那么我们必须使得每个颜色染数尽可能趋向于相等,使得答案的最大。因为一个相同字母对必成回文,且至多带一个奇数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll t;
int n,k;
string s;
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k;
cin>>s;
map<char,int> mp;
for(int i = 0 ; i < s.size() ; i ++ )
{
mp[s[i]] ++ ;
}
int odd = 0 , even = 0;//存放奇数和偶数对
for(auto v:mp)
{
if(v.second % 2 == 0 && v.second > 0) even += v.second/2;
else
{
even += v.second/2;//多的转化为偶数对
odd += v.second%2;
}
}
int ans = 0;
if(even >= k)//足够染k个
{
ans += (even/k)*2;//平均分
int res = even % k;//剩余的给奇数
odd += res*2;
ans += min(1,odd/k);//因为求最少的最多,
//最多为1,或者不够分则为0
}
else ans = 1;//不足染k个,则最少的是1
cout<<ans<<"\n";
}
return 0;
}