PAT (Basic Level) 1070 结绳

题意

给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度就会减半。
给定 N 段绳子的长度,你需要找出它们能串成的绳子的最大长度。

思路

贪心,从小的开始连。注意最后要取整。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	vector<int> a(n);
	for (int& e : a) cin >> e;
	sort(a.begin(), a.end());
	double ans = a[0];
	for (int i = 1; i < n; ++i)
		ans = ans / 2 + a[i] / 2.0;
	cout << static_cast<int>(ans) << '\n';
	return 0;
} 

HINT

不定时更新更多题解,Basic Level 全部AC代码,详见 link ! ! !

发布了92 篇原创文章 · 获赞 16 · 访问量 3768

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104639221