Codeforces 1017C The Phone Number(瞎搞)

题目链接:The Phone Number

题意

构造一个 1 1 n 的排列,使得这个排列中最长上升子序列的长度加上最长下降子序列的长度最小。

输入

输入为一个整数 n (1105) n   ( 1 ≤ 10 5 )

输出

输出 n n 个整数,为 1 n n 的排列,如果有多解输出任意一组。

样例

输入
4
输出
3 4 1 2
提示
最长上升子序列的长度为 2,最长下降子序列的长度为 2 2 ,总和为 4,是所有排列中最小的,但不是唯一的答案。

输入
2
输出
2 1
提示
最长上升子序列的长度为 1 1 ,最长下降子序列的长度为 2,总和为 3 3 ,是所有排列中最小的,但不是唯一的答案,序列 1 2 也是合法的答案。
题解

n n 分成 m 块3,其中每一块中的数字是递增的,各个块之间是递减的,则最长上升子序列的长度和最长下降子序列的长度和最小,为 m+nm m + ⌈ n m ⌉ ,由基本不等式可知,当 m m n m+nm m + ⌈ n m ⌉ 的值最小。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;

#define LL long long
const int maxn = 100000 + 100;
int n;
int num[maxn];

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::sync_with_stdio(false);

    while(scanf("%d", &n) != EOF) {
        int Index = sqrt(n);
        int tmp = 1;
        int Min = n;
        for(int i = n - Index; i >= 0; i -= Index) {
            Min = i;
            for(int j = 0; j < Index; ++j) {
                num[i + j] = tmp++;
            }
        }
        for(int i = 0; i < Min; ++i) {
            num[i] = tmp++;
        }
        for(int i = 0; i < n; ++i) {
            if(i != 0) {
                printf(" ");
            }
            printf("%d", num[i]);
        }
        printf("\n");
    }

    return 0;
}
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值