acm_Sequence one
2016-04-24 23:37
435 查看
题目:
[align=left]Problem Description[/align]
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.<br>Now give you a number sequence, include n (<=1000) 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 sequence of each
integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence
are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully. <br>
[align=left]Input[/align]
The input contains multiple test cases.<br>Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000). <br>
[align=left]Output[/align]
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题意:给你N个数,基本上有点求它的子集的感觉,因为他不能重复。。
想法:深搜加剪枝即可。。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int d[1009];
int n,p;
int l,c;
struct name
{
int now,pos;
};
name w[10009];
bool flag;
bool check(int s,int e)
{
for(int i=s+1;i<e;i++)
{
if(d[i]==d[e])
return false;
}
return true;
}
void set(int length)
{
for(int i=0;i<length-1;i++)
cout<<w[i].now<<" ";
cout<<w[length-1].now<<endl;
}
void dfs(int dep,int pos)
{
if(c>=p)
return;
if(dep==l)
{
c++;
flag=true;
set(l);
return;
}
for(int i=pos;i<n;i++)
{
if((dep!=0&&w[dep-1].now<=d[i])||dep==0)
{
if(dep==0&&!check(-1,i))
continue;
if(dep!=0&&!check(w[dep-1].pos,i))
continue;
w[dep].now=d[i];
w[dep].pos=i;
dfs(dep+1,i+1);
}
}
return;
}
int main()
{
int i;
while(cin>>n>>p)
{
for(i=0;i<n;i++)
cin>>d[i];
c=0;
for(i=1;i<n;i++)
{
flag=false;
l=i;
dfs(0,0);
if(c>=p||!flag)
break;
}
cout<<endl;
}
return 0;
}
感想:好难。。
[align=left]Problem Description[/align]
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.<br>Now give you a number sequence, include n (<=1000) 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 sequence of each
integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence
are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully. <br>
[align=left]Input[/align]
The input contains multiple test cases.<br>Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000). <br>
[align=left]Output[/align]
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
[align=left]Sample Input[/align]
3 5<br>1 3 2<br>3 6<br>1 3 2<br>4 100<br>1 2 3 2<br>
[align=left]Sample Output[/align]
1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>2<br>3<br>1 2<br>1 3<br>2 3<br>2 2<br>1 2 3<br>1 2 2<br><br><br><div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint : You must make sure each subsequence in the subsequences is unique.</div>
题意:给你N个数,基本上有点求它的子集的感觉,因为他不能重复。。
想法:深搜加剪枝即可。。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int d[1009];
int n,p;
int l,c;
struct name
{
int now,pos;
};
name w[10009];
bool flag;
bool check(int s,int e)
{
for(int i=s+1;i<e;i++)
{
if(d[i]==d[e])
return false;
}
return true;
}
void set(int length)
{
for(int i=0;i<length-1;i++)
cout<<w[i].now<<" ";
cout<<w[length-1].now<<endl;
}
void dfs(int dep,int pos)
{
if(c>=p)
return;
if(dep==l)
{
c++;
flag=true;
set(l);
return;
}
for(int i=pos;i<n;i++)
{
if((dep!=0&&w[dep-1].now<=d[i])||dep==0)
{
if(dep==0&&!check(-1,i))
continue;
if(dep!=0&&!check(w[dep-1].pos,i))
continue;
w[dep].now=d[i];
w[dep].pos=i;
dfs(dep+1,i+1);
}
}
return;
}
int main()
{
int i;
while(cin>>n>>p)
{
for(i=0;i<n;i++)
cin>>d[i];
c=0;
for(i=1;i<n;i++)
{
flag=false;
l=i;
dfs(0,0);
if(c>=p||!flag)
break;
}
cout<<endl;
}
return 0;
}
感想:好难。。
相关文章推荐
- UIBarButtonItem的setTitleTextAttributes方法不起作用
- UIRefreshControl下拉刷新的用法
- UITableView的使用
- CompileSdkVersion和buildeToolVersion版本号关系
- LigerUI中通过加载服务端数据进行表格的分页显示
- Dell T3610 台式工作站UEFI模式安装Win7系统
- nc suapUI 破解
- UVA - 424 Integer Inquiry
- LigerUI的简单使用示例
- UITableView 的项目运用
- 使用BlockingQueue的生产者消费者模式
- Builder(生成器)-对象创建型模式
- Lucene 查询(Query)子类
- priority_queue测试
- 关于java中String、StringBuffer、StringBuilder
- Rescue
- CAlayer自定义图层
- CSS3 Media Queries 与Responsive 设计
- c# 高效的线程安全队列ConcurrentQueue(下) Segment类
- c#高效的线程安全队列ConcurrentQueue<T>(上)