前言:就剩一场了

啊,今天下午这场,2点半才睡醒,爬起来开始写题。然后经历了最舒服的前1个小时和最自闭的后2个半小时。

A、模板

这个题不难,可以定义在签到题的位置上。但是!我一上场,一看题干,就和我之前做过的一道题重合了。都是对字符串进行编辑来进行变化。

编辑距离

这两个题长得太像了,前者就是个普通的计数,后者就得dp。完全是两个概念的。刚一看到题,我还想今天说要降难度,咋一上来就整个这。

  1. import java.util.*;
  2. class q{
  3. q(){
  4. Scanner scanner=new Scanner(System.in);
  5. int n=scanner.nextInt();
  6. int m=scanner.nextInt();
  7. char[] s1=scanner.next().toCharArray();
  8. char[] s2=scanner.next().toCharArray();
  9. int times=Math.abs(n-m);
  10. int k=Math.min(n,m);
  11. for(int i=0;i<k;i++){
  12. if(s1[i]!=s2[i])times++;
  13. }
  14. System.out.println(times);
  15. }
  16. }
  17. public class Main { public static void main(String[] args) {new q(); }}

B、牛牛战队的比赛地

一开始没理解这个题啥意思,后来知道他的意思是取能令其考试地点到其所有培训基地最小的横坐标。

有点意思。

  1. import java.util.*;
  2. class q {
  3. private double[][] k;
  4. private int n;
  5. private double length(double x){
  6. double max=0.0;
  7. for(int i=0;i<n;i++)max=Math.max(max,k[i][0]-x)*(k[i][0]-x)+k[i][1]*k[i][1]);
  8. return max;
  9. }
  10. q() {
  11. Scanner scanner=new Scanner(System.in);
  12. n=scanner.nextInt();
  13. k=new double[n][2];
  14. double left=999999.0;
  15. double right=-999999.0;
  16. for(int i=0;i<n;i++){
  17. k[i][0]=scanner.nextDouble();
  18. k[i][1]=scanner.nextDouble();
  19. if(left>k[i][0])left=k[i][0];
  20. if(right<k[i][0])right=k[i][0];
  21. }
  22. double midl,midr;
  23. for(int i=0;i<100;i++){
  24. midl=(left+left+right)/3;
  25. midr=(left+right+right)/3;
  26. if(length(midl)>length(midr)) left=midl;
  27. else right=midr;
  28. }
  29. System.out.println(Math.pow(length(left),0.5));
  30. }
  31. }
  32. public class Main {public static void main(String[] args) {new q();}}

C、C语言IDE

这道题的意思很好想,理解起来不难。就是代码奇丑无比。这里我贴上官方题解。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string source;
  4. void replaceAll(string &s, string oldstr, string newstr)
  5. {
  6. for (string::size_type pos = 0; pos != string::npos; pos += newstr.length())
  7. if ((pos = s.find(oldstr, pos)) != string::npos) s.replace(pos, oldstr.length(), newstr);
  8. else break;
  9. }
  10. struct functions{
  11. string inClass, name, outputType;
  12. vector<string> inputType;
  13. functions(string inClass = "", string name = "", string outputType = "void", vector<string> inputType = vector<string>(0))
  14. :inClass(inClass), name(name), outputType(outputType), inputType(inputType) {}
  15. };
  16. vector<functions> funs;
  17. void solve(string &s)
  18. {
  19. replaceAll(s, "/*", " /* ");
  20. replaceAll(s, "*/", " */ ");
  21. replaceAll(s, "//", " // ");
  22. replaceAll(s, "(", " ( ");
  23. replaceAll(s, ")", " ) ");
  24. replaceAll(s, "{", " { ");
  25. replaceAll(s, "}", " } ");
  26. replaceAll(s, "=", " = ");
  27. replaceAll(s, "\"", " \" ");
  28. replaceAll(s, "'", " ' ");
  29. replaceAll(s, ";", " ; ");
  30. replaceAll(s, ",", " , ");
  31. replaceAll(s, "+ = ", "+=");
  32. replaceAll(s, "- = ", "-=");
  33. replaceAll(s, "* = ", "*=");
  34. replaceAll(s, "/ = ", "/=");
  35. replaceAll(s, "^ = ", "^=");
  36. replaceAll(s, "| = ", "|=");
  37. replaceAll(s, "& = ", "&=");
  38. replaceAll(s, ":", " : ");
  39. replaceAll(s, " : : ", "::");
  40. vector<string> tokens; string now = "";
  41. for (int i = 0; s[i]; i++)
  42. {
  43. if (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n' || s[i] == '\0')
  44. {
  45. if (now != "")
  46. {
  47. if (now == ":" && tokens.back() == ")")
  48. {
  49. string tmpnow = "";
  50. for (int j = i + 1; s[j]; j++)
  51. {
  52. if (s[j] == ' ' || s[j] == '\t' || s[j] == '\r' || s[j] == '\n' || s[j] == '\0')
  53. {
  54. if (tmpnow == "{")
  55. {
  56. now = "{";
  57. i = j - 1;
  58. break;
  59. }
  60. tmpnow = "";
  61. }
  62. else tmpnow += s[j];
  63. }
  64. continue;
  65. }
  66. if (now == "const")
  67. {
  68. now = "";
  69. continue;
  70. }
  71. if (now == "//")
  72. {
  73. for (int j = i; s[j]; j++)
  74. {
  75. if (s[j] == '\n')
  76. {
  77. i = j - 1;
  78. break;
  79. }
  80. }
  81. now = "";
  82. continue;
  83. }
  84. if (now == "/*")
  85. {
  86. int num = 1;
  87. string tmpnow = "";
  88. for (int j = i + 1; s[j]; j++)
  89. {
  90. if (s[j] == ' ' || s[j] == '\t' || s[j] == '\r' || s[j] == '\n' || s[j] == '\0')
  91. {
  92. if (tmpnow == "/*") num++;
  93. if (tmpnow == "*/")
  94. {
  95. num--;
  96. if (num == 0)
  97. {
  98. i = j - 1;
  99. break;
  100. }
  101. }
  102. tmpnow = "";
  103. }
  104. else tmpnow += s[j];
  105. }
  106. now = "";
  107. continue;
  108. }
  109. //cout << now << s[i];
  110. tokens.push_back(now);
  111. now = "";
  112. }
  113. //else cout << s[i];
  114. }
  115. else now += s[i];
  116. }
  117. int cnt = 0;
  118. string nowNamespace = "";
  119. for (int i = 1; i < (int)tokens.size(); i++)
  120. {
  121. if ((tokens[i] == "struct" || tokens[i] == "class") && tokens[i + 2] == "{")
  122. {
  123. cnt = 0;
  124. nowNamespace = tokens[i + 1];
  125. i += 2;
  126. }
  127. functions tmp(nowNamespace);
  128. if (tokens[i] == "{" && tokens[i - 1] == ")")
  129. {
  130. int num = 1;
  131. for (int j = i - 2; j >= 0; j--)
  132. {
  133. if (tokens[j] == ")") num++;
  134. if (tokens[j] == "(")
  135. {
  136. num--;
  137. if (num == 0)
  138. {
  139. tmp.name = tokens[j - 1];
  140. tmp.outputType = "";
  141. for (int k = j - 2; k >= 0; k--)
  142. if (tokens[k] != "}" && tokens[k] != "}" && tokens[k] != ";" &&
  143. tokens[k].back() != ':' && tokens[k] != "inline" &&
  144. tokens[k] != "static" && tokens[k][0] != '#' &&
  145. tokens[k].back() != '\"' && tokens[k].back() != '>')
  146. tmp.outputType = tmp.outputType == "" ? tokens[k] : tokens[k] + " " + tmp.outputType;
  147. else break;
  148. int last = i - 2;
  149. for (int k = i - 2; k >= j; k--)
  150. {
  151. if (tokens[k] == "(" || tokens[k] == ",")
  152. {
  153. string tt = "";
  154. for (int t = k + 1; t < last; t++)
  155. tt = tt == "" ? tokens[t] : tt + " " + tokens[t];
  156. if (tt != "") tmp.inputType.push_back(tt);
  157. last = k - 1;
  158. }
  159. if (tokens[k] == "=" || tokens[k] == ")") last = k - 1;
  160. }
  161. reverse(tmp.inputType.begin(), tmp.inputType.end());
  162. break;
  163. }
  164. }
  165. }
  166. funs.push_back(tmp);
  167. num = 1;
  168. for (int j = i + 1; j < (int)tokens.size(); j++)
  169. {
  170. if (tokens[j] == "{") num++;
  171. if (tokens[j] == "}")
  172. {
  173. num--;
  174. if (num == 0)
  175. {
  176. i = j;
  177. //cout << tmp.outputType << " " << tmp.name << " ";
  178. //cout << j << endl;
  179. break;
  180. }
  181. }
  182. }
  183. continue;
  184. }
  185. if (nowNamespace != "")
  186. {
  187. //cout << i << " " << nowNamespace << " " << cnt << endl;
  188. if (tokens[i] == "{") cnt++;
  189. if (tokens[i] == "}")
  190. {
  191. cnt--;
  192. if (!cnt) nowNamespace = "";
  193. }
  194. }
  195. }
  196. }
  197. int main()
  198. {
  199. char ch;
  200. while ((ch = getchar()) != EOF)
  201. source += ch;
  202. solve(source);
  203. for (auto & i: funs)
  204. {
  205. if (i.outputType != "") cout << i.outputType << " ";
  206. if (i.inClass != "") cout << i.inClass << "::";
  207. cout << i.name << "(";
  208. for (int j = 0; j < (int)i.inputType.size(); j++)
  209. cout << i.inputType[j] << (j == (int)i.inputType.size() - 1 ? ")" : ",");
  210. if ((int)i.inputType.size() == 0) cout << ")";
  211. cout << endl;
  212. }
  213. return 0;
  214. }

D、牛牛与牛妹的约会

嘤嘤嘤,题目跟我秀恩爱可还行。一道典型的贪心题目。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(void)
  4. {
  5. int T; cin>>T;
  6. while(T--){
  7. int a, b; scanf("%d%d", &a, &b);
  8. double ans = 0;
  9. double ca = a, cb = b;
  10. double p = 1.0/3.0;
  11. while(1){
  12. double na;
  13. if(ca < 0) na = -pow(-ca,p);
  14. else na = pow(ca, p);
  15. if(abs(na-cb)+1.0 < abs(ca-cb)) ans += 1.0, ca = na;
  16. else {
  17. ans += abs(ca-cb); break;
  18. }
  19. }
  20. printf("%.9f\n", ans);
  21. }
  22. return 0;
  23. }

E、Enjoy the game

又是一道水题。

这个题看上去挺别扭,其实就是个判断是不是二的倍数。具体流程如下:

这里假定一共 2020牛客寒假算法基础集训营5 - 图1 张牌
1、看看 2020牛客寒假算法基础集训营5 - 图2 是不是奇数,如果是,那么先手必胜。
2、若 2020牛客寒假算法基础集训营5 - 图3 为偶数,那么此时先手应当出偶数个,不然轮到对方时,就成了对方的奇数先手。
3、同理,对方也应当出偶数。这样的话看来,若我们假定双方出的偶数都为2,那么当2020牛客寒假算法基础集训营5 - 图4为奇数时,先手必胜。
4,把2020牛客寒假算法基础集训营5 - 图5,然后在回到步骤2,直到最后可以获得一个不为1的奇数时,可以判断先手必胜,若为1,那么先手必输。

至于为什么当最后为1时就不行…….因为这个数你如果为1的话,从你计算的时候的意义上来看,是你必须在第一次时就拿走所有的牌你才能赢。但这很明显不合题意,所以不行。

  1. import java.util.*;
  2. class q{
  3. private boolean ok(long k){
  4. if(k==1)return false;
  5. if(k%2==1)return true;
  6. else return ok(k/2);
  7. }
  8. q(){
  9. Scanner scanner=new Scanner(System.in);
  10. long k=scanner.nextLong();
  11. System.out.println(ok(k)?"Bob":"Alice");
  12. }
  13. }
  14. public class Main { public static void main(String[] args) {new q(); }}

F、碎碎念

G、街机争霸

H、Hash

I、I题是个签到题

啊,顾名思义,这的确是个签到题。

说白了就是在读取的时候,实时维护整个数据里头前三大的数。然后在最后再判定一下是不是人数大于所有人的2020牛客寒假算法基础集训营5 - 图6。没了,就这,贼水。

  1. import java.util.*;
  2. class q {
  3. private ArrayList<Integer> k = new ArrayList<>(3);
  4. private int where(int qqq) {
  5. int i=k.size();
  6. if(i==0||k.get(0)<=qqq)return 0;
  7. if(i==1||k.get(1)<=qqq)return 1;
  8. if(i==2||k.get(2)<=qqq)return 2;
  9. else return 3;
  10. }
  11. q() {
  12. Scanner scanner = new Scanner(System.in);
  13. int n = scanner.nextInt();
  14. double m = scanner.nextInt();
  15. int kkk = 0;
  16. boolean ok=false;
  17. for (int i = 0; i < n; i++) {
  18. int o = scanner.nextInt();
  19. if(i==8)kkk=o;
  20. int s=where(o);
  21. if(s<3){
  22. if(k.size()==3)k.remove(2);
  23. k.add(s,o);
  24. }
  25. }
  26. double ppp=kkk/m;
  27. if(k.indexOf(kkk)!=-1)ok=true;
  28. if(ppp>=0.8)ok=true;
  29. System.out.println(ok?"Yes":"No");
  30. }
  31. }
  32. public class Main {public static void main(String[] args) {new q();}}

J、牛牛战队的秀场

这是个数学题…..上过初中的就会……

根据给的2020牛客寒假算法基础集训营5 - 图7,先选出来劣弧是哪一段,然后再得到我们要求的长度应为2020牛客寒假算法基础集训营5 - 图8。然后写式子就可以了。

  1. import java.util.*;
  2. class q{
  3. q(){
  4. Scanner scanner=new Scanner(System.in);
  5. double n=scanner.nextInt();
  6. double r=scanner.nextInt();
  7. int i=scanner.nextInt();
  8. int j=scanner.nextInt();
  9. double l=2* r *Math.sin(Math.PI/n);
  10. if(i>j){
  11. if(j+n-i>i-j) System.out.printf("%.6f",(i-j)*l);
  12. else System.out.printf("%.6f",(j+n-i)*l);
  13. }else {
  14. if(i+n-j>j-i) System.out.printf("%.6f",(j-i)*l);
  15. else System.out.printf("%.6f",(i+n-j)*l);
  16. }
  17. }
  18. }
  19. public class Main { public static void main(String[] args) {new q(); }}