首页 > 其他分享 >2019南昌网络赛 E. Magic Master (模拟) The 2019 Asia Nanchang First Round Online Programming Contest -

2019南昌网络赛 E. Magic Master (模拟) The 2019 Asia Nanchang First Round Online Programming Contest -

时间:2023-02-07 12:32:44浏览次数:46  
标签:le Magic Contest int scanf next -- 2019 cards

 

John is not only a magic master but also a shuffling master.  

Famous though he is, he likes interacting with his fans by playing a game with his fantastic shuffling skills. 

The game shows as follows: 

He first shows a deck of pokers contains NNcards indexed 1,2,\dots ,N1,2,…,N and all the cards are the same. He notes that the side contains numbers as the front side and the other side as the back. Later he selects one of his fans to choose a positive integer MM that is not greater than 1010. After that, he will shuffle the cards purposefully and put it on the desktop. Note that at this moment the front sides of the cards are facing to the desk. 

Then, the selected fans should perform the following steps until there is no card on the desk. 

  1. Place the card on the top of the piles to John's hand. If there are already some cards in his hand, put it on the top of them. 
  2. If there remain cards on the desk:

(a) Put a card from the top of the piles to the bottom of it.

(b) Execute the step (aa) of MM times.

(c) Go to step 11.

Next, it is time to witness the miracle.  

John will continually overturn the cards on his hand top to bottom, and we will find that the cards are always in decreasing order. 

One day, one of John's fans, Tom, a smart JBer, understands the key to this magic. He turns to you and comments that the key to that magic is the shuffling John did previously. For any number MM, he will turn the cards in a specific order after the shuffling. As you are not as smart as Tom, you should find the index of the KKth cards from the top of the piles after the shuffling.

Input

The first line contain a positive integer T (1\le T \le10)T(1≤T≤10) – the number of test cases.

For each test cases, the first line contain a positive integer N (1 \le N \le 40000000)N(1≤N≤40000000) , indicating the numberof cards.

The second line contain a positive integer M (1\le M \le 10)M(1≤M≤10) – the number the fans select.

The third line contain an integer Q (1 \le Q \le 100)Q(1≤Q≤100) – indicating that there are QQ questions.

Next, there are QQ lines, each with an integer K (1 \le K \le N)K(1≤K≤N) – representing a query.

Output

For each query, you should output the index of the KKth cards from the top of the piles after the shuffling.

样例输入复制

1
5
1
2
2
3

样例输出复制

5
2

样例解释

Sample description - The indexs of the cards after the shuffling are: 1~5~2~4~31 5 2 4 3 (Left is top)

 

双端队列或者链表倒着模拟即可。

纪念我的指针为啥会TLE

2ms通过服气(大佬做法)

 


#include<bits/stdc++.h>
int main(void)
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        for (int i=0;i<q;++i)
        {
            int k;
            scanf("%d",&k);
            int old=n,pos=k,now=n;
            while (pos>1)
            {
                if (pos<=(m+1))
                {
                    --now;
                    pos-=m+1;
                    while (pos<=0) pos+=now;
                }
                else
                {
                    now=old-(pos/(m+1));
                    pos%=(m+1);
                    if (pos==0) ++now,pos=m+1;
                }
                old=now;
            }
            printf("%d\n",n-old+1);
        }
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=4e7+10;
int a[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        deque<int>Q;
        int n,m;
        scanf("%d%d",&n,&m);

        for(int i=n; i>=1; i--)
        {
			Q.push_front(i);
             if(i==1) break;
            for(int j=1; j<=m; j++)
            {
                int t=Q.back();
                Q.pop_back();
                Q.push_front(t);
            }
            

        }

        for(int i=1; i<=n; i++)
        {
            a[i]=Q.front();
           // cout<<a[i]<<endl;
            Q.pop_front();
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",a[x]);
        }
    }
}

 

数组模拟链表

 

#include<iostream>
using namespace std;
const int MAXN =50000005;
int a[MAXN];
int pre[MAXN];
int nxt[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        if(n==1)
        {
            for(int i=1; i<=q; i++)
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",1);
            }
            continue;
        }
        pre[n]=-1;
        nxt[n]=-1;
        int be=n;
        int en=n;
        int num=1;
        
        for(int i=n-1;i>=1;i--)
		{
			nxt[en]=i;
			pre[i]=en;
			nxt[i]=-1;
			en=i;
			if(i==1) break;
			
			num++;
			int M=m%num;
            if(M==0)
                continue;
		    int f=be;
		    int t=1;
		    while(t<M)
			{
				f=nxt[f];
				t++;
			}
			
			nxt[en]=be;
			pre[be]=en;
			
			be=nxt[f];
			pre[be]=-1;
			
			en=f;
			nxt[en]=-1;
			//cout<<num<<"*"<<f<<" "<<be<<" "<<en<<endl;
			
			
		}
		int j=n;
		
		while(be!=-1)
		{
			a[j--]=be;
			be=nxt[be];
			
			
		}
        
        
        
        for(int i=1; i<=q; i++)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",a[x]);
        }
    }
    return 0;
}

指针TLE

#include<iostream>
using namespace std;
const int MAXN =50000005;
int a[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        if(n==1)
        {
            for(int i=1; i<=q; i++)
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",1);
            }
            continue;
        }
        f=new Node;
        r=new Node;
        Node *s=new Node;
        s->data=n;
        f->next=s;
        r=s;


        int num=1;
        for(int i=n-1; i>=1; i--)
        {
            //cout<<i<<" "<<num<<endl;
            s=new Node;
            s->data=i;
            r->next=s;
            r=s;
            if(i==1)
                break;
            num++;
            int M=m%num;

            if(M==0)
                continue ;
            int temp=0;
            Node *p=f;
            while(temp<M)
            {
                p=p->next;
                temp++;
            }
            r->next=f->next;
            r=p;
            f->next=p->next;


            //cout<<f->next->data<<"*"<<r->data<<endl;
        }
        r->next=NULL;
        Node *p=f->next;
        int j=n;
        while(p!=NULL)
        {
            a[j--]=p->data;
            p=p->next;
        }

        delete p,f,r,s;
        for(int i=1; i<=q; i++)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",a[x]);
        }
    }
    return 0;
}

 

标签:le,Magic,Contest,int,scanf,next,--,2019,cards
From: https://blog.51cto.com/u_14932227/6041985

相关文章

  • 2019-ACM-ICPC 徐州网络赛 M. Longest subsequence 序列自动机
    Stringisaveryusefulthingandasubsequenceofthesamestringisequallyimportant.Nowyouhaveastring ss withlength nn andastring tt withlen......
  • 2019ccpc与icpc网络赛总结
    网络赛总结 ccpc一场网络赛,icpc的六场网络赛,接近一个月的时间,收获了不少东西。    这几场网络赛,我们队伍主要采取的策略是两个人做一道题,这样我们把低级错误......
  • 2019.6.10 周末总结
    昨天晚上忘了写训练博客,今天早上猛然惊醒。这一周还行,日常刷atcoder,有几道题还是相当不错的,很有思维含量。线上比赛打了不少四五场左右吧(除了星期天睡过头了),还打了一场cf......
  • 2019南昌大学邀请赛 M. Subsequence 序列自动机
     Giveastring SS and NN string T_iTi ,determinewhether T_iTi isasubsequenceof SS.Iftiissubsequenceof SS,print ​​YES​​​,elseprint ......
  • 2019南昌网络赛 I. Max answer(DP或者单调栈+思维)
    Alicehasamagicarray.Shesuggeststhatthevalueofaintervalisequaltothesumofthevaluesintheinterval,multipliedbythesmallestvalueinthein......
  • 2019/4/20、21训练博客
         一场中山大学的校赛,一场南昌大学网络赛.    两场都有遗憾,因为基本上前期还ok,但到后期就难以出题了,尤其是昨天的南昌大学网络赛,一道题已经房队......
  • 2019/4/18
     今天把最近打的51nod的比赛认为好题或者比赛的时候没做出来、没做到的题补了补,这些题,大部分都做过,看着眼熟,但依旧有的能出,有的不能出,害的好好下功夫,至少省赛前把三集题做......
  • USACO 2023 January Contest, Bronze Problem 3. Moo Operations
        这道题目灰常简单,我们先从最简单的3个字符串开始有以下几种情况:  可以看到,只有在中间是O的情况下才有可能变成MOO辣么我们不妨在在s串中枚举这个中间O......
  • AtCoder Beginner Contest 288
    A-ManyA+BProblems签到#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongint32_tmain(){intn;cin>>n;for(inta,b......
  • 2022-2023 ICPC Asia East - Hangzhou Regional Contest 中文版题面(部分)
    B给定两个长度为\(n\)的整数序列\(c,d\)和一个长度为\(m\)的\(01\)序列\(v\)。这里的\(c,d,w\)下标从\(1\)开始。有\(q\)次修改,每次会选择一个\(i\in......