zoukankan      html  css  js  c++  java
  • 【sicily】 1934. 移动小球

    
    

    Description

    
    

    你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2)。其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y右边。 指令保证合法,即X不等于Y。 例如,初始状态1,2,3,4,5,6的小球执行1 1 4后,小球1被移动到小球4的左边,即2,3,1,4,5,6。如果再执行2 3 5,结点3将会移到5的右边,即2,1,4,5,3,6。

    
    

    Input

    
    

    第一行为一个整数t(0<t<10),表示测试用例个数。每个测试用例的第一行为两个整数n(1<n<=500000)和m(0<m<100000),n表示小球的个数,m为指令条数,以下m行每行为一条指令。

    
    

    Output

    
    

    为每个测试用例单独输出一行,从左到右输出最后序列,每个数字后面跟一个空格。




    1
    #include<iostream> 2 using namespace std; 3 struct 4 { 5 int l; 6 int r; 7 }data[500001]; //尽量大 8 int main() 9 { 10 int t,n,m,i,h,u; 11 int control,x,y; 12 cin>>t; 13 for(int w=0;w<t;w++) 14 { 15 cin>>n>>m; 16 data[0].r=1; 17 data[n+1].l=n; 18 for(i=1;i<=n;i++) 19 { 20 data[i].l=i-1; 21 data[i].r=i+1; 22 } 23 for(int d=0;d<m;d++) 24 { 25 cin>>control>>x>>y; 26 data[data[x].l].r=data[x].r; 27 data[data[x].r].l=data[x].l; 28 if(control==1) 29 { 30 data[x].l=data[y].l; 31 data[x].r=y; 32 data[data[y].l].r=x; 33 data[y].l=x; 34 } 35 else 36 { 37 data[x].l=y; 38 data[x].r=data[y].r; 39 data[data[y].r].l=x; 40 data[y].r=x; 41 } 42 } 43 h=0; 44 for( u=1;u<=n;u++) 45 { 46 cout<<data[h].r<<" "; 47 h=data[h].r; 48 } 49 cout<<endl; 50 } 51 return 0; 52 }

    如果用小球的绝对位置来做,每动一个小球其他小球的位置信息都要改,应该(肯定)会超时,如果只用相对位置做的话,每次只要改几个相关小球的位置信息就好了,效率高很多。(l左,r右)

    下面是绝对位置堆栈来做:(可运行,sicily不通过)

      1 #include<iostream>
      2 #include<stack>
      3 using namespace std;
      4 int main()
      5 {
      6     stack<int> a; 
      7     stack<int> b;
      8     int n,i,zhiling,x,y,save,temp,temp1;
      9     int r;
     10     int t;
     11     cin>>t; 
     12     for(r=0;r<t;r++)
     13     { 
     14     cin>>n;
     15     for(i=1;i<=n;i++)
     16     a.push(i);
     17     int m;
     18     cin>>m;
     19     int j;
     20     for(j=0;j<m;j++)
     21     {
     22     cin>>zhiling>>x>>y;    
     23     if(zhiling==1)
     24     {
     25         for(i=1;i<=n;i++)
     26         {
     27         if(a.top()!=x)
     28         {
     29         temp=a.top();
     30         a.pop();
     31         b.push(temp);    
     32         } 
     33         else
     34         {
     35         save=a.top();
     36         a.pop();
     37         }
     38         }
     39         for(i=1;i<=n-1;i++)
     40         {
     41         if(b.top()!=y) 
     42         {
     43         temp1=b.top();
     44         b.pop();
     45         a.push(temp1);
     46         }
     47         else
     48         {
     49         a.push(save);
     50         temp1=b.top();
     51         b.pop();
     52         a.push(temp1);
     53         } 
     54         }
     55     }
     56         if(zhiling==2)
     57     {
     58         for(i=1;i<=n;i++)
     59         {
     60         if(a.top()!=x)
     61         {
     62         temp=a.top();
     63         a.pop();
     64         b.push(temp);    
     65         } 
     66         else
     67         {
     68         save=a.top();
     69         a.pop();
     70         }
     71         }
     72         for(i=1;i<=n-1;i++)
     73         {
     74         if(b.top()!=y) 
     75         {
     76         temp1=b.top();
     77         b.pop();
     78         a.push(temp1);
     79         }
     80         else
     81         {
     82         temp1=b.top();
     83         b.pop();
     84         a.push(temp1);
     85         a.push(save);
     86         }
     87         }
     88     }
     89     } 
     90     int w[100];
     91     for(i=0;i<n;i++)
     92     {
     93         w[i]=a.top();
     94         a.pop();
     95     }
     96     for(i=n-1;i>=0;i--)
     97     {
     98         cout<<w[i]<<" ";
     99     }
    100     cout<<endl;
    101     } 
    102     return 0;
    103 }

    下面是绝对位置数组来做:(可运行,sicily不通过,好LOW)

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int t;
     6     cin>>t;
     7     for(int e=0;e<t;e++){
     8     int n;
     9     cin>>n;
    10     int a[100];
    11     int b[100];
    12     int count;
    13     for(int o=0;o<100;o++)
    14     a[o]=0;
    15     int zhiling,x,y,w;
    16     for(int i=1;i<=n;i++)
    17     a[i]=i;
    18     int m;
    19     cin>>m;
    20     for(int j=0;j<m;j++)
    21     {
    22         cin>>zhiling>>x>>y;
    23         if(zhiling==1)
    24         {
    25             for(w=n+1;w>=y+1;w--)
    26             {
    27                 a[w]=a[w-1];
    28             }
    29             a[y]=x;
    30             for(w=1;w<=n+1;w++)
    31             {
    32                 if(a[w]==x&&a[w+1]!=y)
    33                 {
    34                     a[w]=0;
    35                 }
    36             }
    37             
    38         }
    39             if(zhiling==2)
    40         {
    41             for(w=n+1;w>=y+2;w--)
    42             {
    43                 a[w]=a[w-1];
    44             }
    45             a[y+1]=x;
    46             for(w=1;w<=n+1;w++)
    47             {
    48                 if(a[w]==x&&a[w-1]!=y)
    49                 {
    50                     a[w]=0;
    51                 }
    52             }
    53             
    54         }
    55         count=1;
    56         for(int c=0;c<100;c++)
    57         {
    58             if(a[c]!=0)
    59             {
    60             b[count]=a[c];
    61             count++;    
    62             }
    63         }
    64         for(int s=1;s<=n;s++)
    65         {
    66             a[s]=b[s];
    67         }
    68     }
    69     for(int k=1;k<=n;k++)
    70     {
    71         cout<<a[k]<<" ";
    72     }
    73     cout<<endl;
    74 }
    75     return 0;
    76  } 
  • 相关阅读:
    在 Cocos2d-x 中添加自己的微博链接
    关于屏幕适配
    [抽象工厂模式]在游戏开发中的应用
    字符串排列组合
    子矩阵最大和
    网易游戏编程题第二题
    动态规划,最大子段和
    C++的四种显示类型转换
    多线程编程
    预处理等等
  • 原文地址:https://www.cnblogs.com/tenderwx/p/5657453.html
Copyright © 2011-2022 走看看