Codeforces Round #624 (Div. 3)ABCD

昨晚D被队友给HACK了,上分半路夭折…
A:
就是给你两个数a,b
可以对a进行操作:加一个奇数,或者减一个偶数。
问最少操作多少次,使a变成b
直接看ab的差值b-a
差值=0,不需要操作了
差值>0,说明a需要加来达到b,判断差值是不是奇数。
是·奇数,加一个奇数就可以了
是偶数,则需要加两个奇数
差值<0,a需要减来变成b,同上

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		long long a,b;
		cin >> a >> b;
		long long ans;
		long long cha = b-a;
		if(cha == 0)
		ans = 0;
		else if(cha > 0)
		{
			if(cha % 2 == 0)
			{
				ans = 2;
			}
			else
			{
				ans = 1;
			}
		}
		else if(cha < 0)
		{
			if(cha % 2 == 0)
			{
				ans = 1;
			}
			else
			{
				ans = 2;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

B:意思就是给你一个待处理的数组a[],在给你一个操作数组p[]
p[]的意义在于你可以对p[]里面的数当成a[]的下标进行交换操作
问能不能通过p[]来把a[]给排序。
简单地暴力进行冒泡排序即可。

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 105;
int a[maxn],p[maxn];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		for(int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for(int i = 0; i < m; i++)
		{
			cin >> p[i];
		}
		sort(p,p+m);
		for(int j = 0; j < n; j++)
		{
			for(int i = 0; i < m; i++)
			{
				if(a[p[i]-1] > a[p[i]])
				{
					int t = a[p[i]-1];
					a[p[i]-1] = a[p[i]];
					a[p[i]] = t;
				}
			}
		}
		int ans = 0;
		for(int i = 0; i < n; i++)
		{
			if(a[i] > a[i+1] && i != n-1)
			{
				ans = 1;
				break;
			}
		}
		if(ans)
		cout << "NO" << endl;
		else
		cout << "YES" << endl;
	}
	return 0;
}

C:
给你一个字符串,再给你一个数组。
你开始写这个字符串,数组里的数表示你会在这个数的字符处,重新写这个字符串。
例如:abca, 1,3
我要写字符串:a(在1处要重新写)
abc(在3处要重新写)
abca,最终把真个字符串完整写完
问:写完之后,要写26个字母多少次。
建立26个前缀和即可。

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 2e5 + 50;
int p[maxn];
int he[26][maxn] = {0};
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		long long n,m;
		cin >> n >> m;
		long long ans[30] = {0};
		for(int i = 0; i < 26; i++)
		{
			for(int j = 0; j < n; j++)
			{
				he[i][j] = 0;
			}
		}
		string s;
		cin >> s;
		for(int i = 0; i < m; i++)
		{
			cin >> p[i];
		}
		sort(p,p+m);
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < 26; j++)
			{
				if(j == s[i]-'a')
				{
					he[j][i] = he[j][i-1] + 1;
				}
				else
				 	he[j][i] = he[j][i-1];
			}
		}
		for(int i = 0; i < m; i++)
		{
			for(int j = 0; j < 26; j++)
			{
				ans[j] += he[j][p[i]-1];
			}
		}
		for(int i = 0; i < n; i++)
		{
			ans[s[i]-'a']++;
		}
		for(int i = 0; i < 26; i++)
		{
			if(i != 25)
			cout << ans[i] << " ";
			else
			cout << ans[i] << endl;
		}
	}
	return 0;
}

D:魔鬼们,在我被HACK后,我用我被HACK的样例HACK了6个,队友干掉20个。。。。
我们还干掉了不少橙名,紫名的,太可怕了。
题目:3个数abc,可以操作一个数+1或者-1.
最终目标:b%a=0,c%b=0
暴力b,对于每一个b的可能取值,去计算b的操作数。
之后c需要是b的倍数,计算c的最少操作数(向上扩大,还是向下)
接着暴力a,范围是1到根号b即可,需要b%a=0,计算a的操作数(a到当前暴力的a,还有a到b/a)
这道题最坑的就是数据范围,1e4是一个关键点。
abc的范围属于1e4,但是经过操作之后,abc的大小可能会超过1e4,我就是暴力的1e4被HACK了。
样例:
1
73 10000 10000
最后的结果bc应该是10001,10001.

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--)
	{
		long long a,b,c;
		cin >> a >> b >> c;
		long long ansa,ansb,ansc;
		long long ans = 3e4 + 500;
		int x,y,z;
		for(y = 1; y <= 2e4; y++)
		{
			long long bcha = abs(b-y);
			long long ccha = 0;
			if(abs(c-c/y*y) < abs(c-(c/y+1)*y)) z = c/y*y;
            else z = (c/y+1)*y;
            ccha = abs(z-c);
			long long acha = 1e4;
			for(x = 1; x*x <= y; x++)
			{
				if(y % x == 0)
				{
					if(abs(a-x) < abs(a-y/x))
					{
						acha = abs(a-x);
						if(acha+bcha+ccha < ans)
						{
							ans = acha+bcha+ccha;
							ansa = x;
							ansb = y;
							ansc = z;
						}
					}
					else
					{
						acha = abs(a-y/x);
						if(acha+bcha+ccha < ans)
						{
							ans = acha+bcha+ccha;
							ansa = y/x;
							ansb = y;
							ansc = z;
						}
					}
				}
			}
		}
		cout << ans << endl;
		cout << ansa << " " << ansb << " " << ansc << endl;
	}
	return 0;
}

发布了13 篇原创文章 · 获赞 0 · 访问量 1848

猜你喜欢

转载自blog.csdn.net/weixin_43934344/article/details/104495892