基于排列的数列顺序对
2016-08-02 22:21
183 查看
题目描述:
洋洋的作业本上有一个长度为n的排列A,这个排列包含了从1到n的n个数,但是因为一些原因,其中一些位置(不超过10个)看不清了,但是洋洋记得这个数列顺序对的数量为k,顺序对是指满足i<j,且A[i] < A[j]的对数,请帮助洋洋计算出,符合这个要求的合法排列的数目。
输入描述:
每个输入包含一个测试用例。每个测试用例的第一行包含两个整数n和k(1 <= n <= 100,0 <= k <= 1000000000),接下来的1行,包含n个数字表示排列A,其中等于0的项表示看不清的位置(不超过10个)。
输出描述:
输出一行表示合法的排列数目。
输入例子:
5 5
4 0 0 2 0
输出例子:
2
代码:
结果显示:
洋洋的作业本上有一个长度为n的排列A,这个排列包含了从1到n的n个数,但是因为一些原因,其中一些位置(不超过10个)看不清了,但是洋洋记得这个数列顺序对的数量为k,顺序对是指满足i<j,且A[i] < A[j]的对数,请帮助洋洋计算出,符合这个要求的合法排列的数目。
输入描述:
每个输入包含一个测试用例。每个测试用例的第一行包含两个整数n和k(1 <= n <= 100,0 <= k <= 1000000000),接下来的1行,包含n个数字表示排列A,其中等于0的项表示看不清的位置(不超过10个)。
输出描述:
输出一行表示合法的排列数目。
输入例子:
5 5
4 0 0 2 0
输出例子:
2
代码:
<pre name="code" class="cpp">#include<iostream> #include<vector> #include<algorithm> using namespace std; int ordernum(vector<int> n) { int ns = n.size(); int c = 0; for(int i=0; i<ns-1; i++) { if(!n[i]) continue; for(int j=i+1; j<ns; j++) { if(!n[j]) continue; if(n[i] < n[j]) c++; } } return c; } int main(int argc, char* argv[]) { int n, k; vector<int> A; bool *pB; vector<int> vl; // the left number vector<int> vp; // the left position while(cin >> n >> k) { pB = new bool ; for(int i=0; i<n; i++) pB[i] = false; int t; for(int i=0; i<n; i++) { cin >> t; A.push_back(t); // the zeros are the undefined if(t) pB[t-1] = true; else vp.push_back(i); } for(int i=0; i<n; i++) { if(!pB[i]) { vl.push_back(i+1); } } int nl = vl.size(); // the left number int cA = ordernum(A); int cl; int csum = 0; if(cA > k) { cout << 0 << endl; } else { do{ cl = ordernum(vl); if(cl+cA > k) continue; // the cross int cc = 0; // the cross number for(int i=0; i<nl; i++) { for(int j=vp[i]-1; j>=0; j--) // the front cross { if(0 == A[j]) // detlete zeros continue; if(A[j] < vl[i]) cc++; if(cc+cA+cl > k) break; } if(cc+cA+cl > k) break; for(int j=vp[i]+1; j<n; j++) // the back cross { if(vl[i] < A[j]) // because the A[j] is the greate number, there is no need to judge the zeros cc++; if(cc+cA+cl > k) break; } } if(cc+cA+cl == k) { for(vector<int>::iterator it=vl.begin(); it!=vl.end(); it++) cout << *it << " "; cout << " cA: " << cA << " cl: " << cl << " cc: " << cc << endl; csum++; } //csum++; }while( next_permutation(vl.begin(), vl.end()) ); } cout << csum << endl; delete pB; A.clear(); vl.clear(); vp.clear(); } return 0; }
结果显示:
相关文章推荐
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。 Input 输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- 第八题:牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因,其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,顺序对是指满足 i < j 且 A[i] < A[j] 的对数,请帮助牛牛计算出,符合这个要求的合法排列的数目。
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- 网易面试题之 牛牛的作业薄上有一个长度为 n 的排列 A,这个排列包含了从1到n的n个数,但是因为一些原因, * 其中有一些位置(不超过 10 个)看不清了,但是牛牛记得这个数列顺序对的数量是 k,
- UOJ278 【UTR #2】题目排列顺序
- JavaScript实现在数组中查找不同顺序排列的字符串
- 依原始码顺序排列的栏段
- 【codeforces 691 D】【并查集 或者 dfs】aps in Permutation【给一个1到N的排列,M个操作,每次可以交换X Y位置上的数字,求可以得到的最大字典序的数列】
- 基于jQuery实现的百度导航li拖放排列效果,即时更新数据库
- 修改project中source folder的排列顺序
- 10月5日 c语言简单数组 输入10个数 要求从小到大顺序排列
- 20180305(数组,顺序搜索,二分搜索,数组排列,C字符串)
- 选择排序(把一组杂乱的数,按大小顺序排列)
- 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
- Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。 Input 输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义
- python爬虫进阶(九):基于Page Rank的顺序调整