题目总结:
已完成的题目:
1. Joseph
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
3
4
0
Sample Output
5
30
这道题目用暴力的解法难度不大,但是会TLE,不过这道题的范围很小就1到13,所以我用了本地打表的方法直接记录下了所有的数据,然后直接判断输出。
AC代码
//这段代码是我打表完知道答案后的,后来才知道原来可以在程序里打表完把数据存起来。。。
#include <iostream>
#define endl "\n"
using namespace std;
int n;
int main()
{
while(1)
{
cin>>n;
if(n==0) break;
switch(n){
case 1: cout<<"2"<<endl; break;
case 2: cout<<"7"<<endl; break;
case 3: cout<<"5"<<endl; break;
case 4: cout<<"30"<<endl; break;
case 5: cout<<"169"<<endl; break;
case 6: cout<<"441"<<endl; break;
case 7: cout<<"1872"<<endl; break;
case 8: cout<<"7632"<<endl; break;
case 9: cout<<"1740"<<endl; break;
case 10: cout<<"93313"<<endl; break;
case 11: cout<<"459901"<<endl; break;
case 12: cout<<"1358657"<<endl; break;
case 13: cout<<"2504881"<<endl; break;
}
}
return 0;
}
2. 多项式求和
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。
Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。
Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。
Sample Input
2
1 2
Sample Output
1.00
0.55
这是原本是一道水题,但是我给复杂化了,用了递归导致一直出现小错误 ,最后发现直接用循环解题也能过,这道题也反应出了我对于不同算法的时间复杂度认识不够清晰。
AC代码
#include <iostream>
#define endl "\n"
using namespace std;
int n,a;
int main()
{
cin>>n;
while(n--){
cin>>a;
int i=1,f=1;
double sum=0;
while(i<=a){
sum+=(1.0/i)*f;
i++;
f*=-1;
}
printf("%.2lf\n",sum); // cout控制输出小数位数的代码忘了所以用了printf
}
return 0;
}
需要优化的题目:
1. ABC-364 D-K-th Nearest
Time Limit: 3 sec / Memory Limit: 1024 MB
Score : 425425 points
Problem Statement
There are N+Q points A1,…,AN,B1,…,BQ on a number line, where point Ai has a coordinate aiai and point Bj has a coordinate bjbj.
For each j=1,2,…,Q, answer the following question:
- Let X be the point among A1,A2,…,AN that is the kjkj-th closest to point Bj. Find the distance between points X and Bj. More formally, let didi be the distance between points AiAi and BjBj. Sort (d1,d2,…,dN) in ascending order to get the sequence (d1′,d2′,…,dN′). Find dkj′.
Constraints
- 1≤N,Q≤105
- −108≤ai,bj≤108
- 1≤kj≤N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q a1 a2 …… aN b1 k1 b2 k2 ⋮ bQ kQ
Output
Print Q lines. The ll-th line (1≤l≤Q) should contain the answer to the question for j=l as an integer.
Sample Input 1
4 3
-3 -1 5 6
-2 3
2 1
10 4
Sample Output 1
7
3
13
Sample Input 2
2 2
0 0
0 1
0 2
Sample Output 2
0
0
Sample Input 3
10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7
Sample Output 3
11
66
59
54
88
这道题理解起来不难,就是简单的判断一下a,b两个点之间的距离,然后输出距离b点第k个近的距离是多少。但我还是跟上周一样的老问题TLE了。。。还是之后去看大佬的代码学习一下吧。
TLE代码-勿用
#include <bits/stdc++.h>
using namespace std;
int n,q;
int a[100005];
int b[100005];
int c[100005];
int x;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>q;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<q;i++) cin>>b[i]>>c[i];
for(int j=0;j<q;j++){ //超时的问题应该就在这里了,但是现在还没有改的思路。。。
int d[100005];
for(int i=0;i<n;i++){
x=a[i]-b[j];
if(x<0) x*=-1;
d[i]=x;
}
sort(d,d+n);
cout<<d[c[j]-1]<<endl;
}
return 0;
}
小结:
总的来说,这一周学到了很多新知识,Codeforces完成了对0的突破,AtCode也差不多能写出五题了。但是要学习的东西还是太多了,加油 ! n_n !