Codeforces Round #890 (Div. 2)

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,ai1) .

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 a1a2an. 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:

  1. a i ≠ b i a_i≠b_i ai=bi for all i from 1   t o   n 1 \ to \ n 1 to n ,
  2. 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值