社区讨论

为啥卡我 STL map ???

P2564[SCOI2009] 生日礼物参与者 5已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@mi6hdoio
此快照首次捕获于
2025/11/20 04:54
4 个月前
此快照最后确认于
2025/11/20 04:54
4 个月前
查看原帖
RT; 作为一名蒟蒻,十分不明白STL的复杂度到底有多大……
附两次代码(第一次用map ,TLE五个点;第二次改掉之后AC……)
···
CPP
#include<cstdio>
#include<algorithm>
#include<map>
#define N 1000002
#define Min(a,b) a<b?a:b
using namespace std;
int n,k,cnt=0,ans=1e9;
map<int,int> have;
struct point{
    int x,t;
}m[N];
inline void read(int& x){
    int sign =1;x = 0;char c;
    for(c=getchar();(c < '0' || c > '9') && c != '-';c = getchar());
    if(c == '-') sign = -1;else x = c - '0';
    for(c=getchar();c <= '9' && c >= '0';x = x*10 + c - '0',c = getchar());
    x = x*sign;
}
bool comp(point a,point b){
    return a.x<b.x;
}
int main() {
    read(n);read(k);
    for(int i=1;i<=k;i++){
        int j;
        read(j);
        for(;j;j--){
            int x;
            read(x);
            m[++cnt].t=i;
            m[cnt].x=x;
        }
    }
    sort(m+1,m+1+n,comp);
    int h=1,t=1;have[m[1].t]++;
    while(t<=n){
        while(have.size()<k && h+1 <=n){
            h++;
            have[m[h].t]++;
        }
        if(have.size() == k)
            ans= Min(ans,(m[h].x-m[t].x));
        while(m[t+1].x == m[t].x){
            have[m[t].t]--;
            if((have[m[t].t]) == 0){
                have.erase(m[t].t);
            }
            t++;
        }
            have[m[t].t]--;
            if((have[m[t].t]) == 0){
                have.erase(m[t].t);
            }
            t++;
    }
    printf("%d\n",ans);
    return 0;
}
··· ···
CPP
#include<cstdio>
#include<algorithm>
#define N 1000002
#define Min(a,b) a<b?a:b
using namespace std;
int n,k,cnt=0,ans=1e9;
int have[65];
struct point{
    int x,t;
}m[N];
inline void read(int& x){
    int sign =1;x = 0;char c;
    for(c=getchar();(c < '0' || c > '9') && c != '-';c = getchar());
    if(c == '-') sign = -1;else x = c - '0';
    for(c=getchar();c <= '9' && c >= '0';x = x*10 + c - '0',c = getchar());
    x = x*sign;
}
bool comp(point a,point b){
    return a.x<b.x;
}
int main() {
    read(n);read(k);
    for(int i=1;i<=k;i++){
        int j;
        read(j);
        for(;j;j--){
            int x;
            read(x);
            m[++cnt].t=i;
            m[cnt].x=x;
        }
    }
    sort(m+1,m+1+n,comp);
    int h=1,t=1,size=1;have[m[1].t]++;
    while(t<=n){
        while(size<k && h+1 <=n){
            h++;
            if(have[m[h].t]++ == 0)size++;
        }
        if(size == k)
            ans= Min(ans,(m[h].x-m[t].x));
        while(m[t+1].x == m[t].x){
            have[m[t].t]--;
            if((have[m[t].t]) == 0){
                size--;
            }
            t++;
        }
            have[m[t].t]--;
            if((have[m[t].t]) == 0){
                size--;
            }
            t++;
    }
    printf("%d\n",ans);
    return 0;
}
···

回复

5 条回复,欢迎继续交流。

正在加载回复...