CodeForces 705C 我被set.erase(*it)套路了

深夜写题解,我只想说内心是崩溃的。

题意:三种操作1.某APP来一个消息 2.读完APPx的所有消息   3.读完前t个消息(是前t个,包括读过的)

想法:静下心读懂题意,想好思路再做算法设计要有明确的伪代码,才能保证万无一失

set.erase(*it); 删除*it的值之后,你会发现如果你用的是for(it = st.begin(); it != st.end(); it++)  ,你会发现删除一个之后会出现数据错乱!因为st.begin()的位置变了,注意~~~~!!!!!

http://codeforces.com/contest/705/problem/C

题解:

#include <bits/stdc++.h>
using namespace std;
typedef pair<string, string> P;
typedef long long LL;
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define MAX_N 300005
#define LOCAL
set<int> st;        //all index
queue<int> que[MAX_N];
int cnt = 0;


void solve(int type, int xt)
{
    if(type == 1)
    {
         cnt++;         //the index
         que[xt].push(cnt);
         st.insert(cnt);

    }
    else
        if(type == 2)
    {
        while(!que[xt].empty())
        {
            st.erase(que[xt].front());
            que[xt].pop();
        }
    }
    else
        if(type == 3)
    {
        set<int>::iterator it;
        int cntnow = 0;
        for(it = st.begin(); it!=st.end(); it++)            //你妈神坑~~~  记得你st.erase()后再st.begin()就变了
        {
            cout << *it << " " ;
            if(*it <= xt)
                st.erase(*it);
                else
                    break;
        }
       cout << endl;
    }
}
int main()
{
	#ifdef LOCAL
		freopen("b:\\data.in.txt", "r", stdin);
	#endif
	int n, q;
    scanf("%d%d", &n, &q);
    for(int i = 0; i < q; i++)
    {
        int type, xt ; scanf("%d%d", &type, &xt);
        solve(type, xt);
//        printf("%d\n", st.size());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值