ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Educational Codeforces Round 129(补题)

2022-05-25 01:02:19  阅读:158  来源: 互联网

标签:Educational now int ll Codeforces 补题 129 include define


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有