百度之星2019 初赛一 题解
1001 Polynomial:若第一个多项式的次数大于第二个,就是1/0,若小于就是0/1,若等于就是第一个多项式最高次项系数/第二个多项式最高次项系数。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 5 typedef long long ll; 6 using namespace std; 7 8 const int N=10010; 9 int T,n,f[N],g[N]; 10 11 int main(){ 12 freopen("a.in","r",stdin); 13 freopen("a.out","w",stdout); 14 for (scanf("%d",&T); T--; ){ 15 scanf("%d",&n); int s1=n-1,s2=n-1; 16 rep(i,0,n) f[i]=g[i]=0; 17 rep(i,0,n-1) scanf("%d",&f[i]); 18 rep(i,0,n-1) scanf("%d",&g[i]); 19 while (!f[s1]) s1--; 20 while (!g[s2]) s2--; 21 if (s1>s2){ puts("1/0"); continue; } 22 if (s1<s2){ puts("0/1"); continue; } 23 int d=__gcd(f[s1],g[s2]); printf("%d/%d\n",f[s1]/d,g[s2]/d); 24 } 25 return 0; 26 }
1002 Game:做法很多,下面是我的做法。
首先起点一定在某个区间端点上,枚举起点。然后每次需要做的就是,计算从目前位置到下一个区间最少需要多少步。这个直接就是当前位置到下一个区间的较近端点的距离/2上取整,唯一需要考虑的是最后一步是走一格还是两格。这时找到这两个位置分别最多到之后哪个区间为止都不需要移动,若不同则选择更靠后的那个。若相同则计算到那个区间的距离,取较小的那个。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 5 typedef long long ll; 6 using namespace std; 7 8 const int N=100010,inf=1e9; 9 int T,n,a[N],l[N],r[N],L[N],R[N],L1[N],R1[N],L2[N],R2[N]; 10 11 int main(){ 12 freopen("b.in","r",stdin); 13 freopen("b.out","w",stdout); 14 for (scanf("%d",&T); T--; ){ 15 scanf("%d",&n); int tot=0,ans=inf; 16 rep(i,1,n) scanf("%d%d",&l[i],&r[i]),a[++tot]=l[i],a[++tot]=r[i],L1[i]=R1[i]=L2[i]=R2[i]=n+1; 17 rep(i,1,n-1){ 18 rep(j,i+1,n){ 19 if (l[j]>l[i]){ L[i]=1; L1[i]=j; break; } 20 if (r[j]<l[i]){ L[i]=0; L1[i]=j; break; } 21 } 22 rep(j,i+1,n){ 23 if (r[j]<r[i]){ R[i]=1; R1[i]=j; break; } 24 if (l[j]>r[i]){ R[i]=0; R1[i]=j; break; } 25 } 26 rep(j,i+1,n) if (l[j]>l[i]+1 || r[j]<l[i]+1){ L2[i]=j; break; } 27 rep(j,i+1,n) if (l[j]>r[i]-1 || r[j]<r[i]-1){ R2[i]=j; break; } 28 } 29 rep(i,1,tot){ 30 int x=a[i],res=0; 31 rep(j,1,n){ 32 if (l[j]<=x && r[j]>=x) continue; 33 if (l[j]>x){ 34 res+=(l[j]-x+1)/2; 35 if ((l[j]-x)&1 && j<n && r[j]>l[j]){ if (L2[j]>=L1[j] && L[j]) x=l[j]+1; else x=l[j]; } else x=l[j]; 36 }else{ 37 res+=(x-r[j]+1)/2; 38 if ((x-r[j])&1 && j<n && r[j]>l[j]){ if (R2[j]>=R1[j] && R[j]) x=r[j]-1; else x=r[j]; } else x=r[j]; 39 } 40 } 41 ans=min(ans,res); 42 } 43 printf("%d\n",ans); 44 } 45 return 0; 46 }
1003 Mindis:离散化连边后跑最短路。
1 #include<queue> 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #define rep(i,l,r) for (int i=(l); i<=(r); i++) 7 using namespace std; 8 9 const int N=200010,M=410; 10 double d[N]; 11 bool b[N]; 12 int T,n,xs,ys,xe,ye,xc,yc,xa[N],xb[N],ya[N],yb[N],xx[N],yy[N],ax[M][M],ay[M][M]; 13 struct P{ int x; double d; }; 14 bool operator <(const P &a,const P &b){ return a.d>b.d; } 15 vector<P>a[N]; 16 priority_queue<P>Q; 17 18 int main(){ 19 freopen("d.in","r",stdin); 20 freopen("d.out","w",stdout); 21 for (scanf("%d",&T); T--; ){ 22 scanf("%d",&n); xc=yc=0; 23 memset(ax,0,sizeof(ax)); memset(ay,0,sizeof(ay)); memset(b,0,sizeof(b)); 24 rep(i,1,n){ 25 scanf("%d%d%d%d",&xa[i],&ya[i],&xb[i],&yb[i]); 26 xx[++xc]=xa[i]; xx[++xc]=xb[i]; yy[++yc]=ya[i]; yy[++yc]=yb[i]; 27 } 28 scanf("%d%d%d%d",&xs,&ys,&xe,&ye); 29 xx[++xc]=xs; xx[++xc]=xe; yy[++yc]=ys; yy[++yc]=ye; 30 sort(xx+1,xx+xc+1); sort(yy+1,yy+yc+1); 31 xc=unique(xx,xx+xc+1)-xx-1; yc=unique(yy,yy+yc+1)-yy-1; 32 rep(i,1,xc*yc) a[i].clear(); 33 rep(i,1,n){ 34 xa[i]=lower_bound(xx+1,xx+xc+1,xa[i])-xx; 35 xb[i]=lower_bound(xx+1,xx+xc+1,xb[i])-xx; 36 ya[i]=lower_bound(yy+1,yy+yc+1,ya[i])-yy; 37 yb[i]=lower_bound(yy+1,yy+yc+1,yb[i])-yy; 38 } 39 xs=lower_bound(xx+1,xx+xc+1,xs)-xx; xe=lower_bound(xx+1,xx+xc+1,xe)-xx; 40 ys=lower_bound(yy+1,yy+yc+1,ys)-yy; ye=lower_bound(yy+1,yy+yc+1,ye)-yy; 41 rep(i,1,n) rep(j,xa[i],xb[i]-1) rep(k,ya[i],yb[i]) ax[j][k]++; 42 rep(i,1,n) rep(j,xa[i],xb[i]) rep(k,ya[i],yb[i]-1) ay[j][k]++; 43 rep(i,1,xc) rep(j,1,yc-1){ 44 a[(i-1)*yc+j].push_back((P){(i-1)*yc+j+1,(yy[j+1]-yy[j])/(ay[i][j]+1.)}); 45 a[(i-1)*yc+j+1].push_back((P){(i-1)*yc+j,(yy[j+1]-yy[j])/(ay[i][j]+1.)}); 46 } 47 rep(i,1,xc-1) rep(j,1,yc){ 48 a[(i-1)*yc+j].push_back((P){i*yc+j,(xx[i+1]-xx[i])/(ax[i][j]+1.)}); 49 a[i*yc+j].push_back((P){(i-1)*yc+j,(xx[i+1]-xx[i])/(ax[i][j]+1.)}); 50 } 51 rep(i,1,xc*yc) d[i]=1e30; 52 d[(xs-1)*yc+ys]=0; Q.push((P){(xs-1)*yc+ys,d[(xs-1)*yc+ys]}); 53 while (Q.size()){ 54 int x=Q.top().x; Q.pop(); 55 if (b[x]) continue; b[x]=1; 56 for (int i=0; i<(int)a[x].size(); i++){ 57 P e=a[x][i]; 58 if (d[e.x]>d[x]+e.d) d[e.x]=d[x]+e.d,Q.push((P){e.x,d[e.x]}); 59 } 60 } 61 printf("%.5lf\n",d[(xe-1)*yc+ye]); 62 } 63 return 0; 64 }
1005 Seq:打表找结论,具体见代码。
#include<cstdio> #include<cstring> #include<algorithm> #define rep(i,l,r) for (int i=(l); i<=(r); i++) typedef long long ll; using namespace std; int T; ll n; int main(){ freopen("c.in","r",stdin); freopen("c.out","w",stdout); for (scanf("%d",&T); T--; ){ scanf("%lld",&n); if (n%6==0 || n%6==2) printf("%lld\n",n/2); if (n%6==3 || n%6==5) printf("%lld\n",n/6); if (n%6==4) printf("%lld\n",n-1); if (n%6==1) printf("%lld\n",n/2+n/6+1); } return 0; }