POJ 3629 Card Stacking

Card Stacking

原题链接
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4566 Accepted: 1736
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 2
Sample Output

3
7
8
Source

USACO 2007 December Bronze

题解:
这题的意思就是一个叫Bessie的人和他n-1个朋友玩发牌游戏,一共有k张牌,有m=k/n张好牌(k/m张坏牌),从右开始发,也就是说Bessie是最后一个拿到牌的,每发一张牌都需要将最上面连续的p张牌放到下面,Bessie想要获胜,所以请你找出应该把好牌放在那些位置(就是找出Bessie获得牌的位置)并升序输出,在名义上,顶部卡是#1卡,下一张卡是#2,依此类推。
解题思路:
用一个queue数组模仿队列,一个头下标,一个尾下标,然后每发1张牌就将p张牌放到数组最后面,循环发出n-1张牌,下一张要发的牌就是好牌,一共要发k/n张好牌,下面是AC代码:

#include"iostream"
#include"algorithm"

using namespace std;

const int MAX = 10000007; 
int queue[MAX];//模仿队列数组 
int n,k,p;//人数,牌数,需要放到下面的牌数 
int good_cards[MAX];//存放好牌 
int size;//好牌数组的下标 

int main()
{
	cin >> n >> k >> p;
	for(int i=1;i<=k;i++)//初始化队列
		queue[i] = i;
	int top=1;//队列的头 
	int bot=k+1;//队列的尾 
	for(int q=1;q<=k/n;q++)//一共要发出k/n张好牌 
	{
		for(int j=0;j<n-1;j++)//从右边开始发牌,发n-1次 
		{	
			top++;
			for(int i=0;i<p;i++)//将p张牌依次放到最后 
				queue[bot++]=queue[top++];
		}
		good_cards[size++] = queue[top++];//存储好牌 
		for(int i=0;i<p;i++)//将p张牌依次放到最后
			queue[bot++]=queue[top++];	
	}
	sort(good_cards,good_cards+size);//升序 
	for(int i=0;i<size;i++)
		cout << good_cards[i] << endl;
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值