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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383989.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383989.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383989.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據鏈路層: 可靠性傳輸 六個協議

可靠性傳輸 1. 差錯控制 發送方將數據幀發送, 但是當發送方發送的是一個 1的時候此時接受方卻接受的是一個 0. (1)校驗 接收方如果幀校驗接受到的幀沒有問題, 則對發送方發送一個肯定性的確認, 當對這個數據幀進行校驗發現這個幀有問題的時候, 此時接受方一種是將這個數據幀…

c語言實現配置文件的讀寫

配置文件的格式如下&#xff1a; key1 value1 key2 value2 . . . 名值對以一個鏈接&#xff0c;一條記錄以換行符分割 頭文件&#xff1a; #include<stdio.h> #include<stdlib.h> #include <string.h> 函數原型&#xff1a; void trim(char *strIn, char *…

Educational Codeforces Round 73 (Rated for Div. 2)

A 很簡單的一個模擬&#xff0c;只要前面的數字有兩個以上就能合成后面的&#xff0c;我們進行一遍合成看能不能出現2048就可以了。 #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include&…

數據鏈路層: HDLC

一. 協議機 發送方和接收方. 同時有限狀態機把協議形式化為一個四元組 (S,M,I,T), 其中你S表示進程和信道可能進入的集合, M 表示數據幀的狀態, I 表示進程的初始狀態, T 表示兩兩狀態之間的轉化. 每個系統狀態可以分為發送狀態, 接受狀態和信道狀態. 把狀態用一個點進行表示,…

Miller_Rabin算法

為了測試一個大整數是不是素數&#xff0c;我們不能夠使用傳統的測試是否有因子的方法&#xff0c;因為那樣的時間復雜度至少也是O(n)O(n)O(n)&#xff0c;空間復雜度是O(n)O(n)O(n)&#xff08;使用線性篩數法&#xff09;&#xff0c;時間復雜度還好說&#xff0c;空間復雜度…

bob-tong 字符串函數之Strtok()函數

https://www.cnblogs.com/Bob-tong/p/6610806.html Strtok()函數詳解&#xff1a; 該函數包含在"string.h"頭文件中 函數原型&#xff1a; char* strtok (char* str,constchar* delimiters ); 函數功能&#xff1a; ??切割字符串&#xff0c;將str切分成一個個子…

數據鏈路層:SLIP(串型線路IP) PPP(點對點協議)

SLIP 沒有差錯控制, 傳輸時必須知道對方IP, 傳輸使用于低速業務 19.2k.應用非常受限 PPP協議 1. PPP協議功能 處理錯誤檢測 支持多協議(IP, IPX, DECnet 等) 連接時允許協商 IP 地址 允許身份驗證 2. PPP 的組成 串型鏈路上封裝數據報, 即支持異步鏈路也支持面向 比特…

Honeycomb——BFS

【題目描述】 傳送門 【題目分析】 看起來很復雜好像還要建圖什么的&#xff0c;其實直接在原圖上BFS就可以了&#xff0c;設置一下方向數組&#xff0c;然后直接跑就可以了。 【AC代碼】 #include<cstdio> #include<cstring> #include<algorithm> #inc…

C語言中strspn()函數和strcspn()函數的對比使用

C語言strspn()函數&#xff1a;計算字符串str中連續有幾個字符都屬于字符串accept 頭文件&#xff1a;#include <string.h> strspn() 函數用來計算字符串 str 中連續有幾個字符都屬于字符串 accept&#xff0c;其原型為&#xff1a; size_t strspn(const char *str, con…

Codeforces Round #587 (Div. 3)

A 只要每兩個都不一樣就可以&#xff0c;一旦出現兩個一樣的就改一個。 #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #include<queue> #include<set>using namespace std;typede…

信道分配 以太網

1.頻分復用 將信道分為多個頻帶, 用戶得到某個頻帶后,在通信的過程中, 自始至終都都占用這個信道.即頻分復用中, 所有用戶同時占用不同頻帶的信道 2. 時分信道 將時間劃分為一段一段的等長時分復用幀, 每個用戶在不同時間占用相同的數據幀 3. CSMA/CD 載波監聽多點接入/碰撞…

strpbrk函數

http://blog.csdn.net/tommy_wxie/article/details/7554332 函數原型&#xff1a;extern char *strpbrk(char *str1, char *str2) 參數說明&#xff1a;str1待比較的字符串&#xff0c;str2為指定被搜索的字符串。 所在庫名&#xff1a;#include <string.h> …

網絡層網絡層服務及其 IP 地址

ARP 協議功能 將 IP 地址通過廣播(一個網段, 不能跨路由器), 目標 MAC 地址是FFFFFFFF 解析目標IP地址的 MAC 地址. IP 協議 網絡層的一個協議, 是一個協議的統稱, 包括 ARP(地址解析協議) 協議, ICMP(網絡控制報文協議) 協議, IGMP(網際組管理協議) 協議. 其中 ICMP 和 IG…

隨機生成1024個數,存入一段內存,用指針實現獲取1024個數的最大數地址,最小數地址

http://blog.csdn.net/itcastcpp//details/39277193 題目&#xff1a;隨機生成1024個數&#xff0c;存入一段內存&#xff0c;用指針實現獲取1024個數的最大數地址&#xff0c;最小數地址&#xff0c;具體實現如下&#xff1a; [cpp] view plaincopy #include<stdlib.h> …

UVa11134

【題目分析】 覺得是一道挺考驗貪心掌握程度的題目&#xff0c;我就算知道是要用貪心而且肯定和區間有關&#xff0c;肯定要進行一下排序什么的我還是沒有找到合適的貪心策略。 經過大佬的博客后我才明白如何進行貪心。 如果沒有任何提示看這道題&#xff0c;首先&#xff0…

傳輸層:IP 地址解析 路由轉發

IP 地址與硬件地址 1. 地址解析 通過IP地址將其如何轉換為 MAC 地址.解決同一個局域網上的主機或路由的 IP 地址和硬件地址的映射問題. 即以太網上除了主機還有路由. 即如果發出的請求所有的主機都沒有做出相應, 那么該以太網上的路由會對其做出響應. (1) 以太網內部主機與…

UVa11582

一個數學問題,一旦出現循環確定循環節以后就能解決問題啦. 加上一個快速冪取模.需要注意的是數據范圍是264,所以必須用unsigned long long才能解決問題. 覺得板子還是要會自己寫,否則不同的題目具體有一些小的改變就會束手無策. 還有就是發現如果每次初始化數組的話就會超時,所…

輸入一個單向鏈表,輸出該鏈表中倒數第K個結點

http://blog.csdn.net/itcastcpp/article/details/39274891//尹成 單鏈表操作 #include <iostream> using namespace std; class LinkList;struct LinkNode { public:LinkNode(int value 0):m_value(value),pNext(NULL){}~LinkNode(){pNext NULL;}friend class LinkList…

網絡層:構成超網(CIDR)

CIDR構成超網 CIDR消除了原來的傳統的 A,B, C, D類地址, 使用了各種網絡前綴來代替原來分類地址中的網絡號和子網號, IP 地址由原來的三級分類又變成了兩級分類. 其中網絡號和子網號是一個隨機的長度. 其中 CIDR 也可以使用 / 的形式來表示, 其中在 / 前面寫上網絡前綴的位數.…

UVa12169

我們可以暴力枚舉a,然后通過x1和x3確定b的值&#xff0c;然后確定其他的數字&#xff0c;一旦出現錯誤就放棄這組解。 關鍵問題就在于如何通過a,x1,x3確定b的值 x2 ( x1 * a b) % M x3 ( x2 * a b ) % M ( ( x1 * a b ) % M * a b ) % M x3 - k * M x1 * a % M * a %…