【51Nod1296】有限制的排列

面试题:有限制条件的全排列数 给定一个数组,例如 [45,54,2,99,1] 给定一个长度l,例如100 可以对数组中相邻的两个数交换,前提必须满足两个数的和不大于l 求通过这种交换所能够产生的全排列有多少种?对于相同的数,也默认它们不同。 这是某外企的电面题,感觉要找到比较好的算法还是挺难的。目前想到的是一种循环暴力搜索加暴力剪枝并回溯的,从测试数据来看,效果还凑合。 思路是: 暴力搜索:查看任何 阅读详情

计算整数集合{1,2,3,4, …. N }满足下列条件的的排列个数:

在位置a1, a2, …, aK小于其邻居(编号从0开始)。
在位置b1, b2, …, bL大于其邻居。

输出符合条件的排列数量Mod 1000000007的结果。例如:N = 4,a = {1}, b = {2},符合条件的排列为:

2 1 4 3
3 2 4 1
4 2 3 1
3 1 4 2
4 1 3 2
Input
第1行:3个数N, K, L,分别表示数组的长度,限制a的长度,限制b的长度(1 <= N <= 5000, 1 <= K, L <= N)。
第2 - K + 1行:每行一个数,对应限制a的位置(1 <= ai <= N - 2)
第K + 2 - K + L + 1行:每行一个数,对应限制b的位置(1 <= bi <= N - 2)
Output
输出符合条件的排列数量Mod 1000000007的结果。
Input示例
4 1 1
1
2
Output示例
5

题解
dp[i][j]表示i位置,插入j的方案数。我们可以记录每个位置与前一个的位置关系进行转移。
1,i比i-1位置大,dp[i][j]+=dp[i-1][1~j-1]
2,i比i-1位置小,dp[i][j]+=dp[i-1][j~i-1](此处是一个插入技巧:第i个位置插入1~i,当插入的数为j时,把j~i-1的数加1,这样就不会重复插入同一个数了)
3,dp[i][j]+=dp[i-1][1~i-1]
可用前缀和优化至N^2

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#define mod 1000000007
#define ll long long 
#define N 10000005 
#define inf 0x7fffffff
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,k,l,dp[5005],sum[5005],flag[5005];
int main()
{
    n=read();k=read();l=read();
    for (int i=1;i<=k;i++)
    {
        int x=read()+1;
        flag[x]=1;flag[x+1]=2;
    }
    for (int i=1;i<=l;i++)
    {
        int x=read()+1;
        flag[x]=2;flag[x+1]=1;
    }
    sum[1]=dp[1]=1;
    for (int i=2;i<=n;i++)
    {
        for (int j=1;j<=i;j++)
        {
            if (flag[i]==1) dp[j]=(sum[i-1]-sum[j-1]+mod)%mod;
            else if (flag[i]==2) dp[j]=sum[j-1];
            else dp[j]=sum[i-1];
        }
        for (int j=1;j<=i;j++)
            sum[j]=(sum[j-1]+dp[j])%mod;
    }
    printf("%d",sum[n]);
    return 0;
}
51nod1296 有限制的排列 递推+前缀和优化 1296 有限制的排列 题目来源: HackerRank基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注计算整数集合{1,2,3,4, .... N }满足下列条件的的排列个数:在位置a1, a2, ..., aK小于其邻居(编号从0开始)。在位置b1, b2, ..., bL大于其邻居。输出符合条件的排列数量Mod 1000000007的结果。例如:... 阅读详情

相关推荐

BQ25798双电源输入4节电池升降压充电与最大功率跟踪于光伏板应用

介绍一颗具有窄范围直流电压NVDC、路径管理PPMT、光伏板最大功率跟踪及反向放电OTG升降压充电控制器,带I2C接口能对内部寄存器做控制

linkedin_38804946的博客 1688

51Nod 1296 - 有限制的排列(DP)

【题目描述】 【思路】 做这道题首先要知道一种全排列的生成方式:如果要生成 [1,n][1,n][1,n] 的全排列,考虑递推关系,如果现在所有 [1,n−1][1,n-1][1,n−1] 的排列都是已知的,那么假设 [1,n][1,n][1,n] 中的一个排列末尾元素为 xxx ,那么只要把 [1,n−1][1,n-1][1,n−1] 中所有 &...

weixin_30684743的博客 194

LVGL 离线字体转换lv_font_conv本地安装

LVGL FONT CONV 离线版本

liu132641的专栏 3401

【题解】有限制的排列 [51nod1296]

【题解】有限制的排列 [51nod1296] 传送门:有限制的排列 \([51nod1296]\) 【题目描述】 给出 \(n,m_1,m_2\) 和 \(\{a[1],a[2]...a[m_1]\},\{b[1],b[2]...b[m_2]\}\) 计算整数 \([1,n]\) 满足以下条件的排列个数: 位置 \(a[i]\) \((i \in [1,m_1])\) 上的数均小于其邻居...

aiauw80822的博客 315

Appstore 3.2(f)违规自救指南:从触发到申诉的完整应对策略

本文详细解析了Appstore 3.2(f)违规的应对策略,从触发原因到申诉流程,帮助开发者有效自救。文章涵盖隐瞒行为、账户关联和信息造假三大违规场景的判定逻辑,并提供申诉信写作的黄金法则和长期合规防御措施,确保开发者能够顺利通过苹果审核。

weixin_29304985的博客 667

U盘文件变exe?3步教你用火绒+批处理脚本恢复隐藏文档(附注册表修复)

本文详细解析了U盘文件被病毒隐藏并伪装成exe文件的原理,并提供了三步解决方案:首先使用火绒安全软件进行全盘查杀清除病毒;其次通过批处理脚本批量恢复被隐藏的文件属性;最后修复被篡改的注册表设置,彻底解决文件无法显示的问题,帮助用户安全找回数据并加固系统防护。

weixin_28622215的博客 921

51Nod - 1296 有限制的排列

计算整数集合{1,2,3,4, .... N }满足下列条件的的排列个数: 在位置a1, a2, ..., aK小于其邻居(编号从0开始)。 在位置b1, b2, ..., bL大于其邻居。 输出符合条件的排列数量Mod 1000000007的结果。例如:N = 4,a = {1}, b = {2},符合条件的排列为: 2 1 4 3 3 2 4 1 4 2 3 1 ...

brucehb的专栏 613

计算数列位置N的值

本文介绍了一个数列生成问题,要求根据输入的两个整数M和N生成特定规则的数列,并输出第N个位置上的数值。数列生成规则为:前M个元素为1到M;后续元素根据前M个元素是否重复来决定计算方式(求和或求差)。文章提供了C++、Java和Python三种语言的实现方案,均包含输入处理、数列初始化、循环计算和结果输出等步骤。各语言实现中均使用集合去重特性判断重复元素,并通过内置函数或自定义方法求取最大值和最小值。该问题主要考察基础编程能力,包括数组/列表操作、最值计算和重复元素判断等基本编程技巧。

面试高手的博客 671

51Nod-1296-有限制的排列

ACM模版描述题解个人感觉,这个应该算是数位 dp。先通过处理输入数据获取一个 state[],表示每相邻两项之间的大小关系,state[i] = 0,表示无特别关系,state[i] = 1 表示第 i 项小于第 i - 1 项,state[i] = 2 表示第 i 项大于第 i - 1 项。接着搞一个 dp[i][j] 表示由前 i 个数组成的序列且第 i 位为 j 的合法情况数。在规划的过程中

逐梦者 1217

Open3D 使用RANSAC分割平面【2026最新版】

使用RANSAC从点云中分割平面,用segement_plane函数。博客长期更新,本文最新更新时间为:2026年2月28日。

点云侠的博客 37万+

【ARXML专题】-2-创建一个Classic CAN(经典CAN2.0)/CANFD的系统描述ARXML文件

目录 1 创建一个AUTOSAR系统描述 2 物理层命名以及创建ECU 3 新建一个Frame 4 新建一个PDU 5 新建一个signal 6 结尾 AUTOSAR System Description Network Explorer是一个文件编辑器,支持可视化和编辑系统描述的AUTOSAR系统描述文件和ECU摘录的通信内容,它支持架构版本4.0.3、4.1.2、4.2.1、4.2.2和4.3.0。AUTOSAR系统描述(ARXML)文件是基于XML的文件格式,描述网络通信的元素...

「汽车电子助手」的博客 1万+

51nod 1296 有限制的排列 【动态规划】

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1296 ——————————————————————————————————————————— 1296 有限制的排列 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 计算

Tabris的博客 670

SAP ABAP CDS View 里的日期函数

摘要:本文系统介绍了ABAP CDS日期函数在企业级应用中的关键作用,包括账龄计算、合同续期等业务场景。重点解析了DATS_IS_VALID、DATS_DAYS_BETWEEN等核心函数的语义和使用边界,特别强调on_error四种异常处理策略的选择逻辑。针对系统日期获取,对比了经典CDS的参数注入和View Entity会话变量两种方式。文章还提供了完整可运行的示例代码(包含CDS视图和ABAP程序),帮助开发者快速验证不同版本下的函数行为差异,确保在实际项目中正确应用这些日期处理函数。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 384

51nod 1296 有限制的排列 [动态规划]

1296 有限制的排列 1296 有限制的排列 题目简洁,不解释. 这个题目在转移的时候用到一个特点: 假定长为len的一个排列 其中的元素只有1->len, 即每个元素各一个, dp[len] [endval] 表示len长度的排列, 其中以endval作为结尾的排列数量. 当它已知时候, dp[len+1] [endval1]可以理解为从上方已知的dp[len] [endval] 的所有排列中, 对值大于等于endval1的元素都把值加上1, 其他的值不变,那么endval1元素顺利成章的放在 &

cg021111的博客 308

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

51nod1296 有限制的排列

链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1296 题意:中文题。。 分析:首先我们要明白如何生成排列,如果之前有一个长度为n的排列,我们增加一位变成n+1位的话,那么我们只要考虑第n+1位是多少,若a[n+1]=x,那么我们只要将原来n个数的排列中的数字[x~n]都加上1,那么我们就把第n+1位插入到了排

Fsss 762

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

51nod-1296: 有限制的排列

【传送门:51nod-1296】 简要题意:   有一个集合,集合中的数为1到n   给出a限制条件,a[i]表示第a[i]位置的数要比相邻位置的数要小   给出b限制条件,b[i]表示第b[i]位置的数要比相邻位置的数要大   求出符合条件的序列个数 题解:   DP   设f[i][j]为i位数,最后一位为j的情况数   我们要将j合理的放在末尾,那么可以把前面&gt...

weixin_30359021的博客 144

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

51nod 1296 有限制的排列

原题hdu4055 #include using namespace std; const int MAXN=5050; const long long mod=1e9+7; int a[MAXN]; long long dp[2][MAXN]; //dp[i][j]表示i位置填好后,末尾为j的答案 int main() { long long n,k,l,x,flag,i,j,ans

月阁 426

51nod 1296 有限制的排列(DP)

  对于一个i,如果要比邻居大,那么i比i-1大,i+1比i小,比邻居小同理。设v[i]=0表示i与i-1的关系无限制,v[i]=1表示a[i-1]>a[i],v[i]=2表示a[i-1]<a[i]   则有   显然这个是可以用前缀和优化成O(N^2)的 #include<iostream> #include<cstring> #inclu...

weixin_30300523的博客 154

51NOD 1296-dp

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1296 题意就是一个 1--n 的序列,然后给你一些数,要再这些位置比旁边大,另一些数,这些位置要比旁边的小。。 题意就是这样,之前做的题拿出来复习。。 dp思路是 dp[i][j]  表示 前i个数最后一位是j是什么的时候有多少可行方案(是不是有点像

zxl博客 776

有限制条件的排列问题

人总是要有梦想的QAQ的博客 606

Exercise(18):排列(有限制条件)

/* 陈潼升 【排列】 题目描述 Description 给出数字n以及x和y,请输出数字1~n的所有排列,但要求排列中x必须在y的左边方向,其它的数位置随意。 输入描述 Input Description 读入整数n、x和y ( 1<=n<=10,x∈[1,n],y∈[1,n] ) 输出描述 Output D

Rioer的博客 1009

乱序且不重复的输出0-99之间的所有整数

如何高效且不重复的输出0 - 99 之间的所有整数? 实现思路如下: (1) 把N个数放到容器A(int数组)中; (2) 从N个数中随机取出1个数,放入容器B(int数组)中; (3) 把容器A中最后一个数与随机抽取的数对调,或者把容器A中最后一个数覆盖随机抽取出来的数; (4) 这时从容器A(假设N个数,索引 0 到 索引 N-2)之间随机取一个数,再放入容器B中,重复此步骤。 说明:也就是说...

长安明月的博客 1606
上一篇: 【bzoj1592】[Usaco2008 Feb]Making the Grade 路面修整
下一篇: 【51Nod1346】递归
愤怒的愣头青
博客等级 码龄10年 28粉丝 476原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址