AtCode nadafes2022_day1 **A - Max Inversion**

博客内容讲述了如何计算一个长度为N的搅乱顺序数组(即数组元素i不等于i的位置)的最大颠倒数。颠倒数是指满足条件i<j且数组中位置i的元素大于位置j的元素的整数对(i,j)的数量。对于偶数长度的数组,直接倒序即可得到最大颠倒数;而对于奇数长度的数组,倒序后中间位置的元素需要与相邻元素交换以保持搅乱顺序,从而最大化颠倒数。给出的解决方案中,通过考虑数组长度的奇偶性,利用数学公式快速计算出最大颠倒数。

标签

  • 数学

题目地址

A - Max Inversion

  • https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_a

問題描述

長さ N の攪乱順列の転倒数の最大値を求めてください。

攪乱順列とは、1 ≤\le i ≤\le Nに対して Pi_ii ≠\neq= i を満たす順列のことです。

転倒数とは、1 ≤\le i < j ≤\le Nかつ Pi_ii > Pj_jj を満たす整数の組 (i,j)の個数です。

制約

  • 入力は全て整数である。
  • 2 ≤\le N $\le101010^9$

入力

入力は以下の形式で標準入力から与えられる。

N

出力

答えを 1 行に出力してください。


入力例 1

3

出力例 1

2

P=(2,3,1) の場合、これは攪乱順列であり、転倒数は 2です。

  • (2,1)和(3,1)满足条件,所以是:2

P=(3,2,1)の場合、転倒数が 3 となりますが P2_22 = 2 であるためこれは攪乱順列ではありません。

题意

  • 求颠倒数数组最大个数,颠倒数满足下面条件
    • 数据颠倒后Pi_ii ≠\neq= i
    • 满足:1 ≤\le i < j ≤\le N 并且 Pi_ii > Pj_jj 整数数组 (i,j)的个数

思路

从1开始的整数组,为保证颠倒数最多,越大的值必定越往前放,因为可以保证后面的值基本都小于它

  • 如果n为偶数,则可以保证将其倒序后每一位必定不与其本身的值相等

  • 如果是奇数,倒序后数组的中间值会与其原来的数字相同,此时将中间值与其相邻的值互换,则保证了利益最大化,即只会减少一种可能性

每一位都有n - i - 1位数字小于它,可用级数求和的公式推算

题解

小码匠

#include <bits/stdc++.h>
#include <cstdlib>

using namespace std;

#define endl '\n';

int main() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // 输入
    long long n;
    cin >> n;
    cout << fixed;
    if(n % 2 == 0) {
        cout << (n - 1) * n / 2;
    } else {
        cout << (n - 1) * n / 2 - 1;
    }

    return 0;
}

参考题解1

  • 思路一致
void solve() {
	ll n; 
	cin >> n;
	ll ans = n * (n - 1) / 2;
	if (n % 2) ans--;
	cout << ans << "\n";
}
 
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

参考题解2

  • 不需要判断奇偶性,一个字,妙!
using ll = long long;
#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res = 0,buf = 0;
  bool judge = true;
  ll n;
  cin >> n;
  cout << n * (n-1) / 2 - n % 2 << endl;
  return 0;
}

复盘

  • 数据溢出问题,题目中数据范围:2 ≤\le N $\le101010^9$
    • 进行乘法运算时,N过大时会超过10位,不能开int,应该定义long long
    • 这是最近两天做题犯的第三次因为开的过小导致提交WA
/** * Configures the selected GPIO as a digital output. * * @param[in] pmic_chip Each PMIC device in the systems is enumerated * starting with zero. * @param[in] gpio GPIO to configure as digital output. See * #pm_gpio_which_type. * @param[in] out_buffer_config GPIO output buffer configuration (CMOS or open * drain). See #pm_gpio_out_buffer_config_type. * @param[in] voltage_source GPIO voltage source. See * #pm_gpio_voltage_source_type. * @param[in] source Select the source. See #pm_gpio_source_config_type. * @param[in] out_buffer_strength GPIO output buffer strength. See * #pm_gpio_out_buffer_drive_strength_type. * @param[in] out_inversion Invert the output of EXT_PIN. * * @return * SUCCESS or Error -- See #pm_err_flag_type. * * <b>Example </b> \n * The keypad scan module requires GPIO to drive and sense the keypad. \n * The keypad drive signal is to be configured as an open-drain output with * low drive strength \n * The keypad sense module is to be configured as an input with 1.5uA pull up * + 30uA boost, \n * e.g., reference voltage VIN2 for both drive and sense lines. \n * Configure GPIO5 as a drive signal: * @code * errFlag = pm_gpio_config_digital_output(PM_GPIO_5, * PM_GPIO_OUT_BUFFER_CONFIG_OPEN_DRAIN, * PM_GPIO_VIN2, * PM_GPIO_SOURCE_SPECIAL_FUNCTION1, * PM_GPIO_OUT_BUFFER_LOW, * FALSE); @endcode */ pm_err_flag_type pm_dev_gpio_config_digital_output ( uint8 pmic_chip, pm_gpio_perph_index gpio, pm_gpio_out_buffer_config_type out_buffer_config, pm_gpio_volt_src_type voltage_source, pm_gpio_src_config_type source, pm_gpio_out_buffer_drv_strength_type out_buffer_strength, boolean out_inversion ); pm_err_flag_type pm_gpio_config_digital_output ( pm_gpio_perph_index gpio, pm_gpio_out_buffer_config_type out_buffer_config, pm_gpio_volt_src_type voltage_source, pm_gpio_src_config_type source, pm_gpio_out_buffer_drv_strength_type out_buffer_strength, boolean out_inversion );
03-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码匠和老码农

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值