Tsinghua dsa pa2

    第一题,列车调度(train)

    在这个题目中,模拟压栈出栈,同时判断调度方案是否可行。

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAX 1600005
 4 using namespace std;
 5 int stack[MAX];//stack[0]作为旗标
 6 int top;
 7 int pushed;//已经压入的最大值
 8 int out[MAX];//输出
 9 char result[2*MAX][5];
10 int result_len;//result的长度
11 int main(int argc,char *argv[]){
12 //    freopen("in.txt","r",stdin);
13     int len,capacity;
14     scanf("%d %d", &len, &capacity);
15     for(int k=0;k<len;k++)
16         scanf("%d",&(out[k]));
17     int i=0;//出栈的元素的下标
18     while(i<len){//当i==len时,所有out中的元素都成功出栈
19         if(stack[top]==out[i]){
20             strcpy(result[result_len++],"pop");
21             top--;
22             i++;
23         }else if(stack[top]<out[i]){
24             while(pushed<out[i]){
25                 stack[++top]=++pushed;
26                 if(top>capacity){
27                     printf("No
");
28                     return 0;
29                 }
30                 strcpy(result[result_len++],"push");
31             }
32         }else{
33                 printf("No
");
34                 return 0;
35         }
36     }
37     for(int j=0;j<result_len;j++)
38         printf("%s
",result[j]);
39     return 0;
40 }

    第二题,隧道(Tunel),通过95%

    这道题的关键在于如何在O(1)时间内获取队列中的最大值。提示见邓俊辉老师的数据结构习题解答讲义。

 1 #include <cstdio>
 2 using namespace std;
 3 #define MAX 2000005
 4 
 5 int height[MAX];
 6 int head,tail;
 7 
 8 typedef struct{
 9     int index;
10     int count;
11 }Mirrorqueue;
12 Mirrorqueue que[MAX];
13 int headq,tailq;
14 
15 int main(int argc, char *argv[]){
16 //    freopen("in.txt","r",stdin);
17     int n;
18     scanf("%d
", &n);
19     char c;
20     while(n--){
21         scanf("%c", &c);
22         int counttmp=1;
23         int i=0;
24         switch (c){
25         case 'E':
26             scanf("%d
", height+tail);
27             for(i=tailq;i>headq;i--){
28                 if(height[que[tailq-1].index]<height[tail]){
29                     counttmp+=que[tailq-1].count;
30                     tailq--;
31                 }else if(height[que[tailq-1].index]==height[tail]){
32                     que[tailq-1].count+=counttmp;
33                     break;
34                 }else{
35                     que[tailq].index=tail;
36                     que[tailq].count=counttmp;
37                     tailq++;    
38                     break;
39                 }
40             }
41             if(i==headq){
42                 que[tailq].index=tail;
43                 que[tailq].count=counttmp;
44                 tailq++;
45             }
46             tail++;
47             break;
48         case 'M':
49             scanf("%c",&c);//忽略换行符
50             printf("%d
",height[que[headq].index]);
51             break;
52         case 'D':
53             scanf("%c",&c);//忽略换行符
54             que[headq].count--;
55             if(que[headq].count==0)
56                 headq++;
57             printf("%d
",height[head++]);
58         }
59     }
60     return 0;
61 }

    第三题,真二叉树重构(Rebuild),通过95%

    提示:

 1 #include <cstdio>
 2 using namespace std;
 3 #define MAX 4000005
 4 int preord[MAX],postord[MAX];
 5 
 6 int stack[1000005];
 7 int top;
 8 
 9 int printed,nodeNums;//已经打印的结点数目和总的结点数目
10 int search(int *source,int head,int tail,int e){
11     for(int i=head;i<=tail;i++){
12         if(source[i]==e)
13             return i;
14     }
15 }
16 int rebuild(int *pre, int *post,int preh,int pret,int posth,int postt){
17     if(preh==pret){
18         if(printed==nodeNums-1){
19             printf("%d
",pre[preh]);
20             printed++;
21         }
22         else{
23             printf("%d ",pre[preh]);
24             printf("%d ",stack[--top]);
25             printed+=2;
26         }
27         return 0;
28     }
29     //store pre[preh];//
30     stack[top++]=pre[preh];
31     int lroot=search(post,posth,postt,pre[preh+1]);//在后序遍历中查找左子树根
32     int rroot=search(pre,preh,pret,post[postt-1]);
33     rebuild(pre,post,preh+1,rroot-1,posth,lroot);
34     rebuild(pre,post,rroot,pret,lroot+1,postt-1);
35     //输出
36     return 0;
37 }
38 int main(int argc, char *argv[]){
39 //    freopen("in.txt","r",stdin);
40     int nodeNum;
41     char c;
42     scanf("%d
",&nodeNum);
43     nodeNums=nodeNum;
44     for(int i=0;i<nodeNum;i++)
45         scanf("%d",preord+i);
46     scanf("%c",&c);//忽略换行符
47     for(int j=0;j<nodeNum;j++)
48         scanf("%d",postord+j);
49     rebuild(preord,postord,0,nodeNum-1,0,nodeNum-1);
50     return 0;
51 }
原文地址:https://www.cnblogs.com/wangaohui/p/4464074.html