fork(6) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<string.h>
  5. #include<assert.h>
  6. #define REP(i,a,b) for(i=a;i<b;i++)
  7. #define rep(i,n) REP(i,0,n)
  8.  
  9. /* size denotes the number of books in the pile */
  10. /* Under the k-th book in the pile (inclusive), the up[k]-th book has minimum amount of unsolved problems */
  11. char name[1000000][16];
  12. int remain[1000000], up[1000000];
  13.  
  14. int main(){
  15. int N;
  16.  
  17. int i, j, k, len, size;
  18.  
  19. assert( scanf("%d",&N)==1 );
  20. assert( 1<=N && N<=1000000 );
  21.  
  22. size = 0;
  23. while(N--){
  24. assert( scanf("%d",&k)==1 );
  25. if(k==-1){
  26. assert( size > 0 );
  27. printf("%d %s\n",size-1 - up[size-1], name[up[size-1]]);
  28. size = up[size-1];
  29. } else {
  30. assert(scanf("%s",name[size])==1);
  31. len = strlen(name[size]);
  32. assert( 1<=len && len<=15 );
  33. rep(i,len) assert( 'a'<=name[size][i]&&name[size][i]<='z' );
  34. remain[size] = k;
  35.  
  36. if(k==0) continue;
  37.  
  38. if(remain[size] <= remain[up[size-1]]) up[size] = size;
  39. else up[size] = up[size-1];
  40. size++;
  41. }
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.02s 26168KB
stdin
6
1 a
2 b
0 c
1 d
-1
-1
stdout
0 d
1 a