• 2022-10洛谷


    P1160 队列安排 

    list不支持<=等的比较

    感觉自己写的思路还是不太行。。。。。。

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. list<int> lt;
    9. int vist[N];
    10. list<int>:: iterator pos[N];
    11. int main(){
    12. int n, m;
    13. cin >> n;
    14. lt.push_front(1);
    15. pos[1] = lt.begin();
    16. for(int i = 2; i <= n; i ++ ){
    17. int k, p;
    18. cin >> k >> p;
    19. if(!p){
    20. pos[i] = lt.insert(pos[k], i);
    21. }
    22. else{
    23. list<int>::iterator nex = pos[k];
    24. nex ++ ;
    25. pos[i] = lt.insert(nex, i);
    26. }
    27. }
    28. cin >> m;
    29. while(m -- ){
    30. int x;
    31. cin >> x;
    32. if(!vist[x]){
    33. vist[x] = 1;
    34. lt.erase(pos[x]);
    35. }
    36. }
    37. for(list<int>::iterator it = lt.begin(); it != lt.end(); it ++ ){
    38. cout << *it << " ";
    39. }
    40. return 0;
    41. }

    P1449 后缀表达式

    woc二刷忘记了栈的进出顺序,减法和除法出现问题了呜呜呜呜

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. string s;
    9. stack<int> st;
    10. int a, b, t;
    11. int main(){
    12. cin >> s;
    13. for(int i = 0; s[i] != '@'; i ++ ){
    14. switch(s[i]){
    15. case '+': {
    16. a = st.top();
    17. st.pop();
    18. b = st.top();
    19. st.pop();
    20. st.push(a + b);
    21. break;
    22. }
    23. case '-': {
    24. a = st.top();
    25. st.pop();
    26. b = st.top();
    27. st.pop();
    28. st.push(b - a);
    29. break;
    30. }
    31. case '*': {
    32. a = st.top();
    33. st.pop();
    34. b = st.top();
    35. st.pop();
    36. st.push(a * b);
    37. break;
    38. }
    39. case '/': {
    40. a = st.top();
    41. st.pop();
    42. b = st.top();
    43. st.pop();
    44. st.push(b / a);
    45. break;
    46. }
    47. case '.': {
    48. st.push(t);
    49. t = 0;
    50. break;
    51. }
    52. default: {
    53. t = t * 10 + s[i] - '0';
    54. break;
    55. }
    56. }
    57. }
    58. cout << st.top();
    59. return 0;
    60. }
    61. //3.5.2.-*7.+@

    P1981 [NOIP2013 普及组] 表达式求值 

    运算符优先级优先级!!!

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int m = 10000;
    8. string s;
    9. stack<int> st;
    10. stack<char> ope;
    11. int t, a, b, ans;
    12. int main(){
    13. cin >> s;
    14. for(int i = 0; i < s.size(); i ++ ){
    15. if(s[i] >= '0' && s[i] <= '9'){
    16. if(s[i + 1] >= '0' && s[i + 1] <= '9'){
    17. t = t * 10 + s[i] - '0';
    18. }
    19. else{
    20. t = (t * 10 + s[i] - '0') % m;
    21. st.push(t);
    22. t = 0;
    23. }
    24. }
    25. else{
    26. if(ope.empty()) ope.push(s[i]);
    27. else{
    28. if(s[i] < ope.top()) ope.push(s[i]);
    29. else{
    30. int a = st.top();
    31. st.pop();
    32. int b = st.top();
    33. st.pop();
    34. if(ope.top() == '*') st.push((a * b) % m);
    35. else st.push((a + b) % m);
    36. ope.pop();
    37. ope.push(s[i]);
    38. }
    39. }
    40. }
    41. }
    42. while(!ope.empty()){
    43. int a = st.top();
    44. st.pop();
    45. int b = st.top();
    46. st.pop();
    47. char x = ope.top();
    48. ope.pop();
    49. if(x == '*') st.push((a * b) % m);
    50. else st.push((a + b) % m);
    51. }
    52. cout << st.top() << endl;
    53. return 0;
    54. }

    P1175 表达式的转换 

    好难,看了题解懂了思路,但还是不能自己打出来QAQ

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int m = 10000;
    8. stack<char> dat, op;
    9. stack<int> num, dat2;
    10. int rank(char c){
    11. switch(c){
    12. case '+': return 1;
    13. case '-': return 1;
    14. case '*': return 2;
    15. case '/': return 2;
    16. case '^': return 3;
    17. case '(': return 0;
    18. case ')': return 0;
    19. default: return -1;
    20. }
    21. }
    22. int calans(int a, int b, char t){
    23. switch(t){
    24. case '+': return a + b;
    25. case '-': return a - b;
    26. case '*': return a * b;
    27. case '/': return a / b;
    28. case '^': return pow(a, b);
    29. default: return -INF;
    30. }
    31. }
    32. void change(string s){
    33. int len = s.size();
    34. for(int i = 0; i < len; i ++ ){
    35. if(isdigit(s[i])) dat.push(s[i]);
    36. else if(s[i] == '(') op.push(s[i]);
    37. else if(s[i] == ')'){
    38. char t = op.top();
    39. while(t != '('){
    40. op.pop();
    41. dat.push(t);
    42. t = op.top();
    43. }
    44. op.pop();
    45. }
    46. else if(rank(s[i]) >= 1 && rank(s[i]) <= 3){
    47. if(!op.empty()){
    48. char t = op.top();
    49. while(!op.empty() && rank(s[i]) <= rank(t)){
    50. if(rank(s[i]) == rank(t) && s[i] == '^') break;//在s[i]与栈顶都是^号时也能进栈
    51. op.pop();
    52. dat.push(t);
    53. if(!op.empty()) t = op.top();
    54. }
    55. }
    56. op.push(s[i]);
    57. }
    58. }
    59. while(!op.empty()){
    60. char t = op.top();
    61. op.pop();
    62. dat.push(t);
    63. }
    64. while(!dat.empty()){
    65. char t = dat.top();
    66. dat.pop();
    67. op.push(t);
    68. }
    69. while(!op.empty()){
    70. char t = op.top();
    71. cout << t << " ";
    72. op.pop();
    73. dat.push(t);
    74. }
    75. puts("");
    76. }
    77. void calculate(){
    78. while(!dat.empty()){
    79. char t= dat.top();
    80. dat.pop();
    81. op.push(t);
    82. }
    83. while(!op.empty()){
    84. char t = op.top();
    85. op.pop();
    86. if(isdigit(t)) num.push(t - '0');
    87. else{
    88. int a = num.top();
    89. num.pop();
    90. int b = num.top();
    91. num.pop();
    92. num.push(calans(b, a, t));
    93. while(!num.empty()){
    94. int t = num.top();
    95. num.pop();
    96. dat2.push(t);
    97. }
    98. while(!dat2.empty()){
    99. int t = dat2.top();
    100. cout << t << " ";
    101. dat2.pop();
    102. num.push(t);
    103. }
    104. while(!op.empty()){
    105. char t= op.top();
    106. cout << t << " ";
    107. op.pop();
    108. dat.push(t);
    109. }
    110. while(!dat.empty()){
    111. char t = dat.top();
    112. dat.pop();
    113. op.push(t);
    114. }
    115. puts("");
    116. }
    117. }
    118. }
    119. int main(){
    120. string s;
    121. cin >> s;
    122. change(s);
    123. calculate();
    124. return 0;
    125. }

    P1111 修复公路

    kruskal 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. int p[N];
    9. int n, m;
    10. struct edge{
    11. int x, y, t;
    12. bool operator < (const edge &W) const{
    13. return t < W.t;
    14. }
    15. }edge[2 * N];
    16. int find(int x){
    17. if(p[x] != x) p[x] = find(p[x]);
    18. return p[x];
    19. }
    20. int kruskal(){
    21. sort(edge, edge + m);
    22. for(int i = 0; i < m; i ++ ){
    23. int a = find(edge[i].x);
    24. int b = find(edge[i].y);
    25. if(a != b){
    26. p[a] = b;
    27. n -- ;
    28. }
    29. if(n == 1) return edge[i].t;
    30. }
    31. return -1;
    32. }
    33. int main(){
    34. cin >> n >> m;
    35. for(int i = 0; i < n; i ++ ) p[i] = i;
    36. for(int i = 0; i < m; i ++ ){
    37. cin >> edge[i].x >> edge[i].y >> edge[i].t;
    38. }
    39. int t = kruskal();
    40. cout << t << endl;
    41. return 0;
    42. }

    P3958 [NOIP2017 提高组] 奶酪

    谢,一开始直接暴力模拟只过了一个,后面看题解怎么用并查集只有80,对着dalao代码写才ac的,真的会谢 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. int t;
    9. LL p[N];
    10. struct cake{
    11. LL x, y, z;
    12. bool operator < (const cake &W) const {
    13. return z < W.z;
    14. }
    15. }cake[N];
    16. LL find(LL x){
    17. if(p[x] != x) p[x] = find(p[x]);
    18. return p[x];
    19. }
    20. int main(){
    21. cin >> t;
    22. while(t -- ){
    23. LL n, h, r;
    24. cin >> n >> h >> r;
    25. for(int i = 0; i <= n + 1; i ++ ) p[i] = i;
    26. for(int i = 0; i < n; i ++ ){
    27. cin >> cake[i].x >> cake[i].y >> cake[i].z;
    28. }
    29. for(int i = 0; i < n; i ++ ){
    30. for(int j = i + 1; j < n; j ++ ){
    31. if(pow(cake[i].x - cake[j].x, 2) + pow(cake[i].y - cake[j].y, 2) + pow(cake[i].z - cake[j].z, 2) <= 4 * r * r){
    32. LL a = find(i);
    33. LL b = find(j);
    34. if(a != b) p[a] = b;
    35. }
    36. }
    37. if(cake[i].z - r <= 0){
    38. LL a = find(i);
    39. LL b = find(n);
    40. if(a != b) p[a] = b;
    41. }
    42. if(cake[i].z + r >= h){
    43. LL a = find(i);
    44. LL b = find(n + 1);
    45. if(a != b) p[a] = b;
    46. }
    47. }
    48. if(find(n) != find(n + 1)) puts("No");
    49. else puts("Yes");
    50. }
    51. return 0;
    52. }

    提醒自己一下这个(尬


    P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G 

    这道题不难,但一开始没有想到循环条件。。。。。(呃。。好吧应该就是不会。。。。

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. //#define x first
    5. //#define y second
    6. #define mod 7
    7. typedef long long LL;
    8. const int N = 5e3 + 10;
    9. int n, ans;
    10. priority_queue<int, vector<int>, greater<int> > q;
    11. int main(){
    12. cin >> n;
    13. for(int i = 1; i <= n; i ++ ){
    14. int x;
    15. cin >> x;
    16. q.push(x);
    17. }
    18. while(q.size() >= 2){
    19. int a = q.top();
    20. q.pop();
    21. int b = q.top();
    22. q.pop();
    23. q.push(a + b);
    24. ans += a + b;
    25. }
    26. cout << ans;
    27. return 0;
    28. }

    P1087 [NOIP2004 普及组] FBI 树 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. //#define x first
    5. //#define y second
    6. #define mod 7
    7. #define endl '\n'
    8. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
    9. typedef long long LL;
    10. const int N = 15;
    11. int n;
    12. string s;
    13. char FBI(string s){
    14. if(s.length() > 1){
    15. cout << FBI(s.substr(0, s.length() / 2));
    16. cout << FBI(s.substr(s.length() / 2, s.length() / 2));
    17. }
    18. if(s == string(s.length(), '0')) return 'B';
    19. if(s == string(s.length(), '1')) return 'I';
    20. return 'F';
    21. }
    22. int main(){
    23. cin >> n >> s;
    24. cout << FBI(s);
    25. return 0;
    26. }

    P1030 [NOIP2001 普及组] 求先序排列

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. //#define x first
    5. //#define y second
    6. #define mod 7
    7. #define endl '\n'
    8. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
    9. typedef long long LL;
    10. const int N = 15;
    11. string inorder, postorder;
    12. void preorder(string inorder, string postorder){
    13. if(inorder.size()){
    14. char ch = postorder[postorder.size() - 1];
    15. int k = inorder.find(ch);
    16. cout << ch;
    17. preorder(inorder.substr(0, k), postorder.substr(0, k));
    18. preorder(inorder.substr(k + 1), postorder.substr(k, postorder.size() - 1 - k));
    19. }
    20. }
    21. int main(){
    22. cin >> inorder >> postorder;
    23. preorder(inorder, postorder);
    24. return 0;
    25. }

    P1305 新二叉树 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. //#define x first
    5. //#define y second
    6. #define mod 7
    7. #define endl '\n'
    8. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
    9. typedef long long LL;
    10. const int N = 15;
    11. int n;
    12. string s;
    13. int main(){
    14. cin >> n >> s;
    15. for(int i = 0; i < n - 1; i ++ ){
    16. string t;
    17. cin >> t;
    18. int k = s.find(t[0]);
    19. t = t.substr(1);
    20. s.insert(k + 1, t);
    21. }
    22. for(int i = 0; i < 2 * n + 1; i ++ ){
    23. if(s[i] != '*') cout << s[i];
    24. }
    25. return 0;
    26. }

     P1229 遍历问题

    这道题感觉有点考思维和基础概念,一开始我虽然知道必须知道中序排列以及另外一种排列才能求出第三种排列,但具体很细的地方没有细想,所以写这道题把我干蒙了。

    首先明确一个重点,就是只有单一个根只有一个子节点的时候,会出现不同的中序排列。例如如果一棵树的一个根只有左子树,那么前序中根节点右边第一个是左节点,后序中根节点左边第一个是左节点,如果只有右子树,那么前序中根节点的右边第一个是右节点,后序中根节点左边第一个是右节点。

    然而在一般情况下(即有两个子节点的情况下),前序中根节点右边第一个是左节点,后序中左边第一个是右节点。

    所以,当出现前序和后序中相邻两个相反相同(即BA和AB),就说明了出现了单节点,这个时候中序排列就会因为这个而出现了两种情况,总数*2;

    因此在以上想清楚以后,就不停一个个比较有没有相同相邻的,统计次数。

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. //#define x first
    5. //#define y second
    6. #define mod 7
    7. #define endl '\n'
    8. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
    9. typedef long long LL;
    10. const int N = 15;
    11. string s1, s2;
    12. int cnt;
    13. int main(){
    14. cin >> s1 >> s2;
    15. for(int i = 0; i < s1.size(); i ++ ){
    16. for(int j = 0; j < s2.size(); j ++ ){
    17. if(s1[i] == s2[j + 1] && s1[i + 1] == s2[j]){
    18. cnt ++ ;
    19. }
    20. }
    21. }
    22. cout << (1 << cnt);
    23. return 0;
    24. }

  • 相关阅读:
    七大数据分析模型详解,做分析不再没思路
    接口测试步骤和场景分析,其实很简单~~
    vxe-table全选禁用并保留选中项
    mac for m1(arm):安装redis的四种方式(本机安装、homebrew安装、虚拟机安装、docker安装)
    wsl中安装虚拟环境virtualenv,pycharm中配置wsl解释器
    常用系统函数
    小米智能电视投屏方法
    流形上的预积分(上)
    力扣:22-括号生成
    你还不懂《顺序表》?那就不要错过这篇文章!!!
  • 原文地址:https://blog.csdn.net/weixin_63914060/article/details/127341507