其他分享
首页 > 其他分享> > Educational Codeforces Round 129(补题)

Educational Codeforces Round 129(补题)

作者:互联网

C. Double Sort

题意

让我们对两个数组进行排序,每次进行排序要同时将a,b数组同时进行排序,问能不能将数组变为非递减数组

算法(前缀和+手动模拟排序)

我们先找出每个数在数组的位置的范围,每个数在分别的数组上面的位置相对是稳定的,如果对应位置的ai,bi范围相交,那么我们就可以去max(l1,l2),即可。找位置可以使用前缀和找出。

C++

// Problem: C. Double Sort
// Contest: Codeforces - Educational Codeforces Round 129 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1681/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#define endl "\n"
#define x first
#define y second
using namespace std;
const int N = 110;
typedef pair<int,int> PII;
int n, m, k;
int a[N], b[N];
int va[N], vb[N], e[N];
bool check(int l1, int r1, int l2, int r2)
{
	if(l1 > l2)	swap(l1, l2), swap(r1, r2);
	return l2 <= r1;	
}

int main()
{
	cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

	cin >> k;
	while(k--)
	{
		cin >> n;
		for(int i = 1; i <= n; i++) 
			va[i] = vb[i] = 0;
		for(int i = 1; i <= n; i++)
		{
			cin >> a[i];
			va[a[i]]++;
		} 
		for(int i = 1; i <= n; i++)
		{
			cin >> b[i];
			vb[b[i]]++;
		}
		
		for(int i = 1; i <= n; i++)
			va[i] += va[i - 1], vb[i] += vb[i - 1];
			
		bool flag = true;	
		for(int i = 1; i <= n; i++)
		{
			int l1 = va[a[i] - 1] + 1, r1 = va[a[i]];
			int l2 = vb[b[i] - 1] + 1, r2 = vb[b[i]];
			if(!check(l1, r1, l2, r2))
			{
				flag = false;
				break;
			}
			else
				e[i] = max(l1, l2);
		}
		
		if(flag)
		{
			vector<PII> ans;
			for(int i = 1;i <= n; i++)
				for(int j = 1; j <= n - i; j++)
				{
					if(e[j] > e[j + 1])
					{
						swap(e[j], e[j + 1]);
						ans.push_back({j, j + 1});
					}
				}	
			cout << ans.size() << endl;
			for(auto c : ans)
				cout << c.x << " " << c.y << endl;
		}
		else
			cout << -1 << endl;
	}
    return 0;
}

D. Required Length

题意

给我们两个数n,m,让m进行任意多次以在m数字中出现的十进制数相乘,直到m的位数大于等于n即可

算法 bfs

我们将开始的数字放入队列,将到m的数字的每一位进行遍历,之后分别相乘并且放入队列之中,直到位数大于等于n即可。

c++

// Problem: D. Required Length
// Contest: Codeforces - Educational Codeforces Round 129 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1681/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// yyyy-MM-dd HH:mm:ss
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<cstring>
#define endl "\n"
#define x first
#define y second
using namespace std;
typedef unsigned long long ll;
typedef pair<ll, ll> pii;
map<ll, int> vis;
const int N = 1e5 + 10;

ll n, m, k;
ll cnt[11];

int dfs(ll s)
{
	queue<pii> q;
	q.push({s, 0});
	while(q.size())
	{
		pii now = q.front();
		q.pop();
		
		if(now.x == 0)
			return now.y;
		if(vis[now.x])
			continue;
		vis[now.x] = now.y;
		ll t = now.x, len = 0;
		while(t)
		{
			cnt[t % 10] = 1;
			t /= 10;
			len++;
		}
		if(len >= n)
			return now.y;
		for(int i = 2; i <= 9; i++)
			if(cnt[i])
				q.push({now.x * i, now.y + 1});
		memset(cnt, 0, sizeof cnt);
	}
	return -1;
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
	cin >> n >> m;
	cout << dfs(m) << endl;
    return 0;
}

标签:Educational,now,int,ll,Codeforces,补题,129,include,define
来源: https://www.cnblogs.com/K-No-Wei/p/16307815.html