fork(1) download
  1. /// UVA 10172 The Lonesome Cargo Distributor
  2. #include <bits/stdc++.h>
  3. #define X first
  4. #define Y second
  5. #define pb push_back
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair <int,int> ii;
  9. typedef pair <int,ii> i_ii;
  10. typedef vector <int> vi;
  11. typedef vector <ii> vii;
  12.  
  13. const int maxn=102;
  14. const ll MOD=1000000007;
  15.  
  16. int T,N,S,Q,Time,cur_station;
  17.  
  18. int main()
  19. {
  20. // freopen("nhap.inp", "r", stdin);
  21. cin >> T;
  22.  
  23. while(T--)
  24. {
  25. cin >> N >> S >> Q;
  26.  
  27. stack<int> st;
  28. queue<int> qu[102];
  29. cur_station = Time = 0;
  30.  
  31. for (int i = 0; i < N; i++)
  32. {
  33. int nq,x;
  34. cin >> nq;
  35. for (int j = 0; j < nq; j++)
  36. {
  37. cin >> x;
  38. qu[i].push(x - 1);
  39. }
  40. }
  41.  
  42. while (1)
  43. {
  44. while (!st.empty() && (st.top() == cur_station || qu[cur_station].size() < Q))
  45. {
  46. if (st.top() != cur_station)
  47. qu[cur_station].push(st.top());
  48. st.pop();
  49. Time++;
  50. }
  51.  
  52. while (st.size() < S && !qu[cur_station].empty())
  53. {
  54. st.push(qu[cur_station].front());
  55. qu[cur_station].pop();
  56. Time++;
  57. }
  58.  
  59. bool ok = st.empty();
  60. for (int i = 0; i < N; i++)
  61. ok &= qu[i].empty();
  62.  
  63. if (ok) break;
  64.  
  65. cur_station = (cur_station + 1) % N;
  66. Time += 2;
  67. }
  68.  
  69. cout << Time << endl;
  70. }
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
Standard output is empty