POJ 3629 Card Stacking

Card Stacking

Time Limit: 1000MS Memory Limit: 65536K

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

思路:

主要是题目不太容易看懂,首先就是1被第一个玩家拿走,然后就放两张牌到队列尾巴,所以这时就是4 5 6 7 8 9 2 3,然后到了第二个玩家拿走了4,再放两个到队尾就有了7 8 9 2 3 5 6,这样bessie就拿到了7以此类推答案就出来了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int n, k, p;
    queue<int> q;
    vector<int> v;
    scanf("%d %d %d", &n, &k, &p);
    for (int i = 1; i <= k; i++) q.push(i);
    int cnt = 1;
    while (!q.empty()) {
        if (cnt % n == 0) {
            int x = q.front();
            v.push_back(x);
        }
        if (v.size() == k / n) break;
        cnt++;
        q.pop();
        for (int i = 0; i < p; i++) {
            int x = q.front();
            q.pop();
            q.push(x);
        }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
        printf("%d\n", v[i]);
    }
    return 0;
}


全部评论

相关推荐

看起来名字可以很长:笑死 我暑期实习阿里云的意向也被 qq 邮箱放在垃圾箱了
点赞 评论 收藏
分享
想进开水团喝开水:哦 给我一个 就算你真拿到牛友也会为你开心的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务