acm 2 1025 Sequence two
2016-04-24 19:23
344 查看
1.1025
2.Sequence two
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 98 Accepted Submission(s): 46
Problem Description
Search is important in the acm algorithm. When you want to solve a problem by using the search method, try to cut is very important.
Now give you a number sequence, include n (<=100) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences
is that: first order the length of the subsequence. Second order the subsequence by lexicographical. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {2}; {3}; {1,2}; {1,3}. If you also can not understand , please
see the sample carefully.
Input
The input contains multiple test cases.
Each test case include, first two integers n, P. (1<n<=100, 1<p<=100000).
Output
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
Sample Input
3 5
1 3 2
3 6
1 3 2
4 100
1 2 3 2
Sample Output
1
2
3
1 2
1 3
1
2
3
1 2
1 3
1
2
3
1 2
1 3
2 2
2 3
1 2 2
1 2 3
3.和上一题差不多*-*
4.dfs,首先排一次顺序,注意输下标,生成的子串递增,下标也是递增,不能有重复的子串。
5.#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct numberr
{
int number;
int postion;
};
bool cmp(const numberr &a,const numberr &b)
{
if(a.number!=b.number)
return a.number<b.number;
return a.postion<b.postion;
}
numberr b[1005];
int a[1005];
int n,p,cou,dep;
int c[1005];
int dfs(int nowdep,int pos,int rr)
{
if(nowdep==dep)
{
cou++;
for(int i=0;i<nowdep-1;i++)
{
cout<<c[i]<<" ";
}
cout<<c[nowdep-1]<<endl;
if(cou==p) return 1;
return 0;
}
int pre,flag=0;
for(int i=pos;i<=n;i++)
{
if(b[i].postion>rr)
{
if(!flag)
{
flag=1;
pre=b[i].number;
}
else if(pre==b[i].number) continue;
pre=b[i].number;
c[nowdep]=b[i].number;
if(dfs(nowdep+1,i+1,b[i].postion))
return 1;
}
}
return 0;
}
int main()
{
while(cin>>n>>p)
{
for(int i=1;i<=n;i++)
{
cin>>b[i].number;
b[i].postion=i;
}
sort(b+1,b+n+1,cmp);
cou=0;
for(int i=1;i<n;i++)
{
dep=i;
if(dfs(0,1,0))break;
}
cout<<endl;
}
return 0;
}
2.Sequence two
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 98 Accepted Submission(s): 46
Problem Description
Search is important in the acm algorithm. When you want to solve a problem by using the search method, try to cut is very important.
Now give you a number sequence, include n (<=100) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences
is that: first order the length of the subsequence. Second order the subsequence by lexicographical. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {2}; {3}; {1,2}; {1,3}. If you also can not understand , please
see the sample carefully.
Input
The input contains multiple test cases.
Each test case include, first two integers n, P. (1<n<=100, 1<p<=100000).
Output
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
Sample Input
3 5
1 3 2
3 6
1 3 2
4 100
1 2 3 2
Sample Output
1
2
3
1 2
1 3
1
2
3
1 2
1 3
1
2
3
1 2
1 3
2 2
2 3
1 2 2
1 2 3
3.和上一题差不多*-*
4.dfs,首先排一次顺序,注意输下标,生成的子串递增,下标也是递增,不能有重复的子串。
5.#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct numberr
{
int number;
int postion;
};
bool cmp(const numberr &a,const numberr &b)
{
if(a.number!=b.number)
return a.number<b.number;
return a.postion<b.postion;
}
numberr b[1005];
int a[1005];
int n,p,cou,dep;
int c[1005];
int dfs(int nowdep,int pos,int rr)
{
if(nowdep==dep)
{
cou++;
for(int i=0;i<nowdep-1;i++)
{
cout<<c[i]<<" ";
}
cout<<c[nowdep-1]<<endl;
if(cou==p) return 1;
return 0;
}
int pre,flag=0;
for(int i=pos;i<=n;i++)
{
if(b[i].postion>rr)
{
if(!flag)
{
flag=1;
pre=b[i].number;
}
else if(pre==b[i].number) continue;
pre=b[i].number;
c[nowdep]=b[i].number;
if(dfs(nowdep+1,i+1,b[i].postion))
return 1;
}
}
return 0;
}
int main()
{
while(cin>>n>>p)
{
for(int i=1;i<=n;i++)
{
cin>>b[i].number;
b[i].postion=i;
}
sort(b+1,b+n+1,cmp);
cou=0;
for(int i=1;i<n;i++)
{
dep=i;
if(dfs(0,1,0))break;
}
cout<<endl;
}
return 0;
}
相关文章推荐
- acm 2 1024 Sequence one
- 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