A.
如果存在的话,一定是所有的数化为最初的最小值,如果做不到就不可以。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <stack> 12 #define mp make_pair 13 typedef long long ll; 14 typedef unsigned long long ull; 15 const int MAX=1e5+1000; 16 const int INF=1e9+5; 17 using namespace std; 18 typedef pair<int,int> pii; 19 int n,k; 20 int a[MAX]; 21 ll an; 22 int main() 23 { 24 scanf("%d%d",&n,&k); 25 for(int i=1;i<=n;i++) 26 scanf("%d",&a[i]); 27 sort(a+1,a+1+n); 28 an=0; 29 for(int i=2;i<=n;i++) 30 { 31 if((a[i]-a[1])%k) 32 { 33 printf("-1 ");return 0; 34 } 35 else 36 { 37 an+=(long long)((a[i]-a[1])/k); 38 } 39 } 40 cout<<an<<" "; 41 }
B.
最基础的BFS迷宫(类似于连连看那道题)
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <stack> 12 #define mp make_pair 13 typedef long long ll; 14 typedef unsigned long long ull; 15 const int MAX=1e3+5; 16 const int INF=1e9+5; 17 using namespace std; 18 typedef pair<int,int> pii; 19 int n,m; 20 char a[MAX][MAX]; 21 int ci[MAX][MAX]; 22 int dx[4]={0,0,1,-1}; 23 int dy[4]={1,-1,0,0}; 24 bool val(int x,int y) 25 { 26 if(x<=0||y<=0||x>n||y>m||a[x][y]=='*') 27 return false; 28 return true; 29 } 30 bool df(int sx,int sy,int dir,int cis) 31 { 32 if(cis>2||(ci[sx][sy]!=-1&&ci[sx][sy]<=cis)) 33 return false; 34 ci[sx][sy]=cis; 35 if(a[sx][sy]=='T') 36 return true; 37 int cc; 38 int tx,ty; 39 for(int i=0;i<4;i++) 40 { 41 tx=sx+dx[i];ty=sy+dy[i]; 42 if(i==dir) 43 cc=cis; 44 else 45 cc=cis+1; 46 if(cc>2) 47 continue; 48 if(val(tx,ty)) 49 { 50 if(ci[tx][ty]!=-1&&ci[tx][ty]<=cc) 51 continue; 52 if(df(tx,ty,i,cc)) 53 return true; 54 } 55 } 56 return false; 57 } 58 bool dfs(int sx,int sy) 59 { 60 ci[sx][sy]=0; 61 int tx,ty; 62 for(int i=0;i<4;i++) 63 { 64 65 tx=sx+dx[i];ty=sy+dy[i]; 66 if(val(tx,ty)) 67 { 68 memset(ci,-1,sizeof(ci));ci[sx][sy]=0; 69 // printf("~~ "); 70 if(df(tx,ty,i,0)) 71 return true; 72 } 73 } 74 return false; 75 } 76 int main() 77 { 78 scanf("%d%d",&n,&m); 79 // memset(ci,-1,sizeof(ci)); 80 for(int i=1;i<=n;i++) 81 scanf("%s",a[i]+1); 82 for(int i=1;i<=n;i++) 83 { 84 for(int j=1;j<=m;j++) 85 if(a[i][j]=='S') 86 { 87 if(dfs(i,j)) 88 printf("YES "); 89 else 90 printf("NO "); 91 } 92 } 93 }
C.
谁一眼看都知道怎么做,却就是很有可能被坑爹的精度卡。最反感这样的题,不说什么了。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <stack> 12 #define mp make_pair 13 typedef long long ll; 14 typedef unsigned long long ull; 15 const int MAX=1e5+5; 16 const int INF=1e9+5; 17 const double M=4e18; 18 using namespace std; 19 typedef pair<int,int> pii; 20 const double eps=0.000000001; 21 int n; 22 double tx1,tx2,ty1,ty2; 23 double zuo,you; 24 double time1,time2,time3,time4; 25 double nx,ny,vx,vy; 26 int main() 27 { 28 scanf("%d",&n); 29 zuo=0;you=M; 30 // cin>>tx1>>ty1>>tx2>>ty2; 31 scanf("%lf%lf%lf%lf",&tx1,&ty1,&tx2,&ty2); 32 while(n--) 33 { 34 // cin>>nx>>ny>>vx>>vy; 35 scanf("%lf%lf%lf%lf",&nx,&ny,&vx,&vy); 36 if(abs(vx)<=eps) 37 { 38 if(nx>tx2-eps||nx<tx1+eps) 39 { 40 printf("-1 ");return 0; 41 } 42 } 43 else 44 { 45 time1=(tx1-nx)/vx;time2=(tx2-nx)/vx; 46 if(time1>time2) 47 swap(time1,time2); 48 zuo=max(zuo,time1);you=min(you,time2); 49 } 50 if(abs(vy)<=eps) 51 { 52 if(ny>ty2-eps||ny<ty1+eps) 53 { 54 printf("-1 ");return 0; 55 } 56 } 57 else 58 { 59 time1=(ty1-ny)/vy;time2=(ty2-ny)/vy; 60 if(time1>time2) 61 swap(time1,time2); 62 you=min(you,time2);zuo=max(zuo,time1); 63 } 64 } 65 if(zuo>=you) 66 printf("-1 "); 67 else 68 printf("%.20f ",zuo); 69 return 0; 70 }
D.
(n+m)*n^2*k的做法写得好一点也可以勉强卡过,这里只写(n+m)*n*k的。
a[x][y][z]分别表示当下在的位置,所走的方向尽头的坐标(不可以到达),第几个(从大往小写,到1截至)
进行记忆化处理,例如[l,r]假设从l走到某个位置t后,可以走[l,t],或[t,r],并且选择了某个区间以后一定就都在这个区间里。依照此想法递归即可。
1 #include <string> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <list> 11 #include <stack> 12 #define mp make_pair 13 #define Min(a,b) a<b?a:b 14 typedef long long ll; 15 typedef unsigned long long ull; 16 const int MAX=82; 17 const int INF=1e9+5; 18 using namespace std; 19 typedef pair<int,int> pii; 20 int a[MAX][MAX][MAX]; 21 int dis[MAX][MAX]; 22 int n,k,m; 23 int u,v,c; 24 int dp(int x,int y,int knum,int st) 25 { 26 if(a[x][y][knum]!=-1) 27 return a[x][y][knum]; 28 if(knum==1) 29 return a[x][y][knum]=0; 30 int re=INF; 31 if(st) 32 { 33 for(int i=y+1;i<x;i++) 34 { 35 if(dis[x][i]!=INF) 36 { 37 re=Min(re,dis[x][i]+dp(i,x,knum-1,0)); 38 re=Min(re,dis[x][i]+dp(i,y,knum-1,1)); 39 } 40 } 41 } 42 else 43 { 44 for(int i=x+1;i<y;i++) 45 { 46 if(dis[x][i]!=INF) 47 { 48 re=Min(re,dis[x][i]+dp(i,x,knum-1,1)); 49 re=Min(re,dis[x][i]+dp(i,y,knum-1,0)); 50 } 51 } 52 } 53 return a[x][y][knum]=re; 54 } 55 int main() 56 { 57 scanf("%d%d",&n,&k); 58 scanf("%d",&m); 59 int an=INF; 60 for(int i=0;i<=n+1;i++) 61 for(int j=0;j<=n+1;j++) 62 dis[i][j]=INF; 63 memset(a,-1,sizeof(a)); 64 while(m--) 65 { 66 scanf("%d%d%d",&u,&v,&c); 67 dis[u][v]=Min(dis[u][v],c); 68 } 69 for(int i=1;i<=n;i++) 70 { 71 an=Min(an,dp(i,0,k,1)); 72 an=Min(an,dp(i,n+1,k,0)); 73 } 74 if(an==INF) 75 printf("-1 "); 76 else 77 printf("%d ",an); 78 }