A. Verify Password
思路:按照ASCLL值进行比较就行(因为字母的ASCLL本来就在数字后面),所以,只要找到前面比后面的数大就输出NO,反之YES
代码实现:
#include<bits/stdc++.h>
using namespace std;
#define N 100005
typedef long long ll;
ll n, m, num, sum, t;
ll a[N], dp[N];
char s[N];
int main()
{
cin >> t;
while (t--)
{
int flag = 0;
cin >> n;
cin >> s + 1;
for (int i = 1; i < n; i++) {
if (s[i] > s[i + 1]) {
flag = 1;
}
}
if (flag == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
B. Increase/Decrease/Copy
思路:
因为对于前n项,我们并不会改变其位置,只有第n+1项,我们进行特判
对于a[i]->b[i],,转变的最小代价就是它们的差值。
对于b[n+1],得到它的最小代价就是a[i]->b[i]过程中与它最小的差值+1
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define N 200005
typedef long long ll;
ll n, m, num, sum, t;
ll a[N], b[N];
char s[N];
int main()
{
cin >> t;
while (t--)
{
int flag=0;
num = 0;
sum = 1e9;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n + 1; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
num += abs(a[i] - b[i]);
sum = min(sum, abs(a[i] - b[n + 1]));
sum = min(sum, abs(b[i] - b[n + 1]));
}
for (int i = 1; i <= n; i++) {
if (a[i] > b[n + 1] && b[i] > b[n + 1] || a[i] < b[n + 1] && b[i] < b[n + 1])
continue;
else {
flag = 1;
}
}
if (flag == 1) {
num += 1;
}
else {
num += 1 + sum;
}
cout << num << endl;
}
return 0;
}
C. Job Interview
思路:
从前往后遍历,检查一下是a能力值浪费了还是b能力值浪费了,然后从后往前枚举,开一个数组维护一下最近后缀损失能力值。输出答案的时候,如果当前的人的站的职位刚好是能力值被浪费的职位,输出总和减去当前的人的能力值加上最近损失能力值
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1000005
#define inf 1e9+7
typedef long long ll;
ll n, m, sum, ans, t, sum1, sum2, num1, num2, f;
ll q[N], p[N], a[N], b[N];
void solve()
{
cin >> n >> m;
sum1 = sum2 = num1 = num2 = f=0;
for (int i = 0; i <= n+m; i++)
cin >> a[i], q[i] = p[i] = 0;
for (int i = 0; i <= n+m; i++) {
cin >> b[i], f += a[i] > b[i];
if (a[i] > b[i] && num1 <= n || m == i - num1)
num1++, sum1 += a[i], p[i] = 1;
else sum1 += b[i];
if (a[i] < b[i] && num2 <= m || n == i - num2)
num2++, sum2 += b[i], q[i] = 1;
else sum2 += a[i];
}
for (int i = 0; i <= n+m; i++, cout << ' ')
cout << (f > n ? (p[i] ? sum1 - a[i] : sum2 - b[i]) : (q[i] ? sum2 - b[i] : sum1 - a[i]));
/*if (f > n) {
if (p[i])
cout << sum1 - a[i] << ' ';
else
cout << sum2 - b[i] << ' ';
}
else {
if (q[i])
cout << sum1 - b[i] << ' ';
else
cout << sum2 - a[i] << ' ';
}*/
}
int main()
{
cin >> t;
while (t--) {
solve();
}
}