POJ 1879
栈和队列的综合应用,利用栈和队列分别模拟分,5分,时槽,以及小球队列
利用求出一天后的置换可以求出周期,进而求出最大公约数(可以利用矩阵的角度,也许可以简化,因为每次都是乘上一个相同的置换矩阵)
要注意读题,时槽 满12归队的方式很不一样
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
const int maxn= 127+5;
const int dms= 24*60;
int T[maxn], chg[maxn];
stack<int> h, fvm, m;
queue<int> bl;
int ans;
int Gcd(int a, int b)
{
return 0== b? a : Gcd(b, a%b);
}
void Init(int n)
{
ans= 1;
while (!bl.empty()){
bl.pop();
}
while (!h.empty()){
h.pop();
}
while (!fvm.empty()){
fvm.pop();
}
while (!m.empty()){
m.pop();
}
for (int i= 0; i< n; ++i){
T[i]= 1;
}
for (int i= 0; i< n; ++i){
bl.push(i);
}
}
int main()
{
int n;
while (1){
scanf("%d", &n);
if (0== n){
break;
}
Init(n);
for (int i= 0; i< dms; ++i){
int obl= bl.front();
bl.pop();
if (4== m.size()){
for (int j= 0; j< 4; ++j){
bl.push(m.top());
m.pop();
}
if (11== fvm.size()){
for (int j= 0; j< 11; ++j){
bl.push(fvm.top());
fvm.pop();
}
if (11== h.size()){
for (int j= 0; j< 11; ++j){
bl.push(h.top());
h.pop();
}
bl.push(obl);
}else{
h.push(obl);
}
}
else{
fvm.push(obl);
}
}
else{
m.push(obl);
}
}
for (int i= 0; i< n; ++i){
chg[i]= bl.front();
// cout<<i<<" "<<chg[i]<<endl;
bl.pop();
}
for (int i= 0; i< n; ++i){
int ni= chg[i];
while (ni!= i){
ni= chg[ni];
++T[i];
}
}
for (int i= 0; i< n; ++i){
ans= ans*T[i]/Gcd(ans, T[i]);
}
cout << n << " balls cycle after "<< ans<<" days."<<endl;
}
return 0;
}
POJ 1879的更多相关文章
- POJ 1879 Tempus et mobilius Time and motion 队列和栈
很简单的队列和栈的应用,不过读明白题意非常重要:(直接引用白书的题解)三个轨道,一个库.分别是分钟单位的轨道,5min单位的轨道,一小时单位的轨道,还有就是n容量的库.每过一分钟,一个小球从库里面出来 ...
- POj 1879 Tempus et mobilius Time and motion (模拟+群)
题目特别长,大意为球的传递. 三个轨道,一个库.各自是分钟单位的轨道.5min单位的轨道.一小时单位的轨道.还有就是n容量的库. 每过一分钟,一个小球从库里面出来,库符合先进先出,进入分钟轨道.假设分 ...
- poj 1879 Truck History
本题链接:点击打开链接 题目大意: 输入n表示卡车辆数,输入每辆卡车编号.即长度为7的字符串,每辆卡车编号均可由其他类型编号衍生过来,求由当中一辆衍生出其他全部的最小衍生次数(有一个字符不同就需衍生一 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【学术篇】状态压缩动态规划——POJ3254/洛谷1879 玉米田Corn Field
我要开状压dp的坑了..直播从入门到放弃系列.. 那就先拿一道状压dp的水题练练手吧.. 然后就找到了这一道..这道题使我清醒地认识到阻碍我的不是算法,而是视力= = 传送门: poj:http:// ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- deeplearning.ai 序列模型 Week 2 NLP & Word Embeddings
1. Word representation One-hot representation的缺点:把每个单词独立对待,导致对相关词的泛化能力不强.比如训练出“I want a glass of ora ...
- phpcms数组处理后键值插入(php自带库函数)和自己处理办法比较。
phpcms处理: public function insert($data, $table, $return_insert_id = false, $replace = false) { if(! ...
- python中sort和sorted排序的相关方法
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- WordPress快速打造个人博客
前些天用wordpress搭建了现在这个博客,所以总结了一篇文章,讲讲怎么样简单的创建一个博客.开始前这里有篇我搭建时所遇到的问题可以作为参考<WordPress建站注意事项>,首先我们要 ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶(续二)
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- idea常见需求
1.给class加注释模板 /** *@ClassName ${NAME} *@Description TODO *@Author xxx *@Date ${DATE} ${TIME} *@Versi ...
- UnitTest测试框架-操作步骤
一.UnitTest 1. TestCase 说明:测试用例 1.新建类并集成unittest.TestCase 2. TestSuite 说明:测试套件(多条用例) 方法: 1. 实例化 suite ...
- axios 传对象(JavaBean)到后台
//user对象 let user = JSON.stringify({ userAccountNumber: own.userName, userPassword: own.userPassword ...
- vscode写react有warning
[js] Experimental support for decorators is a feature that is subject to change in a future release. ...
- 添砖加瓦:MySQL分布式部署
1.集群环境 管理节点(MGM):这类节点的作用是管理MySQLCluster内的其他节点,如提供配置数据,并停止节点,运行备份等.由于这类节点负责管理其他节点的配置,应该在启动其他节点之前启动这类 ...