CODECHEF(比赛)——TSEC January Codeathon
最终成绩: 300/600
第一题
The Speed Limit
All submissions for this problem are available.Chef is travelling to the nearest cricket stadium to watch his favourite team play. Unfortunately for him, he is running late. The road to the stadium has a speed limit L km/h. If Chef is D kms away from the stadium, find whether he can reach the stadium in H hours, if he cannot travel faster than the speed limit.
Input:
First line of input contains the number of test cases T. Then the test cases follow.
Each test case consists of three space separated integers L, D and H denoting the speed limit in km/h, the distance and the number of hours in which Chef has to reach the stadium.
Output:
For each test case, output in a single line, the word ‘Yes’ if Chef can reach on time, and the word ‘No’ if he cannot.
Constraints:
1≤T≤5
1≤L≤300
1≤D≤7000
1≤H≤24
Sample Input:
2
100 250 2
72 123 3
Sample Output:
No
Yes
题目大意:
已知时间,速度,求是否能赶上棒球比赛。
题目解析:
超级签到题。
具体代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,l,d,h;
cin>>t;
while(t--){
cin>>l>>d>>h;
if(l*h>=d)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
第二题
The Prime Count
All submissions for this problem are available.Consider a generalized form of Fibonacci sequence where the first two terms are defined arbitrarily. Your task is to find total number of primes in the first 20 terms of the sequence, given the first two terms F1 and F2
Input:
First line of input contains the number of test cases T. Then the test cases follow.
Each test case consists of two space separated integers F1 and F2, which are the first two terms of the sequence.
Output:
For each test case, output in a single line the number of primes in the first 20 terms of the sequence.
Constraints:
1≤T≤2∗105
2≤F1,F2≤100
Sample Input:
1
100 37
Sample Output:
4
题目大意:
给出斐波那契数列的前两项,求前20项中的素数个数。
题目解析:
这题要判断的数很多,要用线性筛素数。
具体代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int N = 700000;
bool isprime[N+1];
int prime[N+1];
void init() {
memset(isprime,true,sizeof(isprime));
int cnt=0;
isprime[2]=true;
for (int i=2; i<=N; i++) {
if (isprime[i]) prime[cnt++]=i;
for (int j=0; j<cnt&&i*prime[j]<=N; j++) {
isprime[i*prime[j]]=false;
if (i%prime[j]==0) break;
}
}
}
int main() {
int t;
ll f1,f2;
scanf("%d",&t);
init();
while(t--) {
scanf("%I64d%I64d",&f1,&f2);
ll t;
int count=0;
if(isprime[f1])
count++;
if(isprime[f2])
count++;
for(int i=3; i<=20; i++) {
t=f1+f2;
if(isprime[t]==true) {
count++;
}
f1=f2;
f2=t;
}
printf("%d\n",count);
}
return 0;
}
第三题
Job Hiring
All submissions for this problem are available.Given a list of N people, you are to choose M of them in the following manner:
You have to choose people in M iterations and in each iteration you are to choose a person from beginning or end of list. Then you will delete that person from the list and continue the same process for M iterations.
You will be given the name as a single string and age as an integer of every person.
The people must be chosen in the following manner:
Candidate with less age will be preferred
If age of two candidates is same, then person having lexicographically smallest name will be preferred
If both name and age are same, then candidate with lesser index in list will be chosen After choosing the required candidates, you are to again order them using the above comparision rules, and then display their indexes in the original list.
Indexing in original list is from 0,1,…N−1.
See example for more details.
Input:
First line of input contains the number of test cases T. Then the test cases follow.
Each test case consists of two space separated integers denoting N and M respectively, then N lines follow each with the description of a person as follows:
Each line contains of two values, first of string S which denotes the name of the person and integer A denoting the age of the person
Output:
For each test case, output the required answer in a single line.
See examples for more details.
Constraints:
1≤T≤105
1≤M≤N≤105
Sum of all N in a single file≤105
1≤|Name of a person|≤5
20≤Age of a person≤100
Name will always be in lowercase
题目大意:
招聘会上,给出n个待选人名单(包括姓名,年龄),录取m个人,每次从名单首部或者尾部选一个符合要求的人(选年龄小的,年龄相同选姓名字典序小的,姓名再相同选index小的)。选出来的m个人再按照这个规则从小到大排序,输出index。
题目解析:
结构体保存候选人信息(name,age,index),重载运算符用来判断大小,用vector v保存选中的候选人,最后排序输出index值。
具体代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
struct node {
int index;
char name[55];
int age;
friend bool operator <( const node &a,const node &b)
{
if(a.age!=b.age)
return a.age<b.age;
else if(strcmp(a.name,b.name))
return strcmp(a.name,b.name)<0;
else
return a.index<b.index;
}
} A[100100];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n,m;
vector<node> v;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) {
getchar();
scanf("%s %d",&A[i].name,&A[i].age);
A[i].index=i;
}
int begin=0,end=n-1;
while(m--) {
if(A[begin]<A[end]) {
v.push_back(A[begin]);
begin++;
} else {
v.push_back(A[end]);
end--;
}
}
sort(v.begin(),v.end());
for(int i=0; i<v.size(); i++) {
printf("%d",v[i].index);
if(i!=v.size()-1)
printf(" ");
else
printf("\n");
}
}
return 0;
}
感受:其实前面三题都挺简单的,只是在做第二题的时候,在网上找了一些素数筛法的代码,却都不能用,浪费了不少时间。在做第三题的时候,在输出候选人index值的时候,本来应该输出v,粗心写成了输出A,花了好久用来debug,第三题还剩45s的时候才找出来这个错误,提交通过。后面三题就没时间看了。