云计算百科
云计算领域专业知识百科平台

Educational Codeforces Round 187 (Rated for Div. 2) A+B

A. Towers of Boxes

B. Beautiful Numbers

目录

A. Towers of Boxes

题意

思路

正解代码

B. Beautiful Numbers

题意

思路

正解代码


A. Towers of Boxes

题意

题目的意思大致是:给你 n 个箱子,每个箱子的重量是 m ,每个箱子的承重是 d 。现在要将这些箱子像叠罗汉一样堆成 x 堆,问最小的 x 是多少。

思路

只考虑每一堆最底下的箱子,尽可能往上面叠箱子,这样最终的堆数是最少的。

正解代码

#include<bits/stdc++.h>

using namespace std;

int T;

inline void solve(){
int n,m,d;
cin>>n>>m>>d;
int each = d/m;
int ans = n/(each+1);
if(n%(each+1) != 0) ans++;
cout<<ans<<endl;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T–) solve();
return 0;
}

B. Beautiful Numbers

题意

这道题的意思大致是:给你一个函数 F(x) ,这个函数的值是 x 的各位数字之和。现在给你一个 x ,每一次操作你可以选择其任意一位,改变这一位的数(最大的那位不能变成 0 ),问使 F(F(x)) = F(x) 的最小操作次数是多少。

思路

由于 x 的范围较大(1<=x<=1e18),所以我们考虑 F(x) 的范围,惊讶地发现 0<=F(x)<=162 ,于是我们枚举 F(x) ,找到 F(F(x)) = F(x) 的 F(x) 的值。再次惊喜地发现只有 0 ~ 9 的 F(x) 能够满足条件。所以,我们的目标成为了使用最小的操作次数使 F(x) < 10。

于是,我们可以把 x 的每一位数提取出来,从大到小排序,每一次都将最大的数变成 0 ,直到所有位的数之和小于 10 。这里需要注意的是最高位的数是不能变成 0 的。

正解代码

#include<bits/stdc++.h>
#define int long long

using namespace std;

int T;

inline int F(int x){
int res = 0;
while(x){
res += x%10;
x /= 10;
}
return res;
}

inline void solve(){
int x;
cin>>x;
int sum = 0;
vector<int> v;
while(x >= 10){
v.push_back(x%10);
sum += x%10;
x /= 10;
}
v.push_back(x-1);
sum += x;
x = 0;
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
int cnt = 0;
for(int i:v){
if(sum<10) break;
sum -= i;
cnt++;
}
cout<<cnt<<'\\n';
}

signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T–) solve();
return 0;
}

以上就是这篇文章的全部内容了。

赞(0)
未经允许不得转载:网硕互联帮助中心 » Educational Codeforces Round 187 (Rated for Div. 2) A+B
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!