ecjtu-summer training #5
A - Hello World!
求最小的粘贴次数,有个坑,小于0结束,以为是等于-1结束,错了几次。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 7 int main(){ 8 int n,k=1; 9 while(scanf("%d",&n)!=EOF){ 10 if(n < 0)break; 11 int ans = 1, i; 12 for(i = 0; ans < n; i ++){ 13 ans = ans*2; 14 } 15 printf("Case %d: %d\n",k++,i); 16 } 17 return 0; 18 }
B - Building designing
排个序就行了
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 const int MAX = 500010; 8 int a[MAX],b[MAX]; 9 bool cmp(int x, int y){ 10 return abs(x)>abs(y); 11 } 12 int main(){ 13 int t,n; 14 scanf("%d",&t); 15 while(t--){ 16 scanf("%d",&n); 17 for(int i = 0; i < n; i ++)scanf("%d",&a[i]); 18 sort(a,a+n,cmp); 19 //for(int i = 0; i < n; i ++)printf("%d ",a[i]); 20 int ans = 1; 21 int flag = (a[0]>0)?1:0; 22 int cnt = a[0]; 23 for(int i = 1; i < n; i ++){ 24 if(flag&&a[i]<0){ 25 ans++;flag=0; 26 }else if(flag==0&&a[i]>0){ 27 ans++;flag=1; 28 } 29 } 30 printf("%d\n",ans); 31 } 32 return 0; 33 }
C - Common Subsequence
最长子串,可以不连续的。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 const int MAX = 10100; 7 char str[MAX],str1[MAX]; 8 int a[MAX][MAX]; 9 10 int main(){ 11 while(scanf("%s %s",str+1,str1+1)!=EOF){ 12 int ans = 0; 13 int i,j; 14 for(i = 1; str[i]; i ++){ 15 for(j = 1; str1[j]; j ++){ 16 if(str[i] == str1[j]) a[i][j] = a[i-1][j-1] + 1; 17 else a[i][j] = max(a[i-1][j],a[i][j-1]); 18 } 19 } 20 i--;j--; 21 while(i > 0 && j > 0){ 22 if(str[i] == str1[j]){ 23 ans++; 24 i--;j--; 25 }else if(a[i-1][j] > a[i][j-1]){ 26 i--; 27 }else{ 28 j--; 29 } 30 } 31 printf("%d\n",ans); 32 memset(str,0,sizeof(str)); 33 memset(str1,0,sizeof(str1)); 34 } 35 return 0; 36 }
D - The Triangle
简单的DP,不过一开始用递归超时了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 const int MAX = 110; 7 int tree[MAX][MAX],n; 8 int dp[MAX][MAX]; 9 int main(){ 10 while(scanf("%d",&n)!=EOF){ 11 memset(tree,0,sizeof(tree)); 12 memset(dp,0,sizeof(dp)); 13 int k = 1; 14 for(int i = 1; i <= n; i ++){ 15 for(int j = 1; j <= i; j ++){ 16 scanf("%d",&tree[i][j]); 17 } 18 } 19 for(int i = n; i > 0; i--){ 20 for(int j = 1; j <= i; j ++){ 21 if(i == n)dp[i][j] = tree[i][j]; 22 else dp[i][j] = max(tree[i][j]+dp[i+1][j],tree[i][j]+dp[i+1][j+1]); 23 } 24 } 25 printf("%d\n",dp[1][1]); 26 } 27 return 0; 28 }
E - Watering Grass
比赛时死活不让我过,可能是精度问题,重写了好几次,好无语。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 const double Inf = 1e-9; 8 const int MAX = 10100; 9 struct Nod{ 10 double ll; 11 double rr; 12 }nod[MAX]; 13 bool cmp(Nod a, Nod b){ 14 return a.ll < b.ll; 15 } 16 int main(){ 17 double n,l,w; 18 double x,r; 19 while(scanf("%lf %lf %lf",&n,&l,&w)!=EOF){ 20 int k = 0; 21 for(int i = 0; i < n; i ++){ 22 scanf("%lf %lf",&x,&r); 23 if(r*2<=w){ 24 continue; 25 }else{ 26 double xx = sqrt(r*r-w*w/4.0); 27 nod[k].ll = x-xx; 28 nod[k].rr = x+xx; 29 k++; 30 } 31 } 32 sort(nod,nod+k,cmp); 33 /* for(int i = 0; i < k; i ++){ 34 printf("%.2lf %.2lf\n",nod[i].ll,nod[i].rr); 35 }*/ 36 int ans = 0, i = 0; 37 double pos = 0,righ=0; 38 if(nod[0].ll <= 0){ 39 while(i < k){ 40 int j = i; 41 //printf("%.2lf %.2lf\n",nod[j].ll,pos); 42 while(j < k && nod[j].ll <= pos){ 43 if(nod[j].rr > righ) 44 righ = nod[j].rr; 45 j++; 46 } 47 if(i == j)break; 48 pos = righ; 49 i=j;ans++; 50 if(pos>=l)break; 51 } 52 } 53 if(pos >= l){ 54 printf("%d\n",ans); 55 }else{ 56 printf("-1\n"); 57 } 58 } 59 return 0; 60 }
F - And Then There Was One
约瑟夫环,做了好几次了,有个规律,看这里。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 7 int main(){ 8 int n,k,m; 9 while(scanf("%d %d %d",&n,&k,&m)!=EOF){ 10 if(n==0)break; 11 int f = 0; 12 for(int i = 2; i <= n; i ++){ 13 f = (f+k)%i; 14 } 15 int ans = (m-k+f+1)%n; 16 if(ans <= 0)ans+=n; 17 printf("%d\n",ans); 18 } 19 return 0; 20 }