目录
A. Another String Minimization Problem
A. Another String Minimization Problem
A. Another String Minimization Problem
思路:
模拟
代码如下:
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10, M = 110;
const int mod = 998244353;
int T;
void solve()
{
int n, m;
string s;
cin >> n >> m;
int a[N];
for(int i = 1; i <= n; i ++ )
cin >> a[i];
s += ' ';
for(int i = 0; i < m; i ++ )
s += "B";
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i ++ )
if(s[min(a[i], m - a[i] + 1)] == 'B') s[min(a[i], m - a[i] + 1)] = 'A';
else s[max(a[i], m - a[i] + 1)] = 'A';
//cout << s << endl;
for(int i = 1; i <= m; i ++ )
cout << s[i];
cout << endl;
}
int main()
{
fast;
cin >> T;
while(T -- )
solve();
return 0;
}
B. Making Towers
思路:
只要相同的数字的两者相距为奇数,两者即可叠在一起
代码如下:
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10, M = 110;
const int mod = 998244353;
int T;
void solve()
{
int n, m;
string s;
cin >> n;
map<int, PII> mp;
int a[N];
for(int i = 1; i <= n; i ++ )
{
cin >> a[i];
if(mp[a[i]].y == 0) mp[a[i]] = {i, 1};
else
{
if((i - mp[a[i]].x) % 2)
{
mp[a[i]].x = i;
mp[a[i]].y += 1;
}
}
}
for(int i = 1; i <= n; i ++ )
cout << mp[i].y << " ";
cout << endl;
}
int main()
{
fast;
cin >> T;
while(T -- )
solve();
return 0;
}
C. Qpwoeirut And The City
思路:
两种情况:
总数为奇数时,一定为每个偶数楼层为cool,所以求出将所有的偶数位置成立的最小值
总数为偶数时,
在任意的偶数位置,可在该偶数的右侧选择一个奇数位置,此后所选都是奇数位置,即
选择 一部分偶数位置 + 一部分奇数位置,(其中,偶数位置和奇数位置都可以为0,变成全偶数,或全奇数位置状态)
代码如下:
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(nullptr); cout.tie(nullptr)
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10, M = 110;
const int mod = 998244353;
int T;
void solve()
{
int n, m;
cin >> n;
LL a[N];
for(int i = 1; i <= n; i ++ )
cin >> a[i];
if(n % 2)
{
LL res = 0;
for(int i = 2; i <= n; i += 2 )
{
if(max(a[i - 1], a[i + 1]) >= a[i])
res += max(a[i - 1], a[i + 1]) + 1 - a[i];
}
cout << res << endl;
}
else
{
LL s[N];
s[0] = 0, s[n] = 0;
for(int i = 2; i < n; i ++ )
if(max(a[i - 1], a[i + 1]) >= a[i]) s[i] = max(a[i - 1], a[i + 1]) + 1 - a[i];
else s[i] = 0;
s[0] = 0, s[1] = 0;
for(int i = 2; i <= n; i ++ )
if(i % 2) s[i] = s[i - 2] + s[i];
else s[i] = s[i - 2] + s[i];
LL res = 0;
res = min(s[n - 1], s[n]);
for(int i = 2; i <= n - 2; i += 2 )
res = min(res, s[i] + s[n - 1] - s[i + 1]);
cout << res << endl;
}
}
int main()
{
fast;
cin >> T;
while(T -- )
solve();
return 0;
}