A
Solution
用map记录键盘的值,遍历字符串(从1开始),每次贡献相邻字符在map中的值的差值的绝对值。
Code
/*************************************
* @problem: cf_init.cpp.
* @author: iamshroud.
* @time: 2021-11-02. 周二
*************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll t;
string a, b;
void solve()
{
// cout << "ans == ";
cin >> a >> b;
map<char, int>mp;
for(int i=0; i<a.length(); i++)
{
mp[a[i]] = i;
}
ll ans = 0;
for(int i=1; i<b.length();i++)
{
ans += abs(mp[b[i]]-mp[b[i-1]]);
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
solve();
return 0;
}
B
Solution
打表不难看出,无论奇偶,每四次变回自身,将n余4后,模拟过程即可。
Code
/*************************************
* @problem: cf_init.cpp.
* @author: iamshroud.
* @time: 2021-11-02. 周二
*************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <list>
using namespace std;
typedef long long ll;
#define MOD 1000000007
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll t;
ll x, n;
void solve()
{
// cout << "ans == ";
x = read(), n = read();
ll tt = n/4;
ll left = n%4;
if(!left)
{
cout << x << endl;
}
else
{
ll tmp = 4*tt;
for(ll i=tmp+1; i<=tmp+left; i++)
{
if(x & 1)
{
x += i;
}
else
{
x -= i;
}
}
cout << x << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
t = read();
while (t--)
{
solve();
}
return 0;
}
C
Solution
直接模拟过程,取最小值用优先队列小根堆维护即可。
Code
/*************************************
* @problem: cf_init.cpp.
* @author: iamshroud.
* @time: 2021-11-02. 周二
*************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <list>
using namespace std;
typedef long long ll;
#define MOD 1000000007
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll t;
ll n;
void solve()
{
// cout << "ans == ";
priority_queue <ll,vector<ll>,greater<ll> > pq;
n = read();
// int flag = false;
for(int i=1; i<=n; i++)
{
ll tmp;
tmp = read();
pq.push(tmp);
}
ll delta = 0;
ll ans = -1000000007;
for(int i=1; i<=n; i++)
{
ll node = pq.top()+delta;
pq.pop();
ans = max(ans, node);
delta -= node;
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
t = read();
while (t--)
{
solve();
}
return 0;
}
D
Solution
蓝色放前面红色放后面,同色里面从小到大排序。
遍历每一个数,如果蓝色小于当前下标(不可以通过减到达),或者红色大于当前下标(不可以通过加到达),输出NO
否则输出YES
Code
/*************************************
* @problem: cf_init.cpp.
* @author: iamshroud.
* @time: 2021-11-02. 周二
*************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <list>
using namespace std;
typedef long long ll;
#define MOD 1000000007
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
ll t;
ll n;
struct node
{
int value;
int delta;
}arr[200005];
bool cmp(node a,node b)
{
if(a.delta != b.delta)
return a.delta < b.delta;
return a.value < b.value;
}
void solve()
{
// cout << "ans == ";
cin >> n;
for(int i=1; i<=n;i++)
cin >> arr[i].value;
for(int i=1; i<=n;i++)
{
char ch;
cin >> ch;
if(ch == 'B')
arr[i].delta = -1;
else
arr[i].delta = 1;
}
sort(arr+1, arr+n+1, cmp);
for(int i=1; i<=n; i++)
{
if(arr[i].delta == -1)
{
if(arr[i].value < i)
{
cout << "NO\n";
return ;
}
}
else
{
if(arr[i].value > i)
{
cout << "NO\n";
return ;
}
}
}
cout << "YES\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
solve();
}
return 0;
}
E
Solution
记录某一时刻机器人能到达的最高,最低,最左,最右的点,如果越界,就输出当前一步的状态。
如果遍历完了还没越界,在限定条件下输出即可。
Code
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <list>
#include <map>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define itn int
#define MOD 1000000007
#define intmax 2147483647
#define memmax 0x7fffffff
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
ll t;
ll n, m;
string s;
void solve()
{
cin >> n >> m;
cin >> s;
int up = 0, down = 0, left = 0, right = 0;
int x = 0, y = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'U')
{
// 记录向上最大移动距离
x++;
up = max(up, x);
if (up + down >= n)
{
cout << up << ' ' << 1 + left << endl;
return;
}
}
else if (s[i] == 'D')
{
// 记录向下最大移动距离
x--;
down = max(down, -x);
if (up + down >= n)
{
cout << n - down + 1 << ' ' << 1 + left << endl;
return;
}
}
else if (s[i] == 'L')
{
// 记录向左最大移动距离
y++;
left = max(left, y);
if (left + right >= m)
{
cout << 1 + up << ' ' << left << endl;
return;
}
}
else if (s[i] == 'R')
{
// 记录向右最大移动距离
y--;
right = max(right, -y);
if (left + right >= m)
{
cout << 1 + up << ' ' << m - right + 1 << endl;
return;
}
}
}
cout << 1 + up << ' ' << 1 + left << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
solve();
}