跳到正文

题解

题解:CF2108A Permutation Warm-Up

Baiqi_0327约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年5月15日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 1 分钟
获赞
1
题目
CF2108A

版本

  1. v1当前

CF2108A

主要思路

f(p)f(p) 的取值均为偶数,而且最小值是 00。那么最大值:

max⁡f(p)=∑∣(n+1−i)−i∣\max f(p)=\sum|(n+1-i)-i|

如果 nn 是偶数,则答案就是 n22\frac{n^2}{2},反之奇数,答案就是 n2−12\frac{n^2-1}{2}。

由于我们只取偶数,所以不同值的个数也根据 nn 的奇偶性得来,如果 nn 是偶数,则答案就是 n24+1\frac{n^2}{4}+1,反之奇数,答案就是 n2−14+1\frac{n^2-1}{4}+1。所以直接输出 ((n * n - n % 2) / 4) + 1 即可。

代码

C++
#include <iostream>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t, n;
    cin >> t;
    while (t--){
        cin >> n;
        cout << (((1LL * n * n - n % 2) / 4) + 1) << "\n";
    }
    return 0;
}
原文