[关闭]
@lawk97 2016-12-11T17:57:09.000000Z 字数 7934 阅读 899

集训队第二次作业

作业


题目列表

A - Game with Pearls

[HDU - 5090]


Tom and Jerry are playing a game with tubes and pearls. The rule of
the game is:

1) Tom and Jerry come up together with a number K.

2) Tom provides N tubes. Within each tube, there are several pearls.
The number of pearls in each tube is at least 1 and at most N.

3) Jerry puts some more pearls into each tube. The number of pearls
put into each tube has to be either 0 or a positive multiple of K.
After that Jerry organizes these tubes in the order that the first
tube has exact one pearl, the 2nd tube has exact 2 pearls, …, the Nth
tube has exact N pearls.

4) If Jerry succeeds, he wins the game, otherwise Tom wins.

Write a program to determine who wins the game according to a given N,
K and initial number of pearls in each tube. If Tom wins the game,
output “Tom”, otherwise, output “Jerry”.

Input

The first line contains an integer M (M<=500), then M games follow.
For each game, the first line contains 2 integers, N and K (1 <= N <=
100, 1 <= K <= N), and the second line contains N integers presenting
the number of pearls in each tube.

Output

For each game, output a line containing either “Tom” or “Jerry”.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int m;
  8. int main()
  9. {
  10. scanf("%d",&m);
  11. while(m--)
  12. {
  13. int n,k,flag=1;
  14. scanf("%d%d",&n,&k);
  15. int a[105],v[105];
  16. for(int i=0;i<n;i++)
  17. scanf("%d",&a[i]);
  18. memset(v, 0, sizeof(v));
  19. sort(a, a+n);
  20. for(int i=0;i<n;i++)
  21. {
  22. int x=a[i];
  23. flag=1;
  24. while(x<=n)
  25. {
  26. if(v[x]==0)
  27. {
  28. v[x]=1;
  29. flag=0;
  30. break;
  31. }
  32. x+=k;
  33. }
  34. if(flag)
  35. break;
  36. }
  37. if(flag)
  38. printf("Tom\n");
  39. else
  40. printf("Jerry\n");
  41. }
  42. return 0;
  43. }

C - Seam Carving

[HDU - 5092]


Fish likes to take photo with his friends. Several days ago, he found
that some pictures of him were damaged. The trouble is that there are
some seams across the pictures. So he tried to repair these pictures.

He scanned these pictures and stored them in his computer. He knew it
is an effective way to carve the seams of the images He only knew that
there is optical energy in every pixel.

He learns the following principle of seam carving.
Here seam carving refers to delete through
horizontal or vertical line of pixels across the whole image to
achieve image scaling effect. In order to maintain the characteristics
of the image pixels to delete the importance of the image lines must
be weakest. The importance of the pixel lines is determined in
accordance with the type of scene images of different energy content.
That is, the place with the more energy and the richer texture of the
image should be retained. So the horizontal and vertical lines having
the lowest energy are the object of inspection. By constantly deleting
the low-energy line it can repair the image as the original scene.

For an original image G of m*n, where m and n are the row and column
of the image respectively. Fish obtained the corresponding energy
matrix A. He knew every time a seam with the lowest energy should be
carved. That is, the line with the lowest sum of energy passing
through the pixels along the line, which is a 8-connected path
vertically or horizontally.

Here your task is to carve a pixel from the first row to the final row
along the seam. We call such seam a vertical seam.

Input

There several test cases. The first line of the input is an integer T,
which is the number of test cases, 0 integers m, n, which are the row and column of the energy matrix of an
image, (0

Output

For each test case, print “Case #” on the first line, where # is the
order number of the test case (starting with 1). Then print the column
numbers of the energy matrix from the top to the bottom on the second
line. If there are more than one such seams, just print the column
number of the rightmost seam.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int t;
  8. struct visi
  9. {
  10. int sum,last;
  11. };
  12. int main()
  13. {
  14. scanf("%d",&t);
  15. for(int k=1;k<=t;k++)
  16. {
  17. int m,n,map[105][105];
  18. visi v[105][105];
  19. scanf("%d%d",&m,&n);
  20. for(int i=0;i<m;i++)
  21. for(int j=0;j<n;j++)
  22. scanf("%d",&map[i][j]);
  23. for(int i=0;i<m;i++)
  24. {
  25. for(int j=0;j<n;j++)
  26. {
  27. int pos=j+1;
  28. v[i][j].sum=map[i][j];
  29. if(i==0)
  30. continue;
  31. if(j+1>=n)
  32. pos=j;
  33. else if (v[i-1][j].sum<v[i-1][j+1].sum)
  34. pos=j;
  35. if(j-1>=0&&v[i-1][j-1].sum<v[i-1][pos].sum)
  36. pos=j-1;
  37. v[i][j].sum+=v[i-1][pos].sum;
  38. v[i][j].last=pos;
  39. }
  40. }
  41. int l=n-1;
  42. for(int i=n-2;i>=0;i--)
  43. {
  44. if(v[m-1][i].sum<v[m-1][l].sum)
  45. l=i;
  46. }
  47. int ans[105],step=0;
  48. ans[step++]=l+1;
  49. while(step<m)
  50. {
  51. l=v[m-step][l].last;
  52. ans[step++]=l+1;
  53. }
  54. printf("Case %d\n",k);
  55. for(int i=m-1;i>=0;i--)
  56. {
  57. if(i<m-1)
  58. printf(" ");
  59. printf("%d",ans[i]);
  60. }
  61. printf("\n");
  62. }
  63. return 0;
  64. }

F - Linearization of the kernel functions in SVM

[HDU - 5095]


SVM(Support Vector Machine)is an important classification tool, which
has a wide range of applications in cluster analysis, community
division and so on. SVM The kernel functions used in SVM have many
forms. Here we only discuss the function of the form f(x,y,z) = ax^2 +
by^2 + cy^2 + dxy + eyz + fzx + gx + hy + iz + j. By introducing new
variables p, q, r, u, v, w, the linearization of the function f(x,y,z)
is realized by setting the correspondence x^2 <-> p, y^2 <-> q, z^2
<-> r, xy <-> u, yz <-> v, zx <-> w and the function f(x,y,z) = ax^2 +
by^2 + cy^2 + dxy + eyz + fzx + gx + hy + iz + j can be written as
g(p,q,r,u,v,w,x,y,z) = ap + bq + cr + du + ev + fw + gx + hy + iz + j,
which is a linear function with 9 variables.

Now your task is to write a program to change f into g.

Input

The input of the first line is an integer T, which is the number of
test data (T<120). Then T data follows. For each data, there are 10
integer numbers on one line, which are the coefficients and constant
a, b, c, d, e, f, g, h, i, j of the function f(x,y,z) = ax^2 + by^2 +
cy^2 + dxy + eyz + fzx + gx + hy + iz + j.

Output

For each input function, print its correspondent linear function with
9 variables in conventional way on one line.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int t;
  8. char h[]={'p','q','r','u','v','w','x','y','z'};
  9. int main()
  10. {
  11. scanf("%d",&t);
  12. while(t--)
  13. {
  14. int flag=0,x;
  15. for(int i=0;i<9;i++)
  16. {
  17. scanf("%d",&x);
  18. if(x>1)
  19. {
  20. if(flag)
  21. printf("+");
  22. printf("%d%c",x,h[i]);
  23. flag=1;
  24. }
  25. else if(x==1)
  26. {
  27. if(flag)
  28. printf("+");
  29. printf("%c",h[i]);
  30. flag=1;
  31. }
  32. else if(x==-1)
  33. {
  34. printf("-%c",h[i]);
  35. flag=1;
  36. }
  37. else if(x<0)
  38. {
  39. printf("%d%c",x,h[i]);
  40. flag=1;
  41. }
  42. }
  43. scanf("%d",&x);
  44. if(x>0)
  45. {
  46. if(flag)
  47. printf("+");
  48. printf("%d",x);
  49. }
  50. else if (x==0&&!flag)
  51. printf("%d",x);
  52. else if(x<0)
  53. printf("%d",x);
  54. printf("\n");
  55. }
  56. return 0;
  57. }

P - Friendship of Frog

[HDU - 5578]


NN frogs from different countries are standing in a line. Each
country is represented by a lowercase letter. The distance between
adjacent frogs (e.g. the 1st1st and the 2nd2nd frog, the N−1thN−1th
and the NthNth frog, etc) are exactly 11. Two frogs are friends if
they come from the same country.

The closest friends are a pair of friends with the minimum distance.
Help us find that distance.

Input

First line contains an integer TT, which indicates the number of test
cases.

Every test case only contains a string with length NN, and the ithith
character of the string indicates the country of ithith frogs.

⋅⋅ 1≤T≤50.

⋅⋅ for 80% data, 1≤N≤100.

⋅⋅ for 100% data, 1≤N≤1000.

⋅⋅ the string only contains lowercase letters.

Output

For every test case, you should output " Case #x: y", where xx
indicates the case number and counts from 11 and yy is the result. If
there are no frogs in same country, output −1−1 instead.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int t;
  8. int main()
  9. {
  10. scanf("%d",&t);
  11. for (int i=1; i<=t; i++)
  12. {
  13. char a[1005];
  14. int p[30];
  15. int len,ans,flag=0;
  16. scanf("%s",a);
  17. len=(int)strlen(a)-1;
  18. ans=len;
  19. memset(p, -1, sizeof(p));
  20. for(int i=0;i<len;i++)
  21. {
  22. if(p[a[i]-'a']!=-1)
  23. {
  24. flag=1;
  25. if(i-p[a[i]-'a']<ans)
  26. ans=i-p[a[i]-'a'];
  27. }
  28. p[a[i]-'a']=i;
  29. }
  30. printf("Case #%d: ",i);
  31. if(!flag)
  32. printf("-1\n");
  33. else
  34. printf("%d\n",ans);
  35. }
  36. return 0;
  37. }

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注