列表

详情


NC25028. [USACO 2007 Dec B]Card Stacking

描述

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.

输入描述

* Line 1: Three space-separated integers: N, K, and P

输出描述

* 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.

示例1

输入:

3 9 2

输出:

3
7
8

说明:

Bessie should put the "good" cards in positions 3, 7, and 8. The cards will be dealt as follows; the card numbers are "position in original deck":
Card Deck P1 P2 Bessie
Initial configuration 1 2 3 4 5 6 7 8 9 - - - - - - - - -
Deal top card [1] to Player 1 2 3 4 5 6 7 8 9 1 - - - - - - - -
Top card to bottom (#1 of 2) 3 4 5 6 7 8 9 2 1 - - - - - - - -
Top card to bottom (#2 of 2) 4 5 6 7 8 9 2 3 1 - - - - - - - -
Deal top card [4] to Player 2 5 6 7 8 9 2 3 1 - - 4 - - - - -
Top card to bottom (#1 of 2) 6 7 8 9 2 3 5 1 - - 4 - - - - -
Top card to bottom (#2 of 2) 7 8 9 2 3 5 6 1 - - 4 - - - - -
Deal top card [7] to Bessie 8 9 2 3 5 6 1 - - 4 - - 7 - -
Top card to bottom (#1 of 2) 9 2 3 5 6 8 1 - - 4 - - 7 - -
Top card to bottom (#2 of 2) 2 3 5 6 8 9 1 - - 4 - - 7 - -
Deal top card [2] to Player 1 3 5 6 8 9 1 2 - 4 - - 7 - -
Top card to bottom (#1 of 2) 5 6 8 9 3 1 2 - 4 - - 7 - -
Top card to bottom (#2 of 2) 6 8 9 3 5 1 2 - 4 - - 7 - -
Deal top card [6] to Player 2 8 9 3 5 1 2 - 4 6 - 7 - -
Top card to bottom (#1 of 2) 9 3 5 8 1 2 - 4 6 - 7 - -
Top card to bottom (#2 of 2) 3 5 8 9 1 2 - 4 6 - 7 - -
Deal top card [3] to Bessie 5 8 9 1 2 - 4 6 - 7 3 -
Top card to bottom (#1 of 2) 8 9 5 1 2 - 4 6 - 7 3 -
Top card to bottom (#2 of 2) 9 5 8 1 2 - 4 6 - 7 3 -
Deal top card [9] to Player 1 5 8 1 2 9 4 6 - 7 3 -
Top card to bottom (#1 of 2) 8 5 1 2 9 4 6 - 7 3 -
Top card to bottom (#2 of 2) 5 8 1 2 9 4 6 - 7 3 -
Deal top card [5] to Player 2 8 1 2 9 4 6 5 7 3 -
Top card to bottom (#1 of 2) 8 1 2 9 4 6 5 7 3 -
Top card to bottom (#1 of 2) 8 1 2 9 4 6 5 7 3 -
Deal top card [8] to Bessie - 1 2 9 4 6 5 7 3 8
Bessie will end up with the "good cards" that have been placed in positions 3, 7, and 8 in the original deck.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 19ms, 内存消耗: 1628K, 提交时间: 2019-07-13 12:28:35

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int nex[100005],pre[100005],b[100005];
int main(){
	int n,k,p;
	scanf("%d%d%d",&n,&k,&p);
	for(int i=1;i<=k;i++)nex[i]=i+1,pre[i]=i-1;
	nex[k]=1;pre[1]=k;
	int now=1;
	for(int i=1;i<=k;i++){
		nex[pre[now]]=nex[now];
		pre[nex[now]]=pre[now];
		if(i%n==0)b[i/n]=now;
		for(int j=0;j<=p;j++)now=nex[now];
	}
	sort(b+1,b+k/n+1);
	for(int i=1;i<=k/n;i++)printf("%d\n",b[i]);
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 12ms, 内存消耗: 5212K, 提交时间: 2019-07-13 11:59:15

#include<stdio.h>
#include<algorithm>
using namespace std;
int queue[10000007];
int a[10000007];
int main()
{
	int n,k,p,top,end,count=0;
	scanf("%d%d%d",&n,&k,&p);
	for(int i=1;i<=k;i++){
		queue[i]=i;
	}
	top=1,end=k;
	for(int i=1;i<=k;i++){
		if(i%n==0)a[count++]=queue[top++];
		else top++;
		for(int j=1;j<=p;j++){
			queue[++end]=queue[top++];
		}
	}
	sort(a,a+count);
	for(int i=0;i<count;i++){
		printf("%d\n",a[i]);
	}
 } 

上一题