ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation

2019-11-05 13:02:32  阅读:510  来源: 互联网

标签:Contest int Interesting permutation Programming flag each dp MOD


链接:

https://codeforces.com/gym/102394/problem/I

题意:

DreamGrid has an interesting permutation of 1,2,…,n denoted by a1,a2,…,an. He generates three sequences f, g and h, all of length n, according to the permutation a in the way described below:

For each 1≤i≤n, fi=max{a1,a2,…,ai};
For each 1≤i≤n, gi=min{a1,a2,…,ai};
For each 1≤i≤n, hi=fi−gi.
BaoBao has just found the sequence h DreamGrid generates and decides to restore the original permutation. Given the sequence h, please help BaoBao calculate the number of different permutations that can generate the sequence h. As the answer may be quite large, print the answer modulo 109+7.

思路:

考虑,某个位置为n或者h[i] > h[i+1] ,和第一个位置不为0时,答案都为0.
其他情况,h[i] == h[i+1],考虑前面的空位数, h[i] < h[i+1],可以插最小值,也可以插最大值, 直接×2(没太懂)。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
 
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
 
LL dp[MAXN];
int a[MAXN];
int n;
 
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%d", &n);
        a[0] = 0;
        bool flag = true;
        for (int i = 1;i <= n;i++)
        {
            scanf("%d", &a[i]);
            if (a[i] < a[i-1] || a[i] >= n || (i != 1 && a[i] == 0))
                flag = false;
        }
        if (a[1] != 0)
            flag = false;
        if (!flag)
        {
            puts("0");
            continue;
        }
        dp[1] = 1;
        for (int i = 2;i <= n;i++)
        {
            if (a[i] > a[i-1])
                dp[i] = (dp[i-1]*2)%MOD;
            else
            {
                LL sp = a[i]-i+2;
                dp[i] = (dp[i-1]*sp)%MOD;
            }
        }
        printf("%d\n", (int)(dp[n]%MOD));
    }
 
    return 0;
}

标签:Contest,int,Interesting,permutation,Programming,flag,each,dp,MOD
来源: https://www.cnblogs.com/YDDDD/p/11797745.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有