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;
}
}