【纪中受难记】——C3D6:大小不分
四道b组水题,由于空间开错而痛失成绩
1244. 修建道路 (Standard IO)
Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
Goto ProblemSet
没啥好讲的,最小生成树板子。注意取根号要先long long
1 #include<bits/stdc++.h> 2 using namespace std; 3 int read(){ 4 int x=0,f=1; 5 char c=getchar(); 6 while(!isdigit(c)){ 7 if(c=='-') f=-1; 8 c=getchar(); 9 } 10 while(isdigit(c)){ 11 x=(x<<1)+(x<<3)+(c^48); 12 c=getchar(); 13 } 14 return x*f; 15 } 16 int n,m; 17 const int N=1010; 18 struct node{ 19 int x,y; 20 double dis; 21 bool operator < (const node &x) const{ 22 return dis<x.dis; 23 } 24 }h[N*N+N]; 25 long long X[N],Y[N]; 26 double getdis(int x,int y){ 27 long long tmp=(X[x]-X[y])*(X[x]-X[y])+(Y[x]-Y[y])*(Y[x]-Y[y]); 28 return sqrt(tmp); 29 } 30 int f[N]; 31 int find(int x){ 32 return x==f[x]?x:f[x]=find(f[x]); 33 } 34 int vis[N][N]; 35 int main(){ 36 n=read(); 37 m=read(); 38 for(int i=1;i<=n;i++){ 39 f[i]=i; 40 X[i]=read(); 41 Y[i]=read(); 42 } 43 for(int i=1;i<=m;i++){ 44 h[i].x=read(); 45 h[i].y=read(); 46 h[i].dis=0; 47 vis[h[i].x][h[i].y]=1; 48 } 49 int cnt=m; 50 for(int i=1;i<=n;i++){ 51 for(int j=1;j<i;j++){ 52 if(!vis[i][j]){ 53 h[++cnt].x=i;h[cnt].y=j; 54 h[cnt].dis=getdis(i,j); 55 vis[i][j]=1; 56 } 57 } 58 } 59 sort(h+1,h+cnt+1); 60 double ans=0; 61 for(int i=1;i<=cnt;i++){ 62 int fax=find(h[i].x),fay=find(h[i].y); 63 if(fax!=fay){ 64 ans+=h[i].dis; 65 f[fax]=fay; 66 } 67 } 68 69 printf("%.2lf",ans); 70 return 0; 71 }
1245. 穿越泥地 (Standard IO)
Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
Goto ProblemSet打了个双向宽搜,结果把无用状态也扔了进去,爆空间了……
正解爆搜吧。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int read(){ 4 int x=0,f=1; 5 char c=getchar(); 6 while(!isdigit(c)){ 7 if(c=='-') f=-1; 8 c=getchar(); 9 } 10 while(isdigit(c)){ 11 x=(x<<1)+(x<<3)+(c^48); 12 c=getchar(); 13 } 14 return x*f; 15 } 16 int px[4]={1,0,-1,0}; 17 int py[4]={0,1,0,-1}; 18 const int N=1010; 19 int X,Y,n; 20 int ma[N][N]; 21 int dis[N][N]; 22 int vis[N][N]; 23 struct node{ 24 int x,y; 25 int col; 26 }; 27 queue<node> pos; 28 int main(){ 29 X=read();Y=read(); 30 X+=500; 31 Y+=500; 32 n=read(); 33 for(int i=1;i<=n;i++){ 34 int x=read();int y=read(); 35 ma[x+500][y+500]=1; 36 } 37 node st=(node){500,500,3}; 38 node ed=(node){X,Y,2}; 39 pos.push(st); 40 pos.push(ed); 41 vis[X][Y]=2; 42 while(!pos.empty()){ 43 node tmp=pos.front(); 44 pos.pop(); 45 int tx=tmp.x,ty=tmp.y; 46 vis[tx][ty]=tmp.col; 47 for(int i=0;i<4;i++){ 48 int dx=tx+px[i],dy=ty+py[i]; 49 if(ma[dx][dy]==1) continue; 50 if(vis[dx][dy]){ 51 if(vis[dx][dy]!=tmp.col){ 52 printf("%d",dis[dx][dy]+dis[tx][ty]+1); 53 return 0; 54 } 55 continue; 56 } 57 else{ 58 vis[dx][dy]=tmp.col; 59 dis[dx][dy]=dis[tx][ty]+1; 60 } 61 pos.push((node){dx,dy,tmp.col}); 62 } 63 } 64 return 0; 65 }
1246. 挑剔的美食家 (Standard IO)
Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
Goto ProblemSet
我的做法是拿个multiset维护牛的鲜美度,先按照价格将牛草排序,遍历草,将价格小于草的牛放进multiset,然后二分找最大的比草小的鲜美度弹出,这个牛和草就匹配了。
1 #include<bits/stdc++.h> 2 #include<set> 3 using namespace std; 4 int read(){ 5 int x=0,f=1; 6 char c=getchar(); 7 while(!isdigit(c)){ 8 if(c=='-') f=-1; 9 c=getchar(); 10 } 11 while(isdigit(c)){ 12 x=(x<<1)+(x<<3)+(c^48); 13 c=getchar(); 14 } 15 return x*f; 16 } 17 const int N=1e5+10; 18 int n,m,tot; 19 struct node{ 20 int mon,val; 21 bool operator <(const node &x)const{ 22 return mon<x.mon; 23 } 24 }a[N],b[N]; 25 multiset<int> q; 26 multiset<int> ::iterator it; 27 long long ans; 28 int main(){ 29 n=read();m=read(); 30 for(int i=1;i<=n;i++){ 31 a[i].mon=read(); 32 a[i].val=read(); 33 } 34 sort(a+1,a+n+1); 35 for(int i=1;i<=m;i++){ 36 b[i].mon=read(); 37 b[i].val=read(); 38 } 39 sort(b+1,b+m+1); 40 int j=1; 41 for(int i=1;i<=m;i++){ 42 while(j<=n&&a[j].mon<=b[i].mon){ 43 q.insert(a[j].val); 44 j++; 45 } 46 if(q.empty()) continue; 47 it=q.upper_bound(b[i].val); 48 if(it==q.begin()) continue; 49 it--; 50 q.erase(it); 51 ans+=b[i].mon; 52 tot++; 53 } 54 if(tot<n) printf("-1"); 55 else printf("%lld",ans); 56 return 0; 57 }
1247. 队列变换 (Standard IO)
Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
Goto ProblemSet贪心。
如果两边字母一样,就往里面找,直到两个字母不一样,先输出字典序小的那一边。
但这样会T。
优化一下,考虑这样一个串:BBDCBB,因为外面4个b都是一样的,所以可以判断一下,上面的那个操作做完以后,看下一个字母是不是和上面那个一样,如果是就一起输出,这样就是O(n)的了。
——抓住了时间,却不会利用的人,终究也逃不过失败的命运。