PKU ACM 1012 JOSEPH问题
2010-05-07 01:03
316 查看
题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=1012
今天第一次尝试做ACM的题,挑了道看似最简单的Joseph题做,谁知道过程很坎坷啊,提交了n次,终于搞定了。
首先,我常试用一个循环链表的方式做
代码如下:
报错,Limited Memory Exceeded,应该是申请了太多内存空间,没有释放
接下来,我常试用数组做,也是Memory太大,原来是忘了删除每次生成的新数组的空间,后来添加了delete [] p后,又报错,是Time Limited Exceeded
然后我对程序修改了很久,到最后,连数组都没用了,但还是时间太久,后来,看了大家的讨论,发现数据会重复,所以我添加了一个mem[]数组,用以保存先前计算出来的值,下一次还是这个值时,就直接调用,这样就提交成功了
提交成功的代码如下:
但是,总觉得这个方法有点赖皮,高手们还有其它方法吗??
附录:
Joseph
Description
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
Sample Output
今天第一次尝试做ACM的题,挑了道看似最简单的Joseph题做,谁知道过程很坎坷啊,提交了n次,终于搞定了。
首先,我常试用一个循环链表的方式做
代码如下:
#include <iostream> #include <vector> using namespace std; class node { public: int data; node *next; node(){} node(int d=0,node* n=NULL):data(d),next(n){} ~node(){delete next;} }; bool execute(int m,int k) { int i; int kcnt=0; node *head=new node(1); node *p=head; node *s; for(i=2;i<=2*k;i++) { p->next=new node(i); p=p->next; } p->next=head; while(kcnt<k) //start to kill { for(i=0;i<m;i++) { s=p; p=p->next; } s->next=p->next; kcnt++; if((p->data)<=k) return false; } return true; } int joseph(int k) //choose the minimum m that satisfy the requirements { int m; for(m=k+1;;m++) { if(execute(m,k)) break; } return m; } int main() { int k,i; vector<int> m; cin >> k; while(k!=0) { m.push_back(joseph(k)); cin >> k; } for(i=0;i<m.size();i++) cout << m[i] << endl; }
报错,Limited Memory Exceeded,应该是申请了太多内存空间,没有释放
接下来,我常试用数组做,也是Memory太大,原来是忘了删除每次生成的新数组的空间,后来添加了delete [] p后,又报错,是Time Limited Exceeded
然后我对程序修改了很久,到最后,连数组都没用了,但还是时间太久,后来,看了大家的讨论,发现数据会重复,所以我添加了一个mem[]数组,用以保存先前计算出来的值,下一次还是这个值时,就直接调用,这样就提交成功了
提交成功的代码如下:
#include <iostream> #include <vector> using namespace std; int mem[14]={0}; int joseph(int k) //choose the minimum m that satisfy the requirements { if(mem[k]!=0) return mem[k]; int m,cnt,sp; for(m=k+1;;m++) { cnt=k*2; sp=0; while(cnt>k) //开始杀人 { sp=(sp+m-1)%cnt; if(sp<k) cnt=0; cnt--; } if(cnt==k) { mem[k]=m; return m; } } return 0; } int main() { int k,i; vector<int> m; cin >> k; while(k!=0) { m.push_back(joseph(k)); cin >> k; } for(i=0;i<m.size();i++) cout << m[i] << endl; }
但是,总觉得这个方法有点赖皮,高手们还有其它方法吗??
附录:
Joseph
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28476 | Accepted: 10625 |
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
3 4 0
Sample Output
5 30
相关文章推荐
- 约瑟夫 (Joseph) -- ACM PKU 1012 解题报告
- PKU ACM-1012题 java Joseph
- PKU1012 Joseph 约瑟夫问题
- PKU ACM 1012 Joseph 约瑟夫
- PKU 1012 Joseph
- 约瑟夫问题 pku1012(转自奋斗青春(亚伟)blog)
- POJ 1012 Joseph 约瑟夫问题
- POJ-1012 Joseph-约瑟夫问题好人坏人
- pku acm 1012
- NYOJ 191 && POJ 1012 Joseph(约瑟夫环问题)
- 1012 Joseph 约瑟夫问题 总结了一个公式 n人报m第t轮出列的人的编号(从0到n-1)
- pku1012 Joseph
- ACM_CUGOJ_1012_Joseph Problem约瑟夫
- acm 1012 大数问题
- acm pku 2234 解题报告(取子问题)
- [PKU] 1012 Joseph [约瑟夫环,枚举]
- pku 1012 Joseph
- 新随笔-约瑟夫问题(pku 1012)
- POJ 1012 && HDU 1443 Joseph(约瑟夫问题)
- PKU-1012 Joseph (约瑟夫环)