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 ...
随机推荐
- centos 7 下iptables参数详解
在红帽RHEL7系统中firewalld服务取代了iptables服务,如果我们不想用或者不习惯使用firewalld服务,请看下面的操作: iptables命令中则常见的控制类型有: ACCEPT: ...
- FFT算法的verilog实现
首先需要明白傅里叶相关的基本知识:还是 借用这位英雄的文章,真心写的让人佩服不已http://blog.jobbole.com/70549/ 然后是卷积的理解http://blog.csdn.net/ ...
- linux更改系统ulimit
https://jingyan.baidu.com/article/c85b7a64b65d8c003aac957e.html
- Zabbix-3.0.3实现微信(WeChat)报警
转自:http://blog.sina.com.cn/s/blog_87113ac20102w7hp.html Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来 ...
- webgrind安装使用详细说明
webgrind是一个网页版的性能分析工具,它的主要作用就是分析xdebug生成的cachegrind文件,以一种界面友好详尽的方式来展示性能数据.试用了一下感觉还是很不错的,鉴于网上并没有一个系统介 ...
- 腾讯云服务器(centos7.2)上安装MySQL
1.到MySQL官网找到相应的版本 https://dev.mysql.com/downloads/repo/yum/ 找到下面的Linux7,即CentOS7(CentOS是Red Hat旗下的)点 ...
- python标准库:csv 模块
原文地址:http://www.bugingcode.com/blog/python_csv.html csv 模块被用来读取CSV格式(用逗号分割数值)的数据文件,CSV格式的文件经常在微软的Exc ...
- ServletContext+ServletConfig内容
ServletConfig { ① //读取web.xml配置信息 ServletConfig config = this.getServletConfig(); //读取类名称 config.get ...
- python ftp sftp
ftp 上传下载文件 12345678910111213141516171819202122232425262728293031323334 from ftplib import FTPimport ...
- 吴裕雄--天生自然 HADOOP大数据分布式处理:修改CenterOS 7系统时间为北京时间