A.Tales of a Sort
题目大意
Alphen has an array of positive integers a a a of length n.
Alphen can perform the following operation:
- For all i i i from 1 to n, replace a i a_i ai with max ( 0 , a i − 1 ) \max(0,a_i−1) max(0,ai−1) .
Alphen will perform the above operation until a a a is sorted, that is a a a satisfies a 1 ≤ a 2 ≤ … ≤ a n a_1≤a_2≤…≤a_n a1≤a2≤…≤an. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.
思路
记录最大的逆序对的值即可
#include <bits/stdc++.h>
using namespace std;
const int N = 550;
int a[N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
if (a[i] > a[j])
ans = max(ans, a[i]);
}
cout << ans << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
return 0;
}
B.Good Arrays
题目大意
Let’s call an array of positive integers b of length n good if:
- a i ≠ b i a_i≠b_i ai=bi for all i from 1 t o n 1 \ to \ n 1 to n ,
- a 1 + a 2 + … + a n = b 1 + b 2 + … + b n a_1+a_2+…+a_n=b_1+b_2+…+b_n a1+a2+…+an=b1+b2+…+bn.
思路
这道题将非 1 1 1 的数把值累加到 1 1 1 上面,让自己等于 1 1 1, 然后 1 1 1 变成非 1 1 1 。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve()
{
int n;
cin >> n;
LL cnt1 = 0, sum = 0;
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
if (x == 1)
cnt1++;
else
sum += (x - 1);
}
if (n == 1)
{
puts("NO");
return;
}
if (sum >= cnt1)
{
puts("YES");
return;
}
else
{
puts("NO");
return;
}
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
return 0;
}