fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct no{
  5. int c,s;
  6. struct no *prox;
  7. };
  8.  
  9. typedef struct{
  10. struct no *topo;
  11. }pilha;
  12.  
  13. bool isEmpty(pilha *p){
  14. if(p->topo==NULL)return true;
  15. return false;
  16. }
  17.  
  18. bool push(pilha *p,int c, int s){
  19. struct no *aux;
  20. aux = (struct no *)malloc(sizeof(struct no));
  21. if(aux==NULL)return false;
  22. aux->c = c;
  23. aux->s = s;
  24. aux->prox = p->topo;
  25. p->topo = aux;
  26. return true;
  27. }
  28.  
  29. bool pop(pilha *p,int *c, int *s){
  30. if(isEmpty(p))return false;
  31. struct no *aux;
  32. aux = p->topo;
  33. p->topo = p->topo->prox;
  34. *c = aux->c;
  35. *s = aux->s;
  36. free(aux);
  37. return true;
  38. }
  39.  
  40. bool first(pilha *p,int *c, int *s){
  41. if(isEmpty(p))return false;
  42. *c = p->topo->c;
  43. *s = p->topo->s;
  44. return true;
  45. }
  46. bool seccond(pilha *p,int *c, int *s){
  47. if(isEmpty(p))return false;
  48. if(p->topo->prox==NULL)return false;
  49. *c = p->topo->prox->c;
  50. *s = p->topo->prox->s;
  51. return true;
  52. }
  53.  
  54. bool last(pilha *p, int *c, int *s){
  55. if(isEmpty(p))return false;
  56. struct no *aux = p->topo;
  57. while(aux->prox!=NULL){
  58. aux = aux->prox;
  59. }
  60. *c = aux->c;
  61. *s = aux->s;
  62. return true;
  63. }
  64.  
  65. int contNos(pilha *p){
  66. if(isEmpty(p))return 0;
  67. struct no *aux = p->topo;
  68. int cont=0;
  69. while(aux!=NULL){
  70. aux = aux->prox;
  71. cont++;
  72. }
  73. return cont;
  74. }
  75.  
  76. bool destruct(pilha *p){
  77. if(isEmpty(p))return false;
  78. int x,y;
  79. while(!isEmpty(p)){
  80. pop(p,&x,&y);
  81. }
  82. return true;
  83. }
  84.  
  85. int main(){
  86. pilha p;
  87. int n=0,k=0,c=0,s=0;
  88. bool continua = true;
  89. while(true){
  90. scanf("%d%d", &n,&k);
  91. if(k==n&&n==0)break;
  92. for(int i=0;i<n;i++){
  93. scanf("%d%d", &c,&s);
  94. if(isEmpty(&p)){
  95. push(&p, c,s);
  96. //printf("Primeira insercao\n");
  97. }else{
  98. //printf("Insercao subsequesnte\n");
  99. int c2=0,s2=0;
  100. first(&p,&c2,&s2);
  101. if(s2<=c){
  102. pop(&p,&c2,&s2);
  103. //printf("o carro que chagou as %d saiu agora %d\n", c2,s2);
  104. //first(&p,&c2,&s2);
  105. }
  106. if(!isEmpty(&p)){
  107. struct no *aux = p.topo;
  108. int cont=0;
  109. while(aux!=NULL){
  110. int i=0,j=0;
  111. if(aux->s<=c){
  112. pop(&p,&j,&i);
  113. aux = p.topo;
  114. }else break;
  115.  
  116. }
  117. }
  118. push(&p,c,s);
  119. }
  120. //printf("%d no estacionamento\n", contNos(&p));
  121. if(c>s||contNos(&p)>k){
  122. continua = false;
  123. //break;
  124. }
  125. }
  126. if(!continua)printf("Nao\n");
  127. else{
  128. int x,y,a,b;
  129. first(&p,&a,&b);
  130. //printf("O proximo carro chegou as: %d e vai sair: %d\n", a,b);
  131. bool resp = false;
  132. while(true){
  133. if(isEmpty(&p)){
  134. //printf("Pilha Vazia\n");
  135. resp==true;
  136. break;
  137. }
  138. int x,y,a,b;
  139. first(&p,&a,&b);
  140. if(seccond(&p,&x,&y)){
  141. //printf("a %d x %d b %d y %d\n", a,x,b,y);
  142. if(a>=x&&b<=y){
  143. int m,n;
  144. pop(&p,&m,&n);
  145. }else if(a<=x&&b>=y){
  146. int x=0,y=0,a,b;
  147. pop(&p,&x,&y);
  148. pop(&p,&a,&b);
  149. push(&p,x,y);
  150. }else if(a<=x&&b<=x){
  151. int x=0,y=0;
  152. pop(&p,&x,&y);
  153. }else if(a>=y&&b>=y){
  154. int x=0,y=0,a,b;
  155. pop(&p,&x,&y);
  156. pop(&p,&a,&b);
  157. push(&p,x,y);
  158. }
  159. else{
  160. break;
  161. }
  162. }else{
  163. //printf("Nao possui segundo elemento\n");
  164. resp = true;
  165. break;
  166. }
  167.  
  168. }
  169. if(resp)printf("Sim\n");
  170. else printf("Nao\n");
  171. }
  172. continua = true;
  173. destruct(&p);
  174. }
  175. return 0;
  176. }
Success #stdin #stdout 0s 4552KB
stdin
3 2
1 10
2 5
6 9
3 2
1 10
2 5
6 12
0 0
stdout
Sim
Nao