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

来源:互联网 发布:买房贷款利息怎么算法 编辑:程序博客网 时间:2024/04/19 13:53

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

题意:三种操作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 LOCALset<int> st;        //all indexqueue<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 LOCALfreopen("b:\\data.in.txt", "r", stdin);#endifint 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
原创粉丝点击