I - Space Elevators URAL - 1901 (贪心 简单题)
Nowadays spaceships are never launched from the Earth's surface. There is a huge spaceport placed in the geostationary orbit and connected to the Earth with carbon nanotube cables. People and cargo are delivered to the orbit by elevators moving along these cables. It turned out that the space elevator is much more comfortable and cheaper than a spaceship.
Tomorrow a group of key employees of the “Akross” corporation will go to the spaceport with a secret mission. The spaceport management has reserved a special double elevator for the group. The Head of “Akross” demanded that at any given time the total importance of staff in the elevator must not exceed some fixed value. Under this condition, even in case of fatal accident the corporation will be able to recover. Employees enter the elevator in turns. The elevator is sent up if two people entered, or if only one person entered and the following person behind him is so significant for the corporation that it is impossible to send them together in one elevator.
The spaceport management wants to know the maximum number of elevator runs required to deliver all employees, so the right amount of oxygen cylinders and charged batteries can be prepared in advance.
The first line contains integers
n and
s that are the amount of employees of “Akross” assigned to the mission, and the maximum total importance of two employees which can go together in the elevator (1 ≤
n ≤ 10
5; 1 ≤
s ≤ 10
9). The second line contains integers
v
1, …,
v
n that are the importance of the employees (1 ≤
v
i ≤
s).
In the first line output the maximum amount of trips of the elevator. In the second line output the importance of staff in order from the first employee in the line to the last, for which the elevator will do this amount of trips. If there are several possible answers, output any of them.
input | output |
---|---|
6 6 1 2 3 3 4 5 | 5 2 5 1 3 4 3 |
If there are several possible answers, output any of them. 这个条件没有注意到,然后就错过了一个大水题。。。心痛。。。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string.h>
using namespace std;
const int MAX = 100100;
vector<int> ans, los;
int a[MAX];
int main() {
int n, s, total;
while (cin >> n >> s) {
total = 0;
ans.clear();
los.clear();
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);//从小大到大排序
int left = 0, right = n-1;
while (left < right) {
if (a[left] + a[right] <= s) {
los.push_back(a[left++]);//放置与当前最大值相加不大于s的loser
} else {
ans.push_back(a[left++]);//answer
ans.push_back(a[right--]);
}
}
if (left == right)
los.push_back(a[left]);
sort(los.begin(), los.end());
for (int i = los.size() - 1; i >= 0; --i) {
ans.push_back(los[i]);//把losers从大到小放,因为也有可能,ans中最后一个与loser中最大的组成一个解。
}
for (int i = 0; i < n; ++i) {
if (ans[i] + ans[i+1] <= s) i++;
total++;
}
cout << total << endl;
for (int i = 0; i < n; ++i) {
cout << ans[i] << (i == n-1 ? "\n" : " ");//刚学的,卧槽,还能这么输出2333333
}
}
}