Codeforces Round #287 (Div. 2)

做了前三道,感觉这次的前三道不是很难。

507A - Amr and Music:

学乐器,给出乐器数、天数和每个乐器需要的天数。求能学会哪几个。

结构体排序,输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct course{
int a,b;
course(int a=0,int b=0):a(a),b(b){}
bool operator < (const course& x) const {
return b<x.b;
}
};
vector<course> a;
vector<int> b;
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;++i){
int t;
scanf("%d",&t);
a.push_back(course(i,t));
}
sort(a.begin(),a.end());
int p=0;
while(1){
if(a[p].b<=k){
k-=a[p].b;
b.push_back(a[p].a+1);
++p;
}
else break;
if(p==n) break;
}
printf("%d\n",(int)b.size());
for(int i=0;i<b.size();++i){
if(i) printf(" ");
printf("%d",b[i]);
}
return 0;
}

507B - Amr and Pins:

输入圆的半径初始位置、目标位置。求转移需要的最少的次数。

很容易想思路,一次最长能转移的距离等于直径,求位置之间的距离。然后除以直径向上取整。

1
2
3
4
5
6
7
8
9
#include<cstdio>
#include<cmath>
int main(){
double r,x,y,ax,ay,l;
scanf("%lf%lf%lf%lf%lf",&r,&x,&y,&ax,&ay);
l=sqrt((x-ax)*(x-ax)+(y-ay)*(y-ay));
printf("%d",(int)ceil(l/r/2));
return 0;
}

507C - Guess Your Way Out!:

给出完全二叉树的深度,和目标叶子的编号。求在LRLRLR规则下,到目标叶片之前会经过多少叶子。

每走一层就判断是加整个子树再加一还是只加一,因为如果目标方向与当前左右方向不一致,必定要遍历另一棵子树,相同则只需走一个。

同时让n等于在当前子树中的编号。走到第h层就停止。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main(){
int h,cur=0;
long long n,k=0,x=1;
cin>>h>>n;
bool l=true;
while(cur!=h){
if(n>(x<<(h-cur-1))){
n-=x<<(h-cur-1);
if(l) k+=x<<(h-cur);
else{++k;l=!l;}
}
else{
if(l){++k;l=!l;}
else k+=x<<(h-cur);
}
++cur;
}
cout<<k;
return 0;
}

** 本文迁移自我的CSDN博客,格式可能有所偏差。 **