B. Partial Replacement
思路:
贪心构造
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
// .**.***
void work()
{
cin >> n >> m;
string s;cin >> s;
s = "@" + s;
int l = 1, r = n;
while(s[l] == '.' && l < n) ++l;
while(s[r] == '.' && r > 1) --r;
if(l > r){
cout << 0 << endl;return;
}
else if(l == r){
cout << 1 << endl;return;
}
int ans = 2;
// cout << l << " " << r << endl;
while(r - l > m)
{
int j = min(l + m, n);
while(s[j] != '*' && j > l) --j;
if(j < r && j > l && s[j] == '*'){// 左边没有可选的
++ans;
l = j;
}
else
{
j = min(1ll, r - m);
while(s[j] != '*' && j < r) ++r;
r = j;
++ans;
}
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(0);
int TT;cin>>TT;while(TT--)
work();
return 0;
}
D. Epic Transformation
思路:
优先队列模拟解法
每次删除两个数,复杂度其实是
O
(
n
)
O(n)
O(n) 的,完全可以通过
code:
//使用优先队列模拟操作
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
void work()
{
priority_queue <int> q;
map <int,int> ma;
cin >> n;
for(int i = 1, x; i <= n; ++i) cin >> x, ma[x]++;
for(auto x : ma){
q.push(x.second);
}
int res = 0;
while(q.size() > 1)
{
int now = q.top(); q.pop();
int next = q.top(); q.pop();
-- now; -- next;
if(now) q.push(now);
if(next) q.push(next);
res += 2;
}
cout << n - res << endl;
}
int main()
{
ios::sync_with_stdio(0);
int TT;cin>>TT;while(TT--)
work();
return 0;
}
E. Restoring the Permutation
思路:
贪心构造
较大字典序考虑可用的数中最大的一个,用并查集维护即可
较小字典序考虑用一个指针从
1
1
1 开始移动,模拟即可
code:
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int q[maxn], vis[maxn];
unordered_map<int, int> ma;
int find(int x){
if(!ma.count(x) || ma[x] == x) return x;
else return ma[x] = find(ma[x]);
}
void work()
{
ma.clear();
cin >> n;
for(int i = 1; i <= n; ++i) vis[i] = 0;
for(int i = 1; i <= n; ++i) cin >> q[i], vis[q[i]] = 1;
// Min
int l = 1;
for(int i = 1; i <= n; ++i)
if(q[i] != q[i-1]){
cout << q[i] << " ";
}
else
{
while(vis[l]) ++l;
cout << l << " ";
++l;
}
cout << endl;
// Max
for(int i = 1; i <= n; ++i)
if(q[i] != q[i-1]){
cout << q[i] << " ";
ma[q[i]] = q[i] - 1;
}
else{
int w = find(q[i]);
cout << w << " ";
ma[q[i]] = w - 1;
}
cout << endl;
}
int main()
{
ios::sync_with_stdio(0);
int TT;cin>>TT;while(TT--)
work();
return 0;
}