【例题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 ...
随机推荐
- C++对象模型——关于对象(第一章)
第一章 关于对象 在C语言中,"数据"和"处理数据的操作(函数)"是分开声明的,也就是说,语言本身并没有支持"数据和函数"之间的关联性 ...
- iOS刷新某个cell时候crash
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSec ...
- oc43--野指针和空指针
// // main.m // 野指针和空指针 #import <Foundation/Foundation.h> #import "Person.h" int mai ...
- zoj--3870--Team Formation(位运算好题)
Team Formation Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- codeforces 898F Hash
F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Cuckoo for Hashing
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2719 #include <stdio.h ...
- Cookie、Token与Session介绍(非原创)
文章大纲 一.Cookie介绍二.Token介绍三.Session介绍四.Token.Cookie与Session比较五.参考文章 一.Cookie介绍 1. Cookie是什么 cookie机制 ...
- LeetCode Weekly Contest 28
1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...
- java joor 实现反射简单调用
有时候需要用反射实现代码.直接用工具,joor 上代码: package com.ming.joor; import static org.joor.Reflect.*; public class T ...
- 5.29clone项目地址