鱼C论坛

 找回密码
 立即注册
查看: 186|回复: 1

修改代码实现以下要求

[复制链接]
发表于 2024-2-5 16:01:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. const int N = 2e5 + 5;
  5. int q, x, y, p, mark[N];
  6. list<int> l;
  7. list<int>::iterator it[N], tempit;

  8. void solve()
  9. {
  10.     cin >> q;
  11.     while (q--)
  12.     {
  13.         cin >> p;
  14.         if (p == 1)
  15.         {
  16.             cin >> x >> y;
  17.             if (y != 0)
  18.             {
  19.                 if (l.empty())
  20.                 {
  21.                     l.push_front(y);
  22.                     it[y] = l.begin();
  23.                 }
  24.                 if (it[y] == l.end())
  25.                 {
  26.                     l.push_back(x);
  27.                     it[x] = --l.end();
  28.                 }
  29.                 else
  30.                 {
  31.                     tempit = it[y];
  32.                     tempit++;
  33.                     it[x] = l.insert(tempit, x);
  34.                 }
  35.             }
  36.             else
  37.             {
  38.                 l.push_front(x);
  39.                 it[x] = l.begin();
  40.             }
  41.         }
  42.         else
  43.         {
  44.             cin >> x;
  45.             if (mark[x] == 1)
  46.                 continue;
  47.             mark[x] = 1;
  48.             l.erase(it[x]);
  49.         }
  50.     }
  51.     cout << l.size() << endl;
  52.     for (tempit = l.begin(); tempit != l.end(); tempit++)
  53.     {
  54.         cout << *tempit << " ";
  55.     }
  56. }

  57. signed main()
  58. {
  59.     int t;
  60.     t = 1;
  61.     while (t--)
  62.     {
  63.         solve();
  64.     }
  65. }
复制代码

题目描述 $x$  
小红拿到了一个数组,初始数组为空,她希望你实现以下两种操作:  
1. 翰入 $1 x y$ ,将 $x$ 插入在元素 $y$ 的右边。保证此时数组中没有元素等于 $x$ ,且数组中存在一个 $y$ 。特殊的,如果将 $x$ 插入在数组的最左边,则 $y=0$  
2. 输入 $2 x$ ,将元素 $x$ 删除。  
  
请你在所有操作后输出整个数组。
https://ac.nowcoder.com/acm/contest/74362/D
程序发生了段错误,应该怎么修改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-2-5 16:02:12 | 显示全部楼层
#include <iostream>
#include <list>

using namespace std;

#define ll long long

list<ll> l;
list<ll>::iterator it, tempit;

void solve()
{
    ll q, x, y, p;
    cin >> q;
    while (q--)
    {
        cin >> p;
        if (p == 1)
        {
            cin >> x >> y;
            if (y != 0)
            {
                if (l.empty())
                {
                    l.push_front(y);
                    it = l.begin();
                }
                if (it == l.end())
                {
                    l.push_back(x);
                    it = --l.end();
                }
                else
                {
                    tempit = it;
                    tempit++;
                    it = l.insert(tempit, x);
                }
            }
            else
            {
                l.push_front(x);
                it = l.begin();
            }
        }
        else
        {
            cin >> x;
            if (it != l.end())
            {
                if (*it == x)
                {
                    l.erase(it);
                    it = l.end();
                }
            }
        }
    }

    for (auto num : l)
    {
        cout << num << " ";
    }
    cout << endl;
}

int main()
{
    solve();

    return 0;
}

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表