【例题4-3 uva 133】The Dole Queue
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
写个数组模拟链表
但注意,得用个辅助数组flag。。
不然可能会出现没能跳过中间的被占区域的情况。
比如
1 2 idx # # # idx2 8
(#表示已经出去的位置)
这个时候,idx1和idx2删掉的话。(假设先删idx1,后删idx2)
r[idx1]无法更新为8。。
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n,m,k;
int l[N+10],r[N+10];
int flag[N+10];
void _delete(int idx){
int L = l[idx],R = r[idx];
r[L] = R;
l[R] = L;
}
void step_left(int *x){
*x = l[*x];
while (flag[*x]) *x =l[*x];
}
void step_right(int *x){
*x = r[*x];
while (flag[*x]) *x=r[*x];
}
int main()
{
//freopen("/home/ccy/rush.txt","r",stdin);
ios::sync_with_stdio(0),cin.tie(0);
while (cin >> n >> k >> m){
memset(flag,0,sizeof flag);
if(n==0 && m==0 && k==0) break;
int idx1 = 1,idx2 = n;
for (int i = 1;i <= n;i++) l[i] = i-1,r[i]=i+1;
l[1] = n;r[n] = 1;
int cnt = n;
while (cnt>0){
for (int i = 1;i <= k-1;i++) step_right(&idx1);
for (int i = 1;i <= m-1;i++) step_left(&idx2);
if (idx1==idx2){
_delete(idx1);
cnt--;
cout<<setw(3)<<idx1;
}else{
_delete(idx1);_delete(idx2);
while (flag[idx1]) idx1 = r[idx1];
while (flag[idx2]) idx2 = l[idx2];
cnt-=2;
cout<<setw(3)<<idx1<<setw(3)<<idx2;
}
flag[idx1] = flag[idx2]=1;
if (cnt==0) {
cout<<endl;
break;
}else cout<<",";
step_right(&idx1);step_left(&idx2);
}
}
return 0;
}
【例题4-3 uva 133】The Dole Queue的更多相关文章
- UVA 133 The Dole Queue
The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...
- uva - 133 The Dole Queue(成环状态下的循环走步方法)
类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...
- UVA 133 The Dole Queue(报数问题)
题意:一个长度为N的循环队列,一个人从1号开始逆时针开始数数,第K个出列,一个人从第N个人开始顺时针数数,第M个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...
- UVA 133“The Dole Queue”(循环报数处理技巧)
•参考资料 [1]:紫书P82 •题意(by紫书) 按照被选中的次序输出这 n 个人的编号: 如果A和B选中的是同一个人,输出一个这个人的编号: 输出格式:输出的每个编号占3个字节,不够3个字节在前面 ...
- uva 133(The Dole Queue UVA - 133)
一道比较难想的模拟题,用了队列等东西,发现还是挺难做的,索性直接看了刘汝佳的代码,发现还是刘汝佳厉害! 代码本身难度并不是很大,主要还是p=(p+n+d-1)%n+1;这一句有些难度,实际上经过自己的 ...
- uvaoj 133 - The Dole Queue(逻辑,环形队列数数)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa133.The Dole Queue
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- The Dole Queue UVA - 133
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros ...
- The Dole Queue
The Dole Queue Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit cid ...
随机推荐
- [LeetCode][Java] Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 一个jeecg整合activiti的学习样例,源代码下载
社区成员:刘京华採用技术:jeecg+ activiti源代码下载地址:http://pan.baidu.com/s/1dDxOHrV 截图演示: 2.jpg (71.81 KB, 下载次数: 0) ...
- HDU1010-奇偶剪枝(DFS)
题目链接:Tempter of the Bone 第一次做剪枝的题目,剪枝,说实话研究的时间不短.好像没什么实质性的进展,遇到题目.绝对有会无从下手的感觉,剪枝越来越神奇了. .. . HDU1010 ...
- 使用SetTimer函数为Delphi的Win控件设置时钟
procedure Timertodo(var messag:Tmessage);message WM_TIMER; procedure TForm1.FormCreate(Sender: TObje ...
- Harry Potter and the Order of the Phoenix
书名:Harry Potter and the Order of the Phoenix 作者:J.K. Rowling 篇幅: 870P 蓝思值:950L 用时: 22天 工具: 有道词典 [透析成 ...
- luogu1052 过河
题目大意 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
- heap堆&&priority_queue优先队列
堆(heap)不是stl中的东西...它分为 max heap 和min heap. 但我不想用这些,而是采用了priority_queue,优先队列,定义在queue中.顾名思义,它的作用就是无论怎 ...
- 3-5 第三天 Koa 和 Express 中间件
Koa和Express这两个框架除了在接收请求和返回数据方面有非常通用.好用的封装以外,最有价值的地方就是它们有自己的中间件机制,所以说中间件可以看做是流水线上一个又一个的加工房间,每个加工的房间都只 ...
- 胜利大逃亡(续)(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 #include <stdio.h> #include <queue> #incl ...
- lodop使用
根据相应的操作系统,安装install_lodop32.exe文件,它里面包含两个exe文件install_lodop32.exe和install_lodop64.exe,在页面的头部中引入: < ...