#710 (Div. 3) D. Epic Transformation 优先队列模拟 || 思维+贪心

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值