模拟题(贪心+思维

A. Meximum Array
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 = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int a[maxn];
int cnt[maxn];
void work()
{
	cin >> n;
	for(int i = 0; i <= n; ++i) cnt[a[i]] = 0;
	for(int i = 1; i <= n; ++i) cin >> a[i], cnt[a[i]] += 1;
	int l = 1;
	vector <int> v;
	while(l <= n)
	{
		int mex = 0;
		while(cnt[mex]) mex++;
		vector <bool> vis(mex + 1);
		int c = 0;
		while(l <= n)
		{
			if(a[l] < mex && !vis[a[l]]){
				vis[a[l]] = 1;
				c += 1;
			}
			cnt[a[l]]--;
			++l;
			if(c == mex) break;
		}
		v.push_back(mex);
	}
	cout << v.size() << endl;
	for(auto x : v) cout << x << " ";cout << endl;
}

int main()
{
	ios::sync_with_stdio(0);
	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

B - Reserve or Reverse
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 = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
vector <int> v[26];
string s;

void work()
{
	cin >> n >> s;
	for(int i = 0; i < s.size(); ++i)
		v[s[i]-'a'].push_back(i);
	int l = 0, r = n - 1;
	while(l < r)
	{
		int p = 0;
		while(p < 26)
		{
			while(!v[p].empty() && v[p].back() > r) v[p].pop_back();
			if(v[p].empty() || v[p].back() < l) ++p;
			else break;
		}
		if(s[l] - 'a' == p){
			++l;continue;
		}
		r = v[p].back();
		swap(s[l], s[r]);
		++l;
		--r;
	}
	cout << s;
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

「Nhk R1 D」Apocryphal Vir Pulcher
思路:
题解
n n n 个队列,每次用最小值拓展
每次拓展只能从 [ p o s , n ] [pos,n] [pos,n],否则会出现重复
例如:
n = 3 , a 1 = 3 , a 2 = 4 , a 3 = 5 n=3,a_1=3,a_2=4,a_3=5 n=3,a1=3,a2=4,a3=5
第一小的是 0 0 0,选出来拓展 q 1 = 3 , q 2 = 4 , q 3 = 5 q_1=3,q_2=4,q_3=5 q1=3,q2=4,q3=5
第二小是 3 3 3,然后拓展 q 1 = 6 , q 2 = 4 , 7 , q 3 = 5 , 8 q_1=6,q_2=4,7,q_3=5,8 q1=6,q2=4,7,q3=5,8
第三小是 4 4 4,然后拓展 q 1 = 6 , q 2 = 7 , 8 , q 3 = 5 , 8 , 9 q_1=6,q_2=7,8,q_3=5,8,9 q1=6,q2=7,8,q3=5,8,9
如果第三次拓展从 1 1 1 开始,那么 q 1 = 6 , 7 , q 2 = 7 , 8 , q 3 = 5 , 8 , 9 q_1=6,7,q_2=7,8,q_3=5,8,9 q1=6,7,q2=7,8,q3=5,8,9
事实上这里 q 1 q_1 q1 q 2 q_2 q2 对于的数列 c c c 都是 1 , 1 , 0 1,1,0 1,1,0,也就发生了重复
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 = 2e6 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
queue <ll> q[89];
int a[maxn];

void work()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) cin >> a[i];
	sort(a + 1, a + 1 + n);
	q[1].push(0);
	ll Min, pos;
	for(int i = 1; i <= m; ++i)
	{
		Min = INF, pos = -1;
		for(int j = 1; j <= n; ++j){
			if(!q[j].empty() && q[j].front() < Min){
				Min = q[j].front();
				pos = j;
			}
		}	
		q[pos].pop();
		for(int j = pos; j <= n; ++j) q[j].push(Min + a[j]);
	}	
	cout << Min << endl;
}

int main()
{
	ios::sync_with_stdio(0);
//	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值