2021-11-04

A - Linear Keyboard

题意:

题解:

把字母表重新排一下,然后求两个字母间的距离之差的绝对值。

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		string s1,s2;
		cin>>s1>>s2;
		int p[300];
		for(int i=0;i<26;i++){
			p[s1[i]]=i;
		}
		int ans=0;
		for(int i=1;i<s2.length();i++){
			ans+=abs(p[s2[i]]-p[s2[i-1]]);
		}
		cout<<ans<<endl;
	}	
}

B - Odd Grasshopper

题解:

如果给你的是偶数,那么就是-,+,+,-,-,+,+,-,-......如果给你的是奇数,那么就是+,-,-,+,+,-,-,+,+......我们可以看出除了第一个之外,后面每四个加起来都有规律,偶数时是-4,奇数时是4......

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int t;
        cin >> t;
        while (t--)
        {
                ll x, n;
                cin >> x >> n;
                if (n == 0)
                {
                        cout << x << endl;
                }
                else
                {
                        if (x % 2 == 0)
                        {
                                x--;
                                n--;
                                x = x + n / 4 * (-4);
                                if (n % 4 == 0)
                                {
                                        cout << x << endl;
                                }
                                else if (n % 4 == 1)
                                {
                                        x += (n + 1);
                                        cout << x << endl;
                                }
                                else if (n % 4 == 2)
                                {
                                        x += (2 * n + 1);
                                        cout << x << endl;
                                }
                                else if (n % 4 == 3)
                                {
                                        x += (n - 2);
                                        cout << x << endl;
                                }
                        }
                        else
                        {
                                x++;
                                n--;
                                x = x + n / 4 * 4;
                                if (n % 4 == 0)
                                {
                                        cout << x << endl;
                                }
                                else if (n % 4 == 1)
                                {
                                        x -= (n + 1);
                                        cout << x << endl;
                                }
                                else if (n % 4 == 2)
                                {
                                        x -= (2 * n + 1);
                                        cout << x << endl;
                                }
                                else if (n % 4 == 3)
                                {
                                        x -= (n - 2);
                                        cout << x << endl;
                                }
                        }
                }
        }
        return 0;
}

C - Minimum Extraction

题解:

先排序,然后依次求最小值出来,可以肯定的是我们从最小的开始选的话,在这个过程中每个数都会减去之前删的那个数,在删到某个数前,它必然会变成他和前面那个数的差值,因为,我们从第一个开始,到删到它前面一个数时,这两个数的差值一直没变,所以最大的最小值其实就是排序后的数组的两两相邻数的差值的最大值。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int a[maxn];

int main()
{
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int t;
        cin >> t;
        while (t--)
        {
                int n;
                cin >> n;
                for (int i = 1; i <= n; i++)
                {
                        cin >> a[i];
                }
                sort(a + 1, a + 1 + n);
                int ans = -1000000001;
                for (int i = 1; i <= n; i++)
                {
                        ans = max(ans, a[i] - a[i - 1]);
                }
                cout << ans << endl;
        }
        return 0;
}

D - Blue-Red Permutation

题解:

排 序 + 贪 心 排序+贪心排序+贪心,判断能否操作红蓝元素使得数组变成一个全排列,我们可以将a aa数组排个序,判断每一位是否合法,遇到不合法的话直接P u t s ( " N O " ) Puts("NO")Puts("NO")返回就好了,否则就是都是合法的,最后输出Y E S YESYES。然后进行贪心操作即可

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a[n+5];
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		string s;
		vector<int>p1,p2;
		cin>>s;
		for(int i=0;i<n;i++){
			if(s[i]=='B'){
				p1.push_back(a[i+1]);
			}
			else{
				p2.push_back(a[i+1]);
			}
		}
		sort(p1.begin(),p1.end());
		sort(p2.begin(),p2.end(),greater<int>());
		int ok=1;
		for(int i=0;i<p1.size();i++){
			if(p1[i]<=i){
				ok=0;
				break;
			}
		}
		for(int i=0;i<p2.size();i++){
			if(p2[i]>n-i){
				ok=0;
				break;
			}
		}
		if(ok){
			cout<<"Yes"<<endl;
		}
		else cout<<"No"<<endl;
	}	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值