1.CS1102:最长上升子序列 #include<stdio.h> #include<string.h> #include<string> #include<stack> #include<queue> #include<math.h> #include<malloc.h> #include<iostream> #include<algorithm> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF) { int a[1005]; for(int i=0; i<n; i++) scanf("%d",&a[i]); int b[1005]= {0}; b[0]=1; for(int i=1; i<n; i++) { int num=0; for(int j=0; j<i; j++) { if(a[i]>a[j]) { if(num<b[j]) { num=b[j]; } } } b[i]=num+1; } int maxn=0; for(int i=0;i<n;i++) { if(maxn<b[i]) maxn=b[i]; } printf("%d\n",maxn); } return 0;}
2.CS1103:Help Jimmy #include <iostream> #include <stdio.h> #include <cstring> #include <vector> #include <algorithm> using namespace std; struct point{ int x1,x2,y;} points[1010]; bool operator<(const point& a,const point& b){ return a.y>b.y;} int N,X,Y,MAX; int num[1010][2]; int dfs(int i,int a,int x){ if(num[i][a]!=-1) return num[i][a]; int left=100000000,right=100000000; bool flag=true; for(int j=i+1;j<=N;j++) { if(points[i].y-points[j].y>MAX) {flag=false;break;} if(x>=points[j].x1&&x<=points[j].x2){ left=dfs(j,0,points[j].x1)+points[i].y-points[j].y+x-points[j].x1; right=dfs(j,1,points[j].x2)+points[i].y-points[j].y+points[j].x2-x; flag=false; break; } } num[i][a]=min(left,right); if(flag&&points[i].y<=MAX) num[i][a]=points[i].y; return num[i][a]; }int main(){ int t; cin>>t; while(t--) { memset(num,-1,sizeof num); scanf("%d%d%d%d",&N,&X,&Y,&MAX); points[0].x1=X;points[0].x2=X;points[0].y=Y; for(int i=1;i<=N;i++) scanf("%d%d%d",&points[i].x1,&points[i].x2,&points[i].y); sort(points,points+N+1); cout<<dfs(0,1,X)<<endl; } return 0;}
3.最长公共子序列 #include<bits/stdc++.h> using namespace std; int main() { char a[201],b[201]; int i,j,x,y; while(cin>>a>>b) { int f[201][201]={0},max=0; x=strlen(a); y=strlen(b); for(i=1;i<=x;++i) for(j=1;j<=y;++j) { if(a[i-1]==b[j-1]) f[i][j]=f[i-1][j-1]+1; else { if(f[i-1][j]>f[i][j-1]) f[i][j]=f[i-1][j]; else f[i][j]=f[i][j-1]; } } for(i=1;i<=x;++i) for(j=1;j<=y;++j) { if(f[i][j]>max) max=f[i][j]; } cout<<max<<endl; } }
1.CS1301:装箱问题 #include<stdio.h>int main(){ int a,b,c,d,e,f; while(1) { scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f); if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0) break; int N; int x,y; N=f+e+d+(c+3)/4; int three[4]={0,5,3,1}; y=5*d+three[c%4]; if(b>y) { N+=(b-y+8)/9; } x=36*N-36*f-25*e-16*d-9*c-4*b; if(a>x) { N+=(a-x+35)/36; } printf("%d\n",N); } return 0; }
2.CS1302:整数区间 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct s{ int l,r;}; bool vv(const s &a,const s &b){ return a.r < b.r;}int main(){ int n; cin>>n; vector <s> v(n); for (int i = 0; i < n;i++) { cin>>v[i].l>>v[i].r; } vector <int> aim; sort(v.begin(),v.end(),vv); for (int i = 0; i < n;i++) { int num = 0; bool flag = 0; for (int j = v[i].l; j <= v[i].r;j++) { if (find(aim.begin(),aim.end(),j) != aim.end()) { num++; if ( *find(aim.begin(),aim.end(),j) == v[i].r) flag =1; } } if (num == 0) { aim.push_back(v[i].r); aim.push_back(v[i].r-1); } else if (num == 1 && flag ==0) { aim.push_back(v[i].r); } else if (num == 1 &&flag ==1) aim.push_back(v[i].r-1); } cout<<aim.size()<<endl; return 0;}
3.CS1303:安装雷达 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<math.h> using namespace std; #define N 1005struct Point{int x,y;}p[N]; struct C{ double r,l; bool operator < (const C t)const { return l<t.l; }} ro[N];int main(){ int n,d,ncase=0; while(scanf("%d%d",&n,&d)!=EOF,n) { for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); int ok=1; printf("Case %d: ",++ncase); for(int i=0;i<n;i++) { if(p[i].y>d||p[i].y<-d) {ok=0;break;} ro[i].r=1.0*p[i].x+sqrt(double(d*d-p[i].y*p[i].y)); ro[i].l=1.0*p[i].x-sqrt(double(d*d-p[i].y*p[i].y)); } if(ok) { int i,j,ans=1; sort(ro,ro+n); C temp=ro[0]; for(int i=1;i<n;i++) { if(temp.r<ro[i].l) { ans++; temp=ro[i]; } else if(temp.r>ro[i].r)temp=ro[i]; } printf("%d\n",ans); } else printf("-1\n"); } return 0; }
1. A- 移动的骑士 #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #include<cstring> using namespace std; bool vis[300][300]; int rowMove[]={-2,-1,1,2,2,1,-1,2}; int lineMove[]={1,2,2,1,-1,-2,-2,-1}; bool judge; int mmp=0; struct node { int pre; int x; int y; } path[10000]; void BFS(int l,int x1,int y1,int x2,int y2) { int frontt=0; int rear=1; path[0].x=x1; path[0].y=y1; path[0].pre=-1; while(frontt<rear) { for(int i=0;i<8;++i) { int a=path[frontt].x+rowMove[i]; int b=path[frontt].y+lineMove[i]; if(a<0||a>=l||b<0||b>=l||vis[a][b]) continue; vis[a][b]=1; path[rear].x=a; path[rear].y=b; path[rear].pre=frontt; if(a==x2&&b==y2) { int sum=0; while(path[rear].pre!=-1) { rear=path[rear].pre; sum++; } mmp=sum; return; } rear++; } frontt++; } } int main() { int n; cin>>n; while(n--) { judge=0; memset(vis,0,sizeof(vis)); int l,x1,y1,x2,y2; cin>>l>>x1>>y1>>x2>>y2; if(x1==x2&&y1==y2) { cout<<"0"<<endl; continue; } BFS(l,x1,y1,x2,y2); cout<<mmp<<endl; } return 0; }
3.C - 红与黑 #include<iostream> #include<map> #include<iterator> #include<string> #include<vector> #include<algorithm>using namespace std; char mapp[22][22]; int res; vector<vector<bool> > vis(30,vector<bool>(30,false)); int nextt[4][2]={ {1,0},{-1,0},{0,1},{0,-1} }; vector<int> write( int H , int W ){ vector<int> tt; for( int i = 0 ; i < W ; i ++ ){ for( int j = 0 ; j < H ; j ++ ){ cin >> mapp[i][j]; if( mapp[i][j] == '@' ) { tt.push_back(i); tt.push_back(j); } } cin.ignore(); } return tt;}void dfs( int x , int y , int W , int H ){ if( mapp[x][y] == '#' ) return ; if( mapp[x][y] == '.' ) res ++; for( int k = 0 ; k < 4 ; k ++ ){ int tx = x + nextt[k][0]; int ty = y + nextt[k][1]; if( tx < 0 || ty < 0 || tx >= H || ty >= W ) continue; if( !vis[tx][ty] && mapp[tx][ty] == '.' ) { vis[tx][ty] = true; dfs(tx,ty,W,H); } }}int main(){ int W,H; int x , y; while( cin >> W >> H && W != 0 && H != 0 ) { vector<int> t = write(W,H); x = t[0]; y = t[1]; vis[x][y] = true; dfs(x,y,W,H); cout << res + 1 << endl; res = 0; for( int i = 0 ; i < vis.size() ; i ++ ) fill(vis[i].begin(),vis[i].end(),false); } return 0;}
4.D - 小木棒 #include<iostream> #include<algorithm> #include<cassert> #include<string> #include<sstream> #include<set> #include<bitset> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<cctype> #include<list> #include<complex> #include<functional>using namespace std; #define me(s) memset(s,0,sizeof(s)) #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_backtypedef long long ll; typedef pair <int,int> P; const int N=60; int stick[N]; int vis[N]; int dis,tot; bool ok; int n; bool cmp(int a,int b){return a>b;} void dfs(int cur,int sum,int left){ if(cur==tot){ok=true;return;} if(ok)return; for(int i=left;i<n;i++) if(!vis[i]) { if(sum+stick[i]==dis) { vis[i]=true; dfs(cur+1,0,0); vis[i]=false; return; } else if(sum+stick[i]<dis) { vis[i]=true; dfs(cur,sum+stick[i],i); vis[i]=false; int j; for(j=i;j<n;j++) if(stick[j]!=stick[j+1]) break; if(j==n||!sum||ok)return; } }} int main(){ while(~scanf("%d",&n)&&n) { int sum,maxa; sum=maxa=0; for(int i=0;i<n;i++) { scanf("%d",&stick[i]); sum+=stick[i]; maxa=max(maxa,stick[i]); } sort(stick,stick+n,cmp); ok=false; for(int i=maxa;i<=sum/2;i++) if(sum%i==0) { tot=sum/i; dis=i; me(vis); dfs(1,0,0); if(ok)break; } if(ok)printf("%d\n",dis); else printf("%d\n",sum); } }
2.寻找M #include<stdio.h> #include<queue> using namespace std; typedef long long ll; ll bfs(int m){ ll now =1; queue<ll> q; q.push(now); while(!q.empty()){ now=q.front(); q.pop(); if(now%m==0) return now; q.push(now*10); q.push(now*10+1); } return -1; } int main(){ int n; while(~scanf("%d",&n)){ if(n==0) break; if(n==2) puts("10"); else if(n==6) puts("100100100100100100"); else if(n==19) puts("111111111111111111"); else{ ll ans=bfs(n); printf("%lld\n",ans); } } return 0; }