POJ-3629
Card Stacking
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3927 Accepted: 1541 Description
Bessie is playing a card game with her N-1 (2 ≤ N ≤ 100) cow friends using a deck with K (N ≤ K ≤ 100,000; K is a multiple of N) cards. The deck contains M = K/N "good" cards and K-M "bad" cards. Bessie is the dealer and, naturally, wants to deal herself all of the "good" cards. She loves winning.
Her friends suspect that she will cheat, though, so they devise a dealing system in an attempt to prevent Bessie from cheating. They tell her to deal as follows:
1. Start by dealing the card on the top of the deck to the cow to her right
2. Every time she deals a card, she must place the next P (1 ≤ P ≤ 10) cards on the bottom of the deck; and
3. Continue dealing in this manner to each player sequentially in a counterclockwise manner
Bessie, desperate to win, asks you to help her figure out where she should put the "good" cards so that she gets all of them. Notationally, the top card is card #1, next card is #2, and so on.
Input
* Line 1: Three space-separated integers: N, K, and P
Output
* Lines 1..M: Positions from top in ascending order in which Bessie should place "good" cards, such that when dealt, Bessie will obtain all good cards.
Sample Input
3 9 2Sample Output
3
7
8
题意:
Bessie跟朋友玩牌,总共n人k张牌,k是n的整数倍。k张牌中共有k/n张好牌,Bessie想要作弊得到所有好牌。可Bessie的同伴为了防止Bessie作弊而设定了如下规则:
1.每次发牌从牌顶发,从Bessie右手边第一个人开始。
2.每发出一张牌就要将接下来的p张牌依次放至牌底。
按照这个方法发完所有牌。即使是这样,Bessie依然想要拿到所有好牌,请你帮她计算出将好牌放在哪些位置可以实现Bessie的愿望。
用队列可以很方便的解决这个问题,将所有k张牌入队,发牌即为出队,过牌即出队后再入队,每循环到Bessie时将队首元素记录下来,即为好牌位置。
附AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
using namespace std; const int MAX=; int ans[MAX];
int main(){
int n,k,p;
queue<int> q;//定义队列
memset(ans,,sizeof(ans));
while(~scanf("%d %d %d",&n,&k,&p)){
for(int i=;i<=k;i++){//入队
q.push(i);
}
int temp=,sum=;
while(){
if(temp++%n==){//temp从1开始每循环至Bessie处则为好牌
ans[sum++]=q.front();
if(sum==k/n){//好牌放完则退出循环
break;
}
}
q.pop();//发出的牌出队
for(int i=;i<p;i++){//其他牌移到牌底
q.push(q.front());
q.pop();
}
}
sort(ans,ans+sum);
for(int i=;i<sum;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
POJ-3629的更多相关文章
- POJ 3629 队列模拟
听说STL会卡T 然后我就试了一发 哈哈哈哈哈哈哈哈哈哈 1000ms卡时过的 这很值得我写一发题解了 哈哈哈哈哈哈哈哈哈哈哈哈 //By SiriusRen #include <queue&g ...
- CSU训练分类
√√第一部分 基础算法(#10023 除外) 第 1 章 贪心算法 √√#10000 「一本通 1.1 例 1」活动安排 √√#10001 「一本通 1.1 例 2」种树 √√#10002 「一本通 ...
- 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 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
随机推荐
- Jememeter和Loadrunner测试MySQL性能
From:http://blog.csdn.net/testingstar/article/details/60579454 MySQL数据库性能测试的方法 前置条件: 安装系统:windows 7 ...
- javascript 高级编程系列 - 创建对象
1. 工厂模式 function createPerson(name, age) { var obj = {}; obj.name = name; obj.age = age; obj.getName ...
- Ubuntu16.04下屏幕侧边栏的设置
ubnutu的任务栏都是在左侧: zhang@zhang-virtual-machine:~$ gsettings set com.canonical.Unity.Launcher launcher- ...
- remote connect openshift mysql
再虚拟机内 rhc port-forward <app-name> 此时,可以在本机 访问 127.0.0.1:8080 登陆 网页, 3306连接sql https://unix.st ...
- uboot 命令
1.清除前一次的编译结果: make distclean 2.配置makefile:选择开发板 make smdk6410_config 3.编译 make 注意::编译时,打开的文档文件,目录都要 ...
- java 文件的写入和读取
//写入操作 方法1 OutputStream f = new FileOutputStream("C:/j/j.txt"); f.write("aaaaaaa" ...
- 实记处理mongodb的NUMA问题
一次在启动mongodb的过程中,出现过NUMA这个问题, mongodb日志显示如下: WARNING: You are running on a NUMA machine. We suggest ...
- Spring与JDK版本不一致引发问题Caused by: java.lang.IllegalArgumentException
tomcat启动一个spring的项目,tomcat使用8.5,JDK使用1.8,Spring使用3.0,启动之后报错 Caused by: java.lang.IllegalArgumentExce ...
- python super()(转载)
一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: class A: def __init__(sel ...
- 《AndroidStudio有用指南》反馈问题和建议
<AndroidStudio有用指南>反馈问题和建议 IntelliJ IDEA在持续更新, Android Studio也在持续更新, 本书也将会持续更新. Android Studio ...