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 ...
随机推荐
- cs231n spring 2017 lecture1 Introduction to Convolutional Neural Networks for Visual Recognition
1. 生物学家做实验发现脑皮层对简单的结构比如角.边有反应,而通过复杂的神经元传递,这些简单的结构最终帮助生物体有了更复杂的视觉系统.1970年David Marr提出的视觉处理流程遵循这样的原则,拿 ...
- JAVA递归、非递归遍历二叉树
前序遍历:1.访问根节点 2.前序遍历左子树 3.前序遍历右子树 中序遍历:1.中序遍历左子树 2.访问根节点 3.中序遍历右子树 后序遍历:1.后序遍历左子树 2.后序遍历右子树 3.访问根节点-- ...
- Service层在MVC框架中的意义和职责
https://blog.csdn.net/u012562943/article/details/53462157 mvc框架由model,view,controller组成,执行流程一般是:在con ...
- [CF1009F] Dominant Indices (+dsu on tree详解)
这道题用到了dsu(Disjoint Set Union) on tree,树上启发式合并. 先看了CF的官方英文题解,又看了看zwz大佬的题解,差不多理解了dsu on tree的算法. 但是时间复 ...
- 前端页面设计常见的30个CSS选择器
1. * --> 通配符选择器 * { margin: 0; padding: 0; } 星号符会选择页面每个元素.很多开发者用它把所有margin和padding归零.这当然是快捷测试 ...
- win10 安装VMware Workstation Pro提示无法在windows上运行
win10 安装vm无法在windows上运行 之前还可以用 网上搜了一下 要安装最新15.5.0就不会报错了 没毛病!! 不想注册下载 百度网盘(2019年9月19日版本) https://pan ...
- 【转载】[基础知识]【网络编程】TCP/IP
转自http://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=27043 [基础知识][网络编程]TCP/IP iooops 胖友们楼主我又 ...
- Data Visualization and D3.js 笔记(1)
课程地址: https://classroom.udacity.com/courses/ud507 什么是数据可视化? 高效传达一个故事/概念,探索数据的pattern 通过颜色.尺寸.形式在视觉上表 ...
- docker-compose的安装和设定
docker的1.12版本中,swarm已经合体,docker-engine/swarm/docker-compose的三件套装已经变成两件.后续会不会将docker-compose进一步合体呢,想做 ...
- linux增加history时间戳
增加环境变量到/etc/profile export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " export HISTSIZE=9999