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:
-
Start by dealing the card on the top of the deck to the cow to her right
-
Every time she deals a card, she must place the next P (1 ≤ P ≤ 10) cards on the bottom of the deck; and
-
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 num[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++];
}
num[size++] = queue[top++];//儲存好牌
for(int i=0;i<p;i++)//將p張牌依次放到最後
queue[bot++]=queue[top++];
}
sort(num,num+size);//升序
for(int i=0;i<size;i++)
cout << num[i] << endl;
return 0;
}