这题被set坑了,问了问戴牛原来multiset的count 复杂度很大,是logn+o(元素个数),所以我悲剧的一直TLE啊(除夕夜就这么悲剧....)
其实是用map
#include<stdio.h> #include<utility> #include<map> #include<string.h> using namespace std; int x[100005],y[100005]; int visit[100005]; int h[5]={1,-1,0,0,0}; int g[5]={0,0,1,-1,0}; int main(){ int k,n,m,i,j,u,v; int ca=0; while(scanf("%d %d %d",&k,&n,&m) && !(k==0 && n==0 && m==0)){ memset(visit,0,sizeof(visit)); map<pair<int,int>,int>st; for(i=1;i<=k;i++){ scanf("%d %d",&x[i],&y[i]); map<pair<int,int>, int>::iterator it; it=st.find(make_pair(x[i],y[i])); if(it==st.end()) st.insert(make_pair(make_pair(x[i],y[i]),1)); else it->second++; } int sum=0; for(i=1;i<=k;i++){ int tem=0,temp; for(int j=0;j<=4;j++){ int p=x[i]+h[j]; int q=y[i]+g[j]; if(p>m || p<1 || q>n ||q<1) continue; map<pair<int,int>, int>::iterator it; it=st.find(make_pair(p,q)); if(it==st.end()) continue; if(j!=4) tem+=it->second; else tem+=it->second-1; if(tem>=2) break; } if(tem<2) sum++; } printf("Case %d: %d\n",++ca,sum); } return 0; }
顺便也把我TLE的set贴一下
#include<stdio.h> #include<utility> #include<set> using namespace std; int x[100005],y[100005]; int h[5]={1,-1,0,0,0}; int g[5]={0,0,1,-1,0}; int main(){ int k,n,m,i,j,u,v; int ca=0; while(scanf("%d %d %d",&k,&n,&m) && !(k==0 && n==0 && m==0)){ multiset<pair<int,int> >st; for(i=1;i<=k;i++){ scanf("%d %d",&x[i],&y[i]); st.insert(make_pair(x[i],y[i])); } int sum=0; for(i=1;i<=k;i++){ int tem=0; for(int j=0;j<=4;j++){ int p=x[i]+h[j]; int q=y[i]+g[j]; if(p>m || p<1 || q>n ||q<1) continue; if(j!=4) tem+=st.count(make_pair(p,q)); else tem+=st.count(make_pair(p,q))-1; if(tem>=2) break; } if(tem<2) sum++; } printf("Case %d: %d\n",++ca,sum); } return 0; }