Shuffle Card

Shuffle Card

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0

Problem Description
A deck of card consists of n cards. Each card is different, numbered from 1 to n. At first, the cards were ordered from 1 to n. We complete the shuffle process in the following way, In each operation, we will draw a card and put it in the position of the first card, and repeat this operation for m times.

Please output the order of cards after m operations.

Input
The first line of input contains two positive integers n and m.(1<=n,m<=105)

The second line of the input file has n Numbers, a sequence of 1 through n.

Next there are m rows, each of which has a positive integer si, representing the card number extracted by the i-th operation.

Output
Please output the order of cards after m operations. (There should be one space after each number.)

Sample Input
5 3
1 2 3 4 5
3
4
3

Sample Output
3 4 1 2 5

题意:给你n张卡片,和q个要执行操作的序号;每张卡片都有一个序号1,2…n;操作:将序号为 i 的卡片放到第一位;问你q次操作后这一叠卡片的顺序;
做法:例如n = 3;初始顺序是 2 1 3;q = 3;分别是3 3 3;
那么只要执行最后一个3操作就好了,因为前2个操作相当于多余了;
所以,把 q 个操作序号 出现超过1次的,把前面重复的删掉,只保留最后一个;
然后答案就是:从后往前输出 q个操作序号,然后按顺序输出 初始序列中没有被执行操作的卡片序号

AC_Code

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5;
int a[maxn+10],q[maxn+10];

struct node {
	bool used;
	int preindex;
} use[maxn+10];
bool flagq[maxn+10];

int main() {
	int n,qq;
	cin >> n >> qq;
	int i,j,k;
	for(i = 0; i < n; i++) {
		cin >> a[i];
		use[a[i]].used = false;
	}
	memset(flagq,false,sizeof(flagq));
	for(i = 0; i < qq; i++) {
		cin >> q[i];
		if(use[q[i]].used) {       //yong le
			flagq[use[q[i]].preindex] = false;
			flagq[i] = true;
			use[q[i]].preindex = i;
		} else {                //meiyong guo
			use[q[i]].used = true;
			flagq[i] = true;
			use[q[i]].preindex = i;
		}
	}
	//print()
	bool title = true;
	for(j = qq-1; j >= 0; j--) {
		if(flagq[j]) {
			if(title) {
				cout << q[j];
				title = false;
			} else {
				cout << " " << q[j];
			}
		}
	}
	for(k = 0; k < n; k++) {
		if(!use[a[k]].used) {
			if(title){
				cout << a[k];
			}else{
				cout << " " << a[k];
			}
		}
	}
	cout << endl;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private int[] cards = {R.drawable.card1, R.drawable.card2, R.drawable.card3, R.drawable.card1, R.drawable.card5, R.drawable.card6, R.drawable.card7, R.drawable.card8}; private int[] cardIds = {R.id.card1, R.id.card2, R.id.card3, R.id.card4, R.id.card5, R.id.card6, R.id.card7, R.id.card8}; private int[] cardStatus = new int[8]; private int firstCard = -1, secondCard = -1; private boolean isClickable = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i = 0; i < cardIds.length; i++) { ImageView card = findViewById(cardIds[i]); card.setTag(i); card.setOnClickListener(this); } Collections.shuffle(Arrays.asList(cards)); } @Override public void onClick(View v) { if (!isClickable) { return; } int id = (int) v.getTag(); if (cardStatus[id] == 1) { return; } if (firstCard == -1) { firstCard = id; ImageView card = findViewById(cardIds[id]); card.setImageResource(cards[id]); } else { secondCard = id; ImageView card = findViewById(cardIds[id]); card.setImageResource(cards[id]); if (cards[firstCard] == cards[secondCard]) { cardStatus[firstCard] = 1; cardStatus[secondCard] = 1; firstCard = -1; secondCard = -1; if (checkWin()) { Toast.makeText(this, "You Win!", Toast.LENGTH_SHORT).show(); } } else { isClickable = false; Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { ImageView card1 = findViewById(cardIds[firstCard]); ImageView card2 = findViewById(cardIds[secondCard]); card1.setImageResource(R.drawable.card_back); card2.setImageResource(R.drawable.card_back); firstCard = -1; secondCard = -1; isClickable = true; } }, 1000); } } } private boolean checkWin() { for (int i = 0; i < cardStatus.length; i++) { if (cardStatus[i] == 0) { return false; } } return true; } }请帮我优化这段代码
最新发布
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值