poj1363 Rails Central Europe 1997

 

P.S.:

输出换行

 

三个方法

 

 

1.直接按照要求做

根据给的数,需要push,pop哪些数据,具有唯一性

数最多进栈一次,出栈一次

O(n)

  1 Source Code
  2 Problem: 1363        User: congmingyige
  3 Memory: 728K        Time: 63MS
  4 Language: G++        Result: Accepted
  5 
  6     Source Code
  7 
  8     #include <cstdio>
  9     #include <cstdlib>
 10     #include <cmath>
 11     #include <cstring>
 12     #include <string>
 13     #include <algorithm>
 14     #include <iostream>
 15     using namespace std;
 16     #define ll long long
 17 
 18     const double eps=1e-8;
 19     const ll inf=1e9;
 20     const ll mod=1e9+7;
 21     const int maxn=1e5+10;
 22 
 23     int a[maxn];
 24     int st[maxn];
 25 
 26     int main()
 27     {
 28         int n,i,j,g;
 29         bool line=0;
 30         while (1)
 31         {
 32             scanf("%d",&n);
 33             if (n==0)
 34                 break;
 35 
 36             if (!line)
 37                 line=1;
 38             else
 39                 printf("\n");
 40 
 41             g=0;
 42             while (1)
 43             {
 44                 scanf("%d",&a[1]);
 45                 if (a[1]==0)
 46                     break;
 47                 for (i=2;i<=n;i++)
 48                     scanf("%d",&a[i]);
 49 
 50                 j=0;
 51                 for (i=1;i<=n;i++)
 52                 {
 53                     while (j<a[i])
 54                         st[++g]=++j;
 55                     if (st[g]==a[i])
 56                         g--;
 57                     else
 58                         break;
 59                 }
 60 
 61                 if (i==n+1)
 62                     printf("Yes\n");
 63                 else
 64                     printf("No\n");
 65             }
 66         }
 67         return 0;
 68     }
 69     /*
 70     6
 71     1 3 2 4 6 5
 72     1 4 3 5 2 6
 73     1 3 6 5 2 4
 74     5 4 3 2 1 6
 75     5 3 2 1 6 4
 76     1 3 5 4 6 2
 77     1 3 6 2 5 4
 78 
 79     4
 80     1 2 3 4
 81     1 2 4 3
 82     1 3 2 4
 83     1 3 4 2
 84     1 4 2 3
 85     1 4 3 2
 86     2 1 3 4
 87     2 1 4 3
 88     2 3 1 4
 89     2 3 4 1
 90     2 4 1 3
 91     2 4 3 1
 92     3 1 2 4
 93     3 1 4 2
 94     3 2 1 4
 95     3 2 4 1
 96     3 4 1 2
 97     3 4 2 1
 98     4 1 2 3
 99     4 1 3 2
100     4 2 1 3
101     4 2 3 1
102     4 3 1 2
103     4 3 2 1
104 
105     2 3 1 4
106     */

 

2.

一个序列,数x在第y个位置,第y个位置之后的小于x的数,必须是越往右越小。
即当前的最大数假设为z,则小于z的数必须是z-1,z-2,..,1。
 
时间上为O(n),但常数较大
 
  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <string>
  6 #include <algorithm>
  7 #include <iostream>
  8 using namespace std;
  9 #define ll long long
 10 
 11 const double eps=1e-8;
 12 const ll inf=1e9;
 13 const ll mod=1e9+7;
 14 const int maxn=1e5+10;
 15 
 16 int a[maxn];
 17 bool vis[maxn];
 18 
 19 int main()
 20 {
 21     int n,i,j;
 22     bool line=0;
 23     while (1)
 24     {
 25         scanf("%d",&n);
 26         if (n==0)
 27             break;
 28 
 29         if (!line)
 30             line=1;
 31         else
 32             printf("\n");
 33 
 34         while (1)
 35         {
 36             scanf("%d",&a[1]);
 37             if (a[1]==0)
 38                 break;
 39             for (i=2;i<=n;i++)
 40                 scanf("%d",&a[i]);
 41 
 42             memset(vis,0,sizeof(vis));
 43             j=0;
 44             for (i=1;i<=n;i++)
 45             {
 46                 if (j>a[i])
 47                     break;
 48                 else if (j<=a[i])
 49                     j=a[i]-1;
 50 
 51                 vis[a[i]]=1;
 52                 while (vis[j])
 53                     j--;
 54             }
 55 
 56             if (i==n+1)
 57                 printf("Yes\n");
 58             else
 59                 printf("No\n");
 60         }
 61     }
 62     return 0;
 63 }
 64 /*
 65 6
 66 1 3 2 4 6 5
 67 1 4 3 5 2 6
 68 1 3 6 5 2 4
 69 5 4 3 2 1 6
 70 5 3 2 1 6 4
 71 1 3 5 4 6 2
 72 1 3 6 2 5 4
 73 
 74 8
 75 1 3 6 8 7 5 4 2
 76 
 77 4
 78 1 2 3 4
 79 1 2 4 3
 80 1 3 2 4
 81 1 3 4 2
 82 1 4 2 3
 83 1 4 3 2
 84 2 1 3 4
 85 2 1 4 3
 86 2 3 1 4
 87 2 3 4 1
 88 2 4 1 3
 89 2 4 3 1
 90 3 1 2 4
 91 3 1 4 2
 92 3 2 1 4
 93 3 2 4 1
 94 3 4 1 2
 95 3 4 2 1
 96 4 1 2 3
 97 4 1 3 2
 98 4 2 1 3
 99 4 2 3 1
100 4 3 1 2
101 4 3 2 1
102 
103 2 3 1 4
104 */

 

previous数组

一个数跳出,使用previous数组,这个数不再被使用

O(n)

1 3 2 6 5 4 9 8 7

previous[4] 0

previous[6] 6->5->4->0 0

previous[7] 7->6->0

 

极限数据

1 2 3 4 5 6 7 8 9

按照上述方法O(n^2)

 

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <string>
  6 #include <algorithm>
  7 #include <iostream>
  8 using namespace std;
  9 #define ll long long
 10 
 11 const double eps=1e-8;
 12 const ll inf=1e9;
 13 const ll mod=1e9+7;
 14 const int maxn=1e5+10;
 15 
 16 int a[maxn],pre[maxn];
 17 bool vis[maxn];
 18 
 19 int main()
 20 {
 21     int n,i,j,k;
 22     bool line=0;
 23     while (1)
 24     {
 25         scanf("%d",&n);
 26         if (n==0)
 27             break;
 28 
 29         if (!line)
 30             line=1;
 31         else
 32             printf("\n");
 33 
 34         while (1)
 35         {
 36             scanf("%d",&a[1]);
 37             if (a[1]==0)
 38                 break;
 39             for (i=2;i<=n;i++)
 40                 scanf("%d",&a[i]);
 41 
 42             memset(vis,0,sizeof(vis));
 43             j=0;
 44             for (i=1;i<=n;i++)
 45             {
 46                 if (j>a[i])
 47                     break;
 48                 else if (j<=a[i])
 49                     j=a[i]-1,k=j;
 50 
 51                 vis[a[i]]=1;
 52                 while (vis[j])
 53                 {
 54                     if (pre[j])
 55                         j=pre[j];
 56                     else
 57                         j--;
 58                 }
 59                 if (j!=k)
 60                     pre[k]=j;
 61             }
 62 
 63             if (i==n+1)
 64                 printf("Yes\n");
 65             else
 66                 printf("No\n");
 67         }
 68     }
 69     return 0;
 70 }
 71 /*
 72 6
 73 1 3 2 4 6 5
 74 1 4 3 5 2 6
 75 1 3 6 5 2 4
 76 5 4 3 2 1 6
 77 5 3 2 1 6 4
 78 1 3 5 4 6 2
 79 1 3 6 2 5 4
 80 
 81 8
 82 1 3 6 8 7 5 4 2
 83 
 84 4
 85 1 2 3 4
 86 1 2 4 3
 87 1 3 2 4
 88 1 3 4 2
 89 1 4 2 3
 90 1 4 3 2
 91 2 1 3 4
 92 2 1 4 3
 93 2 3 1 4
 94 2 3 4 1
 95 2 4 1 3
 96 2 4 3 1
 97 3 1 2 4
 98 3 1 4 2
 99 3 2 1 4
100 3 2 4 1
101 3 4 1 2
102 3 4 2 1
103 4 1 2 3
104 4 1 3 2
105 4 2 1 3
106 4 2 3 1
107 4 3 1 2
108 4 3 2 1
109 
110 2 3 1 4
111 */

 

posted @ 2019-06-30 17:39  congmingyige  阅读(212)  评论(0编辑  收藏  举报