2022.10.24思维训练题解

如果有写的不清楚的地方可以问群里的大佬们。

目录

不写了(不会写)。


正文


1001 A.Number Replacement

题意:

有一个长度为 n 的数组 ​a 和 一个字符串 s。如果每两个不同的元素表示的数相同,那么这两个元素表示的字母要相同。(a下标相等时字符应相等)

如果这条件  满足,那么输出 Yes,否则输出 No

ac代码(非最优解)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
#define ll long long 
//typedef long long ll;
int a[N];
char b[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		int flag = 1;
		cin >> n;
		memset(b, 0, sizeof(b));
		for (int i = 0;i < n;i++)
		{
			cin >> a[i];

		}
		string s;
		cin >> s;
		for (int i = 0;i < n;i++)
		{
			if(b[a[i]]==0)
			b[a[i]]=s[i];
			else if (b[a[i]] != s[i])
			{
				cout << "NO" << endl;
				flag = 0;
				break;
			}
		}
		if (flag)
			cout << "YES" << endl;
	}
}



1002 B Even-Odd Increments

  • 0 ,给所有偶数加上 x;
  • 1 ,给所有奇数加上 x;`
  • 思路:计算奇数偶数的数量,直接求和,当0时sum+奇数数量乘x,偶数同理
  • 注意:当一个数加奇数时奇偶性会变。
  • ac代码
  • #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    ll n, q;
    ll a[N];
    ll b[2];
    ll sum = 0;;
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		cin >> n >> q;
    		sum = 0;
    		memset(b, 0, sizeof(b));
    		for (int i = 0;i < n;i++)
    		{
    			cin >> a[i];
    			if (a[i] % 2)
    				b[1]++;
    			else
    				b[0]++;
    			sum += a[i];
    		}
    		for (int i = 0;i < q;i++)
    		{
    			int x, y;
    			cin >> x >> y;
    			if (x == 0)
    			{
    				sum += b[0] * y;
    				if (y % 2 == 1)
    				{
    					b[1] += b[0];
    					b[0] = 0;
    				}
    			}
    			else
    			{
    				sum += b[1] * y;
    
    				if (y % 2 == 1)
    				{
    					b[0] += b[1];
    					b[1] = 0;
    				}
    			}
    			//cout << "sum" << sum << endl;
    			cout << sum << endl;
    		}
    		
    	}
    }
    
    
    

  • 1003 C Sum

  • 给定3个数,判定其中是否有一个数等于另两数之和,若是输出 YES,否则输出 NO

  • ac代码

  • 
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    int a[5];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    	
    		int x;
    		cin >> a[0] >> a[1] >> a[2];
    		sort(a, a + 3);
    		x = a[2];
    		if (a[0] + a[1] == x)
    			cout << "YES" << endl;
    		else
    			cout << "NO" << endl;
    	}
    }
    
    
    

  • 1004 D Increasing

  • 给定一个长为 n 的序列 a,判断你是否能通过重新排序的方式使序列严格递增。

  • 小技巧 直接用数组会爆,用map增加容量

  • ac代码

  • #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    int a[N];
    map<int, int>b;
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		int x;
    		int flag = 1;
    		b.clear();
    		cin >> x;
    		for (int i = 0;i < x;i++)
    		{
    			cin >> a[i];
    		}
    		for(int i=0;i<x;i++)
    		{
    			if (!b[a[i]])
    				b[a[i]]++;
    			else
    			{
    				cout << "NO" << endl;
    				flag = 0;
    				break;
    			}
    		}
    		if (flag)
    			cout << "YES" << endl;
    	}
    }
    
    
    

  • 1005 E Stripes

  • 8×8 的染色地图,初始是白色,给你染完色的地图,. 代表白色,R 代表红色,B 代表蓝色。

    其中染红色的策略必定是涂一行,染蓝色的策略必定是涂一列。颜色会覆盖。

    求最后涂的颜色是什么,R代表红,B 代表蓝。

  • 思路:找完整的一行全为红色,或者完整的一列全为蓝色,暴力求解

  • ac代码

  • #define _CRT_SECURE_NO_WARNINGS 1
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    char a[10][10];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		int flag = 0;
    		//getchar();
    		//getchar();
    		//getchar();
    		for (int i = 0;i < 8;i++)
    		{
    
    			cin >> a[i];
    		}
    		for (int i = 0;i < 8;i++)
    		{
    			int j;
    			for ( j= 0;j < 8;j++)
    				if (a[j][i] != 'B')
    					break;
    			if (j == 8)
    			{
    				flag = 1;
    				break;
    			}
    		}
    		if (flag)
    			cout << "B" << endl;
    		for (int i = 0;i < 8;i++)
    		{
    			int j;
    			for (j = 0;j < 8;j++)
    				if (a[i][j] != 'R')
    					break;
    			if (j == 8)
    			{
    				flag = 2;
    				break;
    			}
    		}
    		if (flag == 2)
    			cout << "R" << endl;
    
    	}
    }
    
    
    

    1006 F Compare T-Shirt Sizes

        

题意:给定两个字符串 a 和 b 表示两件衣服的尺码,我们规定:字符串只能由字符 M(中等尺寸)组成或由几个字符 X(可以是 0 个)加上一个字符 S(小尺寸) 或 L(大尺寸) 组成。

你需要比较两件衣服尺码大小,比较方法如下:

  • 无论前面有多少个字符 X 的小尺寸,都小于中等尺寸和大尺寸;
  • 无论前面有多少个字符 X 的大尺寸,都大于中等尺寸和小尺寸;
  • 字符 S 前的字符 X 越多,尺寸越小;
  • 字符 L 前的字符 X 越多,尺寸越大。

给定 t 组尺寸,若第一件衣服尺寸大,输出 >,若第二件衣服尺寸大,输出 <,否则输出 =。每组数据换行隔开。

我们称同时符合以上两个要求的排列为“有趣的排列”。

例如,当 n = 4 时,[4,3,1,2][4,3,1,2] 是一个“有趣的排列”

给定一个 n,你需要构造出一个长度为 n 的“有趣的排列”,或者输出一个 -1 来表明不存在长度为 n 的“有趣的排列”。

  • #define _CRT_SECURE_NO_WARNINGS 1
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    char a[10][10];
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		string x, y;
    		cin >> x >> y;
    		int x1 = x.size();
    		int y1 = y.size();
    		//cout << x1 << y1 << endl;
    		if (x[x1 - 1]<y[y1 - 1] || x[x1 - 1] == y[y1 - 1]&&x[x1-1]=='L' && x1>y1|| x[x1 - 1] == y[y1 - 1] && x[x1 - 1] == 'S'&&x1<y1)
    			cout << ">" << endl;
    		else if (x == y)
    			cout << "=" << endl;
    		else
    			cout << "<" << endl;
    	}
    }
    
    
    

    1007G - Funny Permutation

  • 题意

    如果一个长度为 n 的数列满足恰好包含 1 到 n 的整数各一个,则我们称之为排列。例如,[3,1,4,2][3,1,4,2]、[1][1] 和 [2,1][2,1] 都是排列,但是 [1,2,1][1,2,1]、[0,1][0,1] 和 [1,3,4][1,3,4] 都不是。

  • 思路:打草稿发现最大规律  偶数位时倒序输出
  • 奇数位时倒序输出且中间两个数交换
  • 特判 3和5时答案
  • ac代码
  • #define _CRT_SECURE_NO_WARNINGS 1
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		int n;
    		cin >> n;
    		if (n & 1)
    		{
    			if (n == 3)
    				cout << "-1" ;
    			else if(n == 5)
    				cout << "4 5 1 2 3" ;
    			else
    			{
    				for (int i = n;i >= 1;i--)
    				{
    					if (i == n)
    						cout << i;
    					else if (i == n / 2)
    						cout << " " << i + 1;
    					else if (i == n / 2 + 1)
    						cout << " " << i - 1;
    					else
    						cout << " " << i;
    				}
    			}
    		}
    		else
    		{
    			for (int i = n;i >= 1;i--)
    			{
    				if (i == n)
    					cout << i;
    				else
    					cout << " " << i;
    			}
    		}
    		cout << endl;
    	}
    	return 0;
    }
    
    
    

    1008 H - Spell Check

  • 输入的第一行一个整数 t ,表示有 t 组数据。

    对于每组数据,第一行一个整数 n,表示输入的名字排列的长度。

    第二行一个字符串,表示需要判断的排列。

  • 判断是否有Timur五个字符且每个仅有一个

  • ac代码

  • #define _CRT_SECURE_NO_WARNINGS 1
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 50;
    #define ll long long 
    //typedef long long ll;
    char ss[] = { 'T','i','m','u','r' };
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int T;
    	cin >> T;
    	while (T--)
    	{
    		char b[10];
    		memset(b, 0, sizeof(b));
    		int num = 0;
    		int n;
    		cin >> n;
    		string s;
    		cin >> s;
    		if (n != 5)
    			cout << "NO" << endl;
    		else
    		{
    			for (int i = 0;i < 5;i++)
    			{
    				for (int j = 0;j < 5;j++)
    				{
    					if (ss[j] == s[i] && b[j] == 0)
    					{
    						num++;
    						b[j]++;
    					}
    				}
    			}
    			if (num == 5)
    				cout << "YES" << endl;
    			else
    				cout << "NO" << endl;
    		}
    	}
    	return 0;
    }
    
    
    


1009 Colourblindness

给定两个由 RGB 组成的字符串,R代表红色,G 代表绿色,B 代表蓝色。Vasya 有色盲症因此他无法分辨蓝色和绿色。问这两个字符串在 Vasya 眼里是否相同。

每个测试点包含多组询问。

思路:所有绿色换成蓝色比较是否相等

ac代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
#define ll long long 
//typedef long long ll;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		int t;
		cin >> t;
		string s1, s2;
		cin >> s1 >> s2;
		for (int i = 0;i <s1.size();i++)
		{
			if (s1[i] == 'G')
				s1[i] = 'B';
		}
		for (int i = 0;i < s2.size();i++)
		{
			if (s2[i] == 'G')
				s2[i] = 'B';
		}
		if (s1 == s2)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}



1010 J - Word Game

t 组数据,每组一个 n 表示字符串数量,之后三行每行 n 个长度为 3 的字符串,表示三个小朋友各自的字符串。

如果一个字符串只有一个小朋友有,那他得三分。如果有两个小朋友有,两人各得一分。若三人都有,不得分。

求出三个小朋友最终得分。

思路:看到觉得能用桶排做,排每个字符串出现的次数

小技巧:map”下标string“字符串

ac代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
#define ll long long 
//typedef long long ll;
string a[4][1005], b[4][1005], c[4][1005];
map < string, int >num;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		num.clear();
		int n;
		cin >> n;
		int ccc[5];
		for (int i = 0;i < 3;i++)
		{
			for (int j = 0;j < n;j++)
			{
				cin >> a[i][j];
				num[a[i][j]]++;
			}
		}
		for (int i = 0;i < 3;i++)
		{
			int cnt = 0;
			for (int j = 0;j < n;j++)
			{
				if (num[a[i][j]] >= 3)
					continue;
				else if (num[a[i][j]] == 2)
					cnt += 1;
				else if (num[a[i][j]] == 1)
					cnt += 3;
			}
			ccc[i] = cnt;
		}
		for (int i = 0;i < 3;i++)
		{
			if (i != 0)
				cout << " " << ccc[i];
			else
				cout << ccc[i];
		}
		cout << endl;
	}
	return 0;
}



1011.K - Technical Support

给定一个只包含大写字母 Q 和 A 的字符串,如果字符串里的每一个 Q 都能与在其之后的 A 一一对应地匹配,则输出字符串Yes,否则输出字符串 No。注意,可以有A 没有被匹配,但每个Q 必须成功地匹配。

输入格式

输入一个只包含大写字母Q 和A 的字符串。

输出格式

输出字符串Yes 或No。

思路 :用cnt暂存后面的a数量,扫到a时-1,扫到q时+1,小于0时清零,然后若结束时cnt>0则说明有q不能配对

ac代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
#define ll long long 
//typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		string s;
		int q = 0, a = 0;
		int t;
		cin >> t;
		cin >> s;
		int cnt=0;
		for (int i = 0;i < s.size();i++)
		{
			if (s[i] == 'Q')
				cnt++;
			else
				cnt--;
			if (cnt < 0)
				cnt = 0;
		}
		if (!cnt)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}



1012 L - Kevin and Permutation

题意:求一个 1 ∼n 的排列 p,使得每个数两端相减绝对值的较小数最大。

输出该排列

思路:分奇偶性考虑

i从n/2开始倒序输出

总体是每输出一个数i,再在它后面输出一个i+n/2

n为奇数时,最后再输出一个n

n位偶数时不用

详细论证我实力不够建议百度。

ac代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
#define ll long long 
//typedef long long ll;
int a[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		int k = n / 2;
		if (n % 2)
		{
			for (int i = n/2;i >=1;i--)
				cout << i << " " << i + k<<" ";
			cout << n << endl;;
		}
		else
		{
			for (int i = n/2;i >=1;i--)
			{
				if (i != 1)
					cout << i << " " << i + k << " ";
				else
					cout << i << " " << i + k;
			}
				cout << endl;
		}
	}
	return 0;
}



就是这样,加油ak!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值