A-游游画U
题解:找规律即可
实现代码
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i = 1 ; i <= 4 * n - n - 1 ; i ++ )
{
if(i <= 4 * n - n - 1)
{
for(int j = 1 ; j <= n ; j ++ ) cout << "*";
for(int j = 1 ; j <= n * 2 ; j ++ ) cout << ".";
for(int j = 1 ; j <= n ; j ++ ) cout << '*';
}
cout << endl;
}
for(int i = 0 ; i < n + 1 ; i ++ )
{
for(int j = 0 ; j < i ; j ++ ) cout << '.';
for(int j = 0 ; j < n ; j ++ ) cout << '*';
for(int j = 0 ; j < 2 * n - i * 2 ; j ++ ) cout << ".";
for(int j = 0 ; j < n ; j ++ ) cout << '*';
for(int j = 0 ; j < i ; j ++ ) cout << '.';
cout << endl;
}
}
B–游游的数组染色
题解:要求我们找到不同颜色但相同数值的取法,那么先用map存取数值中两种颜色各为多少,然后在相乘,最后将所有的叠加。
实现代码
#include <iostream>
#include <map>
using namespace std;
const int N = 200010;
typedef long long LL;
int n;
LL a[N];
map<LL , LL> mp , mp1;
int main()
{
int n;
cin >> n;
for(int i = 0 ; i < n ; i ++ ) cin >> a[i];
string s;
cin >> s;
LL sum = 0 , cnt = 0;
for(int i = 0 ; i < n ; i ++ )
if(s[i] == 'R') mp[a[i]] ++;
else mp1[a[i]] ++ ;
long long ans = 0;
for(auto [x , y] : mp) ans += y * mp1[x];
cout << ans << endl;
}
C-- 游游的交换字符
题解:首先需要我们01字符串相邻不同,所以只有两种可能,10101010…
或者01010101… , 当n为奇数时,放在首位的一定是多的那个数,比如10101 ,01010 , 当偶数时,分别计算取最小值,首先记录每个1的位置,然后计算将其放到指定位置时需要几步,叠加就是最优解的答案
实现代码
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
int main()
{
string s;
cin >> s;
LL n = s.size();
LL sum = 0;
for(int i = 0 ; i < n ; i ++ ) if(s[i] == '1') sum ++;
if(n & 1)
{
vector<LL> a(n);
char c;
if(sum == n / 2 + 1) c = '1';
else c = '0';
LL k = 0;
for(LL i = 0 ; i < n ; i ++ )
if(s[i] == c) a[k ++ ] = i;
LL ans = 0;
for(LL i = 0 ; i < k ; i ++ )
ans += abs(2 * i - a[i]);
cout << ans << endl;
}else
{
LL ans = 0 , res = 0;
vector<LL> a(n);
LL k = 0;
for(LL i = 0 ; i < n ; i ++ ) if(s[i] == '1') a[k ++ ] = i;
for(LL i = 0 ; i < k ; i ++ )
ans += abs(2 * i - a[i]);
for(LL i = 0 ; i < k ; i ++ )
res += abs(2 * i + 1 - a[i]);
cout << min(ans , res) << endl;
}
return 0;
}