fork(1) download
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cassert>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <deque>
  11. #include <functional>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include <limits>
  15. #include <list>
  16. #include <map>
  17. #include <memory>
  18. #include <numeric>
  19. #include <queue>
  20. #include <set>
  21. #include <sstream>
  22. #include <stack>
  23. #include <string>
  24. #include <utility>
  25. #include <vector>
  26.  
  27. using namespace std;
  28.  
  29. #ifndef ONLINE_JUDGE
  30. bool debug = false;
  31. #else
  32. bool debug = true;
  33. #endif
  34.  
  35. typedef long long unsigned llu;
  36. typedef long long lld;
  37. typedef long ld;
  38.  
  39. //definition
  40. #define vi vector < int >
  41. #define vld vector < ld >
  42. #define vlld vector < lld >
  43. #define pii pair <int, int>
  44.  
  45. #define gc getchar
  46. #define pc putchar
  47. #define rr freopen("input.txt", "r", stdin)
  48. #define wr freopen("output.txt", "w", stdout)
  49.  
  50. #define MOD 1000000007
  51. #define MAX 100005
  52. #define pi 3.14159265358979
  53. #define ESP (1e-9)
  54.  
  55. #define fu(i,a,n) for(i=a;i<n;i++)
  56. #define fue(i,a,n) for(i=a;i<=n;i++)
  57. #define fd(i,n,a) for(i=n;i>a;i--)
  58. #define fde(i,n,a) for(i=n;i>=a;i--)
  59. #define fr(i,a) for(i=0;i<a;i++)
  60. #define fe(i,a) for(i=0;i<=a;i++)
  61.  
  62. #define sd(n) scanf("%d",&n)
  63. #define sld(n) scanf("%ld",&n)
  64. #define slld(n) scanf("%lld",&n)
  65. #define pfd(n) printf("%d",n)
  66. #define pfld(n) printf("%ld",n)
  67. #define pflld(n) printf("%lld",n)
  68.  
  69. #define ff first
  70. #define ss second
  71. #define sz size()
  72. #define clr clear()
  73. #define len length()
  74. #define pb push_back
  75. #define mp make_pair
  76. #define gcd(a,b) __gcd(a,b)
  77. #define all(vi) vi.begin(), vi.end()
  78. #define mem(i,n) memset(i,n,sizeof(i))
  79.  
  80. #define imax numeric_limits<int>::max()
  81. #define imin numeric_limits<int>::min()
  82. #define ldmax numeric_limits<ld>::max()
  83. #define ldmin numeric_limits<ld>::min()
  84. #define lldmax numeric_limits<lld>::max()
  85. #define lldmin numeric_limits<lld>::min()
  86. #define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
  87. //end of definition
  88.  
  89. //fast input
  90.  
  91. int scan_d() {register int c=gc();int x=0;for(;(c<48 || c>57);c = gc());for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}return x;}
  92. ld scan_ld() {register ld c=gc();ld x=0;for(;(c<48 || c>57);c = gc());for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}return x;}
  93. lld scan_lld() {register lld c=gc();lld x=0;for(;(c<48 || c>57);c = gc());for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}return x;}
  94. llu scan_llu() {register llu c=gc();llu x=0;for(;(c<48 || c>57);c = gc());for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}return x;}
  95.  
  96. //end of fast input
  97.  
  98. //fast output
  99.  
  100. //no line break
  101. void print_d(int n) {if(n<0){n=-n;pc('-');}int i=10;char output_buffer[10];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<10);}
  102. void print_ld(ld n) {if(n<0){n=-n;pc('-');}int i=11;char output_buffer[11];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<11);}
  103. void print_lld(lld n) {if(n<0){n=-n;pc('-');}int i=21;char output_buffer[21];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<21);}
  104. void print_llu(llu n) {int i=21;char output_buffer[21];do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<21);}
  105.  
  106. //new line
  107. void println_d(int n) {if(n<0){n=-n;pc('-');}int i=10;char output_buffer[11];output_buffer[10]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<11);}
  108. void println_ld(ld n) {if(n<0){n=-n;pc('-');}int i=11;char output_buffer[12];output_buffer[11]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<12);}
  109. void println_lld(lld n) {if(n<0){n=-n;pc('-');}int i=21;char output_buffer[22];output_buffer[21]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<22);}
  110. void println_llu(llu n) {int i=21;char output_buffer[22];output_buffer[21]='\n';do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<22);}
  111.  
  112. //special char
  113. char sp;
  114. void printsp_d(int n) {if(n<0){n=-n;pc('-');}int i=10;char output_buffer[11];output_buffer[10]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<11);}
  115. void printsp_ld(ld n) {if(n<0){n=-n;pc('-');}int i=11;char output_buffer[12];output_buffer[11]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<12);}
  116. void printsp_lld(lld n) {if(n<0){n=-n;pc('-');}int i=21;char output_buffer[22];output_buffer[21]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<22);}
  117. void printsp_llu(llu n) {int i=21;char output_buffer[22];output_buffer[21]=sp;do{output_buffer[--i]=(n%10)+'0';n/=10;}while(n);do{pc(output_buffer[i]);}while(++i<22);}
  118.  
  119. //end of fast output
  120.  
  121. int main()
  122. {
  123. sp=' ';
  124. /*rr;
  125. wr;*/
  126. int n,i,temp;
  127. bool f=true;
  128. stack < int > stk;
  129. n=scan_d();
  130. while(n!=0)
  131. {
  132. f=true;
  133. temp=0;
  134. int a[n];
  135. fr(i,n)
  136. a[i]=scan_d();
  137. fr(i,n)
  138. {
  139. if(temp+1!=a[i])
  140. {
  141. if(stk.size()==0 || stk.top()!=temp+1)
  142. stk.push(a[i]);
  143. else
  144. {
  145. while(true)
  146. {
  147. if(stk.sz==0)
  148. break;
  149. if(temp+1==stk.top())
  150. {
  151. temp++;
  152. stk.pop();
  153. //println_d(temp);
  154. }
  155. else
  156. break;
  157. }
  158. stk.push(a[i]);
  159. }
  160. }
  161. else
  162. {
  163. temp++;
  164. //println_d(temp);
  165. }
  166. }
  167. while(true)
  168. {
  169. if(stk.empty()==true)
  170. break;
  171. if(temp+1!=stk.top())
  172. {
  173. /*printsp_d(temp);
  174. println_d(stk.top());*/
  175. f=0;
  176. break;
  177. }
  178. else
  179. {
  180. /*puts("Stack");
  181. println_d(stk.top());*/
  182. temp++;
  183. stk.pop();
  184. }
  185. }
  186. if(f)
  187. printf("yes\n");
  188. else
  189. printf("no\n");
  190. n=scan_d();
  191. f=true;
  192. while(!stk.empty())
  193. stk.pop();
  194. }
  195. return 0;
  196. }
Success #stdin #stdout 0s 3284KB
stdin
5 
3 1 2 5 4 
5 
4 1 5 3 2 
5 
3 1 2 5 4 
5 
5 3 2 1 4 
10 
1 2 10 5 4 3 7 6 8 9 
10 
1 2 10 5 4 3 9 8 7 6 
5 
3 5 2 4 1 
5 
1 2 4 3 5 
4 
4 2 3 1 
0
stdout
yes
no
yes
yes
yes
yes
no
yes
no