Insertion Sort——打表找规律

【题目描述】

Insertion sort is a simple sorting algorithm that builds the final sorted array one item at an iteration.

More precisely, insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

This type of sorting is typically done in-place, by iterating up the array, growing the sorted array behind it. At each array-position, it checks the value there against the largest value in the sorted array (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted array, shifts all the larger values up to make a space, and inserts into that correct position.

The resulting array after k iterations has the property where the first k entries are sorted. In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result.

Knuth is an ACM-ICPC master and provides a modified pseudocode implementation about the insertion sort for you. His modified algorithm for an array of sortable items A (1-based array) can be expressed as:


He notes that a permutation of 1 to n is almost sorted if the length of its longest increasing subsequence is at least (n−1).

Given the parameter k, you are asked to count the number of distinct permutations of 1 to n meeting the condition that, after his modified insertion sort, each permutation would become an almost sorted permutation.

Input
The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 5000.

For each test case, the only line contains three integers n,k and q indicating the length of the permutations, the parameter in his implementation and a prime number required for the output respectively, where 1≤n,k≤50 and 108≤q≤109.

Output
For each test case, output a line containing "Case #x: y" (without quotes), where x is the test case number starting from 1, and y is the remainder of the number of permutations which meet the requirement divided by q.

Example
Input
4
4 1 998244353
4 2 998244353
4 3 998244353
4 4 998244353
Output
Case #1: 10
Case #2: 14
Case #3: 24
Case #4: 24
Note
In the first sample case, we can discover 10 permutations which meet the condition, and they are listed as follows:

[1,2,3,4];
[1,2,4,3];
[1,3,2,4];
[1,3,4,2];
[1,4,2,3];
[2,1,3,4];
[2,3,1,4];
[2,3,4,1];
[3,1,2,4];
[4,1,2,3].

【题目分析】

这是一道打表题。自己以前对这种题很没有经验,因为不太习惯这种思维方式,虽然清楚找出来的规律肯定是有其深层含义的,但是直接找这种规律是很困难的,而且对于竞赛而言也并不需要理解公式的来源,能够解决问题就行,当然探求问题本质的习惯很好,但是对于竞赛我们很多时候要不求甚解,大胆尝试不去求证。不管黑猫白猫,能够抓到老鼠就是好猫,可能在思维层次上两个有优劣,但是从解决问题的角度都是一样的,不能因为觉得这样没水平或者是没有找到问题的本质就对这种有效解决问题的方式抵触。只能怪自己的思维不允许自己一下看出问题的关键而只能通过这种方式帮助解决问题。

而且快速找到规律也是一种能力,不能过分纠结于细节,这是为了找到规律而不是为了探求为什么。

【AC代码】

打表代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cctype>
#include<queue>
#include<set>

using namespace std;

typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=30;

int nn,k;
int a[MAXN];
int b[MAXN];
int dp[MAXN];

int deal(int n)
{
	for(int i=1;i<=n;i++)
	{
		dp[i]=1;
	}
	int ret=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<i;j++)
		{
			if(b[j]<b[i] && dp[j]+1>dp[i]) dp[i]=dp[j]+1; 
		}
		if(dp[i]>ret) ret=dp[i];
	}
	return ret;
}

int main()
{
	nn=10;
	for(int n=1;n<=nn;n++)
	{
		printf("[%d]\t",n);
		for(int i=1;i<=n;i++) a[i]=i;
		for(int k=1;k<=n;k++)
		{
			int cnt=0;
			do
			{
				memcpy(b,a,sizeof(a));
				sort(b+1,b+1+k);
				if(deal(n)>=n-1) cnt++;
			}while(next_permutation(a+1,a+n+1));
			printf("%d\t",cnt);
		}
		printf("\n");
			
	}
	return 0;
}

AC代码

#include<stdio.h>
int main()
{
	long long t,n,k,q,cot=0;
	scanf("%lld",&t);
	while(cot!=t)
	{
		cot++;
		scanf("%lld%lld%lld",&n,&k,&q);
		if(k>n)k=n;
		long long ans=0,temp=1;
		for(long long i=1;i<=k;i++)
		{
			temp*=i;temp%=q;
		}
		ans=temp;
		long long tz=k;
		for(long long i=k+1;i<=n;i++)
		{
			ans+=temp*k;ans%=q;
			k+=2;
		}
		printf("Case #%lld: %lld\n",cot,ans);
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值