POJ1833 & POJ3187 & POJ3785

要是没有next_permutation这个函数,这些题觉得还不算特别水,不过也不一定,那样可能就会有相应的模板了。反正正是因为next_permutation这个函数,这些题包括之前的POJ1226,都变得简单起来。


排列
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 17486 Accepted: 6970

Description

题目描述: 
大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。 

任务描述: 
给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。 
比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。 

Input

第一行是一个正整数m,表示测试数据的个数,下面是m组测试数据,每组测试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。

Output

对于每组输入数据,输出一行,n个数,中间用空格隔开,表示输入排列的下k个排列。

Sample Input

3
3 1
2 3 1
3 1
3 2 1
10 2	
1 2 3 4 5 6 7 8 9 10

Sample Output

3 1 2
1 2 3
1 2 3 4 5 6 7 9 8 10

直接用next_permutation这个函数即可。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;

int num[1030];

int main()
{
	int Test,N,Q,i;
	cin>>Test;
	while(Test--)
	{
		scanf_s("%d%d",&N,&Q);
		for(i=0;i<N;i++)
			scanf_s("%d",&num[i]);
		for(i=1;i<=Q;i++)
			 next_permutation(num,num+N);
		for(i=0;i<N;i++)
			printf("%d ",num[i]);
		printf("\n");
	}
	return 0;
}

Backward Digit Sums
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5072 Accepted: 2923

Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this: 

    3   1   2   4

      4   3   6

        7   9

         16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities. 

Write a program to help FJ play the game and keep up with the cows.

Input

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

Explanation of the sample: 

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

题意是要输出N个数,这N个数是从1到N这些数的一个顺序,这样的顺序按照杨辉三角的模式相加起来等于sum,输出相等时的第一个字典顺序。

一看到N是大于1小于10的我就想暴力了。。。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	int i,n,sum,a[12];
	cin>>n>>sum;

	for(i=1;i<=10;i++)
		a[i]=i;
	if(n==1)
	{
		cout<<1<<endl;
	}
	else if(n==2)
	{
		cout<<1<<" "<<2<<endl;
	}
	else if(n==3)
	{
		while(1*a[1]+2*a[2]+1*a[3]!=sum)
		{
			next_permutation(a+1,a+3+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
	}
	else if(n==4)
	{
		while(1*a[1]+3*a[2]+3*a[3]+1*a[4]!=sum)
		{
			next_permutation(a+1,a+4+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;
	}
	else if(n==5)
	{
		while(1*a[1]+4*a[2]+6*a[3]+4*a[4]+1*a[5]!=sum)
		{
			next_permutation(a+1,a+5+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<endl;
	}
	else if(n==6)
	{
		while(1*a[1]+5*a[2]+10*a[3]+10*a[4]+5*a[5]+1*a[6]!=sum)
		{
			next_permutation(a+1,a+n+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<endl;
	}
	else if(n==7)
	{
		while(1*a[1]+6*a[2]+15*a[3]+20*a[4]+15*a[5]+6*a[6]+1*a[7]!=sum)
		{
			next_permutation(a+1,a+n+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<endl;
	}
	else if(n==8)
	{
		while(1*a[1]+7*a[2]+21*a[3]+35*a[4]+35*a[5]+21*a[6]+7*a[7]+1*a[8]!=sum)
		{
			next_permutation(a+1,a+n+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<endl;
	}
	else if(n==9)
	{
		while(1*a[1]+8*a[2]+28*a[3]+56*a[4]+70*a[5]+56*a[6]+28*a[7]+8*a[8]+1*a[9]!=sum)
		{
			next_permutation(a+1,a+n+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl;
	}
	else if(n==10)
	{
		while(1*a[1]+9*a[2]+36*a[3]+84*a[4]+126*a[5]+126*a[6]+84*a[7]+36*a[8]+9*a[9]+1*a[10]!=sum)
		{
			next_permutation(a+1,a+n+1);
		}
		cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<" "<<a[10]<<endl;
	}

	return 0;
}

The Next Permutation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 979 Accepted: 717

Description

For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number) than the input number. For example: 

123 -> 132 
279134399742 -> 279134423799 

It is possible that no permutation of the input digits has a larger value. For example, 987.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input value.

Output

For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. If there is a solution, the output should be the data set number, a single space and the next larger permutation of the input digits.

Sample Input

3 
1 123 
2 279134399742 
3 987

Sample Output

1 132
2 279134423799
3 BIGGEST

还是直接使用next_permutation。这个函数是有返回值的,返回值是0时表示已经没有下一个字典顺序了,它要变成第一个字典顺序。返回值是1时表示还有字典顺序的下一个顺序,所以利用函数的这个性质就OK了。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	int Test,num;
	char s[100];

	cin>>Test;
	while(Test--)
	{
		cin>>num>>s;
		cout<<num<<" ";

		int n=next_permutation(s,s+strlen(s));
		
		if(n==0)
			cout<<"BIGGEST"<<endl;
		else
			cout<<s<<endl;
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4785835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在风能领域,准确预测风速对于风电场的运行与管理至关重要。Matlab作为一个强大的数学计算和数据分析平台,被广泛应用于风速预测模型的构建。本文将深入探讨基于四种风速&mdash;&mdash;随机风、基本风、阵风和渐变风的组合风速预测技术。 我们来理解这四种风速类型: 1. **随机风**:随机风是指风速呈现出随机性的变化,通常由大气湍流引起。在建模中,通常通过统计方法如高斯分布或Weibull分布来模拟这种不确定性。 2. **基本风**:基本风速是指在无特定扰动条件下的平均风速,它是长期观测结果的平均值,通常用于结构设计和风能评估。 3. **阵风**:阵风是短时间内风速显著增强的现象,对建筑物和风力发电机造成的主要威胁之一。阵风的预测涉及到风的脉动特性分析。 4. **渐变风**:渐变风是指风速随时间和空间逐渐变化的过程,常见于风向转变或地形影响下的风场变化。 在Matlab中,利用这四种风速类型进行组合预测,可以提高预测的准确性。预测模型可能包括以下几个步骤: 1. **数据收集与预处理**:收集历史风速数据,包括随机风、基本风、阵风和渐变风的数据,进行异常值检测、缺失值填充以及数据标准化。 2. **特征工程**:提取风速变化的相关特征,如平均值、标准差、极值、频率分布等,这些特征可能对预测有重要影响。 3. **模型选择**:可以选择多种预测模型,如时间序列分析(ARIMA、状态空间模型等)、机器学习算法(线性回归、决策树、支持向量机、神经网络等)或深度学习模型(LSTM、GRU等)。 4. **模型训练**:利用历史数据训练选定的模型,调整模型参数以优化性能,例如通过交叉验证来避免过拟合。 5. **模型验证与评估**:使用独立的测试集验证模型预测效果,常见的评估指标有均方误差(MSE)、平均绝对误差(MAE)和决定系数(R&sup2;)。 6. **组合预测**:结合四种风速的不同模型预测结果,可以采用加权平均、集成学习(如bagging、boosting)等方式,以提升整体预测精度。 7. **实时更新与动态调整**:实际应用中,模型需要不断接收新的风速数据并进行在线更新,以适应风场环境的变化。 通过以上步骤,可以构建一个综合考虑各种风速特性的预测系统,这对于风电场的功率输出预测、风电设备的维护计划以及电网调度都具有重要价值。然而,需要注意的是,每个风场的地理环境、气候条件和设备状况都有所不同,因此模型的建立应根据实际情况进行定制和优
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值