Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)

Dashboard - Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2) - Codeforces

A. Increasing and Decreasing

题意:构造一个数组,a1=x, a2=y,a是严格递增的,且相邻元素间的差是严格递减的

思路:倒序暴力赋值,最后判断数组内元素是否为n即可,注意要将最后一个元素定义为x

AC代码:

#include
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair PII;
typedef pair PCI;
const int N = 2e5+10;
const int INF = 0x3f3f3f3f;
int T, n, m, x, y;
int a[N], b[N];

int gcd(int a, int b)
{
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

int main()
{
	fast();
	
	cin >> T;
	while(T --)
	{
		vector q;
		cin >> x >> y >> n;
		int cnt = 0, d = 0;
		for(int i = y; i >= x; i -= d)
		{
			q.push_back(i);
			d ++; 
			cnt ++;
			if(cnt == n) break;
		}
		
		q[q.size() - 1] = x;
		if(q.size() == n) 
		{
			for(int i = q.size() - 1; i >= 0; i --) cout << q[i] << " ";
			cout << endl;
		}
		else cout << "-1" << endl;
	} 
	return 0;
}

B. Swap and Reverse

题意:给出两种操作,使当前字符串达到最小字典序,操作一为交换奇数位或偶数位的字符,操作二为翻转k个连续的字符串

思路:找规律,当k为偶数时,操作一二可互相协助使字符串达到最佳字典序,直接sort排序输出即可;当k为奇数时,只采用操作一即可,即分别排序奇数位字符和偶数位字符,奇数位偶数位交替输出

AC代码:

#include
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair PII;
typedef pair PCI;
const int N = 2e5+10;
const int INF = 0x3f3f3f3f;
int T, n, m, k;
int a[N], b[N];

int gcd(int a, int b)
{
	if(b) while((a %= b) && (b %= a));
	return a + b;
}

int main()
{
	fast();
	
	cin >> T;
	while(T --)
	{
		char t[N];
		cin >> n >> k;
		string s; cin >> s;
	
		if(k % 2 == 0)//k为偶数,必达到最短字典序列
		{
			sort(s.begin(), s.end());
			cout << s << endl;
		}
		else //k为奇数,只采用1操作
		{
			string s1, s2;
			for(int i = 0; i < n; i ++)
			{
				if(i % 2 == 0) s1 += s[i];
				else s2 += s[i];
			}
			
			sort(s1.begin(), s1.end());
			sort(s2.begin(), s2.end());
			for(int i = 0; i < max(s1.size(), s2.size()); i ++)
			{
				if(i < s1.size()) cout << s1[i];
				if(i < s2.size()) cout << s2[i];
			}
			cout << endl;
		}
	} 
	return 0; 
}

C. Divisor Chain

题意:给出一个整数x,把x减到1为止,每次只能减去当前x的因数,可以为1,相同因数最多用两次

思路:题目中可以确定在1000次内一定能把x减到1,且不会重复同一因数两次,我们可以每次减去最大的当前x的2次方幂,直到为1;还有种方法是倒序考虑,从1开始,每次乘2,因为最多可以使用两次1,若最终结果不为x,可将1进行插入操作达到最终x;

AC代码:

#include
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

typedef long long LL;
typedef pair PII;
typedef pair PCI;
const int N = 2e5+10;
const int INF = 0x3f3f3f3f;
LL T, n, m, k, x;
int a[N], b[N];

int gcd(int a, int b)
{
	if(b) while((a %= b) && (b %= a));
	return a + b;
}
bool iprime(int n)
{
	if(n < 2) return false;
	for(int i = 2; i <= n / i; i ++)
	if(n % i == 0)
	return false;
	
	return true;
}
int main()
{
	fast();
	
	cin >> T; 
	while(T --)
	{
		vector q;
		cin >> x;

		if(x <= 3)
		{
			cout << x << endl;
			for(int i = x; i >= 1; i --) cout << i << " ";
			cout << endl;
		}
		else 
		{
			q.push_back(x);
			while(x != 1)
			{
				for(int i = 30; i >= 0; i --)
				{
					LL tt = pow(2, i);
					if(x % tt == 0 && tt  != x)
					{
						x -= pow(2, i);
						q.push_back(x);
						break;
					}
				}
			}
			cout << q.size() << endl;
			for(int i = 0; i < q.size(); i ++)
			cout << q[i] << " ";
			cout << endl;
		}
	}  
	return 0;
}

你可能感兴趣的:(算法,c++)