fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll; typedef pair< int, int > pair_t;
  6.  
  7. const int N = 50000; ll elapsed_time[ N + 1 ];
  8.  
  9. struct pending_tasks_t: vector< int >
  10. {
  11. int order( int i )
  12. {
  13. return lower_bound( begin(), end(), i ) - begin();
  14. }
  15.  
  16. void remove_task( int i )
  17. {
  18. erase( lower_bound( begin(), end(), i ) );
  19. }
  20. };
  21.  
  22. int main()
  23. {
  24. int n; cin >> n; vector< pair_t > task; pending_tasks_t p; ll time = n;
  25.  
  26. for( int i = 1; i <= n; i++ )
  27. {
  28. int t; cin >> t;
  29.  
  30. if ( t == 1 )
  31. elapsed_time[ i ] = i;
  32. else
  33. task.emplace_back( t, i ), p.push_back( i );
  34. }
  35.  
  36. sort( task.begin(), task.end() );
  37.  
  38. for( int i = 0, it = 2, k = 0, m = task.size(), remaining = m, t; i < m; i += k, it = t + 1, k = 0 )
  39. {
  40. time += ( ( t = task[ i ].first ) - it ) * ll( remaining ) + 1;
  41.  
  42. for( int j = i, q; j < m and task[ j ].first == t; j++, k++ )
  43. q = task[ j ].second, elapsed_time[ q ] = time + p.order( q );
  44.  
  45. for( int j = 0; j < k; j++ )
  46. p.remove_task( task[ i + j ].second );
  47.  
  48. time += remaining - 1, remaining -= k;
  49. }
  50.  
  51. for( int i = 1; i <= n;i++ )
  52. cout << elapsed_time[ i ] << '\n';
  53. }
Success #stdin #stdout 0s 15624KB
stdin
10
9 
7 
4 
1 
10 
9 
5 
3 
7 
5
stdout
57
50
31
4
60
59
41
26
53
43