分类讨论,如果数的总数大于2直接return false,否者看个数差
#include
const int N = 105;
int a[N];
void solve()
{
int n;
std::cin >> n;
std::unordered_map<int, int> m;
for (int i = 1; i <= n; i++)
{
std::cin >> a[i];
m[a[i]]++;
}
if (m.size() > 2)
{
std::cout << "No\n";
return;
}
else if (m.size() < 2)
{
std::cout << "Yes\n";
return;
}
int a = 0, b = 0;
for (auto x : m)
{
if (a == 0)
a = x.second;
else
b = x.second;
}
if (abs(a - b) <= 1)
{
std::cout << "Yes\n";
}
else
{
std::cout << "No\n";
}
}
int main()
{
int T;
std::cin >> T;
while (T--)
{
solve();
}
return 0;
}
模拟一遍,如果s不行看t能否救s
#include
const int N = 105;
int a[N];
bool check(std::string s){
int n = s.size();
char last = '2';
for(int i = 0 ; i < n ; i ++){
if(last == '2' || last != s[i]){
last = s[i];
}else{
return false;
}
}
return true;
}
void solve()
{
int n , m;
std::string s , t;
std::cin >> n >> m;
std::cin >> s;
std::cin >> t;
int fi = t[0] , en = t[m - 1];
if(check(s)){
std::cout << "Yes\n";
return ;
}
if(check(t)){
char last = '2';
for(int i = 0 ; i < n ; i ++){
if(last == '2' || last != s[i]){
last = s[i];
}else{
if(fi != s[i - 1] && en != s[i])continue;
else{
std::cout << "No\n";
return ;
}
}
}
}else{
std::cout << "No\n";
return ;
}
std::cout << "Yes\n";
}
int main()
{
int T;
std::cin >> T;
while (T--)
{
solve();
}
return 0;
}
左右两个指针向内扫
不等为0的时候插在右边,为1的时候插在左边
#include
std::string t = "01";
void solve()
{
std::string s;
std::vector<int> res;
int n;
std::cin >> n >> s;
int l = 0, r = n - 1;
if (n % 2)
{
std::cout << "-1\n";
return;
}
while (l < r)
{
if (s[l] == s[r])
{
if (s[l] == '0')
res.push_back(r + 1), s.insert(r + 1, t);
else
res.push_back(l), s.insert(l, t);
r += 2;
}
l++, r--;
if (res.size() > 300)
{
std::cout << "-1\n";
return;
}
}
std::cout << res.size() << std::endl;
for (auto x : res)std::cout << x << " ";
std::cout << std::endl;
}
int main()
{
int T;
std::cin >> T;
while (T--)
{
solve();
}
return 0;
}