acm 2 1024 Sequence one
2016-04-24 19:09
453 查看
1.1024
2.Subset sequence
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2339 Accepted Submission(s): 1242
Problem Description
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.
Input
The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).
Output
For each test case, you should output the m-th subset sequence of An in one line.
Sample Input
1 1
2 1
2 2
2 3
2 4
3 10
Sample Output
1
1
1 2
2
2 1
2 3 1
3.一个从1...n的数组,对这个数组进行规则排序,给出一个m,输出该数组第几个的表达形式
4.例如n=2
1
1
1 2
2
2 1
2 3 1
5.#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct numberr
{
int number;
int postion;
};
numberr b[1005];
int a[1005];
int n,p,cou,dep;
int flag;
int isok(int ss,int ee)
{
for(int i=ss;i<ee;i++)
{
if(a[i]==a[ee])return 0;
}
return 1;
}
void dfs(int nowdep,int pos)
{
if(cou>=p)
return ;
if(nowdep==dep)
{
cou++;
flag=1;
for(int i=0;i<nowdep-1;i++)
{
cout<<b[i].number<<" ";
}
cout<<b[nowdep-1].number<<endl;
return ;
}
for(int i=pos;i<n;i++)
{
if((nowdep!=0&&b[nowdep-1].number<=a[i])||nowdep==0)
{
if(nowdep!=0)
{
if(!isok(b[nowdep-1].postion+1,i))
continue;
}
else
{
if(nowdep==0&&!isok(0,i))
continue;
}
b[nowdep].number=a[i];
b[nowdep].postion=i;
dfs(nowdep+1,i+1);
}
}
}
int main()
{
while(cin>>n>>p)
{
for(int i=0;i<n;i++)
cin>>a[i];
cou=0;
for(int i=1;i<n;i++)
{
flag=0;
dep=i;
dfs(0,0);
if(cou>=p||(!flag))break;
}
cout<<endl;
}
return 0;
}
2.Subset sequence
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2339 Accepted Submission(s): 1242
Problem Description
Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.
Input
The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).
Output
For each test case, you should output the m-th subset sequence of An in one line.
Sample Input
1 1
2 1
2 2
2 3
2 4
3 10
Sample Output
1
1
1 2
2
2 1
2 3 1
3.一个从1...n的数组,对这个数组进行规则排序,给出一个m,输出该数组第几个的表达形式
4.例如n=2
1
1
1 2
2
2 1
2 3 1
5.#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct numberr
{
int number;
int postion;
};
numberr b[1005];
int a[1005];
int n,p,cou,dep;
int flag;
int isok(int ss,int ee)
{
for(int i=ss;i<ee;i++)
{
if(a[i]==a[ee])return 0;
}
return 1;
}
void dfs(int nowdep,int pos)
{
if(cou>=p)
return ;
if(nowdep==dep)
{
cou++;
flag=1;
for(int i=0;i<nowdep-1;i++)
{
cout<<b[i].number<<" ";
}
cout<<b[nowdep-1].number<<endl;
return ;
}
for(int i=pos;i<n;i++)
{
if((nowdep!=0&&b[nowdep-1].number<=a[i])||nowdep==0)
{
if(nowdep!=0)
{
if(!isok(b[nowdep-1].postion+1,i))
continue;
}
else
{
if(nowdep==0&&!isok(0,i))
continue;
}
b[nowdep].number=a[i];
b[nowdep].postion=i;
dfs(nowdep+1,i+1);
}
}
}
int main()
{
while(cin>>n>>p)
{
for(int i=0;i<n;i++)
cin>>a[i];
cou=0;
for(int i=1;i<n;i++)
{
flag=0;
dep=i;
dfs(0,0);
if(cou>=p||(!flag))break;
}
cout<<endl;
}
return 0;
}
相关文章推荐
- response.sendRedirect()与request.getRequestDispatcher().forward()区别
- Animation and Transitions in Qt Quick | Qt Quick 5
- 1012 Rescue
- 60. Permutation Sequence
- stringbuffer和stringbuilder的区别
- Easyui之datagrid实现点击单元格修改单元格背景颜色
- PHP中include和require
- Leetcode Increasing Triplet Subsequence
- [UIPasteboard generalPasteboard]剪贴板的使用
- AmazeUI 模态框封装
- flex build 4 三个命名空间fx,mx,s
- IOS本地推送通知的一些属性 UILocalNotification
- 软件设计模式——建造者模式(Builder)
- 码农小汪-SQL 图形界面设计约束,unqiue,check Default Foreign
- 利用uitouch简单的实现了微信cell效果
- Restricted Boltzmann Machine(to be continued)
- 51. N-Queens
- easyui里关于datagrid日期不能正常显示
- 【UE4学习】07——设置自己的模板 Converting a Project to a Template
- GUI编程基础