[sicily] 1021. Couples

淩亂°似流年 2022-07-24 04:16 272阅读 0赞
  1. 1021. Couples
  2. Constraints
  3. Time Limit: 1 secs, Memory Limit: 32 MB
  4. Description
  5. N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand together. We remove the couples who stand together until the circle is empty or we can't remove a couple any more.
  6. Can we remove all the couples out of the circle?
  7. Input
  8. There may be several test cases in the input file. In each case, the first line is an integer N(1 <= N <= 100000)----the number of couples. In the following N lines, each line contains two integers ---- the numbers of each couple.
  9. N = 0 indicates the end of the input.
  10. Output
  11. Output "Yes" if we can remove all the couples out of the circle. Otherwise, output "No".
  12. Sample Input
  13. 4
  14. 1 4
  15. 2 3
  16. 5 6
  17. 7 8
  18. 2
  19. 1 3
  20. 2 4
  21. 0
  22. Sample Output
  23. Yes
  24. No
  25. #include<iostream>
  26. #include<stack>
  27. using namespace std;
  28. int main()
  29. {
  30. int n;
  31. cin >> n;
  32. while(n != 0)
  33. {
  34. int a,b;
  35. int couple[2*n+1];
  36. for (int i = 1; i <= n; ++i)
  37. {
  38. cin >> a >> b;
  39. couple[a] = couple[b] = i;
  40. }
  41. stack<int> s;
  42. for (int i = 1; i <= 2*n; ++i)
  43. {
  44. if(!s.empty() && s.top() == couple[i])
  45. s.pop();
  46. else
  47. s.push(couple[i]);
  48. }
  49. if(s.empty())
  50. cout << "Yes" << endl;
  51. else
  52. cout << "No" << endl;
  53. cin >> n;
  54. }
  55. return 0;
  56. }

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读

    相关 sicily 1029

    思路:首先创建两个函数operate() 和cycle(),其中operate()求得两个大数之和,cycle()将数组向后平移一位。首先初始化数组num,数组num存储兔子的

    相关 [sicily] 1001. Alphacode

    ![这里写图片描述][20160511210803993] 题目大意: 假设有一规则:’A’ 设为1,’B’设为2,以此类推, ‘Z’设为26。按照这个规则给一串英文字母