02-線性結構4 Pop Sequence(PTA數據結構題)

原文地址:http://www.javashuo.com/article/p-hjierohi-ky.htmlhtml

 

遵循兩條原則:
1.某出棧元素以後出棧的各元素,若比其小(即在原隊列中先進棧),必須爲從大到小排序(即倒序);
2.最大的倒序數列,其元素數目不能夠超過棧大小。例如5 6 4 3 7 2 1,最大倒序數列爲5 4 3 2 1,若棧大小爲4,則不可能存在該序列;ide

注:1.能夠參考https://www.bilibili.com/video/BV125411W7Pa?from=search&seid=14950165865611086071spa

 

 1 int main(){
 2     int pop[1001]={0};
 3     int M,N,K,Y,count;
 4     count=Y=M=N=K=0;
 5     scanf("%d %d %d",&M,&N,&K);
 6     for (int i = 0; i < K; ++i) {
 7         for (int j = 0; j < N; ++j) {
 8             scanf("%d",&pop[j+1]);
 9         }
10         for (int k = N-2; k >0; --k) {
11             int p1;
12             p1=pop[k];
13             for (int j = k+1; j < N+1; ++j) {
14                 if(pop[j]<pop[k]){
15                     if(pop[j]<p1) {
16                         p1 = pop[j];
17                         count++;
18                         if(count>=M){
19                             Y=1;
20                             goto wrong;
21                         }
22                     }
23                     else{
24                         Y=1;
25                         goto wrong;
26                     }
27                 }
28             }
29             count=0;
30         }
31         wrong:
32         Y?printf("NO\n"):printf("YES\n");
33         Y=0;
34         count=0;
35     }
36     return 0;
37 }
相關文章
相關標籤/搜索